sql-formatter icon indicating copy to clipboard operation
sql-formatter copied to clipboard

[FORMATTING] Confusion between LIMIT keyword and column name

Open skubot opened this issue 3 years ago • 4 comments

We have a table with a 'limit' column, when inserting into the table sql-formatter indents characters after the column name.

Input data Which SQL and options did you provide as input? options = { language: 'postgresql' };

Expected Output

INSERT INTO
  search (
    apples,
    limit,
    pears,
    oranges
  )
values
  (
    '__apples',
    '__limit',
    '__pears',
    '__oranges'
  );

Actual Output

INSERT INTO
  search (
    apples,
    limit
      , pears,
    oranges
  )
values
  (
    '__apples',
    '__limit',
    '__pears',
    '__oranges'
  );

Usage

  • How are you calling / using the library? calling inside a Jest snapshot-serializer
  • What SQL language(s) does this apply to? postgresql
  • Which SQL Formatter version are you using? I'm using v8.0.2

skubot avatar Jul 11 '22 03:07 skubot

Thanks for reporting. Related to #156

This is currently a general issue with how the formatter distinguishes language keywords from identifiers. For the time being there's no quick fix for this. We're working towards a more general solution, though no idea when we'll finally get there.

nene avatar Jul 11 '22 06:07 nene

A workaround for the time being is to quote the column name, e.g. "limit".

nene avatar Jul 11 '22 06:07 nene

@nene Yea in our case we insert using Knex.insert() so it builds the query based on the inputs... so wrapping limit in quotes would be messy.

By the way the latest v8.1.0 changed the result slightly...

INSERT INTO
  search (
    apples,
    limit
      , pears,
    oranges,
    apricots
  )
-- etc...

to...

INSERT INTO
  search (
    apples,
    limit
, pears, --shifted to the left
      oranges, --one indent extra to right
      apricots
  )
-- etc...

skubot avatar Jul 13 '22 06:07 skubot

This issue has gone worse in the latest releases. The above SQL will now produce a hard error:

Parse error at token: , Unexpected COMMA token

The problem being that we're now expecting see LIMIT expr ["," expr].

nene avatar Sep 01 '22 14:09 nene