Skip to content

fix(dataset): calculated columns in virtual datasets fail when used as dynamic aggregation filter dimensions#39004

Merged
geido merged 2 commits into
masterfrom
geido/fix/calculated-cols-virtual-datasets
Apr 20, 2026
Merged

fix(dataset): calculated columns in virtual datasets fail when used as dynamic aggregation filter dimensions#39004
geido merged 2 commits into
masterfrom
geido/fix/calculated-cols-virtual-datasets

Conversation

@geido
Copy link
Copy Markdown
Member

@geido geido commented Apr 1, 2026

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 SqlMetricSqlaTable 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:

570342908-208dcc04-dfc4-4cdb-91a8-6fb9418b894c.mp4

AFTER:

570343108-8ebd9dd1-a2b6-4cbc-b76b-fd1adbd6cf2e.mp4

TESTING INSTRUCTIONS

  1. Create a virtual dataset and save it as a table:
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;
  1. Build a chart based on this dataset (Charts → select the virtual table → Bar Chart).

  2. 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
  3. Drag event_date to the x-axis and random_value to metrics. Confirm the bar chart renders.

  4. Create an example dashboard and add the chart.

  5. Create a second virtual dataset for the filter:

SELECT 'category' AS aggregation
UNION ALL
SELECT 'gt_or_lt_50' AS aggregation
  1. Add a Value filter to the dashboard:

    • Filter Name: breakdown_by
    • Dataset: the table from step 6
    • Column: aggregation
    • Apply to all panels/charts.
  2. 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.

  1. Select category from the filter — verify the chart updates correctly.
  2. 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)
    • 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

FEATURE_ENABLE_TEMPLATE_PROCESSING: true

…_column_to_sqla

Calculated columns on virtual datasets fail when used as dynamic aggregation
filter dimensions because get_column() was called with the raw Jinja template
string instead of the rendered column name. Pre-rendering via a dedicated helper
(_render_adhoc_expression_for_metadata_lookup) ensures expressions like
{{ filter_values('x')[0] }} resolve to the calculated column name before the
metadata lookup, so the column's CASE expression is inlined correctly rather
than falling back to literal_column.

Also fixes Mapped[SqlaTable] forward references to use quoted strings and
renames tbl, _ to tbl, _unused_cte to avoid shadowing the lazy_gettext
translation function.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 1, 2026

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 20d8ad3
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/69ccf409fe86790008089f60
😎 Deploy Preview https://deploy-preview-39004--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.14%. Comparing base (a67ca05) to head (20d8ad3).
⚠️ Report is 204 commits behind head on master.

Files with missing lines Patch % Lines
superset/connectors/sqla/models.py 66.66% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #39004      +/-   ##
==========================================
- Coverage   64.42%   64.14%   -0.29%     
==========================================
  Files        2536     2536              
  Lines      131103   131951     +848     
  Branches    30434    30743     +309     
==========================================
+ Hits        84467    84635     +168     
- Misses      45173    45806     +633     
- Partials     1463     1510      +47     
Flag Coverage Δ
hive 39.75% <25.00%> (-0.34%) ⬇️
mysql 60.33% <58.33%> (-0.61%) ⬇️
postgres 60.41% <66.66%> (-0.61%) ⬇️
presto 39.77% <25.00%> (-0.34%) ⬇️
python 61.98% <66.66%> (-0.64%) ⬇️
sqlite 60.04% <58.33%> (-0.61%) ⬇️
unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@geido geido marked this pull request as ready for review April 2, 2026 07:59
@dosubot dosubot Bot added data:dataset Related to dataset configurations explore:filter Related to filters in Explore labels Apr 2, 2026
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented Apr 2, 2026

Code Review Agent Run #daab6f

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 8934f20..20d8ad3
    • superset/connectors/sqla/models.py
    • tests/unit_tests/models/helpers_test.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ 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

@geido geido added the 🎪 ⚡ showtime-trigger-start Create new ephemeral environment for this PR label Apr 2, 2026
@github-actions github-actions Bot added 🎪 20d8ad3 🚦 building Environment 20d8ad3 status: building 🎪 20d8ad3 📅 2026-04-02T10-11 Environment 20d8ad3 created at 2026-04-02T10-11 🎪 20d8ad3 🤡 geido Environment 20d8ad3 requested by geido 🎪 ⌛ 48h Environment expires after 48 hours (default) and removed 🎪 ⚡ showtime-trigger-start Create new ephemeral environment for this PR labels Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 2, 2026

🎪 Showtime is building environment on GHA for 20d8ad3

@github-actions github-actions Bot added 🎪 20d8ad3 🚦 deploying Environment 20d8ad3 status: deploying 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running 🎪 🎯 20d8ad3 Active environment pointer - 20d8ad3 is receiving traffic 🎪 20d8ad3 🌐 34.220.112.132:8080 Environment 20d8ad3 URL: http://34.220.112.132:8080 (click to visit) and removed 🎪 20d8ad3 🚦 building Environment 20d8ad3 status: building 🎪 20d8ad3 🚦 deploying Environment 20d8ad3 status: deploying 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running 🎪 🎯 20d8ad3 Active environment pointer - 20d8ad3 is receiving traffic labels Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 2, 2026

🎪 Showtime deployed environment on GHA for 20d8ad3

Environment: http://34.220.112.132:8080 (admin/admin)
Lifetime: 48h auto-cleanup
Updates: New commits create fresh environments automatically

Copy link
Copy Markdown
Member

@msyavuz msyavuz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@alexandrusoare alexandrusoare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@github-actions github-actions Bot removed the 🎪 20d8ad3 🤡 geido Environment 20d8ad3 requested by geido label Apr 4, 2026
@github-actions github-actions Bot removed 🎪 20d8ad3 🤡 sadpandajoe Environment 20d8ad3 requested by sadpandajoe 🎪 20d8ad3 🌐 44.252.32.247:8080 Environment 20d8ad3 URL: http://44.252.32.247:8080 (click to visit) 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running labels Apr 10, 2026
@sadpandajoe sadpandajoe added 🎪 ⌛ 48h Environment expires after 48 hours (default) 🎪 ⌛ 1w 🎪 ⚡ showtime-trigger-start Create new ephemeral environment for this PR and removed 🎪 ⌛ 48h Environment expires after 48 hours (default) 🎪 ⌛ 1w labels Apr 13, 2026
@github-actions github-actions Bot added 🎪 20d8ad3 🚦 building Environment 20d8ad3 status: building 🎪 20d8ad3 📅 2026-04-13T16-49 Environment 20d8ad3 created at 2026-04-13T16-49 🎪 20d8ad3 🤡 sadpandajoe Environment 20d8ad3 requested by sadpandajoe and removed 🎪 ⚡ showtime-trigger-start Create new ephemeral environment for this PR labels Apr 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🎪 Showtime is building environment on GHA for 20d8ad3

@github-actions github-actions Bot added 🎪 20d8ad3 🚦 deploying Environment 20d8ad3 status: deploying 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running 🎪 🎯 20d8ad3 Active environment pointer - 20d8ad3 is receiving traffic 🎪 20d8ad3 🌐 35.91.242.246:8080 Environment 20d8ad3 URL: http://35.91.242.246:8080 (click to visit) and removed 🎪 20d8ad3 🚦 building Environment 20d8ad3 status: building 🎪 20d8ad3 🚦 deploying Environment 20d8ad3 status: deploying 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running 🎪 🎯 20d8ad3 Active environment pointer - 20d8ad3 is receiving traffic labels Apr 13, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🎪 Showtime deployed environment on GHA for 20d8ad3

Environment: http://35.91.242.246:8080 (admin/admin)
Lifetime: 1w auto-cleanup
Updates: New commits create fresh environments automatically

@michaelmorera-preset
Copy link
Copy Markdown

The application is working as expected.

Scenario

The calculated column used as aggregation filter dimension is not getting break.

Steps

  1. Updated local config to set ENABLE_TEMPLATE_PROCESSING to True.
  2. Rebuilt and started the environment with docker compose up --build.
  3. Re-ran the previously reported bug flow using the same steps from the original report.
  4. Observed application behavior and logs during execution.

Current Result

  • Issue does not occur anymore.
  • Expected behavior is observed throughout the flow.
  • No related errors were seen during this run.

Evidence

Thecalculatedcolumnusedasaggregationfilterdimensionisnotgettingbreak

@geido geido merged commit 51ea2c2 into master Apr 20, 2026
138 of 141 checks passed
@geido geido deleted the geido/fix/calculated-cols-virtual-datasets branch April 20, 2026 10:44
qfcwell pushed a commit to qfcwell/superset that referenced this pull request May 12, 2026
…s dynamic aggregation filter dimensions (apache#39004)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data:dataset Related to dataset configurations explore:filter Related to filters in Explore review:draft size/L testenv-up 🎪 ⌛ 1w 🎪 20d8ad3 🚦 running Environment 20d8ad3 status: running 🎪 20d8ad3 🤡 sadpandajoe Environment 20d8ad3 requested by sadpandajoe 🎪 20d8ad3 🌐 35.91.242.246:8080 Environment 20d8ad3 URL: http://35.91.242.246:8080 (click to visit) 🎪 20d8ad3 📅 2026-04-13T16-49 Environment 20d8ad3 created at 2026-04-13T16-49

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants