[sql lab] improve table name detection in free form SQL (#6793)

* [sql lab] improve table name detection in free form SQL

* flake

* Addressing comments
This commit is contained in:
Maxime Beauchemin
2019-02-04 16:03:23 -08:00
committed by GitHub
parent fc4042a28b
commit 5a40f71710
2 changed files with 70 additions and 39 deletions

View File

@@ -167,7 +167,6 @@ class SupersetTestCase(unittest.TestCase):
# DESCRIBE | DESC qualifiedName
def test_describe(self):
self.assertEquals({'t1'}, self.extract_tables('DESCRIBE t1'))
self.assertEquals({'t1'}, self.extract_tables('DESC t1'))
# SHOW PARTITIONS FROM qualifiedName (WHERE booleanExpression)?
# (ORDER BY sortItem (',' sortItem)*)? (LIMIT limit=(INTEGER_VALUE | ALL))?
@@ -349,6 +348,32 @@ class SupersetTestCase(unittest.TestCase):
{'table_a', 'table_b', 'table_c'},
self.extract_tables(query))
def test_mixed_from_clause(self):
query = """SELECT *
FROM table_a AS a, (select * from table_b) AS b, table_c as c
WHERE a.id = b.id and b.id = c.id"""
self.assertEquals(
{'table_a', 'table_b', 'table_c'},
self.extract_tables(query))
def test_nested_selects(self):
query = """
select (extractvalue(1,concat(0x7e,(select GROUP_CONCAT(TABLE_NAME)
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA like "%bi%"),0x7e)));
"""
self.assertEquals(
{'INFORMATION_SCHEMA.COLUMNS'},
self.extract_tables(query))
query = """
select (extractvalue(1,concat(0x7e,(select GROUP_CONCAT(COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME="bi_achivement_daily"),0x7e)));
"""
self.assertEquals(
{'INFORMATION_SCHEMA.COLUMNS'},
self.extract_tables(query))
def test_complex_extract_tables3(self):
query = """SELECT somecol AS somecol
FROM
@@ -386,6 +411,21 @@ class SupersetTestCase(unittest.TestCase):
{'a', 'b', 'c', 'd', 'e', 'f'},
self.extract_tables(query))
def test_complex_cte_with_prefix(self):
query = """
WITH CTE__test (SalesPersonID, SalesOrderID, SalesYear)
AS (
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
FROM SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM CTE__test
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
"""
self.assertEquals({'SalesOrderHeader'}, self.extract_tables(query))
def test_basic_breakdown_statements(self):
multi_sql = """
SELECT * FROM ab_user;