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
5 changes: 5 additions & 0 deletions src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ export default class ExpressionFormatter {
// would otherwise re-parse as a single dashed identifier.
if (text === '-' && this.dialectCfg.identifierDashes) {
this.layout.add(text, WS.SPACE);
} else if (/^OPERATOR\s*\(/iu.test(text)) {
// PostgreSQL's "OPERATOR(schema.+)" is a keyword-like operator. Densing it
// would glue it to its operands ("aOPERATOR(...)b") and re-parse as invalid
// SQL, so keep its surrounding spaces even with denseOperators.
this.layout.add(text, WS.SPACE);
} else if (this.cfg.denseOperators || this.dialectCfg.alwaysDenseOperators.includes(text)) {
this.layout.add(WS.NO_SPACE, text);
} else if (text === ':') {
Expand Down
13 changes: 13 additions & 0 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ describe('PostgreSqlFormatter', () => {
`);
});

it('keeps spaces around OPERATOR() syntax with denseOperators', () => {
// Densing "foo OPERATOR(public.===) bar" into "fooOPERATOR(public.===)bar"
// glues the operands onto the keyword and re-parses as invalid SQL.
expect(format(`SELECT foo OPERATOR(public.===) bar;`, { denseOperators: true })).toBe(dedent`
SELECT
foo OPERATOR(public.===) bar;
`);
expect(format(`SELECT a OPERATOR(+) b;`, { denseOperators: true })).toBe(dedent`
SELECT
a OPERATOR(+) b;
`);
});

// Issue #813
it('supports OR REPLACE in CREATE FUNCTION', () => {
expect(format(`CREATE OR REPLACE FUNCTION foo ();`)).toBe(dedent`
Expand Down
Loading