This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 34991f5  fix(explore): strip semicolons in virtual table SQL (#13801)
34991f5 is described below

commit 34991f5fabc5ce6ec5715382d52c21c7da216aaa
Author: Phillip Kelley-Dotson <[email protected]>
AuthorDate: Tue Apr 6 13:40:34 2021 -0700

    fix(explore): strip semicolons in virtual table SQL (#13801)
    
    * add method to strip semicolon
    
    * address comments
    
    * test the test
    
    * Update tests/sqla_models_tests.py
    
    Co-authored-by: Jesse Yang <[email protected]>
    
    * Update tests/sqla_models_tests.py
    
    Co-authored-by: Ville Brofeldt <[email protected]>
    
    * fix test
    
    * add suggestion
    
    * fix trailing space
    
    * remove logger
    
    * fix unit test
    
    Co-authored-by: Jesse Yang <[email protected]>
    Co-authored-by: Ville Brofeldt <[email protected]>
---
 superset/connectors/sqla/models.py |  4 ++--
 tests/sqla_models_tests.py         | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/superset/connectors/sqla/models.py 
b/superset/connectors/sqla/models.py
index a433f03..cb506f6 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -773,7 +773,6 @@ class SqlaTable(  # pylint: 
disable=too-many-public-methods,too-many-instance-at
     def get_query_str_extended(self, query_obj: QueryObjectDict) -> 
QueryStringExtended:
         sqlaq = self.get_sqla_query(**query_obj)
         sql = self.database.compile_sqla_query(sqlaq.sqla_query)
-        logger.info(sql)
         sql = sqlparse.format(sql, reindent=True)
         sql = self.mutate_query_from_config(sql)
         return QueryStringExtended(
@@ -818,6 +817,7 @@ class SqlaTable(  # pylint: 
disable=too-many-public-methods,too-many-instance-at
         """
         Render sql with template engine (Jinja).
         """
+
         sql = self.sql
         if template_processor:
             try:
@@ -829,7 +829,7 @@ class SqlaTable(  # pylint: 
disable=too-many-public-methods,too-many-instance-at
                         msg=ex.message,
                     )
                 )
-        sql = sqlparse.format(sql, strip_comments=True)
+        sql = sqlparse.format(sql.strip("\t\r\n; "), strip_comments=True)
         if not sql:
             raise QueryObjectValidationError(_("Virtual dataset query cannot 
be empty"))
         if len(sqlparse.split(sql)) > 1:
diff --git a/tests/sqla_models_tests.py b/tests/sqla_models_tests.py
index cdd77c2..b4cd489 100644
--- a/tests/sqla_models_tests.py
+++ b/tests/sqla_models_tests.py
@@ -223,6 +223,28 @@ class TestDatabaseModel(SupersetTestCase):
             with pytest.raises(QueryObjectValidationError):
                 table.get_sqla_query(**query_obj)
 
+    def test_query_format_strip_trailing_semicolon(self):
+        query_obj = {
+            "granularity": None,
+            "from_dttm": None,
+            "to_dttm": None,
+            "groupby": ["user"],
+            "metrics": [],
+            "is_timeseries": False,
+            "filter": [],
+            "extras": {},
+        }
+
+        # Table with Jinja callable.
+        table = SqlaTable(
+            table_name="test_table",
+            sql="SELECT * from test_table;",
+            database=get_example_database(),
+        )
+        sqlaq = table.get_sqla_query(**query_obj)
+        sql = table.database.compile_sqla_query(sqlaq.sqla_query)
+        assert sql[-1] != ";"
+
     def test_multiple_sql_statements_raises_exception(self):
         base_query_obj = {
             "granularity": None,

Reply via email to