@malloydata/malloy 0.0.304 → 0.0.306

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.
Files changed (74) hide show
  1. package/dist/dialect/dialect.d.ts +1 -1
  2. package/dist/dialect/duckdb/duckdb.d.ts +1 -1
  3. package/dist/dialect/duckdb/duckdb.js +2 -6
  4. package/dist/dialect/mysql/mysql.d.ts +1 -1
  5. package/dist/dialect/mysql/mysql.js +2 -6
  6. package/dist/dialect/postgres/postgres.d.ts +1 -1
  7. package/dist/dialect/postgres/postgres.js +2 -6
  8. package/dist/dialect/snowflake/snowflake.d.ts +1 -1
  9. package/dist/dialect/snowflake/snowflake.js +2 -5
  10. package/dist/dialect/standardsql/standardsql.d.ts +1 -1
  11. package/dist/dialect/standardsql/standardsql.js +2 -6
  12. package/dist/dialect/trino/trino.d.ts +1 -1
  13. package/dist/dialect/trino/trino.js +2 -6
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.js +2 -3
  16. package/dist/lang/ast/expressions/expr-aggregate-function.js +12 -2
  17. package/dist/lang/ast/expressions/expr-count.js +3 -1
  18. package/dist/lang/ast/expressions/expr-func.js +34 -10
  19. package/dist/lang/ast/expressions/expr-props.js +1 -1
  20. package/dist/lang/ast/expressions/expr-ungroup.js +7 -3
  21. package/dist/lang/ast/expressions/function-ordering.d.ts +19 -5
  22. package/dist/lang/ast/expressions/function-ordering.js +61 -9
  23. package/dist/lang/ast/field-space/include-utils.js +1 -1
  24. package/dist/lang/ast/field-space/index-field-space.js +3 -1
  25. package/dist/lang/ast/field-space/query-spaces.js +20 -11
  26. package/dist/lang/ast/query-builders/index-builder.js +1 -1
  27. package/dist/lang/ast/query-builders/reduce-builder.js +1 -1
  28. package/dist/lang/ast/query-elements/query-arrow.js +14 -4
  29. package/dist/lang/ast/query-elements/query-base.d.ts +1 -0
  30. package/dist/lang/ast/query-elements/query-base.js +14 -4
  31. package/dist/lang/ast/query-elements/query-refine.js +2 -0
  32. package/dist/lang/ast/query-properties/drill.js +1 -1
  33. package/dist/lang/ast/source-properties/join.js +6 -2
  34. package/dist/lang/ast/statements/define-source.js +1 -1
  35. package/dist/lang/ast/types/expr-value.js +1 -1
  36. package/dist/lang/ast/view-elements/reference-view.js +4 -1
  37. package/dist/lang/ast/view-elements/refine-utils.js +1 -1
  38. package/dist/{model/composite_source_utils.d.ts → lang/composite-source-utils.d.ts} +4 -17
  39. package/dist/{model/composite_source_utils.js → lang/composite-source-utils.js} +274 -44
  40. package/dist/lang/test/parse-expects.d.ts +1 -1
  41. package/dist/lang/test/parse-expects.js +6 -2
  42. package/dist/lang/test/test-translator.js +1 -1
  43. package/dist/malloy.js +1 -1
  44. package/dist/model/expression_compiler.d.ts +26 -0
  45. package/dist/model/expression_compiler.js +774 -0
  46. package/dist/model/field_instance.d.ts +108 -0
  47. package/dist/model/field_instance.js +520 -0
  48. package/dist/model/index.d.ts +5 -1
  49. package/dist/model/index.js +25 -4
  50. package/dist/model/join_instance.d.ts +18 -0
  51. package/dist/model/join_instance.js +71 -0
  52. package/dist/model/malloy_types.d.ts +48 -2
  53. package/dist/model/malloy_types.js +39 -1
  54. package/dist/model/query_model.d.ts +2 -0
  55. package/dist/model/query_model.js +7 -0
  56. package/dist/model/query_model_contract.d.ts +32 -0
  57. package/dist/model/query_model_contract.js +7 -0
  58. package/dist/model/query_model_impl.d.ts +30 -0
  59. package/dist/model/query_model_impl.js +266 -0
  60. package/dist/model/query_node.d.ts +132 -0
  61. package/dist/model/query_node.js +638 -0
  62. package/dist/model/query_query.d.ts +86 -0
  63. package/dist/model/query_query.js +1724 -0
  64. package/dist/model/sql_block.js +2 -2
  65. package/dist/model/stage_writer.d.ts +25 -0
  66. package/dist/model/stage_writer.js +120 -0
  67. package/dist/model/utils.d.ts +18 -1
  68. package/dist/model/utils.js +66 -1
  69. package/dist/to_stable.js +3 -4
  70. package/dist/version.d.ts +1 -1
  71. package/dist/version.js +1 -1
  72. package/package.json +4 -4
  73. package/dist/model/malloy_query.d.ts +0 -391
  74. package/dist/model/malloy_query.js +0 -3926
@@ -24,7 +24,7 @@
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.compileSQLInterpolation = compileSQLInterpolation;
26
26
  exports.sqlKey = sqlKey;
27
- const malloy_query_1 = require("./malloy_query");
27
+ const query_model_1 = require("./query_model");
28
28
  const malloy_types_1 = require("./malloy_types");
29
29
  const utils_1 = require("./utils");
30
30
  function compileSQLInterpolation(select, connection, partialModel) {
@@ -42,7 +42,7 @@ function compileSQLInterpolation(select, connection, partialModel) {
42
42
  if (!partialModel) {
43
43
  throw new Error('Internal error: Partial model missing when compiling SQL block');
44
44
  }
45
- queryModel = new malloy_query_1.QueryModel(partialModel);
45
+ queryModel = (0, query_model_1.makeQueryModel)(partialModel);
46
46
  }
47
47
  const compiledSql = queryModel.compileQuery(segment, {
48
48
  defaultRowLimit: undefined,
@@ -0,0 +1,25 @@
1
+ import type { Dialect } from '../dialect';
2
+ import type { QueryToMaterialize, StructDef, Query } from './malloy_types';
3
+ export declare class StageWriter {
4
+ parent: StageWriter | undefined;
5
+ withs: string[];
6
+ udfs: string[];
7
+ pdts: string[];
8
+ dependenciesToMaterialize: Record<string, QueryToMaterialize>;
9
+ stagePrefix: string;
10
+ useCTE: boolean;
11
+ constructor(useCTE: boolean | undefined, parent: StageWriter | undefined);
12
+ getName(id: number): string;
13
+ root(): StageWriter;
14
+ addStage(sql: string): string;
15
+ addUDF(stageWriter: StageWriter, dialect: Dialect, structDef: StructDef): string;
16
+ addMaterializedQuery(fieldName: string, query: Query, materializatedTablePrefix?: string): string;
17
+ addPDT(baseName: string, dialect: Dialect): string;
18
+ combineStages(includeLastStage: boolean): {
19
+ sql: string;
20
+ lastStageName: string | undefined;
21
+ };
22
+ /** emit the SQL for all the stages. */
23
+ generateSQLStages(): string;
24
+ generateCoorelatedSubQuery(dialect: Dialect, structDef: StructDef): string;
25
+ }
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright Contributors to the Malloy project
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.StageWriter = void 0;
8
+ const utils_1 = require("./materialization/utils");
9
+ const utils_2 = require("./utils");
10
+ class StageWriter {
11
+ constructor(useCTE = true, parent) {
12
+ this.parent = parent;
13
+ this.withs = [];
14
+ this.udfs = [];
15
+ this.pdts = [];
16
+ this.dependenciesToMaterialize = {};
17
+ this.stagePrefix = '__stage';
18
+ this.useCTE = useCTE;
19
+ }
20
+ getName(id) {
21
+ return `${this.stagePrefix}${id}`;
22
+ }
23
+ root() {
24
+ if (this.parent === undefined) {
25
+ return this;
26
+ }
27
+ else {
28
+ return this.parent.root();
29
+ }
30
+ }
31
+ addStage(sql) {
32
+ if (this.useCTE) {
33
+ this.withs.push(sql);
34
+ return this.getName(this.withs.length - 1);
35
+ }
36
+ else {
37
+ this.withs[0] = sql;
38
+ return (0, utils_2.indent)(`\n(${sql})\n`);
39
+ }
40
+ }
41
+ addUDF(stageWriter, dialect, structDef) {
42
+ var _a;
43
+ // eslint-disable-next-line prefer-const
44
+ let { sql, lastStageName } = stageWriter.combineStages(true);
45
+ if (lastStageName === undefined) {
46
+ throw new Error('Internal Error: no stage to combine');
47
+ }
48
+ sql += dialect.sqlCreateFunctionCombineLastStage(lastStageName, (0, utils_2.getDialectFieldList)(structDef), (_a = structDef.resultMetadata) === null || _a === void 0 ? void 0 : _a.orderBy);
49
+ const id = `${dialect.udfPrefix}${this.root().udfs.length}`;
50
+ sql = dialect.sqlCreateFunction(id, sql);
51
+ this.root().udfs.push(sql);
52
+ return id;
53
+ }
54
+ addMaterializedQuery(fieldName, query, materializatedTablePrefix) {
55
+ var _a;
56
+ const name = query.name;
57
+ if (!name) {
58
+ throw new Error(`Source ${fieldName} on a unnamed query that is tagged as materialize, only named queries can be materialized.`);
59
+ }
60
+ const path = (_a = query.location) === null || _a === void 0 ? void 0 : _a.url;
61
+ if (!path) {
62
+ throw new Error(`Trying to materialize query ${name}, but its path is not set.`);
63
+ }
64
+ // Creating an object that should uniquely identify a query within a Malloy model repo.
65
+ const queryMaterializationSpec = (0, utils_1.buildQueryMaterializationSpec)(path, name, materializatedTablePrefix);
66
+ this.root().dependenciesToMaterialize[queryMaterializationSpec.id] =
67
+ queryMaterializationSpec;
68
+ return queryMaterializationSpec.id;
69
+ }
70
+ addPDT(baseName, dialect) {
71
+ const sql = this.combineStages(false).sql + this.withs[this.withs.length - 1];
72
+ const name = baseName + (0, utils_2.generateHash)(sql);
73
+ const tableName = `scratch.${name}`;
74
+ this.root().pdts.push(dialect.sqlCreateTableAsSelect(tableName, sql));
75
+ return tableName;
76
+ }
77
+ // combine all the stages except the last one into a WITH statement
78
+ // return SQL and the last stage name
79
+ combineStages(includeLastStage) {
80
+ if (!this.useCTE) {
81
+ return { sql: this.withs[0], lastStageName: this.withs[0] };
82
+ }
83
+ let lastStageName = this.getName(0);
84
+ let prefix = 'WITH ';
85
+ let w = '';
86
+ for (let i = 0; i < this.withs.length - (includeLastStage ? 0 : 1); i++) {
87
+ const sql = this.withs[i];
88
+ lastStageName = this.getName(i);
89
+ if (sql === undefined) {
90
+ throw new Error(`Expected sql WITH to be present for stage ${lastStageName}.`);
91
+ }
92
+ w += `${prefix}${lastStageName} AS (\n${(0, utils_2.indent)(sql)})\n`;
93
+ prefix = ', ';
94
+ }
95
+ return { sql: w, lastStageName };
96
+ }
97
+ /** emit the SQL for all the stages. */
98
+ generateSQLStages() {
99
+ const lastStageNum = this.withs.length - 1;
100
+ if (lastStageNum < 0) {
101
+ throw new Error('No SQL generated');
102
+ }
103
+ const udfs = this.udfs.join('\n');
104
+ const pdts = this.pdts.join('\n');
105
+ const sql = this.combineStages(false).sql;
106
+ return udfs + pdts + sql + this.withs[lastStageNum];
107
+ }
108
+ generateCoorelatedSubQuery(dialect, structDef) {
109
+ var _a, _b;
110
+ if (!this.useCTE) {
111
+ return dialect.sqlCreateFunctionCombineLastStage(`(${this.withs[0]})`, (0, utils_2.getDialectFieldList)(structDef), (_a = structDef.resultMetadata) === null || _a === void 0 ? void 0 : _a.orderBy);
112
+ }
113
+ else {
114
+ return (this.combineStages(true).sql +
115
+ dialect.sqlCreateFunctionCombineLastStage(this.getName(this.withs.length - 1), (0, utils_2.getDialectFieldList)(structDef), (_b = structDef.resultMetadata) === null || _b === void 0 ? void 0 : _b.orderBy));
116
+ }
117
+ }
118
+ }
119
+ exports.StageWriter = StageWriter;
120
+ //# sourceMappingURL=stage_writer.js.map
@@ -1,4 +1,5 @@
1
- import type { Expr, GenericSQLExpr } from './malloy_types';
1
+ import type { Expr, GenericSQLExpr, StructDef } from './malloy_types';
2
+ import type { DialectFieldList } from '../dialect';
2
3
  /** simple indent function */
3
4
  export declare function indent(s: string): string;
4
5
  /**
@@ -32,3 +33,19 @@ export type SQLExprElement = string | Expr;
32
33
  * expression node.
33
34
  */
34
35
  export declare function composeSQLExpr(from: SQLExprElement[]): GenericSQLExpr;
36
+ export declare function getDialectFieldList(structDef: StructDef): DialectFieldList;
37
+ /**
38
+ * A little extra paranoia to save us from fields which contain the special
39
+ * characters used to build the grouping key.
40
+ */
41
+ export declare function pathToKey(node: string, fields: string[]): string;
42
+ export declare function groupingKey(node: string, fields: string[]): string;
43
+ export declare function caseGroup(groupSets: number[], s: string): string;
44
+ export declare class GenerateState {
45
+ whereSQL?: string;
46
+ applyValue?: string;
47
+ totalGroupSet: number;
48
+ withWhere(s?: string): GenerateState;
49
+ withApply(s: string): GenerateState;
50
+ withTotal(groupSet: number): GenerateState;
51
+ }
@@ -22,7 +22,7 @@
22
22
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.AndChain = void 0;
25
+ exports.GenerateState = exports.AndChain = void 0;
26
26
  exports.indent = indent;
27
27
  exports.generateSQLStringLiteral = generateSQLStringLiteral;
28
28
  exports.generateHash = generateHash;
@@ -32,6 +32,10 @@ exports.exprKids = exprKids;
32
32
  exports.exprWalk = exprWalk;
33
33
  exports.exprMap = exprMap;
34
34
  exports.composeSQLExpr = composeSQLExpr;
35
+ exports.getDialectFieldList = getDialectFieldList;
36
+ exports.pathToKey = pathToKey;
37
+ exports.groupingKey = groupingKey;
38
+ exports.caseGroup = caseGroup;
35
39
  const uuid_1 = require("uuid");
36
40
  const malloy_types_1 = require("./malloy_types");
37
41
  /** simple indent function */
@@ -195,4 +199,65 @@ function composeSQLExpr(from) {
195
199
  }
196
200
  return ret;
197
201
  }
202
+ function getDialectFieldList(structDef) {
203
+ const dialectFieldList = [];
204
+ for (const f of structDef.fields.filter(malloy_types_1.fieldIsIntrinsic)) {
205
+ dialectFieldList.push({
206
+ typeDef: f,
207
+ sqlExpression: (0, malloy_types_1.getIdentifier)(f),
208
+ rawName: (0, malloy_types_1.getIdentifier)(f),
209
+ sqlOutputName: (0, malloy_types_1.getIdentifier)(f),
210
+ });
211
+ }
212
+ return dialectFieldList;
213
+ }
214
+ /**
215
+ * A little extra paranoia to save us from fields which contain the special
216
+ * characters used to build the grouping key.
217
+ */
218
+ function pathToKey(node, fields) {
219
+ return node + `/${fields.map(f => `${f.length}:${f}`).join(',')}`;
220
+ }
221
+ function groupingKey(node, fields) {
222
+ const sortedFields = [...fields].sort();
223
+ return pathToKey(node, sortedFields);
224
+ }
225
+ function caseGroup(groupSets, s) {
226
+ if (groupSets.length === 0) {
227
+ return s;
228
+ }
229
+ else {
230
+ const exp = groupSets.length === 1
231
+ ? `=${groupSets[0]}`
232
+ : ` IN (${groupSets.join(',')})`;
233
+ return `CASE WHEN group_set${exp} THEN\n ${s}\n END`;
234
+ }
235
+ }
236
+ class GenerateState {
237
+ constructor() {
238
+ this.totalGroupSet = -1;
239
+ }
240
+ withWhere(s) {
241
+ const newState = new GenerateState();
242
+ newState.whereSQL = s;
243
+ newState.applyValue = this.applyValue;
244
+ newState.totalGroupSet = this.totalGroupSet;
245
+ return newState;
246
+ }
247
+ withApply(s) {
248
+ const newState = new GenerateState();
249
+ newState.whereSQL = this.whereSQL;
250
+ newState.applyValue = s;
251
+ newState.totalGroupSet = this.totalGroupSet;
252
+ return newState;
253
+ }
254
+ withTotal(groupSet) {
255
+ const newState = new GenerateState();
256
+ newState.whereSQL = this.whereSQL;
257
+ newState.applyValue = this.applyValue;
258
+ newState.totalGroupSet = groupSet;
259
+ return newState;
260
+ }
261
+ }
262
+ exports.GenerateState = GenerateState;
198
263
  //# sourceMappingURL=utils.js.map
package/dist/to_stable.js CHANGED
@@ -48,7 +48,6 @@ exports.writeMalloyObjectToTag = writeMalloyObjectToTag;
48
48
  exports.extractMalloyObjectFromTag = extractMalloyObjectFromTag;
49
49
  const Malloy = __importStar(require("@malloydata/malloy-interfaces"));
50
50
  const model_1 = require("./model");
51
- const malloy_query_1 = require("./model/malloy_query");
52
51
  const annotation_1 = require("./annotation");
53
52
  const malloy_tag_1 = require("@malloydata/malloy-tag");
54
53
  function sourceDefToSourceInfo(sourceDef) {
@@ -100,7 +99,7 @@ function modelDefToModelInfo(modelDef) {
100
99
  });
101
100
  }
102
101
  else if (entry.type === 'query') {
103
- const outputStruct = (0, malloy_query_1.getResultStructDefForQuery)(modelDef, entry);
102
+ const outputStruct = (0, model_1.getResultStructDefForQuery)(modelDef, entry);
104
103
  const annotations = getAnnotationsFromField(entry);
105
104
  const resultMetadataAnnotation = outputStruct.resultMetadata
106
105
  ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
@@ -121,7 +120,7 @@ function modelDefToModelInfo(modelDef) {
121
120
  }
122
121
  }
123
122
  for (const query of modelDef.queryList) {
124
- const outputStruct = (0, malloy_query_1.getResultStructDefForQuery)(modelDef, query);
123
+ const outputStruct = (0, model_1.getResultStructDefForQuery)(modelDef, query);
125
124
  const annotations = getAnnotationsFromField(query);
126
125
  const resultMetadataAnnotation = outputStruct.resultMetadata
127
126
  ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
@@ -188,7 +187,7 @@ function convertFieldInfos(source, fields) {
188
187
  }));
189
188
  const annotations = rawAnnotations.length > 0 ? rawAnnotations : undefined;
190
189
  if ((0, model_1.isTurtle)(field)) {
191
- const outputStruct = (0, malloy_query_1.getResultStructDefForView)(source, field);
190
+ const outputStruct = (0, model_1.getResultStructDefForView)(source, field);
192
191
  const resultMetadataAnnotation = outputStruct.resultMetadata
193
192
  ? getResultStructMetadataAnnotation(outputStruct, outputStruct.resultMetadata)
194
193
  : undefined;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const MALLOY_VERSION = "0.0.304";
1
+ export declare const MALLOY_VERSION = "0.0.306";
package/dist/version.js CHANGED
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MALLOY_VERSION = void 0;
4
4
  // generated with 'generate-version-file' script; do not edit manually
5
- exports.MALLOY_VERSION = '0.0.304';
5
+ exports.MALLOY_VERSION = '0.0.306';
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.304",
3
+ "version": "0.0.306",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
@@ -41,9 +41,9 @@
41
41
  "generate-version-file": "VERSION=$(npm pkg get version --workspaces=false | tr -d \\\")\necho \"// generated with 'generate-version-file' script; do not edit manually\\nexport const MALLOY_VERSION = '$VERSION';\" > src/version.ts"
42
42
  },
43
43
  "dependencies": {
44
- "@malloydata/malloy-filter": "0.0.304",
45
- "@malloydata/malloy-interfaces": "0.0.304",
46
- "@malloydata/malloy-tag": "0.0.304",
44
+ "@malloydata/malloy-filter": "0.0.306",
45
+ "@malloydata/malloy-interfaces": "0.0.306",
46
+ "@malloydata/malloy-tag": "0.0.306",
47
47
  "antlr4ts": "^0.5.0-alpha.4",
48
48
  "assert": "^2.0.0",
49
49
  "jaro-winkler": "^0.2.8",