From 4a6eaa81b46d4d99baa6813aea8be17ba8549f86 Mon Sep 17 00:00:00 2001 From: Mayur <mayurnewase111@gmail.com> Date: Mon, 10 Oct 2022 07:38:19 +0530 Subject: [PATCH] revision 2 --- superset/connectors/sqla/models.py | 15 ++++++++------- superset/superset_typing.py | 4 ++-- superset/utils/core.py | 4 +++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 88e621156ccbb..43bbcee12563d 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -1315,6 +1315,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma columns_by_name: Dict[str, TableColumn] = { col.column_name: col for col in self.columns } + metrics_by_name: Dict[str, SqlMetric] = {m.metric_name: m for m in self.metrics} if not granularity and is_timeseries: @@ -1439,22 +1440,22 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma elif columns: for selected in columns: if is_adhoc_column(selected): - sql = selected.get("sqlExpression") or "" - column_label = selected.get("label") or "" + _sql = selected["sqlExpression"] + _column_label = selected["label"] elif isinstance(selected, str): - sql = selected - column_label = selected + _sql = selected + _column_label = selected selected = validate_adhoc_subquery( - sql, + _sql, self.database_id, self.schema, ) select_exprs.append( columns_by_name[selected].get_sqla_col() - if not is_adhoc_column(selected) and selected in columns_by_name + if isinstance(selected, str) and selected in columns_by_name else self.make_sqla_column_compatible( - literal_column(selected), column_label + literal_column(selected), _column_label ) ) metrics_exprs = [] diff --git a/superset/superset_typing.py b/superset/superset_typing.py index eeaca0dd511aa..8eaea54176ed3 100644 --- a/superset/superset_typing.py +++ b/superset/superset_typing.py @@ -53,8 +53,8 @@ class AdhocMetric(TypedDict, total=False): class AdhocColumn(TypedDict, total=False): hasCustomLabel: Optional[bool] - label: Optional[str] - sqlExpression: Optional[str] + label: str + sqlExpression: str columnType: Optional[Literal["BASE_AXIS", "SERIES"]] timeGrain: Optional[str] diff --git a/superset/utils/core.py b/superset/utils/core.py index 2786cfa27fea9..a893696e024f3 100644 --- a/superset/utils/core.py +++ b/superset/utils/core.py @@ -1271,7 +1271,9 @@ def is_adhoc_metric(metric: Metric) -> TypeGuard[AdhocMetric]: def is_adhoc_column(column: Column) -> TypeGuard[AdhocColumn]: - return isinstance(column, dict) + return isinstance(column, dict) and ({"label", "sqlExpression"}).issubset( + column.keys() + ) def get_base_axis_labels(columns: Optional[List[Column]]) -> Tuple[str, ...]: