@malloydata/malloy 0.0.32-dev230427172247 → 0.0.32-dev230505153727

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.
@@ -59,7 +59,7 @@ export declare abstract class Dialect {
59
59
  abstract sqlRegexpMatch(expr: Expr, regex: string): Expr;
60
60
  getFunctionInfo(functionName: string): FunctionInfo | undefined;
61
61
  dialectExpr(df: DialectFragment): Expr;
62
- sqlSumDistinct(_key: string, _value: string): string;
62
+ sqlSumDistinct(_key: string, _value: string, _funcName: string): string;
63
63
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
64
64
  sqlOrderBy(orderTerms: string[]): string;
65
65
  }
@@ -68,7 +68,7 @@ class Dialect {
68
68
  return [this.sqlLiteralString(df.literal)];
69
69
  }
70
70
  }
71
- sqlSumDistinct(_key, _value) {
71
+ sqlSumDistinct(_key, _value, _funcName) {
72
72
  return 'sqlSumDistinct called bu not implemented';
73
73
  }
74
74
  sqlSampleTable(tableSQL, sample) {
@@ -45,7 +45,7 @@ export declare class DuckDBDialect extends Dialect {
45
45
  sqlCast(cast: TypecastFragment): Expr;
46
46
  sqlRegexpMatch(expr: Expr, regexp: string): Expr;
47
47
  sqlLiteralTime(timeString: string, type: TimeFieldType, _timezone: string): string;
48
- sqlSumDistinct(key: string, value: string): string;
48
+ sqlSumDistinct(key: string, value: string, funcName: string): string;
49
49
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
50
50
  sqlOrderBy(orderTerms: string[]): string;
51
51
  sqlLiteralString(literal: string): string;
@@ -321,10 +321,10 @@ class DuckDBDialect extends dialect_1.Dialect {
321
321
  throw new Error(`Unknown Literal time format ${type}`);
322
322
  }
323
323
  }
324
- sqlSumDistinct(key, value) {
324
+ sqlSumDistinct(key, value, funcName) {
325
325
  // return `sum_distinct(list({key:${key}, val: ${value}}))`;
326
326
  return `(
327
- SELECT sum(a.val) as value
327
+ SELECT ${funcName}(a.val) as value
328
328
  FROM (
329
329
  SELECT UNNEST(list(distinct {key:${key}, val: ${value}})) a
330
330
  )
@@ -45,7 +45,7 @@ export declare class PostgresDialect extends Dialect {
45
45
  sqlLiteralTime(timeString: string, type: TimeFieldType, _timezone: string): string;
46
46
  getFunctionInfo(functionName: string): FunctionInfo | undefined;
47
47
  sqlMeasureTime(from: TimeValue, to: TimeValue, units: string): Expr;
48
- sqlSumDistinct(key: string, value: string): string;
48
+ sqlSumDistinct(key: string, value: string, funcName: string): string;
49
49
  sqlSampleTable(tableSQL: string, sample: Sampling | undefined): string;
50
50
  sqlOrderBy(orderTerms: string[]): string;
51
51
  sqlLiteralString(literal: string): string;
@@ -296,10 +296,10 @@ class PostgresDialect extends dialect_1.Dialect {
296
296
  }
297
297
  throw new Error(`Unknown or unhandled postgres time unit: ${units}`);
298
298
  }
299
- sqlSumDistinct(key, value) {
299
+ sqlSumDistinct(key, value, funcName) {
300
300
  // return `sum_distinct(list({key:${key}, val: ${value}}))`;
301
301
  return `(
302
- SELECT sum((a::json->>'f2')::DOUBLE PRECISION) as value
302
+ SELECT ${funcName}((a::json->>'f2')::DOUBLE PRECISION) as value
303
303
  FROM (
304
304
  SELECT UNNEST(array_agg(distinct row_to_json(row(${key},${value}))::text)) a
305
305
  ) a
@@ -27,7 +27,7 @@ const utils_1 = require("../model/utils");
27
27
  const malloy_types_1 = require("../model/malloy_types");
28
28
  const dialect_1 = require("./dialect");
29
29
  const castMap = {
30
- number: 'float64',
30
+ 'number': 'float64',
31
31
  };
32
32
  // These are the units that "TIMESTAMP_ADD" accepts
33
33
  const timestampAddUnits = [
@@ -39,8 +39,8 @@ const timestampAddUnits = [
39
39
  'day',
40
40
  ];
41
41
  const extractMap = {
42
- day_of_week: 'dayofweek',
43
- day_of_year: 'dayofyear',
42
+ 'day_of_week': 'dayofweek',
43
+ 'day_of_year': 'dayofyear',
44
44
  };
45
45
  class StandardSQLDialect extends dialect_1.Dialect {
46
46
  constructor() {
@@ -60,10 +60,10 @@ class StandardSQLDialect extends dialect_1.Dialect {
60
60
  this.supportsQualify = true;
61
61
  // I think we want an optional list of parameters types that we force a cast to.
62
62
  this.functionInfo = {
63
- timestamp_seconds: {
63
+ 'timestamp_seconds': {
64
64
  returnType: 'timestamp',
65
65
  },
66
- concat: { returnType: 'string' },
66
+ 'concat': { returnType: 'string' },
67
67
  };
68
68
  this.keywords = `
69
69
  ALL
@@ -105,13 +105,13 @@ class ExprTimeExtract extends expression_def_1.ExpressionDef {
105
105
  }
106
106
  exports.ExprTimeExtract = ExprTimeExtract;
107
107
  ExprTimeExtract.pluralMap = {
108
- years: 'year',
109
- quarters: 'quarter',
110
- months: 'month',
111
- weeks: 'week',
112
- days: 'day',
113
- hours: 'hour',
114
- minutes: 'minute',
115
- seconds: 'second',
108
+ 'years': 'year',
109
+ 'quarters': 'quarter',
110
+ 'months': 'month',
111
+ 'weeks': 'week',
112
+ 'days': 'day',
113
+ 'hours': 'hour',
114
+ 'minutes': 'minute',
115
+ 'seconds': 'second',
116
116
  };
117
117
  //# sourceMappingURL=expr-time-extract.js.map
@@ -35,7 +35,7 @@ function pretty(thing) {
35
35
  }
36
36
  exports.pretty = pretty;
37
37
  const mockSchema = {
38
- aTable: {
38
+ 'aTable': {
39
39
  type: 'struct',
40
40
  name: 'aTable',
41
41
  dialect: 'standardsql',
@@ -290,7 +290,7 @@ class QueryField extends QueryNode {
290
290
  let ret;
291
291
  if (distinctKeySQL) {
292
292
  if (this.parent.dialect.supportsSumDistinctFunction) {
293
- ret = this.parent.dialect.sqlSumDistinct(distinctKeySQL, dimSQL);
293
+ ret = this.parent.dialect.sqlSumDistinct(distinctKeySQL, dimSQL, 'SUM');
294
294
  }
295
295
  else {
296
296
  ret = sqlSumDistinct(this.parent.dialect, dimSQL, distinctKeySQL);
@@ -318,13 +318,15 @@ class QueryField extends QueryNode {
318
318
  countDistinctKeySQL = `CASE WHEN ${state.whereSQL} THEN ${distinctKeySQL} END`;
319
319
  }
320
320
  let sumDistinctSQL;
321
+ let avgDistinctSQL;
321
322
  if (this.parent.dialect.supportsSumDistinctFunction) {
322
- sumDistinctSQL = this.parent.dialect.sqlSumDistinct(distinctKeySQL, dimSQL);
323
+ avgDistinctSQL = this.parent.dialect.sqlSumDistinct(distinctKeySQL, dimSQL, 'AVG');
323
324
  }
324
325
  else {
325
326
  sumDistinctSQL = sqlSumDistinct(this.parent.dialect, dimSQL, distinctKeySQL);
327
+ avgDistinctSQL = `(${sumDistinctSQL})/NULLIF(COUNT(DISTINCT CASE WHEN ${dimSQL} IS NOT NULL THEN ${countDistinctKeySQL} END),0)`;
326
328
  }
327
- return `(${sumDistinctSQL})/NULLIF(COUNT(DISTINCT ${countDistinctKeySQL}),0)`;
329
+ return avgDistinctSQL;
328
330
  }
329
331
  else {
330
332
  return `AVG(${dimSQL})`;
@@ -1667,7 +1669,7 @@ class QueryQuery extends QueryField {
1667
1669
  let structSQL = qs.structSourceSQL(stageWriter);
1668
1670
  if ((0, malloy_types_1.isJoinOn)(structRelationship)) {
1669
1671
  if (ji.makeUniqueKey) {
1670
- structSQL = `(SELECT ${qs.dialect.sqlGenerateUUID()} as __distinct_key, * FROM ${structSQL})`;
1672
+ structSQL = `(SELECT ${qs.dialect.sqlGenerateUUID()} as __distinct_key, * FROM ${structSQL} as x)`;
1671
1673
  }
1672
1674
  let onCondition = '';
1673
1675
  if (qs.parent === undefined) {
@@ -67,51 +67,51 @@ function isUngroupFragment(f) {
67
67
  }
68
68
  exports.isUngroupFragment = isUngroupFragment;
69
69
  exports.malloyFunctions = {
70
- row_number: {
70
+ 'row_number': {
71
71
  returnType: 'number',
72
72
  parameters: 'none',
73
73
  expressionType: 'analytic',
74
74
  },
75
- rank: {
75
+ 'rank': {
76
76
  returnType: 'number',
77
77
  parameters: 'none',
78
78
  expressionType: 'analytic',
79
79
  },
80
- dense_rank: {
80
+ 'dense_rank': {
81
81
  returnType: 'number',
82
82
  parameters: 'none',
83
83
  expressionType: 'analytic',
84
84
  },
85
- first_value_in_column: {
85
+ 'first_value_in_column': {
86
86
  returnType: 'number',
87
87
  parameters: 'any',
88
88
  expressionType: 'analytic',
89
89
  sqlName: 'first_value',
90
90
  },
91
- last_value_in_column: {
91
+ 'last_value_in_column': {
92
92
  returnType: 'number',
93
93
  parameters: 'any',
94
94
  expressionType: 'analytic',
95
95
  sqlName: 'last_value',
96
96
  },
97
- min_in_column: {
97
+ 'min_in_column': {
98
98
  returnType: 'number',
99
99
  parameters: 'any',
100
100
  expressionType: 'analytic',
101
101
  sqlName: 'min',
102
102
  },
103
- max_in_column: {
103
+ 'max_in_column': {
104
104
  returnType: 'number',
105
105
  parameters: 'any',
106
106
  expressionType: 'analytic',
107
107
  sqlName: 'max',
108
108
  },
109
- ntile: {
109
+ 'ntile': {
110
110
  returnType: 'number',
111
111
  parameters: ['nconst'],
112
112
  expressionType: 'analytic',
113
113
  },
114
- lag: {
114
+ 'lag': {
115
115
  returnType: 'number',
116
116
  parameters: ['number', 'nconst'],
117
117
  expressionType: 'analytic',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/malloy",
3
- "version": "0.0.32-dev230427172247",
3
+ "version": "0.0.32-dev230505153727",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",