From a884d5401db29628357938d819391adba2f995ba Mon Sep 17 00:00:00 2001 From: zzylol Date: Sat, 5 Dec 2026 00:54:30 +0000 Subject: [PATCH 1/2] test(data_plane): field restrictions over many series match a plain filter Guard for judging a keyed table's field restriction once per series per read rather than once per series per chunk: 2,000 series over 6 panes, field-only, key-and-field and key-only restrictions, with and without the read cache, equal a plain filter. Passes before and after the change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K1M6bpWvKMxYBvtAgpDYGp --- data_plane/src/store.rs | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/data_plane/src/store.rs b/data_plane/src/store.rs index ec7b4ee1..efa13269 100644 --- a/data_plane/src/store.rs +++ b/data_plane/src/store.rs @@ -2911,6 +2911,53 @@ mod tests { assert!(batches.is_empty()); } + /// A field restriction on a keyed table is judged per series once per + /// read, not once per series per chunk: 2,000 series (one per job) over + /// 6 panes, `instance IN (..)` on the field, with a key restriction too + /// and with a pane where the field never takes the values; the rows + /// equal a plain filter's, and with the restriction on the key only the + /// selected series' rows are read. + #[test] + fn field_restrictions_on_many_series_match_a_plain_filter() { + let jobs: Vec<&'static str> = (0..2_000) + .map(|j| &*Box::leak(format!("j{j}").into_boxed_str())) + .collect(); + let mut rows: Vec = Vec::new(); + for t in (0..30_000).step_by(1_000) { + for (j, job) in jobs.iter().enumerate() { + // i0..i3 rotate over the jobs; i3 never before 20 s. + let i = (j + t as usize / 1_000) % 4; + if i == 3 && t < 20_000 { + continue; + } + rows.push((job, format!("i{i}"), t + 1, (j + t as usize) as f64)); + } + } + let mut keyed = table_data(&[keyed_layout(&rows)]); + keyed.seal_through(30_000); + let cases: Vec<(Vec>, Box bool>)> = vec![ + ( + vec![text_is(2, &["i1", "i3"])], + Box::new(|r| r.1 == "i1" || r.1 == "i3"), + ), + ( + vec![text_is(1, &["j7", "j1999"]), text_is(2, &["i0", "i2"])], + Box::new(|r| (r.0 == "j7" || r.0 == "j1999") && (r.1 == "i0" || r.1 == "i2")), + ), + (vec![text_is(1, &["j42"])], Box::new(|r| r.0 == "j42")), + ]; + for (predicates, keep) in cases { + for reads in [None, Some(RawReads::default())] { + let (batches, _) = table_read_with(&keyed, None, &predicates, reads.as_ref()); + let expected: Vec = rows.iter().filter(|r| keep(r)).cloned().collect(); + assert_eq!( + sorted(rows_of(&batches)), + sorted(table_read(&expected, None)) + ); + } + } + } + /// Eviction drops the panes ending at or before the cutoff, keeps the /// pane the cutoff falls in, and no read returns a sample at or /// before the cutoff: a read of a range starting before it fails (its From ba11870ab00bf45277ae92d98d05ae3946efcc76 Mon Sep 17 00:00:00 2001 From: zzylol Date: Sat, 5 Dec 2026 01:14:57 +0000 Subject: [PATCH 2/2] perf(data_plane): judge a series' lacking labels against a chunk's fields by bitmask A read's label selection judged each series once, but a series lacking a restricted label (a keyed table's field, e.g. login_fail's src under series_key [user]) was re-checked on every chunk by name: one series label lookup and one field lookup per restricted label, per series, per chunk. The verdict now keeps the lacking labels as a bitmask, and each chunk computes once which restricted labels it holds as fields; a series is then one mask test per chunk. Same selection. SB q3 at x4 (count distinct user by target over 1 h of login_fail where src IN $A): 11.9 -> 10.5 s p50. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K1M6bpWvKMxYBvtAgpDYGp --- data_plane/src/store.rs | 65 +++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/data_plane/src/store.rs b/data_plane/src/store.rs index efa13269..10e0addc 100644 --- a/data_plane/src/store.rs +++ b/data_plane/src/store.rs @@ -401,11 +401,11 @@ impl Chunk { } allowed_entries.push((index, allowed)); } - let is_field = |label: &str| self.field(label).is_some(); + let fields = verdicts.field_mask(|label| self.field(label).is_some()); let kept: Vec = self .series .iter() - .map(|series| verdicts.keeps(series, is_field)) + .map(|series| verdicts.keeps(series, fields)) .collect(); self.cut(&kept, &allowed_entries) } @@ -751,8 +751,12 @@ impl RawData { /// a series is judged once per read, however many chunks hold it. struct SeriesVerdicts { selection: Arc, - /// By series id: 0 not judged yet, 1 selected, 2 not. + /// By series id: 0 not judged yet, 1 selected, 2 not, 3 selected if + /// the chunk holds as fields the restricted labels the series lacks. by_id: std::cell::RefCell>, + /// By series id, for verdict 3: the restricted labels it lacks, one bit + /// per label in the selection's order. + lacks_by_id: std::cell::RefCell>, } impl SeriesVerdicts { @@ -760,41 +764,70 @@ impl SeriesVerdicts { Self { selection, by_id: Default::default(), + lacks_by_id: Default::default(), + } + } + + /// The restricted labels (one bit each, in the selection's order) that + /// `is_field` says a chunk holds as fields; judged once per chunk. + fn field_mask(&self, is_field: impl Fn(&str) -> bool) -> u64 { + // Bit 63 stands for every label from the 64th on: set only when + // the chunk holds all of them. + let mut tail_fields = true; + let mut mask = 0; + for (index, label) in self.selection.restrictions().keys().enumerate() { + let field = is_field(label); + if index >= 63 { + tail_fields &= field; + } else if field { + mask |= 1 << index; + } + } + if tail_fields { + mask |= 1 << 63; } + mask } /// Whether `series` holds one of the allowed values of every - /// restricted label, a label it lacks passing when `is_field` says the - /// chunk holds it as a field (its rows are then judged by value). - fn keeps(&self, series: &Series, is_field: impl Fn(&str) -> bool) -> bool { + /// restricted label, a label it lacks passing when the chunk holds it + /// as a field (`fields`: [`Self::field_mask`]; its rows are then judged + /// by value). A series is judged once per read; per chunk only its + /// lacking labels are checked against `fields`. + fn keeps(&self, series: &Series, fields: u64) -> bool { let mut by_id = self.by_id.borrow_mut(); let id = series.id as usize; if by_id.len() <= id { by_id.resize(id + 1, 0); } if by_id[id] == 0 { - let mut lacks = false; + let mut lacks: u64 = 0; let mut holds = true; - for (label, values) in self.selection.restrictions() { + for (index, (label, values)) in self.selection.restrictions().iter().enumerate() { match series.get(label) { Some(value) => holds &= values.contains(value), - None => lacks = true, + // Beyond 64 labels every one counts as lacking: such a + // series is kept only when its chunk holds them all. + None => lacks |= 1 << index.min(63), } } - by_id[id] = match (holds, lacks) { + by_id[id] = match (holds, lacks != 0) { (false, _) => 2, (true, false) => 1, - (true, true) => 3, + (true, true) => { + let mut lacks_by_id = self.lacks_by_id.borrow_mut(); + if lacks_by_id.len() <= id { + lacks_by_id.resize(id + 1, 0); + } + lacks_by_id[id] = lacks; + 3 + } }; } match by_id[id] { 1 => true, 2 => false, - _ => self - .selection - .restrictions() - .keys() - .all(|label| series.get(label).is_some() || is_field(label)), + _ => self.lacks_by_id.borrow()[id] & !fields == 0, } } }