Skip to content

feat(postprocessing): teach pivot() to compute percent-of-row/col/total (#42809) - #42976

Merged
rusackas merged 7 commits into
apache:masterfrom
Abdulrehman-PIAIC80387:fix/pivot-pandas-show-values-as-42809
Aug 29, 2026
Merged

rusackas merged 7 commits into
apache:masterfrom
Abdulrehman-PIAIC80387:fix/pivot-pandas-show-values-as-42809

Conversation

@Abdulrehman-PIAIC80387

Copy link
Copy Markdown
Contributor

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

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

Extends ``superset/utils/pandas_postprocessing/pivot.py`` with an
optional ``show_values_as`` argument that expresses each metric cell
as a fraction of the row / column / grand total after pivoting,
mirroring the pivot chart's client-side ``fractionOf`` semantic in
``plugin-chart-pivot-table/src/react-pivottable/utilities.ts:739`` so
server-side rendering paths (CSV / XLSX exports, scheduled reports)
can eventually reproduce the browser output.

Values:

- ``percent_row``:   cell / row-total     (denominator sums across columns)
- ``percent_col``:   cell / column-total  (denominator sums across rows)
- ``percent_total``: cell / grand-total   (denominator sums the DataFrame)
- ``None`` / ``"actual"``: no-op (default) — no behavior change for existing
  callers of ``pivot`` postprocessing (echarts Timeseries, BigNumber, etc.)

Edge cases mirror the client-side apache#42810 guards:

- NaN/NULL numerator stays NaN — a genuine SQL NULL renders blank rather
  than a measured "0.0%".
- Zero or NaN denominator produces NaN cells rather than Infinity from
  division-by-zero.
- On a multi-metric pivot (MultiIndex columns) the totals are computed
  *within each metric group* so metric A's percentages are never
  contaminated by metric B's values.

This is the server-side foundation for apache#42809. The pivot chart's
frontend ``buildQuery.ts`` still needs to add ``pivot`` postprocessing
with ``show_values_as`` so exports pick it up — that is a separate PR
because it requires a design decision on whether to emit the operation
for all query paths or only for export-format queries (client tolerance
for a pre-pivoted DataFrame).

Refs apache#42809
@dosubot dosubot Bot added the viz:charts:pivot Related to the Pivot Table charts label Aug 10, 2026
@bito-code-review

bito-code-review Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #f9d2c3

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: ce172a1..ce172a1
    • superset/utils/pandas_postprocessing/pivot.py
    • tests/unit_tests/pandas_postprocessing/test_pivot.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@codecov

codecov Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.04%. Comparing base (7e3d092) to head (6ad7fd5).
⚠️ Report is 112 commits behind head on master.

Files with missing lines Patch % Lines
superset/utils/pandas_postprocessing/pivot.py 96.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #42976      +/-   ##
==========================================
+ Coverage   78.96%   79.04%   +0.08%     
==========================================
  Files        2878     2879       +1     
  Lines      165181   165768     +587     
  Branches    38168    38304     +136     
==========================================
+ Hits       130430   131033     +603     
+ Misses      32299    32257      -42     
- Partials     2452     2478      +26     
Flag Coverage Δ
hive 37.92% <14.00%> (-0.10%) ⬇️
mysql 57.64% <16.00%> (-0.13%) ⬇️
postgres 57.67% <16.00%> (-0.12%) ⬇️
presto 39.83% <14.00%> (-0.11%) ⬇️
python 83.75% <96.00%> (+0.13%) ⬆️
sqlite 57.36% <16.00%> (-0.13%) ⬇️
unit 73.97% <96.00%> (+0.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds server-side support for pivot-table “Show values as” percentage modes in the pandas postprocessing pivot() operator, so non-browser rendering paths (CSV/XLSX exports, scheduled reports) can match the pivot table’s client-side fraction display semantics.

Changes:

  • Extend pivot() with an optional show_values_as argument to compute percent-of-row/column/grand-total after pivoting, including multi-metric isolation.
  • Add unit tests covering the new percentage modes and key edge cases (NaN numerator preservation, zero grand total, invalid mode).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
superset/utils/pandas_postprocessing/pivot.py Implements show_values_as percent-of-row/col/total post-pivot transforms and validates mode values.
tests/unit_tests/pandas_postprocessing/test_pivot.py Adds regression tests for show_values_as behaviors and edge cases.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Per review on apache#42976:

1. **marginal_distributions guard** — combining ``show_values_as`` with
   ``marginal_distributions`` would include the ``All`` margin row/col
   in the row/col/grand-total denominators, producing wrong percentages.
   Raise ``InvalidPostProcessingError`` explicitly rather than silently
   returning wrong numbers. Combining the two needs a first-class design
   (probably compute on the non-margin subset then re-insert the
   margins), out of scope here.

2. **Flat-multi-metric percent_total** — a multi-metric pivot with no
   ``columns`` groupby produces a flat column index where each column
   IS a metric. Verified with pandas 2.3.3 that the previous code summed
   across metrics for the grand total (metric a's magnitude leaking
   into metric b's percentages). Now iterate each column as its own
   single-metric block, matching the MultiIndex per-metric semantics.

3. **NaN numerator test was broken** — the previous fixture used
   ``operator="sum"`` on a value that included ``NaN``; ``pandas``
   ``.sum(skipna=True)`` on a single-element ``[NaN]`` group returns
   ``0.0``, not ``NaN``, so the test never actually exercised
   ``_div_preserving_nan``'s NaN-preservation path. Rewrote the fixture
   to use a missing (row, col) combination, which ``pivot_table`` fills
   with a genuine ``NaN`` cell — verified empirically.

4. **Falsy-string validation** — the previous
   ``if show_values_as and show_values_as != "actual"`` guard skipped
   validation for empty strings, silently no-oping bad input. Now
   an explicit sentinel check ``if show_values_as not in (None, "",
   "actual")`` rejects unknown modes uniformly and lets both ``None``
   and ``""`` route to the no-op path.

5. **New zero-row/col-denominator tests** — pin the guard against
   division-by-zero producing ``Infinity`` in row/column modes (already
   handled for grand-total mode). Explicit tests for both axes.

6. **Pandas-3 compat (found while fixing)** — ``df.groupby(level=0,
   axis=1)`` is deprecated in pandas 2.3 (FutureWarning) and removed in
   pandas 3.x. Refactored to iterate ``columns.get_level_values(0)``
   explicitly with ``df.xs``, avoiding the deprecated call and keeping
   the fix forward-compatible.

All six fix paths verified against pandas 2.3.3 with a standalone repro
script before writing tests, mirroring the process discipline learned
from apache#42793.
@netlify

netlify Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit 1a3525e
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a8ccf6fee65ec000846ab9b
😎 Deploy Preview https://deploy-preview-42976--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

…pivot_table

CI caught a real bug in the marginal_distributions test — with
``margins=True`` and no ``margins_name`` value, pandas raises
``ValueError: margins_name argument must be a string`` before our
InvalidPostProcessingError guard at the bottom of ``pivot()`` ever
fires. Test failed with the pandas error instead of our intended one.

Hoisting the ``show_values_as`` validation (unknown-mode +
margins-combination) to the *top* of ``pivot()`` — right after the
existing index/aggregates validation and before ``pivot_table`` runs —
means:

1. The test now sees ``InvalidPostProcessingError`` as intended,
   regardless of whether ``marginal_distribution_name`` is set.
2. Users misconfiguring the argument get an immediate, actionable
   error instead of paying for a full pivot they were about to reject.
3. The narrower typing (``percent_mode: Optional[str]``) satisfies
   mypy without ``cast``/``type: ignore`` at the apply-site.

No behavior change for valid configurations.
@bito-code-review

bito-code-review Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #a17e2f

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: ce172a1..848eb4e
    • superset/utils/pandas_postprocessing/pivot.py
    • tests/unit_tests/pandas_postprocessing/test_pivot.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas rusackas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's land this first and do the frontend wiring as a follow-up, keeps review manageable. Nice work chasing down the codeant and copilot threads, looks like the NaN-numerator test and the zero row/col denominator cases are both fixed in 80585a9. LGTM, approving.

…hape

The show_values_as percent transform was running AFTER the
combine_value_with_metric reshape. That reshape swaps the column
MultiIndex level order from (metric, category) to (category, metric),
so the percent transform's per-metric iteration then walked categories
thinking they were metrics — mixing metrics across denominators and
producing wrong percentages (e.g. metric a's row summed to ~1.78 instead
of 1.0 because metric b's values leaked into a's denominators).

Reorder so the percent transform runs first, on the natural
(metric, category) shape, and the reshape runs on the already-normalized
values. Per-metric isolation is preserved regardless of the final column
layout.

Adds a regression test.

Reported by sadpandajoe on apache#42976.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@bito-code-review

bito-code-review Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #eaeaef

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 848eb4e..5290ebe
    • superset/utils/pandas_postprocessing/pivot.py
    • tests/unit_tests/pandas_postprocessing/test_pivot.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

…ty pivots, and value-is-NULL confusion

Addresses three follow-up findings from sadpandajoe on apache#42976:

1. Non-additive aggregates: percent_row / percent_col / percent_total
   divide each cell by the sum of its axis. That sum only equals the DB
   rollup for additive operators (sum, count). For mean / median / min
   / max / distinct, `sum(cells)` is not the row rollup, so exports
   would disagree with the chart. Reject up front with an explicit
   error rather than silently emit wrong numbers.

2. Empty pivot crash: an empty pivot with a column grouping carries a
   MultiIndex with zero level-0 groups; the metric-iteration loop then
   fed pd.concat an empty list and raised
   `ValueError: No objects to concatenate`. Return the empty frame
   unchanged instead.

3. NULL preservation scope: cells whose input rows all held SQL NULL
   values collapse to 0.0 inside pivot_table (pandas sum([NaN]) == 0)
   before the mask runs, so "blank vs measured zero" cannot be
   reconstructed at this layer. Kept the structural-NaN case (no input
   rows at all for the (row, col) group) as the only supported NULL
   preservation path and documented the boundary on
   `_apply_show_values_as`.

Adds three regression tests covering each.
@bito-code-review

bito-code-review Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Agent Run #8393b6

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 5290ebe..6ad7fd5
    • superset/utils/pandas_postprocessing/pivot.py
    • tests/unit_tests/pandas_postprocessing/test_pivot.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@rusackas
rusackas merged commit 6f69fc6 into apache:master Aug 29, 2026
73 checks passed
rusackas added a commit that referenced this pull request Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL viz:charts:pivot Related to the Pivot Table charts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants