sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Weird parsing for `NULLS LAST`

Open ses4j opened this issue 6 years ago • 1 comments

I'm not sure what the proper parsing should be exactly, but I don't think this is it. The way it parsed the various NULL, NULLs, FIRST, LAST that are identifier modifiers is inconsistent and hard to use.

Ideally for my use case, the whole ORDER BY clause would be a single IdentifierList and none of those would be Keyword, but I don't know if that's right or not.

Related to #487.

sql1 = "SELECT *, NULL as null_col FROM mytable ORDER BY [name] ASC NULLS LAST, [number] DESC NULLS FIRST"

for token in sqlparse.parse(sql1)[0]:
    if token.ttype == T.Whitespace:
        continue
    print(f"{str(token.ttype or token.__class__):40s} {token.value}")

gives this output:

<class 'sqlparse.sql.IdentifierList'>    *, NULL as null_col
Token.Keyword                            FROM
<class 'sqlparse.sql.Identifier'>        mytable
Token.Keyword                            ORDER BY
<class 'sqlparse.sql.Identifier'>        [name] ASC NULLS
<class 'sqlparse.sql.IdentifierList'>    LAST, [number] DESC NULLS
Token.Keyword                            FIRST

ses4j avatar Jan 29 '20 21:01 ses4j

Related to this issue, formatting indentation is also broken:

import sqlparse

query = "SELECT *, NULL as null_col FROM mytable ORDER BY [name] ASC NULLS LAST, [number] DESC NULLS FIRST"
print(sqlparse.format(query, reindent=True))

Current output (v0.5.0):

SELECT *,
       NULL as null_col
FROM mytable
ORDER BY [name] ASC NULLS LAST,
                    [number] DESC NULLS FIRST

Expected output:

SELECT *,
       NULL as null_col
FROM mytable
ORDER BY [name] ASC NULLS LAST,
         [number] DESC NULLS FIRST

adamantike avatar Jun 21 '24 15:06 adamantike

@andialbrecht thanks for implementing the fix! Looking forward for the next release

adamantike avatar Jul 15 '24 18:07 adamantike