vortex_array/arrays/varbin/vtable/
operations.rs1use num_traits::ToPrimitive;
5use vortex_error::VortexExpect;
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::Primitive;
14use crate::arrays::VarBin;
15use crate::arrays::varbin::VarBinArraySlotsExt;
16use crate::arrays::varbin::VarBinSlots;
17use crate::arrays::varbin::varbin_scalar;
18use crate::match_each_integer_ptype;
19use crate::scalar::Scalar;
20
21impl OperationsVTable<VarBin> for VarBin {
22 type ProbeState = ();
23
24 fn probe_scalar(
25 state: &mut ProbeState<'_, VarBin>,
26 index: usize,
27 ctx: &mut ExecutionCtx,
28 ) -> VortexResult<Scalar> {
29 let array = state.array();
30 if !state.is_valid(index, ctx)? {
31 return Ok(Scalar::null(array.dtype().clone()));
32 }
33
34 let start = offset_at(state, index, ctx)?;
35 let end = offset_at(state, index + 1, ctx)?;
36 Ok(varbin_scalar(
37 array.bytes().slice(start..end),
38 array.dtype(),
39 ))
40 }
41
42 fn scalar_at(
43 array: ArrayView<'_, VarBin>,
44 index: usize,
45 ctx: &mut ExecutionCtx,
46 ) -> VortexResult<Scalar> {
47 Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
48 }
49}
50
51fn offset_at(
52 state: &mut ProbeState<'_, VarBin>,
53 index: usize,
54 ctx: &mut ExecutionCtx,
55) -> VortexResult<usize> {
56 if let Some(primitive) = state.array().offsets().as_opt::<Primitive>()
57 && primitive.buffer_handle().is_on_host()
58 {
59 return Ok(match_each_integer_ptype!(primitive.ptype(), |T| {
60 host_offset_at(primitive.as_slice::<T>(), index)
61 }));
62 }
63
64 let offset = state
65 .slot(VarBinSlots::OFFSETS)?
66 .ok_or_else(|| vortex_err!("VarBin offsets slot is missing"))?
67 .execute_scalar(index, ctx)?;
68 Ok((&offset)
69 .try_into()
70 .vortex_expect("Failed to convert offset to usize"))
71}
72
73fn host_offset_at<T: ToPrimitive>(offsets: &[T], index: usize) -> usize {
74 offsets[index]
75 .to_usize()
76 .vortex_expect("Failed to convert offset to usize")
77}