sql-formatter-org/sql-formatter

Parsing of Postgres array slice operator throw error

wirekang opened this issue · 4 comments

Input data

select (array_agg("test"))[:5] as "test" from "user"

Actual Output

Parse error: Unexpected ":5] as "te" at line 1 column 28```

Usage

  • How are you calling / using the library?
    wirekang/kysely-playground#11

  • What SQL language(s) does this apply to?
    Postgres

  • Which SQL Formatter version are you using?
    12.2.0, 12.2.3

nene commented

Thanks for reporting. That's definitely a bug in the parser.

I'm wondering how this issue can be solved. It seems that single parser handles all dialect.

nene commented

You're correct @wirekang. The formatter has a very rudimentary parser which is used for all dialects. There are some options passed to the lexer to define which dialect supports which keywords and operators.

So a simple way to fix this error is to add : to the list of Postgres operators. Additionally should add it to formatOptions.alwaysDenseOperators, so it won't be formatted with spaces around it.

Unfortunately there's also a bug with parenthesis formatting, so we'd end up with the following:

select
  (array_agg("test")) [:5] as "test"
from
  "user"

But definitely better than crashing :)

nene commented

Fixed now in 13.0.1