diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index a433f03fe7a65..cb506f6c4a522 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -773,7 +773,6 @@ def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor: 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 @@ def get_rendered_sql( """ Render sql with template engine (Jinja). """ + sql = self.sql if template_processor: try: @@ -829,7 +829,7 @@ def get_rendered_sql( 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 cdd77c270b90d..b4cd489cf89a8 100644 --- a/tests/sqla_models_tests.py +++ b/tests/sqla_models_tests.py @@ -223,6 +223,28 @@ def test_incorrect_jinja_syntax_raises_correct_exception(self): 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,