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-11694-1a52c3440523103b9457b991e0b6342c9d8a0950 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 49219f867f77422525627a07b84cd7a87207dcbe Author: Aleksandar Zecevic <[email protected]> AuthorDate: Mon Oct 19 01:03:01 2026 +0000 Write empty validity buffers for arrays without nulls in IPC writer (#11694) # Which issue does this PR close? N/A # Rationale for this change The IPC writer writes a validity bitmap for every array that has a validity buffer, also when the array has no nulls. If the array has no null buffer, `write_array_data` creates an all-ones bitmap of `ceil(len / 8)` bytes for it. Readers do not use it: the arrow-rs reader, like the C++ reader, ignores the validity buffer when `null_count` is 0. The format allows a 0-length buffer instead. [`FieldNode.null_count` in `Message.fbs`](https://github.com/apache/arrow/blob/9f0341314ca96eb58081d4132c77b460d880b2b0/format/Message.fbs#L39-L41): > Fields with null_count == 0 may choose not to write their physical validity bitmap out as a materialized buffer, instead setting the length of the bitmap buffer to 0. The [C++](https://github.com/apache/arrow/blob/9f0341314ca96eb58081d4132c77b460d880b2b0/cpp/src/arrow/ipc/writer.cc#L178-L187), [Go](https://github.com/apache/arrow-go/blob/a6e44ad7a6aaa4c02a7d277032af8eebd5cc2508/arrow/ipc/writer.go#L634-L638) and [JS](https://github.com/apache/arrow-js/blob/43c9547baf3766e79eb44d3fedcd0787f899d956/src/visitor/vectorassembler.ts#L95-L98) writers already do this. So arrow-rs already reads such buffers, in the integration tests and in the `arrow-testing` gold files, which C++ wrote. # What changes are included in this PR? - `write_array_data` writes a 0-length validity buffer when `null_count` is 0. This also covers sliced arrays whose null buffer has no nulls in the slice. Arrays with nulls are written as before. - `test_decimal128_alignment8_is_unaligned` relied on the all-ones bitmap to move the decimal values to an 8-byte offset. It now uses a nullable column with a null, so it gets the expected misalignment error with both the old and the new writer. - The exact overage limits in the arrow-flight `verify_encoded_split` tests go down, for example from 120 to 88. # Are these changes tested? Yes. `test_write_empty_validity_buffer_without_nulls` encodes a batch with three columns: an array without a null buffer, a slice without nulls that keeps its null buffer, and an array with a null. It checks the validity buffer lengths in the encoded message (`[0, 0, 1]`) and the round trip. Without the change, the first two columns get 1-byte bitmaps and the test fails. # Are there any significant user-facing changes? The IPC output for arrays without nulls is smaller. The spec allows this, and the C++, Java, Go, JS, C# and nanoarrow readers accept it. Code that compares IPC output byte for byte, or expects exact sizes, will see a difference. There are no API changes. --- arrow-flight/src/encode.rs | 12 +++++------ arrow-ipc/src/writer.rs | 51 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/arrow-flight/src/encode.rs b/arrow-flight/src/encode.rs index 5ae84850be..441e427c4f 100644 --- a/arrow-flight/src/encode.rs +++ b/arrow-flight/src/encode.rs @@ -2044,7 +2044,7 @@ mod tests { ]) .unwrap(); - verify_encoded_split(batch, 120).await; + verify_encoded_split(batch, 88).await; } #[tokio::test] @@ -2055,7 +2055,7 @@ mod tests { // overage is much higher than ideal // https://github.com/apache/arrow-rs/issues/3478 - verify_encoded_split(batch, 4312).await; + verify_encoded_split(batch, 4304).await; } #[tokio::test] @@ -2091,7 +2091,7 @@ mod tests { // 5k over limit (which is 2x larger than limit of 5k) // overage is much higher than ideal // https://github.com/apache/arrow-rs/issues/3478 - verify_encoded_split(batch, 5808).await; + verify_encoded_split(batch, 5784).await; } #[tokio::test] @@ -2121,7 +2121,7 @@ mod tests { // overage is much higher than ideal // https://github.com/apache/arrow-rs/issues/3478 - verify_encoded_split(batch, 3336).await; + verify_encoded_split(batch, 3328).await; } #[tokio::test] @@ -2135,7 +2135,7 @@ mod tests { // overage is much higher than ideal // https://github.com/apache/arrow-rs/issues/3478 - verify_encoded_split(batch, 5288).await; + verify_encoded_split(batch, 5280).await; } #[tokio::test] @@ -2160,7 +2160,7 @@ mod tests { // overage is much higher than ideal // https://github.com/apache/arrow-rs/issues/3478 - verify_encoded_split(batch, 4136).await; + verify_encoded_split(batch, 4112).await; } /// Return size, in memory of flight data diff --git a/arrow-ipc/src/writer.rs b/arrow-ipc/src/writer.rs index 541f8e8f2e..d2ee337186 100644 --- a/arrow-ipc/src/writer.rs +++ b/arrow-ipc/src/writer.rs @@ -35,7 +35,6 @@ use flatbuffers::FlatBufferBuilder; use arrow_array::cast::*; use arrow_array::types::{Int16Type, Int32Type, Int64Type, RunEndIndexType}; use arrow_array::*; -use arrow_buffer::bit_util; use arrow_buffer::{ArrowNativeType, Buffer, MutableBuffer, ToByteSlice}; use arrow_data::{ArrayData, ArrayDataBuilder, BufferSpec, layout}; use arrow_schema::*; @@ -2443,16 +2442,11 @@ fn write_array_data( .push(crate::FieldNode::new(num_rows as i64, num_rows as i64)); } if has_validity_bitmap(array_data.data_type(), write_options) { - // write null buffer if exists + // Fields with null_count == 0 may write a 0-length validity buffer + // instead of a materialized bitmap (see FieldNode in Message.fbs) let null_buffer = match array_data.nulls() { - None => { - // create a buffer and fill it with valid bits - let num_bytes = bit_util::ceil(num_rows, 8); - let buffer = MutableBuffer::new(num_bytes); - let buffer = buffer.with_bitset(num_bytes, true); - buffer.into() - } - Some(buffer) => buffer.inner().sliced(), + Some(buffer) if null_count > 0 => buffer.inner().sliced(), + _ => MutableBuffer::new(0).into(), }; offset = encode_sink_buffer( @@ -3627,6 +3621,37 @@ mod tests { assert_eq!(record_batch_slice, deserialized_batch); } + #[test] + fn test_write_empty_validity_buffer_without_nulls() { + let batch = RecordBatch::try_from_iter([ + ("a", Arc::new(Int32Array::from(vec![1, 2])) as _), + // Keeps a null buffer, but the slice has no nulls + ( + "b", + Arc::new(Int32Array::from(vec![None, Some(2), Some(3)]).slice(1, 2)) as _, + ), + ("c", Arc::new(Int32Array::from(vec![Some(1), None])) as _), + ]) + .unwrap(); + assert_eq!(batch.column(1).nulls().map(|n| n.null_count()), Some(0)); + + let (_, encoded) = IpcDataGenerator::default() + .encode( + &batch, + &mut DictionaryTracker::new(false), + &Default::default(), + &mut Default::default(), + ) + .unwrap(); + let message = crate::root_as_message(&encoded.ipc_message).unwrap(); + let buffers = message.header_as_record_batch().unwrap().buffers().unwrap(); + // Each column writes a validity buffer, then a values buffer + let validity_lengths: Vec<_> = buffers.iter().step_by(2).map(|b| b.length()).collect(); + assert_eq!(validity_lengths, [0, 0, 1]); + + assert_eq!(deserialize_stream(serialize_stream(&batch)), batch); + } + #[test] fn truncate_ipc_dictionary_array() { fn create_batch() -> RecordBatch { @@ -4865,13 +4890,15 @@ mod tests { const IPC_ALIGNMENT: usize = 8; let num_cols = 2; - let num_rows = 1; let mut fields = Vec::new(); let mut arrays = Vec::new(); for i in 0..num_cols { let field = Field::new(format!("col_{i}"), DataType::Decimal128(38, 10), true); - let array = Decimal128Array::from(vec![num_cols as i128; num_rows]); + // The null makes the writer emit a 1-byte validity buffer. With IPC_ALIGNMENT = 8, + // the writer pads that buffer to 8 bytes, so the decimal values that follow start + // at offset 8 of the body, which is not a multiple of 16. + let array = Decimal128Array::from(vec![Some(num_cols as i128), None]); fields.push(field); arrays.push(Arc::new(array) as Arc<dyn Array>); }
