Skip to content

fix(jinja): handle UndefinedError from virtual dataset templates#42366

Open
eschutho wants to merge 1 commit into
apache:masterfrom
eschutho:fix-jinja-filter-values-index-error
Open

fix(jinja): handle UndefinedError from virtual dataset templates#42366
eschutho wants to merge 1 commit into
apache:masterfrom
eschutho:fix-jinja-filter-values-index-error

Conversation

@eschutho

Copy link
Copy Markdown
Member

SUMMARY

Virtual dataset SQL templates that index into filter_values() (e.g. filter_values('col')[0]) can raise a Jinja UndefinedError when no dashboard filter is active for that column. This fix catches that error in get_rendered_sql() and surfaces it as a clear, chart-level validation error instead.

PROBLEM

When a virtual dataset's SQL template does something like:

WHERE col = {{ filter_values('col')[0] }}

and there are no active dashboard filters for col, filter_values('col') returns an empty list. Depending on how the templated value is used further in the query, indexing or operating on that empty list can raise jinja2.exceptions.UndefinedError (e.g. list object has no element 0) during Jinja rendering. This exception was not translated into a Superset-specific validation error, so it could surface as a raw, unhandled 500 instead of a clear, actionable error message shown at the chart level.

FIX

get_rendered_sql() in superset/models/helpers.py now explicitly catches jinja2.exceptions.UndefinedError around template rendering and re-raises it as a QueryObjectValidationError with a descriptive message ("Virtual dataset template error: %(msg)s"), following the same pattern already used for other template-rendering failures in that function.

TESTING INSTRUCTIONS

  • Added a regression test in tests/unit_tests/jinja_context_test.py that creates a virtual dataset whose SQL template indexes into filter_values('col')[0] with no active filters, and asserts that get_rendered_sql() raises QueryObjectValidationError (instead of an unhandled UndefinedError).
  • Ran pytest tests/unit_tests/jinja_context_test.py tests/unit_tests/models/helpers_test.py locally — all tests pass.
  • Ran ruff check, ruff format --check, pylint, and mypy on the changed files.

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

Virtual dataset SQL templates that index into filter_values() (e.g.
filter_values('col')[0]) raise a Jinja UndefinedError when no
dashboard filter is active for that column, since the call returns
an empty list. get_rendered_sql() now catches UndefinedError and
re-raises it as a QueryObjectValidationError with a clear message,
turning an unhandled 500 into a graceful chart-level error.

Co-Authored-By: Claude <noreply@anthropic.com>
@bito-code-review

bito-code-review Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #46276b

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: cbca3a6..cbca3a6
    • superset/models/helpers.py
    • tests/unit_tests/jinja_context_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 the global:jinja Related to Jinja templating label Jul 23, 2026
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 65.25%. Comparing base (206fe7a) to head (cbca3a6).

Files with missing lines Patch % Lines
superset/models/helpers.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #42366      +/-   ##
==========================================
- Coverage   65.25%   65.25%   -0.01%     
==========================================
  Files        2794     2794              
  Lines      157396   157398       +2     
  Branches    35982    35982              
==========================================
- Hits       102706   102704       -2     
- Misses      52713    52716       +3     
- Partials     1977     1978       +1     
Flag Coverage Δ
hive 38.42% <33.33%> (-0.01%) ⬇️
mysql 57.63% <66.66%> (-0.01%) ⬇️
postgres 57.66% <66.66%> (-0.01%) ⬇️
presto 40.35% <33.33%> (-0.01%) ⬇️
python 59.07% <66.66%> (-0.01%) ⬇️
sqlite 57.29% <66.66%> (-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

global:jinja Related to Jinja templating size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant