Repository navigation
fix(cost): charge KMV builds and merges their measured time (build 110 + 0.62, merge 2.6 + 0.43 per compared hash) - #1375
Open
zzylol wants to merge 27 commits into
Conversation
…e covered output values depend on Cases: per-value and whole-result targets without a reduction, a global max, a grouped sum per value and over the whole result, nested reductions, `or` per value and over the whole result, unknown readout values, a record-share scope and a per-value ratio. Seven fail on the current split, which divides by max(values covered, readout values).
…ues depend on A query's failure probability was split per readout value by max(values covered, readout values per evaluation): a per-value target on `quantile_over_time(0.99, m[5m])` over 8 series got δ / 8 although each output value is one readout value, doubling KLL's k (1404 -> 2863). The union bound needs only the readout values the output values one statement covers depend on: min(R, V * fan_in), with R the readout values per evaluation, V the values the scope covers and fan_in the most readout values one output value depends on (1 at a readout, carried value by value through filters, projections, binary operators and `or`, all of the input's values through a reduction). One function states it, `AccuracyTarget::failure_probability_per_readout_value` over `ReadoutValuesRead`; the readout walk computes both counts. An unknown term drops out of the min, so a per-value target over series of unknown count keeps δ, and a whole-result target with unknown n_q splits over R.
…ncatenations and redundant levels A readout group that also holds a reduction over other readouts, a top-k readout, and a concatenation each split δ over the readout values their output values depend on; a level the same listed builds meet as a stricter one is dropped.
…ver all values, drop redundant levels - A readout group takes the larger of its own readout counts and its other candidates' (a reduction over readouts below it), field by field. - A top-k readout's output value depends on every value it ranks. - A concatenation takes each value from one input, like `or`. - A level the same listed builds meet as a stricter level of the group is dropped: a copy at it accepts the same producers. - ReadoutValuesRead is renamed ReadoutValueCounts, with larger and plus.
…the declared groups of rows
A whole-result quantile grouped by date_trunc('minute', ts) over a table
declaring 6 groups of rows returns about 16 values per evaluation, so its
readout must not split δ over 6. Grouped by src alone, the split over the
6 groups of rows still holds.
…en it is keyed by source labels
Memo::max_values_per_evaluation bounded every readout by the series (groups
of rows) scanned below it. A readout grouped by a computed key or a time,
such as date_trunc('minute', ts), returns one value per bucket, which that
count does not bound: a whole-result target with an unknown value count
split δ over 6 for about 16 values. The bound now applies only when every
key of the readout's state (or aggregate) is a source label or column
copied unchanged and not a time; otherwise R is unknown and, with n_q
unknown too, no per-value failure probability follows, as on the base.
… clamped to the rows read - `flows JOIN hosts ... GROUP BY f.src, h.region` splits δ over 6 × 6 = 36 readout values, or over the rows the join reads when fewer. - `GROUP BY bytes`, a measure key with no declared count, splits over the rows read (96 for 6 groups of rows sampled every minute over 15 minutes), and gets no level when they are unknown. - A declared SQL key uses its declared distinct values. - A self-join's readout keyed by both sides' `job` returns up to 20 × 20.
…t values per scan, clamped to the rows read As SQL optimizers estimate a GROUP BY's groups (the product of the key columns' distinct counts, clamped to the input rows), but as an upper bound from declared facts only: - keys from different scans (a join, or a self-join's two sides) multiply; - a SQL key bounds the values only through its declared distinct values, not through the groups of rows the table declares, which do not say which columns form them; - PromQL labels keep their series bound; - every readout's values are at most the rows its input reads per evaluation: each source's series times the samples its window holds at its sample interval, or the rows it holds.
…values per scan, clamped to the rows read GROUP BY dst with 300 dst values declared returns up to 300 values, not the 6 groups of rows the table declares; an undeclared key is bounded by the rows read; keys of joined scans multiply. A PromQL result keeps its series bound.
…r scan, clamped to the rows read A grouped SQL result's n_q was the groups its keys declare, at most the groups of rows the table declares: GROUP BY dst with 300 dst values over a table of 6 groups of rows gave 6, so a whole-result target split delta over 6 instead of 300. n_q now comes from the memo's bound on the root group (Memo::max_values_per_evaluation), the rule R uses: the product, per scan, of the keys' declared distinct values, clamped to the rows the grouping reads. A PromQL instant vector keeps its series bound. The IR-side copy of the group rule in bounds is deleted.
…r join adds a NULL, a LIMIT caps the rows
m{side="l"} * on(x) group_left(instance) m{side="r"}, grouped by job and
instance, pairs a left series' job with a right series' instance: up to
10 x 10 values, not the 10 pairs one series holds (also group_right and
an offset side). flows LEFT JOIN hosts grouped by h.region returns the 6
regions and a NULL group, so the declared 6 do not bound it (also FULL
JOIN and two keys). A grouping over LIMIT 10 returns at most 10 values.
… side's scans; an outer join's nullable side is computed A group_left/group_right match pairs each output series with one series of the other side, as a join pairs rows: the labels it copies from that side now move past the output side's scan ordinals (after_left_scans, as for a join), so job and instance of two series of one metric multiply instead of sharing one label-set count. The columns of an outer join's nullable side (Left: right, Right: left, Full: both) gain a NULL their source may not hold, so their lineage is computed by the join; a key from them is bounded by the rows read. LIMIT n over the whole input keeps at most n rows. column_properties now lints wildcard enum arms: every lineage and sign rule lists each variant, so a new operator states its rule.
…ta the declared facts allow Each review round found another shape where Memo::max_values_per_evaluation (the readout values R and a query's n_q) fell below the values a query can return. This test checks the bounds against data directly: each query of a corpus is lowered as planning lowers it, its Stage 0 memo built, and the query run by the executor over datasets that keep to the declared source facts (series, distinct values per label and label set, sample interval, rows held). Worst-case seekers make the labels the facts bound only together independent, crossed or aligned across series and rows, replace series between the times a query reads, and split series into blocks by literal labels so matches pair them one to one; random datasets add more. Every bounded group's result rows, and the query's, must be at most its bound. The default corpus is the reviewed probe shapes (85 queries, 9 s); an ignored variant runs larger datasets over the dag-viewer workloads and a manifest of workloads. Cases the executor or the frontend cannot run yet are listed by name, so a case joins the check once it can run.
…outer join's nullable side is computed
SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows ... GROUP BY dst ORDER BY q DESC LIMIT 5 returns at most 5 values; the memo gives 96, the rows the window holds. Fails before the fix with Some(96).
`SELECT dst, approx_percentile_cont(bytes, 0.9) AS q FROM flows ... GROUP BY dst ORDER BY q DESC LIMIT 5` returns at most 5 values, but Memo::max_values_per_evaluation read the keys through the Limit and gave the 96 rows the window holds. The bound now also takes the rows the group itself returns (max_rows_per_evaluation), which caps a whole-input LIMIT by n, as it already does for a LIMIT below the grouping.
…name the kept identity checks The Join arm computed the right side's columns after the left's scans even for a semi or anti join, whose output keeps only the left side's columns. The lint header now names each kept `if let`/`matches!` and why every other variant's answer is "no", and says that arithmetic_sign's tuple match ends in an unchecked `_ => Unknown`, the safe answer there.
…ew-valued keys first The value-bounds oracle counted a case as run when any one of its bounds ran, so bounds that compile only alone (the n_q of a scalar() or vector() operand, a DISTINCT, a quantile over count_values) went unchecked silently. It now tracks each bound as `case:what` with the most values any dataset returned: EXECUTOR_GAPS lists the bounds the executor cannot run with the operator each lacks, and NEVER_REACHED the bounds that run but return nothing on every dataset (none now). Any other unrun or always-empty bound fails the test. p13_unless read `qot(0.9, m) unless qot(0.5, m)`, empty on every dataset; it now reads `a unless b` over two metrics. The crossed dataset gave the fastest digit to the label sorted first, so `dst` (300 values) cycled while `src` stayed fixed over the 96 rows a window reads. Declared components are now crossed smallest first: against the earlier bounds code the oracle finds j_full's 42 groups over a bound of 36, and j_left's 7 over 6, on the crossed dataset.
…up expects KMV at its planned 600M-row shape The δ split offers stored KLL windows at several k, so the KLL test compares the smallest k offered with the next larger one instead of fixed values. The distinct rollup test asserted exact counts. Measured, KMV is 1.26x faster at 60,000 rows per state (the planned shape) and exact is 1.39x faster at 12,000. The test is renamed for what it asserts and records the crossover.
…ollup test by rows per state
…tinct rollup counts exactly at 12,000 rows per state and builds a KMV at 60,000 Measured on the executor over 132 builds, the KMV build is charged 0.62-0.84 of its time (at k = 4,323 and 60,000 rows per state, 553 ns per row charged against 712 measured). The rollup tests vary the series' sample interval, which sets the rows the planner prices.
…r compared hash Refit on 132 executor builds (10 to 10,000 states, 1,200 to 60,000 rows per state, 10,000 to 1,000,000 distinct values, k = 1,081 to 106,263): the leave-one-setting-out error is 0.058, and measured / charged is 0.81-1.27, against 1.17-1.63 at 65 and 0.50. The KMV merge keeps its own constants (65 per hash, 0.5 per compared hash), which its measured rollup matches.
Measured on the executor's SummaryMerge over stored KMV states (median of 3 runs on one pinned core), 5 to 200 states per group at k = 1,081 to 106,263: the merge is charged 1.35x to 4.9x its time (200 states of 1,200 rows at k = 1,081: 88,151 ns charged per state, 18,109 measured).
…compared hash, with states sharing hashes Refit on 45 executor merges of stored KMV states (5 to 200 states per group, 1,200 to 60,000 rows each, k = 1,081 to 106,263): the leave-one-setting-out error is 0.050, and measured / charged is 0.90-1.08, against 0.21-1.10 at 65 and 0.5. Each state retains the smallest hashes of the values it folded, so merged states share hashes; StateInputRows now carries the folded column's distinct values, which the merge uses for that. On 24 held-out rollups, measured / charged is 0.84-1.14 (RMS log error 0.091, was 0.436).
zzylol
force-pushed
the
fix/kmv-build-cost-refit
branch
from
November 16, 2026 22:48
c9f874c to
8632711
Compare
… distinct-values plumbing and StateInputRows::merged - The expected distinct values among n draws from V values, -V * exp_m1(-n / V), was written three times (exact distinct pairs, KMV compared hashes, KMV merge folded values); one private helper now. - The KMV rollup merge test's producer declares the 1,000,000 users of the run it quotes, so it covers distinct_values reaching the merge. - StateInputRows::merged keeps the larger distinct_values and None when either is unknown; a test checks both. - Reflow two doc comments in rollup_controlled.rs.
zzylol
force-pushed
the
fix/delta-split-over-dependent-readouts
branch
from
November 16, 2026 23:48
50cf197 to
793859e
Compare
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.
Closes #1373. Stacked on the δ-split PR (base
fix/delta-split-over-dependent-readouts), whose k = 4,323 sizing exposes these charges.Summary.
StateInputRowsnow carries the folded column's distinct values for that.Review guide.
crates/cost/src/node/operations_per_unit.rs. It holds the four KMV constants,update_operations_per_rowandmerge_operations_per_state.StateInputRows::distinct_valuesincrates/cost/src/node/state_bytes/mod.rs, and wherenode_resource_usage_and_costsets it (crates/cost/src/node/usage/mod.rs).crates/cost/src/node/usage/tests.rs:a_kmv_build_charges_its_measured_time_per_row(new);a_kmv_build_costs_per_retained_hash_it_compares(re-measured);a_kmv_merge_charges_its_measured_time_per_state(new);a_kmv_rollup_merge_costs_per_hash_it_inserts(re-measured).crates/integration-tests/tests/rollup_controlled.rs.3ae2dc498tests only; the build tests fail on the base.68a1efa0fthe build fix.310f03f5atest only; the merge test fails on68a1efa0f.8632711b3the merge fix.Why
The planner should pick the plan that measures faster. In the viewer's distinct rollup (
rollupheavy-t11:COUNT(DISTINCT user_id)by service and by (service, endpoint), 5 min every 60 s, 1,000,000 users), measured whole plans flip with the rows per state:Each plan has to be charged its measured cost for the choice to follow that crossover. The KMV build was the term furthest off. Once it is fixed, 56 grid plans move work into merges and windows, so the merge has to be charged its measured cost too.
About the 12,000-rows-per-state case. It was reported as picking KMV on the base. That report came from a probe that changed only
arrival.rows_per_second; the planner prices a scan fromseries_count×sample_interval, so the probe still planned 600,000,000 rows. Planned at 12,000 rows per state (10,000 series sampled every 25 ms), the base already picks exact. The new testproduction_distinct_rollup_counts_exactly_at_12k_rows_per_statetherefore passes on the base too.What
StateInputRowsgainsdistinct_values: the distinct values of the column the state folds. The build sets it from the column's declared distinct values. The build charge now reads it there instead of computing it a second time. Merging keeps the largest.How
The executor's KMV is
asap_sketchlib::Kmv. Itsupdate_with_hashwalks every retained hash to find a duplicate, unless the state is full and the hash is above its largest. So an update is O(k) while the state fills and for each hash that enters it (about k·ln(n/k) of them once full). A merge updates the group's state with every hash the input retains, so it walks the same way. This is a library defect: a membership check could be O(1) or O(log k), and a merge could be an O(k) sorted merge instead of O(k²). That fix belongs inasap_sketchlibas its own PR, pinned here afterwards, and the KMV constants must be measured again after it. This PR describes the executor as it is today.kmv_compared_hashes_per_rowalready counts that walk: k × the fill fraction while filling, then the walks of the repeats and of the hashes that enter. The build fit keeps it as the size term.The merge passes it the hashes the states share:
Vvalues, a state that foldedDdistinct ones and retainshhashes draws them from theV·h/Dsmallest.Example: 1-minute states of (service, endpoint) at k = 4,323, each of 12,000 rows of 1,000,000 users. Each retains 4,323 hashes of about 11,930 users, drawn from the 362,000 smallest.
Key code interfaces added / modified
The private constants in
crates/cost/src/node/operations_per_unit.rs:StateInputRows(public,crates/cost/src/node/state_bytes/mod.rs) and the crate-private merge charge:pub struct StateInputRows { pub rows_per_state: Option<u64>, pub non_negative: bool, pub value_magnitude_range: Option<ValueMagnitudeRange>, + /// The distinct values the folded column takes, each folded row a draw from them; `None` when + /// not declared, which takes every row's value as new. E.g. `Some(1_000_000)` for `user_id`. + pub distinct_values: Option<u64>, } pub(crate) fn merge_operations_per_state( family: StateFamily<'_>, state_bytes: u64, merged_state_bytes: u64, - rows_per_input_state: Option<u64>, + input_rows: Option<StateInputRows>, input_states_per_group: u64, ) -> f64Usage, end to end:
Major algorithm changes
Cost model: the KMV build.
Problem: at 400 states × 60,000 rows of 1,000,000 values at k = 4,323, the build was charged 553 ns per row and measures 712. Every one of the 132 grid builds was under-charged, by 1.17–1.63x.
Before:
65 + 0.5 × comparedper row, fitted on 28 builds of 300,000 rows.After:
110 + 0.62 × comparedper row.How it was fitted: same method as the exact distinct refit.
SummaryAggby (service, endpoint): the build's own CPU (its time minus its input's), median of 3 runs on one pinned core, 10 rows per group per batch.Held-out shapes, not in the grid, each the median of 3 passes:
RMS log error is 0.094 (was 0.322). The first two rows are the shapes in
a_kmv_build_costs_per_retained_hash_it_compares. Their earlier measurements (191 and 7,410) do not reproduce on today's executor (329 and 8,357), so the test now quotes these.Cost model: the KMV merge.
Problem: the merge was charged 65 per hash plus 0.5 per compared hash, taking every merged hash as distinct. Measured, merges cost 0.21–1.10 of that charge on the grid and 0.33–1.58 on held-out rollups. The error grows with the states per group, and it reverses when states share many values.
Before:
retained × (65 + 0.5 × kmv_compared_hashes_per_row(k, retained × states, None))per input state.After:
retained × (2.6 + 0.43 × kmv_compared_hashes_per_row(k, retained × states, Some(shared)))per input state:retainedis the hashes a state keeps, at mostk: the distinct values among its rows.sharedis theV·h/Dsmallest hashes the states draw theirs from (see How).How it was measured:
SummaryMergeover KMV states stored as a materialized view: a 1-minute tumbling-window chain by (service, endpoint) over 5 minutes, read through its time range.ksmallest hashes of their rows'user_ids, hashed as the executor hashes them, inserted in random order.Grid: k = 1,081, 4,323, 26,656 and 106,263 × window states of 1,200, 12,000 and 60,000 rows of 1,000,000 users × 5, 10, 50 and 200 states per group. That is 45 merges; states per group times state size was capped at a few minutes of CPU.
Forms compared by leave-one-setting-out error, where a setting is (k, rows per state, states per group):
Held-out rollups, not in the grid: states merged from 5 windows (6,000 to 300,000 rows each), then rolled up 10 or 100 per group. Each rollup is its node's time less the window merge below it, median of 3 runs. ns per input state, in ms:
This is 12 of the 24. Over all 24, measured ÷ charged is 0.84–1.14 and the RMS log error is 0.091 (was 0.33–1.58 and 0.436).
k = 106,263, 10 × 11,929row is the rollup ina_kmv_rollup_merge_costs_per_hash_it_inserts. It now quotes 299 s for its 1,000 states (median of 3 on an idle core), not the earlier 352 s (a plan's time less its build's).One state per group. A merge whose groups each read one state passes it through. Measured that way, it costs 2–10 µs per state at k = 1,081 and 106,263 (1,000 states). The cost model sends a merge that keeps its input's groups down the pass-through path. A rollup whose estimates tie its input's (10,000 (service, endpoint) states into 10,000 services) still reaches this charge with one state per group. It is then charged as one state filled from empty: 3.9 ms per state at k = 4,323, the most any number of states per group costs. That rule predates this PR and is unchanged (see Limitations).
Correctness: the charge stays O(1) per node (the existing 64-slice average). No search or MILP change.
Before this PR
On the base, the viewer rollup at 600,000,000 rows per evaluation is charged 394.0 s per evaluation for a query-time KMV build (331.9 s of it the build), and 589.8 s for exact. The build measures about 1.3x its charge.
After this PR
The planner selects the KMV built in 1-minute tumbling windows at ingestion, then merged over the 5 windows at each evaluation. The three plans are charged:
At the build-only refit (
68a1efa0f) they were charged 453, 491 and 590.Measured end to end at a scaled shape with the same rows per state:
The planner's choice is the measured fastest. Measured, the window plan is 1.26x faster than the query-time build and 1.8–2.0x faster than exact, with either data mapping. Scaled ×10 to the planned 10,000 series, that is about 313 s, 395 s and 571–629 s per evaluation, against 401, 481 and 590 charged.
Evidence
Plan survey, base (#1360 head) against this PR, through
run_default_search_overwith the default and the per-resource (UsageOverTime) cost inputs:fix(cost): charge sketch states and merges, raw batch visits, grouping keys, distinct counts and KMV at their measured costs, so the measured-best plan is selected #1363's sweep (202 workloads): the grid of 120, showcase 24, held-out 48, 2,000-series 10. All 202 plans are identical node for node, and no cost changes. None of them contains a distinct count.
Distinct-count grid (220 plans):
production_rollup_workloads), plus an ungroupedCOUNT(DISTINCT user_id).rollupheavy-t11and the planner-layering example 2.Project-order changes, and the merge refit's changes below.The merge refit, against the build refit alone (
68a1efa0f): 29 plans change.Projectheld for the evaluationProjectstreamed (same operators, same cost)Verification
PR is
8632711b3. Base counts are from5e17d5727; the #1360 head50cf1976aadds only an assertion todefault_cost_plan_ranking.rsand renames a test.asap-costcargo test --release -p asap-costasap-cost, merge test commit only (310f03f5a)cargo test --release -p asap-cost --lib a_kmv_merge_chargesasap-stage3-plan-selectionlibcargo test --release -p asap-stage3-plan-selection --libasap-integration-testscargo test --release -p asap-integration-tests --no-fail-fastcargo fmt --all --checkcargo clippy -p asap-cost --all-targetsasap-costjoint_search_equivalence×2,plan_sharing×2,promql_to_post_asap::promql_quantile_of_rate_binds_kll_over_rate_accumulator,materialized_views::materialized_views_equal_the_stored_outputs_main_listed.-D warnings, clippy stops at an unusedStorageMediumimport inasap-types, a file this PR does not touch.New and changed tests:
a_kmv_build_charges_its_measured_time_per_row(new): six measured builds (10 and 1,000 states, 120,000–12,000,000 rows, k = 1,081–106,263) are each charged within 0.8–1.25 of their time. It fails on the base (109 charged vs 176 measured at k = 1,081).a_kmv_build_costs_per_retained_hash_it_compares: now quotes the re-measured 329 and 8,357 ns per row. It fails on the base (222 vs 329).a_kmv_merge_charges_its_measured_time_per_state(new): six measured merges (5 to 200 states per group, k = 1,081–106,263, one of 20,000 users) are each charged within 0.8–1.25 of their time. It fails on the build refit (4,223,195 charged vs 3,115,412 measured for 5 window states at k = 4,323; 88,151 vs 18,109 for 200 states at k = 1,081).a_kmv_rollup_merge_costs_per_hash_it_inserts: now quotes the re-measured 299 s, within 0.8–1.25 (was 352 s, within 0.75–1.33).production_distinct_rollup_counts_exactly_at_12k_rows_per_state(new): at a sample every 25 ms, both queries count exactly. It passes on base and PR (see Why).production_distinct_rollup_builds_kmv_at_60k_rows_per_state(changed): the shape is now set bysample_interval, and a KMV built in tumbling windows also counts as a KMV build.Design scope
StateInputRows::distinct_values(cost, the description of what one summary state folds). It is the folded column's declared distinct values. Any summary whose cost depends on repeated values can read it, not only KMV.Architectural decisions
Limitations and follow-up
asap_sketchlib: O(1)/O(log k) duplicate check and O(k) sorted merge (KMV insert and merge are O(k) and O(k²) in asap_sketchlib; unusable at the k the planner sizes #709). After it is pinned, measure the KMV build and merge again.distinct_valuesis the column's declared distinct values. When a workload declares none, the merge takes every hash as distinct. That charges a merge of states that share many values at about 0.36–0.68 of its time.Human review — do not complete with an agent