Skip to content

Commit cd175af

Browse files
eschuthoclaude
andauthored
fix(sql-lab): raise SupersetTemplateException instead of raw UndefinedError in SQLExecutor.execute_async (#42851)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 3758941 commit cd175af

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎superset/sql/execution/executor.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import pyarrow as pa
7171
from flask import current_app as app, g, has_app_context
7272
from flask_babel import gettext as __
73+
from jinja2.exceptions import TemplateError
7374

7475
from superset import db
7576
from superset.db_engine_specs.base import TIME_LIMIT_EXCEPTIONS
@@ -81,6 +82,7 @@
8182
SupersetErrorException,
8283
SupersetParseError,
8384
SupersetSecurityException,
85+
SupersetTemplateException,
8486
SupersetTimeoutException,
8587
)
8688
from superset.extensions import cache_manager
@@ -1327,14 +1329,18 @@ def _render_sql_template(
13271329
:param sql: SQL string potentially containing Jinja2 templates
13281330
:param template_params: Parameters to pass to the template
13291331
:returns: Rendered SQL string
1332+
:raises SupersetTemplateException: if the template fails to render
13301333
"""
13311334
if template_params is None:
13321335
return sql
13331336

13341337
from superset.jinja_context import get_template_processor
13351338

13361339
tp = get_template_processor(database=self.database)
1337-
return tp.process_template(sql, **template_params)
1340+
try:
1341+
return tp.process_template(sql, **template_params)
1342+
except TemplateError as ex:
1343+
raise SupersetTemplateException(str(ex)) from ex
13381344

13391345
def _apply_limit_to_script(self, script: SQLScript, opts: QueryOptions) -> None:
13401346
"""

‎tests/unit_tests/sql/execution/test_executor.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from superset.db_engine_specs.base import TIME_LIMIT_EXCEPTIONS
5050
from superset.models.core import Database
5151
from superset.sql.parse import LimitMethod, SQLScript
52+
from tests.unit_tests.conftest import with_feature_flags
5253

5354
# Note: database, database_with_dml, mock_db_session fixtures and
5455
# mock_query_execution helper are imported from conftest.py
@@ -1156,6 +1157,42 @@ def test_execute_async_dml_without_permission_raises(
11561157
database.execute_async("INSERT INTO users (name) VALUES ('test')")
11571158

11581159

1160+
@with_feature_flags(ENABLE_TEMPLATE_PROCESSING=True)
1161+
def test_execute_async_undefined_template_var_raises_superset_template_exception(
1162+
mocker: MockerFixture, database: Database, app_context: None
1163+
) -> None:
1164+
"""A Jinja template referencing an undefined variable (not called as a
1165+
function) must not leak a raw ``jinja2.exceptions.UndefinedError`` out of
1166+
``execute_async`` - it should surface as ``SupersetTemplateException``."""
1167+
from superset.exceptions import SupersetTemplateException
1168+
1169+
mocker.patch.dict(
1170+
current_app.config, {"SQL_QUERY_MUTATOR": None, "SQLLAB_TIMEOUT": 30}
1171+
)
1172+
1173+
options = QueryOptions(template_params={"foo": "bar"})
1174+
1175+
with pytest.raises(SupersetTemplateException):
1176+
database.execute_async("SELECT {{ missing_var[0] }}", options=options)
1177+
1178+
1179+
@with_feature_flags(ENABLE_TEMPLATE_PROCESSING=True)
1180+
def test_execute_sync_undefined_template_var_returns_failed_result(
1181+
mocker: MockerFixture, database: Database, app_context: None
1182+
) -> None:
1183+
"""The sync ``execute`` path's broad ``except Exception`` still catches the
1184+
template rendering failure and returns a FAILED ``QueryResult``, unchanged
1185+
by the ``_render_sql_template`` fix."""
1186+
mocker.patch.dict(
1187+
current_app.config, {"SQL_QUERY_MUTATOR": None, "SQLLAB_TIMEOUT": 30}
1188+
)
1189+
1190+
options = QueryOptions(template_params={"foo": "bar"})
1191+
result = database.execute("SELECT {{ missing_var[0] }}", options=options)
1192+
1193+
assert result.status == QueryStatus.FAILED
1194+
1195+
11591196
def test_async_handle_get_status(
11601197
mocker: MockerFixture,
11611198
database: Database,

0 commit comments

Comments
 (0)