Skip to content

feat(countsketch-topk): add portable CountSketchWithHeap wire type#78

Merged
zzylol merged 1 commit into
mainfrom
feat/countsketch-with-heap
Jul 25, 2026
Merged

feat(countsketch-topk): add portable CountSketchWithHeap wire type#78
zzylol merged 1 commit into
mainfrom
feat/countsketch-with-heap

Conversation

@zzylol

@zzylol zzylol commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a real correctness gap surfaced while implementing ASAPQuery-backend's SummaryExecutor (data_plane, see ASAPQuery-backend#411): the only heap-bearing frequency wire type this crate exposed was CountMinSketchWithHeap (portable::countminsketch_topk). data_plane's query-side reducer reused that same type for both CmsWithHeap and CountSketchWithHeap sids — meaning every CountSketchWithHeap query was being read out with CMS's min-over-rows estimator instead of Count Sketch's own median-of-signed-rows estimator. The sketches::CSHeap computational type (median estimator, its own doc already says "mirrors CMSHeap but with Count Sketch") already existed and was already correct — there was just no portable/wire-format wrapper for it, unlike every other sketch family in this crate.

Adds message_pack_format::portable::countsketch_topk::CountSketchWithHeap, structurally mirroring CountMinSketchWithHeap (same wire shape: {sketch: {sketch, row_num, col_num}, topk_heap: [{key, value}], heap_size}) but backed by CSHeap instead of CMSHeap.

Not a new cross-language protocol surface. The wire shape is unchanged from what CountMinSketchWithHeap already produces/consumes for this data (I copied its wire struct verbatim) — this is a new Rust-side reader/builder for the same bytes with correct math, not a new format sketchlib-go needs to learn. No Go-side changes needed.

Also adds #[derive(Clone)] to sketches::CSHeap (CMSHeap already had it; CSHeap was missing it — needed for CountSketchWithHeap::merge_refs's structural clone; both of CSHeap's own fields, Count and HHHeap, are already Clone, so this was just an oversight, not a design constraint).

Test plan

  • cargo test message_pack_format::portable::countsketch_topk — 8 new tests, all passing: creation, empty query, merge (+ dimension mismatch), msgpack round-trip, aggregate_topk (+ empty), and test_median_estimator_differs_from_cms_style_min — the actual point of this addition, proving the median estimator behaves differently from a CMS-style min estimator on a skewed insert pattern, not just sharing a struct shape.
  • cargo test (full workspace) — 466 unit + 5 asapv1_golden + 12 msgpack_compat (5 ignored) + 4 sketches_go_parity_probe + 1 xtest_consumer + 19 doctests, all passing.
  • cargo clippy --all-targets --all-features -- -D warnings — clean.
  • cargo fmt --check — clean.

🤖 Generated with Claude Code

Fixes a real correctness gap surfaced while implementing
ASAPQuery-backend's SummaryExecutor (data_plane): the only
heap-bearing frequency wire type this crate exposed was
CountMinSketchWithHeap (portable::countminsketch_topk). data_plane's
query-side reducer reused that same type for BOTH CmsWithHeap and
CountSketchWithHeap sids -- meaning every CountSketchWithHeap query was
being read out with CMS's min-over-rows estimator instead of Count
Sketch's own median-of-signed-rows estimator. The sketches::CSHeap
computational type (median estimator, "mirrors CMSHeap but with Count
Sketch") already existed and was already correct; there was just no
portable/wire-format wrapper for it, unlike every other sketch family.

Adds `message_pack_format::portable::countsketch_topk::CountSketchWithHeap`,
structurally mirroring CountMinSketchWithHeap (same wire shape: {sketch:
{sketch, row_num, col_num}, topk_heap: [{key, value}], heap_size}) but
backed by CSHeap instead of CMSHeap. The wire SHAPE is unchanged from
what CountMinSketchWithHeap already produces/consumes for this data --
this is a new Rust-side reader/builder for the same bytes with correct
math, not a new cross-language protocol; sketchlib-go needs no changes.

Also adds `#[derive(Clone)]` to `sketches::CSHeap` (CMSHeap already had
it; CSHeap was missing it, needed for CountSketchWithHeap::merge_refs's
structural clone -- both of CSHeap's own fields, Count and HHHeap, are
already Clone).

## Test plan
- [x] `cargo test message_pack_format::portable::countsketch_topk` -- 8
      new tests, all passing: creation, empty query, merge (+ dimension
      mismatch), msgpack round-trip, aggregate_topk (+ empty), and
      test_median_estimator_differs_from_cms_style_min -- the actual
      point of this addition, proving the median estimator behaves
      differently from a CMS-style min estimator on a skewed insert
      pattern, not just sharing a struct shape.
- [x] `cargo test` (full workspace) -- 466 unit + 5 asapv1_golden + 12
      msgpack_compat (5 ignored) + 4 sketches_go_parity_probe + 1
      xtest_consumer + 19 doctests, all passing.
- [x] `cargo clippy --all-targets --all-features -- -D warnings` --
      clean.
- [x] `cargo fmt --check` -- clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@zzylol
zzylol merged commit e608eb4 into main Jul 25, 2026
3 checks passed
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