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


##########
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:
   `FILTER` and `TIME_BOUND` both take the non-grouping path with no 
`grouping_grains`, so these two resolve to the same mapping and the 
`all_dimensions if time_dimensions is None else time_dimensions` fallbacks 
below can never pick differently. Is the split here for the sorting and alias 
follow-ups, or could the roles stay inside the resolver until one actually 
diverges?



##########
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:
   Good to have the clear 400 here. `values_for_column` resolves through 
`_unique_dimensions` too, and `get_column_values` only catches `KeyError` and 
`NotImplementedError`, so the same ambiguous catalog answers 
`/column/<name>/values/` with a 500 `Fatal error` while the dashboard charts 
show the friendly message. Worth catching it there as well so the native filter 
picker degrades the same way?



##########
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:
   Small one, not a blocker: the guard keys on `.id` alone, so two variants 
that share a name, grain and id but differ in `definition` or `description` 
pass it, and the winner is then whichever the provider's set happens to yield 
first rather than a fixed choice. Comparing the whole `Dimension` closes that 
and still accepts the repeated identical object:
   
   ```suggestion
       identities: dict[tuple[str, Grain | None], Dimension] = {}
       for dimension in dimensions:
           key: tuple[str, Grain | None] = (dimension.name, dimension.grain)
           previous: Dimension | None = identities.get(key)
           if previous is not None and previous != dimension:
               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
   ```



##########
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:
   `Grains.get()` lets a provider register any valid ISO duration, and those 
all share this one unknown rank above `P1Y` and then sort lexically. A catalog 
that spells its grains `PT60M` and `PT24H` therefore defaults to `PT24H`, and a 
genuine `PT15M` variant loses to a year. Should unknown representations rank by 
their parsed duration instead?



-- 
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