sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.37k stars 405 forks source link

[FORMATTING] Formatter inserts space in between `^@` operator, making the SQL invalid #734

Closed felixfbecker closed 7 months ago

felixfbecker commented 7 months ago

Input data

Which SQL and options did you provide as input?

SELECT 'foo' ^@ ANY ('{f,b}')

Expected Output

SELECT 'foo' ^@ ANY ('{f,b}')

Actual Output

SELECT 'foo' ^ @ ANY ('{f,b}')

which breaks the SQL with

ERROR:  syntax error at or near "ANY"

The ^@ operator is equivalent to starts_with(), but starts_with cannot be used as a workaround when using ANY.

Usage

nene commented 7 months ago

Thanks for reporting. Looks like ^@ is another one of PostgreSQL's billion operators I've managed to not include.

Additionaly, don't use prettier-sql. See the FAQ

nene commented 7 months ago

Fixed in v15.3.1

karlhorky commented 7 months ago

TIL about the ^@ operator, interesting 👀

Seems like documentation around it is hard to locate, but you can confirm existence of it in pg_operator:

=> SELECT oprname, oprleft::regtype, oprright::regtype, oprresult::regtype, oprcode::regproc FROM pg_operator WHERE oprname = '^@';
 oprname | oprleft | oprright | oprresult |   oprcode
---------+---------+----------+-----------+-------------
 ^@      | text    | text     | boolean   | starts_with
(1 row)

Simple example query:

=> SELECT 'example' ^@ 'ex';
 ?column?
----------
 t
(1 row)
felixfbecker commented 7 months ago

Awesome! The docs are here btw: https://www.postgresql.org/docs/15/functions-string.html#:~:text=text-,%5E%40,-text%20%E2%86%92%20boolean

felixfbecker commented 7 months ago

Additionaly, don't use prettier-sql. See the FAQ

I'm not using the VS Code extension, but the prettier-plugin-sql Prettier plugin. Is that discouraged too?

nene commented 7 months ago

That one should be fine :)

You should be even able to upgrade the sql-formatter lib and have this plugin pick up the latest version. Or you could send a dependency update PR to that plugin repo.