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