fix(jinja): handle UndefinedError from virtual dataset templates#42366
fix(jinja): handle UndefinedError from virtual dataset templates#42366eschutho wants to merge 1 commit into
Conversation
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>
Code Review Agent Run #46276bActionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
SUMMARY
Virtual dataset SQL templates that index into
filter_values()(e.g.filter_values('col')[0]) can raise a JinjaUndefinedErrorwhen no dashboard filter is active for that column. This fix catches that error inget_rendered_sql()and surfaces it as a clear, chart-level validation error instead.PROBLEM
When a virtual dataset's SQL template does something like:
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 raisejinja2.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()insuperset/models/helpers.pynow explicitly catchesjinja2.exceptions.UndefinedErroraround template rendering and re-raises it as aQueryObjectValidationErrorwith 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
tests/unit_tests/jinja_context_test.pythat creates a virtual dataset whose SQL template indexes intofilter_values('col')[0]with no active filters, and asserts thatget_rendered_sql()raisesQueryObjectValidationError(instead of an unhandledUndefinedError).pytest tests/unit_tests/jinja_context_test.py tests/unit_tests/models/helpers_test.pylocally — all tests pass.ruff check,ruff format --check,pylint, andmypyon the changed files.ADDITIONAL INFORMATION