Skip to content

Commit

Permalink
fix: handle AExpr and NullTest nodes in SQL parsing (#354)
Browse files Browse the repository at this point in the history
This fix addresses an issue where AExpr and NullTest nodes were not being correctly handled in SQL parsing. This ensures that expressions like  and  are properly interpreted and typed.
  • Loading branch information
Newbie012 authored Jan 1, 2025
1 parent 2b0dcbe commit e583c77
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/olive-coins-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ts-safeql/eslint-plugin": patch
"@ts-safeql/generate": patch
---

fixed an issue when dealing with AExpr and NullTest nodes
21 changes: 21 additions & 0 deletions packages/eslint-plugin/src/rules/check-sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,27 @@ RuleTester.describe("check-sql", () => {
}),
code: "sql<{ phone_number: PhoneNumber }>`select phone_number from caregiver_phone`",
},
{
name: "select case when then jsonb with not like with type reference",
filename,
options: withConnection(connections.withTag, {
targets: [{ tag: "sql", transform: "{type}[]" }],
}),
code: `
type Meta = { is_test: boolean; };
type Caregiver = { meta: Meta | null };
await sql<Caregiver[]>\`
SELECT
CASE WHEN caregiver.id IS NOT NULL
THEN jsonb_build_object('is_test', caregiver.middle_name NOT LIKE '%test%')
ELSE NULL
END AS meta
FROM
caregiver
\`;
`,
},
],
invalid: [
{
Expand Down
40 changes: 40 additions & 0 deletions packages/generate/src/ast-describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,49 @@ function getDescribedNode(params: {
return getDescribedCaseExpr({ alias: alias, node: node.CaseExpr, context });
}

if (node.NullTest !== undefined) {
return getDescribedNullTest({ alias: alias, node: node.NullTest, context });
}

if (node.A_Expr !== undefined) {
return getDescribedAExpr({ alias: alias, node: node.A_Expr, context });
}

return [];
}

function getDescribedAExpr({
alias,
context,
}: GetDescribedParamsOf<LibPgQueryAST.AExpr>): ASTDescribedColumn[] {
return [
{
name: alias ?? "?column?",
type: resolveType({
context: context,
nullable: false,
type: context.toTypeScriptType({ name: "boolean" }),
}),
},
];
}

function getDescribedNullTest({
alias,
context,
}: GetDescribedParamsOf<LibPgQueryAST.NullTest>): ASTDescribedColumn[] {
return [
{
name: alias ?? "?column?",
type: resolveType({
context: context,
nullable: false,
type: context.toTypeScriptType({ name: "boolean" }),
}),
},
];
}

function getDescribedCaseExpr({
alias,
node,
Expand Down
43 changes: 43 additions & 0 deletions packages/generate/src/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,46 @@ test("select case when expr with returned string literals", async () => {
expected: [["case", { kind: "type", value: "boolean" }]],
});
});

test("select case when expr is not null else is not null", async () => {
await testQuery({
query: `
SELECT
CASE WHEN TRUE
THEN caregiver.id IS NOT NULL
ELSE caregiver.id IS NOT NULL
END
FROM caregiver`,
expected: [["case", { kind: "type", value: "boolean" }]],
});
});

test("select case when with jsonb_build_object", async () => {
await testQuery({
query: `
SELECT
CASE
WHEN caregiver.id IS NOT NULL THEN (
jsonb_build_object(
'is_test',
caregiver.first_name NOT LIKE '%test%'
)
)
ELSE NULL
END AS col
FROM
caregiver`,
expected: [
[
"col",
{
kind: "union",
value: [
{ kind: "object", value: [["is_test", { kind: "type", value: "boolean" }]] },
{ kind: "type", value: "null" },
],
},
],
],
});
});

0 comments on commit e583c77

Please sign in to comment.