vortex_array/arrays/listview/vtable/
mod.rs1use std::hash::Hash;
5use std::sync::Arc;
6
7use vortex_error::VortexExpect;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure;
11use vortex_error::vortex_panic;
12use vortex_session::VortexSession;
13
14use crate::ArrayRef;
15use crate::DeserializeMetadata;
16use crate::ExecutionCtx;
17use crate::ExecutionResult;
18use crate::Precision;
19use crate::ProstMetadata;
20use crate::SerializeMetadata;
21use crate::arrays::ListViewArray;
22use crate::arrays::listview::compute::rules::PARENT_RULES;
23use crate::buffer::BufferHandle;
24use crate::dtype::DType;
25use crate::dtype::Nullability;
26use crate::dtype::PType;
27use crate::hash::ArrayEq;
28use crate::hash::ArrayHash;
29use crate::serde::ArrayChildren;
30use crate::stats::StatsSetRef;
31use crate::validity::Validity;
32use crate::vtable;
33use crate::vtable::ArrayId;
34use crate::vtable::VTable;
35use crate::vtable::ValidityVTableFromValidityHelper;
36use crate::vtable::validity_nchildren;
37use crate::vtable::validity_to_child;
38mod operations;
39mod validity;
40vtable!(ListView);
41
42#[derive(Clone, Debug)]
43pub struct ListView;
44
45impl ListView {
46 pub const ID: ArrayId = ArrayId::new_ref("vortex.listview");
47}
48
49#[derive(Clone, prost::Message)]
50pub struct ListViewMetadata {
51 #[prost(uint64, tag = "1")]
52 elements_len: u64,
53 #[prost(enumeration = "PType", tag = "2")]
54 offset_ptype: i32,
55 #[prost(enumeration = "PType", tag = "3")]
56 size_ptype: i32,
57}
58
59impl VTable for ListView {
60 type Array = ListViewArray;
61
62 type Metadata = ProstMetadata<ListViewMetadata>;
63 type OperationsVTable = Self;
64 type ValidityVTable = ValidityVTableFromValidityHelper;
65 fn vtable(_array: &Self::Array) -> &Self {
66 &ListView
67 }
68
69 fn id(&self) -> ArrayId {
70 Self::ID
71 }
72
73 fn len(array: &ListViewArray) -> usize {
74 debug_assert_eq!(array.offsets().len(), array.sizes().len());
75 array.offsets().len()
76 }
77
78 fn dtype(array: &ListViewArray) -> &DType {
79 &array.dtype
80 }
81
82 fn stats(array: &ListViewArray) -> StatsSetRef<'_> {
83 array.stats_set.to_ref(array.as_ref())
84 }
85
86 fn array_hash<H: std::hash::Hasher>(
87 array: &ListViewArray,
88 state: &mut H,
89 precision: Precision,
90 ) {
91 array.dtype.hash(state);
92 array.elements().array_hash(state, precision);
93 array.offsets().array_hash(state, precision);
94 array.sizes().array_hash(state, precision);
95 array.validity.array_hash(state, precision);
96 }
97
98 fn array_eq(array: &ListViewArray, other: &ListViewArray, precision: Precision) -> bool {
99 array.dtype == other.dtype
100 && array.elements().array_eq(other.elements(), precision)
101 && array.offsets().array_eq(other.offsets(), precision)
102 && array.sizes().array_eq(other.sizes(), precision)
103 && array.validity.array_eq(&other.validity, precision)
104 }
105
106 fn nbuffers(_array: &ListViewArray) -> usize {
107 0
108 }
109
110 fn buffer(_array: &ListViewArray, idx: usize) -> BufferHandle {
111 vortex_panic!("ListViewArray buffer index {idx} out of bounds")
112 }
113
114 fn buffer_name(_array: &ListViewArray, idx: usize) -> Option<String> {
115 vortex_panic!("ListViewArray buffer_name index {idx} out of bounds")
116 }
117
118 fn nchildren(array: &ListViewArray) -> usize {
119 3 + validity_nchildren(&array.validity)
120 }
121
122 fn child(array: &ListViewArray, idx: usize) -> ArrayRef {
123 match idx {
124 0 => array.elements().clone(),
125 1 => array.offsets().clone(),
126 2 => array.sizes().clone(),
127 3 => validity_to_child(&array.validity, array.len())
128 .vortex_expect("ListViewArray validity child out of bounds"),
129 _ => vortex_panic!("ListViewArray child index {idx} out of bounds"),
130 }
131 }
132
133 fn child_name(_array: &ListViewArray, idx: usize) -> String {
134 match idx {
135 0 => "elements".to_string(),
136 1 => "offsets".to_string(),
137 2 => "sizes".to_string(),
138 3 => "validity".to_string(),
139 _ => vortex_panic!("ListViewArray child_name index {idx} out of bounds"),
140 }
141 }
142
143 fn metadata(array: &ListViewArray) -> VortexResult<Self::Metadata> {
144 Ok(ProstMetadata(ListViewMetadata {
145 elements_len: array.elements().len() as u64,
146 offset_ptype: PType::try_from(array.offsets().dtype())? as i32,
147 size_ptype: PType::try_from(array.sizes().dtype())? as i32,
148 }))
149 }
150
151 fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
152 Ok(Some(metadata.serialize()))
153 }
154
155 fn deserialize(
156 bytes: &[u8],
157 _dtype: &DType,
158 _len: usize,
159 _buffers: &[BufferHandle],
160 _session: &VortexSession,
161 ) -> VortexResult<Self::Metadata> {
162 let metadata = <Self::Metadata as DeserializeMetadata>::deserialize(bytes)?;
163 Ok(ProstMetadata(metadata))
164 }
165
166 fn build(
167 dtype: &DType,
168 len: usize,
169 metadata: &Self::Metadata,
170 buffers: &[BufferHandle],
171 children: &dyn ArrayChildren,
172 ) -> VortexResult<ListViewArray> {
173 vortex_ensure!(
174 buffers.is_empty(),
175 "`ListViewArray::build` expects no buffers"
176 );
177
178 let DType::List(element_dtype, _) = dtype else {
179 vortex_bail!("Expected List dtype, got {:?}", dtype);
180 };
181
182 let validity = if children.len() == 3 {
183 Validity::from(dtype.nullability())
184 } else if children.len() == 4 {
185 let validity = children.get(3, &Validity::DTYPE, len)?;
186 Validity::Array(validity)
187 } else {
188 vortex_bail!(
189 "`ListViewArray::build` expects 3 or 4 children, got {}",
190 children.len()
191 );
192 };
193
194 let elements = children.get(
196 0,
197 element_dtype.as_ref(),
198 usize::try_from(metadata.0.elements_len)?,
199 )?;
200
201 let offsets = children.get(
203 1,
204 &DType::Primitive(metadata.0.offset_ptype(), Nullability::NonNullable),
205 len,
206 )?;
207
208 let sizes = children.get(
210 2,
211 &DType::Primitive(metadata.0.size_ptype(), Nullability::NonNullable),
212 len,
213 )?;
214
215 ListViewArray::try_new(elements, offsets, sizes, validity)
216 }
217
218 fn with_children(array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
219 vortex_ensure!(
220 children.len() == 3 || children.len() == 4,
221 "ListViewArray expects 3 or 4 children, got {}",
222 children.len()
223 );
224
225 let mut iter = children.into_iter();
226 let elements = iter
227 .next()
228 .vortex_expect("children length already validated");
229 let offsets = iter
230 .next()
231 .vortex_expect("children length already validated");
232 let sizes = iter
233 .next()
234 .vortex_expect("children length already validated");
235 let validity = if let Some(validity_array) = iter.next() {
236 Validity::Array(validity_array)
237 } else {
238 Validity::from(array.dtype.nullability())
239 };
240
241 let new_array = ListViewArray::try_new(elements, offsets, sizes, validity)?;
242 *array = new_array;
243 Ok(())
244 }
245
246 fn execute(array: Arc<Self::Array>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
247 Ok(ExecutionResult::done_upcast::<Self>(array))
248 }
249
250 fn reduce_parent(
251 array: &Self::Array,
252 parent: &ArrayRef,
253 child_idx: usize,
254 ) -> VortexResult<Option<ArrayRef>> {
255 PARENT_RULES.evaluate(array, parent, child_idx)
256 }
257}