Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions UPDATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ assists people when migrating to a new version.
`maxPendingConnections` (`MAX_PENDING_CONNECTIONS`) is recommended, see the
[websocket server README](superset-websocket/README.md).

- [#39681](https://github.com/apache/superset/issues/39681): Charts whose
x-axis is a temporal column take their time bounds only from a
`TEMPORAL_RANGE` filter on that x-axis or from the time range; a chart with
neither no longer borrows the range of another datetime column. A time range,
such as a dashboard Time Range filter, replaces only the `TEMPORAL_RANGE`
filter on the chart's time column or x-axis, and is added as a filter on a
temporal x-axis that has none; filters on other datetime columns keep their
own ranges. Charts with neither a time column nor a temporal x-axis still
apply the time range to every `TEMPORAL_RANGE` filter.

- Semantic-view column pickers only offer saved dimensions, including for older metadata without feature declarations. `ADHOC_COLUMN_EXPRESSIONS` remains importable but is reserved and grants no capability. Providers must use recognized SDK enum members; raw-string declarations are ignored. SQL dataset expression controls are unchanged.

- Semantic-layer connection updates that reuse masked credentials require a
Expand Down
87 changes: 73 additions & 14 deletions superset/common/query_context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -464,22 +471,74 @@ 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)
)

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.

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.

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.

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"]))
]
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved
if not targets and not isinstance(granularity, str):
Comment thread
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,
}
# Time comparison shifts only the first temporal range, so the
# x-axis range goes first.
query_object.filter.insert(0, axis_filter)
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved
targets = [axis_filter]
else:
targets = temporal_filters
Comment on lines +583 to +584

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.

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.

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.

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
19 changes: 17 additions & 2 deletions superset/common/query_object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
apply_max_row_limit,
DatasourceDict,
DatasourceType,
get_base_axis_columns,
QueryObjectFilterClause,
)

Expand Down Expand Up @@ -101,8 +102,19 @@ def create( # pylint: disable=too-many-arguments
preserve_null_row_limit=preserve_null_row_limit,
)

temporal_columns: set[str] | None = None
if (
time_range is None
and datasource_model_instance is not None
and get_base_axis_columns(kwargs.get("columns"))
):
temporal_columns = {
column.column_name
for column in datasource_model_instance.columns
if column.is_dttm
}
processed_time_range = self._process_time_range(
time_range, kwargs.get("filters"), kwargs.get("columns")
time_range, kwargs.get("filters"), kwargs.get("columns"), temporal_columns
)
from_dttm, to_dttm = get_since_until_from_time_range(
processed_time_range, time_shift, processed_extras
Expand Down Expand Up @@ -164,8 +176,11 @@ def _process_time_range(
time_range: str | None,
filters: list[QueryObjectFilterClause] | None = None,
columns: list[Column] | None = None,
temporal_columns: set[str] | None = None,
) -> str:
return get_time_range_from_filters(time_range, filters, columns)
return get_time_range_from_filters(
time_range, filters, columns, temporal_columns
)

# light version of the view.utils.core
# import view.utils require application context
Expand Down
43 changes: 34 additions & 9 deletions superset/common/utils/time_range_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.
from __future__ import annotations

from collections.abc import Mapping, Sequence
from collections.abc import Collection, Mapping, Sequence
from datetime import datetime
from typing import Any, cast

Expand All @@ -30,7 +30,7 @@
from superset.superset_typing import Column
from superset.utils.core import (
FilterOperator,
get_x_axis_label,
get_base_axis_columns,
)
from superset.utils.date_parser import get_since_until

Expand All @@ -39,8 +39,14 @@ def get_time_range_from_filters(
time_range: str | None,
filters: Sequence[Mapping[str, object]] | None,
columns: list[Column] | None,
temporal_columns: Collection[str] | None = None,
) -> str:
"""Select the original range expression without evaluating relative dates."""
"""Select the original range expression without evaluating relative dates.

A temporal x-axis only takes a range that targets it, by label first and
then by expression. Without an x-axis, or with an x-axis whose expression
is not one of ``temporal_columns``, the first temporal range is used.
"""
if time_range is not None:
return time_range
temporal_filters: list[Mapping[str, object]] = [
Expand All @@ -51,12 +57,31 @@ def get_time_range_from_filters(
]
if not temporal_filters:
return NO_TIME_RANGE
x_axis_label: str | None = get_x_axis_label(columns)
selected: Mapping[str, object] = next(
(filter_ for filter_ in temporal_filters if filter_.get("col") == x_axis_label),
temporal_filters[0],
)
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
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved
and cast(Mapping[str, object], base_axes[0]).get("sqlExpression")
not in temporal_columns
):
return cast(str, temporal_filters[0].get("val"))
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved
for identifier in get_column_identifiers(base_axes[0]):
for filter_ in temporal_filters:
if identifier in get_column_identifiers(filter_.get("col")):
return cast(str, filter_.get("val"))
return NO_TIME_RANGE


def get_column_identifiers(column: object) -> tuple[str, ...]:
"""Return the names a column is referenced by: its label, then expression."""
if isinstance(column, str):
return (column,)
if isinstance(column, Mapping):
return tuple(
value
for key in ("label", "sqlExpression")
if isinstance(value := column.get(key), str) and value
)
return ()


def get_since_until_from_time_range(
Expand Down
12 changes: 7 additions & 5 deletions tests/unit_tests/common/test_drill_by_time_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,15 @@ def _bar(rows: list[Any], month: str) -> int:
),
}
EXPECTED_BARS = {
"range_and_ship_filter": [0, 3, 2, 2],
"time_column_and_x_axis_no_filter": [0, 0, 2, 2],
"time_column_and_dashboard_x_axis_filter": [0, 0, 2, 2],
# The time range bounds order_date; ship_date keeps its own range, which drops
# the February order shipped in July.
"range_and_ship_filter": [0, 0, 2, 1],
"time_column_and_x_axis_no_filter": [0, 0, 2, 1],
"time_column_and_dashboard_x_axis_filter": [0, 0, 2, 1],
# Bars count ship_date months: the Time Column replaces the x-axis.
"time_column_overrides_x_axis": [0, 0, 3, 2],
"custom_sql_x_axis": [0, 0, 2, 2],
"custom_sql_x_axis_with_own_label": [0, 0, 2, 2],
"custom_sql_x_axis": [0, 0, 2, 1],
"custom_sql_x_axis_with_own_label": [0, 0, 2, 1],
}


Expand Down
Loading
Loading