This is an automated email from the ASF dual-hosted git repository. beto pushed a commit to branch explorable in repository https://gitbox.apache.org/repos/asf/superset.git
commit 6f6567d5c9b90c4212dce841269d175bafa1420c Author: Beto Dealmeida <[email protected]> AuthorDate: Mon Oct 20 18:38:30 2025 -0400 Working on mapper --- superset/semantic_layers/mapper.py | 60 ++++++++++++++++++++++++++++++++++ superset/semantic_layers/snowflake_.py | 3 ++ superset/semantic_layers/types.py | 15 +++++++++ 3 files changed, 78 insertions(+) diff --git a/superset/semantic_layers/mapper.py b/superset/semantic_layers/mapper.py new file mode 100644 index 0000000000..59d05d101f --- /dev/null +++ b/superset/semantic_layers/mapper.py @@ -0,0 +1,60 @@ +# 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. + +from superset.common.query_object import QueryObject +from superset.semantic_layers.types import ( + Dimension, + Filter, + GroupLimit, + Metric, + OrderDirection, + SemanticQuery, +) + + +def map_query_object(query_object: QueryObject) -> SemanticQuery: + """ + Convert a QueryObject into a SemanticQuery. + + This function maps the `QueryObject` into a query that is less visualization-centric + and more semantic layer-centric. This simplifies the process of adding new semantic + layers to Superset, by providing a domain-specific representation of queries. + """ + metrics: set[Metric] = set() + dimensions: set[Dimension] = set() + filters: set[Filter] = set() + order = None + + group_limit = GroupLimit( + dimensions=[], + top=query_object.series_limit, + metric=None, + direction=( + OrderDirection.DESC if query_object.order_desc else OrderDirection.ASC + ), + group_others=query_object.group_others_when_limit_reached, + ) + + return SemanticQuery( + metrics=metrics, + dimensions=dimensions, + filters=filters, + order=order, + limit=query_object.row_limit, + offset=query_object.row_offset, + group_limit=group_limit, + ) diff --git a/superset/semantic_layers/snowflake_.py b/superset/semantic_layers/snowflake_.py index e45a6a6ca8..c8155bf3ca 100644 --- a/superset/semantic_layers/snowflake_.py +++ b/superset/semantic_layers/snowflake_.py @@ -434,6 +434,9 @@ class SnowflakeSemanticView: self._quote = SnowflakeDialect().identifier_preparer.quote + self.dimensions = self.get_dimensions() + self.metrics = self.get_metrics() + def uid(self) -> str: return ".".join( self._quote(part) diff --git a/superset/semantic_layers/types.py b/superset/semantic_layers/types.py index 7e9194ca48..7772c68647 100644 --- a/superset/semantic_layers/types.py +++ b/superset/semantic_layers/types.py @@ -243,3 +243,18 @@ class SemanticResult: requests: list[SemanticRequest] results: DataFrame + + +@dataclass(frozen=True) +class SemanticQuery: + """ + Represents a semantic query. + """ + + metrics: list[Metric] + dimensions: list[Dimension] + filters: set[Filter | NativeFilter] | None = None + order: list[tuple[Metric | Dimension, OrderDirection]] | None = None + limit: int | None = None + offset: int | None = None + group_limit: GroupLimit | None = None
