mikebridge commented on code in PR #44454:
URL: https://github.com/apache/superset/pull/44454#discussion_r4213279974


##########
superset/datasource/api.py:
##########
@@ -594,14 +595,17 @@ def compatible(self, datasource_type: str, datasource_id: 
int) -> FlaskResponse:
         if (cached := cache_manager.data_cache.get(cache_key)) is not None:
             return self.response(200, result=cached)
 
-        result = {
-            "compatible_metrics": datasource.get_compatible_metrics(
-                selected_metrics, selected_dimensions
-            ),
-            "compatible_dimensions": datasource.get_compatible_dimensions(
-                selected_metrics, selected_dimensions
-            ),
-        }
+        try:
+            result: dict[str, list[str]] = {
+                "compatible_metrics": datasource.get_compatible_metrics(
+                    selected_metrics, selected_dimensions
+                ),
+                "compatible_dimensions": datasource.get_compatible_dimensions(
+                    selected_metrics, selected_dimensions
+                ),
+            }
+        except AmbiguousDimensionError as ex:
+            return self.response(400, message=str(ex))

Review Comment:
   The values endpoint had the same gap. 
[6ef20bf54b](https://github.com/apache/superset/commit/6ef20bf54b0eaf2d4f631e59552c810e05432236)
 catches `AmbiguousDimensionError` in the shared values response path and 
returns 400; the regression reproduced the prior 500 and verifies no provider 
values query or cache write occurs.



##########
superset/semantic_layers/dimension_resolution.py:
##########
@@ -0,0 +1,118 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Resolve immutable provider dimensions for each query usage."""
+
+from collections.abc import Iterable, Mapping
+from enum import Enum
+
+from flask_babel import gettext as _
+from superset_core.semantic_layers.types import Dimension, Grain
+
+from superset.exceptions import QueryObjectValidationError
+
+# Known grains rank finest to coarsest, ahead of unknown representations.
+_GRAIN_FINENESS: dict[str, int] = {
+    "PT1S": 0,
+    "PT1M": 1,
+    "PT1H": 2,
+    "P1D": 3,
+    "P1W": 4,
+    "P1M": 5,
+    "P3M": 6,
+    "P1Y": 7,
+}
+
+
+class DimensionUsage(Enum):
+    """Keep selection policy explicit at each host/provider boundary."""
+
+    METADATA = "metadata"
+    COMPATIBILITY = "compatibility"
+    FILTER = "filter"
+    TIME_BOUND = "time_bound"
+    GROUP = "group"
+    ORDER = "order"
+    SERIES_LIMIT = "series_limit"
+
+
+class AmbiguousDimensionError(QueryObjectValidationError):
+    """A host-detected catalog ambiguity safe to report to an authorized 
user."""
+
+
+def grain_preference(dimension: Dimension) -> tuple[int, int, str]:
+    """Prefer raw, then finest known grain, then lexical representation."""
+    grain: Grain | None = dimension.grain
+    if grain is None:
+        return (0, 0, "")
+    representation: str = grain.representation
+    return (
+        1,
+        _GRAIN_FINENESS.get(representation, len(_GRAIN_FINENESS)),
+        representation,
+    )
+
+
+def resolve_dimensions(
+    dimensions: Iterable[Dimension],
+    *,
+    usage: DimensionUsage,
+    grouping_grains: Mapping[str, Grain | None] | None = None,
+) -> dict[str, Dimension]:
+    """Resolve names to exact provider identities for one usage role.
+
+    Grouping, order and series limits share explicitly selected grouping 
grains.
+    Other roles use the raw/finest default, independently of grouping. Return 
the
+    original Dimension, retaining its id, grain and provider metadata for sort
+    operands and result-alias mapping. This function does not combine 
predicates.
+    Every grain is validated, including variants not selected for this usage.
+    """
+    selected: dict[str, Dimension] = {}
+    use_grouping: bool = usage in {
+        DimensionUsage.GROUP,
+        DimensionUsage.ORDER,
+        DimensionUsage.SERIES_LIMIT,
+    }
+    defaults: dict[str, Dimension] = {}
+    default_preferences: dict[str, tuple[int, int, str]] = {}
+    identities: dict[tuple[str, Grain | None], str] = {}
+    for dimension in dimensions:
+        key: tuple[str, Grain | None] = (dimension.name, dimension.grain)
+        previous_id: str | None = identities.get(key)
+        if previous_id is not None and previous_id != dimension.id:
+            raise AmbiguousDimensionError(
+                _(
+                    "Semantic dimension '%(name)s' has ambiguous variants for "
+                    "grain '%(grain)s'. Use one ID per name and grain.",
+                    name=dimension.name,
+                    grain=dimension.grain.representation if dimension.grain 
else "raw",
+                )
+            )
+        identities[key] = dimension.id

Review Comment:
   Agreed: ID equality alone allowed conflicting dimensions through. 
[6ef20bf54b](https://github.com/apache/superset/commit/6ef20bf54b0eaf2d4f631e59552c810e05432236)
 stores and compares the actual Dimension using core equality, with tests for 
conflicting type, definition and description in both orders across every usage; 
repeated equal dimensions remain accepted.



##########
superset/semantic_layers/dimension_resolution.py:
##########
@@ -0,0 +1,118 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Resolve immutable provider dimensions for each query usage."""
+
+from collections.abc import Iterable, Mapping
+from enum import Enum
+
+from flask_babel import gettext as _
+from superset_core.semantic_layers.types import Dimension, Grain
+
+from superset.exceptions import QueryObjectValidationError
+
+# Known grains rank finest to coarsest, ahead of unknown representations.
+_GRAIN_FINENESS: dict[str, int] = {
+    "PT1S": 0,
+    "PT1M": 1,
+    "PT1H": 2,
+    "P1D": 3,
+    "P1W": 4,
+    "P1M": 5,
+    "P3M": 6,
+    "P1Y": 7,
+}
+
+
+class DimensionUsage(Enum):
+    """Keep selection policy explicit at each host/provider boundary."""
+
+    METADATA = "metadata"
+    COMPATIBILITY = "compatibility"
+    FILTER = "filter"
+    TIME_BOUND = "time_bound"
+    GROUP = "group"
+    ORDER = "order"
+    SERIES_LIMIT = "series_limit"
+
+
+class AmbiguousDimensionError(QueryObjectValidationError):
+    """A host-detected catalog ambiguity safe to report to an authorized 
user."""
+
+
+def grain_preference(dimension: Dimension) -> tuple[int, int, str]:
+    """Prefer raw, then finest known grain, then lexical representation."""
+    grain: Grain | None = dimension.grain
+    if grain is None:
+        return (0, 0, "")
+    representation: str = grain.representation
+    return (
+        1,
+        _GRAIN_FINENESS.get(representation, len(_GRAIN_FINENESS)),

Review Comment:
   Both examples reproduce. This PR deliberately preserves the existing raw → 
finest-known → representation preference documented in UPDATING.md, rather than 
introducing general duration ordering; parsing custom durations would change 
that policy and also needs a defined comparison for calendar months/years 
versus fixed durations. I have left that policy unchanged in the follow-up.



##########
superset/semantic_layers/mapper.py:
##########
@@ -364,59 +371,59 @@ def map_query_object(query_object: ValidatedQueryObject) 
-> list[SemanticQuery]:
 
     grain = _convert_time_grain(query_object.extras.get("time_grain_sqla"))
     time_axis_column = _get_grain_time_axis_column(query_object, 
all_dimensions)
-    # A semantic view can expose multiple Dimension variants per name (one per
-    # supported time grain). Pick exactly one variant per selected column:
-    # for the time-axis column we honor the user's grain selection, falling
-    # back to the raw / no-grain variant when no exact match exists and then
-    # to any available variant so the axis is never silently dropped; for
-    # every other selected column we prefer the raw variant and otherwise
-    # take any available variant.
+    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_dimensions: dict[str, Dimension] = resolve_dimensions(
+        view_dimensions, usage=DimensionUsage.GROUP, 
grouping_grains=grouping_grains
+    )
     dimensions: list[Dimension] = []
-    seen_non_axis: dict[str, Dimension] = {}
-    axis_variants: list[Dimension] = []
-    axis_match: Dimension | None = None
-    for dimension in view_dimensions:
-        if dimension.name not in normalized_columns:
-            continue
-        if dimension.name == time_axis_column:
-            axis_variants.append(dimension)
-            if axis_match is None and dimension.grain == grain:
-                axis_match = dimension
-            continue
-        existing = seen_non_axis.get(dimension.name)
-        if existing is None or (existing.grain is not None and dimension.grain 
is None):
-            seen_non_axis[dimension.name] = dimension
-
-    if axis_match is not None:
-        dimensions.append(axis_match)
-    elif axis_variants:
-        # No variant matches the requested grain. Prefer the raw (grain=None)
-        # variant; otherwise pick a deterministic fallback so the axis stays
-        # on the query instead of being silently dropped.
-        raw_variant = next((v for v in axis_variants if v.grain is None), None)
-        dimensions.append(
-            raw_variant
-            if raw_variant is not None
-            else min(axis_variants, key=lambda v: v.grain.name if v.grain else 
"")
-        )
-    dimensions.extend(seen_non_axis.values())
-
-    order = _get_order_from_query_object(query_object, all_metrics, 
all_dimensions)
+    if (
+        time_axis_column in normalized_columns
+        and time_axis_column in grouping_dimensions
+    ):
+        dimensions.append(grouping_dimensions[time_axis_column])
+    dimensions.extend(
+        dimension
+        for name, dimension in grouping_dimensions.items()
+        if name in normalized_columns and name != time_axis_column
+    )
+    order: list[OrderTuple] = _get_order_from_query_object(
+        query_object,
+        all_metrics,
+        resolve_dimensions(
+            view_dimensions, usage=DimensionUsage.ORDER, 
grouping_grains=grouping_grains
+        ),
+    )
+    time_dimensions: dict[str, Dimension] = resolve_dimensions(
+        view_dimensions, usage=DimensionUsage.TIME_BOUND

Review Comment:
   Agreed: FILTER and TIME_BOUND resolve to the same default map here. 
[6ef20bf54b](https://github.com/apache/superset/commit/6ef20bf54b0eaf2d4f631e59552c810e05432236)
 removes the duplicate map and optional arguments while preserving the separate 
selected-grain maps for grouping, sorting and series limits; the 49 focused 
mapper cases pass before and after.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to