You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2025/11/05 21:21:55 UTC

(superset) branch explorable updated: Working on exporable

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


The following commit(s) were added to refs/heads/explorable by this push:
     new b3f4ac59d3 Working on exporable
b3f4ac59d3 is described below

commit b3f4ac59d3c70f253560df451c1df9009e75b63a
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Wed Nov 5 16:21:35 2025 -0500

    Working on exporable
---
 superset/explorables/base.py                       | 33 ++++------------------
 superset/semantic_layers/models.py                 | 31 ++++++++++++++++++--
 .../semantic_layers/snowflake/semantic_layer.py    | 12 +++++---
 superset/semantic_layers/types.py                  | 15 +++++-----
 4 files changed, 51 insertions(+), 40 deletions(-)

diff --git a/superset/explorables/base.py b/superset/explorables/base.py
index c13b5ddc18..1057611513 100644
--- a/superset/explorables/base.py
+++ b/superset/explorables/base.py
@@ -27,6 +27,7 @@ from collections.abc import Hashable
 from datetime import datetime
 from typing import Any, Protocol, runtime_checkable
 
+from superset.common.query_object import QueryObject
 from superset.models.helpers import QueryResult
 from superset.superset_typing import QueryObjectDict
 
@@ -50,25 +51,15 @@ class Explorable(Protocol):
     # Core Query Interface
     # =========================================================================
 
-    def query(self, query_obj: QueryObjectDict) -> QueryResult:
+    def get_query_result(self, query_object: QueryObject) -> QueryResult:
         """
         Execute a query and return results.
 
         This is the primary method for data retrieval. It takes a query
-        object dictionary describing what data to fetch (columns, metrics,
-        filters, time range, etc.) and returns a QueryResult containing
-        a pandas DataFrame with the results.
-
-        :param query_obj: Dictionary describing the query with keys like:
-            - columns: list of column names to select
-            - metrics: list of metrics to compute
-            - filter: list of filter clauses
-            - from_dttm/to_dttm: time range
-            - granularity: time column for grouping
-            - groupby: columns to group by
-            - orderby: ordering specification
-            - row_limit/row_offset: pagination
-            - extras: additional parameters
+        object describing what data to fetch (columns, metrics, filters, time range,
+        etc.) and returns a QueryResult containing a pandas DataFrame with the results.
+
+        :param query_obj: QueryObject describing the query
 
         :return: QueryResult containing:
             - df: pandas DataFrame with query results
@@ -77,7 +68,6 @@ class Explorable(Protocol):
             - status: QueryStatus (SUCCESS/FAILED)
             - error_message: error details if query failed
         """
-        ...
 
     def get_query_str(self, query_obj: QueryObjectDict) -> str:
         """
@@ -90,7 +80,6 @@ class Explorable(Protocol):
         :param query_obj: Dictionary describing the query
         :return: String representation of the query (SQL, GraphQL, etc.)
         """
-        ...
 
     # =========================================================================
     # Identity & Metadata
@@ -109,7 +98,6 @@ class Explorable(Protocol):
 
         :return: Unique identifier string
         """
-        ...
 
     @property
     def type(self) -> str:
@@ -121,7 +109,6 @@ class Explorable(Protocol):
 
         :return: Type identifier string
         """
-        ...
 
     @property
     def columns(self) -> list[Any]:
@@ -137,7 +124,6 @@ class Explorable(Protocol):
 
         :return: List of column metadata objects
         """
-        ...
 
     @property
     def column_names(self) -> list[str]:
@@ -149,7 +135,6 @@ class Explorable(Protocol):
 
         :return: List of column name strings
         """
-        ...
 
     # =========================================================================
     # Caching
@@ -165,7 +150,6 @@ class Explorable(Protocol):
 
         :return: Cache timeout in seconds, or None for system default
         """
-        ...
 
     @property
     def changed_on(self) -> datetime | None:
@@ -177,7 +161,6 @@ class Explorable(Protocol):
 
         :return: Datetime of last modification, or None
         """
-        ...
 
     def get_extra_cache_keys(self, query_obj: QueryObjectDict) -> list[Hashable]:
         """
@@ -191,7 +174,6 @@ class Explorable(Protocol):
         :param query_obj: The query being executed
         :return: List of additional hashable values for cache key
         """
-        ...
 
     # =========================================================================
     # Security
@@ -208,7 +190,6 @@ class Explorable(Protocol):
 
         :return: Permission identifier string
         """
-        ...
 
     @property
     def schema_perm(self) -> str | None:
@@ -220,7 +201,6 @@ class Explorable(Protocol):
 
         :return: Schema permission string, or None
         """
-        ...
 
     # =========================================================================
     # Time/Date Handling
@@ -236,4 +216,3 @@ class Explorable(Protocol):
 
         :return: Timezone offset in seconds (0 for UTC)
         """
-        ...
diff --git a/superset/semantic_layers/models.py b/superset/semantic_layers/models.py
index b1460075de..e57b5ccb28 100644
--- a/superset/semantic_layers/models.py
+++ b/superset/semantic_layers/models.py
@@ -20,15 +20,20 @@
 from __future__ import annotations
 
 import uuid
+from datetime import timedelta
 from importlib.metadata import entry_points
+from timeit import default_timer
 from typing import Any
 
 from flask_appbuilder import Model
+from pandas import DataFrame
 from sqlalchemy import Column, ForeignKey, Integer, String, Text
 from sqlalchemy.orm import relationship
 from sqlalchemy_utils import UUIDType
 
-from superset.models.helpers import AuditMixinNullable
+from superset.common.query_object import QueryObject
+from superset.models.helpers import AuditMixinNullable, QueryResult
+from superset.semantic_layers.mapper import get_results
 from superset.semantic_layers.types import (
     SemanticLayerImplementation,
     SemanticViewImplementation,
@@ -69,7 +74,9 @@ class SemanticLayer(AuditMixinNullable, Model):
         return self.name or str(self.uuid)
 
     @property
-    def implementation(self) -> SemanticLayerImplementation[Any]:
+    def implementation(
+        self,
+    ) -> SemanticLayerImplementation[Any, SemanticViewImplementation]:
         """
         Return semantic layer implementation.
         """
@@ -138,3 +145,23 @@ class SemanticView(AuditMixinNullable, Model):
     # =========================================================================
     # Explorable protocol implementation
     # =========================================================================
+
+    def get_query_result(self, query_object: QueryObject) -> QueryResult:
+        start = default_timer()
+        try:
+            semantic_result = get_results(query_object)
+        except Exception as ex:  # pylint: disable=broad-except
+            return QueryResult(
+                df=DataFrame(),
+                query="",
+                duration=timedelta(seconds=default_timer() - start),
+                error_message=str(ex),
+            )
+
+        return QueryResult(
+            df=semantic_result.results,
+            query="\n\n".join(
+                request.definition for request in semantic_result.requests
+            ),
+            duration=timedelta(seconds=default_timer() - start),
+        )
diff --git a/superset/semantic_layers/snowflake/semantic_layer.py b/superset/semantic_layers/snowflake/semantic_layer.py
index 9b3591dc4d..8f99f6706f 100644
--- a/superset/semantic_layers/snowflake/semantic_layer.py
+++ b/superset/semantic_layers/snowflake/semantic_layer.py
@@ -18,7 +18,7 @@
 from __future__ import annotations
 
 from textwrap import dedent
-from typing import Any, Literal
+from typing import Any, Literal, TYPE_CHECKING
 
 from pydantic import create_model, Field
 from snowflake.connector import connect
@@ -28,11 +28,15 @@ from superset.semantic_layers.snowflake.schemas import SnowflakeConfiguration
 from superset.semantic_layers.snowflake.utils import get_connection_parameters
 from superset.semantic_layers.types import (
     SemanticLayerImplementation,
-    SemanticViewImplementation,
 )
 
+if TYPE_CHECKING:
+    from superset.semantic_layers.snowflake.semantic_view import SnowflakeSemanticView
 
-class SnowflakeSemanticLayer(SemanticLayerImplementation[SnowflakeConfiguration]):
+
+class SnowflakeSemanticLayer(
+    SemanticLayerImplementation[SnowflakeConfiguration, SnowflakeSemanticView]
+):
     id = "snowflake"
     name = "Snowflake Semantic Layer"
     description = "Connect to semantic views stored in Snowflake."
@@ -204,7 +208,7 @@ class SnowflakeSemanticLayer(SemanticLayerImplementation[SnowflakeConfiguration]
     def get_semantic_views(
         self,
         runtime_configuration: dict[str, Any],
-    ) -> set[SemanticViewImplementation]:
+    ) -> set[SnowflakeSemanticView]:
         """
         Get the semantic views available in the semantic layer.
         """
diff --git a/superset/semantic_layers/types.py b/superset/semantic_layers/types.py
index 54b555ad8e..8233c7b2bc 100644
--- a/superset/semantic_layers/types.py
+++ b/superset/semantic_layers/types.py
@@ -289,11 +289,12 @@ class SemanticViewFeature(enum.Enum):
     GROUP_OTHERS = "GROUP_OTHERS"
 
 
-LayerConfigT = TypeVar("LayerConfigT", bound=BaseModel)
+ConfigT = TypeVar("ConfigT", bound=BaseModel, contravariant=True)
+SemanticViewT = TypeVar("SemanticViewT", bound="SemanticViewImplementation")
 
 
 @runtime_checkable
-class SemanticLayerImplementation(Protocol[LayerConfigT]):
+class SemanticLayerImplementation(Protocol[ConfigT, SemanticViewT]):
     """
     A protocol for semantic layers.
     """
@@ -302,7 +303,7 @@ class SemanticLayerImplementation(Protocol[LayerConfigT]):
     def from_configuration(
         cls,
         configuration: dict[str, Any],
-    ) -> SemanticLayerImplementation[LayerConfigT]:
+    ) -> SemanticLayerImplementation[ConfigT, SemanticViewT]:
         """
         Create a semantic layer from its configuration.
         """
@@ -310,7 +311,7 @@ class SemanticLayerImplementation(Protocol[LayerConfigT]):
     @classmethod
     def get_configuration_schema(
         cls,
-        configuration: LayerConfigT | None = None,
+        configuration: ConfigT | None = None,
     ) -> dict[str, Any]:
         """
         Get the JSON schema for the configuration needed to add the semantic layer.
@@ -334,7 +335,7 @@ class SemanticLayerImplementation(Protocol[LayerConfigT]):
     @classmethod
     def get_runtime_schema(
         cls,
-        configuration: LayerConfigT,
+        configuration: ConfigT,
         runtime_data: dict[str, Any] | None = None,
     ) -> dict[str, Any]:
         """
@@ -361,7 +362,7 @@ class SemanticLayerImplementation(Protocol[LayerConfigT]):
     def get_semantic_views(
         self,
         runtime_configuration: dict[str, Any],
-    ) -> set[SemanticViewImplementation]:
+    ) -> set[SemanticViewT]:
         """
         Get the semantic views available in the semantic layer.
 
@@ -373,7 +374,7 @@ class SemanticLayerImplementation(Protocol[LayerConfigT]):
         self,
         name: str,
         additional_configuration: dict[str, Any],
-    ) -> SemanticViewImplementation:
+    ) -> SemanticViewT:
         """
         Get a specific semantic view by its name and additional configuration.
         """