Skip to content

perf: add grouped accumulators for regression aggregates - #7194

Open
rich7420 wants to merge 12 commits into
apache:mainfrom
rich7420:perf/6886-regr-groups-recovered
Open

rich7420 wants to merge 12 commits into
apache:mainfrom
rich7420:perf/6886-regr-groups-recovered

Conversation

@rich7420

@rich7420 rich7420 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #6886.

Rationale for this change

Grouped regr_* aggregates used DataFusion's GroupsAccumulatorAdapter, which keeps one boxed accumulator per group and slows down as the number of groups grows.

What changes are included in this PR?

Regr gets a GroupsAccumulator that keeps one state row per group, in the partial buffer's column order, and updates all of a statistic's moments in one pass. Four small kernels (regr_sxx/regr_syy, regr_sxy, regr_slope/regr_intercept, regr_r2) define update, merge and result with the same welford helpers the scalar accumulators use, so each moment keeps its floating-point operation order.

The issue suggests composing the existing grouped variance, covariance and correlation accumulators. I tried that first. It was 9-23% slower than the adapter for regr_sxy, regr_slope, regr_intercept and regr_r2 when all rows share one group, because every row reloads the state it just stored and regr_r2 walks the batch three times. The single pass still writes the state back for every row it processes. While consecutive processed rows share a group, the next one continues from a local instead of reloading the state it just stored.

Partial state layouts are unchanged, and the variance, covariance and correlation code is untouched.

How are these changes tested?

Unit tests compare the grouped accumulator with the scalar one bit for bit (NaN by is_nan()) across state, merge, FILTER, sliced inputs, runs of one group and partial emission, for both Spark rule sets. regr.sql adds grouped FILTER and grouped constant-input regr_r2 queries.

Run locally: agg_funcs unit tests, Clippy with -D warnings, and regr.sql on Spark 4.1.

aggregate_stats gets a regr_partial group, and CometAggregateExpressionBenchmark gets six grouped queries. Partial aggregation of 262,144 rows on an Apple M3 Pro, adapter → this PR, median of three runs:

Layout regr_r2 regr_slope regr_sxy regr_sxx
1 group 5.03 → 2.50 ms 3.98 → 3.16 ms 2.85 → 2.44 ms 2.83 → 2.51 ms
1,024 groups, 256-row runs 6.12 → 2.52 ms 5.07 → 3.12 ms 3.88 → 2.54 ms 3.67 → 2.56 ms
1,024 groups, interleaved 13.09 → 1.87 ms 12.94 → 1.88 ms 11.13 → 1.88 ms 5.47 → 1.74 ms
1,024 groups, half NULL 24.26 → 1.86 ms 23.97 → 1.96 ms 11.80 → 1.83 ms 5.72 → 1.86 ms
65,536 groups 93.77 → 3.83 ms 93.63 → 3.93 ms not measured not measured

regr_intercept tracks regr_slope, and regr_syy tracks regr_sxx. The sparse-NULL layout is left out of the table. None of the 32 cases is slower than the adapter.

Reuse the grouped variance, covariance and Pearson kernels while preserving Spark regression state layouts, arithmetic and version-dependent null/error behavior.

Add grouped update/merge/emission coverage and focused native and Spark benchmarks for issue apache#6886.
Compare interleaved keys with long same-key runs and exercise all six regression functions in the single-group control. Preserve the existing benchmark names and cardinality/partial-skip checks.
@github-actions github-actions Bot added enhancement New feature or request performance area:aggregation Hash aggregates, aggregate expressions labels Oct 24, 2026
…ps-recovered

# Conflicts:
#	native/spark-expr/benches/aggregate_stats.rs
Composing the grouped variance, covariance and correlation accumulators made
grouped regr_* slower than the per-group accumulators when rows of one group
arrive together: 9-23% for regr_sxy, regr_slope, regr_intercept and regr_r2
with a single group, and 11% for regr_r2 with 256-row runs.

Replace the composition with one accumulator that keeps a state row per group
in Spark's partial-buffer layout and folds every moment of a statistic in a
single pass. It carries the state in a local while consecutive rows share a
group. The arithmetic and its order are those of the per-row accumulators, so
results stay bit-identical; the existing grouped tests compare the two.

This leaves the shared variance, covariance and correlation kernels as they
are on main. Also drop the test assertion that depended on how DataFusion
splits a Vec on EmitTo::First, and the benchmark changes that the regr cases
do not need.
@rich7420

Copy link
Copy Markdown
Contributor Author

A few things I left out that could be follow-ups:

  • regr_sxx/regr_syy with interleaved keys are 6-12% slower than my first version that reused the variance kernel (still about 3x faster than the adapter). They do the least work per row, so I think the same-group check shows up more there. A separate loop for those two might help.
  • NULLs and FILTER are still checked row by row. Reading the validity bits 64 at a time should help the half-NULL case.
  • The four scalar accumulators could share the update and merge kernels and drop their own composition code. That touches the path without GROUP BY, so I kept it out of this PR.

@rich7420
rich7420 marked this pull request as ready for review November 18, 2026 11:24

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

I read the grouped kernels against the scalar accumulators and welford.rs and found no behavior difference. State layouts, FILTER and NULL handling, EmitTo::First, size() and the merge order all match. One test gap below. I did not run anything.

regr_sxx(x, y) FILTER (WHERE keep), regr_syy(x, y) FILTER (WHERE keep),
regr_sxy(x, y) FILTER (WHERE keep), regr_slope(x, y) FILTER (WHERE keep),
regr_intercept(x, y) FILTER (WHERE keep), regr_r2(x, y) FILTER (WHERE keep)
FROM test_regr_filtered GROUP BY g ORDER BY g

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.

The grouped path is only compared with Comet's own scalar accumulator for NaN, infinities and -0.0 (in the Rust tests), never with Spark. regr.sql has no NaN or Infinity input at all, while corr.sql has a NaN permutation table. Could you add a small grouped table with cast('NaN' as double), cast('Infinity' as double) and -0.0 in y and x, and run the six functions with GROUP BY and FILTER? That would check the new loop against Spark directly. I expect it to pass because the update and merge arithmetic is shared with the scalar path, but I have not run it.

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

Labels

area:aggregation Hash aggregates, aggregate expressions enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a GroupsAccumulator for the native regr_* aggregates

2 participants