rebenitez1802 commented on code in PR #43569: URL: https://github.com/apache/superset/pull/43569#discussion_r3895897040
########## superset/mcp_service/chart/plugins/treemap.py: ########## @@ -0,0 +1,144 @@ +# 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. + +"""Treemap 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, + _treemap_chart_what, + map_treemap_config, +) +from superset.mcp_service.chart.plugin import BaseChartPlugin +from superset.mcp_service.chart.schemas import ColumnRef, TreemapChartConfig +from superset.mcp_service.chart.validation.dataset_validator import DatasetValidator +from superset.mcp_service.common.error_schemas import ChartGenerationError + + +class TreemapChartPlugin(BaseChartPlugin): + """Plugin for treemap chart type.""" + + chart_type = "treemap_v2" + display_name = "Treemap" + native_viz_types: ClassVar[Mapping[str, str]] = { + "treemap_v2": "Treemap", + } + + def pre_validate( + self, + config: dict[str, Any], + ) -> ChartGenerationError | None: + missing_fields = [] + + if not config.get("groupby"): + missing_fields.append("'groupby' (ordered hierarchy columns)") + if "metric" not in config: + missing_fields.append("'metric' (value metric sizing the tiles)") + + if missing_fields: + return ChartGenerationError( + error_type="missing_treemap_fields", + message=( + f"Treemap chart missing required fields: " + f"{', '.join(missing_fields)}" + ), + details=( + "Treemaps size tiles by a metric and nest them by an " + "ordered groupby hierarchy (first column is the outermost " + "level)" + ), + suggestions=[ + "Add 'groupby': [{'name': 'region'}, {'name': 'product'}]", + "Add 'metric': {'name': 'revenue', 'aggregate': 'SUM'}", + "Example: {'chart_type': 'treemap_v2', " + "'groupby': [{'name': 'region'}], " + "'metric': {'name': 'revenue', 'aggregate': 'SUM'}}", + ], + error_code="MISSING_TREEMAP_FIELDS", + ) + + return None + + def extract_column_refs(self, config: Any) -> list[ColumnRef]: + if not isinstance(config, TreemapChartConfig): + return [] + refs: list[ColumnRef] = [*config.groupby, config.metric] + 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_treemap_config(config) + + def generate_name(self, config: Any, dataset_name: str | None = None) -> str: + what = _treemap_chart_what(config) + context = _summarize_filters(config.filters) + return self._with_context(what, context) + + def resolve_viz_type(self, config: Any) -> str: + return "treemap_v2" + + def normalize_column_refs(self, config: Any, dataset_context: Any) -> Any: Review Comment: **`normalize_column_refs` has no test coverage.** This is the most branch-heavy method in the plugin — per-column canonicalization of `groupby`, plus the three-way metric branch (`sql_expression` → passthrough, `saved_metric` → `get_canonical_metric_name`, else → `get_canonical_column_name`) — and it's dispatched in the live validation flow (`superset/mcp_service/chart/validation/dataset_validator.py:537`), yet nothing in `test_treemap_chart.py` exercises it. The repo already guards exactly this saved-metric column-vs-metric casing branch for every other metric-bearing chart type via `TestSavedMetricNormalizationCorrectness` in `tests/unit_tests/mcp_service/chart/test_column_name_normalization.py` (xy / table / pie / big_number / mixed). Treemap is the only metric-bearing type missing, so a casing regression here would fail silently. Suggested: add a treemap case to that class asserting (a) a plain `groupby` column is canonicalized, (b) a `saved_metric` metric routes through `get_canonical_metric_name`, (c) an adhoc-column metric routes through `get_canonical_column_name`, and (d) a `sql_expression` metric is left untouched. -- 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]
