gkneighb commented on code in PR #41860: URL: https://github.com/apache/superset/pull/41860#discussion_r3565560393
########## superset/mcp_service/chart/plugins/histogram.py: ########## @@ -0,0 +1,122 @@ +# 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" Review Comment: Declining, standing rationale: constants and locals with inferable types are unannotated across this codebase (the same patterns appear in all seven in-tree chart plugins and throughout mcp_service); mypy passes. Identical future findings will be declined the same way. ########## superset/mcp_service/chart/plugins/histogram.py: ########## @@ -0,0 +1,122 @@ +# 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" Review Comment: Declining, standing rationale: constants and locals with inferable types are unannotated across this codebase (the same patterns appear in all seven in-tree chart plugins and throughout mcp_service); mypy passes. Identical future findings will be declined the same way. ########## superset/mcp_service/chart/chart_utils.py: ########## @@ -891,6 +893,64 @@ def map_pie_config(config: PieChartConfig) -> Dict[str, Any]: return form_data +def map_histogram_config(config: "HistogramChartConfig") -> Dict[str, Any]: + """Map histogram config to Superset form_data (viz_type histogram_v2). + + Matches the frontend Histogram buildQuery contract: a single ``column`` + string to bin, ``groupby`` name list for series, plus bins/normalize/ + cumulative passed straight through to the histogram post-processing + operator. + """ + form_data: Dict[str, Any] = { + "viz_type": "histogram_v2", + "column": config.column.name, + "groupby": [g.name for g in (config.groupby or [])], + "bins": config.bins, + "normalize": config.normalize, + "cumulative": config.cumulative, + "row_limit": config.row_limit, + } + _add_adhoc_filters(form_data, config.filters) + return form_data + + +# The exact strings the frontend boxplotOperator understands; the percentile +# variant must match its PERCENTILE_REGEX: "<low>/<high> percentiles". +_WHISKER_TYPE_TO_OPTION = { + "tukey": "Tukey", + "min_max": "Min/max (no outliers)", +} Review Comment: Declining, standing rationale: constants and locals with inferable types are unannotated across this codebase (the same patterns appear in all seven in-tree chart plugins and throughout mcp_service); mypy passes. Identical future findings will be declined the same way. ########## 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" Review Comment: Declining, standing rationale: constants and locals with inferable types are unannotated across this codebase (the same patterns appear in all seven in-tree chart plugins and throughout mcp_service); mypy passes. Identical future findings will be declined the same way. ########## 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 = [] Review Comment: Declining, standing rationale: constants and locals with inferable types are unannotated across this codebase (the same patterns appear in all seven in-tree chart plugins and throughout mcp_service); mypy passes. Identical future findings will be declined the same way. ########## superset/mcp_service/chart/schemas.py: ########## @@ -1846,6 +1846,108 @@ def validate_unique_column_labels(self) -> "XYChartConfig": return self +class HistogramChartConfig(UnknownFieldCheckMixin): + """Config for histogram charts (viz_type ``histogram_v2``).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + chart_type: Literal["histogram"] = "histogram" + column: ColumnRef = Field( + ..., + description="Numeric column to bin (a physical dataset column)", + ) + groupby: List[ColumnRef] | None = Field( + None, + description="Optional dimensions to split the distribution into series", + ) + bins: int = Field(5, description="Number of histogram bins", ge=1, le=1000) + normalize: bool = Field(False, description="Normalize bin counts to proportions") + cumulative: bool = Field(False, description="Accumulate bin counts left to right") + filters: List[FilterConfig] | None = Field( + None, + description="Structured filters (column/op/value). " + "Do NOT use adhoc_filters or raw SQL expressions.", + ) + row_limit: int = Field(10000, description="Max rows sampled", ge=1, le=100000) + + @model_validator(mode="after") + def reject_metric_style_column(self) -> "HistogramChartConfig": + """The binned column is a physical column, not a metric.""" + _reject_sql_expression_on_dimension(self.column, "column") + if self.column and self.column.saved_metric: + raise ValueError( + "column cannot use saved_metric=True; histograms bin a " + "physical numeric column" + ) + for i, col in enumerate(self.groupby or []): + _reject_sql_expression_on_dimension(col, f"groupby[{i}]") Review Comment: Fixed in c1cb5c2085 — dimension-position fields now reject saved_metric=True alongside sql_expression, with tests. ########## superset/mcp_service/chart/schemas.py: ########## @@ -1846,6 +1846,108 @@ def validate_unique_column_labels(self) -> "XYChartConfig": return self +class HistogramChartConfig(UnknownFieldCheckMixin): + """Config for histogram charts (viz_type ``histogram_v2``).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + chart_type: Literal["histogram"] = "histogram" + column: ColumnRef = Field( + ..., + description="Numeric column to bin (a physical dataset column)", + ) + groupby: List[ColumnRef] | None = Field( + None, + description="Optional dimensions to split the distribution into series", + ) + bins: int = Field(5, description="Number of histogram bins", ge=1, le=1000) + normalize: bool = Field(False, description="Normalize bin counts to proportions") + cumulative: bool = Field(False, description="Accumulate bin counts left to right") + filters: List[FilterConfig] | None = Field( + None, + description="Structured filters (column/op/value). " + "Do NOT use adhoc_filters or raw SQL expressions.", + ) + row_limit: int = Field(10000, description="Max rows sampled", ge=1, le=100000) + + @model_validator(mode="after") + def reject_metric_style_column(self) -> "HistogramChartConfig": + """The binned column is a physical column, not a metric.""" + _reject_sql_expression_on_dimension(self.column, "column") + if self.column and self.column.saved_metric: + raise ValueError( + "column cannot use saved_metric=True; histograms bin a " + "physical numeric column" + ) + for i, col in enumerate(self.groupby or []): + _reject_sql_expression_on_dimension(col, f"groupby[{i}]") + return self + + +class BoxPlotChartConfig(UnknownFieldCheckMixin): + """Config for box plot charts (viz_type ``box_plot``).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + chart_type: Literal["box_plot"] = "box_plot" + metrics: List[ColumnRef] = Field( + ..., + min_length=1, + description="Metrics whose distributions are plotted (use aggregate " + "e.g. AVG, SUM for ad-hoc, or saved_metric=True for saved metrics)", + ) + distribute_across: List[ColumnRef] = Field( + ..., + min_length=1, + description="Columns whose values form the boxes along the x-axis " + "(one box per value)", + ) + dimensions: List[ColumnRef] | None = Field( + None, + description="Optional series dimensions (one colored box group per value)", + ) + whisker_type: Literal["tukey", "min_max", "percentile"] = Field( + "tukey", + description="Whisker algorithm: 'tukey' (1.5 IQR), 'min_max' (no " + "outliers), or 'percentile' (requires percentile_low/percentile_high)", + ) + percentile_low: int | None = Field( + None, description="Lower whisker percentile (0-100)", ge=0, le=100 + ) + percentile_high: int | None = Field( + None, description="Upper whisker percentile (0-100)", ge=0, le=100 + ) + filters: List[FilterConfig] | None = Field( + None, + description="Structured filters (column/op/value). " + "Do NOT use adhoc_filters or raw SQL expressions.", + ) + row_limit: int = Field(100, description="Max boxes", ge=1, le=10000) + number_format: str = Field("SMART_NUMBER", max_length=50) + date_format: str = Field("smart_date", max_length=50) + + @model_validator(mode="after") + def validate_percentiles_and_dimensions(self) -> "BoxPlotChartConfig": + if self.whisker_type == "percentile": + if self.percentile_low is None or self.percentile_high is None: + raise ValueError( + "whisker_type='percentile' requires both percentile_low " + "and percentile_high" + ) + if self.percentile_low >= self.percentile_high: + raise ValueError("percentile_low must be less than percentile_high") + elif self.percentile_low is not None or self.percentile_high is not None: + raise ValueError( + "percentile_low/percentile_high only apply when " + "whisker_type='percentile'" + ) + for i, col in enumerate(self.distribute_across): + _reject_sql_expression_on_dimension(col, f"distribute_across[{i}]") Review Comment: Fixed in c1cb5c2085 — dimension-position fields now reject saved_metric=True alongside sql_expression, with tests. ########## superset/mcp_service/chart/schemas.py: ########## @@ -1846,6 +1846,108 @@ def validate_unique_column_labels(self) -> "XYChartConfig": return self +class HistogramChartConfig(UnknownFieldCheckMixin): + """Config for histogram charts (viz_type ``histogram_v2``).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + chart_type: Literal["histogram"] = "histogram" + column: ColumnRef = Field( + ..., + description="Numeric column to bin (a physical dataset column)", + ) + groupby: List[ColumnRef] | None = Field( + None, + description="Optional dimensions to split the distribution into series", + ) + bins: int = Field(5, description="Number of histogram bins", ge=1, le=1000) + normalize: bool = Field(False, description="Normalize bin counts to proportions") + cumulative: bool = Field(False, description="Accumulate bin counts left to right") + filters: List[FilterConfig] | None = Field( + None, + description="Structured filters (column/op/value). " + "Do NOT use adhoc_filters or raw SQL expressions.", + ) + row_limit: int = Field(10000, description="Max rows sampled", ge=1, le=100000) + + @model_validator(mode="after") + def reject_metric_style_column(self) -> "HistogramChartConfig": + """The binned column is a physical column, not a metric.""" + _reject_sql_expression_on_dimension(self.column, "column") + if self.column and self.column.saved_metric: + raise ValueError( + "column cannot use saved_metric=True; histograms bin a " + "physical numeric column" + ) + for i, col in enumerate(self.groupby or []): + _reject_sql_expression_on_dimension(col, f"groupby[{i}]") + return self + + +class BoxPlotChartConfig(UnknownFieldCheckMixin): + """Config for box plot charts (viz_type ``box_plot``).""" + + model_config = ConfigDict(extra="ignore", populate_by_name=True) + + chart_type: Literal["box_plot"] = "box_plot" + metrics: List[ColumnRef] = Field( + ..., + min_length=1, + description="Metrics whose distributions are plotted (use aggregate " + "e.g. AVG, SUM for ad-hoc, or saved_metric=True for saved metrics)", + ) + distribute_across: List[ColumnRef] = Field( + ..., + min_length=1, + description="Columns whose values form the boxes along the x-axis " + "(one box per value)", + ) + dimensions: List[ColumnRef] | None = Field( + None, + description="Optional series dimensions (one colored box group per value)", + ) + whisker_type: Literal["tukey", "min_max", "percentile"] = Field( + "tukey", + description="Whisker algorithm: 'tukey' (1.5 IQR), 'min_max' (no " + "outliers), or 'percentile' (requires percentile_low/percentile_high)", + ) + percentile_low: int | None = Field( + None, description="Lower whisker percentile (0-100)", ge=0, le=100 + ) + percentile_high: int | None = Field( + None, description="Upper whisker percentile (0-100)", ge=0, le=100 + ) + filters: List[FilterConfig] | None = Field( + None, + description="Structured filters (column/op/value). " + "Do NOT use adhoc_filters or raw SQL expressions.", + ) + row_limit: int = Field(100, description="Max boxes", ge=1, le=10000) + number_format: str = Field("SMART_NUMBER", max_length=50) + date_format: str = Field("smart_date", max_length=50) + + @model_validator(mode="after") + def validate_percentiles_and_dimensions(self) -> "BoxPlotChartConfig": + if self.whisker_type == "percentile": + if self.percentile_low is None or self.percentile_high is None: + raise ValueError( + "whisker_type='percentile' requires both percentile_low " + "and percentile_high" + ) + if self.percentile_low >= self.percentile_high: + raise ValueError("percentile_low must be less than percentile_high") + elif self.percentile_low is not None or self.percentile_high is not None: + raise ValueError( + "percentile_low/percentile_high only apply when " + "whisker_type='percentile'" + ) + for i, col in enumerate(self.distribute_across): + _reject_sql_expression_on_dimension(col, f"distribute_across[{i}]") + for i, col in enumerate(self.dimensions or []): + _reject_sql_expression_on_dimension(col, f"dimensions[{i}]") Review Comment: Fixed in c1cb5c2085 — dimension-position fields now reject saved_metric=True alongside sql_expression, with tests. -- 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]
