gabotorresruiz commented on code in PR #44454: URL: https://github.com/apache/superset/pull/44454#discussion_r4251072797
########## 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: Also changed since the commit I approved, in the same window: this was keyed on `dimension.id` with a `previous_id != dimension.id` comparison, and it is now the whole `Dimension`. Since `Dimension` is a frozen dataclass with `compare=False` on only `verbose_name` and `metadata`, two entries sharing an `id` but differing in `definition`, `description` or `grain` now raise the conflict where before they did not. Stricter, and probably what you want, but it widens when that user-facing message fires so I wanted to name it. Was that intentional? ########## 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: Flagging something I think a merge resolution ate rather than a deliberate change. At `231cb9e7`, the commit I approved, `mapper.py` carried 11 references to this member: a `resolve_dimensions(view_dimensions, usage=DimensionUsage.TIME_BOUND)` call, a `time_dimensions` parameter threaded through the time-filter helpers, and an `all_dimensions if time_dimensions is None else time_dimensions` branch feeding `_get_time_axis_column`. At the very next commit, the first master merge `e0087c9e`, that count goes to zero, and it has stayed zero through `2ca99162`. `TIME_BOUND` is still declared here, so nothing fails to import and CI stays green, but grepping the head for it outside this line returns nothing. It is dead code now, and the independent time-bound resolution path is gone with it. The description still promises that behaviour, that filters and time bounds independently use raw then finest known grain then lexical representation, so either the body or the code is out of date. All seven master merges on this branch are hand-resolved rather than mechanical, which is presumably where it went. Could you check whether anything else came off in them? Comparing the branch's own diff against its merge base at `231cb9e7` and at the head is what surfaced this one. -- 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]
