@malloydata/malloy 0.0.153-dev240719133924 → 0.0.153

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.
@@ -34,6 +34,7 @@ export declare class SnowflakeDialect extends Dialect {
34
34
  sqlCoaleseMeasuresInline(groupSet: number, fieldList: DialectFieldList): string;
35
35
  sqlUnnestAlias(source: string, alias: string, _fieldList: DialectFieldList, _needDistinctKey: boolean, isArray: boolean, _isInNestedPipeline: boolean): string;
36
36
  sqlSumDistinctHashedKey(sqlDistinctKey: string): string;
37
+ sqlSumDistinct(key: string, value: string, funcName: string): string;
37
38
  sqlGenerateUUID(): string;
38
39
  sqlFieldReference(alias: string, fieldName: string, fieldType: string, isNested: boolean, _isArray: boolean): string;
39
40
  sqlUnnestPipelineHead(isSingleton: boolean, sourceSQLExpression: string): string;
@@ -81,7 +81,7 @@ class SnowflakeDialect extends dialect_1.Dialect {
81
81
  this.udfPrefix = '__udf';
82
82
  this.hasFinalStage = false;
83
83
  this.divisionIsInteger = false;
84
- this.supportsSumDistinctFunction = false;
84
+ this.supportsSumDistinctFunction = true;
85
85
  this.supportsSafeCast = true;
86
86
  this.supportsNesting = true;
87
87
  this.defaultSampling = { rows: 50000 };
@@ -179,8 +179,20 @@ class SnowflakeDialect extends dialect_1.Dialect {
179
179
  sqlDistinctKey = `${sqlDistinctKey}::STRING`;
180
180
  const upperPart = `to_number(substr(md5_hex(${sqlDistinctKey}), 1, 15), repeat('X', 15)) * 4294967296`;
181
181
  const lowerPart = `to_number(substr(md5_hex(${sqlDistinctKey}), 16, 8), repeat('X', 8))`;
182
- const precisionShiftMultiplier = '0.000000001';
183
- return `(${upperPart} + ${lowerPart}) * ${precisionShiftMultiplier}`;
182
+ return `(${upperPart} + ${lowerPart})`;
183
+ }
184
+ sqlSumDistinct(key, value, funcName) {
185
+ const hashKey = this.sqlSumDistinctHashedKey(key);
186
+ const scale = 100000000.0;
187
+ const v = `(COALESCE(${value},0)*${scale})`;
188
+ const sqlSum = `(SUM(DISTINCT ${hashKey} + ${v}) - SUM(DISTINCT ${hashKey}))/${scale}`;
189
+ if (funcName === 'SUM') {
190
+ return sqlSum;
191
+ }
192
+ else if (funcName === 'AVG') {
193
+ return `(${sqlSum})/NULLIF(COUNT(DISTINCT CASE WHEN ${value} IS NOT NULL THEN ${key} END),0)`;
194
+ }
195
+ throw new Error(`Unknown Symmetric Aggregate function ${funcName}`);
184
196
  }
185
197
  sqlGenerateUUID() {
186
198
  return 'UUID_STRING()';
@@ -37,7 +37,9 @@ export declare class TrinoDialect extends Dialect {
37
37
  sqlAnyValueLastTurtle(name: string, groupSet: number, sqlName: string): string;
38
38
  sqlCoaleseMeasuresInline(groupSet: number, fieldList: DialectFieldList): string;
39
39
  sqlUnnestAlias(source: string, alias: string, _fieldList: DialectFieldList, needDistinctKey: boolean, isArray: boolean, _isInNestedPipeline: boolean): string;
40
+ static dtype: string;
40
41
  sqlSumDistinctHashedKey(sqlDistinctKey: string): string;
42
+ sqlSumDistinct(key: string, value: string, funcName: string): string;
41
43
  sqlGenerateUUID(): string;
42
44
  sqlFieldReference(alias: string, fieldName: string, _fieldType: string, _isNested: boolean, _isArray: boolean): string;
43
45
  sqlUnnestPipelineHead(isSingleton: boolean, sourceSQLExpression: string): string;
@@ -65,7 +65,7 @@ class TrinoDialect extends dialect_1.Dialect {
65
65
  this.udfPrefix = '__udf';
66
66
  this.hasFinalStage = false;
67
67
  this.divisionIsInteger = true;
68
- this.supportsSumDistinctFunction = false;
68
+ this.supportsSumDistinctFunction = true;
69
69
  this.unnestWithNumbers = false;
70
70
  this.defaultSampling = { enable: false };
71
71
  this.supportUnnestArrayAgg = false;
@@ -265,10 +265,22 @@ class TrinoDialect extends dialect_1.Dialect {
265
265
  }
266
266
  sqlSumDistinctHashedKey(sqlDistinctKey) {
267
267
  sqlDistinctKey = `CAST(${sqlDistinctKey} AS VARCHAR)`;
268
- const upperPart = `cast(from_base(substr(to_hex(md5(to_utf8(${sqlDistinctKey}))), 1, 15),16) as DECIMAL) * DECIMAL '4294967296' `;
269
- const lowerPart = `cast(from_base(substr(to_hex(md5(to_utf8(${sqlDistinctKey}))), 16, 8),16) as DECIMAL) `;
270
- const precisionShiftMultiplier = '0.000000001';
271
- return `(${upperPart} + ${lowerPart}) * ${precisionShiftMultiplier}`;
268
+ const upperPart = `cast(from_base(substr(to_hex(md5(to_utf8(${sqlDistinctKey}))), 1, 15),16) as ${TrinoDialect.dtype}) * CAST('4294967296' AS ${TrinoDialect.dtype}) `;
269
+ const lowerPart = `cast(from_base(substr(to_hex(md5(to_utf8(${sqlDistinctKey}))), 16, 8),16) as ${TrinoDialect.dtype}) `;
270
+ return `(${upperPart} + ${lowerPart})`;
271
+ }
272
+ sqlSumDistinct(key, value, funcName) {
273
+ const hashKey = this.sqlSumDistinctHashedKey(key);
274
+ const scale = 100000000;
275
+ const v = `CAST(COALESCE(${value},0)*${scale} as ${TrinoDialect.dtype})`;
276
+ const sqlSum = `CAST(SUM(DISTINCT ${hashKey} + ${v}) - SUM(DISTINCT ${hashKey}) AS DOUBLE)/${scale}`;
277
+ if (funcName === 'SUM') {
278
+ return sqlSum;
279
+ }
280
+ else if (funcName === 'AVG') {
281
+ return `(${sqlSum})/NULLIF(COUNT(DISTINCT CASE WHEN ${value} IS NOT NULL THEN ${key} END),0)`;
282
+ }
283
+ throw new Error(`Unknown Symmetric Aggregate function ${funcName}`);
272
284
  }
273
285
  sqlGenerateUUID() {
274
286
  return 'UUID()';
@@ -494,6 +506,7 @@ ${(0, utils_1.indent)(sql)}
494
506
  }
495
507
  }
496
508
  exports.TrinoDialect = TrinoDialect;
509
+ TrinoDialect.dtype = 'DECIMAL(38,0)';
497
510
  class PrestoDialect extends TrinoDialect {
498
511
  constructor() {
499
512
  super(...arguments);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.153-dev240719133924",
3
+ "version": "0.0.153",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",