← Back to Pull requests

perf: merge hll_sketch_agg partial states into one union per group #7345

Open
unknown opened this pull request with 538 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 30, 2026 17:38 UTC
Author

Which issue does this PR close?

Part of #7341. This is the first item of the solution proposed there. The GroupsAccumulator and the datasketches changes it also lists are not in this PR, so #7341 stays open.

Rationale for this change

HllSketchAccumulator::merge_batch merged each partial sketch through SparkHllSketch::merge_sketch, which built a new SparkHllUnion for the row, merged the accumulator and the partial into it, and turned the union back into a sketch. Once a group's sketch holds the dense register array, datasketches 0.3.0 copies that array into the empty union and rescans all 2^lgConfigK registers to rebuild its estimator. So every merged row costs as much as the whole array, 262,144 registers at lgConfigK 18. The issue has the profile and the numbers from a Spark 4.0 job where the Final aggregation's median task took 40.4 s against Spark's 8.9 s.

What changes are included in this PR?

  • HllSketchAccumulator is an enum of a SparkHllSketch and a SparkHllUnion. Input rows are hashed into the sketch as before. The first merge_batch folds the sketch into a union, which merges every partial sketch from then on, as HllUnionAccumulator already does. state() and evaluate() serialize whichever of the two the accumulator holds.

  • The accumulator holds one or the other rather than both, so it grows by 8 bytes, from 96 to 104. An empty sketch kept next to the union would roughly double what a small group is charged in a Final aggregation.

  • DataFusion gives an accumulator either update_batch or merge_batch calls. If one ever gets rows after a merge, they reach the union through a sketch of their own.

  • SparkHllSketch::merge_sketch and the private SparkHllUnion::to_sketch are removed, since nothing else used them. SparkHllUnion::lg_max_k is added for the case above.

  • The hll_agg bench gets a Final-mode case, hll_sketch_agg_final_dense: 8,000 partial states at lgConfigK 18, 20 groups of 400 partials with 200 distinct values each, read one partial per group per round as a reducer would.
  • Estimates can move slightly once a group is dense. Before, every merge into a dense group marked its estimator out of order, which makes the estimate come from the composite estimator instead of the HIP accumulator. Now only merging a dense partial does that, as in datasketches-java, which Spark's HllSketchAgg.merge uses. LIST and SET mode results are unchanged. In the dense probe case below, the sum of the 20 estimates went from 1,600,370 to 1,600,533, against a true 1,600,000.

    How are these changes tested?

    Two new unit tests in hll_sketch_agg.rs:

  • merge_batch_keeps_one_union_across_calls merges 40 partial sketches in batches of 7 and checks after every batch that the accumulator holds the same bytes as one SparkHllUnion fed the same sketches, in SET mode and after the group turns dense.

  • rows_before_and_after_a_merge_all_count gives one accumulator rows, then a partial sketch, then more rows, and checks that the estimate counts all three.
  • expressions/aggregate/hll_sketch_agg.sql compares with Spark 4.0+: every input type with NULLs and bounds, and four files that the Final aggregation merges as one partial per group. Groups whose union stays a coupon set and groups merging partials that already hold the register array match Spark exactly. Where coupon-set partials take the union to the register array, the estimate depends on merge order in Spark too, so that query checks each engine's estimate against the true count, within 1%.

    The existing tests cover the update path, the empty group and a Partial/Final plan.

    Benchmarks, release build on an Apple M5 Max, cargo bench --bench hll_agg -- --sample-size 10 before and after:

    | Bench | Before | After |
    |---|---|---|
    | hll_agg/hll_sketch_agg_final_dense (new) | 1.682 s | 25.8 ms |
    | hll_agg/hll_sketch_agg (Partial) | 1.877 ms | 1.838 ms |
    | hll_agg/hll_union_agg (Partial) | 43.98 ms | 43.86 ms |

    The Final-mode probe from the issue, per partial row:

    | Partial states, lgConfigK 18 | Before | After |
    |---|---|---|
    | 1.7M rows, 200K groups, 0 to 8 values each | 332 ns | 281 ns |
    | 200K rows, 2K groups, 50 values each (SET mode) | 2.31 µs | 1.76 µs |
    | 8K rows, 20 groups, 200 values each (dense) | 207.7 µs | 3.3 µs |

    Diff: 538 lines (+403 / -135) across 6 files Reviewer: andygrove Review duration: 6579m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 538 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.376
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #7345"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 25.6% · Safe to merge
    25.6%