Skip to main content

vortex_array/arrays/chunked/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Chunked`].
5
6use itertools::Itertools;
7use smallvec::SmallVec;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure_eq;
11use vortex_error::vortex_err;
12use vortex_session::VortexSession;
13
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::Chunked;
25use crate::arrays::PrimitiveArray;
26use crate::arrays::chunked::ChunkedData;
27use crate::arrays::chunked::array::ChunkedSlots;
28use crate::dtype::DType;
29use crate::dtype::Nullability;
30use crate::dtype::PType;
31
32impl ArrayPlugin for Chunked {
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            "Chunked plugin cannot serialize an array of another encoding",
46        );
47        Ok(Some(ArraySerialization::from_array(
48            ArrayPlugin::id(self),
49            array,
50            vec![],
51        )))
52    }
53
54    fn deserialize(
55        &self,
56        parts: ArrayDeserialization<'_>,
57        session: &VortexSession,
58    ) -> VortexResult<ArrayRef> {
59        vortex_ensure_eq!(
60            parts.serialized_id,
61            ArrayPlugin::id(self),
62            "Chunked plugin does not recognize the serialized ID",
63        );
64        let ArrayDeserialization {
65            dtype,
66            len,
67            metadata,
68            children,
69            ..
70        } = parts;
71        if !metadata.is_empty() {
72            vortex_bail!(
73                "ChunkedArray expects empty metadata, got {} bytes",
74                metadata.len()
75            );
76        }
77        if children.is_empty() {
78            vortex_bail!("Chunked array needs at least one child");
79        }
80
81        let nchunks = children.len() - 1;
82        let chunk_offsets = children.get(
83            ChunkedSlots::CHUNK_OFFSETS,
84            &DType::Primitive(PType::U64, Nullability::NonNullable),
85            nchunks + 1,
86        )?;
87        let mut ctx = session.create_execution_ctx();
88        let chunk_offsets_buf = chunk_offsets
89            .clone()
90            .execute::<PrimitiveArray>(&mut ctx)?
91            .to_buffer::<u64>();
92        let chunk_offsets_usize = chunk_offsets_buf
93            .iter()
94            .copied()
95            .map(|offset| {
96                usize::try_from(offset)
97                    .map_err(|_| vortex_err!("chunk offset {offset} exceeds usize range"))
98            })
99            .collect::<VortexResult<Vec<_>>>()?;
100        let mut slots = SmallVec::with_capacity(children.len());
101        slots.push(Some(chunk_offsets));
102        for (idx, (start, end)) in chunk_offsets_usize
103            .iter()
104            .copied()
105            .tuple_windows()
106            .enumerate()
107        {
108            let chunk_len = end - start;
109            slots.push(Some(children.get(
110                idx + ChunkedSlots::CHUNKS_OFFSET,
111                dtype,
112                chunk_len,
113            )?));
114        }
115
116        Ok(Array::try_from_parts(ArrayParts::new(
117            self.clone(),
118            dtype.clone(),
119            len,
120            ChunkedData::new(chunk_offsets_usize),
121            slots,
122        ))?
123        .into_array())
124    }
125}