codeant-ai-for-open-source[bot] commented on code in PR #41611:
URL: https://github.com/apache/superset/pull/41611#discussion_r3537927079
##########
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:
**Suggestion:** Add an explicit type annotation for this inferred column
type variable. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The variable is a new local scalar whose type can be explicitly annotated as
str, but the modified code leaves it inferred, which violates the custom
type-hint requirement.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=502666f911eb4bdf8939d5ae63d119c9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=502666f911eb4bdf8939d5ae63d119c9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/utils/response_utils.py
**Line:** 192:192
**Comment:**
*Custom Rule: Add an explicit type annotation for this inferred column
type variable.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=2ab1db598b0e721360a30b4d2b6b127374a78580ae7955aa4a14b0929f9c7296&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=2ab1db598b0e721360a30b4d2b6b127374a78580ae7955aa4a14b0929f9c7296&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type annotation for this intermediate rows
collection to satisfy the type-hint requirement for relevant variables.
[custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The variable is introduced without an explicit type annotation even though
its type can be clearly annotated as a list of row dictionaries, which matches
the custom rule requiring type hints on relevant variables in new or modified
Python code.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=881ed06fbc9845eca6d95ae0c9189f41&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=881ed06fbc9845eca6d95ae0c9189f41&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/utils/response_utils.py
**Line:** 185:185
**Comment:**
*Custom Rule: Add an explicit type annotation for this intermediate
rows collection to satisfy the type-hint requirement for relevant variables.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=ca825769aae5822eac3d100cf5bb008ef2af88da19302d00e25091b4aed4174c&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=ca825769aae5822eac3d100cf5bb008ef2af88da19302d00e25091b4aed4174c&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add a boolean type annotation for this sampling flag
variable. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This is a new boolean local variable that can be explicitly annotated as
bool, but it is left untyped in the modified code, so it fits the type-hint
rule for relevant variables.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=f9f760beda454f8e8fe45e533c7006ba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=f9f760beda454f8e8fe45e533c7006ba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/utils/response_utils.py
**Line:** 186:186
**Comment:**
*Custom Rule: Add a boolean type annotation for this sampling flag
variable.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=a10786178ed94cdb695e28a3b4bc8689b5bacfcaa23d321b45464d0c31ede0f6&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=a10786178ed94cdb695e28a3b4bc8689b5bacfcaa23d321b45464d0c31ede0f6&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type annotation for this list variable to
satisfy the requirement for annotating relevant variables. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The list variable `compatible` is newly introduced without an explicit type
annotation in a Python file, which matches the custom rule requiring type hints
on relevant variables that can be annotated.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=8a9451a3053e409db7e3c69ef4d5d0af&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=8a9451a3053e409db7e3c69ef4d5d0af&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/semantic_layer/tool/get_compatible_metrics.py
**Line:** 138:152
**Comment:**
*Custom Rule: Add an explicit type annotation for this list variable to
satisfy the requirement for annotating relevant variables.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=0b3126743a66e961d0861f49a348d743feddb59c613a935409ff5a906c150fe1&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=0b3126743a66e961d0861f49a348d743feddb59c613a935409ff5a906c150fe1&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type annotation for this SQLAlchemy query
variable to satisfy the type-hint requirement for relevant local variables.
[custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This is a new local variable in modified Python code that could be
explicitly typed, so it fits the rule requiring type hints for relevant
annotatable variables.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=c7d8607336b743cda5306f57c8f85230&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=c7d8607336b743cda5306f57c8f85230&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/semantic_layer/tool/list_metrics.py
**Line:** 101:104
**Comment:**
*Custom Rule: Add an explicit type annotation for this SQLAlchemy query
variable to satisfy the type-hint requirement for relevant local variables.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=2ca4f8a9de3dda95e424adf1de4f6741bd07172887e64bc605d8760017b4f775&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=2ca4f8a9de3dda95e424adf1de4f6741bd07172887e64bc605d8760017b4f775&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add a type annotation to this local variable so the
collected external metrics list is explicitly typed. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
The local variable `external` is assigned a list result and is not
explicitly annotated, so it is a valid type-hint omission under the rule.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=c0241051b7db4b58a72256b8a52a0ed0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=c0241051b7db4b58a72256b8a52a0ed0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/mcp_service/semantic_layer/tool/list_metrics.py
**Line:** 294:294
**Comment:**
*Custom Rule: Add a type annotation to this local variable so the
collected external metrics list is explicitly typed.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=acc12f4e37fc96fa593d48305a7691ee8bd44fbdbbe48679bfd7711bb9db0b05&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=acc12f4e37fc96fa593d48305a7691ee8bd44fbdbbe48679bfd7711bb9db0b05&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type annotation for this local dataset
variable to comply with required Python type hints. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This new local variable is untyped even though its value is a list of
dictionaries and can clearly be annotated. That matches the rule requiring type
hints for new Python variables that can be annotated.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d323ce3396d5416ebf1ea00d1294b9eb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d323ce3396d5416ebf1ea00d1294b9eb&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/mcp_service/utils/test_response_utils.py
**Line:** 50:50
**Comment:**
*Custom Rule: Add an explicit type annotation for this local dataset
variable to comply with required Python type hints.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=3c1336c876197e6b96211c0b40c29a4c964f85c6da073c4991a743af1e0088e7&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=3c1336c876197e6b96211c0b40c29a4c964f85c6da073c4991a743af1e0088e7&reaction=dislike'>👎</a>
##########
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"}]
Review Comment:
**Suggestion:** Add an explicit type annotation for this local dataset
variable to satisfy the enforced type-hint rule. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This is a newly added untyped local variable in Python code, and its
structure is obvious enough to annotate. The rule explicitly requires type
hints for such variables.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4df7cf580a954fd390c0ebe953e76d5e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4df7cf580a954fd390c0ebe953e76d5e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/mcp_service/utils/test_response_utils.py
**Line:** 57:57
**Comment:**
*Custom Rule: Add an explicit type annotation for this local dataset
variable to satisfy the enforced type-hint rule.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=22af1bd6b2e5bbb5b9ff73e31cb3aba26ef068e654ecb3563f27613be9add953&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=22af1bd6b2e5bbb5b9ff73e31cb3aba26ef068e654ecb3563f27613be9add953&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type hint for this list-comprehension-backed
local dataset variable. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This new local variable is untyped even though the list-comprehension result
is easy to annotate. The code therefore violates the Python type-hint
requirement.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=7a8cc60d57044480a009f209b02f5245&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=7a8cc60d57044480a009f209b02f5245&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/mcp_service/utils/test_response_utils.py
**Line:** 71:71
**Comment:**
*Custom Rule: Add an explicit type hint for this
list-comprehension-backed local dataset variable.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=c02553009cd718f90c41c6df093c4ea54345975b6e9a12631102ecd3ff6b6962&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=c02553009cd718f90c41c6df093c4ea54345975b6e9a12631102ecd3ff6b6962&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add an explicit type annotation for this local variable to
comply with mandatory Python typing in new code. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This newly introduced local variable is untyped and has a clear
list-of-dicts shape that can be annotated, so it falls under the required
Python type-hint rule.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b11ee727b25a4201a6f0328cc1ea3c31&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b11ee727b25a4201a6f0328cc1ea3c31&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/mcp_service/utils/test_response_utils.py
**Line:** 97:97
**Comment:**
*Custom Rule: Add an explicit type annotation for this local variable
to comply with mandatory Python typing in new code.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=4f8aab9959a8c4205536395fa4767ee5e33cb86a9ebc2cccebd10b7635dbb344&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=4f8aab9959a8c4205536395fa4767ee5e33cb86a9ebc2cccebd10b7635dbb344&reaction=dislike'>👎</a>
##########
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:
**Suggestion:** Add a concrete type annotation to this local variable rather
than leaving it untyped. [custom_rule]
**Severity Level:** Minor ⚠️
<details>
<summary><b>Why it matters? 🤔 </b></summary>
This local dataset variable is untyped in newly added Python code and can be
annotated as a list of dicts. That is a direct violation of the type-hint rule.
</details>
<details>
<summary><b>Rule source 📖 </b></summary>
.cursor/rules/dev-standard.mdc (line 28)
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b848a57056914ccf8e333d791e2db792&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b848a57056914ccf8e333d791e2db792&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** tests/unit_tests/mcp_service/utils/test_response_utils.py
**Line:** 64:64
**Comment:**
*Custom Rule: Add a concrete type annotation to this local variable
rather than leaving it untyped.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=ff01a2944080b4225cfe74761759b795ba0aa219d431d3db86ff24320249e910&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41611&comment_hash=ff01a2944080b4225cfe74761759b795ba0aa219d431d3db86ff24320249e910&reaction=dislike'>👎</a>
--
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]