Skip to main content

vortex_array/arrow/compute/to_arrow/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::any::Any;
5
6use arrow_array::ArrayRef as ArrowArrayRef;
7use arrow_schema::DataType;
8use vortex_error::VortexResult;
9use vortex_error::vortex_ensure;
10
11use crate::Array;
12use crate::LEGACY_SESSION;
13use crate::VortexSessionExecute;
14use crate::arrow::ArrowArrayExecutor;
15use crate::compute::Options;
16
17/// Convert a Vortex array to an Arrow array with the encoding's preferred `DataType`.
18///
19/// For example, a `VarBinArray` will be converted to an Arrow `VarBin` array, instead of the
20/// canonical `VarBinViewArray`.
21///
22/// Warning: do not use this to convert a Vortex [`crate::stream::ArrayStream`] since each array
23/// may have a different preferred Arrow type. Use [`to_arrow`] instead.
24#[deprecated(note = "Use ArrowArrayExecutor::execute_arrow instead")]
25#[expect(deprecated)]
26pub fn to_arrow_preferred(array: &dyn Array) -> VortexResult<ArrowArrayRef> {
27    to_arrow_opts(array, &ToArrowOptions { arrow_type: None })
28}
29
30/// Convert a Vortex array to an Arrow array of the given type.
31#[deprecated(note = "Use ArrowArrayExecutor::execute_arrow instead")]
32pub fn to_arrow(array: &dyn Array, arrow_type: &DataType) -> VortexResult<ArrowArrayRef> {
33    let mut ctx = LEGACY_SESSION.create_execution_ctx();
34    array.to_array().execute_arrow(Some(arrow_type), &mut ctx)
35}
36
37#[deprecated(note = "Use ArrowArrayExecutor::execute_arrow instead")]
38#[expect(deprecated)]
39pub fn to_arrow_opts(array: &dyn Array, options: &ToArrowOptions) -> VortexResult<ArrowArrayRef> {
40    let data_type = if let Some(data_type) = &options.arrow_type {
41        data_type.clone()
42    } else {
43        array.dtype().to_arrow_dtype()?
44    };
45    let arrow = to_arrow(array, &data_type)?;
46
47    vortex_ensure!(
48        &data_type == arrow.data_type(),
49        "to arrow returned array with data_type {}, expected {}",
50        arrow.data_type(),
51        data_type
52    );
53
54    Ok(arrow)
55}
56
57pub struct ToArrowOptions {
58    /// The Arrow data type to convert to, if specified.
59    pub arrow_type: Option<DataType>,
60}
61
62impl Options for ToArrowOptions {
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66}