Skip to main content

vortex_array/arrays/listview/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`ListView`].
5
6use prost::Message;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_ensure;
10use vortex_error::vortex_ensure_eq;
11use vortex_session::VortexSession;
12
13use super::ListViewMetadata;
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::ListView;
25use crate::arrays::listview::ListViewArraySlotsExt;
26use crate::arrays::listview::ListViewData;
27use crate::dtype::DType;
28use crate::dtype::Nullability;
29use crate::dtype::PType;
30use crate::validity::Validity;
31
32impl ArrayPlugin for ListView {
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            "ListView plugin cannot serialize an array of another encoding",
46        );
47        let array = array.as_::<ListView>();
48        let metadata = ListViewMetadata {
49            elements_len: array.elements().len() as u64,
50            offset_ptype: PType::try_from(array.offsets().dtype())? as i32,
51            size_ptype: PType::try_from(array.sizes().dtype())? as i32,
52        }
53        .encode_to_vec();
54        Ok(Some(ArraySerialization::from_array(
55            ArrayPlugin::id(self),
56            array.array(),
57            metadata,
58        )))
59    }
60
61    fn deserialize(
62        &self,
63        parts: ArrayDeserialization<'_>,
64        session: &VortexSession,
65    ) -> VortexResult<ArrayRef> {
66        vortex_ensure_eq!(
67            parts.serialized_id,
68            ArrayPlugin::id(self),
69            "ListView plugin does not recognize the serialized ID",
70        );
71        let ArrayDeserialization {
72            dtype,
73            len,
74            metadata,
75            buffers,
76            children,
77            ..
78        } = parts;
79        let metadata = ListViewMetadata::decode(metadata)?;
80        let offset_ptype = metadata.get_offset_ptype()?;
81        let size_ptype = metadata.get_size_ptype()?;
82        vortex_ensure!(
83            buffers.is_empty(),
84            "`ListViewArray::build` expects no buffers"
85        );
86
87        let DType::List(element_dtype, _) = dtype else {
88            vortex_bail!("Expected List dtype, got {:?}", dtype);
89        };
90
91        let validity = if children.len() == 3 {
92            Validity::from(dtype.nullability())
93        } else if children.len() == 4 {
94            let validity = children.get(3, &Validity::DTYPE, len)?;
95            Validity::Array(validity)
96        } else {
97            vortex_bail!(
98                "`ListViewArray::build` expects 3 or 4 children, got {}",
99                children.len()
100            );
101        };
102
103        // Get elements with the correct length from metadata.
104        let elements = children.get(
105            0,
106            element_dtype.as_ref(),
107            usize::try_from(metadata.elements_len)?,
108        )?;
109
110        // Get offsets with proper type from metadata.
111        let offsets = children.get(
112            1,
113            &DType::Primitive(offset_ptype, Nullability::NonNullable),
114            len,
115        )?;
116
117        // Get sizes with proper type from metadata.
118        let sizes = children.get(
119            2,
120            &DType::Primitive(size_ptype, Nullability::NonNullable),
121            len,
122        )?;
123
124        ListViewData::validate(
125            &elements,
126            &offsets,
127            &sizes,
128            &validity,
129            &mut session.create_execution_ctx(),
130        )?;
131        let data = ListViewData::try_new()?;
132        let slots = ListViewData::make_slots(&elements, &offsets, &sizes, &validity, len);
133        Ok(Array::try_from_parts(ArrayParts::new(
134            self.clone(),
135            dtype.clone(),
136            len,
137            data,
138            slots,
139        ))?
140        .into_array())
141    }
142}