@malloydata/malloy-query-builder 0.0.240-dev250305162504 → 0.0.240-dev250305163014

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.
@@ -27,61 +27,67 @@ declare class ASTNode<T> {
27
27
  _parentASTAny | void;
28
28
  build(): T;
29
29
  edit(): this;
30
- find(path: Path): ASTAny;
31
- asQuery(): ASTQuery;
32
- findQuery(path: Path): ASTQuery;
33
- asReference(): ASTReference;
34
- findReference(path: Path): ASTReference;
35
- asSourceReference(): ASTSourceReference;
36
- findSourceReference(path: Path): ASTSourceReference;
37
- asParameterValueList(): ASTParameterValueList;
38
- findParameterValueList(path: Path): ASTParameterValueList;
39
- asWhere(): ASTWhere;
40
- findWhere(path: Path): ASTWhere;
41
- asWhereList(): ASTWhereList;
42
- findWhereList(path: Path): ASTWhereList;
43
- asParameterValue(): ASTParameterValue;
44
- findParameterValue(path: Path): ASTParameterValue;
45
- asStringLiteralValue(): ASTStringLiteralValue;
46
- findStringLiteralValue(path: Path): ASTStringLiteralValue;
47
- asNumberLiteralValue(): ASTNumberLiteralValue;
48
- findNumberLiteralValue(path: Path): ASTNumberLiteralValue;
49
- asViewOperationList(): ASTViewOperationList;
50
- findViewOperationList(path: Path): ASTViewOperationList;
51
- asGroupByViewOperation(): ASTGroupByViewOperation;
52
- findGroupByViewOperation(path: Path): ASTGroupByViewOperation;
53
- asAggregateViewOperation(): ASTAggregateViewOperation;
54
- findAggregateViewOperation(path: Path): ASTAggregateViewOperation;
55
- asOrderByViewOperation(): ASTOrderByViewOperation;
56
- findOrderByViewOperation(path: Path): ASTOrderByViewOperation;
57
- asField(): ASTField;
58
- findField(path: Path): ASTField;
59
- asReferenceExpression(): ASTReferenceExpression;
60
- findReferenceExpression(path: Path): ASTReferenceExpression;
61
- asReferenceViewDefinition(): ASTReferenceViewDefinition;
62
- findReferenceViewDefinition(path: Path): ASTReferenceViewDefinition;
63
- asArrowQueryDefinition(): ASTArrowQueryDefinition;
64
- findArrowQueryDefinition(path: Path): ASTArrowQueryDefinition;
65
- asArrowViewDefinition(): ASTArrowViewDefinition;
66
- findArrowViewDefinition(path: Path): ASTArrowViewDefinition;
67
- asRefinementViewDefinition(): ASTRefinementViewDefinition;
68
- findRefinementViewDefinition(path: Path): ASTRefinementViewDefinition;
69
- asTimeTruncationExpression(): ASTTimeTruncationExpression;
70
- findTimeTruncationExpression(path: Path): ASTTimeTruncationExpression;
71
- asFilteredFieldExpression(): ASTFilteredFieldExpression;
72
- findFilteredFieldExpression(path: Path): ASTFilteredFieldExpression;
73
- asNestViewOperation(): ASTNestViewOperation;
74
- findNestViewOperation(path: Path): ASTNestViewOperation;
75
- asView(): ASTView;
76
- findView(path: Path): ASTView;
77
- asSegmentViewDefinition(): ASTSegmentViewDefinition;
78
- findSegmentViewDefinition(path: Path): ASTSegmentViewDefinition;
79
- asLimitViewOperation(): ASTLimitViewOperation;
80
- findLimitViewOperation(path: Path): ASTLimitViewOperation;
81
- asAnnotationList(): ASTAnnotationList;
82
- findAnnotationList(path: Path): ASTAnnotationList;
83
- asAnnotation(): ASTAnnotation;
84
- findAnnotation(path: Path): ASTAnnotation;
30
+ findAny(path: Path): ASTAny;
31
+ get as(): {
32
+ Query(): ASTQuery,
33
+ Reference(): ASTReference,
34
+ SourceReference(): ASTSourceReference,
35
+ ParameterValueList(): ASTParameterValueList,
36
+ Where(): ASTWhere,
37
+ WhereList(): ASTWhereList,
38
+ ParameterValue(): ASTParameterValue,
39
+ StringLiteralValue(): ASTStringLiteralValue,
40
+ NumberLiteralValue(): ASTNumberLiteralValue,
41
+ ViewOperationList(): ASTViewOperationList,
42
+ GroupByViewOperation(): ASTGroupByViewOperation,
43
+ AggregateViewOperation(): ASTAggregateViewOperation,
44
+ OrderByViewOperation(): ASTOrderByViewOperation,
45
+ Field(): ASTField,
46
+ ReferenceExpression(): ASTReferenceExpression,
47
+ ReferenceViewDefinition(): ASTReferenceViewDefinition,
48
+ ArrowQueryDefinition(): ASTArrowQueryDefinition,
49
+ ArrowViewDefinition(): ASTArrowViewDefinition,
50
+ RefinementViewDefinition(): ASTRefinementViewDefinition,
51
+ TimeTruncationExpression(): ASTTimeTruncationExpression,
52
+ FilteredFieldExpression(): ASTFilteredFieldExpression,
53
+ NestViewOperation(): ASTNestViewOperation,
54
+ View(): ASTView,
55
+ SegmentViewDefinition(): ASTSegmentViewDefinition,
56
+ LimitViewOperation(): ASTLimitViewOperation,
57
+ AnnotationList(): ASTAnnotationList,
58
+ Annotation(): ASTAnnotation,
59
+ ...
60
+ };
61
+ get find(): {
62
+ Query(path: Path): ASTQuery,
63
+ Reference(path: Path): ASTReference,
64
+ SourceReference(path: Path): ASTSourceReference,
65
+ ParameterValueList(path: Path): ASTParameterValueList,
66
+ Where(path: Path): ASTWhere,
67
+ WhereList(path: Path): ASTWhereList,
68
+ ParameterValue(path: Path): ASTParameterValue,
69
+ StringLiteralValue(path: Path): ASTStringLiteralValue,
70
+ NumberLiteralValue(path: Path): ASTNumberLiteralValue,
71
+ ViewOperationList(path: Path): ASTViewOperationList,
72
+ GroupByViewOperation(path: Path): ASTGroupByViewOperation,
73
+ AggregateViewOperation(path: Path): ASTAggregateViewOperation,
74
+ OrderByViewOperation(path: Path): ASTOrderByViewOperation,
75
+ Field(path: Path): ASTField,
76
+ ReferenceExpression(path: Path): ASTReferenceExpression,
77
+ ReferenceViewDefinition(path: Path): ASTReferenceViewDefinition,
78
+ ArrowQueryDefinition(path: Path): ASTArrowQueryDefinition,
79
+ ArrowViewDefinition(path: Path): ASTArrowViewDefinition,
80
+ RefinementViewDefinition(path: Path): ASTRefinementViewDefinition,
81
+ TimeTruncationExpression(path: Path): ASTTimeTruncationExpression,
82
+ FilteredFieldExpression(path: Path): ASTFilteredFieldExpression,
83
+ NestViewOperation(path: Path): ASTNestViewOperation,
84
+ View(path: Path): ASTView,
85
+ SegmentViewDefinition(path: Path): ASTSegmentViewDefinition,
86
+ LimitViewOperation(path: Path): ASTLimitViewOperation,
87
+ AnnotationList(path: Path): ASTAnnotationList,
88
+ Annotation(path: Path): ASTAnnotation,
89
+ ...
90
+ };
85
91
  get parent(): ASTAny;
86
92
  set parent(parent: ASTAny): void;
87
93
  schemaTryGet(
@@ -95,6 +101,8 @@ declare class ASTNode<T> {
95
101
  path: string[] | void,
96
102
  ): Malloy.FieldInfo;
97
103
  schemaMerge(a: Malloy.Schema, b: Malloy.Schema): Malloy.Schema;
104
+ tagFor(a: Malloy.FieldInfo, prefix?: string): Tag;
105
+ fieldWasCalculation(a: Malloy.FieldInfo): boolean;
98
106
  }
99
107
  declare class ASTListNode<T, N: ASTNode<T> = ASTNode<T>> extends ASTNode<T[]> {
100
108
  nodeT[];
@@ -109,7 +117,7 @@ declare class ASTListNode<T, N: ASTNode<T> = ASTNode<T>> extends ASTNode<T[]> {
109
117
  add(n: N): void;
110
118
  remove(n: N): this | void;
111
119
  build(): T[];
112
- find(path: Path): ASTAny;
120
+ findAny(path: Path): ASTAny;
113
121
  findIndex(predicate: (n: N) => boolean): number;
114
122
  indexOf(n: N): number;
115
123
  }
@@ -118,7 +126,7 @@ declare class ASTObjectNode<T, Children: ASTChildren<T>> extends ASTNode<T> {
118
126
  childrenChildren;
119
127
  constructor(node: T, children: Children): void;
120
128
  build(): T;
121
- find(path: Path): ASTAny;
129
+ findAny(path: Path): ASTAny;
122
130
  }
123
131
  declare export class ASTQuery
124
132
  extends
@@ -209,6 +217,7 @@ declare export class ASTReference
209
217
  }
210
218
  declare export class ASTFieldReference extends ASTReference {
211
219
  get segment(): ASTSegmentViewDefinition;
220
+ getReferenceSchemaany;
212
221
  getFieldInfo(): Malloy.FieldInfo;
213
222
  }
214
223
  declare export class ASTSourceReference extends ASTReference {
@@ -336,7 +345,7 @@ declare export class ASTUnimplemented<T> extends ASTNode<T> {
336
345
  constructor(node: T): void;
337
346
  get treeEdited(): boolean;
338
347
  build(): T;
339
- find(): empty;
348
+ findAny(): empty;
340
349
  }
341
350
  declare export interface IASTQueryOrViewDefinition {
342
351
  propagateUp(f: PropagationFunction): void;
@@ -599,7 +608,10 @@ declare export class ASTSegmentViewDefinition
599
608
  | ASTNestViewOperation,
600
609
  name: string,
601
610
  ): void;
602
- addOrderBy(name: string, direction?: Malloy.OrderByDirection): void;
611
+ addOrderBy(
612
+ name: string,
613
+ direction?: Malloy.OrderByDirection,
614
+ ): ASTOrderByViewOperation;
603
615
  addEmptyNest(name: string): ASTNestViewOperation;
604
616
  firstIndexOfOperationTypeany;
605
617
  DEFAULT_INSERTION_ORDERany;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy-query-builder",
3
- "version": "0.0.240-dev250305162504",
3
+ "version": "0.0.240-dev250305163014",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,9 +22,9 @@
22
22
  "serve-docs": "npx http-server -o docs"
23
23
  },
24
24
  "dependencies": {
25
- "@malloydata/malloy-filter": "^0.0.240-dev250305162504",
26
- "@malloydata/malloy-interfaces": "^0.0.240-dev250305162504",
27
- "@malloydata/malloy-tag": "^0.0.240-dev250305162504"
25
+ "@malloydata/malloy-filter": "^0.0.240-dev250305163014",
26
+ "@malloydata/malloy-interfaces": "^0.0.240-dev250305163014",
27
+ "@malloydata/malloy-tag": "^0.0.240-dev250305163014"
28
28
  },
29
29
  "devDependencies": {
30
30
  "flow-api-translator": "^0.26.0",
@@ -8,7 +8,7 @@
8
8
  import * as Malloy from '@malloydata/malloy-interfaces';
9
9
  import {flights_model} from './flights_model';
10
10
  import './expects';
11
- import {ASTQuery} from './query-ast';
11
+ import {ASTOrderByViewOperation, ASTQuery} from './query-ast';
12
12
 
13
13
  function dedent(strs: TemplateStringsArray) {
14
14
  const str = strs.join('');
@@ -239,55 +239,101 @@ describe('query builder', () => {
239
239
  `,
240
240
  });
241
241
  });
242
- test('add an aggregate with a where', () => {
243
- const from: Malloy.Query = {
244
- definition: {
245
- kind: 'arrow',
246
- source_reference: {name: 'flights'},
247
- view: {
248
- kind: 'segment',
249
- operations: [],
242
+ describe('aggregate', () => {
243
+ test('added aggregate should have calculation annotation', () => {
244
+ const from: Malloy.Query = {
245
+ definition: {
246
+ kind: 'arrow',
247
+ source_reference: {name: 'flights'},
248
+ view: {
249
+ kind: 'segment',
250
+ operations: [],
251
+ },
250
252
  },
251
- },
252
- };
253
- expect((q: ASTQuery) => {
254
- q.getOrAddDefaultSegment()
255
- .addAggregate('flight_count')
256
- .addWhere('carrier', 'WN, AA');
257
- }).toModifyQuery({
258
- model: flights_model,
259
- from,
260
- to: {
253
+ };
254
+ expect((q: ASTQuery) => {
255
+ const aggregate = q
256
+ .getOrAddDefaultSegment()
257
+ .addAggregate('flight_count');
258
+ expect(ASTQuery.fieldWasCalculation(aggregate.getFieldInfo())).toBe(
259
+ true
260
+ );
261
+ }).toModifyQuery({
262
+ model: flights_model,
263
+ from,
264
+ to: {
265
+ definition: {
266
+ kind: 'arrow',
267
+ source_reference: {name: 'flights'},
268
+ view: {
269
+ kind: 'segment',
270
+ operations: [
271
+ {
272
+ kind: 'aggregate',
273
+ field: {
274
+ expression: {
275
+ kind: 'field_reference',
276
+ name: 'flight_count',
277
+ },
278
+ },
279
+ },
280
+ ],
281
+ },
282
+ },
283
+ },
284
+ malloy: 'run: flights -> { aggregate: flight_count }',
285
+ });
286
+ });
287
+ test('add an aggregate with a where', () => {
288
+ const from: Malloy.Query = {
261
289
  definition: {
262
290
  kind: 'arrow',
263
291
  source_reference: {name: 'flights'},
264
292
  view: {
265
293
  kind: 'segment',
266
- operations: [
267
- {
268
- kind: 'aggregate',
269
- field: {
270
- expression: {
271
- kind: 'filtered_field',
272
- field_reference: {name: 'flight_count'},
273
- where: [
274
- {
275
- filter: {
276
- kind: 'filter_string',
277
- field_reference: {name: 'carrier'},
278
- filter: 'WN, AA',
294
+ operations: [],
295
+ },
296
+ },
297
+ };
298
+ expect((q: ASTQuery) => {
299
+ q.getOrAddDefaultSegment()
300
+ .addAggregate('flight_count')
301
+ .addWhere('carrier', 'WN, AA');
302
+ }).toModifyQuery({
303
+ model: flights_model,
304
+ from,
305
+ to: {
306
+ definition: {
307
+ kind: 'arrow',
308
+ source_reference: {name: 'flights'},
309
+ view: {
310
+ kind: 'segment',
311
+ operations: [
312
+ {
313
+ kind: 'aggregate',
314
+ field: {
315
+ expression: {
316
+ kind: 'filtered_field',
317
+ field_reference: {name: 'flight_count'},
318
+ where: [
319
+ {
320
+ filter: {
321
+ kind: 'filter_string',
322
+ field_reference: {name: 'carrier'},
323
+ filter: 'WN, AA',
324
+ },
279
325
  },
280
- },
281
- ],
326
+ ],
327
+ },
282
328
  },
283
329
  },
284
- },
285
- ],
330
+ ],
331
+ },
286
332
  },
287
333
  },
288
- },
289
- malloy:
290
- 'run: flights -> { aggregate: flight_count { where: carrier ~ f`WN, AA` } }',
334
+ malloy:
335
+ 'run: flights -> { aggregate: flight_count { where: carrier ~ f`WN, AA` } }',
336
+ });
291
337
  });
292
338
  });
293
339
  test('add a where', () => {
@@ -834,46 +880,168 @@ describe('query builder', () => {
834
880
  malloy: 'run: flights -> by_month + { limit: 10 }',
835
881
  });
836
882
  });
837
- test('add an order by in a refinement', () => {
838
- const from: Malloy.Query = {
839
- definition: {
840
- kind: 'arrow',
841
- source_reference: {name: 'flights'},
842
- view: {
843
- kind: 'view_reference',
844
- name: 'by_month',
845
- },
846
- },
847
- };
848
- expect((q: ASTQuery) => {
849
- q.getOrAddDefaultSegment().addOrderBy('dep_month', 'asc');
850
- }).toModifyQuery({
851
- model: flights_model,
852
- from,
853
- to: {
883
+ describe('add order by', () => {
884
+ test('add an order by to a complex refinement', () => {
885
+ const from: Malloy.Query = {
854
886
  definition: {
855
887
  kind: 'arrow',
856
- source_reference: {name: 'flights'},
888
+ source_reference: {
889
+ name: 'flights',
890
+ },
857
891
  view: {
858
892
  kind: 'refinement',
859
893
  base: {
860
894
  kind: 'view_reference',
861
- name: 'by_month',
895
+ name: 'top_carriers',
862
896
  },
863
897
  refinement: {
864
898
  kind: 'segment',
865
899
  operations: [
866
900
  {
867
- kind: 'order_by',
868
- field_reference: {name: 'dep_month'},
869
- direction: 'asc',
901
+ kind: 'group_by',
902
+ field: {
903
+ expression: {
904
+ kind: 'field_reference',
905
+ name: 'carrier',
906
+ path: [],
907
+ },
908
+ },
909
+ },
910
+ {
911
+ kind: 'nest',
912
+ name: 'by_month',
913
+ view: {
914
+ definition: {
915
+ kind: 'refinement',
916
+ base: {
917
+ kind: 'view_reference',
918
+ name: 'by_month',
919
+ },
920
+ refinement: {
921
+ kind: 'segment',
922
+ operations: [],
923
+ },
924
+ },
925
+ },
870
926
  },
871
927
  ],
872
928
  },
873
929
  },
874
930
  },
875
- },
876
- malloy: 'run: flights -> by_month + { order_by: dep_month asc }',
931
+ };
932
+ expect((q: ASTQuery) => {
933
+ q.getOrAddDefaultSegment().addOrderBy('carrier');
934
+ }).toModifyQuery({
935
+ model: flights_model,
936
+ from,
937
+ to: {
938
+ definition: {
939
+ kind: 'arrow',
940
+ source_reference: {
941
+ name: 'flights',
942
+ },
943
+ view: {
944
+ kind: 'refinement',
945
+ base: {
946
+ kind: 'view_reference',
947
+ name: 'top_carriers',
948
+ },
949
+ refinement: {
950
+ kind: 'segment',
951
+ operations: [
952
+ {
953
+ kind: 'group_by',
954
+ field: {
955
+ expression: {
956
+ kind: 'field_reference',
957
+ name: 'carrier',
958
+ path: [],
959
+ },
960
+ },
961
+ },
962
+ {
963
+ kind: 'nest',
964
+ name: 'by_month',
965
+ view: {
966
+ definition: {
967
+ kind: 'refinement',
968
+ base: {
969
+ kind: 'view_reference',
970
+ name: 'by_month',
971
+ },
972
+ refinement: {
973
+ kind: 'segment',
974
+ operations: [],
975
+ },
976
+ },
977
+ },
978
+ },
979
+ {
980
+ kind: 'order_by',
981
+ field_reference: {
982
+ name: 'carrier',
983
+ },
984
+ },
985
+ ],
986
+ },
987
+ },
988
+ },
989
+ },
990
+ malloy: dedent`
991
+ run: flights -> top_carriers + {
992
+ group_by: carrier
993
+ nest: by_month is by_month + { }
994
+ order_by: carrier
995
+ }
996
+ `,
997
+ });
998
+ });
999
+ test('add an order by in a refinement', () => {
1000
+ let orderBy: ASTOrderByViewOperation | undefined;
1001
+ const from: Malloy.Query = {
1002
+ definition: {
1003
+ kind: 'arrow',
1004
+ source_reference: {name: 'flights'},
1005
+ view: {
1006
+ kind: 'view_reference',
1007
+ name: 'by_month',
1008
+ },
1009
+ },
1010
+ };
1011
+ expect((q: ASTQuery) => {
1012
+ orderBy = q.getOrAddDefaultSegment().addOrderBy('dep_month', 'asc');
1013
+ }).toModifyQuery({
1014
+ model: flights_model,
1015
+ from,
1016
+ to: {
1017
+ definition: {
1018
+ kind: 'arrow',
1019
+ source_reference: {name: 'flights'},
1020
+ view: {
1021
+ kind: 'refinement',
1022
+ base: {
1023
+ kind: 'view_reference',
1024
+ name: 'by_month',
1025
+ },
1026
+ refinement: {
1027
+ kind: 'segment',
1028
+ operations: [
1029
+ {
1030
+ kind: 'order_by',
1031
+ field_reference: {name: 'dep_month'},
1032
+ direction: 'asc',
1033
+ },
1034
+ ],
1035
+ },
1036
+ },
1037
+ },
1038
+ },
1039
+ malloy: 'run: flights -> by_month + { order_by: dep_month asc }',
1040
+ });
1041
+ expect(orderBy?.fieldReference.getFieldInfo()).toMatchObject({
1042
+ 'kind': 'dimension',
1043
+ 'name': 'dep_month',
1044
+ });
877
1045
  });
878
1046
  });
879
1047
  test('do nothing', () => {
@@ -914,8 +1082,8 @@ describe('query builder', () => {
914
1082
  },
915
1083
  };
916
1084
  expect((q: ASTQuery) => {
917
- q.definition
918
- .asArrowQueryDefinition()
1085
+ q.definition.as
1086
+ .ArrowQueryDefinition()
919
1087
  .view.addViewRefinement('cool_state_measures');
920
1088
  }).toModifyQuery({
921
1089
  source: {
@@ -1302,10 +1470,10 @@ describe('query builder', () => {
1302
1470
  },
1303
1471
  };
1304
1472
  expect((q: ASTQuery) => {
1305
- const segment = q.definition
1306
- .asArrowQueryDefinition()
1307
- .view.asRefinementViewDefinition()
1308
- .base.asSegmentViewDefinition();
1473
+ const segment = q.definition.as
1474
+ .ArrowQueryDefinition()
1475
+ .view.as.RefinementViewDefinition()
1476
+ .base.as.SegmentViewDefinition();
1309
1477
  segment.getGroupBy('carrier')!.rename('carrier_2');
1310
1478
  }).toModifyQuery({
1311
1479
  model: flights_model,
@@ -1381,10 +1549,10 @@ describe('query builder', () => {
1381
1549
  },
1382
1550
  };
1383
1551
  expect((q: ASTQuery) => {
1384
- const segment = q.definition
1385
- .asArrowQueryDefinition()
1386
- .view.asRefinementViewDefinition()
1387
- .base.asSegmentViewDefinition();
1552
+ const segment = q.definition.as
1553
+ .ArrowQueryDefinition()
1554
+ .view.as.RefinementViewDefinition()
1555
+ .base.as.SegmentViewDefinition();
1388
1556
  segment.getGroupBy('carrier')!.delete();
1389
1557
  }).toModifyQuery({
1390
1558
  model: flights_model,
@@ -1448,10 +1616,10 @@ describe('query builder', () => {
1448
1616
  },
1449
1617
  };
1450
1618
  expect((q: ASTQuery) => {
1451
- const segment = q.definition
1452
- .asArrowQueryDefinition()
1453
- .view.asArrowViewDefinition()
1454
- .source.asSegmentViewDefinition();
1619
+ const segment = q.definition.as
1620
+ .ArrowQueryDefinition()
1621
+ .view.as.ArrowViewDefinition()
1622
+ .source.as.SegmentViewDefinition();
1455
1623
  segment.getGroupBy('carrier')!.delete();
1456
1624
  }).toModifyQuery({
1457
1625
  model: flights_model,
@@ -1669,7 +1837,7 @@ describe('query builder', () => {
1669
1837
  },
1670
1838
  };
1671
1839
  expect((q: ASTQuery) => {
1672
- const source = q.definition.asArrowQueryDefinition().sourceReference;
1840
+ const source = q.definition.as.ArrowQueryDefinition().sourceReference;
1673
1841
  source.setParameter('string_param', 'COOL');
1674
1842
  source.setParameter('number_param', 7);
1675
1843
  source.setParameter('boolean_param', true);