vortex_array/arrow/
datum.rs

1use arrow_array::{Array as ArrowArray, ArrayRef as ArrowArrayRef, Datum as ArrowDatum};
2use vortex_error::{VortexResult, vortex_panic};
3
4use crate::arrays::ConstantArray;
5use crate::arrow::{FromArrowArray, IntoArrowArray};
6use crate::compute::{scalar_at, slice};
7use crate::{Array, ArrayRef};
8
9/// A wrapper around a generic Arrow array that can be used as a Datum in Arrow compute.
10#[derive(Debug)]
11pub struct Datum {
12    array: ArrowArrayRef,
13    is_scalar: bool,
14}
15
16impl Datum {
17    /// Create a new [`Datum`] from an [`ArrayRef`], which can then be passed to Arrow compute.
18    pub fn try_new(array: ArrayRef) -> VortexResult<Self> {
19        if array.is_constant() {
20            Ok(Self {
21                array: slice(&array, 0, 1)?.into_arrow_preferred()?,
22                is_scalar: true,
23            })
24        } else {
25            Ok(Self {
26                array: array.into_arrow_preferred()?,
27                is_scalar: false,
28            })
29        }
30    }
31}
32
33impl ArrowDatum for Datum {
34    fn get(&self) -> (&dyn ArrowArray, bool) {
35        (&self.array, self.is_scalar)
36    }
37}
38
39/// Convert an Arrow array to an Array with a specific length.
40/// This is useful for compute functions that delegate to Arrow using [Datum],
41/// which will return a scalar (length 1 Arrow array) if the input array is constant.
42///
43/// Panics if the length of the array is not 1 and also not equal to the expected length.
44pub fn from_arrow_array_with_len<A>(array: A, len: usize, nullable: bool) -> VortexResult<ArrayRef>
45where
46    ArrayRef: FromArrowArray<A>,
47{
48    let array = ArrayRef::from_arrow(array, nullable);
49    if array.len() == len {
50        return Ok(array);
51    }
52
53    if array.len() != 1 {
54        vortex_panic!(
55            "Array length mismatch, expected {} got {} for encoding {}",
56            len,
57            array.len(),
58            array.encoding()
59        );
60    }
61
62    Ok(ConstantArray::new(scalar_at(&array, 0)?, len).into_array())
63}