@malloydata/malloy 0.0.22-dev230126015148 → 0.0.22-dev230206094309

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 (93) hide show
  1. package/package.json +2 -2
  2. package/dist/connection_utils.d.ts +0 -1
  3. package/dist/connection_utils.js +0 -66
  4. package/dist/dialect/dialect.d.ts +0 -66
  5. package/dist/dialect/dialect.js +0 -85
  6. package/dist/dialect/dialect_map.d.ts +0 -3
  7. package/dist/dialect/dialect_map.js +0 -45
  8. package/dist/dialect/duckdb.d.ts +0 -52
  9. package/dist/dialect/duckdb.js +0 -364
  10. package/dist/dialect/index.d.ts +0 -6
  11. package/dist/dialect/index.js +0 -37
  12. package/dist/dialect/postgres.d.ts +0 -52
  13. package/dist/dialect/postgres.js +0 -330
  14. package/dist/dialect/standardsql.d.ts +0 -49
  15. package/dist/dialect/standardsql.js +0 -381
  16. package/dist/index.d.ts +0 -10
  17. package/dist/index.js +0 -59
  18. package/dist/lang/ast/apply-expr.d.ts +0 -3
  19. package/dist/lang/ast/apply-expr.js +0 -34
  20. package/dist/lang/ast/ast-expr.d.ts +0 -206
  21. package/dist/lang/ast/ast-expr.js +0 -781
  22. package/dist/lang/ast/ast-main.d.ts +0 -915
  23. package/dist/lang/ast/ast-main.js +0 -3672
  24. package/dist/lang/ast/ast-time-expr.d.ts +0 -69
  25. package/dist/lang/ast/ast-time-expr.js +0 -389
  26. package/dist/lang/ast/ast-types.d.ts +0 -83
  27. package/dist/lang/ast/ast-types.js +0 -225
  28. package/dist/lang/ast/index.d.ts +0 -5
  29. package/dist/lang/ast/index.js +0 -44
  30. package/dist/lang/ast/time-utils.d.ts +0 -9
  31. package/dist/lang/ast/time-utils.js +0 -104
  32. package/dist/lang/field-utils.d.ts +0 -5
  33. package/dist/lang/field-utils.js +0 -43
  34. package/dist/lang/index.d.ts +0 -9
  35. package/dist/lang/index.js +0 -32
  36. package/dist/lang/lib/Malloy/MalloyLexer.d.ts +0 -165
  37. package/dist/lang/lib/Malloy/MalloyLexer.js +0 -1232
  38. package/dist/lang/lib/Malloy/MalloyParser.d.ts +0 -1947
  39. package/dist/lang/lib/Malloy/MalloyParser.js +0 -11909
  40. package/dist/lang/lib/Malloy/MalloyParserListener.d.ts +0 -2022
  41. package/dist/lang/lib/Malloy/MalloyParserListener.js +0 -4
  42. package/dist/lang/lib/Malloy/MalloyParserVisitor.d.ts +0 -1274
  43. package/dist/lang/lib/Malloy/MalloyParserVisitor.js +0 -4
  44. package/dist/lang/parse-log.d.ts +0 -27
  45. package/dist/lang/parse-log.js +0 -51
  46. package/dist/lang/parse-malloy.d.ts +0 -236
  47. package/dist/lang/parse-malloy.js +0 -746
  48. package/dist/lang/parse-to-ast.d.ts +0 -155
  49. package/dist/lang/parse-to-ast.js +0 -990
  50. package/dist/lang/parse-tree-walkers/document-completion-walker.d.ts +0 -10
  51. package/dist/lang/parse-tree-walkers/document-completion-walker.js +0 -148
  52. package/dist/lang/parse-tree-walkers/document-help-context-walker.d.ts +0 -9
  53. package/dist/lang/parse-tree-walkers/document-help-context-walker.js +0 -107
  54. package/dist/lang/parse-tree-walkers/document-highlight-walker.d.ts +0 -83
  55. package/dist/lang/parse-tree-walkers/document-highlight-walker.js +0 -385
  56. package/dist/lang/parse-tree-walkers/document-symbol-walker.d.ts +0 -11
  57. package/dist/lang/parse-tree-walkers/document-symbol-walker.js +0 -182
  58. package/dist/lang/parse-tree-walkers/explore-query-walker.d.ts +0 -34
  59. package/dist/lang/parse-tree-walkers/explore-query-walker.js +0 -74
  60. package/dist/lang/parse-tree-walkers/find-external-references.d.ts +0 -11
  61. package/dist/lang/parse-tree-walkers/find-external-references.js +0 -61
  62. package/dist/lang/reference-list.d.ts +0 -10
  63. package/dist/lang/reference-list.js +0 -90
  64. package/dist/lang/space-field.d.ts +0 -17
  65. package/dist/lang/space-field.js +0 -127
  66. package/dist/lang/test/document-help-context-walker.spec.d.ts +0 -1
  67. package/dist/lang/test/document-help-context-walker.spec.js +0 -55
  68. package/dist/lang/test/document-symbol-walker.spec.d.ts +0 -1
  69. package/dist/lang/test/document-symbol-walker.spec.js +0 -110
  70. package/dist/lang/test/field-symbols.spec.d.ts +0 -1
  71. package/dist/lang/test/field-symbols.spec.js +0 -180
  72. package/dist/lang/test/parse.spec.d.ts +0 -14
  73. package/dist/lang/test/parse.spec.js +0 -2134
  74. package/dist/lang/test/test-translator.d.ts +0 -38
  75. package/dist/lang/test/test-translator.js +0 -344
  76. package/dist/lang/zone.d.ts +0 -57
  77. package/dist/lang/zone.js +0 -107
  78. package/dist/malloy.d.ts +0 -1146
  79. package/dist/malloy.js +0 -2413
  80. package/dist/model/index.d.ts +0 -2
  81. package/dist/model/index.js +0 -45
  82. package/dist/model/malloy_query.d.ts +0 -351
  83. package/dist/model/malloy_query.js +0 -3174
  84. package/dist/model/malloy_types.d.ts +0 -551
  85. package/dist/model/malloy_types.js +0 -378
  86. package/dist/model/sql_block.d.ts +0 -7
  87. package/dist/model/sql_block.js +0 -56
  88. package/dist/model/utils.d.ts +0 -22
  89. package/dist/model/utils.js +0 -102
  90. package/dist/run_sql_options.d.ts +0 -3
  91. package/dist/run_sql_options.js +0 -25
  92. package/dist/runtime_types.d.ts +0 -105
  93. package/dist/runtime_types.js +0 -25
@@ -1,781 +0,0 @@
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.Apply = exports.Pick = exports.PickWhen = exports.TopBy = exports.ExprCast = exports.isCastType = exports.ExprFilter = exports.ExprCase = exports.WhenClause = exports.ExprUngroup = exports.ExprSum = exports.ExprAvg = exports.ExprCount = exports.ExprCountDistinct = exports.ExprMax = exports.ExprMin = exports.ExprAlternationTree = exports.ExprMulDiv = exports.ExprAddSub = exports.BinaryNumeric = exports.ExprMinus = exports.ExprParens = exports.ExprNULL = exports.ExprIdReference = exports.ExprLogicalOp = exports.Boolean = exports.ExprNot = exports.ExprRegEx = exports.ExprNumber = exports.ExprString = void 0;
26
- /*
27
- ** This file is the general framework for expression nodes and expression
28
- ** evaluation.
29
- */
30
- const malloy_types_1 = require("../../model/malloy_types");
31
- const ast_main_1 = require("./ast-main");
32
- const ast_types_1 = require("./ast-types");
33
- const apply_expr_1 = require("./apply-expr");
34
- const time_utils_1 = require("./time-utils");
35
- class ExprString extends ast_main_1.ExpressionDef {
36
- constructor(src) {
37
- super();
38
- this.elementType = "string literal";
39
- const bareStr = src.slice(1, -1);
40
- const val = bareStr.replace(/\\(.)/g, "$1");
41
- this.value = val;
42
- }
43
- getExpression(_fs) {
44
- return {
45
- ...ast_types_1.FT.stringT,
46
- value: [
47
- {
48
- type: "dialect",
49
- function: "stringLiteral",
50
- literal: this.value,
51
- },
52
- ],
53
- };
54
- }
55
- }
56
- exports.ExprString = ExprString;
57
- class ExprNumber extends ast_main_1.ExpressionDef {
58
- constructor(n) {
59
- super();
60
- this.n = n;
61
- this.elementType = "numeric literal";
62
- }
63
- getExpression(_fs) {
64
- return this.constantExpression();
65
- }
66
- constantExpression() {
67
- return { ...ast_types_1.FT.numberT, value: [this.n] };
68
- }
69
- }
70
- exports.ExprNumber = ExprNumber;
71
- class ExprRegEx extends ast_main_1.ExpressionDef {
72
- constructor(regex) {
73
- super();
74
- this.regex = regex;
75
- this.elementType = "regular expression literal";
76
- }
77
- getExpression() {
78
- return {
79
- dataType: "regular expression",
80
- expressionType: "scalar",
81
- value: [`r'${this.regex}'`],
82
- };
83
- }
84
- }
85
- exports.ExprRegEx = ExprRegEx;
86
- class Unary extends ast_main_1.ExpressionDef {
87
- constructor(expr) {
88
- super({ expr });
89
- this.expr = expr;
90
- }
91
- }
92
- class ExprNot extends Unary {
93
- constructor(expr) {
94
- super(expr);
95
- this.elementType = "not";
96
- this.legalChildTypes = [ast_types_1.FT.boolT, ast_types_1.FT.nullT];
97
- }
98
- getExpression(fs) {
99
- const notThis = this.expr.getExpression(fs);
100
- if (this.typeCheck(this.expr, notThis)) {
101
- return {
102
- ...notThis,
103
- dataType: "boolean",
104
- value: (0, apply_expr_1.nullsafeNot)(notThis.value),
105
- };
106
- }
107
- return (0, ast_types_1.errorFor)("not requires boolean");
108
- }
109
- }
110
- exports.ExprNot = ExprNot;
111
- class Boolean extends ast_main_1.ExpressionDef {
112
- constructor(value) {
113
- super();
114
- this.value = value;
115
- this.elementType = "boolean literal";
116
- }
117
- getExpression() {
118
- return { ...ast_types_1.FT.boolT, value: [this.value] };
119
- }
120
- }
121
- exports.Boolean = Boolean;
122
- class ExprLogicalOp extends ast_main_1.BinaryBoolean {
123
- constructor() {
124
- super(...arguments);
125
- this.elementType = "logical operator";
126
- this.legalChildTypes = [ast_types_1.FT.boolT, { ...ast_types_1.FT.boolT, aggregate: true }];
127
- }
128
- }
129
- exports.ExprLogicalOp = ExprLogicalOp;
130
- class ExprIdReference extends ast_main_1.ExpressionDef {
131
- constructor(fieldReference) {
132
- super();
133
- this.fieldReference = fieldReference;
134
- this.elementType = "ExpressionIdReference";
135
- this.has({ fieldPath: fieldReference });
136
- }
137
- get refString() {
138
- return this.fieldReference.refString;
139
- }
140
- getExpression(fs) {
141
- const def = this.fieldReference.getField(fs);
142
- if (def.found) {
143
- // TODO if type is a query or a struct this should fail nicely
144
- const typeMixin = def.found.type();
145
- const dataType = typeMixin.type;
146
- const expressionType = typeMixin.expressionType || "scalar";
147
- const value = [{ type: def.found.refType, path: this.refString }];
148
- return { dataType, expressionType, value };
149
- }
150
- this.log(def.error);
151
- return (0, ast_types_1.errorFor)(def.error);
152
- }
153
- apply(fs, op, expr) {
154
- const entry = this.fieldReference.getField(fs).found;
155
- if (entry instanceof ast_main_1.SpaceParam) {
156
- const cParam = entry.parameter();
157
- if ((0, malloy_types_1.isConditionParameter)(cParam)) {
158
- const lval = expr.getExpression(fs);
159
- return {
160
- dataType: "boolean",
161
- expressionType: lval.expressionType,
162
- value: [
163
- {
164
- type: "apply",
165
- value: lval.value,
166
- to: [{ type: "parameter", path: this.refString }],
167
- },
168
- ],
169
- };
170
- }
171
- }
172
- return super.apply(fs, op, expr);
173
- }
174
- }
175
- exports.ExprIdReference = ExprIdReference;
176
- class ExprNULL extends ast_main_1.ExpressionDef {
177
- constructor() {
178
- super(...arguments);
179
- this.elementType = "NULL";
180
- }
181
- getExpression() {
182
- return {
183
- dataType: "null",
184
- value: ["NULL"],
185
- expressionType: "scalar",
186
- };
187
- }
188
- }
189
- exports.ExprNULL = ExprNULL;
190
- class ExprParens extends ast_main_1.ExpressionDef {
191
- constructor(expr) {
192
- super({ expr });
193
- this.expr = expr;
194
- this.elementType = "(expression)";
195
- }
196
- requestExpression(fs) {
197
- return this.expr.requestExpression(fs);
198
- }
199
- getExpression(fs) {
200
- const subExpr = this.expr.getExpression(fs);
201
- return { ...subExpr, value: ["(", ...subExpr.value, ")"] };
202
- }
203
- }
204
- exports.ExprParens = ExprParens;
205
- class ExprMinus extends ast_main_1.ExpressionDef {
206
- constructor(expr) {
207
- super({ expr });
208
- this.expr = expr;
209
- this.elementType = "unary minus";
210
- this.legalChildTypes = [ast_types_1.FT.numberT];
211
- }
212
- getExpression(fs) {
213
- const expr = this.expr.getExpression(fs);
214
- if (this.typeCheck(this.expr, expr)) {
215
- if (expr.value.length > 1) {
216
- return { ...expr, value: ["-(", ...expr.value, ")"] };
217
- }
218
- return { ...expr, value: ["-", ...expr.value] };
219
- }
220
- return (0, ast_types_1.errorFor)("negate requires number");
221
- }
222
- }
223
- exports.ExprMinus = ExprMinus;
224
- class BinaryNumeric extends ast_main_1.ExpressionDef {
225
- constructor(left, op, right) {
226
- super({ left, right });
227
- this.left = left;
228
- this.op = op;
229
- this.right = right;
230
- this.elementType = "numeric binary abstract";
231
- this.legalChildTypes = [ast_types_1.FT.numberT];
232
- }
233
- getExpression(fs) {
234
- return this.right.apply(fs, this.op, this.left);
235
- }
236
- }
237
- exports.BinaryNumeric = BinaryNumeric;
238
- class ExprAddSub extends BinaryNumeric {
239
- constructor() {
240
- super(...arguments);
241
- this.elementType = "+-";
242
- }
243
- }
244
- exports.ExprAddSub = ExprAddSub;
245
- class ExprMulDiv extends BinaryNumeric {
246
- constructor() {
247
- super(...arguments);
248
- this.elementType = "*/%";
249
- }
250
- }
251
- exports.ExprMulDiv = ExprMulDiv;
252
- class ExprAlternationTree extends ast_main_1.BinaryBoolean {
253
- constructor(left, op, right) {
254
- super(left, op, right);
255
- this.elementType = "alternation";
256
- this.elementType = `${op}alternation${op}`;
257
- }
258
- apply(fs, applyOp, expr) {
259
- const choice1 = this.left.apply(fs, applyOp, expr);
260
- const choice2 = this.right.apply(fs, applyOp, expr);
261
- return {
262
- dataType: "boolean",
263
- expressionType: (0, malloy_types_1.maxExpressionType)(choice1.expressionType, choice2.expressionType),
264
- value: (0, ast_types_1.compose)(choice1.value, this.op === "&" ? "and" : "or", choice2.value),
265
- };
266
- }
267
- requestExpression(_fs) {
268
- return undefined;
269
- }
270
- getExpression(_fs) {
271
- this.log(`Alternation tree has no value`);
272
- return (0, ast_types_1.errorFor)("no value from alternation tree");
273
- }
274
- }
275
- exports.ExprAlternationTree = ExprAlternationTree;
276
- class ExprAggregateFunction extends ast_main_1.ExpressionDef {
277
- constructor(func, expr) {
278
- super();
279
- this.func = func;
280
- this.legalChildTypes = [ast_types_1.FT.numberT];
281
- this.elementType = func;
282
- if (expr) {
283
- this.expr = expr;
284
- this.has({ expr });
285
- }
286
- }
287
- returns(_forExpression) {
288
- return "number";
289
- }
290
- getExpression(fs) {
291
- var _a, _b;
292
- let exprVal = (_a = this.expr) === null || _a === void 0 ? void 0 : _a.getExpression(fs);
293
- let structPath = (_b = this.source) === null || _b === void 0 ? void 0 : _b.refString;
294
- if (this.source) {
295
- const sourceFoot = this.source.getField(fs).found;
296
- if (sourceFoot) {
297
- const footType = sourceFoot.type();
298
- if ((0, malloy_types_1.isAtomicFieldType)(footType.type)) {
299
- exprVal = {
300
- dataType: footType.type,
301
- expressionType: footType.expressionType || "scalar",
302
- value: [{ type: "field", path: this.source.refString }],
303
- };
304
- structPath = this.source.sourceString;
305
- }
306
- else {
307
- if (!(sourceFoot instanceof ast_main_1.StructSpaceField)) {
308
- this.log(`Aggregate source cannot be a ${footType.type}`);
309
- return (0, ast_types_1.errorFor)(`Aggregate source cannot be a ${footType.type}`);
310
- }
311
- }
312
- }
313
- else {
314
- this.log(`Reference to undefined value ${this.source.refString}`);
315
- return (0, ast_types_1.errorFor)(`Reference to undefined value ${this.source.refString}`);
316
- }
317
- }
318
- if (exprVal === undefined) {
319
- this.log("Missing expression for aggregate function");
320
- return (0, ast_types_1.errorFor)("agggregate without expression");
321
- }
322
- if (this.typeCheck(this.expr || this, {
323
- ...exprVal,
324
- expressionType: "scalar",
325
- })) {
326
- const f = {
327
- type: "aggregate",
328
- function: this.func,
329
- e: exprVal.value,
330
- };
331
- if (structPath) {
332
- f.structPath = structPath;
333
- }
334
- return {
335
- dataType: this.returns(exprVal),
336
- expressionType: "aggregate",
337
- value: [f],
338
- };
339
- }
340
- return (0, ast_types_1.errorFor)("aggregate type check");
341
- }
342
- }
343
- class ExprMin extends ExprAggregateFunction {
344
- constructor(expr) {
345
- super("min", expr);
346
- this.legalChildTypes = [ast_types_1.FT.numberT, ast_types_1.FT.stringT, ast_types_1.FT.dateT, ast_types_1.FT.timestampT];
347
- }
348
- returns(forExpression) {
349
- return forExpression.dataType;
350
- }
351
- }
352
- exports.ExprMin = ExprMin;
353
- class ExprMax extends ExprAggregateFunction {
354
- constructor(expr) {
355
- super("max", expr);
356
- this.legalChildTypes = [ast_types_1.FT.numberT, ast_types_1.FT.stringT, ast_types_1.FT.dateT, ast_types_1.FT.timestampT];
357
- }
358
- returns(forExpression) {
359
- return forExpression.dataType;
360
- }
361
- }
362
- exports.ExprMax = ExprMax;
363
- class ExprCountDistinct extends ExprAggregateFunction {
364
- constructor(expr) {
365
- super("count_distinct", expr);
366
- this.legalChildTypes = [ast_types_1.FT.numberT, ast_types_1.FT.stringT, ast_types_1.FT.dateT, ast_types_1.FT.timestampT];
367
- }
368
- }
369
- exports.ExprCountDistinct = ExprCountDistinct;
370
- class ExprAsymmetric extends ExprAggregateFunction {
371
- constructor(func, expr, source) {
372
- super(func, expr);
373
- this.func = func;
374
- this.expr = expr;
375
- this.source = source;
376
- this.has({ source });
377
- }
378
- defaultFieldName() {
379
- if (this.source && this.expr === undefined) {
380
- const tag = this.source.nameString;
381
- switch (this.func) {
382
- case "sum":
383
- return `total_${tag}`;
384
- case "avg":
385
- return `avg_${tag}`;
386
- }
387
- }
388
- return undefined;
389
- }
390
- }
391
- class ExprCount extends ExprAggregateFunction {
392
- constructor(source) {
393
- super("count");
394
- this.source = source;
395
- this.elementType = "count";
396
- this.has({ source });
397
- }
398
- defaultFieldName() {
399
- if (this.source) {
400
- return "count_" + this.source.nameString;
401
- }
402
- return undefined;
403
- }
404
- getExpression(_fs) {
405
- const ret = {
406
- type: "aggregate",
407
- function: "count",
408
- e: [],
409
- };
410
- if (this.source) {
411
- ret.structPath = this.source.refString;
412
- }
413
- return {
414
- dataType: "number",
415
- expressionType: "aggregate",
416
- value: [ret],
417
- };
418
- }
419
- }
420
- exports.ExprCount = ExprCount;
421
- class ExprAvg extends ExprAsymmetric {
422
- constructor(expr, source) {
423
- super("avg", expr, source);
424
- this.has({ source });
425
- }
426
- }
427
- exports.ExprAvg = ExprAvg;
428
- class ExprSum extends ExprAsymmetric {
429
- constructor(expr, source) {
430
- super("sum", expr, source);
431
- this.has({ source });
432
- }
433
- }
434
- exports.ExprSum = ExprSum;
435
- class ExprUngroup extends ast_main_1.ExpressionDef {
436
- constructor(control, expr, fields) {
437
- super({ expr, fields });
438
- this.control = control;
439
- this.expr = expr;
440
- this.fields = fields;
441
- this.legalChildTypes = ast_types_1.FT.anyAtomicT;
442
- this.elementType = "ungroup";
443
- }
444
- returns(_forExpression) {
445
- return "number";
446
- }
447
- getExpression(fs) {
448
- const exprVal = this.expr.getExpression(fs);
449
- if (!(0, malloy_types_1.expressionIsAggregate)(exprVal.expressionType)) {
450
- this.expr.log(`${this.control}() expression must be an aggregate`);
451
- return (0, ast_types_1.errorFor)("ungrouped scalar");
452
- }
453
- const ungroup = { type: this.control, e: exprVal.value };
454
- if (this.typeCheck(this.expr, { ...exprVal, expressionType: "scalar" })) {
455
- if (this.fields.length > 0) {
456
- let qs = fs;
457
- if (fs instanceof ast_main_1.DefSpace) {
458
- qs = fs.realFS;
459
- }
460
- if (!(qs instanceof ast_main_1.QuerySpace)) {
461
- this.log(`${this.control}() must be in a query -- weird internal error`);
462
- return (0, ast_types_1.errorFor)("ungroup query check");
463
- }
464
- const output = qs.result;
465
- const dstFields = [];
466
- const isExclude = this.control == "exclude";
467
- for (const mustBeInOutput of this.fields) {
468
- output.whenComplete(() => {
469
- output.checkUngroup(mustBeInOutput, isExclude);
470
- });
471
- dstFields.push(mustBeInOutput.refString);
472
- }
473
- ungroup.fields = dstFields;
474
- }
475
- return {
476
- dataType: this.returns(exprVal),
477
- expressionType: "analytic",
478
- value: [ungroup],
479
- };
480
- }
481
- this.log(`${this.control}() incompatible type`);
482
- return (0, ast_types_1.errorFor)("ungrouped type check");
483
- }
484
- }
485
- exports.ExprUngroup = ExprUngroup;
486
- class WhenClause extends ast_main_1.ExpressionDef {
487
- constructor(whenThis, thenThis) {
488
- super({ whenThis, thenThis });
489
- this.whenThis = whenThis;
490
- this.thenThis = thenThis;
491
- this.elementType = "when clause";
492
- }
493
- getExpression(_fs) {
494
- throw new Error("expression did something unxpected with 'WHEN'");
495
- }
496
- }
497
- exports.WhenClause = WhenClause;
498
- class ExprCase extends ast_main_1.ExpressionDef {
499
- constructor(when, elseClause) {
500
- super({ when });
501
- this.when = when;
502
- this.elseClause = elseClause;
503
- this.elementType = "case statement";
504
- this.has({ elseClause });
505
- }
506
- getExpression(fs) {
507
- let retType;
508
- let expressionType = "scalar";
509
- const caseExpr = ["CASE "];
510
- for (const clause of this.when) {
511
- const whenExpr = clause.whenThis.getExpression(fs);
512
- const thenExpr = clause.thenThis.getExpression(fs);
513
- expressionType = (0, malloy_types_1.maxExpressionType)(expressionType, (0, malloy_types_1.maxExpressionType)(whenExpr.expressionType, thenExpr.expressionType));
514
- if (thenExpr.dataType !== "null") {
515
- if (retType && !ast_types_1.FT.typeEq(retType, thenExpr)) {
516
- this.log(`Mismatched THEN clause types, ${ast_types_1.FT.inspect(retType, thenExpr)}`);
517
- return (0, ast_types_1.errorFor)("then typecheck");
518
- }
519
- else {
520
- retType = thenExpr;
521
- }
522
- }
523
- caseExpr.push("WHEN ", ...whenExpr.value, " THEN ", ...thenExpr.value);
524
- }
525
- if (this.elseClause) {
526
- const elseExpr = this.elseClause.getExpression(fs);
527
- expressionType = (0, malloy_types_1.maxExpressionType)(expressionType, elseExpr.expressionType);
528
- caseExpr.push(" ELSE ", ...elseExpr.value);
529
- if (elseExpr.dataType !== "null") {
530
- if (retType && !ast_types_1.FT.typeEq(retType, elseExpr)) {
531
- this.log(`Mismatched ELSE clause type, ${ast_types_1.FT.inspect(retType, elseExpr)}`);
532
- return (0, ast_types_1.errorFor)("else typecheck");
533
- }
534
- else {
535
- retType = elseExpr;
536
- }
537
- }
538
- }
539
- if (retType === undefined) {
540
- this.log("case statement type not computable");
541
- return (0, ast_types_1.errorFor)("typeless case");
542
- }
543
- caseExpr.push(" END");
544
- return {
545
- dataType: retType.dataType,
546
- expressionType,
547
- value: caseExpr,
548
- };
549
- }
550
- }
551
- exports.ExprCase = ExprCase;
552
- class ExprFilter extends ast_main_1.ExpressionDef {
553
- constructor(expr, filter) {
554
- super({ expr, filter });
555
- this.expr = expr;
556
- this.filter = filter;
557
- this.elementType = "filtered expression";
558
- this.legalChildTypes = ast_types_1.FT.anyAtomicT;
559
- }
560
- getExpression(fs) {
561
- const testList = this.filter.getFilterList(fs);
562
- const resultExpr = this.expr.getExpression(fs);
563
- for (const cond of testList) {
564
- if ((0, malloy_types_1.expressionIsCalculation)(cond.expressionType)) {
565
- this.filter.log("Cannot filter a field with an aggregate or analytical computation");
566
- return (0, ast_types_1.errorFor)("no filter on aggregate");
567
- }
568
- }
569
- if (resultExpr.expressionType === "scalar") {
570
- // TODO could log a warning, but I have a problem with the
571
- // idea of warnings, so for now ...
572
- return resultExpr;
573
- }
574
- if (this.typeCheck(this.expr, { ...resultExpr, expressionType: "scalar" })) {
575
- return {
576
- ...resultExpr,
577
- value: [
578
- {
579
- type: "filterExpression",
580
- e: resultExpr.value,
581
- filterList: testList,
582
- },
583
- ],
584
- };
585
- }
586
- this.expr.log(`Cannot filter '${resultExpr.dataType}' data`);
587
- return (0, ast_types_1.errorFor)("cannot filter type");
588
- }
589
- }
590
- exports.ExprFilter = ExprFilter;
591
- function isCastType(t) {
592
- return ["string", "number", "boolean", "date", "timestamp"].includes(t);
593
- }
594
- exports.isCastType = isCastType;
595
- class ExprCast extends ast_main_1.ExpressionDef {
596
- constructor(expr, castType, safe = false) {
597
- super({ expr });
598
- this.expr = expr;
599
- this.castType = castType;
600
- this.safe = safe;
601
- this.elementType = "cast";
602
- }
603
- getExpression(fs) {
604
- const expr = this.expr.getExpression(fs);
605
- return {
606
- dataType: this.castType,
607
- expressionType: expr.expressionType,
608
- value: (0, ast_types_1.compressExpr)((0, time_utils_1.castTo)(this.castType, expr.value, this.safe)),
609
- };
610
- }
611
- }
612
- exports.ExprCast = ExprCast;
613
- class TopBy extends ast_main_1.MalloyElement {
614
- constructor(by) {
615
- super();
616
- this.by = by;
617
- this.elementType = "topBy";
618
- if (by instanceof ast_main_1.ExpressionDef) {
619
- this.has({ by });
620
- }
621
- }
622
- getBy(fs) {
623
- if (this.by instanceof ast_main_1.ExpressionDef) {
624
- const byExpr = this.by.getExpression(fs);
625
- if (!(0, malloy_types_1.expressionIsAggregate)(byExpr.expressionType)) {
626
- this.log("top by expression must be an aggregate");
627
- }
628
- return { by: "expression", e: (0, ast_types_1.compressExpr)(byExpr.value) };
629
- }
630
- return { by: "name", name: this.by };
631
- }
632
- }
633
- exports.TopBy = TopBy;
634
- class PickWhen extends ast_main_1.MalloyElement {
635
- constructor(pick, when) {
636
- super({ when });
637
- this.pick = pick;
638
- this.when = when;
639
- this.elementType = "pickWhen";
640
- this.has({ pick });
641
- }
642
- }
643
- exports.PickWhen = PickWhen;
644
- class Pick extends ast_main_1.ExpressionDef {
645
- constructor(choices, elsePick) {
646
- super({ choices });
647
- this.choices = choices;
648
- this.elsePick = elsePick;
649
- this.elementType = "pick";
650
- this.has({ elsePick });
651
- }
652
- requestExpression(fs) {
653
- // pick statements are sometimes partials which must be applied
654
- // and sometimes have a value.
655
- if (this.elsePick === undefined) {
656
- return undefined;
657
- }
658
- for (const c of this.choices) {
659
- if (c.pick == undefined) {
660
- return undefined;
661
- }
662
- const whenResp = c.when.requestExpression(fs);
663
- if (whenResp == undefined || whenResp.dataType != "boolean") {
664
- // If when is not a boolean, we'll treat it like a partial compare
665
- return undefined;
666
- }
667
- }
668
- return this.getExpression(fs);
669
- }
670
- apply(fs, op, expr) {
671
- const caseValue = ["CASE"];
672
- let returnType;
673
- let anyExpressionType = "scalar";
674
- for (const choice of this.choices) {
675
- const whenExpr = choice.when.apply(fs, "=", expr);
676
- const thenExpr = choice.pick
677
- ? choice.pick.getExpression(fs)
678
- : expr.getExpression(fs);
679
- anyExpressionType = (0, malloy_types_1.maxExpressionType)(anyExpressionType, (0, malloy_types_1.maxExpressionType)(whenExpr.expressionType, thenExpr.expressionType));
680
- if (returnType) {
681
- if (!ast_types_1.FT.typeEq(returnType, thenExpr, true)) {
682
- const whenType = ast_types_1.FT.inspect(thenExpr);
683
- this.log(`pick type '${whenType}', expected '${returnType.dataType}'`);
684
- return (0, ast_types_1.errorFor)("pick when type");
685
- }
686
- }
687
- else {
688
- returnType = thenExpr;
689
- }
690
- caseValue.push(" WHEN ", ...whenExpr.value, " THEN ", ...thenExpr.value);
691
- }
692
- const elsePart = this.elsePick || expr;
693
- const elseVal = elsePart.getExpression(fs);
694
- returnType || (returnType = elseVal);
695
- if (!ast_types_1.FT.typeEq(returnType, elseVal, true)) {
696
- const errSrc = this.elsePick ? "else" : "pick default";
697
- this.log(`${errSrc} type '${ast_types_1.FT.inspect(elseVal)}', expected '${returnType.dataType}'`);
698
- return (0, ast_types_1.errorFor)("pick else type");
699
- }
700
- return {
701
- dataType: returnType.dataType,
702
- expressionType: (0, malloy_types_1.maxExpressionType)(anyExpressionType, elseVal.expressionType),
703
- value: (0, ast_types_1.compressExpr)([...caseValue, " ELSE ", ...elseVal.value, " END"]),
704
- };
705
- }
706
- getExpression(fs) {
707
- if (this.elsePick === undefined) {
708
- this.log("pick incomplete, missing 'else'");
709
- return (0, ast_types_1.errorFor)("no value for partial pick");
710
- }
711
- const choiceValues = [];
712
- for (const c of this.choices) {
713
- if (c.pick === undefined) {
714
- this.log("pick with no value can only be used with apply");
715
- return (0, ast_types_1.errorFor)("no value for partial pick");
716
- }
717
- const pickWhen = c.when.requestExpression(fs);
718
- if (pickWhen === undefined) {
719
- this.log("pick with partial when can only be used with apply");
720
- return (0, ast_types_1.errorFor)("partial when");
721
- }
722
- choiceValues.push({
723
- pick: c.pick.getExpression(fs),
724
- when: c.when.getExpression(fs),
725
- });
726
- }
727
- const returnType = choiceValues[0].pick;
728
- const caseValue = ["CASE"];
729
- let anyExpressionType = returnType.expressionType;
730
- for (const aChoice of choiceValues) {
731
- if (!ast_types_1.FT.typeEq(aChoice.when, ast_types_1.FT.boolT)) {
732
- this.log(`when expression must be boolean, not '${ast_types_1.FT.inspect(aChoice.when)}`);
733
- return (0, ast_types_1.errorFor)("pick when type");
734
- }
735
- if (!ast_types_1.FT.typeEq(returnType, aChoice.pick, true)) {
736
- const whenType = ast_types_1.FT.inspect(aChoice.pick);
737
- this.log(`pick type '${whenType}', expected '${returnType.dataType}'`);
738
- return (0, ast_types_1.errorFor)("pick value type");
739
- }
740
- anyExpressionType = (0, malloy_types_1.maxExpressionType)(anyExpressionType, (0, malloy_types_1.maxExpressionType)(aChoice.pick.expressionType, aChoice.when.expressionType));
741
- caseValue.push(" WHEN ", ...aChoice.when.value, " THEN ", ...aChoice.pick.value);
742
- }
743
- const defVal = this.elsePick.getExpression(fs);
744
- anyExpressionType = (0, malloy_types_1.maxExpressionType)(anyExpressionType, defVal.expressionType);
745
- if (!ast_types_1.FT.typeEq(returnType, defVal, true)) {
746
- this.elsePick.log(`else type '${ast_types_1.FT.inspect(defVal)}', expected '${returnType.dataType}'`);
747
- return (0, ast_types_1.errorFor)("pick value type mismatch");
748
- }
749
- caseValue.push(" ELSE ", ...defVal.value, " END");
750
- return {
751
- dataType: returnType.dataType,
752
- expressionType: anyExpressionType,
753
- value: (0, ast_types_1.compressExpr)(caseValue),
754
- };
755
- }
756
- }
757
- exports.Pick = Pick;
758
- function nullCompare(left, op, right) {
759
- const not = op === "!=" || op === "!~";
760
- if (left.dataType === "null" || right.dataType === "null") {
761
- const maybeNot = not ? " NOT" : "";
762
- if (left.dataType !== "null") {
763
- return [...left.value, ` IS${maybeNot} NULL`];
764
- }
765
- if (right.dataType !== "null") {
766
- return [...right.value, `IS${maybeNot} NULL`];
767
- }
768
- return [not ? "false" : "true"];
769
- }
770
- return undefined;
771
- }
772
- class Apply extends ast_main_1.ExprCompare {
773
- constructor(left, right) {
774
- super(left, "=", right);
775
- this.left = left;
776
- this.right = right;
777
- this.elementType = "apply";
778
- }
779
- }
780
- exports.Apply = Apply;
781
- //# sourceMappingURL=ast-expr.js.map