From c21a4f882250c5bc544f09e673da529a03e530af Mon Sep 17 00:00:00 2001 From: Bharadwaj Pendyala Date: Mon, 28 Sep 2026 10:23:30 -0500 Subject: [PATCH 1/3] perf(filter): skip building the output null buffer when no null row is selected --- arrow-select/src/filter.rs | 90 +++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/arrow-select/src/filter.rs b/arrow-select/src/filter.rs index ed9b0da63748..dc9306404f06 100644 --- a/arrow-select/src/filter.rs +++ b/arrow-select/src/filter.rs @@ -504,6 +504,24 @@ impl FilterPredicate { } } + /// Returns true if any row selected by this predicate is null in `nulls` + fn selects_null(&self, nulls: &NullBuffer) -> bool { + let len = self.filter.len(); + assert!(nulls.len() >= len); + // Below one selected row per word, probing the precomputed indices + // is cheaper than scanning the mask, as in `filter_bits` + if let IterationStrategy::Indices(indices) = &self.strategy + && self.count <= len / 64 + { + return indices.iter().any(|&idx| nulls.is_null(idx)); + } + let keep = self.filter.values().bit_chunks(); + let valid = BitChunks::new(nulls.buffer(), nulls.offset(), len); + keep.iter_padded() + .zip(valid.iter_padded()) + .any(|(keep, valid)| keep & !valid != 0) + } + /// Filters the given `nulls` buffer using this predicate. /// /// Returns `None` when there is nothing to track in the output, either @@ -512,7 +530,7 @@ impl FilterPredicate { /// [`NullBuffer`] with its precomputed null count. pub fn filter_nulls(&self, nulls: Option<&NullBuffer>) -> Option { let nulls = nulls?; - if nulls.null_count() == 0 { + if nulls.null_count() == 0 || !self.selects_null(nulls) { return None; } @@ -1559,6 +1577,67 @@ mod tests { assert_eq!(9, d.value(1)); } + // Sliced at offset 1 so the null bits sit off a byte boundary, with a + // partial trailing word. Rows 0 and 129 are null. + fn nullable_slice(null_every: i32) -> Int32Array { + let values: Int32Array = (0..131) + .map(|i| (i % null_every != 1).then_some(i)) + .collect(); + values.slice(1, 130) + } + + fn filter_masks(keep: impl Fn(usize) -> bool) -> [BooleanArray; 2] { + let mask: BooleanArray = (0..130).map(|i| Some(keep(i))).collect(); + let sliced: BooleanArray = (0..131).map(|i| Some(i > 0 && keep(i - 1))).collect(); + [mask, sliced.slice(1, 130)] + } + + #[test] + fn test_filter_nulls_selecting_only_valid_rows() { + // Every 43rd row null puts the dense filter above the slices threshold + for null_every in [3, 43] { + let values = nullable_slice(null_every); + let nulls = values.nulls().unwrap(); + let masks = filter_masks(|i| nulls.is_valid(i)) + .into_iter() + .chain(filter_masks(|i| i == 128)); + for mask in masks { + for predicate in [ + FilterBuilder::new(&mask).build(), + FilterBuilder::new(&mask).optimize().build(), + ] { + assert!(!predicate.selects_null(nulls)); + let filtered = predicate.filter(&values).unwrap(); + assert_eq!(filtered.nulls(), None); + assert_eq!(filtered.len(), mask.true_count()); + } + } + } + } + + #[test] + fn test_filter_nulls_selecting_one_null() { + for null_every in [3, 43] { + let values = nullable_slice(null_every); + let nulls = values.nulls().unwrap(); + for null_row in [0, 129] { + let masks = filter_masks(|i| nulls.is_valid(i) || i == null_row) + .into_iter() + .chain(filter_masks(|i| i == 128 || i == null_row)); + for mask in masks { + for predicate in [ + FilterBuilder::new(&mask).build(), + FilterBuilder::new(&mask).optimize().build(), + ] { + assert!(predicate.selects_null(nulls)); + let filtered_nulls = predicate.filter_nulls(Some(nulls)).unwrap(); + assert_eq!(filtered_nulls.null_count(), 1); + } + } + } + } + } + #[test] fn test_filter_run_end_encoding_array() { let run_ends = Int64Array::from(vec![2, 3, 8]); @@ -2616,4 +2695,13 @@ mod tests { let filter = FilterBuilder::new(&predicate).build(); filter_native(&values, &filter); } + + #[test] + #[should_panic(expected = "nulls.len() >= len")] + fn test_filter_nulls_shorter_than_filter() { + let nulls = NullBuffer::from_iter((0..8).map(|i| i != 1)); + let predicate = BooleanArray::from_iter((0..9).map(|i| Some(i == 0))); + let filter = FilterBuilder::new(&predicate).optimize().build(); + filter.filter_nulls(Some(&nulls)); + } } From 27f831250a387c834f959d9bd420b96e31ffdc27 Mon Sep 17 00:00:00 2001 From: Bharadwaj Pendyala Date: Sun, 11 Oct 2026 13:12:34 -0500 Subject: [PATCH 2/3] test(filter): name the null rows and mask cases in the filter_nulls tests --- arrow-select/src/filter.rs | 110 +++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/arrow-select/src/filter.rs b/arrow-select/src/filter.rs index f172fab9f6be..7f058e107b97 100644 --- a/arrow-select/src/filter.rs +++ b/arrow-select/src/filter.rs @@ -1711,62 +1711,74 @@ mod tests { assert_eq!(9, d.value(1)); } - // Sliced at offset 1 so the null bits sit off a byte boundary, with a - // partial trailing word. Rows 0 and 129 are null. - fn nullable_slice(null_every: i32) -> Int32Array { - let values: Int32Array = (0..131) - .map(|i| (i % null_every != 1).then_some(i)) - .collect(); - values.slice(1, 130) - } - - fn filter_masks(keep: impl Fn(usize) -> bool) -> [BooleanArray; 2] { - let mask: BooleanArray = (0..130).map(|i| Some(keep(i))).collect(); - let sliced: BooleanArray = (0..131).map(|i| Some(i > 0 && keep(i - 1))).collect(); - [mask, sliced.slice(1, 130)] + // Three full 64-bit words and an 8-bit partial trailing word + const LEN: usize = 200; + // The first row of the first two words, and the last row of the partial one + const NULL_ROWS: [usize; 3] = [0, 64, 199]; + // A valid row in the partial trailing word + const SPARSE_ROW: usize = 192; + + // Sliced at offset 1 so the null bits start off a byte boundary + fn nullable_values() -> Int32Array { + let mut values = vec![Some(0); LEN + 1]; + for row in NULL_ROWS { + values[row + 1] = None; + } + Int32Array::from(values).slice(1, LEN) + } + + // One case per path through `selects_null` and the filter strategies + fn keep_valid_rows(nulls: &NullBuffer) -> [Vec; 3] { + [ + // Above FILTER_SLICES_SELECTIVITY_THRESHOLD, so a slices strategy + (0..LEN).map(|i| nulls.is_valid(i)).collect(), + // Below it, so an index strategy that scans the mask + (0..LEN).map(|i| i % 2 == 1 && nulls.is_valid(i)).collect(), + // Under one row per word, so the optimized predicate probes its indices + (0..LEN).map(|i| i == SPARSE_ROW).collect(), + ] + } + + fn predicates(keep: &[bool]) -> Vec { + let mask = BooleanArray::from(keep.to_vec()); + let padded: BooleanArray = std::iter::once(false).chain(keep.iter().copied()).collect(); + let offset_mask = padded.slice(1, keep.len()); + [mask, offset_mask] + .iter() + .flat_map(|mask| { + [ + FilterBuilder::new(mask).build(), + FilterBuilder::new(mask).optimize().build(), + ] + }) + .collect() } #[test] fn test_filter_nulls_selecting_only_valid_rows() { - // Every 43rd row null puts the dense filter above the slices threshold - for null_every in [3, 43] { - let values = nullable_slice(null_every); - let nulls = values.nulls().unwrap(); - let masks = filter_masks(|i| nulls.is_valid(i)) - .into_iter() - .chain(filter_masks(|i| i == 128)); - for mask in masks { - for predicate in [ - FilterBuilder::new(&mask).build(), - FilterBuilder::new(&mask).optimize().build(), - ] { - assert!(!predicate.selects_null(nulls)); - let filtered = predicate.filter(&values).unwrap(); - assert_eq!(filtered.nulls(), None); - assert_eq!(filtered.len(), mask.true_count()); - } + let values = nullable_values(); + let nulls = values.nulls().unwrap(); + for keep in keep_valid_rows(nulls) { + for predicate in predicates(&keep) { + assert!(!predicate.selects_null(nulls)); + let filtered = predicate.filter(&values).unwrap(); + assert_eq!(filtered.nulls(), None); + assert_eq!(filtered.len(), keep.iter().filter(|k| **k).count()); } } } #[test] fn test_filter_nulls_selecting_one_null() { - for null_every in [3, 43] { - let values = nullable_slice(null_every); - let nulls = values.nulls().unwrap(); - for null_row in [0, 129] { - let masks = filter_masks(|i| nulls.is_valid(i) || i == null_row) - .into_iter() - .chain(filter_masks(|i| i == 128 || i == null_row)); - for mask in masks { - for predicate in [ - FilterBuilder::new(&mask).build(), - FilterBuilder::new(&mask).optimize().build(), - ] { - assert!(predicate.selects_null(nulls)); - let filtered_nulls = predicate.filter_nulls(Some(nulls)).unwrap(); - assert_eq!(filtered_nulls.null_count(), 1); - } + let values = nullable_values(); + let nulls = values.nulls().unwrap(); + for null_row in NULL_ROWS { + for mut keep in keep_valid_rows(nulls) { + keep[null_row] = true; + for predicate in predicates(&keep) { + assert!(predicate.selects_null(nulls)); + let filtered_nulls = predicate.filter_nulls(Some(nulls)).unwrap(); + assert_eq!(filtered_nulls.null_count(), 1); } } } @@ -2833,8 +2845,10 @@ mod tests { #[test] #[should_panic(expected = "nulls.len() >= len")] fn test_filter_nulls_shorter_than_filter() { - let nulls = NullBuffer::from_iter((0..8).map(|i| i != 1)); - let predicate = BooleanArray::from_iter((0..9).map(|i| Some(i == 0))); + let nulls = NullBuffer::from(vec![true, false, true, true, true, true, true, true]); + let predicate = BooleanArray::from(vec![ + true, false, false, false, false, false, false, false, false, + ]); let filter = FilterBuilder::new(&predicate).optimize().build(); filter.filter_nulls(Some(&nulls)); } From 287ff06afb4ee6c56bc353a79587f6c2b3e53a4a Mon Sep 17 00:00:00 2001 From: Bharadwaj Pendyala Date: Sun, 18 Oct 2026 20:50:45 -0500 Subject: [PATCH 3/3] perf(filter): keep the None and All strategies off the selects_null scan --- arrow-select/src/filter.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/arrow-select/src/filter.rs b/arrow-select/src/filter.rs index 7f058e107b97..6047a499145e 100644 --- a/arrow-select/src/filter.rs +++ b/arrow-select/src/filter.rs @@ -550,10 +550,11 @@ impl FilterPredicate { } } - /// Returns true if any row selected by this predicate is null in `nulls` + /// Returns true if any row selected by this predicate is null in `nulls`. + /// + /// The caller must check that `nulls` covers the filter, as `filter_nulls` does. fn selects_null(&self, nulls: &NullBuffer) -> bool { let len = self.filter.len(); - assert!(nulls.len() >= len); // Below one selected row per word, probing the precomputed indices // is cheaper than scanning the mask, as in `filter_bits` if let IterationStrategy::Indices(indices) = &self.strategy @@ -580,7 +581,7 @@ impl FilterPredicate { /// Panics if the input contains nulls and is shorter than this predicate. pub fn filter_nulls(&self, nulls: Option<&NullBuffer>) -> Option { let nulls = nulls?; - if nulls.null_count() == 0 || !self.selects_null(nulls) { + if nulls.null_count() == 0 { return None; } @@ -594,14 +595,16 @@ impl FilterPredicate { _ => {} } + if !self.selects_null(nulls) { + return None; + } + let nulls = filter_bits(nulls.inner(), self); // The filtered `nulls` has a length of `self.count` bits and therefore // the null count is this minus the number of valid bits let null_count = self.count - nulls.count_set_bits_offset(0, self.count); - - if null_count == 0 { - return None; - } + // `selects_null` found a selected null, and `filter_bits` keeps it + debug_assert_ne!(null_count, 0); let buffer = BooleanBuffer::new(nulls, 0, self.count); debug_assert_eq!(null_count, buffer.len() - buffer.count_set_bits()); @@ -1712,7 +1715,7 @@ mod tests { } // Three full 64-bit words and an 8-bit partial trailing word - const LEN: usize = 200; + const NULLS_LEN: usize = 200; // The first row of the first two words, and the last row of the partial one const NULL_ROWS: [usize; 3] = [0, 64, 199]; // A valid row in the partial trailing word @@ -1720,27 +1723,30 @@ mod tests { // Sliced at offset 1 so the null bits start off a byte boundary fn nullable_values() -> Int32Array { - let mut values = vec![Some(0); LEN + 1]; + let mut values = vec![Some(0); NULLS_LEN + 1]; for row in NULL_ROWS { values[row + 1] = None; } - Int32Array::from(values).slice(1, LEN) + Int32Array::from(values).slice(1, NULLS_LEN) } // One case per path through `selects_null` and the filter strategies fn keep_valid_rows(nulls: &NullBuffer) -> [Vec; 3] { [ // Above FILTER_SLICES_SELECTIVITY_THRESHOLD, so a slices strategy - (0..LEN).map(|i| nulls.is_valid(i)).collect(), + (0..NULLS_LEN).map(|i| nulls.is_valid(i)).collect(), // Below it, so an index strategy that scans the mask - (0..LEN).map(|i| i % 2 == 1 && nulls.is_valid(i)).collect(), + (0..NULLS_LEN) + .map(|i| i % 2 == 1 && nulls.is_valid(i)) + .collect(), // Under one row per word, so the optimized predicate probes its indices - (0..LEN).map(|i| i == SPARSE_ROW).collect(), + (0..NULLS_LEN).map(|i| i == SPARSE_ROW).collect(), ] } fn predicates(keep: &[bool]) -> Vec { let mask = BooleanArray::from(keep.to_vec()); + // The same mask at offset 1, so its bits start off a byte boundary let padded: BooleanArray = std::iter::once(false).chain(keep.iter().copied()).collect(); let offset_mask = padded.slice(1, keep.len()); [mask, offset_mask] @@ -2843,8 +2849,8 @@ mod tests { } #[test] - #[should_panic(expected = "nulls.len() >= len")] - fn test_filter_nulls_shorter_than_filter() { + #[should_panic(expected = "nulls.len() >= self.filter.len()")] + fn test_filter_nulls_too_large() { let nulls = NullBuffer::from(vec![true, false, true, true, true, true, true, true]); let predicate = BooleanArray::from(vec![ true, false, false, false, false, false, false, false, false,