@malloydata/malloy 0.0.142-dev240411154213 → 0.0.142-dev240412191256

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.
@@ -53,6 +53,7 @@ export declare abstract class Dialect {
53
53
  orderByClause: OrderByClauseType;
54
54
  nullMatchesFunctionSignature: boolean;
55
55
  supportsSelectReplace: boolean;
56
+ supportsComplexFilteredSources: boolean;
56
57
  abstract getGlobalFunctionDef(name: string): DialectFunctionOverloadDef[] | undefined;
57
58
  abstract quoteTablePath(tablePath: string): string;
58
59
  abstract sqlGroupSetTable(groupSetCount: number): string;
@@ -94,6 +95,7 @@ export declare abstract class Dialect {
94
95
  sqlOrderBy(orderTerms: string[]): string;
95
96
  sqlTzStr(qi: QueryInfo): string;
96
97
  sqlMakeUnnestKey(key: string, rowKey: string): string;
98
+ sqlStringAggDistinct(distinctKey: string, valueSQL: string, separatorSQL: string): string;
97
99
  abstract sqlTypeToMalloyType(sqlType: string): FieldAtomicTypeDef | undefined;
98
100
  abstract malloyTypeToSQLType(malloyType: FieldAtomicTypeDef): string;
99
101
  abstract validateTypeName(sqlType: string): boolean;
@@ -74,6 +74,8 @@ class Dialect {
74
74
  this.nullMatchesFunctionSignature = true;
75
75
  // support select * replace(...)
76
76
  this.supportsSelectReplace = true;
77
+ // ability to join source with a filter on a joined source.
78
+ this.supportsComplexFilteredSources = true;
77
79
  }
78
80
  sqlFinalStage(_lastStageName, _fields) {
79
81
  throw new Error('Dialect has no final Stage but called Anyway');
@@ -152,6 +154,17 @@ class Dialect {
152
154
  sqlMakeUnnestKey(key, rowKey) {
153
155
  return this.concat(key, "'x'", rowKey);
154
156
  }
157
+ // default implementation
158
+ sqlStringAggDistinct(distinctKey, valueSQL, separatorSQL) {
159
+ const keyStart = '__STRING_AGG_KS__';
160
+ const keyEnd = '__STRING_AGG_KE__';
161
+ const distinctValueSQL = `concat('${keyStart}', ${distinctKey}, '${keyEnd}', ${valueSQL})`;
162
+ return `REGEXP_REPLACE(
163
+ STRING_AGG(DISTINCT ${distinctValueSQL}${separatorSQL.length > 0 ? ',' + separatorSQL : ''}),
164
+ '${keyStart}.*?${keyEnd}',
165
+ ''
166
+ )`;
167
+ }
155
168
  }
156
169
  exports.Dialect = Dialect;
157
170
  //# sourceMappingURL=dialect.js.map
@@ -0,0 +1,3 @@
1
+ import { DialectFunctionOverloadDef } from '../../functions/util';
2
+ export declare function fnStringAgg(): DialectFunctionOverloadDef[];
3
+ export declare function fnStringAggDistinct(): DialectFunctionOverloadDef[];
@@ -0,0 +1,55 @@
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.fnStringAggDistinct = exports.fnStringAgg = void 0;
26
+ const util_1 = require("../../functions/util");
27
+ function fnStringAgg() {
28
+ const value = (0, util_1.makeParam)('value', (0, util_1.maxScalar)('string'));
29
+ const separator = (0, util_1.makeParam)('separator', (0, util_1.literal)((0, util_1.maxScalar)('string')));
30
+ const orderBy = {
31
+ type: 'aggregate_order_by',
32
+ prefix: '',
33
+ suffix: '',
34
+ };
35
+ return [
36
+ (0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `ARRAY_JOIN(ARRAY_AGG(${value.arg} ${orderBy}), ',')`, { supportsOrderBy: true, supportsLimit: false }),
37
+ (0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `ARRAY_JOIN(ARRAY_AGG(${value.arg} ${orderBy}), ${separator.arg})`, { supportsOrderBy: true, supportsLimit: false }),
38
+ ];
39
+ }
40
+ exports.fnStringAgg = fnStringAgg;
41
+ function fnStringAggDistinct() {
42
+ const value = (0, util_1.makeParam)('value', (0, util_1.maxScalar)('string'));
43
+ const separator = (0, util_1.makeParam)('separator', (0, util_1.literal)((0, util_1.maxScalar)('string')));
44
+ const orderBy = {
45
+ type: 'aggregate_order_by',
46
+ prefix: '',
47
+ suffix: '',
48
+ };
49
+ return [
50
+ (0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `ARRAY_JOIN(ARRAY_AGG(DISTINCT ${value.arg} ${orderBy}), ',')`, { supportsOrderBy: true, supportsLimit: false }),
51
+ (0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `ARRAY_JOIN(ARRAY_AGG(DISTINCT ${value.arg} ${orderBy}), ${separator.arg})`, { supportsOrderBy: true, supportsLimit: false }),
52
+ ];
53
+ }
54
+ exports.fnStringAggDistinct = fnStringAggDistinct;
55
+ //# sourceMappingURL=string_agg.js.map
@@ -29,11 +29,14 @@ const log_1 = require("./log");
29
29
  const ifnull_1 = require("./ifnull");
30
30
  const concat_1 = require("./concat");
31
31
  const byte_length_1 = require("./byte_length");
32
+ const string_agg_1 = require("./string_agg");
32
33
  exports.TRINO_FUNCTIONS = functions_1.FUNCTIONS.clone();
33
34
  exports.TRINO_FUNCTIONS.add('trunc', trunc_1.fnTrunc);
34
35
  exports.TRINO_FUNCTIONS.add('log', log_1.fnLog);
35
36
  exports.TRINO_FUNCTIONS.add('ifnull', ifnull_1.fnIfnull);
36
37
  exports.TRINO_FUNCTIONS.add('byte_length', byte_length_1.fnByteLength);
37
38
  exports.TRINO_FUNCTIONS.add('concat', concat_1.fnConcat);
39
+ exports.TRINO_FUNCTIONS.add('string_agg', string_agg_1.fnStringAgg);
40
+ exports.TRINO_FUNCTIONS.add('string_agg_distinct', string_agg_1.fnStringAggDistinct);
38
41
  exports.TRINO_FUNCTIONS.seal();
39
42
  //# sourceMappingURL=trino_functions.js.map
@@ -24,6 +24,7 @@ export declare class TrinoDialect extends Dialect {
24
24
  orderByClause: OrderByClauseType;
25
25
  nullMatchesFunctionSignature: boolean;
26
26
  supportsSelectReplace: boolean;
27
+ supportsComplexFilteredSources: boolean;
27
28
  quoteTablePath(tablePath: string): string;
28
29
  sqlGroupSetTable(groupSetCount: number): string;
29
30
  dialectExpr(qi: QueryInfo, df: DialectFragment): Expr;
@@ -61,5 +62,6 @@ export declare class TrinoDialect extends Dialect {
61
62
  castToString(expression: string): string;
62
63
  concat(...values: string[]): string;
63
64
  sqlMakeUnnestKey(key: string, rowKey: string): string;
65
+ sqlStringAggDistinct(distinctKey: string, valueSQL: string, separatorSQL: string): string;
64
66
  validateTypeName(sqlType: string): boolean;
65
67
  }
@@ -78,6 +78,7 @@ class TrinoDialect extends dialect_1.Dialect {
78
78
  this.orderByClause = 'output_name';
79
79
  this.nullMatchesFunctionSignature = false;
80
80
  this.supportsSelectReplace = false;
81
+ this.supportsComplexFilteredSources = false;
81
82
  // TODO(figutierrez): update.
82
83
  this.keywords = `
83
84
  ALL
@@ -275,7 +276,7 @@ class TrinoDialect extends dialect_1.Dialect {
275
276
  if (fieldName === '__row_id') {
276
277
  return `__row_id_from_${alias}`;
277
278
  }
278
- return `${alias}.${fieldName}`;
279
+ return `${alias}.${this.sqlMaybeQuoteIdentifier(fieldName)}`;
279
280
  }
280
281
  sqlUnnestPipelineHead(isSingleton, sourceSQLExpression) {
281
282
  let p = sourceSQLExpression;
@@ -347,7 +348,7 @@ ${(0, utils_1.indent)(sql)}
347
348
  }
348
349
  }
349
350
  const extracted = (0, malloy_types_1.mkExpr) `EXTRACT(${pgUnits} FROM ${extractFrom})`;
350
- return units === 'day_of_week' ? (0, malloy_types_1.mkExpr) `(${extracted}+1)` : extracted;
351
+ return units === 'day_of_week' ? (0, malloy_types_1.mkExpr) `mod(${extracted}+1,7)` : extracted;
351
352
  }
352
353
  sqlAlterTime(op, expr, n, timeframe) {
353
354
  if (timeframe === 'quarter') {
@@ -433,10 +434,10 @@ ${(0, utils_1.indent)(sql)}
433
434
  sample = this.defaultSampling;
434
435
  }
435
436
  if ((0, malloy_types_1.isSamplingRows)(sample)) {
436
- throw new Error("StandardSQL doesn't support sampling by rows only percent");
437
+ throw new Error("Trino doesn't support sampling by rows only percent");
437
438
  }
438
439
  else if ((0, malloy_types_1.isSamplingPercent)(sample)) {
439
- return `(SELECT * FROM ${tableSQL} TABLESAMPLE SYSTEM (${sample.percent} PERCENT))`;
440
+ return `(SELECT * FROM ${tableSQL} TABLESAMPLE SYSTEM (${sample.percent}))`;
440
441
  }
441
442
  }
442
443
  return tableSQL;
@@ -478,6 +479,10 @@ ${(0, utils_1.indent)(sql)}
478
479
  sqlMakeUnnestKey(key, rowKey) {
479
480
  return `CAST(${key} as VARCHAR) || 'x' || CAST(${rowKey} as VARCHAR)`;
480
481
  }
482
+ sqlStringAggDistinct(distinctKey, valueSQL, separatorSQL) {
483
+ return `
484
+ ARRAY_JOIN(TRANSFORM(ARRAY_AGG(DISTINCT ARRAY[CAST(${valueSQL} AS VARCHAR),CAST(${distinctKey} as VARCHAR)]), x -> x[1]),${separatorSQL.length > 0 ? separatorSQL : "','"})`;
485
+ }
481
486
  validateTypeName(sqlType) {
482
487
  // Letters: BIGINT
483
488
  // Numbers: INT8
@@ -334,16 +334,9 @@ class QueryField extends QueryNode {
334
334
  }
335
335
  const valueSQL = this.generateDimFragment(resultSet, context, value, state);
336
336
  const separatorSQL = separator
337
- ? ' ,' + this.generateDimFragment(resultSet, context, separator, state)
337
+ ? this.generateDimFragment(resultSet, context, separator, state)
338
338
  : '';
339
- const keyStart = '__STRING_AGG_KS__';
340
- const keyEnd = '__STRING_AGG_KE__';
341
- const distinctValueSQL = `concat('${keyStart}', ${distinctKey}, '${keyEnd}', ${valueSQL})`;
342
- return `REGEXP_REPLACE(
343
- STRING_AGG(DISTINCT ${distinctValueSQL}${separatorSQL}),
344
- '${keyStart}.*?${keyEnd}',
345
- ''
346
- )`;
339
+ return this.parent.dialect.sqlStringAggDistinct(distinctKey, valueSQL, separatorSQL);
347
340
  }
348
341
  getParamForArgIndex(params, argIndex) {
349
342
  const prevVariadic = params.slice(0, argIndex).find(p => p.isVariadic);
@@ -1995,6 +1988,9 @@ class QueryQuery extends QueryField {
1995
1988
  s += ` ${matrixOperation} JOIN ${structSQL} AS ${ji.alias}\n ON ${onCondition}${filters}\n`;
1996
1989
  }
1997
1990
  else {
1991
+ if (!this.parent.dialect.supportsComplexFilteredSources) {
1992
+ throw new Error('Cannot join a source with a filter on a joined source');
1993
+ }
1998
1994
  let select = `SELECT ${ji.alias}.*`;
1999
1995
  let joins = '';
2000
1996
  for (const childJoin of ji.children) {
@@ -2503,7 +2499,7 @@ class QueryQuery extends QueryField {
2503
2499
  field instanceof FieldInstanceField &&
2504
2500
  field.fieldUsage.type === 'result') {
2505
2501
  dialectFieldList.push({
2506
- type: field.type,
2502
+ type: field.f.fieldDef.type,
2507
2503
  sqlExpression: field.f.generateExpression(resultStruct),
2508
2504
  rawName: name,
2509
2505
  sqlOutputName: sqlName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.142-dev240411154213",
3
+ "version": "0.0.142-dev240412191256",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",