Skip to content

Commit 32a6520

Browse files
fix(postgresql): keep spaces around OPERATOR() with denseOperators
With denseOperators the PostgreSQL OPERATOR(schema.op) construct was densed into its operands, e.g. "foo OPERATOR(public.===) bar" became "fooOPERATOR(public.===)bar", which re-parses as invalid SQL. It's a keyword-like operator, so it now keeps its surrounding spaces like the dashed "-" case does.
1 parent a9528c9 commit 32a6520

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/formatter/ExpressionFormatter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ export default class ExpressionFormatter {
339339
// would otherwise re-parse as a single dashed identifier.
340340
if (text === '-' && this.dialectCfg.identifierDashes) {
341341
this.layout.add(text, WS.SPACE);
342+
} else if (/^OPERATOR\s*\(/iu.test(text)) {
343+
// PostgreSQL's "OPERATOR(schema.+)" is a keyword-like operator. Densing it
344+
// would glue it to its operands ("aOPERATOR(...)b") and re-parse as invalid
345+
// SQL, so keep its surrounding spaces even with denseOperators.
346+
this.layout.add(text, WS.SPACE);
342347
} else if (this.cfg.denseOperators || this.dialectCfg.alwaysDenseOperators.includes(text)) {
343348
this.layout.add(WS.NO_SPACE, text);
344349
} else if (text === ':') {

test/postgresql.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,19 @@ describe('PostgreSqlFormatter', () => {
221221
`);
222222
});
223223

224+
it('keeps spaces around OPERATOR() syntax with denseOperators', () => {
225+
// Densing "foo OPERATOR(public.===) bar" into "fooOPERATOR(public.===)bar"
226+
// glues the operands onto the keyword and re-parses as invalid SQL.
227+
expect(format(`SELECT foo OPERATOR(public.===) bar;`, { denseOperators: true })).toBe(dedent`
228+
SELECT
229+
foo OPERATOR(public.===) bar;
230+
`);
231+
expect(format(`SELECT a OPERATOR(+) b;`, { denseOperators: true })).toBe(dedent`
232+
SELECT
233+
a OPERATOR(+) b;
234+
`);
235+
});
236+
224237
// Issue #813
225238
it('supports OR REPLACE in CREATE FUNCTION', () => {
226239
expect(format(`CREATE OR REPLACE FUNCTION foo ();`)).toBe(dedent`

0 commit comments

Comments
 (0)