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-12306-d64e6f0424d4ee546fdcf7b1fac563da65576c5d
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 857b0043a5d823b5080df0c366715121a4170c6b
Author: RIchard Baah <[email protected]>
AuthorDate: Thu Nov 19 20:20:31 2026 +0000

    Optimize indexed copying for dictionary arrays (#12306)
    
    # Which issue does this PR close?
    
    - Closes #11940
    
    # Rationale for this change
    
    Sparse indexed copies of byte dictionaries currently materialize a new
    dictionary array before coalescing it. This adds avoidable work and
    prevents the coalescer from reusing the dictionary values array
    directly.
    
    # What changes are included in this PR?
    
    Implement `copy_rows_by_indices` for dictionaries with `Utf8`,
    `LargeUtf8`, `Binary`, and `LargeBinary` values. Sparse selections
    gather keys directly and retain the source values arrays for remapping
    when the batch is finished; denser selections continue to use the
    existing `take` fallback.
    
    # Are these changes tested?
    
    Yes. Unit tests cover sparse indexed copies with sliced and null keys,
    repeated and out-of-order indices, the dense fallback, values-array
    reuse, and remapping across multiple dictionaries with different value
    orderings.
    
    The final implementation was benchmarked across shared and changing
    dictionary values, four selectivities, and nullable/non-null inputs:
    
    | Architecture | Result |
    | --- | --- |
    |
    
[AMD64](https://github.com/apache/arrow-rs/pull/12306#issuecomment-6972366413)
    | All 16 cases improved or tied, with sparse cases up to 2.28x faster |
    |
    
[ARM64](https://github.com/apache/arrow-rs/pull/12306#issuecomment-6972346735)
    | Sparse cases improved by up to 2.57x; changing-values dense cases were
    approximately 1-2% slower |
    
    # Are there any significant user-facing changes?
    
    No public API changes.
---
 arrow-select/src/coalesce/dictionary.rs | 156 +++++++++++++++++++++++++++++++-
 1 file changed, 155 insertions(+), 1 deletion(-)

diff --git a/arrow-select/src/coalesce/dictionary.rs 
b/arrow-select/src/coalesce/dictionary.rs
index ec8e8cc12a..ad62a293eb 100644
--- a/arrow-select/src/coalesce/dictionary.rs
+++ b/arrow-select/src/coalesce/dictionary.rs
@@ -4,11 +4,13 @@
 
 use super::{InProgressArray, generic::GenericInProgressArray};
 use crate::dictionary::{KeyMapping, child_values_ptr_eq, 
merge_dictionary_values_with_masks};
+use crate::take::{TakeOptions, take};
 use ahash::RandomState;
 use arrow_array::cast::AsArray;
 use arrow_array::types::ArrowDictionaryKeyType;
 use arrow_array::{
-    Array, ArrayRef, DictionaryArray, PrimitiveArray, downcast_integer, 
new_empty_array,
+    Array, ArrayRef, DictionaryArray, PrimitiveArray, UInt32Array, 
downcast_integer,
+    new_empty_array,
 };
 use arrow_buffer::{
     ArrowNativeType, BooleanBufferBuilder, NullBuffer, NullBufferBuilder, 
ScalarBuffer,
@@ -18,6 +20,8 @@ use std::collections::HashMap;
 use std::mem::size_of;
 use std::sync::Arc;
 
+const INDEXED_COPY_MAX_SELECTIVITY_DENOMINATOR: usize = 16;
+
 struct KeyChunk<K: ArrowDictionaryKeyType> {
     /// Zero-copy slice of source-local keys for the retained rows.
     keys: ScalarBuffer<K::Native>,
@@ -205,6 +209,66 @@ impl<K: ArrowDictionaryKeyType> InProgressArray for 
InProgressDictionaryArray<K>
         Ok(())
     }
 
+    unsafe fn copy_rows_by_indices(
+        &mut self,
+        source: &ArrayRef,
+        indices: &UInt32Array,
+    ) -> Result<(), ArrowError> {
+        if indices.is_empty() {
+            return Ok(());
+        }
+        if indices.len() > source.len() / 
INDEXED_COPY_MAX_SELECTIVITY_DENOMINATOR {
+            let taken = take(
+                source.as_ref(),
+                indices,
+                Some(TakeOptions {
+                    check_bounds: false,
+                }),
+            )?;
+            self.set_source(Some(taken));
+            let result = self.copy_rows(0, indices.len());
+            self.set_source(None);
+            return result;
+        }
+        let dictionary = source.as_dictionary::<K>();
+        let values = dictionary.values();
+        let keys = dictionary.keys();
+        let key_values = keys.values();
+        debug_assert!(
+            indices
+                .values()
+                .iter()
+                .all(|&idx| (idx as usize) < key_values.len()),
+            "copy_rows_by_indices requires caller-validated in-bounds indices",
+        );
+        let mut gathered = Vec::with_capacity(indices.len());
+        // SAFETY: the caller guarantees every index is less than 
`source.len()`,
+        // which equals `key_values.len()`.
+        unsafe {
+            for &idx in indices.values() {
+                gathered.push(*key_values.get_unchecked(idx as usize));
+            }
+        }
+        let nulls = keys.nulls().and_then(|nulls| {
+            if nulls.null_count() == 0 {
+                return None;
+            }
+            let mut gathered = NullBufferBuilder::new(indices.len());
+            // SAFETY: the caller validated every index against `keys.len()`,
+            // which equals the length of its null buffer.
+            unsafe { super::append_gathered_nulls(&mut gathered, nulls, 
indices) };
+            gathered.finish()
+        });
+        let source = self.source_id(Arc::clone(values));
+        self.chunks.push(KeyChunk {
+            keys: ScalarBuffer::from(gathered),
+            nulls,
+            source,
+        });
+        self.len += indices.len();
+        Ok(())
+    }
+
     fn finish(&mut self) -> Result<ArrayRef, ArrowError> {
         let sources = std::mem::take(&mut self.sources);
         let chunks = std::mem::take(&mut self.chunks);
@@ -309,6 +373,96 @@ mod tests {
         assert!(Arc::ptr_eq(output.values(), &values));
     }
 
+    #[test]
+    fn indexed_copy_reuses_values_and_preserves_sliced_null_keys() {
+        let values = Arc::new(StringArray::from(vec!["a", "b", "c"])) as 
ArrayRef;
+        let keys =
+            Int32Array::from_iter((0..66).map(|idx| (idx != 1).then_some(idx % 
3))).slice(1, 64);
+        let source =
+            Arc::new(DictionaryArray::<Int32Type>::new(keys, 
Arc::clone(&values))) as ArrayRef;
+        let mut in_progress = 
InProgressDictionaryArray::<Int32Type>::new(&DataType::Utf8);
+        // SAFETY: all indices are non-null and in bounds.
+        unsafe {
+            in_progress
+                .copy_rows_by_indices(&source, &UInt32Array::from(vec![9, 1, 
0, 1]))
+                .unwrap()
+        };
+
+        let output = in_progress.finish().unwrap();
+        let output = output.as_dictionary::<Int32Type>();
+        assert!(Arc::ptr_eq(output.values(), &values));
+        assert_eq!(
+            output.keys(),
+            &Int32Array::from(vec![Some(1), Some(2), None, Some(2)])
+        );
+    }
+
+    #[test]
+    fn indexed_copy_remaps_sparse_values_across_sources() {
+        let first_values = Arc::new(StringArray::from(vec!["a", "b", "c"])) as 
ArrayRef;
+        let second_values = Arc::new(StringArray::from(vec!["c", "a", "d"])) 
as ArrayRef;
+        let keys = (0..64)
+            .map(|idx| (idx != 3).then_some(idx % 3))
+            .collect::<Vec<_>>();
+        let first = batch(&first_values, keys.clone());
+        let second = batch(&second_values, keys);
+        let mut coalescer = BatchCoalescer::new(first.schema(), 8);
+
+        // Four out of 64 rows is the maximum selection that uses the direct 
sparse path.
+        coalescer
+            .push_batch_with_indices(first, &UInt32Array::from(vec![3, 1, 0, 
1]))
+            .unwrap();
+        coalescer
+            .push_batch_with_indices(second, &UInt32Array::from(vec![2, 0, 3, 
0]))
+            .unwrap();
+
+        let output = coalescer.next_completed_batch().unwrap();
+        let output = output.column(0).as_dictionary::<Int32Type>();
+        let values = output.values().as_string::<i32>();
+        let decoded = output
+            .keys_iter()
+            .map(|key| key.map(|key| values.value(key)))
+            .collect::<Vec<_>>();
+        assert_eq!(
+            decoded,
+            vec![
+                None,
+                Some("b"),
+                Some("a"),
+                Some("b"),
+                Some("d"),
+                Some("c"),
+                None,
+                Some("c"),
+            ]
+        );
+        assert_eq!(values.len(), 4);
+    }
+
+    #[test]
+    fn indexed_copy_dense_fallback_preserves_null_keys() {
+        let values = Arc::new(StringArray::from(vec!["a", "b", "c"])) as 
ArrayRef;
+        let source = Arc::new(DictionaryArray::<Int32Type>::new(
+            Int32Array::from(vec![Some(0), None, Some(2), Some(1)]),
+            Arc::clone(&values),
+        )) as ArrayRef;
+        let mut in_progress = 
InProgressDictionaryArray::<Int32Type>::new(&DataType::Utf8);
+        // SAFETY: all indices are non-null and in bounds.
+        unsafe {
+            in_progress
+                .copy_rows_by_indices(&source, &UInt32Array::from(vec![3, 1, 
0]))
+                .unwrap()
+        };
+
+        let output = in_progress.finish().unwrap();
+        let output = output.as_dictionary::<Int32Type>();
+        assert!(Arc::ptr_eq(output.values(), &values));
+        assert_eq!(
+            output.keys(),
+            &Int32Array::from(vec![Some(1), None, Some(0)])
+        );
+    }
+
     #[test]
     fn reuses_values_with_shared_buffers() {
         let values = StringArray::from_iter_values((0..128).map(|i| 
format!("value-{i}")));

Reply via email to