@malloydata/malloy-tests 0.0.67 → 0.0.68-dev230808194809

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.
Files changed (81) hide show
  1. package/dist/databases/all/db_index.spec.d.ts +1 -0
  2. package/dist/databases/all/db_index.spec.js +36 -0
  3. package/dist/databases/all/db_index.spec.js.map +1 -0
  4. package/dist/databases/all/expr.spec.d.ts +0 -2
  5. package/dist/databases/all/expr.spec.js +2 -547
  6. package/dist/databases/all/expr.spec.js.map +1 -1
  7. package/dist/databases/all/functions.spec.d.ts +1 -2
  8. package/dist/databases/all/functions.spec.js +2 -751
  9. package/dist/databases/all/functions.spec.js.map +1 -1
  10. package/dist/databases/all/join.spec.d.ts +1 -2
  11. package/dist/databases/all/join.spec.js +2 -276
  12. package/dist/databases/all/join.spec.js.map +1 -1
  13. package/dist/databases/all/nomodel.spec.d.ts +1 -2
  14. package/dist/databases/all/nomodel.spec.js +2 -923
  15. package/dist/databases/all/nomodel.spec.js.map +1 -1
  16. package/dist/databases/all/orderby.spec.d.ts +1 -2
  17. package/dist/databases/all/orderby.spec.js +2 -191
  18. package/dist/databases/all/orderby.spec.js.map +1 -1
  19. package/dist/databases/all/problems.spec.d.ts +1 -2
  20. package/dist/databases/all/problems.spec.js +2 -80
  21. package/dist/databases/all/problems.spec.js.map +1 -1
  22. package/dist/databases/all/sql_expressions.spec.d.ts +1 -2
  23. package/dist/databases/all/sql_expressions.spec.js +2 -62
  24. package/dist/databases/all/sql_expressions.spec.js.map +1 -1
  25. package/dist/databases/all/time.spec.d.ts +1 -3
  26. package/dist/databases/all/time.spec.js +2 -615
  27. package/dist/databases/all/time.spec.js.map +1 -1
  28. package/dist/databases/{all/index.spec.d.ts → shared/db_index.d.ts} +1 -0
  29. package/dist/databases/{all/index.spec.js → shared/db_index.js} +2 -33
  30. package/dist/databases/shared/db_index.js.map +1 -0
  31. package/dist/databases/shared/expr.d.ts +3 -0
  32. package/dist/databases/shared/expr.js +551 -0
  33. package/dist/databases/shared/expr.js.map +1 -0
  34. package/dist/databases/shared/functions.d.ts +3 -0
  35. package/dist/databases/shared/functions.js +754 -0
  36. package/dist/databases/shared/functions.js.map +1 -0
  37. package/dist/databases/shared/join.d.ts +3 -0
  38. package/dist/databases/shared/join.js +302 -0
  39. package/dist/databases/shared/join.js.map +1 -0
  40. package/dist/databases/shared/nomodel.d.ts +3 -0
  41. package/dist/databases/shared/nomodel.js +950 -0
  42. package/dist/databases/shared/nomodel.js.map +1 -0
  43. package/dist/databases/shared/orderby.d.ts +3 -0
  44. package/dist/databases/shared/orderby.js +217 -0
  45. package/dist/databases/shared/orderby.js.map +1 -0
  46. package/dist/databases/shared/problems.d.ts +3 -0
  47. package/dist/databases/shared/problems.js +106 -0
  48. package/dist/databases/shared/problems.js.map +1 -0
  49. package/dist/databases/shared/sql_expressions.d.ts +3 -0
  50. package/dist/databases/shared/sql_expressions.js +88 -0
  51. package/dist/databases/shared/sql_expressions.js.map +1 -0
  52. package/dist/databases/shared/test_list.js +18 -18
  53. package/dist/databases/shared/test_list.js.map +1 -1
  54. package/dist/databases/shared/time.d.ts +3 -0
  55. package/dist/databases/shared/time.js +640 -0
  56. package/dist/databases/shared/time.js.map +1 -0
  57. package/dist/index.d.ts +9 -10
  58. package/dist/index.js +19 -21
  59. package/dist/index.js.map +1 -1
  60. package/package.json +6 -6
  61. package/src/databases/all/db_index.spec.ts +37 -0
  62. package/src/databases/all/expr.spec.ts +2 -670
  63. package/src/databases/all/functions.spec.ts +1 -1101
  64. package/src/databases/all/join.spec.ts +1 -315
  65. package/src/databases/all/nomodel.spec.ts +2 -1124
  66. package/src/databases/all/orderby.spec.ts +1 -234
  67. package/src/databases/all/problems.spec.ts +1 -87
  68. package/src/databases/all/sql_expressions.spec.ts +1 -71
  69. package/src/databases/all/time.spec.ts +1 -761
  70. package/src/databases/{all/index.spec.ts → shared/db_index.ts} +2 -13
  71. package/src/databases/shared/expr.ts +695 -0
  72. package/src/databases/shared/functions.ts +1126 -0
  73. package/src/databases/shared/join.ts +340 -0
  74. package/src/databases/shared/nomodel.ts +1150 -0
  75. package/src/databases/shared/orderby.ts +260 -0
  76. package/src/databases/shared/problems.ts +113 -0
  77. package/src/databases/shared/sql_expressions.ts +96 -0
  78. package/src/databases/shared/test_list.ts +9 -9
  79. package/src/databases/shared/time.ts +786 -0
  80. package/src/index.ts +10 -11
  81. package/dist/databases/all/index.spec.js.map +0 -1
@@ -24,1107 +24,7 @@
24
24
 
25
25
  import {RuntimeList, allDatabases} from '../../runtimes';
26
26
  import {databasesFromEnvironmentOr} from '../../util';
27
- import * as malloy from '@malloydata/malloy';
28
-
29
- export const functionsSharedTests = (
30
- runtimes: RuntimeList,
31
- _splitFunction?: (column: string, splitChar: string) => string
32
- ) => {
33
- const expressionModelText = `
34
- source: aircraft_models is table('malloytest.aircraft_models'){
35
- primary_key: aircraft_model_code
36
- }
37
-
38
- source: aircraft is table('malloytest.aircraft'){
39
- primary_key: tail_num
40
- join_one: aircraft_models with aircraft_model_code
41
- measure: aircraft_count is count()
42
- }
43
-
44
- source: airports is table('malloytest.airports') {}
45
-
46
- source: state_facts is table('malloytest.state_facts') {}
47
- `;
48
-
49
- const expressionModels = new Map<string, malloy.ModelMaterializer>();
50
- runtimes.runtimeMap.forEach((runtime, databaseName) =>
51
- expressionModels.set(databaseName, runtime.loadModel(expressionModelText))
52
- );
53
-
54
- expressionModels.forEach((expressionModel, databaseName) => {
55
- const funcTestGeneral = async (
56
- expr: string,
57
- type: 'group_by' | 'aggregate',
58
- expected:
59
- | {error: string; success?: undefined}
60
- | {success: string | boolean | number | null; error?: undefined}
61
- ) => {
62
- const run = async () => {
63
- return await expressionModel
64
- .loadQuery(
65
- `
66
- query: aircraft -> { ${type}: f is ${expr} }`
67
- )
68
- .run();
69
- };
70
-
71
- if (expected.success !== undefined) {
72
- const result = await run();
73
- expect(result.data.path(0, 'f').value).toBe(expected.success);
74
- } else {
75
- expect(run).rejects.toThrowError(expected.error);
76
- }
77
- };
78
-
79
- const funcTest = (
80
- expr: string,
81
- expexted: string | boolean | number | null
82
- ) => funcTestGeneral(expr, 'group_by', {success: expexted});
83
-
84
- const funcTestAgg = (
85
- expr: string,
86
- expexted: string | boolean | number | null
87
- ) => funcTestGeneral(expr, 'aggregate', {success: expexted});
88
-
89
- const funcTestMultiple = async (
90
- ...testCases: [string, string | boolean | number | null][]
91
- ) => {
92
- const run = async () => {
93
- return await expressionModel
94
- .loadQuery(
95
- `
96
- query: aircraft -> { ${testCases.map(
97
- (testCase, i) => `group_by: f${i} is ${testCase[0]}`
98
- )} }`
99
- )
100
- .run();
101
- };
102
-
103
- const result = await run();
104
- testCases.forEach((testCase, i) => {
105
- expect(result.data.path(0, `f${i}`).value).toBe(testCase[1]);
106
- });
107
- };
108
-
109
- describe('concat', () => {
110
- it(`works - ${databaseName}`, async () => {
111
- await funcTestMultiple(
112
- ["concat('foo', 'bar')", 'foobar'],
113
- ["concat(1, 'bar')", '1bar'],
114
- [
115
- "concat('cons', true)",
116
- databaseName === 'postgres' ? 'const' : 'construe',
117
- ],
118
- ["concat('foo', @2003)", 'foo2003-01-01'],
119
- [
120
- "concat('foo', @2003-01-01 12:00:00)",
121
- databaseName === 'bigquery'
122
- ? 'foo2003-01-01 12:00:00+00'
123
- : 'foo2003-01-01 12:00:00',
124
- ],
125
- // TODO Maybe implement consistent null behavior
126
- // ["concat('foo', null)", null],
127
- ['concat()', '']
128
- );
129
- });
130
- });
131
-
132
- describe('round', () => {
133
- it(`works - ${databaseName}`, async () => {
134
- await funcTestMultiple(
135
- ['round(1.2)', 1],
136
- // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
137
- // that are fixed in 0.8.
138
- ...(databaseName === 'duckdb_wasm'
139
- ? []
140
- : ([['round(12.222, 1)', 12.2]] as [string, number][])),
141
- ['round(12.2, -1)', 10],
142
- ['round(null)', null],
143
- ['round(1, null)', null]
144
- );
145
- });
146
- });
147
-
148
- describe('floor', () => {
149
- it(`works - ${databaseName}`, async () => {
150
- await funcTestMultiple(
151
- ['floor(1.9)', 1],
152
- // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
153
- // that are fixed in 0.8.
154
- ...(databaseName === 'duckdb_wasm'
155
- ? []
156
- : ([['floor(-1.9)', -2]] as [string, number][])),
157
- ['floor(null)', null]
158
- );
159
- await funcTest('floor(1.9)', 1);
160
- });
161
- });
162
-
163
- describe('ceil', () => {
164
- it(`works - ${databaseName}`, async () => {
165
- await funcTestMultiple(
166
- ['ceil(1.9)', 2],
167
- // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
168
- // that are fixed in 0.8.
169
- ...(databaseName === 'duckdb_wasm'
170
- ? []
171
- : ([['ceil(-1.9)', -1]] as [string, number][])),
172
- ['ceil(null)', null]
173
- );
174
- });
175
- });
176
-
177
- describe('length', () => {
178
- it(`works - ${databaseName}`, async () => {
179
- await funcTestMultiple(["length('foo')", 3], ['length(null)', null]);
180
- });
181
- });
182
-
183
- describe('lower', () => {
184
- it(`works - ${databaseName}`, async () => {
185
- await funcTestMultiple(["lower('FoO')", 'foo'], ['lower(null)', null]);
186
- });
187
- });
188
-
189
- describe('upper', () => {
190
- it(`works - ${databaseName}`, async () => {
191
- await funcTestMultiple(["upper('fOo')", 'FOO'], ['upper(null)', null]);
192
- });
193
- });
194
-
195
- describe('regexp_extract', () => {
196
- it(`works - ${databaseName}`, async () => {
197
- await funcTestMultiple(
198
- ["regexp_extract('I have a dog', r'd[aeiou]g')", 'dog'],
199
- ["regexp_extract(null, r'd[aeiou]g')", null],
200
- ["regexp_extract('foo', null)", null],
201
- ["regexp_extract('I have a d0g', r'd\\dg')", 'd0g']
202
- );
203
- });
204
- });
205
-
206
- describe('replace', () => {
207
- it(`works - ${databaseName}`, async () => {
208
- await funcTestMultiple(
209
- ["replace('aaaa', 'a', 'c')", 'cccc'],
210
- ["replace('aaaa', r'.', 'c')", 'cccc'],
211
- [
212
- "replace('axbxc', r'(a).(b).(c)', '\\\\0 - \\\\1 - \\\\2 - \\\\3')",
213
- databaseName === 'postgres'
214
- ? '\\0 - a - b - c'
215
- : 'axbxc - a - b - c',
216
- ],
217
- ["replace('aaaa', '', 'c')", 'aaaa'],
218
- ["replace(null, 'a', 'c')", null],
219
- ["replace('aaaa', null, 'c')", null],
220
- ["replace('aaaa', 'a', null)", null]
221
- );
222
- });
223
- });
224
-
225
- describe('substr', () => {
226
- it(`works - ${databaseName}`, async () => {
227
- await funcTestMultiple(
228
- ["substr('foo', 2)", 'oo'],
229
- ["substr('foo', 2, 1)", 'o'],
230
- ["substr('foo bar baz', -3)", 'baz'],
231
- ['substr(null, 1, 2)', null],
232
- ["substr('aaaa', null, 1)", null],
233
- ["substr('aaaa', 1, null)", null]
234
- );
235
- });
236
- });
237
-
238
- describe('raw function call', () => {
239
- it(`works - ${databaseName}`, async () => {
240
- await funcTestMultiple(
241
- ['floor(cbrt!(27)::number)', 3],
242
- ['floor(cbrt!number(27))', 3],
243
- ["substr('foo bar baz', -3)", 'baz'],
244
- ['substr(null, 1, 2)', null],
245
- ["substr('aaaa', null, 1)", null],
246
- ["substr('aaaa', 1, null)", null]
247
- );
248
- });
249
- });
250
-
251
- describe('stddev', () => {
252
- // TODO symmetric aggregates don't work with custom aggregate functions in BQ currently
253
- if (databaseName === 'bigquery') return;
254
- it(`works - ${databaseName}`, async () => {
255
- await funcTestAgg('round(stddev(aircraft_models.seats))', 29);
256
- });
257
-
258
- it(`works with struct - ${databaseName}`, async () => {
259
- await funcTestAgg(
260
- 'round(aircraft_models.stddev(aircraft_models.seats))',
261
- 41
262
- );
263
- });
264
-
265
- it(`works with implicit parameter - ${databaseName}`, async () => {
266
- await funcTestAgg('round(aircraft_models.seats.stddev())', 41);
267
- });
268
-
269
- it(`works with filter - ${databaseName}`, async () => {
270
- await funcTestAgg(
271
- 'round(aircraft_models.seats.stddev() { where: 1 = 1 })',
272
- 41
273
- );
274
- await funcTestAgg(
275
- 'round(aircraft_models.seats.stddev() { where: aircraft_models.seats > 4 })',
276
- 69
277
- );
278
- });
279
- });
280
-
281
- describe('row_number', () => {
282
- it(`works when the order by is a dimension - ${databaseName}`, async () => {
283
- const result = await expressionModel
284
- .loadQuery(
285
- `query: state_facts -> {
286
- group_by: state
287
- calculate: row_num is row_number()
288
- }`
289
- )
290
- .run();
291
- expect(result.data.path(0, 'row_num').value).toBe(1);
292
- expect(result.data.path(1, 'row_num').value).toBe(2);
293
- });
294
-
295
- it(`works when the order by is a dimension in the other order - ${databaseName}`, async () => {
296
- const result = await expressionModel
297
- .loadQuery(
298
- `query: state_facts -> {
299
- calculate: row_num is row_number()
300
- group_by: state
301
- }`
302
- )
303
- .run();
304
- expect(result.data.path(0, 'row_num').value).toBe(1);
305
- expect(result.data.path(1, 'row_num').value).toBe(2);
306
- });
307
-
308
- it(`works when the order by is a measure - ${databaseName}`, async () => {
309
- const result = await expressionModel
310
- .loadQuery(
311
- `query: state_facts -> {
312
- group_by: popular_name
313
- aggregate: c is count()
314
- calculate: row_num is row_number()
315
- }`
316
- )
317
- .run();
318
- expect(result.data.path(0, 'row_num').value).toBe(1);
319
- expect(result.data.path(1, 'row_num').value).toBe(2);
320
- });
321
-
322
- it(`works when the order by is a measure but there is no group by - ${databaseName}`, async () => {
323
- const result = await expressionModel
324
- .loadQuery(
325
- `query: state_facts -> {
326
- aggregate: c is count()
327
- calculate: row_num is row_number()
328
- }`
329
- )
330
- .run();
331
- expect(result.data.path(0, 'row_num').value).toBe(1);
332
- });
333
-
334
- it(`works inside nest - ${databaseName}`, async () => {
335
- const result = await expressionModel
336
- .loadQuery(
337
- `query: state_facts { join_one: airports on airports.state = state } -> {
338
- group_by: state
339
- nest: q is {
340
- group_by: airports.county
341
- calculate: row_num is row_number()
342
- }
343
- }`
344
- )
345
- .run();
346
- expect(result.data.path(0, 'q', 0, 'row_num').value).toBe(1);
347
- expect(result.data.path(0, 'q', 1, 'row_num').value).toBe(2);
348
- expect(result.data.path(1, 'q', 0, 'row_num').value).toBe(1);
349
- expect(result.data.path(1, 'q', 1, 'row_num').value).toBe(2);
350
- });
351
-
352
- test(`works outside nest, but with a nest nearby - ${databaseName}`, async () => {
353
- const result = await expressionModel
354
- .loadQuery(
355
- `query: state_facts -> {
356
- group_by: state
357
- calculate: row_num is row_number()
358
- nest: nested is {
359
- group_by: state
360
- }
361
- }`
362
- )
363
- .run();
364
- expect(result.data.path(0, 'row_num').value).toBe(1);
365
- expect(result.data.path(1, 'row_num').value).toBe(2);
366
- });
367
- });
368
-
369
- describe('rank', () => {
370
- it(`works ordered by dimension - ${databaseName}`, async () => {
371
- const result = await expressionModel
372
- .loadQuery(
373
- `query: state_facts -> {
374
- group_by:
375
- state,
376
- births_ballpark is ceil(births / 1000000) * 1000000
377
- order_by: births_ballpark desc
378
- calculate: births_ballpark_rank is rank()
379
- limit: 20
380
- }`
381
- )
382
- .run({rowLimit: 20});
383
- expect(result.data.path(0, 'births_ballpark_rank').value).toBe(1);
384
- expect(result.data.path(1, 'births_ballpark_rank').value).toBe(2);
385
- expect(result.data.path(8, 'births_ballpark_rank').value).toBe(9);
386
- expect(result.data.path(9, 'births_ballpark_rank').value).toBe(9);
387
- expect(result.data.path(10, 'births_ballpark_rank').value).toBe(9);
388
- expect(result.data.path(11, 'births_ballpark_rank').value).toBe(12);
389
- });
390
-
391
- it(`works ordered by aggregate - ${databaseName}`, async () => {
392
- const result = await expressionModel
393
- .loadQuery(
394
- `query: state_facts -> {
395
- group_by: first_letter is substr(state, 1, 1)
396
- aggregate: states_with_first_letter_ish is round(count() / 2) * 2
397
- calculate: r is rank()
398
- }`
399
- )
400
- .run();
401
- expect(result.data.path(0, 'r').value).toBe(1);
402
- expect(result.data.path(1, 'r').value).toBe(1);
403
- expect(result.data.path(2, 'r').value).toBe(3);
404
- expect(result.data.path(3, 'r').value).toBe(3);
405
- });
406
- });
407
-
408
- describe('lag', () => {
409
- it(`works with one param - ${databaseName}`, async () => {
410
- const result = await expressionModel
411
- .loadQuery(
412
- `query: state_facts -> {
413
- group_by: state
414
- calculate: prev_state is lag(state)
415
- }`
416
- )
417
- .run();
418
- expect(result.data.path(0, 'state').value).toBe('AK');
419
- expect(result.data.path(0, 'prev_state').value).toBe(null);
420
- expect(result.data.path(1, 'prev_state').value).toBe('AK');
421
- expect(result.data.path(1, 'state').value).toBe('AL');
422
- expect(result.data.path(2, 'prev_state').value).toBe('AL');
423
- });
424
-
425
- it(`works with expression field - ${databaseName}`, async () => {
426
- const result = await expressionModel
427
- .loadQuery(
428
- `query: state_facts -> {
429
- group_by: lower_state is lower(state)
430
- calculate: prev_state is lag(lower_state)
431
- }`
432
- )
433
- .run();
434
- expect(result.data.path(0, 'lower_state').value).toBe('ak');
435
- expect(result.data.path(0, 'prev_state').value).toBe(null);
436
- expect(result.data.path(1, 'prev_state').value).toBe('ak');
437
- expect(result.data.path(1, 'lower_state').value).toBe('al');
438
- expect(result.data.path(2, 'prev_state').value).toBe('al');
439
- });
440
-
441
- it(`works with expression - ${databaseName}`, async () => {
442
- const result = await expressionModel
443
- .loadQuery(
444
- `query: state_facts -> {
445
- group_by: state
446
- calculate: prev_state is lag(lower(state))
447
- }`
448
- )
449
- .run();
450
- expect(result.data.path(0, 'state').value).toBe('AK');
451
- expect(result.data.path(0, 'prev_state').value).toBe(null);
452
- expect(result.data.path(1, 'prev_state').value).toBe('ak');
453
- expect(result.data.path(1, 'state').value).toBe('AL');
454
- expect(result.data.path(2, 'prev_state').value).toBe('al');
455
- });
456
-
457
- it(`works with field, ordering by expression field - ${databaseName}`, async () => {
458
- const result = await expressionModel
459
- .loadQuery(
460
- `query: state_facts -> {
461
- group_by: lower_state is lower(state)
462
- aggregate: c is count()
463
- order_by: lower_state
464
- calculate: prev_count is lag(c)
465
- }`
466
- )
467
- .run();
468
- expect(result.data.path(0, 'lower_state').value).toBe('ak');
469
- expect(result.data.path(0, 'prev_count').value).toBe(null);
470
- expect(result.data.path(1, 'prev_count').value).toBe(1);
471
- expect(result.data.path(1, 'lower_state').value).toBe('al');
472
- expect(result.data.path(2, 'prev_count').value).toBe(1);
473
- });
474
-
475
- it(`works with offset - ${databaseName}`, async () => {
476
- const result = await expressionModel
477
- .loadQuery(
478
- `query: state_facts -> {
479
- group_by: state
480
- calculate: prev_prev_state is lag(state, 2)
481
- }`
482
- )
483
- .run();
484
- expect(result.data.path(0, 'state').value).toBe('AK');
485
- expect(result.data.path(0, 'prev_prev_state').value).toBe(null);
486
- expect(result.data.path(1, 'prev_prev_state').value).toBe(null);
487
- expect(result.data.path(2, 'prev_prev_state').value).toBe('AK');
488
- expect(result.data.path(1, 'state').value).toBe('AL');
489
- expect(result.data.path(3, 'prev_prev_state').value).toBe('AL');
490
- });
491
-
492
- it(`works with default value - ${databaseName}`, async () => {
493
- const result = await expressionModel
494
- .loadQuery(
495
- `query: state_facts -> {
496
- group_by: state
497
- calculate: prev_state is lag(state, 1, 'NONE')
498
- }`
499
- )
500
- .run();
501
- expect(result.data.path(0, 'prev_state').value).toBe('NONE');
502
- });
503
-
504
- it(`works with now as the default value - ${databaseName}`, async () => {
505
- const result = await expressionModel
506
- .loadQuery(
507
- `
508
- query: state_facts -> {
509
- group_by: state
510
- calculate: lag_val is lag(@2011-11-11 11:11:11, 1, now).year = now.year
511
- }`
512
- )
513
- .run();
514
- expect(result.data.path(0, 'lag_val').value).toBe(true);
515
- expect(result.data.path(1, 'lag_val').value).toBe(false);
516
- });
517
- });
518
-
519
- describe('output field in calculate', () => {
520
- it(`dotted aggregates work with an output field - ${databaseName}`, async () => {
521
- const result = await expressionModel
522
- .loadQuery(
523
- `query: aircraft -> {
524
- group_by: aircraft_models.seats
525
- aggregate: s is aircraft_models.seats.sum()
526
- calculate: a is lag(seats.sum())
527
- }`
528
- )
529
- .run();
530
- expect(result.data.path(1, 'a').value).toBe(
531
- result.data.path(0, 's').value
532
- );
533
- });
534
- });
535
-
536
- describe('first_value', () => {
537
- test(`works in nest - ${databaseName}`, async () => {
538
- const result = await expressionModel
539
- .loadQuery(
540
- `
541
- query: aircraft -> {
542
- group_by: state
543
- where: state != null
544
- nest: by_county is {
545
- limit: 2
546
- group_by: county
547
- aggregate: aircraft_count
548
- calculate: first_count is first_value(count())
549
- }
550
- }`
551
- )
552
- .run();
553
- expect(result.data.path(0, 'by_county', 1, 'first_count').value).toBe(
554
- result.data.path(0, 'by_county', 0, 'aircraft_count').value
555
- );
556
- expect(result.data.path(1, 'by_county', 1, 'first_count').value).toBe(
557
- result.data.path(1, 'by_county', 0, 'aircraft_count').value
558
- );
559
- });
560
- it(`works outside nest - ${databaseName}`, async () => {
561
- const result = await expressionModel
562
- .loadQuery(
563
- `
564
- query: state_facts -> {
565
- group_by: state, births
566
- order_by: births desc
567
- calculate: most_births is first_value(births)
568
- }`
569
- )
570
- .run();
571
- const firstBirths = result.data.path(0, 'births').value;
572
- expect(result.data.path(0, 'most_births').value).toBe(firstBirths);
573
- expect(result.data.path(1, 'most_births').value).toBe(firstBirths);
574
- });
575
- it(`works with an aggregate which is not in the query - ${databaseName}`, async () => {
576
- const result = await expressionModel
577
- .loadQuery(
578
- `
579
- query: airports { measure: airport_count is count() } -> {
580
- group_by: state
581
- where: state != null
582
- calculate: prev_airport_count is lag(airport_count)
583
- }`
584
- )
585
- .run();
586
- expect(result.data.path(0, 'prev_airport_count').value).toBe(null);
587
- expect(result.data.path(1, 'prev_airport_count').value).toBe(608);
588
- expect(result.data.path(2, 'prev_airport_count').value).toBe(260);
589
- });
590
- it(`works with a localized aggregate - ${databaseName}`, async () => {
591
- const result = await expressionModel
592
- .loadQuery(
593
- `
594
- query: aircraft -> {
595
- group_by: aircraft_models.seats,
596
- calculate: prev_sum_of_seats is lag(aircraft_models.seats.sum())
597
- }`
598
- )
599
- .run();
600
- expect(result.data.path(0, 'prev_sum_of_seats').value).toBe(null);
601
- expect(result.data.path(1, 'prev_sum_of_seats').value).toBe(0);
602
- expect(result.data.path(2, 'prev_sum_of_seats').value).toBe(230);
603
- });
604
- });
605
-
606
- describe('trunc', () => {
607
- it(`works - ${databaseName}`, async () => {
608
- await funcTestMultiple(
609
- ['trunc(1.9)', 1],
610
- // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
611
- // that are fixed in 0.8.
612
- ...(databaseName === 'duckdb_wasm'
613
- ? []
614
- : ([['trunc(-1.9)', -1]] as [string, number][])),
615
- ['trunc(12.29, 1)', 12.2],
616
- ['trunc(19.2, -1)', 10],
617
- ['trunc(null)', null],
618
- ['trunc(1, null)', null]
619
- );
620
- });
621
- });
622
- describe('log', () => {
623
- it(`works - ${databaseName}`, async () => {
624
- await funcTestMultiple(
625
- ['log(10, 10)', 1],
626
- ['log(100, 10)', 2],
627
- ['log(32, 2)', 5],
628
- ['log(null, 2)', null],
629
- ['log(10, null)', null]
630
- );
631
- });
632
- });
633
- describe('ln', () => {
634
- it(`works - ${databaseName}`, async () => {
635
- await funcTestMultiple(
636
- ['ln(exp(1))', 1],
637
- ['ln(exp(2))', 2],
638
- ['ln(null)', null]
639
- );
640
- });
641
- });
642
- describe('exp', () => {
643
- it(`works - ${databaseName}`, async () => {
644
- await funcTestMultiple(
645
- ['exp(0)', 1],
646
- ['ln(exp(1))', 1],
647
- ['exp(null)', null]
648
- );
649
- });
650
- });
651
-
652
- // TODO trig functions could have more exhaustive tests -- these are mostly just here to
653
- // ensure they exist
654
- describe('cos', () => {
655
- it(`works - ${databaseName}`, async () => {
656
- await funcTestMultiple(['cos(0)', 1], ['cos(null)', null]);
657
- });
658
- });
659
- describe('acos', () => {
660
- it(`works - ${databaseName}`, async () => {
661
- await funcTestMultiple(['acos(1)', 0], ['acos(null)', null]);
662
- });
663
- });
664
-
665
- describe('sin', () => {
666
- it(`works - ${databaseName}`, async () => {
667
- await funcTestMultiple(['sin(0)', 0], ['sin(null)', null]);
668
- });
669
- });
670
- describe('asin', () => {
671
- it(`works - ${databaseName}`, async () => {
672
- await funcTestMultiple(['asin(0)', 0], ['asin(null)', null]);
673
- });
674
- });
675
-
676
- describe('tan', () => {
677
- it(`works - ${databaseName}`, async () => {
678
- await funcTestMultiple(['tan(0)', 0], ['tan(null)', null]);
679
- });
680
- });
681
- describe('atan', () => {
682
- it(`works - ${databaseName}`, async () => {
683
- await funcTestMultiple(['atan(0)', 0], ['atan(null)', null]);
684
- });
685
- });
686
- describe('atan2', () => {
687
- it(`works - ${databaseName}`, async () => {
688
- await funcTestMultiple(
689
- ['atan2(0, 1)', 0],
690
- ['atan2(null, 1)', null],
691
- ['atan2(1, null)', null]
692
- );
693
- });
694
- });
695
- describe('sqrt', () => {
696
- it(`works - ${databaseName}`, async () => {
697
- await funcTestMultiple(
698
- ['sqrt(9)', 3],
699
- ['sqrt(6.25)', 2.5],
700
- ['sqrt(null)', null]
701
- );
702
- });
703
- });
704
- describe('pow', () => {
705
- it(`works - ${databaseName}`, async () => {
706
- await funcTestMultiple(
707
- ['pow(2, 3)', 8],
708
- ['pow(null, 3)', null],
709
- ['pow(2, null)', null]
710
- );
711
- });
712
- });
713
- describe('abs', () => {
714
- it(`works - ${databaseName}`, async () => {
715
- await funcTestMultiple(
716
- ['abs(-3)', 3],
717
- ['abs(3)', 3],
718
- ['abs(null)', null]
719
- );
720
- });
721
- });
722
- describe('sign', () => {
723
- it(`works - ${databaseName}`, async () => {
724
- await funcTestMultiple(
725
- ['sign(100)', 1],
726
- ['sign(-2)', -1],
727
- ['sign(0)', 0],
728
- ['sign(null)', null]
729
- );
730
- });
731
- });
732
- describe('is_inf', () => {
733
- it(`works - ${databaseName}`, async () => {
734
- await funcTestMultiple(
735
- ["is_inf('+inf'::number)", true],
736
- ['is_inf(100)', false],
737
- ['is_inf(null)', false]
738
- );
739
- });
740
- });
741
- describe('is_nan', () => {
742
- it(`works - ${databaseName}`, async () => {
743
- await funcTestMultiple(
744
- ["is_nan('NaN'::number)", true],
745
- ['is_nan(100)', false],
746
- ['is_nan(null)', false]
747
- );
748
- });
749
- });
750
- describe('greatest', () => {
751
- it(`works - ${databaseName}`, async () => {
752
- await funcTestMultiple(
753
- ['greatest(1, 10, -100)', 10],
754
- ['greatest(@2003, @2004, @1994) = @2004', true],
755
- [
756
- 'greatest(@2023-05-26 11:58:00, @2023-05-26 11:59:00) = @2023-05-26 11:59:00',
757
- true,
758
- ],
759
- ["greatest('a', 'b')", 'b'],
760
- ['greatest(1, null, 0)', null],
761
- ['greatest(null, null)', null]
762
- );
763
- });
764
- });
765
- describe('least', () => {
766
- it(`works - ${databaseName}`, async () => {
767
- await funcTestMultiple(
768
- ['least(1, 10, -100)', -100],
769
- ['least(@2003, @2004, @1994) = @1994', true],
770
- [
771
- 'least(@2023-05-26 11:58:00, @2023-05-26 11:59:00) = @2023-05-26 11:58:00',
772
- true,
773
- ],
774
- ["least('a', 'b')", 'a'],
775
- ['least(1, null, 0)', null],
776
- ['least(null, null)', null]
777
- );
778
- });
779
- });
780
- describe('div', () => {
781
- it(`works - ${databaseName}`, async () => {
782
- await funcTestMultiple(
783
- ['div(3, 2)', 1],
784
- ['div(null, 2)', null],
785
- ['div(2, null)', null]
786
- );
787
- });
788
- });
789
- describe('strpos', () => {
790
- it(`works - ${databaseName}`, async () => {
791
- await funcTestMultiple(
792
- ["strpos('123456789', '3')", 3],
793
- ["strpos('123456789', '0')", 0],
794
- ["strpos(null, '0')", null],
795
- ["strpos('123456789', null)", null]
796
- );
797
- });
798
- });
799
- describe('starts_with', () => {
800
- it(`works - ${databaseName}`, async () => {
801
- await funcTestMultiple(
802
- ["starts_with('hello world', 'hello')", true],
803
- ["starts_with('hello world', 'world')", false],
804
- ["starts_with(null, 'world')", false],
805
- ["starts_with('hello world', null)", false]
806
- );
807
- });
808
- });
809
- describe('ends_with', () => {
810
- it(`works - ${databaseName}`, async () => {
811
- await funcTestMultiple(
812
- ["ends_with('hello world', 'world')", true],
813
- ["ends_with('hello world', 'hello')", false],
814
- ["ends_with(null, 'world')", false],
815
- ["ends_with('hello world', null)", false]
816
- );
817
- });
818
- });
819
- describe('trim', () => {
820
- it(`works - ${databaseName}`, async () => {
821
- await funcTestMultiple(
822
- ["trim(' keep this ')", 'keep this'],
823
- ["trim('_ _keep_this_ _', '_ ')", 'keep_this'],
824
- ["trim(' keep everything ', '')", ' keep everything '],
825
- ["trim('null example', null)", null],
826
- ["trim(null, 'a')", null],
827
- ['trim(null)', null]
828
- );
829
- });
830
- });
831
- describe('ltrim', () => {
832
- it(`works - ${databaseName}`, async () => {
833
- await funcTestMultiple(
834
- ["ltrim(' keep this -> ')", 'keep this -> '],
835
- ["ltrim('_ _keep_this -> _ _', '_ ')", 'keep_this -> _ _'],
836
- ["ltrim(' keep everything ', '')", ' keep everything '],
837
- ["ltrim('null example', null)", null],
838
- ["ltrim(null, 'a')", null],
839
- ['ltrim(null)', null]
840
- );
841
- });
842
- });
843
- describe('rtrim', () => {
844
- it(`works - ${databaseName}`, async () => {
845
- await funcTestMultiple(
846
- ["rtrim(' <- keep this ')", ' <- keep this'],
847
- ["rtrim('_ _ <- keep_this_ _', '_ ')", '_ _ <- keep_this'],
848
- ["rtrim(' keep everything ', '')", ' keep everything '],
849
- ["rtrim('null example', null)", null],
850
- ["rtrim(null, 'a')", null],
851
- ['rtrim(null)', null]
852
- );
853
- });
854
- });
855
- describe('rand', () => {
856
- it(`is usually not the same value - ${databaseName}`, async () => {
857
- // There are around a billion values that rand() can be, so if this
858
- // test fails, most likely something is broken. Otherwise, you're the lucky
859
- // one in a billion!
860
- await funcTest('rand() = rand()', false);
861
- });
862
- });
863
- describe('pi', () => {
864
- it(`is pi - ${databaseName}`, async () => {
865
- await funcTest('abs(pi() - 3.141592653589793) < 0.0000000000001', true);
866
- });
867
- });
868
-
869
- describe('byte_length', () => {
870
- it(`works - ${databaseName}`, async () => {
871
- await funcTestMultiple(
872
- ["byte_length('hello')", 5],
873
- ["byte_length('©')", 2],
874
- ['byte_length(null)', null]
875
- );
876
- });
877
- });
878
- describe('ifnull', () => {
879
- it(`works - ${databaseName}`, async () => {
880
- await funcTestMultiple(
881
- ["ifnull('a', 'b')", 'a'],
882
- ["ifnull(null, 'b')", 'b'],
883
- ["ifnull('a', null)", 'a'],
884
- ['ifnull(null, null)', null]
885
- );
886
- });
887
- });
888
- describe('coalesce', () => {
889
- it(`works - ${databaseName}`, async () => {
890
- await funcTestMultiple(
891
- ["coalesce('a')", 'a'],
892
- ["coalesce('a', 'b')", 'a'],
893
- ["coalesce(null, 'a', 'b')", 'a'],
894
- ["coalesce(null, 'b')", 'b'],
895
- ["coalesce('a', null)", 'a'],
896
- ['coalesce(null, null)', null],
897
- ['coalesce(null)', null]
898
- );
899
- });
900
- });
901
- describe('nullif', () => {
902
- it(`works - ${databaseName}`, async () => {
903
- await funcTestMultiple(
904
- ["nullif('a', 'a')", null],
905
- ["nullif('a', 'b')", 'a'],
906
- ["nullif('a', null)", 'a'],
907
- ['nullif(null, null)', null],
908
- ['nullif(null, 2)', null]
909
- );
910
- });
911
- });
912
- describe('chr', () => {
913
- it(`works - ${databaseName}`, async () => {
914
- await funcTestMultiple(
915
- ['chr(65)', 'A'],
916
- ['chr(255)', 'ÿ'],
917
- ['chr(null)', null],
918
- // BigQuery's documentation says that `chr(0)` returns the empty string, but it doesn't,
919
- // it actually returns the null character. We generate code so that it does this.
920
- ['chr(0)', '']
921
- );
922
- });
923
- });
924
- describe('ascii', () => {
925
- it(`works - ${databaseName}`, async () => {
926
- await funcTestMultiple(
927
- ["ascii('A')", 65],
928
- ["ascii('ABC')", 65],
929
- ["ascii('')", 0],
930
- ['ascii(null)', null]
931
- );
932
- });
933
- });
934
- describe('unicode', () => {
935
- it(`works - ${databaseName}`, async () => {
936
- await funcTestMultiple(
937
- ["unicode('A')", 65],
938
- ["unicode('â')", 226],
939
- ["unicode('âBC')", 226],
940
- ["unicode('')", 0],
941
- ['unicode(null)', null]
942
- );
943
- });
944
- });
945
-
946
- describe('repeat', () => {
947
- it(`works - ${databaseName}`, async () => {
948
- await funcTestMultiple(
949
- ["repeat('foo', 0)", ''],
950
- ["repeat('foo', 1)", 'foo'],
951
- ["repeat('foo', 2)", 'foofoo'],
952
- ['repeat(null, 2)', null],
953
- ["repeat('foo', null)", null]
954
- );
955
- });
956
- // TODO how does a user do this: the second argument needs to be an integer, but floor doesn't cast to "integer" type.
957
- it.skip(`works floor decimal - ${databaseName}`, async () => {
958
- await funcTest("repeat('foo', floor(2.5))", 'foofoo');
959
- });
960
- // undefined behavior when negative, undefined behavior (likely error) when non-integer
961
- });
962
- describe('reverse', () => {
963
- it(`works - ${databaseName}`, async () => {
964
- await funcTestMultiple(
965
- ["reverse('foo')", 'oof'],
966
- ["reverse('')", ''],
967
- ['reverse(null)', null]
968
- );
969
- });
970
- });
971
-
972
- describe('lead', () => {
973
- it(`works with one param - ${databaseName}`, async () => {
974
- const result = await expressionModel
975
- .loadQuery(
976
- `query: state_facts -> {
977
- group_by: state
978
- calculate: next_state is lead(state)
979
- }`
980
- )
981
- .run();
982
- expect(result.data.path(0, 'state').value).toBe('AK');
983
- expect(result.data.path(0, 'next_state').value).toBe('AL');
984
- expect(result.data.path(1, 'state').value).toBe('AL');
985
- });
986
-
987
- it(`works with offset - ${databaseName}`, async () => {
988
- const result = await expressionModel
989
- .loadQuery(
990
- `query: state_facts -> {
991
- group_by: state
992
- calculate: next_next_state is lead(state, 2)
993
- }`
994
- )
995
- .run();
996
- expect(result.data.path(0, 'state').value).toBe('AK');
997
- expect(result.data.path(0, 'next_next_state').value).toBe('AR');
998
- expect(result.data.path(1, 'next_next_state').value).toBe('AZ');
999
- expect(result.data.path(1, 'state').value).toBe('AL');
1000
- expect(result.data.path(2, 'state').value).toBe('AR');
1001
- expect(result.data.path(3, 'state').value).toBe('AZ');
1002
- });
1003
-
1004
- it(`works with default value - ${databaseName}`, async () => {
1005
- const result = await expressionModel
1006
- .loadQuery(
1007
- `query: state_facts -> { project: *; limit: 10 } -> {
1008
- group_by: state
1009
- calculate: next_state is lead(state, 1, 'NONE')
1010
- }`
1011
- )
1012
- .run();
1013
- expect(result.data.path(9, 'next_state').value).toBe('NONE');
1014
- });
1015
- });
1016
- describe('last_value', () => {
1017
- it(`works - ${databaseName}`, async () => {
1018
- const result = await expressionModel
1019
- .loadQuery(
1020
- `
1021
- query: state_facts -> {
1022
- group_by: state, births
1023
- order_by: births desc
1024
- calculate: least_births is last_value(births)
1025
- }`
1026
- )
1027
- .run({rowLimit: 100});
1028
- const numRows = result.data.toObject().length;
1029
- const lastBirths = result.data.path(numRows - 1, 'births').value;
1030
- expect(result.data.path(0, 'least_births').value).toBe(lastBirths);
1031
- expect(result.data.path(1, 'least_births').value).toBe(lastBirths);
1032
- });
1033
- });
1034
- describe('avg_moving', () => {
1035
- it(`works - ${databaseName}`, async () => {
1036
- const result = await expressionModel
1037
- .loadQuery(
1038
- `
1039
- query: state_facts -> {
1040
- group_by: state, births
1041
- order_by: births desc
1042
- calculate: rolling_avg is avg_moving(births, 2)
1043
- }`
1044
- )
1045
- .run({rowLimit: 100});
1046
- const births0 = result.data.path(0, 'births').number.value;
1047
- const births1 = result.data.path(1, 'births').number.value;
1048
- const births2 = result.data.path(2, 'births').number.value;
1049
- const births3 = result.data.path(3, 'births').number.value;
1050
- expect(result.data.path(0, 'rolling_avg').number.value).toBe(births0);
1051
- expect(
1052
- Math.floor(result.data.path(1, 'rolling_avg').number.value)
1053
- ).toBe(Math.floor((births0 + births1) / 2));
1054
- expect(
1055
- Math.floor(result.data.path(2, 'rolling_avg').number.value)
1056
- ).toBe(Math.floor((births0 + births1 + births2) / 3));
1057
- expect(
1058
- Math.floor(result.data.path(3, 'rolling_avg').number.value)
1059
- ).toBe(Math.floor((births1 + births2 + births3) / 3));
1060
- });
1061
-
1062
- it(`works forward - ${databaseName}`, async () => {
1063
- const result = await expressionModel
1064
- .loadQuery(
1065
- `
1066
- query: state_facts -> { project: *; limit: 3 } -> {
1067
- group_by: state, births
1068
- order_by: births desc
1069
- calculate: rolling_avg is avg_moving(births, 0, 2)
1070
- }`
1071
- )
1072
- .run({rowLimit: 100});
1073
- const births0 = result.data.path(0, 'births').number.value;
1074
- const births1 = result.data.path(1, 'births').number.value;
1075
- const births2 = result.data.path(2, 'births').number.value;
1076
- expect(
1077
- Math.floor(result.data.path(0, 'rolling_avg').number.value)
1078
- ).toBe(Math.floor((births0 + births1 + births2) / 3));
1079
- expect(
1080
- Math.floor(result.data.path(1, 'rolling_avg').number.value)
1081
- ).toBe(Math.floor((births1 + births2) / 2));
1082
- expect(result.data.path(2, 'rolling_avg').number.value).toBe(births2);
1083
- });
1084
- });
1085
- describe('min, max, sum / window, cumulative', () => {
1086
- it(`works - ${databaseName}`, async () => {
1087
- const result = await expressionModel
1088
- .loadQuery(
1089
- `
1090
- query: state_facts -> { project: *; limit: 5 } -> {
1091
- group_by: state, births
1092
- order_by: births asc
1093
- calculate: min_c is min_cumulative(births)
1094
- calculate: max_c is max_cumulative(births)
1095
- calculate: sum_c is sum_cumulative(births)
1096
- calculate: min_w is min_window(births)
1097
- calculate: max_w is max_window(births)
1098
- calculate: sum_w is sum_window(births)
1099
- }`
1100
- )
1101
- .run({rowLimit: 100});
1102
- const births0 = result.data.path(0, 'births').number.value;
1103
- const births1 = result.data.path(1, 'births').number.value;
1104
- const births2 = result.data.path(2, 'births').number.value;
1105
- const births3 = result.data.path(3, 'births').number.value;
1106
- const births4 = result.data.path(4, 'births').number.value;
1107
- const births = [births0, births1, births2, births3, births4];
1108
- for (let r = 0; r < 5; r++) {
1109
- expect(result.data.path(r, 'min_c').number.value).toBe(births0);
1110
- expect(result.data.path(r, 'max_c').number.value).toBe(births[r]);
1111
- expect(result.data.path(r, 'sum_c').number.value).toBe(
1112
- births.slice(0, r + 1).reduce((a, b) => a + b)
1113
- );
1114
- expect(result.data.path(r, 'min_w').number.value).toBe(births0);
1115
- expect(result.data.path(r, 'max_w').number.value).toBe(births4);
1116
- expect(result.data.path(r, 'sum_w').number.value).toBe(
1117
- births.reduce((a, b) => a + b)
1118
- );
1119
- }
1120
- });
1121
- });
1122
- });
1123
-
1124
- afterAll(async () => {
1125
- await runtimes.closeAll();
1126
- });
1127
- };
27
+ import {functionsSharedTests} from '../shared/functions';
1128
28
 
1129
29
  const runtimes = new RuntimeList(databasesFromEnvironmentOr(allDatabases));
1130
30