FrancescoCastaldi opened a new pull request, #44464:
URL: https://github.com/apache/superset/pull/44464
### SUMMARY
Fixes #42627.
MCP bar charts and other XY charts had no option to configure sorting;
non-temporal charts were hardcoded to alphabetical ordering by category name
(`x_axis_sort_series_type = "name"` and `x_axis_sort_series_ascending = True`).
This PR adds the `sort_by` field to `XYChartConfig`:
- Adds `sort_by: SortByConfig | str | List[SortByConfig | str] | None` to
`XYChartConfig` with alias support (`x_axis_sort`, `order_by`) and a
`coerce_sort_by` field validator supporting bare strings, dicts, or single-item
lists (defaulting bare strings to descending order).
- Implements `add_xy_sort_config` in
`superset/mcp_service/chart/chart_utils.py`:
- When non-temporal, sets `x_axis_sort` and `x_axis_sort_asc`, alongside
backward-compatibility keys `x_axis_sort_series_type` (`"name"` if sorting by
x-axis dimension, `"value"` otherwise) and `x_axis_sort_series_ascending`.
- When temporal, ignores `sort_by` with a warning in
`form_data["_mcp_warnings"]` to maintain chronological sorting.
- Maintains default behavior when `sort_by` is not specified.
- Updates `XYChartPlugin` in `superset/mcp_service/chart/plugins/xy.py`:
- Extracts `ColumnRef` for the sort column in `extract_column_refs`.
- Resolves canonical column or metric name in `normalize_column_refs`.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A (Backend MCP API / Schema change)
### TESTING INSTRUCTIONS
Run unit tests for MCP chart schemas and chart utils:
```bash
pytest tests/unit_tests/mcp_service/chart/test_chart_schemas.py
tests/unit_tests/mcp_service/chart/test_chart_utils.py
```
All 297 unit tests pass, covering:
- Coercion of bare strings, single-item lists, dicts, and `SortByConfig`
objects.
- Validation aliases (`x_axis_sort`, `order_by`).
- Non-temporal metric and category sorting with ascending/descending flags.
- Temporal chart warning behavior.
- Column reference extraction and canonicalization in `XYChartPlugin`.
### ADDITIONAL INFORMATION
- [x] Has associated issue: Fixes #42627
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration
- [ ] 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]