codeant-ai-for-open-source[bot] commented on code in PR #41860: URL: https://github.com/apache/superset/pull/41860#discussion_r3575455410
########## superset/mcp_service/chart/plugins/histogram.py: ########## @@ -0,0 +1,186 @@ +# 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. + +"""Histogram chart type plugin.""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any, ClassVar + +from superset.mcp_service.chart.chart_utils import ( + _summarize_filters, + map_histogram_config, +) +from superset.mcp_service.chart.plugin import BaseChartPlugin +from superset.mcp_service.chart.schemas import ColumnRef, HistogramChartConfig +from superset.mcp_service.chart.validation.dataset_validator import DatasetValidator +from superset.mcp_service.common.error_schemas import ChartGenerationError + + +class HistogramChartPlugin(BaseChartPlugin): + """Plugin for histogram chart type.""" + + chart_type = "histogram" + display_name = "Histogram" + native_viz_types: ClassVar[Mapping[str, str]] = { + "histogram_v2": "Histogram", + } + + def pre_validate( + self, + config: dict[str, Any], + ) -> ChartGenerationError | None: + if "column" not in config: + return ChartGenerationError( + error_type="missing_histogram_fields", + message="Histogram missing required field: 'column'", + details=( + "Histograms bin the values of a single numeric column " + "into frequency buckets" + ), + suggestions=[ + "Add 'column' field: {'name': 'numeric_column'}", + "Example: {'chart_type': 'histogram', " + "'column': {'name': 'trip_duration'}, 'bins': 10}", + ], + error_code="MISSING_HISTOGRAM_FIELDS", + ) + return None + + def extract_column_refs(self, config: Any) -> list[ColumnRef]: + if not isinstance(config, HistogramChartConfig): + return [] + refs: list[ColumnRef] = [config.column] + refs.extend(config.groupby or []) + if config.filters: + for f in config.filters: + refs.append(ColumnRef(name=f.column)) + return refs + + def to_form_data( + self, config: Any, dataset_id: int | str | None = None + ) -> dict[str, Any]: + return map_histogram_config(config) + + def generate_name(self, config: Any, dataset_name: str | None = None) -> str: + what = f"Distribution of {config.column.label or config.column.name}" + if config.groupby: + what += " by " + ", ".join(g.label or g.name for g in config.groupby) + context = _summarize_filters(config.filters) + return self._with_context(what, context) + + def post_map_validate( + self, + config: Any, + form_data: dict[str, Any], + dataset_id: int | str | None = None, + ) -> ChartGenerationError | None: + """Require a numeric binned column, mirroring the Explore UI. + + The frontend control panel restricts the histogram column to + ``GenericDataType.Numeric``; without this check an LLM picking a + text column passes pre-validation and only fails at query time. + """ + if not isinstance(config, HistogramChartConfig) or dataset_id is None: + return None + + dataset_context = DatasetValidator._get_dataset_context(dataset_id) + if dataset_context is None: + return None + + col_info = next( + ( + col + for col in dataset_context.available_columns + if col["name"].lower() == (config.column.name or "").lower() + ), + None, + ) + if col_info is None: + # Column existence is validated separately; don't double-report. + return None + + numeric_types = ["INTEGER", "FLOAT", "DOUBLE", "DECIMAL", "NUMERIC"] + if col_info.get("is_numeric", False) or ( + col_info.get("type", "").upper() in numeric_types + ): + return None Review Comment: **Suggestion:** The numeric-type fallback check is too narrow and will reject valid numeric columns when `is_numeric` is false but the backend reports types like `BIGINT`, `SMALLINT`, `INT`, `REAL`, or `DOUBLE PRECISION`. Expand the allowed SQL type set (or use a broader numeric-type predicate) so valid numeric histogram columns are not incorrectly blocked. [incorrect condition logic] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ❌ Histograms rejected for BIGINT or similar numeric types. - ⚠️ LLM agents blocked from plotting distributions on affected columns. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Prepare a dataset whose ORM column has a SQL type like `BIGINT` or `DOUBLE PRECISION` and where the ORM `is_numeric` flag is False; `build_dataset_context_from_orm()` (superset/mcp_service/chart/validation/dataset_validator.py:51-59) will record this in `DatasetContext.available_columns` as `{"name": <column_name>, "type": "BIGINT", "is_temporal": False, "is_numeric": False}`. 2. Call the MCP `generate_chart()` tool (superset/mcp_service/chart/tool/generate_chart.py:39-144) with `config.chart_type` set to `"histogram"` and `config.column` referencing that numeric-looking column by name, plus any valid `bins`/`groupby` values (the schema for `HistogramChartConfig` is at superset/mcp_service/chart/schemas.py:50-73). 3. During validation, `ValidationPipeline.validate_request_with_warnings()` (imported in superset/mcp_service/chart/validation/__init__.py:20 and invoked at generate_chart.py:181-183) obtains a `DatasetContext` via `DatasetValidator._get_dataset_context()` (superset/mcp_service/chart/validation/dataset_validator.py:249-259), then passes the typed `HistogramChartConfig` and `dataset_id` into `HistogramChartPlugin.post_map_validate()` (superset/mcp_service/chart/plugins/histogram.py:87-149). 4. In `post_map_validate()`, the plugin looks up the column metadata (lines 106-113) and evaluates the numeric guard at lines 118-122: `numeric_types = ["INTEGER", "FLOAT", "DOUBLE", "DECIMAL", "NUMERIC"]; if col_info.get("is_numeric", False) or (col_info.get("type", "").upper() in numeric_types): return None`. Because `is_numeric` is False and `type.upper()` is `"BIGINT"` (not in the allowlist), the check fails and the function returns a `ChartGenerationError` `"NON_NUMERIC_HISTOGRAM_COLUMN"` (lines 130-149), incorrectly rejecting a valid numeric histogram column purely due to the narrow type allowlist. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=97ee024776aa427bbf65450ba5ba4afb&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=97ee024776aa427bbf65450ba5ba4afb&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/histogram.py **Line:** 118:122 **Comment:** *Incorrect Condition Logic: The numeric-type fallback check is too narrow and will reject valid numeric columns when `is_numeric` is false but the backend reports types like `BIGINT`, `SMALLINT`, `INT`, `REAL`, or `DOUBLE PRECISION`. Expand the allowed SQL type set (or use a broader numeric-type predicate) so valid numeric histogram columns are not incorrectly blocked. 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%2F41860&comment_hash=9d5ebbd306c5e4d9c907c23373827c6413d13b5b5195f45d90081870e6b980d0&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41860&comment_hash=9d5ebbd306c5e4d9c907c23373827c6413d13b5b5195f45d90081870e6b980d0&reaction=dislike'>👎</a> ########## superset/mcp_service/chart/plugins/box_plot.py: ########## @@ -0,0 +1,145 @@ +# 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. + +"""Box plot chart type plugin.""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any, ClassVar + +from superset.mcp_service.chart.chart_utils import ( + _summarize_filters, + map_box_plot_config, +) +from superset.mcp_service.chart.plugin import BaseChartPlugin +from superset.mcp_service.chart.schemas import BoxPlotChartConfig, ColumnRef +from superset.mcp_service.chart.validation.dataset_validator import DatasetValidator +from superset.mcp_service.common.error_schemas import ChartGenerationError + + +class BoxPlotChartPlugin(BaseChartPlugin): + """Plugin for box plot chart type.""" + + chart_type = "box_plot" + display_name = "Box Plot" + native_viz_types: ClassVar[Mapping[str, str]] = { + "box_plot": "Box Plot", + } + + def pre_validate( + self, + config: dict[str, Any], + ) -> ChartGenerationError | None: + missing_fields = [] + if "metrics" not in config: + missing_fields.append("'metrics' (values whose spread is plotted)") + if "distribute_across" not in config: + missing_fields.append( + "'distribute_across' (columns forming one box per value)" + ) + + if missing_fields: + return ChartGenerationError( + error_type="missing_box_plot_fields", + message=( + f"Box plot missing required fields: {', '.join(missing_fields)}" + ), + details=( + "Box plots show the distribution of one or more metrics " + "across the values of the distribute_across columns" + ), + suggestions=[ + "Add 'metrics': [{'name': 'value_column', 'aggregate': 'AVG'}]", + "Add 'distribute_across': [{'name': 'category_column'}]", + "Example: {'chart_type': 'box_plot', 'metrics': " + "[{'name': 'fare', 'aggregate': 'AVG'}], " + "'distribute_across': [{'name': 'day_of_week'}]}", + ], + error_code="MISSING_BOX_PLOT_FIELDS", + ) + return None + + def extract_column_refs(self, config: Any) -> list[ColumnRef]: + if not isinstance(config, BoxPlotChartConfig): + return [] + refs: list[ColumnRef] = [] + refs.extend(config.metrics) + refs.extend(config.distribute_across) + refs.extend(config.dimensions or []) + if config.filters: + for f in config.filters: + refs.append(ColumnRef(name=f.column)) + return refs + + def to_form_data( + self, config: Any, dataset_id: int | str | None = None + ) -> dict[str, Any]: + return map_box_plot_config(config) + + def generate_name(self, config: Any, dataset_name: str | None = None) -> str: + metric_names = ", ".join(m.label or m.name for m in config.metrics) + across = ", ".join(c.label or c.name for c in config.distribute_across) + what = f"{metric_names} distribution by {across}" + context = _summarize_filters(config.filters) + return self._with_context(what, context) + + def resolve_viz_type(self, config: Any) -> str: + return "box_plot" + + def normalize_column_refs(self, config: Any, dataset_context: Any) -> Any: + config_dict = config.model_dump() + + for metric in config_dict.get("metrics") or []: + if metric.get("sql_expression"): + continue + if metric.get("saved_metric"): + metric["name"] = DatasetValidator.get_canonical_metric_name( + metric["name"], dataset_context + ) + else: + metric["name"] = DatasetValidator.get_canonical_column_name( + metric["name"], dataset_context + ) + for key in ("distribute_across", "dimensions"): + for col in config_dict.get(key) or []: + if not col.get("sql_expression") and not col.get("saved_metric"): + col["name"] = DatasetValidator.get_canonical_column_name( + col["name"], dataset_context + ) + DatasetValidator.normalize_filters(config_dict, dataset_context) + return BoxPlotChartConfig.model_validate(config_dict) + + def schema_error_hint(self) -> ChartGenerationError | None: + return ChartGenerationError( + error_type="box_plot_validation_error", + message="Box plot configuration validation failed", + details=( + "The box plot configuration is missing required fields or " + "has invalid structure" + ), + suggestions=[ + "Ensure 'metrics' is a non-empty list with 'name' and 'aggregate'", Review Comment: **Suggestion:** This validation hint incorrectly states that every metric must include `aggregate`, but box-plot metrics can also be valid saved metrics (`saved_metric=True`) or SQL metrics (`sql_expression` with `label`). Update the hint text so it matches the actual accepted schema and does not mislead callers into sending the wrong payload shape. [comment mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Box plot validation hint misstates required metric fields. - ⚠️ Saved metrics guidance conflicts with ColumnRef schema description. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Invoke the MCP `generate_chart()` tool (superset/mcp_service/chart/tool/generate_chart.py:39-144) with a request whose `config.chart_type` is `"box_plot"` but whose `config` either omits `metrics` or has an invalid shape so that Pydantic cannot parse the `ChartConfig` discriminated union defined at superset/mcp_service/chart/schemas.py:167-186. 2. During validation, `ValidationPipeline.validate_request_with_warnings()` (imported in superset/mcp_service/chart/validation/__init__.py:20 and called at generate_chart.py:181-183) delegates to `SchemaValidator`, which per the `schema_error_hint` docstring in superset/mcp_service/chart/plugin.py:17-23 uses the plugin-specific `BoxPlotChartPlugin.schema_error_hint()` (superset/mcp_service/chart/plugins/box_plot.py:127-145) when union parsing fails for a known `chart_type`. 3. The error returned from `BoxPlotChartPlugin.schema_error_hint()` contains the suggestion string at superset/mcp_service/chart/plugins/box_plot.py:136: `Ensure 'metrics' is a non-empty list with 'name' and 'aggregate'`, which states that every metric must provide an `aggregate`. 4. However, the actual box-plot schema `BoxPlotChartConfig.metrics` at superset/mcp_service/chart/schemas.py:93-104 explicitly allows both ad-hoc metrics with `aggregate` and saved metrics using `saved_metric=True`, as well as SQL metrics using `sql_expression`+`label` per the general metric guidance in `generate_chart()`'s docstring (superset/mcp_service/chart/tool/generate_chart.py:114-132). This contradiction can mislead callers (especially LLM agents) into sending only `name`+`aggregate` even for saved or SQL metrics, triggering additional saved-metric validation errors via `DatasetValidator._build_saved_metric_hint_error()` (superset/mcp_service/chart/validation/dataset_validator.py:219-245). ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=392991e656d1481689c72c35e8ed4d92&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=392991e656d1481689c72c35e8ed4d92&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/box_plot.py **Line:** 136:136 **Comment:** *Comment Mismatch: This validation hint incorrectly states that every metric must include `aggregate`, but box-plot metrics can also be valid saved metrics (`saved_metric=True`) or SQL metrics (`sql_expression` with `label`). Update the hint text so it matches the actual accepted schema and does not mislead callers into sending the wrong payload shape. 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%2F41860&comment_hash=61cec564be75e6504441b46b638d998aeef650d2e19bed77d9ec1aab699388e4&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41860&comment_hash=61cec564be75e6504441b46b638d998aeef650d2e19bed77d9ec1aab699388e4&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]
