Repository navigation
fix(query): take chart time bounds from the temporal x-axis only - #46345
sadpandajoe wants to merge 9 commits into
Conversation
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>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #46345 +/- ##
==========================================
- Coverage 86.23% 85.94% -0.30%
==========================================
Files 3146 3128 -18
Lines 220018 218233 -1785
Branches 52110 51250 -860
==========================================
- Hits 189725 187551 -2174
- Misses 26823 27213 +390
+ Partials 3470 3469 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
# Conflicts: # tests/unit_tests/common/test_time_range_utils.py
aminghadersohi
left a comment
There was a problem hiding this comment.
Time comparison on a temporal x-axis returns an empty series when the chart also filters another datetime column.
| else: | ||
| targets = temporal_filters |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The dashboard time range case holds at 16716e0; a time cross-filter target's drill still diverges from the chart, see the new comment at query_context_factory.py:534.
…ts it Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
aminghadersohi
left a comment
There was a problem hiding this comment.
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.
| base_axes: list[Column] = get_base_axis_columns( | ||
| getattr(query_object, "columns", None) | ||
| ) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The rows and totals queries agree at 16716e0; the time comparison still takes the groupby as the time column, see the new comment at helpers.py:3880.
rusackas
left a comment
There was a problem hiding this comment.
Thanks @sadpandajoe! I'm with @aminghadersohi on the open threads here, the cross-filter one especially. I'd love to see those sorted before this goes in, since it changes where dashboard time ranges land on a lot of charts.
…uery A time range replaces the TEMPORAL_RANGE filters on the chart's time column, taken from the same inputs in every consumer: - The chart's x-axis comes from the form data, not from any base-axis column, so a table, pivot table or box plot temporal groupby is not taken for it and its rows and totals query apply the range alike. - A Custom SQL x-axis also matches the datetime columns its expression reads. - A time x-axis cross-filter's range replaces ranges on the cross-filtered column only, not the target chart's x-axis. - Without a filter on the time column, the range fills the chart's unbounded time filter before a filter is added on a temporal x-axis. - Drill to Detail sends the chart's x-axis, so its rows keep the chart's other datetime ranges. - The semantic mapper selects the axis range with the dataset's datetime columns, as the query object factory does. - Time comparison queries replace or shift only the compared range and keep other datetime columns' ranges, as the main query applies them. Charts with no known time column still apply the range to every TEMPORAL_RANGE filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…racking Addresses the CodeQL py/polynomial-redos alert on _SQL_IDENTIFIER. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A time comparison chose the column to replace or shift by position: the first TEMPORAL_RANGE filter. With another datetime range listed before the x-axis range, a date-range comparison replaced that other range, and a relative comparison shifted it while the x-axis stayed bounded to the main period, so the comparison series was empty. The comparison column is chosen as the main query chooses it: a filter on the Time Column, the x-axis or a datetime column the x-axis reads, then the Time Column, then the filter carrying the time range. Without a known time column the first temporal filter is still used. Also ignore an x_axis key in a stored query dict, which clashed with the chart's x-axis from the form data, and test the bounded identifier regex. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aminghadersohi
left a comment
There was a problem hiding this comment.
The chart-query threads are addressed; Drill to Detail on a time cross-filter target and table time comparisons still bound a different column than the chart does.
| cross_filter_columns: set[str] = { | ||
| identifier | ||
| for filter_object in ( | ||
| extra_form_data.get("filters") or [] | ||
| if isinstance(extra_form_data, dict) | ||
| else [] | ||
| ) | ||
| if isinstance(filter_object, dict) | ||
| and filter_object.get("op") == "TEMPORAL_RANGE" | ||
| and filter_object.get("val") == query_object.time_range | ||
| for identifier in get_column_identifiers(filter_object.get("col")) | ||
| } |
There was a problem hiding this comment.
Drill to Detail sends no extra_form_data, so on a time cross-filter target whose x-axis is created_at this set is empty: the drill rewrites created_at to the bucket and keeps the saved ds range, while the chart does the reverse, so the detail rows differ or come back empty.
| base_axes = get_base_axis_columns(query_object.columns) | ||
| time_columns: set[str] = { | ||
| *get_column_identifiers(query_object.granularity), | ||
| *( | ||
| identifier | ||
| for axis in base_axes[:1] | ||
| for identifier in ( | ||
| *get_column_identifiers(axis), | ||
| *get_axis_temporal_columns(axis, get_temporal_column_names(self)), | ||
| ) | ||
| ), | ||
| } |
There was a problem hiding this comment.
A table sends its temporal groupby as BASE_AXIS with no x-axis, so with a saved created_at range and the dashboard range on ds, a relative comparison shifts created_at and leaves ds unshifted (a date-range one drops ds), while the main query bounded ds.
| from another datetime column. Charts with no known time column still apply a | ||
| time range to every `TEMPORAL_RANGE` filter. A Custom SQL x-axis without a |
There was a problem hiding this comment.
Nit: Without a known time column the range lands only on the unbounded TEMPORAL_RANGE filters when one exists, so a categorical x-axis keeps a saved created_at range; every filter takes it only when none is unbounded.
| from another datetime column. Charts with no known time column still apply a | |
| time range to every `TEMPORAL_RANGE` filter. A Custom SQL x-axis without a | |
| from another datetime column. Charts with no known time column apply a time | |
| range to their unbounded `TEMPORAL_RANGE` filters, or else to every one. A | |
| Custom SQL x-axis without a |
| names: list[str] = [ | ||
| next(group for group in match.groups() if group) | ||
| for match in _SQL_IDENTIFIER.finditer(expression) | ||
| ] |
There was a problem hiding this comment.
Nit: Every bare word counts, so on a dataset with a datetime column named date, cast(created_at as date) reads as both created_at and date: a saved date range is replaced by the dashboard range and no x-axis filter is added.
SUMMARY
Supersedes #39683.
Closes: #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:
get_time_range_from_filters(called byQueryObjectFactoryto computefrom_dttm/to_dttm) compared eachTEMPORAL_RANGEfilter'scolonly with the x-axis label and otherwise fell back to the firstTEMPORAL_RANGEfilter (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 whosecolis an adhoc column, never matched.QueryContextFactory._apply_filterswrotetime_rangeinto everyTEMPORAL_RANGEfilter (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:
TEMPORAL_RANGEfilters by label and then by expression, accepting plain strings and adhoc column dicts.TEMPORAL_RANGEfilters 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, everyTEMPORAL_RANGEfilter takes the range as before._apply_granularityunchanged, so the dashboard Time Column override and the x-axis label it preserves keep working.TESTING INSTRUCTIONS
order_dateandship_date.order_date, a time filterorder_date= No filter, and a second adhoc filtership_datewith a range that excludes some rows. Save it to a dashboard with a Time Range native filter and pick a range.order_dateby the dashboard range and still applies the chart's ownship_daterange. Before this change both columns were bounded by the dashboard range.order_datetime filter from the chart: the query still boundsorder_dateby the dashboard range and keepsship_date's own range. Before this changeorder_datewas unbounded andship_datetook the dashboard range.ship_dateon a dataset whose main datetime column isorder_date, with aship_datetime filter: bothorder_dateandship_dateare bounded by the dashboard range.status) with only a time filter onorder_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 noTEMPORAL_RANGEon 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 intest_query_context_factory.pyand the expected bar counts of five setups intest_drill_by_time_column.py(the February order shipped in July now stays outside the chart's ownship_daterange). The drill-by contract, that drill rows equal the clicked bar, still holds.Notes:
_apply_granularityis left untouched.TEMPORAL_RANGEfilter (_get_temporal_column_for_filter); this PR does not change that path.🤖 Generated with Claude Code