Skip to content

fix(cms): stop clamping RegularPath estimates at i32::MAX#76

Merged
GordonYuanyc merged 1 commit into
mainfrom
fix/cms-estimate-i32max-clamp
Jul 20, 2026
Merged

fix(cms): stop clamping RegularPath estimates at i32::MAX#76
GordonYuanyc merged 1 commit into
mainfrom
fix/cms-estimate-i32max-clamp

Conversation

@GordonYuanyc

Copy link
Copy Markdown
Collaborator

Problem

CountMin::estimate on the regular path seeds the running minimum with S::Counter::from(i32::MAX):

let mut min = S::Counter::from(i32::MAX);
for r in 0..rows {
    ...
    if v < min { min = v; }
}
min

For counters wider than i32 (i64/i128), any key whose probed cells all exceed 2147483647 never fires the v < min update, so estimate returns the seed unchanged — silently capping every large count at exactly 2147483647.

This bites real workloads: e.g. CMSHeap<Vector2D<i64>, RegularPath> over a SUM(pkt_len) window where true per-key totals run into the tens of billions. Every such key comes back as 2147483647.

Scope

  • CMS RegularPath — affected (this fix).
  • CMS FastPath — not affected; fast_query_min already seeds from row 0.
  • CMSHeap — affected only when built on RegularPath (the struct default Mode); it delegates to cms.estimate.
  • CountSketch / CSHeap — not affected; they take a median over f64, no constant seed.

The bug only manifests with counts > i32::MAX and a counter type wider than i32, which is why existing tests (small counts / i32 counters) never caught it.

Fix

Seed the running minimum from row 0's probed cell, then fold in rows 1..rows — mirroring the fast path's fast_query_min. No trait-bound or API changes.

Test

Adds estimate_does_not_clamp_i64_counts_above_i32_max: an i64 sketch loaded with ~35 billion and asserting the estimate is not clamped. Fails on the old seed, passes now. Full countminsketch suite (64 tests) green.

🤖 Generated with Claude Code

CountMin::estimate on the regular path seeded the running minimum with
S::Counter::from(i32::MAX). For counters wider than i32 (i64/i128), any
key whose probed cells all exceed 2147483647 never triggered the
`v < min` update, so estimate silently returned the seed — capping every
large count at exactly 2147483647.

Seed the minimum from row 0's probed cell instead, matching the fast
path's fast_query_min. Add a regression test covering an i64 sketch with
counts of ~35 billion.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@GordonYuanyc
GordonYuanyc merged commit 94d76f6 into main Jul 20, 2026
3 checks passed
@GordonYuanyc
GordonYuanyc deleted the fix/cms-estimate-i32max-clamp branch July 20, 2026 04:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant