Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions docs/docs/using-superset/exploring-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ grouping columns). Adding the temporal dimension again reuses the saved choice.
Unknown columns or grain values are not silently normalized. Ordinary dataset
charts and raw-record queries are unchanged.

This applies to requests rebuilt by the updated frontend in Explore and
dashboards; it does not rewrite saved charts. Direct API requests and saved
chart-data GET paths that bypass rebuilding remain subject to strict validation,
so old stored query contexts can still fail. Updated frontend assets are required.
The semantic-view backend also ignores a grain on a known non-temporal field
when that field has no advertised grain variants, including in direct API
requests and stored query contexts. A chart-wide grain is ignored when no
temporal field is grouped. A grain that the view does not support on a date or
time field is still refused, and unknown fields or types retain validation.
A legacy time column that points to a non-temporal field with no grain variants
is still refused when a grain and time bounds would filter that field; Superset
does not redirect those bounds to another column.

Dashboards use current dataset metadata to classify selected columns. Semantic
Table and Interactive Table charts with a saved or dashboard-supplied grain wait
for pending metadata requests before querying, including when added in edit mode
Expand Down
98 changes: 85 additions & 13 deletions superset/semantic_layers/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,17 @@ def _map_query_object(
metrics = [all_metrics[metric] for metric in (query_object.metrics or [])]

grain = _convert_time_grain(query_object.extras.get("time_grain_sqla"))
time_axis_column = _get_grain_time_axis_column(query_object, all_dimensions)
time_axis_column: str | None = _get_grain_time_axis_column(
query_object, all_dimensions, view_dimensions
)
grouping_grains: dict[str, Grain | None] = (
{time_axis_column: grain}
if time_axis_column is not None and time_axis_column in normalized_columns
else {}
)
grouping_grains.update(_explicit_column_grains(query_object, dimension_names))
grouping_grains.update(
_explicit_column_grains(query_object, all_dimensions, view_dimensions)
)
grouping_dimensions: dict[str, Dimension] = resolve_dimensions(
view_dimensions, usage=DimensionUsage.GROUP, grouping_grains=grouping_grains
)
Expand Down Expand Up @@ -1390,10 +1394,41 @@ def _get_group_limit_filters(
return filters if filters else None


def _is_temporal_dimension(dimension: Dimension) -> bool:
"""Use the grain axis's Arrow date, time and timestamp classification."""
return (
pa.types.is_timestamp(dimension.type)
or pa.types.is_date(dimension.type)
or pa.types.is_time(dimension.type)
)


def _is_non_temporal_column(
column: str | AdhocColumn,
all_dimensions: dict[str, Dimension],
view_dimensions: tuple[Dimension, ...],
) -> bool:
"""A known non-temporal field is dormant only without advertised grains."""
name: str = _normalize_column(column, set(all_dimensions))
dimension: Dimension | None = all_dimensions.get(name)
return (
dimension is not None
and isinstance(dimension.type, pa.DataType)
and not pa.types.is_null(dimension.type)
and not _is_temporal_dimension(dimension)
and not any(
variant.name == name and variant.grain for variant in view_dimensions
)
)


def _explicit_column_grains(
query_object: ValidatedQueryObject, dimension_names: set[str]
query_object: ValidatedQueryObject,
all_dimensions: dict[str, Dimension],
view_dimensions: tuple[Dimension, ...],
) -> dict[str, Grain | None]:
"""Read grains explicitly attached to physical BASE_AXIS references."""
"""Read applicable grains attached to physical BASE_AXIS references."""
dimension_names: set[str] = set(all_dimensions)
return {
_normalize_column(column, dimension_names): _convert_time_grain(
column["timeGrain"]
Expand All @@ -1403,12 +1438,14 @@ def _explicit_column_grains(
and column.get("columnType") == "BASE_AXIS"
and column.get("isColumnReference") is True
and column.get("timeGrain")
and not _is_non_temporal_column(column, all_dimensions, view_dimensions)
}


def _get_grain_time_axis_column(
query_object: ValidatedQueryObject,
all_dimensions: dict[str, Dimension],
view_dimensions: tuple[Dimension, ...],
) -> str | None:
"""
Determine which selected column is the time-axis (the one a time grain
Expand All @@ -1426,7 +1463,13 @@ def _get_grain_time_axis_column(
temporal column happens to be iterated first.
"""
if query_object.granularity:
return query_object.granularity
return (
None
if _is_non_temporal_column(
query_object.granularity, all_dimensions, view_dimensions
)
else query_object.granularity
)

dimension_names = set(all_dimensions.keys())
temporal_columns: list[str] = []
Expand All @@ -1438,11 +1481,7 @@ def _get_grain_time_axis_column(
dim = all_dimensions.get(name)
if dim is None:
continue
if (
pa.types.is_timestamp(dim.type)
or pa.types.is_date(dim.type)
or pa.types.is_time(dim.type)
):
if _is_temporal_dimension(dim):
temporal_columns.append(name)
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved

if len(temporal_columns) == 1:
Expand Down Expand Up @@ -1635,9 +1674,42 @@ def _validate_granularity(
"The time column must be defined in the Semantic View dimensions."
)

column_grains = _explicit_column_grains(query_object, dimension_names)
if time_grain := query_object.extras.get("time_grain_sqla"):
time_column = _get_grain_time_axis_column(query_object, all_dimensions)
column_grains: dict[str, Grain | None] = _explicit_column_grains(
query_object, all_dimensions, view_dimensions
)
time_grain: str | None = query_object.extras.get("time_grain_sqla")
if legacy_time_column and _is_non_temporal_column(
legacy_time_column, all_dimensions, view_dimensions
):
Comment thread
aminghadersohi marked this conversation as resolved.
Resolved
if (
query_object.from_dttm is not None or query_object.to_dttm is not None
) and (
time_grain
or any(
isinstance(column, dict)
and column.get("timeGrain")
and _normalize_column(column, dimension_names) == legacy_time_column
for column in query_object.columns
)
):
raise QueryObjectValidationError(
"The time grain is not supported for the time column in the "
"Semantic View."
)
time_grain = None
if time_grain and (
(
legacy_time_column
and pa.types.is_null(all_dimensions[legacy_time_column].type)
)
or not all(
_is_non_temporal_column(column, all_dimensions, view_dimensions)
for column in query_object.columns
)
):
time_column: str | None = _get_grain_time_axis_column(
query_object, all_dimensions, view_dimensions
)
if not time_column and not column_grains:
raise SemanticQueryInputError(
"A time column must be specified when a time grain is provided."
Expand Down
Loading
Loading