@malloydata/malloy-query-builder 0.0.294 → 0.0.295
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/dist/query-ast.d.ts +9 -5
- package/dist/query-ast.js +62 -30
- package/dist/query-ast.js.map +1 -1
- package/dist/query-ast.spec.js +76 -21
- package/dist/query-ast.spec.js.map +1 -1
- package/package.json +4 -4
- package/src/query-ast.spec.ts +76 -21
- package/src/query-ast.ts +73 -35
package/src/query-ast.spec.ts
CHANGED
|
@@ -356,7 +356,10 @@ describe('query builder', () => {
|
|
|
356
356
|
{
|
|
357
357
|
filter: {
|
|
358
358
|
kind: 'filter_string',
|
|
359
|
-
|
|
359
|
+
expression: {
|
|
360
|
+
kind: 'field_reference',
|
|
361
|
+
name: 'carrier',
|
|
362
|
+
},
|
|
360
363
|
filter: 'WN, AA',
|
|
361
364
|
},
|
|
362
365
|
},
|
|
@@ -406,7 +409,10 @@ describe('query builder', () => {
|
|
|
406
409
|
kind: 'where',
|
|
407
410
|
filter: {
|
|
408
411
|
kind: 'filter_string',
|
|
409
|
-
|
|
412
|
+
expression: {
|
|
413
|
+
kind: 'field_reference',
|
|
414
|
+
name: 'carrier',
|
|
415
|
+
},
|
|
410
416
|
filter: 'WN, AA',
|
|
411
417
|
},
|
|
412
418
|
},
|
|
@@ -450,7 +456,11 @@ describe('query builder', () => {
|
|
|
450
456
|
kind: 'where',
|
|
451
457
|
filter: {
|
|
452
458
|
kind: 'filter_string',
|
|
453
|
-
|
|
459
|
+
expression: {
|
|
460
|
+
kind: 'field_reference',
|
|
461
|
+
name: 'state',
|
|
462
|
+
path: ['origin'],
|
|
463
|
+
},
|
|
454
464
|
filter: 'TX',
|
|
455
465
|
},
|
|
456
466
|
},
|
|
@@ -497,7 +507,10 @@ describe('query builder', () => {
|
|
|
497
507
|
kind: 'where',
|
|
498
508
|
filter: {
|
|
499
509
|
kind: 'filter_string',
|
|
500
|
-
|
|
510
|
+
expression: {
|
|
511
|
+
kind: 'field_reference',
|
|
512
|
+
name: 'carrier',
|
|
513
|
+
},
|
|
501
514
|
filter: 'WN, AA',
|
|
502
515
|
},
|
|
503
516
|
},
|
|
@@ -553,7 +566,10 @@ describe('query builder', () => {
|
|
|
553
566
|
kind: 'where',
|
|
554
567
|
filter: {
|
|
555
568
|
kind: 'filter_string',
|
|
556
|
-
|
|
569
|
+
expression: {
|
|
570
|
+
kind: 'field_reference',
|
|
571
|
+
name: 'carrier',
|
|
572
|
+
},
|
|
557
573
|
filter: "'",
|
|
558
574
|
},
|
|
559
575
|
},
|
|
@@ -561,7 +577,10 @@ describe('query builder', () => {
|
|
|
561
577
|
kind: 'where',
|
|
562
578
|
filter: {
|
|
563
579
|
kind: 'filter_string',
|
|
564
|
-
|
|
580
|
+
expression: {
|
|
581
|
+
kind: 'field_reference',
|
|
582
|
+
name: 'carrier',
|
|
583
|
+
},
|
|
565
584
|
filter: '\'"',
|
|
566
585
|
},
|
|
567
586
|
},
|
|
@@ -569,7 +588,10 @@ describe('query builder', () => {
|
|
|
569
588
|
kind: 'where',
|
|
570
589
|
filter: {
|
|
571
590
|
kind: 'filter_string',
|
|
572
|
-
|
|
591
|
+
expression: {
|
|
592
|
+
kind: 'field_reference',
|
|
593
|
+
name: 'carrier',
|
|
594
|
+
},
|
|
573
595
|
filter: '`',
|
|
574
596
|
},
|
|
575
597
|
},
|
|
@@ -577,7 +599,10 @@ describe('query builder', () => {
|
|
|
577
599
|
kind: 'where',
|
|
578
600
|
filter: {
|
|
579
601
|
kind: 'filter_string',
|
|
580
|
-
|
|
602
|
+
expression: {
|
|
603
|
+
kind: 'field_reference',
|
|
604
|
+
name: 'carrier',
|
|
605
|
+
},
|
|
581
606
|
filter: "`'",
|
|
582
607
|
},
|
|
583
608
|
},
|
|
@@ -585,7 +610,10 @@ describe('query builder', () => {
|
|
|
585
610
|
kind: 'where',
|
|
586
611
|
filter: {
|
|
587
612
|
kind: 'filter_string',
|
|
588
|
-
|
|
613
|
+
expression: {
|
|
614
|
+
kind: 'field_reference',
|
|
615
|
+
name: 'carrier',
|
|
616
|
+
},
|
|
589
617
|
filter: '`\'"',
|
|
590
618
|
},
|
|
591
619
|
},
|
|
@@ -593,7 +621,10 @@ describe('query builder', () => {
|
|
|
593
621
|
kind: 'where',
|
|
594
622
|
filter: {
|
|
595
623
|
kind: 'filter_string',
|
|
596
|
-
|
|
624
|
+
expression: {
|
|
625
|
+
kind: 'field_reference',
|
|
626
|
+
name: 'carrier',
|
|
627
|
+
},
|
|
597
628
|
filter: "`'\"\"\"'''",
|
|
598
629
|
},
|
|
599
630
|
},
|
|
@@ -632,7 +663,8 @@ describe('query builder', () => {
|
|
|
632
663
|
segment.addDrill({
|
|
633
664
|
filter: {
|
|
634
665
|
kind: 'filter_string',
|
|
635
|
-
|
|
666
|
+
expression: {
|
|
667
|
+
kind: 'field_reference',
|
|
636
668
|
name: 'carrier',
|
|
637
669
|
},
|
|
638
670
|
filter: 'WN, AA',
|
|
@@ -641,7 +673,8 @@ describe('query builder', () => {
|
|
|
641
673
|
segment.addDrill({
|
|
642
674
|
filter: {
|
|
643
675
|
kind: 'literal_equality',
|
|
644
|
-
|
|
676
|
+
expression: {
|
|
677
|
+
kind: 'field_reference',
|
|
645
678
|
name: 'nickname',
|
|
646
679
|
path: ['top_carriers'],
|
|
647
680
|
},
|
|
@@ -668,7 +701,10 @@ describe('query builder', () => {
|
|
|
668
701
|
kind: 'drill',
|
|
669
702
|
filter: {
|
|
670
703
|
kind: 'filter_string',
|
|
671
|
-
|
|
704
|
+
expression: {
|
|
705
|
+
kind: 'field_reference',
|
|
706
|
+
name: 'carrier',
|
|
707
|
+
},
|
|
672
708
|
filter: 'WN, AA',
|
|
673
709
|
},
|
|
674
710
|
},
|
|
@@ -676,7 +712,8 @@ describe('query builder', () => {
|
|
|
676
712
|
kind: 'drill',
|
|
677
713
|
filter: {
|
|
678
714
|
kind: 'literal_equality',
|
|
679
|
-
|
|
715
|
+
expression: {
|
|
716
|
+
kind: 'field_reference',
|
|
680
717
|
name: 'nickname',
|
|
681
718
|
path: ['top_carriers'],
|
|
682
719
|
},
|
|
@@ -718,7 +755,8 @@ describe('query builder', () => {
|
|
|
718
755
|
const drill = segment.addDrill({
|
|
719
756
|
filter: {
|
|
720
757
|
kind: 'literal_equality',
|
|
721
|
-
|
|
758
|
+
expression: {
|
|
759
|
+
kind: 'field_reference',
|
|
722
760
|
name: 'nickname',
|
|
723
761
|
path: ['top_carriers'],
|
|
724
762
|
},
|
|
@@ -728,7 +766,7 @@ describe('query builder', () => {
|
|
|
728
766
|
},
|
|
729
767
|
},
|
|
730
768
|
});
|
|
731
|
-
expect(drill.filter.
|
|
769
|
+
expect(drill.filter.expression.getFieldInfo()).toMatchObject({
|
|
732
770
|
kind: 'dimension',
|
|
733
771
|
type: {kind: 'string_type'},
|
|
734
772
|
});
|
|
@@ -766,7 +804,10 @@ describe('query builder', () => {
|
|
|
766
804
|
kind: 'having',
|
|
767
805
|
filter: {
|
|
768
806
|
kind: 'filter_string',
|
|
769
|
-
|
|
807
|
+
expression: {
|
|
808
|
+
kind: 'field_reference',
|
|
809
|
+
name: 'flight_count',
|
|
810
|
+
},
|
|
770
811
|
filter: '>100',
|
|
771
812
|
},
|
|
772
813
|
},
|
|
@@ -3051,7 +3092,10 @@ describe('query builder', () => {
|
|
|
3051
3092
|
filter: {
|
|
3052
3093
|
kind: 'filter_string',
|
|
3053
3094
|
filter: '> 10',
|
|
3054
|
-
|
|
3095
|
+
expression: {
|
|
3096
|
+
kind: 'field_reference',
|
|
3097
|
+
name: 'd1',
|
|
3098
|
+
},
|
|
3055
3099
|
},
|
|
3056
3100
|
},
|
|
3057
3101
|
{
|
|
@@ -3059,7 +3103,10 @@ describe('query builder', () => {
|
|
|
3059
3103
|
filter: {
|
|
3060
3104
|
kind: 'filter_string',
|
|
3061
3105
|
filter: '> 10',
|
|
3062
|
-
|
|
3106
|
+
expression: {
|
|
3107
|
+
kind: 'field_reference',
|
|
3108
|
+
name: 'm1',
|
|
3109
|
+
},
|
|
3063
3110
|
},
|
|
3064
3111
|
},
|
|
3065
3112
|
{kind: 'limit', limit: 10},
|
|
@@ -3242,7 +3289,11 @@ describe('query builder', () => {
|
|
|
3242
3289
|
kind: 'where',
|
|
3243
3290
|
filter: {
|
|
3244
3291
|
kind: 'filter_string',
|
|
3245
|
-
|
|
3292
|
+
expression: {
|
|
3293
|
+
kind: 'field_reference',
|
|
3294
|
+
name: 'd1',
|
|
3295
|
+
path: ['r1'],
|
|
3296
|
+
},
|
|
3246
3297
|
filter: 'WN, AA',
|
|
3247
3298
|
},
|
|
3248
3299
|
},
|
|
@@ -3286,7 +3337,11 @@ describe('query builder', () => {
|
|
|
3286
3337
|
kind: 'where',
|
|
3287
3338
|
filter: {
|
|
3288
3339
|
kind: 'filter_string',
|
|
3289
|
-
|
|
3340
|
+
expression: {
|
|
3341
|
+
kind: 'field_reference',
|
|
3342
|
+
name: 'd1',
|
|
3343
|
+
path: ['rr1'],
|
|
3344
|
+
},
|
|
3290
3345
|
filter: 'WN, AA',
|
|
3291
3346
|
},
|
|
3292
3347
|
},
|
package/src/query-ast.ts
CHANGED
|
@@ -1124,16 +1124,7 @@ export class ASTFieldReference extends ASTReference {
|
|
|
1124
1124
|
parent instanceof ASTFilterWithFilterString ||
|
|
1125
1125
|
parent instanceof ASTFilterWithLiteralEquality
|
|
1126
1126
|
) {
|
|
1127
|
-
|
|
1128
|
-
| ASTFilterOperation
|
|
1129
|
-
| ASTWhereViewOperation
|
|
1130
|
-
| ASTDrillViewOperation
|
|
1131
|
-
| ASTHavingViewOperation;
|
|
1132
|
-
if (grand instanceof ASTFilterOperation) {
|
|
1133
|
-
return grand.list.expression.field.segment;
|
|
1134
|
-
} else {
|
|
1135
|
-
return grand.list.segment;
|
|
1136
|
-
}
|
|
1127
|
+
return parent.segment;
|
|
1137
1128
|
} else {
|
|
1138
1129
|
return parent.list.segment;
|
|
1139
1130
|
}
|
|
@@ -1148,14 +1139,7 @@ export class ASTFieldReference extends ASTReference {
|
|
|
1148
1139
|
|
|
1149
1140
|
getFieldInfo() {
|
|
1150
1141
|
const schema = this.getReferenceSchema();
|
|
1151
|
-
|
|
1152
|
-
this.parent instanceof ASTFilterWithLiteralEquality &&
|
|
1153
|
-
this.parent.parent instanceof ASTDrillViewOperation;
|
|
1154
|
-
if (isDrill) {
|
|
1155
|
-
return ASTNode.schemaGetDrillField(schema, this.name, this.path);
|
|
1156
|
-
} else {
|
|
1157
|
-
return ASTNode.schemaGet(schema, this.name, this.path);
|
|
1158
|
-
}
|
|
1142
|
+
return ASTNode.schemaGet(schema, this.name, this.path);
|
|
1159
1143
|
}
|
|
1160
1144
|
}
|
|
1161
1145
|
|
|
@@ -2942,7 +2926,11 @@ export class ASTSegmentViewDefinition
|
|
|
2942
2926
|
kind: 'where',
|
|
2943
2927
|
filter: {
|
|
2944
2928
|
kind: 'filter_string',
|
|
2945
|
-
|
|
2929
|
+
expression: {
|
|
2930
|
+
kind: 'field_reference',
|
|
2931
|
+
name,
|
|
2932
|
+
path,
|
|
2933
|
+
},
|
|
2946
2934
|
filter: filterString,
|
|
2947
2935
|
},
|
|
2948
2936
|
});
|
|
@@ -2980,7 +2968,11 @@ export class ASTSegmentViewDefinition
|
|
|
2980
2968
|
kind: 'having',
|
|
2981
2969
|
filter: {
|
|
2982
2970
|
kind: 'filter_string',
|
|
2983
|
-
|
|
2971
|
+
expression: {
|
|
2972
|
+
kind: 'field_reference',
|
|
2973
|
+
name,
|
|
2974
|
+
path,
|
|
2975
|
+
},
|
|
2984
2976
|
filter: filterString,
|
|
2985
2977
|
},
|
|
2986
2978
|
});
|
|
@@ -3758,7 +3750,11 @@ export class ASTAggregateViewOperation
|
|
|
3758
3750
|
const where: Malloy.FilterOperation = {
|
|
3759
3751
|
filter: {
|
|
3760
3752
|
kind: 'filter_string',
|
|
3761
|
-
|
|
3753
|
+
expression: {
|
|
3754
|
+
kind: 'field_reference',
|
|
3755
|
+
name,
|
|
3756
|
+
path,
|
|
3757
|
+
},
|
|
3762
3758
|
filter: filterString,
|
|
3763
3759
|
},
|
|
3764
3760
|
};
|
|
@@ -3966,8 +3962,18 @@ export class ASTReferenceExpression
|
|
|
3966
3962
|
/**
|
|
3967
3963
|
* @internal
|
|
3968
3964
|
*/
|
|
3969
|
-
get
|
|
3970
|
-
|
|
3965
|
+
get segment() {
|
|
3966
|
+
const parent = this.parent;
|
|
3967
|
+
if (parent instanceof ASTField) {
|
|
3968
|
+
return parent.segment;
|
|
3969
|
+
} else if (
|
|
3970
|
+
parent instanceof ASTFilterWithFilterString ||
|
|
3971
|
+
parent instanceof ASTFilterWithLiteralEquality
|
|
3972
|
+
) {
|
|
3973
|
+
return parent.segment;
|
|
3974
|
+
} else {
|
|
3975
|
+
throw new Error('Invalid expression parent');
|
|
3976
|
+
}
|
|
3971
3977
|
}
|
|
3972
3978
|
|
|
3973
3979
|
get path() {
|
|
@@ -3979,8 +3985,13 @@ export class ASTReferenceExpression
|
|
|
3979
3985
|
}
|
|
3980
3986
|
|
|
3981
3987
|
getFieldInfo(): Malloy.FieldInfoWithDimension | Malloy.FieldInfoWithMeasure {
|
|
3982
|
-
const schema = this.
|
|
3983
|
-
const
|
|
3988
|
+
const schema = this.segment.getInputSchema();
|
|
3989
|
+
const isDrill =
|
|
3990
|
+
this.parent instanceof ASTFilterWithLiteralEquality &&
|
|
3991
|
+
this.parent.parent instanceof ASTDrillViewOperation;
|
|
3992
|
+
const def = isDrill
|
|
3993
|
+
? ASTNode.schemaGetDrillField(schema, this.name, this.path)
|
|
3994
|
+
: ASTNode.schemaGet(schema, this.name, this.path);
|
|
3984
3995
|
if (def.kind !== 'dimension' && def.kind !== 'measure') {
|
|
3985
3996
|
throw new Error('Invalid field for ASTReferenceExpression');
|
|
3986
3997
|
}
|
|
@@ -4117,6 +4128,10 @@ export class ASTLiteralValueExpression extends ASTObjectNode<
|
|
|
4117
4128
|
getInheritedAnnotations(): Malloy.Annotation[] {
|
|
4118
4129
|
return [];
|
|
4119
4130
|
}
|
|
4131
|
+
|
|
4132
|
+
getFieldInfo() {
|
|
4133
|
+
return undefined;
|
|
4134
|
+
}
|
|
4120
4135
|
}
|
|
4121
4136
|
|
|
4122
4137
|
export class ASTFilterOperation extends ASTObjectNode<
|
|
@@ -4499,13 +4514,25 @@ export const ASTFilter = {
|
|
|
4499
4514
|
return new ASTFilterWithLiteralEquality(filter);
|
|
4500
4515
|
}
|
|
4501
4516
|
},
|
|
4517
|
+
getSegment(filter: ASTFilter) {
|
|
4518
|
+
const grand = filter.parent as
|
|
4519
|
+
| ASTFilterOperation
|
|
4520
|
+
| ASTWhereViewOperation
|
|
4521
|
+
| ASTDrillViewOperation
|
|
4522
|
+
| ASTHavingViewOperation;
|
|
4523
|
+
if (grand instanceof ASTFilterOperation) {
|
|
4524
|
+
return grand.list.expression.field.segment;
|
|
4525
|
+
} else {
|
|
4526
|
+
return grand.list.segment;
|
|
4527
|
+
}
|
|
4528
|
+
},
|
|
4502
4529
|
};
|
|
4503
4530
|
|
|
4504
4531
|
export class ASTFilterWithFilterString extends ASTObjectNode<
|
|
4505
4532
|
Malloy.FilterWithFilterString,
|
|
4506
4533
|
{
|
|
4507
4534
|
kind: 'filter_string';
|
|
4508
|
-
|
|
4535
|
+
expression: ASTExpression;
|
|
4509
4536
|
filter: string;
|
|
4510
4537
|
}
|
|
4511
4538
|
> {
|
|
@@ -4513,13 +4540,13 @@ export class ASTFilterWithFilterString extends ASTObjectNode<
|
|
|
4513
4540
|
constructor(public node: Malloy.FilterWithFilterString) {
|
|
4514
4541
|
super(node, {
|
|
4515
4542
|
kind: 'filter_string',
|
|
4516
|
-
|
|
4543
|
+
expression: ASTExpression.from(node.expression),
|
|
4517
4544
|
filter: node.filter,
|
|
4518
4545
|
});
|
|
4519
4546
|
}
|
|
4520
4547
|
|
|
4521
|
-
get
|
|
4522
|
-
return this.children.
|
|
4548
|
+
get expression() {
|
|
4549
|
+
return this.children.expression;
|
|
4523
4550
|
}
|
|
4524
4551
|
|
|
4525
4552
|
get filterString() {
|
|
@@ -4537,9 +4564,16 @@ export class ASTFilterWithFilterString extends ASTObjectNode<
|
|
|
4537
4564
|
this.filterString = filterString;
|
|
4538
4565
|
}
|
|
4539
4566
|
|
|
4567
|
+
get segment() {
|
|
4568
|
+
return ASTFilter.getSegment(this);
|
|
4569
|
+
}
|
|
4570
|
+
|
|
4540
4571
|
getFieldInfo() {
|
|
4541
|
-
const field = this.
|
|
4542
|
-
if (
|
|
4572
|
+
const field = this.expression.getFieldInfo();
|
|
4573
|
+
if (
|
|
4574
|
+
field === undefined ||
|
|
4575
|
+
(field.kind !== 'dimension' && field.kind !== 'measure')
|
|
4576
|
+
) {
|
|
4543
4577
|
throw new Error('Invalid field type for filter with filter string');
|
|
4544
4578
|
}
|
|
4545
4579
|
return field;
|
|
@@ -4570,7 +4604,7 @@ export class ASTFilterWithLiteralEquality extends ASTObjectNode<
|
|
|
4570
4604
|
Malloy.FilterWithLiteralEquality,
|
|
4571
4605
|
{
|
|
4572
4606
|
kind: 'literal_equality';
|
|
4573
|
-
|
|
4607
|
+
expression: ASTExpression;
|
|
4574
4608
|
value: ASTLiteralValue;
|
|
4575
4609
|
}
|
|
4576
4610
|
> {
|
|
@@ -4578,18 +4612,22 @@ export class ASTFilterWithLiteralEquality extends ASTObjectNode<
|
|
|
4578
4612
|
constructor(public node: Malloy.FilterWithLiteralEquality) {
|
|
4579
4613
|
super(node, {
|
|
4580
4614
|
kind: 'literal_equality',
|
|
4581
|
-
|
|
4615
|
+
expression: ASTExpression.from(node.expression),
|
|
4582
4616
|
value: ASTLiteralValue.from(node.value),
|
|
4583
4617
|
});
|
|
4584
4618
|
}
|
|
4585
4619
|
|
|
4586
|
-
get
|
|
4587
|
-
return this.children.
|
|
4620
|
+
get expression() {
|
|
4621
|
+
return this.children.expression;
|
|
4588
4622
|
}
|
|
4589
4623
|
|
|
4590
4624
|
get value() {
|
|
4591
4625
|
return this.children.value;
|
|
4592
4626
|
}
|
|
4627
|
+
|
|
4628
|
+
get segment() {
|
|
4629
|
+
return ASTFilter.getSegment(this);
|
|
4630
|
+
}
|
|
4593
4631
|
}
|
|
4594
4632
|
|
|
4595
4633
|
export class ASTView
|