betodealmeida commented on code in PR #37816: URL: https://github.com/apache/superset/pull/37816#discussion_r2913387467
########## superset/semantic_layers/models.py: ########## @@ -0,0 +1,391 @@ +# 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. + +"""Semantic layer models.""" + +from __future__ import annotations + +import uuid +from collections.abc import Hashable +from dataclasses import dataclass +from functools import cached_property +from typing import Any, TYPE_CHECKING + +import pyarrow as pa +from flask_appbuilder import Model +from sqlalchemy import Column, ForeignKey, Identity, Integer, String, Text +from sqlalchemy.orm import relationship +from sqlalchemy_utils import UUIDType +from sqlalchemy_utils.types.json import JSONType +from superset_core.semantic_layers.layer import ( + SemanticLayer as SemanticLayerABC, +) +from superset_core.semantic_layers.view import ( + SemanticView as SemanticViewABC, +) + +from superset.common.query_object import QueryObject +from superset.explorables.base import TimeGrainDict +from superset.extensions import encrypted_field_factory +from superset.models.helpers import AuditMixinNullable, QueryResult +from superset.semantic_layers.mapper import get_results +from superset.semantic_layers.registry import registry +from superset.utils import json +from superset.utils.core import GenericDataType + +if TYPE_CHECKING: + from superset.superset_typing import ExplorableData, QueryObjectDict + + +def get_column_type(semantic_type: pa.DataType) -> GenericDataType: + """ + Map Arrow data types to generic data types. + """ + if pa.types.is_date(semantic_type) or pa.types.is_timestamp(semantic_type): + return GenericDataType.TEMPORAL + if pa.types.is_time(semantic_type): + return GenericDataType.TEMPORAL + if ( + pa.types.is_integer(semantic_type) + or pa.types.is_floating(semantic_type) + or pa.types.is_decimal(semantic_type) + or pa.types.is_duration(semantic_type) + ): + return GenericDataType.NUMERIC + if pa.types.is_boolean(semantic_type): + return GenericDataType.BOOLEAN + return GenericDataType.STRING + + +@dataclass(frozen=True) +class MetricMetadata: + metric_name: str + expression: str + verbose_name: str | None = None + description: str | None = None + d3format: str | None = None + currency: dict[str, Any] | None = None + warning_text: str | None = None + certified_by: str | None = None + certification_details: str | None = None + + +@dataclass(frozen=True) +class ColumnMetadata: + column_name: str + type: str + is_dttm: bool + verbose_name: str | None = None + description: str | None = None + groupby: bool = True + filterable: bool = True + expression: str | None = None + python_date_format: str | None = None + advanced_data_type: str | None = None + extra: str | None = None + + +class SemanticLayer(AuditMixinNullable, Model): + """ + Semantic layer model. + + A semantic layer provides an abstraction over data sources, + allowing users to query data through a semantic interface. + """ + + __tablename__ = "semantic_layers" + + uuid = Column(UUIDType(binary=True), primary_key=True, default=uuid.uuid4) + + # Core fields + name = Column(String(250), nullable=False) + description = Column(Text, nullable=True) + type = Column(String(250), nullable=False) # snowflake, etc + + configuration = Column(encrypted_field_factory.create(JSONType), default="{}") + cache_timeout = Column(Integer, nullable=True) + + # Semantic views relationship + semantic_views: list[SemanticView] = relationship( + "SemanticView", + back_populates="semantic_layer", + cascade="all, delete-orphan", + passive_deletes=True, + ) + + def __repr__(self) -> str: + return self.name or str(self.uuid) + + @cached_property + def implementation( + self, + ) -> SemanticLayerABC[Any, SemanticViewABC]: + """ + Return semantic layer implementation. + """ + # TODO (betodealmeida): + # return extension_manager.get_contribution("semanticLayers", self.type) + class_ = registry[self.type] + return class_.from_configuration(json.loads(self.configuration)) Review Comment: > which means the ORM will typically return a Python object (dict) rather than a JSON string; This is not true. -- 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]
