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 20:34:12 UTC

(superset) 03/05: WIP

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 e6beebc88cdfa4121a07b9c11e012b186025915b
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Wed Nov 5 13:19:12 2025 -0500

    WIP
---
 superset/semantic_layers/models.py                 | 37 ++++++++++++++++++++++
 .../semantic_layers/snowflake/semantic_layer.py    | 13 ++++----
 .../semantic_layers/snowflake/semantic_view.py     |  5 +--
 superset/semantic_layers/types.py                  | 28 ++++++++++++++--
 .../snowflake/test_semantic_view.py                | 10 +++---
 5 files changed, 77 insertions(+), 16 deletions(-)

diff --git a/superset/semantic_layers/models.py b/superset/semantic_layers/models.py
index c80902eb3a..622133737d 100644
--- a/superset/semantic_layers/models.py
+++ b/superset/semantic_layers/models.py
@@ -20,6 +20,8 @@
 from __future__ import annotations
 
 import uuid
+from importlib.metadata import entry_points
+from typing import Any
 
 from flask_appbuilder import Model
 from sqlalchemy import Column, ForeignKey, Integer, String, Text
@@ -27,6 +29,10 @@ from sqlalchemy.orm import relationship
 from sqlalchemy_utils import UUIDType
 
 from superset.models.helpers import AuditMixinNullable
+from superset.semantic_layers.types import (
+    SemanticLayerImplementation,
+    SemanticViewImplementation,
+)
 from superset.utils import core as utils
 
 
@@ -47,6 +53,7 @@ class SemanticLayer(AuditMixinNullable, Model):
     description = Column(Text, nullable=True)
     type = Column(String(250), nullable=False)
 
+    # XXX: encrypt at rest
     configuration = Column(utils.MediumText(), default="{}")
     cache_timeout = Column(Integer, nullable=True)
 
@@ -61,6 +68,25 @@ class SemanticLayer(AuditMixinNullable, Model):
     def __repr__(self) -> str:
         return self.name or str(self.uuid)
 
+    @property
+    def implementation(self) -> SemanticLayerImplementation[Any]:
+        """
+        Return semantic layer implementation.
+        """
+        entry_point = next(
+            iter(
+                entry_points(
+                    group="superset.semantic_layers",
+                    name=self.type,
+                )
+            )
+        )
+        implementation_class = entry_point.load()
+        schema = implementation_class.configuration_schema()
+        configuration = schema.model_validate(self.configuration)
+
+        return implementation_class(configuration)
+
 
 class SemanticView(AuditMixinNullable, Model):
     """
@@ -76,6 +102,7 @@ class SemanticView(AuditMixinNullable, Model):
     # Core fields
     name = Column(String(250), nullable=False)
 
+    # XXX: encrypt at rest
     configuration = Column(utils.MediumText(), default="{}")
     cache_timeout = Column(Integer, nullable=True)
 
@@ -93,3 +120,13 @@ class SemanticView(AuditMixinNullable, Model):
 
     def __repr__(self) -> str:
         return self.name or str(self.uuid)
+
+    @property
+    def implementation(self) -> SemanticViewImplementation[Any]:
+        """
+        Return semantic view implementation.
+        """
+        return self.semantic_layer.implementation.get_semantic_view(
+            self.name,
+            self.configuration,
+        )
diff --git a/superset/semantic_layers/snowflake/semantic_layer.py b/superset/semantic_layers/snowflake/semantic_layer.py
index 5b111d3c0d..abba331f26 100644
--- a/superset/semantic_layers/snowflake/semantic_layer.py
+++ b/superset/semantic_layers/snowflake/semantic_layer.py
@@ -20,18 +20,19 @@ from __future__ import annotations
 from textwrap import dedent
 from typing import Any, Literal, TYPE_CHECKING
 
-from pydantic import BaseModel, create_model, Field
+from pydantic import create_model, Field
 from snowflake.connector import connect
 from snowflake.connector.connection import SnowflakeConnection
 
 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
 
 if TYPE_CHECKING:
     from superset.semantic_layers.snowflake.semantic_view import SnowflakeSemanticView
 
 
-class SnowflakeSemanticLayer:
+class SnowflakeSemanticLayer(SemanticLayerImplementation[SnowflakeConfiguration]):
     id = "snowflake"
     name = "Snowflake Semantic Layer"
     description = "Connect to semantic views stored in Snowflake."
@@ -193,7 +194,7 @@ class SnowflakeSemanticLayer:
 
     def get_semantic_views(
         self,
-        runtime_configuration: BaseModel,
+        runtime_configuration: dict[str, Any],
     ) -> set[SnowflakeSemanticView]:
         """
         Get the semantic views available in the semantic layer.
@@ -204,9 +205,7 @@ class SnowflakeSemanticLayer:
         )
 
         # create a new configuration with the runtime parameters
-        configuration = self.configuration.model_copy(
-            update=runtime_configuration.model_dump()
-        )
+        configuration = self.configuration.model_copy(update=runtime_configuration)
 
         connection_parameters = get_connection_parameters(configuration)
         with connect(**connection_parameters) as connection:
@@ -218,6 +217,6 @@ class SnowflakeSemanticLayer:
                 """
             ).strip()
             return {
-                SnowflakeSemanticView(configuration, row[0])
+                SnowflakeSemanticView(row[0], configuration)
                 for row in cursor.execute(query)
             }
diff --git a/superset/semantic_layers/snowflake/semantic_view.py b/superset/semantic_layers/snowflake/semantic_view.py
index e75fbe2153..926c953e8d 100644
--- a/superset/semantic_layers/snowflake/semantic_view.py
+++ b/superset/semantic_layers/snowflake/semantic_view.py
@@ -56,6 +56,7 @@ from superset.semantic_layers.types import (
     SemanticRequest,
     SemanticResult,
     SemanticViewFeature,
+    SemanticViewImplementation,
     STRING,
     TIME,
     Type,
@@ -64,7 +65,7 @@ from superset.semantic_layers.types import (
 REQUEST_TYPE = "snowflake"
 
 
-class SnowflakeSemanticView:
+class SnowflakeSemanticView(SemanticViewImplementation[SnowflakeConfiguration]):
     features = frozenset(
         {
             SemanticViewFeature.ADHOC_EXPRESSIONS_IN_ORDERBY,
@@ -73,7 +74,7 @@ class SnowflakeSemanticView:
         }
     )
 
-    def __init__(self, configuration: SnowflakeConfiguration, name: str):
+    def __init__(self, name: str, configuration: SnowflakeConfiguration):
         self.configuration = configuration
         self.name = name
 
diff --git a/superset/semantic_layers/types.py b/superset/semantic_layers/types.py
index b84a1658f2..3dc3cbd979 100644
--- a/superset/semantic_layers/types.py
+++ b/superset/semantic_layers/types.py
@@ -300,6 +300,11 @@ class SemanticLayerImplementation(Protocol[ConfigT]):
 
     configuration_schema: type[ConfigT]
 
+    def __init__(self, configuration: ConfigT) -> None:
+        """
+        Initialize the semantic layer with the given configuration.
+        """
+
     @classmethod
     def get_configuration_schema(
         cls,
@@ -353,21 +358,38 @@ class SemanticLayerImplementation(Protocol[ConfigT]):
 
     def get_semantic_views(
         self,
-        runtime_configuration: BaseModel,
-    ) -> set[SemanticViewImplementation]:
+        runtime_configuration: dict[str, Any],
+    ) -> set[SemanticViewImplementation[Any]]:
         """
         Get the semantic views available in the semantic layer.
+
+        The runtime configuration can provide information like a given project or
+        schema, used to restrict the semantic views returned.
+        """
+
+    def get_semantic_view(
+        self,
+        name: str,
+        additional_configuration: dict[str, Any],
+    ) -> SemanticViewImplementation[Any]:
+        """
+        Get a specific semantic view by its name and additional configuration.
         """
 
 
 @runtime_checkable
-class SemanticViewImplementation(Protocol):
+class SemanticViewImplementation(Protocol[ConfigT]):
     """
     A protocol for semantic views.
     """
 
     features: frozenset[SemanticViewFeature]
 
+    def __init__(self, name: str, configuration: ConfigT):
+        """
+        Initialize the semantic view with the given name and configuration.
+        """
+
     def uid(self) -> str:
         """
         Returns a unique identifier for the semantic view.
diff --git a/tests/unit_tests/semantic_layers/snowflake/test_semantic_view.py b/tests/unit_tests/semantic_layers/snowflake/test_semantic_view.py
index e78937a7e9..0f076da05a 100644
--- a/tests/unit_tests/semantic_layers/snowflake/test_semantic_view.py
+++ b/tests/unit_tests/semantic_layers/snowflake/test_semantic_view.py
@@ -210,7 +210,7 @@ def semantic_view(
         metric_rows,
     ]
 
-    return SnowflakeSemanticView(configuration, "TPCDS_SEMANTIC_VIEW_SM")
+    return SnowflakeSemanticView("TPCDS_SEMANTIC_VIEW_SM", configuration)
 
 
 def test_get_dimensions(
@@ -1039,9 +1039,11 @@ def test_get_query_with_group_limit(
     group_limit = GroupLimit(
         dimensions=[dimension_map[name] for name in group_limit_config["dimensions"]],
         top=group_limit_config["top"],
-        metric=metric_map[group_limit_config["metric"]]
-        if group_limit_config["metric"]
-        else None,
+        metric=(
+            metric_map[group_limit_config["metric"]]
+            if group_limit_config["metric"]
+            else None
+        ),
         direction=group_limit_config["direction"],
         group_others=group_limit_config["group_others"],
         filters=group_limit_config["filters"],