@malloydata/malloy-query-builder 0.0.298 → 0.0.300

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.
@@ -856,12 +856,12 @@ export declare class ASTSegmentViewDefinition extends ASTObjectNode<Malloy.ViewD
856
856
  private firstIndexOfOperationType;
857
857
  private DEFAULT_INSERTION_ORDER;
858
858
  private findInsertionPoint;
859
- getFieldNamed(name: string): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | undefined;
859
+ getFieldNamed(name: string): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | ASTCalculateViewOperation | undefined;
860
860
  hasFieldNamed(name: string): boolean;
861
861
  hasField(name: string, path?: string[]): boolean;
862
- getField(name: string, path?: string[]): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation;
863
- tryGetField(name: string, path?: string[]): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | undefined;
864
- tryGetFieldNamed(name: string): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | undefined;
862
+ getField(name: string, path?: string[]): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | ASTCalculateViewOperation;
863
+ tryGetField(name: string, path?: string[]): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | ASTCalculateViewOperation | undefined;
864
+ tryGetFieldNamed(name: string): ASTGroupByViewOperation | ASTAggregateViewOperation | ASTNestViewOperation | ASTCalculateViewOperation | undefined;
865
865
  getGroupBy(name: string): ASTGroupByViewOperation | undefined;
866
866
  removeGroupBy(name: string): this;
867
867
  /**
@@ -930,6 +930,7 @@ export declare class ASTSegmentViewDefinition extends ASTObjectNode<Malloy.ViewD
930
930
  addHaving(name: string, filterString: string): ASTHavingViewOperation;
931
931
  addHaving(name: string, path: string[], filter: ParsedFilter): ASTHavingViewOperation;
932
932
  addHaving(name: string, path: string[], filterString: string): ASTHavingViewOperation;
933
+ addCalculateMovingAverage(outputName: string, inputName: string, inputPath: string[], rowsPreceding: number, rowsFollowing: number): ASTCalculateViewOperation;
933
934
  private addTimeGroupBy;
934
935
  addDateGroupBy(name: string, path: string[], timeframe: Malloy.DateTimeframe): ASTGroupByViewOperation;
935
936
  addDateGroupBy(name: string, timeframe: Malloy.DateTimeframe): ASTGroupByViewOperation;
@@ -1030,7 +1031,7 @@ export declare class ASTViewOperationList extends ASTListNode<Malloy.ViewOperati
1030
1031
  */
1031
1032
  get segment(): ASTSegmentViewDefinition;
1032
1033
  }
1033
- export type ASTViewOperation = ASTGroupByViewOperation | ASTAggregateViewOperation | ASTOrderByViewOperation | ASTNestViewOperation | ASTLimitViewOperation | ASTWhereViewOperation | ASTDrillViewOperation | ASTHavingViewOperation;
1034
+ export type ASTViewOperation = ASTGroupByViewOperation | ASTAggregateViewOperation | ASTOrderByViewOperation | ASTNestViewOperation | ASTLimitViewOperation | ASTWhereViewOperation | ASTDrillViewOperation | ASTHavingViewOperation | ASTCalculateViewOperation;
1034
1035
  export declare const ASTViewOperation: {
1035
1036
  from(value: Malloy.ViewOperation): ASTViewOperation;
1036
1037
  isLimit(x: ASTViewOperation): x is ASTLimitViewOperation;
@@ -1186,6 +1187,11 @@ export declare class ASTAggregateViewOperation extends ASTObjectNode<Malloy.View
1186
1187
  * @param name The new name
1187
1188
  */
1188
1189
  rename(name: string): void;
1190
+ /**
1191
+ * Removes this aggregate item from the query and replaces it with a smoothed
1192
+ * calculation of the same field.
1193
+ */
1194
+ convertToCalculateMovingAverage(name: string, rows_preceding: number, rows_following?: number, partition_fields?: string[]): ASTCalculateViewOperation;
1189
1195
  /**
1190
1196
  * @internal
1191
1197
  */
@@ -1238,7 +1244,7 @@ export declare class ASTField extends ASTObjectNode<Malloy.Field, {
1238
1244
  get segment(): ASTSegmentViewDefinition;
1239
1245
  getInheritedAnnotations(): Malloy.Annotation[];
1240
1246
  }
1241
- export type ASTExpression = ASTReferenceExpression | ASTFilteredFieldExpression | ASTTimeTruncationExpression | ASTLiteralValueExpression;
1247
+ export type ASTExpression = ASTReferenceExpression | ASTFilteredFieldExpression | ASTTimeTruncationExpression | ASTLiteralValueExpression | ASTMovingAverageExpression;
1242
1248
  export declare const ASTExpression: {
1243
1249
  from(value: Malloy.Expression): ASTExpression;
1244
1250
  };
@@ -1307,6 +1313,33 @@ export declare class ASTLiteralValueExpression extends ASTObjectNode<Malloy.Expr
1307
1313
  getInheritedAnnotations(): Malloy.Annotation[];
1308
1314
  getFieldInfo(): undefined;
1309
1315
  }
1316
+ export declare class ASTFieldReferenceList extends ASTListNode<Malloy.Reference, ASTFieldReference> {
1317
+ constructor(fields: Malloy.Reference[]);
1318
+ }
1319
+ export declare class ASTMovingAverageExpression extends ASTObjectNode<Malloy.ExpressionWithMovingAverage, {
1320
+ kind: 'moving_average';
1321
+ field_reference: ASTFieldReference;
1322
+ rows_preceding?: number;
1323
+ rows_following?: number;
1324
+ partition_fields: ASTFieldReferenceList;
1325
+ }> {
1326
+ node: Malloy.ExpressionWithMovingAverage;
1327
+ readonly kind: Malloy.ExpressionType;
1328
+ constructor(node: Malloy.ExpressionWithMovingAverage);
1329
+ getReference(): Malloy.Reference;
1330
+ get fieldReference(): ASTFieldReference;
1331
+ get rowsPreceding(): number | undefined;
1332
+ set rowsPreceding(rows: number | undefined);
1333
+ get rowsFollowing(): number | undefined;
1334
+ set rowsFollowing(rows: number | undefined);
1335
+ get name(): string;
1336
+ get field(): ASTField;
1337
+ get partitionFields(): ASTFieldReferenceList;
1338
+ setPartitionFields(references: Malloy.Reference[]): void;
1339
+ getFieldInfo(): Malloy.FieldInfoWithDimension | Malloy.FieldInfoWithMeasure;
1340
+ get fieldType(): Malloy.AtomicType;
1341
+ getInheritedAnnotations(): Malloy.Annotation[];
1342
+ }
1310
1343
  export declare class ASTFilterOperation extends ASTObjectNode<Malloy.FilterOperation, {
1311
1344
  filter: ASTFilter;
1312
1345
  }> {
@@ -1455,6 +1488,25 @@ export declare class ASTHavingViewOperation extends ASTObjectNode<Malloy.ViewOpe
1455
1488
  get list(): ASTViewOperationList;
1456
1489
  delete(): void;
1457
1490
  }
1491
+ export declare class ASTCalculateViewOperation extends ASTObjectNode<Malloy.ViewOperationWithCalculate, {
1492
+ kind: 'calculate';
1493
+ field: ASTField;
1494
+ name: string;
1495
+ }> {
1496
+ node: Malloy.ViewOperationWithCalculate;
1497
+ readonly kind: Malloy.ViewOperationType;
1498
+ constructor(node: Malloy.ViewOperationWithCalculate);
1499
+ get expression(): ASTMovingAverageExpression;
1500
+ set expression(expression: ASTMovingAverageExpression);
1501
+ get name(): string;
1502
+ set name(name: string);
1503
+ getFieldInfo(): Malloy.FieldInfo;
1504
+ /**
1505
+ * @internal
1506
+ */
1507
+ get list(): ASTViewOperationList;
1508
+ delete(): void;
1509
+ }
1458
1510
  export type ASTFilter = ASTFilterWithFilterString | ASTFilterWithLiteralEquality;
1459
1511
  export declare const ASTFilter: {
1460
1512
  from(filter: Malloy.Filter): ASTFilterWithFilterString | ASTFilterWithLiteralEquality;
package/dist/query-ast.js CHANGED
@@ -33,7 +33,8 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.ASTAnnotation = exports.ASTAnnotationList = exports.ASTLimitViewOperation = exports.ASTView = exports.ASTFilterWithLiteralEquality = exports.ASTFilterWithFilterString = exports.ASTFilter = exports.ASTHavingViewOperation = exports.ASTDrillViewOperation = exports.ASTWhereViewOperation = exports.ASTNestViewOperation = exports.ASTFilteredFieldExpression = exports.ASTFilterOperationList = exports.ASTFilterOperation = exports.ASTLiteralValueExpression = exports.ASTTimeTruncationExpression = exports.ASTReferenceExpression = exports.ASTExpression = exports.ASTField = exports.ASTAggregateViewOperation = exports.ASTGroupByViewOperation = exports.ASTOrderByViewOperation = exports.ASTViewOperation = exports.ASTViewOperationList = exports.ASTSegmentViewDefinition = exports.ASTRefinementViewDefinition = exports.ASTArrowViewDefinition = exports.ASTReferenceViewDefinition = exports.ASTReferenceQueryArrowSource = exports.ASTReferenceQueryDefinition = exports.ASTRefinementQueryDefinition = exports.ASTArrowQueryDefinition = exports.ASTQueryDefinition = exports.ASTQueryArrowSource = exports.ASTUnimplemented = exports.ASTFilterExpressionLiteralValue = exports.ASTTimestampLiteralValue = exports.ASTDateLiteralValue = exports.ASTBooleanLiteralValue = exports.ASTNumberLiteralValue = exports.ASTNullLiteralValue = exports.ASTStringLiteralValue = exports.ASTLiteralValue = exports.ASTParameterValue = exports.ASTParameterValueList = exports.ASTFieldReference = exports.ASTReference = exports.ASTQuery = void 0;
36
+ exports.ASTAnnotationList = exports.ASTLimitViewOperation = exports.ASTView = exports.ASTFilterWithLiteralEquality = exports.ASTFilterWithFilterString = exports.ASTFilter = exports.ASTCalculateViewOperation = exports.ASTHavingViewOperation = exports.ASTDrillViewOperation = exports.ASTWhereViewOperation = exports.ASTNestViewOperation = exports.ASTFilteredFieldExpression = exports.ASTFilterOperationList = exports.ASTFilterOperation = exports.ASTMovingAverageExpression = exports.ASTFieldReferenceList = exports.ASTLiteralValueExpression = exports.ASTTimeTruncationExpression = exports.ASTReferenceExpression = exports.ASTExpression = exports.ASTField = exports.ASTAggregateViewOperation = exports.ASTGroupByViewOperation = exports.ASTOrderByViewOperation = exports.ASTViewOperation = exports.ASTViewOperationList = exports.ASTSegmentViewDefinition = exports.ASTRefinementViewDefinition = exports.ASTArrowViewDefinition = exports.ASTReferenceViewDefinition = exports.ASTReferenceQueryArrowSource = exports.ASTReferenceQueryDefinition = exports.ASTRefinementQueryDefinition = exports.ASTArrowQueryDefinition = exports.ASTQueryDefinition = exports.ASTQueryArrowSource = exports.ASTUnimplemented = exports.ASTFilterExpressionLiteralValue = exports.ASTTimestampLiteralValue = exports.ASTDateLiteralValue = exports.ASTBooleanLiteralValue = exports.ASTNumberLiteralValue = exports.ASTNullLiteralValue = exports.ASTStringLiteralValue = exports.ASTLiteralValue = exports.ASTParameterValue = exports.ASTParameterValueList = exports.ASTFieldReference = exports.ASTReference = exports.ASTQuery = void 0;
37
+ exports.ASTAnnotation = void 0;
37
38
  /*
38
39
  * Copyright (c) Meta Platforms, Inc. and affiliates.
39
40
  *
@@ -1898,6 +1899,7 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
1898
1899
  'having',
1899
1900
  'nest',
1900
1901
  'order_by',
1902
+ 'calculate',
1901
1903
  'limit',
1902
1904
  ];
1903
1905
  }
@@ -2169,6 +2171,14 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
2169
2171
  return operation;
2170
2172
  }
2171
2173
  }
2174
+ else if (operation instanceof ASTCalculateViewOperation) {
2175
+ const reference = operation.expression.field.getReference();
2176
+ if (reference &&
2177
+ reference.name === name &&
2178
+ pathsMatch(reference.path, path)) {
2179
+ return operation;
2180
+ }
2181
+ }
2172
2182
  }
2173
2183
  return undefined;
2174
2184
  }
@@ -2176,7 +2186,8 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
2176
2186
  for (const operation of this.operations.iter()) {
2177
2187
  if (operation instanceof ASTGroupByViewOperation ||
2178
2188
  operation instanceof ASTAggregateViewOperation ||
2179
- operation instanceof ASTNestViewOperation) {
2189
+ operation instanceof ASTNestViewOperation ||
2190
+ operation instanceof ASTCalculateViewOperation) {
2180
2191
  if (operation.name === name) {
2181
2192
  return operation;
2182
2193
  }
@@ -2315,6 +2326,22 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
2315
2326
  this.addOperation(item);
2316
2327
  return item;
2317
2328
  }
2329
+ addCalculateMovingAverage(outputName, inputName, inputPath, rowsPreceding, rowsFollowing) {
2330
+ const item = new ASTCalculateViewOperation({
2331
+ kind: 'calculate',
2332
+ name: outputName,
2333
+ field: {
2334
+ expression: {
2335
+ kind: 'moving_average',
2336
+ field_reference: { name: inputName, path: inputPath },
2337
+ rows_preceding: rowsPreceding,
2338
+ rows_following: rowsFollowing,
2339
+ },
2340
+ },
2341
+ });
2342
+ this.addOperation(item);
2343
+ return item;
2344
+ }
2318
2345
  addTimeGroupBy(name, path, timeframe, type) {
2319
2346
  const schema = this.getInputSchema();
2320
2347
  const fieldInfo = ASTNode.schemaGet(schema, name, path);
@@ -2428,7 +2455,8 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
2428
2455
  addOperation(item) {
2429
2456
  if (item instanceof ASTGroupByViewOperation ||
2430
2457
  item instanceof ASTAggregateViewOperation ||
2431
- item instanceof ASTNestViewOperation) {
2458
+ item instanceof ASTNestViewOperation ||
2459
+ item instanceof ASTCalculateViewOperation) {
2432
2460
  if (this.hasFieldNamed(item.name)) {
2433
2461
  throw new Error(`Query already has a field named ${item.name}`);
2434
2462
  }
@@ -2445,7 +2473,8 @@ class ASTSegmentViewDefinition extends ASTObjectNode {
2445
2473
  for (const operation of this.operations.iter()) {
2446
2474
  if (operation instanceof ASTGroupByViewOperation ||
2447
2475
  operation instanceof ASTAggregateViewOperation ||
2448
- operation instanceof ASTNestViewOperation) {
2476
+ operation instanceof ASTNestViewOperation ||
2477
+ operation instanceof ASTCalculateViewOperation) {
2449
2478
  // TODO convert measures into dimensions for output
2450
2479
  fields.push(operation.getFieldInfo());
2451
2480
  }
@@ -2561,6 +2590,8 @@ exports.ASTViewOperation = {
2561
2590
  return new ASTDrillViewOperation(value);
2562
2591
  case 'having':
2563
2592
  return new ASTHavingViewOperation(value);
2593
+ case 'calculate':
2594
+ return new ASTCalculateViewOperation(value);
2564
2595
  }
2565
2596
  },
2566
2597
  isLimit(x) {
@@ -2833,6 +2864,36 @@ class ASTAggregateViewOperation extends ASTObjectNode {
2833
2864
  rename(name) {
2834
2865
  this.list.segment.renameField(this, name);
2835
2866
  }
2867
+ /**
2868
+ * Removes this aggregate item from the query and replaces it with a smoothed
2869
+ * calculation of the same field.
2870
+ */
2871
+ convertToCalculateMovingAverage(name, rows_preceding, rows_following = 0, partition_fields = []) {
2872
+ if (!(this.field.expression instanceof ASTReferenceExpression)) {
2873
+ throw new Error('Cannot convert aggregate to smoothed metric unless it is a field reference');
2874
+ }
2875
+ this.list.remove(this);
2876
+ const calculateItem = new ASTCalculateViewOperation({
2877
+ kind: 'calculate',
2878
+ name: name,
2879
+ field: {
2880
+ expression: {
2881
+ kind: 'moving_average',
2882
+ field_reference: {
2883
+ name: this.field.expression.name,
2884
+ path: this.field.expression.path,
2885
+ },
2886
+ rows_preceding,
2887
+ rows_following,
2888
+ partition_fields: partition_fields.map(fieldName => ({
2889
+ name: fieldName,
2890
+ })),
2891
+ },
2892
+ },
2893
+ });
2894
+ this.list.add(calculateItem);
2895
+ return calculateItem;
2896
+ }
2836
2897
  /**
2837
2898
  * @internal
2838
2899
  */
@@ -3031,6 +3092,8 @@ exports.ASTExpression = {
3031
3092
  return new ASTTimeTruncationExpression(value);
3032
3093
  case 'literal_value':
3033
3094
  return new ASTLiteralValueExpression(value);
3095
+ case 'moving_average':
3096
+ return new ASTMovingAverageExpression(value);
3034
3097
  }
3035
3098
  },
3036
3099
  };
@@ -3195,6 +3258,74 @@ class ASTLiteralValueExpression extends ASTObjectNode {
3195
3258
  }
3196
3259
  }
3197
3260
  exports.ASTLiteralValueExpression = ASTLiteralValueExpression;
3261
+ class ASTFieldReferenceList extends ASTListNode {
3262
+ constructor(fields) {
3263
+ super(fields, fields.map(p => new ASTFieldReference(p)));
3264
+ }
3265
+ }
3266
+ exports.ASTFieldReferenceList = ASTFieldReferenceList;
3267
+ class ASTMovingAverageExpression extends ASTObjectNode {
3268
+ constructor(node) {
3269
+ super(node, {
3270
+ kind: node.kind,
3271
+ field_reference: new ASTFieldReference(node.field_reference),
3272
+ rows_preceding: node.rows_preceding,
3273
+ rows_following: node.rows_following,
3274
+ partition_fields: new ASTFieldReferenceList(node.partition_fields || []),
3275
+ });
3276
+ this.node = node;
3277
+ this.kind = 'moving_average';
3278
+ }
3279
+ getReference() {
3280
+ return this.fieldReference.build();
3281
+ }
3282
+ get fieldReference() {
3283
+ return this.children.field_reference;
3284
+ }
3285
+ get rowsPreceding() {
3286
+ return this.children.rows_preceding;
3287
+ }
3288
+ set rowsPreceding(rows) {
3289
+ this.edit();
3290
+ this.children.rows_preceding = rows;
3291
+ }
3292
+ get rowsFollowing() {
3293
+ return this.children.rows_following;
3294
+ }
3295
+ set rowsFollowing(rows) {
3296
+ this.edit();
3297
+ this.children.rows_following = rows;
3298
+ }
3299
+ get name() {
3300
+ return this.fieldReference.name;
3301
+ }
3302
+ get field() {
3303
+ return this.parent.as.Field();
3304
+ }
3305
+ get partitionFields() {
3306
+ return this.children.partition_fields;
3307
+ }
3308
+ setPartitionFields(references) {
3309
+ this.children.partition_fields = new ASTFieldReferenceList(references);
3310
+ }
3311
+ getFieldInfo() {
3312
+ const schema = this.field.segment.getInputSchema();
3313
+ const def = ASTNode.schemaGet(schema, this.name, this.fieldReference.path);
3314
+ if (def.kind !== 'dimension' && def.kind !== 'measure') {
3315
+ throw new Error('Invalid field for ASTReferenceExpression');
3316
+ }
3317
+ return def;
3318
+ }
3319
+ get fieldType() {
3320
+ return this.getFieldInfo().type;
3321
+ }
3322
+ getInheritedAnnotations() {
3323
+ var _a;
3324
+ const field = this.getFieldInfo();
3325
+ return (_a = field.annotations) !== null && _a !== void 0 ? _a : [];
3326
+ }
3327
+ }
3328
+ exports.ASTMovingAverageExpression = ASTMovingAverageExpression;
3198
3329
  class ASTFilterOperation extends ASTObjectNode {
3199
3330
  constructor(node) {
3200
3331
  super(node, {
@@ -3487,6 +3618,56 @@ class ASTHavingViewOperation extends ASTObjectNode {
3487
3618
  }
3488
3619
  }
3489
3620
  exports.ASTHavingViewOperation = ASTHavingViewOperation;
3621
+ class ASTCalculateViewOperation extends ASTObjectNode {
3622
+ constructor(node) {
3623
+ super(node, {
3624
+ kind: 'calculate',
3625
+ field: new ASTField(node.field),
3626
+ name: node.name,
3627
+ });
3628
+ this.node = node;
3629
+ this.kind = 'calculate';
3630
+ }
3631
+ get expression() {
3632
+ return this.children.field.expression;
3633
+ }
3634
+ set expression(expression) {
3635
+ this.edit();
3636
+ this.children.field.expression = expression;
3637
+ expression.parent = this;
3638
+ }
3639
+ get name() {
3640
+ return this.node.name;
3641
+ }
3642
+ set name(name) {
3643
+ if (this.name === name)
3644
+ return;
3645
+ this.edit();
3646
+ this.node.name = name;
3647
+ }
3648
+ getFieldInfo() {
3649
+ return {
3650
+ annotations: [
3651
+ {
3652
+ value: malloy_tag_1.Tag.withPrefix('#(malloy) ').set(['calculation']).toString(),
3653
+ },
3654
+ ],
3655
+ kind: 'calculate',
3656
+ name: this.name,
3657
+ type: this.expression.fieldType,
3658
+ };
3659
+ }
3660
+ /**
3661
+ * @internal
3662
+ */
3663
+ get list() {
3664
+ return this.parent.as.ViewOperationList();
3665
+ }
3666
+ delete() {
3667
+ this.list.remove(this);
3668
+ }
3669
+ }
3670
+ exports.ASTCalculateViewOperation = ASTCalculateViewOperation;
3490
3671
  exports.ASTFilter = {
3491
3672
  from(filter) {
3492
3673
  switch (filter.kind) {
@@ -3717,6 +3898,8 @@ function fieldTypeToAction(type) {
3717
3898
  return 'nest';
3718
3899
  case 'join':
3719
3900
  return 'join';
3901
+ case 'calculate':
3902
+ return 'calculate';
3720
3903
  }
3721
3904
  }
3722
3905
  function fieldTypeName(type) {