sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Format of postgresql json in select

Open tobstarr opened this issue 5 years ago • 1 comments

For some reason when accessing json/jsonb structures in a select statement these statements get indented incorrectly:

select id,
       created_at,
       updated_at,
       row->'name',
            row->'age',
                 row->'email'
from users;

The command I am using is sqlformat --reindent --keywords lower --identifiers lower -

Is there something one can do so that result looks like this:

select id,
       created_at,
       updated_at,
       row->'name',
       row->'age',
       row->'email'
from users;

?

tobstarr avatar Apr 01 '20 12:04 tobstarr

See #682

andialbrecht avatar Dec 30 '22 08:12 andialbrecht

Note, this is "almost" fixed. There's another issue with this concrete example: ROW is a reserved word in MySQL (see https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R). Right now it's not possible to choose a dialect in sqlparse, but we're working on it. So, right now your example still doesn't work as expected, but it would work on columns with other names, for example:

>>> sql = """select id,
...        created_at,
...        updated_at,
...        foo -> 'name',
...             foo->'age',
...                  foo -> 'email'
... from users;"""
>>>
>>> print(sqlparse.format(sql, reindent=True))
select id,
       created_at,
       updated_at,
       foo -> 'name',
       foo->'age',
       foo -> 'email'
from users;
>>>

andialbrecht avatar Mar 16 '24 09:03 andialbrecht