Skip to content

fix(time-comparison): shift offset filter when X-axis is adhoc Custom SQL#40586

Merged
sadpandajoe merged 1 commit into
apache:masterfrom
jesperct:fix/time-comparison-offset-temporal-filter-adhoc-x-axis
Jun 2, 2026
Merged

fix(time-comparison): shift offset filter when X-axis is adhoc Custom SQL#40586
sadpandajoe merged 1 commit into
apache:masterfrom
jesperct:fix/time-comparison-offset-temporal-filter-adhoc-x-axis

Conversation

@jesperct
Copy link
Copy Markdown
Contributor

@jesperct jesperct commented Jun 1, 2026

SUMMARY

When a chart with a Custom SQL X-axis (adhoc column whose label differs from the underlying time column) uses a relative time comparison like "1 year ago", the offset query's temporal filter never gets shifted. The original time range and the granularity-derived shifted range both end up in the WHERE clause, AND'd together. The intersection covers only the overlap of the two ranges, so the comparison series shows values for just that overlap (e.g. last ~4 months of the requested window) instead of the full range.

In processing_time_offsets (relative-offset, datetime-series branch), the existing TEMPORAL_RANGE filter was matched against query_object_clone.granularity or x_axis_label. For an adhoc Custom SQL X-axis the label is the user's column label (e.g. wedding_date_cast), not the dataset column the filter is on (wedding_date), so the match fails and the filter's val stays at the original range.

The fix uses _get_temporal_column_for_filter, which is already used by the date-range-offset branch. It prefers the column from an existing TEMPORAL_RANGE filter over the X-axis label.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Same chart, same X-axis Custom SQL, time range 2025-01-01 to 2026-06-01, "1 year ago" comparison. Dataset has year-dependent revenue (2024 baseline + 80/year) so the comparison line is visually offset from the main line.

Before — 1 year ago series (purple) only renders for the last ~4 months of the range (the overlap of original and shifted windows):

After — 1 year ago series plots across the full range, ~80 below the main SUM(revenue) line (as expected from the dataset formula):

Offset query WHERE clause before/after:

Before (offset has impossible intersection of original + shifted ranges):

WHERE wedding_date >= '2024-01-01' AND wedding_date < '2025-06-01'
  AND wedding_date >= '2025-01-01' AND wedding_date < '2026-06-01'

After (offset uses the shifted range only):

WHERE wedding_date >= '2024-01-01' AND wedding_date < '2025-06-01'
  AND wedding_date >= '2024-01-01' AND wedding_date < '2025-06-01'

TESTING INSTRUCTIONS

  1. Create a virtual dataset with a temporal column (e.g. wedding_date).
  2. Create a Mixed/Line/Bar chart on that dataset.
  3. Set X-axis to Custom SQL with a Jinja expression like:
    {% set tc = time_column if time_column else 'wedding_date' %} CAST({{ tc }} AS TIMESTAMP)
    
  4. Add a SUM metric, a TEMPORAL_RANGE filter on wedding_date, time grain Month.
  5. Enable a relative time comparison such as "1 year ago".
  6. Confirm the comparison line plots values across the full range rather than only the trailing overlap.

Unit test: test_processing_time_offsets_updates_temporal_filter_with_adhoc_x_axis.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
  • Introduces new feature or API
  • Removes existing feature or API

@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented Jun 1, 2026

Code Review Agent Run #f1a130

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 65c15d2..65c15d2
    • superset/models/helpers.py
    • tests/unit_tests/common/test_query_context_processor.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

@dosubot dosubot Bot added the explore:time Related to the time filters in Explore label Jun 1, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.95%. Comparing base (1523d79) to head (fdb2d62).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #40586   +/-   ##
=======================================
  Coverage   63.94%   63.95%           
=======================================
  Files        2658     2658           
  Lines      143011   143011           
  Branches    32866    32866           
=======================================
+ Hits        91454    91456    +2     
+ Misses      49994    49992    -2     
  Partials     1563     1563           
Flag Coverage Δ
hive 39.75% <0.00%> (ø)
mysql 58.40% <100.00%> (ø)
postgres 58.47% <100.00%> (ø)
presto 41.36% <0.00%> (ø)
python 59.96% <100.00%> (+<0.01%) ⬆️
sqlite 58.13% <100.00%> (+<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 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.

@jesperct jesperct force-pushed the fix/time-comparison-offset-temporal-filter-adhoc-x-axis branch from 65c15d2 to afa24ce Compare June 1, 2026 18:54
… SQL

When the X-axis is an adhoc Custom SQL column (label != underlying time
column), the relative time-offset datetime-series branch matched the
existing TEMPORAL_RANGE filter against `granularity or x_axis_label`,
which never equaled the dataset column. The filter kept the original
range and the offset query AND'd both ranges together, returning an
empty intersection. Match against the column resolved from the existing
filter via `_get_temporal_column_for_filter` (already used for date-range
offsets) so the filter is shifted correctly.
@jesperct jesperct force-pushed the fix/time-comparison-offset-temporal-filter-adhoc-x-axis branch from afa24ce to fdb2d62 Compare June 1, 2026 21:02
@sadpandajoe sadpandajoe requested a review from betodealmeida June 1, 2026 21:38
@bito-code-review
Copy link
Copy Markdown
Contributor

bito-code-review Bot commented Jun 1, 2026

Code Review Agent Run #a27f44

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: fdb2d62..fdb2d62
    • superset/models/helpers.py
    • tests/unit_tests/common/test_query_context_processor.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

Copy link
Copy Markdown
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this! We need to spend some time cleaning up this logic, it's getting very messy.

@sadpandajoe sadpandajoe merged commit 699e741 into apache:master Jun 2, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

explore:time Related to the time filters in Explore size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants