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;
9use vortex_error::vortex_ensure_eq;
10use vortex_session::VortexSession;
11
12use super::PatchedMetadata;
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::arrays::Patched;
23use crate::arrays::patched::PatchedArrayExt;
24use crate::arrays::patched::PatchedArraySlotsExt;
25use crate::arrays::patched::PatchedData;
26use crate::arrays::patched::PatchedSlots;
27use crate::dtype::PType;
28
29impl ArrayPlugin for Patched {
30    fn id(&self) -> ArrayId {
31        VTable::id(self)
32    }
33
34    fn serialize(
35        &self,
36        array: &ArrayRef,
37        _session: &VortexSession,
38    ) -> VortexResult<Option<ArraySerialization>> {
39        vortex_ensure_eq!(
40            array.encoding_id(),
41            ArrayPlugin::id(self),
42            "Patched plugin cannot serialize an array of another encoding",
43        );
44        let array = array.as_::<Patched>();
45        // Readers derive the lane offsets length from the metadata, so reject arrays they could
46        // not read back.
47        let data = PatchedData {
48            n_lanes: array.n_lanes(),
49            offset: array.offset(),
50        };
51        vortex_ensure_eq!(
52            array.lane_offsets().len(),
53            data.lane_offsets_len(array.dtype(), array.len())?,
54        );
55        let metadata = PatchedMetadata {
56            n_patches: u32::try_from(array.patch_indices().len())?,
57            n_lanes: u32::try_from(array.n_lanes())?,
58            offset: u32::try_from(array.offset())?,
59        }
60        .encode_to_vec();
61        Ok(Some(ArraySerialization::from_array(
62            ArrayPlugin::id(self),
63            array.array(),
64            metadata,
65        )))
66    }
67
68    fn deserialize(
69        &self,
70        parts: ArrayDeserialization<'_>,
71        _session: &VortexSession,
72    ) -> VortexResult<ArrayRef> {
73        vortex_ensure_eq!(
74            parts.serialized_id,
75            ArrayPlugin::id(self),
76            "Patched plugin does not recognize the serialized ID",
77        );
78        let ArrayDeserialization {
79            dtype,
80            len,
81            metadata,
82            buffers,
83            children,
84            ..
85        } = parts;
86        vortex_ensure!(
87            buffers.is_empty(),
88            "PatchedArray expects 0 buffers, got {}",
89            buffers.len()
90        );
91        vortex_ensure!(
92            children.len() >= PatchedSlots::COUNT,
93            "PatchedArray expects at least {} children, got {}",
94            PatchedSlots::COUNT,
95            children.len()
96        );
97        let metadata = PatchedMetadata::decode(metadata)?;
98        let n_patches = usize::try_from(metadata.n_patches)?;
99        let data = PatchedData {
100            n_lanes: usize::try_from(metadata.n_lanes)?,
101            offset: usize::try_from(metadata.offset)?,
102        };
103        // Check the metadata before reading children, whose lengths and dtypes derive from it.
104        let lane_offsets_len = data.lane_offsets_len(dtype, len)?;
105
106        let inner = children.get(PatchedSlots::INNER, dtype, len)?;
107        let lane_offsets = children.get(
108            PatchedSlots::LANE_OFFSETS,
109            PType::U32.into(),
110            lane_offsets_len,
111        )?;
112        let indices = children.get(PatchedSlots::PATCH_INDICES, PType::U16.into(), n_patches)?;
113        let values = children.get(
114            PatchedSlots::PATCH_VALUES,
115            &dtype.as_nonnullable(),
116            n_patches,
117        )?;
118
119        let slots = PatchedSlots {
120            inner,
121            lane_offsets,
122            patch_indices: indices,
123            patch_values: values,
124        }
125        .into_slots();
126        Ok(Array::try_from_parts(ArrayParts::new(
127            self.clone(),
128            dtype.clone(),
129            len,
130            data,
131            slots,
132        ))?
133        .into_array())
134    }
135}