fix: keep spaces around - operator in dialects with dashed identifiers by sarathfrancis90 · Pull Request #953 · sql-formatter-org/sql-formatter · GitHub
Skip to content

fix: keep spaces around - operator in dialects with dashed identifiers#953

Merged
nene merged 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-dense-minus-bigquery-identifier
Jun 21, 2026
Merged

fix: keep spaces around - operator in dialects with dashed identifiers#953
nene merged 1 commit into
sql-formatter-org:masterfrom
sarathfrancis90:fix-dense-minus-bigquery-identifier

Conversation

@sarathfrancis90

@sarathfrancis90 sarathfrancis90 commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

With denseOperators: true on BigQuery, a subtraction like a - b comes out as a-b. That's not just ugly: BigQuery allows dashes inside identifiers, so the densed output re-parses as a single identifier a-b, and format(format(sql)) no longer matches format(sql). a - foo(y) is worse — it becomes a-foo (y), turning the expression into an identifier followed by a call.

The fix is to leave the - operator spaced in dialects that allow dashed identifiers (currently just BigQuery). I derived that flag from the existing identChars.dashes tokenizer option so there's a single source of truth. Numeric cases like 1 - 2 were already safe (the - is parsed into the literal), so this only affects the identifier case.

The shared dense-operator test in operators.ts was actually asserting the broken foo-bar output for BigQuery; I scoped that to the dashed-identifier dialects and added a BigQuery regression test.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed formatting of the - operator in dialects that support dashes in identifiers, such as BigQuery. When dense operator formatting is enabled, spaces around the - operator are now properly preserved to prevent expressions from being incorrectly merged into dashed identifiers.

In BigQuery (and any dialect allowing dashes inside identifiers) the
denseOperators option turned "a - b" into "a-b", which then re-parses
as a single dashed identifier - corrupting the query. Skip densing the
"-" operator for those dialects so the output round-trips.
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

@nene

nene commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Hi, sorry for the delay.

This is probably the best pull request I've received over the years. Very well thought through fix for this quirky bug. Thanks a lot.

@nene nene merged commit 311c478 into sql-formatter-org:master Jun 21, 2026
2 checks passed
@nene

nene commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Released in 15.8.2

@karlhorky

Copy link
Copy Markdown
Member

@coderabbitai why did you write this?

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.

and can you suppress it for all future reviews?

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

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.

3 participants