vortex_array/arrow/
record_batch.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use arrow_array::RecordBatch;
5use arrow_array::cast::AsArray;
6use arrow_schema::{DataType, Schema};
7use vortex_error::{VortexError, VortexResult};
8
9use crate::arrays::StructArray;
10use crate::arrow::compute::{to_arrow, to_arrow_preferred};
11use crate::{Array, ToCanonical};
12
13impl TryFrom<&dyn Array> for RecordBatch {
14    type Error = VortexError;
15
16    fn try_from(value: &dyn Array) -> VortexResult<Self> {
17        value.to_struct().into_record_batch()
18    }
19}
20
21impl StructArray {
22    pub fn into_record_batch(self) -> VortexResult<RecordBatch> {
23        let array_ref = to_arrow_preferred(self.as_ref())?;
24        Ok(RecordBatch::from(array_ref.as_struct()))
25    }
26
27    pub fn into_record_batch_with_schema(self, schema: &Schema) -> VortexResult<RecordBatch> {
28        let data_type = DataType::Struct(schema.fields.clone());
29        let array_ref = to_arrow(self.as_ref(), &data_type)?;
30        Ok(RecordBatch::from(array_ref.as_struct()))
31    }
32}