This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new e02da695a5 perf(coalesce): add Boolean indexed copy (#12336)
e02da695a5 is described below

commit e02da695a51d1a941fd99beb91a0afbb00d49cdc
Author: RIchard Baah <[email protected]>
AuthorDate: Sun Nov 22 05:12:41 2026 +0000

    perf(coalesce): add Boolean indexed copy (#12336)
    
    # Which issue does this PR close?
    
    - Closes #11567
    
    # Rationale for this change
    
    `BatchCoalescer::push_batch_with_indices` currently materializes an
    intermediate `BooleanArray` through `take` before copying it into the
    coalescer. This adds work for sparse indexed selections.
    
    # What changes are included in this PR?
    
    Add a Boolean-specific indexed-copy path that gathers selected value and
    validity bits directly into the in-progress buffers. The existing bounds
    checking and dense-selection fallback are preserved.
    
    # Are these changes tested?
    
    Yes. Added coverage for Boolean indexed copy, including nulls, slices,
    repeated and out-of-order indices, and empty indices.
    
    | Nulls | Selectivity | Base | PR | Speedup |
    |---:|---:|---:|---:|---:|
    | 0 | 0.001 | 9.7 ms | 5.6 ms | 1.74x |
    | 0 | 0.01 | 1248.2 µs | 786.6 µs | 1.59x |
    | 0 | 0.1 | 370.4 µs | 295.7 µs | 1.25x |
    | 0 | 0.8 | 247.1 µs | 246.6 µs | 1.00x |
    | 0.1 | 0.001 | 15.6 ms | 9.8 ms | 1.59x |
    | 0.1 | 0.01 | 2.0 ms | 1406.5 µs | 1.45x |
    | 0.1 | 0.1 | 627.4 µs | 545.9 µs | 1.15x |
    | 0.1 | 0.8 | 447.7 µs | 449.2 µs | 1.00x |
    
    [Full benchmark
    
report](https://github.com/apache/arrow-rs/pull/12336#issuecomment-7015720417)
    
    # Are there any significant user-facing changes?
    
    No.
---
 arrow-select/src/coalesce/boolean.rs | 80 +++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 2 deletions(-)

diff --git a/arrow-select/src/coalesce/boolean.rs 
b/arrow-select/src/coalesce/boolean.rs
index 125ff496e5..87ce4f6ac4 100644
--- a/arrow-select/src/coalesce/boolean.rs
+++ b/arrow-select/src/coalesce/boolean.rs
@@ -2,10 +2,12 @@
 // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
 // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
 
-use super::InProgressArray;
+use super::{InProgressArray, append_gathered_nulls};
 use crate::filter::{FilterPredicate, Packer};
+use crate::take::take_bits;
 use arrow_array::cast::AsArray;
-use arrow_array::{Array, ArrayRef, BooleanArray};
+use arrow_array::types::UInt32Type;
+use arrow_array::{Array, ArrayRef, BooleanArray, UInt32Array};
 use arrow_buffer::bit_chunk_iterator::BitChunks;
 use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder, NullBufferBuilder};
 use arrow_schema::ArrowError;
@@ -203,6 +205,28 @@ impl InProgressArray for InProgressBooleanArray {
         Ok(())
     }
 
+    unsafe fn copy_rows_by_indices(
+        &mut self,
+        source: &ArrayRef,
+        indices: &UInt32Array,
+    ) -> Result<(), ArrowError> {
+        if indices.is_empty() {
+            return Ok(());
+        }
+        let source = source.as_boolean();
+        let values = take_bits::<UInt32Type, false>(source.values(), indices);
+        self.values().append_buffer(&values);
+
+        if let Some(source_nulls) = source.nulls() {
+            // SAFETY: the caller validated every index against `source.len()`,
+            // which equals the length of its null buffer.
+            unsafe { append_gathered_nulls(&mut self.nulls, source_nulls, 
indices) };
+        } else {
+            self.nulls.append_n_non_nulls(indices.len());
+        }
+        Ok(())
+    }
+
     fn finish(&mut self) -> Result<ArrayRef, ArrowError> {
         let values = self
             .values
@@ -279,6 +303,58 @@ mod tests {
         assert_eq!(in_progress.finish().unwrap().len(), 0);
     }
 
+    #[test]
+    fn indexed_copy_matches_take_for_sliced_values_and_validity() {
+        let values: BooleanBuffer = [true, false, true, true, false, false, 
true, false]
+            .into_iter()
+            .collect();
+        let validity: BooleanBuffer = [
+            true, true, false, true, true, false, true, true, true, false,
+        ]
+        .into_iter()
+        .collect();
+        let source = BooleanArray::new(
+            values.slice(1, 6),
+            Some(NullBuffer::new(validity.slice(3, 6))),
+        );
+        assert_ne!(source.values().offset(), source.nulls().unwrap().offset());
+        let source = Arc::new(source) as ArrayRef;
+        let indices = UInt32Array::from(vec![5, 0, 2, 2, 1]);
+        let expected = crate::take::take(source.as_ref(), &indices, 
None).unwrap();
+
+        let mut in_progress = InProgressBooleanArray::new(8);
+        // SAFETY: the indices are non-null and in bounds for `source`.
+        unsafe { in_progress.copy_rows_by_indices(&source, &indices).unwrap() 
};
+
+        let actual = in_progress.finish().unwrap();
+        assert_eq!(actual.as_boolean(), expected.as_boolean());
+    }
+
+    #[test]
+    fn indexed_copy_preserves_lazy_validity() {
+        let values: BooleanBuffer = [true, false, true, 
false].into_iter().collect();
+        let without_validity = BooleanArray::new(values.clone(), None);
+        let all_valid = BooleanArray::new(values, 
Some(NullBuffer::new_valid(4)));
+        let indices = UInt32Array::from(vec![3, 1, 1, 0]);
+
+        for source in [without_validity, all_valid] {
+            let source = Arc::new(source) as ArrayRef;
+            let mut in_progress = InProgressBooleanArray::new(8);
+            // SAFETY: the indices are non-null and in bounds for `source`.
+            unsafe {
+                in_progress.copy_rows_by_indices(&source, &indices).unwrap();
+                in_progress
+                    .copy_rows_by_indices(&source, 
&UInt32Array::from(Vec::<u32>::new()))
+                    .unwrap();
+            }
+
+            let actual = in_progress.finish().unwrap();
+            let expected = crate::take::take(source.as_ref(), &indices, 
None).unwrap();
+            assert_eq!(actual.as_boolean(), expected.as_boolean());
+            assert!(actual.nulls().is_none());
+        }
+    }
+
     const FILTER_LEN: usize = 269;
     const SOURCE_OFFSET: usize = 3;
 

Reply via email to