Skip to content

fix: percentile_cont interpolation causes NaN for f16 input#20208

Merged
alamb merged 5 commits intoapache:mainfrom
kumarUjjawal:fix/percentile_cont_f16
Feb 13, 2026
Merged

fix: percentile_cont interpolation causes NaN for f16 input#20208
alamb merged 5 commits intoapache:mainfrom
kumarUjjawal:fix/percentile_cont_f16

Conversation

@kumarUjjawal
Copy link
Copy Markdown
Contributor

@kumarUjjawal kumarUjjawal commented Feb 7, 2026

Which issue does this PR close?

Rationale for this change

percentile_cont interpolation for Float16 could overflow f16 intermediates (e.g. when scaling the fractional component), producing inf/NaN and incorrect results. This PR makes interpolation numerically safe for f16.

What changes are included in this PR?

• Perform percentile interpolation in f64 and cast back to the input float type (f16/f32/f64) to avoid f16 overflow.
• Add a regression unit test covering Float16 interpolation near the maximum finite value.

Are these changes tested?

Yes

Are there any user-facing changes?

Yes. percentile_cont on Float16 inputs no longer returns NaN due to interpolation overflow and will produce correct
finite results for valid finite f16 data

@github-actions github-actions Bot added the functions Changes to functions implementation label Feb 7, 2026
@github-actions github-actions Bot added the sqllogictest SQL Logic Tests (.slt) label Feb 7, 2026
Comment thread datafusion/functions-aggregate/src/percentile_cont.rs Outdated
Comment thread datafusion/sqllogictest/test_files/aggregate.slt Outdated
Copy link
Copy Markdown
Contributor

@Jefffrey Jefffrey left a comment

Choose a reason for hiding this comment

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

• Remove the unused interpolation precision constant and associated wrapping-math code paths/comments.

Could we please ensure we keep the PR body consistent with the latest changes

Comment thread datafusion/functions-aggregate/src/percentile_cont.rs Outdated
Comment thread datafusion/functions-aggregate/src/percentile_cont.rs
Comment thread datafusion/functions-aggregate/src/percentile_cont.rs Outdated
/// is computed as: `lower + ((upper - lower) * (fraction * PRECISION)) / PRECISION`
/// to avoid floating-point operations on integer types while maintaining precision.
/// Interpolation is performed in f64 and then cast back to the native type to
/// avoid overflowing Float16 intermediates.
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'd still prefer we keep the old documentation as it was and just add on any amendments; I don't like how it reads about a "previous implementation" considering there is no information about such implementation (without looking at the git history)

.div_wrapping(T::Native::usize_as(INTERPOLATION_PRECISION)),
);
Some(interpolated)
let scaled = (fraction * INTERPOLATION_PRECISION as f64) as usize;
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.

If we always cast INTERPOLATION_PRECISION to f64 at all its usages we should just define it as f64

// Linear interpolation.
//
// We quantize the fractional component (via `INTERPOLATION_PRECISION`) to
// minimize output changes for Float32/Float64 compared to the previous
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.

What previous implementation are we referring to here?

@alamb alamb added this pull request to the merge queue Feb 13, 2026
@alamb
Copy link
Copy Markdown
Contributor

alamb commented Feb 13, 2026

Thanks @kumarUjjawal and @Jefffrey

Merged via the queue into apache:main with commit f5a2ac3 Feb 13, 2026
31 checks passed
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
…0208)

## 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#18945

## Rationale for this change

percentile_cont interpolation for Float16 could overflow f16
intermediates (e.g. when scaling the fractional component), producing
inf/NaN and incorrect results. This PR makes interpolation numerically
safe for f16.


<!--
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?

• Perform percentile interpolation in f64 and cast back to the input
float type (f16/f32/f64) to avoid f16 overflow.
• Add a regression unit test covering Float16 interpolation near the
maximum finite value.

<!--
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?

Yes

<!--
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?

Yes. percentile_cont on Float16 inputs no longer returns NaN due to
interpolation overflow and will produce correct
finite results for valid finite f16 data

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

percentile_cont interpolation causes NaN for f16 input

3 participants