vortex_zigzag/
serde.rs

1use vortex_array::serde::ArrayChildren;
2use vortex_array::vtable::{EncodeVTable, SerdeVTable, VisitorVTable};
3use vortex_array::{
4    ArrayBufferVisitor, ArrayChildVisitor, Canonical, DeserializeMetadata, EmptyMetadata,
5};
6use vortex_buffer::ByteBuffer;
7use vortex_dtype::{DType, PType};
8use vortex_error::{VortexResult, vortex_bail};
9
10use crate::{ZigZagArray, ZigZagEncoding, ZigZagVTable, zigzag_encode};
11
12impl SerdeVTable<ZigZagVTable> for ZigZagVTable {
13    type Metadata = EmptyMetadata;
14
15    fn metadata(_array: &ZigZagArray) -> VortexResult<Option<Self::Metadata>> {
16        Ok(Some(EmptyMetadata))
17    }
18
19    fn build(
20        _encoding: &ZigZagEncoding,
21        dtype: &DType,
22        len: usize,
23        _metadata: &<Self::Metadata as DeserializeMetadata>::Output,
24        _buffers: &[ByteBuffer],
25        children: &dyn ArrayChildren,
26    ) -> VortexResult<ZigZagArray> {
27        if children.len() != 1 {
28            vortex_bail!("Expected 1 child, got {}", children.len());
29        }
30
31        let ptype = PType::try_from(dtype)?;
32        let encoded_type = DType::Primitive(ptype.to_unsigned(), dtype.nullability());
33
34        let encoded = children.get(0, &encoded_type, len)?;
35        ZigZagArray::try_new(encoded)
36    }
37}
38
39impl EncodeVTable<ZigZagVTable> for ZigZagVTable {
40    fn encode(
41        encoding: &ZigZagEncoding,
42        canonical: &Canonical,
43        _like: Option<&ZigZagArray>,
44    ) -> VortexResult<Option<ZigZagArray>> {
45        let parray = canonical.clone().into_primitive()?;
46
47        if !parray.ptype().is_signed_int() {
48            vortex_bail!(
49                "only signed integers can be encoded into {}, got {}",
50                encoding.id(),
51                parray.ptype()
52            )
53        }
54
55        Ok(Some(zigzag_encode(parray)?))
56    }
57}
58
59impl VisitorVTable<ZigZagVTable> for ZigZagVTable {
60    fn visit_buffers(_array: &ZigZagArray, _visitor: &mut dyn ArrayBufferVisitor) {}
61
62    fn visit_children(array: &ZigZagArray, visitor: &mut dyn ArrayChildVisitor) {
63        visitor.visit_child("encoded", array.encoded())
64    }
65}