Skip to content

fix(sqla): catch TemplateError alongside SupersetSyntaxErrorException in models.py#42401

Open
eschutho wants to merge 1 commit into
apache:masterfrom
eschutho:fix-jinja-templateerror-sqla-models
Open

fix(sqla): catch TemplateError alongside SupersetSyntaxErrorException in models.py#42401
eschutho wants to merge 1 commit into
apache:masterfrom
eschutho:fix-jinja-templateerror-sqla-models

Conversation

@eschutho

Copy link
Copy Markdown
Member

SUMMARY

Four call sites in superset/connectors/sqla/models.py that render Jinja expressions only caught SupersetSyntaxErrorException, missing jinja2.exceptions.TemplateError. A raw TemplateError (e.g. UndefinedError) from these sites propagated as an unhandled 500 instead of a clear, actionable QueryObjectValidationError. This mirrors the same bug class fixed for get_rendered_sql() in #42366, and this file already has two sibling call sites (RLS filters, get_fetch_values_predicate) that catch the error correctly — this PR brings the remaining four in line with that existing pattern.

PROBLEM

TableColumn.get_sqla_col, TableColumn.get_timestamp_expression, SqlMetric.get_sqla_col, and SqlaTable._render_adhoc_expression_for_metadata_lookup each wrap template_processor.process_template(...) in a try/except SupersetSyntaxErrorException. Since jinja2.exceptions.UndefinedError is a subclass of TemplateError, not SupersetSyntaxErrorException, a column/metric/adhoc-column Jinja expression that references an undefined variable (e.g. an arithmetic operation on an undefined name) raises a raw UndefinedError that these except clauses don't catch, surfacing as an unhandled 500 during chart explore/render.

FIX

Widen the except clause at all four sites to except (TemplateError, SupersetSyntaxErrorException) as ex: and extract the message via getattr(ex, "message", str(ex)), exactly matching the two already-correct sibling call sites in this same file. TemplateError was already imported at the top of the file. No other exception handling was touched, and superset/models/helpers.py (already fixed in #42366) was left untouched.

TESTING INSTRUCTIONS

  • Added tests/unit_tests/connectors/sqla/models_test.py::test_get_sqla_col_wraps_raw_jinja_undefined_error, which constructs a TableColumn with the Jinja expression {{ nonexistent_var + 1 }} and asserts get_sqla_col() raises QueryObjectValidationError instead of a raw UndefinedError. Verified the test fails on pre-fix code and passes after.
  • Ran pytest tests/unit_tests/connectors/sqla/models_test.py (44 passed) and pytest tests/unit_tests/jinja_context_test.py (101 passed, no regressions).
  • Ran ruff check and mypy on the changed files — no new issues.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

… in models.py

Four call sites around template_processor.process_template() in
superset/connectors/sqla/models.py only caught SupersetSyntaxErrorException,
missing jinja2.exceptions.TemplateError (e.g. UndefinedError raised when
indexing into filter_values() with no active filter). This let a raw
jinja2 exception propagate as an unhandled 500 instead of surfacing as a
QueryObjectValidationError, mirroring the same bug class already fixed for
get_rendered_sql() and the two other correct call sites in this file.

Widen the except clause and message extraction in TableColumn.get_sqla_col,
TableColumn.get_timestamp_expression, SqlMetric.get_sqla_col, and
SqlaTable._render_adhoc_expression_for_metadata_lookup to match the
existing correct sibling sites (RLS filters and get_fetch_values_predicate).
@bito-code-review

bito-code-review Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #d58554

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: a71a972..a71a972
    • superset/connectors/sqla/models.py
    • tests/unit_tests/connectors/sqla/models_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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@dosubot dosubot Bot added change:backend Requires changing the backend global:jinja Related to Jinja templating labels Jul 24, 2026
@netlify

netlify Bot commented Jul 24, 2026

Copy link
Copy Markdown

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit a71a972
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a63d37d78e30d0008f6a5ee
😎 Deploy Preview https://deploy-preview-42401--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.25%. Comparing base (14c9676) to head (a71a972).
⚠️ Report is 31 commits behind head on master.

Files with missing lines Patch % Lines
superset/connectors/sqla/models.py 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #42401      +/-   ##
==========================================
- Coverage   65.25%   65.25%   -0.01%     
==========================================
  Files        2794     2795       +1     
  Lines      157410   157482      +72     
  Branches    35992    36000       +8     
==========================================
+ Hits       102720   102763      +43     
- Misses      52713    52742      +29     
  Partials     1977     1977              
Flag Coverage Δ
hive 38.43% <0.00%> (+<0.01%) ⬆️
mysql 57.63% <0.00%> (+<0.01%) ⬆️
postgres 57.67% <0.00%> (+<0.01%) ⬆️
presto 40.35% <0.00%> (+<0.01%) ⬆️
python 59.08% <0.00%> (+<0.01%) ⬆️
sqlite 57.30% <0.00%> (+<0.01%) ⬆️
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend global:jinja Related to Jinja templating size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant