Skip to content

Reserved words used as aliases cause a crash #801

Open

Description

@pgiraud

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

Activity

  1. nene commented on Nov 14, 2024

    @nene
    Collaborator

    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.

  2. changed the title [-][FORMATTING] Reserved words can't be used as aliases[/-] [+]Reserved words used as aliases cause a crash[/+] on Nov 14, 2024
  3. nene commented on Sep 23, 2026

    @nene
    Collaborator

    This has now been improved in 15.9.0 release. Thing should be better now if you use AS keyword to declare aliases. Without the AS, there will still be problems though.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions