sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.37k stars 405 forks source link

[FORMATTING] Formatter Adds Unwanted Space Before Dash and Number in Project Names (e.g., project -12345) #791

Open motherway opened 1 month ago

motherway commented 1 month ago

Screenshot 2024-10-22 at 9 20 17 AM

Whenever a project name contains a dash followed by a number then it is being interpreted as a minus number value and being separated when formatted. This is troublesome for us as a lot of our project names contain this format and the formatter is breaking our queries as a result.

We can't always rely on escaping the project name as these queries are written by end users in the browser.

Input data

-- BigQuery
select * from project-12345.table.column

Expected Output

select
  *
from
  project-12345.table.column

Actual Output

select 
  *
from
  project -12345.table.column

Usage

nene commented 1 month ago

Thanks for reporting.

Unfortunately this is a problem that's pretty much impossible to fix with the current architecture of SQL Formatter. This is because foo-bar.baz.zap would have to be treated differently based on the context:

SQL Formatter doesn't properly parse the SQL, so it can't really tell when it should treat it in one or the other way. I guess one option would be to always parse such constructs as project-name, as this way the formatter would avoid introducing syntax errors.

Anyway... Because of problems like that I created another SQL formatting tool prettier-plugin-sql-cst which does have a pretty solid BigQuery support and it does not suffer from issues like this. I suggest you give it a try.

nene commented 1 month ago

PS. Just FYI, when reporting bugs to this or any other open-source project, please don't answer "latest" when asked which version number you're using. That "latest" will no more be the latest version when somebody looks at this bug report a year from now. Or I might have released a new version just minutes ago, and would not have any idea whether your definition of "latest" refers to that version or some earlier one.

motherway commented 1 month ago

PS. Just FYI, when reporting bugs to this or any other open-source project, please don't answer "latest" when asked which version number you're using. That "latest" will no more be the latest version when somebody looks at this bug report a year from now. Or I might have released a new version just minutes ago, and would not have any idea whether your definition of "latest" refers to that version or some earlier one.

That makes total sense. Apologies I wrote it in a hurry.