Skip to content

feat(pivot): warehouse-computed row totals for pivot tables#23780

Merged
ZeRego merged 1 commit into
mainfrom
pivot-table-row-totals
Jun 2, 2026
Merged

feat(pivot): warehouse-computed row totals for pivot tables#23780
ZeRego merged 1 commit into
mainfrom
pivot-table-row-totals

Conversation

@ZeRego
Copy link
Copy Markdown
Collaborator

@ZeRego ZeRego commented Jun 2, 2026

Closes: PROD-7974

Description:

Adds kind: 'rowTotal' to POST /api/v2/projects/{projectUuid}/query/{queryUuid}/calculate-total, which re-runs the source pivot query with groupByColumns cleared and only the index dims kept, so the warehouse returns one correct total per index-value row for every metric type. The frontend (useAsyncCalculateRowTotal) streams those totals into the pivot worker, which now computes pivot-table row totals exclusively from the warehouse — no client-side cell summation — for both the metrics-as-columns and metrics-as-rows layouts. This fixes row totals for non-additive metrics (count distinct, average, ratios) that were previously wrong or hidden, leaving a cell blank when no warehouse value is available rather than showing an incorrect sum. This is the row-totals sibling of the merged column-totals PR #23590.

Demos

CleanShot 2026-06-02 at 13 16 02@2x CleanShot 2026-06-02 at 13 14 22@2x

@linear-code
Copy link
Copy Markdown

linear-code Bot commented Jun 2, 2026

PROD-7974

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

🧪 Test Selection

✅ Tests that will run

Test Description
Preview Environment Deploys a preview environment for testing
Frontend E2E Tests Runs Cypress app tests
Backend API Tests Runs Vitest API tests
CLI Tests Runs CLI integration and dbt version tests

⏭️ Tests skipped (no relevant file changes detected)

Test How to trigger manually
Timezone Tests Add test-timezone to PR description

Tip: Add test-all to your PR description to run all tests.

@mintlify
Copy link
Copy Markdown

mintlify Bot commented Jun 2, 2026

Docs PR opened: lightdash/mintlify-docs#740

Clarified that pivot table row totals, like column totals, are computed by the warehouse for accurate non-additive metrics.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

Your preview environment pr-23780 has been deployed.

Preview environment endpoints are available at:

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

Preview Environment

🌐 URL: https://lightdash-preview-pr-23780.lightdash.okteto.dev

📋 Logs: View in GCP Console

🔧 SSH: ./scripts/okteto-ssh.sh 23780

Comment thread packages/frontend/src/hooks/useAsyncCalculateTotal.ts
Backend: extends POST /api/v2/projects/{projectUuid}/query/{queryUuid}/calculate-total
with kind: 'rowTotal'. The source MetricQuery + PivotConfiguration are re-run
with groupByColumns cleared and only the index dims kept, so the warehouse
produces one correct total per index-value row (collapsed across all pivoted
columns) for every metric type. Row totals require a pivoted source; non-pivoted
sources are rejected.

Frontend: useAsyncCalculateRowTotal streams the flat totals query and keys each
row by its index-dim values (buildPivotRowTotalKey). useTableConfig feeds the
resulting map into the SQL-pivot worker.

Row totals are now EXCLUSIVELY warehouse-computed — there is no client-side
summation. The worker allocates a row-total slot for every visible base metric
(both metrics-as-columns and metrics-as-rows layouts) and fills it from the
warehouse map, leaving the cell blank (null) when no value is available
(loading, errored, or a source query that can't be totalled). This makes row
totals correct for count_distinct/avg/ratio metrics instead of summing
pre-aggregated cells, and stops showing wrong sums for non-additive metrics.

Key matching handles the wire shape the totals query returns: metric columns
carry the `<metric>_any` aggregation suffix, and date index values serialize
with milliseconds (`...00.000Z`) unlike the pivot query (`...00Z`), so the
lookup normalizes ISO datetimes and tries the suffixed column name.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ZeRego ZeRego force-pushed the pivot-table-row-totals branch from a884001 to 697685b Compare June 2, 2026 14:21
@ZeRego ZeRego merged commit e3a3d10 into main Jun 2, 2026
39 of 40 checks passed
@ZeRego ZeRego deleted the pivot-table-row-totals branch June 2, 2026 15:07
lightdash-bot pushed a commit that referenced this pull request Jun 2, 2026
# [0.3080.0](0.3079.0...0.3080.0) (2026-06-02)

### Bug Fixes

* **pivot:** alias multi-join CTE columns so ClickHouse resolves them downstream (PROD-8026) ([#23711](#23711)) ([#23790](#23790)) ([bda5e29](bda5e29))

### Features

* **ai:** searchSemanticLayer tool for project-wide metric inventory (PROD-8072) ([#23787](#23787)) ([7a4ae18](7a4ae18))
* **pivot:** warehouse-computed row totals for pivot tables ([#23780](#23780)) ([e3a3d10](e3a3d10))
@lightdash-bot
Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 0.3080.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants