@malloydata/malloy-tests 0.0.437 → 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/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/runtimes.ts +1 -0
package/CONTEXT.md
CHANGED
|
@@ -181,7 +181,7 @@ All cross-database tests (`test/src/databases/all/`) reference tables as `malloy
|
|
|
181
181
|
| Trino/Presto | The hive connector reads the parquet in place: each file is mounted into the container and declared as an external table | `test/trino/hive_ddl.ts`, `test/trino/hive.properties`, `test/presto/hive.properties` | `trino_start.sh` / `presto_start.sh` derive the DDL from the parquet schemas at container start. The hive metastore lowercases names, so a table with nested columns (`ga_sample`) is a `_hive` table plus a view that casts the nested field names back; top-level column names stay lowercase, since Trino lowercases those in every connector |
|
|
182
182
|
| BigQuery | Pre-loaded manually in `malloydata-org` project | (none) | No loader script in repo. Data assumed to exist. |
|
|
183
183
|
| Snowflake | SQL: `PUT` local parquet → stage, `COPY INTO` table | `test/snowflake/load_test_data.sh` (wraps `load_test_data.sql`) | Run via `sh test/snowflake/load_test_data.sh` (needs the `snowsql` CLI) |
|
|
184
|
-
| Databricks | TS script: upload to Volume via REST, `CREATE TABLE AS SELECT FROM read_files()` | `test/databricks/load_test_data.sh` (wraps `load_test_data.ts`) | Run manually with the `DATABRICKS_*` env vars set: `sh test/databricks/load_test_data.sh` |
|
|
184
|
+
| Databricks | TS script: upload to Volume via REST, `CREATE TABLE AS SELECT FROM read_files()` | `test/databricks/load_test_data.sh` (wraps `load_test_data.ts`) | Run manually with the `DATABRICKS_*` env vars set: `sh test/databricks/load_test_data.sh`. From a network that reaches the workspace only through a proxy, `DATABRICKS_HTTPS_PROXY` is the connection's `proxy` setting for the tests and `diag_databricks.ts`; the loader uploads with `fetch`, which does not use it |
|
|
185
185
|
|
|
186
186
|
The DuckDB-driven loaders share `test/data/parquet_loader.ts`, which runs everything through Malloy's own `DuckDBConnection`, so a database is built by the same DuckDB the tests read it with. It reads the table list from the parquet directory and takes a `LoadPlan` naming what a target cannot hold.
|
|
187
187
|
|
|
@@ -17,6 +17,7 @@ async function main() {
|
|
|
17
17
|
token: process.env['DATABRICKS_TOKEN'],
|
|
18
18
|
defaultCatalog: process.env['DATABRICKS_CATALOG'],
|
|
19
19
|
defaultSchema: process.env['DATABRICKS_SCHEMA'],
|
|
20
|
+
proxy: process.env['DATABRICKS_HTTPS_PROXY'],
|
|
20
21
|
});
|
|
21
22
|
|
|
22
23
|
const sql = process.argv[2];
|
package/package.json
CHANGED
|
@@ -20,16 +20,16 @@
|
|
|
20
20
|
"malloyc": "ts-node ../scripts/malloy-to-json"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@malloydata/db-bigquery": "0.0.
|
|
24
|
-
"@malloydata/db-duckdb": "0.0.
|
|
25
|
-
"@malloydata/db-postgres": "0.0.
|
|
26
|
-
"@malloydata/db-snowflake": "0.0.
|
|
27
|
-
"@malloydata/db-sqlserver": "0.0.
|
|
28
|
-
"@malloydata/db-trino": "0.0.
|
|
29
|
-
"@malloydata/malloy": "0.0.
|
|
30
|
-
"@malloydata/malloy-tag": "0.0.
|
|
31
|
-
"@malloydata/render": "0.0.
|
|
32
|
-
"@malloydata/render-validator": "0.0.
|
|
23
|
+
"@malloydata/db-bigquery": "0.0.438",
|
|
24
|
+
"@malloydata/db-duckdb": "0.0.438",
|
|
25
|
+
"@malloydata/db-postgres": "0.0.438",
|
|
26
|
+
"@malloydata/db-snowflake": "0.0.438",
|
|
27
|
+
"@malloydata/db-sqlserver": "0.0.438",
|
|
28
|
+
"@malloydata/db-trino": "0.0.438",
|
|
29
|
+
"@malloydata/malloy": "0.0.438",
|
|
30
|
+
"@malloydata/malloy-tag": "0.0.438",
|
|
31
|
+
"@malloydata/render": "0.0.438",
|
|
32
|
+
"@malloydata/render-validator": "0.0.438",
|
|
33
33
|
"events": "^3.3.0",
|
|
34
34
|
"jsdom": "^22.1.0",
|
|
35
35
|
"luxon": "^3.7.2",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"overrides": {
|
|
44
44
|
"typescript": "^6.0.3"
|
|
45
45
|
},
|
|
46
|
-
"version": "0.0.
|
|
46
|
+
"version": "0.0.438"
|
|
47
47
|
}
|
package/presto/presto_start.sh
CHANGED
|
@@ -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
|
+
});
|
|
@@ -87,6 +87,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
|
|
|
87
87
|
select: nm; order_by: nm asc
|
|
88
88
|
}`).toEqualResult(abcTestModel, got('def,xback,z-empty,z-null'));
|
|
89
89
|
});
|
|
90
|
+
test('!~ abc', async () => {
|
|
91
|
+
await expect(`
|
|
92
|
+
run: abc -> {
|
|
93
|
+
where: s !~ f'abc',
|
|
94
|
+
select: nm; order_by: nm asc
|
|
95
|
+
}`).toEqualResult(abcTestModel, got('def,xback,z-empty,z-null'));
|
|
96
|
+
});
|
|
90
97
|
test('-starts', async () => {
|
|
91
98
|
await expect(`
|
|
92
99
|
# test.verbose
|
|
@@ -323,6 +330,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
|
|
|
323
330
|
select: t; order_by: t asc
|
|
324
331
|
}`).toEqualResult(numsTestModel, [{t: '0'}, {t: '4'}]);
|
|
325
332
|
});
|
|
333
|
+
test('!~ [1 to 3]', async () => {
|
|
334
|
+
await expect(`
|
|
335
|
+
run: nums -> {
|
|
336
|
+
where: n !~ f'[1 to 3]'
|
|
337
|
+
select: t; order_by: t asc
|
|
338
|
+
}`).toEqualResult(numsTestModel, [{t: '0'}, {t: '4'}, {t: 'null'}]);
|
|
339
|
+
});
|
|
326
340
|
test('123', async () => {
|
|
327
341
|
await expect(`
|
|
328
342
|
run: nums -> {
|
|
@@ -437,6 +451,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
|
|
|
437
451
|
select: t; order_by: t asc
|
|
438
452
|
}`).toEqualResult(factsTestModel, [{t: 'true'}]);
|
|
439
453
|
});
|
|
454
|
+
test.when(testBoolean)('!~ true', async () => {
|
|
455
|
+
await expect(`
|
|
456
|
+
run: ${factsSrc} -> {
|
|
457
|
+
where: b !~ f'true'
|
|
458
|
+
select: t; order_by: t asc
|
|
459
|
+
}`).toEqualResult(factsTestModel, [{t: 'false'}, {t: 'null'}]);
|
|
460
|
+
});
|
|
440
461
|
test.when(testBoolean)('=true', async () => {
|
|
441
462
|
await expect(`
|
|
442
463
|
run: ${factsSrc} -> {
|
|
@@ -697,6 +718,12 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
|
|
|
697
718
|
run: range + { where: t ~ f'2001' }
|
|
698
719
|
`).toMatchRows(range, inRange);
|
|
699
720
|
});
|
|
721
|
+
test('date !~ year', async () => {
|
|
722
|
+
const range = mkDateRange('2001-01-01', '2002-01-01');
|
|
723
|
+
await expect(`
|
|
724
|
+
run: range + { where: t !~ f'2001' }
|
|
725
|
+
`).toMatchRows(range, [...notInRange, {n: 'z-null'}]);
|
|
726
|
+
});
|
|
700
727
|
test('date in month, in a query time zone east of UTC', async () => {
|
|
701
728
|
// Midnight starting a month in Tokyo is still the previous day in UTC
|
|
702
729
|
const range = mkDateRange('2001-01-01', '2001-02-01');
|
|
@@ -460,7 +460,9 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
|
|
|
460
460
|
|
|
461
461
|
it('many_field.sum() has correct locality', async () => {
|
|
462
462
|
await expect(`
|
|
463
|
-
source: a is ${databaseName}.table('malloytest.aircraft')
|
|
463
|
+
source: a is ${databaseName}.table('malloytest.aircraft') extend {
|
|
464
|
+
primary_key: tail_num
|
|
465
|
+
}
|
|
464
466
|
source: am is ${databaseName}.table('malloytest.aircraft_models') extend {
|
|
465
467
|
join_many: a on a.aircraft_model_code = a.aircraft_model_code
|
|
466
468
|
dimension: a_year_built is a.year_built
|
|
@@ -867,6 +869,104 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
|
|
|
867
869
|
`).toMatchResult(testModel, {grandchild_state: 'AL'});
|
|
868
870
|
}
|
|
869
871
|
);
|
|
872
|
+
// An array is a column of the join above it: the subquery packs that
|
|
873
|
+
// column, and the array is unnested outside the subquery from it.
|
|
874
|
+
describe('an array two levels below', () => {
|
|
875
|
+
const arrayChain = (filterTwo: string) => `
|
|
876
|
+
source: sf is ${databaseName}.table('malloytest.state_facts')
|
|
877
|
+
source: three is ${databaseName}.table('malloytest.airports') -> {
|
|
878
|
+
group_by: state
|
|
879
|
+
nest: types is { group_by: fac_type; aggregate: c is count() }
|
|
880
|
+
}
|
|
881
|
+
source: two is sf extend {
|
|
882
|
+
${filterTwo}
|
|
883
|
+
join_one: three on three.state = state
|
|
884
|
+
}
|
|
885
|
+
source: one is sf extend {
|
|
886
|
+
where: state ~ 'A%'
|
|
887
|
+
join_one: two on two.state = state
|
|
888
|
+
}
|
|
889
|
+
source: zero is sf extend { join_one: one on one.state = state }
|
|
890
|
+
`;
|
|
891
|
+
|
|
892
|
+
test.when(runtime.dialect.supportsComplexFilteredSources)(
|
|
893
|
+
'read and aggregated',
|
|
894
|
+
async () => {
|
|
895
|
+
await expect(`
|
|
896
|
+
${arrayChain('')}
|
|
897
|
+
run: zero -> {
|
|
898
|
+
group_by: s is one.two.three.state
|
|
899
|
+
aggregate: n is one.two.three.types.count()
|
|
900
|
+
order_by: s
|
|
901
|
+
limit: 2
|
|
902
|
+
}
|
|
903
|
+
`).toMatchResult(testModel, {s: 'AK', n: 3}, {s: 'AL', n: 5});
|
|
904
|
+
|
|
905
|
+
await expect(`
|
|
906
|
+
${arrayChain('')}
|
|
907
|
+
run: zero -> {
|
|
908
|
+
where: one.two.three.state = 'AK'
|
|
909
|
+
group_by: fac_type is one.two.three.types.fac_type
|
|
910
|
+
order_by: fac_type
|
|
911
|
+
limit: 1
|
|
912
|
+
}
|
|
913
|
+
`).toMatchResult(testModel, {fac_type: 'AIRPORT'});
|
|
914
|
+
}
|
|
915
|
+
);
|
|
916
|
+
|
|
917
|
+
// An array the filtered join reads itself, in its `where:` or in an
|
|
918
|
+
// `on` inside it, stays inside the subquery.
|
|
919
|
+
test.when(runtime.dialect.supportsComplexFilteredSources)(
|
|
920
|
+
'read by the filtered join itself',
|
|
921
|
+
async () => {
|
|
922
|
+
const twoArrays = `
|
|
923
|
+
source: sf is ${databaseName}.table('malloytest.state_facts')
|
|
924
|
+
source: three is ${databaseName}.table('malloytest.airports') -> {
|
|
925
|
+
group_by: state
|
|
926
|
+
nest: types is { group_by: fac_type; aggregate: c is count() }
|
|
927
|
+
}
|
|
928
|
+
source: two is sf extend {
|
|
929
|
+
join_one: three on three.state = state
|
|
930
|
+
join_one: sf2 is sf on sf2.state = three.types.fac_type
|
|
931
|
+
}`;
|
|
932
|
+
// Of Alabama's facility types, only airports number over 100
|
|
933
|
+
await expect(`
|
|
934
|
+
${twoArrays}
|
|
935
|
+
source: one is two extend { where: three.types.c > 100 }
|
|
936
|
+
source: zero is sf extend { join_one: one on one.state = state }
|
|
937
|
+
run: zero -> {
|
|
938
|
+
where: state = 'AL'
|
|
939
|
+
select: state, x is one.three.state
|
|
940
|
+
}
|
|
941
|
+
`).toMatchResult(testModel, {state: 'AL', x: 'AL'});
|
|
942
|
+
// No facility type is a state's name
|
|
943
|
+
await expect(`
|
|
944
|
+
${twoArrays}
|
|
945
|
+
source: one is two extend { where: state ~ 'A%' }
|
|
946
|
+
source: zero is sf extend { join_one: one on one.state = state }
|
|
947
|
+
run: zero -> {
|
|
948
|
+
where: state = 'AL'
|
|
949
|
+
aggregate: n is count(one.sf2.state)
|
|
950
|
+
}
|
|
951
|
+
`).toMatchResult(testModel, {n: 0});
|
|
952
|
+
}
|
|
953
|
+
);
|
|
954
|
+
|
|
955
|
+
test.when(runtime.dialect.supportsComplexFilteredSources)(
|
|
956
|
+
'below two filtered joins',
|
|
957
|
+
async () => {
|
|
958
|
+
await expect(`
|
|
959
|
+
${arrayChain("where: state != 'AK'")}
|
|
960
|
+
run: zero -> {
|
|
961
|
+
group_by: s is one.two.three.state
|
|
962
|
+
aggregate: n is one.two.three.types.count()
|
|
963
|
+
order_by: s
|
|
964
|
+
limit: 1
|
|
965
|
+
}
|
|
966
|
+
`).toMatchResult(testModel, {s: 'AL', n: 5});
|
|
967
|
+
}
|
|
968
|
+
);
|
|
969
|
+
});
|
|
870
970
|
});
|
|
871
971
|
|
|
872
972
|
test('joined filtered explores with NO dependencies', async () => {
|
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
import {RuntimeList, allDatabases, testFileSpace} from '../../runtimes';
|
|
7
7
|
import {databasesFromEnvironmentOr} from '../../util';
|
|
8
8
|
import '@malloydata/malloy/test/matchers';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
extendTestModel,
|
|
11
|
+
mkTestModel,
|
|
12
|
+
TV,
|
|
13
|
+
wrapTestModel,
|
|
14
|
+
} from '@malloydata/malloy/test';
|
|
10
15
|
|
|
11
16
|
const runtimes = new RuntimeList(databasesFromEnvironmentOr(allDatabases));
|
|
12
17
|
|
|
@@ -350,4 +355,263 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
|
|
|
350
355
|
testFileSpace.deleteFile(userURL);
|
|
351
356
|
}
|
|
352
357
|
});
|
|
358
|
+
|
|
359
|
+
// Carol is the customer no order matches: in the joined result she has one
|
|
360
|
+
// row, in which the order is all NULLs.
|
|
361
|
+
describe('aggregate at a joined source with an unmatched row', () => {
|
|
362
|
+
// float, because Databricks overflows on the fanned-out sum of a 32 bit
|
|
363
|
+
// integer column.
|
|
364
|
+
const num = TV.float;
|
|
365
|
+
const unmatched = extendTestModel(
|
|
366
|
+
mkTestModel(runtime, {
|
|
367
|
+
customers: [
|
|
368
|
+
{customer_id: 1, name: 'Alice', credit: num(100)},
|
|
369
|
+
{customer_id: 2, name: 'Bob', credit: num(400)},
|
|
370
|
+
{customer_id: 3, name: 'Carol', credit: num(5000)},
|
|
371
|
+
],
|
|
372
|
+
order_rows: [
|
|
373
|
+
{order_id: 10, customer_id: 1, amount: num(5)},
|
|
374
|
+
{order_id: 11, customer_id: 1, amount: num(7)},
|
|
375
|
+
{order_id: 12, customer_id: 2, amount: num(11)},
|
|
376
|
+
],
|
|
377
|
+
tags: [
|
|
378
|
+
{customer_id: 1, tag: 'a'},
|
|
379
|
+
{customer_id: 1, tag: 'b'},
|
|
380
|
+
{customer_id: 2, tag: 'c'},
|
|
381
|
+
],
|
|
382
|
+
profiles: [
|
|
383
|
+
{customer_id: 1, score: num(7)},
|
|
384
|
+
{customer_id: 2, score: num(9)},
|
|
385
|
+
],
|
|
386
|
+
}),
|
|
387
|
+
`
|
|
388
|
+
source: keyless_orders is order_rows extend {
|
|
389
|
+
measure:
|
|
390
|
+
order_count is count()
|
|
391
|
+
padded_total is sum((amount ?? 0) + 1)
|
|
392
|
+
tripled_avg is avg((amount ?? 0) * 3)
|
|
393
|
+
}
|
|
394
|
+
source: keyed_orders is keyless_orders extend { primary_key: order_id }
|
|
395
|
+
source: buyers is customers extend {
|
|
396
|
+
primary_key: customer_id
|
|
397
|
+
join_many: keyed is keyed_orders on keyed.customer_id = customer_id
|
|
398
|
+
join_many: keyless is keyless_orders on keyless.customer_id = customer_id
|
|
399
|
+
join_many: tags on tags.customer_id = customer_id
|
|
400
|
+
join_one: keyed_profile is profiles extend {
|
|
401
|
+
primary_key: customer_id
|
|
402
|
+
} on keyed_profile.customer_id = customer_id
|
|
403
|
+
join_one: keyless_profile is profiles
|
|
404
|
+
on keyless_profile.customer_id = customer_id
|
|
405
|
+
}
|
|
406
|
+
`
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
// A second join_many fans the orders out, which changes how every
|
|
410
|
+
// aggregate at the orders is written.
|
|
411
|
+
const shapes = [
|
|
412
|
+
{orders: 'keyed', fanOut: ''},
|
|
413
|
+
{orders: 'keyed', fanOut: 'tag_count is tags.count()'},
|
|
414
|
+
{orders: 'keyless', fanOut: ''},
|
|
415
|
+
{orders: 'keyless', fanOut: 'tag_count is tags.count()'},
|
|
416
|
+
].map(s => ({...s, shape: `${s.orders}${s.fanOut ? ', fanned out' : ''}`}));
|
|
417
|
+
|
|
418
|
+
describe.each(shapes)('$shape', ({orders, fanOut}) => {
|
|
419
|
+
it('count, sum and avg skip the unmatched row', async () => {
|
|
420
|
+
await expect(`
|
|
421
|
+
run: buyers -> {
|
|
422
|
+
group_by: name
|
|
423
|
+
aggregate:
|
|
424
|
+
order_count is ${orders}.count()
|
|
425
|
+
big_credit_count is ${orders}.count() { where: credit > 100 }
|
|
426
|
+
credit_sum is ${orders}.sum(credit)
|
|
427
|
+
big_credit_sum is ${orders}.sum(credit) { where: credit > 100 }
|
|
428
|
+
credit_avg is ${orders}.avg(credit)
|
|
429
|
+
amount_plus_one is ${orders}.sum((${orders}.amount ?? 0) + 1)
|
|
430
|
+
amount_sum is ${orders}.amount.sum()
|
|
431
|
+
${fanOut}
|
|
432
|
+
order_by: name
|
|
433
|
+
}
|
|
434
|
+
`).toMatchResult(
|
|
435
|
+
unmatched,
|
|
436
|
+
{
|
|
437
|
+
name: 'Alice',
|
|
438
|
+
order_count: 2,
|
|
439
|
+
big_credit_count: 0,
|
|
440
|
+
credit_sum: 200,
|
|
441
|
+
big_credit_sum: 0,
|
|
442
|
+
credit_avg: 100,
|
|
443
|
+
amount_plus_one: 14,
|
|
444
|
+
amount_sum: 12,
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
name: 'Bob',
|
|
448
|
+
order_count: 1,
|
|
449
|
+
big_credit_count: 1,
|
|
450
|
+
credit_sum: 400,
|
|
451
|
+
big_credit_sum: 400,
|
|
452
|
+
credit_avg: 400,
|
|
453
|
+
amount_plus_one: 12,
|
|
454
|
+
amount_sum: 11,
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
name: 'Carol',
|
|
458
|
+
order_count: 0,
|
|
459
|
+
big_credit_count: 0,
|
|
460
|
+
credit_sum: 0,
|
|
461
|
+
big_credit_sum: 0,
|
|
462
|
+
credit_avg: null,
|
|
463
|
+
amount_plus_one: 0,
|
|
464
|
+
amount_sum: 0,
|
|
465
|
+
}
|
|
466
|
+
);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('a measure is the same through the join as on its own source', async () => {
|
|
470
|
+
const measures = ['order_count', 'padded_total', 'tripled_avg'];
|
|
471
|
+
const onItsOwn = await unmatched.model
|
|
472
|
+
.loadQuery(
|
|
473
|
+
`run: ${orders}_orders -> { aggregate: ${measures.join(', ')} }`
|
|
474
|
+
)
|
|
475
|
+
.run();
|
|
476
|
+
await expect(`
|
|
477
|
+
run: buyers -> {
|
|
478
|
+
aggregate:
|
|
479
|
+
${measures.map(m => `${orders}.${m}`).join('\n')}
|
|
480
|
+
${fanOut}
|
|
481
|
+
}
|
|
482
|
+
`).toMatchResult(unmatched, onItsOwn.data.toObject()[0]);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('totals skip the unmatched row', async () => {
|
|
486
|
+
await expect(`
|
|
487
|
+
run: buyers -> {
|
|
488
|
+
aggregate:
|
|
489
|
+
order_count is ${orders}.count()
|
|
490
|
+
credit_sum is ${orders}.sum(credit)
|
|
491
|
+
credit_avg is ${orders}.avg(credit)
|
|
492
|
+
customer_count is count()
|
|
493
|
+
${fanOut}
|
|
494
|
+
}
|
|
495
|
+
`).toMatchResult(unmatched, {
|
|
496
|
+
order_count: 3,
|
|
497
|
+
credit_sum: 600,
|
|
498
|
+
credit_avg: 200,
|
|
499
|
+
customer_count: 3,
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const canStringAgg =
|
|
504
|
+
fanOut === '' ||
|
|
505
|
+
!['snowflake', 'mysql', 'sqlserver'].includes(databaseName);
|
|
506
|
+
it.when(canStringAgg)(
|
|
507
|
+
'an aggregate function skips the unmatched row',
|
|
508
|
+
async () => {
|
|
509
|
+
await expect(`
|
|
510
|
+
run: buyers -> {
|
|
511
|
+
group_by: name
|
|
512
|
+
aggregate:
|
|
513
|
+
name_chars is length(${orders}.string_agg(name, ',')) ?? 0
|
|
514
|
+
${fanOut}
|
|
515
|
+
order_by: name
|
|
516
|
+
}
|
|
517
|
+
`).toMatchResult(
|
|
518
|
+
unmatched,
|
|
519
|
+
{name: 'Alice', name_chars: 11},
|
|
520
|
+
{name: 'Bob', name_chars: 3},
|
|
521
|
+
{name: 'Carol', name_chars: 0}
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
describe.each(['keyed_profile', 'keyless_profile'])(
|
|
528
|
+
'join_one %s',
|
|
529
|
+
profile => {
|
|
530
|
+
it('count, sum and avg skip the unmatched row', async () => {
|
|
531
|
+
await expect(`
|
|
532
|
+
run: buyers -> {
|
|
533
|
+
group_by: name
|
|
534
|
+
aggregate:
|
|
535
|
+
profile_count is ${profile}.count()
|
|
536
|
+
credit_sum is ${profile}.sum(credit)
|
|
537
|
+
credit_avg is ${profile}.avg(credit)
|
|
538
|
+
score_plus_one is ${profile}.sum((${profile}.score ?? 0) + 1)
|
|
539
|
+
order_by: name
|
|
540
|
+
}
|
|
541
|
+
`).toMatchResult(
|
|
542
|
+
unmatched,
|
|
543
|
+
{
|
|
544
|
+
name: 'Alice',
|
|
545
|
+
profile_count: 1,
|
|
546
|
+
credit_sum: 100,
|
|
547
|
+
credit_avg: 100,
|
|
548
|
+
score_plus_one: 8,
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
name: 'Bob',
|
|
552
|
+
profile_count: 1,
|
|
553
|
+
credit_sum: 400,
|
|
554
|
+
credit_avg: 400,
|
|
555
|
+
score_plus_one: 10,
|
|
556
|
+
},
|
|
557
|
+
{
|
|
558
|
+
name: 'Carol',
|
|
559
|
+
profile_count: 0,
|
|
560
|
+
credit_sum: 0,
|
|
561
|
+
credit_avg: null,
|
|
562
|
+
score_plus_one: 0,
|
|
563
|
+
}
|
|
564
|
+
);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
// An empty array unnests to one unmatched row.
|
|
570
|
+
it.when(databaseName === 'duckdb')(
|
|
571
|
+
'an aggregate at an array skips an empty array',
|
|
572
|
+
async () => {
|
|
573
|
+
await expect(`
|
|
574
|
+
run: ${databaseName}.sql("""
|
|
575
|
+
SELECT 'Alice' as name, 100 as credit, [5, 7] as amounts
|
|
576
|
+
UNION ALL SELECT 'Carol', 5000, []::INTEGER[]
|
|
577
|
+
""") -> {
|
|
578
|
+
group_by: name
|
|
579
|
+
aggregate:
|
|
580
|
+
amount_count is amounts.count()
|
|
581
|
+
credit_sum is amounts.sum(credit)
|
|
582
|
+
credit_avg is amounts.avg(credit)
|
|
583
|
+
order_by: name
|
|
584
|
+
}
|
|
585
|
+
`).toMatchResult(
|
|
586
|
+
unmatched,
|
|
587
|
+
{name: 'Alice', amount_count: 2, credit_sum: 200, credit_avg: 100},
|
|
588
|
+
{name: 'Carol', amount_count: 0, credit_sum: 0, credit_avg: null}
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
);
|
|
592
|
+
|
|
593
|
+
// A right join leaves the query's own source unmatched: Carol's row has
|
|
594
|
+
// no order in it.
|
|
595
|
+
it('the query source skips its own unmatched row', async () => {
|
|
596
|
+
await expect(`
|
|
597
|
+
##! experimental.join_types
|
|
598
|
+
run: keyed_orders -> {
|
|
599
|
+
extend: {
|
|
600
|
+
join_one: customers right on customers.customer_id = customer_id
|
|
601
|
+
}
|
|
602
|
+
group_by: customers.name
|
|
603
|
+
aggregate:
|
|
604
|
+
order_count is count()
|
|
605
|
+
ones is sum(1)
|
|
606
|
+
amount_avg is avg(amount ?? 0)
|
|
607
|
+
order_by: name
|
|
608
|
+
}
|
|
609
|
+
`).toMatchResult(
|
|
610
|
+
unmatched,
|
|
611
|
+
{name: 'Alice', order_count: 2, ones: 2, amount_avg: 6},
|
|
612
|
+
{name: 'Bob', order_count: 1, ones: 1, amount_avg: 11},
|
|
613
|
+
{name: 'Carol', order_count: 0, ones: 0, amount_avg: null}
|
|
614
|
+
);
|
|
615
|
+
});
|
|
616
|
+
});
|
|
353
617
|
});
|
|
@@ -234,4 +234,38 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
|
|
|
234
234
|
expect(row['capped']).not.toBe(null);
|
|
235
235
|
}
|
|
236
236
|
);
|
|
237
|
+
|
|
238
|
+
// Alaska's three facility types: 474 airports, 104 seaplane bases and 30
|
|
239
|
+
// heliports.
|
|
240
|
+
const alaskaByFacType = (nest: string) => `
|
|
241
|
+
run: ${databaseName}.table('malloytest.airports') -> {
|
|
242
|
+
where: state = 'AK'
|
|
243
|
+
group_by: state
|
|
244
|
+
nest: by_fac_type is { ${nest} }
|
|
245
|
+
}`;
|
|
246
|
+
|
|
247
|
+
// Counting a nest's elements reads each element's distinct key back out of
|
|
248
|
+
// the nest.
|
|
249
|
+
test.when(runtime.supportsNesting)(
|
|
250
|
+
`the elements of a nest can be counted in the next stage - ${databaseName}`,
|
|
251
|
+
async () => {
|
|
252
|
+
await expect(`
|
|
253
|
+
${alaskaByFacType('group_by: fac_type; aggregate: c is count()')} -> {
|
|
254
|
+
aggregate:
|
|
255
|
+
fac_types is by_fac_type.count()
|
|
256
|
+
airports is by_fac_type.c.sum()
|
|
257
|
+
}`).toMatchResult(model, {fac_types: 3, airports: 608});
|
|
258
|
+
}
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
test.when(runtime.supportsNesting)(
|
|
262
|
+
`a boolean read back out of a nest is a boolean - ${databaseName}`,
|
|
263
|
+
async () => {
|
|
264
|
+
await expect(`
|
|
265
|
+
${alaskaByFacType("group_by: fac_type, is_airport is fac_type = 'AIRPORT'")} -> {
|
|
266
|
+
where: by_fac_type.is_airport = true
|
|
267
|
+
group_by: by_fac_type.fac_type
|
|
268
|
+
}`).toEqualResult(model, [{fac_type: 'AIRPORT'}]);
|
|
269
|
+
}
|
|
270
|
+
);
|
|
237
271
|
});
|
|
@@ -263,7 +263,11 @@ describe.each(runtimes.runtimeList)(
|
|
|
263
263
|
aggregate: aircraft.aircraft_count
|
|
264
264
|
aggregate: model_count
|
|
265
265
|
}
|
|
266
|
-
`).toMatchResult(joinTestModel, {
|
|
266
|
+
`).toMatchResult(joinTestModel, {
|
|
267
|
+
state: 'CA',
|
|
268
|
+
aircraft_count: 367,
|
|
269
|
+
model_count: 277,
|
|
270
|
+
});
|
|
267
271
|
});
|
|
268
272
|
|
|
269
273
|
it(`hand join symmetric agg - ${databaseName}`, async () => {
|
|
@@ -274,7 +278,7 @@ describe.each(runtimes.runtimeList)(
|
|
|
274
278
|
}
|
|
275
279
|
`).toMatchResult(joinTestModel, {
|
|
276
280
|
total_seats: 452415,
|
|
277
|
-
aircraft_count:
|
|
281
|
+
aircraft_count: 3599,
|
|
278
282
|
});
|
|
279
283
|
});
|
|
280
284
|
}
|
|
@@ -90,7 +90,7 @@ describe('Wildcard BigQuery Tables', () => {
|
|
|
90
90
|
{state: 'OK', aircraft_count: 1},
|
|
91
91
|
{state: 'OR', aircraft_count: 2},
|
|
92
92
|
{state: 'TX', aircraft_count: 1},
|
|
93
|
-
{state: null, aircraft_count:
|
|
93
|
+
{state: null, aircraft_count: 0},
|
|
94
94
|
]);
|
|
95
95
|
}
|
|
96
96
|
});
|
|
@@ -123,7 +123,7 @@ describe('Wildcard BigQuery Tables', () => {
|
|
|
123
123
|
{state: 'LA', aircraft_count: 1},
|
|
124
124
|
{state: 'OK', aircraft_count: 1},
|
|
125
125
|
{state: 'OR', aircraft_count: 1},
|
|
126
|
-
{state: null, aircraft_count:
|
|
126
|
+
{state: null, aircraft_count: 0},
|
|
127
127
|
]);
|
|
128
128
|
}
|
|
129
129
|
});
|
|
@@ -180,7 +180,7 @@ describe('Wildcard BigQuery Tables', () => {
|
|
|
180
180
|
.run();
|
|
181
181
|
expect(result.data.value).toStrictEqual([
|
|
182
182
|
{_TABLE_SUFFIX: '02', aircraft_count: 4},
|
|
183
|
-
{_TABLE_SUFFIX: null, aircraft_count:
|
|
183
|
+
{_TABLE_SUFFIX: null, aircraft_count: 0},
|
|
184
184
|
]);
|
|
185
185
|
}
|
|
186
186
|
});
|
package/src/runtimes.ts
CHANGED
|
@@ -296,6 +296,7 @@ export function runtimeFor(dbName: string): SingleConnectionRuntime {
|
|
|
296
296
|
token: process.env['DATABRICKS_TOKEN'],
|
|
297
297
|
defaultCatalog: process.env['DATABRICKS_CATALOG'],
|
|
298
298
|
defaultSchema: process.env['DATABRICKS_SCHEMA'],
|
|
299
|
+
proxy: process.env['DATABRICKS_HTTPS_PROXY'],
|
|
299
300
|
});
|
|
300
301
|
}
|
|
301
302
|
break;
|