use std::hash::Hasher;
use std::sync::Arc;
use prost::Message;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_panic;
use vortex_session::VortexSession;
use crate::ArrayEq;
use crate::ArrayHash;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::ExecutionResult;
use crate::IntoArray;
use crate::Precision;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::ArrayView;
use crate::array::VTable;
use crate::arrays::list::ListArrayExt;
use crate::arrays::list::ListData;
use crate::arrays::list::array::NUM_SLOTS;
use crate::arrays::list::array::SLOT_NAMES;
use crate::arrays::list::compute::PARENT_KERNELS;
use crate::arrays::list::compute::rules::PARENT_RULES;
use crate::arrays::listview::list_view_from_list;
use crate::buffer::BufferHandle;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::serde::ArrayChildren;
use crate::validity::Validity;
mod operations;
mod validity;
pub type ListArray = Array<List>;
#[derive(Clone, prost::Message)]
pub struct ListMetadata {
#[prost(uint64, tag = "1")]
elements_len: u64,
#[prost(enumeration = "PType", tag = "2")]
offset_ptype: i32,
}
impl ArrayHash for ListData {
fn array_hash<H: Hasher>(&self, _state: &mut H, _precision: Precision) {}
}
impl ArrayEq for ListData {
fn array_eq(&self, _other: &Self, _precision: Precision) -> bool {
true
}
}
impl VTable for List {
type ArrayData = ListData;
type OperationsVTable = Self;
type ValidityVTable = Self;
fn id(&self) -> ArrayId {
Self::ID
}
fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
0
}
fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
vortex_panic!("ListArray buffer index {idx} out of bounds")
}
fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
vortex_panic!("ListArray buffer_name index {idx} out of bounds")
}
fn reduce_parent(
array: ArrayView<'_, Self>,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
PARENT_RULES.evaluate(array, parent, child_idx)
}
fn serialize(
array: ArrayView<'_, Self>,
_session: &VortexSession,
) -> VortexResult<Option<Vec<u8>>> {
Ok(Some(
ListMetadata {
elements_len: array.elements().len() as u64,
offset_ptype: PType::try_from(array.offsets().dtype())? as i32,
}
.encode_to_vec(),
))
}
fn validate(
&self,
_data: &ListData,
dtype: &DType,
len: usize,
slots: &[Option<ArrayRef>],
) -> VortexResult<()> {
vortex_ensure!(
slots.len() == NUM_SLOTS,
"ListArray expected {NUM_SLOTS} slots, found {}",
slots.len()
);
let elements = slots[crate::arrays::list::array::ELEMENTS_SLOT]
.as_ref()
.vortex_expect("ListArray elements slot");
let offsets = slots[crate::arrays::list::array::OFFSETS_SLOT]
.as_ref()
.vortex_expect("ListArray offsets slot");
vortex_ensure!(
offsets.len().saturating_sub(1) == len,
"ListArray length {} does not match outer length {}",
offsets.len().saturating_sub(1),
len
);
let actual_dtype = DType::List(Arc::new(elements.dtype().clone()), dtype.nullability());
vortex_ensure!(
&actual_dtype == dtype,
"ListArray dtype {} does not match outer dtype {}",
actual_dtype,
dtype
);
Ok(())
}
fn deserialize(
&self,
dtype: &DType,
len: usize,
metadata: &[u8],
_buffers: &[BufferHandle],
children: &dyn ArrayChildren,
_session: &VortexSession,
) -> VortexResult<crate::array::ArrayParts<Self>> {
let metadata = ListMetadata::decode(metadata)?;
let validity = if children.len() == 2 {
Validity::from(dtype.nullability())
} else if children.len() == 3 {
let validity = children.get(2, &Validity::DTYPE, len)?;
Validity::Array(validity)
} else {
vortex_bail!("Expected 2 or 3 children, got {}", children.len());
};
let DType::List(element_dtype, _) = &dtype else {
vortex_bail!("Expected List dtype, got {:?}", dtype);
};
let elements = children.get(
0,
element_dtype.as_ref(),
usize::try_from(metadata.elements_len)?,
)?;
let offsets = children.get(
1,
&DType::Primitive(metadata.offset_ptype(), Nullability::NonNullable),
len + 1,
)?;
let data = ListData::try_build(elements.clone(), offsets.clone(), validity.clone())?;
let slots = ListData::make_slots(&elements, &offsets, &validity, len);
Ok(crate::array::ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
}
fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
SLOT_NAMES[idx].to_string()
}
fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
Ok(ExecutionResult::done(
list_view_from_list(array, ctx)?.into_array(),
))
}
fn execute_parent(
array: ArrayView<'_, Self>,
parent: &ArrayRef,
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
PARENT_KERNELS.execute(array, parent, child_idx, ctx)
}
}
#[derive(Clone, Debug)]
pub struct List;
impl List {
pub const ID: ArrayId = ArrayId::new_ref("vortex.list");
}