Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip null buffer when importing FFI ArrowArray struct if no null buffer in the spec #3293

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions arrow/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,18 @@ impl FFI_ArrowArray {
/// This method releases `buffers`. Consumers of this struct *must* call `release` before
/// releasing this struct, or contents in `buffers` leak.
pub fn new(data: &ArrayData) -> Self {
// * insert the null buffer at the start
// * make all others `Option<Buffer>`.
let buffers = iter::once(data.null_buffer().cloned())
.chain(data.buffers().iter().map(|b| Some(b.clone())))
.collect::<Vec<_>>();
let data_layout = layout(data.data_type());

let buffers = if data_layout.can_contain_null_mask {
// * insert the null buffer at the start
// * make all others `Option<Buffer>`.
iter::once(data.null_buffer().cloned())
.chain(data.buffers().iter().map(|b| Some(b.clone())))
.collect::<Vec<_>>()
} else {
data.buffers().iter().map(|b| Some(b.clone())).collect()
};

// `n_buffers` is the number of buffers by the spec.
let n_buffers = {
data_layout.buffers.len() + {
Expand Down Expand Up @@ -616,8 +622,15 @@ pub trait ArrowArrayRef {
let len = self.array().len();
let offset = self.array().offset();
let null_count = self.array().null_count();
let buffers = self.buffers()?;
let null_bit_buffer = self.null_bit_buffer();

let data_layout = layout(&data_type);
let buffers = self.buffers(data_layout.can_contain_null_mask)?;

let null_bit_buffer = if data_layout.can_contain_null_mask {
self.null_bit_buffer()
} else {
None
};

let mut child_data: Vec<ArrayData> = (0..self.array().n_children as usize)
.map(|i| {
Expand Down Expand Up @@ -649,19 +662,24 @@ pub trait ArrowArrayRef {
}

/// returns all buffers, as organized by Rust (i.e. null buffer is skipped)
fn buffers(&self) -> Result<Vec<Buffer>> {
(0..self.array().n_buffers - 1)
fn buffers(&self, can_contain_null_mask: bool) -> Result<Vec<Buffer>> {
let buffer_begin = if can_contain_null_mask {
// + 1: skip null buffer
1
} else {
0
};
(buffer_begin..self.array().n_buffers)
.map(|index| {
// + 1: skip null buffer
let index = (index + 1) as usize;
let index = index as usize;

let len = self.buffer_len(index)?;

unsafe { create_buffer(self.owner().clone(), self.array(), index, len) }
.ok_or_else(|| {
ArrowError::CDataInterface(format!(
"The external buffer at position {} is null.",
index - 1
index
))
})
})
Expand Down