You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2025/07/29 15:00:04 UTC
(arrow-rs) branch main updated: Create empty buffer for a buffer specified in the C Data Interface with length zero (#8009)
This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 499de7dea6 Create empty buffer for a buffer specified in the C Data Interface with length zero (#8009)
499de7dea6 is described below
commit 499de7dea66b9500f82bd9f1096c62e6b868ee6a
Author: Liang-Chi Hsieh <vi...@gmail.com>
AuthorDate: Tue Jul 29 07:59:59 2025 -0700
Create empty buffer for a buffer specified in the C Data Interface with length zero (#8009)
# Which issue does this PR close?
- Closes #7549.
# Rationale for this change
The failure was described in the issue. In short, the buffer pointer of
an empty buffer array exported from polars is a dangling pointer not
aligned. But currently we take the raw pointer from C Data Interface and
check its alignment before interpreting it as `ScalarBuffer`. Thus it
causes the failure case.
# What changes are included in this PR?
This patch changes FFI module to create an empty buffer for exported
buffer with length zero. As we never dereference the dangling pointer,
seems it's not necessary to require the alignment for it.
For non empty buffers, we still keep the alignment check.
# Are these changes tested?
Added a unit test with necessary utility functions.
# Are there any user-facing changes?
No
---------
Co-authored-by: Liang-Chi Hsieh <li...@databricks.com>
Co-authored-by: Andrew Lamb <an...@nerdnetworks.org>
---
.github/workflows/arrow.yml | 5 ++++-
arrow-array/src/ffi.rs | 37 ++++++++++++++++++++++++++++++++++++-
arrow-buffer/src/buffer/scalar.rs | 23 +++++++++++++++++++++++
3 files changed, 63 insertions(+), 2 deletions(-)
diff --git a/.github/workflows/arrow.yml b/.github/workflows/arrow.yml
index 0b90a78577..9d2d776172 100644
--- a/.github/workflows/arrow.yml
+++ b/.github/workflows/arrow.yml
@@ -68,7 +68,10 @@ jobs:
- name: Test arrow-schema
run: cargo test -p arrow-schema --all-features
- name: Test arrow-array
- run: cargo test -p arrow-array --all-features
+ run: |
+ cargo test -p arrow-array --all-features
+ # Disable feature `force_validate`
+ cargo test -p arrow-array --features=ffi
- name: Test arrow-select
run: cargo test -p arrow-select --all-features
- name: Test arrow-cast
diff --git a/arrow-array/src/ffi.rs b/arrow-array/src/ffi.rs
index f3c34f6ccd..2ee2fd379e 100644
--- a/arrow-array/src/ffi.rs
+++ b/arrow-array/src/ffi.rs
@@ -408,7 +408,17 @@ impl ImportedArrowArray<'_> {
.map(|index| {
let len = self.buffer_len(index, variadic_buffer_lens, &self.data_type)?;
match unsafe { create_buffer(self.owner.clone(), self.array, index, len) } {
- Some(buf) => Ok(buf),
+ Some(buf) => {
+ // External libraries may use a dangling pointer for a buffer with length 0.
+ // We respect the array length specified in the C Data Interface. Actually,
+ // if the length is incorrect, we cannot create a correct buffer even if
+ // the pointer is valid.
+ if buf.is_empty() {
+ Ok(MutableBuffer::new(0).into())
+ } else {
+ Ok(buf)
+ }
+ }
None if len == 0 => {
// Null data buffer, which Rust doesn't allow. So create
// an empty buffer.
@@ -1296,9 +1306,15 @@ mod tests_to_then_from_ffi {
#[cfg(test)]
mod tests_from_ffi {
+ #[cfg(not(feature = "force_validate"))]
+ use std::ptr::NonNull;
use std::sync::Arc;
+ #[cfg(feature = "force_validate")]
use arrow_buffer::{bit_util, buffer::Buffer};
+ #[cfg(not(feature = "force_validate"))]
+ use arrow_buffer::{bit_util, buffer::Buffer, ScalarBuffer};
+
use arrow_data::transform::MutableArrayData;
use arrow_data::ArrayData;
use arrow_schema::{DataType, Field};
@@ -1660,6 +1676,25 @@ mod tests_from_ffi {
}
}
+ #[test]
+ #[cfg(not(feature = "force_validate"))]
+ fn test_utf8_view_ffi_from_dangling_pointer() {
+ let empty = GenericByteViewBuilder::<StringViewType>::new().finish();
+ let buffers = empty.data_buffers().to_vec();
+ let nulls = empty.nulls().cloned();
+
+ // Create a dangling pointer to a view buffer with zero length.
+ let alloc = Arc::new(1);
+ let buffer = unsafe { Buffer::from_custom_allocation(NonNull::<u8>::dangling(), 0, alloc) };
+ let views = unsafe { ScalarBuffer::new_unchecked(buffer) };
+
+ let str_view: GenericByteViewArray<StringViewType> =
+ unsafe { GenericByteViewArray::new_unchecked(views, buffers, nulls) };
+ let imported = roundtrip_byte_view_array(str_view);
+ assert_eq!(imported.len(), 0);
+ assert_eq!(&imported, &empty);
+ }
+
#[test]
fn test_round_trip_byte_view() {
fn test_case<T>()
diff --git a/arrow-buffer/src/buffer/scalar.rs b/arrow-buffer/src/buffer/scalar.rs
index 6c66060fb9..4dd516c708 100644
--- a/arrow-buffer/src/buffer/scalar.rs
+++ b/arrow-buffer/src/buffer/scalar.rs
@@ -72,6 +72,19 @@ impl<T: ArrowNativeType> ScalarBuffer<T> {
buffer.slice_with_length(byte_offset, byte_len).into()
}
+ /// Unsafe function to create a new [`ScalarBuffer`] from a [`Buffer`].
+ /// Only use for testing purpose.
+ ///
+ /// # Safety
+ ///
+ /// This function is unsafe because it does not check if the `buffer` is aligned
+ pub unsafe fn new_unchecked(buffer: Buffer) -> Self {
+ Self {
+ buffer,
+ phantom: Default::default(),
+ }
+ }
+
/// Free up unused memory.
pub fn shrink_to_fit(&mut self) {
self.buffer.shrink_to_fit();
@@ -99,6 +112,16 @@ impl<T: ArrowNativeType> ScalarBuffer<T> {
pub fn ptr_eq(&self, other: &Self) -> bool {
self.buffer.ptr_eq(&other.buffer)
}
+
+ /// Returns the number of elements in the buffer
+ pub fn len(&self) -> usize {
+ self.buffer.len() / std::mem::size_of::<T>()
+ }
+
+ /// Returns if the buffer is empty
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
}
impl<T: ArrowNativeType> Deref for ScalarBuffer<T> {