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