mikebridge commented on code in PR #44454: URL: https://github.com/apache/superset/pull/44454#discussion_r4252457328
########## 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" Review Comment: This was a deliberate follow-up in [6ef20bf54b](https://github.com/apache/superset/commit/6ef20bf54b0eaf2d4f631e59552c810e05432236), between the approved 231cb9e7 and the first master merge. It addresses [the earlier FILTER/TIME_BOUND discussion](https://github.com/apache/superset/pull/44454#discussion_r4212351733); [the reply there](https://github.com/apache/superset/pull/44454#discussion_r4213280415) records the shared-map change. Both roles choose the same raw/finest/lexical default, so time helpers receive that common FILTER map. Grouping, sorting and series limits still have separate selected-grain maps: time-bound identity remains independent of grouping. Existing permutation tests pin raw bounds with month grouping and finest-grain bounds when raw is absent. All 408 resolver/mapper tests and focused Python hooks pass at the current head; none of the 24 test functions added at the approved revision was lost. No restoration needed. History and tests checked with Codex; unchanged implementation retains prior independent Claude coverage. ########## 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], Dimension] = {} Review Comment: Yes, this was intentional in [6ef20bf54b](https://github.com/apache/superset/commit/6ef20bf54b0eaf2d4f631e59552c810e05432236), before the first master merge. It applies [your earlier whole-Dimension suggestion](https://github.com/apache/superset/pull/44454#discussion_r4212351704), with [the implementation reply](https://github.com/apache/superset/pull/44454#discussion_r4213280142) linking the commit. For a fixed name/grain key, matching IDs with conflicting type, definition or description are rejected, while repeated equal dimensions remain accepted. UPDATING.md documents this stricter contract; regression tests cover all resolver roles and both catalog orders. The current 408 resolver/mapper tests and focused Python hooks pass. No merge correction needed. Verified with Codex; unchanged implementation retains prior independent Claude coverage. -- 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]
