@malloydata/malloy-tests 0.0.437 → 0.0.439

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.
@@ -404,6 +404,33 @@ describe.each(runtimes.runtimeList)(
404
404
  -> { select: odd is nums.odds.each }
405
405
  `).toEqualResult(testModel, [{odd: 1}, {odd: 3}]);
406
406
  });
407
+ // Both rows hold the same array, so the aggregates are only right if
408
+ // each element's key includes the row the record belongs to.
409
+ const twoRows = `${conName}.sql("""
410
+ SELECT 1 AS ${quote('id')} UNION ALL SELECT 2
411
+ """)`;
412
+ test('aggregate through array in a record column', async () => {
413
+ await expect(`
414
+ run: ${twoRows} -> { select: id, rec is {xs is [1, 2]} }
415
+ -> { aggregate: total is rec.xs.each.sum(), n is rec.xs.count() }
416
+ `).toMatchResult(testModel, {total: 6, n: 4});
417
+ });
418
+ test('aggregate through array in a record dimension', async () => {
419
+ await expect(`
420
+ run: ${twoRows} extend { dimension: rec is {xs is [1, 2]} }
421
+ -> { aggregate: total is rec.xs.each.sum(), n is rec.xs.count() }
422
+ `).toMatchResult(testModel, {total: 6, n: 4});
423
+ });
424
+ test('aggregate through array in a record in a record', async () => {
425
+ await expect(`
426
+ run: ${twoRows} -> { select: id, rec is {mid is {xs is [1, 2]}} }
427
+ -> {
428
+ aggregate:
429
+ total is rec.mid.xs.each.sum()
430
+ n is rec.mid.xs.count()
431
+ }
432
+ `).toMatchResult(testModel, {total: 6, n: 4});
433
+ });
407
434
  const abc = "rec is {a is 'a', bc is {b is 'b', c is 'c'}}";
408
435
  test('record with a record property', async () => {
409
436
  await expect(`
@@ -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,219 @@ 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
+ });
970
+
971
+ // An array the filtered join's `where:` reads is unnested inside its
972
+ // subquery; read from outside, it answers as the filtered source does on
973
+ // its own.
974
+ describe('an array the filtered join reads, read outside it', () => {
975
+ // Ten values, as the result matchers read at most ten rows
976
+ const countdown = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
977
+ const readArrays = `
978
+ source: sf is ${databaseName}.table('malloytest.state_facts')
979
+ source: by_state is ${databaseName}.table('malloytest.airports') -> {
980
+ group_by: state
981
+ nest: types is { group_by: fac_type; aggregate: c is count() }
982
+ }
983
+ source: big is by_state extend { where: types.c > 100 }
984
+ source: nums is sf -> { select: state, top is [1, 2, 3] }
985
+ source: big_nums is nums extend { where: top.each > 1 }
986
+ source: long is sf -> { select: state, la is [${countdown.join(', ')}] }
987
+ source: long_f is long extend { where: la.each > 0 }
988
+ source: zero is sf extend {
989
+ join_one: b is big on b.state = state
990
+ join_one: bn is big_nums on bn.state = state
991
+ join_one: lf is long_f on lf.state = state
992
+ }
993
+ `;
994
+
995
+ test.when(
996
+ runtime.dialect.supportsComplexFilteredSources &&
997
+ runtime.dialect.requiresExplicitUnnestOrdering
998
+ )('array of values keeps its order', async () => {
999
+ await expect(`
1000
+ ${readArrays}
1001
+ run: zero -> {
1002
+ where: state = 'AL'
1003
+ select: state, e is lf.la.each
1004
+ order_by: state
1005
+ }
1006
+ `).toEqualResult(
1007
+ testModel,
1008
+ countdown.filter(n => n > 0).map(e => ({state: 'AL', e}))
1009
+ );
1010
+ });
1011
+
1012
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
1013
+ 'array of records aggregated',
1014
+ async () => {
1015
+ await expect(`
1016
+ ${readArrays}
1017
+ run: zero -> {
1018
+ where: state ~ 'A%'
1019
+ group_by: state
1020
+ aggregate:
1021
+ rows is b.count()
1022
+ n is b.types.count()
1023
+ total is b.types.c.sum()
1024
+ order_by: state
1025
+ limit: 2
1026
+ }
1027
+ `).toMatchResult(
1028
+ testModel,
1029
+ {state: 'AK', rows: 1, n: 2, total: 578},
1030
+ {state: 'AL', rows: 1, n: 1, total: 180}
1031
+ );
1032
+ }
1033
+ );
1034
+
1035
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
1036
+ 'array of records selected',
1037
+ async () => {
1038
+ await expect(`
1039
+ ${readArrays}
1040
+ run: zero -> {
1041
+ where: state = 'AK'
1042
+ select: state, fac_type is b.types.fac_type, c is b.types.c
1043
+ order_by: fac_type
1044
+ }
1045
+ `).toEqualResult(testModel, [
1046
+ {state: 'AK', fac_type: 'AIRPORT', c: 474},
1047
+ {state: 'AK', fac_type: 'SEAPLANE BASE', c: 104},
1048
+ ]);
1049
+ }
1050
+ );
1051
+
1052
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
1053
+ 'array of values aggregated',
1054
+ async () => {
1055
+ await expect(`
1056
+ ${readArrays}
1057
+ run: zero -> {
1058
+ where: state = 'AL'
1059
+ aggregate:
1060
+ rows is bn.count()
1061
+ n is bn.top.count()
1062
+ total is bn.top.each.sum()
1063
+ }
1064
+ `).toMatchResult(testModel, {rows: 1, n: 2, total: 5});
1065
+ }
1066
+ );
1067
+
1068
+ test.when(runtime.dialect.supportsComplexFilteredSources)(
1069
+ 'array of values selected',
1070
+ async () => {
1071
+ await expect(`
1072
+ ${readArrays}
1073
+ run: zero -> {
1074
+ where: state = 'AL'
1075
+ select: state, e is bn.top.each
1076
+ order_by: e
1077
+ }
1078
+ `).toEqualResult(testModel, [
1079
+ {state: 'AL', e: 2},
1080
+ {state: 'AL', e: 3},
1081
+ ]);
1082
+ }
1083
+ );
1084
+ });
870
1085
  });
871
1086
 
872
1087
  test('joined filtered explores with NO dependencies', async () => {
@@ -1076,6 +1291,114 @@ describe.each(runtimes.runtimeList)('%s', (databaseName, runtime) => {
1076
1291
  no_paren: 'no',
1077
1292
  });
1078
1293
  });
1294
+
1295
+ describe('an operand with one operand of its own keeps its grouping', () => {
1296
+ const tm = mkTestModel(runtime, {nums: [{a: 7, c: 2}]});
1297
+
1298
+ test('negation beside a minus', async () => {
1299
+ await expect(`run: nums -> {
1300
+ select:
1301
+ minus_paren_neg is c - (-a)
1302
+ minus_neg is c - -a
1303
+ neg_paren_neg is -(-a)
1304
+ neg_neg is - -a
1305
+ }`).toMatchResult(tm, {
1306
+ minus_paren_neg: 9,
1307
+ minus_neg: 9,
1308
+ neg_paren_neg: 7,
1309
+ neg_neg: 7,
1310
+ });
1311
+ });
1312
+
1313
+ test.when(runtime.dialect.booleanType !== 'none')(
1314
+ 'a null check on the right of =',
1315
+ async () => {
1316
+ await expect(`run: nums -> {
1317
+ select:
1318
+ is_null is pick 'yes' when (a = 1) = (c is null) else 'no'
1319
+ is_not_null is pick 'yes' when (a = 1) = (c is not null) else 'no'
1320
+ }`).toMatchResult(tm, {is_null: 'yes', is_not_null: 'no'});
1321
+ }
1322
+ );
1323
+ });
1324
+
1325
+ describe('an expression written into other SQL keeps its grouping', () => {
1326
+ const tm = mkTestModel(runtime, {nums: [{a: 7, c: 2}]});
1327
+
1328
+ test('as the argument of a function', async () => {
1329
+ await expect(`run: nums -> {
1330
+ select:
1331
+ paren_divisor is div(10, (c + 3))
1332
+ bare_divisor is div(10, c + 3)
1333
+ dividend is div(a - 1, c)
1334
+ }`).toMatchResult(tm, {paren_divisor: 2, bare_divisor: 2, dividend: 3});
1335
+ });
1336
+
1337
+ test('as a computed field of a nested select', async () => {
1338
+ await expect(`run: nums -> {
1339
+ group_by: a
1340
+ nest: n is { select: m is a % c }
1341
+ }`).toMatchResult(tm, {a: 7, n: [{m: 1}]});
1342
+ });
1343
+
1344
+ test('as the value of a parameter', async () => {
1345
+ await expect(`
1346
+ ##! experimental.parameters
1347
+ source: nums_p(neg :: number is -5, diff :: number is 1 - 2) is nums extend {
1348
+ dimension:
1349
+ minus_neg is c - neg
1350
+ times_diff is c * diff
1351
+ }
1352
+ run: nums_p -> { select: minus_neg, times_diff }
1353
+ `).toMatchResult(tm, {minus_neg: 7, times_diff: -2});
1354
+ });
1355
+
1356
+ test('as the value of a given', async () => {
1357
+ await expect(`
1358
+ ##! experimental.givens
1359
+ given: NEG :: number is -5
1360
+ given: DIFF :: number is 1 - 2
1361
+ run: nums -> {
1362
+ select:
1363
+ minus_neg is c - $NEG
1364
+ times_diff is c * $DIFF
1365
+ }
1366
+ `).toMatchResult(tm, {minus_neg: 7, times_diff: -2});
1367
+ });
1368
+ });
1369
+
1370
+ test('a condition of a join is a single term beside the others', async () => {
1371
+ // Flags are numbers so that the same rows mean the same on every dialect
1372
+ const tm = mkTestModel(runtime, {
1373
+ rws: [
1374
+ {k: 1, ai: 1, bi: 9, fa: 0, fb: 0},
1375
+ {k: 2, ai: 2, bi: 9, fa: 1, fb: 0},
1376
+ ],
1377
+ });
1378
+ await expect(`
1379
+ source: only_a is rws extend { where: fa = 1 }
1380
+ source: two_filters is rws extend { where: (fa = 1 or fb = 1), k > 5 }
1381
+ run: rws extend {
1382
+ join_one: by_or is only_a on (by_or.k = ai or by_or.k = bi)
1383
+ join_one: filtered is two_filters on filtered.k = ai
1384
+ } -> {
1385
+ aggregate:
1386
+ or_matched is by_or.count()
1387
+ filter_matched is filtered.count()
1388
+ }
1389
+ `).toMatchResult(tm, {or_matched: 1, filter_matched: 0});
1390
+ });
1391
+
1392
+ test('a symmetric average is a single term of a larger expression', async () => {
1393
+ await expect(`
1394
+ run: ${databaseName}.table('malloytest.aircraft') extend {
1395
+ join_one: aircraft_models is ${databaseName}.table('malloytest.aircraft_models')
1396
+ on aircraft_model_code = aircraft_models.aircraft_model_code
1397
+ } -> {
1398
+ aggregate: back_to_100 is
1399
+ round(100 / aircraft_models.seats.avg() * aircraft_models.seats.avg())
1400
+ }`).toMatchResult(testModel, {back_to_100: 100});
1401
+ });
1079
1402
  });
1080
1403
 
1081
1404
  afterAll(async () => {