rypipe_core/engine/predicate.rs
1use crate::value::Value;
2use smallvec::SmallVec;
3
4/// Outcome of a predicate evaluation for one row.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub enum PredicateState {
8 #[default]
9 Undecided,
10 Pass,
11 Fail,
12}
13
14/// Heap-allocated row buffer for predicate-first deferred materialization.
15/// Boxed so that unfiltered parses (the common case) don't carry 1 KB of
16/// inline SmallVec in every `TableBuilder`.
17pub(crate) struct RowBuffer {
18 /// Per-row field buffer: `(slot_index, value)`. u32 is the column index
19 /// from `field_index`, eliminating per-field String allocation.
20 pub(crate) fields: SmallVec<[(u32, Value<'static>); 32]>,
21 pub(crate) state: PredicateState,
22 /// Bitmask of predicate column slots. Bit `i` is set when slot `i` appears
23 /// in the filter predicate. For ≤64 columns this is a single u64; above
24 /// 64 it grows like `row_dirty`.
25 pub(crate) predicate_mask: SmallVec<[u64; 1]>,
26 /// When true, the predicate has already resolved to Pass and remaining
27 /// fields are pushed directly to columns (no buffering).
28 pub(crate) direct: bool,
29 /// Learned ordinal of the predicate column (0-based), set after the first
30 /// row. When the predicate is late (> 4/5 of columns), buffering is a net
31 /// loss; we switch to direct push + pop-on-reject instead.
32 pub(crate) predicate_ordinal: Option<u32>,
33 /// Whether the adaptive strategy has decided buffering is worthwhile.
34 /// True by default; set to false on row 2 when predicate_ordinal is late.
35 pub(crate) buffer_worthwhile: bool,
36 /// Resolved predicate field names, cached once by `build_predicate_mask`.
37 /// Used to check newly-created columns against the filter without cloning
38 /// the filter tree on every non-predicate field.
39 pub(crate) pred_names: SmallVec<[String; 4]>,
40}
41
42impl RowBuffer {
43 pub(crate) fn new() -> Self {
44 Self {
45 fields: SmallVec::new(),
46 state: PredicateState::Undecided,
47 predicate_mask: SmallVec::new(),
48 direct: false,
49 predicate_ordinal: None,
50 buffer_worthwhile: true,
51 pred_names: SmallVec::new(),
52 }
53 }
54}