Skip to main content

vortex_array/arrays/primitive/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Primitive`].
5
6use vortex_buffer::Alignment;
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 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::Primitive;
23use crate::arrays::fixed_width::vtable as fixed_width;
24use crate::arrays::primitive::PrimitiveData;
25use crate::dtype::PType;
26
27impl ArrayPlugin for Primitive {
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            "Primitive 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            "Primitive 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                "PrimitiveArray expects empty metadata, got {} bytes",
70                metadata.len()
71            );
72        }
73        let buffer = fixed_width::single_buffer(buffers)?;
74
75        let validity = fixed_width::deserialize_validity(dtype.nullability(), len, children)?;
76
77        let ptype = PType::try_from(dtype)?;
78
79        vortex_ensure!(
80            buffer.is_aligned_to(Alignment::new(ptype.byte_width())),
81            "Misaligned buffer cannot be used to build PrimitiveArray of {ptype}"
82        );
83
84        if buffer.len() != ptype.byte_width() * len {
85            vortex_bail!(
86                "Buffer length {} does not match expected length {} for {}, {}",
87                buffer.len(),
88                ptype.byte_width() * len,
89                ptype.byte_width(),
90                len,
91            );
92        }
93
94        // SAFETY: the buffer length and alignment are checked above.
95        let slots = PrimitiveData::make_slots(&validity, len);
96        let data = unsafe { PrimitiveData::new_unchecked_from_handle(buffer, ptype, validity) };
97        Ok(Array::try_from_parts(ArrayParts::new(
98            self.clone(),
99            dtype.clone(),
100            len,
101            data,
102            slots,
103        ))?
104        .into_array())
105    }
106}