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

Bug: table alias missing #184

Closed
ReinierKoops opened this issue Jul 9, 2021 · 4 comments
Closed

Bug: table alias missing #184

ReinierKoops opened this issue Jul 9, 2021 · 4 comments
Labels

Comments

@ReinierKoops
Copy link

ReinierKoops commented Jul 9, 2021

Hi,

I think I have found a bug regarding table aliases. I have the following query:

query = "select distinct e1.eid as eid from uw_emp e1, uw_emp e2, uw_payroll p where e1.eid = e2.eid and e1.eid = p.eid"
print(query.columns, "\n", query.columns_aliases_names, "\n", query.tables, "\n", query.tables_aliases)

which outputs:
['e1.eid', 'e2.eid', 'uw_payroll.eid']
['eid']
['uw_emp', 'uw_payroll']
{'p': 'uw_payroll'}

I am missing e1 and e2 for aliases.

@ReinierKoops ReinierKoops changed the title Does not properly predict table aliases Bug: table alias missing Jul 9, 2021
@collerek collerek added the bug label Jul 9, 2021
@collerek
Copy link
Collaborator

There sqlparse treat e1 and e2 as floats, I expect it has to do something with exponentials in sqlparse?

If you change aliases to something else it should work, i.e. em instead of e:

query = """
    select distinct em1.eid as eid from uw_emp em1, uw_emp em2, uw_payroll p 
    where em1.eid = em2.eid and em1.eid = p.eid
    """

If you want to keep the old aliases you have to quote them, i.e.:

query = """
    select distinct e1.eid as eid from uw_emp "e1", uw_emp "e2", uw_payroll p 
    where e1.eid = e2.eid and e1.eid = p.eid
    """
parser = Parser(query)
assert parser.tables == ["uw_emp", "uw_payroll"]
assert parser.tables_aliases == {
    'p': 'uw_payroll',
    'e1': 'uw_emp',
    'e2': 'uw_emp'
}

@collerek
Copy link
Collaborator

It's even reported andialbrecht/sqlparse#399 since... 2018 😅

@collerek
Copy link
Collaborator

It was fixed in sqlparse but was not yet released (0.4.2), so you need to install it from github before the next release.

@ReinierKoops
Copy link
Author

ReinierKoops commented Jul 14, 2021

Haha, thank you, this is great! I see its merged to their master branch. I will install that version.

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