use std::borrow::Cow;
use std::sync::Arc;
use vortex_array::EmptyMetadata;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_err;
use vortex_session::registry::CachedId;
use crate::plan::Plan;
use crate::plan::PlanId;
use crate::plan::PlanParts;
use crate::plan::PlanRef;
use crate::plan::PlanSlots;
use crate::plan::PlanVTable;
const ELEMENTS: usize = 0;
const OFFSETS: usize = 1;
const VALIDITY: usize = 2;
#[derive(Clone, Debug)]
pub struct ListPack;
#[derive(Clone, Debug)]
pub struct ListPackData;
pub type ListPackPlan = Plan<ListPack>;
impl ListPackPlan {
pub unsafe fn from_slots_unchecked(dtype: DType, row_count: u64, slots: PlanSlots) -> Self {
PlanParts {
vtable: ListPack,
dtype,
row_count,
slots,
data: ListPackData,
}
.into_typed()
}
pub fn try_new(
nullability: Nullability,
row_count: u64,
elements: PlanRef,
offsets: PlanRef,
validity: Option<PlanRef>,
) -> VortexResult<Self> {
let dtype = DType::List(Arc::new(elements.dtype().clone()), nullability);
let slots = [Some(elements), Some(offsets), validity]
.into_iter()
.collect();
validate_slots(&dtype, row_count, &slots)?;
Ok(unsafe { Self::from_slots_unchecked(dtype, row_count, slots) })
}
pub fn elements(&self) -> VortexResult<PlanRef> {
self.slot_required(ELEMENTS)
}
pub fn offsets(&self) -> VortexResult<PlanRef> {
self.slot_required(OFFSETS)
}
pub fn validity(&self) -> VortexResult<Option<PlanRef>> {
self.slot(VALIDITY)
}
}
impl PlanVTable for ListPack {
type PlanData = ListPackData;
type Metadata = EmptyMetadata;
fn id(&self) -> PlanId {
static ID: CachedId = CachedId::new("vortex.plan.list_pack");
*ID
}
fn metadata(_plan: &Plan<Self>) -> Option<Self::Metadata> {
Some(EmptyMetadata)
}
fn with_slots(
plan: &Plan<Self>,
slots: &PlanSlots,
_data: &mut Self::PlanData,
) -> VortexResult<()> {
validate_slots(plan.dtype(), plan.row_count(), slots)
}
fn slot_name(_plan: &Plan<Self>, slot: usize) -> Cow<'_, str> {
match slot {
ELEMENTS => Cow::Borrowed("elements"),
OFFSETS => Cow::Borrowed("offsets"),
VALIDITY => Cow::Borrowed("validity"),
_ => Cow::Owned(format!("slot[{slot}]")),
}
}
}
fn validate_slots(dtype: &DType, row_count: u64, slots: &PlanSlots) -> VortexResult<()> {
let elements_dtype = dtype
.as_list_element_opt()
.ok_or_else(|| vortex_err!("ListPack output dtype must be a list, got {dtype}"))?;
slots.check_len("ListPack", VALIDITY + 1)?;
let elements = slots.required(ELEMENTS)?;
if elements.dtype() != elements_dtype.as_ref() {
vortex_bail!(
"ListPack elements child has dtype {} but the list element dtype is {}",
elements.dtype(),
elements_dtype
);
}
let offsets = slots.required(OFFSETS)?;
if !offsets.dtype().is_int() || offsets.dtype().is_nullable() {
vortex_bail!(
"ListPack offsets child must have a non-nullable integer dtype, got {}",
offsets.dtype()
);
}
let offsets_row_count = row_count
.checked_add(1)
.ok_or_else(|| vortex_err!("ListPack offsets row count overflow"))?;
if offsets.row_count() != offsets_row_count {
vortex_bail!(
"ListPack offsets child has {} rows but must have {offsets_row_count}",
offsets.row_count()
);
}
match (dtype.is_nullable(), slots.get(VALIDITY)?) {
(true, Some(validity)) => {
let validity_dtype = DType::Bool(Nullability::NonNullable);
if validity.dtype() != &validity_dtype {
vortex_bail!(
"ListPack validity child has dtype {} but must have dtype {validity_dtype}",
validity.dtype()
);
}
if validity.row_count() != row_count {
vortex_bail!(
"ListPack validity child has {} rows but the plan has {row_count}",
validity.row_count()
);
}
}
(false, None) => {}
(true, None) => vortex_bail!("Nullable ListPack has no validity child"),
(false, Some(_)) => vortex_bail!("Non-nullable ListPack has a validity child"),
}
Ok(())
}