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-12410-5c260682dc26ee97dc526feaf706ebd96e10d5a6 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 6ec6d4600293f4e759e878c9dcb08c1417bf9f88 Author: Aloneangel20 <[email protected]> AuthorDate: Tue Nov 24 02:46:28 2026 +0000 fix(arrow-array): strip trailing bytes in values data when constructing `FixedSizeBinaryArray`s (#12410) # Which issue does this PR close? - Closes #12409 # Rationale for this change `FixedSizeBinaryArray::try_new` and `FixedSizeBinaryArray::try_new_with_len` allow constructing an array from a `Buffer` with trailing remainder bytes (tested in `test_constructors` for a 10-byte buffer with `value_length = 3` yielding `len() == 3`), but stored the unsliced `Buffer`. As a result, `FixedSizeBinaryArray::value_data()` and `values()` exposed trailing bytes past `len * value_size`, causing callers such as `FixedSizeBinaryBuilder::append_array` to append extra non-element bytes and shift subsequent elements. # What changes are included in this PR? - Update `FixedSizeBinaryArray::try_new_with_len` in `arrow-array/src/array/fixed_size_binary_array.rs` to slice `values` to `0..len * value_size` whenever `values.len() > len * value_size`, matching the invariant already maintained by `From<ArrayData>`, `slice()`, and `new_unchecked()`. - Add unit tests covering `value_data()`, `values()`, `to_data()`, `into_parts()`, and `FixedSizeBinaryBuilder::append_array` when the source `FixedSizeBinaryArray` is constructed from a buffer containing trailing bytes. # Are these changes tested? Yes. Added unit tests `test_try_new_strips_trailing_bytes` (`arrow-array/src/array/fixed_size_binary_array.rs`) and `test_fixed_size_binary_builder_append_array_trailing_bytes` (`arrow-array/src/builder/fixed_size_binary_builder.rs`) covering construction with trailing buffer bytes and appending into `FixedSizeBinaryBuilder`, and ran `arrow-array` and `arrow-select` tests alongside `rustfmt` and `clippy`. # Are there any significant user-facing changes? No. `FixedSizeBinaryArray::value_data()`, `values()`, `to_data()`, and `into_parts()` now expose only the `len * value_size` bytes belonging to the array rather than trailing buffer padding bytes. # AI usage disclosure An LLM coding assistant helped inspect callers, draft the fix and unit tests, and run local `cargo` verification, with all changes reviewed and verified before submission. --------- Co-authored-by: aloneangel45 <[email protected]> --- arrow-array/src/array/fixed_size_binary_array.rs | 41 ++++++++++++++++++++-- .../src/builder/fixed_size_binary_builder.rs | 17 +++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/arrow-array/src/array/fixed_size_binary_array.rs b/arrow-array/src/array/fixed_size_binary_array.rs index 2f02b3b33e..0b54aa6205 100644 --- a/arrow-array/src/array/fixed_size_binary_array.rs +++ b/arrow-array/src/array/fixed_size_binary_array.rs @@ -98,6 +98,9 @@ impl FixedSizeBinaryArray { /// Create a new [`FixedSizeBinaryArray`] with `value_length` bytes per element, panicking on /// failure /// + /// If `values.len()` is not an exact multiple of `value_length`, trailing bytes beyond + /// `(values.len() / value_length) * value_length` are truncated. + /// /// # Panics /// /// Panics if [`Self::try_new`] returns an error @@ -146,6 +149,10 @@ impl FixedSizeBinaryArray { /// buffer. If no null buffer is provided, the resulting array will have length zero. /// You can use [`Self::try_new_with_len`] to provide the length /// + /// If `values.len()` is not an exact multiple of `value_length`, trailing bytes beyond + /// `(values.len() / value_length) * value_length` are truncated so the stored values buffer + /// always has length `len * value_length`. + /// /// # Errors /// /// * `value_length < 0` @@ -195,6 +202,10 @@ impl FixedSizeBinaryArray { /// /// This is useful when the length cannot be determined from the provided values (in case of `value_length == 0`) or nulls (`nulls.is_none()`). /// + /// If `values` contains trailing bytes beyond `len * value_length` (while still satisfying + /// `values.len() / value_length == len`), they are truncated so the stored values buffer always + /// has length `len * value_length`. + /// /// # Errors /// /// * `value_length < 0` @@ -239,12 +250,22 @@ impl FixedSizeBinaryArray { )); } + // Guaranteed not to overflow: either `value_size == 0` or `values.len() / value_size == len` above. + let required_len = len + .checked_mul(value_size) + .expect("length bounded by values buffer check"); + let value_data = if values.len() > required_len { + values.slice_with_length(0, required_len) + } else { + values + }; + Ok(Self { data_type, - value_data: values, - value_size, + value_data, nulls, len, + value_size, }) } @@ -1346,4 +1367,20 @@ mod tests { "Invalid argument error: Buffer cannot have non-zero length if the value length is zero" ); } + + #[test] + fn test_try_new_strips_trailing_bytes() { + let buffer = Buffer::from_vec(vec![1_u8, 2, 3, 4, 5, 6, 7, 8, 9, 99]); + + let arr = FixedSizeBinaryArray::new(3, buffer.clone(), None); + assert_eq!(arr.len(), 3); + assert_eq!(arr.value_data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + assert_eq!(arr.values().len(), 9); + + let arr_with_len = FixedSizeBinaryArray::try_new_with_len(3, buffer, None, 3).unwrap(); + assert_eq!(arr_with_len.value_data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + assert_eq!(arr_with_len.values().len(), 9); + assert_eq!(arr_with_len.to_data().buffers()[0].len(), 9); + assert_eq!(arr_with_len.into_parts().1.len(), 9); + } } diff --git a/arrow-array/src/builder/fixed_size_binary_builder.rs b/arrow-array/src/builder/fixed_size_binary_builder.rs index cc18deadca..2367620f14 100644 --- a/arrow-array/src/builder/fixed_size_binary_builder.rs +++ b/arrow-array/src/builder/fixed_size_binary_builder.rs @@ -332,4 +332,21 @@ mod tests { let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 5); builder.append_array(&other_array).unwrap(); } + + #[test] + fn test_fixed_size_binary_builder_append_array_trailing_bytes() { + let trailing = FixedSizeBinaryArray::new(3, vec![1_u8, 2, 3, 4, 5, 6, 99].into(), None); + assert_eq!(trailing.len(), 2); + + let mut builder = FixedSizeBinaryBuilder::with_capacity(3, 3); + builder.append_array(&trailing).unwrap(); + builder.append_value([7, 8, 9]).unwrap(); + let array = builder.finish(); + + assert_eq!(array.len(), 3); + assert_eq!(array.value(0), &[1, 2, 3]); + assert_eq!(array.value(1), &[4, 5, 6]); + assert_eq!(array.value(2), &[7, 8, 9]); + assert_eq!(array.value_data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9]); + } }
