Skip to content

fix(postgresql): keep spaces around OPERATOR() with denseOperators#958

Open
sarathfrancis90 wants to merge 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-dense-operator-keyword
Open

fix(postgresql): keep spaces around OPERATOR() with denseOperators#958
sarathfrancis90 wants to merge 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-dense-operator-keyword

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

I was trying out denseOperators on some Postgres queries and noticed it mangles the OPERATOR(schema.op) syntax:

format('SELECT foo OPERATOR(public.===) bar;', { language: 'postgresql', denseOperators: true })
// =>
// SELECT
//   fooOPERATOR(public.===)bar;

The spaces around the operator get stripped, so foo runs into the OPERATOR keyword and the result no longer parses. Unlike +/=/etc., this one is keyword-like and can't be glued to its operands, so I made it keep its surrounding spaces even in dense mode — same idea as the existing - exception for dialects with dashed identifiers.

Added a test under the existing OPERATOR() case in postgresql.test.ts.

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.
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 105fb65e-d750-4518-97d8-8f9f3e1efafd

📥 Commits

Reviewing files that changed from the base of the PR and between a9528c9 and 32a6520.

📒 Files selected for processing (2)
  • src/formatter/ExpressionFormatter.ts
  • test/postgresql.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved SQL formatting for PostgreSQL operator syntax so OPERATOR(...) expressions keep the needed spaces around operands.
    • Prevented dense operator formatting from producing invalid SQL in cases using keyword-like operators.
    • Added regression coverage for PostgreSQL formatting to verify these expressions remain valid after formatting.

Walkthrough

In ExpressionFormatter.formatOperator, a regex check for OPERATOR\s*\( is added to force surrounding spaces for PostgreSQL's OPERATOR(schema.+)-style operators, bypassing denseOperators behavior. A regression test verifies both OPERATOR(public.===) and OPERATOR(+) retain spaces under denseOperators: true.

Changes

OPERATOR() dense-operator spacing fix

Layer / File(s) Summary
formatOperator fix and regression test
src/formatter/ExpressionFormatter.ts, test/postgresql.test.ts
Adds a regex branch detecting OPERATOR(...) syntax to output surrounding spaces regardless of denseOperators setting; adds two assertions confirming spacing is preserved for schema-qualified and bare OPERATOR() forms.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: preserving spaces around PostgreSQL OPERATOR() under denseOperators.
Description check ✅ Passed The description accurately explains the PostgreSQL OPERATOR() spacing fix and the added regression test.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant