Repository navigation
feat(mcp): treemap chart type plugin #43569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: | ||
| config_dict = config.model_dump() | ||
|
|
||
| for col in config_dict.get("groupby") or []: | ||
| if not col.get("sql_expression") and not col.get("saved_metric"): | ||
| col["name"] = DatasetValidator.get_canonical_column_name( | ||
| col["name"], dataset_context | ||
| ) | ||
| if config_dict.get("metric"): | ||
| if config_dict["metric"].get("sql_expression"): | ||
| pass | ||
| elif config_dict["metric"].get("saved_metric"): | ||
| config_dict["metric"]["name"] = ( | ||
| DatasetValidator.get_canonical_metric_name( | ||
| config_dict["metric"]["name"], dataset_context | ||
| ) | ||
| ) | ||
| else: | ||
| config_dict["metric"]["name"] = ( | ||
| DatasetValidator.get_canonical_column_name( | ||
| config_dict["metric"]["name"], dataset_context | ||
| ) | ||
| ) | ||
| DatasetValidator.normalize_filters(config_dict, dataset_context) | ||
| return TreemapChartConfig.model_validate(config_dict) | ||
|
|
||
| def schema_error_hint(self) -> ChartGenerationError | None: | ||
| return ChartGenerationError( | ||
| error_type="treemap_validation_error", | ||
| message="Treemap chart configuration validation failed", | ||
| details=( | ||
| "The treemap chart configuration is missing required " | ||
| "fields or has invalid structure" | ||
| ), | ||
| suggestions=[ | ||
| "Ensure 'groupby' has at least one column for the hierarchy", | ||
| "Ensure 'metric' field has 'name' and 'aggregate'", | ||
| "Example: {'chart_type': 'treemap_v2', " | ||
| "'groupby': [{'name': 'region'}], " | ||
| "'metric': {'name': 'revenue', 'aggregate': 'SUM'}}", | ||
| ], | ||
| error_code="TREEMAP_VALIDATION_ERROR", | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1358,6 +1358,62 @@ def parse_numbers(value: str, field_name: str) -> list[float]: | |
| return self | ||
|
|
||
|
|
||
| class TreemapChartConfig(BaseChartConfig): | ||
| """Config for treemap charts (viz_type ``treemap_v2``). | ||
|
|
||
| Matches the frontend Treemap buildQuery contract: one ``metric`` sizing | ||
| the tiles plus an ordered ``groupby`` hierarchy — the first column is the | ||
| outermost level and each subsequent column nests inside it. When | ||
| ``sort_by_metric`` is set, tiles are ordered by the metric descending. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(extra="ignore", populate_by_name=True) | ||
|
|
||
| chart_type: Literal["treemap_v2"] = "treemap_v2" | ||
| groupby: List[ColumnRef] = Field( | ||
| ..., | ||
| min_length=1, | ||
| description="Ordered category columns forming the treemap hierarchy " | ||
| "(first = outermost level; order defines nesting)", | ||
| ) | ||
| metric: ColumnRef = Field( | ||
| ..., | ||
| description="Value metric sizing the tiles (use aggregate e.g. SUM, " | ||
| "COUNT for ad-hoc, or set saved_metric=True for a saved dataset metric)", | ||
| ) | ||
| sort_by_metric: bool = Field( | ||
| True, | ||
| description="Order tiles by the metric descending (frontend default)", | ||
| ) | ||
| row_limit: int = Field(100, description="Max rows queried", ge=1, le=10000) | ||
| filters: List[FilterConfig] | None = Field( | ||
| None, | ||
| description="Structured filters (column/op/value). " | ||
| "Do NOT use adhoc_filters or raw SQL expressions.", | ||
| ) | ||
| color_scheme: str | None = Field( | ||
| None, | ||
| description=( | ||
| "Superset color scheme ID (e.g. 'supersetColors', 'lyftColors', " | ||
| "'googleCategory10c', 'd3Category10'). Defaults to 'supersetColors'." | ||
| ), | ||
| max_length=100, | ||
| ) | ||
|
|
||
| @model_validator(mode="after") | ||
| def reject_metric_style_groupby(self) -> "TreemapChartConfig": | ||
| """groupby entries are hierarchy dimensions, not metrics.""" | ||
| for i, col in enumerate(self.groupby or []): | ||
| _reject_sql_expression_on_dimension(col, f"groupby[{i}]") | ||
| if col.is_metric: | ||
| raise ValueError( | ||
| f"groupby[{i}] must be a plain column, not a metric; drop " | ||
| "'aggregate'/'saved_metric' (metrics belong in the 'metric' " | ||
| "field)" | ||
| ) | ||
| return self | ||
|
Comment on lines
+1403
to
+1414
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The validator rejects Severity Level: Minor 🧹- ⚠️ `generate_chart` accepts invalid metric-style treemap hierarchy entries.
- ⚠️ Treemap form-data silently drops requested aggregates.
- ⚠️ Generated hierarchy differs from the caller’s configuration.Prompt for AI Agent 🤖This is a comment left during a code review.
**Path:** superset/mcp_service/chart/schemas.py
**Line:** 1090:1100
**Comment:**
*Api Mismatch: The validator rejects `saved_metric` and `sql_expression`, but it still accepts `aggregate` on a `groupby` entry even though `aggregate` also marks a `ColumnRef` as a metric. `map_treemap_config` later emits only each entry's name, silently discarding the aggregate and changing an invalid metric-style hierarchy entry into an unaggregated dimension. Reject non-null `aggregate` values for treemap hierarchy columns as well.
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 |
||
|
|
||
|
|
||
| class PivotTableChartConfig(BaseChartConfig): | ||
| model_config = ConfigDict(extra="ignore", populate_by_name=True) | ||
|
|
||
|
|
@@ -2601,6 +2657,7 @@ def reject_metric_style_dimensions(self) -> "WaterfallChartConfig": | |
| | TableChartConfig | ||
| | PieChartConfig | ||
| | GaugeChartConfig | ||
| | TreemapChartConfig | ||
| | PivotTableChartConfig | ||
| | InteractivePivotChartConfig | ||
| | MixedTimeseriesChartConfig | ||
|
|
@@ -2613,7 +2670,7 @@ def reject_metric_style_dimensions(self) -> "WaterfallChartConfig": | |
| discriminator="chart_type", | ||
| description=( | ||
| "Chart configuration - specify chart_type as 'xy', 'table', " | ||
| "'pie', 'gauge', 'pivot_table', 'interactive_pivot', " | ||
| "'pie', 'gauge', 'treemap_v2', 'pivot_table', 'interactive_pivot', " | ||
| "'mixed_timeseries', 'handlebars', " | ||
| "'big_number', 'histogram', 'box_plot', or 'waterfall'" | ||
| ), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normalize_column_refshas 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 intest_treemap_chart.pyexercises it.The repo already guards exactly this saved-metric column-vs-metric casing branch for every other metric-bearing chart type via
TestSavedMetricNormalizationCorrectnessintests/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
groupbycolumn is canonicalized, (b) asaved_metricmetric routes throughget_canonical_metric_name, (c) an adhoc-column metric routes throughget_canonical_column_name, and (d) asql_expressionmetric is left untouched.