Skip to main content

vortex_arrow/
datum.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use arrow_array::Array as ArrowArray;
5use arrow_array::ArrayRef as ArrowArrayRef;
6use arrow_array::Datum as ArrowDatum;
7use arrow_schema::DataType;
8use arrow_schema::Field;
9use vortex_array::ArrayRef;
10use vortex_array::ExecutionCtx;
11use vortex_array::IntoArray;
12use vortex_array::arrays::Constant;
13use vortex_array::arrays::ConstantArray;
14use vortex_error::VortexResult;
15use vortex_error::vortex_panic;
16
17use crate::ArrowSessionExt;
18
19/// A wrapper around a generic Arrow array that can be used as a Datum in Arrow compute.
20///
21/// A `Datum` has no Arrow field to hold extension metadata, so it fails for the same types that
22/// [`ArrowSession::default_arrow_export_field`] rejects.
23///
24/// [`ArrowSession::default_arrow_export_field`]: crate::ArrowSession::default_arrow_export_field
25#[derive(Debug)]
26pub struct Datum {
27    array: ArrowArrayRef,
28    is_scalar: bool,
29}
30
31impl Datum {
32    /// Create a new [`Datum`] from an [`ArrayRef`], which can then be passed to Arrow compute.
33    pub fn try_new(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
34        let session = ctx.session().clone();
35        if array.is::<Constant>() {
36            Ok(Self {
37                array: session
38                    .arrow()
39                    .execute_arrow(array.slice(0..1)?, None, ctx)?,
40                is_scalar: true,
41            })
42        } else {
43            Ok(Self {
44                array: session.arrow().execute_arrow(array.clone(), None, ctx)?,
45                is_scalar: false,
46            })
47        }
48    }
49
50    /// Create a new [`Datum`] from an `DynArrayData`, which can then be passed to Arrow compute.
51    /// This not try and convert the array to a scalar if it is constant.
52    pub fn try_new_array(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<Self> {
53        let session = ctx.session().clone();
54        Ok(Self {
55            array: session.arrow().execute_arrow(array.clone(), None, ctx)?,
56            is_scalar: false,
57        })
58    }
59
60    pub fn try_new_with_target_datatype(
61        array: &ArrayRef,
62        target_datatype: &DataType,
63        ctx: &mut ExecutionCtx,
64    ) -> VortexResult<Self> {
65        let session = ctx.session().clone();
66        let target_field = Field::new(
67            String::new(),
68            target_datatype.clone(),
69            array.dtype().is_nullable(),
70        );
71
72        if array.is::<Constant>() {
73            Ok(Self {
74                array: session.arrow().execute_arrow(
75                    array.slice(0..1)?,
76                    Some(&target_field),
77                    ctx,
78                )?,
79                is_scalar: true,
80            })
81        } else {
82            Ok(Self {
83                array: session
84                    .arrow()
85                    .execute_arrow(array.clone(), Some(&target_field), ctx)?,
86                is_scalar: false,
87            })
88        }
89    }
90
91    pub fn data_type(&self) -> &DataType {
92        self.array.data_type()
93    }
94}
95
96impl ArrowDatum for Datum {
97    fn get(&self) -> (&dyn ArrowArray, bool) {
98        (&self.array, self.is_scalar)
99    }
100}
101
102/// Convert an Arrow array to an Array with a specific length, using the provided
103/// [`ExecutionCtx`].
104///
105/// This is useful for compute functions that delegate to Arrow using [Datum],
106/// which will return a scalar (length 1 Arrow array) if the input array is constant.
107///
108/// The array is imported through the [`ArrowSession`](crate::ArrowSession) of `ctx`, so nested
109/// extension fields reach their registered import plugins.
110///
111/// # Error
112///
113/// The provided array must have length `len` or `1`.
114pub fn from_arrow_columnar(
115    array: ArrowArrayRef,
116    len: usize,
117    nullable: bool,
118    ctx: &mut ExecutionCtx,
119) -> VortexResult<ArrayRef> {
120    let session = ctx.session().clone();
121    let array = session.arrow().from_arrow_array(array, nullable)?;
122    if array.len() == len {
123        return Ok(array);
124    }
125
126    if array.len() != 1 {
127        vortex_panic!(
128            "Array length mismatch, expected {} got {} for encoding {}",
129            len,
130            array.len(),
131            array.encoding_id()
132        );
133    }
134
135    Ok(ConstantArray::new(array.execute_scalar(0, ctx)?, len).into_array())
136}