This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12149-57071551e6329209356a550f7683b6b1e4572871 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 32165430f475ca9f9e78a107eeebf3d64be3cd81 Author: AYUSH <[email protected]> AuthorDate: Sun Nov 15 17:18:12 2026 +0000 feat(arrow-array): add GenericByteArray::try_new_repeated (#12143) (#12149) Fixes #12143. `GenericByteArray::new_repeated` panics if the repeated byte length exceeds the offset type (e.g. `i32::MAX`), but its rustdoc only documented the `usize` overflow condition. This PR documents the offset overflow in `new_repeated` and adds a fallible `try_new_repeated` constructor that returns `Result<Self, ArrowError>`. Added unit tests in `arrow-array` covering `usize`, `i32`, and `i64` overflow boundaries, zero repeats, and empty values. All existing tests and clippy checks pass cleanly. Let me know if you would prefer any adjustments! --- arrow-array/src/array/byte_array.rs | 100 +++++++++++++++++++++++++++++++++++- arrow-buffer/src/buffer/offset.rs | 21 ++++++-- 2 files changed, 114 insertions(+), 7 deletions(-) diff --git a/arrow-array/src/array/byte_array.rs b/arrow-array/src/array/byte_array.rs index bd1d1b0443..f1781c0aab 100644 --- a/arrow-array/src/array/byte_array.rs +++ b/arrow-array/src/array/byte_array.rs @@ -180,8 +180,11 @@ impl<T: ByteArrayType> GenericByteArray<T> { /// Create a new [`GenericByteArray`] where `value` is repeated `repeat_count` times. /// /// # Panics - /// This will panic if value's length multiplied by `repeat_count` overflows usize. /// + /// Panics if value's length multiplied by `repeat_count` overflows `usize` + /// or exceeds `T::Offset::MAX`. + /// + /// Use [`Self::try_new_repeated`] for a fallible version. pub fn new_repeated(value: impl AsRef<T::Native>, repeat_count: usize) -> Self { let s: &[u8] = value.as_ref().as_ref(); let value_offsets = OffsetBuffer::from_repeated_length(s.len(), repeat_count); @@ -200,6 +203,46 @@ impl<T: ByteArrayType> GenericByteArray<T> { } } + /// Create a new [`GenericByteArray`] where `value` is repeated `repeat_count` times. + /// + /// # Errors + /// + /// Returns an error if value's length multiplied by `repeat_count` overflows `usize`, + /// exceeds `T::Offset::MAX`, or if the buffer cannot be allocated. + /// + /// # Example + /// ``` + /// # use arrow_array::{Array, StringArray}; + /// let array = StringArray::try_new_repeated("hello", 3).unwrap(); + /// assert_eq!(array.len(), 3); + /// assert_eq!(array.value(0), "hello"); + /// assert_eq!(array.value(1), "hello"); + /// assert_eq!(array.value(2), "hello"); + /// ``` + pub fn try_new_repeated( + value: impl AsRef<T::Native>, + repeat_count: usize, + ) -> Result<Self, ArrowError> { + let s: &[u8] = value.as_ref().as_ref(); + let value_offsets = OffsetBuffer::try_from_repeated_length(s.len(), repeat_count) + .map_err(|err| ArrowError::OffsetOverflowError(err.value().unwrap_or(usize::MAX)))?; + let bytes: Buffer = { + let mut mutable_buffer = MutableBuffer::with_capacity(0); + mutable_buffer + .try_repeat_slice_n_times(s, repeat_count) + .map_err(|e| ArrowError::MemoryError(e.to_string()))?; + + mutable_buffer.into() + }; + + Ok(Self { + data_type: T::DATA_TYPE, + value_data: bytes, + value_offsets, + nulls: None, + }) + } + /// Creates a [`GenericByteArray`] based on an iterator of values without nulls /// /// # Panics @@ -601,8 +644,9 @@ where #[cfg(test)] mod tests { - use crate::{Array, BinaryArray, StringArray}; + use crate::{Array, BinaryArray, LargeBinaryArray, StringArray}; use arrow_buffer::{Buffer, NullBuffer, OffsetBuffer}; + use arrow_schema::ArrowError; /// `from_iter_values` must work with iterators that report no upper size bound, /// and must not trust the size hint it does get. @@ -717,4 +761,56 @@ mod tests { fn create_repeated_i32_offset_overflow_2() { let _arr = BinaryArray::new_repeated(b"hello", ((i32::MAX as usize) / "hello".len()) + 1); } + + #[test] + fn try_new_repeated() { + // BinaryArray with multiple elements + let arr = BinaryArray::try_new_repeated(b"\x00\x01\xff", 3).unwrap(); + assert_eq!(arr.len(), 3); + assert_eq!(arr.null_count(), 0); + for i in 0..3 { + assert_eq!(arr.value(i), b"\x00\x01\xff"); + } + assert_eq!(arr.value_offsets(), &[0, 3, 6, 9]); + + // Zero repeats + let arr = BinaryArray::try_new_repeated(b"test", 0).unwrap(); + assert_eq!(arr.len(), 0); + assert_eq!(arr.value_offsets(), &[0]); + + // Empty values with non-zero repeats + let arr = BinaryArray::try_new_repeated(b"", 5).unwrap(); + assert_eq!(arr.len(), 5); + assert_eq!(arr.value_offsets(), &[0, 0, 0, 0, 0, 0]); + } + + #[test] + fn try_new_repeated_overflow() { + // usize multiplication overflow + let err = + BinaryArray::try_new_repeated(b"hello", (usize::MAX / "hello".len()) + 1).unwrap_err(); + assert!(matches!(err, ArrowError::OffsetOverflowError(usize::MAX))); + + // i32 offset overflow (fits in usize, exceeds i32::MAX) + let err = BinaryArray::try_new_repeated(b"a", (i32::MAX as usize) + 1).unwrap_err(); + assert!(matches!( + err, + ArrowError::OffsetOverflowError(val) if val == (i32::MAX as usize) + 1 + )); + + // i64 offset overflow (fits in usize, exceeds i64::MAX on 64-bit platforms) + #[cfg(target_pointer_width = "64")] + { + // Valid within i64 offset bounds, but offset-buffer allocation overflows memory/usize + let err = LargeBinaryArray::try_new_repeated(b"x", i64::MAX as usize).unwrap_err(); + assert!(matches!(err, ArrowError::OffsetOverflowError(_))); + + let err = + LargeBinaryArray::try_new_repeated(b"a", (i64::MAX as usize) + 1).unwrap_err(); + assert!(matches!( + err, + ArrowError::OffsetOverflowError(9223372036854775808) + )); + } + } } diff --git a/arrow-buffer/src/buffer/offset.rs b/arrow-buffer/src/buffer/offset.rs index fa7df6e95d..f2059babe9 100644 --- a/arrow-buffer/src/buffer/offset.rs +++ b/arrow-buffer/src/buffer/offset.rs @@ -190,7 +190,7 @@ impl<O: ArrowNativeType> OffsetBuffer<O> { /// /// # Errors /// - /// Errors if `length * n` overflows `usize` or `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()); @@ -208,11 +208,22 @@ impl<O: ArrowNativeType> OffsetBuffer<O> { O::from_usize(total_length) .ok_or_else(|| OverflowError::new::<O>("offset").with_value(total_length))?; - let offsets = (0..=n) - .map(|index| O::usize_as(index * length)) - .collect::<Vec<O>>(); + let len_bytes = n + .checked_add(1) + .and_then(|o| o.checked_mul(std::mem::size_of::<O>())) + .ok_or_else(|| OverflowError::new::<usize>("buffer length"))?; + + let mut buffer = MutableBuffer::try_with_capacity(len_bytes) + .map_err(|_| OverflowError::new::<usize>("buffer length"))?; - Ok(Self(ScalarBuffer::from(offsets))) + for index in 0..=n { + // SAFETY: buffer capacity was allocated for `(n + 1) * size_of::<O>()` bytes. + unsafe { + buffer.push_unchecked(O::usize_as(index * length)); + } + } + + Ok(Self(buffer.into_buffer().into())) } /// The first offset, i.e. the start of the first range.
