@leonardovida-md/drizzle-neo-duckdb 1.1.1 → 1.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.
- package/README.md +44 -85
- package/dist/client.d.ts +15 -1
- package/dist/columns.d.ts +3 -2
- package/dist/driver.d.ts +7 -3
- package/dist/duckdb-introspect.mjs +595 -50
- package/dist/helpers.mjs +31 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +621 -50
- package/dist/options.d.ts +10 -0
- package/dist/session.d.ts +11 -5
- package/dist/sql/query-rewriters.d.ts +12 -0
- package/package.json +1 -1
- package/src/client.ts +300 -40
- package/src/columns.ts +51 -4
- package/src/driver.ts +30 -5
- package/src/index.ts +1 -0
- package/src/options.ts +40 -0
- package/src/session.ts +128 -27
- package/src/sql/query-rewriters.ts +503 -0
|
@@ -171,6 +171,14 @@ function walkRight(
|
|
|
171
171
|
}
|
|
172
172
|
|
|
173
173
|
export function adaptArrayOperators(query: string): string {
|
|
174
|
+
if (
|
|
175
|
+
query.indexOf('@>') === -1 &&
|
|
176
|
+
query.indexOf('<@') === -1 &&
|
|
177
|
+
query.indexOf('&&') === -1
|
|
178
|
+
) {
|
|
179
|
+
return query;
|
|
180
|
+
}
|
|
181
|
+
|
|
174
182
|
let rewritten = query;
|
|
175
183
|
let scrubbed = scrubForRewrite(query);
|
|
176
184
|
let searchStart = 0;
|
|
@@ -203,3 +211,498 @@ export function adaptArrayOperators(query: string): string {
|
|
|
203
211
|
|
|
204
212
|
return rewritten;
|
|
205
213
|
}
|
|
214
|
+
|
|
215
|
+
// Join column qualification types and helpers
|
|
216
|
+
|
|
217
|
+
type TableSource = {
|
|
218
|
+
name: string; // The table/CTE name (without quotes)
|
|
219
|
+
alias?: string; // Optional alias
|
|
220
|
+
position: number; // Position in the query where this source was introduced
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
type JoinClause = {
|
|
224
|
+
joinType: string; // 'left', 'right', 'inner', 'full', 'cross', ''
|
|
225
|
+
tableName: string; // The joined table name
|
|
226
|
+
tableAlias?: string; // Optional alias
|
|
227
|
+
onStart: number; // Start of ON clause content (after "on ")
|
|
228
|
+
onEnd: number; // End of ON clause content
|
|
229
|
+
leftSource: string; // The table/alias on the left side of this join
|
|
230
|
+
rightSource: string; // The table/alias for the joined table
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Extracts the identifier name from a quoted identifier like "foo" -> foo
|
|
235
|
+
* Uses the original query string, not the scrubbed one.
|
|
236
|
+
*/
|
|
237
|
+
function extractQuotedIdentifier(
|
|
238
|
+
original: string,
|
|
239
|
+
start: number
|
|
240
|
+
): { name: string; end: number } | null {
|
|
241
|
+
if (original[start] !== '"') {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let pos = start + 1;
|
|
246
|
+
while (pos < original.length && original[pos] !== '"') {
|
|
247
|
+
// Handle escaped quotes
|
|
248
|
+
if (original[pos] === '"' && original[pos + 1] === '"') {
|
|
249
|
+
pos += 2;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
pos++;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (pos >= original.length) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
name: original.slice(start + 1, pos),
|
|
261
|
+
end: pos + 1,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Finds the main SELECT's FROM clause, skipping any FROM inside CTEs.
|
|
267
|
+
*/
|
|
268
|
+
function findMainFromClause(scrubbed: string): number {
|
|
269
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
270
|
+
|
|
271
|
+
// If there's a WITH clause, find where the CTEs end
|
|
272
|
+
let searchStart = 0;
|
|
273
|
+
const withMatch = /\bwith\s+/i.exec(lowerScrubbed);
|
|
274
|
+
if (withMatch) {
|
|
275
|
+
// Find the main SELECT after the CTEs
|
|
276
|
+
// CTEs are separated by commas and end with the main SELECT
|
|
277
|
+
let depth = 0;
|
|
278
|
+
let pos = withMatch.index + withMatch[0].length;
|
|
279
|
+
|
|
280
|
+
while (pos < scrubbed.length) {
|
|
281
|
+
const char = scrubbed[pos];
|
|
282
|
+
if (char === '(') {
|
|
283
|
+
depth++;
|
|
284
|
+
} else if (char === ')') {
|
|
285
|
+
depth--;
|
|
286
|
+
} else if (depth === 0) {
|
|
287
|
+
// Check if we're at "select" keyword (main query)
|
|
288
|
+
const remaining = lowerScrubbed.slice(pos);
|
|
289
|
+
if (/^\s*select\s+/i.test(remaining)) {
|
|
290
|
+
searchStart = pos;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
pos++;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Find FROM after the main SELECT
|
|
299
|
+
const fromPattern = /\bfrom\s+/gi;
|
|
300
|
+
fromPattern.lastIndex = searchStart;
|
|
301
|
+
const fromMatch = fromPattern.exec(lowerScrubbed);
|
|
302
|
+
|
|
303
|
+
return fromMatch ? fromMatch.index + fromMatch[0].length : -1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Parses table sources from FROM and JOIN clauses.
|
|
308
|
+
* Returns an array of table sources in order of appearance.
|
|
309
|
+
*/
|
|
310
|
+
function parseTableSources(original: string, scrubbed: string): TableSource[] {
|
|
311
|
+
const sources: TableSource[] = [];
|
|
312
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
313
|
+
|
|
314
|
+
// Find the main FROM clause (after CTEs if present)
|
|
315
|
+
const fromPos = findMainFromClause(scrubbed);
|
|
316
|
+
if (fromPos < 0) {
|
|
317
|
+
return sources;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const fromTable = parseTableRef(original, scrubbed, fromPos);
|
|
321
|
+
if (fromTable) {
|
|
322
|
+
sources.push(fromTable);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const joinPattern =
|
|
326
|
+
/\b(left\s+|right\s+|inner\s+|full\s+|cross\s+)?join\s+/gi;
|
|
327
|
+
joinPattern.lastIndex = fromPos;
|
|
328
|
+
|
|
329
|
+
let joinMatch;
|
|
330
|
+
while ((joinMatch = joinPattern.exec(lowerScrubbed)) !== null) {
|
|
331
|
+
const tableStart = joinMatch.index + joinMatch[0].length;
|
|
332
|
+
const joinTable = parseTableRef(original, scrubbed, tableStart);
|
|
333
|
+
if (joinTable) {
|
|
334
|
+
sources.push(joinTable);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return sources;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Parses a table reference (potentially with alias) starting at the given position.
|
|
343
|
+
* Handles: "table", "table" "alias", "table" as "alias", "schema"."table"
|
|
344
|
+
*/
|
|
345
|
+
function parseTableRef(
|
|
346
|
+
original: string,
|
|
347
|
+
scrubbed: string,
|
|
348
|
+
start: number
|
|
349
|
+
): TableSource | null {
|
|
350
|
+
let pos = start;
|
|
351
|
+
while (pos < scrubbed.length && isWhitespace(scrubbed[pos])) {
|
|
352
|
+
pos++;
|
|
353
|
+
}
|
|
354
|
+
if (original[pos] !== '"') {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const nameStart = pos;
|
|
359
|
+
const firstIdent = extractQuotedIdentifier(original, pos);
|
|
360
|
+
if (!firstIdent) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
let name = firstIdent.name;
|
|
365
|
+
pos = firstIdent.end;
|
|
366
|
+
|
|
367
|
+
// Check for schema.table pattern
|
|
368
|
+
let afterName = pos;
|
|
369
|
+
while (afterName < scrubbed.length && isWhitespace(scrubbed[afterName])) {
|
|
370
|
+
afterName++;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (scrubbed[afterName] === '.') {
|
|
374
|
+
afterName++;
|
|
375
|
+
while (afterName < scrubbed.length && isWhitespace(scrubbed[afterName])) {
|
|
376
|
+
afterName++;
|
|
377
|
+
}
|
|
378
|
+
if (original[afterName] === '"') {
|
|
379
|
+
const tableIdent = extractQuotedIdentifier(original, afterName);
|
|
380
|
+
if (tableIdent) {
|
|
381
|
+
name = tableIdent.name;
|
|
382
|
+
pos = tableIdent.end;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let alias: string | undefined;
|
|
388
|
+
let aliasPos = pos;
|
|
389
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
390
|
+
aliasPos++;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const afterTable = scrubbed.slice(aliasPos).toLowerCase();
|
|
394
|
+
if (afterTable.startsWith('as ')) {
|
|
395
|
+
aliasPos += 3;
|
|
396
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
397
|
+
aliasPos++;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (
|
|
402
|
+
original[aliasPos] === '"' &&
|
|
403
|
+
!afterTable.startsWith('on ') &&
|
|
404
|
+
!afterTable.startsWith('left ') &&
|
|
405
|
+
!afterTable.startsWith('right ') &&
|
|
406
|
+
!afterTable.startsWith('inner ') &&
|
|
407
|
+
!afterTable.startsWith('full ') &&
|
|
408
|
+
!afterTable.startsWith('cross ') &&
|
|
409
|
+
!afterTable.startsWith('join ') &&
|
|
410
|
+
!afterTable.startsWith('where ') &&
|
|
411
|
+
!afterTable.startsWith('group ') &&
|
|
412
|
+
!afterTable.startsWith('order ') &&
|
|
413
|
+
!afterTable.startsWith('limit ') &&
|
|
414
|
+
!afterTable.startsWith('as ')
|
|
415
|
+
) {
|
|
416
|
+
const aliasIdent = extractQuotedIdentifier(original, aliasPos);
|
|
417
|
+
if (aliasIdent) {
|
|
418
|
+
alias = aliasIdent.name;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return {
|
|
423
|
+
name,
|
|
424
|
+
alias,
|
|
425
|
+
position: nameStart,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Finds all JOIN clauses with their ON clause boundaries.
|
|
431
|
+
*/
|
|
432
|
+
function findJoinClauses(
|
|
433
|
+
original: string,
|
|
434
|
+
scrubbed: string,
|
|
435
|
+
sources: TableSource[]
|
|
436
|
+
): JoinClause[] {
|
|
437
|
+
const clauses: JoinClause[] = [];
|
|
438
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
439
|
+
|
|
440
|
+
// Pattern to find JOINs with ON clauses
|
|
441
|
+
// Use scrubbed for matching, original for extracting values
|
|
442
|
+
// Handle optional schema prefix: "schema"."table" or just "table"
|
|
443
|
+
const joinPattern =
|
|
444
|
+
/\b(left\s+|right\s+|inner\s+|full\s+|cross\s+)?join\s+"[^"]*"(\s*\.\s*"[^"]*")?(\s+as)?(\s+"[^"]*")?\s+on\s+/gi;
|
|
445
|
+
|
|
446
|
+
let match;
|
|
447
|
+
let sourceIndex = 1; // Start at 1 since index 0 is the FROM table
|
|
448
|
+
|
|
449
|
+
while ((match = joinPattern.exec(lowerScrubbed)) !== null) {
|
|
450
|
+
const joinType = (match[1] || '').trim().toLowerCase();
|
|
451
|
+
const joinKeywordEnd =
|
|
452
|
+
match.index + (match[1] || '').length + 'join'.length;
|
|
453
|
+
let tableStart = joinKeywordEnd;
|
|
454
|
+
while (tableStart < original.length && isWhitespace(original[tableStart])) {
|
|
455
|
+
tableStart++;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const tableIdent = extractQuotedIdentifier(original, tableStart);
|
|
459
|
+
if (!tableIdent) continue;
|
|
460
|
+
|
|
461
|
+
let tableName = tableIdent.name;
|
|
462
|
+
let afterTable = tableIdent.end;
|
|
463
|
+
let checkPos = afterTable;
|
|
464
|
+
while (checkPos < scrubbed.length && isWhitespace(scrubbed[checkPos])) {
|
|
465
|
+
checkPos++;
|
|
466
|
+
}
|
|
467
|
+
if (scrubbed[checkPos] === '.') {
|
|
468
|
+
checkPos++;
|
|
469
|
+
while (checkPos < scrubbed.length && isWhitespace(scrubbed[checkPos])) {
|
|
470
|
+
checkPos++;
|
|
471
|
+
}
|
|
472
|
+
const realTableIdent = extractQuotedIdentifier(original, checkPos);
|
|
473
|
+
if (realTableIdent) {
|
|
474
|
+
tableName = realTableIdent.name;
|
|
475
|
+
afterTable = realTableIdent.end;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
let tableAlias: string | undefined;
|
|
480
|
+
let aliasPos = afterTable;
|
|
481
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
482
|
+
aliasPos++;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const afterTableStr = scrubbed.slice(aliasPos).toLowerCase();
|
|
486
|
+
if (afterTableStr.startsWith('as ')) {
|
|
487
|
+
aliasPos += 3;
|
|
488
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
489
|
+
aliasPos++;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (original[aliasPos] === '"' && !afterTableStr.startsWith('on ')) {
|
|
494
|
+
const aliasIdent = extractQuotedIdentifier(original, aliasPos);
|
|
495
|
+
if (aliasIdent) {
|
|
496
|
+
tableAlias = aliasIdent.name;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const onStart = match.index + match[0].length;
|
|
501
|
+
|
|
502
|
+
// Find the end of the ON clause (next JOIN, WHERE, GROUP, ORDER, LIMIT, or end)
|
|
503
|
+
const endPattern =
|
|
504
|
+
/\b(left\s+join|right\s+join|inner\s+join|full\s+join|cross\s+join|join|where|group\s+by|order\s+by|limit|$)/i;
|
|
505
|
+
const remaining = lowerScrubbed.slice(onStart);
|
|
506
|
+
const endMatch = endPattern.exec(remaining);
|
|
507
|
+
const onEnd = endMatch ? onStart + endMatch.index : scrubbed.length;
|
|
508
|
+
|
|
509
|
+
// Determine the left source (previous table/CTE)
|
|
510
|
+
let leftSource = '';
|
|
511
|
+
if (sourceIndex > 0 && sourceIndex <= sources.length) {
|
|
512
|
+
const prev = sources[sourceIndex - 1];
|
|
513
|
+
leftSource = prev?.alias || prev?.name || '';
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const rightSource = tableAlias || tableName;
|
|
517
|
+
|
|
518
|
+
clauses.push({
|
|
519
|
+
joinType,
|
|
520
|
+
tableName,
|
|
521
|
+
tableAlias,
|
|
522
|
+
onStart,
|
|
523
|
+
onEnd,
|
|
524
|
+
leftSource,
|
|
525
|
+
rightSource,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
sourceIndex++;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return clauses;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Qualifies unqualified column references in JOIN ON clauses.
|
|
536
|
+
*
|
|
537
|
+
* Transforms patterns like:
|
|
538
|
+
* `left join "b" on "col" = "col"`
|
|
539
|
+
* To:
|
|
540
|
+
* `left join "b" on "a"."col" = "b"."col"`
|
|
541
|
+
*
|
|
542
|
+
* This fixes the issue where drizzle-orm generates unqualified column
|
|
543
|
+
* references when joining CTEs with eq().
|
|
544
|
+
*/
|
|
545
|
+
export function qualifyJoinColumns(query: string): string {
|
|
546
|
+
const lowerQuery = query.toLowerCase();
|
|
547
|
+
if (!lowerQuery.includes('join')) {
|
|
548
|
+
return query;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const scrubbed = scrubForRewrite(query);
|
|
552
|
+
const sources = parseTableSources(query, scrubbed);
|
|
553
|
+
if (sources.length < 2) {
|
|
554
|
+
return query;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const joinClauses = findJoinClauses(query, scrubbed, sources);
|
|
558
|
+
|
|
559
|
+
if (joinClauses.length === 0) {
|
|
560
|
+
return query;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let result = query;
|
|
564
|
+
let offset = 0;
|
|
565
|
+
|
|
566
|
+
for (const join of joinClauses) {
|
|
567
|
+
const scrubbedOnClause = scrubbed.slice(join.onStart, join.onEnd);
|
|
568
|
+
const originalOnClause = query.slice(join.onStart, join.onEnd);
|
|
569
|
+
let clauseResult = originalOnClause;
|
|
570
|
+
let clauseOffset = 0;
|
|
571
|
+
let eqPos = -1;
|
|
572
|
+
while ((eqPos = scrubbedOnClause.indexOf('=', eqPos + 1)) !== -1) {
|
|
573
|
+
let lhsEnd = eqPos - 1;
|
|
574
|
+
while (lhsEnd >= 0 && isWhitespace(scrubbedOnClause[lhsEnd])) {
|
|
575
|
+
lhsEnd--;
|
|
576
|
+
}
|
|
577
|
+
if (scrubbedOnClause[lhsEnd] !== '"') continue;
|
|
578
|
+
|
|
579
|
+
let lhsStartPos = lhsEnd - 1;
|
|
580
|
+
while (lhsStartPos >= 0 && scrubbedOnClause[lhsStartPos] !== '"') {
|
|
581
|
+
lhsStartPos--;
|
|
582
|
+
}
|
|
583
|
+
if (lhsStartPos < 0) continue;
|
|
584
|
+
|
|
585
|
+
const lhsIsQualified =
|
|
586
|
+
lhsStartPos > 0 && scrubbedOnClause[lhsStartPos - 1] === '.';
|
|
587
|
+
let rhsStartPos = eqPos + 1;
|
|
588
|
+
while (
|
|
589
|
+
rhsStartPos < scrubbedOnClause.length &&
|
|
590
|
+
isWhitespace(scrubbedOnClause[rhsStartPos])
|
|
591
|
+
) {
|
|
592
|
+
rhsStartPos++;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const rhsChar = originalOnClause[rhsStartPos];
|
|
596
|
+
const rhsIsParam = rhsChar === '$';
|
|
597
|
+
const rhsIsStringLiteral = rhsChar === "'";
|
|
598
|
+
const rhsIsColumn = rhsChar === '"';
|
|
599
|
+
|
|
600
|
+
if (!rhsIsParam && !rhsIsStringLiteral && !rhsIsColumn) continue;
|
|
601
|
+
|
|
602
|
+
const rhsIsQualified =
|
|
603
|
+
!rhsIsColumn ||
|
|
604
|
+
(rhsStartPos > 0 && scrubbedOnClause[rhsStartPos - 1] === '.');
|
|
605
|
+
if (lhsIsQualified || rhsIsQualified) continue;
|
|
606
|
+
|
|
607
|
+
const lhsIdent = extractQuotedIdentifier(originalOnClause, lhsStartPos);
|
|
608
|
+
if (!lhsIdent) continue;
|
|
609
|
+
|
|
610
|
+
let rhsIdent: { name: string; end: number } | null = null;
|
|
611
|
+
let rhsValue = '';
|
|
612
|
+
let rhsEnd = rhsStartPos;
|
|
613
|
+
|
|
614
|
+
if (rhsIsParam) {
|
|
615
|
+
let paramEnd = rhsStartPos + 1;
|
|
616
|
+
while (
|
|
617
|
+
paramEnd < originalOnClause.length &&
|
|
618
|
+
/\d/.test(originalOnClause[paramEnd]!)
|
|
619
|
+
) {
|
|
620
|
+
paramEnd++;
|
|
621
|
+
}
|
|
622
|
+
rhsValue = originalOnClause.slice(rhsStartPos, paramEnd);
|
|
623
|
+
rhsEnd = paramEnd;
|
|
624
|
+
} else if (rhsIsStringLiteral) {
|
|
625
|
+
let literalEnd = rhsStartPos + 1;
|
|
626
|
+
while (literalEnd < originalOnClause.length) {
|
|
627
|
+
if (originalOnClause[literalEnd] === "'") {
|
|
628
|
+
if (originalOnClause[literalEnd + 1] === "'") {
|
|
629
|
+
literalEnd += 2;
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
literalEnd++;
|
|
635
|
+
}
|
|
636
|
+
rhsValue = originalOnClause.slice(rhsStartPos, literalEnd + 1);
|
|
637
|
+
rhsEnd = literalEnd + 1;
|
|
638
|
+
} else if (rhsIsColumn) {
|
|
639
|
+
rhsIdent = extractQuotedIdentifier(originalOnClause, rhsStartPos);
|
|
640
|
+
if (rhsIdent) {
|
|
641
|
+
// Check if this identifier is followed by a dot (meaning it's a table prefix, not the column)
|
|
642
|
+
if (
|
|
643
|
+
rhsIdent.end < scrubbedOnClause.length &&
|
|
644
|
+
scrubbedOnClause[rhsIdent.end] === '.'
|
|
645
|
+
) {
|
|
646
|
+
// This is a qualified reference "table"."column" - skip, it's already qualified
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
rhsValue = `"${rhsIdent.name}"`;
|
|
650
|
+
rhsEnd = rhsIdent.end;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (!rhsValue) continue;
|
|
655
|
+
|
|
656
|
+
// Only qualify when both sides are columns with the same name.
|
|
657
|
+
// Only same-named columns cause "Ambiguous reference" errors in DuckDB.
|
|
658
|
+
if (!rhsIsColumn || !rhsIdent || lhsIdent.name !== rhsIdent.name) {
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const lhsOriginal = `"${lhsIdent.name}"`;
|
|
663
|
+
let newLhs = lhsOriginal;
|
|
664
|
+
let newRhs = rhsValue;
|
|
665
|
+
|
|
666
|
+
if (!lhsIsQualified && join.leftSource) {
|
|
667
|
+
newLhs = `"${join.leftSource}"."${lhsIdent.name}"`;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if (!rhsIsQualified && rhsIsColumn && rhsIdent && join.rightSource) {
|
|
671
|
+
newRhs = `"${join.rightSource}"."${rhsIdent.name}"`;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
if (newLhs !== lhsOriginal || newRhs !== rhsValue) {
|
|
675
|
+
const opStart = lhsIdent.end;
|
|
676
|
+
let opEnd = opStart;
|
|
677
|
+
while (
|
|
678
|
+
opEnd < rhsEnd &&
|
|
679
|
+
(isWhitespace(originalOnClause[opEnd]) ||
|
|
680
|
+
originalOnClause[opEnd] === '=')
|
|
681
|
+
) {
|
|
682
|
+
opEnd++;
|
|
683
|
+
}
|
|
684
|
+
const operator = originalOnClause.slice(opStart, opEnd);
|
|
685
|
+
|
|
686
|
+
const newExpr = `${newLhs}${operator}${newRhs}`;
|
|
687
|
+
const oldExprLength = rhsEnd - lhsStartPos;
|
|
688
|
+
|
|
689
|
+
clauseResult =
|
|
690
|
+
clauseResult.slice(0, lhsStartPos + clauseOffset) +
|
|
691
|
+
newExpr +
|
|
692
|
+
clauseResult.slice(lhsStartPos + oldExprLength + clauseOffset);
|
|
693
|
+
|
|
694
|
+
clauseOffset += newExpr.length - oldExprLength;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (clauseResult !== originalOnClause) {
|
|
699
|
+
result =
|
|
700
|
+
result.slice(0, join.onStart + offset) +
|
|
701
|
+
clauseResult +
|
|
702
|
+
result.slice(join.onEnd + offset);
|
|
703
|
+
offset += clauseResult.length - originalOnClause.length;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
return result;
|
|
708
|
+
}
|