@malloydata/malloy 0.0.2 → 0.0.4

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 (67) hide show
  1. package/README.md +12 -11
  2. package/dist/dialect/duckdb.d.ts +1 -1
  3. package/dist/dialect/postgres.d.ts +1 -1
  4. package/dist/index.d.ts +4 -4
  5. package/dist/lang/ast/ast-expr.d.ts +0 -1
  6. package/dist/lang/field-space.d.ts +9 -7
  7. package/dist/lang/lib/Malloy/MalloyLexer.d.ts +108 -106
  8. package/dist/lang/lib/Malloy/MalloyLexer.js +1019 -1006
  9. package/dist/lang/lib/Malloy/MalloyParser.d.ts +581 -590
  10. package/dist/lang/lib/Malloy/MalloyParser.js +1141 -1202
  11. package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +2015 -0
  12. package/dist/lang/lib/Malloy/MalloyParserListener.js +4 -0
  13. package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +1269 -0
  14. package/dist/lang/lib/Malloy/MalloyParserVisitor.js +4 -0
  15. package/dist/lang/parse-to-ast.d.ts +3 -4
  16. package/dist/lang/parse-to-ast.js +1 -1
  17. package/dist/lang/parse-tree-walkers/document-completion-walker.js +2 -0
  18. package/dist/lang/parse-tree-walkers/explore-query-walker.d.ts +2 -2
  19. package/dist/malloy.d.ts +1 -1
  20. package/dist/md5.d.ts +1 -0
  21. package/dist/md5.js +30 -0
  22. package/dist/model/malloy_types.d.ts +3 -1
  23. package/package.json +9 -14
  24. package/dist/connection_utils.js +0 -56
  25. package/dist/dialect/dialect-map.d.ts +0 -3
  26. package/dist/dialect/dialect-map.js +0 -33
  27. package/dist/dialect/dialect.js +0 -77
  28. package/dist/dialect/dialect_map.js +0 -35
  29. package/dist/dialect/duckdb.js +0 -349
  30. package/dist/dialect/index.js +0 -27
  31. package/dist/dialect/postgres.js +0 -308
  32. package/dist/dialect/standardsql.js +0 -361
  33. package/dist/index.js +0 -47
  34. package/dist/lang/ast/apply-expr.js +0 -231
  35. package/dist/lang/ast/ast-expr.js +0 -1051
  36. package/dist/lang/ast/ast-main.js +0 -2086
  37. package/dist/lang/ast/ast-time-expr.js +0 -549
  38. package/dist/lang/ast/ast-types.js +0 -215
  39. package/dist/lang/ast/index.js +0 -34
  40. package/dist/lang/ast/time-utils.js +0 -94
  41. package/dist/lang/field-space.js +0 -629
  42. package/dist/lang/field-utils.js +0 -33
  43. package/dist/lang/index.js +0 -22
  44. package/dist/lang/parse-log.js +0 -41
  45. package/dist/lang/reference-list.js +0 -80
  46. package/dist/lang/space-field.js +0 -346
  47. package/dist/lang/test/document-help-context-walker.spec.js +0 -45
  48. package/dist/lang/test/document-symbol-walker.spec.js +0 -100
  49. package/dist/lang/test/field-symbols.spec.js +0 -172
  50. package/dist/lang/test/parse.spec.js +0 -1936
  51. package/dist/lang/test/test-translator.js +0 -334
  52. package/dist/lang/zone.js +0 -97
  53. package/dist/malloy.js +0 -2354
  54. package/dist/model/index.js +0 -34
  55. package/dist/model/malloy-query.d.ts +0 -313
  56. package/dist/model/malloy-query.js +0 -2603
  57. package/dist/model/malloy-types.d.ts +0 -404
  58. package/dist/model/malloy-types.js +0 -242
  59. package/dist/model/malloy_query.js +0 -2996
  60. package/dist/model/malloy_types.js +0 -283
  61. package/dist/model/sql-block.d.ts +0 -11
  62. package/dist/model/sql-block.js +0 -38
  63. package/dist/model/sql_block.js +0 -41
  64. package/dist/model/utils.js +0 -84
  65. package/dist/runtime-types.d.ts +0 -116
  66. package/dist/runtime-types.js +0 -40
  67. package/dist/runtime_types.js +0 -15
@@ -1,349 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2021 Google LLC
4
- *
5
- * This program is free software; you can redistribute it and/or
6
- * modify it under the terms of the GNU General Public License
7
- * version 2 as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- */
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.DuckDBDialect = void 0;
16
- const model_1 = require("../model");
17
- const utils_1 = require("../model/utils");
18
- const dialect_1 = require("./dialect");
19
- // need to refactor runSQL to take a SQLBlock instead of just a sql string.
20
- const hackSplitComment = "-- hack: split on this";
21
- const keywords = `
22
- ALL
23
- ANALYSE
24
- ANALYZE
25
- AND
26
- ANY
27
- ARRAY
28
- AS
29
- ASC_P
30
- ASYMMETRIC
31
- BOTH
32
- CASE
33
- CAST
34
- CHECK_P
35
- COLLATE
36
- COLUMN
37
- CONSTRAINT
38
- CREATE_P
39
- CURRENT_CATALOG
40
- CURRENT_DATE
41
- CURRENT_ROLE
42
- CURRENT_TIME
43
- CURRENT_TIMESTAMP
44
- CURRENT_USER
45
- DEFAULT
46
- DEFERRABLE
47
- DESC_P
48
- DISTINCT
49
- DO
50
- ELSE
51
- END_P
52
- EXCEPT
53
- FALSE_P
54
- FETCH
55
- FOR
56
- FOREIGN
57
- FROM
58
- GRANT
59
- GROUP_P
60
- HAVING
61
- IN_P
62
- INITIALLY
63
- INTERSECT
64
- INTO
65
- LATERAL_P
66
- LEADING
67
- LIMIT
68
- LOCALTIME
69
- LOCALTIMESTAMP
70
- NOT
71
- NULL_P
72
- OFFSET
73
- ON
74
- ONLY
75
- OR
76
- ORDER
77
- PLACING
78
- PRIMARY
79
- REFERENCES
80
- RETURNING
81
- SELECT
82
- SESSION_USER
83
- SOME
84
- SYMMETRIC
85
- TABLE
86
- THEN
87
- TO
88
- TRAILING
89
- TRUE_P
90
- UNION
91
- UNIQUE
92
- USER
93
- USING
94
- VARIADIC
95
- WHEN
96
- WHERE
97
- WINDOW
98
- WITH
99
- `.split(/\s/);
100
- const castMap = {
101
- number: "double precision",
102
- string: "varchar",
103
- };
104
- const pgExtractionMap = {
105
- day_of_week: "dow",
106
- day_of_year: "doy",
107
- };
108
- const inSeconds = {
109
- second: 1,
110
- minute: 60,
111
- hour: 3600,
112
- day: 86400,
113
- week: 604800,
114
- };
115
- class DuckDBDialect extends dialect_1.Dialect {
116
- constructor() {
117
- super(...arguments);
118
- this.name = "duckdb";
119
- this.defaultNumberType = "DOUBLE";
120
- this.hasFinalStage = false;
121
- this.stringTypeName = "VARCHAR";
122
- this.divisionIsInteger = true;
123
- this.supportsSumDistinctFunction = true;
124
- this.unnestWithNumbers = true;
125
- this.defaultSampling = { rows: 50000 };
126
- this.supportUnnestArrayAgg = true;
127
- this.supportsCTEinCoorelatedSubQueries = true;
128
- this.functionInfo = {};
129
- }
130
- // hack until they support temporary macros.
131
- get udfPrefix() {
132
- return `__udf${Math.floor(Math.random() * 100000)}`;
133
- }
134
- quoteTablePath(tableName) {
135
- return tableName.match(/\//) ? `'${tableName}'` : tableName;
136
- }
137
- sqlGroupSetTable(groupSetCount) {
138
- return `CROSS JOIN (SELECT UNNEST(GENERATE_SERIES(0,${groupSetCount},1)) as group_set ) as group_set`;
139
- }
140
- sqlAnyValue(groupSet, fieldName) {
141
- return `FIRST(${fieldName}) FILTER (WHERE ${fieldName} IS NOT NULL)`;
142
- }
143
- mapFields(fieldList) {
144
- return fieldList.join(", ");
145
- }
146
- sqlAggregateTurtle(groupSet, fieldList, orderBy, limit) {
147
- let tail = "";
148
- if (limit !== undefined) {
149
- tail += `[1:${limit}]`;
150
- }
151
- const fields = fieldList
152
- .map((f) => `\n ${f.sqlOutputName}: ${f.sqlExpression}`)
153
- .join(", ");
154
- return `COALESCE(LIST({${fields}} ${orderBy}) FILTER (WHERE group_set=${groupSet})${tail},[])`;
155
- }
156
- sqlAnyValueTurtle(groupSet, fieldList) {
157
- const fields = fieldList
158
- .map((f) => `${f.sqlExpression} as ${f.sqlOutputName}`)
159
- .join(", ");
160
- return `ANY_VALUE(CASE WHEN group_set=${groupSet} THEN ROW(${fields}))`;
161
- }
162
- sqlAnyValueLastTurtle(name, groupSet, sqlName) {
163
- return `MAX(CASE WHEN group_set=${groupSet} THEN ${name}__${groupSet} END) as ${sqlName}`;
164
- }
165
- sqlCoaleseMeasuresInline(groupSet, fieldList) {
166
- const fields = fieldList
167
- .map((f) => `${f.sqlOutputName}: ${f.sqlExpression} `)
168
- .join(", ");
169
- const nullValues = fieldList
170
- .map((f) => `${f.sqlOutputName}: NULL`)
171
- .join(", ");
172
- return `COALESCE(FIRST({${fields}}) FILTER(WHERE group_set=${groupSet}), {${nullValues}})`;
173
- }
174
- sqlUnnestAlias(source, alias, _fieldList, _needDistinctKey) {
175
- return `LEFT JOIN (select UNNEST(generate_series(1,
176
- 100000, --
177
- -- (SELECT genres_length FROM movies limit 1),
178
- 1)) as __row_id) as ${alias} ON ${alias}.__row_id <= array_length(${source})`;
179
- }
180
- sqlSumDistinctHashedKey(_sqlDistinctKey) {
181
- return "uses sumDistinctFunction, should not be called";
182
- }
183
- sqlGenerateUUID() {
184
- return `GEN_RANDOM_UUID()`;
185
- }
186
- sqlDateToString(sqlDateExp) {
187
- return `(${sqlDateExp})::date::varchar`;
188
- }
189
- sqlFieldReference(alias, fieldName, _fieldType, _isNested, isArray) {
190
- if (isArray) {
191
- return alias;
192
- }
193
- else {
194
- return `${alias}.${this.sqlMaybeQuoteIdentifier(fieldName)}`;
195
- }
196
- }
197
- sqlUnnestPipelineHead(isSingleton, sourceSQLExpression) {
198
- let p = sourceSQLExpression;
199
- if (isSingleton) {
200
- p = `[${p}]`;
201
- }
202
- return `(SELECT UNNEST(${p}) as base)`;
203
- }
204
- sqlCreateFunction(id, funcText) {
205
- return `DROP MACRO IF EXISTS ${id}; \n${hackSplitComment}\n CREATE MACRO ${id}(_param) AS (\n${(0, utils_1.indent)(funcText)}\n);\n${hackSplitComment}\n`;
206
- }
207
- sqlCreateFunctionCombineLastStage(lastStageName, structDef) {
208
- return `SELECT LIST(ROW(${structDef.fields
209
- .map((fieldDef) => this.sqlMaybeQuoteIdentifier((0, model_1.getIdentifier)(fieldDef)))
210
- .join(",")})) FROM ${lastStageName}\n`;
211
- }
212
- sqlSelectAliasAsStruct(alias, physicalFieldNames) {
213
- return `ROW(${physicalFieldNames
214
- .map((name) => `${alias}.${name}`)
215
- .join(", ")})`;
216
- }
217
- // TODO
218
- // sqlMaybeQuoteIdentifier(identifier: string): string {
219
- // return keywords.indexOf(identifier.toUpperCase()) > 0 ||
220
- // identifier.match(/[a-zA-Z][a-zA-Z0-9]*/) === null || true
221
- // ? '"' + identifier + '"'
222
- // : identifier;
223
- // }
224
- sqlMaybeQuoteIdentifier(identifier) {
225
- return '"' + identifier + '"';
226
- }
227
- // The simple way to do this is to add a comment on the table
228
- // with the expiration time. https://www.postgresql.org/docs/current/sql-comment.html
229
- // and have a reaper that read comments.
230
- sqlCreateTableAsSelect(_tableName, _sql) {
231
- throw new Error("Not implemented Yet");
232
- }
233
- getFunctionInfo(_functionName) {
234
- return undefined;
235
- }
236
- sqlMeasureTime(from, to, units) {
237
- let lVal = from.value;
238
- let rVal = to.value;
239
- if (inSeconds[units]) {
240
- lVal = (0, model_1.mkExpr) `EXTRACT(EPOCH FROM ${lVal})`;
241
- rVal = (0, model_1.mkExpr) `EXTRACT(EPOCH FROM ${rVal})`;
242
- const duration = (0, model_1.mkExpr) `(${rVal} - ${lVal})`;
243
- return units == "second"
244
- ? duration
245
- : (0, model_1.mkExpr) `TRUNC(${duration}/${inSeconds[units].toString()})`;
246
- }
247
- const yearDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(YEAR FROM ${rVal}) - EXTRACT(YEAR FROM ${lVal}))`;
248
- if (units == "year") {
249
- return yearDiff;
250
- }
251
- if (units == "month") {
252
- const monthDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(MONTH FROM ${rVal}) - EXTRACT(MONTH FROM ${lVal}))`;
253
- return (0, model_1.mkExpr) `${yearDiff} * 12 + ${monthDiff}`;
254
- }
255
- if (units == "quarter") {
256
- const qDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(QUARTER FROM ${rVal}) - EXTRACT(QUARTER FROM ${lVal}))`;
257
- return (0, model_1.mkExpr) `${yearDiff} * 4 + ${qDiff}`;
258
- }
259
- throw new Error(`Unknown or unhandled postgres time unit: ${units}`);
260
- }
261
- sqlNow() {
262
- return (0, model_1.mkExpr) `CURRENT_TIMESTAMP`;
263
- }
264
- sqlTrunc(sqlTime, units) {
265
- // adjusting for monday/sunday weeks
266
- const week = units == "week";
267
- const truncThis = week
268
- ? (0, model_1.mkExpr) `${sqlTime.value}+interval'1'day`
269
- : sqlTime.value;
270
- const trunced = (0, model_1.mkExpr) `DATE_TRUNC('${units}', ${truncThis})`;
271
- return week ? (0, model_1.mkExpr) `(${trunced}-interval'1'day)` : trunced;
272
- }
273
- sqlExtract(from, units) {
274
- const pgUnits = pgExtractionMap[units] || units;
275
- const extracted = (0, model_1.mkExpr) `EXTRACT(${pgUnits} FROM ${from.value})`;
276
- return units == "day_of_week" ? (0, model_1.mkExpr) `(${extracted}+1)` : extracted;
277
- }
278
- sqlAlterTime(op, expr, n, timeframe) {
279
- if (timeframe == "quarter") {
280
- timeframe = "month";
281
- n = (0, model_1.mkExpr) `${n}*3`;
282
- }
283
- if (timeframe == "week") {
284
- timeframe = "day";
285
- n = (0, model_1.mkExpr) `${n}*7`;
286
- }
287
- const interval = (0, model_1.mkExpr) `INTERVAL ${n} ${timeframe}`;
288
- return (0, model_1.mkExpr) `((${expr.value})${op}${interval})`;
289
- }
290
- sqlCast(cast) {
291
- if (cast.dstType !== cast.srcType) {
292
- const castTo = castMap[cast.dstType] || cast.dstType;
293
- return (0, model_1.mkExpr) `cast(${cast.expr} as ${castTo})`;
294
- }
295
- return cast.expr;
296
- }
297
- sqlRegexpMatch(expr, regexp) {
298
- return (0, model_1.mkExpr) `REGEXP_MATCHES(${expr}, ${regexp})`;
299
- }
300
- sqlLiteralTime(timeString, type, _timezone) {
301
- if (type == "date") {
302
- return `DATE('${timeString}')`;
303
- }
304
- else if (type == "timestamp") {
305
- return `TIMESTAMP '${timeString}'`;
306
- }
307
- else {
308
- throw new Error(`Unknown Literal time format ${type}`);
309
- }
310
- }
311
- // sqlSumDistinct(key: string, value: string): string {
312
- // // return `sum_distinct(list({key:${key}, val: ${value}}))`;
313
- // return `(
314
- // fail -- force the query for fail until the bug is fixed.
315
- // SELECT sum(a.val) as value
316
- // FROM (
317
- // SELECT UNNEST(list(distinct {key:${key}, val: ${value}})) a
318
- // )
319
- // )`;
320
- // }
321
- sqlSumDistinct(key, value) {
322
- const _factor = 32;
323
- const precision = 0.000001;
324
- const keySQL = `md5_number_lower(${key}::varchar)::int128`;
325
- return `
326
- (SUM(DISTINCT ${keySQL} + FLOOR(IFNULL(${value},0)/${precision})::int128) - SUM(DISTINCT ${keySQL}))*${precision}
327
- `;
328
- }
329
- // default duckdb to sampling 50K rows.
330
- sqlSampleTable(tableSQL, sample) {
331
- if (sample !== undefined) {
332
- if ((0, model_1.isSamplingEnable)(sample) && sample.enable) {
333
- sample = this.defaultSampling;
334
- }
335
- if ((0, model_1.isSamplingRows)(sample)) {
336
- return `(SELECT * FROM ${tableSQL} USING SAMPLE ${sample.rows})`;
337
- }
338
- else if ((0, model_1.isSamplingPercent)(sample)) {
339
- return `(SELECT * FROM ${tableSQL} USING SAMPLE ${sample.percent} PERCENT (bernoulli))`;
340
- }
341
- }
342
- return tableSQL;
343
- }
344
- sqlOrderBy(orderTerms) {
345
- return `ORDER BY ${orderTerms.map((t) => `${t} NULLS LAST`).join(",")}`;
346
- }
347
- }
348
- exports.DuckDBDialect = DuckDBDialect;
349
- //# sourceMappingURL=duckdb.js.map
@@ -1,27 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2021 Google LLC
4
- *
5
- * This program is free software; you can redistribute it and/or
6
- * modify it under the terms of the GNU General Public License
7
- * version 2 as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- */
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.registerDialect = exports.getDialect = exports.DuckDBDialect = exports.PostgresDialect = exports.StandardSQLDialect = exports.Dialect = void 0;
16
- var dialect_1 = require("./dialect");
17
- Object.defineProperty(exports, "Dialect", { enumerable: true, get: function () { return dialect_1.Dialect; } });
18
- var standardsql_1 = require("./standardsql");
19
- Object.defineProperty(exports, "StandardSQLDialect", { enumerable: true, get: function () { return standardsql_1.StandardSQLDialect; } });
20
- var postgres_1 = require("./postgres");
21
- Object.defineProperty(exports, "PostgresDialect", { enumerable: true, get: function () { return postgres_1.PostgresDialect; } });
22
- var duckdb_1 = require("./duckdb");
23
- Object.defineProperty(exports, "DuckDBDialect", { enumerable: true, get: function () { return duckdb_1.DuckDBDialect; } });
24
- var dialect_map_1 = require("./dialect_map");
25
- Object.defineProperty(exports, "getDialect", { enumerable: true, get: function () { return dialect_map_1.getDialect; } });
26
- Object.defineProperty(exports, "registerDialect", { enumerable: true, get: function () { return dialect_map_1.registerDialect; } });
27
- //# sourceMappingURL=index.js.map
@@ -1,308 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2021 Google LLC
4
- *
5
- * This program is free software; you can redistribute it and/or
6
- * modify it under the terms of the GNU General Public License
7
- * version 2 as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
13
- */
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.PostgresDialect = void 0;
16
- const utils_1 = require("../model/utils");
17
- const model_1 = require("../model");
18
- const dialect_1 = require("./dialect");
19
- const castMap = {
20
- number: "double precision",
21
- string: "varchar",
22
- };
23
- const pgExtractionMap = {
24
- day_of_week: "dow",
25
- day_of_year: "doy",
26
- };
27
- const pgMakeIntervalMap = {
28
- year: "years",
29
- month: "months",
30
- week: "weeks",
31
- day: "days",
32
- hour: "hours",
33
- minute: "mins",
34
- second: "secs",
35
- };
36
- const inSeconds = {
37
- second: 1,
38
- minute: 60,
39
- hour: 3600,
40
- day: 86400,
41
- week: 604800,
42
- };
43
- class PostgresDialect extends dialect_1.Dialect {
44
- constructor() {
45
- super(...arguments);
46
- this.name = "postgres";
47
- this.defaultNumberType = "DOUBLE PRECISION";
48
- this.udfPrefix = "pg_temp.__udf";
49
- this.hasFinalStage = true;
50
- this.stringTypeName = "VARCHAR";
51
- this.divisionIsInteger = true;
52
- this.supportsSumDistinctFunction = false;
53
- this.unnestWithNumbers = false;
54
- this.defaultSampling = { rows: 50000 };
55
- this.supportUnnestArrayAgg = true;
56
- this.supportsCTEinCoorelatedSubQueries = true;
57
- this.functionInfo = {};
58
- }
59
- quoteTablePath(tablePath) {
60
- return tablePath
61
- .split(".")
62
- .map((part) => `"${part}"`)
63
- .join(".");
64
- }
65
- sqlGroupSetTable(groupSetCount) {
66
- return `CROSS JOIN GENERATE_SERIES(0,${groupSetCount},1) as group_set`;
67
- }
68
- sqlAnyValue(groupSet, fieldName) {
69
- return `MAX(${fieldName})`;
70
- }
71
- mapFields(fieldList) {
72
- return fieldList
73
- .map((f) => `\n ${f.sqlExpression}${f.type == "number" ? `::${this.defaultNumberType}` : ""} as ${f.sqlOutputName}`
74
- //`${f.sqlExpression} ${f.type} as ${f.sqlOutputName}`
75
- )
76
- .join(", ");
77
- }
78
- sqlAggregateTurtle(groupSet, fieldList, orderBy, limit) {
79
- let tail = "";
80
- if (limit !== undefined) {
81
- tail += `[1:${limit}]`;
82
- }
83
- const fields = this.mapFields(fieldList);
84
- // return `(ARRAY_AGG((SELECT __x FROM (SELECT ${fields}) as __x) ${orderBy} ) FILTER (WHERE group_set=${groupSet}))${tail}`;
85
- return `COALESCE(TO_JSONB((ARRAY_AGG((SELECT TO_JSONB(__x) FROM (SELECT ${fields}\n ) as __x) ${orderBy} ) FILTER (WHERE group_set=${groupSet}))${tail}),'[]'::JSONB)`;
86
- }
87
- sqlAnyValueTurtle(groupSet, fieldList) {
88
- const fields = fieldList
89
- .map((f) => `${f.sqlExpression} as ${f.sqlOutputName}`)
90
- .join(", ");
91
- return `ANY_VALUE(CASE WHEN group_set=${groupSet} THEN STRUCT(${fields}))`;
92
- }
93
- sqlAnyValueLastTurtle(name, groupSet, sqlName) {
94
- return `(ARRAY_AGG(${name}__${groupSet}) FILTER (WHERE group_set=${groupSet} AND ${name}__${groupSet} IS NOT NULL))[1] as ${sqlName}`;
95
- }
96
- sqlCoaleseMeasuresInline(groupSet, fieldList) {
97
- const fields = this.mapFields(fieldList);
98
- return `TO_JSONB((ARRAY_AGG((SELECT __x FROM (SELECT ${fields}) as __x)) FILTER (WHERE group_set=${groupSet}))[1])`;
99
- }
100
- // UNNEST((select ARRAY((SELECT ROW(gen_random_uuid()::text, state, airport_count) FROM UNNEST(base.by_state) as by_state(state text, airport_count numeric, by_fac_type record[]))))) as by_state(__distinct_key text, state text, airport_count numeric)
101
- // sqlUnnestAlias(
102
- // source: string,
103
- // alias: string,
104
- // fieldList: DialectFieldList,
105
- // needDistinctKey: boolean
106
- // ): string {
107
- // const fields = [];
108
- // for (const f of fieldList) {
109
- // let t = undefined;
110
- // switch (f.type) {
111
- // case "string":
112
- // t = "text";
113
- // break;
114
- // case "number":
115
- // t = this.defaultNumberType;
116
- // break;
117
- // case "struct":
118
- // t = "record[]";
119
- // break;
120
- // }
121
- // fields.push(`${f.sqlOutputName} ${t || f.type}`);
122
- // }
123
- // if (needDistinctKey) {
124
- // return `UNNEST((select ARRAY((SELECT ROW(gen_random_uuid()::text, ${fieldList
125
- // .map((f) => f.sqlOutputName)
126
- // .join(", ")}) FROM UNNEST(${source}) as ${alias}(${fields.join(
127
- // ", "
128
- // )}))))) as ${alias}(__distinct_key text, ${fields.join(", ")})`;
129
- // } else {
130
- // return `UNNEST(${source}) as ${alias}(${fields.join(", ")})`;
131
- // }
132
- // }
133
- sqlUnnestAlias(source, alias, fieldList, needDistinctKey, isArray) {
134
- if (isArray) {
135
- if (needDistinctKey) {
136
- return `LEFT JOIN UNNEST(ARRAY((SELECT jsonb_build_object('__row_id', row_number() over (), 'value', v) FROM UNNEST(${source}) as v))) as ${alias} ON true`;
137
- }
138
- else {
139
- return `LEFT JOIN UNNEST(ARRAY((SELECT jsonb_build_object('value', v) FROM UNNEST(${source}) as v))) as ${alias} ON true`;
140
- }
141
- }
142
- else if (needDistinctKey) {
143
- // return `UNNEST(ARRAY(( SELECT AS STRUCT GENERATE_UUID() as __distinct_key, * FROM UNNEST(${source})))) as ${alias}`;
144
- return `LEFT JOIN UNNEST(ARRAY((SELECT jsonb_build_object('__row_number', row_number() over())|| __xx::jsonb as b FROM JSONB_ARRAY_ELEMENTS(${source}) __xx ))) as ${alias} ON true`;
145
- }
146
- else {
147
- // return `CROSS JOIN LATERAL JSONB_ARRAY_ELEMENTS(${source}) as ${alias}`;
148
- return `LEFT JOIN JSONB_ARRAY_ELEMENTS(${source}) as ${alias} ON true`;
149
- }
150
- }
151
- sqlSumDistinctHashedKey(sqlDistinctKey) {
152
- return `('x' || MD5(${sqlDistinctKey}::varchar))::bit(64)::bigint::DECIMAL(65,0) *18446744073709551616 + ('x' || SUBSTR(MD5(${sqlDistinctKey}::varchar),17))::bit(64)::bigint::DECIMAL(65,0)`;
153
- }
154
- sqlGenerateUUID() {
155
- return `GEN_RANDOM_UUID()`;
156
- }
157
- sqlFieldReference(alias, fieldName, fieldType, isNested, _isArray) {
158
- let ret = `${alias}->>'${fieldName}'`;
159
- if (isNested) {
160
- switch (fieldType) {
161
- case "string":
162
- break;
163
- case "number":
164
- ret = `(${ret})::double precision`;
165
- break;
166
- case "struct":
167
- ret = `(${ret})::jsonb`;
168
- break;
169
- }
170
- return ret;
171
- }
172
- else {
173
- return `${alias}."${fieldName}"`;
174
- }
175
- }
176
- sqlUnnestPipelineHead(isSingleton, sourceSQLExpression) {
177
- if (isSingleton) {
178
- return `UNNEST(ARRAY((SELECT ${sourceSQLExpression})))`;
179
- }
180
- else {
181
- return `JSONB_ARRAY_ELEMENTS(${sourceSQLExpression})`;
182
- }
183
- }
184
- sqlCreateFunction(id, funcText) {
185
- return `CREATE FUNCTION ${id}(JSONB) RETURNS JSONB AS $$\n${(0, utils_1.indent)(funcText)}\n$$ LANGUAGE SQL;\n`;
186
- }
187
- sqlCreateFunctionCombineLastStage(lastStageName) {
188
- return `SELECT JSONB_AGG(__stage0) FROM ${lastStageName}\n`;
189
- }
190
- sqlFinalStage(lastStageName, _fields) {
191
- return `SELECT row_to_json(finalStage) as row FROM ${lastStageName} AS finalStage`;
192
- }
193
- sqlSelectAliasAsStruct(alias) {
194
- return `ROW(${alias})`;
195
- }
196
- // TODO
197
- sqlMaybeQuoteIdentifier(identifier) {
198
- return `"${identifier}"`;
199
- }
200
- // The simple way to do this is to add a comment on the table
201
- // with the expiration time. https://www.postgresql.org/docs/current/sql-comment.html
202
- // and have a reaper that read comments.
203
- sqlCreateTableAsSelect(_tableName, _sql) {
204
- throw new Error("Not implemented Yet");
205
- }
206
- sqlNow() {
207
- return (0, model_1.mkExpr) `CURRENT_TIMESTAMP`;
208
- }
209
- sqlTrunc(sqlTime, units) {
210
- // adjusting for monday/sunday weeks
211
- const week = units == "week";
212
- const truncThis = week
213
- ? (0, model_1.mkExpr) `${sqlTime.value}+interval'1'day`
214
- : sqlTime.value;
215
- const trunced = (0, model_1.mkExpr) `DATE_TRUNC('${units}', ${truncThis})`;
216
- return week ? (0, model_1.mkExpr) `(${trunced}-interval'1'day)` : trunced;
217
- }
218
- sqlExtract(from, units) {
219
- const pgUnits = pgExtractionMap[units] || units;
220
- const extracted = (0, model_1.mkExpr) `EXTRACT(${pgUnits} FROM ${from.value})`;
221
- return units == "day_of_week" ? (0, model_1.mkExpr) `(${extracted}+1)` : extracted;
222
- }
223
- sqlAlterTime(op, expr, n, timeframe) {
224
- if (timeframe == "quarter") {
225
- timeframe = "month";
226
- n = (0, model_1.mkExpr) `${n}*3`;
227
- }
228
- const interval = (0, model_1.mkExpr) `make_interval(${pgMakeIntervalMap[timeframe]}=>${n})`;
229
- return (0, model_1.mkExpr) `((${expr.value})${op}${interval})`;
230
- }
231
- sqlCast(cast) {
232
- if (cast.dstType !== cast.srcType) {
233
- const castTo = castMap[cast.dstType] || cast.dstType;
234
- return (0, model_1.mkExpr) `cast(${cast.expr} as ${castTo})`;
235
- }
236
- return cast.expr;
237
- }
238
- sqlRegexpMatch(expr, regexp) {
239
- return (0, model_1.mkExpr) `(${expr} ~ ${regexp})`;
240
- }
241
- sqlLiteralTime(timeString, type, _timezone) {
242
- if (type == "date") {
243
- return `DATE('${timeString}')`;
244
- }
245
- else if (type == "timestamp") {
246
- return `TIMESTAMP '${timeString}'`;
247
- }
248
- else {
249
- throw new Error(`Unknown Literal time format ${type}`);
250
- }
251
- }
252
- getFunctionInfo(_functionName) {
253
- return undefined;
254
- }
255
- sqlMeasureTime(from, to, units) {
256
- let lVal = from.value;
257
- let rVal = to.value;
258
- if (inSeconds[units]) {
259
- lVal = (0, model_1.mkExpr) `EXTRACT(EPOCH FROM ${lVal})`;
260
- rVal = (0, model_1.mkExpr) `EXTRACT(EPOCH FROM ${rVal})`;
261
- const duration = (0, model_1.mkExpr) `(${rVal} - ${lVal})`;
262
- return units == "second"
263
- ? duration
264
- : (0, model_1.mkExpr) `TRUNC(${duration}/${inSeconds[units].toString()})`;
265
- }
266
- const yearDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(YEAR FROM ${rVal}) - EXTRACT(YEAR FROM ${lVal}))`;
267
- if (units == "year") {
268
- return yearDiff;
269
- }
270
- if (units == "month") {
271
- const monthDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(MONTH FROM ${rVal}) - EXTRACT(MONTH FROM ${lVal}))`;
272
- return (0, model_1.mkExpr) `${yearDiff} * 12 + ${monthDiff}`;
273
- }
274
- if (units == "quarter") {
275
- const qDiff = (0, model_1.mkExpr) `TRUNC(EXTRACT(QUARTER FROM ${rVal}) - EXTRACT(QUARTER FROM ${lVal}))`;
276
- return (0, model_1.mkExpr) `${yearDiff} * 4 + ${qDiff}`;
277
- }
278
- throw new Error(`Unknown or unhandled postgres time unit: ${units}`);
279
- }
280
- sqlSumDistinct(key, value) {
281
- // return `sum_distinct(list({key:${key}, val: ${value}}))`;
282
- return `(
283
- SELECT sum((a::json->>'f2')::DOUBLE PRECISION) as value
284
- FROM (
285
- SELECT UNNEST(array_agg(distinct row_to_json(row(${key},${value}))::text)) a
286
- ) a
287
- )`;
288
- }
289
- sqlSampleTable(tableSQL, sample) {
290
- if (sample !== undefined) {
291
- if ((0, model_1.isSamplingEnable)(sample) && sample.enable) {
292
- sample = this.defaultSampling;
293
- }
294
- if ((0, model_1.isSamplingRows)(sample)) {
295
- return `(SELECT * FROM ${tableSQL} TABLESAMPLE SYSTEM_ROWS(${sample.rows}))`;
296
- }
297
- else if ((0, model_1.isSamplingPercent)(sample)) {
298
- return `(SELECT * FROM ${tableSQL} TABLESAMPLE SYSTEM (${sample.percent}))`;
299
- }
300
- }
301
- return tableSQL;
302
- }
303
- sqlOrderBy(orderTerms) {
304
- return `ORDER BY ${orderTerms.map((t) => `${t} NULLS LAST`).join(",")}`;
305
- }
306
- }
307
- exports.PostgresDialect = PostgresDialect;
308
- //# sourceMappingURL=postgres.js.map