eschutho opened a new pull request, #44316:
URL: https://github.com/apache/superset/pull/44316

   ### SUMMARY
   
   `get_virtual_table_metadata()` (the entry point behind "Sync columns from 
source" / dataset metadata refresh, reachable via `POST 
/api/v1/dataset/<id>/refresh`) wraps `process_template()` in a narrow `except 
SupersetSyntaxErrorException` handler.
   
   `BaseTemplateProcessor.process_template` (`superset/jinja_context.py`) has 
two separate try/except blocks:
   - the **parse** step (`self.env.from_string(sql)`) wraps 
`TemplateSyntaxError`/`SecurityError`/`UndefinedError`/Unicode errors into 
`SupersetSyntaxErrorException`;
   - the **render** step (`template.render(context)`) catches `UndefinedError` 
and, when the undefined reference *isn't* shaped like a function call, does a 
bare `raise` — letting the raw `jinja2.exceptions.UndefinedError` propagate 
unwrapped.
   
   #### PROBLEM
   
   A virtual dataset whose SQL uses an undefined variable in a non-call 
expression — e.g. `SELECT {{ nonexistent_var + 1 }}` (arithmetic, not `{{ 
nonexistent_var(...) }}`) — raises the raw `UndefinedError` during `render()`, 
hitting that render-path bare-`raise`. `UndefinedError` is not a subclass of 
`SupersetSyntaxErrorException`, so `get_virtual_table_metadata`'s handler 
doesn't catch it. It isn't softened by `RefreshDatasetCommand` either (that 
only catches `SupersetVirtualTableParseException`), nor by the `@transaction` 
`on_error` decorator (default `catches=(SQLAlchemyError,)`). Net effect: a 
completely raw, unhandled `jinja2.exceptions.UndefinedError` surfaces — instead 
of the graceful "log a warning and skip the refresh; the row is already 
persisted" treatment (`SupersetVirtualTableParseException`, see #38012) that 
the *parse-time* UndefinedError sibling case gets two lines below in the same 
function.
   
   #### FIX
   
   Add an `except UndefinedError as ex:` branch **before** the existing `except 
SupersetSyntaxErrorException` branch, raising 
`SupersetVirtualTableParseException` (already imported) — the same target 
exception the parse-time sibling raises, so `RefreshDatasetCommand` softens 
both identically. `UndefinedError` is already imported from `jinja2` in this 
file. The diff is surgical: one new branch plus a regression test.
   
   This mirrors the precedent pattern from #42366 (explicit early 
`UndefinedError` branch ordered before a broader handler, in 
`models/helpers.py::get_rendered_sql`). It is a **distinct site/bug** from 
#42401 (open), which fixes a missing `TemplateError` in an `except` tuple 
across four methods in `superset/connectors/sqla/models.py` — different file, 
different bug class. No open PR touches `connectors/sqla/utils.py`.
   
   ### TESTING INSTRUCTIONS
   
   New unit test `test_get_virtual_table_metadata_render_undefined_error` in 
`tests/unit_tests/connectors/sqla/utils_test.py` asserts a render-step 
`UndefinedError` is softened to `SupersetVirtualTableParseException`. Verified 
it fails on pre-fix code (raw `UndefinedError` propagates) and passes after the 
fix. Full `utils_test.py` (12 passed) and `models_test.py` (85 passed) green; 
`ruff`, `ruff format`, `mypy`, and `pylint` pass via pre-commit.
   
   Manual: create a virtual dataset with SQL `SELECT {{ nonexistent_var + 1 
}}`, then click "Sync columns from source" — refresh is softened gracefully 
instead of surfacing a raw `UndefinedError`.
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] 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
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to