gabotorresruiz opened a new pull request, #40448:
URL: https://github.com/apache/superset/pull/40448

   <!---
   Please write the PR title following the conventions at 
https://www.conventionalcommits.org/en/v1.0.0/
   Example:
   fix(dashboard): load charts correctly
   -->
   
   ### SUMMARY
   <!--- Describe the change below, including rationale and design decisions -->
   The MCP `generate_chart` and `update_chart` tools only accepted column-based 
metrics (`SUM(amount)`, `COUNT(*)`, or a named saved metric). Anything else, 
ratios, conditional aggregations, `ABS` wrappers, unit conversions, required 
the LLM to ask the user to follow up in the Explore UI.
   
   This PR adds an optional `sql_expression` field to the shared `ColumnRef` 
metric model so the LLM can express the SQL adhoc metric directly:
   ```json
     {
       "y": [{
         "sql_expression": "COUNT(CASE WHEN closed_won THEN 1 END)::numeric / 
NULLIF(COUNT(*), 0)",
         "label": "Win Rate"
       }]
     }
   ```
   
   It maps to Superset's existing `AdhocMetric` shape with expressionType: 
"SQL". No DB migration, no frontend work, no API contract break, the field is 
additive and optional.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   <!--- Skip this if not applicable -->
   N/A - This is an API-level change (no UI)
   
   ### TESTING INSTRUCTIONS
   <!--- Required! What steps can be taken to manually verify the changes? -->
   1. Call `generate_chart` with a y metric carrying `sql_expression` + `label` 
(omit `name`/`aggregate`), e.g. `{"sql_expression": "COUNT(CASE WHEN gender = 
'boy' THEN 1 END)::numeric / NULLIF(COUNT(*), 0)", "label": "Boy Ratio"}` 
against the `birth_names` example dataset.
   2. Verify the returned `form_data.metrics[0]` has `expressionType: "SQL"`, 
`sqlExpression` matches the input, and `label` is preserved.
   3. Open the returned `explore_url` — the chart should render real ratio 
values (~0.47-0.59 per year). The metric editor's "Custom SQL" tab should show 
the expression pre-filled.
   4. Call `update_chart` on a SIMPLE-metric chart with a `sql_expression` y — 
verify the chart's metric is replaced with the SQL adhoc dict.
   5. Sanity-check rejections each surface a clear validation error: 
`sql_expression` without `label`; `sql_expression` combined with 
`name`/`aggregate`/`saved_metric`; `sql_expression` on x-axis, `group_by`, pie 
`dimension`, pivot `rows`, or `TableChartConfig` with `query_mode='raw'`; bad 
SQL syntax (e.g. `SUM(num`); subquery while `ALLOW_ADHOC_SUBQUERY=False`.
   
   ### ADDITIONAL INFORMATION
   <!--- Check any relevant boxes with "x" -->
   <!--- HINT: Include "Fixes #nnn" if you are fixing an existing issue -->
   - [ ] 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]

Reply via email to