Abdulrehman-PIAIC80387 opened a new pull request, #42976:
URL: https://github.com/apache/superset/pull/42976

   ### SUMMARY
   
   First half of #42809. Extends 
`superset/utils/pandas_postprocessing/pivot.py` with an optional 
`show_values_as` argument that computes percent-of-row, percent-of-column, or 
percent-of-grand-total after pivoting, mirroring the client-side `fractionOf` 
semantic in 
[`react-pivottable/utilities.ts:739`](../blob/master/superset-frontend/plugins/plugin-chart-pivot-table/src/react-pivottable/utilities.ts#L739)
 (hardened by @rusackas in #42810).
   
   ### WHY THIS IS HALF THE FIX
   
   #42809 has two halves:
   
   1. **Server-side capability** — `pivot()` needs to know how to compute 
percentages. **This PR.**
   2. **Frontend wiring** — the pivot chart's `buildQuery.ts` needs to include 
`pivot` postprocessing (with `show_values_as`) in the query so CSV/XLSX exports 
pick it up. **Follow-up PR** — needs a design decision on whether to emit the 
operation always (client would need to tolerate pre-pivoted DataFrames) or only 
for export flows (`result_type` in `{CSV, XLSX}`).
   
   @rusackas / @sadpandajoe — happy to fold Path B into this PR if you have a 
preferred design, or land this first and open the follow-up. Let me know which 
shape you want.
   
   ### FIX
   
   - Adds `show_values_as` param on `pivot()`, accepting `percent_row`, 
`percent_col`, `percent_total`, or `None` / `"actual"` (no-op).
   - Post-pivot, when the mode is set, each cell is divided by the appropriate 
rollup total.
   - On a **multi-metric pivot** (`MultiIndex` columns) the totals are computed 
*within each metric group* — never across metrics — so metric A's percentages 
are never contaminated by metric B's values (matches the client's `metricAxis` 
handling).
   - Default is `None` — no behavior change for existing callers of `pivot` 
postprocessing (echarts Timeseries, BigNumber, MixedTimeseries).
   
   ### EDGE CASES (mirror #42810's client-side guards)
   
   | Guard | Behavior |
   |---|---|
   | NaN/NULL numerator | Stays NaN (renders blank) — a genuine SQL NULL does 
not become `0.0%` |
   | Zero row/column denominator | Cells for that row/column become NaN, not 
`Infinity` |
   | Zero grand total | All cells become NaN |
   | Invalid `show_values_as` value | Raises `InvalidPostProcessingError` (no 
silent no-op) |
   
   ### BEHAVIOR MATRIX
   
   | Mode | Effect |
   |---|---|
   | `None` / `"actual"` | pivoted DataFrame unchanged (no-op) — default |
   | `"percent_row"` | each cell = cell / row-total |
   | `"percent_col"` | each cell = cell / column-total |
   | `"percent_total"` | each cell = cell / grand-total |
   
   ### TESTING INSTRUCTIONS
   
   ```bash
   pytest tests/unit_tests/pandas_postprocessing/test_pivot.py -v -k 
show_values_as
   ```
   
   Eight new tests: actual-is-noop, percent_row, percent_col, percent_total, 
NaN-numerator-preserved, zero-grand-total-produces-NaN, 
multi-metric-keeps-metrics-separate, invalid-mode-raises.
   
   ### ADDITIONAL INFORMATION
   
   - [x] Has associated issue: partial fix for #42809
   - [x] Required feature flags: none
   - [x] Changes UI: no
   - [x] Includes DB Migration: no
   - [x] Includes CLI or Node.js commands: no
   - [x] Breaking change: no (additive param with `None` default)


-- 
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