@malloydata/malloy-tests 0.0.436 → 0.0.438

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -87,6 +87,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
87
87
  select: nm; order_by: nm asc
88
88
  }`).toEqualResult(abcTestModel, got('def,xback,z-empty,z-null'));
89
89
  });
90
+ test('!~ abc', async () => {
91
+ await expect(`
92
+ run: abc -> {
93
+ where: s !~ f'abc',
94
+ select: nm; order_by: nm asc
95
+ }`).toEqualResult(abcTestModel, got('def,xback,z-empty,z-null'));
96
+ });
90
97
  test('-starts', async () => {
91
98
  await expect(`
92
99
  # test.verbose
@@ -323,6 +330,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
323
330
  select: t; order_by: t asc
324
331
  }`).toEqualResult(numsTestModel, [{t: '0'}, {t: '4'}]);
325
332
  });
333
+ test('!~ [1 to 3]', async () => {
334
+ await expect(`
335
+ run: nums -> {
336
+ where: n !~ f'[1 to 3]'
337
+ select: t; order_by: t asc
338
+ }`).toEqualResult(numsTestModel, [{t: '0'}, {t: '4'}, {t: 'null'}]);
339
+ });
326
340
  test('123', async () => {
327
341
  await expect(`
328
342
  run: nums -> {
@@ -437,6 +451,13 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
437
451
  select: t; order_by: t asc
438
452
  }`).toEqualResult(factsTestModel, [{t: 'true'}]);
439
453
  });
454
+ test.when(testBoolean)('!~ true', async () => {
455
+ await expect(`
456
+ run: ${factsSrc} -> {
457
+ where: b !~ f'true'
458
+ select: t; order_by: t asc
459
+ }`).toEqualResult(factsTestModel, [{t: 'false'}, {t: 'null'}]);
460
+ });
440
461
  test.when(testBoolean)('=true', async () => {
441
462
  await expect(`
442
463
  run: ${factsSrc} -> {
@@ -697,6 +718,12 @@ describe.each(runtimes.runtimeList)('filter expressions %s', (dbName, db) => {
697
718
  run: range + { where: t ~ f'2001' }
698
719
  `).toMatchRows(range, inRange);
699
720
  });
721
+ test('date !~ year', async () => {
722
+ const range = mkDateRange('2001-01-01', '2002-01-01');
723
+ await expect(`
724
+ run: range + { where: t !~ f'2001' }
725
+ `).toMatchRows(range, [...notInRange, {n: 'z-null'}]);
726
+ });
700
727
  test('date in month, in a query time zone east of UTC', async () => {
701
728
  // Midnight starting a month in Tokyo is still the previous day in UTC
702
729
  const range = mkDateRange('2001-01-01', '2001-02-01');
@@ -460,7 +460,9 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
460
460
 
461
461
  it('many_field.sum() has correct locality', async () => {
462
462
  await expect(`
463
- source: a is ${databaseName}.table('malloytest.aircraft')
463
+ source: a is ${databaseName}.table('malloytest.aircraft') extend {
464
+ primary_key: tail_num
465
+ }
464
466
  source: am is ${databaseName}.table('malloytest.aircraft_models') extend {
465
467
  join_many: a on a.aircraft_model_code = a.aircraft_model_code
466
468
  dimension: a_year_built is a.year_built
@@ -867,6 +869,104 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
867
869
  `).toMatchResult(testModel, {grandchild_state: 'AL'});
868
870
  }
869
871
  );
872
+ // An array is a column of the join above it: the subquery packs that
873
+ // column, and the array is unnested outside the subquery from it.
874
+ describe('an array two levels below', () => {
875
+ const arrayChain = (filterTwo: string) => `
876
+ source: sf is ${databaseName}.table('malloytest.state_facts')
877
+ source: three is ${databaseName}.table('malloytest.airports') -> {
878
+ group_by: state
879
+ nest: types is { group_by: fac_type; aggregate: c is count() }
880
+ }
881
+ source: two is sf extend {
882
+ ${filterTwo}
883
+ join_one: three on three.state = state
884
+ }
885
+ source: one is sf extend {
886
+ where: state ~ 'A%'
887
+ join_one: two on two.state = state
888
+ }
889
+ source: zero is sf extend { join_one: one on one.state = state }
890
+ `;
891
+
892
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
893
+ 'read and aggregated',
894
+ async () => {
895
+ await expect(`
896
+ ${arrayChain('')}
897
+ run: zero -> {
898
+ group_by: s is one.two.three.state
899
+ aggregate: n is one.two.three.types.count()
900
+ order_by: s
901
+ limit: 2
902
+ }
903
+ `).toMatchResult(testModel, {s: 'AK', n: 3}, {s: 'AL', n: 5});
904
+
905
+ await expect(`
906
+ ${arrayChain('')}
907
+ run: zero -> {
908
+ where: one.two.three.state = 'AK'
909
+ group_by: fac_type is one.two.three.types.fac_type
910
+ order_by: fac_type
911
+ limit: 1
912
+ }
913
+ `).toMatchResult(testModel, {fac_type: 'AIRPORT'});
914
+ }
915
+ );
916
+
917
+ // An array the filtered join reads itself, in its `where:` or in an
918
+ // `on` inside it, stays inside the subquery.
919
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
920
+ 'read by the filtered join itself',
921
+ async () => {
922
+ const twoArrays = `
923
+ source: sf is ${databaseName}.table('malloytest.state_facts')
924
+ source: three is ${databaseName}.table('malloytest.airports') -> {
925
+ group_by: state
926
+ nest: types is { group_by: fac_type; aggregate: c is count() }
927
+ }
928
+ source: two is sf extend {
929
+ join_one: three on three.state = state
930
+ join_one: sf2 is sf on sf2.state = three.types.fac_type
931
+ }`;
932
+ // Of Alabama's facility types, only airports number over 100
933
+ await expect(`
934
+ ${twoArrays}
935
+ source: one is two extend { where: three.types.c > 100 }
936
+ source: zero is sf extend { join_one: one on one.state = state }
937
+ run: zero -> {
938
+ where: state = 'AL'
939
+ select: state, x is one.three.state
940
+ }
941
+ `).toMatchResult(testModel, {state: 'AL', x: 'AL'});
942
+ // No facility type is a state's name
943
+ await expect(`
944
+ ${twoArrays}
945
+ source: one is two extend { where: state ~ 'A%' }
946
+ source: zero is sf extend { join_one: one on one.state = state }
947
+ run: zero -> {
948
+ where: state = 'AL'
949
+ aggregate: n is count(one.sf2.state)
950
+ }
951
+ `).toMatchResult(testModel, {n: 0});
952
+ }
953
+ );
954
+
955
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
956
+ 'below two filtered joins',
957
+ async () => {
958
+ await expect(`
959
+ ${arrayChain("where: state != 'AK'")}
960
+ run: zero -> {
961
+ group_by: s is one.two.three.state
962
+ aggregate: n is one.two.three.types.count()
963
+ order_by: s
964
+ limit: 1
965
+ }
966
+ `).toMatchResult(testModel, {s: 'AL', n: 5});
967
+ }
968
+ );
969
+ });
870
970
  });
871
971
 
872
972
  test('joined filtered explores with NO dependencies', async () => {
@@ -6,7 +6,12 @@
6
6
  import {RuntimeList, allDatabases, testFileSpace} from '../../runtimes';
7
7
  import {databasesFromEnvironmentOr} from '../../util';
8
8
  import '@malloydata/malloy/test/matchers';
9
- import {wrapTestModel} from '@malloydata/malloy/test';
9
+ import {
10
+ extendTestModel,
11
+ mkTestModel,
12
+ TV,
13
+ wrapTestModel,
14
+ } from '@malloydata/malloy/test';
10
15
 
11
16
  const runtimes = new RuntimeList(databasesFromEnvironmentOr(allDatabases));
12
17
 
@@ -350,4 +355,263 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
350
355
  testFileSpace.deleteFile(userURL);
351
356
  }
352
357
  });
358
+
359
+ // Carol is the customer no order matches: in the joined result she has one
360
+ // row, in which the order is all NULLs.
361
+ describe('aggregate at a joined source with an unmatched row', () => {
362
+ // float, because Databricks overflows on the fanned-out sum of a 32 bit
363
+ // integer column.
364
+ const num = TV.float;
365
+ const unmatched = extendTestModel(
366
+ mkTestModel(runtime, {
367
+ customers: [
368
+ {customer_id: 1, name: 'Alice', credit: num(100)},
369
+ {customer_id: 2, name: 'Bob', credit: num(400)},
370
+ {customer_id: 3, name: 'Carol', credit: num(5000)},
371
+ ],
372
+ order_rows: [
373
+ {order_id: 10, customer_id: 1, amount: num(5)},
374
+ {order_id: 11, customer_id: 1, amount: num(7)},
375
+ {order_id: 12, customer_id: 2, amount: num(11)},
376
+ ],
377
+ tags: [
378
+ {customer_id: 1, tag: 'a'},
379
+ {customer_id: 1, tag: 'b'},
380
+ {customer_id: 2, tag: 'c'},
381
+ ],
382
+ profiles: [
383
+ {customer_id: 1, score: num(7)},
384
+ {customer_id: 2, score: num(9)},
385
+ ],
386
+ }),
387
+ `
388
+ source: keyless_orders is order_rows extend {
389
+ measure:
390
+ order_count is count()
391
+ padded_total is sum((amount ?? 0) + 1)
392
+ tripled_avg is avg((amount ?? 0) * 3)
393
+ }
394
+ source: keyed_orders is keyless_orders extend { primary_key: order_id }
395
+ source: buyers is customers extend {
396
+ primary_key: customer_id
397
+ join_many: keyed is keyed_orders on keyed.customer_id = customer_id
398
+ join_many: keyless is keyless_orders on keyless.customer_id = customer_id
399
+ join_many: tags on tags.customer_id = customer_id
400
+ join_one: keyed_profile is profiles extend {
401
+ primary_key: customer_id
402
+ } on keyed_profile.customer_id = customer_id
403
+ join_one: keyless_profile is profiles
404
+ on keyless_profile.customer_id = customer_id
405
+ }
406
+ `
407
+ );
408
+
409
+ // A second join_many fans the orders out, which changes how every
410
+ // aggregate at the orders is written.
411
+ const shapes = [
412
+ {orders: 'keyed', fanOut: ''},
413
+ {orders: 'keyed', fanOut: 'tag_count is tags.count()'},
414
+ {orders: 'keyless', fanOut: ''},
415
+ {orders: 'keyless', fanOut: 'tag_count is tags.count()'},
416
+ ].map(s => ({...s, shape: `${s.orders}${s.fanOut ? ', fanned out' : ''}`}));
417
+
418
+ describe.each(shapes)('$shape', ({orders, fanOut}) => {
419
+ it('count, sum and avg skip the unmatched row', async () => {
420
+ await expect(`
421
+ run: buyers -> {
422
+ group_by: name
423
+ aggregate:
424
+ order_count is ${orders}.count()
425
+ big_credit_count is ${orders}.count() { where: credit > 100 }
426
+ credit_sum is ${orders}.sum(credit)
427
+ big_credit_sum is ${orders}.sum(credit) { where: credit > 100 }
428
+ credit_avg is ${orders}.avg(credit)
429
+ amount_plus_one is ${orders}.sum((${orders}.amount ?? 0) + 1)
430
+ amount_sum is ${orders}.amount.sum()
431
+ ${fanOut}
432
+ order_by: name
433
+ }
434
+ `).toMatchResult(
435
+ unmatched,
436
+ {
437
+ name: 'Alice',
438
+ order_count: 2,
439
+ big_credit_count: 0,
440
+ credit_sum: 200,
441
+ big_credit_sum: 0,
442
+ credit_avg: 100,
443
+ amount_plus_one: 14,
444
+ amount_sum: 12,
445
+ },
446
+ {
447
+ name: 'Bob',
448
+ order_count: 1,
449
+ big_credit_count: 1,
450
+ credit_sum: 400,
451
+ big_credit_sum: 400,
452
+ credit_avg: 400,
453
+ amount_plus_one: 12,
454
+ amount_sum: 11,
455
+ },
456
+ {
457
+ name: 'Carol',
458
+ order_count: 0,
459
+ big_credit_count: 0,
460
+ credit_sum: 0,
461
+ big_credit_sum: 0,
462
+ credit_avg: null,
463
+ amount_plus_one: 0,
464
+ amount_sum: 0,
465
+ }
466
+ );
467
+ });
468
+
469
+ it('a measure is the same through the join as on its own source', async () => {
470
+ const measures = ['order_count', 'padded_total', 'tripled_avg'];
471
+ const onItsOwn = await unmatched.model
472
+ .loadQuery(
473
+ `run: ${orders}_orders -> { aggregate: ${measures.join(', ')} }`
474
+ )
475
+ .run();
476
+ await expect(`
477
+ run: buyers -> {
478
+ aggregate:
479
+ ${measures.map(m => `${orders}.${m}`).join('\n')}
480
+ ${fanOut}
481
+ }
482
+ `).toMatchResult(unmatched, onItsOwn.data.toObject()[0]);
483
+ });
484
+
485
+ it('totals skip the unmatched row', async () => {
486
+ await expect(`
487
+ run: buyers -> {
488
+ aggregate:
489
+ order_count is ${orders}.count()
490
+ credit_sum is ${orders}.sum(credit)
491
+ credit_avg is ${orders}.avg(credit)
492
+ customer_count is count()
493
+ ${fanOut}
494
+ }
495
+ `).toMatchResult(unmatched, {
496
+ order_count: 3,
497
+ credit_sum: 600,
498
+ credit_avg: 200,
499
+ customer_count: 3,
500
+ });
501
+ });
502
+
503
+ const canStringAgg =
504
+ fanOut === '' ||
505
+ !['snowflake', 'mysql', 'sqlserver'].includes(databaseName);
506
+ it.when(canStringAgg)(
507
+ 'an aggregate function skips the unmatched row',
508
+ async () => {
509
+ await expect(`
510
+ run: buyers -> {
511
+ group_by: name
512
+ aggregate:
513
+ name_chars is length(${orders}.string_agg(name, ',')) ?? 0
514
+ ${fanOut}
515
+ order_by: name
516
+ }
517
+ `).toMatchResult(
518
+ unmatched,
519
+ {name: 'Alice', name_chars: 11},
520
+ {name: 'Bob', name_chars: 3},
521
+ {name: 'Carol', name_chars: 0}
522
+ );
523
+ }
524
+ );
525
+ });
526
+
527
+ describe.each(['keyed_profile', 'keyless_profile'])(
528
+ 'join_one %s',
529
+ profile => {
530
+ it('count, sum and avg skip the unmatched row', async () => {
531
+ await expect(`
532
+ run: buyers -> {
533
+ group_by: name
534
+ aggregate:
535
+ profile_count is ${profile}.count()
536
+ credit_sum is ${profile}.sum(credit)
537
+ credit_avg is ${profile}.avg(credit)
538
+ score_plus_one is ${profile}.sum((${profile}.score ?? 0) + 1)
539
+ order_by: name
540
+ }
541
+ `).toMatchResult(
542
+ unmatched,
543
+ {
544
+ name: 'Alice',
545
+ profile_count: 1,
546
+ credit_sum: 100,
547
+ credit_avg: 100,
548
+ score_plus_one: 8,
549
+ },
550
+ {
551
+ name: 'Bob',
552
+ profile_count: 1,
553
+ credit_sum: 400,
554
+ credit_avg: 400,
555
+ score_plus_one: 10,
556
+ },
557
+ {
558
+ name: 'Carol',
559
+ profile_count: 0,
560
+ credit_sum: 0,
561
+ credit_avg: null,
562
+ score_plus_one: 0,
563
+ }
564
+ );
565
+ });
566
+ }
567
+ );
568
+
569
+ // An empty array unnests to one unmatched row.
570
+ it.when(databaseName === 'duckdb')(
571
+ 'an aggregate at an array skips an empty array',
572
+ async () => {
573
+ await expect(`
574
+ run: ${databaseName}.sql("""
575
+ SELECT 'Alice' as name, 100 as credit, [5, 7] as amounts
576
+ UNION ALL SELECT 'Carol', 5000, []::INTEGER[]
577
+ """) -> {
578
+ group_by: name
579
+ aggregate:
580
+ amount_count is amounts.count()
581
+ credit_sum is amounts.sum(credit)
582
+ credit_avg is amounts.avg(credit)
583
+ order_by: name
584
+ }
585
+ `).toMatchResult(
586
+ unmatched,
587
+ {name: 'Alice', amount_count: 2, credit_sum: 200, credit_avg: 100},
588
+ {name: 'Carol', amount_count: 0, credit_sum: 0, credit_avg: null}
589
+ );
590
+ }
591
+ );
592
+
593
+ // A right join leaves the query's own source unmatched: Carol's row has
594
+ // no order in it.
595
+ it('the query source skips its own unmatched row', async () => {
596
+ await expect(`
597
+ ##! experimental.join_types
598
+ run: keyed_orders -> {
599
+ extend: {
600
+ join_one: customers right on customers.customer_id = customer_id
601
+ }
602
+ group_by: customers.name
603
+ aggregate:
604
+ order_count is count()
605
+ ones is sum(1)
606
+ amount_avg is avg(amount ?? 0)
607
+ order_by: name
608
+ }
609
+ `).toMatchResult(
610
+ unmatched,
611
+ {name: 'Alice', order_count: 2, ones: 2, amount_avg: 6},
612
+ {name: 'Bob', order_count: 1, ones: 1, amount_avg: 11},
613
+ {name: 'Carol', order_count: 0, ones: 0, amount_avg: null}
614
+ );
615
+ });
616
+ });
353
617
  });
@@ -234,4 +234,38 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
234
234
  expect(row['capped']).not.toBe(null);
235
235
  }
236
236
  );
237
+
238
+ // Alaska's three facility types: 474 airports, 104 seaplane bases and 30
239
+ // heliports.
240
+ const alaskaByFacType = (nest: string) => `
241
+ run: ${databaseName}.table('malloytest.airports') -> {
242
+ where: state = 'AK'
243
+ group_by: state
244
+ nest: by_fac_type is { ${nest} }
245
+ }`;
246
+
247
+ // Counting a nest's elements reads each element's distinct key back out of
248
+ // the nest.
249
+ test.when(runtime.supportsNesting)(
250
+ `the elements of a nest can be counted in the next stage - ${databaseName}`,
251
+ async () => {
252
+ await expect(`
253
+ ${alaskaByFacType('group_by: fac_type; aggregate: c is count()')} -> {
254
+ aggregate:
255
+ fac_types is by_fac_type.count()
256
+ airports is by_fac_type.c.sum()
257
+ }`).toMatchResult(model, {fac_types: 3, airports: 608});
258
+ }
259
+ );
260
+
261
+ test.when(runtime.supportsNesting)(
262
+ `a boolean read back out of a nest is a boolean - ${databaseName}`,
263
+ async () => {
264
+ await expect(`
265
+ ${alaskaByFacType("group_by: fac_type, is_airport is fac_type = 'AIRPORT'")} -> {
266
+ where: by_fac_type.is_airport = true
267
+ group_by: by_fac_type.fac_type
268
+ }`).toEqualResult(model, [{fac_type: 'AIRPORT'}]);
269
+ }
270
+ );
237
271
  });
@@ -263,7 +263,11 @@ describe.each(runtimes.runtimeList)(
263
263
  aggregate: aircraft.aircraft_count
264
264
  aggregate: model_count
265
265
  }
266
- `).toMatchResult(joinTestModel, {model_count: 59104});
266
+ `).toMatchResult(joinTestModel, {
267
+ state: 'CA',
268
+ aircraft_count: 367,
269
+ model_count: 277,
270
+ });
267
271
  });
268
272
 
269
273
  it(`hand join symmetric agg - ${databaseName}`, async () => {
@@ -274,7 +278,7 @@ describe.each(runtimes.runtimeList)(
274
278
  }
275
279
  `).toMatchResult(joinTestModel, {
276
280
  total_seats: 452415,
277
- aircraft_count: 62644,
281
+ aircraft_count: 3599,
278
282
  });
279
283
  });
280
284
  }
@@ -90,7 +90,7 @@ describe('Wildcard BigQuery Tables', () => {
90
90
  {state: 'OK', aircraft_count: 1},
91
91
  {state: 'OR', aircraft_count: 2},
92
92
  {state: 'TX', aircraft_count: 1},
93
- {state: null, aircraft_count: 43},
93
+ {state: null, aircraft_count: 0},
94
94
  ]);
95
95
  }
96
96
  });
@@ -123,7 +123,7 @@ describe('Wildcard BigQuery Tables', () => {
123
123
  {state: 'LA', aircraft_count: 1},
124
124
  {state: 'OK', aircraft_count: 1},
125
125
  {state: 'OR', aircraft_count: 1},
126
- {state: null, aircraft_count: 47},
126
+ {state: null, aircraft_count: 0},
127
127
  ]);
128
128
  }
129
129
  });
@@ -180,7 +180,7 @@ describe('Wildcard BigQuery Tables', () => {
180
180
  .run();
181
181
  expect(result.data.value).toStrictEqual([
182
182
  {_TABLE_SUFFIX: '02', aircraft_count: 4},
183
- {_TABLE_SUFFIX: null, aircraft_count: 47},
183
+ {_TABLE_SUFFIX: null, aircraft_count: 0},
184
184
  ]);
185
185
  }
186
186
  });
@@ -116,6 +116,28 @@ describe('Postgres tests', () => {
116
116
  }
117
117
  `).toMatchResult(testModel, {tenx: 10});
118
118
  });
119
+ it('keeps NUMERIC precision in symmetric aggregates', async () => {
120
+ // The join_many makes amt.sum() a symmetric aggregate. Its exact total,
121
+ // 1234567890123456.78, has more digits than a DOUBLE PRECISION holds; the
122
+ // second stage keeps only the cents, so the result fits in a JS number.
123
+ await expect(`
124
+ source: big is postgres.sql("""
125
+ SELECT 1 AS id, 1234567890123456.77::NUMERIC(18,2) AS amt
126
+ UNION ALL SELECT 2, 0.01
127
+ """) extend {
128
+ primary_key: id
129
+ join_many: dup is postgres.sql("""
130
+ SELECT 1 AS id UNION ALL SELECT 1 UNION ALL SELECT 2
131
+ """) on id = dup.id
132
+ }
133
+ run: big -> {
134
+ aggregate: total is amt.sum(), dup_rows is dup.count()
135
+ } -> {
136
+ select: cents is (total - 1234567890123456) * 100, dup_rows
137
+ }
138
+ `).toMatchResult(testModel, {cents: 78, dup_rows: 3});
139
+ });
140
+
119
141
  describe('all-aggregates query with joins and nested views', () => {
120
142
  // Reproduces a bug where an all-aggregates query on a source with
121
143
  // joins and nested views throws on Postgres. Postgres simulates
package/src/runtimes.ts CHANGED
@@ -296,6 +296,7 @@ export function runtimeFor(dbName: string): SingleConnectionRuntime {
296
296
  token: process.env['DATABRICKS_TOKEN'],
297
297
  defaultCatalog: process.env['DATABRICKS_CATALOG'],
298
298
  defaultSchema: process.env['DATABRICKS_SCHEMA'],
299
+ proxy: process.env['DATABRICKS_HTTPS_PROXY'],
299
300
  });
300
301
  }
301
302
  break;