Skip to content

Commit cef9488

Browse files
committed
Fix issue #217: Wrap FuncCall arguments in parentheses when using :: cast syntax
- Fixes operator precedence issues with double casts like CAST(...AS text)::date - Wraps FuncCall arguments (e.g., AT TIME ZONE expressions) in parentheses - Prevents :: operator from binding to wrong operand - Refactored empty if block to pass lint checks - Updated pg-catalog snapshot to reflect correct parenthesization This is a minimal fix with only 13 lines changed in the TypeCast function. Co-Authored-By: Dan Lynch <[email protected]>
1 parent 95ae97e commit cef9488

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/deparser/__tests__/misc/__snapshots__/pg-catalog.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`should format pg_catalog.char with pretty option enabled 1`] = `
44
"CREATE TABLE dashboard_jobs.jobs (
55
id bigserial PRIMARY KEY,
6-
queue_name text DEFAULT public.gen_random_uuid()::text,
6+
queue_name text DEFAULT (public.gen_random_uuid())::text,
77
task_identifier text NOT NULL,
88
payload pg_catalog.json DEFAULT '{}'::json NOT NULL,
99
priority int DEFAULT 0 NOT NULL,

packages/deparser/src/deparser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,9 +2255,14 @@ export class Deparser implements DeparserVisitor {
22552255

22562256
if (isSimpleArgument || isFunctionCall) {
22572257
// For simple arguments, avoid :: syntax if they have complex structure
2258-
if (isSimpleArgument && (arg.includes('(') || arg.startsWith('-'))) {
2259-
} else {
2258+
const shouldUseCastSyntax = isSimpleArgument && (arg.includes('(') || arg.startsWith('-'));
2259+
2260+
if (!shouldUseCastSyntax) {
22602261
const cleanTypeName = typeName.replace('pg_catalog.', '');
2262+
// Wrap FuncCall arguments in parentheses to prevent operator precedence issues
2263+
if (isFunctionCall) {
2264+
return `(${arg})::${cleanTypeName}`;
2265+
}
22612266
return `${arg}::${cleanTypeName}`;
22622267
}
22632268
}

0 commit comments

Comments
 (0)