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
62 changes: 62 additions & 0 deletions packages/deparser/__tests__/misc/access-method-quoting.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
29 changes: 14 additions & 15 deletions packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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('.');
}
Expand Down Expand Up @@ -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 '));
}
Expand Down Expand Up @@ -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));
}
Comment on lines 8535 to 8541

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Handle schema-qualified opclass rename/owner lists

RenameStmt (packages/deparser/src/deparser.ts:8536) and AlterOwnerStmt (line 8611) special-case operator class/family objects only when the object List has exactly 2 items; a 3-item schema-qualified list (accessMethod, schema, name) falls through to the generic List visit, which emits the parts without the required USING access_method suffix. ALTER OPERATOR FAMILY s.f USING btree RENAME TO g is valid PostgreSQL and deparses to SQL that does not reparse. The sibling DropStmt path (line 4748) handles the 3-item shape, so coverage is inconsistent across the same statement family.

📋 Prompt for AI Agents

In packages/deparser/src/deparser.ts, extend the OPFAMILY/OPCLASS handling in RenameStmt (lines 8534-8542) and AlterOwnerStmt (lines 8609-8617) to handle items.length === 3 by emitting QuoteUtils.quoteQualifiedIdentifier(items[1].String.sval, items[2].String.sval) USING QuoteUtils.quoteIdentifier(items[0].String.sval), matching the existing pattern at line 4748 in DropStmt. Add round-trip tests for the schema-qualified RENAME and OWNER TO forms in packages/deparser/tests/misc/access-method-quoting.test.ts.

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down
Loading