From 07301aa437e314776149e2636160d05fecef971e Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 23 Nov 2026 08:21:06 +0000 Subject: [PATCH 1/2] fix(deparser): quote keyword column names before types --- __fixtures__/generated/generated.json | 6 ++++++ .../kitchen-sink/misc/keyword-column-names.sql | 7 +++++++ .../misc-keyword-column-names.test.ts | 14 ++++++++++++++ packages/deparser/src/deparser.ts | 18 +++++++++++++----- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 __fixtures__/kitchen-sink/misc/keyword-column-names.sql create mode 100644 packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts diff --git a/__fixtures__/generated/generated.json b/__fixtures__/generated/generated.json index ba54a0d0..2ba3b053 100644 --- a/__fixtures__/generated/generated.json +++ b/__fixtures__/generated/generated.json @@ -21805,6 +21805,12 @@ "misc/launchql-ext-default-roles-1.sql": "DO $$\n BEGIN\n IF NOT EXISTS (\n SELECT\n 1\n FROM\n pg_roles\n WHERE\n rolname = 'anonymous') THEN\n CREATE ROLE anonymous;\n COMMENT ON ROLE anonymous IS 'Anonymous group';\n ALTER USER anonymous WITH NOCREATEDB;\n ALTER USER anonymous WITH NOCREATEROLE;\n ALTER USER anonymous WITH NOLOGIN;\n ALTER USER anonymous WITH NOBYPASSRLS;\nEND IF;\nEND $$", "misc/launchql-ext-default-roles-2.sql": "DO $$\n BEGIN\n IF NOT EXISTS (\n SELECT\n 1\n FROM\n pg_roles\n WHERE\n rolname = 'authenticated') THEN\n CREATE ROLE authenticated;\n COMMENT ON ROLE authenticated IS 'Authenticated group';\n ALTER USER authenticated WITH NOCREATEDB;\n ALTER USER authenticated WITH NOCREATEROLE;\n ALTER USER authenticated WITH NOLOGIN;\n ALTER USER authenticated WITH NOBYPASSRLS;\nEND IF;\nEND $$", "misc/launchql-ext-default-roles-3.sql": "DO $$\n BEGIN\n IF NOT EXISTS (\n SELECT\n 1\n FROM\n pg_roles\n WHERE\n rolname = 'administrator') THEN\n CREATE ROLE administrator;\n COMMENT ON ROLE administrator IS 'Administration group';\n ALTER USER administrator WITH NOCREATEDB;\n ALTER USER administrator WITH NOCREATEROLE;\n ALTER USER administrator WITH NOLOGIN;\n ALTER USER administrator WITH BYPASSRLS;\n GRANT anonymous TO administrator;\n GRANT authenticated TO administrator;\nEND IF;\nEND $$", + "misc/keyword-column-names-1.sql": "CREATE TABLE t (\"without\" time)", + "misc/keyword-column-names-2.sql": "CREATE TABLE t (\"nulls\" first, \"format\" json, \"with\" time)", + "misc/keyword-column-names-3.sql": "CREATE TABLE t (\"nulls\" last[])", + "misc/keyword-column-names-4.sql": "ALTER TABLE t ADD COLUMN \"without\" time(3) with time zone", + "misc/keyword-column-names-5.sql": "SELECT * FROM XMLTABLE('/r' PASSING doc COLUMNS \"without\" time PATH 'x')", + "misc/keyword-column-names-6.sql": "CREATE FUNCTION f() RETURNS TABLE (\"without\" time) LANGUAGE sql AS $$SELECT NULL::time$$", "misc/issues-1.sql": "select from test_table WHERE status = 'complete'::text", "misc/issues-2.sql": "select from test_table WHERE status = 'complete'", "misc/issues-3.sql": "CREATE TABLE new_style (\n id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,\n val1 TEXT NOT NULL,\n val2 TEXT NULL,\n CONSTRAINT uq_val1_val2_new UNIQUE NULLS NOT DISTINCT (val1, val2)\n)", diff --git a/__fixtures__/kitchen-sink/misc/keyword-column-names.sql b/__fixtures__/kitchen-sink/misc/keyword-column-names.sql new file mode 100644 index 00000000..b3fac0dc --- /dev/null +++ b/__fixtures__/kitchen-sink/misc/keyword-column-names.sql @@ -0,0 +1,7 @@ +-- Keyword column names that can conflict with lexer lookahead before a type +CREATE TABLE t ("without" time); +CREATE TABLE t ("nulls" first, "format" json, "with" time); +CREATE TABLE t ("nulls" last[]); +ALTER TABLE t ADD COLUMN "without" time(3) with time zone; +SELECT * FROM XMLTABLE('/r' PASSING doc COLUMNS "without" time PATH 'x'); +CREATE FUNCTION f() RETURNS TABLE ("without" time) LANGUAGE sql AS $$SELECT NULL::time$$; diff --git a/packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts b/packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts new file mode 100644 index 00000000..52709d9d --- /dev/null +++ b/packages/deparser/__tests__/kitchen-sink/misc-keyword-column-names.test.ts @@ -0,0 +1,14 @@ + +import { FixtureTestUtils } from '../../test-utils'; +const fixtures = new FixtureTestUtils(); + +it('misc-keyword-column-names', async () => { + await fixtures.runFixtureTests([ + "misc/keyword-column-names-1.sql", + "misc/keyword-column-names-2.sql", + "misc/keyword-column-names-3.sql", + "misc/keyword-column-names-4.sql", + "misc/keyword-column-names-5.sql", + "misc/keyword-column-names-6.sql" +]); +}); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 90ce5efd..11f46de4 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -2784,11 +2784,19 @@ export class Deparser implements DeparserVisitor { return output.join(' '); } + private quoteTypedIdentifier(name: string, typeName: t.TypeName | undefined): string { + // PostgreSQL's lexer treats these keywords differently before certain type names. + if (typeName && (name === 'without' || name === 'nulls' || name === 'format')) { + return `"${name}"`; + } + return QuoteUtils.quoteIdentifier(name); + } + ColumnDef(node: t.ColumnDef, context: DeparserContext): string { const output: string[] = []; if (node.colname) { - output.push(QuoteUtils.quoteIdentifier(node.colname)); + output.push(this.quoteTypedIdentifier(node.colname, node.typeName)); } if (node.typeName) { @@ -5054,7 +5062,7 @@ export class Deparser implements DeparserVisitor { const indentedParts: string[] = []; if (colDefData.colname) { - parts.push(QuoteUtils.quoteIdentifier(colDefData.colname)); + parts.push(this.quoteTypedIdentifier(colDefData.colname, colDefData.typeName)); } if (colDefData.typeName) { @@ -5118,7 +5126,7 @@ export class Deparser implements DeparserVisitor { const parts: string[] = []; if (colDefData.colname) { - parts.push(QuoteUtils.quoteIdentifier(colDefData.colname)); + parts.push(this.quoteTypedIdentifier(colDefData.colname, colDefData.typeName)); } if (colDefData.typeName) { @@ -5832,7 +5840,7 @@ export class Deparser implements DeparserVisitor { } if (node.name) { - output.push(QuoteUtils.quoteIdentifier(node.name)); + output.push(this.quoteTypedIdentifier(node.name, node.argType)); } if (node.argType) { @@ -10920,7 +10928,7 @@ export class Deparser implements DeparserVisitor { const output: string[] = []; if (node.colname) { - output.push(QuoteUtils.quoteIdentifier(node.colname)); + output.push(this.quoteTypedIdentifier(node.colname, node.typeName)); } if (node.for_ordinality) { From bba99ada7e920b7d8919de53f467552d166251a8 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 23 Nov 2026 08:54:40 +0000 Subject: [PATCH 2/2] fix(deparser): use QuoteUtils for typed identifiers --- packages/deparser/src/deparser.ts | 28 ++++++++++--------- packages/quotes/__tests__/quote-utils.test.ts | 21 ++++++++++++++ packages/quotes/src/quote-utils.ts | 12 ++++++++ 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 516bf51e..276a171e 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -2779,19 +2779,13 @@ export class Deparser implements DeparserVisitor { return output.join(' '); } - private quoteTypedIdentifier(name: string, typeName: t.TypeName | undefined): string { - // PostgreSQL's lexer treats these keywords differently before certain type names. - if (typeName && (name === 'without' || name === 'nulls' || name === 'format')) { - return `"${name}"`; - } - return QuoteUtils.quoteIdentifier(name); - } - ColumnDef(node: t.ColumnDef, context: DeparserContext): string { const output: string[] = []; if (node.colname) { - output.push(this.quoteTypedIdentifier(node.colname, node.typeName)); + output.push(node.typeName + ? QuoteUtils.quoteIdentifierBeforeType(node.colname) + : QuoteUtils.quoteIdentifier(node.colname)); } if (node.typeName) { @@ -5057,7 +5051,9 @@ export class Deparser implements DeparserVisitor { const indentedParts: string[] = []; if (colDefData.colname) { - parts.push(this.quoteTypedIdentifier(colDefData.colname, colDefData.typeName)); + parts.push(colDefData.typeName + ? QuoteUtils.quoteIdentifierBeforeType(colDefData.colname) + : QuoteUtils.quoteIdentifier(colDefData.colname)); } if (colDefData.typeName) { @@ -5121,7 +5117,9 @@ export class Deparser implements DeparserVisitor { const parts: string[] = []; if (colDefData.colname) { - parts.push(this.quoteTypedIdentifier(colDefData.colname, colDefData.typeName)); + parts.push(colDefData.typeName + ? QuoteUtils.quoteIdentifierBeforeType(colDefData.colname) + : QuoteUtils.quoteIdentifier(colDefData.colname)); } if (colDefData.typeName) { @@ -5835,7 +5833,9 @@ export class Deparser implements DeparserVisitor { } if (node.name) { - output.push(this.quoteTypedIdentifier(node.name, node.argType)); + output.push(node.argType + ? QuoteUtils.quoteIdentifierBeforeType(node.name) + : QuoteUtils.quoteIdentifier(node.name)); } if (node.argType) { @@ -10923,7 +10923,9 @@ export class Deparser implements DeparserVisitor { const output: string[] = []; if (node.colname) { - output.push(this.quoteTypedIdentifier(node.colname, node.typeName)); + output.push(node.typeName + ? QuoteUtils.quoteIdentifierBeforeType(node.colname) + : QuoteUtils.quoteIdentifier(node.colname)); } if (node.for_ordinality) { diff --git a/packages/quotes/__tests__/quote-utils.test.ts b/packages/quotes/__tests__/quote-utils.test.ts index 7acee7fd..df423326 100644 --- a/packages/quotes/__tests__/quote-utils.test.ts +++ b/packages/quotes/__tests__/quote-utils.test.ts @@ -118,6 +118,27 @@ describe('QuoteUtils.quoteIdentifier', () => { }); }); +describe('QuoteUtils.quoteIdentifierBeforeType', () => { + it.each([ + ['without', '"without"'], + ['nulls', '"nulls"'], + ['format', '"format"'], + ])('quotes the lookahead keyword %s only in the typed position', (ident, quoted) => { + expect(QuoteUtils.quoteIdentifierBeforeType(ident)).toBe(quoted); + expect(QuoteUtils.quoteIdentifier(ident)).toBe(ident); + }); + + it.each([ + ['my_col', 'my_col'], + ['schema', 'schema'], + ['select', '"select"'], + ['Without', '"Without"'], + ['a"b', '"a""b"'], + ])('applies regular identifier quoting to %s', (ident, quoted) => { + expect(QuoteUtils.quoteIdentifierBeforeType(ident)).toBe(quoted); + }); +}); + describe('QuoteUtils.quoteIdentifierAfterDot', () => { it('should not quote simple lowercase identifiers', () => { expect(QuoteUtils.quoteIdentifierAfterDot('my_col')).toBe('my_col'); diff --git a/packages/quotes/src/quote-utils.ts b/packages/quotes/src/quote-utils.ts index 4792d396..e6009a85 100644 --- a/packages/quotes/src/quote-utils.ts +++ b/packages/quotes/src/quote-utils.ts @@ -86,6 +86,18 @@ export class QuoteUtils { return ident; // no change needed } + return QuoteUtils.quoteIdentifierAlways(ident); + } + + static quoteIdentifierBeforeType(ident: string): string { + // These unreserved keywords can become lookahead tokens before a type name. + if (ident === 'without' || ident === 'nulls' || ident === 'format') { + return QuoteUtils.quoteIdentifierAlways(ident); + } + return QuoteUtils.quoteIdentifier(ident); + } + + private static quoteIdentifierAlways(ident: string): string { // Build quoted identifier with escaped embedded quotes let result = '"'; for (let i = 0; i < ident.length; i++) {