df_interchange/
to_arrow.rsuse paste::paste;
use std::mem::transmute;
use crate::{error::InterchangeError, ArrowArray, ArrowSchema, Interchange};
macro_rules! ffi_to_arrow {
($to_ver:literal) => {
paste! {
impl Interchange {
#[doc = "Move Arrow data interchange format to Arrow version `" $to_ver "`."]
pub fn [<to_arrow_ $to_ver>](mut self) -> Result<Vec<[<arrow_crate_ $to_ver>]::record_batch::RecordBatch>, InterchangeError> {
let num_cols = self.ffi.len();
let num_chunks = self.ffi[0].1.len();
let mut batches = Vec::with_capacity(num_chunks);
for _ in 0..num_chunks {
let mut arrays = Vec::with_capacity(num_cols);
let mut fields = Vec::with_capacity(num_cols);
for col_num in 0..num_cols {
let chunk = self.ffi[col_num].1.pop().unwrap();
let ffi_array = unsafe { transmute::<ArrowArray, [<arrow_crate_ $to_ver>]::ffi::FFI_ArrowArray>(chunk.0) };
let ffi_schema = unsafe { transmute::<ArrowSchema, [<arrow_crate_ $to_ver>]::ffi::FFI_ArrowSchema>(chunk.1) };
let from_ffi = unsafe { [<arrow_crate_ $to_ver>]::ffi::from_ffi(ffi_array, &ffi_schema) }?;
let array_ref = [<arrow_crate_ $to_ver>]::array::make_array(from_ffi);
let field = std::convert::TryInto::<[<arrow_crate_ $to_ver>]::datatypes::Field>::try_into(&ffi_schema)?;
arrays.push(array_ref);
fields.push(std::sync::Arc::new(field));
}
let schema = [<arrow_crate_ $to_ver>]::datatypes::Schema::new(fields);
let record_batch = [<arrow_crate_ $to_ver>]::record_batch::RecordBatch::try_new(std::sync::Arc::new(schema), arrays)?;
batches.push(record_batch)
}
Ok(batches)
}
}
}
};
}
#[cfg(feature = "arrow_50")]
ffi_to_arrow!("50");
#[cfg(feature = "arrow_51")]
ffi_to_arrow!("51");
#[cfg(feature = "arrow_52")]
ffi_to_arrow!("52");
#[cfg(feature = "arrow_53")]
ffi_to_arrow!("53");
#[cfg(feature = "arrow_54")]
ffi_to_arrow!("54");