@malloydata/malloy-tests 0.0.436 → 0.0.438
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/CONTEXT.md +1 -1
- package/databricks/diag_databricks.ts +1 -0
- package/package.json +11 -11
- package/presto/presto_start.sh +1 -1
- package/src/core/bugless.spec.ts +20 -0
- package/src/databases/all/array_query.spec.ts +720 -0
- package/src/databases/all/db_filter_expressions.spec.ts +27 -0
- package/src/databases/all/expr.spec.ts +101 -1
- package/src/databases/all/join.spec.ts +265 -1
- package/src/databases/all/nest.spec.ts +34 -0
- package/src/databases/bigquery/handexpr.spec.ts +6 -2
- package/src/databases/bigquery/wildcard_table_names.spec.ts +3 -3
- package/src/databases/postgres/postgres.spec.ts +22 -0
- package/src/runtimes.ts +1 -0
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Contributors to the Malloy project
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {RuntimeList, allDatabases} from '../../runtimes';
|
|
7
|
+
import {databasesFromEnvironmentOr} from '../../util';
|
|
8
|
+
import '@malloydata/malloy/test/matchers';
|
|
9
|
+
import {wrapTestModel} from '@malloydata/malloy/test';
|
|
10
|
+
|
|
11
|
+
const runtimes = new RuntimeList(databasesFromEnvironmentOr(allDatabases));
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
* `array -> { aggregate: ... }`: a query on the elements of an array of the
|
|
15
|
+
* current row, whose value is a value of that row. Experimental.
|
|
16
|
+
*/
|
|
17
|
+
describe.each(runtimes.runtimeList)('array queries %s', (db, runtime) => {
|
|
18
|
+
const support = runtime.dialect.arrayQueries;
|
|
19
|
+
const supported = support !== undefined;
|
|
20
|
+
// A query on an array with rows of its own returns them as an array.
|
|
21
|
+
const arrays = support?.arrays === true;
|
|
22
|
+
// A value which array functions cannot compute, such as one with a
|
|
23
|
+
// `having:`, is a correlated subquery where the dialect has one: it
|
|
24
|
+
// computes values that way, or it returns arrays.
|
|
25
|
+
const anyValue = runtime.dialect.arrayQueryHasSubquery();
|
|
26
|
+
// In place, one condition on both an element and an array inside it
|
|
27
|
+
// needs a lambda inside a lambda which reads the outer one's parameter.
|
|
28
|
+
const readsBoth =
|
|
29
|
+
support !== undefined &&
|
|
30
|
+
(support.value === 'subquery' || support.innerLambdaReadsOuter);
|
|
31
|
+
// Queries on an array which return its rows; elsewhere each is a
|
|
32
|
+
// translation error, so the model has them only where they compile.
|
|
33
|
+
const withRows = `
|
|
34
|
+
small_types is by_fac_type -> { select: *; where: airport_count < 50 }
|
|
35
|
+
doubled is by_fac_type -> {
|
|
36
|
+
select: fac_type, double_count is airport_count * 2
|
|
37
|
+
}
|
|
38
|
+
by_size is by_fac_type -> {
|
|
39
|
+
group_by: big is airport_count > 100
|
|
40
|
+
aggregate: n is airport_count.sum()
|
|
41
|
+
}
|
|
42
|
+
by_size_again is by_size
|
|
43
|
+
busy_types is by_fac_type -> {
|
|
44
|
+
group_by: fac_type
|
|
45
|
+
aggregate: n is airport_count.sum()
|
|
46
|
+
} -> {
|
|
47
|
+
where: n > 50
|
|
48
|
+
select: fac_type
|
|
49
|
+
}
|
|
50
|
+
none is by_fac_type -> { select: *; where: airport_count > 1000 }`;
|
|
51
|
+
const model = `
|
|
52
|
+
##! experimental.array_queries
|
|
53
|
+
|
|
54
|
+
// One row per state, with its airports by facility type nested inside.
|
|
55
|
+
source: a is ${db}.table('malloytest.airports') -> {
|
|
56
|
+
group_by: state
|
|
57
|
+
aggregate: airport_count is count()
|
|
58
|
+
nest: by_fac_type is {
|
|
59
|
+
group_by: fac_type
|
|
60
|
+
aggregate: airport_count is count()
|
|
61
|
+
}
|
|
62
|
+
} extend {
|
|
63
|
+
dimension:
|
|
64
|
+
state_airports is by_fac_type -> { aggregate: n is airport_count.sum() }
|
|
65
|
+
seaplanes is by_fac_type -> {
|
|
66
|
+
aggregate: n is airport_count.sum() { where: fac_type = 'SEAPLANE BASE' }
|
|
67
|
+
}
|
|
68
|
+
${arrays ? withRows : ''}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// States as "movies", each with an array of genres: its most popular
|
|
72
|
+
// name, and 'any'.
|
|
73
|
+
source: movies is ${db}.table('malloytest.state_facts') -> {
|
|
74
|
+
select: state, genres is [popular_name, 'any']
|
|
75
|
+
} extend {
|
|
76
|
+
dimension:
|
|
77
|
+
genre_count is genres -> { aggregate: n is count() }
|
|
78
|
+
isabella is genres -> { aggregate: n is count() { where: each = 'Isabella' } }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// One row per state, with its counties, each with its airports by
|
|
82
|
+
// facility type: an array inside the elements.
|
|
83
|
+
source: states is ${db}.table('malloytest.airports') -> {
|
|
84
|
+
group_by: state
|
|
85
|
+
aggregate: airport_count is count()
|
|
86
|
+
nest: by_county is {
|
|
87
|
+
group_by: county
|
|
88
|
+
// Null for most counties
|
|
89
|
+
aggregate: high is max(elevation) { where: elevation > 8000 }
|
|
90
|
+
nest: by_fac_type is {
|
|
91
|
+
group_by: fac_type
|
|
92
|
+
aggregate: airport_count is count()
|
|
93
|
+
}
|
|
94
|
+
// Empty for most counties
|
|
95
|
+
nest: heliports is {
|
|
96
|
+
where: fac_type = 'HELIPORT'
|
|
97
|
+
group_by: fac_type
|
|
98
|
+
aggregate: airport_count is count()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} extend {
|
|
102
|
+
dimension:
|
|
103
|
+
biggest_group is by_county -> {
|
|
104
|
+
aggregate: n is by_fac_type.airport_count.max()
|
|
105
|
+
}
|
|
106
|
+
type_count is by_county -> {
|
|
107
|
+
aggregate: n is count(by_fac_type.fac_type)
|
|
108
|
+
}
|
|
109
|
+
heliport_total is by_county -> {
|
|
110
|
+
aggregate: n is heliports.airport_count.sum()
|
|
111
|
+
}
|
|
112
|
+
heliport_most is by_county -> {
|
|
113
|
+
aggregate: n is heliports.airport_count.max()
|
|
114
|
+
}
|
|
115
|
+
high_sum is by_county -> { aggregate: n is high.sum() }
|
|
116
|
+
high_max is by_county -> { aggregate: n is high.max() }
|
|
117
|
+
high_avg is by_county -> { aggregate: n is high.avg() }
|
|
118
|
+
nested_total is by_county -> {
|
|
119
|
+
aggregate: n is by_fac_type.airport_count.sum()
|
|
120
|
+
}
|
|
121
|
+
a_heliports is by_county -> {
|
|
122
|
+
where: county ~ 'A%'
|
|
123
|
+
aggregate: n is by_fac_type.airport_count.sum() {
|
|
124
|
+
where: by_fac_type.fac_type = 'HELIPORT'
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
heliport_or_jefferson is by_county -> {
|
|
128
|
+
aggregate: n is by_fac_type.count() {
|
|
129
|
+
where: by_fac_type.fac_type = 'HELIPORT' or county = 'JEFFERSON'
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
`;
|
|
134
|
+
const tm = wrapTestModel(runtime, model);
|
|
135
|
+
|
|
136
|
+
test.when(supported)('aggregates a record array per row', async () => {
|
|
137
|
+
// Alaska has 608 airports, 104 of them seaplane bases
|
|
138
|
+
await expect(`
|
|
139
|
+
run: a -> {
|
|
140
|
+
where: state = 'AK'
|
|
141
|
+
select: state, state_airports, seaplanes
|
|
142
|
+
}
|
|
143
|
+
`).toEqualResult(tm, [{state: 'AK', state_airports: 608, seaplanes: 104}]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test.when(supported)('in a where:, without fanning out', async () => {
|
|
147
|
+
// 41 states have seaplane bases
|
|
148
|
+
const query = `
|
|
149
|
+
run: a -> {
|
|
150
|
+
where: seaplanes > 0
|
|
151
|
+
aggregate: state_count is count()
|
|
152
|
+
}
|
|
153
|
+
`;
|
|
154
|
+
const sql = await runtime.loadModel(model).loadQuery(query).getSQL();
|
|
155
|
+
expect(sql).not.toMatch(/COUNT\(DISTINCT/i);
|
|
156
|
+
await expect(query).toMatchResult(tm, {state_count: 41});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test.when(supported)('aggregates an array of scalars per row', async () => {
|
|
160
|
+
// Every movie has two genres; 24 of them have Isabella
|
|
161
|
+
await expect(`
|
|
162
|
+
run: movies -> {
|
|
163
|
+
aggregate:
|
|
164
|
+
genres_total is genre_count.sum()
|
|
165
|
+
isabella_movies is isabella.sum()
|
|
166
|
+
}
|
|
167
|
+
`).toMatchResult(tm, {genres_total: 102, isabella_movies: 24});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test.when(supported)('in a group_by', async () => {
|
|
171
|
+
await expect(`
|
|
172
|
+
run: movies -> {
|
|
173
|
+
group_by: genre_count
|
|
174
|
+
aggregate: movie_count is count()
|
|
175
|
+
}
|
|
176
|
+
`).toEqualResult(tm, [{genre_count: 2, movie_count: 51}]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Alaska's three facility types: 474 airports, 104 seaplane bases and 30
|
|
180
|
+
// heliports.
|
|
181
|
+
const alaska = (dimension: string) => `
|
|
182
|
+
run: a extend { dimension: d is ${dimension} } -> {
|
|
183
|
+
where: state = 'AK'
|
|
184
|
+
select: d
|
|
185
|
+
}
|
|
186
|
+
`;
|
|
187
|
+
|
|
188
|
+
test.when(supported)('min, max, avg and count of a value', async () => {
|
|
189
|
+
await expect(`
|
|
190
|
+
run: a extend {
|
|
191
|
+
dimension:
|
|
192
|
+
fewest is by_fac_type -> { aggregate: n is airport_count.min() }
|
|
193
|
+
most is by_fac_type -> { aggregate: n is airport_count.max() }
|
|
194
|
+
mean is by_fac_type -> { aggregate: n is airport_count.avg() }
|
|
195
|
+
types is by_fac_type -> { aggregate: n is count(fac_type) }
|
|
196
|
+
} -> {
|
|
197
|
+
where: state = 'AK'
|
|
198
|
+
// Each database rounds 608 / 3 its own way; MySQL keeps four places.
|
|
199
|
+
select: fewest, most, mean is round(mean, 4), types
|
|
200
|
+
}
|
|
201
|
+
`).toMatchResult(tm, {fewest: 30, most: 474, mean: 202.6667, types: 3});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// A fold's state has one type; a sum of integers must not be a DOUBLE.
|
|
205
|
+
test.when(support?.value === 'lambdas')(
|
|
206
|
+
'a sum of integers is not folded as a DOUBLE',
|
|
207
|
+
async () => {
|
|
208
|
+
const sql = await runtime
|
|
209
|
+
.loadModel(model)
|
|
210
|
+
.loadQuery(
|
|
211
|
+
alaska('by_fac_type -> { aggregate: n is airport_count.sum() }')
|
|
212
|
+
)
|
|
213
|
+
.getSQL();
|
|
214
|
+
expect(sql).not.toMatch(/0 AS DOUBLE\)|0::DOUBLE/i);
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
test.when(supported)("the array query's own where:", async () => {
|
|
219
|
+
const query = alaska(
|
|
220
|
+
'by_fac_type -> { where: airport_count > 50; aggregate: n is airport_count.sum() }'
|
|
221
|
+
);
|
|
222
|
+
await expect(query).toEqualResult(tm, [{d: 578}]);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test.when(anyValue)("the array query's own having:", async () => {
|
|
226
|
+
// Alaska has three facility types; a having: which no row passes leaves
|
|
227
|
+
// no row, so no value
|
|
228
|
+
await expect(
|
|
229
|
+
alaska('by_fac_type -> { aggregate: n is count(); having: n > 3 }')
|
|
230
|
+
).toEqualResult(tm, [{d: null}]);
|
|
231
|
+
await expect(
|
|
232
|
+
alaska('by_fac_type -> { aggregate: n is count(); having: n > 2 }')
|
|
233
|
+
).toEqualResult(tm, [{d: 3}]);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test.when(supported && !anyValue)(
|
|
237
|
+
'with only array functions, a having: is an error',
|
|
238
|
+
async () => {
|
|
239
|
+
await expect(
|
|
240
|
+
runtime
|
|
241
|
+
.loadModel(model)
|
|
242
|
+
.loadQuery(
|
|
243
|
+
alaska('by_fac_type -> { aggregate: n is count(); having: n > 3 }')
|
|
244
|
+
)
|
|
245
|
+
.getSQL()
|
|
246
|
+
).rejects.toThrow(/cannot query this array: it has a `having:`/);
|
|
247
|
+
}
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
// What only the compiler can tell, it reports at the array the arrow
|
|
251
|
+
// queries.
|
|
252
|
+
test.when(supported && !anyValue)(
|
|
253
|
+
'with only array functions, a read of an inner array is an error at the arrow',
|
|
254
|
+
async () => {
|
|
255
|
+
const query = `run: states -> {
|
|
256
|
+
where: state = 'AK'
|
|
257
|
+
select: d is by_county -> {
|
|
258
|
+
aggregate: n is count() { where: by_fac_type.fac_type = 'HELIPORT' }
|
|
259
|
+
}
|
|
260
|
+
}`;
|
|
261
|
+
let problems: {code: string; at?: unknown}[] = [];
|
|
262
|
+
try {
|
|
263
|
+
await runtime.loadModel(model).loadQuery(query).getSQL();
|
|
264
|
+
} catch (e) {
|
|
265
|
+
problems = e.problems ?? [];
|
|
266
|
+
}
|
|
267
|
+
expect(problems).toEqual([
|
|
268
|
+
expect.objectContaining({
|
|
269
|
+
code: 'array-query-not-in-place',
|
|
270
|
+
message: expect.stringMatching(
|
|
271
|
+
/it reads `by_fac_type.fac_type`, from an array inside the elements/
|
|
272
|
+
),
|
|
273
|
+
at: expect.objectContaining({range: expect.anything()}),
|
|
274
|
+
}),
|
|
275
|
+
]);
|
|
276
|
+
}
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
test.when(supported)('a where: and a filter with an or', async () => {
|
|
280
|
+
// Of Alaska's facility types, only heliports number under 50. Read as
|
|
281
|
+
// (n < 50 and heliport) or seaplane base, it would be 2.
|
|
282
|
+
const query = alaska(`by_fac_type -> {
|
|
283
|
+
where: airport_count < 50
|
|
284
|
+
aggregate: n is count() {
|
|
285
|
+
where: fac_type = 'HELIPORT' or fac_type = 'SEAPLANE BASE'
|
|
286
|
+
}
|
|
287
|
+
}`);
|
|
288
|
+
await expect(query).toEqualResult(tm, [{d: 1}]);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test.when(supported)('a where: and a filtered aggregate', async () => {
|
|
292
|
+
const query = alaska(`by_fac_type -> {
|
|
293
|
+
where: airport_count > 50
|
|
294
|
+
aggregate: n is count() { where: fac_type != 'AIRPORT' }
|
|
295
|
+
}`);
|
|
296
|
+
await expect(query).toEqualResult(tm, [{d: 1}]);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test.when(supported)('when the query also unnests the array', async () => {
|
|
300
|
+
// Grouped by facility type, the states with seaplane bases have 473
|
|
301
|
+
const query = `
|
|
302
|
+
run: a -> {
|
|
303
|
+
where: by_fac_type.fac_type = 'SEAPLANE BASE'
|
|
304
|
+
group_by: by_fac_type.fac_type
|
|
305
|
+
aggregate: seaplane_bases is seaplanes.sum()
|
|
306
|
+
}
|
|
307
|
+
`;
|
|
308
|
+
await expect(query).toEqualResult(tm, [
|
|
309
|
+
{fac_type: 'SEAPLANE BASE', seaplane_bases: 473},
|
|
310
|
+
]);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
describe('an aggregate of an array inside the elements', () => {
|
|
314
|
+
test.when(supported)('is over all the inner elements', async () => {
|
|
315
|
+
// Each state's facility types, summed across its counties, are all
|
|
316
|
+
// of its airports.
|
|
317
|
+
await expect(`
|
|
318
|
+
run: states -> {
|
|
319
|
+
aggregate:
|
|
320
|
+
total is nested_total.sum()
|
|
321
|
+
mismatched is count() { where: nested_total != airport_count }
|
|
322
|
+
}
|
|
323
|
+
`).toEqualResult(tm, [{total: 19793, mismatched: 0}]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test.when(supported)(
|
|
327
|
+
'min, max and count(x) of the inner array',
|
|
328
|
+
async () => {
|
|
329
|
+
// Alaska's biggest county and facility type group is 41 airports,
|
|
330
|
+
// of 3 facility types
|
|
331
|
+
await expect(`
|
|
332
|
+
run: states -> {
|
|
333
|
+
where: state = 'AK'
|
|
334
|
+
select: biggest_group, type_count
|
|
335
|
+
}
|
|
336
|
+
`).toEqualResult(tm, [{biggest_group: 41, type_count: 3}]);
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
test.when(supported)('empty inner arrays and null values', async () => {
|
|
341
|
+
// Most counties have no heliports, and no airport above 8,000 feet.
|
|
342
|
+
// There are 5,135 heliports, at most 151 in one county; 4 states
|
|
343
|
+
// have airports above 8,000 feet, the highest at 12,442.
|
|
344
|
+
await expect(`
|
|
345
|
+
run: states -> {
|
|
346
|
+
aggregate:
|
|
347
|
+
heliports is heliport_total.sum()
|
|
348
|
+
most is heliport_most.max()
|
|
349
|
+
high_total is high_sum.sum()
|
|
350
|
+
highest is high_max.max()
|
|
351
|
+
high_states is count() { where: high_avg is not null }
|
|
352
|
+
}
|
|
353
|
+
`).toEqualResult(tm, [
|
|
354
|
+
{
|
|
355
|
+
heliports: 5135,
|
|
356
|
+
most: 151,
|
|
357
|
+
high_total: 350120,
|
|
358
|
+
highest: 12442,
|
|
359
|
+
high_states: 4,
|
|
360
|
+
},
|
|
361
|
+
]);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test.when(supported)(
|
|
365
|
+
'with conditions on the outer and the inner elements',
|
|
366
|
+
async () => {
|
|
367
|
+
// 169 heliports are in counties whose names start with A
|
|
368
|
+
await expect(
|
|
369
|
+
'run: states -> { aggregate: n is a_heliports.sum() }'
|
|
370
|
+
).toEqualResult(tm, [{n: 169}]);
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
test.when(readsBoth)(
|
|
375
|
+
'a condition on both the outer and the inner element',
|
|
376
|
+
async () => {
|
|
377
|
+
// Of the 4,991 county and facility type groups, 1,590 are heliports
|
|
378
|
+
// or in a county named Jefferson
|
|
379
|
+
await expect(
|
|
380
|
+
'run: states -> { aggregate: n is heliport_or_jefferson.sum() }'
|
|
381
|
+
).toEqualResult(tm, [{n: 1590}]);
|
|
382
|
+
}
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
test.when(supported && !readsBoth)(
|
|
386
|
+
'where an inner lambda cannot read the outer one, a condition on both is an error',
|
|
387
|
+
async () => {
|
|
388
|
+
await expect(
|
|
389
|
+
runtime
|
|
390
|
+
.loadModel(model)
|
|
391
|
+
.loadQuery(
|
|
392
|
+
'run: states -> { aggregate: n is heliport_or_jefferson.sum() }'
|
|
393
|
+
)
|
|
394
|
+
.getSQL()
|
|
395
|
+
).rejects.toThrow(
|
|
396
|
+
/one condition reads both `by_fac_type` and the elements' own fields/
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
/*
|
|
403
|
+
* DuckDB computes a query on an array which is one aggregate with list
|
|
404
|
+
* functions, in place, instead of a correlated subquery over UNNEST.
|
|
405
|
+
*/
|
|
406
|
+
describe('DuckDB list functions', () => {
|
|
407
|
+
const duckdb = db === 'duckdb';
|
|
408
|
+
async function sqlOf(query: string): Promise<string> {
|
|
409
|
+
return runtime.loadModel(model).loadQuery(query).getSQL();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
test.when(duckdb)('an aggregate is computed in place', async () => {
|
|
413
|
+
for (const query of [
|
|
414
|
+
alaska('by_fac_type -> { aggregate: n is airport_count.sum() }'),
|
|
415
|
+
alaska(
|
|
416
|
+
'by_fac_type -> { where: airport_count > 50; aggregate: n is count() }'
|
|
417
|
+
),
|
|
418
|
+
"run: movies -> { select: isabella; where: state = 'AL' }",
|
|
419
|
+
alaska('by_fac_type -> { aggregate: n is count(fac_type) }'),
|
|
420
|
+
"run: states -> { select: nested_total; where: state = 'AK' }",
|
|
421
|
+
]) {
|
|
422
|
+
const sql = await sqlOf(query);
|
|
423
|
+
expect(sql).toMatch(/LIST_|LEN\(/);
|
|
424
|
+
// The correlated form unnests a column of the row.
|
|
425
|
+
expect(sql).not.toMatch(/SELECT UNNEST\(base\./);
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test.when(duckdb)('also while the query unnests the array', async () => {
|
|
430
|
+
// The element gets a name of its own, which the unnested column of the
|
|
431
|
+
// array cannot hide.
|
|
432
|
+
const sql = await sqlOf(`
|
|
433
|
+
run: a -> {
|
|
434
|
+
group_by: by_fac_type.fac_type
|
|
435
|
+
aggregate: seaplane_bases is seaplanes.sum()
|
|
436
|
+
}
|
|
437
|
+
`);
|
|
438
|
+
expect(sql).toMatch(/LIST_SUM/);
|
|
439
|
+
expect(sql).toMatch(/__by_fac_type_element_0 ->/);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test.when(duckdb)('empty and null arrays', async () => {
|
|
443
|
+
await expect(`
|
|
444
|
+
source: lists is duckdb.sql("""
|
|
445
|
+
SELECT 1 AS id, [1, 1, 2, NULL] AS xs
|
|
446
|
+
UNION ALL SELECT 2, []::INTEGER[]
|
|
447
|
+
UNION ALL SELECT 3, NULL::INTEGER[]
|
|
448
|
+
""") extend {
|
|
449
|
+
dimension:
|
|
450
|
+
total is xs -> { aggregate: n is value.sum() }
|
|
451
|
+
elements is xs -> { aggregate: n is count() }
|
|
452
|
+
distinct_values is xs -> { aggregate: n is count(value) }
|
|
453
|
+
mean is xs -> { aggregate: n is value.avg() }
|
|
454
|
+
biggest is xs -> { aggregate: n is value.max() }
|
|
455
|
+
}
|
|
456
|
+
run: lists -> {
|
|
457
|
+
select: id, total, elements, distinct_values, mean, biggest
|
|
458
|
+
order_by: id
|
|
459
|
+
}
|
|
460
|
+
`).toEqualResult(tm, [
|
|
461
|
+
{
|
|
462
|
+
id: 1,
|
|
463
|
+
total: 4,
|
|
464
|
+
elements: 4,
|
|
465
|
+
distinct_values: 2,
|
|
466
|
+
mean: 4 / 3,
|
|
467
|
+
biggest: 2,
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
id: 2,
|
|
471
|
+
total: 0,
|
|
472
|
+
elements: 0,
|
|
473
|
+
distinct_values: 0,
|
|
474
|
+
mean: null,
|
|
475
|
+
biggest: null,
|
|
476
|
+
},
|
|
477
|
+
{
|
|
478
|
+
id: 3,
|
|
479
|
+
total: 0,
|
|
480
|
+
elements: 0,
|
|
481
|
+
distinct_values: 0,
|
|
482
|
+
mean: null,
|
|
483
|
+
biggest: null,
|
|
484
|
+
},
|
|
485
|
+
]);
|
|
486
|
+
});
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
/*
|
|
490
|
+
* A query on an array with rows of its own returns them as an array of
|
|
491
|
+
* records, which can be selected, or reached into with a path like any
|
|
492
|
+
* other array.
|
|
493
|
+
*/
|
|
494
|
+
describe('array results', () => {
|
|
495
|
+
test.when(supported && !arrays)(
|
|
496
|
+
'where the dialect returns no arrays, a query with rows is an error',
|
|
497
|
+
async () => {
|
|
498
|
+
await expect(
|
|
499
|
+
runtime
|
|
500
|
+
.loadModel(model)
|
|
501
|
+
.loadQuery(alaska('by_fac_type -> { select: fac_type }'))
|
|
502
|
+
.getSQL()
|
|
503
|
+
).rejects.toThrow(/can only query an array for one value/);
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
|
|
507
|
+
test.when(arrays)('select: with a where:', async () => {
|
|
508
|
+
await expect(`
|
|
509
|
+
run: a -> { where: state = 'AK'; select: small_types }
|
|
510
|
+
`).toEqualResult(tm, [
|
|
511
|
+
{small_types: [{fac_type: 'HELIPORT', airport_count: 30}]},
|
|
512
|
+
]);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
test.when(arrays)('none selected is an empty array', async () => {
|
|
516
|
+
await expect(`
|
|
517
|
+
run: a -> { where: state = 'AK'; select: none }
|
|
518
|
+
`).toEqualResult(tm, [{none: []}]);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
test.when(arrays)('a path into the result', async () => {
|
|
522
|
+
// 167 groups of a state and a facility type have fewer than 50
|
|
523
|
+
// airports, 1236 in all. One more has no state, and an element's
|
|
524
|
+
// distinct key is made of its row's key, so it is left out.
|
|
525
|
+
await expect(`
|
|
526
|
+
run: a -> {
|
|
527
|
+
where: state is not null
|
|
528
|
+
aggregate:
|
|
529
|
+
small_groups is small_types.count()
|
|
530
|
+
small_total is small_types.airport_count.sum()
|
|
531
|
+
}
|
|
532
|
+
`).toMatchResult(tm, {small_groups: 167, small_total: 1236});
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
test.when(arrays)('select: of an expression', async () => {
|
|
536
|
+
await expect(`
|
|
537
|
+
run: a -> {
|
|
538
|
+
where: state = 'AK'
|
|
539
|
+
aggregate: n is doubled.double_count.sum()
|
|
540
|
+
}
|
|
541
|
+
`).toMatchResult(tm, {n: 1216});
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
test.when(arrays)('group_by:, through another dimension', async () => {
|
|
545
|
+
await expect(`
|
|
546
|
+
run: a -> {
|
|
547
|
+
where: state = 'AK' and by_size_again.big = true
|
|
548
|
+
aggregate: n is by_size_again.n.sum()
|
|
549
|
+
}
|
|
550
|
+
`).toMatchResult(tm, {n: 578});
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
test.when(arrays)('group_by:', async () => {
|
|
554
|
+
// Alaska's airports and seaplane bases each number over 100
|
|
555
|
+
await expect(`
|
|
556
|
+
run: a -> {
|
|
557
|
+
where: state = 'AK' and by_size.big = true
|
|
558
|
+
aggregate: n is by_size.n.sum()
|
|
559
|
+
}
|
|
560
|
+
`).toMatchResult(tm, {n: 578});
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
test.when(arrays)('several stages', async () => {
|
|
564
|
+
await expect(`
|
|
565
|
+
run: a -> {
|
|
566
|
+
where: state = 'AK'
|
|
567
|
+
aggregate: n is busy_types.count()
|
|
568
|
+
}
|
|
569
|
+
`).toMatchResult(tm, {n: 2});
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
test.when(arrays)('group_by:, selected, in its order', async () => {
|
|
573
|
+
await expect(`
|
|
574
|
+
run: a -> { where: state = 'AK'; select: by_size }
|
|
575
|
+
`).toEqualResult(tm, [
|
|
576
|
+
{
|
|
577
|
+
by_size: [
|
|
578
|
+
{big: true, n: 578},
|
|
579
|
+
{big: false, n: 30},
|
|
580
|
+
],
|
|
581
|
+
},
|
|
582
|
+
]);
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
test.when(arrays)(
|
|
586
|
+
'group_by: with where:, having:, order_by: and limit:',
|
|
587
|
+
async () => {
|
|
588
|
+
// Alaska has 474 airports, 104 seaplane bases and 30 heliports
|
|
589
|
+
await expect(
|
|
590
|
+
alaska(`by_fac_type -> {
|
|
591
|
+
where: fac_type != 'AIRPORT'
|
|
592
|
+
group_by: fac_type
|
|
593
|
+
aggregate: n is airport_count.sum()
|
|
594
|
+
having: n > 50
|
|
595
|
+
}`)
|
|
596
|
+
).toEqualResult(tm, [{d: [{fac_type: 'SEAPLANE BASE', n: 104}]}]);
|
|
597
|
+
await expect(
|
|
598
|
+
alaska(`by_fac_type -> {
|
|
599
|
+
group_by: fac_type
|
|
600
|
+
aggregate: n is airport_count.sum()
|
|
601
|
+
order_by: n desc
|
|
602
|
+
limit: 2
|
|
603
|
+
}`)
|
|
604
|
+
).toEqualResult(tm, [
|
|
605
|
+
{
|
|
606
|
+
d: [
|
|
607
|
+
{fac_type: 'AIRPORT', n: 474},
|
|
608
|
+
{fac_type: 'SEAPLANE BASE', n: 104},
|
|
609
|
+
],
|
|
610
|
+
},
|
|
611
|
+
]);
|
|
612
|
+
}
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
test.when(arrays)('group_by:, queried again with ->', async () => {
|
|
616
|
+
await expect(`
|
|
617
|
+
run: a -> {
|
|
618
|
+
where: state = 'AK'
|
|
619
|
+
select: big is by_size -> { where: big = true; aggregate: t is n.sum() }
|
|
620
|
+
}
|
|
621
|
+
`).toEqualResult(tm, [{big: 578}]);
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
test.when(arrays)('several stages ending in one aggregate', async () => {
|
|
625
|
+
await expect(`
|
|
626
|
+
run: a -> {
|
|
627
|
+
where: state = 'AK'
|
|
628
|
+
select: busy is by_fac_type -> {
|
|
629
|
+
group_by: fac_type
|
|
630
|
+
aggregate: n is airport_count.sum()
|
|
631
|
+
} -> {
|
|
632
|
+
where: n > 50
|
|
633
|
+
aggregate: c is count()
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
`).toEqualResult(tm, [{busy: [{c: 2}]}]);
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
// Alaska's facility types, as a query on its array
|
|
640
|
+
const alaskaTypes = (query: string) => `
|
|
641
|
+
run: a extend { dimension: types is by_fac_type -> ${query} } -> {
|
|
642
|
+
where: state = 'AK'
|
|
643
|
+
select: types
|
|
644
|
+
}
|
|
645
|
+
`;
|
|
646
|
+
|
|
647
|
+
test.when(arrays)('order_by: and limit:', async () => {
|
|
648
|
+
await expect(
|
|
649
|
+
alaskaTypes(`{
|
|
650
|
+
select: fac_type, airport_count
|
|
651
|
+
order_by: airport_count desc
|
|
652
|
+
limit: 1
|
|
653
|
+
}`)
|
|
654
|
+
).toEqualResult(tm, [
|
|
655
|
+
{types: [{fac_type: 'AIRPORT', airport_count: 474}]},
|
|
656
|
+
]);
|
|
657
|
+
await expect(
|
|
658
|
+
alaskaTypes(`{
|
|
659
|
+
select: fac_type, airport_count
|
|
660
|
+
order_by: airport_count
|
|
661
|
+
limit: 2
|
|
662
|
+
}`)
|
|
663
|
+
).toEqualResult(tm, [
|
|
664
|
+
{
|
|
665
|
+
types: [
|
|
666
|
+
{fac_type: 'HELIPORT', airport_count: 30},
|
|
667
|
+
{fac_type: 'SEAPLANE BASE', airport_count: 104},
|
|
668
|
+
],
|
|
669
|
+
},
|
|
670
|
+
]);
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
test.when(arrays)('order_by: a position', async () => {
|
|
674
|
+
await expect(
|
|
675
|
+
alaskaTypes('{ select: fac_type, airport_count; order_by: 2 desc }')
|
|
676
|
+
).toEqualResult(tm, [
|
|
677
|
+
{
|
|
678
|
+
types: [
|
|
679
|
+
{fac_type: 'AIRPORT', airport_count: 474},
|
|
680
|
+
{fac_type: 'SEAPLANE BASE', airport_count: 104},
|
|
681
|
+
{fac_type: 'HELIPORT', airport_count: 30},
|
|
682
|
+
],
|
|
683
|
+
},
|
|
684
|
+
]);
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
test.when(arrays)('where: with order_by:', async () => {
|
|
688
|
+
await expect(
|
|
689
|
+
alaskaTypes(`{
|
|
690
|
+
select: fac_type
|
|
691
|
+
where: airport_count > 50
|
|
692
|
+
order_by: fac_type desc
|
|
693
|
+
}`)
|
|
694
|
+
).toEqualResult(tm, [
|
|
695
|
+
{types: [{fac_type: 'SEAPLANE BASE'}, {fac_type: 'AIRPORT'}]},
|
|
696
|
+
]);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
test.when(arrays)('order_by: keys in both directions', async () => {
|
|
700
|
+
await expect(
|
|
701
|
+
alaskaTypes(`{
|
|
702
|
+
select: fac_type, big is airport_count > 50
|
|
703
|
+
order_by: big desc, fac_type
|
|
704
|
+
}`)
|
|
705
|
+
).toEqualResult(tm, [
|
|
706
|
+
{
|
|
707
|
+
types: [
|
|
708
|
+
{fac_type: 'AIRPORT', big: true},
|
|
709
|
+
{fac_type: 'SEAPLANE BASE', big: true},
|
|
710
|
+
{fac_type: 'HELIPORT', big: false},
|
|
711
|
+
],
|
|
712
|
+
},
|
|
713
|
+
]);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
afterAll(async () => {
|
|
719
|
+
await runtimes.closeAll();
|
|
720
|
+
});
|