Skip to content

feat: Handle edge case with corr with single row and NaN - #18677

Merged
comphead merged 6 commits into
apache:mainfrom
comphead:dev2
Nov 14, 2025
Merged

comphead merged 6 commits into
apache:mainfrom
comphead:dev2

Conversation

@comphead

@comphead comphead commented Nov 13, 2025 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Fix an edge case in corr and NaN

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels Nov 13, 2025
// If BOTH sum_x AND sum_y are NaN, then both input values are NaN → return NaN
// If only ONE of them is NaN, then only one input value is NaN → return NULL
// This takes precedence over the count < 2 check
if sum_x.is_nan() && sum_y.is_nan() {

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.

this is the actual change

@martin-g martin-g left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does CorrelationAccumulator::evaluate() also need improvements related to NaN ?
At the moment if count < 2 it returns return Ok(ScalarValue::Float64(None)); without any checks for NAN.
https://github.com/comphead/arrow-datafusion/blob/2d344a58b48af6c866b6ff024dbd00aa0c8d8293/datafusion/functions-aggregate/src/correlation.rs#L201

// Check for NaN in the sums BEFORE checking count
// If BOTH sum_x AND sum_y are NaN, then both input values are NaN → return NaN
// If only ONE of them is NaN, then only one input value is NaN → return NULL
// This takes precedence over the count < 2 check

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO line 434 should be moved one line up.
At the moment it is not very clear what takes precedence. I think you want to say that the checks for NaN should be before the check for count < 2 but it also sounds like the check for one of them being NaN should be before count < 2, but they are done together.

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.

I was thinking about the same on CorrelationAccumulator::evaluate() but the test passed without having CorrelationAccumulator amended, one thing comes to my mind is it might depend on batch_size, checking this

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.

I found another edge case and added tests for that as well

@comphead
comphead requested a review from martin-g November 13, 2025 23:59
@comphead

Copy link
Copy Markdown
Contributor Author

@andygrove @mbutrovich please take a look

@comphead
comphead requested a review from Jefffrey November 14, 2025 03:53
@comphead
comphead added this pull request to the merge queue Nov 14, 2025
Merged via the queue into apache:main with commit 2b3b220 Nov 14, 2025
jizezhang pushed a commit to jizezhang/datafusion that referenced this pull request Nov 15, 2025
…18677)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18659.

## Rationale for this change

Fix an edge case in `corr` and `NaN`

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
logan-keede pushed a commit to logan-keede/datafusion that referenced this pull request Nov 23, 2025
…18677)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18659.

## Rationale for this change

Fix an edge case in `corr` and `NaN`

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
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.

corr incorrect result with NaN

3 participants