vortex_array/arrays/list/vtable/
operations.rs1use std::sync::Arc;
5
6use vortex_error::VortexResult;
7use vortex_error::vortex_err;
8
9use crate::ExecutionCtx;
10use crate::array::ArrayView;
11use crate::array::OperationsVTable;
12use crate::array::ProbeState;
13use crate::arrays::List;
14use crate::arrays::list::ListSlots;
15use crate::dtype::DType;
16use crate::scalar::Scalar;
17
18impl OperationsVTable<List> for List {
19 type ProbeState = ();
20
21 fn probe_scalar(
22 state: &mut ProbeState<'_, List>,
23 index: usize,
24 ctx: &mut ExecutionCtx,
25 ) -> VortexResult<Scalar> {
26 let array = state.array();
27 if !state.is_valid(index, ctx)? {
28 return Ok(Scalar::null(array.dtype().clone()));
29 }
30
31 let DType::List(element_dtype, nullability) = array.dtype() else {
32 unreachable!("List arrays have a list dtype")
33 };
34 let start = offset_at(state, index, ctx)?;
35 let end = offset_at(state, index + 1, ctx)?;
36 let mut elements = state
37 .slot(ListSlots::ELEMENTS)?
38 .ok_or_else(|| vortex_err!("List elements slot is missing"))?;
39 let scalars = (start..end)
40 .map(|i| elements.execute_scalar(i, ctx))
41 .collect::<VortexResult<Vec<_>>>()?;
42
43 Ok(Scalar::list(
44 Arc::clone(element_dtype),
45 scalars,
46 *nullability,
47 ))
48 }
49
50 fn scalar_at(
51 array: ArrayView<'_, List>,
52 index: usize,
53 ctx: &mut ExecutionCtx,
54 ) -> VortexResult<Scalar> {
55 Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
56 }
57}
58
59fn offset_at(
60 state: &mut ProbeState<'_, List>,
61 index: usize,
62 ctx: &mut ExecutionCtx,
63) -> VortexResult<usize> {
64 state
65 .slot(ListSlots::OFFSETS)?
66 .ok_or_else(|| vortex_err!("List offsets slot is missing"))?
67 .execute_scalar(index, ctx)?
68 .as_primitive()
69 .as_::<usize>()
70 .ok_or_else(|| vortex_err!("offset value does not fit in usize"))
71}