Skip to main content

vortex_array/arrays/variant/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Variant`].
5
6use prost::Message;
7use vortex_error::VortexResult;
8use vortex_error::vortex_ensure;
9use vortex_error::vortex_ensure_eq;
10use vortex_session::VortexSession;
11
12use super::VariantMetadataProto;
13use crate::Array;
14use crate::ArrayDeserialization;
15use crate::ArrayId;
16use crate::ArrayParts;
17use crate::ArrayPlugin;
18use crate::ArrayRef;
19use crate::ArraySerialization;
20use crate::IntoArray;
21use crate::VTable;
22use crate::array::EmptyArrayData;
23use crate::arrays::Variant;
24use crate::arrays::variant::VariantArraySlotsExt;
25use crate::arrays::variant::VariantSlots;
26use crate::dtype::DType;
27
28impl ArrayPlugin for Variant {
29    fn id(&self) -> ArrayId {
30        VTable::id(self)
31    }
32
33    fn serialize(
34        &self,
35        array: &ArrayRef,
36        _session: &VortexSession,
37    ) -> VortexResult<Option<ArraySerialization>> {
38        vortex_ensure_eq!(
39            array.encoding_id(),
40            ArrayPlugin::id(self),
41            "Variant plugin cannot serialize an array of another encoding",
42        );
43        let array = array.as_::<Variant>();
44        let shredded_dtype = array
45            .shredded()
46            .map(|shredded| shredded.dtype().try_into())
47            .transpose()?;
48        let metadata = VariantMetadataProto { shredded_dtype }.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            "Variant 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        vortex_ensure!(
75            buffers.is_empty(),
76            "VariantArray expects 0 buffers, got {}",
77            buffers.len()
78        );
79        let proto = VariantMetadataProto::decode(metadata)?;
80        let shredded_dtype = proto
81            .shredded_dtype
82            .as_ref()
83            .map(|dtype| DType::from_proto(dtype, session))
84            .transpose()?;
85        vortex_ensure!(matches!(dtype, DType::Variant(_)), "Expected Variant DType");
86        let expected_children = 1 + usize::from(shredded_dtype.is_some());
87        vortex_ensure_eq!(children.len(), expected_children);
88        let core_storage = children.get(0, dtype, len)?;
89        let shredded = shredded_dtype
90            .map(|dtype| children.get(1, &dtype, len))
91            .transpose()?;
92        Ok(Array::try_from_parts(ArrayParts::new(
93            self.clone(),
94            dtype.clone(),
95            len,
96            EmptyArrayData,
97            VariantSlots {
98                core_storage,
99                shredded,
100            }
101            .into_slots(),
102        ))?
103        .into_array())
104    }
105}