Skip to content

fix(query): take chart time bounds from the temporal x-axis only - #46345

Open
sadpandajoe wants to merge 5 commits into
masterfrom
ultraset-temporal-range-filter-follows-chart-x-axis
Open

sadpandajoe wants to merge 5 commits into
masterfrom
ultraset-temporal-range-filter-follows-chart-x-axis

Conversation

@sadpandajoe

Copy link
Copy Markdown
Member

SUMMARY

Supersedes #39683.

On datasets with several datetime columns, a chart's time bounds could come from a column other than its x-axis, and a dashboard time range could overwrite filters on other datetime columns:

  1. get_time_range_from_filters (called by QueryObjectFactory to compute from_dttm/to_dttm) compared each TEMPORAL_RANGE filter's col only with the x-axis label and otherwise fell back to the first TEMPORAL_RANGE filter (fallback introduced in fix: the temporal x-axis results in a none time_range. #25429). A chart whose temporal x-axis had no range of its own took another column's range, and that range was then applied to the time column. A filter on the axis expression, or one whose col is an adhoc column, never matched.
  2. QueryContextFactory._apply_filters wrote time_range into every TEMPORAL_RANGE filter (since fix: Time Column on Generic X-axis #23021), so a range on a second datetime column was replaced by the dashboard time range, and a chart with no filter on its x-axis had the range applied to another column instead.

This PR:

  • Matches the x-axis to TEMPORAL_RANGE filters by label and then by expression, accepting plain strings and adhoc column dicts.
  • When the x-axis is a temporal column of the dataset and no filter targets it, uses no time range instead of another column's range. Charts with no x-axis, or with a non-temporal one (for example a bar chart by country), keep using the first temporal range, which a Time Column override relies on.
  • Applies the time range only to the TEMPORAL_RANGE filters on the chart's time column (granularity) or x-axis, by label or expression. A temporal x-axis with no such filter gets one carrying the range, unless a time column already carries it. When neither column is known, every TEMPORAL_RANGE filter takes the range as before.
  • Leaves _apply_granularity unchanged, so the dashboard Time Column override and the x-axis label it preserves keep working.

TESTING INSTRUCTIONS

  1. Use a dataset with two datetime columns, e.g. order_date and ship_date.
  2. Create a Bar Chart with x-axis order_date, a time filter order_date = No filter, and a second adhoc filter ship_date with a range that excludes some rows. Save it to a dashboard with a Time Range native filter and pick a range.
  3. In View query, the WHERE clause bounds order_date by the dashboard range and still applies the chart's own ship_date range. Before this change both columns were bounded by the dashboard range.
  4. Remove the order_date time filter from the chart: the query still bounds order_date by the dashboard range and keeps ship_date's own range. Before this change order_date was unbounded and ship_date took the dashboard range.
  5. Change the x-axis to a Custom SQL ship_date on a dataset whose main datetime column is order_date, with a ship_date time filter: both order_date and ship_date are bounded by the dashboard range.
  6. Change the x-axis to a categorical column (e.g. status) with only a time filter on order_date: that filter takes the dashboard range, as before.

ADDITIONAL INFORMATION

Behaviour change, recorded in UPDATING.md: a chart with a temporal x-axis but no TEMPORAL_RANGE on it no longer borrows another column's range, and a time range no longer overwrites filters on other datetime columns. It applies to the time column or x-axis filter and, for a temporal x-axis without one, is added as a filter on the x-axis. Existing tests that asserted the old overwrite were updated: the cache-values test in test_query_context_factory.py and the expected bar counts of five setups in test_drill_by_time_column.py (the February order shipped in July now stays outside the chart's own ship_date range). The drill-by contract, that drill rows equal the clicked bar, still holds.

Notes:

  • A Custom SQL x-axis on a datetime column other than the dataset's main datetime column, with no time filter of its own, still receives the dashboard range on the main datetime column, not on the x-axis column. This is unchanged from before, because the granularity inference in _apply_granularity is left untouched.
  • Time-comparison offsets still pick the first TEMPORAL_RANGE filter (_get_temporal_column_for_filter); this PR does not change that path.

🤖 Generated with Claude Code

sadpandajoe and others added 3 commits November 19, 2026 00:13
When a chart's x-axis is a temporal column, the main time range comes only
from a TEMPORAL_RANGE filter on that axis, matched by label and then by
expression and comparing adhoc filter columns by label or expression. A
chart with no such filter no longer borrows another datetime column's range.
Charts without an x-axis, or with a non-temporal one, still use the first
temporal range.

With a known time column, a time range only replaces the TEMPORAL_RANGE
filter on that column, so filters on other datetime columns keep their own
ranges. Without one, every TEMPORAL_RANGE filter is still rewritten.

Fixes #39681

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ange-filter-follows-chart-x-axis

# Conflicts:
#	UPDATING.md
…lumn

Charts with a temporal x-axis and no Time Column carry no granularity, so
the time range still replaced the ranges of other datetime columns. An
expression x-axis on a datetime column other than the main one infers the
main datetime column as granularity, so its own range filter was skipped.

The time range now replaces the TEMPORAL_RANGE filters that target the time
column or the x-axis. A temporal x-axis without one gets a filter for the
range unless a time column already carries it. With neither column known,
every TEMPORAL_RANGE filter takes the range as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment on lines +58 to +63
if not base_axes or (
temporal_columns is not None
and cast(Mapping[str, object], base_axes[0]).get("sqlExpression")
not in temporal_columns
):
return cast(str, temporal_filters[0].get("val"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion: For an axis like DATE(ds), this returns the first unrelated temporal filter before checking for an axis match, so the query gets bounds from the wrong column.

Assessment: 🟠 Major · 🔁 Occurrence: Sometimes · 🏷️ Incorrect condition logic

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/common/utils/time_range_utils.py
**Line:** 58:63
**Comment:**
	*Incorrect Condition Logic: For an axis like `DATE(ds)`, this returns the first unrelated temporal filter before checking for an axis match, so the query gets bounds from the wrong column.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟡  Medium Risk

  • The PR needs attention before merging because computed temporal x-axes can inherit time bounds from an unrelated temporal filter.
  • In get_time_range_from_filters, an axis expression such as DATE(ds) is not matched against filters unless it exactly matches a datasource column name.

Assessed commit: baa5ca55d9fa

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.68%. Comparing base (f357ec3) to head (baa5ca5).
⚠️ Report is 94 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #46345      +/-   ##
==========================================
+ Coverage   85.66%   85.68%   +0.01%     
==========================================
  Files        3111     3110       -1     
  Lines      214938   215616     +678     
  Branches    50908    50817      -91     
==========================================
+ Hits       184136   184751     +615     
- Misses      27381    27446      +65     
+ Partials     3421     3419       -2     
Flag Coverage Δ
hive 33.87% <11.42%> (-0.13%) ⬇️
mysql 51.56% <82.85%> (-0.11%) ⬇️
postgres 51.59% <82.85%> (?)
presto 35.55% <42.85%> (-0.14%) ⬇️
python 88.42% <100.00%> (+0.13%) ⬆️
sqlite 51.26% <88.57%> (-0.12%) ⬇️
unit 83.84% <100.00%> (+0.26%) ⬆️

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

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

@sadpandajoe
sadpandajoe requested a review from rusackas November 19, 2026 18:14
@github-actions github-actions Bot added the requires:rebase Requires rebasing on top of current master label Nov 19, 2026
# Conflicts:
#	tests/unit_tests/common/test_time_range_utils.py
@github-actions github-actions Bot removed the requires:rebase Requires rebasing on top of current master label Nov 20, 2026
@aminghadersohi
aminghadersohi self-requested a review November 21, 2026 16:39

@aminghadersohi aminghadersohi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Time comparison on a temporal x-axis returns an empty series when the chart also filters another datetime column.

Comment on lines +533 to +534
else:
targets = temporal_filters

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Drill to Detail sends no x-axis column, so it reaches this branch and overwrites every TEMPORAL_RANGE filter, while the chart query keeps other datetime columns' own ranges; with a dashboard time range the detail rows no longer match the clicked bar.

return cast(str, selected.get("val"))
base_axes: list[Column] = get_base_axis_columns(columns)
if not base_axes or (
temporal_columns is not None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: _get_adhoc_filters in semantic_layers/mapper.py still calls this without temporal_columns, so for a categorical x-axis it now selects No filter while the factory used the first range, and that range filter is no longer dropped in favour of the factory bounds.

…ts it

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@netlify

netlify Bot commented Nov 22, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit baa5ca5
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6b024969bf91f81c8a945dc4
😎 Deploy Preview https://deploy-preview-46345--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@aminghadersohi aminghadersohi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Time comparison now shifts the x-axis range; the dashboard range still lands on the wrong column for Custom SQL x-axes, table groupbys and cross-filter targets.

Comment on lines +496 to +498
base_axes: list[Column] = get_base_axis_columns(
getattr(query_object, "columns", None)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Table, pivot and box plot queries send a temporal groupby as BASE_AXIS with no x-axis, so the dashboard range now bounds that groupby column instead of the time filter column, while the table's totals query (no columns) still bounds ds: totals stop matching the rows.

for filter_object in temporal_filters
if time_columns.intersection(get_column_identifiers(filter_object["col"]))
]
if not targets and not isinstance(granularity, str):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With a Custom SQL x-axis such as DATE(created_at), _apply_granularity sets the main datetime column as granularity, so nothing matches and this branch is skipped: the dashboard range bounds only ds, and created_at days outside it show on the axis (base bounded both).

Comment on lines +508 to +512
targets: list[QueryObjectFilterClause] = [
filter_object
for filter_object in temporal_filters
if time_columns.intersection(get_column_identifiers(filter_object["col"]))
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Time x-axis cross-filters send the clicked bucket as time_range and rely on every TEMPORAL_RANGE being rewritten (see EchartsTimeseries.tsx); a target on another datetime x-axis now has that axis bounded to the bucket and keeps its saved range, so a disjoint saved range empties it.

Comment on lines +531 to +533
# Time comparison shifts only the first temporal range, so the
# x-axis range goes first.
query_object.filter.insert(0, axis_filter)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With DATE_RANGE_TIMESHIFTS_ENABLED, a date-range offset drops every TEMPORAL_RANGE and re-adds only the x-axis one, so the comparison series ignores the created_at range this change now keeps on the main series.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Temporal filters and granularity can diverge from the chart x-axis when multiple datetime columns exist

2 participants