kgabryje opened a new pull request, #38955:
URL: https://github.com/apache/superset/pull/38955
### SUMMARY
The MCP `generate_chart` tool only supported ad-hoc metrics (column name +
aggregate function). When an LLM used `get_dataset_info` and saw saved metrics
like "total_revenue" (expression: `SUM(amount) / COUNT(*)`), it could not
reference them in chart generation — it had to reconstruct them as ad-hoc
expressions, which breaks for complex SQL expressions.
This PR adds a `saved_metric: bool` flag to `ColumnRef`. When `true`, the
`name` field references a saved metric from the dataset, and the form_data
output uses a plain string (which Superset's query engine already resolves via
`metrics_by_name` lookup).
**Changes:**
- `schemas.py`: Added `saved_metric` field to `ColumnRef` with a model
validator that clears `aggregate` when set. Updated label computation in
`TableChartConfig` and `XYChartConfig` validators.
- `chart_utils.py`: `create_metric_object` returns a plain string for saved
metrics. `map_table_config` routes saved metrics to the metrics list.
`_humanize_column` handles saved metric labels.
- `dataset_validator.py`: Skips aggregation validation for saved metrics
(they have built-in aggregation).
Fully backward-compatible — `saved_metric` defaults to `False`.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A — backend-only change to MCP tool schemas.
### TESTING INSTRUCTIONS
1. Use `get_dataset_info` to find a dataset with saved metrics
2. Use `generate_chart` with a saved metric reference:
```json
{
"dataset_id": 1,
"config": {
"chart_type": "xy",
"x": {"name": "order_date"},
"y": [{"name": "total_revenue", "saved_metric": true}]
}
}
```
3. Verify the chart renders correctly using the saved metric
4. Run tests: `pytest
tests/unit_tests/mcp_service/chart/test_chart_schemas.py
tests/unit_tests/mcp_service/chart/test_chart_utils.py -x`
### ADDITIONAL INFORMATION
- [ ] Has associated issue:
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration (follow approval process in
[SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [x] Introduces new feature or API
- [ ] Removes existing feature or API
--
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]