Module arrow::ffi

source ·
Expand description

Contains declarations to bind to the C Data Interface.

Generally, this module is divided in two main interfaces: One interface maps C ABI to native Rust types, i.e. convert c-pointers, c_char, to native rust. This is handled by FFI_ArrowSchema and FFI_ArrowArray.

The second interface maps native Rust types to the Rust-specific implementation of Arrow such as format to Datatype, Buffer, etc. This is handled by ArrowArray.

Export to FFI

// create an array natively
let array = Int32Array::from(vec![Some(1), None, Some(3)]);
let data = array.into_data();

// Export it
let out_array = FFI_ArrowArray::new(&data);
let out_schema = FFI_ArrowSchema::try_from(data.data_type())?;

// import it
let array = ArrowArray::new(out_array, out_schema);
let array = Int32Array::from(ArrayData::try_from(array)?);

// perform some operation
let array = arithmetic::add(&array, &array)?;

// verify
assert_eq!(array, Int32Array::from(vec![Some(2), None, Some(6)]));

Import from FFI

/// A foreign data container that can export to C Data interface
struct ForeignArray {};

impl ForeignArray {
    /// Export from foreign array representation to C Data interface
    /// e.g. <https://github.com/apache/arrow/blob/fc1f9ebbc4c3ae77d5cfc2f9322f4373d3d19b8a/python/pyarrow/array.pxi#L1552>
    fn export_to_c(&self, array: *mut FFI_ArrowArray, schema: *mut FFI_ArrowSchema) {
        // ...
    }
}

/// Import an [`ArrayRef`] from a [`ForeignArray`]
fn import_array(foreign: &ForeignArray) -> Result<ArrayRef, ArrowError> {
    let mut schema = FFI_ArrowSchema::empty();
    let mut array = FFI_ArrowArray::empty();
    foreign.export_to_c(addr_of_mut!(array), addr_of_mut!(schema));
    Ok(make_array(ArrowArray::new(array, schema).try_into()?))
}

Structs

Traits