Skip to content

fix: stabilize grouped correlation with centered moments - #24953

Draft
bvolpato wants to merge 1 commit into
apache:mainfrom
bvolpato:bvolpato/stabilize-grouped-correlation
Draft

fix: stabilize grouped correlation with centered moments#24953
bvolpato wants to merge 1 commit into
apache:mainfrom
bvolpato:bvolpato/stabilize-grouped-correlation

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

No linked issue. The regression below reproduces the problem on current upstream.

Rationale for this change

Grouped CORR uses raw sums whose subtraction loses precision when values have a large common offset. It can return a negative correlation for a column correlated with itself:

SELECT g, corr(x, x)
FROM (VALUES
  (1, 1000000000.0),
  (1, 1000000007.0),
  (1, 1000000015.0),
  (2, 1.0)
) AS t(g, x)
GROUP BY g;

Group 1 can return -1 instead of 1.

What changes are included in this PR?

  • Use paired Welford updates and centered moments for grouped correlation.
  • Merge partial states using weighted mean differences, applying weights before multiplying deltas to avoid avoidable intermediate overflow.
  • Match the scalar CORR state layout across update, merge, singleton conversion, and preserving/draining emission.
  • Add regressions for large offsets, multiple batch sizes, scalar/grouped state interchange, and large-range merges, plus a grouped self-correlation case in aggregate.slt.

What is the testing strategy for this PR?

  • The large-offset and scalar/grouped state compatibility tests fail against upstream production code at 35f58f53cde2f634c21f6370e385cf1d7e9bc55c and pass with the fix.
  • cargo test --locked -p datafusion-functions-aggregate --lib: 210 passed.
  • cargo fmt --all -- --check and git diff --check passed.
  • Draft pending all-target/all-feature Clippy, the extended workspace suite, and execution of aggregate.slt.
  • The H2O groupby q09 benchmark exercises CORR. Comparison against main is still pending.

Are there any user-facing changes?

Grouped CORR produces stable results for the covered large-offset inputs. Floating-point results can differ in their final bits. SQL signatures and output types remain unchanged.

Downsides

Centered updates add arithmetic, including per-row divisions. The grouped intermediate state representation changes from raw sums to centered moments, so partial states produced by the old implementation cannot be mixed with the new representation.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Sep 5, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.16466% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.61%. Comparing base (35f58f5) to head (7bdd886).

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/correlation.rs 91.16% 3 Missing and 19 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24953      +/-   ##
==========================================
- Coverage   81.61%   81.61%   -0.01%     
==========================================
  Files        1124     1124              
  Lines      411978   412086     +108     
  Branches   411978   412086     +108     
==========================================
+ Hits       336236   336318      +82     
- Misses      55936    55941       +5     
- Partials    19806    19827      +21     

☔ 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.

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

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants