Skip to main content

vortex_arrow/executor/
primitive.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use arrow_array::ArrayRef as ArrowArrayRef;
7use arrow_array::ArrowPrimitiveType;
8use arrow_array::PrimitiveArray as ArrowPrimitiveArray;
9use vortex_array::ArrayRef;
10use vortex_array::ExecutionCtx;
11use vortex_array::arrays::PrimitiveArray;
12use vortex_array::builtins::ArrayBuiltins;
13use vortex_array::dtype::DType;
14use vortex_array::dtype::NativePType;
15use vortex_error::VortexResult;
16
17use crate::null_buffer::to_null_buffer;
18
19/// Convert a canonical PrimitiveArray directly to Arrow.
20pub fn canonical_primitive_to_arrow<T: ArrowPrimitiveType>(
21    array: PrimitiveArray,
22    ctx: &mut ExecutionCtx,
23) -> VortexResult<ArrowArrayRef>
24where
25    T::Native: NativePType,
26{
27    let validity = array
28        .as_ref()
29        .validity()?
30        .execute_mask(array.as_ref().len(), ctx)?;
31    let null_buffer = to_null_buffer(validity);
32    let buffer = array.into_buffer::<T::Native>().into_arrow_scalar_buffer();
33    Ok(Arc::new(ArrowPrimitiveArray::<T>::new(buffer, null_buffer)))
34}
35
36pub(super) fn to_arrow_primitive<T: ArrowPrimitiveType>(
37    array: ArrayRef,
38    ctx: &mut ExecutionCtx,
39) -> VortexResult<ArrowArrayRef>
40where
41    T::Native: NativePType,
42{
43    // Arrow exports validity separately, so no nullability change is needed.
44    let dtype = DType::Primitive(T::Native::PTYPE, array.dtype().nullability());
45    let array = array.cast(dtype)?;
46    let primitive = array.execute::<PrimitiveArray>(ctx)?;
47    canonical_primitive_to_arrow::<T>(primitive, ctx)
48}