superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grace...@apache.org
Subject [incubator-superset] branch master updated: fix sqlparse bug and refactor (#5768)
Date Wed, 29 Aug 2018 15:30:57 GMT
This is an automated email from the ASF dual-hosted git repository.

graceguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new d5d5927  fix sqlparse bug and refactor (#5768)
d5d5927 is described below

commit d5d592788c5b88fa4a2ab0ebf19340f5e866b43d
Author: timifasubaa <30888507+timifasubaa@users.noreply.github.com>
AuthorDate: Wed Aug 29 08:30:49 2018 -0700

    fix sqlparse bug and refactor (#5768)
---
 superset/sql_parse.py    |  4 ++--
 tests/sql_parse_tests.py | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/superset/sql_parse.py b/superset/sql_parse.py
index ccef350..240a30c 100644
--- a/superset/sql_parse.py
+++ b/superset/sql_parse.py
@@ -11,7 +11,7 @@ import sqlparse
 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'}
 
@@ -128,7 +128,7 @@ class SupersetQuery(object):
             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
diff --git a/tests/sql_parse_tests.py b/tests/sql_parse_tests.py
index 5306760..3f66fed 100644
--- a/tests/sql_parse_tests.py
+++ b/tests/sql_parse_tests.py
@@ -338,3 +338,40 @@ class SupersetTestCase(unittest.TestCase):
         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))


Mime
View raw message