How Pivot aggregates data
Automatic hierarchical aggregation
Section titled “Automatic hierarchical aggregation”Local Pivot automatically uses hierarchical aggregation whenever its input, operations and layout support safe reuse. Eligible built-in aggregates accumulate facts into leaf states, then reuse those states to calculate parent groups and totals. This reduces repeated accumulation in reports with nested rows, columns and subtotals.
Configure your dimensions, measures and totals as usual. No aggregation strategy option is required:
import type { PivotConfig } from '@revolist/pivot';
const pivot: PivotConfig = { rows: ['region', 'product'], columns: ['year'], columnCollapse: false, // Keep this layout eligible for child-state reuse. dimensions: [ { prop: 'region' }, { prop: 'product' }, { prop: 'year' }, { prop: 'sales' }, ], values: [{ prop: 'sales', aggregator: 'sum' }], totals: { subtotals: true, grandTotal: true },};How the work changes
Section titled “How the work changes”Direct accumulation
FallbackAccumulate source facts at each required grain.
Facts update each relevant destination in the original order.
Hierarchical
AutomaticRead facts into leaves, then merge states.
Parents combine statistics from disjoint children.
In the direct accumulation path, each destination receives its contributing facts in source order. Hierarchical aggregation first builds leaf statistics and then merges children into their ancestors. Each original contribution reaches its parent once; generated subtotal rows and columns are presentation outputs, not additional facts.
Pivot selects the execution path internally. Custom callbacks, independent group/total operations, and layouts or policies that cannot safely reuse these states retain their original calculation contract. Existing compact-summary optimizations remain available.
The current packed path runs in synchronous local controller/plugin execution without resource accounting, source/result filters or ranking. It supports up to four row dimensions, two column dimensions and eight built-in measures shown on columns, with columnCollapse: false. Custom row trees, per-level column settings, suppressed totals, calculations and field sorting/parsing use the existing paths. Source access must also pass scalar-input safety checks. Cooperative execution retains its existing aggregation path and scheduling.
Merge statistics, then calculate the result
Section titled “Merge statistics, then calculate the result”A displayed cell can omit information its parent needs. Pivot retains the statistics required by eligible operations instead of treating every child cell as an interchangeable number.
| Operation | State that can be reused | Parent calculation |
|---|---|---|
sum | Child sums | Add the sums of disjoint children. |
count | Child occurrence counts | Add the contributing counts. |
avg | Numeric sum, eligible numeric count and mean state | Combine the statistics, then calculate the average at the parent grain. |
min / max | Eligible child extrema | Select the minimum or maximum under the same input policy. |
For example, one child contains [10] and another contains [20, 20, 20]. Their displayed averages are 10 and 20. The correct parent average uses sum 70 / count 4 = 17.5. Averaging the two displayed averages would give 15 and assign the children equal weight despite different fact counts.
Counts also retain their declared meaning. Under the default callbacks, count counts source occurrences, while avg divides by the number of finite values after numeric normalization. On [10, undefined, 'invalid'], default COUNT is 3 and AVG is 10. The count used by AVG is 1. Default numeric coercion still treats null, false and empty strings as zero; hierarchical reuse does not change those rules. Explicit numeric/scalar operation policies can select different eligible inputs. See input policies and counts.
Independent operations keep their original fact scope
Section titled “Independent operations keep their original fact scope”values[].summaries can select different local operations for leaves, groups and totals:
const values: PivotConfig['values'] = [ { prop: 'sales', aggregator: 'sum', summaries: { group: 'avg', total: 'max' }, },];Suppose two leaves contain [10, 20] and [5]. Their leaf SUM results are 30 and 5. A group AVG over both leaves is 35 / 3, and a grand MAX is 20, the largest original sale. Averaging the displayed sums would incorrectly give 17.5; taking their maximum would incorrectly give 30.
These independent scope overrides retain the existing original-fact calculation path. Hierarchical reuse does not change their meaning or replace them with a calculation over displayed child totals.
Floating-point results can change
Section titled “Floating-point results can change”JavaScript numbers use binary floating-point arithmetic. Addition can lose a small contribution when it is combined with a much larger value, so regrouping additions can change the result:
| Calculation order | JavaScript result |
|---|---|
(1e16 - 1e16) + 1 | 1 |
(1e16 + 1) - 1e16 | 0 |
The mathematical sum is 1 in both expressions. Either calculation order can be closer to a high-precision reference on a particular dataset; neither gives universally more accurate sums. Differences may be significant for cancellation-heavy data, even when every input is finite. Hierarchical aggregation does not promise compensated summation, decimal accounting arithmetic or arbitrary precision.
If a requested SUM becomes nonfinite while building or merging hierarchical states, Pivot discards that candidate and uses the direct calculation path. This preserves the existing overflow behavior; it does not make an unrepresentable result finite. AVG retains separate mean statistics so it need not divide an overflowing raw sum.
Use a high-precision reference and a tolerance appropriate to the measure when validating numerical accuracy. Test uneven group sizes, cancellation, large magnitudes, missing values and multiple hierarchy levels. A second grid’s floating-point result is a comparison point, not the correctness definition. Check previously accepted numerical results when upgrading reports that depend on exact floating-point reproducibility.
Upgrading existing reports
Section titled “Upgrading existing reports”Existing configurations and saved reports do not require changes. Pivot now automatically reuses child statistics when eligible. Because floating-point addition is not associative, SUM and AVG can differ from results produced by earlier releases, especially for cancellation-heavy data. This is a numerical behavior change, not a change to the meaning of the aggregates.
If your workflow depends on exact historical results, validate it before upgrading. No calculation-order setting is exposed.
Custom aggregators keep their existing input and callback contract unless a supported composition explicitly supplies reusable statistics. A function named sum is not, by itself, permission to merge its outputs. Composable custom aggregates describes the pipeline contract and its input-timing requirements.
Measure performance on your report
Section titled “Measure performance on your report”Hierarchical aggregation avoids accumulating every source occurrence independently at every eligible ancestor. The opportunity grows when many facts share leaves and a report has several levels of totals. Small reports, sparse leaves, source preparation, result materialization or a fallback path can limit the benefit.
When comparing releases, benchmark the same source, dimensions, measures, totals, filters and execution mode. Include the complete model build and the memory required for aggregate state. The improvement depends on the report and its eligibility for child-state reuse.
aggregationSemantics independently pins where a report may execute. Remote aggregation remains owned by the backend; local aggregation optimizations do not change the standard remote request contract or reconstruct server totals from loaded pages.
Continue learning
Section titled “Continue learning”Aggregate values · Totals and subtotals · Configuration reference · Execution and limits.