← Back to Pull requests

Match Spark histogram percentile rounding and rank bounds #5551

Merged
res-life merged 5 files (419 lines) on Nov 25, 2026 11:24 UTC
View on GitHub
res-life res-life commented on Nov 25, 2026 02:11 UTC
Author

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.

  • Match Spark's rank conversion and binary search for large frequencies. With a total count of 2^53 + 4 and a percentile of 1.0, Spark throws IndexOutOfBoundsException. JNI could read the first value of the next histogram. Check the rank before reading either endpoint.

  • Evaluate offsets and frequency validity within each input slice. Handle leading empty histograms and repeat validity for each percentile in flat output.

  • Retain ownership of null-mask buffers when moving their contents.
  • Validation

  • Compared the Java histogram JNI entry point with Spark's Percentile.update and Percentile.eval across 43 Apache Spark/Scala combinations. All 7,310 ascending and descending cases matched, including 57,448 double values compared with Double.doubleToLongBits, null results, and expected exceptions.

  • Ran all 22 HistogramTest tests 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.

  • Recompiled the changed native files and relinked them with an existing cuDF build. A full build with the pinned dependencies remains pending.
  • Diff: 419 lines (+310 / -109) across 5 files Reviewer: thirtiseven Review duration: 552m
    All Vouch review confidence checks passed
    Residual risk 0.08 = Change Risk (0.45) × (1 − Review Confidence (0.81)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 419 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.76 · 5 substantive comment(s)
    0.76
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    81%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.361
    Real PR Conversation Timeline
    11 items
    copy-pr-bot[bot] copy-pr-bot[bot] commented on Nov 25, 2026 02:11 UTC
    Bot

    This pull request requires additional validation before any workflows can run on NVIDIA's runners.

    Pull request vetters can view their responsibilities here.

    Contributors can view more details about this message here.

    greptile-apps[bot] greptile-apps[bot] commented on Nov 25, 2026 07:23 UTC
    Bot

    <!-- greptile_summary -->

    <h2><a href="https://app.greptile.com/api/retrigger?id=118910204&amp;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&amp;next=%2FNVIDIA%2Fcudf-spark-jni%2Fpull%2F5551&amp;utm_source=greptile_review&amp;utm_medium=github&amp;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.

  • Histogram percentiles use Spark’s rounding and rank rules.

  • Empty and sliced histograms return correctly placed percentile results.

  • Histogram creation moves generated null masks into its output columns.
  • 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>

    thirtiseven thirtiseven commented on code on Nov 25, 2026 08:12 UTC
    Code Review
    src/main/cpp/src/HistogramJni.cpp:1
    @@ -92,7 +72,30 @@ Java_com_nvidia_spark_rapids_jni_Histogram_percentileFromHistogramWithOptions(
             static_cast<spark_rapids_jni::value_order>(value_order))
             .release());
       }
    -  JNI_CATCH(env, 0);
    +  JNI_CATCH_BEGIN(env, 0)
    +  catch (std::out_of_range const& e)
    +  {
    +    JNI_THROW_NEW(env, "java/lang/IndexOutOfBoundsException", e.what(), 0);
    +  }
    +  CATCH_SPECIAL_EXCEPTION(env, 0)
    +  CATCH_STD_EXCEPTION(env, 0);

    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.

    thirtiseven thirtiseven commented on code on Nov 25, 2026 08:12 UTC
    Code Review
    src/test/java/com/nvidia/spark/rapids/jni/HistogramTest.java:1
    @@ -142,6 +143,101 @@ void testAllNulls() {
         }
       }
     
    +  @Test
    +  void testLeadingEmptyHistogramWithNullValues() {
    +    try (ColumnVector histogram = histograms(DOUBLE_HISTOGRAM_TYPE,
    +             Collections.emptyList(), Arrays.asList(entry(null, 1L)),
    +             Arrays.asList(entry(10.0, 1L)));
    +         ColumnVector result = Histogram.percentileFromHistogram(
    +             histogram, new double[]{0.5}, false);
    +         ColumnVector expected = ColumnVector.fromBoxedDoubles(null, null, 10.0)) {
    +      AssertUtils.assertColumnsAreEqual(expected, result);
    +    }
    +  }
    +
    +  @Test
    +  void testRoundedRankPastLastCount() {
    +    // 2^53 + 3 rounds to 2^53 + 4, the total count of the first histogram.
    +    try (ColumnVector histogram = histograms(DOUBLE_HISTOGRAM_TYPE,
    +             Arrays.asList(entry(5.0, 1L), entry(9.0, (1L << 53) + 3)),
    +             Arrays.asList(entry(47.0, 1L)))) {
    +      assertThrows(IndexOutOfBoundsException.class, () -> {
    +        try (ColumnVector ignored = Histogram.percentileFromHistogram(
    +            histogram, new double[]{0.5, 1.0}, false)) {
    +          // Close the result if the expected exception is not thrown.
    +        }
    +      });
    +    }
    +  }

    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-apps[bot] greptile-apps[bot] commented on Nov 25, 2026 08:35 UTC
    Bot

    <!-- greptile_outside_diff -->

    <h3>Comments Outside Diff</h3>

    No findings outside the diff remain.

    thirtiseven thirtiseven approved these changes on Nov 25, 2026 09:24 UTC
    Approved Reviewer

    LGTM

    res-life res-life commented on Nov 25, 2026 09:40 UTC
    Author

    build

    pxLi pxLi commented on Nov 25, 2026 09:52 UTC
    Contributor

    /ok to test

    copy-pr-bot[bot] copy-pr-bot[bot] commented on Nov 25, 2026 09:52 UTC
    Bot

    /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/

    pxLi pxLi commented on Nov 25, 2026 09:52 UTC
    Contributor

    /ok to test d2a20c74787f774aab5577f4005aa0bc26800ae5

    Reviewers
    Assignees
    res-life res-life
    Labels
    risk: low confidence: 81.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.0% (MEDIUM) · Higher = larger blast radius
    45.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 81.4% · Lower = rushed or shallow review
    81.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 8.3% · Safe to merge
    8.3%