mesubash opened a new pull request, #27799: URL: https://github.com/apache/datafusion/pull/27799
## Which issue does this PR close? - Closes #25266. - Closes #25267. ## Rationale for this change `percentile_cont` on floating-point columns returned wrong results in two cases: - A small percentile such as `2.5e-7` over `(0, 1, 2)` returned `0.0` instead of `5e-7`, because the interpolation weight was rounded down to a multiple of 1e-6. - The median of `-f64::MAX` and `f64::MAX` returned `inf` instead of `0.0`, because `upper - lower` overflowed before being multiplied by the weight. Both come from the same few lines in `FloatInterpolator::interpolate`, so this PR fixes them together. It replaces #25274 and #25275, which were closed; I took the approach reviewed there and the extra tests requested by @kosiew, and wrote it again myself. ## What changes are included in this PR? In `FloatInterpolator::interpolate`: - The exact `fraction` is used as the weight, without rounding it to six decimal places. `DecimalInterpolator` still uses `INTERPOLATION_PRECISION`, so decimal results do not change. - If both endpoints are finite but `upper - lower` is not, the result is computed as `lower * (1 - f) + upper * f`, which stays finite. All other inputs, including infinities and NaN, use `lower + (upper - lower) * f` as before. One existing expectation in `aggregate.slt` changes: `percentile_cont(0.3) WITHIN GROUP (ORDER BY v)` over `(10, 20, 30, 40, 50)` now returns `22` instead of `21.99999`. The exact value is 22 (20% of the way from 20 to 30); `21.99999` came from the rounding this PR removes. One assumption to point out: without the rounding, the result is not clamped to `[lower, upper]`, so in rare cases it may differ from an endpoint by floating-point rounding. The previous code had the same property when the rounded weight was 1.0, so I did not add a clamp. ## What is the testing strategy for this PR? - Unit tests in `percentile_cont.rs`: `float_interpolation_keeps_small_fractions` and `float_interpolation_does_not_overflow_for_finite_inputs`. Both fail on `main` (`0.0` instead of `5e-7`, and `inf` instead of `0.0`) and pass with this change. - `aggregate.slt`: the small-percentile case in both the `percentile_cont(x, p)` and `percentile_cont(p) WITHIN GROUP (ORDER BY x)` forms, the extreme-values case in both forms, and a window-function query over the extreme values. - I compared the actual output of every `.slt` file that uses `percentile_cont` or `median` on `main` and on this branch. The only differences are the new tests and the `21.99999` to `22` change above. - `cargo test -p datafusion-functions-aggregate --lib` (282 tests), `cargo fmt --all -- --check` and `cargo clippy -p datafusion-functions-aggregate --all-targets --all-features -- -D warnings` pass. ## Are there any user-facing changes? Yes: `percentile_cont` on float inputs now returns exact interpolated values, for example `22` instead of `21.99999` above, and no longer returns `inf` for finite inputs near the `f64` limits. `median` uses the same accumulator; its weight is always 0 or 0.5, so for `median` only the overflow case changes. No API changes. I used an AI assistant while working on this change. I have read and understood all of it and can answer questions about it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
