Skip to content

Commit 50b4b12

Browse files
authored
fix: select from nested join expression (#287)
* fix: properly retrieve join expression name Ensure that the join expression name is correctly retrieved by using a recursive function to handle nested joins. * changeset
1 parent d2acb02 commit 50b4b12

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

.changeset/forty-tips-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ts-safeql/generate": patch
3+
---
4+
5+
Fixed an issue when selecting from nested join expressions

packages/generate/src/generate.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,37 @@ test("select union select", async () => {
12451245
});
12461246
});
12471247

1248+
test("join inside join without an alias", async () => {
1249+
await testQuery({
1250+
query: `
1251+
SELECT cc.caregiver_id
1252+
FROM caregiver c
1253+
LEFT JOIN (
1254+
caregiver_certification cc
1255+
INNER JOIN caregiver_phonenumber cp
1256+
ON cp.caregiver_id = cc.caregiver_id
1257+
) ON cc.caregiver_id = c.id;
1258+
`,
1259+
expected: [["caregiver_id", { kind: "type", value: "number" }]],
1260+
});
1261+
});
1262+
1263+
test("join inside join without an alias (duplicate columns)", async () => {
1264+
await testQuery({
1265+
query: `
1266+
SELECT *
1267+
FROM caregiver c
1268+
LEFT JOIN (
1269+
caregiver_certification cc
1270+
INNER JOIN caregiver_phonenumber cp
1271+
ON cp.caregiver_id = cc.caregiver_id
1272+
) ON cc.caregiver_id = c.id;
1273+
`,
1274+
expectedError:
1275+
"Duplicate columns: caregiver_certification.caregiver_id, caregiver_phonenumber.caregiver_id",
1276+
});
1277+
});
1278+
12481279
test("should distinguish between schema", async () => {
12491280
await testQuery({
12501281
query: `SELECT name FROM table1`,

packages/generate/src/utils/get-relations-with-joins.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,22 @@ export function getRelationsWithJoins(parsed: LibPgQueryAST.ParseResult): Relati
2626
return results;
2727
}
2828

29+
function recursiveGetJoinName(joinExpr: LibPgQueryAST.JoinExpr): string | undefined {
30+
if (joinExpr.rarg?.JoinExpr !== undefined) {
31+
return recursiveGetJoinName(joinExpr.rarg.JoinExpr);
32+
}
33+
34+
return joinExpr.rarg?.RangeVar?.relname ?? joinExpr.rarg?.RangeSubselect?.alias?.aliasname;
35+
}
36+
2937
function recursiveTraverseJoins(
3038
joins: Join[],
3139
joinExpr: LibPgQueryAST.JoinExpr,
3240
): {
3341
relName: string;
3442
joins: Join[];
3543
} {
36-
const joinName =
37-
joinExpr.rarg?.RangeVar?.relname ?? joinExpr.rarg?.RangeSubselect?.alias?.aliasname;
44+
const joinName = recursiveGetJoinName(joinExpr);
3845
const aliasName = joinExpr.rarg?.RangeVar?.alias?.aliasname;
3946

4047
if (joinName === undefined) {

0 commit comments

Comments
 (0)