Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error with aliased functions #781

Open
andialbrecht opened this issue Jul 12, 2024 · 2 comments
Open

Parsing error with aliased functions #781

andialbrecht opened this issue Jul 12, 2024 · 2 comments
Labels

Comments

@andialbrecht
Copy link
Owner

andialbrecht commented Jul 12, 2024

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):

  • Python: 3.11
  • sqlparse: 0.5.0

Additional context
Add any other context about the problem here.

@andialbrecht andialbrecht modified the milestone: 0.5.1 Jul 12, 2024
@andialbrecht
Copy link
Owner Author

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
Copy link

Better to use AS YEAR then, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants