rawsql-ts 0.11.25-beta → 0.11.27-beta
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/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +23 -23
- package/dist/esm/index.min.js.map +4 -4
- package/dist/esm/src/index.d.ts +2 -0
- package/dist/esm/src/index.js +2 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/transformers/CTEDependencyAnalyzer.d.ts +1 -0
- package/dist/esm/src/transformers/CTEDependencyAnalyzer.js +10 -2
- package/dist/esm/src/transformers/CTEDependencyAnalyzer.js.map +1 -1
- package/dist/esm/src/transformers/CTEQueryDecomposer.d.ts +6 -0
- package/dist/esm/src/transformers/CTEQueryDecomposer.js +79 -1
- package/dist/esm/src/transformers/CTEQueryDecomposer.js.map +1 -1
- package/dist/esm/src/transformers/CTETableReferenceCollector.d.ts +87 -0
- package/dist/esm/src/transformers/CTETableReferenceCollector.js +354 -0
- package/dist/esm/src/transformers/CTETableReferenceCollector.js.map +1 -0
- package/dist/esm/src/transformers/JoinAggregationDecomposer.d.ts +188 -0
- package/dist/esm/src/transformers/JoinAggregationDecomposer.js +490 -0
- package/dist/esm/src/transformers/JoinAggregationDecomposer.js.map +1 -0
- package/dist/esm/src/transformers/QueryBuilder.d.ts +22 -0
- package/dist/esm/src/transformers/QueryBuilder.js +49 -3
- package/dist/esm/src/transformers/QueryBuilder.js.map +1 -1
- package/dist/esm/src/transformers/TableSourceCollector.js +2 -2
- package/dist/esm/src/transformers/TableSourceCollector.js.map +1 -1
- package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
- package/dist/index.min.js +25 -25
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/transformers/CTEDependencyAnalyzer.d.ts +1 -0
- package/dist/src/transformers/CTEDependencyAnalyzer.js +10 -2
- package/dist/src/transformers/CTEDependencyAnalyzer.js.map +1 -1
- package/dist/src/transformers/CTEQueryDecomposer.d.ts +6 -0
- package/dist/src/transformers/CTEQueryDecomposer.js +79 -1
- package/dist/src/transformers/CTEQueryDecomposer.js.map +1 -1
- package/dist/src/transformers/CTETableReferenceCollector.d.ts +87 -0
- package/dist/src/transformers/CTETableReferenceCollector.js +358 -0
- package/dist/src/transformers/CTETableReferenceCollector.js.map +1 -0
- package/dist/src/transformers/JoinAggregationDecomposer.d.ts +188 -0
- package/dist/src/transformers/JoinAggregationDecomposer.js +497 -0
- package/dist/src/transformers/JoinAggregationDecomposer.js.map +1 -0
- package/dist/src/transformers/QueryBuilder.d.ts +22 -0
- package/dist/src/transformers/QueryBuilder.js +49 -3
- package/dist/src/transformers/QueryBuilder.js.map +1 -1
- package/dist/src/transformers/TableSourceCollector.js +2 -2
- package/dist/src/transformers/TableSourceCollector.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { SimpleSelectQuery } from "../models/SimpleSelectQuery";
|
|
2
|
+
import { SelectClause, SelectItem, FromClause, WithClause, CommonTable, GroupByClause, SourceExpression, TableSource } from "../models/Clause";
|
|
3
|
+
import { FunctionCall, ColumnReference, IdentifierString } from "../models/ValueComponent";
|
|
4
|
+
import { SqlFormatter } from "./SqlFormatter";
|
|
5
|
+
/**
|
|
6
|
+
* Error thrown when query decomposition fails
|
|
7
|
+
*/
|
|
8
|
+
export class DecompositionError extends Error {
|
|
9
|
+
constructor(message, originalQuery, cause) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.originalQuery = originalQuery;
|
|
12
|
+
this.cause = cause;
|
|
13
|
+
this.name = 'DecompositionError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decomposes queries that combine table joins with aggregations into separate detail and aggregation queries using CTEs
|
|
18
|
+
*
|
|
19
|
+
* This transformer separates JOIN operations from aggregation operations to make queries easier to debug:
|
|
20
|
+
* - Detail query: Contains JOINs and column selection
|
|
21
|
+
* - Aggregation query: Contains GROUP BY and aggregation functions, referencing the CTE
|
|
22
|
+
*
|
|
23
|
+
* Provides two patterns following existing codebase conventions:
|
|
24
|
+
* - analyze(): Safe analysis (Result pattern like SelectQueryParser.analyze)
|
|
25
|
+
* - decompose(): Direct decomposition with exceptions (Exception pattern like SelectQueryParser.parse)
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const decomposer = new JoinAggregationDecomposer();
|
|
30
|
+
*
|
|
31
|
+
* // Safe analysis (Result pattern)
|
|
32
|
+
* const analysis = decomposer.analyze(query);
|
|
33
|
+
* if (analysis.success) {
|
|
34
|
+
* console.log('Can decompose with', analysis.metadata.joinCount, 'joins');
|
|
35
|
+
* if (analysis.limitations) {
|
|
36
|
+
* console.log('Known limitations:', analysis.limitations);
|
|
37
|
+
* }
|
|
38
|
+
* } else {
|
|
39
|
+
* console.log('Cannot decompose:', analysis.error);
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // Direct decomposition (Exception pattern)
|
|
43
|
+
* try {
|
|
44
|
+
* const decomposed = decomposer.decompose(query);
|
|
45
|
+
* // Success: decomposed query ready to use
|
|
46
|
+
* } catch (error) {
|
|
47
|
+
* if (error instanceof DecompositionError) {
|
|
48
|
+
* console.log('Decomposition failed:', error.message);
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export class JoinAggregationDecomposer {
|
|
54
|
+
constructor(options = {}) {
|
|
55
|
+
this.options = {
|
|
56
|
+
detailCTEName: options.detailCTEName || "detail_data"
|
|
57
|
+
};
|
|
58
|
+
this.formatter = new SqlFormatter({ identifierEscape: { start: "", end: "" } });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Analyzes a query for decomposition without throwing errors (safe analysis)
|
|
62
|
+
* Follows the same pattern as SelectQueryParser.analyze()
|
|
63
|
+
*
|
|
64
|
+
* @param query The query to analyze
|
|
65
|
+
* @returns Analysis result with success status, error information, and metadata
|
|
66
|
+
*/
|
|
67
|
+
analyze(query) {
|
|
68
|
+
const metadata = this.extractMetadata(query);
|
|
69
|
+
try {
|
|
70
|
+
// Phase 1: Validate query structure
|
|
71
|
+
const validationError = this.getValidationError(query, metadata);
|
|
72
|
+
if (validationError) {
|
|
73
|
+
return {
|
|
74
|
+
success: false,
|
|
75
|
+
error: validationError,
|
|
76
|
+
metadata
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Phase 2: Attempt decomposition
|
|
80
|
+
const decomposed = this.performDecomposition(query);
|
|
81
|
+
// Phase 3: Check for formatting issues (critical validation)
|
|
82
|
+
try {
|
|
83
|
+
this.formatter.format(decomposed);
|
|
84
|
+
}
|
|
85
|
+
catch (formatError) {
|
|
86
|
+
return {
|
|
87
|
+
success: false,
|
|
88
|
+
error: `Decomposed query cannot be formatted: ${formatError instanceof Error ? formatError.message : String(formatError)}. This usually indicates complex expressions in aggregations that are not supported.`,
|
|
89
|
+
metadata
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
// Check for known limitations
|
|
93
|
+
const limitations = this.detectLimitations(metadata);
|
|
94
|
+
return {
|
|
95
|
+
success: true,
|
|
96
|
+
decomposedQuery: decomposed,
|
|
97
|
+
limitations: limitations.length > 0 ? limitations : undefined,
|
|
98
|
+
metadata
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
return {
|
|
103
|
+
success: false,
|
|
104
|
+
error: `Analysis failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
105
|
+
metadata
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Decomposes a JOIN + aggregation query into separate detail and aggregation queries
|
|
111
|
+
* Follows the same pattern as SelectQueryParser.parse() - throws on error
|
|
112
|
+
*
|
|
113
|
+
* @param query The query to decompose
|
|
114
|
+
* @returns The decomposed query with CTE structure
|
|
115
|
+
* @throws DecompositionError if the query cannot be decomposed or formatted
|
|
116
|
+
*/
|
|
117
|
+
decompose(query) {
|
|
118
|
+
try {
|
|
119
|
+
// Phase 1: Validate query structure
|
|
120
|
+
const metadata = this.extractMetadata(query);
|
|
121
|
+
const validationError = this.getValidationError(query, metadata);
|
|
122
|
+
if (validationError) {
|
|
123
|
+
throw new DecompositionError(validationError, query);
|
|
124
|
+
}
|
|
125
|
+
// Phase 2: Perform decomposition
|
|
126
|
+
const decomposed = this.performDecomposition(query);
|
|
127
|
+
// Phase 3: Critical validation - ensure the result can be formatted
|
|
128
|
+
try {
|
|
129
|
+
this.formatter.format(decomposed);
|
|
130
|
+
}
|
|
131
|
+
catch (formatError) {
|
|
132
|
+
throw new DecompositionError(`Decomposed query cannot be formatted: ${formatError instanceof Error ? formatError.message : String(formatError)}. ` +
|
|
133
|
+
`This usually indicates complex expressions in aggregations that are not supported.`, query, formatError instanceof Error ? formatError : undefined);
|
|
134
|
+
}
|
|
135
|
+
return decomposed;
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error instanceof DecompositionError) {
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
throw new DecompositionError(`Decomposition failed: ${error instanceof Error ? error.message : String(error)}`, query, error instanceof Error ? error : undefined);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Gets validation error message without throwing (for analyze method)
|
|
146
|
+
*/
|
|
147
|
+
getValidationError(query, metadata) {
|
|
148
|
+
if (!query.fromClause) {
|
|
149
|
+
return "Query does not contain FROM clause";
|
|
150
|
+
}
|
|
151
|
+
if (metadata.joinCount === 0) {
|
|
152
|
+
return "Query does not contain JOINs";
|
|
153
|
+
}
|
|
154
|
+
if (metadata.aggregationCount === 0 && !query.groupByClause) {
|
|
155
|
+
return "Query does not contain GROUP BY or aggregation functions";
|
|
156
|
+
}
|
|
157
|
+
if (metadata.hasWindowFunctions) {
|
|
158
|
+
return "Window functions are not fully supported - column references in window functions are not converted to CTE references";
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Performs the actual decomposition
|
|
164
|
+
*/
|
|
165
|
+
performDecomposition(query) {
|
|
166
|
+
// Extract columns needed for detail CTE
|
|
167
|
+
const detailColumns = this.extractDetailColumns(query);
|
|
168
|
+
// Build detail query (CTE)
|
|
169
|
+
const detailQuery = this.buildDetailQuery(query, detailColumns);
|
|
170
|
+
// Build aggregation query
|
|
171
|
+
const aggregationQuery = this.buildAggregationQuery(query);
|
|
172
|
+
// Create WITH clause
|
|
173
|
+
const withClause = new WithClause(false, // not recursive
|
|
174
|
+
[
|
|
175
|
+
new CommonTable(detailQuery, this.options.detailCTEName, null // not materialized
|
|
176
|
+
)
|
|
177
|
+
]);
|
|
178
|
+
// Combine into final query
|
|
179
|
+
aggregationQuery.withClause = withClause;
|
|
180
|
+
return aggregationQuery;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Extracts metadata about the query
|
|
184
|
+
*/
|
|
185
|
+
extractMetadata(query) {
|
|
186
|
+
const joinCount = this.countJoins(query);
|
|
187
|
+
const aggregationCount = this.countAggregationFunctions(query);
|
|
188
|
+
const hasHaving = !!query.havingClause;
|
|
189
|
+
const hasOrderBy = !!query.orderByClause;
|
|
190
|
+
const hasWindowFunctions = this.hasWindowFunctions(query);
|
|
191
|
+
const detailColumns = this.extractDetailColumnNames(query);
|
|
192
|
+
return {
|
|
193
|
+
joinCount,
|
|
194
|
+
aggregationCount,
|
|
195
|
+
detailColumns,
|
|
196
|
+
hasHaving,
|
|
197
|
+
hasOrderBy,
|
|
198
|
+
hasWindowFunctions
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Detects known limitations based on metadata
|
|
203
|
+
*/
|
|
204
|
+
detectLimitations(metadata) {
|
|
205
|
+
const limitations = [];
|
|
206
|
+
if (metadata.hasWindowFunctions && metadata.aggregationCount > 0) {
|
|
207
|
+
limitations.push("Window functions may reference original table columns instead of CTE columns");
|
|
208
|
+
}
|
|
209
|
+
if (metadata.hasHaving) {
|
|
210
|
+
limitations.push("HAVING clause column references are not converted to CTE references");
|
|
211
|
+
}
|
|
212
|
+
if (metadata.hasOrderBy) {
|
|
213
|
+
limitations.push("ORDER BY clause column references are not converted to CTE references");
|
|
214
|
+
}
|
|
215
|
+
return limitations;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Counts the number of JOINs in the query
|
|
219
|
+
*/
|
|
220
|
+
countJoins(query) {
|
|
221
|
+
var _a, _b;
|
|
222
|
+
return ((_b = (_a = query.fromClause) === null || _a === void 0 ? void 0 : _a.joins) === null || _b === void 0 ? void 0 : _b.length) || 0;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Counts aggregation functions in the query
|
|
226
|
+
*/
|
|
227
|
+
countAggregationFunctions(query) {
|
|
228
|
+
var _a;
|
|
229
|
+
let count = 0;
|
|
230
|
+
if ((_a = query.selectClause) === null || _a === void 0 ? void 0 : _a.items) {
|
|
231
|
+
for (const item of query.selectClause.items) {
|
|
232
|
+
if (this.containsAggregationFunction(item.value)) {
|
|
233
|
+
count++;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return count;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Checks if query contains window functions
|
|
241
|
+
*/
|
|
242
|
+
hasWindowFunctions(query) {
|
|
243
|
+
var _a;
|
|
244
|
+
if ((_a = query.selectClause) === null || _a === void 0 ? void 0 : _a.items) {
|
|
245
|
+
for (const item of query.selectClause.items) {
|
|
246
|
+
if (this.containsWindowFunction(item.value)) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Checks if an expression contains aggregation functions
|
|
255
|
+
*/
|
|
256
|
+
containsAggregationFunction(expression) {
|
|
257
|
+
if (expression instanceof FunctionCall) {
|
|
258
|
+
const funcName = this.getFunctionName(expression).toLowerCase();
|
|
259
|
+
return ['count', 'sum', 'avg', 'min', 'max'].includes(funcName);
|
|
260
|
+
}
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Checks if an expression contains window functions
|
|
265
|
+
*/
|
|
266
|
+
containsWindowFunction(expression) {
|
|
267
|
+
if (expression instanceof FunctionCall && expression.over) {
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
// Fallback: check for common window function names
|
|
271
|
+
if (expression instanceof FunctionCall) {
|
|
272
|
+
const funcName = this.getFunctionName(expression).toLowerCase();
|
|
273
|
+
return ['row_number', 'rank', 'dense_rank', 'lead', 'lag'].includes(funcName);
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Gets function name from FunctionCall
|
|
279
|
+
*/
|
|
280
|
+
getFunctionName(func) {
|
|
281
|
+
const name = func.qualifiedName.name;
|
|
282
|
+
if (name instanceof IdentifierString) {
|
|
283
|
+
return name.name;
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
return name.value;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Extracts detail column names for metadata
|
|
291
|
+
*/
|
|
292
|
+
extractDetailColumnNames(query) {
|
|
293
|
+
var _a, _b;
|
|
294
|
+
const columns = [];
|
|
295
|
+
// Add GROUP BY columns
|
|
296
|
+
if ((_a = query.groupByClause) === null || _a === void 0 ? void 0 : _a.grouping) {
|
|
297
|
+
for (const expr of query.groupByClause.grouping) {
|
|
298
|
+
columns.push(expr.toString());
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// Add columns from aggregation functions
|
|
302
|
+
if ((_b = query.selectClause) === null || _b === void 0 ? void 0 : _b.items) {
|
|
303
|
+
for (const item of query.selectClause.items) {
|
|
304
|
+
if (this.containsAggregationFunction(item.value) && item.value instanceof FunctionCall) {
|
|
305
|
+
if (item.value.argument) {
|
|
306
|
+
columns.push(item.value.argument.toString());
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return [...new Set(columns)]; // Remove duplicates
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Extracts columns needed for the detail CTE
|
|
315
|
+
*/
|
|
316
|
+
extractDetailColumns(query) {
|
|
317
|
+
var _a, _b;
|
|
318
|
+
const columns = [];
|
|
319
|
+
const columnSet = new Set();
|
|
320
|
+
// Add GROUP BY columns
|
|
321
|
+
if ((_a = query.groupByClause) === null || _a === void 0 ? void 0 : _a.grouping) {
|
|
322
|
+
for (const expr of query.groupByClause.grouping) {
|
|
323
|
+
if (expr instanceof ColumnReference) {
|
|
324
|
+
const key = this.getColumnKey(expr);
|
|
325
|
+
if (!columnSet.has(key)) {
|
|
326
|
+
columns.push(expr);
|
|
327
|
+
columnSet.add(key);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// Add columns from aggregation function arguments
|
|
333
|
+
if ((_b = query.selectClause) === null || _b === void 0 ? void 0 : _b.items) {
|
|
334
|
+
for (const item of query.selectClause.items) {
|
|
335
|
+
this.extractColumnsFromExpression(item.value, columns, columnSet);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return columns;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Extracts column references from an expression
|
|
342
|
+
*/
|
|
343
|
+
extractColumnsFromExpression(expression, columns, columnSet) {
|
|
344
|
+
if (expression instanceof FunctionCall) {
|
|
345
|
+
if (expression.argument) {
|
|
346
|
+
if (expression.argument instanceof ColumnReference) {
|
|
347
|
+
const key = this.getColumnKey(expression.argument);
|
|
348
|
+
if (!columnSet.has(key)) {
|
|
349
|
+
columns.push(expression.argument);
|
|
350
|
+
columnSet.add(key);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
else if (expression.argument.toString() === '*') {
|
|
354
|
+
// Handle COUNT(*) by adding special marker
|
|
355
|
+
const starColumn = new ColumnReference(null, '*');
|
|
356
|
+
const key = this.getColumnKey(starColumn);
|
|
357
|
+
if (!columnSet.has(key)) {
|
|
358
|
+
columns.push(starColumn);
|
|
359
|
+
columnSet.add(key);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
else if (expression instanceof ColumnReference) {
|
|
365
|
+
const key = this.getColumnKey(expression);
|
|
366
|
+
if (!columnSet.has(key)) {
|
|
367
|
+
columns.push(expression);
|
|
368
|
+
columnSet.add(key);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Gets a unique key for a column reference
|
|
374
|
+
*/
|
|
375
|
+
getColumnKey(column) {
|
|
376
|
+
var _a;
|
|
377
|
+
const namespace = ((_a = column.namespaces) === null || _a === void 0 ? void 0 : _a.map(ns => ns.name).join('.')) || '';
|
|
378
|
+
const columnName = column.column.name;
|
|
379
|
+
return namespace ? `${namespace}.${columnName}` : columnName;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Builds the detail query (CTE content)
|
|
383
|
+
*/
|
|
384
|
+
buildDetailQuery(originalQuery, detailColumns) {
|
|
385
|
+
const selectItems = detailColumns.map(col => new SelectItem(col));
|
|
386
|
+
return new SimpleSelectQuery({
|
|
387
|
+
selectClause: new SelectClause(selectItems),
|
|
388
|
+
fromClause: originalQuery.fromClause,
|
|
389
|
+
whereClause: originalQuery.whereClause
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Builds the aggregation query that references the CTE
|
|
394
|
+
*/
|
|
395
|
+
buildAggregationQuery(originalQuery) {
|
|
396
|
+
var _a, _b, _c, _d;
|
|
397
|
+
// Transform SELECT items to reference CTE columns
|
|
398
|
+
const transformedSelectItems = ((_b = (_a = originalQuery.selectClause) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.map(item => {
|
|
399
|
+
var _a;
|
|
400
|
+
const transformedExpression = this.transformExpressionForCTE(item.value);
|
|
401
|
+
return new SelectItem(transformedExpression, ((_a = item.identifier) === null || _a === void 0 ? void 0 : _a.name) || null);
|
|
402
|
+
})) || [];
|
|
403
|
+
// Transform GROUP BY to reference CTE columns
|
|
404
|
+
const transformedGroupBy = (_d = (_c = originalQuery.groupByClause) === null || _c === void 0 ? void 0 : _c.grouping) === null || _d === void 0 ? void 0 : _d.map(expr => {
|
|
405
|
+
return this.transformExpressionForCTE(expr);
|
|
406
|
+
});
|
|
407
|
+
// Create FROM clause that references the CTE
|
|
408
|
+
const cteFromClause = new FromClause(new SourceExpression(new TableSource(null, new IdentifierString(this.options.detailCTEName)), null), null);
|
|
409
|
+
return new SimpleSelectQuery({
|
|
410
|
+
selectClause: new SelectClause(transformedSelectItems),
|
|
411
|
+
fromClause: cteFromClause,
|
|
412
|
+
groupByClause: transformedGroupBy ? new GroupByClause(transformedGroupBy) : undefined,
|
|
413
|
+
havingClause: originalQuery.havingClause, // TODO: Transform references if needed
|
|
414
|
+
orderByClause: originalQuery.orderByClause // TODO: Transform references if needed
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Transforms an expression to reference CTE columns instead of original table columns
|
|
419
|
+
*/
|
|
420
|
+
transformExpressionForCTE(expression) {
|
|
421
|
+
if (expression instanceof FunctionCall) {
|
|
422
|
+
// Transform aggregation function arguments
|
|
423
|
+
const transformedArg = expression.argument ?
|
|
424
|
+
(expression.argument instanceof ColumnReference ?
|
|
425
|
+
// Convert table.column to just column for CTE reference
|
|
426
|
+
new ColumnReference(null, expression.argument.column.name) :
|
|
427
|
+
expression.argument) : null;
|
|
428
|
+
return new FunctionCall(expression.qualifiedName.namespaces, expression.qualifiedName.name, transformedArg, expression.over, expression.withinGroup);
|
|
429
|
+
}
|
|
430
|
+
else if (expression instanceof ColumnReference) {
|
|
431
|
+
// Convert table.column to just column for CTE reference
|
|
432
|
+
return new ColumnReference(null, expression.column.name);
|
|
433
|
+
}
|
|
434
|
+
return expression;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Utility function to analyze a JOIN + aggregation query from SQL string (safe, no exceptions)
|
|
439
|
+
*
|
|
440
|
+
* @param sql The SQL string to parse and analyze
|
|
441
|
+
* @param options Decomposer options
|
|
442
|
+
* @returns Analysis result with success status, error information, and metadata
|
|
443
|
+
*/
|
|
444
|
+
export function analyzeJoinAggregation(sql, options) {
|
|
445
|
+
try {
|
|
446
|
+
// Import using ES module syntax to avoid require issues
|
|
447
|
+
const { SelectQueryParser } = eval('require("../parsers/SelectQueryParser")');
|
|
448
|
+
const query = SelectQueryParser.parse(sql);
|
|
449
|
+
const decomposer = new JoinAggregationDecomposer(options);
|
|
450
|
+
return decomposer.analyze(query);
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
return {
|
|
454
|
+
success: false,
|
|
455
|
+
error: `Failed to parse SQL: ${error instanceof Error ? error.message : String(error)}`,
|
|
456
|
+
metadata: {
|
|
457
|
+
joinCount: 0,
|
|
458
|
+
aggregationCount: 0,
|
|
459
|
+
detailColumns: [],
|
|
460
|
+
hasHaving: false,
|
|
461
|
+
hasOrderBy: false,
|
|
462
|
+
hasWindowFunctions: false
|
|
463
|
+
}
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Utility function to decompose a JOIN + aggregation query from SQL string
|
|
469
|
+
*
|
|
470
|
+
* @param sql The SQL string to parse and decompose
|
|
471
|
+
* @param options Decomposer options
|
|
472
|
+
* @returns The decomposed query
|
|
473
|
+
* @throws DecompositionError if parsing or decomposition fails
|
|
474
|
+
*/
|
|
475
|
+
export function decomposeJoinAggregation(sql, options) {
|
|
476
|
+
try {
|
|
477
|
+
// Import using ES module syntax to avoid require issues
|
|
478
|
+
const { SelectQueryParser } = eval('require("../parsers/SelectQueryParser")');
|
|
479
|
+
const query = SelectQueryParser.parse(sql);
|
|
480
|
+
const decomposer = new JoinAggregationDecomposer(options);
|
|
481
|
+
return decomposer.decompose(query);
|
|
482
|
+
}
|
|
483
|
+
catch (error) {
|
|
484
|
+
if (error instanceof DecompositionError) {
|
|
485
|
+
throw error;
|
|
486
|
+
}
|
|
487
|
+
throw new DecompositionError(`Failed to parse SQL: ${error instanceof Error ? error.message : String(error)}`, new SimpleSelectQuery({ selectClause: new SelectClause([]) }), error instanceof Error ? error : undefined);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
//# sourceMappingURL=JoinAggregationDecomposer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JoinAggregationDecomposer.js","sourceRoot":"","sources":["../../../../src/transformers/JoinAggregationDecomposer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAyB,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACtK,OAAO,EAAE,YAAY,EAAE,eAAe,EAAkB,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC3G,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAuC9C;;GAEG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzC,YACI,OAAe,EACC,aAAgC,EAChC,KAAa;QAE7B,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,kBAAa,GAAb,aAAa,CAAmB;QAChC,UAAK,GAAL,KAAK,CAAQ;QAG7B,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACrC,CAAC;CACJ;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,MAAM,UAAU,GAAG,IAAI,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC1D,OAAO,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,IAAI,KAAK,YAAY,kBAAkB,EAAE,CAAC;YACtC,MAAM,KAAK,CAAC;QAChB,CAAC;QACD,MAAM,IAAI,kBAAkB,CACxB,wBAAwB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAChF,IAAI,iBAAiB,CAAC,EAAE,YAAY,EAAE,IAAI,YAAY,CAAC,EAAE,CAAC,EAAE,CAAC,EAC7D,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAC7C,CAAC;IACN,CAAC;AACL,CAAC"}
|
|
@@ -22,6 +22,28 @@ export declare class QueryBuilder {
|
|
|
22
22
|
*/
|
|
23
23
|
static buildSimpleQuery(query: SelectQuery): SimpleSelectQuery;
|
|
24
24
|
private static buildSimpleBinaryQuery;
|
|
25
|
+
/**
|
|
26
|
+
* Extracts ORDER BY clause from the rightmost query in a binary query tree and removes it.
|
|
27
|
+
* This clarifies the semantics by moving the ORDER BY from the ambiguous position
|
|
28
|
+
* in the UNION to the explicit outer SimpleSelectQuery level.
|
|
29
|
+
*
|
|
30
|
+
* NOTE: ORDER BY in UNION context applies to the entire result set, not individual subqueries.
|
|
31
|
+
* Therefore, table prefixes (e.g., "a.column") in ORDER BY are invalid SQL and would cause
|
|
32
|
+
* syntax errors. Valid ORDER BY clauses should only reference column names without prefixes
|
|
33
|
+
* or use positional notation (ORDER BY 1, 2). Since we only process valid SQL, the current
|
|
34
|
+
* implementation correctly handles legitimate cases without additional prefix processing.
|
|
35
|
+
*
|
|
36
|
+
* @param query BinarySelectQuery to process
|
|
37
|
+
* @returns Extracted OrderByClause or null if none found
|
|
38
|
+
*/
|
|
39
|
+
private static extractAndRemoveOrderByFromBinaryQuery;
|
|
40
|
+
/**
|
|
41
|
+
* Recursively finds and removes ORDER BY from the rightmost query in a binary tree.
|
|
42
|
+
*
|
|
43
|
+
* @param query Current query being processed
|
|
44
|
+
* @returns Extracted OrderByClause or null
|
|
45
|
+
*/
|
|
46
|
+
private static findAndRemoveRightmostOrderBy;
|
|
25
47
|
/**
|
|
26
48
|
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns or user-specified columns
|
|
27
49
|
*
|
|
@@ -59,7 +59,9 @@ export class QueryBuilder {
|
|
|
59
59
|
throw new Error("Unsupported query type for buildSimpleQuery");
|
|
60
60
|
}
|
|
61
61
|
static buildSimpleBinaryQuery(query) {
|
|
62
|
-
//
|
|
62
|
+
// Extract ORDER BY from the rightmost query in the binary tree and remove it
|
|
63
|
+
const extractedOrderBy = QueryBuilder.extractAndRemoveOrderByFromBinaryQuery(query);
|
|
64
|
+
// Create a subquery source from the binary query (now without ORDER BY)
|
|
63
65
|
const subQuerySource = new SubQuerySource(query);
|
|
64
66
|
// Create a source expression with alias
|
|
65
67
|
const sourceExpr = new SourceExpression(subQuerySource, new SourceAliasExpression("bq", null));
|
|
@@ -67,13 +69,57 @@ export class QueryBuilder {
|
|
|
67
69
|
const fromClause = new FromClause(sourceExpr, null);
|
|
68
70
|
// Create SELECT clause with * (all columns)
|
|
69
71
|
const selectClause = QueryBuilder.createSelectAllClause();
|
|
70
|
-
// Create the final simple select query
|
|
72
|
+
// Create the final simple select query with extracted ORDER BY
|
|
71
73
|
const q = new SimpleSelectQuery({
|
|
72
74
|
selectClause,
|
|
73
|
-
fromClause
|
|
75
|
+
fromClause,
|
|
76
|
+
orderByClause: extractedOrderBy
|
|
74
77
|
});
|
|
75
78
|
return CTENormalizer.normalize(q);
|
|
76
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Extracts ORDER BY clause from the rightmost query in a binary query tree and removes it.
|
|
82
|
+
* This clarifies the semantics by moving the ORDER BY from the ambiguous position
|
|
83
|
+
* in the UNION to the explicit outer SimpleSelectQuery level.
|
|
84
|
+
*
|
|
85
|
+
* NOTE: ORDER BY in UNION context applies to the entire result set, not individual subqueries.
|
|
86
|
+
* Therefore, table prefixes (e.g., "a.column") in ORDER BY are invalid SQL and would cause
|
|
87
|
+
* syntax errors. Valid ORDER BY clauses should only reference column names without prefixes
|
|
88
|
+
* or use positional notation (ORDER BY 1, 2). Since we only process valid SQL, the current
|
|
89
|
+
* implementation correctly handles legitimate cases without additional prefix processing.
|
|
90
|
+
*
|
|
91
|
+
* @param query BinarySelectQuery to process
|
|
92
|
+
* @returns Extracted OrderByClause or null if none found
|
|
93
|
+
*/
|
|
94
|
+
static extractAndRemoveOrderByFromBinaryQuery(query) {
|
|
95
|
+
return QueryBuilder.findAndRemoveRightmostOrderBy(query);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Recursively finds and removes ORDER BY from the rightmost query in a binary tree.
|
|
99
|
+
*
|
|
100
|
+
* @param query Current query being processed
|
|
101
|
+
* @returns Extracted OrderByClause or null
|
|
102
|
+
*/
|
|
103
|
+
static findAndRemoveRightmostOrderBy(query) {
|
|
104
|
+
if (query instanceof BinarySelectQuery) {
|
|
105
|
+
// For binary queries, check right side first (rightmost takes precedence)
|
|
106
|
+
const rightOrderBy = QueryBuilder.findAndRemoveRightmostOrderBy(query.right);
|
|
107
|
+
if (rightOrderBy) {
|
|
108
|
+
return rightOrderBy;
|
|
109
|
+
}
|
|
110
|
+
// If no ORDER BY on right side, check left side
|
|
111
|
+
return QueryBuilder.findAndRemoveRightmostOrderBy(query.left);
|
|
112
|
+
}
|
|
113
|
+
else if (query instanceof SimpleSelectQuery) {
|
|
114
|
+
// Extract ORDER BY from SimpleSelectQuery and remove it
|
|
115
|
+
const orderBy = query.orderByClause;
|
|
116
|
+
if (orderBy) {
|
|
117
|
+
query.orderByClause = null;
|
|
118
|
+
return orderBy;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
77
123
|
/**
|
|
78
124
|
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns or user-specified columns
|
|
79
125
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryBuilder.js","sourceRoot":"","sources":["../../../../src/transformers/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,cAAc,EAAE,UAAU,EAAe,YAAY,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"QueryBuilder.js","sourceRoot":"","sources":["../../../../src/transformers/QueryBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,UAAU,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,cAAc,EAAE,UAAU,EAAe,YAAY,EAAE,YAAY,EAAiB,MAAM,kBAAkB,CAAC;AAC5O,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,iBAAiB,EAAe,iBAAiB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACvG,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,sBAAsB,EAAE,MAAM,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AAC,EACzC,GAAG,EACH,IAAI,eAAe,CAAC,gBAAgB,EAAE,EAAE,CAAC,CAC5C,CAAC;YACF,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACpE,CAAC;QACD,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,KAAM,CAAC,CAAC;QAE5C,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC;YAChC,YAAY,EAAE,YAAY;YAC1B,SAAS,EAAE,SAAS;YACpB,UAAU,EAAE,IAAI;YAChB,WAAW,EAAE,WAAW;YACxB,UAAU,EAAE,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,SAAS;SAC1F,CAAC,CAAC;QACH,OAAO,WAAW,CAAC;IACvB,CAAC;CACJ"}
|
|
@@ -345,7 +345,7 @@ export class TableSourceCollector {
|
|
|
345
345
|
visitFetchClause(clause) {
|
|
346
346
|
clause.expression.accept(this);
|
|
347
347
|
}
|
|
348
|
-
visitForClause(
|
|
348
|
+
visitForClause(_clause) {
|
|
349
349
|
// FOR clause doesn't contain table sources
|
|
350
350
|
}
|
|
351
351
|
visitOrderByItem(item) {
|
|
@@ -422,7 +422,7 @@ export class TableSourceCollector {
|
|
|
422
422
|
}
|
|
423
423
|
}
|
|
424
424
|
// Handle StringSpecifierExpression (PostgreSQL E-strings)
|
|
425
|
-
visitStringSpecifierExpression(
|
|
425
|
+
visitStringSpecifierExpression(_expr) {
|
|
426
426
|
// StringSpecifierExpression is just a literal string with an escape specifier
|
|
427
427
|
// It doesn't contain table references, so we don't need to visit any children
|
|
428
428
|
// This is a no-op method to prevent "No handler" errors
|