vortex_array/arrays/bool/vtable/
plugin.rs1use prost::Message;
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure_eq;
11use vortex_session::VortexSession;
12
13use super::BoolMetadata;
14use crate::Array;
15use crate::ArrayDeserialization;
16use crate::ArrayId;
17use crate::ArrayParts;
18use crate::ArrayPlugin;
19use crate::ArrayRef;
20use crate::ArraySerialization;
21use crate::IntoArray;
22use crate::VTable;
23use crate::arrays::Bool;
24use crate::arrays::bool::BoolData;
25use crate::validity::Validity;
26
27impl ArrayPlugin for Bool {
28 fn id(&self) -> ArrayId {
29 VTable::id(self)
30 }
31
32 fn serialize(
33 &self,
34 array: &ArrayRef,
35 _session: &VortexSession,
36 ) -> VortexResult<Option<ArraySerialization>> {
37 vortex_ensure_eq!(
38 array.encoding_id(),
39 ArrayPlugin::id(self),
40 "Bool plugin cannot serialize an array of another encoding",
41 );
42 let array = array.as_::<Bool>();
43 let offset = array.meta.offset();
44 assert!(offset < 8, "Offset must be <8, got {offset}");
45 let metadata = BoolMetadata {
46 offset: u32::try_from(offset).vortex_expect("checked"),
47 }
48 .encode_to_vec();
49 Ok(Some(ArraySerialization::from_array(
50 ArrayPlugin::id(self),
51 array.array(),
52 metadata,
53 )))
54 }
55
56 fn deserialize(
57 &self,
58 parts: ArrayDeserialization<'_>,
59 _session: &VortexSession,
60 ) -> VortexResult<ArrayRef> {
61 vortex_ensure_eq!(
62 parts.serialized_id,
63 ArrayPlugin::id(self),
64 "Bool plugin does not recognize the serialized ID",
65 );
66 let ArrayDeserialization {
67 dtype,
68 len,
69 metadata,
70 buffers,
71 children,
72 ..
73 } = parts;
74 let metadata = BoolMetadata::decode(metadata)?;
75 if buffers.len() != 1 {
76 vortex_bail!("Expected 1 buffer, got {}", buffers.len());
77 }
78
79 let validity = if children.is_empty() {
80 Validity::from(dtype.nullability())
81 } else if children.len() == 1 {
82 let validity = children.get(0, &Validity::DTYPE, len)?;
83 Validity::Array(validity)
84 } else {
85 vortex_bail!("Expected 0 or 1 child, got {}", children.len());
86 };
87
88 let buffer = buffers[0].clone();
89 let slots = BoolData::make_slots(&validity, len);
90 let data = BoolData::try_new_from_handle(buffer, metadata.offset as usize, len, validity)?;
91 Ok(Array::try_from_parts(ArrayParts::new(
92 self.clone(),
93 dtype.clone(),
94 len,
95 data,
96 slots,
97 ))?
98 .into_array())
99 }
100}