use crate::data::ArrayDataLayout;
use crate::{ArrayData, ArrayDataBuilder, Buffers};
use arrow_buffer::buffer::NullBuffer;
use arrow_schema::DataType;
#[derive(Debug, Clone)]
pub struct StructArrayData {
data_type: DataType,
len: usize,
nulls: Option<NullBuffer>,
children: Vec<ArrayData>,
}
impl StructArrayData {
pub unsafe fn new_unchecked(
data_type: DataType,
len: usize,
nulls: Option<NullBuffer>,
children: Vec<ArrayData>,
) -> Self {
Self {
data_type,
len,
nulls,
children,
}
}
pub(crate) unsafe fn from_raw(builder: ArrayDataBuilder) -> Self {
let children = builder
.child_data
.into_iter()
.map(|x| x.slice(builder.offset, builder.len))
.collect();
Self {
data_type: builder.data_type,
len: builder.len,
nulls: builder.nulls,
children,
}
}
#[inline]
pub fn len(&self) -> usize {
self.len
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn nulls(&self) -> Option<&NullBuffer> {
self.nulls.as_ref()
}
#[inline]
pub fn children(&self) -> &[ArrayData] {
&self.children
}
#[inline]
pub fn data_type(&self) -> &DataType {
&self.data_type
}
pub fn into_parts(self) -> (DataType, Option<NullBuffer>, Vec<ArrayData>) {
(self.data_type, self.nulls, self.children)
}
pub fn slice(&self, offset: usize, len: usize) -> Self {
Self {
len,
data_type: self.data_type.clone(),
nulls: self.nulls.as_ref().map(|x| x.slice(offset, len)),
children: self.children.iter().map(|c| c.slice(offset, len)).collect(),
}
}
pub(crate) fn layout(&self) -> ArrayDataLayout<'_> {
ArrayDataLayout {
data_type: &self.data_type,
len: self.len,
offset: 0,
nulls: self.nulls.as_ref(),
buffers: Buffers::default(),
child_data: &self.children,
}
}
}