@malloydata/malloy 0.0.122 → 0.0.123-dev240205152128
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.
- package/dist/dialect/dialect_map.js +2 -0
- package/dist/dialect/functions/string_agg.js +12 -4
- package/dist/dialect/functions/util.d.ts +4 -2
- package/dist/dialect/functions/util.js +1 -0
- package/dist/dialect/postgres/functions/string_agg.js +10 -2
- package/dist/dialect/standardsql/functions/string_agg.js +14 -4
- package/dist/lang/ast/expressions/expr-func.js +3 -1
- package/dist/lang/ast/expressions/function-ordering.d.ts +4 -4
- package/dist/lang/ast/expressions/function-ordering.js +26 -8
- package/dist/lang/lib/Malloy/MalloyParser.d.ts +1 -1
- package/dist/lang/lib/Malloy/MalloyParser.js +968 -904
- package/dist/lang/malloy-to-ast.js +2 -1
- package/dist/lang/test/expressions.spec.js +36 -0
- package/dist/model/malloy_query.d.ts +5 -2
- package/dist/model/malloy_query.js +96 -25
- package/dist/model/malloy_types.d.ts +9 -2
- package/package.json +1 -1
|
@@ -86,6 +86,7 @@ function getDialectFunction(name) {
|
|
|
86
86
|
existingOverload.dialect[dialect.name] = {
|
|
87
87
|
e: overload.e,
|
|
88
88
|
supportsOrderBy: overload.supportsOrderBy,
|
|
89
|
+
defaultOrderByArgIndex: overload.defaultOrderByArgIndex,
|
|
89
90
|
supportsLimit: overload.supportsLimit,
|
|
90
91
|
};
|
|
91
92
|
handled = true;
|
|
@@ -98,6 +99,7 @@ function getDialectFunction(name) {
|
|
|
98
99
|
[dialect.name]: {
|
|
99
100
|
e: overload.e,
|
|
100
101
|
supportsOrderBy: overload.supportsOrderBy,
|
|
102
|
+
defaultOrderByArgIndex: overload.defaultOrderByArgIndex,
|
|
101
103
|
supportsLimit: overload.supportsLimit,
|
|
102
104
|
},
|
|
103
105
|
},
|
|
@@ -29,8 +29,8 @@ function fnStringAgg() {
|
|
|
29
29
|
const separator = (0, util_1.makeParam)('separator', (0, util_1.literal)((0, util_1.maxScalar)('string')));
|
|
30
30
|
const orderBy = { type: 'aggregate_order_by' };
|
|
31
31
|
return [
|
|
32
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(${value.arg}${orderBy})`, { supportsOrderBy: true }),
|
|
33
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(${value.arg}, ${separator.arg}${orderBy})`, { supportsOrderBy: true }),
|
|
32
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(${value.arg}${orderBy})`, { supportsOrderBy: true, defaultOrderByArgIndex: 0 }),
|
|
33
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(${value.arg}, ${separator.arg}${orderBy})`, { supportsOrderBy: true, defaultOrderByArgIndex: 0 }),
|
|
34
34
|
];
|
|
35
35
|
}
|
|
36
36
|
exports.fnStringAgg = fnStringAgg;
|
|
@@ -39,8 +39,16 @@ function fnStringAggDistinct() {
|
|
|
39
39
|
const separator = (0, util_1.makeParam)('separator', (0, util_1.literal)((0, util_1.maxScalar)('string')));
|
|
40
40
|
const orderBy = { type: 'aggregate_order_by' };
|
|
41
41
|
return [
|
|
42
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}${orderBy})`, {
|
|
43
|
-
|
|
42
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}${orderBy})`, {
|
|
43
|
+
isSymmetric: true,
|
|
44
|
+
supportsOrderBy: 'only_default',
|
|
45
|
+
defaultOrderByArgIndex: 0,
|
|
46
|
+
}),
|
|
47
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy})`, {
|
|
48
|
+
isSymmetric: true,
|
|
49
|
+
supportsOrderBy: 'only_default',
|
|
50
|
+
defaultOrderByArgIndex: 0,
|
|
51
|
+
}),
|
|
44
52
|
];
|
|
45
53
|
}
|
|
46
54
|
exports.fnStringAggDistinct = fnStringAggDistinct;
|
|
@@ -5,7 +5,8 @@ export interface DialectFunctionOverloadDef {
|
|
|
5
5
|
e: Expr;
|
|
6
6
|
needsWindowOrderBy?: boolean;
|
|
7
7
|
isSymmetric?: boolean;
|
|
8
|
-
supportsOrderBy?: boolean;
|
|
8
|
+
supportsOrderBy?: boolean | 'only_default';
|
|
9
|
+
defaultOrderByArgIndex?: number;
|
|
9
10
|
supportsLimit?: boolean;
|
|
10
11
|
between: {
|
|
11
12
|
preceding: number | string;
|
|
@@ -47,5 +48,6 @@ export declare function overload(returnType: TypeDesc, params: FunctionParameter
|
|
|
47
48
|
};
|
|
48
49
|
isSymmetric?: boolean;
|
|
49
50
|
supportsLimit?: boolean;
|
|
50
|
-
|
|
51
|
+
defaultOrderByArgIndex?: number;
|
|
52
|
+
supportsOrderBy?: boolean | 'only_default';
|
|
51
53
|
}): DialectFunctionOverloadDef;
|
|
@@ -183,6 +183,7 @@ function overload(returnType, params, e, options) {
|
|
|
183
183
|
between: options === null || options === void 0 ? void 0 : options.between,
|
|
184
184
|
isSymmetric: options === null || options === void 0 ? void 0 : options.isSymmetric,
|
|
185
185
|
supportsOrderBy: options === null || options === void 0 ? void 0 : options.supportsOrderBy,
|
|
186
|
+
defaultOrderByArgIndex: options === null || options === void 0 ? void 0 : options.defaultOrderByArgIndex,
|
|
186
187
|
supportsLimit: options === null || options === void 0 ? void 0 : options.supportsLimit,
|
|
187
188
|
};
|
|
188
189
|
}
|
|
@@ -39,8 +39,16 @@ function fnStringAggDistinct() {
|
|
|
39
39
|
const separator = (0, util_1.makeParam)('separator', (0, util_1.literal)((0, util_1.maxScalar)('string')));
|
|
40
40
|
const orderBy = { type: 'aggregate_order_by' };
|
|
41
41
|
return [
|
|
42
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}, ','${orderBy})`, {
|
|
43
|
-
|
|
42
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}, ','${orderBy})`, {
|
|
43
|
+
isSymmetric: true,
|
|
44
|
+
supportsOrderBy: 'only_default',
|
|
45
|
+
defaultOrderByArgIndex: 0,
|
|
46
|
+
}),
|
|
47
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy})`, {
|
|
48
|
+
isSymmetric: true,
|
|
49
|
+
supportsOrderBy: 'only_default',
|
|
50
|
+
defaultOrderByArgIndex: 0,
|
|
51
|
+
}),
|
|
44
52
|
];
|
|
45
53
|
}
|
|
46
54
|
exports.fnStringAggDistinct = fnStringAggDistinct;
|
|
@@ -30,8 +30,8 @@ function fnStringAgg() {
|
|
|
30
30
|
const orderBy = { type: 'aggregate_order_by' };
|
|
31
31
|
const limit = { type: 'aggregate_limit' };
|
|
32
32
|
return [
|
|
33
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(${value.arg}${orderBy}${limit})`, { supportsOrderBy: true, supportsLimit: true }),
|
|
34
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(${value.arg}, ${separator.arg}${orderBy}${limit})`, { supportsOrderBy: true, supportsLimit: true }),
|
|
33
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(${value.arg}${orderBy}${limit})`, { supportsOrderBy: true, supportsLimit: true, defaultOrderByArgIndex: 0 }),
|
|
34
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(${value.arg}, ${separator.arg}${orderBy}${limit})`, { supportsOrderBy: true, supportsLimit: true, defaultOrderByArgIndex: 0 }),
|
|
35
35
|
];
|
|
36
36
|
}
|
|
37
37
|
exports.fnStringAgg = fnStringAgg;
|
|
@@ -41,8 +41,18 @@ function fnStringAggDistinct() {
|
|
|
41
41
|
const orderBy = { type: 'aggregate_order_by' };
|
|
42
42
|
const limit = { type: 'aggregate_limit' };
|
|
43
43
|
return [
|
|
44
|
-
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}${orderBy}${limit})`, {
|
|
45
|
-
|
|
44
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}${orderBy}${limit})`, {
|
|
45
|
+
isSymmetric: true,
|
|
46
|
+
supportsOrderBy: 'only_default',
|
|
47
|
+
supportsLimit: true,
|
|
48
|
+
defaultOrderByArgIndex: 0,
|
|
49
|
+
}),
|
|
50
|
+
(0, util_1.overload)((0, util_1.minAggregate)('string'), [value.param, separator.param], (0, util_1.sql) `STRING_AGG(DISTINCT ${value.arg}, ${separator.arg}${orderBy}${limit})`, {
|
|
51
|
+
isSymmetric: true,
|
|
52
|
+
supportsOrderBy: 'only_default',
|
|
53
|
+
supportsLimit: true,
|
|
54
|
+
defaultOrderByArgIndex: 0,
|
|
55
|
+
}),
|
|
46
56
|
];
|
|
47
57
|
}
|
|
48
58
|
exports.fnStringAggDistinct = fnStringAggDistinct;
|
|
@@ -176,6 +176,7 @@ class ExprFunc extends expression_def_1.ExpressionDef {
|
|
|
176
176
|
const frag = {
|
|
177
177
|
type: 'function_call',
|
|
178
178
|
overload,
|
|
179
|
+
name: this.name,
|
|
179
180
|
args: argExprs.map(x => x.value),
|
|
180
181
|
expressionType,
|
|
181
182
|
structPath,
|
|
@@ -193,9 +194,10 @@ class ExprFunc extends expression_def_1.ExpressionDef {
|
|
|
193
194
|
if ((props === null || props === void 0 ? void 0 : props.orderBys) && props.orderBys.length > 0) {
|
|
194
195
|
const isAnalytic = (0, malloy_types_1.expressionIsAnalytic)(overload.returnType.expressionType);
|
|
195
196
|
if (dialectOverload.supportsOrderBy || isAnalytic) {
|
|
197
|
+
const allowExpression = dialectOverload.supportsOrderBy !== 'only_default';
|
|
196
198
|
const allObs = props.orderBys.flatMap(orderBy => isAnalytic
|
|
197
199
|
? orderBy.getAnalyticOrderBy(fs)
|
|
198
|
-
: orderBy.getAggregateOrderBy(fs));
|
|
200
|
+
: orderBy.getAggregateOrderBy(fs, allowExpression));
|
|
199
201
|
frag.orderBy = allObs;
|
|
200
202
|
}
|
|
201
203
|
else {
|
|
@@ -3,16 +3,16 @@ import { ExpressionDef } from '../types/expression-def';
|
|
|
3
3
|
import { FieldSpace } from '../types/field-space';
|
|
4
4
|
import { ListOf, MalloyElement } from '../types/malloy-element';
|
|
5
5
|
export declare class FunctionOrderBy extends MalloyElement {
|
|
6
|
-
readonly field
|
|
6
|
+
readonly field?: ExpressionDef | undefined;
|
|
7
7
|
readonly dir?: "asc" | "desc" | undefined;
|
|
8
8
|
elementType: string;
|
|
9
|
-
constructor(field
|
|
9
|
+
constructor(field?: ExpressionDef | undefined, dir?: "asc" | "desc" | undefined);
|
|
10
10
|
getAnalyticOrderBy(fs: FieldSpace): ModelFunctionOrderBy;
|
|
11
|
-
getAggregateOrderBy(fs: FieldSpace): ModelFunctionOrderBy;
|
|
11
|
+
getAggregateOrderBy(fs: FieldSpace, allowExpression: boolean): ModelFunctionOrderBy;
|
|
12
12
|
}
|
|
13
13
|
export declare class FunctionOrdering extends ListOf<FunctionOrderBy> {
|
|
14
14
|
elementType: string;
|
|
15
15
|
constructor(list: FunctionOrderBy[]);
|
|
16
16
|
getAnalyticOrderBy(fs: FieldSpace): ModelFunctionOrderBy[];
|
|
17
|
-
getAggregateOrderBy(fs: FieldSpace): ModelFunctionOrderBy[];
|
|
17
|
+
getAggregateOrderBy(fs: FieldSpace, allowExpression: boolean): ModelFunctionOrderBy[];
|
|
18
18
|
}
|
|
@@ -32,9 +32,14 @@ class FunctionOrderBy extends malloy_element_1.MalloyElement {
|
|
|
32
32
|
this.field = field;
|
|
33
33
|
this.dir = dir;
|
|
34
34
|
this.elementType = 'orderBy';
|
|
35
|
-
|
|
35
|
+
if (field)
|
|
36
|
+
this.has({ field });
|
|
36
37
|
}
|
|
37
38
|
getAnalyticOrderBy(fs) {
|
|
39
|
+
if (!this.field) {
|
|
40
|
+
this.log('analytic `order_by` must specify an aggregate expression or output field reference');
|
|
41
|
+
return { e: ['error'], dir: this.dir };
|
|
42
|
+
}
|
|
38
43
|
const expr = this.field.getExpression(fs);
|
|
39
44
|
if ((0, malloy_types_1.expressionIsAggregate)(expr.expressionType)) {
|
|
40
45
|
// Aggregates are okay
|
|
@@ -50,12 +55,25 @@ class FunctionOrderBy extends malloy_element_1.MalloyElement {
|
|
|
50
55
|
}
|
|
51
56
|
return { e: expr.value, dir: this.dir };
|
|
52
57
|
}
|
|
53
|
-
getAggregateOrderBy(fs) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
getAggregateOrderBy(fs, allowExpression) {
|
|
59
|
+
if (this.field) {
|
|
60
|
+
const expr = this.field.getExpression(fs);
|
|
61
|
+
if (!(0, malloy_types_1.expressionIsScalar)(expr.expressionType)) {
|
|
62
|
+
this.field.log('aggregate `order_by` must be scalar');
|
|
63
|
+
}
|
|
64
|
+
if (!allowExpression) {
|
|
65
|
+
this.field.log('`order_by` must be only `asc` or `desc` with no expression');
|
|
66
|
+
}
|
|
67
|
+
return { e: expr.value, dir: this.dir };
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
if (this.dir === undefined) {
|
|
71
|
+
// This error should technically never happen because it can't parse this way
|
|
72
|
+
this.log('field or order direction must be specified');
|
|
73
|
+
return { e: undefined, dir: 'asc' };
|
|
74
|
+
}
|
|
75
|
+
return { e: undefined, dir: this.dir };
|
|
57
76
|
}
|
|
58
|
-
return { e: expr.value, dir: this.dir };
|
|
59
77
|
}
|
|
60
78
|
}
|
|
61
79
|
exports.FunctionOrderBy = FunctionOrderBy;
|
|
@@ -67,8 +85,8 @@ class FunctionOrdering extends malloy_element_1.ListOf {
|
|
|
67
85
|
getAnalyticOrderBy(fs) {
|
|
68
86
|
return this.list.map(el => el.getAnalyticOrderBy(fs));
|
|
69
87
|
}
|
|
70
|
-
getAggregateOrderBy(fs) {
|
|
71
|
-
return this.list.map(el => el.getAggregateOrderBy(fs));
|
|
88
|
+
getAggregateOrderBy(fs, allowExpression) {
|
|
89
|
+
return this.list.map(el => el.getAggregateOrderBy(fs, allowExpression));
|
|
72
90
|
}
|
|
73
91
|
}
|
|
74
92
|
exports.FunctionOrdering = FunctionOrdering;
|
|
@@ -1346,7 +1346,7 @@ export declare class AggregateOrderingContext extends ParserRuleContext {
|
|
|
1346
1346
|
accept<Result>(visitor: MalloyParserVisitor<Result>): Result;
|
|
1347
1347
|
}
|
|
1348
1348
|
export declare class AggregateOrderBySpecContext extends ParserRuleContext {
|
|
1349
|
-
fieldExpr(): FieldExprContext;
|
|
1349
|
+
fieldExpr(): FieldExprContext | undefined;
|
|
1350
1350
|
ASC(): TerminalNode | undefined;
|
|
1351
1351
|
DESC(): TerminalNode | undefined;
|
|
1352
1352
|
constructor(parent: ParserRuleContext | undefined, invokingState: number);
|