From 8fc8028cf9e85b97b4c8758c1da36f10d5e90e26 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 23 Nov 2026 08:05:44 +0000 Subject: [PATCH] Choose TypeCast syntax from the argument AST --- .../deparser/__tests__/misc/typecast.test.ts | 23 +++++++++ packages/deparser/src/deparser.ts | 49 +++++++++---------- 2 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 packages/deparser/__tests__/misc/typecast.test.ts diff --git a/packages/deparser/__tests__/misc/typecast.test.ts b/packages/deparser/__tests__/misc/typecast.test.ts new file mode 100644 index 00000000..cb08c053 --- /dev/null +++ b/packages/deparser/__tests__/misc/typecast.test.ts @@ -0,0 +1,23 @@ +import { expectParseDeparse } from '../../test-utils'; + +it.each([ + ["CAST('(' AS text)", "'('::text"], + ["CAST('(-)' AS text)", "'(-)'::text"], + ["CAST('-hello' AS text)", "'-hello'::text"], + ['CAST("a(b" AS integer)', '"a(b"::int'], + ['-1::integer', '- 1::int'], + ['(-1)::integer', 'CAST(-1 AS int)'], + ['-1.5::numeric', '- 1.5::numeric'], + ['(-1.5)::numeric', 'CAST(-1.5 AS numeric)'], + ['(-0.0)::numeric', 'CAST(-0.0 AS numeric)'], + ['(-9223372036854775808)::integer', 'CAST(-9223372036854775808 AS int)'], + ['(1 + 2)::integer', 'CAST(1 + 2 AS int)'], + ["length('abc')::integer", "(length('abc'))::int"], + ["'x'::pg_catalog.bpchar", "CAST('x' AS pg_catalog.bpchar)"], + ["'x'::pg_catalog.bpchar(3)", "CAST('x' AS char(3))"], + ["'x'::pg_catalog.bpchar[]", "CAST('x' AS pg_catalog.bpchar[])"], + ["'x'::bpchar", "'x'::bpchar"], + ["'x'::app.some_type", "CAST('x' AS app.some_type)"], +])('deparses %s without changing the cast AST', async (input, output) => { + expect(await expectParseDeparse(`SELECT ${input}`)).toBe(`SELECT ${output}`); +}); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 90ce5efd..d8db0184 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -2446,40 +2446,35 @@ export class Deparser implements DeparserVisitor { return `COALESCE(${argStrs.join(', ')})`; } + private isNegativeConstant(node: t.A_Const): boolean { + const constant = node as any; + const integer = constant.ival?.ival ?? constant.ival ?? constant.val?.Integer?.ival ?? + constant.Integer?.ival ?? constant.Integer; + const numeric = constant.fval?.fval ?? constant.fval ?? constant.val?.Float?.fval ?? + constant.Float?.fval ?? constant.Float; + // PG stores floats and oversized integers as lexemes, including the sign of -0.0. + return String(integer ?? numeric ?? '').startsWith('-'); + } + TypeCast(node: t.TypeCast, context: DeparserContext): string { const arg = this.visit(node.arg, context); const typeName = this.TypeName(node.typeName, context); - // Check if this is a bpchar typecast that should preserve original syntax for AST consistency - if (typeName === 'bpchar' || typeName === 'pg_catalog.bpchar') { - const names = node.typeName?.names; - const isQualifiedBpchar = names && names.length === 2 && - (names[0] as any)?.String?.sval === 'pg_catalog' && - (names[1] as any)?.String?.sval === 'bpchar'; - - if (isQualifiedBpchar) { - return `CAST(${arg} AS ${typeName})`; - } + const names = node.typeName?.names; + if (names?.length === 2 && + 'String' in names[0] && names[0].String.sval === 'pg_catalog' && + 'String' in names[1] && names[1].String.sval === 'bpchar') { + return `CAST(${arg} AS ${typeName})`; } if (this.isPgCatalogType(typeName)) { - const argType = this.getNodeType(node.arg); - - const isSimpleArgument = argType === 'A_Const' || argType === 'ColumnRef'; - const isFunctionCall = argType === 'FuncCall'; - - if (isSimpleArgument || isFunctionCall) { - // For simple arguments, avoid :: syntax if they have complex structure - const shouldUseCastSyntax = isSimpleArgument && (arg.includes('(') || arg.startsWith('-')); - - if (!shouldUseCastSyntax) { - const cleanTypeName = typeName.replace('pg_catalog.', ''); - // Wrap FuncCall arguments in parentheses to prevent operator precedence issues - if (isFunctionCall) { - return `${context.parens(arg)}::${cleanTypeName}`; - } - return `${arg}::${cleanTypeName}`; - } + const cleanTypeName = typeName.replace(/^pg_catalog\./, ''); + if ('FuncCall' in node.arg) { + return `${context.parens(arg)}::${cleanTypeName}`; + } + // A bare minus binds outside a postfix cast, not inside its argument. + if ('ColumnRef' in node.arg || ('A_Const' in node.arg && !this.isNegativeConstant(node.arg.A_Const))) { + return `${arg}::${cleanTypeName}`; } }