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

Reserved words used as aliases cause a crash #801

Open
pgiraud opened this issue Nov 14, 2024 · 1 comment
Open

Reserved words used as aliases cause a crash #801

pgiraud opened this issue Nov 14, 2024 · 1 comment

Comments

@pgiraud
Copy link

pgiraud commented Nov 14, 2024

Input data

SELECT set.foo FROM settings AS set

Here's a more complex example (the error is a bit different):

SELECT id AS srvid,
  CASE WHEN id = $1 THEN $2
  ELSE
     COALESCE(alias, hostname || $3 || port)
  END AS alias,
  CASE WHEN id = $4 THEN $5 ELSE hostname END as hostname,
  CASE WHEN id = $6 THEN $7 ELSE port END AS port,
  CASE WHEN id = $8 THEN set.setting
      ELSE s.version::text
  END AS version
FROM public.powa_servers s
LEFT JOIN pg_settings set ON set.name = $9 AND s.id = $10

Usage

  • How are you calling / using the library?
import { formatDialect, postgresql } from "sql-formatter";
formatDialect(value, { dialect: postgresql })

Tested in the online demo as well.

  • What SQL language(s) does this apply to?
    PostgreSQL, but the first example fails with any dialect with the same error

  • Which SQL Formatter version are you using?
    15.4.6

@nene
Copy link
Collaborator

nene commented Nov 14, 2024

Thanks for reporting.

Although this error occurs with other dialects, it's more of a PostgreSQL-specific issue as most SQL dialects don't allow reserved keywords to be used as aliases. PostgreSQL also only allows some, like the following won't work:

SELECT select.foo FROM settings AS select;

However this syntax is valid in DB2, where one can even write:

SELECT select FROM from;

Isn't SQL a wonderful language :D

Usually I would recommend checking out prettier-plugin-sql-cst, which does a much better job at formatting PostgreSQL than this library, but turns out that the same issue is also present in there. Though my hopes of fixing this issue in there are much higher than fixing it in here.

@nene nene changed the title [FORMATTING] Reserved words can't be used as aliases Reserved words used as aliases cause a crash Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants