From 543cbd49df7a598b600df78f12b76ff153f9578c Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Fri, 20 Nov 2026 07:27:52 +0000 Subject: [PATCH 1/2] fix(plpgsql-deparser): preserve internal char declaration quotes --- __fixtures__/plpgsql-generated/generated.json | 1 + .../plpgsql/plpgsql_deparser_fixes.sql | 11 ++++++++++ .../__snapshots__/quoted-char.test.ts.snap | 11 ++++++++++ .../__tests__/quoted-char.test.ts | 20 +++++++++++++++++++ .../plpgsql-deparser/src/plpgsql-deparser.ts | 4 ++-- 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap create mode 100644 packages/plpgsql-deparser/__tests__/quoted-char.test.ts diff --git a/__fixtures__/plpgsql-generated/generated.json b/__fixtures__/plpgsql-generated/generated.json index 3b11f9c2..b8848748 100644 --- a/__fixtures__/plpgsql-generated/generated.json +++ b/__fixtures__/plpgsql-generated/generated.json @@ -168,6 +168,7 @@ "plpgsql_deparser_fixes-68.sql": "CREATE FUNCTION test_multiline_literals(x boolean) RETURNS text\nLANGUAGE plpgsql AS $$\nDECLARE\n v text := 'declared\ndefault';\nBEGIN\n IF x THEN\n FOR i IN 1..2 LOOP\n RAISE NOTICE 'line one\nline two';\n PERFORM length('a\nb');\n v := 'c\nd' || E'tab\\there\nquote\\'s';\n EXECUTE $q$SELECT 'e\nf'$q$;\n BEGIN\n INSERT INTO t (memo) VALUES ('g\nh') ON CONFLICT (memo) DO UPDATE SET memo = 'i\nj';\n EXCEPTION WHEN others THEN\n RAISE EXCEPTION 'failed:\n%', sqlerrm;\n END;\n END LOOP;\n ELSIF v = 'k\nl' THEN\n CASE v WHEN 'm\nn' THEN\n RETURN 'o\np';\n ELSE\n NULL;\n END CASE;\n END IF;\n RETURN v;\nEND$$", "plpgsql_deparser_fixes-69.sql": "DO $do$\nDECLARE\n r record;\nBEGIN\n FOR r IN SELECT \"odd\ncolumn\" AS c FROM t WHERE memo = $s$dollar\nquoted$s$ LOOP\n WHILE r.c IS NOT NULL LOOP\n RAISE NOTICE '%', r.c || '\nsuffix';\n EXIT;\n END LOOP;\n END LOOP;\nEND$do$", "plpgsql_deparser_fixes-70.sql": "CREATE FUNCTION test_searched_case_in(x int) RETURNS text\nLANGUAGE plpgsql AS $$\nBEGIN\n CASE\n WHEN x IN (1, 2) THEN\n RETURN 'low';\n WHEN x IN (\n 3, 4) THEN\n RETURN 'mid';\n ELSE\n RETURN 'high';\n END CASE;\nEND$$", + "plpgsql_deparser_fixes-71.sql": "CREATE FUNCTION test_internal_char() RETURNS \"char\"\nLANGUAGE plpgsql AS $$\nDECLARE\n c \"char\" := 'a';\n chars \"char\"[] := ARRAY['a'::\"char\"];\n ordinary char := 'b';\nBEGIN\n RETURN c;\nEND$$", "plpgsql_control-1.sql": "do $$\nbegin\n -- basic case\n for i in 1..3 loop\n raise notice '1..3: i = %', i;\n end loop;\n -- with BY, end matches exactly\n for i in 1..10 by 3 loop\n raise notice '1..10 by 3: i = %', i;\n end loop;\n -- with BY, end does not match\n for i in 1..11 by 3 loop\n raise notice '1..11 by 3: i = %', i;\n end loop;\n -- zero iterations\n for i in 1..0 by 3 loop\n raise notice '1..0 by 3: i = %', i;\n end loop;\n -- REVERSE\n for i in reverse 10..0 by 3 loop\n raise notice 'reverse 10..0 by 3: i = %', i;\n end loop;\n -- potential overflow\n for i in 2147483620..2147483647 by 10 loop\n raise notice '2147483620..2147483647 by 10: i = %', i;\n end loop;\n -- potential overflow, reverse direction\n for i in reverse -2147483620..-2147483647 by 10 loop\n raise notice 'reverse -2147483620..-2147483647 by 10: i = %', i;\n end loop;\nend$$", "plpgsql_control-2.sql": "do $$\nbegin\n for i in 1..3 by 0 loop\n raise notice '1..3 by 0: i = %', i;\n end loop;\nend$$", "plpgsql_control-3.sql": "do $$\nbegin\n for i in 1..3 by -1 loop\n raise notice '1..3 by -1: i = %', i;\n end loop;\nend$$", diff --git a/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql b/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql index ba6eaff7..b53e656d 100644 --- a/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql +++ b/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql @@ -908,3 +908,14 @@ BEGIN RETURN 'high'; END CASE; END$$; + +-- Test 71: internal "char" declarations retain their distinct type +CREATE FUNCTION test_internal_char() RETURNS "char" +LANGUAGE plpgsql AS $$ +DECLARE + c "char" := 'a'; + chars "char"[] := ARRAY['a'::"char"]; + ordinary char := 'b'; +BEGIN + RETURN c; +END$$; diff --git a/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap b/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap new file mode 100644 index 00000000..1e5e294a --- /dev/null +++ b/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`preserves internal char and char array declarations 1`] = ` +"DECLARE + c "char" := 'a'; + chars "char"[] := ARRAY['a'::"char"]; + ordinary char := 'b'; +BEGIN + RETURN c; +END" +`; diff --git a/packages/plpgsql-deparser/__tests__/quoted-char.test.ts b/packages/plpgsql-deparser/__tests__/quoted-char.test.ts new file mode 100644 index 00000000..312bdf59 --- /dev/null +++ b/packages/plpgsql-deparser/__tests__/quoted-char.test.ts @@ -0,0 +1,20 @@ +import { loadModule, parsePlPgSQLSync } from 'libpg-query'; + +import { deparseSync, PLpgSQLParseResult } from '../src'; +import { PLpgSQLTestUtils } from '../test-utils'; + +const fixtures = require('../../../__fixtures__/plpgsql-generated/generated.json'); + +beforeAll(async () => { + await loadModule(); +}); + +it('preserves internal char and char array declarations', async () => { + const sql = fixtures['plpgsql_deparser_fixes-71.sql']; + await new PLpgSQLTestUtils().expectAstMatch('quoted char', sql); + const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult; + const output = deparseSync(parsed); + expect(output).toContain('c "char"'); + expect(output).toContain('chars "char"[]'); + expect(output).toMatchSnapshot(); +}); diff --git a/packages/plpgsql-deparser/src/plpgsql-deparser.ts b/packages/plpgsql-deparser/src/plpgsql-deparser.ts index fe24310b..a19bcdd3 100644 --- a/packages/plpgsql-deparser/src/plpgsql-deparser.ts +++ b/packages/plpgsql-deparser/src/plpgsql-deparser.ts @@ -883,8 +883,8 @@ export class PLpgSQLDeparser { const isSchemaQualified = this.isSchemaQualifiedType(typname); if (!isSchemaQualified) { - // Simple type - just strip quotes and return as-is - return typname.replace(/"/g, '').trim(); + const trimmed = typname.trim(); + return /^"char"(\[\d*\])*$/.test(trimmed) ? trimmed : trimmed.replace(/"/g, ''); } // Schema-qualified type - apply proper quoting From 0f3ca118f3a159ae768d2129f844612fab1ec7bb Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Sat, 21 Nov 2026 00:39:45 +0000 Subject: [PATCH 2/2] fix(plpgsql-deparser): preserve required simple type quoting --- __fixtures__/plpgsql-generated/generated.json | 1 + __fixtures__/plpgsql/plpgsql_deparser_fixes.sql | 15 +++++++++++++++ .../__snapshots__/quoted-char.test.ts.snap | 14 ++++++++++++++ .../__tests__/quoted-char.test.ts | 16 ++++++++++++++++ .../plpgsql-deparser/src/plpgsql-deparser.ts | 7 ++++++- 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/__fixtures__/plpgsql-generated/generated.json b/__fixtures__/plpgsql-generated/generated.json index b8848748..f49b2329 100644 --- a/__fixtures__/plpgsql-generated/generated.json +++ b/__fixtures__/plpgsql-generated/generated.json @@ -169,6 +169,7 @@ "plpgsql_deparser_fixes-69.sql": "DO $do$\nDECLARE\n r record;\nBEGIN\n FOR r IN SELECT \"odd\ncolumn\" AS c FROM t WHERE memo = $s$dollar\nquoted$s$ LOOP\n WHILE r.c IS NOT NULL LOOP\n RAISE NOTICE '%', r.c || '\nsuffix';\n EXIT;\n END LOOP;\n END LOOP;\nEND$do$", "plpgsql_deparser_fixes-70.sql": "CREATE FUNCTION test_searched_case_in(x int) RETURNS text\nLANGUAGE plpgsql AS $$\nBEGIN\n CASE\n WHEN x IN (1, 2) THEN\n RETURN 'low';\n WHEN x IN (\n 3, 4) THEN\n RETURN 'mid';\n ELSE\n RETURN 'high';\n END CASE;\nEND$$", "plpgsql_deparser_fixes-71.sql": "CREATE FUNCTION test_internal_char() RETURNS \"char\"\nLANGUAGE plpgsql AS $$\nDECLARE\n c \"char\" := 'a';\n chars \"char\"[] := ARRAY['a'::\"char\"];\n ordinary char := 'b';\nBEGIN\n RETURN c;\nEND$$", + "plpgsql_deparser_fixes-72.sql": "CREATE FUNCTION test_quoted_types() RETURNS void\nLANGUAGE plpgsql AS $$\nDECLARE\n keyword_type \"end\";\n spaced_type \"has space\";\n mixed_type \"MixedCase\";\n escaped_type \"has\"\"quote\";\n dotted_type \"has.dot\";\n array_type \"MixedCase\"[][];\n ordinary_type simple;\nBEGIN\n NULL;\nEND$$", "plpgsql_control-1.sql": "do $$\nbegin\n -- basic case\n for i in 1..3 loop\n raise notice '1..3: i = %', i;\n end loop;\n -- with BY, end matches exactly\n for i in 1..10 by 3 loop\n raise notice '1..10 by 3: i = %', i;\n end loop;\n -- with BY, end does not match\n for i in 1..11 by 3 loop\n raise notice '1..11 by 3: i = %', i;\n end loop;\n -- zero iterations\n for i in 1..0 by 3 loop\n raise notice '1..0 by 3: i = %', i;\n end loop;\n -- REVERSE\n for i in reverse 10..0 by 3 loop\n raise notice 'reverse 10..0 by 3: i = %', i;\n end loop;\n -- potential overflow\n for i in 2147483620..2147483647 by 10 loop\n raise notice '2147483620..2147483647 by 10: i = %', i;\n end loop;\n -- potential overflow, reverse direction\n for i in reverse -2147483620..-2147483647 by 10 loop\n raise notice 'reverse -2147483620..-2147483647 by 10: i = %', i;\n end loop;\nend$$", "plpgsql_control-2.sql": "do $$\nbegin\n for i in 1..3 by 0 loop\n raise notice '1..3 by 0: i = %', i;\n end loop;\nend$$", "plpgsql_control-3.sql": "do $$\nbegin\n for i in 1..3 by -1 loop\n raise notice '1..3 by -1: i = %', i;\n end loop;\nend$$", diff --git a/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql b/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql index b53e656d..889c3115 100644 --- a/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql +++ b/__fixtures__/plpgsql/plpgsql_deparser_fixes.sql @@ -919,3 +919,18 @@ DECLARE BEGIN RETURN c; END$$; + +-- Test 72: simple type identifiers retain required quoting +CREATE FUNCTION test_quoted_types() RETURNS void +LANGUAGE plpgsql AS $$ +DECLARE + keyword_type "end"; + spaced_type "has space"; + mixed_type "MixedCase"; + escaped_type "has""quote"; + dotted_type "has.dot"; + array_type "MixedCase"[][]; + ordinary_type simple; +BEGIN + NULL; +END$$; diff --git a/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap b/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap index 1e5e294a..4a886351 100644 --- a/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap +++ b/packages/plpgsql-deparser/__tests__/__snapshots__/quoted-char.test.ts.snap @@ -9,3 +9,17 @@ BEGIN RETURN c; END" `; + +exports[`preserves required quoting in simple type declarations 1`] = ` +"DECLARE + keyword_type "end"; + spaced_type "has space"; + mixed_type "MixedCase"; + escaped_type "has""quote"; + dotted_type "has.dot"; + array_type "MixedCase"[][]; + ordinary_type simple; +BEGIN + RETURN; +END" +`; diff --git a/packages/plpgsql-deparser/__tests__/quoted-char.test.ts b/packages/plpgsql-deparser/__tests__/quoted-char.test.ts index 312bdf59..292879fe 100644 --- a/packages/plpgsql-deparser/__tests__/quoted-char.test.ts +++ b/packages/plpgsql-deparser/__tests__/quoted-char.test.ts @@ -16,5 +16,21 @@ it('preserves internal char and char array declarations', async () => { const output = deparseSync(parsed); expect(output).toContain('c "char"'); expect(output).toContain('chars "char"[]'); + expect(output).not.toContain('ordinary "char"'); + expect(output).toMatchSnapshot(); +}); + +it('preserves required quoting in simple type declarations', async () => { + const sql = fixtures['plpgsql_deparser_fixes-72.sql']; + await new PLpgSQLTestUtils().expectAstMatch('quoted simple types', sql); + const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult; + const output = deparseSync(parsed); + expect(output).toContain('keyword_type "end"'); + expect(output).toContain('spaced_type "has space"'); + expect(output).toContain('mixed_type "MixedCase"'); + expect(output).toContain('escaped_type "has""quote"'); + expect(output).toContain('dotted_type "has.dot"'); + expect(output).toContain('array_type "MixedCase"[][]'); + expect(output).toContain('ordinary_type simple'); expect(output).toMatchSnapshot(); }); diff --git a/packages/plpgsql-deparser/src/plpgsql-deparser.ts b/packages/plpgsql-deparser/src/plpgsql-deparser.ts index a19bcdd3..2bd28dff 100644 --- a/packages/plpgsql-deparser/src/plpgsql-deparser.ts +++ b/packages/plpgsql-deparser/src/plpgsql-deparser.ts @@ -884,7 +884,12 @@ export class PLpgSQLDeparser { if (!isSchemaQualified) { const trimmed = typname.trim(); - return /^"char"(\[\d*\])*$/.test(trimmed) ? trimmed : trimmed.replace(/"/g, ''); + const quoted = trimmed.match(/^"((?:[^"]|"")*)"((?:\[\d*\])*)$/); + if (quoted) { + const name = quoted[1].replace(/""/g, '"'); + return (name === 'char' ? '"char"' : QuoteUtils.quoteIdentifier(name)) + quoted[2]; + } + return trimmed; } // Schema-qualified type - apply proper quoting