use std::sync::Arc;
use itertools::Itertools;
use kernel::PARENT_KERNELS;
use vortex_dtype::DType;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_session::VortexSession;
use crate::ArrayRef;
use crate::EmptyMetadata;
use crate::ExecutionCtx;
use crate::arrays::struct_::StructArray;
use crate::arrays::struct_::compute::rules::PARENT_RULES;
use crate::buffer::BufferHandle;
use crate::serde::ArrayChildren;
use crate::validity::Validity;
use crate::vtable;
use crate::vtable::VTable;
use crate::vtable::ValidityVTableFromValidityHelper;
mod array;
mod kernel;
mod operations;
mod validity;
mod visitor;
use crate::vtable::ArrayId;
vtable!(Struct);
impl VTable for StructVTable {
type Array = StructArray;
type Metadata = EmptyMetadata;
type ArrayVTable = Self;
type OperationsVTable = Self;
type ValidityVTable = ValidityVTableFromValidityHelper;
type VisitorVTable = Self;
fn id(_array: &Self::Array) -> ArrayId {
Self::ID
}
fn metadata(_array: &StructArray) -> VortexResult<Self::Metadata> {
Ok(EmptyMetadata)
}
fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(vec![]))
}
fn deserialize(
_bytes: &[u8],
_dtype: &DType,
_len: usize,
_buffers: &[BufferHandle],
_session: &VortexSession,
) -> VortexResult<Self::Metadata> {
Ok(EmptyMetadata)
}
fn build(
dtype: &DType,
len: usize,
_metadata: &Self::Metadata,
_buffers: &[BufferHandle],
children: &dyn ArrayChildren,
) -> VortexResult<StructArray> {
let DType::Struct(struct_dtype, nullability) = dtype else {
vortex_bail!("Expected struct dtype, found {:?}", dtype)
};
let (validity, non_data_children) = if children.len() == struct_dtype.nfields() {
(Validity::from(*nullability), 0_usize)
} else if children.len() == struct_dtype.nfields() + 1 {
let validity = children.get(0, &Validity::DTYPE, len)?;
(Validity::Array(validity), 1_usize)
} else {
vortex_bail!(
"Expected {} or {} children, found {}",
struct_dtype.nfields(),
struct_dtype.nfields() + 1,
children.len()
);
};
let children: Vec<_> = (0..struct_dtype.nfields())
.map(|i| {
let child_dtype = struct_dtype
.field_by_index(i)
.vortex_expect("no out of bounds");
children.get(non_data_children + i, &child_dtype, len)
})
.try_collect()?;
StructArray::try_new_with_dtype(children, struct_dtype.clone(), len, validity)
}
fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
let DType::Struct(struct_dtype, _nullability) = &array.dtype else {
vortex_bail!("Expected struct dtype, found {:?}", array.dtype)
};
let (validity, non_data_children) = if children.len() == struct_dtype.nfields() {
(array.validity.clone(), 0_usize)
} else if children.len() == struct_dtype.nfields() + 1 {
(Validity::Array(children[0].clone()), 1_usize)
} else {
vortex_bail!(
"Expected {} or {} children, found {}",
struct_dtype.nfields(),
struct_dtype.nfields() + 1,
children.len()
);
};
let fields: Arc<[ArrayRef]> = children.into_iter().skip(non_data_children).collect();
vortex_ensure!(
fields.len() == struct_dtype.nfields(),
"Expected {} field children, found {}",
struct_dtype.nfields(),
fields.len()
);
array.fields = fields;
array.validity = validity;
Ok(())
}
fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
Ok(array.to_array())
}
fn reduce_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
PARENT_RULES.evaluate(array, parent, child_idx)
}
fn execute_parent(
array: &Self::Array,
parent: &ArrayRef,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
PARENT_KERNELS.execute(array, parent, child_idx, ctx)
}
}
#[derive(Debug)]
pub struct StructVTable;
impl StructVTable {
pub const ID: ArrayId = ArrayId::new_ref("vortex.struct");
}