vortex_scan/plan/builtins/
list_pack.rs1use std::borrow::Cow;
5use std::sync::Arc;
6
7use vortex_array::EmptyMetadata;
8use vortex_array::dtype::DType;
9use vortex_array::dtype::Nullability;
10use vortex_error::VortexResult;
11use vortex_error::vortex_bail;
12use vortex_error::vortex_err;
13use vortex_session::registry::CachedId;
14
15use crate::plan::Plan;
16use crate::plan::PlanId;
17use crate::plan::PlanParts;
18use crate::plan::PlanRef;
19use crate::plan::PlanSlots;
20use crate::plan::PlanVTable;
21
22const ELEMENTS: usize = 0;
23const OFFSETS: usize = 1;
24const VALIDITY: usize = 2;
25
26#[derive(Clone, Debug)]
29pub struct ListPack;
30
31#[derive(Clone, Debug)]
33pub struct ListPackData;
34
35pub type ListPackPlan = Plan<ListPack>;
37
38impl ListPackPlan {
39 pub unsafe fn from_slots_unchecked(dtype: DType, row_count: u64, slots: PlanSlots) -> Self {
48 PlanParts {
49 vtable: ListPack,
50 dtype,
51 row_count,
52 slots,
53 data: ListPackData,
54 }
55 .into_typed()
56 }
57
58 pub fn try_new(
63 nullability: Nullability,
64 row_count: u64,
65 elements: PlanRef,
66 offsets: PlanRef,
67 validity: Option<PlanRef>,
68 ) -> VortexResult<Self> {
69 let dtype = DType::List(Arc::new(elements.dtype().clone()), nullability);
70 let slots = [Some(elements), Some(offsets), validity]
71 .into_iter()
72 .collect();
73 validate_slots(&dtype, row_count, &slots)?;
74
75 Ok(unsafe { Self::from_slots_unchecked(dtype, row_count, slots) })
77 }
78
79 pub fn elements(&self) -> VortexResult<PlanRef> {
81 self.slot_required(ELEMENTS)
82 }
83
84 pub fn offsets(&self) -> VortexResult<PlanRef> {
86 self.slot_required(OFFSETS)
87 }
88
89 pub fn validity(&self) -> VortexResult<Option<PlanRef>> {
91 self.slot(VALIDITY)
92 }
93}
94
95impl PlanVTable for ListPack {
96 type PlanData = ListPackData;
97 type Metadata = EmptyMetadata;
98
99 fn id(&self) -> PlanId {
100 static ID: CachedId = CachedId::new("vortex.plan.list_pack");
101 *ID
102 }
103
104 fn metadata(_plan: &Plan<Self>) -> Option<Self::Metadata> {
105 Some(EmptyMetadata)
107 }
108
109 fn with_slots(
110 plan: &Plan<Self>,
111 slots: &PlanSlots,
112 _data: &mut Self::PlanData,
113 ) -> VortexResult<()> {
114 validate_slots(plan.dtype(), plan.row_count(), slots)
115 }
116
117 fn slot_name(_plan: &Plan<Self>, slot: usize) -> Cow<'_, str> {
118 match slot {
119 ELEMENTS => Cow::Borrowed("elements"),
120 OFFSETS => Cow::Borrowed("offsets"),
121 VALIDITY => Cow::Borrowed("validity"),
122 _ => Cow::Owned(format!("slot[{slot}]")),
123 }
124 }
125}
126
127fn validate_slots(dtype: &DType, row_count: u64, slots: &PlanSlots) -> VortexResult<()> {
128 let elements_dtype = dtype
129 .as_list_element_opt()
130 .ok_or_else(|| vortex_err!("ListPack output dtype must be a list, got {dtype}"))?;
131 slots.check_len("ListPack", VALIDITY + 1)?;
132
133 let elements = slots.required(ELEMENTS)?;
134 if elements.dtype() != elements_dtype.as_ref() {
135 vortex_bail!(
136 "ListPack elements child has dtype {} but the list element dtype is {}",
137 elements.dtype(),
138 elements_dtype
139 );
140 }
141
142 let offsets = slots.required(OFFSETS)?;
143 if !offsets.dtype().is_int() || offsets.dtype().is_nullable() {
144 vortex_bail!(
145 "ListPack offsets child must have a non-nullable integer dtype, got {}",
146 offsets.dtype()
147 );
148 }
149 let offsets_row_count = row_count
150 .checked_add(1)
151 .ok_or_else(|| vortex_err!("ListPack offsets row count overflow"))?;
152 if offsets.row_count() != offsets_row_count {
153 vortex_bail!(
154 "ListPack offsets child has {} rows but must have {offsets_row_count}",
155 offsets.row_count()
156 );
157 }
158
159 match (dtype.is_nullable(), slots.get(VALIDITY)?) {
160 (true, Some(validity)) => {
161 let validity_dtype = DType::Bool(Nullability::NonNullable);
162 if validity.dtype() != &validity_dtype {
163 vortex_bail!(
164 "ListPack validity child has dtype {} but must have dtype {validity_dtype}",
165 validity.dtype()
166 );
167 }
168 if validity.row_count() != row_count {
169 vortex_bail!(
170 "ListPack validity child has {} rows but the plan has {row_count}",
171 validity.row_count()
172 );
173 }
174 }
175 (false, None) => {}
176 (true, None) => vortex_bail!("Nullable ListPack has no validity child"),
177 (false, Some(_)) => vortex_bail!("Non-nullable ListPack has a validity child"),
178 }
179 Ok(())
180}