Repository navigation
fix(query): take chart time bounds from the temporal x-axis only #46345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
4a7a222
08f7d2d
a9fabf5
8de5158
baa5ca5
c3d1f49
94b65ae
f45c0e6
16716e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,14 +24,21 @@ | |
| from superset.common.query_context import QueryContext | ||
| from superset.common.query_object import QueryObject | ||
| from superset.common.query_object_factory import QueryObjectFactory | ||
| from superset.common.utils.time_range_utils import get_column_identifiers | ||
| from superset.daos.chart import ChartDAO | ||
| from superset.daos.datasource import DatasourceDAO | ||
| from superset.explorables.base import Explorable | ||
| from superset.extensions import security_manager | ||
| from superset.models.slice import Slice | ||
| from superset.security.manager import STOCK_RAISE_FOR_ACCESS | ||
| from superset.superset_typing import Column | ||
| from superset.utils.core import DatasourceDict, DatasourceType, is_adhoc_column | ||
| from superset.utils.core import ( | ||
| DatasourceDict, | ||
| DatasourceType, | ||
| get_base_axis_columns, | ||
| is_adhoc_column, | ||
| QueryObjectFilterClause, | ||
| ) | ||
| from superset.utils.date_parser import is_no_time_range | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -175,7 +182,7 @@ def _process_query_object( | |
| query_object: QueryObject, | ||
| ) -> QueryObject: | ||
| self._apply_granularity(query_object, form_data, datasource) | ||
| self._apply_filters(query_object, form_data) | ||
| self._apply_filters(query_object, form_data, datasource) | ||
| self._add_tooltip_columns(query_object, form_data) | ||
| self._add_currency_column(query_object, form_data, datasource) | ||
| return query_object | ||
|
|
@@ -464,22 +471,72 @@ def _apply_granularity( # noqa: C901 | |
| ] | ||
|
|
||
| def _apply_filters( | ||
| self, query_object: QueryObject, form_data: dict[str, Any] | None = None | ||
| self, | ||
| query_object: QueryObject, | ||
| form_data: dict[str, Any] | None = None, | ||
| datasource: Explorable | None = None, | ||
| ) -> None: | ||
| """Apply the time range, retaining the source of dashboard overrides.""" | ||
| """Apply the time range, retaining the source of dashboard overrides. | ||
|
|
||
| The range replaces the ``TEMPORAL_RANGE`` filters on the time column | ||
| (granularity) or the x-axis, so other datetime columns keep their own | ||
| ranges. A temporal x-axis without such a filter gets one, unless a time | ||
| column already carries the range. When neither column is known, every | ||
| ``TEMPORAL_RANGE`` filter takes the range. | ||
| """ | ||
| extra_form_data: object = (form_data or {}).get("extra_form_data") | ||
| dashboard_time_override: bool = ( | ||
| isinstance(extra_form_data, dict) | ||
| and "time_range" in extra_form_data | ||
| and extra_form_data["time_range"] == query_object.time_range | ||
| ) | ||
| if query_object.time_range: | ||
| for filter_object in query_object.filter: | ||
| if filter_object["op"] == "TEMPORAL_RANGE": | ||
| saved: object = filter_object.get("val") | ||
| was_unbounded: bool = saved is None or ( | ||
| isinstance(saved, str) and is_no_time_range(saved) | ||
| ) | ||
| filter_object["val"] = query_object.time_range | ||
| if dashboard_time_override and was_unbounded: | ||
| filter_object["isExtra"] = True | ||
| if not query_object.time_range: | ||
| return | ||
| granularity: object = getattr(query_object, "granularity", None) | ||
| base_axes: list[Column] = get_base_axis_columns( | ||
| getattr(query_object, "columns", None) | ||
| ) | ||
| time_columns: set[str] = { | ||
| *get_column_identifiers(granularity), | ||
| *(get_column_identifiers(base_axes[0]) if base_axes else ()), | ||
| } | ||
| temporal_filters: list[QueryObjectFilterClause] = [ | ||
| filter_object | ||
| for filter_object in query_object.filter | ||
| if filter_object["op"] == "TEMPORAL_RANGE" | ||
| ] | ||
| targets: list[QueryObjectFilterClause] = [ | ||
| filter_object | ||
| for filter_object in temporal_filters | ||
| if time_columns.intersection(get_column_identifiers(filter_object["col"])) | ||
| ] | ||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||
| if not targets and not isinstance(granularity, str): | ||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||
| axis_column: object = ( | ||
| cast(dict[str, object], base_axes[0]).get("sqlExpression") | ||
| if base_axes | ||
| else None | ||
| ) | ||
| if datasource is not None and axis_column in { | ||
| column["column_name"] | ||
| if isinstance(column, dict) | ||
| else column.column_name | ||
| for column in datasource.columns | ||
| if (column["is_dttm"] if isinstance(column, dict) else column.is_dttm) | ||
| }: | ||
| axis_filter: QueryObjectFilterClause = { | ||
| "col": cast(str, axis_column), | ||
| "op": "TEMPORAL_RANGE", | ||
| "val": None, | ||
| } | ||
| query_object.filter.append(axis_filter) | ||
|
aminghadersohi marked this conversation as resolved.
Resolved
|
||
| targets = [axis_filter] | ||
| else: | ||
| targets = temporal_filters | ||
|
Comment on lines
+583
to
+584
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
| for filter_object in targets: | ||
| saved: object = filter_object.get("val") | ||
| was_unbounded: bool = saved is None or ( | ||
| isinstance(saved, str) and is_no_time_range(saved) | ||
| ) | ||
| filter_object["val"] = query_object.time_range | ||
| if dashboard_time_override and was_unbounded: | ||
| filter_object["isExtra"] = True | ||
There was a problem hiding this comment.
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_AXISwith 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 boundsds: totals stop matching the rows.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.