Repository navigation
Conversation
… view KLL and Count-Min seal into their view layout, as DDSketch does, at every sealing site (a chain's own window, a query-time build, a level merged from narrower windows), and the cost model prices a sealed KLL or Count-Min state at that layout. - values::tests::sealing: the rule per family and site; the exact bytes a sealed KLL (k = 200) and Count-Min (3 x 64) column holds (8,604 B, 74,604 B, 78,604 B); sealed levels answer as unsealed; a merge's KLL output holds 1,704 B, shrunk, not the 2,664 B the merge grew. - integration sealed_state_bytes: sketch_state_bytes equals what the executor holds per state (exact up to k; past k at least the column's mean state and within 1.25x of it), and a DDSketch guard. Fails on main: KLL and Count-Min are held as owned sketches (a 15-row KLL window 19,200 B, a 180-row level 133,200 B, a Count-Min level 84,800 B), priced as owned (384 B vs a 168 B view at 15 rows), and a merge's KLL output keeps its doubled buffer.
One sealing rule, StateColumn::sealed: every family with a view layout (DDSketch, KLL, Count-Min) is held in it, any other as owned sketches, at a chain's own window, a query-time build and a merged chain level. - Executor: KLL quantiles and Count-Min totals are read on their views (MappedSketches::estimate), as DDSketch quantiles were, so no owned sketch is built per estimate. AggregateCore::shrink_to_fit, and a merge shrinks each state it built before handing it on. - Cost: a sealed KLL is priced at its view (40-byte header, level boundaries, retained items); past k its retained items come from the library's compaction schedule, run with its own coin over 16 seeds, the most taken. A sealed Count-Min is priced at 32 + 8 per counter. Open states (merge targets, builds) keep the owned prices. A DDSketch's 4-byte column offset is now counted once, in the row beside the state (state_offset_bytes_per_row), not in its price. - Eleven sketch plan snapshots regenerated: two DDSketch plans move to KLL, the rest change only their cost and memory.
zzylol
force-pushed
the
fix/one-window-sealing-rule
branch
from
November 26, 2026 05:53
b9d85a0 to
8321079
Compare
- A grouped (non-series) state row of a view-layout sketch holds its
4-byte column offset too, so the offset is counted once on every row.
- kll_retained -> kll_retained_levels_and_items, cached by (k, rows):
one run of all 16 seeds near 2^20 rows takes about 20 ms.
- kll_owned_bytes documents that an open sketch holds more than it
charges (level slots, doubling buffer).
- An empty view quantile's error names the sketch ("Kll quantile of an
empty population").
- Two SQL rollup snapshots regenerated (+4 B per grouped KLL row).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kit charged the open buffer (5.4x what is retained). A chain level merged from narrower windows held a KLL or Count-Min in yet another form: views on main, or unshrunk owned sketches at this PR's first head (133,200 B for 50 states).StateColumn::sealedis the one sealing rule. Every family with a view layout (DDSketch, KLL, Count-Min) is sealed into it at every sealing site: a chain's own window, a query-time build, and a merged level. The cost model prices a sealed KLL and a sealed Count-Min at that layout. Pastk, a KLL's retained items come from the library's own compaction schedule. A merge also shrinks the states it builds.nested-windows-15m-1h-mvpsketch plan, the quantile moves from a DDSketch chain to a KLL (k = 132) chain: 5.73e-6 → 3.96e-6 $/s.Review guide
main(b3351bc, which includes refactor: one home for chain cover windows and view packing; delete the mapped-bytes input path #1521 and fix(cost): stored view windows have a decoded and a stored size; rows hold an offset and a key per label column (#1486) #1487). This replaces the earlier head of this PR (option 1, owned KLL/Count-Min). The branch was rebased; the old head is kept locally as a backup.test:— the failing tests, approved before implementation.fix:— the change, the updated existing tests, and the regenerated snapshots.fix: review follow-ups— the offset on grouped rows, the cache, naming and error text, and two snapshots.crates/executor/src/values/state_column.rs—StateColumn::sealedcrates/executor/src/values/mapped_states.rs—MappedSketches::estimatecrates/executor/src/operators/summary/mod.rs—evaluate_states(the view readout), andObjectMerger::finish(shrink)crates/cost/src/node/state_bytes/kll.rs—kll_view_bytes,kll_retained_levels_and_items,kll_retained_with_seed,KllCoincrates/cost/src/node/state_bytes/mod.rs—sketch_state_bytes,open_sketch_state_bytes,count_min_view_bytes,ddsketch_state_bytes(offset removed)state_bytes/kll.rs(kll_sealed_bytesbecamekll_owned_bytes, unchanged).coverstexts that named DDSketch.values/batch/tests.rsbuilds its object columns from KMV, a family with no view layout.Behavior: before → after
Planning the
nested-windows-15m-1h-mvpworkload (tools/dag-viewer/memo-examples/promql/rangeheavy-t5-q09-15m-1h.json: q0.9 over 15 min and over 1 h, 10,000 series, MVP deployment):Before, a sealed KLL with 12,000 rows per window was priced at its open buffer (
kll_buffer_items, more than 1,000 items). The KLL plan was never chosen.After, it is priced at the items its compaction schedule retains, in the view layout the executor holds. The CPU saving now outweighs the larger states.
In the executor, a sealed KLL or Count-Min column now holds exactly its view bytes (S2):
This serves the cost-accuracy goal: planned bytes equal the bytes held.
Interfaces
crates/executor/src/summary_kernels/traits.rs/// In-memory state of one population's summary; stays. pub trait AggregateCore: Send + Sync { fn clone_boxed_core(&self) -> Box<dyn AggregateCore>; fn as_any(&self) -> &dyn std::any::Any; fn as_any_mut(&mut self) -> &mut dyn std::any::Any; fn merge_with(&self, other: &dyn AggregateCore) -> Result<Box<dyn AggregateCore>, KernelError>; fn estimate(&self, query: &SketchStatistic) -> Result<f64, KernelError>; fn is_empty(&self) -> bool; fn approx_memory_bytes(&self) -> usize; fn shared_state(&self) -> Option<(usize, usize)>; + /// Free what the state holds only for later updates and merges, once + /// no more come (a sealed window, a finished merge). Answers, merges + /// and encodings are unchanged; a state that holds no such room keeps + /// the default, which does nothing. + fn shrink_to_fit(&mut self) {} }crates/cost/src/node/state_bytes/mod.rs(the signatures are unchanged; what they return changes)Crate-internal (not public):
MappedSketches::ddsketch_quantilebecameMappedSketches::estimate(index, &SketchStatistic) -> Result<Option<f64>, String>. It answers DDSketch and KLL quantiles and Count-Min totals on views.StateColumn::sealeddecides byfrom_sealed_views(it returnsNonefor a family with no view layout).values::MappedSketchKernelre-export is now test-only. Nothing outsidevaluesnames it any more.Usage (the path a sealed KLL takes, as the tests run it):
Algorithm changes
Sealed KLL bytes past
k(cost model)kll_buffer_items). The capacity of its levels, which bounds it, is 3,928 B (2.5x).248 + 8(L+1) + 8n.248 + 8(L+1) + 8 · kll_buffer_items(k, n).kll_view_bytes):40 + pad8(4(L+1)) + 8n.kll_retained_levels_and_items(k, n)returns them (cached by(k, n)). It runs the library's compaction schedule on level sizes alone:ceil((pop − coin)/2).max(ceil(k(2/3)^i), 8)by height from the top.KllCoin, the same xorshift-multiply bits) is run with seeds 1–16, and the most items is taken.Kll::with_seed(k, 8, s)after n updates.the_schedule_retains_what_the_library_retainschecks this for k ∈ {8, 200, 1,000}, n ∈ {1, 15, 200, 201, 1,100, 2,000, 13,200, 100,000} and three seeds.(k, n)(cached; about 20 ms near the cap). At most 2^20 rows are simulated (about 10^5 compactions per seed). Past that, the capacity of the L(n) levels is used, an upper bound. The sketch snapshot suite takes 5.76 s before and 6.46 s after (the run includes the corpus test).Kll::with_k.Sealing rule and view readouts (executor)
sealedput DDSketch into views and everything else into objects. Estimates read DDSketch quantiles on views; every other Mapped state was rebuilt as an owned sketch for each estimate.sealeduses views for every family with a view layout.MappedSketches::estimateanswers DDSketch and KLL quantiles and Count-Min totals on the view, so no owned sketch is built per estimate.ObjectMerger::finishcallsshrink_to_fiton every state the merge owns alone (Arc::get_mut). A merged KLL of 180 rows then holds 1,704 B, not 2,664 B.Decisions and open questions
Decided (2026-11-26, user):
state_offset_bytes_per_row, fix(cost): stored view windows have a decoded and a stored size; rows hold an offset and a key per label column (#1486) #1487) and is counted once. DDSketch prices drop by 4 B. A grouped (non-series) row of a DDSketch, KLL or Count-Min state now also holds that offset (state_row_bytes_beside_state), so no row type drops it.Needs reviewer decision: none.
Verification
All run on the remote build host. The suites ran at 8321079 and again with the review follow-ups (5bc2c29), all passing; the clippy, fmt and snapshot rows are for 5bc2c29.
cargo test -p asap-executor(lib + 7 test binaries)cargo test -p asap-costcargo test -p asap-integration-testsa_kll_k_range_moves_the_quantile_to_a_larger_kupdatedcargo test -p asap-devtoolscargo test -p asap-stage3-plan-selectioncorpus_snapshots --ignored(release)cargo fmt --all --checkcargo clippy -p asap-executor -p asap-cost -p asap-integration-tests -p asap-planner-cli --all-targets -D warningsNew tests:
every_family_with_a_view_layout_seals_into_views_at_both_sites: each family sealed into views exactly when it has a view layout, at the chain window and at the merged level.sealed_kll_and_count_min_hold_their_view_layout_bytes: exact byte charges, 8,604 / 74,604 / 78,604 B.sealed_kll_and_count_min_levels_answer_as_unsealed: the view readouts answer as owned sketches.a_merge_shrinks_the_kll_states_it_builds: a merged KLL holds 248 + 16 + 1,440 = 1,704 B.a_sealed_states_price_is_the_view_the_executor_holds: the price equals the held bytes up to k; past k, mean ≤ price ≤ 1.25 x mean.a_sealed_ddsketchs_price_bounds_its_view.the_schedule_retains_what_the_library_retains: the schedule matches the library sketch for each seed.kll_view_bytes_are_the_views_length.Changed expectations of existing tests:
kll_state_bytes_grow_with_the_samples_it_sees: sealed = view, past k = the most over seeds 1–16; open = owned.state_bytes_bound_the_executors_chargeandpriced_bytes_agree_with_the_executorcompare owned KLL/Count-Min accumulators with the open price.open_ddsketch_states_hold_their_first_blocks: KLL open 1,064 vs sealed 848.stored_ddsketch_states_are_priced_at_their_view_layout: 92 → 88 B header (Q3).a_kll_k_range_moves_the_quantile_to_a_larger_k:values/batch/tests.rsobject-column tests now use KMV.Snapshot changes (sketch plan snapshots; 11 of the 45 changed)
nested-windows-15m-1h-mvpshared-quantiles-1h-mvpchains-10m-5m-3m-10s-mvpsql-rollup-quantile-same-window-raw-retainedsql-rollup-quantile-different-windows-raw-retainedquantile-single-mvpquantile-grouped-mvpratio-of-quantiles-1h-mvprandom-mixed-seed-862-long-w01-q3-mvprandom-mixed-seed-862-w01-q3-q8-mvprandom-mixed-seed-862-w02-q2-q7-mvpmixed-quantiles-raw-retainedandquantile-over-time-phase-0-raw-retainedselect KLL but do not change, because they hold no sealed KLL state. The corpus snapshots (exact plans) do not change.Calibration still needed
SummaryEstimatens_per_unitfor KLL and Count-Min read on views (the atomic-cost conversion has 400 and 350 ns);SummaryMergens_per_unitfor merges reading KLL/Count-Min views (merge_view);ns_per_state_emittedfor KLL/Count-Min chains and query-time builds.memory_bytes_per_stateexists for KLL or Count-Min. A deployment's measured tables fitted from owned sizes need refitting to view bytes (thememory_bytes_per_statedoc now says so).stored_bytes_per_state(fix(cost): stored view windows have a decoded and a stored size; rows hold an offset and a key per label column (#1486) #1487) for KLL/Count-Min view files.Follow-ups
open_sketch_state_bytes, the grown buffer), although the merge now shrinks them. Pricing them shrunk would lower query-time memory in the two plans that flipped to KLL.kll_owned_bytes, unchanged by this PR): an open sketch holds 62 level slots and a buffer that doubles from 16 items, for example 872 B at 1 row, priced at 272 B. This affects the newest window of a chain and the working memory of query-time builds.