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

Aliases with "Union" #401

Open
usbo opened this issue May 29, 2023 · 1 comment
Open

Aliases with "Union" #401

usbo opened this issue May 29, 2023 · 1 comment
Labels

Comments

@usbo
Copy link

usbo commented May 29, 2023

SQL:

select a.A as M
from tab1 a
union all
select b.B as M
from tab2 b
parser = Parser(SQL)
print(parser.columns_dict, parser.columns_aliases)

Result:

{"M": "tab1.A"}

Expected:

{"M": ["tab1.A", "tab2.B"]}
@trevdoz
Copy link

trevdoz commented Jul 28, 2023

ditto...to solve this I had to hack together something that does this:

  • breaks up query into each query type ("WITH" queries, subqueries and the main select query)
  • for each query, if "UNION" in query split on union type ("UNION" or "UNION ALL")
  • parse those select statements individually and merge them into a single 'union_column_aliases_dict'
  • update the original columns_aliases attribute with this 'union_column_aliases_dict'

definitely not 'Pythonic' and wish it could be handled automatically by this library but SQLToken.is_potential_column_alias stops any 'updates' from happening if the parser sees the same alias twice in the query

@macbre macbre added the bug label Jul 28, 2023
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

3 participants