Skip to main content

vortex_array/arrays/bool/
compact.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::BitBufferMeta;
5use vortex_error::VortexResult;
6
7use crate::arrays::BoolArray;
8use crate::buffer::BufferHandle;
9
10impl BoolArray {
11    /// Trims the packed bit buffer to the bytes backing the array's visible bits.
12    ///
13    /// Arrays built over a shared buffer, such as one decoded from a file segment, can hold
14    /// trailing bytes that no read can see but that `nbytes` and serialization still pay for. The
15    /// trim is zero-copy and keeps the leading bit offset, so only whole bytes are dropped.
16    pub fn trim_bits(&self) -> VortexResult<Self> {
17        let byte_len = BitBufferMeta::new(self.meta.offset(), self.len()).byte_len();
18
19        let bits = match self.bits.as_host_opt() {
20            Some(host) => BufferHandle::new_host(host.slice_unaligned(..byte_len)),
21            None => self.bits.slice(0..byte_len),
22        };
23
24        Self::try_new_from_handle(bits, self.meta.offset(), self.len(), self.validity()?)
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use std::sync::LazyLock;
31
32    use rstest::rstest;
33    use vortex_buffer::BitBuffer;
34    use vortex_buffer::ByteBuffer;
35    use vortex_error::VortexResult;
36    use vortex_session::VortexSession;
37
38    use crate::Canonical;
39    use crate::VortexSessionExecute;
40    use crate::arrays::BoolArray;
41    use crate::assert_arrays_eq;
42    use crate::buffer::BufferHandle;
43    use crate::validity::Validity;
44
45    static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
46
47    #[rstest]
48    #[case(0, 16, Validity::NonNullable)]
49    #[case(0, 13, Validity::NonNullable)]
50    #[case(3, 10, Validity::AllValid)]
51    #[case(7, 1, Validity::NonNullable)]
52    #[case(0, 0, Validity::NonNullable)]
53    fn trims_to_visible_bytes(
54        #[case] offset: usize,
55        #[case] len: usize,
56        #[case] validity: Validity,
57    ) -> VortexResult<()> {
58        let mut ctx = SESSION.create_execution_ctx();
59        let bytes: ByteBuffer = (0..64u8).collect();
60        let handle = BufferHandle::new_host(bytes.clone());
61
62        let trimmed =
63            BoolArray::try_new_from_handle(handle, offset, len, validity.clone())?.trim_bits()?;
64
65        assert_eq!(trimmed.bits.len(), (offset + len).div_ceil(8));
66        let expected = BoolArray::new(BitBuffer::new_with_offset(bytes, len, offset), validity);
67        assert_arrays_eq!(trimmed, expected, &mut ctx);
68        Ok(())
69    }
70
71    #[test]
72    fn canonical_compact_trims_bits() -> VortexResult<()> {
73        let mut ctx = SESSION.create_execution_ctx();
74        let handle = BufferHandle::new_host((0..64u8).collect());
75        let array = BoolArray::try_new_from_handle(handle, 0, 20, Validity::NonNullable)?;
76
77        let trimmed = Canonical::Bool(array).compact(&mut ctx)?.into_bool();
78
79        assert_eq!(trimmed.bits.len(), 3);
80        let expected = BoolArray::new(
81            BitBuffer::new_with_offset((0..64u8).collect(), 20, 0),
82            Validity::NonNullable,
83        );
84        assert_arrays_eq!(trimmed, expected, &mut ctx);
85        Ok(())
86    }
87}