Skip to main content

rudb_vector/
validity.rs

1//! Which values in a vector are not null.
2//!
3//! `spec/07-execution.md` section 7.1: validity has three representations and the distinction is
4//! load-bearing. All valid is the absence of a mask and gets the fastest kernels. All invalid is a
5//! flag and short circuits entirely. Anything else is a bitmap.
6//!
7//! Photon's published result is that separate no-null kernels are worth a measurable amount on
8//! real data, because real data is mostly not null. The cost of knowing which case you are in is
9//! one branch per vector rather than one per value, which is why the three cases are an enum here
10//! rather than a bitmap that happens to be all ones.
11
12/// Which values in a vector are valid.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum Validity {
15    /// Nothing is null. No mask is stored and the kernels that read this take the fast path.
16    AllValid,
17    /// Everything is null. Most operators can answer without looking at the data at all.
18    AllInvalid,
19    /// Some of each, one bit per value, set meaning valid.
20    Mask(Bitmap),
21}
22
23impl Validity {
24    /// Whether the value at `index` is not null.
25    ///
26    /// Out of range reads report invalid rather than panicking, because this is called from
27    /// kernels that are allowed to read past the end of a partially filled vector.
28    #[must_use]
29    pub fn is_valid(&self, index: usize) -> bool {
30        match self {
31            Self::AllValid => true,
32            Self::AllInvalid => false,
33            Self::Mask(mask) => mask.get(index),
34        }
35    }
36
37    /// Whether any value in the first `len` is null.
38    #[must_use]
39    pub fn has_nulls(&self, len: usize) -> bool {
40        match self {
41            Self::AllValid => false,
42            Self::AllInvalid => len > 0,
43            Self::Mask(mask) => mask.count_valid(len) != len,
44        }
45    }
46
47    /// How many of the first `len` values are not null.
48    #[must_use]
49    pub fn count_valid(&self, len: usize) -> usize {
50        match self {
51            Self::AllValid => len,
52            Self::AllInvalid => 0,
53            Self::Mask(mask) => mask.count_valid(len),
54        }
55    }
56
57    /// Collapses a mask that turned out to be uniform back to one of the flag cases.
58    ///
59    /// Worth doing at the end of any operation that builds a mask, because every kernel
60    /// downstream then gets to take the branch it wants rather than walking a bitmap to find out
61    /// what it already could have been told.
62    #[must_use]
63    pub fn normalize(self, len: usize) -> Self {
64        match self {
65            Self::Mask(ref mask) => {
66                let valid = mask.count_valid(len);
67                if valid == len {
68                    Self::AllValid
69                } else if valid == 0 {
70                    Self::AllInvalid
71                } else {
72                    self
73                }
74            }
75            other => other,
76        }
77    }
78
79    /// The validity of a vector where `index` has just been made null.
80    ///
81    /// Takes and returns by value because setting a null on an `AllValid` vector has to
82    /// materialize a mask, and hiding that behind `&mut self` hides an allocation.
83    #[must_use]
84    pub fn with_null(self, index: usize, len: usize) -> Self {
85        let mut mask = match self {
86            Self::AllValid => Bitmap::all_valid(len),
87            Self::AllInvalid => return Self::AllInvalid,
88            Self::Mask(mask) => mask,
89        };
90        mask.set(index, false);
91        Self::Mask(mask)
92    }
93
94    /// Validity built from a per-value predicate, normalized.
95    pub fn from_iter(len: usize, valid: impl Fn(usize) -> bool) -> Self {
96        let mut mask = Bitmap::all_valid(len);
97        for index in 0..len {
98            if !valid(index) {
99                mask.set(index, false);
100            }
101        }
102        Self::Mask(mask).normalize(len)
103    }
104
105    /// The validity of a value that is valid in both inputs, which is what almost every binary
106    /// operator wants and is worth having in one place.
107    #[must_use]
108    pub fn and(&self, other: &Self, len: usize) -> Self {
109        match (self, other) {
110            (Self::AllInvalid, _) | (_, Self::AllInvalid) => Self::AllInvalid,
111            (Self::AllValid, Self::AllValid) => Self::AllValid,
112            (Self::AllValid, right) => right.clone().normalize(len),
113            (left, Self::AllValid) => left.clone().normalize(len),
114            (Self::Mask(left), Self::Mask(right)) => {
115                let mut result = left.clone();
116                result.and_with(right);
117                Self::Mask(result).normalize(len)
118            }
119        }
120    }
121}
122
123/// One bit per value, set meaning valid.
124///
125/// Words are `u64` because that is the width the popcount and the mask tests want, and because a
126/// 1024 value vector is exactly 16 of them, which fits in a quarter of a cache line pair and is
127/// the reason the vector size is 1024 rather than DuckDB's 2048.
128#[derive(Debug, Clone, PartialEq, Eq)]
129pub struct Bitmap {
130    words: Vec<u64>,
131}
132
133impl Bitmap {
134    /// A bitmap with room for `len` values, all valid.
135    #[must_use]
136    pub fn all_valid(len: usize) -> Self {
137        Self { words: vec![u64::MAX; len.div_ceil(64)] }
138    }
139
140    /// A bitmap with room for `len` values, all null.
141    #[must_use]
142    pub fn all_invalid(len: usize) -> Self {
143        Self { words: vec![0; len.div_ceil(64)] }
144    }
145
146    /// Whether the value at `index` is valid. Past the end reads as invalid.
147    #[must_use]
148    pub fn get(&self, index: usize) -> bool {
149        let word = index / 64;
150        self.words.get(word).is_some_and(|w| w >> (index % 64) & 1 == 1)
151    }
152
153    /// Sets whether the value at `index` is valid, growing the bitmap if it has to.
154    pub fn set(&mut self, index: usize, valid: bool) {
155        let word = index / 64;
156        if word >= self.words.len() {
157            self.words.resize(word + 1, 0);
158        }
159        let bit = 1u64 << (index % 64);
160        if valid {
161            self.words[word] |= bit;
162        } else {
163            self.words[word] &= !bit;
164        }
165    }
166
167    /// How many of the first `len` values are valid.
168    #[must_use]
169    pub fn count_valid(&self, len: usize) -> usize {
170        let mut count = 0usize;
171        let full_words = len / 64;
172        for word in self.words.iter().take(full_words) {
173            count += word.count_ones() as usize;
174        }
175        let tail = len % 64;
176        if tail > 0 {
177            // A let chain would read better here, but let chains want Rust 1.88 and the declared
178            // minimum in the manifest is 1.85.0. Written the long way rather than moving the
179            // minimum, since nothing about this needs a newer compiler.
180            if let Some(word) = self.words.get(full_words) {
181                // Mask off the bits past the end, which are whatever the last resize left there.
182                let keep = u64::MAX >> (64 - tail);
183                count += (word & keep).count_ones() as usize;
184            }
185        }
186        count
187    }
188
189    /// Intersects this bitmap with another, in place.
190    pub fn and_with(&mut self, other: &Self) {
191        for (index, word) in self.words.iter_mut().enumerate() {
192            *word &= other.words.get(index).copied().unwrap_or(0);
193        }
194    }
195}
196
197#[cfg(test)]
198mod tests {
199    use super::{Bitmap, Validity};
200
201    #[test]
202    fn the_three_cases_answer_the_same_question_the_same_way() {
203        let mut mask = Bitmap::all_valid(8);
204        assert!(Validity::AllValid.is_valid(3));
205        assert!(!Validity::AllInvalid.is_valid(3));
206        assert!(Validity::Mask(mask.clone()).is_valid(3));
207        mask.set(3, false);
208        assert!(!Validity::Mask(mask).is_valid(3));
209    }
210
211    #[test]
212    fn a_uniform_mask_collapses_to_the_flag_it_should_have_been() {
213        // The point of doing this at the end of every operation that builds a mask: the kernel
214        // downstream gets to branch once rather than walk a bitmap to learn what it was told.
215        assert_eq!(Validity::Mask(Bitmap::all_valid(64)).normalize(64), Validity::AllValid);
216        assert_eq!(Validity::Mask(Bitmap::all_invalid(64)).normalize(64), Validity::AllInvalid);
217        let mut mask = Bitmap::all_valid(64);
218        mask.set(7, false);
219        assert!(matches!(Validity::Mask(mask).normalize(64), Validity::Mask(_)));
220    }
221
222    #[test]
223    fn counting_stops_at_the_length_and_not_at_the_word_boundary() {
224        // A 1024 vector is 16 words exactly, but a partially filled one is not, and the bits past
225        // the end are whatever the last resize left there. Getting this wrong makes a count that
226        // is right in tests of length 64 and wrong on real data.
227        let mask = Bitmap::all_valid(100);
228        assert_eq!(mask.count_valid(100), 100);
229        assert_eq!(mask.count_valid(65), 65);
230        assert_eq!(mask.count_valid(1), 1);
231        assert_eq!(mask.count_valid(0), 0);
232    }
233
234    #[test]
235    fn setting_a_null_on_an_all_valid_vector_materializes_a_mask() {
236        let validity = Validity::AllValid.with_null(5, 64);
237        assert!(!validity.is_valid(5));
238        assert!(validity.is_valid(4));
239        assert_eq!(validity.count_valid(64), 63);
240        assert!(validity.has_nulls(64));
241    }
242
243    #[test]
244    fn setting_a_null_on_an_all_invalid_vector_changes_nothing() {
245        assert_eq!(Validity::AllInvalid.with_null(5, 64), Validity::AllInvalid);
246    }
247
248    #[test]
249    fn intersection_short_circuits_on_the_flags() {
250        let mut left = Bitmap::all_valid(8);
251        left.set(0, false);
252        let mut right = Bitmap::all_valid(8);
253        right.set(1, false);
254        let both = Validity::Mask(left.clone()).and(&Validity::Mask(right), 8);
255        assert!(!both.is_valid(0));
256        assert!(!both.is_valid(1));
257        assert!(both.is_valid(2));
258        assert_eq!(both.count_valid(8), 6);
259
260        assert_eq!(Validity::AllValid.and(&Validity::AllValid, 8), Validity::AllValid);
261        assert_eq!(Validity::AllInvalid.and(&Validity::Mask(left), 8), Validity::AllInvalid);
262    }
263
264    #[test]
265    fn validity_from_a_predicate_normalizes_itself() {
266        assert_eq!(Validity::from_iter(16, |_| true), Validity::AllValid);
267        assert_eq!(Validity::from_iter(16, |_| false), Validity::AllInvalid);
268        let mixed = Validity::from_iter(16, |i| i % 2 == 0);
269        assert_eq!(mixed.count_valid(16), 8);
270    }
271}