Skip to content

Commit

Permalink
fix sqlparse bug
Browse files Browse the repository at this point in the history
  • Loading branch information
timifasubaa committed Aug 22, 2018
1 parent 54c9ceb commit eecb864
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sqlparse.tokens import Keyword, Name

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


Expand Down Expand Up @@ -128,7 +129,8 @@ def __extract_from_token(self, token):
continue

if item.ttype in Keyword:
if self.__is_result_operation(item.value):
if (self.__is_result_operation(item.value) or
item.value.upper() == ON_KEYWORD):
table_name_preceding_token = False
continue
# FROM clause is over
Expand Down
20 changes: 20 additions & 0 deletions tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,23 @@ def test_explain(self):
self.assertEquals(True, sql.is_explain())
self.assertEquals(False, sql.is_select())
self.assertEquals(True, sql.is_readonly())

def test_complex_extract_tables(self):
query = """SELECT sum(m_examples) AS "sum__m_example"
FROM
(SELECT COUNT(DISTINCT id_userid) AS m_examples,
some_more_info
FROM my_b_table b
JOIN my_t_table t ON b.ds=t.ds
JOIN my_l_table l ON b.uid=l.uid
WHERE b.rid IN
(SELECT other_col
FROM inner_table)
AND l.bla IN ('x', 'y')
GROUP BY 2
ORDER BY 2 ASC) AS "meh"
ORDER BY "sum__m_example" DESC
LIMIT 10;"""
self.assertEquals(
{'my_l_table', 'my_b_table', 'my_t_table', 'inner_table'},
self.extract_tables(query))

0 comments on commit eecb864

Please sign in to comment.