geido opened a new pull request, #39004:
URL: https://github.com/apache/superset/pull/39004
### SUMMARY
This PR fixes a regression introduced in #36215, where
`adhoc_column_to_sqla` was refactored to perform a metadata lookup
(`get_column`) before rendering Jinja templates. As a result, templated
expressions (e.g. using `filter_values`) were passed as raw strings, preventing
calculated columns from being matched and expanded from dataset metadata, and
instead falling back to `literal_column`, which breaks for virtual datasets.
This change restores the correct order by rendering the template first (via
a dedicated helper `_render_adhoc_expression_for_metadata_lookup`) and then
performing the metadata lookup, ensuring calculated columns resolve properly
while preserving the improvements introduced in the original refactor.
Additionally:
- Fixes `Mapped[SqlaTable]` forward references to use quoted strings
(`Mapped["SqlaTable"]`) in `TableColumn` and `SqlMetric` — `SqlaTable` is
defined after both classes so the unquoted form is a forward reference error.
- Renames `tbl, _` to `tbl, _unused_cte` to avoid shadowing the
`lazy_gettext` translation function (`_`) imported at module level.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
BEFORE:
<!-- add video/screenshot here -->
AFTER:
<!-- add video/screenshot here -->
### TESTING INSTRUCTIONS
1. Create a virtual dataset and save it as a table:
```sql
SELECT
generated_date AS event_date,
FLOOR(RAND() * 101) AS random_value,
IF(RAND() < 0.5, 'A', 'B') AS category
FROM
UNNEST(GENERATE_DATE_ARRAY('2025-08-01', '2026-02-28', INTERVAL 1 DAY)) AS
generated_date;
```
2. Build a chart based on this dataset (Charts → select the virtual table →
Bar Chart).
3. Create a calculated column on the virtual dataset:
- **column_name:** `gt_or_lt_50`
- **expression:** `CASE WHEN random_value > 50 THEN 'GT 50' ELSE 'LT 50'
END`
4. Drag `event_date` to the x-axis and `random_value` to metrics. Confirm
the bar chart renders.
5. Create an example dashboard and add the chart.
6. Create a second virtual dataset for the filter:
```sql
SELECT 'category' AS aggregation
UNION ALL
SELECT 'gt_or_lt_50' AS aggregation
```
7. Add a Value filter to the dashboard:
- **Filter Name:** `breakdown_by`
- **Dataset:** the table from step 6
- **Column:** `aggregation`
- Apply to all panels/charts.
8. Open the chart and add the following custom SQL to the Dimension field:
```
{{ filter_values('aggregation')[0] if filter_values('aggregation') else
"'Total'" }}
```
Save the chart and return to the dashboard.
9. Select `category` from the filter — verify the chart updates correctly.
10. Select `gt_or_lt_50` from the filter — **before this fix this would
error**; after the fix the chart should update and display the calculated
column breakdown correctly.
### 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
--
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]