From e893f3af4be3ef66dcdff159f623d28919249eaa Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Tue, 17 Nov 2026 22:49:38 +0000 Subject: [PATCH 1/2] fix(deparser): quote access method names IndexStmt, CreateStmt, IntoClause, exclusion constraints and the operator class/family forms wrote the access method after USING verbatim, so a hand-built AST could put any SQL there. It is an identifier: quote it. --- .../misc/access-method-quoting.test.ts | 58 +++++++++++++++++++ packages/deparser/src/deparser.ts | 26 ++++----- 2 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 packages/deparser/__tests__/misc/access-method-quoting.test.ts diff --git a/packages/deparser/__tests__/misc/access-method-quoting.test.ts b/packages/deparser/__tests__/misc/access-method-quoting.test.ts new file mode 100644 index 00000000..17f99833 --- /dev/null +++ b/packages/deparser/__tests__/misc/access-method-quoting.test.ts @@ -0,0 +1,58 @@ +import { Deparser } from '../../src'; +import { expectParseDeparse } from '../../test-utils'; + +// An access method is a name. Parsed SQL only ever yields a plain identifier +// there, so the hostile values live in hand-built ASTs, not a fixture. + +const smuggled = 'btree (a); DROP TABLE t; --'; +const quoted = `USING "${smuggled}"`; + +const relation = { relname: 't', inh: true, relpersistence: 'p' }; +const indexParams = [{ IndexElem: { name: 'a', ordering: 'SORTBY_DEFAULT', nulls_ordering: 'SORTBY_NULLS_DEFAULT' } }]; + +describe('access method names in hand-built ASTs', () => { + it.each<[string, any]>([ + ['IndexStmt', { IndexStmt: { idxname: 'i', relation, accessMethod: smuggled, indexParams } }], + ['CreateStmt', { CreateStmt: { relation, accessMethod: smuggled, oncommit: 'ONCOMMIT_NOOP' } }], + [ + 'exclusion Constraint', + { + AlterTableStmt: { + relation, + objtype: 'OBJECT_TABLE', + cmds: [{ + AlterTableCmd: { + subtype: 'AT_AddConstraint', + behavior: 'DROP_RESTRICT', + def: { + Constraint: { + contype: 'CONSTR_EXCLUSION', + conname: 'c', + access_method: smuggled, + exclusions: [{ List: { items: [indexParams[0], { List: { items: [{ String: { sval: '=' } }] } }] } }] + } + } + } + }] + } + } + ] + ])('quotes a %s access method that is not a plain identifier', (_label, node) => { + const sql = Deparser.deparse(node); + expect(sql).toContain(quoted); + expect(sql.replace(quoted, '')).not.toContain(';'); + }); + + it.each([ + 'CREATE INDEX i ON t USING gin (a)', + 'CREATE INDEX i ON t USING hnsw (a vector_cosine_ops)', + 'CREATE INDEX i ON t USING ivfflat (a vector_cosine_ops) WITH (lists = 100)', + 'CREATE INDEX i ON t USING bm25 (a) WITH (k1 = 1.2)', + 'CREATE INDEX i ON t USING "Odd Name" (a)', + 'ALTER TABLE t ADD CONSTRAINT c EXCLUDE USING gist (a WITH =)', + 'CREATE TABLE t (a int) USING heap', + 'ALTER OPERATOR FAMILY f USING btree RENAME TO g' + ])('round-trips %s', async (sql) => { + await expectParseDeparse(sql); + }); +}); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 8907fd87..3f605280 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -3172,7 +3172,7 @@ export class Deparser implements DeparserVisitor { output.push('EXCLUDE'); if (node.access_method) { output.push('USING'); - output.push(node.access_method); + output.push(QuoteUtils.quoteIdentifier(node.access_method)); } if (node.exclusions && node.exclusions.length > 0) { const exclusionElements = ListUtils.unwrapList(node.exclusions).map(elem => { @@ -3861,7 +3861,7 @@ export class Deparser implements DeparserVisitor { if (node.accessMethod && node.accessMethod !== 'btree') { output.push('USING'); - output.push(node.accessMethod); + output.push(QuoteUtils.quoteIdentifier(node.accessMethod)); } if (node.indexParams && node.indexParams.length > 0) { @@ -4744,12 +4744,12 @@ export class Deparser implements DeparserVisitor { if (items.length === 2) { const accessMethod = items[0]; const objectName = items[1]; - return `${QuoteUtils.quoteIdentifier(objectName)} USING ${accessMethod}`; + return `${QuoteUtils.quoteIdentifier(objectName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`; } else if (items.length === 3) { const accessMethod = items[0]; const schemaName = items[1]; const objectName = items[2]; - return `${QuoteUtils.quoteQualifiedIdentifier(schemaName, objectName)} USING ${accessMethod}`; + return `${QuoteUtils.quoteQualifiedIdentifier(schemaName, objectName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`; } return items.join('.'); } @@ -7137,7 +7137,7 @@ export class Deparser implements DeparserVisitor { if (objectParts.length >= 2) { const className = objectParts[objectParts.length - 1]; const accessMethod = objectParts[objectParts.length - 2]; - output.push(`${className} USING ${accessMethod}`); + output.push(`${className} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else { output.push(objectParts.join(' USING ')); } @@ -8536,7 +8536,7 @@ export class Deparser implements DeparserVisitor { if (items.length === 2) { const accessMethod = items[0].String?.sval || ''; const objectName = items[1].String?.sval || ''; - output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else { output.push(this.visit(node.object, context)); } @@ -8611,7 +8611,7 @@ export class Deparser implements DeparserVisitor { if (items.length === 2) { const accessMethod = items[0].String?.sval || ''; const objectName = items[1].String?.sval || ''; - output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteIdentifier(objectName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else { output.push(this.visit(node.object, context)); } @@ -9995,7 +9995,7 @@ export class Deparser implements DeparserVisitor { if (node.into && node.into.accessMethod) { output.push('USING'); - output.push(node.into.accessMethod); + output.push(QuoteUtils.quoteIdentifier(node.into.accessMethod)); } if (node.into && node.into.options && node.into.options.length > 0) { @@ -10630,7 +10630,7 @@ export class Deparser implements DeparserVisitor { } if (node.accessMethod) { - output.push('USING', node.accessMethod); + output.push('USING', QuoteUtils.quoteIdentifier(node.accessMethod)); } if (node.options && node.options.length > 0) { @@ -11814,12 +11814,12 @@ export class Deparser implements DeparserVisitor { if (items.length === 2) { const accessMethod = items[0].String?.sval || ''; const opClassName = items[1].String?.sval || ''; - output.push(`${QuoteUtils.quoteIdentifier(opClassName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteIdentifier(opClassName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else if (items.length === 3) { const accessMethod = items[0].String?.sval || ''; const schemaName = items[1].String?.sval || ''; const opClassName = items[2].String?.sval || ''; - output.push(`${QuoteUtils.quoteQualifiedIdentifier(schemaName, opClassName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteQualifiedIdentifier(schemaName, opClassName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else { output.push(this.visit(node.object as any, context)); } @@ -11829,12 +11829,12 @@ export class Deparser implements DeparserVisitor { if (items.length === 2) { const accessMethod = items[0].String?.sval || ''; const opFamilyName = items[1].String?.sval || ''; - output.push(`${QuoteUtils.quoteIdentifier(opFamilyName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteIdentifier(opFamilyName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else if (items.length === 3) { const accessMethod = items[0].String?.sval || ''; const schemaName = items[1].String?.sval || ''; const opFamilyName = items[2].String?.sval || ''; - output.push(`${QuoteUtils.quoteQualifiedIdentifier(schemaName, opFamilyName)} USING ${accessMethod}`); + output.push(`${QuoteUtils.quoteQualifiedIdentifier(schemaName, opFamilyName)} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); } else { output.push(this.visit(node.object as any, context)); } From 5de0085de2ceacffeb29cda4f9f30eeeb0e3d4e5 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Tue, 17 Nov 2026 22:52:39 +0000 Subject: [PATCH 2/2] fix(deparser): COMMENT ON OPERATOR CLASS/FAMILY keeps its access method The name parts are already quoted, so quoting the method again doubled the quotes; and a schema-qualified name took the schema for the method. --- .../deparser/__tests__/misc/access-method-quoting.test.ts | 6 +++++- packages/deparser/src/deparser.ts | 5 ++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/deparser/__tests__/misc/access-method-quoting.test.ts b/packages/deparser/__tests__/misc/access-method-quoting.test.ts index 17f99833..12388ea5 100644 --- a/packages/deparser/__tests__/misc/access-method-quoting.test.ts +++ b/packages/deparser/__tests__/misc/access-method-quoting.test.ts @@ -51,7 +51,11 @@ describe('access method names in hand-built ASTs', () => { 'CREATE INDEX i ON t USING "Odd Name" (a)', 'ALTER TABLE t ADD CONSTRAINT c EXCLUDE USING gist (a WITH =)', 'CREATE TABLE t (a int) USING heap', - 'ALTER OPERATOR FAMILY f USING btree RENAME TO g' + 'ALTER OPERATOR FAMILY f USING btree RENAME TO g', + 'ALTER OPERATOR FAMILY f USING "Odd Name" RENAME TO g', + 'DROP OPERATOR CLASS c USING "Odd Name"', + 'COMMENT ON OPERATOR CLASS c USING "Odd Name" IS \'x\'', + 'COMMENT ON OPERATOR FAMILY s.f USING "Odd Name" IS \'x\'' ])('round-trips %s', async (sql) => { await expectParseDeparse(sql); }); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index 3f605280..4aaa36f6 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -7135,9 +7135,8 @@ export class Deparser implements DeparserVisitor { } else if (node.objtype === 'OBJECT_OPCLASS' || node.objtype === 'OBJECT_OPFAMILY') { // Handle OPERATOR CLASS/FAMILY syntax: don't prefix with schema if (objectParts.length >= 2) { - const className = objectParts[objectParts.length - 1]; - const accessMethod = objectParts[objectParts.length - 2]; - output.push(`${className} USING ${QuoteUtils.quoteIdentifier(accessMethod)}`); + const [accessMethod, ...className] = objectParts; + output.push(`${className.join('.')} USING ${accessMethod}`); } else { output.push(objectParts.join(' USING ')); }