Skip to content

Commit 301f0a4

Browse files
mikebridgehainenber
authored andcommitted
fix(semantic-layer): ignore inapplicable time grains (#46339)
1 parent d182748 commit 301f0a4

3 files changed

Lines changed: 534 additions & 38 deletions

File tree

‎docs/docs/using-superset/exploring-data.mdx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ grouping columns). Adding the temporal dimension again reuses the saved choice.
6868
Unknown columns or grain values are not silently normalized. Ordinary dataset
6969
charts and raw-record queries are unchanged.
7070

71-
This applies to requests rebuilt by the updated frontend in Explore and
72-
dashboards; it does not rewrite saved charts. Direct API requests and saved
73-
chart-data GET paths that bypass rebuilding remain subject to strict validation,
74-
so old stored query contexts can still fail. Updated frontend assets are required.
71+
The semantic-view backend also ignores a grain on a known non-temporal field
72+
when that field has no advertised grain variants, including in direct API
73+
requests and stored query contexts. A chart-wide grain is ignored when no
74+
temporal field is grouped. A grain that the view does not support on a date or
75+
time field is still refused, and unknown fields or types retain validation.
76+
A legacy time column that points to a non-temporal field with no grain variants
77+
is still refused when a grain and time bounds would filter that field; Superset
78+
does not redirect those bounds to another column.
79+
7580
Dashboards use current dataset metadata to classify selected columns. Semantic
7681
Table and Interactive Table charts with a saved or dashboard-supplied grain wait
7782
for pending metadata requests before querying, including when added in edit mode

‎superset/semantic_layers/mapper.py‎

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,17 @@ def _map_query_object(
587587
metrics = [all_metrics[metric] for metric in (query_object.metrics or [])]
588588

589589
grain = _convert_time_grain(query_object.extras.get("time_grain_sqla"))
590-
time_axis_column = _get_grain_time_axis_column(query_object, all_dimensions)
590+
time_axis_column: str | None = _get_grain_time_axis_column(
591+
query_object, all_dimensions, view_dimensions
592+
)
591593
grouping_grains: dict[str, Grain | None] = (
592594
{time_axis_column: grain}
593595
if time_axis_column is not None and time_axis_column in normalized_columns
594596
else {}
595597
)
596-
grouping_grains.update(_explicit_column_grains(query_object, dimension_names))
598+
grouping_grains.update(
599+
_explicit_column_grains(query_object, all_dimensions, view_dimensions)
600+
)
597601
grouping_dimensions: dict[str, Dimension] = resolve_dimensions(
598602
view_dimensions, usage=DimensionUsage.GROUP, grouping_grains=grouping_grains
599603
)
@@ -1391,10 +1395,41 @@ def _get_group_limit_filters(
13911395
return filters if filters else None
13921396

13931397

1398+
def _is_temporal_dimension(dimension: Dimension) -> bool:
1399+
"""Use the grain axis's Arrow date, time and timestamp classification."""
1400+
return (
1401+
pa.types.is_timestamp(dimension.type)
1402+
or pa.types.is_date(dimension.type)
1403+
or pa.types.is_time(dimension.type)
1404+
)
1405+
1406+
1407+
def _is_non_temporal_column(
1408+
column: str | AdhocColumn,
1409+
all_dimensions: dict[str, Dimension],
1410+
view_dimensions: tuple[Dimension, ...],
1411+
) -> bool:
1412+
"""A known non-temporal field is dormant only without advertised grains."""
1413+
name: str = _normalize_column(column, set(all_dimensions))
1414+
dimension: Dimension | None = all_dimensions.get(name)
1415+
return (
1416+
dimension is not None
1417+
and isinstance(dimension.type, pa.DataType)
1418+
and not pa.types.is_null(dimension.type)
1419+
and not _is_temporal_dimension(dimension)
1420+
and not any(
1421+
variant.name == name and variant.grain for variant in view_dimensions
1422+
)
1423+
)
1424+
1425+
13941426
def _explicit_column_grains(
1395-
query_object: ValidatedQueryObject, dimension_names: set[str]
1427+
query_object: ValidatedQueryObject,
1428+
all_dimensions: dict[str, Dimension],
1429+
view_dimensions: tuple[Dimension, ...],
13961430
) -> dict[str, Grain | None]:
1397-
"""Read grains explicitly attached to physical BASE_AXIS references."""
1431+
"""Read applicable grains attached to physical BASE_AXIS references."""
1432+
dimension_names: set[str] = set(all_dimensions)
13981433
return {
13991434
_normalize_column(column, dimension_names): _convert_time_grain(
14001435
column["timeGrain"]
@@ -1404,12 +1439,14 @@ def _explicit_column_grains(
14041439
and column.get("columnType") == "BASE_AXIS"
14051440
and column.get("isColumnReference") is True
14061441
and column.get("timeGrain")
1442+
and not _is_non_temporal_column(column, all_dimensions, view_dimensions)
14071443
}
14081444

14091445

14101446
def _get_grain_time_axis_column(
14111447
query_object: ValidatedQueryObject,
14121448
all_dimensions: dict[str, Dimension],
1449+
view_dimensions: tuple[Dimension, ...],
14131450
) -> str | None:
14141451
"""
14151452
Determine which selected column is the time-axis (the one a time grain
@@ -1427,7 +1464,13 @@ def _get_grain_time_axis_column(
14271464
temporal column happens to be iterated first.
14281465
"""
14291466
if query_object.granularity:
1430-
return query_object.granularity
1467+
return (
1468+
None
1469+
if _is_non_temporal_column(
1470+
query_object.granularity, all_dimensions, view_dimensions
1471+
)
1472+
else query_object.granularity
1473+
)
14311474

14321475
dimension_names = set(all_dimensions.keys())
14331476
temporal_columns: list[str] = []
@@ -1439,11 +1482,7 @@ def _get_grain_time_axis_column(
14391482
dim = all_dimensions.get(name)
14401483
if dim is None:
14411484
continue
1442-
if (
1443-
pa.types.is_timestamp(dim.type)
1444-
or pa.types.is_date(dim.type)
1445-
or pa.types.is_time(dim.type)
1446-
):
1485+
if _is_temporal_dimension(dim):
14471486
temporal_columns.append(name)
14481487

14491488
if len(temporal_columns) == 1:
@@ -1636,9 +1675,42 @@ def _validate_granularity(
16361675
"The time column must be defined in the Semantic View dimensions."
16371676
)
16381677

1639-
column_grains = _explicit_column_grains(query_object, dimension_names)
1640-
if time_grain := query_object.extras.get("time_grain_sqla"):
1641-
time_column = _get_grain_time_axis_column(query_object, all_dimensions)
1678+
column_grains: dict[str, Grain | None] = _explicit_column_grains(
1679+
query_object, all_dimensions, view_dimensions
1680+
)
1681+
time_grain: str | None = query_object.extras.get("time_grain_sqla")
1682+
if legacy_time_column and _is_non_temporal_column(
1683+
legacy_time_column, all_dimensions, view_dimensions
1684+
):
1685+
if (
1686+
query_object.from_dttm is not None or query_object.to_dttm is not None
1687+
) and (
1688+
time_grain
1689+
or any(
1690+
isinstance(column, dict)
1691+
and column.get("timeGrain")
1692+
and _normalize_column(column, dimension_names) == legacy_time_column
1693+
for column in query_object.columns
1694+
)
1695+
):
1696+
raise QueryObjectValidationError(
1697+
"The time grain is not supported for the time column in the "
1698+
"Semantic View."
1699+
)
1700+
time_grain = None
1701+
if time_grain and (
1702+
(
1703+
legacy_time_column
1704+
and pa.types.is_null(all_dimensions[legacy_time_column].type)
1705+
)
1706+
or not all(
1707+
_is_non_temporal_column(column, all_dimensions, view_dimensions)
1708+
for column in query_object.columns
1709+
)
1710+
):
1711+
time_column: str | None = _get_grain_time_axis_column(
1712+
query_object, all_dimensions, view_dimensions
1713+
)
16421714
if not time_column and not column_grains:
16431715
raise SemanticQueryInputError(
16441716
"A time column must be specified when a time grain is provided."

0 commit comments

Comments
 (0)