@malloydata/malloy-tests 0.0.69-dev230808200646 → 0.0.69-dev230809221417

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 (79) hide show
  1. package/dist/databases/all/db_index.spec.d.ts +1 -1
  2. package/dist/databases/all/db_index.spec.js +116 -6
  3. package/dist/databases/all/db_index.spec.js.map +1 -1
  4. package/dist/databases/all/expr.spec.js +542 -6
  5. package/dist/databases/all/expr.spec.js.map +1 -1
  6. package/dist/databases/all/functions.spec.d.ts +1 -1
  7. package/dist/databases/all/functions.spec.js +745 -6
  8. package/dist/databases/all/functions.spec.js.map +1 -1
  9. package/dist/databases/all/join.spec.d.ts +1 -1
  10. package/dist/databases/all/join.spec.js +272 -6
  11. package/dist/databases/all/join.spec.js.map +1 -1
  12. package/dist/databases/all/nomodel.spec.d.ts +1 -1
  13. package/dist/databases/all/nomodel.spec.js +919 -6
  14. package/dist/databases/all/nomodel.spec.js.map +1 -1
  15. package/dist/databases/all/orderby.spec.d.ts +1 -1
  16. package/dist/databases/all/orderby.spec.js +187 -6
  17. package/dist/databases/all/orderby.spec.js.map +1 -1
  18. package/dist/databases/all/problems.spec.d.ts +1 -1
  19. package/dist/databases/all/problems.spec.js +76 -6
  20. package/dist/databases/all/problems.spec.js.map +1 -1
  21. package/dist/databases/all/sql_expressions.spec.d.ts +1 -1
  22. package/dist/databases/all/sql_expressions.spec.js +58 -6
  23. package/dist/databases/all/sql_expressions.spec.js.map +1 -1
  24. package/dist/databases/all/time.spec.d.ts +1 -1
  25. package/dist/databases/all/time.spec.js +609 -5
  26. package/dist/databases/all/time.spec.js.map +1 -1
  27. package/dist/databases/shared/test_list.js +1 -20
  28. package/dist/databases/shared/test_list.js.map +1 -1
  29. package/dist/index.d.ts +0 -8
  30. package/dist/index.js +1 -17
  31. package/dist/index.js.map +1 -1
  32. package/package.json +6 -6
  33. package/src/databases/all/db_index.spec.ts +137 -6
  34. package/src/databases/all/expr.spec.ts +661 -7
  35. package/src/databases/all/functions.spec.ts +1092 -6
  36. package/src/databases/all/join.spec.ts +309 -6
  37. package/src/databases/all/nomodel.spec.ts +1114 -7
  38. package/src/databases/all/orderby.spec.ts +229 -6
  39. package/src/databases/all/problems.spec.ts +82 -6
  40. package/src/databases/all/sql_expressions.spec.ts +65 -6
  41. package/src/databases/all/time.spec.ts +734 -5
  42. package/src/databases/shared/test_list.ts +1 -20
  43. package/src/index.ts +0 -9
  44. package/dist/databases/shared/db_index.d.ts +0 -3
  45. package/dist/databases/shared/db_index.js +0 -123
  46. package/dist/databases/shared/db_index.js.map +0 -1
  47. package/dist/databases/shared/expr.d.ts +0 -3
  48. package/dist/databases/shared/expr.js +0 -551
  49. package/dist/databases/shared/expr.js.map +0 -1
  50. package/dist/databases/shared/functions.d.ts +0 -3
  51. package/dist/databases/shared/functions.js +0 -754
  52. package/dist/databases/shared/functions.js.map +0 -1
  53. package/dist/databases/shared/join.d.ts +0 -3
  54. package/dist/databases/shared/join.js +0 -302
  55. package/dist/databases/shared/join.js.map +0 -1
  56. package/dist/databases/shared/nomodel.d.ts +0 -3
  57. package/dist/databases/shared/nomodel.js +0 -950
  58. package/dist/databases/shared/nomodel.js.map +0 -1
  59. package/dist/databases/shared/orderby.d.ts +0 -3
  60. package/dist/databases/shared/orderby.js +0 -217
  61. package/dist/databases/shared/orderby.js.map +0 -1
  62. package/dist/databases/shared/problems.d.ts +0 -3
  63. package/dist/databases/shared/problems.js +0 -106
  64. package/dist/databases/shared/problems.js.map +0 -1
  65. package/dist/databases/shared/sql_expressions.d.ts +0 -3
  66. package/dist/databases/shared/sql_expressions.js +0 -88
  67. package/dist/databases/shared/sql_expressions.js.map +0 -1
  68. package/dist/databases/shared/time.d.ts +0 -3
  69. package/dist/databases/shared/time.js +0 -640
  70. package/dist/databases/shared/time.js.map +0 -1
  71. package/src/databases/shared/db_index.ts +0 -167
  72. package/src/databases/shared/expr.ts +0 -695
  73. package/src/databases/shared/functions.ts +0 -1126
  74. package/src/databases/shared/join.ts +0 -340
  75. package/src/databases/shared/nomodel.ts +0 -1150
  76. package/src/databases/shared/orderby.ts +0 -260
  77. package/src/databases/shared/problems.ts +0 -113
  78. package/src/databases/shared/sql_expressions.ts +0 -96
  79. package/src/databases/shared/time.ts +0 -786
@@ -25,11 +25,750 @@
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const runtimes_1 = require("../../runtimes");
27
27
  const util_1 = require("../../util");
28
- const functions_1 = require("../shared/functions");
28
+ require("../../util/db-jest-matchers");
29
29
  const runtimes = new runtimes_1.RuntimeList((0, util_1.databasesFromEnvironmentOr)(runtimes_1.allDatabases));
30
- /*
31
- * This test file reuses common tests definitions.
32
- * For actual test development please go to: test/src/databases/shared/functions.spec.ts
33
- */
34
- (0, functions_1.functionsSharedTests)(runtimes);
30
+ const expressionModelText = `
31
+ source: aircraft_models is table('malloytest.aircraft_models'){
32
+ primary_key: aircraft_model_code
33
+ }
34
+
35
+ source: aircraft is table('malloytest.aircraft'){
36
+ primary_key: tail_num
37
+ join_one: aircraft_models with aircraft_model_code
38
+ measure: aircraft_count is count()
39
+ }
40
+
41
+ source: airports is table('malloytest.airports') {}
42
+
43
+ source: state_facts is table('malloytest.state_facts') {}
44
+ `;
45
+ const expressionModels = new Map();
46
+ runtimes.runtimeMap.forEach((runtime, databaseName) => expressionModels.set(databaseName, runtime.loadModel(expressionModelText)));
47
+ expressionModels.forEach((expressionModel, databaseName) => {
48
+ const funcTestGeneral = async (expr, type, expected) => {
49
+ const run = async () => {
50
+ return await expressionModel
51
+ .loadQuery(`
52
+ query: aircraft -> { ${type}: f is ${expr} }`)
53
+ .run();
54
+ };
55
+ if (expected.success !== undefined) {
56
+ const result = await run();
57
+ expect(result.data.path(0, 'f').value).toBe(expected.success);
58
+ }
59
+ else {
60
+ expect(run).rejects.toThrowError(expected.error);
61
+ }
62
+ };
63
+ const funcTest = (expr, expexted) => funcTestGeneral(expr, 'group_by', { success: expexted });
64
+ const funcTestAgg = (expr, expexted) => funcTestGeneral(expr, 'aggregate', { success: expexted });
65
+ const funcTestMultiple = async (...testCases) => {
66
+ const run = async () => {
67
+ return await expressionModel
68
+ .loadQuery(`
69
+ query: aircraft -> { ${testCases.map((testCase, i) => `group_by: f${i} is ${testCase[0]}`)} }`)
70
+ .run();
71
+ };
72
+ const result = await run();
73
+ testCases.forEach((testCase, i) => {
74
+ expect(result.data.path(0, `f${i}`).value).toBe(testCase[1]);
75
+ });
76
+ };
77
+ describe('concat', () => {
78
+ it(`works - ${databaseName}`, async () => {
79
+ await funcTestMultiple(["concat('foo', 'bar')", 'foobar'], ["concat(1, 'bar')", '1bar'], [
80
+ "concat('cons', true)",
81
+ databaseName === 'postgres' ? 'const' : 'construe',
82
+ ], ["concat('foo', @2003)", 'foo2003-01-01'], [
83
+ "concat('foo', @2003-01-01 12:00:00)",
84
+ databaseName === 'bigquery'
85
+ ? 'foo2003-01-01 12:00:00+00'
86
+ : 'foo2003-01-01 12:00:00',
87
+ ],
88
+ // TODO Maybe implement consistent null behavior
89
+ // ["concat('foo', null)", null],
90
+ ['concat()', '']);
91
+ });
92
+ });
93
+ describe('round', () => {
94
+ it(`works - ${databaseName}`, async () => {
95
+ await funcTestMultiple(['round(1.2)', 1],
96
+ // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
97
+ // that are fixed in 0.8.
98
+ ...(databaseName === 'duckdb_wasm'
99
+ ? []
100
+ : [['round(12.222, 1)', 12.2]]), ['round(12.2, -1)', 10], ['round(null)', null], ['round(1, null)', null]);
101
+ });
102
+ });
103
+ describe('floor', () => {
104
+ it(`works - ${databaseName}`, async () => {
105
+ await funcTestMultiple(['floor(1.9)', 1],
106
+ // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
107
+ // that are fixed in 0.8.
108
+ ...(databaseName === 'duckdb_wasm'
109
+ ? []
110
+ : [['floor(-1.9)', -2]]), ['floor(null)', null]);
111
+ await funcTest('floor(1.9)', 1);
112
+ });
113
+ });
114
+ describe('ceil', () => {
115
+ it(`works - ${databaseName}`, async () => {
116
+ await funcTestMultiple(['ceil(1.9)', 2],
117
+ // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
118
+ // that are fixed in 0.8.
119
+ ...(databaseName === 'duckdb_wasm'
120
+ ? []
121
+ : [['ceil(-1.9)', -1]]), ['ceil(null)', null]);
122
+ });
123
+ });
124
+ describe('length', () => {
125
+ it(`works - ${databaseName}`, async () => {
126
+ await funcTestMultiple(["length('foo')", 3], ['length(null)', null]);
127
+ });
128
+ });
129
+ describe('lower', () => {
130
+ it(`works - ${databaseName}`, async () => {
131
+ await funcTestMultiple(["lower('FoO')", 'foo'], ['lower(null)', null]);
132
+ });
133
+ });
134
+ describe('upper', () => {
135
+ it(`works - ${databaseName}`, async () => {
136
+ await funcTestMultiple(["upper('fOo')", 'FOO'], ['upper(null)', null]);
137
+ });
138
+ });
139
+ describe('regexp_extract', () => {
140
+ it(`works - ${databaseName}`, async () => {
141
+ await funcTestMultiple(["regexp_extract('I have a dog', r'd[aeiou]g')", 'dog'], ["regexp_extract(null, r'd[aeiou]g')", null], ["regexp_extract('foo', null)", null], ["regexp_extract('I have a d0g', r'd\\dg')", 'd0g']);
142
+ });
143
+ });
144
+ describe('replace', () => {
145
+ it(`works - ${databaseName}`, async () => {
146
+ await funcTestMultiple(["replace('aaaa', 'a', 'c')", 'cccc'], ["replace('aaaa', r'.', 'c')", 'cccc'], [
147
+ "replace('axbxc', r'(a).(b).(c)', '\\\\0 - \\\\1 - \\\\2 - \\\\3')",
148
+ databaseName === 'postgres' ? '\\0 - a - b - c' : 'axbxc - a - b - c',
149
+ ], ["replace('aaaa', '', 'c')", 'aaaa'], ["replace(null, 'a', 'c')", null], ["replace('aaaa', null, 'c')", null], ["replace('aaaa', 'a', null)", null]);
150
+ });
151
+ });
152
+ describe('substr', () => {
153
+ it(`works - ${databaseName}`, async () => {
154
+ await funcTestMultiple(["substr('foo', 2)", 'oo'], ["substr('foo', 2, 1)", 'o'], ["substr('foo bar baz', -3)", 'baz'], ['substr(null, 1, 2)', null], ["substr('aaaa', null, 1)", null], ["substr('aaaa', 1, null)", null]);
155
+ });
156
+ });
157
+ describe('raw function call', () => {
158
+ it(`works - ${databaseName}`, async () => {
159
+ await funcTestMultiple(['floor(cbrt!(27)::number)', 3], ['floor(cbrt!number(27))', 3], ["substr('foo bar baz', -3)", 'baz'], ['substr(null, 1, 2)', null], ["substr('aaaa', null, 1)", null], ["substr('aaaa', 1, null)", null]);
160
+ });
161
+ });
162
+ describe('stddev', () => {
163
+ // TODO symmetric aggregates don't work with custom aggregate functions in BQ currently
164
+ if (databaseName === 'bigquery')
165
+ return;
166
+ it(`works - ${databaseName}`, async () => {
167
+ await funcTestAgg('round(stddev(aircraft_models.seats))', 29);
168
+ });
169
+ it(`works with struct - ${databaseName}`, async () => {
170
+ await funcTestAgg('round(aircraft_models.stddev(aircraft_models.seats))', 41);
171
+ });
172
+ it(`works with implicit parameter - ${databaseName}`, async () => {
173
+ await funcTestAgg('round(aircraft_models.seats.stddev())', 41);
174
+ });
175
+ it(`works with filter - ${databaseName}`, async () => {
176
+ await funcTestAgg('round(aircraft_models.seats.stddev() { where: 1 = 1 })', 41);
177
+ await funcTestAgg('round(aircraft_models.seats.stddev() { where: aircraft_models.seats > 4 })', 69);
178
+ });
179
+ });
180
+ describe('row_number', () => {
181
+ it(`works when the order by is a dimension - ${databaseName}`, async () => {
182
+ const result = await expressionModel
183
+ .loadQuery(`query: state_facts -> {
184
+ group_by: state
185
+ calculate: row_num is row_number()
186
+ }`)
187
+ .run();
188
+ expect(result.data.path(0, 'row_num').value).toBe(1);
189
+ expect(result.data.path(1, 'row_num').value).toBe(2);
190
+ });
191
+ it(`works when the order by is a dimension in the other order - ${databaseName}`, async () => {
192
+ const result = await expressionModel
193
+ .loadQuery(`query: state_facts -> {
194
+ calculate: row_num is row_number()
195
+ group_by: state
196
+ }`)
197
+ .run();
198
+ expect(result.data.path(0, 'row_num').value).toBe(1);
199
+ expect(result.data.path(1, 'row_num').value).toBe(2);
200
+ });
201
+ it(`works when the order by is a measure - ${databaseName}`, async () => {
202
+ const result = await expressionModel
203
+ .loadQuery(`query: state_facts -> {
204
+ group_by: popular_name
205
+ aggregate: c is count()
206
+ calculate: row_num is row_number()
207
+ }`)
208
+ .run();
209
+ expect(result.data.path(0, 'row_num').value).toBe(1);
210
+ expect(result.data.path(1, 'row_num').value).toBe(2);
211
+ });
212
+ it(`works when the order by is a measure but there is no group by - ${databaseName}`, async () => {
213
+ const result = await expressionModel
214
+ .loadQuery(`query: state_facts -> {
215
+ aggregate: c is count()
216
+ calculate: row_num is row_number()
217
+ }`)
218
+ .run();
219
+ expect(result.data.path(0, 'row_num').value).toBe(1);
220
+ });
221
+ it(`works inside nest - ${databaseName}`, async () => {
222
+ const result = await expressionModel
223
+ .loadQuery(`query: state_facts { join_one: airports on airports.state = state } -> {
224
+ group_by: state
225
+ nest: q is {
226
+ group_by: airports.county
227
+ calculate: row_num is row_number()
228
+ }
229
+ }`)
230
+ .run();
231
+ expect(result.data.path(0, 'q', 0, 'row_num').value).toBe(1);
232
+ expect(result.data.path(0, 'q', 1, 'row_num').value).toBe(2);
233
+ expect(result.data.path(1, 'q', 0, 'row_num').value).toBe(1);
234
+ expect(result.data.path(1, 'q', 1, 'row_num').value).toBe(2);
235
+ });
236
+ test(`works outside nest, but with a nest nearby - ${databaseName}`, async () => {
237
+ const result = await expressionModel
238
+ .loadQuery(`query: state_facts -> {
239
+ group_by: state
240
+ calculate: row_num is row_number()
241
+ nest: nested is {
242
+ group_by: state
243
+ }
244
+ }`)
245
+ .run();
246
+ expect(result.data.path(0, 'row_num').value).toBe(1);
247
+ expect(result.data.path(1, 'row_num').value).toBe(2);
248
+ });
249
+ });
250
+ describe('rank', () => {
251
+ it(`works ordered by dimension - ${databaseName}`, async () => {
252
+ const result = await expressionModel
253
+ .loadQuery(`query: state_facts -> {
254
+ group_by:
255
+ state,
256
+ births_ballpark is ceil(births / 1000000) * 1000000
257
+ order_by: births_ballpark desc
258
+ calculate: births_ballpark_rank is rank()
259
+ limit: 20
260
+ }`)
261
+ .run({ rowLimit: 20 });
262
+ expect(result.data.path(0, 'births_ballpark_rank').value).toBe(1);
263
+ expect(result.data.path(1, 'births_ballpark_rank').value).toBe(2);
264
+ expect(result.data.path(8, 'births_ballpark_rank').value).toBe(9);
265
+ expect(result.data.path(9, 'births_ballpark_rank').value).toBe(9);
266
+ expect(result.data.path(10, 'births_ballpark_rank').value).toBe(9);
267
+ expect(result.data.path(11, 'births_ballpark_rank').value).toBe(12);
268
+ });
269
+ it(`works ordered by aggregate - ${databaseName}`, async () => {
270
+ const result = await expressionModel
271
+ .loadQuery(`query: state_facts -> {
272
+ group_by: first_letter is substr(state, 1, 1)
273
+ aggregate: states_with_first_letter_ish is round(count() / 2) * 2
274
+ calculate: r is rank()
275
+ }`)
276
+ .run();
277
+ expect(result.data.path(0, 'r').value).toBe(1);
278
+ expect(result.data.path(1, 'r').value).toBe(1);
279
+ expect(result.data.path(2, 'r').value).toBe(3);
280
+ expect(result.data.path(3, 'r').value).toBe(3);
281
+ });
282
+ });
283
+ describe('lag', () => {
284
+ it(`works with one param - ${databaseName}`, async () => {
285
+ const result = await expressionModel
286
+ .loadQuery(`query: state_facts -> {
287
+ group_by: state
288
+ calculate: prev_state is lag(state)
289
+ }`)
290
+ .run();
291
+ expect(result.data.path(0, 'state').value).toBe('AK');
292
+ expect(result.data.path(0, 'prev_state').value).toBe(null);
293
+ expect(result.data.path(1, 'prev_state').value).toBe('AK');
294
+ expect(result.data.path(1, 'state').value).toBe('AL');
295
+ expect(result.data.path(2, 'prev_state').value).toBe('AL');
296
+ });
297
+ it(`works with expression field - ${databaseName}`, async () => {
298
+ const result = await expressionModel
299
+ .loadQuery(`query: state_facts -> {
300
+ group_by: lower_state is lower(state)
301
+ calculate: prev_state is lag(lower_state)
302
+ }`)
303
+ .run();
304
+ expect(result.data.path(0, 'lower_state').value).toBe('ak');
305
+ expect(result.data.path(0, 'prev_state').value).toBe(null);
306
+ expect(result.data.path(1, 'prev_state').value).toBe('ak');
307
+ expect(result.data.path(1, 'lower_state').value).toBe('al');
308
+ expect(result.data.path(2, 'prev_state').value).toBe('al');
309
+ });
310
+ it(`works with expression - ${databaseName}`, async () => {
311
+ const result = await expressionModel
312
+ .loadQuery(`query: state_facts -> {
313
+ group_by: state
314
+ calculate: prev_state is lag(lower(state))
315
+ }`)
316
+ .run();
317
+ expect(result.data.path(0, 'state').value).toBe('AK');
318
+ expect(result.data.path(0, 'prev_state').value).toBe(null);
319
+ expect(result.data.path(1, 'prev_state').value).toBe('ak');
320
+ expect(result.data.path(1, 'state').value).toBe('AL');
321
+ expect(result.data.path(2, 'prev_state').value).toBe('al');
322
+ });
323
+ it(`works with field, ordering by expression field - ${databaseName}`, async () => {
324
+ const result = await expressionModel
325
+ .loadQuery(`query: state_facts -> {
326
+ group_by: lower_state is lower(state)
327
+ aggregate: c is count()
328
+ order_by: lower_state
329
+ calculate: prev_count is lag(c)
330
+ }`)
331
+ .run();
332
+ expect(result.data.path(0, 'lower_state').value).toBe('ak');
333
+ expect(result.data.path(0, 'prev_count').value).toBe(null);
334
+ expect(result.data.path(1, 'prev_count').value).toBe(1);
335
+ expect(result.data.path(1, 'lower_state').value).toBe('al');
336
+ expect(result.data.path(2, 'prev_count').value).toBe(1);
337
+ });
338
+ it(`works with offset - ${databaseName}`, async () => {
339
+ const result = await expressionModel
340
+ .loadQuery(`query: state_facts -> {
341
+ group_by: state
342
+ calculate: prev_prev_state is lag(state, 2)
343
+ }`)
344
+ .run();
345
+ expect(result.data.path(0, 'state').value).toBe('AK');
346
+ expect(result.data.path(0, 'prev_prev_state').value).toBe(null);
347
+ expect(result.data.path(1, 'prev_prev_state').value).toBe(null);
348
+ expect(result.data.path(2, 'prev_prev_state').value).toBe('AK');
349
+ expect(result.data.path(1, 'state').value).toBe('AL');
350
+ expect(result.data.path(3, 'prev_prev_state').value).toBe('AL');
351
+ });
352
+ it(`works with default value - ${databaseName}`, async () => {
353
+ const result = await expressionModel
354
+ .loadQuery(`query: state_facts -> {
355
+ group_by: state
356
+ calculate: prev_state is lag(state, 1, 'NONE')
357
+ }`)
358
+ .run();
359
+ expect(result.data.path(0, 'prev_state').value).toBe('NONE');
360
+ });
361
+ it(`works with now as the default value - ${databaseName}`, async () => {
362
+ const result = await expressionModel
363
+ .loadQuery(`
364
+ query: state_facts -> {
365
+ group_by: state
366
+ calculate: lag_val is lag(@2011-11-11 11:11:11, 1, now).year = now.year
367
+ }`)
368
+ .run();
369
+ expect(result.data.path(0, 'lag_val').value).toBe(true);
370
+ expect(result.data.path(1, 'lag_val').value).toBe(false);
371
+ });
372
+ });
373
+ describe('output field in calculate', () => {
374
+ it(`dotted aggregates work with an output field - ${databaseName}`, async () => {
375
+ const result = await expressionModel
376
+ .loadQuery(`query: aircraft -> {
377
+ group_by: aircraft_models.seats
378
+ aggregate: s is aircraft_models.seats.sum()
379
+ calculate: a is lag(seats.sum())
380
+ }`)
381
+ .run();
382
+ expect(result.data.path(1, 'a').value).toBe(result.data.path(0, 's').value);
383
+ });
384
+ });
385
+ describe('first_value', () => {
386
+ test(`works in nest - ${databaseName}`, async () => {
387
+ const result = await expressionModel
388
+ .loadQuery(`
389
+ query: aircraft -> {
390
+ group_by: state
391
+ where: state != null
392
+ nest: by_county is {
393
+ limit: 2
394
+ group_by: county
395
+ aggregate: aircraft_count
396
+ calculate: first_count is first_value(count())
397
+ }
398
+ }`)
399
+ .run();
400
+ expect(result.data.path(0, 'by_county', 1, 'first_count').value).toBe(result.data.path(0, 'by_county', 0, 'aircraft_count').value);
401
+ expect(result.data.path(1, 'by_county', 1, 'first_count').value).toBe(result.data.path(1, 'by_county', 0, 'aircraft_count').value);
402
+ });
403
+ it(`works outside nest - ${databaseName}`, async () => {
404
+ const result = await expressionModel
405
+ .loadQuery(`
406
+ query: state_facts -> {
407
+ group_by: state, births
408
+ order_by: births desc
409
+ calculate: most_births is first_value(births)
410
+ }`)
411
+ .run();
412
+ const firstBirths = result.data.path(0, 'births').value;
413
+ expect(result.data.path(0, 'most_births').value).toBe(firstBirths);
414
+ expect(result.data.path(1, 'most_births').value).toBe(firstBirths);
415
+ });
416
+ it(`works with an aggregate which is not in the query - ${databaseName}`, async () => {
417
+ const result = await expressionModel
418
+ .loadQuery(`
419
+ query: airports { measure: airport_count is count() } -> {
420
+ group_by: state
421
+ where: state != null
422
+ calculate: prev_airport_count is lag(airport_count)
423
+ }`)
424
+ .run();
425
+ expect(result.data.path(0, 'prev_airport_count').value).toBe(null);
426
+ expect(result.data.path(1, 'prev_airport_count').value).toBe(608);
427
+ expect(result.data.path(2, 'prev_airport_count').value).toBe(260);
428
+ });
429
+ it(`works with a localized aggregate - ${databaseName}`, async () => {
430
+ const result = await expressionModel
431
+ .loadQuery(`
432
+ query: aircraft -> {
433
+ group_by: aircraft_models.seats,
434
+ calculate: prev_sum_of_seats is lag(aircraft_models.seats.sum())
435
+ }`)
436
+ .run();
437
+ expect(result.data.path(0, 'prev_sum_of_seats').value).toBe(null);
438
+ expect(result.data.path(1, 'prev_sum_of_seats').value).toBe(0);
439
+ expect(result.data.path(2, 'prev_sum_of_seats').value).toBe(230);
440
+ });
441
+ });
442
+ describe('trunc', () => {
443
+ it(`works - ${databaseName}`, async () => {
444
+ await funcTestMultiple(['trunc(1.9)', 1],
445
+ // TODO Remove when we upgrade to DuckDB 0.8.X -- DuckDB has some bugs with rounding
446
+ // that are fixed in 0.8.
447
+ ...(databaseName === 'duckdb_wasm'
448
+ ? []
449
+ : [['trunc(-1.9)', -1]]), ['trunc(12.29, 1)', 12.2], ['trunc(19.2, -1)', 10], ['trunc(null)', null], ['trunc(1, null)', null]);
450
+ });
451
+ });
452
+ describe('log', () => {
453
+ it(`works - ${databaseName}`, async () => {
454
+ await funcTestMultiple(['log(10, 10)', 1], ['log(100, 10)', 2], ['log(32, 2)', 5], ['log(null, 2)', null], ['log(10, null)', null]);
455
+ });
456
+ });
457
+ describe('ln', () => {
458
+ it(`works - ${databaseName}`, async () => {
459
+ await funcTestMultiple(['ln(exp(1))', 1], ['ln(exp(2))', 2], ['ln(null)', null]);
460
+ });
461
+ });
462
+ describe('exp', () => {
463
+ it(`works - ${databaseName}`, async () => {
464
+ await funcTestMultiple(['exp(0)', 1], ['ln(exp(1))', 1], ['exp(null)', null]);
465
+ });
466
+ });
467
+ // TODO trig functions could have more exhaustive tests -- these are mostly just here to
468
+ // ensure they exist
469
+ describe('cos', () => {
470
+ it(`works - ${databaseName}`, async () => {
471
+ await funcTestMultiple(['cos(0)', 1], ['cos(null)', null]);
472
+ });
473
+ });
474
+ describe('acos', () => {
475
+ it(`works - ${databaseName}`, async () => {
476
+ await funcTestMultiple(['acos(1)', 0], ['acos(null)', null]);
477
+ });
478
+ });
479
+ describe('sin', () => {
480
+ it(`works - ${databaseName}`, async () => {
481
+ await funcTestMultiple(['sin(0)', 0], ['sin(null)', null]);
482
+ });
483
+ });
484
+ describe('asin', () => {
485
+ it(`works - ${databaseName}`, async () => {
486
+ await funcTestMultiple(['asin(0)', 0], ['asin(null)', null]);
487
+ });
488
+ });
489
+ describe('tan', () => {
490
+ it(`works - ${databaseName}`, async () => {
491
+ await funcTestMultiple(['tan(0)', 0], ['tan(null)', null]);
492
+ });
493
+ });
494
+ describe('atan', () => {
495
+ it(`works - ${databaseName}`, async () => {
496
+ await funcTestMultiple(['atan(0)', 0], ['atan(null)', null]);
497
+ });
498
+ });
499
+ describe('atan2', () => {
500
+ it(`works - ${databaseName}`, async () => {
501
+ await funcTestMultiple(['atan2(0, 1)', 0], ['atan2(null, 1)', null], ['atan2(1, null)', null]);
502
+ });
503
+ });
504
+ describe('sqrt', () => {
505
+ it(`works - ${databaseName}`, async () => {
506
+ await funcTestMultiple(['sqrt(9)', 3], ['sqrt(6.25)', 2.5], ['sqrt(null)', null]);
507
+ });
508
+ });
509
+ describe('pow', () => {
510
+ it(`works - ${databaseName}`, async () => {
511
+ await funcTestMultiple(['pow(2, 3)', 8], ['pow(null, 3)', null], ['pow(2, null)', null]);
512
+ });
513
+ });
514
+ describe('abs', () => {
515
+ it(`works - ${databaseName}`, async () => {
516
+ await funcTestMultiple(['abs(-3)', 3], ['abs(3)', 3], ['abs(null)', null]);
517
+ });
518
+ });
519
+ describe('sign', () => {
520
+ it(`works - ${databaseName}`, async () => {
521
+ await funcTestMultiple(['sign(100)', 1], ['sign(-2)', -1], ['sign(0)', 0], ['sign(null)', null]);
522
+ });
523
+ });
524
+ describe('is_inf', () => {
525
+ it(`works - ${databaseName}`, async () => {
526
+ await funcTestMultiple(["is_inf('+inf'::number)", true], ['is_inf(100)', false], ['is_inf(null)', false]);
527
+ });
528
+ });
529
+ describe('is_nan', () => {
530
+ it(`works - ${databaseName}`, async () => {
531
+ await funcTestMultiple(["is_nan('NaN'::number)", true], ['is_nan(100)', false], ['is_nan(null)', false]);
532
+ });
533
+ });
534
+ describe('greatest', () => {
535
+ it(`works - ${databaseName}`, async () => {
536
+ await funcTestMultiple(['greatest(1, 10, -100)', 10], ['greatest(@2003, @2004, @1994) = @2004', true], [
537
+ 'greatest(@2023-05-26 11:58:00, @2023-05-26 11:59:00) = @2023-05-26 11:59:00',
538
+ true,
539
+ ], ["greatest('a', 'b')", 'b'], ['greatest(1, null, 0)', null], ['greatest(null, null)', null]);
540
+ });
541
+ });
542
+ describe('least', () => {
543
+ it(`works - ${databaseName}`, async () => {
544
+ await funcTestMultiple(['least(1, 10, -100)', -100], ['least(@2003, @2004, @1994) = @1994', true], [
545
+ 'least(@2023-05-26 11:58:00, @2023-05-26 11:59:00) = @2023-05-26 11:58:00',
546
+ true,
547
+ ], ["least('a', 'b')", 'a'], ['least(1, null, 0)', null], ['least(null, null)', null]);
548
+ });
549
+ });
550
+ describe('div', () => {
551
+ it(`works - ${databaseName}`, async () => {
552
+ await funcTestMultiple(['div(3, 2)', 1], ['div(null, 2)', null], ['div(2, null)', null]);
553
+ });
554
+ });
555
+ describe('strpos', () => {
556
+ it(`works - ${databaseName}`, async () => {
557
+ await funcTestMultiple(["strpos('123456789', '3')", 3], ["strpos('123456789', '0')", 0], ["strpos(null, '0')", null], ["strpos('123456789', null)", null]);
558
+ });
559
+ });
560
+ describe('starts_with', () => {
561
+ it(`works - ${databaseName}`, async () => {
562
+ await funcTestMultiple(["starts_with('hello world', 'hello')", true], ["starts_with('hello world', 'world')", false], ["starts_with(null, 'world')", false], ["starts_with('hello world', null)", false]);
563
+ });
564
+ });
565
+ describe('ends_with', () => {
566
+ it(`works - ${databaseName}`, async () => {
567
+ await funcTestMultiple(["ends_with('hello world', 'world')", true], ["ends_with('hello world', 'hello')", false], ["ends_with(null, 'world')", false], ["ends_with('hello world', null)", false]);
568
+ });
569
+ });
570
+ describe('trim', () => {
571
+ it(`works - ${databaseName}`, async () => {
572
+ await funcTestMultiple(["trim(' keep this ')", 'keep this'], ["trim('_ _keep_this_ _', '_ ')", 'keep_this'], ["trim(' keep everything ', '')", ' keep everything '], ["trim('null example', null)", null], ["trim(null, 'a')", null], ['trim(null)', null]);
573
+ });
574
+ });
575
+ describe('ltrim', () => {
576
+ it(`works - ${databaseName}`, async () => {
577
+ await funcTestMultiple(["ltrim(' keep this -> ')", 'keep this -> '], ["ltrim('_ _keep_this -> _ _', '_ ')", 'keep_this -> _ _'], ["ltrim(' keep everything ', '')", ' keep everything '], ["ltrim('null example', null)", null], ["ltrim(null, 'a')", null], ['ltrim(null)', null]);
578
+ });
579
+ });
580
+ describe('rtrim', () => {
581
+ it(`works - ${databaseName}`, async () => {
582
+ await funcTestMultiple(["rtrim(' <- keep this ')", ' <- keep this'], ["rtrim('_ _ <- keep_this_ _', '_ ')", '_ _ <- keep_this'], ["rtrim(' keep everything ', '')", ' keep everything '], ["rtrim('null example', null)", null], ["rtrim(null, 'a')", null], ['rtrim(null)', null]);
583
+ });
584
+ });
585
+ describe('rand', () => {
586
+ it(`is usually not the same value - ${databaseName}`, async () => {
587
+ // There are around a billion values that rand() can be, so if this
588
+ // test fails, most likely something is broken. Otherwise, you're the lucky
589
+ // one in a billion!
590
+ await funcTest('rand() = rand()', false);
591
+ });
592
+ });
593
+ describe('pi', () => {
594
+ it(`is pi - ${databaseName}`, async () => {
595
+ await funcTest('abs(pi() - 3.141592653589793) < 0.0000000000001', true);
596
+ });
597
+ });
598
+ describe('byte_length', () => {
599
+ it(`works - ${databaseName}`, async () => {
600
+ await funcTestMultiple(["byte_length('hello')", 5], ["byte_length('©')", 2], ['byte_length(null)', null]);
601
+ });
602
+ });
603
+ describe('ifnull', () => {
604
+ it(`works - ${databaseName}`, async () => {
605
+ await funcTestMultiple(["ifnull('a', 'b')", 'a'], ["ifnull(null, 'b')", 'b'], ["ifnull('a', null)", 'a'], ['ifnull(null, null)', null]);
606
+ });
607
+ });
608
+ describe('coalesce', () => {
609
+ it(`works - ${databaseName}`, async () => {
610
+ await funcTestMultiple(["coalesce('a')", 'a'], ["coalesce('a', 'b')", 'a'], ["coalesce(null, 'a', 'b')", 'a'], ["coalesce(null, 'b')", 'b'], ["coalesce('a', null)", 'a'], ['coalesce(null, null)', null], ['coalesce(null)', null]);
611
+ });
612
+ });
613
+ describe('nullif', () => {
614
+ it(`works - ${databaseName}`, async () => {
615
+ await funcTestMultiple(["nullif('a', 'a')", null], ["nullif('a', 'b')", 'a'], ["nullif('a', null)", 'a'], ['nullif(null, null)', null], ['nullif(null, 2)', null]);
616
+ });
617
+ });
618
+ describe('chr', () => {
619
+ it(`works - ${databaseName}`, async () => {
620
+ await funcTestMultiple(['chr(65)', 'A'], ['chr(255)', 'ÿ'], ['chr(null)', null],
621
+ // BigQuery's documentation says that `chr(0)` returns the empty string, but it doesn't,
622
+ // it actually returns the null character. We generate code so that it does this.
623
+ ['chr(0)', '']);
624
+ });
625
+ });
626
+ describe('ascii', () => {
627
+ it(`works - ${databaseName}`, async () => {
628
+ await funcTestMultiple(["ascii('A')", 65], ["ascii('ABC')", 65], ["ascii('')", 0], ['ascii(null)', null]);
629
+ });
630
+ });
631
+ describe('unicode', () => {
632
+ it(`works - ${databaseName}`, async () => {
633
+ await funcTestMultiple(["unicode('A')", 65], ["unicode('â')", 226], ["unicode('âBC')", 226], ["unicode('')", 0], ['unicode(null)', null]);
634
+ });
635
+ });
636
+ describe('repeat', () => {
637
+ it(`works - ${databaseName}`, async () => {
638
+ await funcTestMultiple(["repeat('foo', 0)", ''], ["repeat('foo', 1)", 'foo'], ["repeat('foo', 2)", 'foofoo'], ['repeat(null, 2)', null], ["repeat('foo', null)", null]);
639
+ });
640
+ // TODO how does a user do this: the second argument needs to be an integer, but floor doesn't cast to "integer" type.
641
+ it.skip(`works floor decimal - ${databaseName}`, async () => {
642
+ await funcTest("repeat('foo', floor(2.5))", 'foofoo');
643
+ });
644
+ // undefined behavior when negative, undefined behavior (likely error) when non-integer
645
+ });
646
+ describe('reverse', () => {
647
+ it(`works - ${databaseName}`, async () => {
648
+ await funcTestMultiple(["reverse('foo')", 'oof'], ["reverse('')", ''], ['reverse(null)', null]);
649
+ });
650
+ });
651
+ describe('lead', () => {
652
+ it(`works with one param - ${databaseName}`, async () => {
653
+ const result = await expressionModel
654
+ .loadQuery(`query: state_facts -> {
655
+ group_by: state
656
+ calculate: next_state is lead(state)
657
+ }`)
658
+ .run();
659
+ expect(result.data.path(0, 'state').value).toBe('AK');
660
+ expect(result.data.path(0, 'next_state').value).toBe('AL');
661
+ expect(result.data.path(1, 'state').value).toBe('AL');
662
+ });
663
+ it(`works with offset - ${databaseName}`, async () => {
664
+ const result = await expressionModel
665
+ .loadQuery(`query: state_facts -> {
666
+ group_by: state
667
+ calculate: next_next_state is lead(state, 2)
668
+ }`)
669
+ .run();
670
+ expect(result.data.path(0, 'state').value).toBe('AK');
671
+ expect(result.data.path(0, 'next_next_state').value).toBe('AR');
672
+ expect(result.data.path(1, 'next_next_state').value).toBe('AZ');
673
+ expect(result.data.path(1, 'state').value).toBe('AL');
674
+ expect(result.data.path(2, 'state').value).toBe('AR');
675
+ expect(result.data.path(3, 'state').value).toBe('AZ');
676
+ });
677
+ it(`works with default value - ${databaseName}`, async () => {
678
+ const result = await expressionModel
679
+ .loadQuery(`query: state_facts -> { project: *; limit: 10 } -> {
680
+ group_by: state
681
+ calculate: next_state is lead(state, 1, 'NONE')
682
+ }`)
683
+ .run();
684
+ expect(result.data.path(9, 'next_state').value).toBe('NONE');
685
+ });
686
+ });
687
+ describe('last_value', () => {
688
+ it(`works - ${databaseName}`, async () => {
689
+ const result = await expressionModel
690
+ .loadQuery(`
691
+ query: state_facts -> {
692
+ group_by: state, births
693
+ order_by: births desc
694
+ calculate: least_births is last_value(births)
695
+ }`)
696
+ .run({ rowLimit: 100 });
697
+ const numRows = result.data.toObject().length;
698
+ const lastBirths = result.data.path(numRows - 1, 'births').value;
699
+ expect(result.data.path(0, 'least_births').value).toBe(lastBirths);
700
+ expect(result.data.path(1, 'least_births').value).toBe(lastBirths);
701
+ });
702
+ });
703
+ describe('avg_moving', () => {
704
+ it(`works - ${databaseName}`, async () => {
705
+ const result = await expressionModel
706
+ .loadQuery(`
707
+ query: state_facts -> {
708
+ group_by: state, births
709
+ order_by: births desc
710
+ calculate: rolling_avg is avg_moving(births, 2)
711
+ }`)
712
+ .run({ rowLimit: 100 });
713
+ const births0 = result.data.path(0, 'births').number.value;
714
+ const births1 = result.data.path(1, 'births').number.value;
715
+ const births2 = result.data.path(2, 'births').number.value;
716
+ const births3 = result.data.path(3, 'births').number.value;
717
+ expect(result.data.path(0, 'rolling_avg').number.value).toBe(births0);
718
+ expect(Math.floor(result.data.path(1, 'rolling_avg').number.value)).toBe(Math.floor((births0 + births1) / 2));
719
+ expect(Math.floor(result.data.path(2, 'rolling_avg').number.value)).toBe(Math.floor((births0 + births1 + births2) / 3));
720
+ expect(Math.floor(result.data.path(3, 'rolling_avg').number.value)).toBe(Math.floor((births1 + births2 + births3) / 3));
721
+ });
722
+ it(`works forward - ${databaseName}`, async () => {
723
+ const result = await expressionModel
724
+ .loadQuery(`
725
+ query: state_facts -> { project: *; limit: 3 } -> {
726
+ group_by: state, births
727
+ order_by: births desc
728
+ calculate: rolling_avg is avg_moving(births, 0, 2)
729
+ }`)
730
+ .run({ rowLimit: 100 });
731
+ const births0 = result.data.path(0, 'births').number.value;
732
+ const births1 = result.data.path(1, 'births').number.value;
733
+ const births2 = result.data.path(2, 'births').number.value;
734
+ expect(Math.floor(result.data.path(0, 'rolling_avg').number.value)).toBe(Math.floor((births0 + births1 + births2) / 3));
735
+ expect(Math.floor(result.data.path(1, 'rolling_avg').number.value)).toBe(Math.floor((births1 + births2) / 2));
736
+ expect(result.data.path(2, 'rolling_avg').number.value).toBe(births2);
737
+ });
738
+ });
739
+ describe('min, max, sum / window, cumulative', () => {
740
+ it(`works - ${databaseName}`, async () => {
741
+ const result = await expressionModel
742
+ .loadQuery(`
743
+ query: state_facts -> { project: *; limit: 5 } -> {
744
+ group_by: state, births
745
+ order_by: births asc
746
+ calculate: min_c is min_cumulative(births)
747
+ calculate: max_c is max_cumulative(births)
748
+ calculate: sum_c is sum_cumulative(births)
749
+ calculate: min_w is min_window(births)
750
+ calculate: max_w is max_window(births)
751
+ calculate: sum_w is sum_window(births)
752
+ }`)
753
+ .run({ rowLimit: 100 });
754
+ const births0 = result.data.path(0, 'births').number.value;
755
+ const births1 = result.data.path(1, 'births').number.value;
756
+ const births2 = result.data.path(2, 'births').number.value;
757
+ const births3 = result.data.path(3, 'births').number.value;
758
+ const births4 = result.data.path(4, 'births').number.value;
759
+ const births = [births0, births1, births2, births3, births4];
760
+ for (let r = 0; r < 5; r++) {
761
+ expect(result.data.path(r, 'min_c').number.value).toBe(births0);
762
+ expect(result.data.path(r, 'max_c').number.value).toBe(births[r]);
763
+ expect(result.data.path(r, 'sum_c').number.value).toBe(births.slice(0, r + 1).reduce((a, b) => a + b));
764
+ expect(result.data.path(r, 'min_w').number.value).toBe(births0);
765
+ expect(result.data.path(r, 'max_w').number.value).toBe(births4);
766
+ expect(result.data.path(r, 'sum_w').number.value).toBe(births.reduce((a, b) => a + b));
767
+ }
768
+ });
769
+ });
770
+ });
771
+ afterAll(async () => {
772
+ await runtimes.closeAll();
773
+ });
35
774
  //# sourceMappingURL=functions.spec.js.map