chore: refactor VarianceAccumulator, add tests and benchmark - #23977
Conversation
…elper Adds a criterion benchmark covering VarianceAccumulator::update_batch and retract_batch on null-free and 10%-null f64 batches, and unit tests covering null handling for update, retract, and retract underflow. Extracts the retract_batch loop body into a `retract` free function alongside the existing `update`. A null-free fast path for update_batch/retract_batch (iterating the values buffer directly when null_count() == 0, as COUNT(DISTINCT) does after apache#23956 and percentile_cont/median after apache#23954) was evaluated and rejected: back-to-back runs of the new benchmark show it is within noise (-0.3% to +1.6%), because the Welford update is a serial floating-point dependency chain whose latency hides the per-element validity check.
|
FYI: I explored whether adding a null-free fast path to cc @viirya |
viirya
left a comment
There was a problem hiding this comment.
LGTM. The retract helper is a byte-for-byte extraction of the previous inline Welford inverse — same count <= 1 reset, same delta/mean/m2 recurrence — and it reads better paired with the existing update free function.
The tests are a good addition, especially since they pin down the NULL and underflow behavior that retract_batch relies on:
update_batch_ignores_nulls/retract_batch_ignores_nulls— dense vs. sparse equivalence (I verified the expected sample variances: 5/3 for {1,2,3,4}, 0.5 for the remaining {3,4}).retract_batch_resets_when_underflowing— nicely covers thecount <= 1→ empty reset for both dense and null-containing retract batches, which is exactly the branch that keeps variance correct when a sliding window drains.
Benchmark shape looks right too (the iter_batched retract setup accumulates two batches so retracting one leaves rows, mirroring a sliding window). LGTM.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #23977 +/- ##
==========================================
- Coverage 80.75% 80.75% -0.01%
==========================================
Files 1096 1096
Lines 373506 373582 +76
Branches 373506 373582 +76
==========================================
+ Hits 301629 301677 +48
- Misses 53893 53896 +3
- Partials 17984 18009 +25 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@viirya Thanks for the review! |
…e#23977) ## Which issue does this PR close? - N/A ## Rationale for this change This PR adds a benchmark for `VarianceAccumulator::update_batch`, and `retract_batch`, adds some unit tests, and refactors the `retract_batch` code to add a helper. ## What changes are included in this PR? See above. ## Are these changes tested? Yes, new tests added. ## Are there any user-facing changes? No.
Which issue does this PR close?
Rationale for this change
This PR adds a benchmark for
VarianceAccumulator::update_batch, andretract_batch, adds some unit tests, and refactors theretract_batchcode to add a helper.What changes are included in this PR?
See above.
Are these changes tested?
Yes, new tests added.
Are there any user-facing changes?
No.