Skip to content

fix: reject DISTINCT in corr, covar and regr_* instead of ignoring it - #26484

Merged
alamb merged 2 commits into
apache:mainfrom
abp002:reject-unsupported-distinct
Oct 21, 2026
Merged

alamb merged 2 commits into
apache:mainfrom
abp002:reject-unsupported-distinct

Conversation

@abp002

@abp002 abp002 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

corr, covar_pop, covar_samp and the regr_* functions return the same result with DISTINCT as without it, because their accumulators never read is_distinct.

What changes are included in this PR?

Their accumulators now return a not implemented error for DISTINCT, as stddev and approx_median already do. corr(DISTINCT x, x) still works, since SingleDistinctToGroupBy rewrites it before it reaches the accumulator.

What is the testing strategy for this PR?

New cases in aggregate.slt. The two REGR_COUNT(DISTINCT ...) results in eliminate_join_distinct.slt were the non-distinct counts, so they now expect the error.

Are there any user-facing changes?

These queries now fail with an error instead of returning a wrong result.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Oct 18, 2026
SELECT covar_pop(x, y), covar_pop(DISTINCT x, y) FROM (VALUES (1.0, 1.0), (1.0, 1.0), (2.0, 3.0)) t(x, y)

statement error DataFusion error: This feature is not implemented: REGR_COUNT\(DISTINCT\) aggregations are not available
SELECT regr_count(x, y), regr_count(DISTINCT x, y) FROM (VALUES (1.0, 1.0), (1.0, 1.0), (2.0, 3.0)) t(x, y)

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.

nit: could you add a window case too? f(DISTINCT ..) OVER (...) reaches the check through create_sliding_accumulator rather than accumulator, so a test keeps that path covered if one of these functions ever overrides it. I checked locally that both of these already fail with the new error:

statement error DataFusion error: This feature is not implemented: CORR\(DISTINCT\) aggregations are not available
SELECT corr(DISTINCT x, y) OVER () FROM (VALUES (1.0, 1.0), (1.0, 1.0), (2.0, 3.0)) t(x, y)

statement error DataFusion error: This feature is not implemented: REGR_SLOPE\(DISTINCT\) aggregations are not available
SELECT regr_slope(DISTINCT x, y) OVER () FROM (VALUES (1.0, 1.0), (1.0, 1.0), (2.0, 3.0)) t(x, y)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added both window cases, thanks for checking them.

Comment on lines +158 to +159
// The accumulator rejects `DISTINCT` with `not_impl_err!`, so the
// planner has to deduplicate the input first.

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.

nit: the planner only removes DISTINCT here when SingleDistinctToGroupBy applies, which needs a single distinct field like corr(DISTINCT x, x). Every other case now fails. Maybe word it like this (same for the copies in covariance.rs and regr.rs):

-        // The accumulator rejects `DISTINCT` with `not_impl_err!`, so the
-        // planner has to deduplicate the input first.
+        // The accumulator rejects `DISTINCT` with `not_impl_err!`. It only
+        // works when the planner removes it first (`SingleDistinctToGroupBy`
+        // for a single distinct field, e.g. `corr(DISTINCT x, x)`).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, reworded in all four places, with each function's own name in the example.

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

Thanks @abp002 , overall LGTM

@codecov-commenter

codecov-commenter commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.92%. Comparing base (fe612e2) to head (bcea39f).
⚠️ Report is 52 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26484      +/-   ##
==========================================
+ Coverage   82.89%   82.92%   +0.03%     
==========================================
  Files        1154     1154              
  Lines      458022   457885     -137     
  Branches   458022   457885     -137     
==========================================
+ Hits       379683   379717      +34     
+ Misses      55303    55141     -162     
+ Partials    23036    23027       -9     

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

@github-actions

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
     Cloning apache/main
    Building datafusion-functions-aggregate v55.1.0 (current)
       Built [  32.297s] (current)
     Parsing datafusion-functions-aggregate v55.1.0 (current)
      Parsed [   0.047s] (current)
    Building datafusion-functions-aggregate v55.1.0 (baseline)
       Built [  28.701s] (baseline)
     Parsing datafusion-functions-aggregate v55.1.0 (baseline)
      Parsed [   0.048s] (baseline)
    Checking datafusion-functions-aggregate v55.1.0 -> v55.1.0 (no change; assume patch)
     Checked [   0.207s] 229 checks: 228 pass, 1 fail, 0 warn, 31 skip

--- failure pub_module_level_const_missing: pub module-level const is missing ---

Description:
A public const is missing or renamed
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.51.0/src/lints/pub_module_level_const_missing.ron

Failed in:
  STATE_PERCENTILE_IDX in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/48c9d7a4baabd9c1307fe768d68d5dba3cf02591/datafusion/functions-aggregate/src/approx_percentile_cont.rs:51

     Summary semver requires new major version: 1 major and 0 minor checks failed
    Finished [  62.797s] datafusion-functions-aggregate
    Building datafusion-sqllogictest v55.1.0 (current)
       Built [  90.845s] (current)
     Parsing datafusion-sqllogictest v55.1.0 (current)
      Parsed [   0.016s] (current)
    Building datafusion-sqllogictest v55.1.0 (baseline)
       Built [  90.853s] (baseline)
     Parsing datafusion-sqllogictest v55.1.0 (baseline)
      Parsed [   0.017s] (baseline)
    Checking datafusion-sqllogictest v55.1.0 -> v55.1.0 (no change; assume patch)
     Checked [   0.102s] 229 checks: 229 pass, 31 skip
     Summary no semver update required
    Finished [ 184.333s] datafusion-sqllogictest

@github-actions github-actions Bot added the auto detected api change Auto detected API change label Oct 21, 2026
@alamb
alamb added this pull request to the merge queue Oct 21, 2026
Merged via the queue into apache:main with commit 61ec845 Oct 21, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto detected api change Auto detected API change functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

corr, covar_pop, covar_samp and regr_* silently ignore DISTINCT

4 participants