vortex_array/arrays/list/vtable/
mod.rs1use std::hash::Hasher;
5use std::sync::Arc;
6
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure_eq;
11use vortex_error::vortex_err;
12use vortex_error::vortex_panic;
13use vortex_session::registry::CachedId;
14
15use crate::ArrayEq;
16use crate::ArrayHash;
17use crate::ArrayRef;
18use crate::EqMode;
19use crate::ExecutionCtx;
20use crate::ExecutionResult;
21use crate::IntoArray;
22use crate::array::Array;
23use crate::array::ArrayId;
24use crate::array::ArrayParts;
25use crate::array::ArrayView;
26use crate::array::VTable;
27use crate::array::with_empty_buffers;
28use crate::arrays::Primitive;
29use crate::arrays::list::ListArraySlotsExt;
30use crate::arrays::list::ListData;
31use crate::arrays::list::ListSlots;
32use crate::arrays::listview::list_view_from_list;
33use crate::buffer::BufferHandle;
34use crate::builders::ArrayBuilder;
35use crate::dtype::DType;
36use crate::dtype::PType;
37use crate::match_each_list_builder;
38use crate::require_child;
39mod operations;
40mod plugin;
41mod validity;
42pub type ListArray = Array<List>;
44
45#[derive(Clone, prost::Message)]
46pub struct ListMetadata {
47 #[prost(uint64, tag = "1")]
48 elements_len: u64,
49 #[prost(int32, tag = "2")]
50 offset_ptype: i32,
51}
52
53impl ListMetadata {
54 fn get_offset_ptype(&self) -> VortexResult<PType> {
55 PType::try_from(self.offset_ptype).map_err(|_| {
56 vortex_err!(
57 "ListMetadata offset_ptype must be a known PType, got {}",
58 self.offset_ptype
59 )
60 })
61 }
62}
63
64impl ArrayHash for ListData {
65 fn array_hash<H: Hasher>(&self, _state: &mut H, _accuracy: EqMode) {}
66}
67
68impl ArrayEq for ListData {
69 fn array_eq(&self, _other: &Self, _accuracy: EqMode) -> bool {
70 true
71 }
72}
73
74impl VTable for List {
75 type TypedArrayData = ListData;
76
77 type OperationsVTable = Self;
78 type ValidityVTable = Self;
79 fn id(&self) -> ArrayId {
80 static ID: CachedId = CachedId::new("vortex.list");
81 *ID
82 }
83
84 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
85 0
86 }
87
88 fn buffer(_array: ArrayView<'_, Self>, idx: usize) -> BufferHandle {
89 vortex_panic!("ListArray buffer index {idx} out of bounds")
90 }
91
92 fn buffer_name(_array: ArrayView<'_, Self>, idx: usize) -> Option<String> {
93 vortex_panic!("ListArray buffer_name index {idx} out of bounds")
94 }
95
96 fn with_buffers(
97 &self,
98 array: ArrayView<'_, Self>,
99 buffers: &[BufferHandle],
100 ) -> VortexResult<ArrayParts<Self>> {
101 with_empty_buffers(self, array, buffers)
102 }
103
104 fn validate(
105 &self,
106 _data: &ListData,
107 dtype: &DType,
108 len: usize,
109 slots: &[Option<ArrayRef>],
110 ) -> VortexResult<()> {
111 vortex_ensure_eq!(slots.len(), ListSlots::COUNT);
112 let elements = slots[ListSlots::ELEMENTS]
113 .as_ref()
114 .vortex_expect("ListArray elements slot");
115 let offsets = slots[ListSlots::OFFSETS]
116 .as_ref()
117 .vortex_expect("ListArray offsets slot");
118 vortex_ensure_eq!(
119 offsets.len().saturating_sub(1),
120 len,
121 "ListArray length does not match outer length",
122 );
123
124 let actual_dtype = DType::List(Arc::new(elements.dtype().clone()), dtype.nullability());
125 vortex_ensure_eq!(
126 &actual_dtype,
127 dtype,
128 "ListArray dtype does not match outer dtype",
129 );
130
131 Ok(())
132 }
133
134 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
135 ListSlots::NAMES[idx].to_string()
136 }
137
138 fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
139 let array = require_child!(array, array.offsets(), ListSlots::OFFSETS => Primitive);
140
141 Ok(ExecutionResult::done(
142 list_view_from_list(array, ctx)?.into_array(),
143 ))
144 }
145
146 #[expect(clippy::cognitive_complexity)]
148 fn append_to_builder(
149 array: ArrayView<'_, Self>,
150 builder: &mut dyn ArrayBuilder,
151 ctx: &mut ExecutionCtx,
152 ) -> VortexResult<()> {
153 match match_each_list_builder!(&mut *builder, |b| b.append_list_array(array, ctx)) {
154 Some(result) => result,
155 None => vortex_bail!(
156 "cannot append a List array of dtype {} to a {} builder",
157 array.dtype(),
158 builder.dtype()
159 ),
160 }
161 }
162}
163
164#[derive(Clone, Debug)]
165pub struct List;
166
167#[cfg(test)]
168mod tests {
169 use std::sync::Arc;
170 use std::sync::LazyLock;
171
172 use prost::Message;
173 use vortex_error::VortexResult;
174 use vortex_session::VortexSession;
175
176 use super::List;
177 use super::ListMetadata;
178 use crate::ArrayRef;
179 use crate::array::ArrayDeserialization;
180 use crate::array::ArrayPlugin;
181 use crate::array::VTable;
182 use crate::dtype::DType;
183 use crate::dtype::Nullability;
184 use crate::dtype::PType;
185
186 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
187
188 #[test]
189 fn test_deserialize_rejects_unknown_offset_ptype() -> VortexResult<()> {
190 let dtype = DType::List(
191 Arc::new(DType::Primitive(PType::I32, Nullability::NonNullable)),
192 Nullability::NonNullable,
193 );
194 let metadata = ListMetadata {
195 offset_ptype: 11,
196 ..Default::default()
197 }
198 .encode_to_vec();
199 let children: Vec<ArrayRef> = Vec::new();
200 let result = ArrayPlugin::deserialize(
201 &List,
202 ArrayDeserialization::new(VTable::id(&List), &dtype, 0, &metadata, &[], &children),
203 &SESSION,
204 );
205 assert!(result.is_err_and(|err| {
206 err.to_string()
207 .contains("ListMetadata offset_ptype must be a known PType, got 11")
208 }));
209
210 let known = ListMetadata {
211 offset_ptype: PType::U32 as i32,
212 ..Default::default()
213 };
214 assert_eq!(known.get_offset_ptype()?, PType::U32);
215
216 Ok(())
217 }
218}