This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] 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 44a4bdb287 Use fallible MutableBuffer methods in fallible APIs (#11973)
44a4bdb287 is described below
commit 44a4bdb287dfcc7297c5fb4c98d8c6d8c43f6e70
Author: RIchard Baah <[email protected]>
AuthorDate: Thu Nov 19 04:19:03 2026 +0000
Use fallible MutableBuffer methods in fallible APIs (#11973)
# Which issue does this PR address?
Closes #11900, following #11912.
# What changes are included in this PR?
Replace panicking buffer calls inside existing fallible APIs with their
direct fallible equivalents:
- Use `OffsetBuffer::try_new_zeroed(0)` for the empty
`try_from_repeated_length` case.
- Use `MutableBuffer::try_extend_from_slice` for sparse and dense union
type IDs.
- Map allocation failures from the union transform call sites to
`ArrowError::MemoryError`.
There are no changes to `MutableArrayData` orchestration or output
semantics.
---
arrow-array/src/array/fixed_size_binary_array.rs | 17 +++++++---
arrow-buffer/src/buffer/mutable.rs | 42 ++++++++++++++++++++++++
arrow-buffer/src/buffer/offset.rs | 20 +++++++++--
3 files changed, 72 insertions(+), 7 deletions(-)
diff --git a/arrow-array/src/array/fixed_size_binary_array.rs
b/arrow-array/src/array/fixed_size_binary_array.rs
index cb14445b7a..2f02b3b33e 100644
--- a/arrow-array/src/array/fixed_size_binary_array.rs
+++ b/arrow-array/src/array/fixed_size_binary_array.rs
@@ -406,7 +406,11 @@ impl FixedSizeBinaryArray {
///
/// # Errors
///
- /// Returns error if argument has length zero, or sizes of nested slices
don't match.
+ /// Returns an error for:
+ /// - Empty input
+ /// - Mismatched lengths
+ /// - Overflow
+ /// - Allocation failure
#[deprecated(
since = "28.0.0",
note = "This function will fail if the iterator produces only None
values; prefer `try_from_sparse_iter_with_size`"
@@ -421,14 +425,17 @@ impl FixedSizeBinaryArray {
let mut byte = 0;
let iter_size_hint = iter.size_hint().0;
- let mut null_buf = MutableBuffer::new(bit_util::ceil(iter_size_hint,
8));
- let mut buffer = MutableBuffer::new(0);
+ let mut null_buf =
MutableBuffer::try_with_capacity(bit_util::ceil(iter_size_hint, 8))
+ .map_err(|e| ArrowError::MemoryError(e.to_string()))?;
+ let mut buffer = MutableBuffer::default();
let mut prepend = 0;
iter.try_for_each(|item| -> Result<(), ArrowError> {
// extend null bitmask by one byte per each 8 items
if byte == 0 {
- null_buf.push(0u8);
+ null_buf
+ .try_push(0u8)
+ .map_err(|e| ArrowError::MemoryError(e.to_string()))?;
byte = 8;
}
byte -= 1;
@@ -562,7 +569,7 @@ impl FixedSizeBinaryArray {
// extend null bitmask by one byte per each 8 items
if byte == 0 {
null_buf
- .try_extend_from_slice(&[0u8])
+ .try_push(0u8)
.map_err(|e| ArrowError::MemoryError(e.to_string()))?;
byte = 8;
}
diff --git a/arrow-buffer/src/buffer/mutable.rs
b/arrow-buffer/src/buffer/mutable.rs
index 40db1161da..c87c33c55c 100644
--- a/arrow-buffer/src/buffer/mutable.rs
+++ b/arrow-buffer/src/buffer/mutable.rs
@@ -247,6 +247,12 @@ impl MutableBuffer {
MutableBuffer::from_len_zeroed(num_bytes)
}
+ /// Fallible version of [`MutableBuffer::new_null`].
+ pub fn try_new_null(len: usize) -> Result<Self, MutableBufferError> {
+ let num_bytes = bit_util::ceil(len, 8);
+ MutableBuffer::try_from_len_zeroed(num_bytes)
+ }
+
/// Set the bits in the range of `[0, end)` to 0 (if `val` is false), or 1
(if `val`
/// is true). Also extend the length of this buffer to be `end`.
///
@@ -675,6 +681,20 @@ impl MutableBuffer {
self.len += additional;
}
+ /// Fallible version of [`MutableBuffer::push`].
+ #[inline]
+ pub fn try_push<T: ToByteSlice>(&mut self, item: T) -> Result<(),
MutableBufferError> {
+ let additional = std::mem::size_of::<T>();
+ self.try_reserve(additional)?;
+ unsafe {
+ let src = item.to_byte_slice().as_ptr();
+ let dst = self.data.as_ptr().add(self.len);
+ std::ptr::copy_nonoverlapping(src, dst, additional);
+ }
+ self.len += additional;
+ Ok(())
+ }
+
/// Extends the buffer with a new item, without checking for sufficient
capacity
/// # Safety
/// Caller must ensure that the capacity()-len()>=`size_of<T>`()
@@ -1371,6 +1391,28 @@ mod tests {
assert_eq!(&[1u8, 1u8, 0, 0, 0, 0, 0, 0, 0], buf.as_slice());
}
+ #[test]
+ fn mutable_try_push_matches_push() {
+ let mut fallible = MutableBuffer::new(0);
+ fallible.try_push(1_u8).unwrap();
+ fallible.try_push(1_u64).unwrap();
+
+ let mut infallible = MutableBuffer::new(0);
+ infallible.push(1_u8);
+ infallible.push(1_u64);
+
+ assert_eq!(fallible.as_slice(), infallible.as_slice());
+ assert_eq!(fallible.len(), infallible.len());
+ assert_eq!(fallible.capacity(), infallible.capacity());
+ }
+
+ #[test]
+ fn mutable_try_new_null() {
+ let buffer = MutableBuffer::try_new_null(9).unwrap();
+ assert_eq!(buffer.len(), 2);
+ assert_eq!(buffer.as_slice(), &[0, 0]);
+ }
+
#[test]
fn mutable_push_unchecked_unaligned_u64() {
let mut buf = MutableBuffer::new(16);
diff --git a/arrow-buffer/src/buffer/offset.rs
b/arrow-buffer/src/buffer/offset.rs
index f2059babe9..8ef9f7755e 100644
--- a/arrow-buffer/src/buffer/offset.rs
+++ b/arrow-buffer/src/buffer/offset.rs
@@ -76,11 +76,20 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
}
/// Create a new [`OffsetBuffer`] containing a single 0 value
+ ///
+ /// # Panics
+ ///
+ /// Panics if allocation fails. Use [`Self::try_new_empty`] for a fallible
version.
pub fn new_empty() -> Self {
let buffer = MutableBuffer::from_len_zeroed(std::mem::size_of::<O>());
Self(buffer.into_buffer().into())
}
+ /// Fallible version of [`Self::new_empty`].
+ pub fn try_new_empty() -> Result<Self, OverflowError> {
+ Self::try_new_zeroed(0)
+ }
+
/// Create a new [`OffsetBuffer`] containing `len + 1` `0` values
///
/// # Panics
@@ -174,7 +183,8 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
///
/// # Panics
///
- /// Panics on overflow. Use [`Self::try_from_repeated_length`] for a
fallible version.
+ /// Panics on overflow or allocation failure.
+ /// Use [`Self::try_from_repeated_length`] for a fallible version.
pub fn from_repeated_length(length: usize, n: usize) -> Self {
Self::try_from_repeated_length(length, n).unwrap_or_else(|err|
panic!("{err}"))
}
@@ -193,7 +203,7 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
/// Errors if `length * n` overflows `usize` or `O`, or if the buffer
cannot be allocated.
pub fn try_from_repeated_length(length: usize, n: usize) -> Result<Self,
OverflowError> {
if n == 0 {
- return Ok(Self::new_empty());
+ return Self::try_new_empty();
}
if length == 0 {
@@ -706,6 +716,12 @@ mod tests {
assert_eq!(default.as_ref(), &[0]);
}
+ #[test]
+ fn try_new_empty() {
+ let buffer = OffsetBuffer::<i32>::try_new_empty().unwrap();
+ assert_eq!(buffer.as_ref(), &[0]);
+ }
+
#[test]
fn from_repeated_length_basic() {
// Basic case with length 4, repeated 3 times