vortex_array/arrays/bool/vtable/
serde.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::ByteBuffer;
5use vortex_dtype::DType;
6use vortex_error::{VortexExpect, VortexResult, vortex_bail};
7
8use super::BoolArray;
9use crate::ProstMetadata;
10use crate::arrays::BoolVTable;
11use crate::serde::ArrayChildren;
12use crate::validity::Validity;
13use crate::vtable::{SerdeVTable, VTable};
14
15#[derive(prost::Message)]
16pub struct BoolMetadata {
17    // The offset in bits must be <8
18    #[prost(uint32, tag = "1")]
19    pub offset: u32,
20}
21
22impl SerdeVTable<BoolVTable> for BoolVTable {
23    type Metadata = ProstMetadata<BoolMetadata>;
24
25    fn metadata(array: &BoolArray) -> VortexResult<Option<Self::Metadata>> {
26        let bit_offset = array.bit_buffer().offset();
27        assert!(bit_offset < 8, "Offset must be <8, got {bit_offset}");
28        Ok(Some(ProstMetadata(BoolMetadata {
29            offset: u32::try_from(bit_offset).vortex_expect("checked"),
30        })))
31    }
32
33    fn build(
34        _encoding: &<BoolVTable as VTable>::Encoding,
35        dtype: &DType,
36        len: usize,
37        metadata: &BoolMetadata,
38        buffers: &[ByteBuffer],
39        children: &dyn ArrayChildren,
40    ) -> VortexResult<BoolArray> {
41        if buffers.len() != 1 {
42            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
43        }
44
45        let validity = if children.is_empty() {
46            Validity::from(dtype.nullability())
47        } else if children.len() == 1 {
48            let validity = children.get(0, &Validity::DTYPE, len)?;
49            Validity::Array(validity)
50        } else {
51            vortex_bail!("Expected 0 or 1 child, got {}", children.len());
52        };
53
54        BoolArray::try_new(buffers[0].clone(), metadata.offset as usize, len, validity)
55    }
56}