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-12119-fb8b1268ccfb1103fa8c559759014934f528b660 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 58298546609000554a1aa55fbe802294d5dfbd35 Author: EaswarKilari <[email protected]> AuthorDate: Mon Nov 9 23:40:53 2026 +0000 docs(parquet): add SAFETY comments for build_unchecked in array readers (#12119) # Which issue does this PR close? - Part of #7044. # Rationale for this change A few `build_unchecked` calls in the parquet array readers had no comment explaining why they're safe. # What changes are included in this PR? - Added `SAFETY` comments in the list, fixed size list and fixed len byte array readers. - In the fixed len byte array reader, the buffer length check was a `debug_assert`, so it's now a real check that returns an error in release builds too. I left `OffsetBuffer::into_array` out for now, since #11144 is fixing the UTF-8 issue there (#11140). Happy to add the comment once that's merged. # Are these changes tested? Covered by existing tests. The new check shouldn't trigger for valid files. # Are there any significant user-facing changes? No. --------- Co-authored-by: Richard Baah <[email protected]> --- .../src/arrow/array_reader/fixed_len_byte_array.rs | 24 ++++++++++++++-------- .../arrow/array_reader/fixed_size_list_array.rs | 2 ++ parquet/src/arrow/array_reader/list_array.rs | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/parquet/src/arrow/array_reader/fixed_len_byte_array.rs b/parquet/src/arrow/array_reader/fixed_len_byte_array.rs index 4515dcdc86..c021fe82e5 100644 --- a/parquet/src/arrow/array_reader/fixed_len_byte_array.rs +++ b/parquet/src/arrow/array_reader/fixed_len_byte_array.rs @@ -202,14 +202,21 @@ impl ArrayReader for FixedLenByteArrayReader { let len = self.record_reader.values_written(); let record_data = self.record_reader.consume_record_data(); - debug_assert_eq!( - record_data.buffer.len(), - len * self.byte_length, - "fixed-len byte array buffer size mismatch: {} bytes for {} elements of size {}", - record_data.buffer.len(), - len, - self.byte_length, - ); + let expected_len = len.checked_mul(self.byte_length).ok_or_else(|| { + general_err!( + "fixed-len byte array buffer size overflow: {} elements of size {}", + len, + self.byte_length + ) + })?; + if record_data.buffer.len() != expected_len { + return Err(general_err!( + "fixed-len byte array buffer size mismatch: {} bytes for {} elements of size {}", + record_data.buffer.len(), + len, + self.byte_length + )); + } let null_bit_buffer = self.record_reader.consume_compact_bitmap(); let array_data = ArrayDataBuilder::new(ArrowType::FixedSizeBinary(self.byte_length as i32)) @@ -217,6 +224,7 @@ impl ArrayReader for FixedLenByteArrayReader { .add_buffer(Buffer::from_vec(record_data.buffer)) .null_bit_buffer(null_bit_buffer); + // SAFETY: the buffer is exactly `len * byte_length` bytes and the null bitmap has `len` bits let binary = FixedSizeBinaryArray::from(unsafe { array_data.build_unchecked() }); // TODO: An improvement might be to do this conversion on read diff --git a/parquet/src/arrow/array_reader/fixed_size_list_array.rs b/parquet/src/arrow/array_reader/fixed_size_list_array.rs index e9483da500..df54495017 100644 --- a/parquet/src/arrow/array_reader/fixed_size_list_array.rs +++ b/parquet/src/arrow/array_reader/fixed_size_list_array.rs @@ -196,6 +196,8 @@ impl ArrayReader for FixedSizeListArrayReader { list_builder = list_builder.null_bit_buffer(Some(validity.into())); } + // SAFETY: the child holds `fixed_size` values for every list, including null lists, + // and the null bitmap has one bit per list let list_data = unsafe { list_builder.build_unchecked() }; Ok(Arc::new(FixedSizeListArray::from(list_data))) } diff --git a/parquet/src/arrow/array_reader/list_array.rs b/parquet/src/arrow/array_reader/list_array.rs index 87359ff342..8f2b83d7c2 100644 --- a/parquet/src/arrow/array_reader/list_array.rs +++ b/parquet/src/arrow/array_reader/list_array.rs @@ -239,6 +239,8 @@ impl<OffsetSize: OffsetSizeTrait> ArrayReader for ListArrayReader<OffsetSize> { data_builder = data_builder.null_bit_buffer(Some(builder.into())); } + // SAFETY: offsets are non-decreasing and end at `child_array.len()`, and the null + // bitmap has one bit per list let list_data = unsafe { data_builder.build_unchecked() }; Ok(Arc::new(GenericListArray::<OffsetSize>::from(list_data))) }
