data-finder-ts 0.1.0 → 0.1.3

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 (63) hide show
  1. package/dist/chunk-3SSWFES4.js +448 -0
  2. package/dist/chunk-3SSWFES4.js.map +1 -0
  3. package/dist/chunk-5K2Z3YDY.js +393 -0
  4. package/dist/chunk-5K2Z3YDY.js.map +1 -0
  5. package/dist/chunk-6QTCGJWX.js +98 -0
  6. package/dist/chunk-6QTCGJWX.js.map +1 -0
  7. package/dist/chunk-DFYZVBII.js +652 -0
  8. package/dist/chunk-DFYZVBII.js.map +1 -0
  9. package/dist/chunk-EYXBYV2X.js +90 -0
  10. package/dist/chunk-EYXBYV2X.js.map +1 -0
  11. package/dist/chunk-FLEIUDZZ.js +14 -0
  12. package/dist/chunk-FLEIUDZZ.js.map +1 -0
  13. package/dist/chunk-V647VNMY.js +135 -0
  14. package/dist/chunk-V647VNMY.js.map +1 -0
  15. package/dist/datafinder/attribute.cjs +269 -0
  16. package/dist/datafinder/attribute.cjs.map +1 -0
  17. package/dist/datafinder/attribute.d.cts +45 -0
  18. package/dist/datafinder/attribute.d.ts +45 -0
  19. package/dist/datafinder/attribute.js +8 -0
  20. package/dist/datafinder/attribute.js.map +1 -0
  21. package/dist/datafinder/finder-registry.cjs +39 -0
  22. package/dist/datafinder/finder-registry.cjs.map +1 -0
  23. package/dist/datafinder/finder-registry.d.cts +4 -0
  24. package/dist/datafinder/finder-registry.d.ts +4 -0
  25. package/dist/datafinder/finder-registry.js +9 -0
  26. package/dist/datafinder/finder-registry.js.map +1 -0
  27. package/dist/datafinder/runner.cjs +1346 -0
  28. package/dist/datafinder/runner.cjs.map +1 -0
  29. package/dist/datafinder/runner.d.cts +33 -0
  30. package/dist/datafinder/runner.d.ts +33 -0
  31. package/dist/datafinder/runner.js +20 -0
  32. package/dist/datafinder/runner.js.map +1 -0
  33. package/dist/datafinder/sql-generator.cjs +1261 -0
  34. package/dist/datafinder/sql-generator.cjs.map +1 -0
  35. package/dist/datafinder/sql-generator.d.cts +17 -0
  36. package/dist/datafinder/sql-generator.d.ts +17 -0
  37. package/dist/datafinder/sql-generator.js +17 -0
  38. package/dist/datafinder/sql-generator.js.map +1 -0
  39. package/dist/datafinder/typed-attributes.cjs +751 -0
  40. package/dist/datafinder/typed-attributes.cjs.map +1 -0
  41. package/dist/datafinder/typed-attributes.d.cts +135 -0
  42. package/dist/datafinder/typed-attributes.d.ts +135 -0
  43. package/dist/datafinder/typed-attributes.js +25 -0
  44. package/dist/datafinder/typed-attributes.js.map +1 -0
  45. package/dist/index.cjs +1822 -0
  46. package/dist/index.cjs.map +1 -0
  47. package/dist/index.d.cts +27 -0
  48. package/dist/index.d.ts +27 -0
  49. package/dist/index.js +159 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/model/milestoning.cjs +145 -0
  52. package/dist/model/milestoning.cjs.map +1 -0
  53. package/dist/model/milestoning.d.cts +40 -0
  54. package/dist/model/milestoning.d.ts +40 -0
  55. package/dist/model/milestoning.js +18 -0
  56. package/dist/model/milestoning.js.map +1 -0
  57. package/dist/model/relational.cjs +514 -0
  58. package/dist/model/relational.cjs.map +1 -0
  59. package/dist/model/relational.d.cts +246 -0
  60. package/dist/model/relational.d.ts +246 -0
  61. package/dist/model/relational.js +91 -0
  62. package/dist/model/relational.js.map +1 -0
  63. package/package.json +3 -3
@@ -0,0 +1,652 @@
1
+ import {
2
+ BiTemporalColumns,
3
+ BusinessDateAndProcessingTemporalColumns,
4
+ MilestonedTable,
5
+ ProcessingTemporalColumns,
6
+ SingleBusinessDateColumn
7
+ } from "./chunk-EYXBYV2X.js";
8
+ import {
9
+ DateAttribute,
10
+ DateTimeAttribute
11
+ } from "./chunk-5K2Z3YDY.js";
12
+ import {
13
+ Attribute
14
+ } from "./chunk-V647VNMY.js";
15
+ import {
16
+ AggregateOperation,
17
+ BooleanConstantOperation,
18
+ Column,
19
+ ColumnWithJoin,
20
+ ComparisonOperation,
21
+ ConstantOperation,
22
+ CountAllOperation,
23
+ DateArithmeticOperation,
24
+ DateConstantOperation,
25
+ DateDiffOperation,
26
+ DateExtractOperation,
27
+ DateTimeConstantOperation,
28
+ DecimalConstantOperation,
29
+ FloatConstantOperation,
30
+ IntegerConstantOperation,
31
+ IsNotNullOperation,
32
+ IsNullOperation,
33
+ LogicalOperation,
34
+ NoOperation,
35
+ RelationalOperationElement,
36
+ ScalarFunctionOperation,
37
+ SortOperation,
38
+ StringConstantOperation,
39
+ UnaryOperation,
40
+ WindowFunctionOperation,
41
+ WindowSpecification
42
+ } from "./chunk-3SSWFES4.js";
43
+
44
+ // src/datafinder/finder.ts
45
+ var ExistsOperation = class extends RelationalOperationElement {
46
+ constructor(node) {
47
+ super();
48
+ this.node = node;
49
+ }
50
+ node;
51
+ andOp(other) {
52
+ return new LogicalOperation(this, "AND" /* AND */, other);
53
+ }
54
+ };
55
+ var NotExistsOperation = class extends RelationalOperationElement {
56
+ constructor(node) {
57
+ super();
58
+ this.node = node;
59
+ }
60
+ node;
61
+ andOp(other) {
62
+ return new LogicalOperation(this, "AND" /* AND */, other);
63
+ }
64
+ };
65
+ var RelatedFinder = class {
66
+ constructor(_node) {
67
+ this._node = _node;
68
+ }
69
+ _node;
70
+ exists() {
71
+ return new ExistsOperation(this._node);
72
+ }
73
+ notExists() {
74
+ return new NotExistsOperation(this._node);
75
+ }
76
+ };
77
+
78
+ // src/datafinder/sql-generator.ts
79
+ var Alias = class {
80
+ constructor(element, name) {
81
+ this.element = element;
82
+ this.name = name;
83
+ }
84
+ element;
85
+ name;
86
+ };
87
+ var TableAlias = class {
88
+ constructor(table, alias) {
89
+ this.table = table;
90
+ this.alias = alias;
91
+ }
92
+ table;
93
+ alias;
94
+ };
95
+ var TableAliasColumn = class extends RelationalOperationElement {
96
+ constructor(column, tableAlias) {
97
+ super();
98
+ this.column = column;
99
+ this.tableAlias = tableAlias;
100
+ }
101
+ column;
102
+ tableAlias;
103
+ };
104
+ var Join = class {
105
+ constructor(source, target, filterOp) {
106
+ this.source = source;
107
+ this.target = target;
108
+ this.filterOp = filterOp;
109
+ }
110
+ source;
111
+ target;
112
+ filterOp;
113
+ };
114
+ var SelectOperation = class {
115
+ constructor(display, filter, orderBy = [], groupBy = [], limit, table) {
116
+ this.display = display;
117
+ this.filter = filter;
118
+ this.orderBy = orderBy;
119
+ this.groupBy = groupBy;
120
+ this.limit = limit;
121
+ this.table = table;
122
+ }
123
+ display;
124
+ filter;
125
+ orderBy;
126
+ groupBy;
127
+ limit;
128
+ table;
129
+ };
130
+ function sqlFormatDatetime(value) {
131
+ const pad = (n, w = 2) => String(n).padStart(w, "0");
132
+ return `'${value.getUTCFullYear()}-${pad(value.getUTCMonth() + 1)}-${pad(value.getUTCDate())} ${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:${pad(value.getUTCSeconds())}'`;
133
+ }
134
+ function sqlFormatDate(value) {
135
+ const pad = (n) => String(n).padStart(2, "0");
136
+ return `'${value.getUTCFullYear()}-${pad(value.getUTCMonth() + 1)}-${pad(value.getUTCDate())}'`;
137
+ }
138
+ var LOGICAL_OPERATOR_STR = {
139
+ ["AND" /* AND */]: " AND ",
140
+ ["OR" /* OR */]: " OR "
141
+ };
142
+ var COMPARISON_OPERATOR_STR = {
143
+ ["EQUAL" /* EQUAL */]: " = ",
144
+ ["NOT_EQUAL" /* NOT_EQUAL */]: " <> ",
145
+ ["LESS_THAN" /* LESS_THAN */]: " < ",
146
+ ["GREATER_THAN" /* GREATER_THAN */]: " > ",
147
+ ["LESS_THAN_OR_EQUAL_TO" /* LESS_THAN_OR_EQUAL_TO */]: " <= ",
148
+ ["GREATER_THAN_OR_EQUAL_TO" /* GREATER_THAN_OR_EQUAL_TO */]: " >= ",
149
+ ["LIKE" /* LIKE */]: " LIKE ",
150
+ ["NOT_LIKE" /* NOT_LIKE */]: " NOT LIKE "
151
+ };
152
+ var AGGREGATE_SQL_NAMES = {
153
+ ["AVERAGE" /* AVERAGE */]: "AVG"
154
+ };
155
+ var WINDOW_SQL_NAMES = {
156
+ ["ROW_NUMBER" /* ROW_NUMBER */]: "ROW_NUMBER",
157
+ ["RANK" /* RANK */]: "RANK",
158
+ ["DENSE_RANK" /* DENSE_RANK */]: "DENSE_RANK",
159
+ ["NTILE" /* NTILE */]: "NTILE",
160
+ ["LAG" /* LAG */]: "LAG",
161
+ ["LEAD" /* LEAD */]: "LEAD",
162
+ ["FIRST_VALUE" /* FIRST_VALUE */]: "FIRST_VALUE",
163
+ ["LAST_VALUE" /* LAST_VALUE */]: "LAST_VALUE",
164
+ ["CUME_DIST" /* CUME_DIST */]: "CUME_DIST",
165
+ ["PERCENT_RANK" /* PERCENT_RANK */]: "PERCENT_RANK"
166
+ };
167
+ var SCALAR_SQL_NAMES = {
168
+ ["ABS" /* ABS */]: "ABS",
169
+ ["CEILING" /* CEILING */]: "CEILING",
170
+ ["FLOOR" /* FLOOR */]: "FLOOR",
171
+ ["MOD" /* MOD */]: "MOD",
172
+ ["POWER" /* POWER */]: "POWER",
173
+ ["SQRT" /* SQRT */]: "SQRT",
174
+ ["ROUND" /* ROUND */]: "ROUND",
175
+ ["UPPER" /* UPPER */]: "UPPER",
176
+ ["LOWER" /* LOWER */]: "LOWER",
177
+ ["TRIM" /* TRIM */]: "TRIM",
178
+ ["LTRIM" /* LTRIM */]: "LTRIM",
179
+ ["RTRIM" /* RTRIM */]: "RTRIM",
180
+ ["LENGTH" /* LENGTH */]: "LENGTH",
181
+ ["REVERSE" /* REVERSE */]: "REVERSE",
182
+ ["LEFT" /* LEFT */]: "LEFT",
183
+ ["RIGHT" /* RIGHT */]: "RIGHT",
184
+ ["REPEAT" /* REPEAT */]: "REPEAT",
185
+ ["REPLACE" /* REPLACE */]: "REPLACE",
186
+ ["SUBSTRING" /* SUBSTRING */]: "SUBSTRING"
187
+ };
188
+ function constantValueString(op) {
189
+ if (op instanceof StringConstantOperation) return "'" + op.value + "'";
190
+ if (op instanceof DateConstantOperation) return sqlFormatDate(op.value);
191
+ if (op instanceof DateTimeConstantOperation) return sqlFormatDatetime(op.value);
192
+ if (op instanceof IntegerConstantOperation) return String(op.value);
193
+ if (op instanceof FloatConstantOperation) return String(op.value);
194
+ if (op instanceof BooleanConstantOperation) return op.value ? "TRUE" : "FALSE";
195
+ if (op instanceof DecimalConstantOperation) return String(op.value);
196
+ throw new Error("Unknown constant operation");
197
+ }
198
+ function windowSpecToString(window) {
199
+ const parts = [];
200
+ if (window.partitionBy.length > 0) {
201
+ parts.push("PARTITION BY " + window.partitionBy.map((p) => sqlOperationToString(p)).join(", "));
202
+ }
203
+ if (window.orderBy.length > 0) {
204
+ parts.push("ORDER BY " + window.orderBy.map(
205
+ (s) => sqlOperationToString(s.column) + (s.direction === "ASC" /* ASC */ ? " ASC" : " DESC")
206
+ ).join(", "));
207
+ }
208
+ return "OVER (" + parts.join(" ") + ")";
209
+ }
210
+ function sqlOperationToString(operation) {
211
+ if (operation instanceof TableAliasColumn) {
212
+ return operation.tableAlias.alias + "." + operation.column.name;
213
+ }
214
+ if (operation instanceof WindowFunctionOperation) {
215
+ const fn = WINDOW_SQL_NAMES[operation.func];
216
+ const parts = [];
217
+ if (operation.element !== null && operation.element !== void 0) {
218
+ parts.push(sqlOperationToString(operation.element));
219
+ }
220
+ if (operation.secondArg !== void 0) parts.push(String(operation.secondArg));
221
+ for (const arg of operation.extraArgs) {
222
+ parts.push(typeof arg === "string" ? "'" + arg + "'" : String(arg));
223
+ }
224
+ const sql = fn + "(" + parts.join(", ") + ")";
225
+ return sql + (operation.window !== void 0 ? " " + windowSpecToString(operation.window) : " OVER ()");
226
+ }
227
+ if (operation instanceof AggregateOperation) {
228
+ const fn = AGGREGATE_SQL_NAMES[operation.operator] ?? operation.operator;
229
+ let sql = fn + "(" + sqlOperationToString(operation.element) + ")";
230
+ if (operation.window !== void 0) {
231
+ sql += " " + windowSpecToString(operation.window);
232
+ }
233
+ return sql;
234
+ }
235
+ if (operation instanceof ScalarFunctionOperation) {
236
+ const fn = SCALAR_SQL_NAMES[operation.func];
237
+ const parts = [sqlOperationToString(operation.element)];
238
+ if (operation.secondArg !== void 0) parts.push(String(operation.secondArg));
239
+ for (const arg of operation.extraArgs) {
240
+ parts.push(typeof arg === "string" ? "'" + arg + "'" : String(arg));
241
+ }
242
+ return fn + "(" + parts.join(", ") + ")";
243
+ }
244
+ if (operation instanceof DateExtractOperation) {
245
+ return "EXTRACT(" + operation.part + " FROM " + sqlOperationToString(operation.element) + ")";
246
+ }
247
+ if (operation instanceof DateArithmeticOperation) {
248
+ const op = operation.isAdd ? "+" : "-";
249
+ return sqlOperationToString(operation.element) + " " + op + " INTERVAL " + operation.n + " " + operation.unit;
250
+ }
251
+ if (operation instanceof DateDiffOperation) {
252
+ const otherSql = sqlFormatDatetime(operation.other);
253
+ return "DATE_DIFF('" + operation.unit.toLowerCase() + "', " + sqlOperationToString(operation.element) + ", " + otherSql + ")";
254
+ }
255
+ if (operation instanceof CountAllOperation) return "COUNT(*)";
256
+ if (operation instanceof Alias) {
257
+ return sqlOperationToString(operation.element) + ' AS "' + operation.name + '"';
258
+ }
259
+ throw new TypeError("Unknown operation in sqlOperationToString: " + operation.constructor.name);
260
+ }
261
+ function findColumn(operation) {
262
+ if (operation instanceof UnaryOperation) return findColumn(operation.element);
263
+ if (operation instanceof ColumnWithJoin) return operation;
264
+ throw new TypeError("Cannot find ColumnWithJoin in: " + operation.constructor.name);
265
+ }
266
+ function openEndClause(endAttr, value, infiniteDatetime) {
267
+ const gtOp = endAttr instanceof DateTimeAttribute ? endAttr.gt(value) : endAttr.gt(value);
268
+ if (infiniteDatetime === void 0) {
269
+ const colRef = new ColumnWithJoin(endAttr.column(), endAttr.parent());
270
+ return new LogicalOperation(gtOp, "OR" /* OR */, new IsNullOperation(colRef));
271
+ }
272
+ return gtOp;
273
+ }
274
+ function buildMilestoningFilterOperation(businessDate, processingDatetime, table, joinNode) {
275
+ const mc = table.milestoningColumns;
276
+ const ops = [];
277
+ if (mc instanceof BiTemporalColumns) {
278
+ if (businessDate !== null) {
279
+ const dateFrom = new DateAttribute("business_date_from", mc.businessDateFromColumn.name, mc.businessDateFromColumn.type, mc.businessDateFromColumn.table.qualifiedName, joinNode);
280
+ const dateTo = new DateAttribute("business_date_to", mc.businessDateToColumn.name, mc.businessDateToColumn.type, mc.businessDateToColumn.table.qualifiedName, joinNode);
281
+ ops.push(new LogicalOperation(dateFrom.lte(businessDate), "AND" /* AND */, openEndClause(dateTo, businessDate, mc.infiniteDatetime)));
282
+ }
283
+ if (processingDatetime !== null) {
284
+ const startAt = new DateTimeAttribute("start_at", mc.startAtColumn.name, mc.startAtColumn.type, mc.startAtColumn.table.qualifiedName, joinNode);
285
+ const endAt = new DateTimeAttribute("end_at", mc.endAtColumn.name, mc.endAtColumn.type, mc.endAtColumn.table.qualifiedName, joinNode);
286
+ ops.push(new LogicalOperation(startAt.lte(processingDatetime), "AND" /* AND */, openEndClause(endAt, processingDatetime, mc.infiniteDatetime)));
287
+ }
288
+ } else if (mc instanceof BusinessDateAndProcessingTemporalColumns) {
289
+ if (businessDate !== null) {
290
+ const businessAtt = new DateAttribute("business_date", mc.businessDateColumn.name, mc.businessDateColumn.type, mc.businessDateColumn.table.qualifiedName, joinNode);
291
+ ops.push(businessAtt.eq(businessDate));
292
+ }
293
+ if (processingDatetime !== null) {
294
+ const startAt = new DateTimeAttribute("start_at", mc.startAtColumn.name, mc.startAtColumn.type, mc.startAtColumn.table.qualifiedName, joinNode);
295
+ const endAt = new DateTimeAttribute("end_at", mc.endAtColumn.name, mc.endAtColumn.type, mc.endAtColumn.table.qualifiedName, joinNode);
296
+ ops.push(new LogicalOperation(startAt.lte(processingDatetime), "AND" /* AND */, openEndClause(endAt, processingDatetime, mc.infiniteDatetime)));
297
+ }
298
+ } else if (mc instanceof ProcessingTemporalColumns && processingDatetime !== null) {
299
+ const startAt = new DateTimeAttribute("start_at", mc.startAtColumn.name, mc.startAtColumn.type, mc.startAtColumn.table.qualifiedName, joinNode);
300
+ const endAt = new DateTimeAttribute("end_at", mc.endAtColumn.name, mc.endAtColumn.type, mc.endAtColumn.table.qualifiedName, joinNode);
301
+ ops.push(new LogicalOperation(startAt.lte(processingDatetime), "AND" /* AND */, openEndClause(endAt, processingDatetime, mc.infiniteDatetime)));
302
+ } else if (mc instanceof SingleBusinessDateColumn && businessDate !== null) {
303
+ const businessAtt = new DateAttribute("business_date", mc.businessDateColumn.name, mc.businessDateColumn.type, mc.businessDateColumn.table.qualifiedName, joinNode);
304
+ ops.push(businessAtt.eq(businessDate));
305
+ }
306
+ if (ops.length === 0) return null;
307
+ if (ops.length === 1) return ops[0];
308
+ return new LogicalOperation(ops[0], "AND" /* AND */, ops[1]);
309
+ }
310
+ function buildQueryOperation(businessDate, processingDatetime, columns, table, op, orderBy = [], groupBy = [], limit) {
311
+ let filter = op;
312
+ if (table instanceof MilestonedTable) {
313
+ const milestonedOp = buildMilestoningFilterOperation(businessDate, processingDatetime, table);
314
+ if (milestonedOp !== null) {
315
+ filter = filter instanceof NoOperation ? milestonedOp : new LogicalOperation(filter, "AND" /* AND */, milestonedOp);
316
+ }
317
+ }
318
+ const requiredJoins = [];
319
+ const requiredJoinIds = /* @__PURE__ */ new Set();
320
+ for (const col of columns) {
321
+ if (col instanceof CountAllOperation) continue;
322
+ let node;
323
+ if (col instanceof Attribute) {
324
+ node = col.parent();
325
+ } else {
326
+ node = findColumn(col).parent;
327
+ }
328
+ if (node !== void 0 && !requiredJoinIds.has(node)) {
329
+ requiredJoinIds.add(node);
330
+ requiredJoins.push(node);
331
+ }
332
+ }
333
+ for (const node of requiredJoins) {
334
+ if (node.join.target instanceof MilestonedTable) {
335
+ const milestonedOp = buildMilestoningFilterOperation(businessDate, processingDatetime, node.join.target, node);
336
+ if (milestonedOp !== null) {
337
+ node.join.filter = milestonedOp;
338
+ }
339
+ }
340
+ }
341
+ return new SelectOperation(columns, filter, orderBy, groupBy, limit, table);
342
+ }
343
+ var SQLQueryGenerator = class {
344
+ _select = [];
345
+ _from = /* @__PURE__ */ new Set();
346
+ _join = [];
347
+ _groupByParts = [];
348
+ _orderByParts = [];
349
+ _where = "";
350
+ _limit;
351
+ _tableAliasIncr = 0;
352
+ _tableAliasesByTable = /* @__PURE__ */ new Map();
353
+ _addedJoinIds = /* @__PURE__ */ new Set();
354
+ _rootTable;
355
+ generate(select) {
356
+ this._rootTable = select.table;
357
+ if (this._rootTable !== void 0) {
358
+ this._from.add(this._tableAliasForTable(this._rootTable.qualifiedName, this._rootTable.qualifiedName));
359
+ }
360
+ this.processSelect(select.display);
361
+ this._where = this.buildFilter(select.filter);
362
+ const groupByJoins = /* @__PURE__ */ new Map();
363
+ for (const expr of select.groupBy) {
364
+ this._collectRequiredJoins(expr, groupByJoins);
365
+ }
366
+ for (const node of groupByJoins.values()) {
367
+ this._addJoin(node);
368
+ }
369
+ this._groupByParts = select.groupBy.map((a) => this._attrToColString(a));
370
+ this._orderByParts = select.orderBy.map(
371
+ (s) => this.buildFilter(s.column) + (s.direction === "ASC" /* ASC */ ? " ASC" : " DESC")
372
+ );
373
+ this._limit = select.limit;
374
+ }
375
+ _attrToColString(attr) {
376
+ const node = attr.parent();
377
+ const ta = this._tableAliasForTable(attr.owner(), node ?? attr.owner());
378
+ return ta.alias + "." + attr.column().name;
379
+ }
380
+ _collectRequiredJoins(op, out) {
381
+ if (op === null || op === void 0) return;
382
+ if (op instanceof Attribute) {
383
+ const p = op.parent();
384
+ if (p !== void 0) out.set(p, p);
385
+ } else if (op instanceof ColumnWithJoin) {
386
+ if (op.parent !== void 0) out.set(op.parent, op.parent);
387
+ } else if (op instanceof AggregateOperation) {
388
+ this._collectRequiredJoins(op.element, out);
389
+ if (op.window) this._collectRequiredJoins(op.window, out);
390
+ } else if (op instanceof WindowFunctionOperation) {
391
+ if (op.element) this._collectRequiredJoins(op.element, out);
392
+ if (op.window) this._collectRequiredJoinsFromWindowSpec(op.window, out);
393
+ } else if (op instanceof ScalarFunctionOperation || op instanceof DateExtractOperation || op instanceof DateArithmeticOperation || op instanceof DateDiffOperation) {
394
+ this._collectRequiredJoins(op.element, out);
395
+ } else if (op instanceof SortOperation) {
396
+ this._collectRequiredJoins(op.column, out);
397
+ } else if (op instanceof LogicalOperation || op instanceof ComparisonOperation) {
398
+ this._collectRequiredJoins(op.left, out);
399
+ this._collectRequiredJoins(op.right, out);
400
+ }
401
+ }
402
+ _collectRequiredJoinsFromWindowSpec(window, out) {
403
+ for (const p of window.partitionBy) {
404
+ this._collectRequiredJoins(p, out);
405
+ }
406
+ for (const s of window.orderBy) {
407
+ this._collectRequiredJoins(s.column, out);
408
+ }
409
+ }
410
+ _rewriteWindowSpec(window) {
411
+ const partitionBy = window.partitionBy.map((p) => this._rewriteOperation(p));
412
+ const orderBy = window.orderBy.map((s) => new SortOperation(this._rewriteOperation(s.column), s.direction));
413
+ return new WindowSpecification(partitionBy, orderBy);
414
+ }
415
+ _rewriteOperation(op) {
416
+ if (op instanceof Attribute) {
417
+ const ta = this._tableAliasForTable(op.owner(), op.parent() ?? op.owner());
418
+ return new TableAliasColumn(op.column(), ta);
419
+ }
420
+ if (op instanceof ColumnWithJoin) {
421
+ const ta = op.parent !== void 0 ? this._tableAliasForTable(op.column.owner, op.parent) : this._tableAliasForTable(op.column.owner, op.column.owner);
422
+ return new TableAliasColumn(op.column, ta);
423
+ }
424
+ if (op instanceof Column) {
425
+ const ta = this._tableAliasForTable(op.owner, op.owner);
426
+ return new TableAliasColumn(op, ta);
427
+ }
428
+ if (op instanceof AggregateOperation) {
429
+ return new AggregateOperation(
430
+ this._rewriteOperation(op.element),
431
+ op.operator,
432
+ op.displayName,
433
+ op.window ? this._rewriteWindowSpec(op.window) : void 0
434
+ );
435
+ }
436
+ if (op instanceof WindowFunctionOperation) {
437
+ const el = op.element ? this._rewriteOperation(op.element) : null;
438
+ return new WindowFunctionOperation(
439
+ el,
440
+ op.func,
441
+ op.displayName,
442
+ op.secondArg,
443
+ [...op.extraArgs],
444
+ op.window ? this._rewriteWindowSpec(op.window) : void 0
445
+ );
446
+ }
447
+ if (op instanceof ScalarFunctionOperation) {
448
+ return new ScalarFunctionOperation(this._rewriteOperation(op.element), op.func, op.displayName, op.secondArg, op.extraArgs);
449
+ }
450
+ if (op instanceof DateExtractOperation) {
451
+ return new DateExtractOperation(this._rewriteOperation(op.element), op.part, op.displayName);
452
+ }
453
+ if (op instanceof DateArithmeticOperation) {
454
+ return new DateArithmeticOperation(this._rewriteOperation(op.element), op.n, op.unit, op.isAdd, op.displayName);
455
+ }
456
+ if (op instanceof DateDiffOperation) {
457
+ return new DateDiffOperation(this._rewriteOperation(op.element), op.other, op.unit, op.displayName);
458
+ }
459
+ return op;
460
+ }
461
+ processSelect(cols) {
462
+ const requiredJoins = /* @__PURE__ */ new Map();
463
+ for (const col of cols) {
464
+ this._collectRequiredJoins(col, requiredJoins);
465
+ }
466
+ for (const node of requiredJoins.values()) {
467
+ this._addJoin(node);
468
+ }
469
+ for (const col of cols) {
470
+ if (col instanceof Attribute) {
471
+ const node = col.parent();
472
+ let ta;
473
+ if (node !== void 0) {
474
+ ta = this._tableAliasForTable(col.owner(), node);
475
+ } else {
476
+ ta = this._tableAliasForTable(col.owner(), col.owner());
477
+ this._from.add(ta);
478
+ }
479
+ this._select.push(new Alias(new TableAliasColumn(col.column(), ta), col.displayName()));
480
+ } else if (col instanceof WindowFunctionOperation) {
481
+ const rewritten = this._rewriteOperation(col);
482
+ let alias;
483
+ if (col.displayName) {
484
+ alias = col.displayName;
485
+ } else if (["ROW_NUMBER" /* ROW_NUMBER */, "RANK" /* RANK */, "DENSE_RANK" /* DENSE_RANK */].includes(col.func)) {
486
+ alias = col.func.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
487
+ } else if (col.element) {
488
+ alias = col.func + " " + findColumn(col).column.name;
489
+ } else {
490
+ alias = col.func;
491
+ }
492
+ this._select.push(new Alias(rewritten, alias));
493
+ } else if (col instanceof AggregateOperation) {
494
+ const colNested = findColumn(col);
495
+ const node = colNested.parent;
496
+ let ta;
497
+ if (node !== void 0) {
498
+ ta = this._tableAliasForTable(colNested.column.owner, node);
499
+ } else {
500
+ ta = this._tableAliasForTable(colNested.column.owner, colNested.column.owner);
501
+ this._from.add(ta);
502
+ }
503
+ const alias = col.displayName ?? col.operator + " " + colNested.column.name;
504
+ const rewrittenAgg = new AggregateOperation(
505
+ new TableAliasColumn(colNested.column, ta),
506
+ col.operator,
507
+ void 0,
508
+ col.window ? this._rewriteWindowSpec(col.window) : void 0
509
+ );
510
+ this._select.push(new Alias(rewrittenAgg, alias));
511
+ } else if (col instanceof ScalarFunctionOperation) {
512
+ const colNested = findColumn(col);
513
+ const node = colNested.parent;
514
+ let ta;
515
+ if (node !== void 0) {
516
+ ta = this._tableAliasForTable(colNested.column.owner, node);
517
+ } else {
518
+ ta = this._tableAliasForTable(colNested.column.owner, colNested.column.owner);
519
+ this._from.add(ta);
520
+ }
521
+ const alias = col.displayName ?? col.func + " " + colNested.column.name;
522
+ this._select.push(new Alias(
523
+ new ScalarFunctionOperation(new TableAliasColumn(colNested.column, ta), col.func, void 0, col.secondArg, col.extraArgs),
524
+ alias
525
+ ));
526
+ } else if (col instanceof DateExtractOperation || col instanceof DateArithmeticOperation || col instanceof DateDiffOperation) {
527
+ const colNested = findColumn(col);
528
+ const node = colNested.parent;
529
+ let ta;
530
+ if (node !== void 0) {
531
+ ta = this._tableAliasForTable(colNested.column.owner, node);
532
+ } else {
533
+ ta = this._tableAliasForTable(colNested.column.owner, colNested.column.owner);
534
+ this._from.add(ta);
535
+ }
536
+ const alias = col.displayName ?? colNested.column.name;
537
+ let aliasedOp;
538
+ if (col instanceof DateExtractOperation) {
539
+ aliasedOp = new DateExtractOperation(new TableAliasColumn(colNested.column, ta), col.part);
540
+ } else if (col instanceof DateArithmeticOperation) {
541
+ aliasedOp = new DateArithmeticOperation(new TableAliasColumn(colNested.column, ta), col.n, col.unit, col.isAdd);
542
+ } else {
543
+ aliasedOp = new DateDiffOperation(new TableAliasColumn(colNested.column, ta), col.other, col.unit);
544
+ }
545
+ this._select.push(new Alias(aliasedOp, alias));
546
+ } else if (col instanceof CountAllOperation) {
547
+ const ta = this._tableAliasForTable(col.table, col.table);
548
+ this._from.add(ta);
549
+ this._select.push(new Alias(col, "Count"));
550
+ }
551
+ }
552
+ }
553
+ _addJoin(node) {
554
+ if (this._addedJoinIds.has(node)) return;
555
+ if (node.parent !== void 0) this._addJoin(node.parent);
556
+ this._addedJoinIds.add(node);
557
+ const join = node.join;
558
+ const left = join.left;
559
+ const srcTa = this._tableAliasForTable(left.owner, node.parent ?? left.owner);
560
+ if (node.parent === void 0) this._from.add(srcTa);
561
+ const sc = new TableAliasColumn(left, srcTa);
562
+ const right = join.right;
563
+ const tc = new TableAliasColumn(right, this._tableAliasForTable(right.owner, node));
564
+ this._join.push(new Join(sc, tc, join.filter));
565
+ }
566
+ buildFilter(op) {
567
+ if (op instanceof NoOperation) return "";
568
+ if (op instanceof ExistsOperation) {
569
+ const col = op.node.join.right;
570
+ return this.buildFilter(new ColumnWithJoin(col, op.node)) + " IS NOT NULL";
571
+ }
572
+ if (op instanceof NotExistsOperation) {
573
+ const col = op.node.join.right;
574
+ return this.buildFilter(new ColumnWithJoin(col, op.node)) + " IS NULL";
575
+ }
576
+ if (op instanceof IsNotNullOperation) return this.buildFilter(op.element) + " IS NOT NULL";
577
+ if (op instanceof IsNullOperation) return this.buildFilter(op.element) + " IS NULL";
578
+ if (op instanceof LogicalOperation) {
579
+ let left = this.buildFilter(op.left);
580
+ let right = this.buildFilter(op.right);
581
+ if (op.left instanceof LogicalOperation) left = "(" + left + ")";
582
+ if (op.right instanceof LogicalOperation) right = "(" + right + ")";
583
+ return left + LOGICAL_OPERATOR_STR[op.operator] + right;
584
+ }
585
+ if (op instanceof ComparisonOperation) {
586
+ return this.buildFilter(op.left) + COMPARISON_OPERATOR_STR[op.operator] + this.buildFilter(op.right);
587
+ }
588
+ if (op instanceof ConstantOperation) return constantValueString(op);
589
+ if (op instanceof ColumnWithJoin) {
590
+ const node = op.parent;
591
+ let ta;
592
+ if (node !== void 0) {
593
+ ta = this._tableAliasForTable(op.column.owner, node);
594
+ this._addJoin(node);
595
+ } else {
596
+ ta = this._tableAliasForTable(op.column.owner, op.column.owner);
597
+ }
598
+ return ta.alias + "." + op.column.name;
599
+ }
600
+ if (op instanceof Column) {
601
+ const ta = this._tableAliasForTable(op.owner, op.owner);
602
+ return ta.alias + "." + op.name;
603
+ }
604
+ throw new Error("Unknown operation in buildFilter: " + op.constructor.name);
605
+ }
606
+ _tableAliasForTable(table, key) {
607
+ if (this._tableAliasesByTable.has(key)) {
608
+ return this._tableAliasesByTable.get(key);
609
+ }
610
+ const ta = new TableAlias(table, "t" + this._tableAliasIncr++);
611
+ this._tableAliasesByTable.set(key, ta);
612
+ return ta;
613
+ }
614
+ buildQueryString() {
615
+ const joins = this._join.map(
616
+ (j) => " LEFT OUTER JOIN " + j.target.tableAlias.table + " AS " + j.target.tableAlias.alias + " ON " + j.source.tableAlias.alias + "." + j.source.column.name + " = " + j.target.tableAlias.alias + "." + j.target.column.name + (j.filterOp ? " AND " + this.buildFilter(j.filterOp) : "")
617
+ );
618
+ return "SELECT " + this._select.map((ca) => sqlOperationToString(ca)).join(",") + " FROM " + Array.from(this._from).map((ta) => ta.table + " AS " + ta.alias).join(",") + joins.join("") + this._buildWhere() + this._buildGroupBy() + this._buildOrderBy() + this._buildLimit();
619
+ }
620
+ _buildWhere() {
621
+ return this._where.length > 0 ? " WHERE " + this._where : "";
622
+ }
623
+ _buildGroupBy() {
624
+ return this._groupByParts.length > 0 ? " GROUP BY " + this._groupByParts.join(", ") : "";
625
+ }
626
+ _buildOrderBy() {
627
+ return this._orderByParts.length > 0 ? " ORDER BY " + this._orderByParts.join(", ") : "";
628
+ }
629
+ _buildLimit() {
630
+ return this._limit !== void 0 ? " LIMIT " + this._limit : "";
631
+ }
632
+ };
633
+ function selectSqlToString(selectOperation) {
634
+ const qg = new SQLQueryGenerator();
635
+ qg.generate(selectOperation);
636
+ return qg.buildQueryString();
637
+ }
638
+ function toSql(businessDate, processingDatetime, columns, table, op, orderBy = [], groupBy = [], limit) {
639
+ const selectOp = buildQueryOperation(businessDate, processingDatetime, columns, table, op, orderBy, groupBy, limit);
640
+ return selectSqlToString(selectOp);
641
+ }
642
+
643
+ export {
644
+ ExistsOperation,
645
+ NotExistsOperation,
646
+ RelatedFinder,
647
+ SelectOperation,
648
+ buildQueryOperation,
649
+ selectSqlToString,
650
+ toSql
651
+ };
652
+ //# sourceMappingURL=chunk-DFYZVBII.js.map