Match Spark histogram percentile rounding and rank bounds #5551
<!-- greptile_summary -->
<h2><a href="https://app.greptile.com/api/retrigger?id=118910204&sig=fecf111d255ac873ac3791f207b07d466ee4fab2d0ff09c537b69b2703e4e700"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=nvidia-public-github&next=%2FNVIDIA%2Fcudf-spark-jni%2Fpull%2F5551&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5</h2>
[Medium impact] The changes since the previous review appear safe to merge.
<h3>Summary</h3>
This PR aligns histogram percentile rounding and rank bounds with Spark.
No new actionable issues were found in the changes since the previous review. Tests were not run during this review.
<!-- greptile_confidence_score:5 -->
<sub>Reviews (3) · Last reviewed commit: "Test list percentiles from empty histogr..." · Reviewed by Greptile</sub>
Catching generic std::out_of_range here will also translate internal cuDF std::out_of_range errors (e.g. from get_sliced_child -> cudf::detail::slice or cudf::detail::null_count on malformed inputs) into IndexOutOfBoundsException instead of CudfException.
Consider throwing a dedicated exception type (e.g. struct percentile_rank_out_of_range : std::out_of_range, preserving C++ std::out_of_range compatibility) and catching only that type here—matching timestamp_parse_overflow in CastStringJni.cpp—along with JNI_EXCEPTION_OCCURRED_CHECK(env, 0); before JNI_THROW_NEW. It would also be helpful to document the new throw contract in histogram.hpp and Histogram.java.
In testRoundedRankPastLastCount, position (2^53 + 4) is integral so lower == higher and both indices hit end. That leaves the lower_index < end && higher_index == end case uncovered—even though it is reachable with positive frequencies and p = 0.5 when cumulative counts wrap, e.g. [(1.0, Long.MAX_VALUE), (2.0, Long.MAX_VALUE), (3.0, 2L)] (accumulated_counts = [Long.MAX_VALUE, -2, 0], max_position = -1, position = -0.5 -> lower = -1 finds index 2, higher = 0 yields 3 == end). Could we add a case for that path?
Separately, all percentileFromHistogram* tests here pass outputAsLists = false; adding a quick outputAsLists = true case for empty histograms with multiple percentages (which also exercises the histogram.cu empty-input sizing fix through wrap_in_list) would cover that path directly at the JNI layer too.
<!-- greptile_outside_diff -->
<h3>Comments Outside Diff</h3>
No findings outside the diff remain.
LGTM
build
/ok to test
/ok to test
@pxLi, there was an error processing your request: E1
See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/1/
/ok to test d2a20c74787f774aab5577f4005aa0bc26800ae5
thirtiseven
res-life
Fixes #5520.
Changes
Match Spark's rounding in both exact percentile interpolation formulas. CUDA can contract a multiply and an add across the original volatile temporaries, shifting the result by one ULP. Explicit round-to-nearest operations preserve the rounding of each JVM operation.
2^53 + 4and a percentile of1.0, Spark throwsIndexOutOfBoundsException. JNI could read the first value of the next histogram. Check the rank before reading either endpoint.Validation
Percentile.updateandPercentile.evalacross 43 Apache Spark/Scala combinations. All 7,310 ascending and descending cases matched, including 57,448 double values compared withDouble.doubleToLongBits, null results, and expected exceptions.HistogramTesttests and six layout checks on a Quadro RTX 8000 (sm75) with CUDA 12.9.1. Compute Sanitizer memcheck reported zero errors for the JNI cases. H100 and A30 hardware validation remains pending.