From 4ac791c0650006a28f3f092bfec9159d48f6f008 Mon Sep 17 00:00:00 2001 From: "JUST.in DO IT" Date: Fri, 13 Oct 2023 13:07:54 -0400 Subject: [PATCH] fix(sqllab): template validation error within comments (#25626) --- superset/sqllab/query_render.py | 4 +++- tests/integration_tests/sqllab_tests.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/superset/sqllab/query_render.py b/superset/sqllab/query_render.py index 0852aaa891209..4fb64c8ce2d76 100644 --- a/superset/sqllab/query_render.py +++ b/superset/sqllab/query_render.py @@ -25,6 +25,7 @@ from superset import is_feature_enabled from superset.errors import SupersetErrorType +from superset.sql_parse import ParsedQuery from superset.sqllab.commands.execute import SqlQueryRender from superset.sqllab.exceptions import SqlLabException from superset.utils import core as utils @@ -57,8 +58,9 @@ def render(self, execution_context: SqlJsonExecutionContext) -> str: database=query_model.database, query=query_model ) + parsed_query = ParsedQuery(query_model.sql, strip_comments=True) rendered_query = sql_template_processor.process_template( - query_model.sql, **execution_context.template_params + parsed_query.stripped(), **execution_context.template_params ) self._validate(execution_context, rendered_query, sql_template_processor) return rendered_query diff --git a/tests/integration_tests/sqllab_tests.py b/tests/integration_tests/sqllab_tests.py index 3b8941e55611c..03c3bdfc235e9 100644 --- a/tests/integration_tests/sqllab_tests.py +++ b/tests/integration_tests/sqllab_tests.py @@ -514,6 +514,13 @@ def test_sql_json_parameter_error(self): ) assert data["status"] == "success" + data = self.run_sql( + "SELECT * FROM birth_names WHERE state = '{{ state }}' -- blabblah {{ extra1 }} {{fake.fn()}}\nLIMIT 10", + "3", + template_params=json.dumps({"state": "CA"}), + ) + assert data["status"] == "success" + data = self.run_sql( "SELECT * FROM birth_names WHERE state = '{{ stat }}' LIMIT 10", "2",