Skip to main content

vortex_array/arrays/struct_/vtable/
operations.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::VortexResult;
5
6use crate::ExecutionCtx;
7use crate::array::ArrayView;
8use crate::array::OperationsVTable;
9use crate::arrays::Struct;
10use crate::arrays::struct_::StructArrayExt;
11use crate::scalar::Scalar;
12use crate::scalar::ScalarValue;
13
14impl OperationsVTable<Struct> for Struct {
15    fn scalar_at(
16        array: ArrayView<'_, Struct>,
17        index: usize,
18        ctx: &mut ExecutionCtx,
19    ) -> VortexResult<Scalar> {
20        let field_values = array
21            .iter_unmasked_fields()
22            .map(|field| field.execute_scalar(index, ctx).map(Scalar::into_value))
23            .collect::<VortexResult<Vec<_>>>()?;
24        // SAFETY: The vtable guarantees index is in-bounds and non-null before this is called.
25        // Each field's scalar_at returns a value with the field's own dtype.
26        Ok(unsafe {
27            Scalar::new_unchecked(
28                array.dtype().clone(),
29                Some(ScalarValue::Tuple(field_values)),
30            )
31        })
32    }
33}