aminghadersohi commented on code in PR #41611: URL: https://github.com/apache/superset/pull/41611#discussion_r3538085820
########## tests/unit_tests/mcp_service/utils/test_response_utils.py: ########## @@ -0,0 +1,109 @@ +# 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. + +""" +Unit tests for MCP service response utilities. +""" + +from typing import Any + +from superset.mcp_service.utils.response_utils import ( + format_data_columns, + STATS_ROW_CAP, +) + + +class TestFormatDataColumns: + """Test format_data_columns function.""" + + def test_infers_numeric_type(self) -> None: + """Should infer numeric data_type from sample values.""" + data: list[dict[str, Any]] = [ + {"revenue": 100}, + {"revenue": 200}, + {"revenue": None}, + ] + columns = format_data_columns(data, ["revenue"]) + + assert len(columns) == 1 + assert columns[0].name == "revenue" + assert columns[0].data_type == "numeric" + assert columns[0].null_count == 1 + assert columns[0].unique_count == 2 + + def test_infers_boolean_type(self) -> None: + """Should infer boolean data_type from sample values.""" + data = [{"is_active": True}, {"is_active": False}] + columns = format_data_columns(data, ["is_active"]) + + assert columns[0].data_type == "boolean" + + def test_infers_string_type_by_default(self) -> None: + """Should default to string data_type when values aren't numeric/boolean.""" + data = [{"region": "west"}, {"region": "east"}] + columns = format_data_columns(data, ["region"]) + + assert columns[0].data_type == "string" + + def test_string_type_when_no_sample_values(self) -> None: + """Should default to string data_type when all sampled values are null.""" + data = [{"region": None}] + columns = format_data_columns(data, ["region"]) + + assert columns[0].data_type == "string" + + def test_sample_values_capped_at_three(self) -> None: + """Should only take the first 3 non-null values as samples.""" + data = [{"region": f"r{i}"} for i in range(10)] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[dict[str, Any]]` annotation to the test's `data` variable. ########## tests/unit_tests/mcp_service/utils/test_response_utils.py: ########## @@ -0,0 +1,109 @@ +# 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. + +""" +Unit tests for MCP service response utilities. +""" + +from typing import Any + +from superset.mcp_service.utils.response_utils import ( + format_data_columns, + STATS_ROW_CAP, +) + + +class TestFormatDataColumns: + """Test format_data_columns function.""" + + def test_infers_numeric_type(self) -> None: + """Should infer numeric data_type from sample values.""" + data: list[dict[str, Any]] = [ + {"revenue": 100}, + {"revenue": 200}, + {"revenue": None}, + ] + columns = format_data_columns(data, ["revenue"]) + + assert len(columns) == 1 + assert columns[0].name == "revenue" + assert columns[0].data_type == "numeric" + assert columns[0].null_count == 1 + assert columns[0].unique_count == 2 + + def test_infers_boolean_type(self) -> None: + """Should infer boolean data_type from sample values.""" + data = [{"is_active": True}, {"is_active": False}] + columns = format_data_columns(data, ["is_active"]) + + assert columns[0].data_type == "boolean" + + def test_infers_string_type_by_default(self) -> None: + """Should default to string data_type when values aren't numeric/boolean.""" + data = [{"region": "west"}, {"region": "east"}] + columns = format_data_columns(data, ["region"]) + + assert columns[0].data_type == "string" + + def test_string_type_when_no_sample_values(self) -> None: + """Should default to string data_type when all sampled values are null.""" + data = [{"region": None}] + columns = format_data_columns(data, ["region"]) + + assert columns[0].data_type == "string" + + def test_sample_values_capped_at_three(self) -> None: + """Should only take the first 3 non-null values as samples.""" + data = [{"region": f"r{i}"} for i in range(10)] + columns = format_data_columns(data, ["region"]) + + assert columns[0].sample_values == ["r0", "r1", "r2"] + + def test_null_and_unique_counts_reflect_full_small_dataset(self) -> None: + """Should count nulls/uniques across all rows when under the cap.""" + data: list[dict[str, Any]] = [ + {"region": "west"}, + {"region": "west"}, + {"region": "east"}, + {"region": None}, + ] + columns = format_data_columns(data, ["region"]) + + assert columns[0].null_count == 1 + assert columns[0].unique_count == 2 + assert columns[0].statistics is None + + def test_stats_marked_as_sampled_beyond_row_cap(self) -> None: + """Should mark statistics as sampled when data exceeds STATS_ROW_CAP. + + Regression test: null_count/unique_count are computed on a capped + sample for performance, but were previously returned as if they were + exact full-dataset totals with no indication of sampling. + """ + data = [{"id": i} for i in range(STATS_ROW_CAP + 10)] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[dict[str, Any]]` annotation to the test's `data` variable. ########## superset/mcp_service/semantic_layer/tool/list_metrics.py: ########## @@ -0,0 +1,337 @@ +# 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. + +"""MCP tool: list_metrics + +Unified metric discovery across built-in datasets and external semantic views. +""" + +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.daos.dataset import DatasetDAO +from superset.daos.semantic_layer import SemanticViewDAO +from superset.exceptions import SupersetSecurityException +from superset.extensions import db, event_logger +from superset.mcp_service.privacy import ( + DATA_MODEL_METADATA_ERROR_TYPE, + requires_data_model_metadata_access, + user_can_view_data_model_metadata, +) +from superset.mcp_service.semantic_layer.schemas import ( + DimensionInfo, + ListMetricsRequest, + MetricInfo, + MetricList, + SemanticLayerError, +) +from superset.semantic_layers.models import ColumnMetadata, SemanticView + +logger = logging.getLogger(__name__) + + +def _matches_search(text: str | None, search: str) -> bool: + if not text: + return False + return search.lower() in text.lower() + + +def _builtin_compatible_dims(dataset: Any) -> list[DimensionInfo]: + """Return groupby-enabled columns as compatible dimensions for a built-in metric.""" + return [ + DimensionInfo( + name=col.column_name, + verbose_name=col.verbose_name or None, + description=col.description or None, + type=col.type or None, + is_dttm=bool(col.is_dttm), + groupby=bool(col.groupby), + filterable=bool(col.filterable), + source="builtin", + ) + for col in dataset.columns + if col.groupby + ] + + +def _collect_builtin_metrics(request: ListMetricsRequest) -> list[MetricInfo]: + """Collect metrics from built-in SqlaTable datasets.""" + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + with event_logger.log_context(action="mcp.list_metrics.builtin_query"): + if request.dataset_id is not None: + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + dataset: SqlaTable | None = DatasetDAO.find_by_id( + request.dataset_id, + query_options=[ + subqueryload(SqlaTable.columns), + subqueryload(SqlaTable.metrics), + ], + ) + datasets: list[SqlaTable] = [dataset] if dataset else [] + else: + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + # Use _apply_base_filter with explicit eager loading to avoid + # N+1 queries when iterating dataset.metrics / dataset.columns. + query = db.session.query(SqlaTable).options( + subqueryload(SqlaTable.columns), + subqueryload(SqlaTable.metrics), + ) Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `Query` annotation to `query`. ########## superset/mcp_service/utils/response_utils.py: ########## @@ -161,3 +164,56 @@ def add_extracted_field( def build(self) -> Dict[str, str]: """Return the omission metadata dict.""" return dict(self._fields) + + +STATS_ROW_CAP: int = 5000 + + +def format_data_columns( + data: list[dict[str, Any]], raw_columns: list[str] +) -> list[DataColumn]: + """Build column metadata from query result data. + + Caps null_count/unique_count computation at STATS_ROW_CAP rows to avoid + O(rows*cols) overhead on large result sets. When the result exceeds the + cap, those counts are marked as sampled/approximate via ``statistics`` + instead of being reported as exact full-dataset totals. + """ + # Local import breaks the chart.schemas ↔ response_utils circular dependency. + from superset.mcp_service.chart.schemas import DataColumn # noqa: PLC0415 + + stats_rows = data[:STATS_ROW_CAP] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[dict[str, Any]]` annotation to `stats_rows`. ########## tests/unit_tests/mcp_service/utils/test_response_utils.py: ########## @@ -0,0 +1,109 @@ +# 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. + +""" +Unit tests for MCP service response utilities. +""" + +from typing import Any + +from superset.mcp_service.utils.response_utils import ( + format_data_columns, + STATS_ROW_CAP, +) + + +class TestFormatDataColumns: + """Test format_data_columns function.""" + + def test_infers_numeric_type(self) -> None: + """Should infer numeric data_type from sample values.""" + data: list[dict[str, Any]] = [ + {"revenue": 100}, + {"revenue": 200}, + {"revenue": None}, + ] + columns = format_data_columns(data, ["revenue"]) + + assert len(columns) == 1 + assert columns[0].name == "revenue" + assert columns[0].data_type == "numeric" + assert columns[0].null_count == 1 + assert columns[0].unique_count == 2 + + def test_infers_boolean_type(self) -> None: + """Should infer boolean data_type from sample values.""" + data = [{"is_active": True}, {"is_active": False}] + columns = format_data_columns(data, ["is_active"]) + + assert columns[0].data_type == "boolean" + + def test_infers_string_type_by_default(self) -> None: + """Should default to string data_type when values aren't numeric/boolean.""" + data = [{"region": "west"}, {"region": "east"}] + columns = format_data_columns(data, ["region"]) + + assert columns[0].data_type == "string" + + def test_string_type_when_no_sample_values(self) -> None: + """Should default to string data_type when all sampled values are null.""" + data = [{"region": None}] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[dict[str, Any]]` annotation to the test's `data` variable. ########## superset/mcp_service/semantic_layer/tool/list_metrics.py: ########## @@ -0,0 +1,337 @@ +# 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. + +"""MCP tool: list_metrics + +Unified metric discovery across built-in datasets and external semantic views. +""" + +import logging +from typing import Any + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.daos.dataset import DatasetDAO +from superset.daos.semantic_layer import SemanticViewDAO +from superset.exceptions import SupersetSecurityException +from superset.extensions import db, event_logger +from superset.mcp_service.privacy import ( + DATA_MODEL_METADATA_ERROR_TYPE, + requires_data_model_metadata_access, + user_can_view_data_model_metadata, +) +from superset.mcp_service.semantic_layer.schemas import ( + DimensionInfo, + ListMetricsRequest, + MetricInfo, + MetricList, + SemanticLayerError, +) +from superset.semantic_layers.models import ColumnMetadata, SemanticView + +logger = logging.getLogger(__name__) + + +def _matches_search(text: str | None, search: str) -> bool: + if not text: + return False + return search.lower() in text.lower() + + +def _builtin_compatible_dims(dataset: Any) -> list[DimensionInfo]: + """Return groupby-enabled columns as compatible dimensions for a built-in metric.""" + return [ + DimensionInfo( + name=col.column_name, + verbose_name=col.verbose_name or None, + description=col.description or None, + type=col.type or None, + is_dttm=bool(col.is_dttm), + groupby=bool(col.groupby), + filterable=bool(col.filterable), + source="builtin", + ) + for col in dataset.columns + if col.groupby + ] + + +def _collect_builtin_metrics(request: ListMetricsRequest) -> list[MetricInfo]: + """Collect metrics from built-in SqlaTable datasets.""" + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + with event_logger.log_context(action="mcp.list_metrics.builtin_query"): + if request.dataset_id is not None: + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + dataset: SqlaTable | None = DatasetDAO.find_by_id( + request.dataset_id, + query_options=[ + subqueryload(SqlaTable.columns), + subqueryload(SqlaTable.metrics), + ], + ) + datasets: list[SqlaTable] = [dataset] if dataset else [] + else: + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + + # Use _apply_base_filter with explicit eager loading to avoid + # N+1 queries when iterating dataset.metrics / dataset.columns. + query = db.session.query(SqlaTable).options( + subqueryload(SqlaTable.columns), + subqueryload(SqlaTable.metrics), + ) + datasets = DatasetDAO._apply_base_filter(query).all() + + results: list[MetricInfo] = [] + for dataset in datasets: + compat_dims = ( + _builtin_compatible_dims(dataset) + if request.include_compatible_dimensions + else [] + ) + for metric in dataset.metrics: + name = metric.metric_name or "" + desc = metric.description or "" + if request.search and not ( + _matches_search(name, request.search) + or _matches_search(desc, request.search) + ): + continue + results.append( + MetricInfo( + name=name, + verbose_name=metric.verbose_name or None, + description=desc or None, + expression=metric.expression or None, + d3format=metric.d3format or None, + warning_text=metric.warning_text or None, + source="builtin", + dataset_id=dataset.id, + dataset_name=dataset.table_name, + compatible_dimensions=compat_dims, + ) + ) + return results + + +async def _collect_external_metrics( + request: ListMetricsRequest, + ctx: Context, +) -> list[MetricInfo]: + """Collect metrics from external SemanticView models.""" + with event_logger.log_context(action="mcp.list_metrics.external_query"): + if request.view_id is not None: + view: SemanticView | None = SemanticViewDAO.find_by_id(request.view_id) + views: list[SemanticView] = [view] if view else [] + else: + # find_accessible filters at SQL level, avoiding a per-row + # Python permission check and the audit noise of raise_for_access. + views = SemanticViewDAO.find_accessible() + + await ctx.debug("Found %d semantic views to scan for metrics" % len(views)) + + # Explicit single-view lookups aren't pre-filtered like find_accessible(), + # so raise_for_access must run outside the broad except block below so + # access errors are never silently swallowed as "could not load metrics". + check_access = request.view_id is not None + + results: list[MetricInfo] = [] + for view in views: + if check_access: + view.raise_for_access() + try: + raw_metrics = view.metrics + all_cols: dict[str, ColumnMetadata] = { + col.column_name: col + for col in ( + view.columns if request.include_compatible_dimensions else [] + ) + } + for metric in raw_metrics: + name = metric.metric_name or "" + desc = metric.description or "" + if request.search and not ( + _matches_search(name, request.search) + or _matches_search(desc, request.search) + ): + continue + compat_dims: list[DimensionInfo] = [] + if request.include_compatible_dimensions: + # Unlike built-in SQL datasets, external views enforce + # per-metric compatibility, so dimensions must be resolved + # per metric rather than reused across all metrics. + compatible_names = view.get_compatible_dimensions([name], []) + compat_dims = [ + DimensionInfo( + name=dim_name, + verbose_name=all_cols[dim_name].verbose_name + if dim_name in all_cols + else None, + description=all_cols[dim_name].description + if dim_name in all_cols + else None, + type=all_cols[dim_name].type + if dim_name in all_cols + else None, + is_dttm=all_cols[dim_name].is_dttm + if dim_name in all_cols + else False, + groupby=all_cols[dim_name].groupby + if dim_name in all_cols + else True, + filterable=all_cols[dim_name].filterable + if dim_name in all_cols + else True, + source="external", + ) + for dim_name in compatible_names + ] + results.append( + MetricInfo( + name=name, + verbose_name=metric.verbose_name or None, + description=desc or None, + expression=metric.expression or None, + source="external", + view_id=view.id, + view_name=view.name, + compatible_dimensions=compat_dims, + ) + ) + except Exception as exc: # noqa: BLE001 + # External registry may be empty in OSS — degrade gracefully + await ctx.warning( + "Could not load metrics for view id=%s: %s" % (view.id, str(exc)) + ) + return results + + +@tool( + tags=["data", "semantic"], + class_permission_name="Dataset", + annotations=ToolAnnotations( + title="List metrics", + readOnlyHint=True, + destructiveHint=False, + ), +) +@requires_data_model_metadata_access +async def list_metrics( + request: ListMetricsRequest | None = None, + ctx: Context | None = None, +) -> MetricList | SemanticLayerError: + """List available metrics across built-in datasets and external semantic views. + + This is the primary entry point for semantic layer exploration. Returns a + unified list of metrics from all data sources the current user can access, + with compatible dimensions included inline. + + Workflow: + 1. list_metrics -> discover available metrics and their compatible dimensions + 2. get_table -> query data using chosen metrics and dimensions + + Use ``search`` to filter by metric name or description. Use ``dataset_id`` + or ``view_id`` to scope to a specific data source. + + Example: + ```json + {"search": "revenue", "include_compatible_dimensions": true, "page": 1} + ``` + """ + if ctx is None: + raise RuntimeError("FastMCP context is required for list_metrics") + + request = request or ListMetricsRequest() + + await ctx.info( + "Listing metrics: search=%s, dataset_id=%s, view_id=%s, page=%s" + % (request.search, request.dataset_id, request.view_id, request.page) + ) + + if not user_can_view_data_model_metadata(): + await ctx.warning("Metric listing blocked by data-model privacy controls") + return SemanticLayerError.create( + error="You don't have permission to access dataset details for your role.", + error_type=DATA_MODEL_METADATA_ERROR_TYPE, + ) + + if request.dataset_id is not None and request.view_id is not None: + return SemanticLayerError.create( + error="Provide only one of dataset_id or view_id to scope the search, not both.", + error_type="ValidationError", + ) + + try: + all_metrics: list[MetricInfo] = [] + + if request.view_id is None: + all_metrics.extend(_collect_builtin_metrics(request)) + await ctx.debug("Collected %d built-in metrics" % len(all_metrics)) + + if request.dataset_id is None: + external = await _collect_external_metrics(request, ctx) Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[MetricInfo]` annotation to `external`. ########## tests/unit_tests/mcp_service/utils/test_response_utils.py: ########## @@ -0,0 +1,109 @@ +# 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. + +""" +Unit tests for MCP service response utilities. +""" + +from typing import Any + +from superset.mcp_service.utils.response_utils import ( + format_data_columns, + STATS_ROW_CAP, +) + + +class TestFormatDataColumns: + """Test format_data_columns function.""" + + def test_infers_numeric_type(self) -> None: + """Should infer numeric data_type from sample values.""" + data: list[dict[str, Any]] = [ + {"revenue": 100}, + {"revenue": 200}, + {"revenue": None}, + ] + columns = format_data_columns(data, ["revenue"]) + + assert len(columns) == 1 + assert columns[0].name == "revenue" + assert columns[0].data_type == "numeric" + assert columns[0].null_count == 1 + assert columns[0].unique_count == 2 + + def test_infers_boolean_type(self) -> None: + """Should infer boolean data_type from sample values.""" + data = [{"is_active": True}, {"is_active": False}] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[dict[str, Any]]` annotation to the test's `data` variable. ########## superset/mcp_service/semantic_layer/tool/get_compatible_metrics.py: ########## @@ -0,0 +1,233 @@ +# 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. + +"""MCP tool: get_compatible_metrics + +Returns metrics compatible with the current dimension/metric selection. +""" + +import logging + +from fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.privacy import ( + DATA_MODEL_METADATA_ERROR_TYPE, + requires_data_model_metadata_access, + user_can_view_data_model_metadata, +) +from superset.mcp_service.semantic_layer.schemas import ( + CompatibleMetricsResponse, + GetCompatibleMetricsRequest, + MetricInfo, + SemanticLayerError, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["data", "semantic"], + class_permission_name="Dataset", + annotations=ToolAnnotations( + title="Get compatible metrics", + readOnlyHint=True, + destructiveHint=False, + ), +) +@requires_data_model_metadata_access +async def get_compatible_metrics( + request: GetCompatibleMetricsRequest, + ctx: Context, +) -> CompatibleMetricsResponse | SemanticLayerError: + """Return metrics compatible with the current dimension/metric selection. + + Used to progressively refine a query: given a set of already-selected + metrics and dimensions, returns the additional metrics that can be + combined without breaking the underlying semantic constraints. + + Provide exactly one of ``dataset_id`` (built-in) or ``view_id`` (external). + + For built-in datasets, all metrics from the dataset are considered + compatible (SQL GROUP BY imposes no metric-level constraints). + + For external semantic views, delegates to the view's + ``get_compatible_metrics`` implementation. + + Example: + ```json + { + "selected_metrics": [], + "selected_dimensions": ["region"], + "view_id": 5 + } + ``` + """ + await ctx.info( + "Getting compatible metrics: dataset_id=%s, view_id=%s, " + "metrics=%s, dims=%s" + % ( + request.dataset_id, + request.view_id, + request.selected_metrics, + request.selected_dimensions, + ) + ) + + if not user_can_view_data_model_metadata(): + return SemanticLayerError.create( + error="You don't have permission to access dataset details for your role.", + error_type=DATA_MODEL_METADATA_ERROR_TYPE, + ) + + if request.dataset_id is None and request.view_id is None: + return SemanticLayerError.create( + error="Provide either dataset_id (built-in) or view_id (external).", + error_type="ValidationError", + ) + if request.dataset_id is not None and request.view_id is not None: + return SemanticLayerError.create( + error="Provide only one of dataset_id or view_id, not both.", + error_type="ValidationError", + ) + + try: + # ------------------------------------------------------------------ + # Built-in dataset path + # ------------------------------------------------------------------ + if request.dataset_id is not None: + from sqlalchemy.orm import subqueryload + + from superset.connectors.sqla.models import SqlaTable + from superset.daos.dataset import DatasetDAO + + with event_logger.log_context(action="mcp.get_compatible_metrics.builtin"): + dataset: SqlaTable | None = DatasetDAO.find_by_id( + request.dataset_id, + query_options=[ + subqueryload(SqlaTable.columns), + subqueryload(SqlaTable.metrics), + ], + ) + + if dataset is None: + return SemanticLayerError.create( + error=f"No dataset found with id: {request.dataset_id}.", + error_type="NotFound", + ) + + # All metrics on a SQL dataset are always mutually compatible; + # exclude ones already selected so clients don't get duplicate + # suggestions for metrics they've already added. + selected_metrics: set[str] = set(request.selected_metrics) + compatible = [ + MetricInfo( + name=m.metric_name, + verbose_name=m.verbose_name or None, + description=m.description or None, + expression=m.expression or None, + d3format=m.d3format or None, + warning_text=m.warning_text or None, + source="builtin", + dataset_id=dataset.id, + dataset_name=dataset.table_name, + ) + for m in dataset.metrics + if m.metric_name not in selected_metrics + ] Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `list[MetricInfo]` annotation to `compatible`. ########## superset/mcp_service/utils/response_utils.py: ########## @@ -161,3 +164,56 @@ def add_extracted_field( def build(self) -> Dict[str, str]: """Return the omission metadata dict.""" return dict(self._fields) + + +STATS_ROW_CAP: int = 5000 + + +def format_data_columns( + data: list[dict[str, Any]], raw_columns: list[str] +) -> list[DataColumn]: + """Build column metadata from query result data. + + Caps null_count/unique_count computation at STATS_ROW_CAP rows to avoid + O(rows*cols) overhead on large result sets. When the result exceeds the + cap, those counts are marked as sampled/approximate via ``statistics`` + instead of being reported as exact full-dataset totals. + """ + # Local import breaks the chart.schemas ↔ response_utils circular dependency. + from superset.mcp_service.chart.schemas import DataColumn # noqa: PLC0415 + + stats_rows = data[:STATS_ROW_CAP] + is_sampled = len(data) > STATS_ROW_CAP + columns_meta: list[DataColumn] = [] + for col_name in raw_columns: + sample_values = [ + row.get(col_name) for row in data[:3] if row.get(col_name) is not None + ] + data_type = "string" Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `str` annotation to `data_type`. ########## superset/mcp_service/utils/response_utils.py: ########## @@ -161,3 +164,56 @@ def add_extracted_field( def build(self) -> Dict[str, str]: """Return the omission metadata dict.""" return dict(self._fields) + + +STATS_ROW_CAP: int = 5000 + + +def format_data_columns( + data: list[dict[str, Any]], raw_columns: list[str] +) -> list[DataColumn]: + """Build column metadata from query result data. + + Caps null_count/unique_count computation at STATS_ROW_CAP rows to avoid + O(rows*cols) overhead on large result sets. When the result exceeds the + cap, those counts are marked as sampled/approximate via ``statistics`` + instead of being reported as exact full-dataset totals. + """ + # Local import breaks the chart.schemas ↔ response_utils circular dependency. + from superset.mcp_service.chart.schemas import DataColumn # noqa: PLC0415 + + stats_rows = data[:STATS_ROW_CAP] + is_sampled = len(data) > STATS_ROW_CAP Review Comment: Fixed in 7e91cf2db9bb35980368241d5061a244127a64d6: added an explicit `bool` annotation to `is_sampled`. -- 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]
