@malloydata/malloy 0.0.97-dev231026184251 → 0.0.97

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.
@@ -98,6 +98,7 @@ import { IgnoredModelAnnotationsContext } from "./MalloyParser";
98
98
  import { TopLevelQueryDefsContext } from "./MalloyParser";
99
99
  import { TopLevelQueryDefContext } from "./MalloyParser";
100
100
  import { RefineOperatorContext } from "./MalloyParser";
101
+ import { TurtleNameContext } from "./MalloyParser";
101
102
  import { QueryRefinementContext } from "./MalloyParser";
102
103
  import { SourceExtensionContext } from "./MalloyParser";
103
104
  import { SqlSourceContext } from "./MalloyParser";
@@ -1352,6 +1353,16 @@ export interface MalloyParserListener extends ParseTreeListener {
1352
1353
  * @param ctx the parse tree
1353
1354
  */
1354
1355
  exitRefineOperator?: (ctx: RefineOperatorContext) => void;
1356
+ /**
1357
+ * Enter a parse tree produced by `MalloyParser.turtleName`.
1358
+ * @param ctx the parse tree
1359
+ */
1360
+ enterTurtleName?: (ctx: TurtleNameContext) => void;
1361
+ /**
1362
+ * Exit a parse tree produced by `MalloyParser.turtleName`.
1363
+ * @param ctx the parse tree
1364
+ */
1365
+ exitTurtleName?: (ctx: TurtleNameContext) => void;
1355
1366
  /**
1356
1367
  * Enter a parse tree produced by `MalloyParser.queryRefinement`.
1357
1368
  * @param ctx the parse tree
@@ -98,6 +98,7 @@ import { IgnoredModelAnnotationsContext } from "./MalloyParser";
98
98
  import { TopLevelQueryDefsContext } from "./MalloyParser";
99
99
  import { TopLevelQueryDefContext } from "./MalloyParser";
100
100
  import { RefineOperatorContext } from "./MalloyParser";
101
+ import { TurtleNameContext } from "./MalloyParser";
101
102
  import { QueryRefinementContext } from "./MalloyParser";
102
103
  import { SourceExtensionContext } from "./MalloyParser";
103
104
  import { SqlSourceContext } from "./MalloyParser";
@@ -883,6 +884,12 @@ export interface MalloyParserVisitor<Result> extends ParseTreeVisitor<Result> {
883
884
  * @return the visitor result
884
885
  */
885
886
  visitRefineOperator?: (ctx: RefineOperatorContext) => Result;
887
+ /**
888
+ * Visit a parse tree produced by `MalloyParser.turtleName`.
889
+ * @param ctx the parse tree
890
+ * @return the visitor result
891
+ */
892
+ visitTurtleName?: (ctx: TurtleNameContext) => Result;
886
893
  /**
887
894
  * Visit a parse tree produced by `MalloyParser.queryRefinement`.
888
895
  * @param ctx the parse tree
@@ -81,7 +81,7 @@ export declare class MalloyToAST extends AbstractParseTreeVisitor<ast.MalloyElem
81
81
  visitMalloyDocument(pcx: parse.MalloyDocumentContext): ast.Document;
82
82
  visitDefineSourceStatement(pcx: parse.DefineSourceStatementContext): ast.DefineSourceList;
83
83
  visitSourceDefinition(pcx: parse.SourceDefinitionContext): ast.DefineSource;
84
- protected getQueryRefinements(pcx: parse.QueryRefinementContext): ast.QOPDesc;
84
+ protected getQueryRefinements(pcx: parse.QueryRefinementContext): ast.QOPDesc | ast.ViewFieldReference;
85
85
  protected getSourceExtensions(pcx: parse.SourceExtensionContext): ast.SourceDesc;
86
86
  visitExploreProperties(pcx: parse.ExplorePropertiesContext): ast.SourceDesc;
87
87
  visitTableFunction(pcx: parse.TableFunctionContext): ast.TableSource;
@@ -292,14 +292,22 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
292
292
  return this.astAt(exploreDef, pcx);
293
293
  }
294
294
  getQueryRefinements(pcx) {
295
- const properties = this.astAt(this.visitQueryProperties(pcx.queryProperties()), pcx);
296
- if (pcx.REFINE()) {
297
- this.contextError(pcx, 'The experimental "refine" operator is deprecated, use the "+" operator', 'warn');
295
+ const propertiesCx = pcx.queryProperties();
296
+ if (propertiesCx) {
297
+ const properties = this.astAt(this.visitQueryProperties(propertiesCx), pcx);
298
+ if (pcx.REFINE()) {
299
+ this.contextError(pcx, 'The experimental "refine" operator is deprecated, use the "+" operator', 'warn');
300
+ }
301
+ else if (!pcx.refineOperator()) {
302
+ this.contextError(pcx, 'Implicit query refinement is deprecated, use the `+` operator', 'warn');
303
+ }
304
+ return properties;
298
305
  }
299
- else if (!pcx.refineOperator()) {
300
- this.contextError(pcx, 'Implicit query refinement is deprecated, use the `+` operator', 'warn');
306
+ const turtleNameCx = pcx.turtleName();
307
+ if (turtleNameCx) {
308
+ return this.astAt(new ast.ViewFieldReference([this.getFieldName(turtleNameCx)]), turtleNameCx);
301
309
  }
302
- return properties;
310
+ throw this.internalError(pcx, 'Expected either query properties or a view name in refinements');
303
311
  }
304
312
  getSourceExtensions(pcx) {
305
313
  const extensions = pcx === null || pcx === void 0 ? void 0 : pcx.exploreProperties();
@@ -760,10 +768,10 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
760
768
  const queryDesc = this.visitQueryProperties(propsCx);
761
769
  pipe.addSegments(queryDesc);
762
770
  }
763
- const rcx = firstCx.queryRefinement();
764
- if (rcx) {
765
- const queryDesc = this.getQueryRefinements(rcx);
766
- pipe.refineWith(queryDesc);
771
+ const rcxs = firstCx.queryRefinement();
772
+ if (rcxs.length > 0) {
773
+ const queryDescs = rcxs.map(rcx => this.getQueryRefinements(rcx));
774
+ pipe.refineWith(queryDescs);
767
775
  }
768
776
  const tail = this.getSegments(pipeCx.pipeElement());
769
777
  pipe.addSegments(...tail);
@@ -817,11 +825,11 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
817
825
  }
818
826
  visitNestExisting(pcx) {
819
827
  const name = this.getFieldName(pcx.queryName());
820
- const rcx = pcx.queryRefinement();
828
+ const rcxs = pcx.queryRefinement();
821
829
  const notes = this.getNotes(pcx.tags());
822
- if (rcx) {
830
+ if (rcxs.length > 0) {
823
831
  const nestRefine = new ast.NestRefinement(name);
824
- const queryDesc = this.getQueryRefinements(rcx);
832
+ const queryDesc = rcxs.map(rcx => this.getQueryRefinements(rcx));
825
833
  nestRefine.refineWith(queryDesc);
826
834
  nestRefine.extendNote({ notes });
827
835
  return this.astAt(nestRefine, pcx);
@@ -1291,9 +1299,9 @@ class MalloyToAST extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor {
1291
1299
  viewParts.push(new ast.ViewFieldReference([
1292
1300
  this.astAt(new ast.FieldName(headId), headIdCx),
1293
1301
  ]));
1294
- const andRefined = headCx.queryRefinement();
1295
- if (andRefined) {
1296
- viewParts.push(this.getQueryRefinements(andRefined));
1302
+ const rcxs = headCx.queryRefinement();
1303
+ if (rcxs.length > 0) {
1304
+ viewParts.push(...rcxs.map(rcx => this.getQueryRefinements(rcx)));
1297
1305
  }
1298
1306
  }
1299
1307
  const qopCx = headCx.queryProperties();
@@ -0,0 +1 @@
1
+ import './parse-expects';
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files
7
+ * (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge,
9
+ * publish, distribute, sublicense, and/or sell copies of the Software,
10
+ * and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const test_translator_1 = require("./test-translator");
26
+ require("./parse-expects");
27
+ describe('lenses', () => {
28
+ test('long lens patterns', () => {
29
+ expect((0, test_translator_1.markSource) `
30
+ source: s1 is a extend {
31
+ view: v1 is { group_by: d1 is 1 }
32
+ view: v2 is { group_by: d2 is 2 }
33
+ view: v3 is { group_by: d3 is 3 }
34
+ view: v4 is v1 + v2 + v3
35
+ view: v5 is {
36
+ nest: n1 is v1 + v2 + v3
37
+ nest: n2 is { group_by: d1 is 1 } + v2 + v3
38
+ nest: n3 is v1 + v2 + { group_by: d3 is 3 }
39
+ }
40
+ view: v6 is { group_by: d1 is 1 } + v2 + v3
41
+ }
42
+ run: s1 -> v4
43
+ source: s2 is s1 -> v1 + v2 + v3
44
+ source: s3 is s1 -> { group_by: d1 is 1 } + v2 + v3
45
+ `).toTranslate();
46
+ });
47
+ test('cannot have overlapping names', () => {
48
+ expect((0, test_translator_1.markSource) `
49
+ source: x is a extend {
50
+ dimension: n is 1
51
+ view: d is { group_by: n }
52
+ }
53
+ run: x -> d + d
54
+ `).translationToFailWith('overlapping fields in refinement: n');
55
+ });
56
+ test('cannot override limit', () => {
57
+ expect((0, test_translator_1.markSource) `
58
+ source: x is a extend {
59
+ view: d1 is { group_by: n1 is 1; limit: 10 }
60
+ view: d2 is { group_by: n2 is 2; limit: 20 }
61
+ }
62
+ run: x -> d1 + d2
63
+ `).translationToFailWith('refinement cannot override existing limit');
64
+ });
65
+ test('cannot override ordering', () => {
66
+ expect((0, test_translator_1.markSource) `
67
+ source: x is a extend {
68
+ view: d1 is { group_by: n1 is 1; order_by: n1 }
69
+ view: d2 is { group_by: n2 is 2; order_by: n2 }
70
+ }
71
+ run: x -> d1 + d2
72
+ `).translationToFailWith('refinement cannot override existing ordering');
73
+ });
74
+ test('can add a limit late', () => {
75
+ expect((0, test_translator_1.markSource) `
76
+ source: x is a extend {
77
+ view: d1 is { group_by: n1 is 1 }
78
+ view: d2 is { group_by: n2 is 2 }
79
+ }
80
+ run: x -> d1 + d2 + { limit: 10 }
81
+ `).toTranslate();
82
+ });
83
+ test('cannot refine with incompatible view types', () => {
84
+ expect((0, test_translator_1.markSource) `
85
+ source: x is a extend {
86
+ view: grp is { group_by: n1 is 1 }
87
+ view: proj is { select: n2 is 2 }
88
+ view: idx is { index: * }
89
+ }
90
+ run: x -> grp + proj
91
+ run: x -> grp + idx
92
+ run: x -> proj + idx
93
+ run: x -> proj + grp
94
+ run: x -> idx + grp
95
+ run: x -> idx + proj
96
+ `).translationToFailWith('cannot refine reduce view with project view', 'cannot refine reduce view with index view', 'cannot refine project view with index view', 'cannot refine project view with reduce view', 'cannot refine index view with reduce view', 'cannot refine index view with project view');
97
+ });
98
+ test('cannot reference dimension', () => {
99
+ expect((0, test_translator_1.markSource) `
100
+ source: x is a extend {
101
+ dimension: n is 1
102
+ view: d is { group_by: n }
103
+ }
104
+ run: x -> d + n
105
+ `).translationToFailWith('named refinement `n` must be a view, found a number');
106
+ });
107
+ test('cannot reference join', () => {
108
+ expect((0, test_translator_1.markSource) `
109
+ source: x is a extend {
110
+ join_one: b is a on true
111
+ dimension: n is 1
112
+ view: d is { group_by: n }
113
+ }
114
+ run: x -> d + b
115
+ `).translationToFailWith('named refinement `b` must be a view, found a struct');
116
+ });
117
+ test('cannot named-refine multi-stage query', () => {
118
+ expect((0, test_translator_1.markSource) `
119
+ source: x is a extend {
120
+ join_one: b is a on true
121
+ dimension: n is 1
122
+ view: multi is { group_by: n } -> { group_by: n }
123
+ view: d is { group_by: n }
124
+ }
125
+ run: x -> multi + d
126
+ run: x -> d + multi
127
+ `).translationToFailWith('Named refinements of multi-stage views are not supported', 'named refinement `multi` must have exactly one stage');
128
+ });
129
+ });
130
+ //# sourceMappingURL=lenses.spec.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.97-dev231026184251",
3
+ "version": "0.0.97",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",