Skip to main content

vortex_array/arrays/varbin/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`VarBin`].
5
6use 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::VarBinMetadata;
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::VortexSessionExecute;
24use crate::arrays::VarBin;
25use crate::arrays::varbin::VarBinArraySlotsExt;
26use crate::arrays::varbin::VarBinData;
27use crate::dtype::DType;
28use crate::dtype::Nullability;
29use crate::dtype::PType;
30use crate::validity::Validity;
31
32impl ArrayPlugin for VarBin {
33    fn id(&self) -> ArrayId {
34        VTable::id(self)
35    }
36
37    fn serialize(
38        &self,
39        array: &ArrayRef,
40        _session: &VortexSession,
41    ) -> VortexResult<Option<ArraySerialization>> {
42        vortex_ensure_eq!(
43            array.encoding_id(),
44            ArrayPlugin::id(self),
45            "VarBin plugin cannot serialize an array of another encoding",
46        );
47        let array = array.as_::<VarBin>();
48        let metadata = VarBinMetadata {
49            offsets_ptype: PType::try_from(array.offsets().dtype())
50                .vortex_expect("Must be a valid PType") as i32,
51        }
52        .encode_to_vec();
53        Ok(Some(ArraySerialization::from_array(
54            ArrayPlugin::id(self),
55            array.array(),
56            metadata,
57        )))
58    }
59
60    fn deserialize(
61        &self,
62        parts: ArrayDeserialization<'_>,
63        session: &VortexSession,
64    ) -> VortexResult<ArrayRef> {
65        vortex_ensure_eq!(
66            parts.serialized_id,
67            ArrayPlugin::id(self),
68            "VarBin plugin does not recognize the serialized ID",
69        );
70        let ArrayDeserialization {
71            dtype,
72            len,
73            metadata,
74            buffers,
75            children,
76            ..
77        } = parts;
78        let metadata = VarBinMetadata::decode(metadata)?;
79        let offsets_ptype = metadata.get_offsets_ptype()?;
80        let validity = if children.len() == 1 {
81            Validity::from(dtype.nullability())
82        } else if children.len() == 2 {
83            let validity = children.get(1, &Validity::DTYPE, len)?;
84            Validity::Array(validity)
85        } else {
86            vortex_bail!("Expected 1 or 2 children, got {}", children.len());
87        };
88
89        let offsets = children.get(
90            0,
91            &DType::Primitive(offsets_ptype, Nullability::NonNullable),
92            len + 1,
93        )?;
94
95        if buffers.len() != 1 {
96            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
97        }
98        let bytes = buffers[0].clone().try_to_host_sync()?;
99
100        let data = VarBinData::try_build(
101            offsets.clone(),
102            bytes,
103            dtype.clone(),
104            validity.clone(),
105            &mut session.create_execution_ctx(),
106        )?;
107        let slots = VarBinData::make_slots(offsets, &validity, len);
108        Ok(Array::try_from_parts(ArrayParts::new(
109            self.clone(),
110            dtype.clone(),
111            len,
112            data,
113            slots,
114        ))?
115        .into_array())
116    }
117}