Skip to main content

vortex_array/arrays/widen/vtable/
operations.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5use vortex_error::vortex_err;
6
7use crate::ExecutionCtx;
8use crate::array::ArrayView;
9use crate::array::OperationsVTable;
10use crate::array::ProbeState;
11use crate::arrays::Widen;
12use crate::arrays::widen::WidenSlots;
13use crate::dtype::DType;
14use crate::scalar::Scalar;
15
16impl OperationsVTable<Widen> for Widen {
17    type ProbeState = ();
18
19    fn probe_scalar(
20        state: &mut ProbeState<'_, Widen>,
21        index: usize,
22        ctx: &mut ExecutionCtx,
23    ) -> VortexResult<Scalar> {
24        let array = state.array();
25        let value = state
26            .slot(WidenSlots::VALUES)?
27            .ok_or_else(|| vortex_err!("Widen values slot is missing"))?
28            .execute_scalar(index, ctx)?;
29
30        decode_narrow_scalar(&value, array.dtype())
31    }
32
33    fn scalar_at(
34        array: ArrayView<'_, Widen>,
35        index: usize,
36        ctx: &mut ExecutionCtx,
37    ) -> VortexResult<Scalar> {
38        Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
39    }
40}
41
42/// Decodes `value`, a scalar of the values of a `Widen`, to its `logical` dtype.
43///
44/// The values have the nullability of the logical dtype, so a null value decodes to a null.
45fn decode_narrow_scalar(value: &Scalar, logical: &DType) -> VortexResult<Scalar> {
46    value.cast(logical)
47}