vortex_array/
data.rs

1use vortex_buffer::ByteBuffer;
2use vortex_dtype::DType;
3use vortex_error::{VortexError, VortexResult, vortex_bail, vortex_err};
4
5use crate::serde::ArrayChildren;
6use crate::{Array, ArrayRef, EncodingRef};
7
8#[derive(Clone, Debug)]
9pub struct ArrayData {
10    encoding: EncodingRef,
11    len: usize,
12    dtype: DType,
13    metadata: Vec<u8>,
14    buffers: Vec<ByteBuffer>,
15    children: Vec<ArrayData>,
16}
17
18impl ArrayChildren for ArrayData {
19    fn get(&self, index: usize, dtype: &DType, len: usize) -> VortexResult<ArrayRef> {
20        if index >= self.children.len() {
21            vortex_bail!("Index out of bounds");
22        }
23        let child = &self.children[index];
24        if child.len != len {
25            vortex_bail!(
26                "Child length mismatch. Provided {}, but actually {}",
27                len,
28                child.len
29            );
30        }
31        if child.dtype != *dtype {
32            vortex_bail!(
33                "Child dtype mismatch. Provided {:?}, but actually {:?}",
34                dtype,
35                child.dtype
36            );
37        }
38        ArrayRef::try_from(child)
39    }
40
41    fn len(&self) -> usize {
42        self.children.len()
43    }
44}
45
46impl TryFrom<&dyn Array> for ArrayData {
47    type Error = VortexError;
48
49    fn try_from(value: &dyn Array) -> Result<Self, Self::Error> {
50        Ok(ArrayData {
51            encoding: value.encoding(),
52            len: value.len(),
53            dtype: value.dtype().clone(),
54            metadata: value
55                .metadata()?
56                .ok_or_else(|| vortex_err!("Array does not support serialization"))?,
57            buffers: value.buffers().to_vec(),
58            children: value
59                .children()
60                .iter()
61                .map(|child| child.as_ref().try_into())
62                .try_collect()?,
63        })
64    }
65}
66
67impl TryFrom<&ArrayData> for ArrayRef {
68    type Error = VortexError;
69
70    fn try_from(value: &ArrayData) -> Result<Self, Self::Error> {
71        value.encoding.build(
72            &value.dtype,
73            value.len,
74            &value.metadata,
75            &value.buffers,
76            value,
77        )
78    }
79}