Repository navigation
feat(cost): IN-list selectivity for SIL bindings, the top-k share under a declared skew, and the compensation filter below the window merge - #1559
Draft
zzylol wants to merge 8 commits into
Conversation
…n filter below the time merge Test-first phase 3 of the IN-list selectivity plan (sil-inlist-selectivity): cost unit tests U1-U16, group_count_bound tests T1-T4, asap_planner end-to-end tests E1-E7 over drill_cnt x1 fixtures, SIL facade tests S1-S3, and Q5 tests (Pass 2 offers Filter below the time merge; forced-lightcone cost; hole guard). Signatures only, current behaviour kept: group_count_bound takes an unread kept_values_per_column; DataFactAssumption::UniformRowsPerValue (never emitted yet). New-behaviour tests fail on assertions; anchors pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…d skew A `Scan`'s predicates and a `Filter`'s keep their IN-lists' share of the rows (`kept_share`): k of a column's N declared values keep k/N (rows per value assumed uniform, warned as `UniformRowsPerValue`), or the heaviest k values' Zipf mass H_k(s)/H_N(s) when the sources declare `zipf_exponent_per_label` and the values stand for a SIL binding from an upstream count-ranked top-k (`DataWorkload::heaviest_value_placeholders`, added by the intent lowering). A query-time scan reads only the selected rows (series selected by label); an ingestion-time scan sees every row. `OutputFacts::kept_values_per_column` carries the restricted columns up; groups and per-series counts use the kept share (`group_count_bound`), and an ingestion-time grouping reads its input's rate. Stage 3's input baseline keeps a column restricted by every candidate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
A `Filter` on a summary state group's keys gets the candidate
`SummaryMerge{group_by: keys}(Filter(p, W))`, `W` the chain windows the
group's window merge reads, so the merge reads only the kept keys' states
(drill_cnt's lightcone: 10 of 10,000 hosts' states, not 1,003,333).
Aggregate coverage derives for a key filter over a chain's windows, and a
regrouping merge maps keys past the windows' `window_end`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
- E2/Q5-E3/S2: the lightcone's chain build is the same for every |$Y|; the wider levels kept over it may differ (they trade off against the query-time merge, which now shrinks with |$Y|), so only the chain lines are compared, and the windows each filter reads may differ. - Q5-E2: the selection finds a cheaper shape than the plan's estimate (one 10 s level filtered before the merge, 89.03/s): at most ~97/s. - S1: q2's time range over the rows read grows 10x from |$Y| = 5 to 50; its scan's kept raw data and per-request cost do not scale. - planner_fuzz (l): the numerator's label matcher keeps its share, so the plain graft may be the cheapest witness. - New: E8 (heaviest bound hosts keep H_10/H_10000 under Zipf(1); literal hosts keep k/N) and the lowering marking a count-ranked binding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
… state With one host bound, the compensation filter keeps 10 of the windows' 1,003,333 states, and the merge by the windows' keys counted its states per group as rows / groups (10/1 vs 10.03), so it folded 60.14 rows a state where the merge of all windows folds 60 and Stage 3's same-group check failed. A regrouping merge keeping each window's groups now merges its input's (exact) windows per group. The uniform rows-per-value warning names the missing `zipf_exponent_per_label`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…rows' Under a declared skew the heaviest 10 of 10,000 hosts hold 0.2993 of the rows but 10/10,000 of the per-key states (one per key and window). The compensation filter over the lightcone's windows kept 0.2993 of the states, so the merge by key folded ~18,000 rows a state against 60 for the merge of all windows. `KeptShare::value_share` (k/N per column) is what a filter over states keeps; a filter or scan over rows keeps `share`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
A key filter over states keeps the values' share of them; under a declared
skew each kept state then folds the rows' share over the values' share of
the average (0.2993 / 0.001 for the heaviest 10 of 10,000 hosts), as a
query-time build over the same selected rows does, so Stage 3's same-group
check holds (E6 drill_cnt with `zipf_exponent_per_label {host: 1.0}`).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…the executor The Q5 shape the planner picks when the lightcone pays, a `Filter` on the keys over a chain's windows read by a `SummaryMerge` by the chain's keys (then merged by user), planned from SQL and run on the executor's data plane: every view refreshed in ingestion runs, the query answered from the windows, each user's count of the selected service's rows exact. The executor already runs it (the merge groups by its keys whatever window a state is from); this pins it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Contributor
Author
|
Follow-up dae00dc: |
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
host IN $Y(10 of 10,000 hosts) was priced as reading every host, so E6drill_cntx1 cost 177.91/s on demand and the planner kept a (host, user) lightcone (169.26/s).Scan's predicates and aFilterkeep their IN-lists' share of the rows. That share is k/N, or the top-k Zipf mass when the sources declare a skew and the values are a binding from an upstream count-ranked top-k.drill_cntx1 costs 28.90/s in all three modes; the lightcone is not kept. Forced onto the lightcone, it costs 89.03/s instead of 169.26/s.Stacked on #1553 (base
feat/sil-intent). The draft #1555 (cost calibration) stacks on this branch. The per-row constants (scan vs merge) are being recalibrated separately in #1555, so the absolute numbers below may still move.Review guide
feat/sil-intent(feat(sil): plan SIL intents: parse/check, lowering, hole guard, planning modes, group latency (L_G) #1553), rebased on its head b9c7fdf.feat(cost): IN-list selectivity and the skew share.feat(pass2): the filter below the window merge.test: test definitions that changed (listed below), plus the skew tests.crates/cost/src/node/usage/kept_share.rs, the whole rule.share_of_rows_of_valuesis the one function a skew estimate replaces.crates/cost/src/node/usage/mod.rs:kept_by_predicate;Scanarm: at query time it reads only the selected rows; at ingestion time it sees every row;Filterbranch of the pass-through arm;rows_readat ingestion time, which now uses the input's rate;crates/stage1-pass2-reuse-candidates/src/rules/filter_below_window_merge.rs;crates/types/src/stages/logical/group/aggregate_coverage.rs:narrowed_by, and thewindow_endoffset inregrouped.unchanged_source_columnsnow passes columns throughTimeRangeCoverandSummaryMergeWindows;kept_values_per_column;StateInputRows::folds_the_same_asalso accepts rows within 0.1%.heaviest_value_placeholders: Vec::new()added toDataWorkloadliterals in tests and frontends;kept_values_per_column: BTreeMap::new()added toOutputFactsliterals in cost tests.Behavior: before → after
E6
drill_cntx1, planned withasap_planner::intent::plan_intentsunder the default cost model (cost model 1):Filter(host IN …)over the 10 s windows, then merge by key (6,000 → 100 states)zipf_exponent_per_label {host: 1.0}declaredThis serves the eval (E6, the value of intents) and correctness: the predicted cost of a dependent query now follows the number of values bound.
Interfaces
crates/types/src/workload/data.rs/// What is known about one source. (Stays.) pub struct SourceFacts { pub series_count: Option<u64>, pub metric_type: Option<PrometheusMetricType>, pub column_signs: BTreeMap<String, ValueSign>, pub column_magnitudes: BTreeMap<String, ValueMagnitudeRange>, pub sample_interval: Option<DurationMs>, pub stream_norms: Option<StreamNorms>, pub max_distinct_per_evaluation: Option<u64>, pub distinct_values_per_label: BTreeMap<String, u64>, + /// The Zipf exponent s of rows over a label's values (rank i holds a share ∝ 1/i^s), e.g. + /// `{"host": 1.0}`. An IN-list of the heaviest k of N values keeps H_k(s)/H_N(s) of the rows. + /// Default: none (every IN-list keeps k/N). Omitted from JSON when empty. + pub zipf_exponent_per_label: BTreeMap<String, f64>, pub distinct_values_per_label_set: Vec<LabelSetDistinctValues>, pub rows_held: Option<u64>, } /// The data workload. (Stays.) pub struct DataWorkload { pub arrival: DataArrival, pub sources: BTreeMap<String, SourceFacts>, + /// The first placeholder of each SIL binding filled by an upstream count-ranked top-k + /// (`ORDER BY … DESC LIMIT k` lowered to `TopK`), which the intent lowering adds: an IN-list + /// holding one keeps the top-k share under the column's declared skew. Default: empty. + pub heaviest_value_placeholders: Vec<ScalarValue>, }(
SourceFactsis shown with the fields this PR leaves alone, as of #1553.)crates/types/src/workload/assumed_data_fact.rs/// The default the planner used for one undeclared fact. (Stays.) pub enum DataFactAssumption { DDSketchValueMagnitudeRange { column: Option<String>, assumed_range: ValueMagnitudeRange }, InOrderArrival, + /// An IN-list on `column` was costed at k/N of the rows because no source declares + /// `zipf_exponent_per_label` for it. Cost only: the accuracy guarantee does not depend on it. + UniformRowsPerValue { column: String }, }crates/types/src/workload/bounds.rs/// The groups a `by (keys)` is priced at. (Stays.) pub fn group_count_bound( keys: &GroupKeys, input: Option<&Schema>, sources: &BTreeSet<&str>, unchanged_input_columns: &BTreeSet<&str>, data: &DataWorkload, rows_read: u64, + /// The distinct values left of each column that IN-lists below restrict: a restricted key + /// counts its kept share; the declared series scale by every restricted column's share. + kept_values_per_column: &BTreeMap<&str, u64>, ) -> u64;crates/cost/src/node/usage/mod.rs/// What one node outputs, as its consumers cost it. (Stays.) pub struct OutputFacts<'a> { pub statistics: EdgeStatistics, pub sources: BTreeSet<&'a str>, pub family: StateFamily<'a>, pub unchanged_columns: BTreeSet<&'a str>, pub tumbling_windows: f64, pub chain_tumbling_window_width_ms: Option<u64>, pub state_input_rows: Option<StateInputRows>, pub states_last_merged: bool, pub raw_rows_span_ms: Option<u64>, pub raw_rows_per_ms: Option<f64>, pub folded_per_row: Option<StateInputRows>, pub raw_rows_short_of_range: Option<RawRowsShortOfRange>, pub state_data_span_ms: Option<u64>, pub raw_rows_shifted_back_ms: u64, + /// The distinct values left of each source column that an IN-list below restricts, for the + /// columns passed on unchanged; absent means unrestricted. E.g. `host IN (10)` over `auth`: + /// `{host: 10}`. + pub kept_values_per_column: BTreeMap<&'a str, u64>, }Usage, end to end (
crates/planner-cli/tests/inlist_selectivity.rs::the_heaviest_bound_values_keep_their_zipf_share):Algorithm changes
IN-list share (cost model)
FilterandScanpassed every row on, and their predicates were never read. In drill_cnt, q2 was charged 6,000,000 rows into 100,000 groups whatever |$Y| was.Scan's predicates or aFilter's, each conjunct thatconstraint_ofreads ascolumn IN (k),= v, or anORof=, where the column is a source column passed on unchanged and N is declared.values_after = min(k, values_before). A column restricted below keeps the values it has left.share_of_rows_of_values(after, before, zipf). That is H_after(s)/H_before(s) when the values are a heaviest-values binding and the column declares s, else after/before.rows_kept(rows, share)(max(1, round(rows × share))); the rate is scaled too.value_shareof them, and each kept state foldsshare / value_sharetimes the average rows.Scanreads only the selected rows (Q1). An ingestion-timeScansees every row and outputs the share.kept_values_per_column).group_count_boundcounts a restricted key's kept share and scales the declared series.Inkeeps share 1. These still pass: U12–U14, T4, and E6 (177.9132 / 169.2572 / 140.9361 withhostundeclared). Corpus snapshots, sketch snapshots and goldens are unchanged.UniformRowsPerValue).ORDER BY p99 DESC LIMIT 5is not: its values are not the heaviest by rows.NOT INand ranges keep share 1 (Q3).Compensation filter below the window merge (Pass 2)
Filtercould only read the time-merged state. The time merge paid for all 1,003,333 states whatever |$Y| was (34.09/s).Merge_by(user)(Filter(p, Merge(TimeRangeCover(levels)))).Merge_by(user)(Merge_by(keys)(Filter(p', W))), whereWis the chain windows (TimeRangeorTimeRangeCover).add_filters_below_window_mergesruns afteradd_chain_groups, for everyFilteron a state group's keys.narrowed_by), and a regrouping merge maps keys pastwindow_end.Filterholds the placeholders and runs only at query time (Q5-E3,hole_filter_never_runs_at_ingestion_and_bind_slots_name_it).Filterof windows does not derive its schema.SummaryMergeover aFilterof chain windows.Decisions and open questions
Decided
UniformRowsPerValuewarning. Superseded the same day by the next item.zipf_exponent_per_label. Uniform k/N, with the warning, only when no skew is declared. Rejected: matching placeholder text in the cost model; a separate skew type.Inconstraints count (IN,=, OR of=).Test definitions that changed. These are definitions, not regressions:
ratio_witness_costs_over_the_shared_rate: the plain graft may now be the cheapest witness, because the numerator's label matcher keeps its share of the rows.a_finest_common_group_no_query_asks_for_serves_both: the key filter over G* now also has the merge-over-filtered-windows candidates (14 = 7 + 7).Needs reviewer decision: none blocking.
StateInputRows::folds_the_same_astolerates 0.1%; it only feeds a debug assertion.Verification
Run on the build host, debug profile, at commit 0c51235.
asap-cost,asap-plannerandinlist_selectivitywere re-run at 86ea4fd. Clippy is clean on all changed crates.asap-costlib (U1–U16,kept_shareunit tests)asap-typeslib (T1–T4, W1)asap-stage1-pass2-reuse-candidateslibasap-stage3-plan-selectionlibasap-planner--test planner(sil_intents S1–S3, the lowering marks the heaviest values)asap-planner-cliplanner_cli(inlist_selectivity E1–E9, Q5-M1, Q5-E1–E3)asap-planner-clicorpus_snapshots / sketch snapshots / goldensasap-devtools(includes planner_fuzz)E6 x1 predicted costs, cost model 1, with |$Y| set to each config's LIMIT k. These are the predictions before the constants are recalibrated in #1555. In every config, the planner's full-intent plan keeps no lightcone at the configured |$Y|.
Lightcone break-even. For each |$Y|, the on-demand plan is compared with the plan forced onto the lightcone (no query-time scan may read the placeholder):
NoValidCandidate), so there is no lightcone plan to compareThe forced-lightcone cost does not change with the skew, because a filter over per-key states keeps the values' share, k/N. The skew raises only the on-demand side.
Follow-ups
zipf_exponent_per_label.NOT IN.SummaryMergeover aFilterof chain windows. The planner selects this only when the lightcone pays (dash at |$Y| ≳ 25).sil-inlist-selectivity.mdin the private notes. The Zipf fact here supersedes it; it was not filed.🤖 Generated with Claude Code