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