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

Fix SQLParse when query has reserved word #7305

Merged
merged 2 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def __is_identifier(token):
return isinstance(token, (IdentifierList, Identifier))

def __process_identifier(self, identifier):
if not self.__is_identifier(identifier):
identifier = Identifier([identifier])

# exclude subselects
if '(' not in str(identifier):
table_name = self.__get_full_name(identifier)
Expand Down
25 changes: 25 additions & 0 deletions tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,28 @@ def test_messy_breakdown_statements(self):
'SELECT * FROM ab_user LIMIT 1',
]
self.assertEquals(statements, expected)

def test_extract_from_statement(self):
query = 'SELECT a, b FROM c WHERE d'
result = sql_parse.ParsedQuery(query)._table_names
expected = {'c'}
self.assertEquals(result, expected)

def test_extract_from_statement_with_reserved(self):
query = """
WITH
columns AS (SELECT metric FROM a),
rows AS (SELECT metric FROM b)
SELECT
c.metric AS m1,
r.metric AS m2
FROM columns c
JOIN rows r
"""
result = sql_parse.ParsedQuery(query)._table_names
expected = {'a', 'b'}
self.assertEquals(result, expected)


if __name__ == '__main__':
unittest.main()