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

KeyError: 'JOIN' when parsing a query #424

Open
giacomochiarella opened this issue Sep 17, 2023 · 0 comments
Open

KeyError: 'JOIN' when parsing a query #424

giacomochiarella opened this issue Sep 17, 2023 · 0 comments

Comments

@giacomochiarella
Copy link

giacomochiarella commented Sep 17, 2023

Using the following code:

from sql_metadata import Parser

sql = """
select 
        dependent_schema.name as dependent_schema,
        relationships.dependent_name as dependent_name
from relationships
    join schema as dependent_schema on relationships.dependent_schema_id=dependent_schema.id
    join schema as referenced_schema on relationships.referenced_schema_id=referenced_schema.id
    group by dependent_schema, dependent_name
    order by dependent_schema, dependent_name;
"""

parser = Parser(sql)
print(parser.tables)
print(parser.columns)
print(parser.columns_dict)

produces the following error:

['relationships', 'schema']
Traceback (most recent call last):
  File "/Users/giacomochiarella/github/dwh_dbt/fake_dataset.py", line 83, in <module>
    print(parser.columns)
  File "/Users/giacomochiarella/github/dwh_dbt/venv/lib/python3.9/site-packages/sql_metadata/parser.py", line 202, in columns
    columns_aliases_names=self.columns_aliases_names,
  File "/Users/giacomochiarella/github/dwh_dbt/venv/lib/python3.9/site-packages/sql_metadata/parser.py", line 326, in columns_aliases_names
    self._handle_column_alias_subquery_level_update(token=token)
  File "/Users/giacomochiarella/github/dwh_dbt/venv/lib/python3.9/site-packages/sql_metadata/parser.py", line 674, in _handle_column_alias_subquery_level_update
    self._add_to_columns_aliases_subsection(token=token)
  File "/Users/giacomochiarella/github/dwh_dbt/venv/lib/python3.9/site-packages/sql_metadata/parser.py", line 740, in _add_to_columns_aliases_subsection
    section = COLUMNS_SECTIONS[keyword]
KeyError: 'JOIN'

if I comment the first selected column (dependent_schema.name as dependent_schema,) or I comment the first join (join schema as dependent_schema on relationships.dependent_schema_id=dependent_schema.id) I don't get any error.
I know the query does make much sense, but the sql code should be syntactically correct. Anyone can help me?

@giacomochiarella giacomochiarella changed the title KeyError: 'JOIN' KeyError: 'JOIN' when parsing a query Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant