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 bug and refactor #5768

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
4 changes: 2 additions & 2 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlparse.sql import Identifier, IdentifierList
from sqlparse.tokens import Keyword, Name

RESULT_OPERATIONS = {'UNION', 'INTERSECT', 'EXCEPT'}
RESULT_OPERATIONS = {'UNION', 'INTERSECT', 'EXCEPT', 'SELECT'}
ON_KEYWORD = 'ON'
PRECEDES_TABLE_NAME = {'FROM', 'JOIN', 'DESC', 'DESCRIBE', 'WITH'}

Expand Down Expand Up @@ -128,7 +128,7 @@ def __extract_from_token(self, token):
if not table_name_preceding_token:
continue

if item.ttype in Keyword:
if item.ttype in Keyword or item.value == ',':
if (self.__is_result_operation(item.value) or
item.value.upper() == ON_KEYWORD):
table_name_preceding_token = False
Expand Down
37 changes: 37 additions & 0 deletions tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,40 @@ def test_complex_extract_tables2(self):
self.assertEquals(
{'table_a', 'table_b', 'table_c'},
self.extract_tables(query))

def test_complex_extract_tables3(self):
query = """SELECT somecol AS somecol
FROM
(WITH bla AS
(SELECT col_a
FROM a
WHERE 1=1
AND column_of_choice NOT IN
( SELECT interesting_col
FROM b ) ),
rb AS
( SELECT yet_another_column
FROM
( SELECT a
FROM c
GROUP BY the_other_col ) not_table
LEFT JOIN bla foo ON foo.prop = not_table.bad_col0
WHERE 1=1
GROUP BY not_table.bad_col1 ,
not_table.bad_col2 ,
ORDER BY not_table.bad_col_3 DESC , not_table.bad_col4 ,
not_table.bad_col5) SELECT random_col
FROM d
WHERE 1=1
UNION ALL SELECT even_more_cols
FROM e
WHERE 1=1
UNION ALL SELECT lets_go_deeper
FROM f
WHERE 1=1
WHERE 2=2
GROUP BY last_col
LIMIT 50000;"""
self.assertEquals(
{'a', 'b', 'c', 'd', 'e', 'f'},
self.extract_tables(query))