@malloydata/malloy 0.0.133-dev240319171426 → 0.0.133

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.
@@ -27,6 +27,7 @@ const duckdb_1 = require("./duckdb");
27
27
  const postgres_1 = require("./postgres");
28
28
  const snowflake_1 = require("./snowflake");
29
29
  const standardsql_1 = require("./standardsql");
30
+ const trino_1 = require("./trino");
30
31
  const dialectMap = new Map();
31
32
  function getDialect(name) {
32
33
  const d = dialectMap.get(name);
@@ -44,6 +45,7 @@ registerDialect(new postgres_1.PostgresDialect());
44
45
  registerDialect(new standardsql_1.StandardSQLDialect());
45
46
  registerDialect(new duckdb_1.DuckDBDialect());
46
47
  registerDialect(new snowflake_1.SnowflakeDialect());
48
+ registerDialect(new trino_1.TrinoDialect());
47
49
  function paramsEqual(a, b) {
48
50
  return (a.params.length === b.params.length &&
49
51
  a.params.every((param, i) => {
@@ -6,5 +6,6 @@ export { StandardSQLDialect } from './standardsql';
6
6
  export { PostgresDialect } from './postgres';
7
7
  export { DuckDBDialect } from './duckdb';
8
8
  export { SnowflakeDialect } from './snowflake';
9
+ export { TrinoDialect } from './trino';
9
10
  export { getDialect, registerDialect, getDialectFunction } from './dialect_map';
10
11
  export { FUNCTIONS } from './functions';
@@ -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.FUNCTIONS = exports.getDialectFunction = exports.registerDialect = exports.getDialect = exports.SnowflakeDialect = exports.DuckDBDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.qtz = exports.Dialect = exports.sql = exports.literal = exports.params = exports.param = exports.sqlFragment = exports.spread = exports.maxScalar = exports.minAggregate = exports.minScalar = exports.overload = exports.makeParam = exports.anyExprType = exports.arg = void 0;
25
+ exports.FUNCTIONS = exports.getDialectFunction = exports.registerDialect = exports.getDialect = exports.TrinoDialect = exports.SnowflakeDialect = exports.DuckDBDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.qtz = exports.Dialect = exports.sql = exports.literal = exports.params = exports.param = exports.sqlFragment = exports.spread = exports.maxScalar = exports.minAggregate = exports.minScalar = exports.overload = exports.makeParam = exports.anyExprType = exports.arg = void 0;
26
26
  var util_1 = require("./functions/util");
27
27
  Object.defineProperty(exports, "arg", { enumerable: true, get: function () { return util_1.arg; } });
28
28
  Object.defineProperty(exports, "anyExprType", { enumerable: true, get: function () { return util_1.anyExprType; } });
@@ -48,6 +48,8 @@ var duckdb_1 = require("./duckdb");
48
48
  Object.defineProperty(exports, "DuckDBDialect", { enumerable: true, get: function () { return duckdb_1.DuckDBDialect; } });
49
49
  var snowflake_1 = require("./snowflake");
50
50
  Object.defineProperty(exports, "SnowflakeDialect", { enumerable: true, get: function () { return snowflake_1.SnowflakeDialect; } });
51
+ var trino_1 = require("./trino");
52
+ Object.defineProperty(exports, "TrinoDialect", { enumerable: true, get: function () { return trino_1.TrinoDialect; } });
51
53
  var dialect_map_1 = require("./dialect_map");
52
54
  Object.defineProperty(exports, "getDialect", { enumerable: true, get: function () { return dialect_map_1.getDialect; } });
53
55
  Object.defineProperty(exports, "registerDialect", { enumerable: true, get: function () { return dialect_map_1.registerDialect; } });
@@ -0,0 +1 @@
1
+ export { TRINO_FUNCTIONS } from './trino_functions';
@@ -0,0 +1,28 @@
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
+ exports.TRINO_FUNCTIONS = void 0;
26
+ var trino_functions_1 = require("./trino_functions");
27
+ Object.defineProperty(exports, "TRINO_FUNCTIONS", { enumerable: true, get: function () { return trino_functions_1.TRINO_FUNCTIONS; } });
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export declare const TRINO_FUNCTIONS: import("../../functions/function_map").FunctionMap;
@@ -0,0 +1,29 @@
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
+ exports.TRINO_FUNCTIONS = void 0;
26
+ const functions_1 = require("../../functions");
27
+ exports.TRINO_FUNCTIONS = functions_1.FUNCTIONS.clone();
28
+ exports.TRINO_FUNCTIONS.seal();
29
+ //# sourceMappingURL=trino_functions.js.map
@@ -0,0 +1 @@
1
+ export * from './trino';
@@ -0,0 +1,40 @@
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
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ var desc = Object.getOwnPropertyDescriptor(m, k);
27
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
28
+ desc = { enumerable: true, get: function() { return m[k]; } };
29
+ }
30
+ Object.defineProperty(o, k2, desc);
31
+ }) : (function(o, m, k, k2) {
32
+ if (k2 === undefined) k2 = k;
33
+ o[k2] = m[k];
34
+ }));
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ __exportStar(require("./trino"), exports);
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,60 @@
1
+ import { Expr, ExtractUnit, Sampling, TimeValue, TimestampUnit, TypecastFragment, FieldAtomicTypeDef } from '../../model/malloy_types';
2
+ import { DialectFunctionOverloadDef } from '../functions';
3
+ import { Dialect, DialectFieldList, QueryInfo } from '../dialect';
4
+ export declare class TrinoDialect extends Dialect {
5
+ name: string;
6
+ defaultNumberType: string;
7
+ defaultDecimalType: string;
8
+ udfPrefix: string;
9
+ hasFinalStage: boolean;
10
+ divisionIsInteger: boolean;
11
+ supportsSumDistinctFunction: boolean;
12
+ unnestWithNumbers: boolean;
13
+ defaultSampling: {
14
+ enable: boolean;
15
+ };
16
+ supportUnnestArrayAgg: boolean;
17
+ supportsAggDistinct: boolean;
18
+ supportsCTEinCoorelatedSubQueries: boolean;
19
+ dontUnionIndex: boolean;
20
+ supportsQualify: boolean;
21
+ supportsSafeCast: boolean;
22
+ supportsNesting: boolean;
23
+ cantPartitionWindowFunctionsOnExpressions: boolean;
24
+ quoteTablePath(tablePath: string): string;
25
+ sqlGroupSetTable(groupSetCount: number): string;
26
+ sqlAnyValue(groupSet: number, fieldName: string): string;
27
+ sqlAggregateTurtle(groupSet: number, fieldList: DialectFieldList, orderBy: string | undefined, limit: number | undefined): string;
28
+ sqlAnyValueTurtle(groupSet: number, fieldList: DialectFieldList): string;
29
+ sqlAnyValueLastTurtle(name: string, groupSet: number, sqlName: string): string;
30
+ sqlCoaleseMeasuresInline(groupSet: number, fieldList: DialectFieldList): string;
31
+ sqlUnnestAlias(source: string, alias: string, fieldList: DialectFieldList, needDistinctKey: boolean, isArray: boolean, _isInNestedPipeline: boolean): string;
32
+ sqlSumDistinctHashedKey(sqlDistinctKey: string): string;
33
+ sqlGenerateUUID(): string;
34
+ sqlFieldReference(alias: string, fieldName: string, _fieldType: string, _isNested: boolean, _isArray: boolean): string;
35
+ sqlUnnestPipelineHead(isSingleton: boolean, sourceSQLExpression: string): string;
36
+ sqlCreateFunction(id: string, funcText: string): string;
37
+ sqlCreateTableAsSelect(tableName: string, sql: string): string;
38
+ sqlCreateFunctionCombineLastStage(lastStageName: string): string;
39
+ sqlSelectAliasAsStruct(alias: string): string;
40
+ keywords: string[];
41
+ sqlMaybeQuoteIdentifier(identifier: string): string;
42
+ sqlNow(): Expr;
43
+ sqlTrunc(qi: QueryInfo, sqlTime: TimeValue, units: TimestampUnit): Expr;
44
+ sqlExtract(qi: QueryInfo, expr: TimeValue, units: ExtractUnit): Expr;
45
+ sqlAlterTime(op: '+' | '-', expr: TimeValue, n: Expr, timeframe: TimestampUnit): Expr;
46
+ ignoreInProject(fieldName: string): boolean;
47
+ sqlCast(qi: QueryInfo, cast: TypecastFragment): Expr;
48
+ sqlRegexpMatch(expr: Expr, regexp: Expr): Expr;
49
+ sqlLiteralTime(qi: QueryInfo, timeString: string, type: 'date' | 'timestamp', timezone: string | undefined): string;
50
+ sqlMeasureTime(from: TimeValue, to: TimeValue, units: string): Expr;
51
+ sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
52
+ sqlLiteralString(literal: string): string;
53
+ sqlLiteralRegexp(literal: string): string;
54
+ getGlobalFunctionDef(name: string): DialectFunctionOverloadDef[] | undefined;
55
+ malloyTypeToSQLType(malloyType: FieldAtomicTypeDef): string;
56
+ sqlTypeToMalloyType(_sqlType: string): FieldAtomicTypeDef | undefined;
57
+ castToString(expression: string): string;
58
+ concat(...values: string[]): string;
59
+ validateTypeName(sqlType: string): boolean;
60
+ }
@@ -0,0 +1,456 @@
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
+ exports.TrinoDialect = void 0;
26
+ const utils_1 = require("../../model/utils");
27
+ const malloy_types_1 = require("../../model/malloy_types");
28
+ const functions_1 = require("./functions");
29
+ const dialect_1 = require("../dialect");
30
+ // These are the units that "TIMESTAMP_ADD" "TIMESTAMP_DIFF" accept
31
+ function timestampMeasureable(units) {
32
+ return [
33
+ 'microsecond',
34
+ 'millisecond',
35
+ 'second',
36
+ 'minute',
37
+ 'hour',
38
+ 'day',
39
+ ].includes(units);
40
+ }
41
+ function dateMeasureable(units) {
42
+ return ['day', 'week', 'month', 'quarter', 'year'].includes(units);
43
+ }
44
+ const extractMap = {
45
+ 'day_of_week': 'dayofweek',
46
+ 'day_of_year': 'dayofyear',
47
+ };
48
+ /**
49
+ * Return a non UTC timezone, if one was specificed.
50
+ */
51
+ function qtz(qi) {
52
+ const tz = qi.queryTimezone;
53
+ if (tz && tz !== 'UTC') {
54
+ return tz;
55
+ }
56
+ }
57
+ class TrinoDialect extends dialect_1.Dialect {
58
+ constructor() {
59
+ super(...arguments);
60
+ this.name = 'trino';
61
+ this.defaultNumberType = 'FLOAT64';
62
+ this.defaultDecimalType = 'NUMERIC';
63
+ this.udfPrefix = '__udf';
64
+ this.hasFinalStage = false;
65
+ this.divisionIsInteger = false;
66
+ this.supportsSumDistinctFunction = false;
67
+ this.unnestWithNumbers = false;
68
+ this.defaultSampling = { enable: false };
69
+ this.supportUnnestArrayAgg = false;
70
+ this.supportsAggDistinct = false;
71
+ this.supportsCTEinCoorelatedSubQueries = false;
72
+ this.dontUnionIndex = true; // bigquery can't use a sample table more than once in a query.
73
+ this.supportsQualify = true;
74
+ this.supportsSafeCast = true;
75
+ this.supportsNesting = true;
76
+ this.cantPartitionWindowFunctionsOnExpressions = true;
77
+ // TODO(figutierrez): update.
78
+ this.keywords = `
79
+ ALL
80
+ AND
81
+ ANY
82
+ ARRAY
83
+ AS
84
+ ASC
85
+ ASSERT_ROWS_MODIFIED
86
+ AT
87
+ BETWEEN
88
+ BY
89
+ CASE
90
+ CAST
91
+ COLLATE
92
+ CONTAINS
93
+ CREATE
94
+ CROSS
95
+ CUBE
96
+ CURRENT
97
+ DEFAULT
98
+ DEFINE
99
+ DESC
100
+ DISTINCT
101
+ ELSE
102
+ END
103
+ ENUM
104
+ ESCAPE
105
+ EXCEPT
106
+ EXCLUDE
107
+ EXISTS
108
+ EXTRACT
109
+ FALSE
110
+ FETCH
111
+ FOLLOWING
112
+ FOR
113
+ FROM
114
+ FULL
115
+ GROUP
116
+ GROUPING
117
+ GROUPS
118
+ HASH
119
+ HAVING
120
+ IF
121
+ IGNORE
122
+ IN
123
+ INNER
124
+ INTERSECT
125
+ INTERVAL
126
+ INTO
127
+ IS
128
+ JOIN
129
+ LATERAL
130
+ LEFT
131
+ LIKE
132
+ LIMIT
133
+ LOOKUP
134
+ MERGE
135
+ NATURAL
136
+ NEW
137
+ NO
138
+ NOT
139
+ NULL
140
+ NULLS
141
+ OF
142
+ ON
143
+ OR
144
+ ORDER
145
+ OUTER
146
+ OVER
147
+ PARTITION
148
+ PRECEDING
149
+ PROTO
150
+ RANGE
151
+ RECURSIVE
152
+ RESPECT
153
+ RIGHT
154
+ ROLLUP
155
+ ROWS
156
+ SELECT
157
+ SET
158
+ SOME
159
+ STRUCT
160
+ TABLESAMPLE
161
+ THEN
162
+ TO
163
+ TREAT
164
+ TRUE
165
+ UNBOUNDED
166
+ UNION
167
+ UNNEST
168
+ USING
169
+ WHEN
170
+ WHERE
171
+ WINDOW
172
+ WITH
173
+ WITHIN`.split(/\s/);
174
+ }
175
+ quoteTablePath(tablePath) {
176
+ // TODO: look into escaping.
177
+ return `${tablePath.replace(/malloytest/g, 'malloy_demo.faa')}`;
178
+ }
179
+ sqlGroupSetTable(groupSetCount) {
180
+ return `CROSS JOIN (SELECT row_number() OVER() -1 group_set FROM UNNEST(SEQUENCE(0,${groupSetCount})))`;
181
+ }
182
+ sqlAnyValue(groupSet, fieldName) {
183
+ return `ANY_VALUE(CASE WHEN group_set=${groupSet} THEN ${fieldName} END)`;
184
+ }
185
+ // can array agg or any_value a struct...
186
+ sqlAggregateTurtle(groupSet, fieldList, orderBy, limit) {
187
+ let tail = '';
188
+ if (limit !== undefined) {
189
+ tail += ` LIMIT ${limit}`;
190
+ }
191
+ const fields = fieldList
192
+ .map(f => `\n '${f.sqlOutputName}' VALUE ${f.sqlExpression}`)
193
+ .join(', ');
194
+ return `cast(COALESCE(SLICE(COALESCE(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN JSON_PARSE(JSON_OBJECT(${fields})) END \n ${orderBy} -- ${tail}\n), ARRAY[]), 1, ${limit}), ARRAY[]) as json)`;
195
+ }
196
+ sqlAnyValueTurtle(groupSet, fieldList) {
197
+ const fields = fieldList
198
+ .map(f => `\n '${f.sqlOutputName}' VALUE ${f.sqlExpression}`)
199
+ .join(', ');
200
+ return `ANY_VALUE(CASE WHEN group_set=${groupSet} THEN JSON_OBJECT(${fields}))`;
201
+ }
202
+ sqlAnyValueLastTurtle(name, groupSet, sqlName) {
203
+ return `ANY_VALUE(CASE WHEN group_set=${groupSet} THEN ${name} END) as ${sqlName}`;
204
+ }
205
+ sqlCoaleseMeasuresInline(groupSet, fieldList) {
206
+ const fields = fieldList
207
+ .map(f => `${f.sqlExpression} as ${f.sqlOutputName}`)
208
+ .join(', ');
209
+ const nullValues = fieldList
210
+ .map(f => `NULL as ${f.sqlOutputName}`)
211
+ .join(', ');
212
+ return `COALESCE(ANY_VALUE(CASE WHEN group_set=${groupSet} THEN STRUCT(${fields}) END), STRUCT(${nullValues}))`;
213
+ }
214
+ //
215
+ // this code used to be:
216
+ //
217
+ // from += `JOIN UNNEST(GENERATE_ARRAY(0,${this.maxGroupSet},1)) as group_set\n`;
218
+ //
219
+ // BigQuery will allocate more resources if we use a CROSS JOIN so we do that instead.
220
+ //
221
+ sqlUnnestAlias(source, alias, fieldList, needDistinctKey, isArray, _isInNestedPipeline) {
222
+ if (isArray) {
223
+ if (needDistinctKey) {
224
+ return `LEFT JOIN UNNEST(ARRAY(( SELECT AS STRUCT row_number() over() as __row_id, value FROM UNNEST(${source}) value))) as ${alias}`;
225
+ }
226
+ else {
227
+ return `LEFT JOIN UNNEST(ARRAY((SELECT AS STRUCT value FROM unnest(${source}) value))) as ${alias}`;
228
+ }
229
+ }
230
+ else if (needDistinctKey) {
231
+ return `LEFT JOIN UNNEST(ARRAY(( SELECT AS STRUCT row_number() over() as __row_id, * FROM UNNEST(${source})))) as ${alias}`;
232
+ }
233
+ else {
234
+ return `LEFT JOIN UNNEST(${source}) as ${alias}`;
235
+ }
236
+ }
237
+ sqlSumDistinctHashedKey(sqlDistinctKey) {
238
+ sqlDistinctKey = `CAST(${sqlDistinctKey} AS STRING)`;
239
+ const upperPart = `cast(cast(concat('0x', substr(to_hex(md5(${sqlDistinctKey})), 1, 15)) as int64) as numeric) * 4294967296`;
240
+ const lowerPart = `cast(cast(concat('0x', substr(to_hex(md5(${sqlDistinctKey})), 16, 8)) as int64) as numeric)`;
241
+ // See the comment below on `sql_sum_distinct` for why we multiply by this decimal
242
+ const precisionShiftMultiplier = '0.000000001';
243
+ return `(${upperPart} + ${lowerPart}) * ${precisionShiftMultiplier}`;
244
+ }
245
+ sqlGenerateUUID() {
246
+ return 'GENERATE_UUID()';
247
+ }
248
+ sqlFieldReference(alias, fieldName, _fieldType, _isNested, _isArray) {
249
+ return `${alias}.${fieldName}`;
250
+ }
251
+ sqlUnnestPipelineHead(isSingleton, sourceSQLExpression) {
252
+ let p = sourceSQLExpression;
253
+ if (isSingleton) {
254
+ p = `[${p}]`;
255
+ }
256
+ return `UNNEST(${p})`;
257
+ }
258
+ sqlCreateFunction(id, funcText) {
259
+ return `CREATE TEMPORARY FUNCTION ${id}(__param ANY TYPE) AS ((\n${(0, utils_1.indent)(funcText)}));\n`;
260
+ }
261
+ sqlCreateTableAsSelect(tableName, sql) {
262
+ return `
263
+ CREATE TABLE IF NOT EXISTS \`${tableName}\`
264
+ OPTIONS (
265
+ expiration_timestamp=TIMESTAMP_ADD(current_timestamp(), INTERVAL 1 hour)
266
+ )
267
+ AS (
268
+ ${(0, utils_1.indent)(sql)}
269
+ );
270
+ `;
271
+ }
272
+ sqlCreateFunctionCombineLastStage(lastStageName) {
273
+ return `SELECT ARRAY((SELECT AS STRUCT * FROM ${lastStageName}))\n`;
274
+ }
275
+ sqlSelectAliasAsStruct(alias) {
276
+ return `(SELECT AS STRUCT ${alias}.*)`;
277
+ }
278
+ sqlMaybeQuoteIdentifier(identifier) {
279
+ // return this.keywords.indexOf(identifier.toUpperCase()) > 0
280
+ // ? '`' + identifier + '`'
281
+ // : identifier;
282
+ return identifier;
283
+ // TODO: may need to escape;
284
+ //return `"${identifier}"`;
285
+ }
286
+ sqlNow() {
287
+ return (0, malloy_types_1.mkExpr) `CURRENT_TIMESTAMP()`;
288
+ }
289
+ sqlTrunc(qi, sqlTime, units) {
290
+ const tz = qtz(qi);
291
+ const tzAdd = tz ? `, "${tz}"` : '';
292
+ if (sqlTime.valueType === 'date') {
293
+ if (dateMeasureable(units)) {
294
+ return (0, malloy_types_1.mkExpr) `DATE_TRUNC(${sqlTime.value},${units})`;
295
+ }
296
+ return (0, malloy_types_1.mkExpr) `TIMESTAMP(${sqlTime.value}${tzAdd})`;
297
+ }
298
+ return (0, malloy_types_1.mkExpr) `TIMESTAMP_TRUNC(${sqlTime.value},${units}${tzAdd})`;
299
+ }
300
+ sqlExtract(qi, expr, units) {
301
+ const extractTo = extractMap[units] || units;
302
+ const tz = expr.valueType === 'timestamp' && qtz(qi);
303
+ const tzAdd = tz ? ` AT TIME ZONE '${tz}'` : '';
304
+ return (0, malloy_types_1.mkExpr) `EXTRACT(${extractTo} FROM ${expr.value}${tzAdd})`;
305
+ }
306
+ sqlAlterTime(op, expr, n, timeframe) {
307
+ let theTime = expr.value;
308
+ let computeType = expr.valueType;
309
+ if (timeframe !== 'day' && timestampMeasureable(timeframe)) {
310
+ // The units must be done in timestamp, no matter the input type
311
+ computeType = 'timestamp';
312
+ if (expr.valueType !== 'timestamp') {
313
+ theTime = (0, malloy_types_1.mkExpr) `TIMESTAMP(${theTime})`;
314
+ }
315
+ }
316
+ else if (expr.valueType === 'timestamp') {
317
+ theTime = (0, malloy_types_1.mkExpr) `DATETIME(${theTime})`;
318
+ computeType = 'datetime';
319
+ }
320
+ const funcName = computeType.toUpperCase() + (op === '+' ? '_ADD' : '_SUB');
321
+ const newTime = (0, malloy_types_1.mkExpr) `${funcName}(${theTime}, INTERVAL ${n} ${timeframe})`;
322
+ if (computeType === expr.valueType) {
323
+ return newTime;
324
+ }
325
+ return (0, malloy_types_1.mkExpr) `${expr.valueType.toUpperCase()}(${newTime})`;
326
+ }
327
+ ignoreInProject(fieldName) {
328
+ return fieldName === '_PARTITIONTIME';
329
+ }
330
+ sqlCast(qi, cast) {
331
+ const op = `${cast.srcType}::${cast.dstType}`;
332
+ const tz = qtz(qi);
333
+ if (op === 'timestamp::date' && tz) {
334
+ return (0, malloy_types_1.mkExpr) `DATE(${cast.expr},'${tz}')`;
335
+ }
336
+ if (op === 'date::timestamp' && tz) {
337
+ return (0, malloy_types_1.mkExpr) `TIMESTAMP(${cast.expr}, '${tz}')`;
338
+ }
339
+ if (cast.srcType !== cast.dstType) {
340
+ const dstType = typeof cast.dstType === 'string'
341
+ ? this.malloyTypeToSQLType({ type: cast.dstType })
342
+ : cast.dstType.raw;
343
+ const castFunc = cast.safe ? 'SAFE_CAST' : 'CAST';
344
+ return (0, malloy_types_1.mkExpr) `${castFunc}(${cast.expr} AS ${dstType})`;
345
+ }
346
+ return cast.expr;
347
+ }
348
+ sqlRegexpMatch(expr, regexp) {
349
+ return (0, malloy_types_1.mkExpr) `REGEXP_CONTAINS(${expr}, ${regexp})`;
350
+ }
351
+ sqlLiteralTime(qi, timeString, type, timezone) {
352
+ if (type === 'date') {
353
+ return `DATE('${timeString}')`;
354
+ }
355
+ else if (type === 'timestamp') {
356
+ let timestampArgs = `'${timeString}'`;
357
+ const tz = timezone || qtz(qi);
358
+ if (tz && tz !== 'UTC') {
359
+ timestampArgs += `,'${tz}'`;
360
+ }
361
+ return `TIMESTAMP(${timestampArgs})`;
362
+ }
363
+ else {
364
+ throw new Error(`Unsupported Literal time format ${type}`);
365
+ }
366
+ }
367
+ sqlMeasureTime(from, to, units) {
368
+ const measureMap = {
369
+ 'microsecond': { use: 'microsecond', ratio: 1 },
370
+ 'millisecond': { use: 'microsecond', ratio: 1000 },
371
+ 'second': { use: 'millisecond', ratio: 1000 },
372
+ 'minute': { use: 'second', ratio: 60 },
373
+ 'hour': { use: 'minute', ratio: 60 },
374
+ 'day': { use: 'hour', ratio: 24 },
375
+ 'week': { use: 'day', ratio: 7 },
376
+ };
377
+ let lVal = from.value;
378
+ let rVal = to.value;
379
+ if (measureMap[units]) {
380
+ const { use: measureIn, ratio } = measureMap[units];
381
+ if (!timestampMeasureable(measureIn)) {
382
+ throw new Error(`Measure in '${measureIn} not implemented`);
383
+ }
384
+ if (from.valueType !== to.valueType) {
385
+ throw new Error("Can't measure difference between different types");
386
+ }
387
+ if (from.valueType === 'date') {
388
+ lVal = (0, malloy_types_1.mkExpr) `TIMESTAMP(${lVal})`;
389
+ rVal = (0, malloy_types_1.mkExpr) `TIMESTAMP(${rVal})`;
390
+ }
391
+ let measured = (0, malloy_types_1.mkExpr) `TIMESTAMP_DIFF(${rVal},${lVal},${measureIn})`;
392
+ if (ratio !== 1) {
393
+ measured = (0, malloy_types_1.mkExpr) `FLOOR(${measured}/${ratio.toString()}.0)`;
394
+ }
395
+ return measured;
396
+ }
397
+ throw new Error(`Measure '${units} not implemented`);
398
+ }
399
+ sqlSampleTable(tableSQL, sample) {
400
+ if (sample !== undefined) {
401
+ if ((0, malloy_types_1.isSamplingEnable)(sample) && sample.enable) {
402
+ sample = this.defaultSampling;
403
+ }
404
+ if ((0, malloy_types_1.isSamplingRows)(sample)) {
405
+ throw new Error("StandardSQL doesn't support sampling by rows only percent");
406
+ }
407
+ else if ((0, malloy_types_1.isSamplingPercent)(sample)) {
408
+ return `(SELECT * FROM ${tableSQL} TABLESAMPLE SYSTEM (${sample.percent} PERCENT))`;
409
+ }
410
+ }
411
+ return tableSQL;
412
+ }
413
+ sqlLiteralString(literal) {
414
+ const noVirgule = literal.replace(/\\/g, '\\\\');
415
+ return "'" + noVirgule.replace(/'/g, "\\'") + "'";
416
+ }
417
+ sqlLiteralRegexp(literal) {
418
+ const noVirgule = literal.replace(/\\/g, '\\\\');
419
+ return "'" + noVirgule.replace(/'/g, "\\'") + "'";
420
+ }
421
+ getGlobalFunctionDef(name) {
422
+ // TODO: implement
423
+ return functions_1.TRINO_FUNCTIONS.get(name);
424
+ }
425
+ malloyTypeToSQLType(malloyType) {
426
+ if (malloyType.type === 'number') {
427
+ if (malloyType.numberType === 'integer') {
428
+ return 'INT64';
429
+ }
430
+ else {
431
+ return 'FLOAT64';
432
+ }
433
+ }
434
+ return malloyType.type;
435
+ }
436
+ sqlTypeToMalloyType(_sqlType) {
437
+ // TODO(figutierrez): unimplemented.
438
+ return undefined;
439
+ }
440
+ castToString(expression) {
441
+ return `CAST(${expression} as STRING)`;
442
+ }
443
+ concat(...values) {
444
+ return values.join(' || ');
445
+ }
446
+ validateTypeName(sqlType) {
447
+ // Letters: BIGINT
448
+ // Numbers: INT8
449
+ // Spaces,
450
+ // Parentheses, Commas: NUMERIC(5, 2)
451
+ // Angle Brackets: ARRAY<INT64>
452
+ return sqlType.match(/^[A-Za-z\s(),<>0-9]*$/) !== null;
453
+ }
454
+ }
455
+ exports.TrinoDialect = TrinoDialect;
456
+ //# sourceMappingURL=trino.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.133-dev240319171426",
3
+ "version": "0.0.133",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",