vortex_array/arrays/list/vtable/
mod.rs1use std::hash::Hasher;
5use std::sync::Arc;
6
7use prost::Message;
8use vortex_error::VortexExpect;
9use vortex_error::VortexResult;
10use vortex_error::vortex_bail;
11use vortex_error::vortex_ensure;
12use vortex_error::vortex_panic;
13use vortex_session::VortexSession;
14
15use crate::ArrayEq;
16use crate::ArrayHash;
17use crate::ArrayRef;
18use crate::ExecutionCtx;
19use crate::ExecutionResult;
20use crate::IntoArray;
21use crate::Precision;
22use crate::array::Array;
23use crate::array::ArrayId;
24use crate::array::ArrayView;
25use crate::array::VTable;
26use crate::arrays::list::ListArrayExt;
27use crate::arrays::list::ListData;
28use crate::arrays::list::array::NUM_SLOTS;
29use crate::arrays::list::array::SLOT_NAMES;
30use crate::arrays::list::compute::PARENT_KERNELS;
31use crate::arrays::list::compute::rules::PARENT_RULES;
32use crate::arrays::listview::list_view_from_list;
33use crate::buffer::BufferHandle;
34use crate::dtype::DType;
35use crate::dtype::Nullability;
36use crate::dtype::PType;
37use crate::serde::ArrayChildren;
38use crate::validity::Validity;
39mod operations;
40mod validity;
41pub type ListArray = Array<List>;
43
44#[derive(Clone, prost::Message)]
45pub struct ListMetadata {
46 #[prost(uint64, tag = "1")]
47 elements_len: u64,
48 #[prost(enumeration = "PType", tag = "2")]
49 offset_ptype: i32,
50}
51
52impl ArrayHash for ListData {
53 fn array_hash<H: Hasher>(&self, _state: &mut H, _precision: Precision) {}
54}
55
56impl ArrayEq for ListData {
57 fn array_eq(&self, _other: &Self, _precision: Precision) -> bool {
58 true
59 }
60}
61
62impl VTable for List {
63 type ArrayData = ListData;
64
65 type OperationsVTable = Self;
66 type ValidityVTable = Self;
67
68 fn id(&self) -> ArrayId {
69 Self::ID
70 }
71
72 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
73 0
74 }
75
76 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
77 vortex_panic!("ListArray buffer index {idx} out of bounds")
78 }
79
80 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
81 vortex_panic!("ListArray buffer_name index {idx} out of bounds")
82 }
83
84 fn reduce_parent(
85 array: ArrayView<'_, Self>,
86 parent: &ArrayRef,
87 child_idx: usize,
88 ) -> VortexResult<Option<ArrayRef>> {
89 PARENT_RULES.evaluate(array, parent, child_idx)
90 }
91
92 fn serialize(
93 array: ArrayView<'_, Self>,
94 _session: &VortexSession,
95 ) -> VortexResult<Option<Vec<u8>>> {
96 Ok(Some(
97 ListMetadata {
98 elements_len: array.elements().len() as u64,
99 offset_ptype: PType::try_from(array.offsets().dtype())? as i32,
100 }
101 .encode_to_vec(),
102 ))
103 }
104
105 fn validate(
106 &self,
107 _data: &ListData,
108 dtype: &DType,
109 len: usize,
110 slots: &[Option<ArrayRef>],
111 ) -> VortexResult<()> {
112 vortex_ensure!(
113 slots.len() == NUM_SLOTS,
114 "ListArray expected {NUM_SLOTS} slots, found {}",
115 slots.len()
116 );
117 let elements = slots[crate::arrays::list::array::ELEMENTS_SLOT]
118 .as_ref()
119 .vortex_expect("ListArray elements slot");
120 let offsets = slots[crate::arrays::list::array::OFFSETS_SLOT]
121 .as_ref()
122 .vortex_expect("ListArray offsets slot");
123 vortex_ensure!(
124 offsets.len().saturating_sub(1) == len,
125 "ListArray length {} does not match outer length {}",
126 offsets.len().saturating_sub(1),
127 len
128 );
129
130 let actual_dtype = DType::List(Arc::new(elements.dtype().clone()), dtype.nullability());
131 vortex_ensure!(
132 &actual_dtype == dtype,
133 "ListArray dtype {} does not match outer dtype {}",
134 actual_dtype,
135 dtype
136 );
137
138 Ok(())
139 }
140
141 fn deserialize(
142 &self,
143 dtype: &DType,
144 len: usize,
145 metadata: &[u8],
146
147 _buffers: &[BufferHandle],
148 children: &dyn ArrayChildren,
149 _session: &VortexSession,
150 ) -> VortexResult<crate::array::ArrayParts<Self>> {
151 let metadata = ListMetadata::decode(metadata)?;
152 let validity = if children.len() == 2 {
153 Validity::from(dtype.nullability())
154 } else if children.len() == 3 {
155 let validity = children.get(2, &Validity::DTYPE, len)?;
156 Validity::Array(validity)
157 } else {
158 vortex_bail!("Expected 2 or 3 children, got {}", children.len());
159 };
160
161 let DType::List(element_dtype, _) = &dtype else {
162 vortex_bail!("Expected List dtype, got {:?}", dtype);
163 };
164 let elements = children.get(
165 0,
166 element_dtype.as_ref(),
167 usize::try_from(metadata.elements_len)?,
168 )?;
169
170 let offsets = children.get(
171 1,
172 &DType::Primitive(metadata.offset_ptype(), Nullability::NonNullable),
173 len + 1,
174 )?;
175
176 let data = ListData::try_build(elements.clone(), offsets.clone(), validity.clone())?;
177 let slots = ListData::make_slots(&elements, &offsets, &validity, len);
178 Ok(crate::array::ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
179 }
180
181 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
182 SLOT_NAMES[idx].to_string()
183 }
184
185 fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
186 Ok(ExecutionResult::done(
187 list_view_from_list(array, ctx)?.into_array(),
188 ))
189 }
190
191 fn execute_parent(
192 array: ArrayView<'_, Self>,
193 parent: &ArrayRef,
194 child_idx: usize,
195 ctx: &mut ExecutionCtx,
196 ) -> VortexResult<Option<ArrayRef>> {
197 PARENT_KERNELS.execute(array, parent, child_idx, ctx)
198 }
199}
200
201#[derive(Clone, Debug)]
202pub struct List;
203
204impl List {
205 pub const ID: ArrayId = ArrayId::new_ref("vortex.list");
206}