perf: merge hll_sketch_agg partial states into one union per group #7345
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
andygrove
Which issue does this PR close?
Part of #7341. This is the first item of the solution proposed there. The
GroupsAccumulatorand thedatasketcheschanges it also lists are not in this PR, so #7341 stays open.Rationale for this change
HllSketchAccumulator::merge_batchmerged each partial sketch throughSparkHllSketch::merge_sketch, which built a newSparkHllUnionfor 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,datasketches0.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?
HllSketchAccumulatoris an enum of aSparkHllSketchand aSparkHllUnion. Input rows are hashed into the sketch as before. The firstmerge_batchfolds the sketch into a union, which merges every partial sketch from then on, asHllUnionAccumulatoralready does.state()andevaluate()serialize whichever of the two the accumulator holds.update_batchormerge_batchcalls. If one ever gets rows after a merge, they reach the union through a sketch of their own.SparkHllSketch::merge_sketchand the privateSparkHllUnion::to_sketchare removed, since nothing else used them.SparkHllUnion::lg_max_kis added for the case above.hll_aggbench 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.mergeuses. 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_callsmerges 40 partial sketches in batches of 7 and checks after every batch that the accumulator holds the same bytes as oneSparkHllUnionfed the same sketches, in SET mode and after the group turns dense.rows_before_and_after_a_merge_all_countgives one accumulator rows, then a partial sketch, then more rows, and checks that the estimate counts all three.expressions/aggregate/hll_sketch_agg.sqlcompares 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 10before 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 |