Skip to content

fix(alerts): wrap Jinja rendering errors in AlertCommand._execute_query - #42714

Merged
eschutho merged 1 commit into
apache:masterfrom
eschutho:fix-alert-jinja-render-error
Aug 4, 2026
Merged

eschutho merged 1 commit into
apache:masterfrom
eschutho:fix-alert-jinja-render-error

Conversation

@eschutho

@eschutho eschutho commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

SUMMARY

AlertCommand._execute_query() in superset/commands/report/alert.py calls sql_template.process_template(self._report_schedule.sql) before the try: block a few lines below it, even though that block's except Exception as ex: raise AlertQueryError(...) from ex is clearly meant to cover the whole query-execution flow (the docstring documents :raises AlertQueryError: SQL query is not valid).

Because the Jinja render call sits outside the try, any exception raised while templating the alert's SQL — SupersetSyntaxErrorException, SupersetTemplateException, or a bare jinja2.exceptions.TemplateError/UndefinedError re-raised by process_template()'s own fallback path in superset/jinja_context.py — propagates unhandled instead of becoming the documented AlertQueryError.

Alert SQL is a normal user-editable field with documented Jinja templating support, so a user-authored alert query referencing an undefined Jinja variable currently crashes the alert-execution task with a raw exception instead of surfacing the typed, expected error. Same bug class (raw system/library exception escaping instead of a proper Superset exception) as #42366 and #42401.

PROBLEM

A raw jinja2.exceptions.TemplateError (or a raw SupersetSyntaxErrorException/SupersetTemplateException) can propagate out of AlertCommand._execute_query() uncaught, because the Jinja-rendering line is physically placed one statement above the try: block that's supposed to wrap the whole alert-query execution.

FIX

Move rendered_sql = sql_template.process_template(self._report_schedule.sql) to be the first statement inside the existing try: block, so it's covered by the same exception handling that already wraps the rest of the query-execution flow. sql_template = jinja_context.get_template_processor(...) is left in place — this is a 2-line, additive-only change with no behavior change for any alert query that renders successfully today.

TESTING INSTRUCTIONS

  • Added tests/unit_tests/commands/report/alert_test.py::test_execute_query_wraps_template_rendering_error, which mocks process_template to raise a jinja2.exceptions.TemplateError and asserts _execute_query() raises AlertQueryError instead of the raw error.
  • Confirmed empirically that this test fails on pre-fix code (raises the raw TemplateError uncaught) and passes post-fix.
  • pytest tests/unit_tests/commands/report/alert_test.py — 25 passed.
  • ruff check / ruff format --check on both changed files — clean.
  • mypy on changed files — no new errors.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration
  • Introduces new feature or API
  • Removes existing feature or API

Tradeoffs: Additive exception-handling fix only, no change for any query that renders/executes successfully today. Two failure-path behaviors change, both bringing the code in line with the method's own documented contract:

  1. A raw Jinja/Superset template exception during rendering now becomes AlertQueryError (the primary fix, per the docstring's :raises AlertQueryError: SQL query is not valid).
  2. A celery SoftTimeLimitExceeded firing during template rendering (e.g. a pathological/slow Jinja macro) is now also caught by the existing except SoftTimeLimitExceeded handler and converted to AlertQueryTimeout, instead of propagating raw as it did before — consistent with the docstring's :raises AlertQueryTimeout: The SQL query received a celery soft timeout.

Related: #42366, #42401 (same bug class: raw Jinja/system exceptions propagating instead of typed Superset exceptions).

The rendered_sql = sql_template.process_template(...) call sat before
the try block in _execute_query, even though that block's except
Exception -> AlertQueryError handler is documented to cover the whole
query-execution flow. A Jinja rendering error (e.g. an undefined
variable in the alert SQL) therefore propagated as a raw
jinja2.exceptions.TemplateError instead of the documented
AlertQueryError. Move the process_template call inside the try block
so templating errors are wrapped like any other query execution error.
@dosubot dosubot Bot added alert-reports Namespace | Anything related to the Alert & Reports feature global:jinja Related to Jinja templating labels Aug 3, 2026
@bito-code-review

bito-code-review Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #fca45d

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: e15e95a..e15e95a
    • superset/commands/report/alert.py
    • tests/unit_tests/commands/report/alert_test.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@eschutho
eschutho requested a review from rebenitez1802 August 3, 2026 16:58
@codecov

codecov Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.43%. Comparing base (10f7927) to head (e15e95a).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #42714   +/-   ##
=======================================
  Coverage   65.42%   65.43%           
=======================================
  Files        2810     2810           
  Lines      159528   159528           
  Branches    36410    36410           
=======================================
+ Hits       104377   104380    +3     
+ Misses      53104    53102    -2     
+ Partials     2047     2046    -1     
Flag Coverage Δ
hive 38.06% <0.00%> (ø)
mysql 57.77% <100.00%> (ø)
postgres 57.82% <100.00%> (+<0.01%) ⬆️
presto 39.95% <0.00%> (ø)
python 59.20% <100.00%> (+<0.01%) ⬆️
sqlite 57.44% <100.00%> (ø)
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rebenitez1802 rebenitez1802 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eschutho
eschutho merged commit 7c03736 into apache:master Aug 4, 2026
82 checks passed
eschutho added a commit to preset-io/agor-teammate that referenced this pull request Aug 13, 2026
eschutho added a commit to preset-io/agor-teammate that referenced this pull request Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

alert-reports Namespace | Anything related to the Alert & Reports feature global:jinja Related to Jinja templating size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants