Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table name or alias "call" misinterpreted by formatter #732

Open
ipus1 opened this issue Apr 17, 2024 · 1 comment
Open

Table name or alias "call" misinterpreted by formatter #732

ipus1 opened this issue Apr 17, 2024 · 1 comment
Labels

Comments

@ipus1
Copy link

ipus1 commented Apr 17, 2024

Describe the bug
When an alias for a table or expression is called "call", the formatter throws an error when trying to access its fields.

Expected behavior
Running

sql = `SELECT call.id as "id" FROM c as call`;
console.log(format(sql, { language: "postgresql" }));

should work, but it doesn't.
This, however, does:

sql = `SELECT calls.id as "id" FROM c as calls`;
console.log(format(sql, { language: "postgresql" }));

Actual behavior
I got this error:

Error: Parse error at token: . at line 1 column 12
Unexpected PROPERTY_ACCESS_OPERATOR token: {"type":"PROPERTY_ACCESS_OPERATOR","raw":".","text":".","start":11}.

I assume this might happen for other keywords as well, not only "CALL".

Usage

  • How are you calling / using the library? As described above
  • What SQL language(s) does this apply to? PostgreSQL
  • Which SQL Formatter version are you using? 13.1, but I also tested it on 15.3
@ipus1 ipus1 added the bug label Apr 17, 2024
@nene
Copy link
Collaborator

nene commented Apr 17, 2024

Thanks for reporting.

This happens because the formatter expects CALL to be a start of a CALL statement. Similar problem happens with other keywords that mark the start of some statement like SELECT, UPDATE, etc.

Even when that particular crash were to be fixed (we could detect that CALL is followed by .), this will still leave the problem that call inside an alias definition would get formatted on a separate line (as again, the formatter assumes it'll start a CALL-statement):

SELECT
  call.id as "id"
FROM
  c as
call

You might be interested in trying out prettier-plugin-sql-cst, which has a much better support for PostgreSQL (although that support is far from 100%) and doesn't have fundamental problems like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants