@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.
package/src/query-ast.ts CHANGED
@@ -62,422 +62,209 @@ abstract class ASTNode<T> {
62
62
  /**
63
63
  * @internal
64
64
  */
65
- abstract find(path: Path): ASTAny;
65
+ abstract findAny(path: Path): ASTAny;
66
66
 
67
- /**
68
- * Returns this node as an `ASTQuery`. Throws if it is not an `ASTQuery`.
69
- *
70
- * There are variants of this method for _all_ ASTXYZ nodes `asXYZ`, but they
71
- * are not shown here so the docs aren't crazy big.
72
- *
73
- * @returns Returns this node as an `ASTQuery`.
74
- */
75
- asQuery(): ASTQuery {
76
- if (this instanceof ASTQuery) return this;
77
- throw new Error('Not an ASTQuery');
78
- }
79
-
80
- /**
81
- * Finds the AST node at the given `path`. Throws if it is not an `ASTQuery`.
82
- *
83
- * There are variants of this method for _all_ ASTXYZ nodes `findXYZ`, but they
84
- * are not shown here so the docs aren't crazy big.
85
- *
86
- * @param path Path to the desired ASTNode, e.g. `['source', 'parameters', 0]`
87
- * @returns Returns this node as an `ASTQuery`.
88
- */
89
- findQuery(path: Path): ASTQuery {
90
- return this.find(path).asQuery();
91
- }
92
-
93
- /**
94
- * @hidden
95
- */
96
- asReference(): ASTReference {
97
- if (this instanceof ASTReference) return this;
98
- throw new Error('Not an ASTReference');
99
- }
100
-
101
- /**
102
- * @hidden
103
- */
104
- findReference(path: Path): ASTReference {
105
- return this.find(path).asReference();
106
- }
107
-
108
- /**
109
- * @hidden
110
- */
111
- asSourceReference(): ASTSourceReference {
112
- if (this instanceof ASTSourceReference) return this;
113
- throw new Error('Not an ASTSourceReference');
114
- }
115
-
116
- /**
117
- * @hidden
118
- */
119
- findSourceReference(path: Path): ASTSourceReference {
120
- return this.find(path).asSourceReference();
121
- }
122
-
123
- /**
124
- * @hidden
125
- */
126
- asParameterValueList(): ASTParameterValueList {
127
- if (this instanceof ASTParameterValueList) return this;
128
- throw new Error('Not an ASTParameterValueList');
129
- }
130
-
131
- /**
132
- * @hidden
133
- */
134
- findParameterValueList(path: Path): ASTParameterValueList {
135
- return this.find(path).asParameterValueList();
136
- }
137
-
138
- /**
139
- * @hidden
140
- */
141
- asWhere(): ASTWhere {
142
- if (this instanceof ASTWhere) return this;
143
- throw new Error('Not an ASTWhere');
144
- }
145
-
146
- /**
147
- * @hidden
148
- */
149
- findWhere(path: Path): ASTWhere {
150
- return this.find(path).asWhere();
151
- }
152
-
153
- /**
154
- * @hidden
155
- */
156
- asWhereList(): ASTWhereList {
157
- if (this instanceof ASTWhereList) return this;
158
- throw new Error('Not an ASTWhereList');
159
- }
160
-
161
- /**
162
- * @hidden
163
- */
164
- findWhereList(path: Path): ASTWhereList {
165
- return this.find(path).asWhereList();
166
- }
167
-
168
- /**
169
- * @hidden
170
- */
171
- asParameterValue(): ASTParameterValue {
172
- if (this instanceof ASTParameterValue) return this;
173
- throw new Error('Not an ASTParameterValue');
174
- }
175
-
176
- /**
177
- * @hidden
178
- */
179
- findParameterValue(path: Path): ASTParameterValue {
180
- return this.find(path).asParameterValue();
181
- }
182
-
183
- /**
184
- * @hidden
185
- */
186
- asStringLiteralValue(): ASTStringLiteralValue {
187
- if (this instanceof ASTStringLiteralValue) return this;
188
- throw new Error('Not an ASTStringLiteralValue');
189
- }
190
-
191
- /**
192
- * @hidden
193
- */
194
- findStringLiteralValue(path: Path): ASTStringLiteralValue {
195
- return this.find(path).asStringLiteralValue();
196
- }
197
-
198
- /**
199
- * @hidden
200
- */
201
- asNumberLiteralValue(): ASTNumberLiteralValue {
202
- if (this instanceof ASTNumberLiteralValue) return this;
203
- throw new Error('Not an ASTNumberLiteralValue');
204
- }
205
-
206
- /**
207
- * @hidden
208
- */
209
- findNumberLiteralValue(path: Path): ASTNumberLiteralValue {
210
- return this.find(path).asNumberLiteralValue();
211
- }
212
-
213
- /**
214
- * @hidden
215
- */
216
- asViewOperationList(): ASTViewOperationList {
217
- if (this instanceof ASTViewOperationList) return this;
218
- throw new Error('Not an ASTViewOperationList');
219
- }
220
-
221
- /**
222
- * @hidden
223
- */
224
- findViewOperationList(path: Path): ASTViewOperationList {
225
- return this.find(path).asViewOperationList();
226
- }
227
-
228
- /**
229
- * @hidden
230
- */
231
- asGroupByViewOperation(): ASTGroupByViewOperation {
232
- if (this instanceof ASTGroupByViewOperation) return this;
233
- throw new Error('Not an ASTGroupByViewOperation');
234
- }
235
-
236
- /**
237
- * @hidden
238
- */
239
- findGroupByViewOperation(path: Path): ASTGroupByViewOperation {
240
- return this.find(path).asGroupByViewOperation();
241
- }
242
-
243
- /**
244
- * @hidden
245
- */
246
- asAggregateViewOperation(): ASTAggregateViewOperation {
247
- if (this instanceof ASTAggregateViewOperation) return this;
248
- throw new Error('Not an ASTAggregateViewOperation');
249
- }
250
-
251
- /**
252
- * @hidden
253
- */
254
- findAggregateViewOperation(path: Path): ASTAggregateViewOperation {
255
- return this.find(path).asAggregateViewOperation();
256
- }
257
-
258
- /**
259
- * @hidden
260
- */
261
- asOrderByViewOperation(): ASTOrderByViewOperation {
262
- if (this instanceof ASTOrderByViewOperation) return this;
263
- throw new Error('Not an ASTOrderByViewOperation');
264
- }
265
-
266
- /**
267
- * @hidden
268
- */
269
- findOrderByViewOperation(path: Path): ASTOrderByViewOperation {
270
- return this.find(path).asOrderByViewOperation();
271
- }
272
-
273
- /**
274
- * @hidden
275
- */
276
- asField(): ASTField {
277
- if (this instanceof ASTField) return this;
278
- throw new Error('Not an ASTField');
279
- }
280
-
281
- /**
282
- * @hidden
283
- */
284
- findField(path: Path): ASTField {
285
- return this.find(path).asField();
286
- }
287
-
288
- /**
289
- * @hidden
290
- */
291
- asReferenceExpression(): ASTReferenceExpression {
292
- if (this instanceof ASTReferenceExpression) return this;
293
- throw new Error('Not an ASTReferenceExpression');
294
- }
295
-
296
- /**
297
- * @hidden
298
- */
299
- findReferenceExpression(path: Path): ASTReferenceExpression {
300
- return this.find(path).asReferenceExpression();
301
- }
302
-
303
- /**
304
- * @hidden
305
- */
306
- asReferenceViewDefinition(): ASTReferenceViewDefinition {
307
- if (this instanceof ASTReferenceViewDefinition) return this;
308
- throw new Error('Not an ASTReferenceViewDefinition');
309
- }
310
-
311
- /**
312
- * @hidden
313
- */
314
- findReferenceViewDefinition(path: Path): ASTReferenceViewDefinition {
315
- return this.find(path).asReferenceViewDefinition();
316
- }
317
-
318
- /**
319
- * @hidden
320
- */
321
- asArrowQueryDefinition(): ASTArrowQueryDefinition {
322
- if (this instanceof ASTArrowQueryDefinition) return this;
323
- throw new Error('Not an ASTArrowQueryDefinition');
324
- }
325
-
326
- /**
327
- * @hidden
328
- */
329
- findArrowQueryDefinition(path: Path): ASTArrowQueryDefinition {
330
- return this.find(path).asArrowQueryDefinition();
331
- }
332
-
333
- /**
334
- * @hidden
335
- */
336
- asArrowViewDefinition(): ASTArrowViewDefinition {
337
- if (this instanceof ASTArrowViewDefinition) return this;
338
- throw new Error('Not an ASTArrowViewDefinition');
339
- }
340
-
341
- /**
342
- * @hidden
343
- */
344
- findArrowViewDefinition(path: Path): ASTArrowViewDefinition {
345
- return this.find(path).asArrowViewDefinition();
346
- }
347
-
348
- /**
349
- * @hidden
350
- */
351
- asRefinementViewDefinition(): ASTRefinementViewDefinition {
352
- if (this instanceof ASTRefinementViewDefinition) return this;
353
- throw new Error('Not an ASTRefinementViewDefinition');
354
- }
355
-
356
- /**
357
- * @hidden
358
- */
359
- findRefinementViewDefinition(path: Path): ASTRefinementViewDefinition {
360
- return this.find(path).asRefinementViewDefinition();
361
- }
362
-
363
- /**
364
- * @hidden
365
- */
366
- asTimeTruncationExpression(): ASTTimeTruncationExpression {
367
- if (this instanceof ASTTimeTruncationExpression) return this;
368
- throw new Error('Not an ASTTimeTruncationExpression');
369
- }
370
-
371
- /**
372
- * @hidden
373
- */
374
- findTimeTruncationExpression(path: Path): ASTTimeTruncationExpression {
375
- return this.find(path).asTimeTruncationExpression();
376
- }
377
-
378
- /**
379
- * @hidden
380
- */
381
- asFilteredFieldExpression(): ASTFilteredFieldExpression {
382
- if (this instanceof ASTFilteredFieldExpression) return this;
383
- throw new Error('Not an ASTFilteredFieldExpression');
384
- }
385
-
386
- /**
387
- * @hidden
388
- */
389
- findFilteredFieldExpression(path: Path): ASTFilteredFieldExpression {
390
- return this.find(path).asFilteredFieldExpression();
391
- }
392
-
393
- /**
394
- * @hidden
395
- */
396
- asNestViewOperation(): ASTNestViewOperation {
397
- if (this instanceof ASTNestViewOperation) return this;
398
- throw new Error('Not an ASTNestViewOperation');
399
- }
400
-
401
- /**
402
- * @hidden
403
- */
404
- findNestViewOperation(path: Path): ASTNestViewOperation {
405
- return this.find(path).asNestViewOperation();
406
- }
407
-
408
- /**
409
- * @hidden
410
- */
411
- asView(): ASTView {
412
- if (this instanceof ASTView) return this;
413
- throw new Error('Not an ASTView');
414
- }
415
-
416
- /**
417
- * @hidden
418
- */
419
- findView(path: Path): ASTView {
420
- return this.find(path).asView();
421
- }
422
-
423
- /**
424
- * @hidden
425
- */
426
- asSegmentViewDefinition(): ASTSegmentViewDefinition {
427
- if (this instanceof ASTSegmentViewDefinition) return this;
428
- throw new Error('Not an ASTSegmentViewDefinition');
429
- }
430
-
431
- /**
432
- * @hidden
433
- */
434
- findSegmentViewDefinition(path: Path): ASTSegmentViewDefinition {
435
- return this.find(path).asSegmentViewDefinition();
436
- }
437
-
438
- /**
439
- * @hidden
440
- */
441
- asLimitViewOperation(): ASTLimitViewOperation {
442
- if (this instanceof ASTLimitViewOperation) return this;
443
- throw new Error('Not an ASTLimitViewOperation');
444
- }
445
-
446
- /**
447
- * @hidden
448
- */
449
- findLimitViewOperation(path: Path): ASTLimitViewOperation {
450
- return this.find(path).asLimitViewOperation();
451
- }
452
-
453
- /**
454
- * @hidden
455
- */
456
- asAnnotationList(): ASTAnnotationList {
457
- if (this instanceof ASTAnnotationList) return this;
458
- throw new Error('Not an ASTAnnotationList');
459
- }
460
-
461
- /**
462
- * @hidden
463
- */
464
- findAnnotationList(path: Path): ASTAnnotationList {
465
- return this.find(path).asAnnotationList();
466
- }
467
-
468
- /**
469
- * @hidden
470
- */
471
- asAnnotation(): ASTAnnotation {
472
- if (this instanceof ASTAnnotation) return this;
473
- throw new Error('Not an ASTAnnotation');
67
+ get as() {
68
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
69
+ const node = this;
70
+ return {
71
+ Query(): ASTQuery {
72
+ if (node instanceof ASTQuery) return node;
73
+ throw new Error('Not an ASTQuery');
74
+ },
75
+ Reference(): ASTReference {
76
+ if (node instanceof ASTReference) return node;
77
+ throw new Error('Not an ASTReference');
78
+ },
79
+ SourceReference(): ASTSourceReference {
80
+ if (node instanceof ASTSourceReference) return node;
81
+ throw new Error('Not an ASTSourceReference');
82
+ },
83
+ ParameterValueList(): ASTParameterValueList {
84
+ if (node instanceof ASTParameterValueList) return node;
85
+ throw new Error('Not an ASTParameterValueList');
86
+ },
87
+ Where(): ASTWhere {
88
+ if (node instanceof ASTWhere) return node;
89
+ throw new Error('Not an ASTWhere');
90
+ },
91
+ WhereList(): ASTWhereList {
92
+ if (node instanceof ASTWhereList) return node;
93
+ throw new Error('Not an ASTWhereList');
94
+ },
95
+ ParameterValue(): ASTParameterValue {
96
+ if (node instanceof ASTParameterValue) return node;
97
+ throw new Error('Not an ASTParameterValue');
98
+ },
99
+ StringLiteralValue(): ASTStringLiteralValue {
100
+ if (node instanceof ASTStringLiteralValue) return node;
101
+ throw new Error('Not an ASTStringLiteralValue');
102
+ },
103
+ NumberLiteralValue(): ASTNumberLiteralValue {
104
+ if (node instanceof ASTNumberLiteralValue) return node;
105
+ throw new Error('Not an ASTNumberLiteralValue');
106
+ },
107
+ ViewOperationList(): ASTViewOperationList {
108
+ if (node instanceof ASTViewOperationList) return node;
109
+ throw new Error('Not an ASTViewOperationList');
110
+ },
111
+ GroupByViewOperation(): ASTGroupByViewOperation {
112
+ if (node instanceof ASTGroupByViewOperation) return node;
113
+ throw new Error('Not an ASTGroupByViewOperation');
114
+ },
115
+ AggregateViewOperation(): ASTAggregateViewOperation {
116
+ if (node instanceof ASTAggregateViewOperation) return node;
117
+ throw new Error('Not an ASTAggregateViewOperation');
118
+ },
119
+ OrderByViewOperation(): ASTOrderByViewOperation {
120
+ if (node instanceof ASTOrderByViewOperation) return node;
121
+ throw new Error('Not an ASTOrderByViewOperation');
122
+ },
123
+ Field(): ASTField {
124
+ if (node instanceof ASTField) return node;
125
+ throw new Error('Not an ASTField');
126
+ },
127
+ ReferenceExpression(): ASTReferenceExpression {
128
+ if (node instanceof ASTReferenceExpression) return node;
129
+ throw new Error('Not an ASTReferenceExpression');
130
+ },
131
+ ReferenceViewDefinition(): ASTReferenceViewDefinition {
132
+ if (node instanceof ASTReferenceViewDefinition) return node;
133
+ throw new Error('Not an ASTReferenceViewDefinition');
134
+ },
135
+ ArrowQueryDefinition(): ASTArrowQueryDefinition {
136
+ if (node instanceof ASTArrowQueryDefinition) return node;
137
+ throw new Error('Not an ASTArrowQueryDefinition');
138
+ },
139
+ ArrowViewDefinition(): ASTArrowViewDefinition {
140
+ if (node instanceof ASTArrowViewDefinition) return node;
141
+ throw new Error('Not an ASTArrowViewDefinition');
142
+ },
143
+ RefinementViewDefinition(): ASTRefinementViewDefinition {
144
+ if (node instanceof ASTRefinementViewDefinition) return node;
145
+ throw new Error('Not an ASTRefinementViewDefinition');
146
+ },
147
+ TimeTruncationExpression(): ASTTimeTruncationExpression {
148
+ if (node instanceof ASTTimeTruncationExpression) return node;
149
+ throw new Error('Not an ASTTimeTruncationExpression');
150
+ },
151
+ FilteredFieldExpression(): ASTFilteredFieldExpression {
152
+ if (node instanceof ASTFilteredFieldExpression) return node;
153
+ throw new Error('Not an ASTFilteredFieldExpression');
154
+ },
155
+ NestViewOperation(): ASTNestViewOperation {
156
+ if (node instanceof ASTNestViewOperation) return node;
157
+ throw new Error('Not an ASTNestViewOperation');
158
+ },
159
+ View(): ASTView {
160
+ if (node instanceof ASTView) return node;
161
+ throw new Error('Not an ASTView');
162
+ },
163
+ SegmentViewDefinition(): ASTSegmentViewDefinition {
164
+ if (node instanceof ASTSegmentViewDefinition) return node;
165
+ throw new Error('Not an ASTSegmentViewDefinition');
166
+ },
167
+ LimitViewOperation(): ASTLimitViewOperation {
168
+ if (node instanceof ASTLimitViewOperation) return node;
169
+ throw new Error('Not an ASTLimitViewOperation');
170
+ },
171
+ AnnotationList(): ASTAnnotationList {
172
+ if (node instanceof ASTAnnotationList) return node;
173
+ throw new Error('Not an ASTAnnotationList');
174
+ },
175
+ Annotation(): ASTAnnotation {
176
+ if (node instanceof ASTAnnotation) return node;
177
+ throw new Error('Not an ASTAnnotation');
178
+ },
179
+ };
474
180
  }
475
181
 
476
- /**
477
- * @hidden
478
- */
479
- findAnnotation(path: Path): ASTAnnotation {
480
- return this.find(path).asAnnotation();
182
+ get find() {
183
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
184
+ const node = this;
185
+ return {
186
+ Query(path: Path): ASTQuery {
187
+ return node.findAny(path).as.Query();
188
+ },
189
+ Reference(path: Path): ASTReference {
190
+ return node.findAny(path).as.Reference();
191
+ },
192
+ SourceReference(path: Path): ASTSourceReference {
193
+ return node.findAny(path).as.SourceReference();
194
+ },
195
+ ParameterValueList(path: Path): ASTParameterValueList {
196
+ return node.findAny(path).as.ParameterValueList();
197
+ },
198
+ Where(path: Path): ASTWhere {
199
+ return node.findAny(path).as.Where();
200
+ },
201
+ WhereList(path: Path): ASTWhereList {
202
+ return node.findAny(path).as.WhereList();
203
+ },
204
+ ParameterValue(path: Path): ASTParameterValue {
205
+ return node.findAny(path).as.ParameterValue();
206
+ },
207
+ StringLiteralValue(path: Path): ASTStringLiteralValue {
208
+ return node.findAny(path).as.StringLiteralValue();
209
+ },
210
+ NumberLiteralValue(path: Path): ASTNumberLiteralValue {
211
+ return node.findAny(path).as.NumberLiteralValue();
212
+ },
213
+ ViewOperationList(path: Path): ASTViewOperationList {
214
+ return node.findAny(path).as.ViewOperationList();
215
+ },
216
+ GroupByViewOperation(path: Path): ASTGroupByViewOperation {
217
+ return node.findAny(path).as.GroupByViewOperation();
218
+ },
219
+ AggregateViewOperation(path: Path): ASTAggregateViewOperation {
220
+ return node.findAny(path).as.AggregateViewOperation();
221
+ },
222
+ OrderByViewOperation(path: Path): ASTOrderByViewOperation {
223
+ return node.findAny(path).as.OrderByViewOperation();
224
+ },
225
+ Field(path: Path): ASTField {
226
+ return node.findAny(path).as.Field();
227
+ },
228
+ ReferenceExpression(path: Path): ASTReferenceExpression {
229
+ return node.findAny(path).as.ReferenceExpression();
230
+ },
231
+ ReferenceViewDefinition(path: Path): ASTReferenceViewDefinition {
232
+ return node.findAny(path).as.ReferenceViewDefinition();
233
+ },
234
+ ArrowQueryDefinition(path: Path): ASTArrowQueryDefinition {
235
+ return node.findAny(path).as.ArrowQueryDefinition();
236
+ },
237
+ ArrowViewDefinition(path: Path): ASTArrowViewDefinition {
238
+ return node.findAny(path).as.ArrowViewDefinition();
239
+ },
240
+ RefinementViewDefinition(path: Path): ASTRefinementViewDefinition {
241
+ return node.findAny(path).as.RefinementViewDefinition();
242
+ },
243
+ TimeTruncationExpression(path: Path): ASTTimeTruncationExpression {
244
+ return node.findAny(path).as.TimeTruncationExpression();
245
+ },
246
+ FilteredFieldExpression(path: Path): ASTFilteredFieldExpression {
247
+ return node.findAny(path).as.FilteredFieldExpression();
248
+ },
249
+ NestViewOperation(path: Path): ASTNestViewOperation {
250
+ return node.findAny(path).as.NestViewOperation();
251
+ },
252
+ View(path: Path): ASTView {
253
+ return node.findAny(path).as.View();
254
+ },
255
+ SegmentViewDefinition(path: Path): ASTSegmentViewDefinition {
256
+ return node.findAny(path).as.SegmentViewDefinition();
257
+ },
258
+ LimitViewOperation(path: Path): ASTLimitViewOperation {
259
+ return node.findAny(path).as.LimitViewOperation();
260
+ },
261
+ AnnotationList(path: Path): ASTAnnotationList {
262
+ return node.findAny(path).as.AnnotationList();
263
+ },
264
+ Annotation(path: Path): ASTAnnotation {
265
+ return node.findAny(path).as.Annotation();
266
+ },
267
+ };
481
268
  }
482
269
 
483
270
  /**
@@ -543,6 +330,23 @@ abstract class ASTNode<T> {
543
330
  fields: [...a.fields, ...b.fields],
544
331
  };
545
332
  }
333
+
334
+ static tagFor(a: Malloy.FieldInfo, prefix = '# ') {
335
+ const lines = a.annotations
336
+ ?.map(a => a.value)
337
+ ?.filter(l => l.startsWith(prefix));
338
+ return Tag.fromTagLines(lines ?? []).tag ?? new Tag();
339
+ }
340
+
341
+ static fieldWasCalculation(a: Malloy.FieldInfo) {
342
+ if (a.kind !== 'dimension') {
343
+ throw new Error(
344
+ `${a.name} could not be an output field, because it is a ${a.kind}, and only dimensions can appear in output schemas`
345
+ );
346
+ }
347
+ const tag = ASTNode.tagFor(a, '#(malloy) ');
348
+ return tag.has('calculation');
349
+ }
546
350
  }
547
351
 
548
352
  function isBasic(
@@ -637,7 +441,7 @@ abstract class ASTListNode<
637
441
  /**
638
442
  * @internal
639
443
  */
640
- find(path: Path): ASTAny {
444
+ findAny(path: Path): ASTAny {
641
445
  if (path.length === 0) {
642
446
  return this;
643
447
  }
@@ -648,7 +452,7 @@ abstract class ASTListNode<
648
452
  );
649
453
  }
650
454
  const child = this.children[head];
651
- return child.find(rest);
455
+ return child.findAny(rest);
652
456
  }
653
457
 
654
458
  /**
@@ -709,7 +513,7 @@ abstract class ASTObjectNode<
709
513
  /**
710
514
  * @internal
711
515
  */
712
- find(path: Path): ASTAny {
516
+ findAny(path: Path): ASTAny {
713
517
  if (path.length === 0) {
714
518
  return this;
715
519
  }
@@ -725,7 +529,7 @@ abstract class ASTObjectNode<
725
529
  `${this.constructor.name}.${head} refers to a basic type, not an ASTNode`
726
530
  );
727
531
  }
728
- return child.find(rest);
532
+ return child._find(rest);
729
533
  }
730
534
  }
731
535
 
@@ -901,7 +705,7 @@ export class ASTQuery
901
705
  operations: [],
902
706
  },
903
707
  });
904
- return this.definition.view.asSegmentViewDefinition();
708
+ return this.definition.view.as.SegmentViewDefinition();
905
709
  }
906
710
 
907
711
  isRunnable() {
@@ -1064,7 +868,7 @@ export class ASTQuery
1064
868
  name,
1065
869
  },
1066
870
  });
1067
- return this.definition.view.asReferenceViewDefinition();
871
+ return this.definition.view.as.ReferenceViewDefinition();
1068
872
  }
1069
873
 
1070
874
  getInheritedAnnotations(): Malloy.Annotation[] {
@@ -1233,8 +1037,15 @@ export class ASTFieldReference extends ASTReference {
1233
1037
  }
1234
1038
  }
1235
1039
 
1040
+ private getReferenceSchema() {
1041
+ if (this.parent instanceof ASTOrderByViewOperation) {
1042
+ return this.segment.getOutputSchema();
1043
+ }
1044
+ return this.segment.getInputSchema();
1045
+ }
1046
+
1236
1047
  getFieldInfo() {
1237
- const schema = this.segment.getInputSchema();
1048
+ const schema = this.getReferenceSchema();
1238
1049
  return ASTNode.schemaGet(schema, this.name, this.path);
1239
1050
  }
1240
1051
  }
@@ -1244,7 +1055,7 @@ export class ASTSourceReference extends ASTReference {
1244
1055
  * @internal
1245
1056
  */
1246
1057
  get query(): ASTQuery {
1247
- return this.parent.parent.asQuery();
1058
+ return this.parent.parent.as.Query();
1248
1059
  }
1249
1060
 
1250
1061
  /**
@@ -1502,7 +1313,7 @@ export class ASTUnimplemented<T> extends ASTNode<T> {
1502
1313
  return this.node;
1503
1314
  }
1504
1315
 
1505
- find(): never {
1316
+ findAny(): never {
1506
1317
  throw new Error('Tried to find a node from an unimplemented node type');
1507
1318
  }
1508
1319
  }
@@ -1600,7 +1411,7 @@ export class ASTArrowQueryDefinition
1600
1411
  * @internal
1601
1412
  */
1602
1413
  get query() {
1603
- return this.parent.asQuery();
1414
+ return this.parent.as.Query();
1604
1415
  }
1605
1416
 
1606
1417
  /**
@@ -1668,7 +1479,7 @@ export class ASTRefinementQueryDefinition
1668
1479
  * @internal
1669
1480
  */
1670
1481
  get query() {
1671
- return this.parent.asQuery();
1482
+ return this.parent.as.Query();
1672
1483
  }
1673
1484
 
1674
1485
  getOrAddDefaultSegment(): ASTSegmentViewDefinition {
@@ -1736,7 +1547,7 @@ export class ASTReferenceQueryDefinition
1736
1547
  }
1737
1548
 
1738
1549
  get query() {
1739
- return this.parent.asQuery();
1550
+ return this.parent.as.Query();
1740
1551
  }
1741
1552
 
1742
1553
  get parameters() {
@@ -1766,7 +1577,7 @@ export class ASTReferenceQueryDefinition
1766
1577
  },
1767
1578
  });
1768
1579
  this.query.definition = newQuery;
1769
- return newQuery.refinement.asSegmentViewDefinition();
1580
+ return newQuery.refinement.as.SegmentViewDefinition();
1770
1581
  }
1771
1582
 
1772
1583
  /**
@@ -1946,7 +1757,7 @@ export class ASTReferenceViewDefinition
1946
1757
  this.build()
1947
1758
  );
1948
1759
  swapViewInParent(this, newView);
1949
- return newView.refinement.asSegmentViewDefinition();
1760
+ return newView.refinement.as.SegmentViewDefinition();
1950
1761
  }
1951
1762
 
1952
1763
  addViewRefinement(name: string, path?: string[]): ASTReferenceViewDefinition {
@@ -1960,7 +1771,7 @@ export class ASTReferenceViewDefinition
1960
1771
  path
1961
1772
  );
1962
1773
  swapViewInParent(this, newView);
1963
- return newView.refinement.asReferenceViewDefinition();
1774
+ return newView.refinement.as.ReferenceViewDefinition();
1964
1775
  }
1965
1776
 
1966
1777
  isValidViewRefinement(
@@ -2440,7 +2251,10 @@ export class ASTSegmentViewDefinition
2440
2251
  * @param name The name of the field to order by.
2441
2252
  * @param direction The order by direction (ascending or descending).
2442
2253
  */
2443
- public addOrderBy(name: string, direction?: Malloy.OrderByDirection) {
2254
+ public addOrderBy(
2255
+ name: string,
2256
+ direction?: Malloy.OrderByDirection
2257
+ ): ASTOrderByViewOperation {
2444
2258
  // Ensure output schema has a field with this name
2445
2259
  const outputSchema = this.getOutputSchema();
2446
2260
  try {
@@ -2453,18 +2267,18 @@ export class ASTSegmentViewDefinition
2453
2267
  if (operation instanceof ASTOrderByViewOperation) {
2454
2268
  if (operation.name === name) {
2455
2269
  operation.direction = direction;
2456
- return;
2270
+ return operation;
2457
2271
  }
2458
2272
  }
2459
2273
  }
2274
+ const operation = new ASTOrderByViewOperation({
2275
+ kind: 'order_by',
2276
+ field_reference: {name},
2277
+ direction,
2278
+ });
2460
2279
  // add a new order by operation
2461
- this.addOperation(
2462
- new ASTOrderByViewOperation({
2463
- kind: 'order_by',
2464
- field_reference: {name},
2465
- direction,
2466
- })
2467
- );
2280
+ this.addOperation(operation);
2281
+ return operation;
2468
2282
  }
2469
2283
 
2470
2284
  /**
@@ -2990,7 +2804,7 @@ export class ASTSegmentViewDefinition
2990
2804
  addEmptyRefinement(): ASTSegmentViewDefinition {
2991
2805
  const view = ASTRefinementViewDefinition.segmentRefinementOf(this.build());
2992
2806
  swapViewInParent(this, view);
2993
- return view.refinement.asSegmentViewDefinition();
2807
+ return view.refinement.as.SegmentViewDefinition();
2994
2808
  }
2995
2809
 
2996
2810
  addViewRefinement(name: string, path?: string[]): ASTReferenceViewDefinition {
@@ -3004,7 +2818,7 @@ export class ASTSegmentViewDefinition
3004
2818
  path
3005
2819
  );
3006
2820
  swapViewInParent(this, view);
3007
- return view.refinement.asReferenceViewDefinition();
2821
+ return view.refinement.as.ReferenceViewDefinition();
3008
2822
  }
3009
2823
 
3010
2824
  getInputSchema(): Malloy.Schema {
@@ -3062,7 +2876,7 @@ export class ASTViewOperationList extends ASTListNode<
3062
2876
  * @internal
3063
2877
  */
3064
2878
  get segment() {
3065
- return this.parent.asSegmentViewDefinition();
2879
+ return this.parent.as.SegmentViewDefinition();
3066
2880
  }
3067
2881
  }
3068
2882
 
@@ -3140,7 +2954,7 @@ export class ASTOrderByViewOperation extends ASTObjectNode<
3140
2954
  }
3141
2955
 
3142
2956
  get list() {
3143
- return this.parent.asViewOperationList();
2957
+ return this.parent.as.ViewOperationList();
3144
2958
  }
3145
2959
 
3146
2960
  delete() {
@@ -3202,7 +3016,7 @@ export class ASTGroupByViewOperation
3202
3016
  * @internal
3203
3017
  */
3204
3018
  get list() {
3205
- return this.parent.asViewOperationList();
3019
+ return this.parent.as.ViewOperationList();
3206
3020
  }
3207
3021
 
3208
3022
  /**
@@ -3414,7 +3228,7 @@ export class ASTAggregateViewOperation
3414
3228
  * @internal
3415
3229
  */
3416
3230
  get list() {
3417
- return this.parent.asViewOperationList();
3231
+ return this.parent.as.ViewOperationList();
3418
3232
  }
3419
3233
 
3420
3234
  delete() {
@@ -3428,6 +3242,11 @@ export class ASTAggregateViewOperation
3428
3242
 
3429
3243
  getFieldInfo(): Malloy.FieldInfo {
3430
3244
  return {
3245
+ annotations: [
3246
+ {
3247
+ value: Tag.withPrefix('#(malloy) ').set(['calculation']).toString(),
3248
+ },
3249
+ ],
3431
3250
  kind: 'dimension',
3432
3251
  name: this.name,
3433
3252
  type: this.field.type,
@@ -3685,7 +3504,7 @@ export class ASTReferenceExpression
3685
3504
  * @internal
3686
3505
  */
3687
3506
  get field() {
3688
- return this.parent.asField();
3507
+ return this.parent.as.Field();
3689
3508
  }
3690
3509
 
3691
3510
  get path() {
@@ -3765,7 +3584,7 @@ export class ASTTimeTruncationExpression extends ASTObjectNode<
3765
3584
  * @internal
3766
3585
  */
3767
3586
  get field() {
3768
- return this.parent.asField();
3587
+ return this.parent.as.Field();
3769
3588
  }
3770
3589
 
3771
3590
  getFieldInfo(): Malloy.FieldInfoWithDimension | Malloy.FieldInfoWithMeasure {
@@ -3808,7 +3627,7 @@ export class ASTWhere extends ASTObjectNode<Malloy.Where, {filter: ASTFilter}> {
3808
3627
  }
3809
3628
 
3810
3629
  get list() {
3811
- return this.parent.asWhereList();
3630
+ return this.parent.as.WhereList();
3812
3631
  }
3813
3632
 
3814
3633
  delete() {
@@ -3825,7 +3644,7 @@ export class ASTWhereList extends ASTListNode<Malloy.Where, ASTWhere> {
3825
3644
  }
3826
3645
 
3827
3646
  get expression() {
3828
- return this.parent.asFilteredFieldExpression();
3647
+ return this.parent.as.FilteredFieldExpression();
3829
3648
  }
3830
3649
  }
3831
3650
 
@@ -3867,7 +3686,7 @@ export class ASTFilteredFieldExpression extends ASTObjectNode<
3867
3686
  * @internal
3868
3687
  */
3869
3688
  get field() {
3870
- return this.parent.asField();
3689
+ return this.parent.as.Field();
3871
3690
  }
3872
3691
 
3873
3692
  getFieldInfo(): Malloy.FieldInfoWithMeasure {
@@ -3954,7 +3773,7 @@ export class ASTNestViewOperation
3954
3773
  * @internal
3955
3774
  */
3956
3775
  get list() {
3957
- return this.parent.asViewOperationList();
3776
+ return this.parent.as.ViewOperationList();
3958
3777
  }
3959
3778
 
3960
3779
  getOrAddAnnotations() {
@@ -4088,7 +3907,7 @@ export class ASTWhereViewOperation extends ASTObjectNode<
4088
3907
  * @internal
4089
3908
  */
4090
3909
  get list() {
4091
- return this.parent.asViewOperationList();
3910
+ return this.parent.as.ViewOperationList();
4092
3911
  }
4093
3912
 
4094
3913
  delete() {
@@ -4207,7 +4026,7 @@ export class ASTView
4207
4026
  * @internal
4208
4027
  */
4209
4028
  get nest() {
4210
- return this.parent.asNestViewOperation();
4029
+ return this.parent.as.NestViewOperation();
4211
4030
  }
4212
4031
 
4213
4032
  getInputSchema() {
@@ -4215,7 +4034,7 @@ export class ASTView
4215
4034
  }
4216
4035
 
4217
4036
  getOutputSchema() {
4218
- return this.definition.getOutputSchema();
4037
+ return this.definition.getRefinementSchema();
4219
4038
  }
4220
4039
 
4221
4040
  /**
@@ -4309,7 +4128,7 @@ export class ASTLimitViewOperation extends ASTObjectNode<
4309
4128
  * @internal
4310
4129
  */
4311
4130
  get list() {
4312
- return this.parent.asViewOperationList();
4131
+ return this.parent.as.ViewOperationList();
4313
4132
  }
4314
4133
 
4315
4134
  delete() {
@@ -4450,7 +4269,7 @@ export class ASTAnnotation extends ASTObjectNode<
4450
4269
  * @internal
4451
4270
  */
4452
4271
  get list() {
4453
- return this.parent.asAnnotationList();
4272
+ return this.parent.as.AnnotationList();
4454
4273
  }
4455
4274
 
4456
4275
  get index() {