andialbrecht / sqlparse

A non-validating SQL parser module for Python
BSD 3-Clause "New" or "Revised" License
3.68k stars 687 forks source link

Parsing error with aliased functions #781

Open andialbrecht opened 1 month ago

andialbrecht commented 1 month ago

Describe the bug A SQL containing aliased function calls is parsed (and formatted) wrong when the AS keyword is mussing.

To Reproduce

good_sql_no_alias = "select extract(year from foo), extract(month from foo), value from orders"
bad_sql_alias = "select extract(year from foo) year, extract(month from foo) month, value from orders"
good_sql_alias = "select extract(year from foo) as year, extract(month from foo) as month, value from orders"

import sqlparse

>>> print(sqlparse.format(good_sql_no_alias, reindent=True))
select extract(year
               from foo),
       extract(month
               from foo),
       value
from orders

>>> print(sqlparse.format(bad_sql_alias, reindent=True))
select extract(year
               from foo) year,
                         extract(month
                                 from foo) month,
                                           value
from orders

>>> print(sqlparse.format(good_sql_alias, reindent=True))
select extract(year
               from foo) as year,
       extract(month
               from foo) as month,
       value
from orders

The parser identifies year and month as keywords.

Expected behavior The formatting (and parsing) of bad_sql_alias should be the same way as for the good ones.

Versions (please complete the following information):

Additional context Add any other context about the problem here.

andialbrecht commented 1 month ago

Removing milestone again, this is once again the ambiguity of year, month, etc. being a keyword and being used as an identifier. That's why the as year variant works, this removes the ambiguity. I'm not sure how to handle this in the parser right now, as the parser has little to none context.

@mhmtsoydam, I'm sorry, but I don't think that this gets fixed any time soon...

mhmtsoydam commented 1 month ago

Better to use AS YEAR then, thanks!