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