Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions __fixtures__/generated/generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
7 changes: 7 additions & 0 deletions __fixtures__/kitchen-sink/misc/keyword-column-names.sql
Original file line number Diff line number Diff line change
@@ -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$$;
Original file line number Diff line number Diff line change
@@ -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"
]);
});
20 changes: 15 additions & 5 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions packages/quotes/__tests__/quote-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions packages/quotes/src/quote-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Loading