rerun_c/
arrow_utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use arrow::ffi::FFI_ArrowArray;

use crate::{CError, CErrorCode};

/// Converts a C-FFI arrow array into a Rust component batch, taking ownership of the underlying arrow data.
///
/// ### Safety
/// This struct assumes that the incoming data agrees with the C data interface.
#[allow(unsafe_code)]
#[allow(clippy::result_large_err)]
pub unsafe fn arrow_array_from_c_ffi(
    array: FFI_ArrowArray,
    datatype: arrow::datatypes::DataType,
) -> Result<arrow::array::ArrayRef, CError> {
    // arrow-rs implements `Drop` for `FFI_ArrowArray`.
    //
    // Therefore, for things to work correctly we have to take ownership of the array!
    // All methods passing arrow arrays through our C interface are documented to take ownership of the component batch.
    // I.e. the user should NOT call `release`.
    //
    // This makes sense because from here on out we want to manage the lifetime of the underlying schema and array data
    // from the rust side.
    unsafe { arrow::ffi::from_ffi_and_data_type(array, datatype) }
        .map(arrow::array::make_array)
        .map_err(|err| {
            CError::new(
                CErrorCode::ArrowFfiArrayImportError,
                &format!("Failed to import ffi array: {err}"),
            )
        })
}