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-12113-548c7bbd02042b1d2719dae9e39ae213d89135ce in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit f2d96279c5145c357e4faec5577a055f39693d74 Author: Chinmay Shrivastava <[email protected]> AuthorDate: Sun Nov 8 04:19:13 2026 +0000 Optimize byte-array dictionary gathering in arrow-row (#12113) # Which issue does this PR close? Closes #12053. # Rationale for this change The sparse-dictionary encoding path gathers one dictionary value per input row before encoding. The current gather performs a dynamic MutableArrayData extension for every key, which adds overhead for common string and binary dictionaries. # What changes are included in this PR? Add typed gather implementations for Utf8, LargeUtf8, Binary, and LargeBinary dictionary values. These build the offsets, value buffer, and validity bitmap directly. Other value types continue to use the generic MutableArrayData implementation. Offset growth is checked so that overflow continues to reach the existing fallback. The dictionary-gather thresholds are unchanged. # Are these changes tested? I’ve simplified test_gather_dictionary_byte_values to use StringArray, since the string and binary variants share the same gather implementation. The test still covers sliced keys, null keys, and keys that reference null dictionary values. All 110 arrow-row library tests pass. Workspace tests, workspace Clippy checks, formatting, and spelling checks also pass. Performance was measured using the existing prepared 8,192-row string-dictionary benchmark on an Intel Xeon E5-2673 v4, Linux x86_64, with Rust 1.99.1. This benchmark cycles through contiguous key chunks. Two paired Criterion runs used 50 samples, a one-second warmup, and a three-second measurement period. Execution order was reversed for the second pair. The table reports the average of the two runs’ mean estimates. | Dictionary values | Before | After | Change in mean time | | ---: | ---: | ---: | ---: | | 10,000 | 170.3 µs | 174.5 µs | +2.5% | | 32,768 | 310.8 µs | 242.0 µs | -22.1% | | 100,000 | 318.5 µs | 242.9 µs | -23.7% | | 1,000,000 | 309.4 µs | 244.9 µs | -20.8% | | 4,000,000 | 312.4 µs | 240.1 µs | -23.1% | The 10,000-value control does not enter the dictionary-gather path and averaged 2.5% slower in these measurements. Individual runs varied by several percent. The additional dictionary sizes were used through a separate local benchmark patch; they are not included in this implementation diff. # Are there any significant user-facing changes? No public API or intended result changes. AI assistance: AI tools assisted with implementation, tests, independent review, and validation. I reviewed the code, tests, and description before submission. --- arrow-row/src/lib.rs | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) diff --git a/arrow-row/src/lib.rs b/arrow-row/src/lib.rs index 4bb5f29719..fcce0ecbba 100644 --- a/arrow-row/src/lib.rs +++ b/arrow-row/src/lib.rs @@ -160,9 +160,12 @@ use std::slice::Windows; use std::sync::Arc; use arrow_array::cast::*; -use arrow_array::types::{ArrowDictionaryKeyType, ByteArrayType, ByteViewType}; +use arrow_array::types::{ + ArrowDictionaryKeyType, BinaryType, ByteArrayType, ByteViewType, LargeBinaryType, + LargeUtf8Type, Utf8Type, +}; use arrow_array::*; -use arrow_buffer::{ArrowNativeType, Buffer, OffsetBuffer, ScalarBuffer}; +use arrow_buffer::{ArrowNativeType, Buffer, NullBufferBuilder, OffsetBuffer, ScalarBuffer}; use arrow_data::transform::MutableArrayData; use arrow_schema::*; use variable::{decode_binary_view, decode_string_view}; @@ -2293,6 +2296,74 @@ pub fn encode_dictionary_values<K: ArrowDictionaryKeyType>( fn gather_dictionary_values<K: ArrowDictionaryKeyType>( array: &DictionaryArray<K>, +) -> Result<ArrayRef, ArrowError> { + let values = array.values(); + match values.data_type() { + DataType::Utf8 => gather_dictionary_byte_values::<K, Utf8Type>( + array, + values.as_any().downcast_ref::<StringArray>().unwrap(), + ), + DataType::LargeUtf8 => gather_dictionary_byte_values::<K, LargeUtf8Type>( + array, + values.as_any().downcast_ref::<LargeStringArray>().unwrap(), + ), + DataType::Binary => gather_dictionary_byte_values::<K, BinaryType>( + array, + values.as_any().downcast_ref::<BinaryArray>().unwrap(), + ), + DataType::LargeBinary => gather_dictionary_byte_values::<K, LargeBinaryType>( + array, + values.as_any().downcast_ref::<LargeBinaryArray>().unwrap(), + ), + _ => gather_dictionary_values_generic(array), + } +} + +/// Gathers byte values in dictionary-key order, preserving null keys and values. +/// Returns an error if the accumulated byte offsets overflow. +fn gather_dictionary_byte_values<K: ArrowDictionaryKeyType, T: ByteArrayType>( + array: &DictionaryArray<K>, + values: &GenericByteArray<T>, +) -> Result<ArrayRef, ArrowError> { + let mut offsets = Vec::with_capacity(array.len() + 1); + offsets.push(T::Offset::from_usize(0).unwrap()); + let mut bytes = Vec::new(); + let mut nulls = NullBufferBuilder::new(array.len()); + + for key in array.keys() { + match key { + Some(key) if values.is_valid(key.as_usize()) => { + let index = key.as_usize(); + let start = values.value_offsets()[index].as_usize(); + let end = values.value_offsets()[index + 1].as_usize(); + let value = &values.value_data()[start..end]; + let end = bytes + .len() + .checked_add(value.len()) + .ok_or(ArrowError::OffsetOverflowError(usize::MAX))?; + let offset = + T::Offset::from_usize(end).ok_or(ArrowError::OffsetOverflowError(end))?; + bytes.extend_from_slice(value); + offsets.push(offset); + nulls.append(true); + } + _ => { + offsets.push(*offsets.last().unwrap()); + nulls.append(false); + } + } + } + + let offsets = OffsetBuffer::new(ScalarBuffer::from(offsets)); + Ok(Arc::new(GenericByteArray::<T>::try_new( + offsets, + Buffer::from_vec(bytes), + nulls.finish(), + )?)) +} + +fn gather_dictionary_values_generic<K: ArrowDictionaryKeyType>( + array: &DictionaryArray<K>, ) -> Result<ArrayRef, ArrowError> { let data = array.values().to_data(); let mut mutable = MutableArrayData::new(vec![&data], false, array.len()); @@ -3387,6 +3458,60 @@ mod tests { dictionary_eq(&decoded[0], &expected); } + #[test] + fn test_gather_dictionary_byte_values() { + let keys = Int8Array::from(vec![Some(0), Some(3), None, Some(1), Some(3), Some(2)]); + let values = Arc::new(StringArray::from(vec![ + Some("unused"), + None, + Some("é"), + Some("selected"), + ])); + let dictionary = DictionaryArray::<Int8Type>::try_new(keys, values).unwrap(); + let sliced = dictionary.slice(1, 4); + let gathered = gather_dictionary_values(&sliced).unwrap(); + let expected = StringArray::from(vec![Some("selected"), None, None, Some("selected")]); + assert_eq!(gathered.as_ref(), &expected as &dyn Array); + } + + #[test] + fn test_gather_dictionary_generic_values() { + let keys = UInt8Array::from(vec![Some(2), None, Some(0), Some(1), Some(2)]); + let values = Arc::new(Int32Array::from(vec![Some(1), None, Some(3)])); + let dictionary = DictionaryArray::<UInt8Type>::try_new(keys, values).unwrap(); + let sliced = dictionary.slice(1, 4); + let gathered = gather_dictionary_values(&sliced).unwrap(); + let expected = Int32Array::from(vec![Some(1), Some(1), None, Some(3)]); + assert_eq!(gathered.as_ref(), &expected as &dyn Array); + } + + #[test] + fn test_sparse_binary_dictionary_with_null_values() { + let values = BinaryArray::from_iter((0..DICTIONARY_GATHER_MIN_VALUES).map(|index| { + if index == 0 { + None + } else { + Some(&b"value"[..]) + } + })); + let keys = UInt64Array::from(vec![Some(0), Some(32_767), None]); + let dictionary = DictionaryArray::<UInt64Type>::try_new(keys, Arc::new(values)).unwrap(); + let input = Arc::new(dictionary) as ArrayRef; + let expected = + Arc::new(BinaryArray::from(vec![None, Some(&b"value"[..]), None])) as ArrayRef; + let converter = RowConverter::new(vec![SortField::new(input.data_type().clone())]).unwrap(); + let actual_rows = converter.convert_columns(&[input]).unwrap(); + let expected_converter = RowConverter::new(vec![SortField::new(DataType::Binary)]).unwrap(); + let expected_rows = expected_converter + .convert_columns(&[Arc::clone(&expected)]) + .unwrap(); + for (actual, expected) in actual_rows.iter().zip(&expected_rows) { + assert_eq!(actual, expected); + } + let decoded = converter.convert_rows(&actual_rows).unwrap(); + assert_eq!(decoded[0].as_ref(), expected.as_ref()); + } + #[test] fn test_sparse_dictionary_gather_overflow_falls_back() { let inner = RunArray::<Int16Type>::try_new(
