Skip to content

feat(table): allow choosing Sum or Average for the "Show summary" totals row - #43027

Merged
sadpandajoe merged 3 commits into
apache:masterfrom
rusackas:feat/table-totals-aggregate-choice
Aug 13, 2026
Merged

feat(table): allow choosing Sum or Average for the "Show summary" totals row#43027
sadpandajoe merged 3 commits into
apache:masterfrom
rusackas:feat/table-totals-aggregate-choice

Conversation

@rusackas

Copy link
Copy Markdown
Member

SUMMARY

Today the "Show summary" (show_totals) row in the Table chart and AG Grid Table chart always uses whatever aggregation the underlying metric already specifies (e.g. a SUM(x) metric totals as a sum). A user displaying an AVG/rate/score metric per row has no way to get a meaningful summary — they'd have to add a second, differently-aggregated metric and hide/relabel it, which doesn't work at all for Aggregate-mode metrics that are already pre-aggregated.

This adds a totals_aggregate control (Sum / Average, default Sum, only visible when "Show summary" is on) that lets the user pick the totals row's aggregation independently of each metric's own aggregation. It's implemented via a new shared getTotalsMetrics utility (@superset-ui/chart-controls) that clones each Simple (adhoc) metric with its aggregate swapped, used by both plugins' totals query construction.

This is safe because the "Show summary" row is produced by a separate query with no GROUP BY (columns: []) — the database evaluates each metric fresh over all rows, so swapping the aggregate for just that query is a correct, independent computation, not a re-aggregation of already-aggregated per-row values (the class of bug SIP-216 fixed for Pivot Table subtotals via GROUPING SETS).

No backend changes are required: show_totals has always been a frontend-only form-data key, and ChartDataAdhocMetricSchema already accepts AVG for any Simple metric on any query object.

Known limitations (out of scope for this PR):

  • Median is not supported. There's no universal SQL MEDIAN — Postgres needs PERCENTILE_CONT(0.5) WITHIN GROUP (...), MySQL <8 lacks it entirely — so it needs new backend aggregate-function support. Will be filed as a follow-up issue linked from here.
  • Custom-SQL and saved (string) metrics keep their own native aggregate in the totals row; the Sum/Average override only applies to Simple (adhoc) metrics, since there's no safe way to rewrite an arbitrary SQL expression's aggregate function without parsing it.

Fixes #43021
Related: #41463 / #41184 (SIP-216 — same underlying "totals row aggregation" concern, but for making totals correct for whatever aggregation a metric already uses, on Pivot Table only)

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — this PR was implemented and verified via automated tests only (see Testing instructions); no interactive browser session was available to capture screenshots.

TESTING INSTRUCTIONS

  1. Create/open a Table (or AG Grid Table) chart in Aggregate mode with a SUM-aggregated Simple metric, group by a dimension.
  2. Enable Show summary — note the new Summary aggregation dropdown appears underneath, defaulted to "Sum".
  3. Confirm the summary row shows the sum of the metric (unchanged behavior).
  4. Switch Summary aggregation to "Average" and re-run the query. Confirm the summary row now shows the average of the metric across all underlying rows (verify against a manual AVG() query) — not the sum, and not a naive average of the displayed per-row values.
  5. Repeat for AG Grid Table in both Aggregate mode and Raw/records query mode (the summary columns primed via rawSummaryColumns).
  6. Add a custom-SQL metric or a saved metric alongside a Simple metric; confirm its totals-row value is unaffected by the Summary aggregation setting (it keeps using its own native aggregation).

Automated coverage added:

  • plugin-chart-table/test/buildQuery.test.ts — new Totals Aggregation describe block (default SUM, AVG override, SQL/saved metrics pass through unchanged).
  • plugin-chart-ag-grid-table/test/buildQuery.test.ts — new cases for aggregate-mode and raw-mode totals aggregate override.
  • tests/integration_tests/non_additive_totals_tests.py — new TestTableTotalsAggregateOverride, an end-to-end guard (against the real birth_names fixture) that an AVG-override totals query returns a true SUM / COUNT row-level average, not the metric's own SUM.

All frontend unit tests for the two plugin packages pass locally (456/456), tsc --build is clean, and pre-commit run is green on the changed files. The new backend integration test could not be run in this environment (pre-existing local test-DB migration issue unrelated to this change — reproduced with pre-existing tests in the same file) and should be verified in CI.

ADDITIONAL INFORMATION

@dosubot dosubot Bot added change:frontend Requires changing the frontend explore:control Related to the controls panel of Explore viz:charts:table Related to the Table chart labels Aug 10, 2026

@bito-code-review bito-code-review Bot 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.

Code Review Agent Run #5a2352

Actionable Suggestions - 2
  • superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.ts - 1
  • superset-frontend/packages/superset-ui-chart-controls/src/utils/index.ts - 1
    • Missing unit tests for new utility · Line 25-25
Additional Suggestions - 3
  • superset-frontend/plugins/plugin-chart-table/src/controlPanel.tsx - 1
    • Missing renderTrigger on new control · Line 489-489
      The `totals_aggregate` control is missing `renderTrigger: true`, which is inconsistent with the equivalent control in `plugin-chart-ag-grid-table` (line 503). Without it, chart re-renders after changing the aggregate selection are deferred until the user moves focus away, degrading UX.
  • superset-frontend/plugins/plugin-chart-ag-grid-table/src/controlPanel.tsx - 1
    • Missing control panel unit tests · Line 498-522
      The new `totals_aggregate` control is added but `controlPanel.test.tsx` lacks coverage for it. Existing tests in `buildQuery.test.ts` verify the build-time behavior, but the control-panel visibility and default value should be exercised in unit tests as well.
  • tests/integration_tests/non_additive_totals_tests.py - 1
    • Missing docstring on new test · Line 205-205
      Add a docstring to `test_avg_totals_aggregate_matches_sum_over_count` explaining this test validates that an AVG override in the totals row produces a true row-level average (SUM/COUNT over all rows) rather than the metric's native SUM aggregation, consistent with the pattern used by `test_backend_computes_percent_column_for_summary_query` in the same file.
Review Details
  • Files reviewed - 9 · Commit Range: 79cec2e..79cec2e
    • superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.ts
    • superset-frontend/packages/superset-ui-chart-controls/src/utils/index.ts
    • superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts
    • superset-frontend/plugins/plugin-chart-ag-grid-table/src/controlPanel.tsx
    • superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts
    • superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts
    • superset-frontend/plugins/plugin-chart-table/src/controlPanel.tsx
    • superset-frontend/plugins/plugin-chart-table/test/buildQuery.test.ts
    • tests/integration_tests/non_additive_totals_tests.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
    • Eslint (Linter) - ✔︎ 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 evan@preset.io.

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 78.57143% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.64%. Comparing base (762fdcc) to head (eae7a1e).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
...nd/plugins/plugin-chart-table/src/controlPanel.tsx 0.00% 2 Missing ⚠️
...ns/plugin-chart-ag-grid-table/src/controlPanel.tsx 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #43027   +/-   ##
=======================================
  Coverage   66.64%   66.64%           
=======================================
  Files        2866     2867    +1     
  Lines      162896   162908   +12     
  Branches    37525    37534    +9     
=======================================
+ Hits       108568   108577    +9     
- Misses      52203    52206    +3     
  Partials     2125     2125           
Flag Coverage Δ
hive 38.17% <ø> (ø)
javascript 73.69% <78.57%> (+<0.01%) ⬆️
mysql 57.91% <ø> (ø)
postgres 57.94% <ø> (ø)
presto 40.12% <ø> (ø)
python 59.33% <ø> (ø)
sqlite 57.58% <ø> (ø)
unit 100.00% <ø> (ø)

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.

@netlify

netlify Bot commented Aug 11, 2026

Copy link
Copy Markdown

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit eae7a1e
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a7dfc6a2ec3fd0008c26969
😎 Deploy Preview https://deploy-preview-43027--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.

@bito-code-review

bito-code-review Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #e141fa

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 79cec2e..afefabd
    • superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.test.ts
  • Files skipped - 0
  • Tools
    • Eslint (Linter) - ✔︎ 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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@sadpandajoe
sadpandajoe requested review from Antonio-RiveroMartnez and EnxDev and a lite review from Copilot August 11, 2026 17:15

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Adds a user-facing control to choose how the Table and AG Grid Table “Show summary” totals row aggregates metrics (Sum vs Average), implemented by rewriting only Simple (adhoc) metrics for the separate totals query.

Changes:

  • Introduces a new totals_aggregate control (Sum/Average) for both table plugins.
  • Adds shared getTotalsMetrics utility in @superset-ui/chart-controls and uses it when constructing totals queries.
  • Expands unit + integration test coverage to validate correct AVG totals behavior and pass-through for SQL/saved metrics.

Reviewed changes

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

Show a summary per file
File Description
tests/integration_tests/non_additive_totals_tests.py Adds an end-to-end guard asserting AVG totals equals SUM/COUNT at row-level.
superset-frontend/plugins/plugin-chart-table/test/buildQuery.test.ts Adds tests for default SUM totals and AVG override behavior.
superset-frontend/plugins/plugin-chart-table/src/controlPanel.tsx Adds “Summary aggregation” dropdown (visible when Show summary is enabled in agg mode).
superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts Uses shared getTotalsMetrics to rewrite totals-query metrics.
superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts Adds tests for aggregate-mode and raw-mode totals aggregation override.
superset-frontend/plugins/plugin-chart-ag-grid-table/src/controlPanel.tsx Adds “Summary aggregation” dropdown for AG Grid table.
superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts Applies totals_aggregate to both aggregate-mode totals and raw-mode summary columns.
superset-frontend/packages/superset-ui-chart-controls/src/utils/index.ts Re-exports new getTotalsMetrics utility.
superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.ts Implements shared logic to override Simple (adhoc) metric aggregates for totals queries.
superset-frontend/packages/superset-ui-chart-controls/src/utils/getTotalsMetrics.test.ts Adds unit tests for getTotalsMetrics.

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

Comment thread superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts Outdated
Comment thread superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts Outdated
Comment thread superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts Outdated
Comment thread tests/integration_tests/non_additive_totals_tests.py
Comment thread tests/integration_tests/non_additive_totals_tests.py
@bito-code-review

bito-code-review Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #a12d31

Actionable Suggestions - 0
Review Details
  • Files reviewed - 3 · Commit Range: afefabd..e633e5f
    • superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts
    • superset-frontend/plugins/plugin-chart-table/src/buildQuery.ts
    • tests/integration_tests/non_additive_totals_tests.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
    • Eslint (Linter) - ✔︎ 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 evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@sha174n sha174n 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.

LGTM. Backward compat is clean (default SUM reproduces prior behavior) and the totals query has no GROUP BY so AVG is a true row-level SUM/COUNT rather than re-aggregation. One nit worth considering: with AVG selected, the override is applied to every simple metric, so a COUNT/MIN/MAX metric's totals cell also becomes AVG(col), which can read as surprising — might be worth only overriding SUM-aggregated metrics, or documenting that all simple metrics are affected.

@sha174n sha174n added the merge-if-green If approved and tests are green, please go ahead and merge it for me label Aug 13, 2026
@rusackas

Copy link
Copy Markdown
Member Author

@sha174n thanks for the review! Same tradeoff a bot flagged elsewhere on this PR, and I landed on the same answer: guarding it properly needs column-type info getTotalsMetrics doesn't have, just the metric shape. Docstring on getTotalsMetrics now spells out that all simple metrics get the override, so at least it's not a surprise.

claude and others added 3 commits August 13, 2026 10:01
…als row

The Table and AG Grid Table chart's "Show summary" (show_totals) row
always reused each metric's own aggregation, so a metric already
aggregated as AVG/rate/score had no meaningful way to summarize -- a
user would need a second, differently-aggregated metric hidden/relabeled
just for the totals row, which doesn't work at all in Aggregate mode
where metrics are already pre-aggregated.

Add a `totals_aggregate` control (Sum/Average, default Sum) that
overrides just the totals query's Simple (adhoc) metrics via a new
shared `getTotalsMetrics` utility. The totals query has no GROUP BY, so
the database evaluates the metric fresh over all rows -- swapping the
aggregate for that query alone is a correct, independent computation,
not a re-aggregation of already-aggregated per-row values.

Fixes apache#43021

Known limitations, called out in the PR description:
- Median is out of scope (needs a new backend aggregate function; no
  universal SQL MEDIAN, Postgres needs PERCENTILE_CONT, MySQL <8 lacks
  it) -- filed as a follow-up.
- Custom-SQL and saved (string) metrics keep their own native aggregate
  in the totals row; there's no safe way to rewrite arbitrary SQL.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Clamp totals_aggregate to a known-safe value before it reaches
getTotalsMetrics/adhoc metric aggregate, simplify the ag-grid-table
totals-metrics branching into one variable, and guard the AVG-vs-SUM
integration test against a single-row fixture.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@rusackas
rusackas force-pushed the feat/table-totals-aggregate-choice branch from e633e5f to eae7a1e Compare August 13, 2026 17:18
@sadpandajoe
sadpandajoe merged commit f7a2f0e into apache:master Aug 13, 2026
76 checks passed
@bito-code-review

Copy link
Copy Markdown
Contributor

Bito Automatic Review Skipped – PR Already Merged

Bito scheduled an automatic review for this pull request, but the review was skipped because this PR was merged before the review could be run.
No action is needed if you didn't intend to review it. To get a review, you can type /review in a comment and save it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:frontend Requires changing the frontend explore:control Related to the controls panel of Explore merge-if-green If approved and tests are green, please go ahead and merge it for me packages plugins size/L viz:charts:table Related to the Table chart

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support average/median as alternative aggregations for "Show summary" (show_totals) row

5 participants