codeant-ai-for-open-source[bot] commented on code in PR #39922:
URL: https://github.com/apache/superset/pull/39922#discussion_r3482787732


##########
superset/mcp_service/chart/plugins/table.py:
##########
@@ -0,0 +1,135 @@
+# 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.
+
+"""Table chart type plugin."""
+
+from __future__ import annotations
+
+from typing import Any
+
+from superset.mcp_service.chart.chart_utils import (
+    _summarize_filters,
+    _table_chart_what,
+    map_table_config,
+)
+from superset.mcp_service.chart.plugin import BaseChartPlugin
+from superset.mcp_service.chart.schemas import ColumnRef, TableChartConfig
+from superset.mcp_service.chart.validation.dataset_validator import 
DatasetValidator
+from superset.mcp_service.common.error_schemas import ChartGenerationError
+
+
+class TableChartPlugin(BaseChartPlugin):
+    """Plugin for table chart type."""
+
+    chart_type = "table"
+    display_name = "Table"
+    native_viz_types = {
+        "table": "Table",
+        "ag-grid-table": "Interactive Table",
+    }
+
+    def pre_validate(
+        self,
+        config: dict[str, Any],
+    ) -> ChartGenerationError | None:
+        columns = (
+            config.get("columns") or config.get("all_columns") or 
config.get("groupby")
+        )
+        if not columns:
+            return ChartGenerationError(
+                error_type="missing_columns",
+                message="Table chart missing required field: columns",
+                details=(
+                    "Table charts require a 'columns' array to specify which "
+                    "columns to display"
+                ),
+                suggestions=[
+                    "Add 'columns' field with array of column specifications",
+                    "Example: 'columns': [{'name': 'product'}, {'name': 
'sales', "
+                    "'aggregate': 'SUM'}]",
+                    "Each column can have optional 'aggregate' for metrics",
+                ],
+                error_code="MISSING_COLUMNS",
+            )
+
+        if not isinstance(columns, list):
+            return ChartGenerationError(
+                error_type="invalid_columns_format",
+                message="Columns must be a list",
+                details="The 'columns' field must be an array of column 
specifications",
+                suggestions=[
+                    "Ensure columns is an array: 'columns': [...]",
+                    "Each column should be an object with 'name' field",
+                ],
+                error_code="INVALID_COLUMNS_FORMAT",
+            )
+
+        return None
+
+    def extract_column_refs(self, config: Any) -> list[ColumnRef]:
+        if not isinstance(config, TableChartConfig):
+            return []
+        refs: list[ColumnRef] = list(config.columns)
+        if config.filters:
+            for f in config.filters:
+                refs.append(ColumnRef(name=f.column))
+        return refs

Review Comment:
   **Suggestion:** `sort_by` references are not included in extracted column 
refs, so dataset validation never checks whether sort targets exist. A typo in 
`sort_by` can pass validation and then fail later during query 
generation/execution when Superset receives an invalid `order_by_cols` entry. 
Include `sort_by` columns in `extract_column_refs` (for `SortByConfig` entries 
and string entries that represent real columns) so invalid sort targets are 
caught early. [incomplete implementation]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Dataset validation misses invalid sort_by targets for table charts.
   - ⚠️ Users see runtime sort/query failures instead of early errors.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Call the MCP chart tool `generate_explore_link` (entrypoint
   `superset/mcp_service/explore/tool/generate_explore_link.py:63`) with a
   `GenerateExploreLinkRequest` whose `config` is a `TableChartConfig` 
containing a `sort_by`
   entry that references a non-existent column (e.g. `"sort_by": ["Nonexistent 
Column"]`)
   while `columns` and `filters` reference valid dataset fields.
   
   2. Inside `generate_explore_link`, the request is mapped to form_data via
   `map_config_to_form_data` and then validated using `validate_and_compile`
   (`superset/mcp_service/explore/tool/generate_explore_link.py:254-276`), 
which calls
   `DatasetValidator.validate_against_dataset`
   (`superset/mcp_service/chart/compile.py:429-432` →
   `superset/mcp_service/chart/validation/dataset_validator.py:56-111`).
   
   3. `DatasetValidator.validate_against_dataset` collects column references via
   `_extract_column_references` (`dataset_validator.py:84` → 
`_extract_column_references` at
   `dataset_validator.py:8-33`), which delegates to the plugin for 
`chart_type="table"`; the
   registry returns `TableChartPlugin`
   (`superset/mcp_service/chart/plugins/__init__.py:37-44`), whose 
`extract_column_refs`
   implementation at `superset/mcp_service/chart/plugins/table.py:83-90` only 
includes
   `config.columns` and `config.filters`, and completely ignores 
`config.sort_by`
   (`TableChartConfig.sort_by` is defined at
   `superset/mcp_service/chart/schemas.py:1566-1572`).
   
   4. Because `sort_by` entries are never converted into `ColumnRef` objects, 
the invalid
   sort target is not part of `column_refs`, so `_validate_columns_exist`
   (`dataset_validator.py:114-174`) never checks it; validation succeeds, 
`order_by_cols` is
   still populated from `config.sort_by` in `map_table_config`
   (`superset/mcp_service/chart/chart_utils.py:39-47`), and only when Superset 
later executes
   the query for that explore URL or saved chart does the bad `order_by_cols` 
entry trigger a
   runtime sort/query failure instead of being caught during dataset validation.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=a8e92b636284443b8295719a300e976d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=a8e92b636284443b8295719a300e976d&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/chart/plugins/table.py
   **Line:** 86:90
   **Comment:**
        *Incomplete Implementation: `sort_by` references are not included in 
extracted column refs, so dataset validation never checks whether sort targets 
exist. A typo in `sort_by` can pass validation and then fail later during query 
generation/execution when Superset receives an invalid `order_by_cols` entry. 
Include `sort_by` columns in `extract_column_refs` (for `SortByConfig` entries 
and string entries that represent real columns) so invalid sort targets are 
caught early.
   
   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%2F39922&comment_hash=cf15f58e3299e85fb5f1693ea20f7eff1b1443dbd19501e2e60ec7b3fdd05db2&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39922&comment_hash=cf15f58e3299e85fb5f1693ea20f7eff1b1443dbd19501e2e60ec7b3fdd05db2&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]

Reply via email to