[sqlparse] Fixing table name extraction for ill-defined query (#7029)

(cherry picked from commit 07c340cf82)
This commit is contained in:
John Bodley
2019-03-15 09:19:30 -07:00
committed by Grace Guo
parent b64a452a6d
commit c43d0fd378
2 changed files with 8 additions and 3 deletions

View File

@@ -76,7 +76,7 @@ class ParsedQuery(object):
@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()
@@ -89,8 +89,8 @@ class ParsedQuery(object):
# 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

View File

@@ -47,6 +47,11 @@ class SupersetTestCase(unittest.TestCase):
{'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))