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-12052-258b0d203168186a9ed8064ae3c135f7b921b1e7 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 53ef3bf27737a7d6e57d174cf2e9feb8e78c7337 Author: llxgdtop <[email protected]> AuthorDate: Wed Nov 4 03:01:23 2026 +0000 fix(arrow-array): return an error instead of wrapping on MapBuilder offset overflow (#12052) # Which issue does this PR close? - Closes #12031 # Rationale for this change When the key and value builders hold more than `i32::MAX` entries, the `as i32` cast of the entry count wraps to a negative number. `append` still returns `Ok(())`, so the caller has no way to notice and ends up with a `MapArray` whose offsets are invalid. # What changes are included in this PR? - Add a private `next_offset` helper that converts the entry count with `i32::try_from` and returns an `InvalidArgumentError` on overflow. - Use it in `append` and `append_nulls`. The check runs before any state is mutated, so a rejected call leaves the builder unchanged. - Document the new error condition on both methods. # Are these changes tested? Yes. A new unit test uses a mock `ArrayBuilder` that reports a length of `i32::MAX + 1` without allocating, and asserts that both `append` and `append_nulls` return an error and leave the builder untouched. The test fails on `main` and passes with this change. # Are there any significant user-facing changes? No. The method signatures already returned `Result`; the only behavioural difference is that an overflow now surfaces as an error instead of silently corrupting the array. --- --- arrow-array/src/builder/map_builder.rs | 72 ++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/arrow-array/src/builder/map_builder.rs b/arrow-array/src/builder/map_builder.rs index 74093f8f98..7a92514504 100644 --- a/arrow-array/src/builder/map_builder.rs +++ b/arrow-array/src/builder/map_builder.rs @@ -167,24 +167,40 @@ impl<K: ArrayBuilder, V: ArrayBuilder> MapBuilder<K, V> { Ok(()) } + /// Returns the offset of the next map slot, i.e. the current number of entries. + /// + /// Returns an error if the number of entries does not fit in an `i32` offset. + #[inline] + fn next_offset(&self) -> Result<i32, ArrowError> { + let len = self.key_builder.len(); + i32::try_from(len).map_err(|_| { + ArrowError::InvalidArgumentError(format!( + "Cannot append to a map builder with {len} entries: offset exceeds i32::MAX" + )) + }) + } + /// Finish the current map array slot /// - /// Returns an error if the key and values builders are in an inconsistent state. + /// Returns an error if the key and values builders are in an inconsistent state, + /// or if the number of entries exceeds `i32::MAX`. #[inline] pub fn append(&mut self, is_valid: bool) -> Result<(), ArrowError> { self.validate_equal_lengths()?; - self.offsets_builder.push(self.key_builder.len() as i32); + let offset = self.next_offset()?; + self.offsets_builder.push(offset); self.null_buffer_builder.append(is_valid); Ok(()) } /// Append `n` nulls to this [`MapBuilder`] /// - /// Returns an error if the key and values builders are in an inconsistent state. + /// Returns an error if the key and values builders are in an inconsistent state, + /// or if the number of entries exceeds `i32::MAX`. #[inline] pub fn append_nulls(&mut self, n: usize) -> Result<(), ArrowError> { self.validate_equal_lengths()?; - let offset = self.key_builder.len() as i32; + let offset = self.next_offset()?; self.offsets_builder.extend(std::iter::repeat_n(offset, n)); self.null_buffer_builder.append_n_nulls(n); Ok(()) @@ -584,4 +600,52 @@ mod tests { assert_eq!(1, builder.keys().called); assert_eq!(1, builder.values().called); } + + /// Mock builder that reports an arbitrary length without allocating anything, + /// so offset overflow can be exercised without building `i32::MAX` entries. + struct FixedLenBuilder(usize); + + impl ArrayBuilder for FixedLenBuilder { + fn as_any(&self) -> &dyn Any { + self + } + + fn as_any_mut(&mut self) -> &mut dyn Any { + self + } + + fn into_box_any(self: Box<Self>) -> Box<dyn Any> { + self + } + + fn len(&self) -> usize { + self.0 + } + + fn finish(&mut self) -> ArrayRef { + panic!("finish should never be called on FixedLenBuilder") + } + + fn finish_cloned(&self) -> ArrayRef { + panic!("finish_cloned should never be called on FixedLenBuilder") + } + } + + #[test] + fn test_append_offset_overflow() { + let len = i32::MAX as usize + 1; + let mut builder = MapBuilder::new(None, FixedLenBuilder(len), FixedLenBuilder(len)); + + let result = builder.append(true); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("exceeds i32::MAX")); + + let result = builder.append_nulls(1); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("exceeds i32::MAX")); + + // The builder must be left untouched after a rejected append + assert_eq!(builder.offsets_builder, vec![0]); + assert_eq!(builder.len(), 0); + } }
