Skip to main content

vortex_array/arrays/patched/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Patched`].
5
6use prost::Message;
7use vortex_error::VortexResult;
8use vortex_error::vortex_ensure_eq;
9use vortex_session::VortexSession;
10
11use super::PatchedMetadata;
12use crate::Array;
13use crate::ArrayDeserialization;
14use crate::ArrayId;
15use crate::ArrayParts;
16use crate::ArrayPlugin;
17use crate::ArrayRef;
18use crate::ArraySerialization;
19use crate::IntoArray;
20use crate::VTable;
21use crate::arrays::Patched;
22use crate::arrays::patched::PatchedArrayExt;
23use crate::arrays::patched::PatchedArraySlotsExt;
24use crate::arrays::patched::PatchedData;
25use crate::arrays::patched::PatchedSlots;
26use crate::dtype::PType;
27
28impl ArrayPlugin for Patched {
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            "Patched plugin cannot serialize an array of another encoding",
42        );
43        let array = array.as_::<Patched>();
44        let metadata = PatchedMetadata {
45            n_patches: u32::try_from(array.patch_indices().len())?,
46            n_lanes: u32::try_from(array.n_lanes())?,
47            offset: u32::try_from(array.offset())?,
48        }
49        .encode_to_vec();
50        Ok(Some(ArraySerialization::from_array(
51            ArrayPlugin::id(self),
52            array.array(),
53            metadata,
54        )))
55    }
56
57    fn deserialize(
58        &self,
59        parts: ArrayDeserialization<'_>,
60        _session: &VortexSession,
61    ) -> VortexResult<ArrayRef> {
62        vortex_ensure_eq!(
63            parts.serialized_id,
64            ArrayPlugin::id(self),
65            "Patched plugin does not recognize the serialized ID",
66        );
67        let ArrayDeserialization {
68            dtype,
69            len,
70            metadata,
71            children,
72            ..
73        } = parts;
74        let metadata = PatchedMetadata::decode(metadata)?;
75        let n_patches = metadata.n_patches as usize;
76        let n_lanes = metadata.n_lanes as usize;
77        let offset = metadata.offset as usize;
78
79        // n_chunks should correspond to the chunk in the `inner`.
80        // After slicing when offset > 0, there may be additional chunks.
81        let n_chunks = (len + offset).div_ceil(1024);
82
83        let inner = children.get(0, dtype, len)?;
84        let lane_offsets = children.get(1, PType::U32.into(), n_chunks * n_lanes + 1)?;
85        let indices = children.get(2, PType::U16.into(), n_patches)?;
86        let values = children.get(3, dtype, n_patches)?;
87
88        let data = PatchedData { n_lanes, offset };
89        let slots = PatchedSlots {
90            inner,
91            lane_offsets,
92            patch_indices: indices,
93            patch_values: values,
94        }
95        .into_slots();
96        Ok(Array::try_from_parts(ArrayParts::new(
97            self.clone(),
98            dtype.clone(),
99            len,
100            data,
101            slots,
102        ))?
103        .into_array())
104    }
105}