Skip to main content

vortex_array/arrays/map/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Map`].
5
6use vortex_error::VortexResult;
7use vortex_error::vortex_bail;
8use vortex_error::vortex_ensure;
9use vortex_error::vortex_ensure_eq;
10use vortex_session::VortexSession;
11
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::ListView;
22use crate::arrays::Map;
23use crate::arrays::map::MapData;
24use crate::arrays::map::MapSlots;
25use crate::dtype::DType;
26
27impl ArrayPlugin for Map {
28    fn id(&self) -> ArrayId {
29        VTable::id(self)
30    }
31
32    fn serialize(
33        &self,
34        array: &ArrayRef,
35        _session: &VortexSession,
36    ) -> VortexResult<Option<ArraySerialization>> {
37        vortex_ensure_eq!(
38            array.encoding_id(),
39            ArrayPlugin::id(self),
40            "Map plugin cannot serialize an array of another encoding",
41        );
42        Ok(Some(ArraySerialization::from_array(
43            ArrayPlugin::id(self),
44            array,
45            vec![],
46        )))
47    }
48
49    fn deserialize(
50        &self,
51        parts: ArrayDeserialization<'_>,
52        _session: &VortexSession,
53    ) -> VortexResult<ArrayRef> {
54        vortex_ensure_eq!(
55            parts.serialized_id,
56            ArrayPlugin::id(self),
57            "Map plugin does not recognize the serialized ID",
58        );
59        let ArrayDeserialization {
60            dtype,
61            len,
62            metadata,
63            buffers,
64            children,
65            ..
66        } = parts;
67        if !metadata.is_empty() {
68            vortex_bail!(
69                "MapArray expects empty metadata, got {} bytes",
70                metadata.len()
71            );
72        }
73        vortex_ensure!(buffers.is_empty(), "MapArray expects no buffers");
74
75        let DType::Map(map_dtype, nullability) = dtype else {
76            vortex_bail!("Expected map dtype, got {dtype}");
77        };
78        vortex_ensure_eq!(children.len(), MapSlots::COUNT);
79
80        let expected_entries_dtype =
81            DType::List(std::sync::Arc::new(map_dtype.entries_dtype()), *nullability);
82        let entries = children.get(MapSlots::ENTRIES, &expected_entries_dtype, len)?;
83        vortex_ensure!(
84            entries.is::<ListView>(),
85            "MapArray entries must use vortex.listview encoding, got {}",
86            entries.encoding_id()
87        );
88
89        let slots = MapData::make_slots(entries);
90        Ok(Array::try_from_parts(ArrayParts::new(
91            self.clone(),
92            dtype.clone(),
93            len,
94            MapData,
95            slots,
96        ))?
97        .into_array())
98    }
99}