• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16979224669

14 Aug 2025 11:42PM UTC coverage: 23.728%. First build
16979224669

Pull #2456

github

web-flow
Merge 30049dfa7 into aaf3e36ad
Pull Request #2456: feat: basic BoolBuffer / BoolBufferMut

68 of 1065 new or added lines in 82 files covered. (6.38%)

8616 of 36312 relevant lines covered (23.73%)

146.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.04
/vortex-array/src/builders/lazy_validity_builder.rs
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
use vortex_buffer::{BitBuffer, BitBufferMut};
5
use vortex_dtype::Nullability;
6
use vortex_dtype::Nullability::{NonNullable, Nullable};
7
use vortex_error::{VortexExpect, vortex_panic};
8
use vortex_mask::Mask;
9

10
use crate::validity::Validity;
11

12
/// This is borrowed from arrow's null buffer builder, however we expose a `append_buffer`
13
/// method to append a boolean buffer directly.
14
pub struct LazyBitBufferBuilder {
15
    inner: Option<BitBufferMut>,
16
    len: usize,
17
    capacity: usize,
18
}
19

20
impl LazyBitBufferBuilder {
21
    /// Creates a new empty builder.
22
    /// `capacity` is the number of bits in the null buffer.
23
    pub fn new(capacity: usize) -> Self {
56✔
24
        Self {
56✔
25
            inner: None,
56✔
26
            len: 0,
56✔
27
            capacity,
56✔
28
        }
56✔
29
    }
56✔
30

31
    #[inline]
32
    pub fn append_n_non_nulls(&mut self, n: usize) {
172✔
33
        if let Some(buf) = self.inner.as_mut() {
172✔
NEW
34
            buf.append_n(true, n)
×
35
        } else {
172✔
36
            self.len += n;
172✔
37
        }
172✔
38
    }
172✔
39

40
    #[inline]
41
    pub fn append_non_null(&mut self) {
72✔
42
        if let Some(buf) = self.inner.as_mut() {
72✔
43
            buf.append(true)
×
44
        } else {
72✔
45
            self.len += 1;
72✔
46
        }
72✔
47
    }
72✔
48

49
    #[inline]
50
    pub fn append_n_nulls(&mut self, n: usize) {
×
51
        self.materialize_if_needed();
×
52
        self.inner
×
53
            .as_mut()
×
54
            .vortex_expect("cannot append null to non-nullable builder")
×
NEW
55
            .append_n(false, n);
×
56
    }
×
57

58
    #[inline]
59
    pub fn append_null(&mut self) {
×
60
        self.materialize_if_needed();
×
61
        self.inner
×
62
            .as_mut()
×
63
            .vortex_expect("cannot append null to non-nullable builder")
×
64
            .append(false);
×
65
    }
×
66

67
    #[inline]
68
    pub fn append(&mut self, not_null: bool) {
72✔
69
        if not_null {
72✔
70
            self.append_non_null()
72✔
71
        } else {
72
            self.append_null()
×
73
        }
74
    }
72✔
75

76
    #[inline]
NEW
77
    pub fn append_buffer(&mut self, bool_buffer: &BitBuffer) {
×
78
        self.materialize_if_needed();
×
79
        self.inner
×
80
            .as_mut()
×
81
            .vortex_expect("buffer just materialized")
×
82
            .append_buffer(bool_buffer);
×
83
    }
×
84

85
    pub fn append_validity_mask(&mut self, validity_mask: Mask) {
172✔
86
        match validity_mask {
172✔
87
            Mask::AllTrue(len) => self.append_n_non_nulls(len),
172✔
88
            Mask::AllFalse(len) => self.append_n_nulls(len),
×
NEW
89
            Mask::Values(is_valid) => self.append_buffer(is_valid.bit_buffer()),
×
90
        }
91
    }
172✔
92

93
    pub fn set_bit(&mut self, index: usize, v: bool) {
×
94
        self.materialize_if_needed();
×
95
        self.inner
×
96
            .as_mut()
×
97
            .vortex_expect("buffer just materialized")
×
NEW
98
            .set_to(index, v);
×
99
    }
×
100

101
    pub fn len(&self) -> usize {
×
102
        // self.len is the length of the builder if the inner buffer is not materialized
103
        self.inner.as_ref().map(|i| i.len()).unwrap_or(self.len)
×
104
    }
×
105

106
    pub fn truncate(&mut self, len: usize) {
×
107
        if let Some(b) = self.inner.as_mut() {
×
108
            b.truncate(len)
×
109
        }
×
110
        self.len = len;
×
111
    }
×
112

113
    pub fn reserve(&mut self, n: usize) {
×
114
        self.materialize_if_needed();
×
115
        self.inner
×
116
            .as_mut()
×
117
            .vortex_expect("buffer just materialized")
×
118
            .reserve(n);
×
119
    }
×
120

121
    fn finish(&mut self) -> Option<BitBuffer> {
56✔
122
        self.len = 0;
56✔
123
        self.inner.take().map(|b| b.freeze())
56✔
124
    }
56✔
125

126
    pub fn finish_with_nullability(&mut self, nullability: Nullability) -> Validity {
56✔
127
        let nulls = self.finish();
56✔
128

129
        match (nullability, nulls) {
56✔
130
            (NonNullable, None) => Validity::NonNullable,
20✔
131
            (Nullable, None) => Validity::AllValid,
36✔
132
            (Nullable, Some(arr)) => Validity::from(arr),
×
133
            _ => vortex_panic!("Invalid nullability/nulls combination"),
×
134
        }
135
    }
56✔
136

137
    pub fn ensure_capacity(&mut self, capacity: usize) {
×
138
        if self.inner.is_none() {
×
139
            self.capacity = capacity;
×
140
        } else {
×
141
            let inner = self
×
142
                .inner
×
143
                .as_mut()
×
144
                .vortex_expect("buffer just materialized");
×
145
            if capacity < inner.capacity() {
×
146
                inner.reserve(capacity - inner.len());
×
147
            }
×
148
        }
149
    }
×
150

151
    #[inline]
152
    fn materialize_if_needed(&mut self) {
×
153
        if self.inner.is_none() {
×
154
            self.materialize()
×
155
        }
×
156
    }
×
157

158
    // This only happens once per builder
159
    #[cold]
160
    #[inline(never)]
161
    fn materialize(&mut self) {
×
162
        if self.inner.is_none() {
×
NEW
163
            let mut bit_mut = BitBufferMut::new(self.len.max(self.capacity));
×
NEW
164
            bit_mut.append_n(true, self.len);
×
NEW
165
            self.inner = Some(bit_mut);
×
166
        }
×
167
    }
×
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc