arrow_array

Module 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 from_ffi and to_ffi.

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, out_schema) = to_ffi(&data)?;

// import it
let data = unsafe { from_ffi(out_array, &out_schema) }?;
let array = Int32Array::from(data);

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

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(unsafe { from_ffi(array, &schema) }?))
}

Structsยง

Functionsยง

  • bit_width ๐Ÿ”’
  • create_buffer ๐Ÿ”’ โš 
    returns a new buffer corresponding to the index i of the FFI array. It may not exist (null pointer). bits is the number of bits that the native type of this buffer has. The size of the buffer will be ceil(self.length * bits, 8).
  • export_array_into_rawโš Deprecated
    Exports an array to raw pointers of the C Data Interface provided by the consumer.
  • from_ffiโš 
    Import ArrayData from the C Data Interface
  • Import ArrayData from the C Data Interface
  • Export to the C Data Interface

Type Aliasesยง