@malloydata/malloy-tests 0.0.300 → 0.0.302

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -21,14 +21,14 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@jest/globals": "^29.4.3",
24
- "@malloydata/db-bigquery": "0.0.300",
25
- "@malloydata/db-duckdb": "0.0.300",
26
- "@malloydata/db-postgres": "0.0.300",
27
- "@malloydata/db-snowflake": "0.0.300",
28
- "@malloydata/db-trino": "0.0.300",
29
- "@malloydata/malloy": "0.0.300",
30
- "@malloydata/malloy-tag": "0.0.300",
31
- "@malloydata/render": "0.0.300",
24
+ "@malloydata/db-bigquery": "0.0.302",
25
+ "@malloydata/db-duckdb": "0.0.302",
26
+ "@malloydata/db-postgres": "0.0.302",
27
+ "@malloydata/db-snowflake": "0.0.302",
28
+ "@malloydata/db-trino": "0.0.302",
29
+ "@malloydata/malloy": "0.0.302",
30
+ "@malloydata/malloy-tag": "0.0.302",
31
+ "@malloydata/render": "0.0.302",
32
32
  "events": "^3.3.0",
33
33
  "jsdom": "^22.1.0",
34
34
  "luxon": "^2.4.0",
@@ -38,5 +38,5 @@
38
38
  "@types/jsdom": "^21.1.1",
39
39
  "@types/luxon": "^2.4.0"
40
40
  },
41
- "version": "0.0.300"
41
+ "version": "0.0.302"
42
42
  }
@@ -275,7 +275,7 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
275
275
  ),
276
276
  state_facts extend { dimension: foo is 1.3, bar is 2.3, baz is 3.3 }
277
277
  )
278
- // even though the first composite has all the fields foo, bar, baz; it is impossible
278
+ // even though the first composite hAS all the fields foo, bar, baz; it is impossible
279
279
  // to resolve it using the first composite, because you can't have both bar and baz
280
280
  // so the second input source is used instead
281
281
  run: x -> { group_by: foo, bar, baz }
@@ -541,4 +541,81 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
541
541
  run: x -> { group_by: x }
542
542
  `).malloyResultMatches(runtime, {x: 'b1'});
543
543
  });
544
+ describe('partition composites', () => {
545
+ const id = (n: string) => (databaseName === 'snowflake' ? `"${n}"` : n);
546
+ test('partition composite basic', async () => {
547
+ await expect(`
548
+ #! experimental { partition_composite { partition_field=p partitions={a={a} b={b}} } }
549
+ source: comp is ${databaseName}.sql("""
550
+ SELECT 10 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
551
+ UNION ALL SELECT 20 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
552
+ UNION ALL SELECT 0 AS ${id('a')}, 1 AS ${id('b')}, 'b' AS ${id('p')}
553
+ UNION ALL SELECT 0 AS ${id('a')}, 2 AS ${id('b')}, 'b' AS ${id('p')}
554
+ """)
555
+
556
+ run: comp -> {
557
+ aggregate: a_avg is a.avg()
558
+ aggregate: c is count()
559
+ }
560
+ `).malloyResultMatches(runtime, {a_avg: 15, c: 2});
561
+ });
562
+ test('partition composite with fields different from partition name', async () => {
563
+ await expect(`
564
+ #! experimental { partition_composite { partition_field=p partitions={x={a} y={b}} } }
565
+ source: comp is ${databaseName}.sql("""
566
+ SELECT 10 AS ${id('a')}, 0 AS ${id('b')}, 'x' AS ${id('p')}
567
+ UNION ALL SELECT 20 AS ${id('a')}, 0 AS ${id('b')}, 'x' AS ${id('p')}
568
+ UNION ALL SELECT 0 AS ${id('a')}, 1 AS ${id('b')}, 'y' AS ${id('p')}
569
+ UNION ALL SELECT 0 AS ${id('a')}, 2 AS ${id('b')}, 'y' AS ${id('p')}
570
+ """)
571
+
572
+ run: comp -> {
573
+ aggregate: a_avg is a.avg()
574
+ aggregate: c is count()
575
+ }
576
+ `).malloyResultMatches(runtime, {a_avg: 15, c: 2});
577
+ });
578
+ test('extended partition composite', async () => {
579
+ await expect(`
580
+ #! experimental { partition_composite { partition_field=p partitions={a={a} b={b}} } }
581
+ source: comp is ${databaseName}.sql("""
582
+ SELECT 10 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
583
+ UNION ALL SELECT 20 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
584
+ UNION ALL SELECT 0 AS ${id('a')}, 1 AS ${id('b')}, 'b' AS ${id('p')}
585
+ UNION ALL SELECT 0 AS ${id('a')}, 2 AS ${id('b')}, 'b' AS ${id('p')}
586
+ """)
587
+
588
+ source: comp_ext is comp extend {
589
+ measure: a_avg is a.avg()
590
+ }
591
+
592
+ run: comp_ext -> {
593
+ aggregate: a_avg
594
+ aggregate: c is count()
595
+ }
596
+ `).malloyResultMatches(runtime, {a_avg: 15, c: 2});
597
+ });
598
+ test('partition composite nested in composite', async () => {
599
+ await expect(`
600
+ ##! experimental.composite_sources
601
+ #! experimental { partition_composite { partition_field=p partitions={a={a} b={b}} } }
602
+ source: part_comp is ${databaseName}.sql("""
603
+ SELECT 10 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
604
+ UNION ALL SELECT 20 AS ${id('a')}, 0 AS ${id('b')}, 'a' AS ${id('p')}
605
+ UNION ALL SELECT 0 AS ${id('a')}, 1 AS ${id('b')}, 'b' AS ${id('p')}
606
+ UNION ALL SELECT 0 AS ${id('a')}, 2 AS ${id('b')}, 'b' AS ${id('p')}
607
+ """)
608
+
609
+ source: comp is compose(
610
+ part_comp,
611
+ ${databaseName}.sql('SELECT 10 AS ${id('c')}')
612
+ )
613
+
614
+ run: comp -> {
615
+ aggregate: a_avg is a.avg()
616
+ aggregate: c is count()
617
+ }
618
+ `).malloyResultMatches(runtime, {a_avg: 15, c: 2});
619
+ });
620
+ });
544
621
  });
@@ -22,7 +22,7 @@
22
22
  */
23
23
 
24
24
  import type {WriteStream} from '@malloydata/malloy';
25
- import {CSVWriter} from '@malloydata/malloy';
25
+ import {CSVWriter, JSONWriter} from '@malloydata/malloy';
26
26
  import {describeIfDatabaseAvailable} from '../../util';
27
27
  import {RuntimeList} from '../../runtimes';
28
28
 
@@ -40,7 +40,7 @@ class StringAccumulator implements WriteStream {
40
40
 
41
41
  const runtimes = ['duckdb'];
42
42
 
43
- const [_describe, databases] = describeIfDatabaseAvailable(runtimes);
43
+ const [describe, databases] = describeIfDatabaseAvailable(runtimes);
44
44
 
45
45
  function modelText(databaseName: string) {
46
46
  return `source: airports is ${databaseName}.table('test/data/duckdb/airports.parquet') extend {
@@ -101,15 +101,17 @@ describe('Streaming tests', () => {
101
101
  });
102
102
 
103
103
  runtimes.runtimeMap.forEach((runtime, databaseName) => {
104
- it(`stream nested results to CSV - ${databaseName}`, async () => {
105
- const stream = runtime
106
- .loadModel(modelText(databaseName))
107
- .loadQuery('run: airports -> airports_by_region')
108
- .runStream();
109
- const accummulator = new StringAccumulator();
110
- const csvWriter = new CSVWriter(accummulator);
111
- await csvWriter.process(stream);
112
- const expectedCsv = `faa_region,by_state,,,,by_facility_type,,airport_count
104
+ describe('csv', () => {
105
+ it(`stream nested results - ${databaseName}`, async () => {
106
+ const stream = runtime
107
+ .loadModel(modelText(databaseName))
108
+ .loadQuery('run: airports -> airports_by_region')
109
+ .runStream();
110
+ const accumulator = new StringAccumulator();
111
+ const csvWriter = new CSVWriter(accumulator);
112
+ await csvWriter.process(stream);
113
+ const expectedCsv = `\
114
+ faa_region,by_state,,,,by_facility_type,,airport_count
113
115
  AGL,state,airport_count,by_county,,facility_type,airport_count,4437
114
116
  ,IL,890,county,airport_count,AIRPORT,3443,
115
117
  ,,,COOK,51,HELIPORT,826,
@@ -125,25 +127,172 @@ ASW,state,airport_count,by_county,,facility_type,airport_count,3268
125
127
  ,,,PLAQUEMINES,31,,,
126
128
  ,,,VERMILION,29,,,
127
129
  `;
128
- expect(accummulator.accumulatedValue).toBe(expectedCsv);
129
- });
130
+ expect(accumulator.accumulatedValue).toBe(expectedCsv);
131
+ });
130
132
 
131
- it(`stream simple results to CSV - ${databaseName}`, async () => {
132
- const stream = runtime
133
- .loadModel(modelText(databaseName))
134
- .loadQuery('run: airports -> higher_elevation')
135
- .runStream();
136
- const accummulator = new StringAccumulator();
137
- const csvWriter = new CSVWriter(accummulator);
138
- await csvWriter.process(stream);
139
- const expectedCsv = `faa_region,airport_count,avg_elevation
133
+ it(`stream simple results - ${databaseName}`, async () => {
134
+ const stream = runtime
135
+ .loadModel(modelText(databaseName))
136
+ .loadQuery('run: airports -> higher_elevation')
137
+ .runStream();
138
+ const accumulator = new StringAccumulator();
139
+ const csvWriter = new CSVWriter(accumulator);
140
+ await csvWriter.process(stream);
141
+ const expectedCsv = `\
142
+ faa_region,airport_count,avg_elevation
140
143
  ANM,2102,3284.3910561370126
141
144
  AWP,1503,1667.0991350632069
142
145
  ACE,1579,1339.0139328689045
143
146
  ASW,3268,1007.2873317013464
144
147
  AGL,4437,983.4800540906018
145
148
  `;
146
- expect(accummulator.accumulatedValue).toBe(expectedCsv);
149
+ expect(accumulator.accumulatedValue).toBe(expectedCsv);
150
+ });
151
+ });
152
+
153
+ describe('json', () => {
154
+ it(`stream nested results - ${databaseName}`, async () => {
155
+ const stream = runtime
156
+ .loadModel(modelText(databaseName))
157
+ .loadQuery('run: airports -> airports_by_region')
158
+ .runStream();
159
+ const accumulator = new StringAccumulator();
160
+ const jsonWriter = new JSONWriter(accumulator);
161
+ await jsonWriter.process(stream);
162
+ const expectedJson = `\
163
+ [
164
+ {
165
+ "faa_region": "AGL",
166
+ "by_state": [
167
+ {
168
+ "state": "IL",
169
+ "airport_count": 890,
170
+ "by_county": [
171
+ {
172
+ "county": "COOK",
173
+ "airport_count": 51
174
+ },
175
+ {
176
+ "county": "LA SALLE",
177
+ "airport_count": 39
178
+ }
179
+ ]
180
+ },
181
+ {
182
+ "state": "OH",
183
+ "airport_count": 749,
184
+ "by_county": [
185
+ {
186
+ "county": "FRANKLIN",
187
+ "airport_count": 27
188
+ },
189
+ {
190
+ "county": "CUYAHOGA",
191
+ "airport_count": 27
192
+ }
193
+ ]
194
+ }
195
+ ],
196
+ "by_facility_type": [
197
+ {
198
+ "facility_type": "AIRPORT",
199
+ "airport_count": 3443
200
+ },
201
+ {
202
+ "facility_type": "HELIPORT",
203
+ "airport_count": 826
204
+ }
205
+ ],
206
+ "airport_count": 4437
207
+ },
208
+ {
209
+ "faa_region": "ASW",
210
+ "by_state": [
211
+ {
212
+ "state": "TX",
213
+ "airport_count": 1845,
214
+ "by_county": [
215
+ {
216
+ "county": "HARRIS",
217
+ "airport_count": 135
218
+ },
219
+ {
220
+ "county": "TARRANT",
221
+ "airport_count": 63
222
+ }
223
+ ]
224
+ },
225
+ {
226
+ "state": "LA",
227
+ "airport_count": 500,
228
+ "by_county": [
229
+ {
230
+ "county": "PLAQUEMINES",
231
+ "airport_count": 31
232
+ },
233
+ {
234
+ "county": "VERMILION",
235
+ "airport_count": 29
236
+ }
237
+ ]
238
+ }
239
+ ],
240
+ "by_facility_type": [
241
+ {
242
+ "facility_type": "AIRPORT",
243
+ "airport_count": 2341
244
+ },
245
+ {
246
+ "facility_type": "HELIPORT",
247
+ "airport_count": 861
248
+ }
249
+ ],
250
+ "airport_count": 3268
251
+ }
252
+ ]
253
+ `;
254
+ expect(accumulator.accumulatedValue).toBe(expectedJson);
255
+ });
256
+
257
+ it(`stream simple results - ${databaseName}`, async () => {
258
+ const stream = runtime
259
+ .loadModel(modelText(databaseName))
260
+ .loadQuery('run: airports -> higher_elevation')
261
+ .runStream();
262
+ const accumulator = new StringAccumulator();
263
+ const jsonWriter = new JSONWriter(accumulator);
264
+ await jsonWriter.process(stream);
265
+ const expectedJson = `\
266
+ [
267
+ {
268
+ "faa_region": "ANM",
269
+ "airport_count": 2102,
270
+ "avg_elevation": 3284.3910561370126
271
+ },
272
+ {
273
+ "faa_region": "AWP",
274
+ "airport_count": 1503,
275
+ "avg_elevation": 1667.0991350632069
276
+ },
277
+ {
278
+ "faa_region": "ACE",
279
+ "airport_count": 1579,
280
+ "avg_elevation": 1339.0139328689045
281
+ },
282
+ {
283
+ "faa_region": "ASW",
284
+ "airport_count": 3268,
285
+ "avg_elevation": 1007.2873317013464
286
+ },
287
+ {
288
+ "faa_region": "AGL",
289
+ "airport_count": 4437,
290
+ "avg_elevation": 983.4800540906018
291
+ }
292
+ ]
293
+ `;
294
+ expect(accumulator.accumulatedValue).toBe(expectedJson);
295
+ });
147
296
  });
148
297
  });
149
298
  });