@malloydata/malloy 0.0.126-dev240221165840 → 0.0.126-dev240305182920

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 (45) hide show
  1. package/README.md +1 -0
  2. package/dist/dialect/dialect.d.ts +1 -0
  3. package/dist/dialect/dialect_map.js +2 -0
  4. package/dist/dialect/functions/concat.js +2 -2
  5. package/dist/dialect/functions/length.js +1 -0
  6. package/dist/dialect/index.d.ts +1 -0
  7. package/dist/dialect/index.js +3 -1
  8. package/dist/dialect/snowflake/functions/chr.d.ts +2 -0
  9. package/dist/dialect/snowflake/functions/chr.js +36 -0
  10. package/dist/dialect/snowflake/functions/div.d.ts +2 -0
  11. package/dist/dialect/snowflake/functions/div.js +37 -0
  12. package/dist/dialect/snowflake/functions/index.d.ts +1 -0
  13. package/dist/dialect/snowflake/functions/index.js +28 -0
  14. package/dist/dialect/snowflake/functions/is_inf.d.ts +2 -0
  15. package/dist/dialect/snowflake/functions/is_inf.js +34 -0
  16. package/dist/dialect/snowflake/functions/is_nan.d.ts +2 -0
  17. package/dist/dialect/snowflake/functions/is_nan.js +33 -0
  18. package/dist/dialect/snowflake/functions/length.d.ts +3 -0
  19. package/dist/dialect/snowflake/functions/length.js +39 -0
  20. package/dist/dialect/snowflake/functions/log.d.ts +2 -0
  21. package/dist/dialect/snowflake/functions/log.js +37 -0
  22. package/dist/dialect/snowflake/functions/rand.d.ts +2 -0
  23. package/dist/dialect/snowflake/functions/rand.js +33 -0
  24. package/dist/dialect/snowflake/functions/regexp_extract.d.ts +2 -0
  25. package/dist/dialect/snowflake/functions/regexp_extract.js +35 -0
  26. package/dist/dialect/snowflake/functions/snowflake_functions.d.ts +1 -0
  27. package/dist/dialect/snowflake/functions/snowflake_functions.js +56 -0
  28. package/dist/dialect/snowflake/functions/starts_ends_with.d.ts +3 -0
  29. package/dist/dialect/snowflake/functions/starts_ends_with.js +43 -0
  30. package/dist/dialect/snowflake/functions/string_agg.d.ts +3 -0
  31. package/dist/dialect/snowflake/functions/string_agg.js +47 -0
  32. package/dist/dialect/snowflake/functions/strpos.d.ts +2 -0
  33. package/dist/dialect/snowflake/functions/strpos.js +35 -0
  34. package/dist/dialect/snowflake/functions/trunc.d.ts +2 -0
  35. package/dist/dialect/snowflake/functions/trunc.js +39 -0
  36. package/dist/dialect/snowflake/index.d.ts +1 -0
  37. package/dist/dialect/snowflake/index.js +40 -0
  38. package/dist/dialect/snowflake/snowflake.d.ts +62 -0
  39. package/dist/dialect/snowflake/snowflake.js +408 -0
  40. package/dist/index.d.ts +1 -1
  41. package/dist/index.js +3 -2
  42. package/dist/malloy.d.ts +2 -0
  43. package/dist/malloy.js +7 -0
  44. package/dist/model/malloy_query.js +16 -11
  45. package/package.json +1 -1
@@ -0,0 +1,35 @@
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.fnStrpos = void 0;
26
+ const util_1 = require("../../functions/util");
27
+ function fnStrpos() {
28
+ const testString = (0, util_1.makeParam)('test_string', (0, util_1.anyExprType)('string'));
29
+ const searchString = (0, util_1.makeParam)('search_string', (0, util_1.anyExprType)('string'));
30
+ return [
31
+ (0, util_1.overload)((0, util_1.minScalar)('number'), [testString.param, searchString.param], (0, util_1.sql) `POSITION(${searchString.arg}, ${testString.arg})`),
32
+ ];
33
+ }
34
+ exports.fnStrpos = fnStrpos;
35
+ //# sourceMappingURL=strpos.js.map
@@ -0,0 +1,2 @@
1
+ import { DialectFunctionOverloadDef } from '../../functions/util';
2
+ export declare function fnTrunc(): DialectFunctionOverloadDef[];
@@ -0,0 +1,39 @@
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.fnTrunc = void 0;
26
+ const util_1 = require("../../functions/util");
27
+ function fnTrunc() {
28
+ const value = (0, util_1.makeParam)('value', (0, util_1.anyExprType)('number'));
29
+ const precision = (0, util_1.makeParam)('precision', (0, util_1.anyExprType)('number'));
30
+ // trunc function doesn't exist in Snowflake, so we emulate it.
31
+ // For both overloads, we switch between CEIL and FLOOR based on the sign of the arugment
32
+ // For the overload with precision, we multiply by a power of 10 before rounding, then divide.
33
+ return [
34
+ (0, util_1.overload)((0, util_1.minScalar)('number'), [value.param], (0, util_1.sql) `CASE WHEN ${value.arg} < 0 THEN CEIL(${value.arg}) ELSE FLOOR(${value.arg}) END`),
35
+ (0, util_1.overload)((0, util_1.minScalar)('number'), [value.param, precision.param], (0, util_1.sql) `CASE WHEN ${value.arg} < 0 THEN CEIL(${value.arg} * POW(10, ${precision.arg})) / POW(10, ${precision.arg}) ELSE FLOOR(${value.arg} * POW(10, ${precision.arg})) / POW(10, ${precision.arg}) END`),
36
+ ];
37
+ }
38
+ exports.fnTrunc = fnTrunc;
39
+ //# sourceMappingURL=trunc.js.map
@@ -0,0 +1 @@
1
+ export * from './snowflake';
@@ -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("./snowflake"), exports);
40
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,62 @@
1
+ import { DateUnit, Expr, ExtractUnit, Sampling, TimeFieldType, TimeValue, TimestampUnit, TypecastFragment, FieldAtomicTypeDef } from '../../model/malloy_types';
2
+ import { DialectFunctionOverloadDef } from '../functions';
3
+ import { Dialect, DialectFieldList, QueryInfo } from '../dialect';
4
+ export declare class SnowflakeDialect extends Dialect {
5
+ name: string;
6
+ defaultNumberType: string;
7
+ defaultDecimalType: string;
8
+ udfPrefix: string;
9
+ hasFinalStage: boolean;
10
+ divisionIsInteger: boolean;
11
+ supportsSumDistinctFunction: boolean;
12
+ defaultSampling: {
13
+ rows: number;
14
+ };
15
+ globalFunctions: import("../functions/function_map").FunctionMap;
16
+ unnestWithNumbers: boolean;
17
+ supportUnnestArrayAgg: boolean;
18
+ supportsAggDistinct: boolean;
19
+ supportsCTEinCoorelatedSubQueries: boolean;
20
+ supportsSafeCast: boolean;
21
+ dontUnionIndex: boolean;
22
+ supportsQualify: boolean;
23
+ supportsNesting: boolean;
24
+ quoteTablePath(tablePath: string): string;
25
+ sqlGroupSetTable(groupSetCount: number): string;
26
+ sqlAnyValue(groupSet: number, fieldName: string): string;
27
+ mapFields(fieldList: DialectFieldList): string;
28
+ mapFieldsForObjectConstruct(fieldList: DialectFieldList): string;
29
+ sqlAggregateTurtle(groupSet: number, fieldList: DialectFieldList, orderBy: string | undefined, limit: number | undefined): string;
30
+ sqlAnyValueTurtle(groupSet: number, fieldList: DialectFieldList): string;
31
+ sqlAnyValueLastTurtle(name: string, groupSet: number, sqlName: string): string;
32
+ sqlCoaleseMeasuresInline(groupSet: number, fieldList: DialectFieldList): string;
33
+ sqlUnnestAlias(source: string, alias: string, _fieldList: DialectFieldList, _needDistinctKey: boolean, isArray: boolean, _isInNestedPipeline: boolean): string;
34
+ sqlSumDistinctHashedKey(sqlDistinctKey: string): string;
35
+ sqlGenerateUUID(): string;
36
+ sqlFieldReference(alias: string, fieldName: string, fieldType: string, isNested: boolean, _isArray: boolean): string;
37
+ sqlUnnestPipelineHead(isSingleton: boolean, sourceSQLExpression: string): string;
38
+ sqlCreateFunction(_id: string, _funcText: string): string;
39
+ sqlCreateFunctionCombineLastStage(_lastStageName: string): string;
40
+ sqlSelectAliasAsStruct(alias: string): string;
41
+ sqlMaybeQuoteIdentifier(identifier: string): string;
42
+ sqlCreateTableAsSelect(tableName: string, sql: string): string;
43
+ sqlTrunc(qi: QueryInfo, sqlTime: TimeValue, units: TimestampUnit): Expr;
44
+ sqlExtract(qi: QueryInfo, from: TimeValue, units: ExtractUnit): Expr;
45
+ sqlAlterTime(op: '+' | '-', expr: TimeValue, n: Expr, timeframe: DateUnit): Expr;
46
+ private atTz;
47
+ sqlNow(): Expr;
48
+ sqlCast(qi: QueryInfo, cast: TypecastFragment): Expr;
49
+ sqlLiteralTime(qi: QueryInfo, timeString: string, type: TimeFieldType, timezone: string | undefined): string;
50
+ sqlMeasureTime(from: TimeValue, to: TimeValue, units: string): Expr;
51
+ sqlRegexpMatch(expr: Expr, regexp: Expr): Expr;
52
+ sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
53
+ sqlOrderBy(orderTerms: string[]): string;
54
+ sqlLiteralString(literal: string): string;
55
+ sqlLiteralRegexp(literal: string): string;
56
+ getGlobalFunctionDef(name: string): DialectFunctionOverloadDef[] | undefined;
57
+ malloyTypeToSQLType(malloyType: FieldAtomicTypeDef): string;
58
+ sqlTypeToMalloyType(sqlType: string): FieldAtomicTypeDef | undefined;
59
+ castToString(expression: string): string;
60
+ concat(...values: string[]): string;
61
+ validateTypeName(sqlType: string): boolean;
62
+ }
@@ -0,0 +1,408 @@
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.SnowflakeDialect = 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
+ const extractionMap = {
31
+ 'day_of_week': 'dayofweek',
32
+ 'day_of_year': 'dayofyear',
33
+ };
34
+ const snowflakeToMalloyTypes = {
35
+ // string
36
+ 'varchar': { type: 'string' },
37
+ 'text': { type: 'string' },
38
+ 'string': { type: 'string' },
39
+ 'char': { type: 'string' },
40
+ 'character': { type: 'string' },
41
+ 'nvarchar': { type: 'string' },
42
+ 'nvarchar2': { type: 'string' },
43
+ 'char varying': { type: 'string' },
44
+ 'nchar varying': { type: 'string' },
45
+ // numbers
46
+ 'number': { type: 'number', numberType: 'integer' },
47
+ 'numeric': { type: 'number', numberType: 'integer' },
48
+ 'decimal': { type: 'number', numberType: 'integer' },
49
+ 'dec': { type: 'number', numberType: 'integer' },
50
+ 'integer': { type: 'number', numberType: 'integer' },
51
+ 'int': { type: 'number', numberType: 'integer' },
52
+ 'bigint': { type: 'number', numberType: 'integer' },
53
+ 'smallint': { type: 'number', numberType: 'integer' },
54
+ 'tinyint': { type: 'number', numberType: 'integer' },
55
+ 'byteint': { type: 'number', numberType: 'integer' },
56
+ 'float': { type: 'number', numberType: 'float' },
57
+ 'float4': { type: 'number', numberType: 'float' },
58
+ 'float8': { type: 'number', numberType: 'float' },
59
+ 'double': { type: 'number', numberType: 'float' },
60
+ 'double precision': { type: 'number', numberType: 'float' },
61
+ 'real': { type: 'number', numberType: 'float' },
62
+ 'boolean': { type: 'boolean' },
63
+ // time and date
64
+ 'date': { type: 'date' },
65
+ 'timestamp': { type: 'timestamp' },
66
+ 'timestampntz': { type: 'timestamp' },
67
+ 'timestamp_ntz': { type: 'timestamp' },
68
+ 'timestamp without time zone': { type: 'timestamp' },
69
+ 'timestamptz': { type: 'timestamp' },
70
+ 'timestamp_tz': { type: 'timestamp' },
71
+ 'timestamp with time zone': { type: 'timestamp' },
72
+ /* timestamp_ltz is not supported in malloy snowflake dialect */
73
+ };
74
+ class SnowflakeDialect extends dialect_1.Dialect {
75
+ constructor() {
76
+ super(...arguments);
77
+ this.name = 'snowflake';
78
+ this.defaultNumberType = 'NUMBER';
79
+ this.defaultDecimalType = 'NUMBER';
80
+ this.udfPrefix = '__udf';
81
+ this.hasFinalStage = false;
82
+ this.divisionIsInteger = false;
83
+ this.supportsSumDistinctFunction = false;
84
+ this.defaultSampling = { rows: 50000 };
85
+ this.globalFunctions = functions_1.SNOWFLAKE_FUNCTIONS;
86
+ // NOTE: safely setting all these to false for now
87
+ // more many be implemented in future
88
+ this.unnestWithNumbers = false;
89
+ this.supportUnnestArrayAgg = false;
90
+ this.supportsAggDistinct = false;
91
+ this.supportsCTEinCoorelatedSubQueries = false;
92
+ this.supportsSafeCast = false;
93
+ this.dontUnionIndex = false;
94
+ this.supportsQualify = false;
95
+ this.supportsNesting = true;
96
+ }
97
+ // don't mess with the table pathing.
98
+ quoteTablePath(tablePath) {
99
+ return tablePath;
100
+ }
101
+ sqlGroupSetTable(groupSetCount) {
102
+ return `CROSS JOIN (SELECT seq4() as group_set FROM TABLE(GENERATOR(ROWCOUNT => ${groupSetCount + 1}))) as group_set`;
103
+ }
104
+ /*
105
+ // this just returns null in snowflake
106
+ select
107
+ any_value (t is not null) as av
108
+ from
109
+ (
110
+ select
111
+ case
112
+ when group_set = 1 then 'one'
113
+ end as t
114
+ from
115
+ (
116
+ SELECT
117
+ seq4 () as group_set
118
+ FROM
119
+ TABLE (GENERATOR (ROWCOUNT => 3))
120
+ ) as tbl
121
+ );
122
+ */
123
+ sqlAnyValue(groupSet, fieldName) {
124
+ return `(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN ${fieldName} END) WITHIN GROUP (ORDER BY ${fieldName} ASC NULLS LAST))[0]`;
125
+ }
126
+ mapFields(fieldList) {
127
+ return fieldList
128
+ .map(f => `\n ${f.sqlExpression} as ${f.sqlOutputName}`)
129
+ .join(', ');
130
+ }
131
+ mapFieldsForObjectConstruct(fieldList) {
132
+ return fieldList
133
+ .map(f => `'${f.rawName}', (${f.sqlExpression})`)
134
+ .join(', ');
135
+ }
136
+ sqlAggregateTurtle(groupSet, fieldList, orderBy, limit) {
137
+ const fields = this.mapFieldsForObjectConstruct(fieldList);
138
+ const orderByClause = orderBy ? ` WITHIN GROUP (${orderBy})` : '';
139
+ const aggClause = `ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN OBJECT_CONSTRUCT(${fields}) END)${orderByClause}`;
140
+ if (limit === undefined) {
141
+ return `COALESCE(${aggClause}, [])`;
142
+ }
143
+ return `COALESCE(ARRAY_SLICE(${aggClause}, 0, ${limit}), [])`;
144
+ }
145
+ sqlAnyValueTurtle(groupSet, fieldList) {
146
+ const fields = this.mapFieldsForObjectConstruct(fieldList);
147
+ return `(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN OBJECT_CONSTRUCT(${fields}) END) WITHIN GROUP (ORDER BY 1 ASC NULLS LAST))[0]`;
148
+ }
149
+ sqlAnyValueLastTurtle(name, groupSet, sqlName) {
150
+ return `(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN ${name} END) WITHIN GROUP (ORDER BY ${name} ASC NULLS LAST))[0] AS ${sqlName}`;
151
+ }
152
+ sqlCoaleseMeasuresInline(groupSet, fieldList) {
153
+ const fields = this.mapFieldsForObjectConstruct(fieldList);
154
+ const nullValues = fieldList
155
+ .map(f => `'${f.sqlOutputName}', NULL`)
156
+ .join(', ');
157
+ return `COALESCE(ARRAY_AGG(CASE WHEN group_set=${groupSet} THEN OBJECT_CONSTRUCT(${fields}) END)[0], OBJECT_CONSTRUCT_KEEP_NULL(${nullValues}))`;
158
+ }
159
+ sqlUnnestAlias(source, alias, _fieldList, _needDistinctKey, isArray, _isInNestedPipeline) {
160
+ if (isArray) {
161
+ // if (needDistinctKey) {
162
+ // // return `LEFT JOIN UNNEST(ARRAY(( SELECT AS STRUCT row_number() over() as __row_id, value FROM UNNEST(${source}) value))) as ${alias}`;
163
+ // } else {
164
+ // return `LEFT JOIN UNNEST(ARRAY((SELECT AS STRUCT value FROM unnest(${source}) value))) as ${alias}`;
165
+ // }
166
+ throw new Error('not implemented yet');
167
+ }
168
+ else {
169
+ // have to have a non empty row or it treats it like an inner join :barf-emoji:
170
+ return `LEFT JOIN LATERAL FLATTEN(INPUT => ifnull(${source},[1])) AS ${alias}`;
171
+ // return `LEFT JOIN UNNEST(ARRAY(( SELECT AS STRUCT row_number() over() as __row_id, * FROM UNNEST(${source})))) as ${alias}`;
172
+ // } else {
173
+ // return `LEFT JOIN UNNEST(${source}) as ${alias}`;
174
+ // }
175
+ }
176
+ }
177
+ /*
178
+ // For comparison against the equivalent function implemented in standardsql dialect
179
+ select
180
+ (
181
+ to_number (
182
+ substr (md5_hex ('hello'), 1, 15),
183
+ repeat ('X', 15)
184
+ ) * 4294967296 + to_number (
185
+ substr (md5_hex ('hello'), 16, 8),
186
+ repeat ('X', 8)
187
+ )
188
+ ) * 0.000000001 as hash;
189
+ +-------------------------------+
190
+ | HASH |
191
+ |-------------------------------|
192
+ | 1803811819465386377.040304601 |
193
+ +-------------------------------+
194
+ */
195
+ sqlSumDistinctHashedKey(sqlDistinctKey) {
196
+ sqlDistinctKey = `${sqlDistinctKey}::STRING`;
197
+ const upperPart = `to_number(substr(md5_hex(${sqlDistinctKey}), 1, 15), repeat('X', 15)) * 4294967296`;
198
+ const lowerPart = `to_number(substr(md5_hex(${sqlDistinctKey}), 16, 8), repeat('X', 8))`;
199
+ const precisionShiftMultiplier = '0.000000001';
200
+ return `(${upperPart} + ${lowerPart}) * ${precisionShiftMultiplier}`;
201
+ }
202
+ sqlGenerateUUID() {
203
+ return 'UUID_STRING()';
204
+ }
205
+ sqlFieldReference(alias, fieldName, fieldType, isNested, _isArray) {
206
+ if (fieldName === '__row_id') {
207
+ return `${alias}.INDEX::varchar`;
208
+ }
209
+ else if (!isNested) {
210
+ return `${alias}."${fieldName}"`;
211
+ }
212
+ else {
213
+ let snowflakeType = fieldType;
214
+ if (fieldType === 'string') {
215
+ snowflakeType = 'varchar';
216
+ }
217
+ return `${alias}.value:"${fieldName}"::${snowflakeType}`;
218
+ }
219
+ }
220
+ sqlUnnestPipelineHead(isSingleton, sourceSQLExpression) {
221
+ let p = sourceSQLExpression;
222
+ if (isSingleton) {
223
+ p = `[${p}]`;
224
+ }
225
+ return `TABLE(FLATTEN(input =>${p}))`;
226
+ }
227
+ sqlCreateFunction(_id, _funcText) {
228
+ throw new Error('not implemented yet');
229
+ }
230
+ sqlCreateFunctionCombineLastStage(_lastStageName) {
231
+ throw new Error('not implemented yet');
232
+ }
233
+ sqlSelectAliasAsStruct(alias) {
234
+ return `OBJECT_CONSTRUCT(${alias}.*)`;
235
+ }
236
+ sqlMaybeQuoteIdentifier(identifier) {
237
+ return `"${identifier}"`;
238
+ }
239
+ sqlCreateTableAsSelect(tableName, sql) {
240
+ return `
241
+ CREATE TEMP TABLE IF NOT EXISTS \`${tableName}\`
242
+ AS (
243
+ ${(0, utils_1.indent)(sql)}
244
+ );
245
+ `;
246
+ }
247
+ sqlTrunc(qi, sqlTime, units) {
248
+ const tz = (0, dialect_1.qtz)(qi);
249
+ let truncThis = sqlTime.value;
250
+ if (tz && sqlTime.valueType === 'timestamp') {
251
+ truncThis = (0, malloy_types_1.mkExpr) `CONVERT_TIMEZONE('${tz}', ${truncThis})`;
252
+ }
253
+ return (0, malloy_types_1.mkExpr) `DATE_TRUNC('${units}', ${truncThis})`;
254
+ }
255
+ sqlExtract(qi, from, units) {
256
+ const extractUnits = extractionMap[units] || units;
257
+ let extractFrom = from.value;
258
+ const tz = (0, dialect_1.qtz)(qi);
259
+ if (tz && from.valueType === 'timestamp') {
260
+ extractFrom = (0, malloy_types_1.mkExpr) `CONVERT_TIMEZONE('${tz}', ${extractFrom})`;
261
+ }
262
+ const extracted = (0, malloy_types_1.mkExpr) `EXTRACT(${extractUnits} FROM ${extractFrom})`;
263
+ return extracted;
264
+ }
265
+ sqlAlterTime(op, expr, n, timeframe) {
266
+ const interval = (0, malloy_types_1.mkExpr) `INTERVAL '${n} ${timeframe}'`;
267
+ return (0, malloy_types_1.mkExpr) `((${expr.value})${op}${interval})`;
268
+ }
269
+ atTz(expr, tz) {
270
+ if (tz !== undefined) {
271
+ return (0, malloy_types_1.mkExpr) `(
272
+ TO_CHAR(${expr}::TIMESTAMP_NTZ, 'YYYY-MM-DD HH24:MI:SS.FF9') ||
273
+ TO_CHAR(CONVERT_TIMEZONE('${tz}', '1970-01-01 00:00:00'), 'TZHTZM')
274
+ )::TIMESTAMP_TZ`;
275
+ }
276
+ return (0, malloy_types_1.mkExpr) `${expr}::TIMESTAMP_NTZ`;
277
+ }
278
+ sqlNow() {
279
+ return (0, malloy_types_1.mkExpr) `CURRENT_TIMESTAMP()`;
280
+ }
281
+ sqlCast(qi, cast) {
282
+ if (cast.srcType === cast.dstType) {
283
+ return cast.expr;
284
+ }
285
+ if (cast.safe) {
286
+ // safe cast is only supported for a few combinations of src -> dst types
287
+ // so we will not support it in the general case
288
+ throw new Error("Snowflake dialect doesn't support safe cast for a few types");
289
+ }
290
+ const tz = (0, dialect_1.qtz)(qi);
291
+ // casting timestamps and dates
292
+ if (cast.dstType === 'date' && cast.srcType === 'timestamp') {
293
+ let castExpr = cast.expr;
294
+ if (tz) {
295
+ castExpr = (0, malloy_types_1.mkExpr) `CONVERT_TIMEZONE('${tz}', ${castExpr})`;
296
+ }
297
+ return (0, malloy_types_1.mkExpr) `TO_DATE(${castExpr})`;
298
+ }
299
+ else if (cast.dstType === 'timestamp' && cast.srcType === 'date') {
300
+ const retExpr = (0, malloy_types_1.mkExpr) `TO_TIMESTAMP(${cast.expr})`;
301
+ return this.atTz(retExpr, tz);
302
+ }
303
+ const dstType = typeof cast.dstType === 'string'
304
+ ? this.malloyTypeToSQLType({ type: cast.dstType })
305
+ : cast.dstType.raw;
306
+ return (0, malloy_types_1.mkExpr) `CAST(${cast.expr} AS ${dstType})`;
307
+ }
308
+ sqlLiteralTime(qi, timeString, type, timezone) {
309
+ const tz = (0, dialect_1.qtz)(qi);
310
+ // just making it explicit that timestring does not have timezone info
311
+ let ret = `'${timeString}'::TIMESTAMP_NTZ`;
312
+ // now do the hack to add timezone to a timestamp ntz
313
+ const targetTimeZone = timezone !== null && timezone !== void 0 ? timezone : tz;
314
+ if (targetTimeZone) {
315
+ const targetTimeZoneSuffix = `TO_CHAR(CONVERT_TIMEZONE('${targetTimeZone}', '1970-01-01 00:00:00'), 'TZHTZM')`;
316
+ const retTimeString = `TO_CHAR(${ret}, 'YYYY-MM-DD HH24:MI:SS.FF9')`;
317
+ ret = `${retTimeString} || ${targetTimeZoneSuffix}`;
318
+ ret = `(${ret})::TIMESTAMP_TZ`;
319
+ }
320
+ switch (type) {
321
+ case 'date':
322
+ return `TO_DATE(${ret})`;
323
+ case 'timestamp': {
324
+ return ret;
325
+ }
326
+ }
327
+ }
328
+ sqlMeasureTime(from, to, units) {
329
+ let extractUnits = 'nanoseconds';
330
+ if (from.valueType === 'date' || to.valueType === 'date') {
331
+ extractUnits = 'seconds';
332
+ }
333
+ return (0, malloy_types_1.mkExpr) `TIMESTAMPDIFF(
334
+ '${units}',
335
+ '1970-01-01 00:00:00'::TIMESTAMP_NTZ,
336
+ TIMESTAMPADD(
337
+ '${extractUnits}',
338
+ EXTRACT('epoch_${extractUnits}', ${to.value}) - EXTRACT('epoch_${extractUnits}', ${from.value}),
339
+ '1970-01-01 00:00:00'::TIMESTAMP_NTZ
340
+ )
341
+ )`;
342
+ }
343
+ sqlRegexpMatch(expr, regexp) {
344
+ // regexp_match captures any partial match
345
+ return (0, malloy_types_1.mkExpr) `(REGEXP_INSTR(${expr}, ${regexp}) != 0)`;
346
+ }
347
+ sqlSampleTable(tableSQL, sample) {
348
+ if (sample !== undefined) {
349
+ if ((0, malloy_types_1.isSamplingEnable)(sample) && sample.enable) {
350
+ sample = this.defaultSampling;
351
+ }
352
+ if ((0, malloy_types_1.isSamplingRows)(sample)) {
353
+ return `(SELECT * FROM ${tableSQL} TABLESAMPLE (${sample.rows} ROWS))`;
354
+ }
355
+ else if ((0, malloy_types_1.isSamplingPercent)(sample)) {
356
+ return `(SELECT * FROM ${tableSQL} TABLESAMPLE (${sample.percent}))`;
357
+ }
358
+ }
359
+ return tableSQL;
360
+ }
361
+ sqlOrderBy(orderTerms) {
362
+ return `ORDER BY ${orderTerms.map(t => `${t} NULLS LAST`).join(',')}`;
363
+ }
364
+ sqlLiteralString(literal) {
365
+ const noVirgule = literal.replace(/\\/g, '\\\\');
366
+ return "'" + noVirgule.replace(/'/g, "\\'") + "'";
367
+ }
368
+ sqlLiteralRegexp(literal) {
369
+ const noVirgule = literal.replace(/\\/g, '\\\\');
370
+ return "'" + noVirgule.replace(/'/g, "\\'") + "'";
371
+ }
372
+ getGlobalFunctionDef(name) {
373
+ return functions_1.SNOWFLAKE_FUNCTIONS.get(name);
374
+ }
375
+ malloyTypeToSQLType(malloyType) {
376
+ if (malloyType.type === 'number') {
377
+ if (malloyType.numberType === 'integer') {
378
+ return 'integer';
379
+ }
380
+ else {
381
+ return 'double';
382
+ }
383
+ }
384
+ return malloyType.type;
385
+ }
386
+ sqlTypeToMalloyType(sqlType) {
387
+ var _a, _b;
388
+ // Remove trailing params
389
+ const baseSqlType = (_b = (_a = sqlType.match(/^([\w\s]+)/)) === null || _a === void 0 ? void 0 : _a.at(0)) !== null && _b !== void 0 ? _b : sqlType;
390
+ return snowflakeToMalloyTypes[baseSqlType.trim().toLowerCase()];
391
+ }
392
+ castToString(expression) {
393
+ return `TO_VARCHAR(${expression})`;
394
+ }
395
+ concat(...values) {
396
+ return values.join(' || ');
397
+ }
398
+ validateTypeName(sqlType) {
399
+ // Letters: BIGINT
400
+ // Numbers: INT8
401
+ // Spaces: TIMESTAMP WITH TIME ZONE
402
+ // Parentheses, Commas: NUMERIC(5, 2)
403
+ // Square Brackets: INT64[]
404
+ return sqlType.match(/^[A-Za-z\s(),[\]0-9]*$/) !== null;
405
+ }
406
+ }
407
+ exports.SnowflakeDialect = SnowflakeDialect;
408
+ //# sourceMappingURL=snowflake.js.map
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { DuckDBDialect, StandardSQLDialect, PostgresDialect, registerDialect, arg, qtz, overload, minScalar, anyExprType, minAggregate, maxScalar, sql, makeParam, sqlFragment, param, params, literal, spread, Dialect, FUNCTIONS, } from './dialect';
1
+ export { DuckDBDialect, StandardSQLDialect, PostgresDialect, SnowflakeDialect, registerDialect, arg, qtz, overload, minScalar, anyExprType, minAggregate, maxScalar, sql, makeParam, sqlFragment, param, params, literal, spread, Dialect, FUNCTIONS, } from './dialect';
2
2
  export type { DialectFieldList, DialectFunctionOverloadDef, QueryInfo, } from './dialect';
3
3
  export type { QueryDataRow, Fragment, StructDef, StructRelationship, NamedStructDefs, MalloyQueryData, AtomicFieldType as AtomicFieldTypeInner, DateUnit, ExtractUnit, TimestampUnit, TimeFieldType, QueryData, QueryValue, FieldTypeDef, Expr, DialectFragment, TimeValue, FilterExpression, SQLBlock, FieldAtomicDef, FieldDef, PipeSegment, QueryFieldDef, IndexFieldDef, TurtleDef, SearchValueMapResult, SearchIndexResult, ModelDef, Query, QueryResult, QueryRunStats, NamedQuery, NamedModelObject, ExpressionType, FunctionDef, FunctionOverloadDef, FunctionParameterDef, ExpressionValueType, TypeDesc, FieldValueType, ExpressionTypeDesc, FunctionParamTypeDesc, DocumentLocation, DocumentRange, DocumentPosition, Sampling, TypecastFragment, Annotation, FieldAtomicTypeDef, SQLBlockStructDef, } from './model';
4
4
  export { Segment, isSamplingEnable, isSamplingPercent, isSamplingRows, mkExpr, expressionIsCalculation, indent, } from './model';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Explore = exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.MalloyTranslator = exports.HighlightType = exports.indent = exports.expressionIsCalculation = exports.mkExpr = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.Segment = exports.FUNCTIONS = exports.Dialect = exports.spread = exports.literal = exports.params = exports.param = exports.sqlFragment = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
4
- exports.Tag = exports.toAsyncGenerator = void 0;
3
+ exports.DataWriter = exports.Parse = exports.JSONWriter = exports.CSVWriter = exports.QueryMaterializer = exports.Result = exports.PreparedResult = exports.TimestampTimeframe = exports.DateTimeframe = exports.SourceRelationship = exports.JoinRelationship = exports.MalloyError = exports.FixedConnectionMap = exports.InMemoryURLReader = exports.EmptyURLReader = exports.SingleConnectionRuntime = exports.ConnectionRuntime = exports.AtomicFieldType = exports.Runtime = exports.Malloy = exports.MalloyTranslator = exports.HighlightType = exports.indent = exports.expressionIsCalculation = exports.mkExpr = exports.isSamplingRows = exports.isSamplingPercent = exports.isSamplingEnable = exports.Segment = exports.FUNCTIONS = exports.Dialect = exports.spread = exports.literal = exports.params = exports.param = exports.sqlFragment = exports.makeParam = exports.sql = exports.maxScalar = exports.minAggregate = exports.anyExprType = exports.minScalar = exports.overload = exports.qtz = exports.arg = exports.registerDialect = exports.SnowflakeDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.DuckDBDialect = void 0;
4
+ exports.Tag = exports.toAsyncGenerator = exports.Explore = void 0;
5
5
  /*
6
6
  * Copyright 2023 Google LLC
7
7
  *
@@ -28,6 +28,7 @@ var dialect_1 = require("./dialect");
28
28
  Object.defineProperty(exports, "DuckDBDialect", { enumerable: true, get: function () { return dialect_1.DuckDBDialect; } });
29
29
  Object.defineProperty(exports, "StandardSQLDialect", { enumerable: true, get: function () { return dialect_1.StandardSQLDialect; } });
30
30
  Object.defineProperty(exports, "PostgresDialect", { enumerable: true, get: function () { return dialect_1.PostgresDialect; } });
31
+ Object.defineProperty(exports, "SnowflakeDialect", { enumerable: true, get: function () { return dialect_1.SnowflakeDialect; } });
31
32
  Object.defineProperty(exports, "registerDialect", { enumerable: true, get: function () { return dialect_1.registerDialect; } });
32
33
  Object.defineProperty(exports, "arg", { enumerable: true, get: function () { return dialect_1.arg; } });
33
34
  Object.defineProperty(exports, "qtz", { enumerable: true, get: function () { return dialect_1.qtz; } });
package/dist/malloy.d.ts CHANGED
@@ -850,6 +850,8 @@ export declare class SingleConnectionRuntime<T extends Connection = Connection>
850
850
  constructor(urls: URLReader, connection: T);
851
851
  constructor(connection: T);
852
852
  get supportsNesting(): boolean;
853
+ quote(column: string): string;
854
+ getQuoter(): (arg: TemplateStringsArray) => string;
853
855
  }
854
856
  declare class FluentState<T> {
855
857
  protected runtime: Runtime;
package/dist/malloy.js CHANGED
@@ -1754,6 +1754,13 @@ class SingleConnectionRuntime extends Runtime {
1754
1754
  get supportsNesting() {
1755
1755
  return (0, dialect_1.getDialect)(this.connection.dialectName).supportsNesting;
1756
1756
  }
1757
+ // quote a column name
1758
+ quote(column) {
1759
+ return (0, dialect_1.getDialect)(this.connection.dialectName).sqlMaybeQuoteIdentifier(column);
1760
+ }
1761
+ getQuoter() {
1762
+ return (x) => this.quote(x.toString());
1763
+ }
1757
1764
  }
1758
1765
  exports.SingleConnectionRuntime = SingleConnectionRuntime;
1759
1766
  class FluentState {