@rex0220/kintone-sql-tools 3.45.0 → 3.46.0
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-cli/ksql.js +169 -12
- package/dist-engine/index.cjs +10 -10
- package/dist-engine/index.mjs +10 -10
- package/dist-engine/ksql-engine.umd.js +9 -9
- package/dist-engine/meta/bundle-baseline.json +7 -7
- package/dist-engine/meta/cjs.json +16 -16
- package/dist-engine/meta/esm.json +16 -16
- package/dist-engine/meta/umd.json +16 -16
- package/dist-mcp/ksql-mcp.js +171 -14
- package/dist-mcpb/ksql-mcp.mcpb +0 -0
- package/package.json +1 -1
package/dist-cli/ksql.js
CHANGED
|
@@ -752,7 +752,7 @@ function caseLabel(expr) {
|
|
|
752
752
|
}
|
|
753
753
|
function stringFuncArgLabel(arg) {
|
|
754
754
|
if (arg.type === "AGG_REF") return aggregateSyntheticName(arg.func, arg.distinct, arg.arg);
|
|
755
|
-
if (arg.type === "AGG_ARITH") return aggregateOperandLabel(arg);
|
|
755
|
+
if (arg.type === "AGG_ARITH" || arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") return aggregateOperandLabel(arg);
|
|
756
756
|
return scalarValueLabel(arg);
|
|
757
757
|
}
|
|
758
758
|
function stringFuncLabel(expr) {
|
|
@@ -790,6 +790,8 @@ function aggregateSyntheticName(func, distinct, arg) {
|
|
|
790
790
|
function aggregateOperandLabel(node) {
|
|
791
791
|
if (node.type === "NUMBER") return numberLiteralText(node);
|
|
792
792
|
if (node.type === "AGG_REF") return aggregateSyntheticName(node.func, node.distinct, node.arg);
|
|
793
|
+
if (node.type === "AGG_GROUP_KEY") return node.tableAlias ? `${node.tableAlias}.${node.field}` : node.field;
|
|
794
|
+
if (node.type === "VARIABLE") return `@${node.name}`;
|
|
793
795
|
return `${aggregateOperandLabel(node.left)}${node.op}${aggregateOperandLabel(node.right)}`;
|
|
794
796
|
}
|
|
795
797
|
|
|
@@ -1068,6 +1070,8 @@ var Parser = class {
|
|
|
1068
1070
|
this.cteNames = /* @__PURE__ */ new Set();
|
|
1069
1071
|
/** パース中に出現した一時テーブル参照(#name)のトークン。単文 API での拒否に使う */
|
|
1070
1072
|
this.tempTableRefs = [];
|
|
1073
|
+
/** GROUP BY を読む前に作る B124 候補 leaf の診断位置。AST 公開型へ位置情報を足さない。 */
|
|
1074
|
+
this.aggregateGroupKeyTokens = /* @__PURE__ */ new WeakMap();
|
|
1071
1075
|
this.allowSelectArithVariable = false;
|
|
1072
1076
|
}
|
|
1073
1077
|
// ----------------------------------------------------------
|
|
@@ -1879,6 +1883,7 @@ var Parser = class {
|
|
|
1879
1883
|
if (grouping && orderMode === "KINTONE_NATIVE") {
|
|
1880
1884
|
throw new ParseError("B65: KORDER BY cannot be combined with grouping sets in Phase1.", this.peek());
|
|
1881
1885
|
}
|
|
1886
|
+
this.validateAggregateGroupKeyRefs(columns, having, groupBy, grouping);
|
|
1882
1887
|
return {
|
|
1883
1888
|
type: "SELECT",
|
|
1884
1889
|
distinct,
|
|
@@ -1971,6 +1976,12 @@ var Parser = class {
|
|
|
1971
1976
|
this.peek()
|
|
1972
1977
|
);
|
|
1973
1978
|
}
|
|
1979
|
+
if (this.isNonAggregateArithmeticStartWithAggregate()) {
|
|
1980
|
+
throw new ParseError(
|
|
1981
|
+
`\u96C6\u8A08\u7B97\u8853\u5F0F\u306F\u96C6\u8A08\u95A2\u6570\u304B\u3089\u59CB\u307E\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08${this.peek().value}\uFF09\u3002`,
|
|
1982
|
+
this.peek()
|
|
1983
|
+
);
|
|
1984
|
+
}
|
|
1974
1985
|
if (this.tryAggregateFunc() === null && this.hasTopLevelTokenBeforeValueEnd("||" /* CONCAT_OP */)) {
|
|
1975
1986
|
const expr = this.parseScalarValueExpr({ allowAggregateArgs: true });
|
|
1976
1987
|
const parsedAlias2 = this.consume("AS" /* AS */) ? this.parseAliasName() : null;
|
|
@@ -2194,6 +2205,7 @@ var Parser = class {
|
|
|
2194
2205
|
}
|
|
2195
2206
|
stringFuncArgHasAggregate(arg) {
|
|
2196
2207
|
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") return true;
|
|
2208
|
+
if (arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") return false;
|
|
2197
2209
|
return this.scalarValueHasAggregate(arg);
|
|
2198
2210
|
}
|
|
2199
2211
|
scalarValueHasAggregate(expr) {
|
|
@@ -2236,7 +2248,7 @@ var Parser = class {
|
|
|
2236
2248
|
parseAggArithMulDiv() {
|
|
2237
2249
|
return this.continueAggArithMulDiv(this.parseAggPrimary());
|
|
2238
2250
|
}
|
|
2239
|
-
/** 集計算術式の一次式: 集計関数 /
|
|
2251
|
+
/** 集計算術式の一次式: 集計関数 / 数値 / 変数 / GROUP BY キー候補 / 括弧 */
|
|
2240
2252
|
parseAggPrimary() {
|
|
2241
2253
|
if (this.consume("(" /* LPAREN */)) {
|
|
2242
2254
|
const inner = this.continueAggArith(this.parseAggPrimary());
|
|
@@ -2253,14 +2265,99 @@ var Parser = class {
|
|
|
2253
2265
|
const tok = this.advance();
|
|
2254
2266
|
return makeNumberLiteral(tok.value);
|
|
2255
2267
|
}
|
|
2268
|
+
if (this.peek().kind === "VARIABLE" /* VARIABLE */) {
|
|
2269
|
+
const tok = this.advance();
|
|
2270
|
+
return { type: "VARIABLE", name: tok.value.slice(1).toLowerCase() };
|
|
2271
|
+
}
|
|
2256
2272
|
const aggFunc = this.tryAggregateFunc();
|
|
2257
2273
|
if (aggFunc !== null) {
|
|
2258
2274
|
const ref = this.parseAggregateRef(aggFunc);
|
|
2259
2275
|
this.rejectAggregateWindowOutsideSelect();
|
|
2260
2276
|
return ref;
|
|
2261
2277
|
}
|
|
2278
|
+
if (this.peek().kind === "IDENT" /* IDENT */ || this.peek().kind === "BIDENT" /* BIDENT */) {
|
|
2279
|
+
const tok = this.peek();
|
|
2280
|
+
const parsed = this.parseQualifiedIdent();
|
|
2281
|
+
const ref = {
|
|
2282
|
+
type: "AGG_GROUP_KEY",
|
|
2283
|
+
field: parsed.field,
|
|
2284
|
+
...parsed.tableAlias ? { tableAlias: parsed.tableAlias } : {}
|
|
2285
|
+
};
|
|
2286
|
+
this.aggregateGroupKeyTokens.set(ref, tok);
|
|
2287
|
+
return ref;
|
|
2288
|
+
}
|
|
2262
2289
|
throw new ParseError("\u96C6\u8A08\u7B97\u8853\u5F0F\u306B\u306F\u96C6\u8A08\u95A2\u6570\u307E\u305F\u306F\u6570\u5024\u304C\u5FC5\u8981\u3067\u3059", this.peek());
|
|
2263
2290
|
}
|
|
2291
|
+
/** B124 Phase 1 はトップレベルの非集計オペランド開始形を明示拒否する。 */
|
|
2292
|
+
isNonAggregateArithmeticStartWithAggregate() {
|
|
2293
|
+
const first = this.peek();
|
|
2294
|
+
if (this.tryAggregateFunc() !== null || first.kind === "CASE" /* CASE */ || first.kind === "IF" /* IF */) return false;
|
|
2295
|
+
if (this.isGroupingFunctionStart()) return false;
|
|
2296
|
+
if (this.tryStringFuncName() !== null) return false;
|
|
2297
|
+
if (!["IDENT" /* IDENT */, "BIDENT" /* BIDENT */, "VARIABLE" /* VARIABLE */, "(" /* LPAREN */].includes(first.kind)) return false;
|
|
2298
|
+
let depth = 0;
|
|
2299
|
+
let sawArithmetic = false;
|
|
2300
|
+
for (let index = this.pos; index < this.tokens.length; index++) {
|
|
2301
|
+
const token = this.tokens[index];
|
|
2302
|
+
if (depth === 0 && index > this.pos && (token.kind === "," /* COMMA */ || token.kind === "AS" /* AS */ || token.kind === "FROM" /* FROM */ || token.kind === ";" /* SEMICOLON */ || token.kind === "EOF" /* EOF */ || token.kind === "=" /* EQ */ || token.kind === "!=" /* NEQ */ || token.kind === "<>" /* LT_GT */ || token.kind === ">" /* GT */ || token.kind === "<" /* LT */ || token.kind === ">=" /* GTE */ || token.kind === "<=" /* LTE */ || token.kind === "AND" /* AND */ || token.kind === "OR" /* OR */)) break;
|
|
2303
|
+
if (this.isArithOp(token.kind)) sawArithmetic = true;
|
|
2304
|
+
if (PARSER_AGGREGATE_FUNCTION_TOKEN_MAP[token.kind] !== void 0 && this.tokens[index + 1]?.kind === "(" /* LPAREN */) return sawArithmetic;
|
|
2305
|
+
if (token.kind === "(" /* LPAREN */) depth++;
|
|
2306
|
+
else if (token.kind === ")" /* RPAREN */) {
|
|
2307
|
+
depth--;
|
|
2308
|
+
if (depth < 0) break;
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
return false;
|
|
2312
|
+
}
|
|
2313
|
+
/** SELECT ローカルの B124 leaf だけを ordinary GROUP BY の表記と照合する。 */
|
|
2314
|
+
validateAggregateGroupKeyRefs(columns, having, groupBy, grouping) {
|
|
2315
|
+
const refs = [];
|
|
2316
|
+
const visit = (node) => {
|
|
2317
|
+
if (Array.isArray(node)) {
|
|
2318
|
+
node.forEach(visit);
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
if (node === null || typeof node !== "object") return;
|
|
2322
|
+
const value = node;
|
|
2323
|
+
if (value["type"] === "SELECT" || value["type"] === "SCALAR_SUBQUERY") return;
|
|
2324
|
+
if (value["type"] === "AGG_GROUP_KEY") {
|
|
2325
|
+
refs.push(node);
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
Object.values(value).forEach(visit);
|
|
2329
|
+
};
|
|
2330
|
+
visit(columns);
|
|
2331
|
+
visit(having);
|
|
2332
|
+
if (refs.length === 0) return;
|
|
2333
|
+
const first = refs[0];
|
|
2334
|
+
const firstToken = this.aggregateGroupKeyTokens.get(first) ?? this.peek();
|
|
2335
|
+
if (grouping !== void 0) {
|
|
2336
|
+
throw new ParseError(
|
|
2337
|
+
"ROLLUP / CUBE / GROUPING SETS \u3067\u306F\u96C6\u8A08\u7B97\u8853\u5F0F\u306B\u30D5\u30A3\u30FC\u30EB\u30C9\u3092\u66F8\u3051\u307E\u305B\u3093\uFF08\u5C0F\u8A08\u30FB\u7DCF\u8A08\u884C\u3067\u5024\u304C\u5B9A\u307E\u3089\u306A\u3044\u305F\u3081\u3067\u3059\uFF09\u3002",
|
|
2338
|
+
firstToken
|
|
2339
|
+
);
|
|
2340
|
+
}
|
|
2341
|
+
if (groupBy.length === 0) {
|
|
2342
|
+
throw new ParseError(
|
|
2343
|
+
`\u96C6\u8A08\u7B97\u8853\u5F0F\u306B\u30D5\u30A3\u30FC\u30EB\u30C9\u3092\u66F8\u304F\u306B\u306F GROUP BY \u304C\u5FC5\u8981\u3067\u3059\uFF08${this.aggregateGroupKeyDisplay(first)}\uFF09\u3002`,
|
|
2344
|
+
firstToken
|
|
2345
|
+
);
|
|
2346
|
+
}
|
|
2347
|
+
const ordinaryNames = new Set(groupBy.filter((key) => key.type === "FIELD_NAME").map((key) => key.name));
|
|
2348
|
+
for (const ref of refs) {
|
|
2349
|
+
const display = this.aggregateGroupKeyDisplay(ref);
|
|
2350
|
+
if (!ordinaryNames.has(display)) {
|
|
2351
|
+
throw new ParseError(
|
|
2352
|
+
`\u96C6\u8A08\u7B97\u8853\u5F0F\u306E\u30D5\u30A3\u30FC\u30EB\u30C9\u53C2\u7167\u306F GROUP BY \u306B\u66F8\u3044\u305F\u8868\u8A18\u3068\u4E00\u81F4\u3059\u308B\u5217\u3060\u3051\u3067\u3059\uFF08${display}\uFF09\u3002\u30B0\u30EB\u30FC\u30D7\u5185\u3067\u5024\u304C\u5B9A\u307E\u3089\u306A\u3044\u305F\u3081\u3067\u3059\u3002`,
|
|
2353
|
+
this.aggregateGroupKeyTokens.get(ref) ?? firstToken
|
|
2354
|
+
);
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
aggregateGroupKeyDisplay(ref) {
|
|
2359
|
+
return ref.tableAlias ? `${ref.tableAlias}.${ref.field}` : ref.field;
|
|
2360
|
+
}
|
|
2264
2361
|
// ──────────────────────────────────────────────────
|
|
2265
2362
|
// 汎用スカラー値式パーサー(B38)
|
|
2266
2363
|
// ──────────────────────────────────────────────────
|
|
@@ -3008,6 +3105,12 @@ var Parser = class {
|
|
|
3008
3105
|
// - 集計関数(HAVING のみ): COUNT(*) / SUM(f) ...
|
|
3009
3106
|
// - 通常フィールド参照: [alias.]field
|
|
3010
3107
|
parseFieldValue() {
|
|
3108
|
+
if (this.groupingFieldContext === "HAVING" && this.isNonAggregateArithmeticStartWithAggregate()) {
|
|
3109
|
+
throw new ParseError(
|
|
3110
|
+
`\u96C6\u8A08\u7B97\u8853\u5F0F\u306F\u96C6\u8A08\u95A2\u6570\u304B\u3089\u59CB\u307E\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059\uFF08${this.peek().value}\uFF09\u3002`,
|
|
3111
|
+
this.peek()
|
|
3112
|
+
);
|
|
3113
|
+
}
|
|
3011
3114
|
if (this.isUnsupportedGroupingIdStart()) {
|
|
3012
3115
|
throw new ParseError("B65: GROUPING_ID is not supported in Phase1.", this.peek());
|
|
3013
3116
|
}
|
|
@@ -5811,7 +5914,7 @@ function collectStringFuncFields(expr, out) {
|
|
|
5811
5914
|
}
|
|
5812
5915
|
}
|
|
5813
5916
|
function collectStringFuncArgFields(arg, out) {
|
|
5814
|
-
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") {
|
|
5917
|
+
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH" || arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") {
|
|
5815
5918
|
collectAggOperandFields(arg, out);
|
|
5816
5919
|
return;
|
|
5817
5920
|
}
|
|
@@ -5857,6 +5960,9 @@ function collectAggOperandFields(node, out) {
|
|
|
5857
5960
|
collectAggOperandFields(node.left, out);
|
|
5858
5961
|
collectAggOperandFields(node.right, out);
|
|
5859
5962
|
}
|
|
5963
|
+
if (node.type === "AGG_GROUP_KEY") {
|
|
5964
|
+
out.push(normalizeSimpleFieldRef(node.tableAlias ? `${node.tableAlias}.${node.field}` : node.field));
|
|
5965
|
+
}
|
|
5860
5966
|
}
|
|
5861
5967
|
function collectAggregateArgFields(node, out) {
|
|
5862
5968
|
if (node.type === "FIELD_REF" || node.type === "ARITH") collectArithNode(node, out);
|
|
@@ -5865,6 +5971,7 @@ function collectAggregateArgFields(node, out) {
|
|
|
5865
5971
|
function hasAggregateInStringFuncExpr(expr) {
|
|
5866
5972
|
return expr.args.some((arg) => {
|
|
5867
5973
|
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") return true;
|
|
5974
|
+
if (arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") return false;
|
|
5868
5975
|
return scalarValueHasAggregate(arg);
|
|
5869
5976
|
});
|
|
5870
5977
|
}
|
|
@@ -6052,9 +6159,12 @@ function collectRequiredFieldsByTable(stmt, plainGroupByPlan, sourceAware) {
|
|
|
6052
6159
|
walkAgg(node.left, phase);
|
|
6053
6160
|
walkAgg(node.right, phase);
|
|
6054
6161
|
}
|
|
6162
|
+
if (node.type === "AGG_GROUP_KEY") {
|
|
6163
|
+
addFieldRef(node.field, node.tableAlias ?? null, phase);
|
|
6164
|
+
}
|
|
6055
6165
|
};
|
|
6056
6166
|
const walkStringArg = (arg, phase = "select") => {
|
|
6057
|
-
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") {
|
|
6167
|
+
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH" || arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") {
|
|
6058
6168
|
walkAgg(arg, phase);
|
|
6059
6169
|
return;
|
|
6060
6170
|
}
|
|
@@ -8155,7 +8265,7 @@ function formatWithComma(num, digits) {
|
|
|
8155
8265
|
return decStr ? `${intFmt}.${decStr}` : intFmt;
|
|
8156
8266
|
}
|
|
8157
8267
|
function evalStringFuncArg(arg, row, resolveFieldType, resolveFieldSemantics2) {
|
|
8158
|
-
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") {
|
|
8268
|
+
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH" || arg.type === "AGG_GROUP_KEY") {
|
|
8159
8269
|
return String(evalMaterializedAggregateOperand(arg, row));
|
|
8160
8270
|
}
|
|
8161
8271
|
if (arg.type === "NUMBER") return numberLiteralText(arg);
|
|
@@ -8166,6 +8276,13 @@ function evalMaterializedAggregateOperand(node, row) {
|
|
|
8166
8276
|
if (node.type === "AGG_REF") {
|
|
8167
8277
|
return row[aggregateSyntheticName(node.func, node.distinct, node.arg)] ?? "";
|
|
8168
8278
|
}
|
|
8279
|
+
if (node.type === "AGG_GROUP_KEY") {
|
|
8280
|
+
const field = node.tableAlias ? `${node.tableAlias}.${node.field}` : node.field;
|
|
8281
|
+
return Number(resolveFieldRef(row, field));
|
|
8282
|
+
}
|
|
8283
|
+
if (node.type === "VARIABLE") {
|
|
8284
|
+
throw new Error(`InternalError: unresolved aggregate arithmetic variable @${node.name}.`);
|
|
8285
|
+
}
|
|
8169
8286
|
const left = Number(evalMaterializedAggregateOperand(node.left, row));
|
|
8170
8287
|
const right = Number(evalMaterializedAggregateOperand(node.right, row));
|
|
8171
8288
|
switch (node.op) {
|
|
@@ -8667,7 +8784,7 @@ function collectStringFuncFields2(expr, out) {
|
|
|
8667
8784
|
for (const arg of expr.args) collectStringFuncArgFields2(arg, out);
|
|
8668
8785
|
}
|
|
8669
8786
|
function collectStringFuncArgFields2(arg, out) {
|
|
8670
|
-
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") {
|
|
8787
|
+
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH" || arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") {
|
|
8671
8788
|
collectAggOperandFields2(arg, out);
|
|
8672
8789
|
return;
|
|
8673
8790
|
}
|
|
@@ -8698,6 +8815,7 @@ function collectAggOperandFields2(node, out) {
|
|
|
8698
8815
|
collectAggOperandFields2(node.left, out);
|
|
8699
8816
|
collectAggOperandFields2(node.right, out);
|
|
8700
8817
|
}
|
|
8818
|
+
if (node.type === "AGG_GROUP_KEY") out.add(node.tableAlias ? `${node.tableAlias}.${node.field}` : node.field);
|
|
8701
8819
|
}
|
|
8702
8820
|
function collectAggregateArgFields2(node, out) {
|
|
8703
8821
|
if (node.type === "FIELD_REF" || node.type === "ARITH") collectArithNode2(node, out);
|
|
@@ -13431,6 +13549,13 @@ function toAggregateFieldRef(field) {
|
|
|
13431
13549
|
function evalAggArithExpr(node, rows, resolveAggSortKind) {
|
|
13432
13550
|
if (node.type === "NUMBER") return node.value;
|
|
13433
13551
|
if (node.type === "AGG_REF") return Number(evalAggregate(node.func, node.distinct, node.arg, node.separator, rows, resolveAggSortKind));
|
|
13552
|
+
if (node.type === "AGG_GROUP_KEY") {
|
|
13553
|
+
const field = node.tableAlias ? `${node.tableAlias}.${node.field}` : node.field;
|
|
13554
|
+
return Number(resolveFieldRef(rows[0] ?? {}, field));
|
|
13555
|
+
}
|
|
13556
|
+
if (node.type === "VARIABLE") {
|
|
13557
|
+
throw new Error(`InternalError: unresolved aggregate arithmetic variable @${node.name}.`);
|
|
13558
|
+
}
|
|
13434
13559
|
const l = evalAggArithExpr(node.left, rows, resolveAggSortKind);
|
|
13435
13560
|
const r = evalAggArithExpr(node.right, rows, resolveAggSortKind);
|
|
13436
13561
|
switch (node.op) {
|
|
@@ -14069,7 +14194,8 @@ function arithColDefaultKey(expr) {
|
|
|
14069
14194
|
}
|
|
14070
14195
|
function stringFuncDefaultKey(expr) {
|
|
14071
14196
|
const argStrs = expr.args.map((a) => {
|
|
14072
|
-
if (a.type === "AGG_REF" || a.type === "AGG_ARITH") return aggArithDefaultKey(a);
|
|
14197
|
+
if (a.type === "AGG_REF" || a.type === "AGG_ARITH" || a.type === "AGG_GROUP_KEY") return aggArithDefaultKey(a);
|
|
14198
|
+
if (a.type === "VARIABLE") return `@${a.name}`;
|
|
14073
14199
|
return scalarValueDefaultKey(a);
|
|
14074
14200
|
});
|
|
14075
14201
|
return `${expr.func}(${argStrs.join(",")})`;
|
|
@@ -14096,6 +14222,7 @@ function scalarValueDefaultKey(expr) {
|
|
|
14096
14222
|
}
|
|
14097
14223
|
function hasAggregateInStringFuncArg(arg) {
|
|
14098
14224
|
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") return true;
|
|
14225
|
+
if (arg.type === "AGG_GROUP_KEY" || arg.type === "VARIABLE") return false;
|
|
14099
14226
|
return scalarValueHasAggregate2(arg);
|
|
14100
14227
|
}
|
|
14101
14228
|
function scalarValueHasAggregate2(expr) {
|
|
@@ -14125,6 +14252,13 @@ function resolveAggInStringFuncArg(arg, rows, resolveAggSortKind) {
|
|
|
14125
14252
|
const value = evalAggArithExpr(arg, rows, resolveAggSortKind);
|
|
14126
14253
|
return { type: "NUMBER", value, raw: String(value) };
|
|
14127
14254
|
}
|
|
14255
|
+
if (arg.type === "AGG_GROUP_KEY") {
|
|
14256
|
+
const field = arg.tableAlias ? `${arg.tableAlias}.${arg.field}` : arg.field;
|
|
14257
|
+
return { type: "NUMBER", value: Number(resolveFieldRef(rows[0] ?? {}, field)), raw: resolveFieldRef(rows[0] ?? {}, field) };
|
|
14258
|
+
}
|
|
14259
|
+
if (arg.type === "VARIABLE") {
|
|
14260
|
+
throw new Error(`InternalError: unresolved aggregate arithmetic variable @${arg.name}.`);
|
|
14261
|
+
}
|
|
14128
14262
|
if (arg.type === "STRING_FUNC") {
|
|
14129
14263
|
return resolveAggInStringFuncExpr(arg, rows, resolveAggSortKind);
|
|
14130
14264
|
}
|
|
@@ -17117,7 +17251,15 @@ function resolveBatchVariableReferencesInternal(node, variables, numericArithmet
|
|
|
17117
17251
|
`ArgumentError: variable @${obj["name"]} is not numeric and cannot be used in arithmetic.`
|
|
17118
17252
|
);
|
|
17119
17253
|
}
|
|
17120
|
-
return numericArithmeticOperand && value.type === "string" && value.placeholder === true ? {
|
|
17254
|
+
return numericArithmeticOperand && value.type === "string" && value.placeholder === true ? {
|
|
17255
|
+
type: "NUMBER",
|
|
17256
|
+
value: 0,
|
|
17257
|
+
raw: numericArithmeticOperand === "AGG_ARITH" ? `@${obj["name"]}` : value.value
|
|
17258
|
+
} : value.type === "number" ? {
|
|
17259
|
+
type: "NUMBER",
|
|
17260
|
+
value: value.value,
|
|
17261
|
+
raw: numericArithmeticOperand === "AGG_ARITH" ? `@${obj["name"]}` : value.raw ?? String(value.value)
|
|
17262
|
+
} : { type: "STRING", value: value.value };
|
|
17121
17263
|
}
|
|
17122
17264
|
if (obj["type"] === "VARIABLE_COL" && typeof obj["name"] === "string" && typeof obj["alias"] === "string") {
|
|
17123
17265
|
const value = variables.get(obj["name"]);
|
|
@@ -17136,7 +17278,7 @@ function resolveBatchVariableReferencesInternal(node, variables, numericArithmet
|
|
|
17136
17278
|
resolveBatchVariableReferencesInternal(
|
|
17137
17279
|
value,
|
|
17138
17280
|
variables,
|
|
17139
|
-
|
|
17281
|
+
key === "left" || key === "right" ? obj["type"] === "AGG_ARITH" ? "AGG_ARITH" : obj["type"] === "ARITH" || obj["type"] === "SCALAR_ARITH" ? "ARITH" : false : false
|
|
17140
17282
|
)
|
|
17141
17283
|
])
|
|
17142
17284
|
);
|
|
@@ -17853,6 +17995,8 @@ function arithHasFieldRef(node) {
|
|
|
17853
17995
|
return false;
|
|
17854
17996
|
}
|
|
17855
17997
|
function stringFuncArgHasFieldRef(arg) {
|
|
17998
|
+
if (arg.type === "AGG_GROUP_KEY") return true;
|
|
17999
|
+
if (arg.type === "VARIABLE") return false;
|
|
17856
18000
|
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") return true;
|
|
17857
18001
|
return scalarValueHasFieldRef(arg);
|
|
17858
18002
|
}
|
|
@@ -18410,7 +18554,11 @@ function collectAggregateArgFieldRefs(arg, out) {
|
|
|
18410
18554
|
}
|
|
18411
18555
|
if (arg.type === "STRING_FUNC") {
|
|
18412
18556
|
for (const child of arg.args) {
|
|
18413
|
-
if (child.type
|
|
18557
|
+
if (child.type === "AGG_GROUP_KEY") {
|
|
18558
|
+
out.push({ type: "FIELD", tableAlias: child.tableAlias ?? null, field: child.field });
|
|
18559
|
+
} else if (child.type !== "AGG_REF" && child.type !== "AGG_ARITH" && child.type !== "VARIABLE") {
|
|
18560
|
+
collectAggregateArgFieldRefs(child, out);
|
|
18561
|
+
}
|
|
18414
18562
|
}
|
|
18415
18563
|
return;
|
|
18416
18564
|
}
|
|
@@ -18430,11 +18578,16 @@ function collectAggregateOperandRefs(node, out) {
|
|
|
18430
18578
|
collectAggregateOperandRefs(node.left, out);
|
|
18431
18579
|
collectAggregateOperandRefs(node.right, out);
|
|
18432
18580
|
}
|
|
18581
|
+
if (node.type === "AGG_GROUP_KEY") out.push({ type: "FIELD", tableAlias: node.tableAlias ?? null, field: node.field });
|
|
18433
18582
|
}
|
|
18434
18583
|
function collectStringFuncAggregateRefs(expr, out) {
|
|
18435
18584
|
for (const arg of expr.args) {
|
|
18436
18585
|
if (arg.type === "AGG_REF" || arg.type === "AGG_ARITH") {
|
|
18437
18586
|
collectAggregateOperandRefs(arg, out);
|
|
18587
|
+
} else if (arg.type === "AGG_GROUP_KEY") {
|
|
18588
|
+
out.push({ type: "FIELD", tableAlias: arg.tableAlias ?? null, field: arg.field });
|
|
18589
|
+
} else if (arg.type === "VARIABLE") {
|
|
18590
|
+
continue;
|
|
18438
18591
|
} else {
|
|
18439
18592
|
collectScalarAggregateRefs(arg, out);
|
|
18440
18593
|
}
|
|
@@ -24416,7 +24569,8 @@ function collectArithNodeRefs(node, out) {
|
|
|
24416
24569
|
}
|
|
24417
24570
|
if (node.type === "STRING_FUNC") {
|
|
24418
24571
|
for (const arg of node.args) {
|
|
24419
|
-
if (arg.type
|
|
24572
|
+
if (arg.type === "AGG_GROUP_KEY") out.add(arg.tableAlias ? `${arg.tableAlias}.${arg.field}` : arg.field);
|
|
24573
|
+
else if (arg.type !== "AGG_REF" && arg.type !== "AGG_ARITH" && arg.type !== "VARIABLE") collectScalarNodeRefs(arg, out);
|
|
24420
24574
|
}
|
|
24421
24575
|
}
|
|
24422
24576
|
}
|
|
@@ -24478,7 +24632,10 @@ function collectScalarNodeRefs(node, out) {
|
|
|
24478
24632
|
return;
|
|
24479
24633
|
}
|
|
24480
24634
|
if (node.type === "STRING_FUNC") {
|
|
24481
|
-
for (const arg of node.args)
|
|
24635
|
+
for (const arg of node.args) {
|
|
24636
|
+
if (arg.type === "AGG_GROUP_KEY") out.add(arg.tableAlias ? `${arg.tableAlias}.${arg.field}` : arg.field);
|
|
24637
|
+
else if (arg.type !== "AGG_REF" && arg.type !== "AGG_ARITH" && arg.type !== "VARIABLE") collectScalarNodeRefs(arg, out);
|
|
24638
|
+
}
|
|
24482
24639
|
return;
|
|
24483
24640
|
}
|
|
24484
24641
|
if (node.type === "SCALAR_ARITH" || node.type === "CONCAT_OP") {
|