@malloydata/malloy 0.0.306 → 0.0.307
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/lang/malloy-to-stable-query.js +12 -0
- package/dist/model/constant_expression_compiler.d.ts +22 -0
- package/dist/model/constant_expression_compiler.js +144 -0
- package/dist/model/index.d.ts +1 -0
- package/dist/model/index.js +3 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -4
|
@@ -885,6 +885,18 @@ class MalloyToQuery extends AbstractParseTreeVisitor_1.AbstractParseTreeVisitor
|
|
|
885
885
|
else if (literalCx instanceof parse.ExprNULLContext) {
|
|
886
886
|
return { kind: 'null_literal' };
|
|
887
887
|
}
|
|
888
|
+
else if (literalCx instanceof parse.FilterString_stubContext) {
|
|
889
|
+
const filterContext = literalCx.getChild(0);
|
|
890
|
+
if (filterContext instanceof parse.FilterStringContext) {
|
|
891
|
+
const filterString = this.getFilterString(filterContext);
|
|
892
|
+
if (filterString) {
|
|
893
|
+
return {
|
|
894
|
+
kind: 'filter_expression_literal',
|
|
895
|
+
filter_expression_value: filterString,
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
888
900
|
return null;
|
|
889
901
|
}
|
|
890
902
|
getDrill(drillCx) {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Expr, Parameter } from './malloy_types';
|
|
2
|
+
import type { Dialect } from '../dialect';
|
|
3
|
+
import type { EventStream } from '../runtime_types';
|
|
4
|
+
type ConstantExpressionResult = {
|
|
5
|
+
sql: string;
|
|
6
|
+
error?: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
sql?: undefined;
|
|
9
|
+
error: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Compiles an IR expression containing only constants and parameters to SQL.
|
|
13
|
+
* This is useful for expressions that don't reference source fields.
|
|
14
|
+
*
|
|
15
|
+
* @param expr The expression to compile (should contain only literals, parameters, and operations on them)
|
|
16
|
+
* @param dialect The SQL dialect to use for generation
|
|
17
|
+
* @param parameters Parameters that can be referenced in the expression
|
|
18
|
+
* @param eventStream Optional event stream for debugging/tracking
|
|
19
|
+
* @returns Either {sql: string} on success or {error: string} on failure
|
|
20
|
+
*/
|
|
21
|
+
export declare function constantExprToSQL(expr: Expr, dialect: Dialect, parameters?: Record<string, Parameter>, eventStream?: EventStream): ConstantExpressionResult;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,144 @@
|
|
|
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.constantExprToSQL = constantExprToSQL;
|
|
8
|
+
const expression_compiler_1 = require("./expression_compiler");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
const query_node_1 = require("./query_node");
|
|
11
|
+
const field_instance_1 = require("./field_instance");
|
|
12
|
+
/**
|
|
13
|
+
* Custom error class for constant expression compilation errors.
|
|
14
|
+
* Used to distinguish expected errors from unexpected ones.
|
|
15
|
+
*/
|
|
16
|
+
class ConstantExpressionError extends Error {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'ConstantExpressionError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Minimal FieldInstanceResultRoot for constant expressions.
|
|
24
|
+
* This serves as both the result set and its own root, providing
|
|
25
|
+
* only what's needed by exprToSQL for constants.
|
|
26
|
+
*/
|
|
27
|
+
class ConstantFieldInstanceResultRoot extends field_instance_1.FieldInstanceResultRoot {
|
|
28
|
+
constructor() {
|
|
29
|
+
// Create a minimal TurtleDef
|
|
30
|
+
const minimalTurtleDef = {
|
|
31
|
+
type: 'turtle',
|
|
32
|
+
name: '__constant__',
|
|
33
|
+
pipeline: [],
|
|
34
|
+
};
|
|
35
|
+
super(minimalTurtleDef);
|
|
36
|
+
this.joins = new Map();
|
|
37
|
+
this.havings = new utils_1.AndChain();
|
|
38
|
+
this.isComplexQuery = false;
|
|
39
|
+
this.queryUsesPartitioning = false;
|
|
40
|
+
this.computeOnlyGroups = [];
|
|
41
|
+
this.elimatedComputeGroups = false;
|
|
42
|
+
// Create minimal SourceDef for outputStruct
|
|
43
|
+
const minimalOutputStruct = {
|
|
44
|
+
type: 'table',
|
|
45
|
+
name: '__constant_output__',
|
|
46
|
+
fields: [],
|
|
47
|
+
tablePath: '__constant__',
|
|
48
|
+
connection: '__constant__',
|
|
49
|
+
dialect: 'standardsql',
|
|
50
|
+
};
|
|
51
|
+
// Create minimal QuerySegment
|
|
52
|
+
const minimalSegment = {
|
|
53
|
+
type: 'project',
|
|
54
|
+
filterList: [],
|
|
55
|
+
queryFields: [],
|
|
56
|
+
outputStruct: minimalOutputStruct,
|
|
57
|
+
isRepeated: false,
|
|
58
|
+
};
|
|
59
|
+
this.firstSegment = minimalSegment;
|
|
60
|
+
}
|
|
61
|
+
root() {
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
getQueryInfo() {
|
|
65
|
+
// Return minimal query info - constants don't need timezone
|
|
66
|
+
return {
|
|
67
|
+
queryTimezone: 'UTC',
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Minimal QueryStruct subclass for constant expression compilation.
|
|
73
|
+
* This provides only what's needed to compile expressions containing
|
|
74
|
+
* literals and parameters, without requiring a full query structure.
|
|
75
|
+
*/
|
|
76
|
+
class ConstantQueryStruct extends query_node_1.QueryStruct {
|
|
77
|
+
constructor(dialect, parameters, eventStream) {
|
|
78
|
+
// Create a minimal StructDef that satisfies the constructor requirements
|
|
79
|
+
const minimalStructDef = {
|
|
80
|
+
type: 'table',
|
|
81
|
+
name: '__constant__',
|
|
82
|
+
fields: [],
|
|
83
|
+
tablePath: '__constant__',
|
|
84
|
+
connection: dialect.name,
|
|
85
|
+
dialect: dialect.name,
|
|
86
|
+
};
|
|
87
|
+
// Create a minimal model interface that only provides eventStream
|
|
88
|
+
const minimalModel = { eventStream };
|
|
89
|
+
// Create minimal prepare result options
|
|
90
|
+
const minimalPrepareOptions = {};
|
|
91
|
+
// Call parent constructor with minimal requirements
|
|
92
|
+
super(minimalStructDef, undefined, // no source arguments initially
|
|
93
|
+
{ model: minimalModel }, minimalPrepareOptions);
|
|
94
|
+
this.dialect = dialect;
|
|
95
|
+
this._constantArguments = parameters;
|
|
96
|
+
if (eventStream) {
|
|
97
|
+
this.model.eventStream = eventStream;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Override arguments() to return our parameters
|
|
102
|
+
*/
|
|
103
|
+
arguments() {
|
|
104
|
+
return this._constantArguments;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* These methods should not be called for constant expressions
|
|
108
|
+
*/
|
|
109
|
+
getFieldByName(path) {
|
|
110
|
+
throw new ConstantExpressionError(`Illegal reference to '${path.join('.')}' in constant expressions`);
|
|
111
|
+
}
|
|
112
|
+
getStructByName(path) {
|
|
113
|
+
throw new ConstantExpressionError(`Illegal reference to '${path.join('.')}' in constant expressions`);
|
|
114
|
+
}
|
|
115
|
+
getSQLIdentifier() {
|
|
116
|
+
throw new ConstantExpressionError('Constant expressions do not need SQL identifiers');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Compiles an IR expression containing only constants and parameters to SQL.
|
|
121
|
+
* This is useful for expressions that don't reference source fields.
|
|
122
|
+
*
|
|
123
|
+
* @param expr The expression to compile (should contain only literals, parameters, and operations on them)
|
|
124
|
+
* @param dialect The SQL dialect to use for generation
|
|
125
|
+
* @param parameters Parameters that can be referenced in the expression
|
|
126
|
+
* @param eventStream Optional event stream for debugging/tracking
|
|
127
|
+
* @returns Either {sql: string} on success or {error: string} on failure
|
|
128
|
+
*/
|
|
129
|
+
function constantExprToSQL(expr, dialect, parameters = {}, eventStream) {
|
|
130
|
+
try {
|
|
131
|
+
const context = new ConstantQueryStruct(dialect, parameters, eventStream);
|
|
132
|
+
const resultSet = new ConstantFieldInstanceResultRoot();
|
|
133
|
+
const sql = (0, expression_compiler_1.exprToSQL)(resultSet, context, expr);
|
|
134
|
+
return { sql };
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (error instanceof ConstantExpressionError) {
|
|
138
|
+
return { error: error.message };
|
|
139
|
+
}
|
|
140
|
+
// Re-throw unexpected errors
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=constant_expression_compiler.js.map
|
package/dist/model/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ import { QueryModelImpl } from './query_model_impl';
|
|
|
5
5
|
export { QueryField, QueryStruct, QueryQuery, QueryModelImpl as QueryModel };
|
|
6
6
|
export { getResultStructDefForQuery, getResultStructDefForView, } from './query_model_impl';
|
|
7
7
|
export { indent, composeSQLExpr } from './utils';
|
|
8
|
+
export { constantExprToSQL } from './constant_expression_compiler';
|
package/dist/model/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
36
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.composeSQLExpr = exports.indent = exports.getResultStructDefForView = exports.getResultStructDefForQuery = exports.QueryModel = exports.QueryQuery = exports.QueryStruct = exports.QueryField = void 0;
|
|
39
|
+
exports.constantExprToSQL = exports.composeSQLExpr = exports.indent = exports.getResultStructDefForView = exports.getResultStructDefForQuery = exports.QueryModel = exports.QueryQuery = exports.QueryStruct = exports.QueryField = void 0;
|
|
40
40
|
__exportStar(require("./malloy_types"), exports);
|
|
41
41
|
const query_node_1 = require("./query_node");
|
|
42
42
|
Object.defineProperty(exports, "QueryField", { enumerable: true, get: function () { return query_node_1.QueryField; } });
|
|
@@ -65,4 +65,6 @@ Object.defineProperty(exports, "getResultStructDefForView", { enumerable: true,
|
|
|
65
65
|
var utils_1 = require("./utils");
|
|
66
66
|
Object.defineProperty(exports, "indent", { enumerable: true, get: function () { return utils_1.indent; } });
|
|
67
67
|
Object.defineProperty(exports, "composeSQLExpr", { enumerable: true, get: function () { return utils_1.composeSQLExpr; } });
|
|
68
|
+
var constant_expression_compiler_1 = require("./constant_expression_compiler");
|
|
69
|
+
Object.defineProperty(exports, "constantExprToSQL", { enumerable: true, get: function () { return constant_expression_compiler_1.constantExprToSQL; } });
|
|
68
70
|
//# sourceMappingURL=index.js.map
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const MALLOY_VERSION = "0.0.
|
|
1
|
+
export declare const MALLOY_VERSION = "0.0.307";
|
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.
|
|
5
|
+
exports.MALLOY_VERSION = '0.0.307';
|
|
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.
|
|
3
|
+
"version": "0.0.307",
|
|
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.
|
|
45
|
-
"@malloydata/malloy-interfaces": "0.0.
|
|
46
|
-
"@malloydata/malloy-tag": "0.0.
|
|
44
|
+
"@malloydata/malloy-filter": "0.0.307",
|
|
45
|
+
"@malloydata/malloy-interfaces": "0.0.307",
|
|
46
|
+
"@malloydata/malloy-tag": "0.0.307",
|
|
47
47
|
"antlr4ts": "^0.5.0-alpha.4",
|
|
48
48
|
"assert": "^2.0.0",
|
|
49
49
|
"jaro-winkler": "^0.2.8",
|