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..12388ea5 --- /dev/null +++ b/packages/deparser/__tests__/misc/access-method-quoting.test.ts @@ -0,0 +1,62 @@ +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', + '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 8907fd87..4aaa36f6 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('.'); } @@ -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 ${accessMethod}`); + const [accessMethod, ...className] = objectParts; + output.push(`${className.join('.')} USING ${accessMethod}`); } else { output.push(objectParts.join(' USING ')); } @@ -8536,7 +8535,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 +8610,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 +9994,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 +10629,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 +11813,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 +11828,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)); }