rawsql-ts 0.26.0 → 0.26.1

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 (40) hide show
  1. package/dist/esm/index.d.ts +1 -0
  2. package/dist/esm/index.js +1 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +2 -2
  5. package/dist/esm/index.min.js.map +4 -4
  6. package/dist/esm/transformers/ConditionOptimization.d.ts +10 -1
  7. package/dist/esm/transformers/ConditionOptimization.js +29 -13
  8. package/dist/esm/transformers/ConditionOptimization.js.map +1 -1
  9. package/dist/esm/transformers/ParameterConditionPlacementOptimizer.js +1 -4
  10. package/dist/esm/transformers/ParameterConditionPlacementOptimizer.js.map +1 -1
  11. package/dist/esm/transformers/SSSQLFilterBuilder.d.ts +6 -0
  12. package/dist/esm/transformers/SSSQLFilterBuilder.js +84 -8
  13. package/dist/esm/transformers/SSSQLFilterBuilder.js.map +1 -1
  14. package/dist/esm/transformers/SqlComponentFormatter.d.ts +3 -0
  15. package/dist/esm/transformers/SqlComponentFormatter.js +7 -0
  16. package/dist/esm/transformers/SqlComponentFormatter.js.map +1 -0
  17. package/dist/esm/transformers/StaticPredicatePlacementOptimizer.d.ts +85 -0
  18. package/dist/esm/transformers/StaticPredicatePlacementOptimizer.js +1198 -0
  19. package/dist/esm/transformers/StaticPredicatePlacementOptimizer.js.map +1 -0
  20. package/dist/index.js +1 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.min.js +2 -2
  23. package/dist/index.min.js.map +4 -4
  24. package/dist/src/index.d.ts +1 -0
  25. package/dist/src/transformers/ConditionOptimization.d.ts +10 -1
  26. package/dist/src/transformers/SSSQLFilterBuilder.d.ts +6 -0
  27. package/dist/src/transformers/SqlComponentFormatter.d.ts +3 -0
  28. package/dist/src/transformers/StaticPredicatePlacementOptimizer.d.ts +85 -0
  29. package/dist/transformers/ConditionOptimization.js +50 -22
  30. package/dist/transformers/ConditionOptimization.js.map +1 -1
  31. package/dist/transformers/ParameterConditionPlacementOptimizer.js +6 -9
  32. package/dist/transformers/ParameterConditionPlacementOptimizer.js.map +1 -1
  33. package/dist/transformers/SSSQLFilterBuilder.js +92 -16
  34. package/dist/transformers/SSSQLFilterBuilder.js.map +1 -1
  35. package/dist/transformers/SqlComponentFormatter.js +11 -0
  36. package/dist/transformers/SqlComponentFormatter.js.map +1 -0
  37. package/dist/transformers/StaticPredicatePlacementOptimizer.js +1208 -0
  38. package/dist/transformers/StaticPredicatePlacementOptimizer.js.map +1 -0
  39. package/dist/tsconfig.browser.tsbuildinfo +1 -1
  40. package/package.json +1 -1
@@ -0,0 +1,1198 @@
1
+ import { SubQuerySource, TableSource, WhereClause } from "../models/Clause";
2
+ import { BinarySelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
3
+ import { ArrayExpression, ArrayQueryExpression, BetweenExpression, BinaryExpression, CaseExpression, CastExpression, ColumnReference, FunctionCall, InlineQuery, JsonPredicateExpression, ParameterExpression, ParenExpression, TupleExpression, TypeValue, UnaryExpression, ValueList } from "../models/ValueComponent";
4
+ import { SelectQueryParser } from "../parsers/SelectQueryParser";
5
+ import { ValueParser } from "../parsers/ValueParser";
6
+ import { formatSqlComponent } from "./SqlComponentFormatter";
7
+ const VOLATILE_OR_UNSUPPORTED_FUNCTION_REASON = "Predicate contains a function call; volatile and expression predicates are not moved in the safe-only implementation.";
8
+ const normalizeIdentifier = (value) => value.trim().toLowerCase();
9
+ const unwrapParens = (expression) => {
10
+ let candidate = expression;
11
+ while (candidate instanceof ParenExpression) {
12
+ candidate = candidate.expression;
13
+ }
14
+ return candidate;
15
+ };
16
+ const isBinaryOperator = (expression, operator) => {
17
+ const candidate = unwrapParens(expression);
18
+ return candidate instanceof BinaryExpression
19
+ && candidate.operator.value.trim().toLowerCase() === operator;
20
+ };
21
+ const collectTopLevelAndTerms = (expression) => {
22
+ const candidate = unwrapParens(expression);
23
+ if (!isBinaryOperator(candidate, "and")) {
24
+ return [expression];
25
+ }
26
+ return [
27
+ ...collectTopLevelAndTerms(candidate.left),
28
+ ...collectTopLevelAndTerms(candidate.right)
29
+ ];
30
+ };
31
+ const rebuildWhereWithoutTerms = (query, termsToRemove) => {
32
+ if (!query.whereClause || termsToRemove.size === 0) {
33
+ return;
34
+ }
35
+ const remaining = collectTopLevelAndTerms(query.whereClause.condition)
36
+ .filter(term => !termsToRemove.has(term));
37
+ if (remaining.length === 0) {
38
+ query.whereClause = null;
39
+ return;
40
+ }
41
+ let rebuilt = remaining[0];
42
+ for (let index = 1; index < remaining.length; index += 1) {
43
+ rebuilt = new BinaryExpression(rebuilt, "and", remaining[index]);
44
+ }
45
+ query.whereClause = new WhereClause(rebuilt);
46
+ };
47
+ const cloneValueComponent = (expression) => {
48
+ return ValueParser.parse(formatSqlComponent(expression));
49
+ };
50
+ const cloneColumnReference = (reference) => {
51
+ var _a, _b;
52
+ const namespaces = (_b = (_a = reference.namespaces) === null || _a === void 0 ? void 0 : _a.map(namespace => namespace.name)) !== null && _b !== void 0 ? _b : null;
53
+ return new ColumnReference(namespaces, reference.column.name);
54
+ };
55
+ const columnReferenceText = (reference) => {
56
+ const namespace = reference.getNamespace();
57
+ return namespace ? `${namespace}.${reference.column.name}` : reference.column.name;
58
+ };
59
+ const sameColumnReference = (left, right) => {
60
+ return normalizeIdentifier(left.column.name) === normalizeIdentifier(right.column.name)
61
+ && normalizeIdentifier(left.getNamespace()) === normalizeIdentifier(right.getNamespace());
62
+ };
63
+ const appendUnique = (items, value) => {
64
+ if (!items.includes(value)) {
65
+ items.push(value);
66
+ }
67
+ };
68
+ export class StaticPredicatePlacementOptimizer {
69
+ plan(input, options = {}) {
70
+ var _a, _b, _c;
71
+ const parsed = this.parseInput(input);
72
+ const warnings = [...parsed.warnings];
73
+ const errors = [...parsed.errors];
74
+ if (!parsed.query) {
75
+ return this.buildResult({
76
+ sql: parsed.sql,
77
+ applied: [],
78
+ skipped: [],
79
+ warnings,
80
+ errors,
81
+ dryRun: (_a = options.dryRun) !== null && _a !== void 0 ? _a : true,
82
+ formatterGeneratedSource: parsed.formatterGeneratedSource
83
+ });
84
+ }
85
+ if (!(parsed.query instanceof SimpleSelectQuery)) {
86
+ warnings.push({
87
+ code: "UNSUPPORTED_ROOT_QUERY",
88
+ message: "Static predicate placement currently supports only SimpleSelectQuery roots."
89
+ });
90
+ return this.buildResult({
91
+ sql: parsed.sql,
92
+ applied: [],
93
+ skipped: [],
94
+ warnings,
95
+ errors,
96
+ dryRun: (_b = options.dryRun) !== null && _b !== void 0 ? _b : true,
97
+ formatterGeneratedSource: parsed.formatterGeneratedSource
98
+ });
99
+ }
100
+ const query = parsed.query;
101
+ const applied = [];
102
+ const skipped = [];
103
+ const movedTerms = [];
104
+ for (const term of query.whereClause ? collectTopLevelAndTerms(query.whereClause.condition) : []) {
105
+ const candidate = this.analyzeCandidate(query, term);
106
+ if (!candidate) {
107
+ continue;
108
+ }
109
+ if ("code" in candidate) {
110
+ skipped.push(this.makeSkipped(term, candidate));
111
+ continue;
112
+ }
113
+ const target = this.resolveTarget(query, candidate);
114
+ if ("code" in target) {
115
+ skipped.push(this.makeSkipped(term, target));
116
+ continue;
117
+ }
118
+ const targetColumns = this.resolveTargetColumns(target.query, candidate.references);
119
+ if ("code" in targetColumns) {
120
+ skipped.push(this.makeSkipped(term, targetColumns));
121
+ continue;
122
+ }
123
+ const movedPredicate = this.rebasePredicate(candidate.expression, targetColumns);
124
+ target.query.appendWhere(movedPredicate);
125
+ movedTerms.push(term);
126
+ applied.push({
127
+ kind: "move_static_predicate",
128
+ predicateSql: candidate.predicateSql,
129
+ fromScopeId: "scope:root",
130
+ toScopeId: target.scopeId,
131
+ reason: "All outer references resolve to direct upstream outputs before unsafe query boundaries.",
132
+ columnReferences: candidate.references.map(columnReferenceText)
133
+ });
134
+ }
135
+ rebuildWhereWithoutTerms(query, new Set(movedTerms));
136
+ const sql = applied.length > 0 ? formatSqlComponent(query) : parsed.sql;
137
+ return this.buildResult({
138
+ sql,
139
+ applied,
140
+ skipped,
141
+ warnings,
142
+ errors,
143
+ dryRun: (_c = options.dryRun) !== null && _c !== void 0 ? _c : true,
144
+ formatterGeneratedSource: parsed.formatterGeneratedSource
145
+ });
146
+ }
147
+ optimize(input, options = {}) {
148
+ var _a;
149
+ return this.plan(input, Object.assign(Object.assign({}, options), { dryRun: (_a = options.dryRun) !== null && _a !== void 0 ? _a : false }));
150
+ }
151
+ parseInput(input) {
152
+ const warnings = [];
153
+ const errors = [];
154
+ try {
155
+ const sourceSql = typeof input === "string"
156
+ ? input
157
+ : formatSqlComponent(input);
158
+ if (typeof input !== "string") {
159
+ warnings.push({
160
+ code: "AST_INPUT_FORMATTED",
161
+ message: "AST input is cloned through formatter output so the caller-owned query is not mutated."
162
+ });
163
+ }
164
+ return {
165
+ query: SelectQueryParser.parse(sourceSql),
166
+ sql: sourceSql,
167
+ formatterGeneratedSource: typeof input !== "string",
168
+ warnings,
169
+ errors
170
+ };
171
+ }
172
+ catch (error) {
173
+ const detail = error instanceof Error ? error.message : String(error);
174
+ errors.push({
175
+ code: "PARSE_FAILED",
176
+ message: "Static predicate placement could not parse the input SQL.",
177
+ detail
178
+ });
179
+ return {
180
+ query: null,
181
+ sql: typeof input === "string" ? input : "",
182
+ formatterGeneratedSource: typeof input !== "string",
183
+ warnings,
184
+ errors
185
+ };
186
+ }
187
+ }
188
+ analyzeCandidate(root, expression) {
189
+ if (this.collectParameterNames(expression).length > 0) {
190
+ return null;
191
+ }
192
+ const unsupported = this.findUnsupportedExpression(expression, this.isExistsPredicate(expression));
193
+ if (unsupported) {
194
+ return unsupported;
195
+ }
196
+ const references = this.collectRootColumnReferences(root, expression);
197
+ if (references.length === 0) {
198
+ return {
199
+ code: "NO_COLUMN_REFERENCE",
200
+ reason: "Static predicate has no outer column reference to anchor the move."
201
+ };
202
+ }
203
+ return {
204
+ expression,
205
+ predicateSql: formatSqlComponent(expression),
206
+ references
207
+ };
208
+ }
209
+ isExistsPredicate(expression) {
210
+ const candidate = unwrapParens(expression);
211
+ if (!(candidate instanceof UnaryExpression)) {
212
+ return false;
213
+ }
214
+ const operator = candidate.operator.value.trim().toLowerCase();
215
+ return operator === "exists" && unwrapParens(candidate.expression) instanceof InlineQuery;
216
+ }
217
+ findUnsupportedExpression(expression, allowExistsSubquery) {
218
+ let found = null;
219
+ const visitSelect = (query) => {
220
+ var _a, _b, _c, _d;
221
+ if (found) {
222
+ return;
223
+ }
224
+ if (query instanceof BinarySelectQuery) {
225
+ found = {
226
+ code: "UNION_BOUNDARY",
227
+ reason: "Static predicate would need distribution into a UNION branch, which is unsupported."
228
+ };
229
+ return;
230
+ }
231
+ if (!(query instanceof SimpleSelectQuery)) {
232
+ found = {
233
+ code: "UNSUPPORTED_SUBQUERY",
234
+ reason: "Static predicate contains a non-simple subquery, which is unsupported."
235
+ };
236
+ return;
237
+ }
238
+ query.selectClause.items.forEach(item => visit(item.value));
239
+ for (const join of (_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.joins) !== null && _b !== void 0 ? _b : []) {
240
+ if (join.condition) {
241
+ visit(join.condition.condition);
242
+ }
243
+ const source = join.source.datasource;
244
+ if (source instanceof SubQuerySource) {
245
+ visitSelect(source.query);
246
+ }
247
+ }
248
+ if (query.whereClause) {
249
+ visit(query.whereClause.condition);
250
+ }
251
+ if (query.havingClause) {
252
+ visit(query.havingClause.condition);
253
+ }
254
+ for (const cte of (_d = (_c = query.withClause) === null || _c === void 0 ? void 0 : _c.tables) !== null && _d !== void 0 ? _d : []) {
255
+ if (cte.query instanceof SimpleSelectQuery || cte.query instanceof BinarySelectQuery) {
256
+ visitSelect(cte.query);
257
+ }
258
+ }
259
+ };
260
+ const visit = (value) => {
261
+ if (found) {
262
+ return;
263
+ }
264
+ const candidate = unwrapParens(value);
265
+ if (candidate instanceof BinaryExpression) {
266
+ if (candidate.operator.value.trim().toLowerCase() === "or") {
267
+ found = {
268
+ code: "OR_PREDICATE_UNSUPPORTED",
269
+ reason: "Predicate contains OR predicates, which are not moved in the first safe-only implementation."
270
+ };
271
+ return;
272
+ }
273
+ visit(candidate.left);
274
+ visit(candidate.right);
275
+ return;
276
+ }
277
+ if (candidate instanceof FunctionCall) {
278
+ found = {
279
+ code: "FUNCTION_PREDICATE_UNSUPPORTED",
280
+ reason: VOLATILE_OR_UNSUPPORTED_FUNCTION_REASON
281
+ };
282
+ return;
283
+ }
284
+ if (candidate instanceof CaseExpression) {
285
+ found = {
286
+ code: "CASE_PREDICATE_UNSUPPORTED",
287
+ reason: "CASE predicates are not moved in the first safe-only implementation."
288
+ };
289
+ return;
290
+ }
291
+ if (candidate instanceof InlineQuery) {
292
+ if (!allowExistsSubquery) {
293
+ found = {
294
+ code: "SUBQUERY_PREDICATE_UNSUPPORTED",
295
+ reason: "Subquery predicates are only moved when they are simple EXISTS predicates."
296
+ };
297
+ return;
298
+ }
299
+ visitSelect(candidate.selectQuery);
300
+ return;
301
+ }
302
+ if (candidate instanceof ArrayQueryExpression) {
303
+ found = {
304
+ code: "SUBQUERY_PREDICATE_UNSUPPORTED",
305
+ reason: "Array subquery predicates are not moved in the first safe-only implementation."
306
+ };
307
+ return;
308
+ }
309
+ if (candidate instanceof BetweenExpression) {
310
+ found = {
311
+ code: "BETWEEN_PREDICATE_UNSUPPORTED",
312
+ reason: "BETWEEN predicates are not moved in the first safe-only implementation."
313
+ };
314
+ return;
315
+ }
316
+ if (candidate instanceof UnaryExpression) {
317
+ visit(candidate.expression);
318
+ return;
319
+ }
320
+ if (candidate instanceof CastExpression) {
321
+ visit(candidate.input);
322
+ return;
323
+ }
324
+ if (candidate instanceof JsonPredicateExpression) {
325
+ visit(candidate.expression);
326
+ return;
327
+ }
328
+ if (candidate instanceof ArrayExpression) {
329
+ visit(candidate.expression);
330
+ return;
331
+ }
332
+ if (candidate instanceof ValueList) {
333
+ candidate.values.forEach(visit);
334
+ return;
335
+ }
336
+ if (candidate instanceof TupleExpression) {
337
+ candidate.values.forEach(visit);
338
+ return;
339
+ }
340
+ if (candidate instanceof TypeValue && candidate.argument) {
341
+ visit(candidate.argument);
342
+ }
343
+ };
344
+ visit(expression);
345
+ return found;
346
+ }
347
+ resolveTarget(root, candidate) {
348
+ const boundary = this.findCurrentQueryBoundary(root);
349
+ if (boundary) {
350
+ return boundary;
351
+ }
352
+ if (!root.fromClause) {
353
+ return {
354
+ code: "NO_FROM_CLAUSE",
355
+ reason: "Predicate has no FROM source that can receive it safely."
356
+ };
357
+ }
358
+ const bindings = [];
359
+ for (const reference of candidate.references) {
360
+ const binding = this.resolveSourceBinding(root, reference);
361
+ if ("code" in binding) {
362
+ return binding;
363
+ }
364
+ if (!bindings.some(item => item.source === binding.source)) {
365
+ bindings.push(binding);
366
+ }
367
+ }
368
+ if (bindings.length !== 1) {
369
+ return {
370
+ code: "MULTIPLE_SOURCE_REFERENCES",
371
+ reason: "Static predicate references multiple source query blocks; moving it may change semantics."
372
+ };
373
+ }
374
+ const binding = bindings[0];
375
+ const nullableSide = this.findNullableSideBoundary(root.fromClause, binding);
376
+ if (nullableSide) {
377
+ return nullableSide;
378
+ }
379
+ const upstream = this.resolveUpstreamQuery(root, binding);
380
+ if ("code" in upstream) {
381
+ return upstream;
382
+ }
383
+ const targetBoundary = this.findTargetQueryBoundary(upstream.query);
384
+ if (targetBoundary) {
385
+ return targetBoundary;
386
+ }
387
+ return upstream;
388
+ }
389
+ findCurrentQueryBoundary(query) {
390
+ if (query.selectClause.distinct) {
391
+ return {
392
+ code: "DISTINCT_BOUNDARY",
393
+ reason: "Predicate crosses DISTINCT boundary; moving it may change semantics."
394
+ };
395
+ }
396
+ if (query.groupByClause || query.havingClause) {
397
+ return {
398
+ code: "GROUP_BY_BOUNDARY",
399
+ reason: "Predicate crosses GROUP BY boundary; moving it may change semantics."
400
+ };
401
+ }
402
+ if (this.hasWindowUsage(query)) {
403
+ return {
404
+ code: "WINDOW_BOUNDARY",
405
+ reason: "Predicate crosses WINDOW boundary; moving it may change semantics."
406
+ };
407
+ }
408
+ return null;
409
+ }
410
+ findTargetQueryBoundary(query) {
411
+ const commonBoundary = this.findCurrentQueryBoundary(query);
412
+ if (commonBoundary) {
413
+ return commonBoundary;
414
+ }
415
+ if (query.limitClause || query.offsetClause || query.fetchClause) {
416
+ return {
417
+ code: "ROW_LIMIT_BOUNDARY",
418
+ reason: "Predicate crosses LIMIT/OFFSET/FETCH boundary; moving it may change row selection semantics."
419
+ };
420
+ }
421
+ if (query.fromClause && this.hasOuterJoin(query.fromClause)) {
422
+ return {
423
+ code: "OUTER_JOIN_BOUNDARY",
424
+ reason: "Target query contains an OUTER JOIN boundary that is not moved across in the safe-only implementation."
425
+ };
426
+ }
427
+ return null;
428
+ }
429
+ resolveSourceBinding(root, column) {
430
+ const fromClause = root.fromClause;
431
+ if (!fromClause) {
432
+ return {
433
+ code: "NO_FROM_CLAUSE",
434
+ reason: "Predicate has no FROM source that can receive it safely."
435
+ };
436
+ }
437
+ const bindings = this.getSourceBindings(fromClause);
438
+ const namespace = normalizeIdentifier(column.getNamespace());
439
+ const columnName = column.column.name;
440
+ if (namespace) {
441
+ const matches = bindings.filter(binding => normalizeIdentifier(binding.alias) === namespace);
442
+ if (matches.length !== 1) {
443
+ return {
444
+ code: "AMBIGUOUS_COLUMN_SOURCE",
445
+ reason: `Column source alias '${column.getNamespace()}' is not uniquely resolvable.`
446
+ };
447
+ }
448
+ if (this.resolveSourceQueryForColumns(root, matches[0].source) instanceof BinarySelectQuery) {
449
+ return matches[0];
450
+ }
451
+ const matchCount = this.getOutputColumnMatchCount(root, matches[0], columnName);
452
+ if (matchCount === 0) {
453
+ return {
454
+ code: "COLUMN_NOT_AVAILABLE_UPSTREAM",
455
+ reason: `Column '${columnReferenceText(column)}' is not a direct output of the referenced source.`
456
+ };
457
+ }
458
+ if (matchCount > 1) {
459
+ return {
460
+ code: "AMBIGUOUS_COLUMN_REFERENCE",
461
+ reason: `Column '${columnReferenceText(column)}' resolves to multiple outputs in the referenced source.`
462
+ };
463
+ }
464
+ return matches[0];
465
+ }
466
+ const matches = [];
467
+ let unionSource = null;
468
+ for (const binding of bindings) {
469
+ if (this.resolveSourceQueryForColumns(root, binding.source) instanceof BinarySelectQuery) {
470
+ unionSource !== null && unionSource !== void 0 ? unionSource : (unionSource = binding);
471
+ continue;
472
+ }
473
+ const matchCount = this.getOutputColumnMatchCount(root, binding, columnName);
474
+ if (matchCount > 1) {
475
+ return {
476
+ code: "AMBIGUOUS_COLUMN_REFERENCE",
477
+ reason: `Column '${columnName}' resolves to multiple outputs in source '${binding.alias}'.`
478
+ };
479
+ }
480
+ if (matchCount === 1) {
481
+ matches.push(binding);
482
+ }
483
+ }
484
+ if (matches.length === 0 && unionSource) {
485
+ return {
486
+ code: "UNION_BOUNDARY",
487
+ reason: "Predicate would need distribution into a UNION branch, which is unsupported."
488
+ };
489
+ }
490
+ if (matches.length !== 1) {
491
+ return {
492
+ code: "AMBIGUOUS_COLUMN_REFERENCE",
493
+ reason: matches.length === 0
494
+ ? `Column '${columnName}' is not uniquely resolvable to a safe upstream source.`
495
+ : `Column '${columnName}' is ambiguous across source query blocks.`
496
+ };
497
+ }
498
+ return matches[0];
499
+ }
500
+ resolveUpstreamQuery(root, binding) {
501
+ const source = binding.source.datasource;
502
+ if (source instanceof SubQuerySource) {
503
+ if (source.query instanceof SimpleSelectQuery) {
504
+ return {
505
+ query: source.query,
506
+ scopeId: `subquery:${binding.alias}`,
507
+ sourceBinding: binding
508
+ };
509
+ }
510
+ return {
511
+ code: "UNION_BOUNDARY",
512
+ reason: "Predicate would need distribution into a UNION or non-simple subquery, which is unsupported."
513
+ };
514
+ }
515
+ if (!(source instanceof TableSource)) {
516
+ return {
517
+ code: "UNSUPPORTED_SOURCE",
518
+ reason: "Only CTE and simple derived-table sources can receive moved static predicates."
519
+ };
520
+ }
521
+ const cteName = source.table.name;
522
+ const commonTable = this.findCte(root, cteName);
523
+ if (!commonTable) {
524
+ return {
525
+ code: "NO_SAFE_UPSTREAM_QUERY",
526
+ reason: "The referenced source is a base table, so there is no upstream query block to move into."
527
+ };
528
+ }
529
+ const referenceCount = this.countTableSourceReferences(root, cteName);
530
+ if (referenceCount !== 1) {
531
+ return {
532
+ code: "CTE_REUSE_UNSUPPORTED",
533
+ reason: `CTE '${cteName}' is referenced ${referenceCount} times; moving a predicate into it may affect other consumers.`
534
+ };
535
+ }
536
+ if (commonTable.query instanceof BinarySelectQuery) {
537
+ return {
538
+ code: "UNION_BOUNDARY",
539
+ reason: "Predicate would need distribution into a UNION branch, which is unsupported."
540
+ };
541
+ }
542
+ if (!(commonTable.query instanceof SimpleSelectQuery)) {
543
+ return {
544
+ code: "UNSUPPORTED_CTE_QUERY",
545
+ reason: "Writable or non-select CTE bodies are not moved into by static predicate placement."
546
+ };
547
+ }
548
+ return {
549
+ query: commonTable.query,
550
+ scopeId: `cte:${commonTable.getSourceAliasName()}`,
551
+ sourceBinding: binding,
552
+ cteName: commonTable.getSourceAliasName()
553
+ };
554
+ }
555
+ resolveTargetColumns(query, references) {
556
+ const resolved = [];
557
+ for (const reference of references) {
558
+ const matches = query.selectClause.items.filter(item => { var _a; return normalizeIdentifier((_a = this.getSelectItemOutputName(item)) !== null && _a !== void 0 ? _a : "") === normalizeIdentifier(reference.column.name); });
559
+ if (matches.length !== 1) {
560
+ return {
561
+ code: "AMBIGUOUS_TARGET_COLUMN",
562
+ reason: matches.length === 0
563
+ ? `Target query does not expose '${reference.column.name}' as a direct output column.`
564
+ : `Target query exposes multiple '${reference.column.name}' columns.`
565
+ };
566
+ }
567
+ const value = matches[0].value;
568
+ if (!(value instanceof ColumnReference)) {
569
+ return {
570
+ code: "EXPRESSION_OUTPUT_UNSUPPORTED",
571
+ reason: `Target output '${reference.column.name}' is an expression, not a direct column reference.`
572
+ };
573
+ }
574
+ const sourceResolution = this.verifyColumnResolvableInQuery(query, value);
575
+ if (sourceResolution) {
576
+ return sourceResolution;
577
+ }
578
+ resolved.push({
579
+ sourceColumn: reference,
580
+ targetColumn: value
581
+ });
582
+ }
583
+ return resolved;
584
+ }
585
+ verifyColumnResolvableInQuery(query, column) {
586
+ if (!query.fromClause) {
587
+ return {
588
+ code: "NO_TARGET_FROM_CLAUSE",
589
+ reason: "Target query has no FROM clause where the moved column can be resolved."
590
+ };
591
+ }
592
+ const bindings = this.getSourceBindings(query.fromClause);
593
+ const namespace = normalizeIdentifier(column.getNamespace());
594
+ if (namespace) {
595
+ const matches = bindings.filter(binding => normalizeIdentifier(binding.alias) === namespace);
596
+ return matches.length === 1
597
+ ? null
598
+ : {
599
+ code: "AMBIGUOUS_TARGET_COLUMN",
600
+ reason: `Target column '${columnReferenceText(column)}' is not uniquely resolvable in the destination query.`
601
+ };
602
+ }
603
+ if (bindings.length === 1) {
604
+ return null;
605
+ }
606
+ return {
607
+ code: "AMBIGUOUS_TARGET_COLUMN",
608
+ reason: `Unqualified target column '${column.column.name}' is ambiguous in a multi-source destination query.`
609
+ };
610
+ }
611
+ rebasePredicate(expression, targetColumns) {
612
+ const cloned = cloneValueComponent(expression);
613
+ const visitSelect = (query, inheritedLocalAliases) => {
614
+ var _a, _b, _c, _d;
615
+ if (!(query instanceof SimpleSelectQuery)) {
616
+ return;
617
+ }
618
+ const localAliases = new Set(inheritedLocalAliases);
619
+ for (const alias of this.collectSourceAliases(query)) {
620
+ localAliases.add(alias);
621
+ }
622
+ query.selectClause.items.forEach(item => visit(item.value, localAliases));
623
+ for (const join of (_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.joins) !== null && _b !== void 0 ? _b : []) {
624
+ if (join.condition) {
625
+ visit(join.condition.condition, localAliases);
626
+ }
627
+ const source = join.source.datasource;
628
+ if (source instanceof SubQuerySource) {
629
+ visitSelect(source.query, localAliases);
630
+ }
631
+ }
632
+ if (query.whereClause) {
633
+ visit(query.whereClause.condition, localAliases);
634
+ }
635
+ if (query.havingClause) {
636
+ visit(query.havingClause.condition, localAliases);
637
+ }
638
+ for (const cte of (_d = (_c = query.withClause) === null || _c === void 0 ? void 0 : _c.tables) !== null && _d !== void 0 ? _d : []) {
639
+ if (cte.query instanceof SimpleSelectQuery || cte.query instanceof BinarySelectQuery) {
640
+ visitSelect(cte.query, localAliases);
641
+ }
642
+ }
643
+ };
644
+ const visit = (value, localAliases) => {
645
+ const candidate = unwrapParens(value);
646
+ if (candidate instanceof ColumnReference) {
647
+ const namespace = normalizeIdentifier(candidate.getNamespace());
648
+ if (namespace && localAliases.has(namespace)) {
649
+ return;
650
+ }
651
+ const target = targetColumns.find(item => sameColumnReference(candidate, item.sourceColumn));
652
+ if (target) {
653
+ candidate.qualifiedName = cloneColumnReference(target.targetColumn).qualifiedName;
654
+ }
655
+ return;
656
+ }
657
+ if (candidate instanceof BinaryExpression) {
658
+ visit(candidate.left, localAliases);
659
+ visit(candidate.right, localAliases);
660
+ return;
661
+ }
662
+ if (candidate instanceof UnaryExpression) {
663
+ visit(candidate.expression, localAliases);
664
+ return;
665
+ }
666
+ if (candidate instanceof InlineQuery) {
667
+ visitSelect(candidate.selectQuery, localAliases);
668
+ return;
669
+ }
670
+ if (candidate instanceof FunctionCall) {
671
+ if (candidate.argument) {
672
+ visit(candidate.argument, localAliases);
673
+ }
674
+ if (candidate.filterCondition) {
675
+ visit(candidate.filterCondition, localAliases);
676
+ }
677
+ return;
678
+ }
679
+ if (candidate instanceof CastExpression) {
680
+ visit(candidate.input, localAliases);
681
+ return;
682
+ }
683
+ if (candidate instanceof CaseExpression) {
684
+ if (candidate.condition) {
685
+ visit(candidate.condition, localAliases);
686
+ }
687
+ for (const pair of candidate.switchCase.cases) {
688
+ visit(pair.key, localAliases);
689
+ visit(pair.value, localAliases);
690
+ }
691
+ if (candidate.switchCase.elseValue) {
692
+ visit(candidate.switchCase.elseValue, localAliases);
693
+ }
694
+ return;
695
+ }
696
+ if (candidate instanceof BetweenExpression) {
697
+ visit(candidate.expression, localAliases);
698
+ visit(candidate.lower, localAliases);
699
+ visit(candidate.upper, localAliases);
700
+ return;
701
+ }
702
+ if (candidate instanceof JsonPredicateExpression) {
703
+ visit(candidate.expression, localAliases);
704
+ return;
705
+ }
706
+ if (candidate instanceof ArrayExpression) {
707
+ visit(candidate.expression, localAliases);
708
+ return;
709
+ }
710
+ if (candidate instanceof ValueList) {
711
+ candidate.values.forEach(item => visit(item, localAliases));
712
+ return;
713
+ }
714
+ if (candidate instanceof TupleExpression) {
715
+ candidate.values.forEach(item => visit(item, localAliases));
716
+ return;
717
+ }
718
+ if (candidate instanceof TypeValue && candidate.argument) {
719
+ visit(candidate.argument, localAliases);
720
+ }
721
+ };
722
+ visit(cloned, new Set());
723
+ return cloned;
724
+ }
725
+ getSourceBindings(fromClause) {
726
+ var _a, _b, _c;
727
+ const bindings = [{
728
+ source: fromClause.source,
729
+ alias: (_a = fromClause.source.getAliasName()) !== null && _a !== void 0 ? _a : "",
730
+ join: null,
731
+ joinIndex: -1,
732
+ isPrimary: true
733
+ }];
734
+ for (let index = 0; index < ((_b = fromClause.joins) !== null && _b !== void 0 ? _b : []).length; index += 1) {
735
+ const join = fromClause.joins[index];
736
+ bindings.push({
737
+ source: join.source,
738
+ alias: (_c = join.source.getAliasName()) !== null && _c !== void 0 ? _c : "",
739
+ join,
740
+ joinIndex: index,
741
+ isPrimary: false
742
+ });
743
+ }
744
+ return bindings;
745
+ }
746
+ findNullableSideBoundary(fromClause, binding) {
747
+ var _a;
748
+ const joins = (_a = fromClause.joins) !== null && _a !== void 0 ? _a : [];
749
+ if (binding.isPrimary) {
750
+ return this.hasLaterJoinThatNullsPriorSources(joins, -1)
751
+ ? {
752
+ code: "OUTER_JOIN_NULLABLE_SIDE",
753
+ reason: "Predicate crosses OUTER JOIN nullable side; moving it may change semantics."
754
+ }
755
+ : null;
756
+ }
757
+ const join = binding.join;
758
+ if (!join) {
759
+ return null;
760
+ }
761
+ const joinType = join.joinType.value.toLowerCase();
762
+ if (joinType.includes("left")) {
763
+ return {
764
+ code: "OUTER_JOIN_NULLABLE_SIDE",
765
+ reason: "Predicate crosses LEFT JOIN nullable side; moving it may change semantics."
766
+ };
767
+ }
768
+ if (joinType.includes("full")) {
769
+ return {
770
+ code: "OUTER_JOIN_NULLABLE_SIDE",
771
+ reason: "Predicate crosses FULL JOIN nullable side; moving it may change semantics."
772
+ };
773
+ }
774
+ if (this.hasLaterJoinThatNullsPriorSources(joins, binding.joinIndex)) {
775
+ return {
776
+ code: "OUTER_JOIN_NULLABLE_SIDE",
777
+ reason: "Predicate crosses later RIGHT/FULL JOIN nullable side; moving it may change semantics."
778
+ };
779
+ }
780
+ return null;
781
+ }
782
+ hasLaterJoinThatNullsPriorSources(joins, sourceJoinIndex) {
783
+ return joins
784
+ .slice(sourceJoinIndex + 1)
785
+ .some(join => {
786
+ const joinType = join.joinType.value.toLowerCase();
787
+ return joinType.includes("right") || joinType.includes("full");
788
+ });
789
+ }
790
+ hasOuterJoin(fromClause) {
791
+ var _a;
792
+ return ((_a = fromClause.joins) !== null && _a !== void 0 ? _a : []).some(join => {
793
+ const joinType = join.joinType.value.toLowerCase();
794
+ return joinType.includes("left") || joinType.includes("right") || joinType.includes("full") || joinType.includes("outer");
795
+ });
796
+ }
797
+ getOutputColumnMatchCount(root, binding, columnName) {
798
+ const target = this.resolveSourceQueryForColumns(root, binding.source);
799
+ if (!target || !(target instanceof SimpleSelectQuery)) {
800
+ return 0;
801
+ }
802
+ return target.selectClause.items.filter(item => { var _a; return normalizeIdentifier((_a = this.getSelectItemOutputName(item)) !== null && _a !== void 0 ? _a : "") === normalizeIdentifier(columnName); }).length;
803
+ }
804
+ resolveSourceQueryForColumns(root, source) {
805
+ var _a;
806
+ if (source.datasource instanceof SubQuerySource) {
807
+ return source.datasource.query;
808
+ }
809
+ if (source.datasource instanceof TableSource) {
810
+ const cteQuery = (_a = this.findCte(root, source.datasource.table.name)) === null || _a === void 0 ? void 0 : _a.query;
811
+ return cteQuery instanceof SimpleSelectQuery || cteQuery instanceof BinarySelectQuery
812
+ ? cteQuery
813
+ : null;
814
+ }
815
+ return null;
816
+ }
817
+ getSelectItemOutputName(item) {
818
+ if (item.identifier) {
819
+ return item.identifier.name;
820
+ }
821
+ if (item.value instanceof ColumnReference) {
822
+ return item.value.column.name;
823
+ }
824
+ return null;
825
+ }
826
+ findCte(root, name) {
827
+ var _a, _b;
828
+ const normalized = normalizeIdentifier(name);
829
+ const matches = ((_b = (_a = root.withClause) === null || _a === void 0 ? void 0 : _a.tables) !== null && _b !== void 0 ? _b : [])
830
+ .filter(table => normalizeIdentifier(table.getSourceAliasName()) === normalized);
831
+ return matches.length === 1 ? matches[0] : null;
832
+ }
833
+ countTableSourceReferences(query, tableName) {
834
+ const normalized = normalizeIdentifier(tableName);
835
+ let count = 0;
836
+ const visitSelect = (select) => {
837
+ var _a, _b;
838
+ if (select instanceof BinarySelectQuery) {
839
+ visitSelect(select.left);
840
+ visitSelect(select.right);
841
+ return;
842
+ }
843
+ if (!(select instanceof SimpleSelectQuery)) {
844
+ return;
845
+ }
846
+ if (select.fromClause) {
847
+ for (const binding of this.getSourceBindings(select.fromClause)) {
848
+ const source = binding.source.datasource;
849
+ if (source instanceof TableSource && normalizeIdentifier(source.table.name) === normalized) {
850
+ count += 1;
851
+ }
852
+ if (source instanceof SubQuerySource) {
853
+ visitSelect(source.query);
854
+ }
855
+ }
856
+ }
857
+ for (const cte of (_b = (_a = select.withClause) === null || _a === void 0 ? void 0 : _a.tables) !== null && _b !== void 0 ? _b : []) {
858
+ if (cte.query instanceof SimpleSelectQuery || cte.query instanceof BinarySelectQuery) {
859
+ visitSelect(cte.query);
860
+ }
861
+ }
862
+ };
863
+ visitSelect(query);
864
+ return count;
865
+ }
866
+ collectSourceAliases(query) {
867
+ var _a, _b;
868
+ const aliases = new Set();
869
+ for (const source of (_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.getSources()) !== null && _b !== void 0 ? _b : []) {
870
+ const alias = source.getAliasName();
871
+ if (alias) {
872
+ aliases.add(normalizeIdentifier(alias));
873
+ }
874
+ }
875
+ return aliases;
876
+ }
877
+ collectRootColumnReferences(root, expression) {
878
+ const references = [];
879
+ const rootAliases = this.collectSourceAliases(root);
880
+ const visitSelect = (query, inheritedLocalAliases) => {
881
+ var _a, _b, _c, _d;
882
+ if (!(query instanceof SimpleSelectQuery)) {
883
+ return;
884
+ }
885
+ const localAliases = new Set(inheritedLocalAliases);
886
+ for (const alias of this.collectSourceAliases(query)) {
887
+ localAliases.add(alias);
888
+ }
889
+ query.selectClause.items.forEach(item => visit(item.value, localAliases));
890
+ for (const join of (_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.joins) !== null && _b !== void 0 ? _b : []) {
891
+ if (join.condition) {
892
+ visit(join.condition.condition, localAliases);
893
+ }
894
+ const source = join.source.datasource;
895
+ if (source instanceof SubQuerySource) {
896
+ visitSelect(source.query, localAliases);
897
+ }
898
+ }
899
+ if (query.whereClause) {
900
+ visit(query.whereClause.condition, localAliases);
901
+ }
902
+ if (query.havingClause) {
903
+ visit(query.havingClause.condition, localAliases);
904
+ }
905
+ for (const cte of (_d = (_c = query.withClause) === null || _c === void 0 ? void 0 : _c.tables) !== null && _d !== void 0 ? _d : []) {
906
+ if (cte.query instanceof SimpleSelectQuery || cte.query instanceof BinarySelectQuery) {
907
+ visitSelect(cte.query, localAliases);
908
+ }
909
+ }
910
+ };
911
+ const collect = (reference) => {
912
+ if (!references.some(existing => sameColumnReference(existing, reference))) {
913
+ references.push(reference);
914
+ }
915
+ };
916
+ const visit = (value, localAliases) => {
917
+ const candidate = unwrapParens(value);
918
+ if (candidate instanceof ColumnReference) {
919
+ const namespace = normalizeIdentifier(candidate.getNamespace());
920
+ if (namespace) {
921
+ if (rootAliases.has(namespace) && !localAliases.has(namespace)) {
922
+ collect(candidate);
923
+ }
924
+ return;
925
+ }
926
+ if (localAliases.size === 0) {
927
+ collect(candidate);
928
+ }
929
+ return;
930
+ }
931
+ if (candidate instanceof BinaryExpression) {
932
+ visit(candidate.left, localAliases);
933
+ visit(candidate.right, localAliases);
934
+ return;
935
+ }
936
+ if (candidate instanceof UnaryExpression) {
937
+ visit(candidate.expression, localAliases);
938
+ return;
939
+ }
940
+ if (candidate instanceof InlineQuery) {
941
+ visitSelect(candidate.selectQuery, localAliases);
942
+ return;
943
+ }
944
+ if (candidate instanceof FunctionCall) {
945
+ if (candidate.argument) {
946
+ visit(candidate.argument, localAliases);
947
+ }
948
+ if (candidate.filterCondition) {
949
+ visit(candidate.filterCondition, localAliases);
950
+ }
951
+ return;
952
+ }
953
+ if (candidate instanceof CastExpression) {
954
+ visit(candidate.input, localAliases);
955
+ return;
956
+ }
957
+ if (candidate instanceof CaseExpression) {
958
+ if (candidate.condition) {
959
+ visit(candidate.condition, localAliases);
960
+ }
961
+ for (const pair of candidate.switchCase.cases) {
962
+ visit(pair.key, localAliases);
963
+ visit(pair.value, localAliases);
964
+ }
965
+ if (candidate.switchCase.elseValue) {
966
+ visit(candidate.switchCase.elseValue, localAliases);
967
+ }
968
+ return;
969
+ }
970
+ if (candidate instanceof BetweenExpression) {
971
+ visit(candidate.expression, localAliases);
972
+ visit(candidate.lower, localAliases);
973
+ visit(candidate.upper, localAliases);
974
+ return;
975
+ }
976
+ if (candidate instanceof JsonPredicateExpression) {
977
+ visit(candidate.expression, localAliases);
978
+ return;
979
+ }
980
+ if (candidate instanceof ArrayExpression) {
981
+ visit(candidate.expression, localAliases);
982
+ return;
983
+ }
984
+ if (candidate instanceof ArrayQueryExpression) {
985
+ visitSelect(candidate.query, localAliases);
986
+ return;
987
+ }
988
+ if (candidate instanceof ValueList) {
989
+ candidate.values.forEach(item => visit(item, localAliases));
990
+ return;
991
+ }
992
+ if (candidate instanceof TupleExpression) {
993
+ candidate.values.forEach(item => visit(item, localAliases));
994
+ return;
995
+ }
996
+ if (candidate instanceof TypeValue && candidate.argument) {
997
+ visit(candidate.argument, localAliases);
998
+ }
999
+ };
1000
+ visit(expression, new Set());
1001
+ return references;
1002
+ }
1003
+ hasWindowUsage(query) {
1004
+ if (query.windowClause) {
1005
+ return true;
1006
+ }
1007
+ let found = false;
1008
+ const visit = (value) => {
1009
+ if (found) {
1010
+ return;
1011
+ }
1012
+ const candidate = unwrapParens(value);
1013
+ if (candidate instanceof FunctionCall) {
1014
+ if (candidate.over) {
1015
+ found = true;
1016
+ return;
1017
+ }
1018
+ if (candidate.argument) {
1019
+ visit(candidate.argument);
1020
+ }
1021
+ if (candidate.filterCondition) {
1022
+ visit(candidate.filterCondition);
1023
+ }
1024
+ return;
1025
+ }
1026
+ if (candidate instanceof BinaryExpression) {
1027
+ visit(candidate.left);
1028
+ visit(candidate.right);
1029
+ return;
1030
+ }
1031
+ if (candidate instanceof UnaryExpression) {
1032
+ visit(candidate.expression);
1033
+ return;
1034
+ }
1035
+ if (candidate instanceof CastExpression) {
1036
+ visit(candidate.input);
1037
+ return;
1038
+ }
1039
+ if (candidate instanceof CaseExpression) {
1040
+ if (candidate.condition) {
1041
+ visit(candidate.condition);
1042
+ }
1043
+ for (const pair of candidate.switchCase.cases) {
1044
+ visit(pair.key);
1045
+ visit(pair.value);
1046
+ }
1047
+ if (candidate.switchCase.elseValue) {
1048
+ visit(candidate.switchCase.elseValue);
1049
+ }
1050
+ return;
1051
+ }
1052
+ if (candidate instanceof ValueList) {
1053
+ candidate.values.forEach(visit);
1054
+ }
1055
+ };
1056
+ query.selectClause.items.forEach(item => visit(item.value));
1057
+ return found;
1058
+ }
1059
+ collectParameterNames(expression) {
1060
+ const names = [];
1061
+ const visitSelect = (query) => {
1062
+ var _a, _b, _c, _d;
1063
+ if (query instanceof BinarySelectQuery) {
1064
+ visitSelect(query.left);
1065
+ visitSelect(query.right);
1066
+ return;
1067
+ }
1068
+ if (!(query instanceof SimpleSelectQuery)) {
1069
+ return;
1070
+ }
1071
+ query.selectClause.items.forEach(item => visit(item.value));
1072
+ for (const join of (_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.joins) !== null && _b !== void 0 ? _b : []) {
1073
+ if (join.condition) {
1074
+ visit(join.condition.condition);
1075
+ }
1076
+ const source = join.source.datasource;
1077
+ if (source instanceof SubQuerySource) {
1078
+ visitSelect(source.query);
1079
+ }
1080
+ }
1081
+ if (query.whereClause) {
1082
+ visit(query.whereClause.condition);
1083
+ }
1084
+ if (query.havingClause) {
1085
+ visit(query.havingClause.condition);
1086
+ }
1087
+ for (const cte of (_d = (_c = query.withClause) === null || _c === void 0 ? void 0 : _c.tables) !== null && _d !== void 0 ? _d : []) {
1088
+ if (cte.query instanceof SimpleSelectQuery || cte.query instanceof BinarySelectQuery) {
1089
+ visitSelect(cte.query);
1090
+ }
1091
+ }
1092
+ };
1093
+ const visit = (value) => {
1094
+ const candidate = unwrapParens(value);
1095
+ if (candidate instanceof ParameterExpression) {
1096
+ appendUnique(names, candidate.name.value);
1097
+ return;
1098
+ }
1099
+ if (candidate instanceof BinaryExpression) {
1100
+ visit(candidate.left);
1101
+ visit(candidate.right);
1102
+ return;
1103
+ }
1104
+ if (candidate instanceof UnaryExpression) {
1105
+ visit(candidate.expression);
1106
+ return;
1107
+ }
1108
+ if (candidate instanceof InlineQuery) {
1109
+ visitSelect(candidate.selectQuery);
1110
+ return;
1111
+ }
1112
+ if (candidate instanceof ArrayQueryExpression) {
1113
+ visitSelect(candidate.query);
1114
+ return;
1115
+ }
1116
+ if (candidate instanceof FunctionCall) {
1117
+ if (candidate.argument) {
1118
+ visit(candidate.argument);
1119
+ }
1120
+ if (candidate.filterCondition) {
1121
+ visit(candidate.filterCondition);
1122
+ }
1123
+ return;
1124
+ }
1125
+ if (candidate instanceof CastExpression) {
1126
+ visit(candidate.input);
1127
+ return;
1128
+ }
1129
+ if (candidate instanceof CaseExpression) {
1130
+ if (candidate.condition) {
1131
+ visit(candidate.condition);
1132
+ }
1133
+ for (const pair of candidate.switchCase.cases) {
1134
+ visit(pair.key);
1135
+ visit(pair.value);
1136
+ }
1137
+ if (candidate.switchCase.elseValue) {
1138
+ visit(candidate.switchCase.elseValue);
1139
+ }
1140
+ return;
1141
+ }
1142
+ if (candidate instanceof BetweenExpression) {
1143
+ visit(candidate.expression);
1144
+ visit(candidate.lower);
1145
+ visit(candidate.upper);
1146
+ return;
1147
+ }
1148
+ if (candidate instanceof JsonPredicateExpression) {
1149
+ visit(candidate.expression);
1150
+ return;
1151
+ }
1152
+ if (candidate instanceof ArrayExpression) {
1153
+ visit(candidate.expression);
1154
+ return;
1155
+ }
1156
+ if (candidate instanceof ValueList) {
1157
+ candidate.values.forEach(visit);
1158
+ return;
1159
+ }
1160
+ if (candidate instanceof TupleExpression) {
1161
+ candidate.values.forEach(visit);
1162
+ return;
1163
+ }
1164
+ if (candidate instanceof TypeValue && candidate.argument) {
1165
+ visit(candidate.argument);
1166
+ }
1167
+ };
1168
+ visit(expression);
1169
+ return names;
1170
+ }
1171
+ makeSkipped(expression, draft) {
1172
+ return Object.assign({ predicateSql: formatSqlComponent(expression), scopeId: "scope:root" }, draft);
1173
+ }
1174
+ buildResult(params) {
1175
+ return {
1176
+ ok: params.errors.length === 0,
1177
+ sql: params.sql,
1178
+ applied: params.applied,
1179
+ skipped: params.skipped,
1180
+ warnings: params.warnings,
1181
+ errors: params.errors,
1182
+ safety: {
1183
+ mode: "safe_only",
1184
+ unsafeRewriteApplied: false,
1185
+ dryRun: params.dryRun,
1186
+ formatterGeneratedSource: params.formatterGeneratedSource
1187
+ },
1188
+ staticPredicateMoves: params.applied
1189
+ };
1190
+ }
1191
+ }
1192
+ export const planStaticPredicatePlacement = (input, options = {}) => {
1193
+ return new StaticPredicatePlacementOptimizer().plan(input, options);
1194
+ };
1195
+ export const optimizeStaticPredicatePlacement = (input, options = {}) => {
1196
+ return new StaticPredicatePlacementOptimizer().optimize(input, options);
1197
+ };
1198
+ //# sourceMappingURL=StaticPredicatePlacementOptimizer.js.map