Skip to content

Commit

Permalink
[sqlparse] Fixing table name extraction for ill-defined query (#7029)
Browse files Browse the repository at this point in the history
(cherry picked from commit 07c340c)
  • Loading branch information
john-bodley authored and xtinec committed Mar 21, 2019
1 parent ee85089 commit 7bc3006
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_statements(self):

@staticmethod
def __get_full_name(identifier):
if len(identifier.tokens) > 1 and identifier.tokens[1].value == '.':
if len(identifier.tokens) > 2 and identifier.tokens[1].value == '.':
return '{}.{}'.format(identifier.tokens[0].value,
identifier.tokens[2].value)
return identifier.get_real_name()
Expand All @@ -89,8 +89,8 @@ def __process_identifier(self, identifier):
# exclude subselects
if '(' not in str(identifier):
table_name = self.__get_full_name(identifier)
if not table_name.startswith(CTE_PREFIX):
self._table_names.add(self.__get_full_name(identifier))
if table_name and not table_name.startswith(CTE_PREFIX):
self._table_names.add(table_name)
return

# store aliases
Expand Down
5 changes: 5 additions & 0 deletions tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def test_simple_select(self):
{'schemaname.tbname'},
self.extract_tables('SELECT * FROM schemaname.tbname'))

# Ill-defined schema/table.
self.assertEquals(
set(),
self.extract_tables('SELECT * FROM schemaname.'))

# quotes
query = 'SELECT field1, field2 FROM tb_name'
self.assertEquals({'tb_name'}, self.extract_tables(query))
Expand Down

0 comments on commit 7bc3006

Please sign in to comment.