diff --git a/__fixtures__/generated/generated.json b/__fixtures__/generated/generated.json index 53e910a4..fb30ff50 100644 --- a/__fixtures__/generated/generated.json +++ b/__fixtures__/generated/generated.json @@ -21822,6 +21822,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/json-table-1.sql": "SELECT * FROM JSON_TABLE(jsonb '[1, 2]', '$[*]' COLUMNS (a int PATH '$'))", "misc/json-table-2.sql": "SELECT * FROM JSON_TABLE(jsonb '[1, 2]', '$[*]' COLUMNS (id FOR ORDINALITY, a int)) jt", "misc/json-table-3.sql": "SELECT * FROM JSON_TABLE(jsonb '[1, 2]', '$[*]' AS root COLUMNS (a int PATH '$')) AS jt (x)", 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 b691659e..276a171e 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -2783,7 +2783,9 @@ export class Deparser implements DeparserVisitor { const output: string[] = []; if (node.colname) { - output.push(QuoteUtils.quoteIdentifier(node.colname)); + output.push(node.typeName + ? QuoteUtils.quoteIdentifierBeforeType(node.colname) + : QuoteUtils.quoteIdentifier(node.colname)); } if (node.typeName) { @@ -5049,7 +5051,9 @@ export class Deparser implements DeparserVisitor { const indentedParts: string[] = []; if (colDefData.colname) { - parts.push(QuoteUtils.quoteIdentifier(colDefData.colname)); + parts.push(colDefData.typeName + ? QuoteUtils.quoteIdentifierBeforeType(colDefData.colname) + : QuoteUtils.quoteIdentifier(colDefData.colname)); } if (colDefData.typeName) { @@ -5113,7 +5117,9 @@ export class Deparser implements DeparserVisitor { const parts: string[] = []; if (colDefData.colname) { - parts.push(QuoteUtils.quoteIdentifier(colDefData.colname)); + parts.push(colDefData.typeName + ? QuoteUtils.quoteIdentifierBeforeType(colDefData.colname) + : QuoteUtils.quoteIdentifier(colDefData.colname)); } if (colDefData.typeName) { @@ -5827,7 +5833,9 @@ export class Deparser implements DeparserVisitor { } if (node.name) { - output.push(QuoteUtils.quoteIdentifier(node.name)); + output.push(node.argType + ? QuoteUtils.quoteIdentifierBeforeType(node.name) + : QuoteUtils.quoteIdentifier(node.name)); } if (node.argType) { @@ -10915,7 +10923,9 @@ export class Deparser implements DeparserVisitor { const output: string[] = []; if (node.colname) { - output.push(QuoteUtils.quoteIdentifier(node.colname)); + 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++) {