You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/01/30 00:04:07 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request #1248: POC: Specialized filter kernels

tustvold opened a new pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248


   **Unfortunately I didn't get as much time to work on this today as I hoped, but I thought I'd get it up for some visibility into what I'm playing around with**
   
   # Which issue does this PR close?
   
   Closes #TBD
   
   # Rationale for this change
    
   Make filter kernels faster. This relates to the observation underlying #1225 and #1229, that the filter kernels are surprisingly slow for what they are doing. In many cases the filter kernels on master are slower than converting the filter to a list of indices and using `take`...
   
   Currently this PR is ~10x faster than master, and I suspect this could be pushed further.
   
   ```
   filter u8               time:   [47.525 us 47.538 us 47.554 us]                       
                           change: [-90.065% -90.056% -90.048%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 high selectivity                                                                             
                           time:   [2.3351 us 2.3358 us 2.3365 us]
                           change: [-81.582% -81.570% -81.558%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 low selectivity                                                                             
                           time:   [1.3204 us 1.3218 us 1.3233 us]
                           change: [-70.639% -70.572% -70.512%] (p = 0.00 < 0.05)
                           Performance has improved.
   ```
   
   # What changes are included in this PR?
   
   This builds on #1228 and adds specialized filter implementations for primitive array types. I suspect specialized implementations for byte array types, and dictionaries would likely yield similarly significant benefits and I may include them in this PR or a follow up.
   
   Aside from the  performance benefits, having specialized impls, much like we do for the `take` kernels will also allow us to take advantage of SIMD intrinsics either through manual implementation, or just helping the compiler's auto-vectorization. 
   
   # Are there any user-facing changes?
   
   No


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (9fc5fac) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.00%`.
   > The diff coverage is `75.60%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.99%   -0.01%     
   ==========================================
     Files         180      180              
     Lines       52288    52587     +299     
   ==========================================
   + Hits        43396    43644     +248     
   - Misses       8892     8943      +51     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `84.77% <75.60%> (-7.73%)` | :arrow_down: |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `65.98% <0.00%> (-0.23%)` | :arrow_down: |
   | [arrow/src/ffi.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2ZmaS5ycw==) | `84.69% <0.00%> (ø)` | |
   | [arrow/src/csv/reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2Nzdi9yZWFkZXIucnM=) | `88.12% <0.00%> (ø)` | |
   | [parquet/src/data\_type.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvZGF0YV90eXBlLnJz) | `76.61% <0.00%> (ø)` | |
   | [arrow/src/array/array\_list.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L2FycmF5X2xpc3QucnM=) | `95.52% <0.00%> (ø)` | |
   | [arrow/src/array/array\_union.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L2FycmF5X3VuaW9uLnJz) | `90.76% <0.00%> (ø)` | |
   | [arrow/src/array/raw\_pointer.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3Jhd19wb2ludGVyLnJz) | `100.00% <0.00%> (ø)` | |
   | ... and [9 more](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...9fc5fac](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] viirya commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801072818



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,600 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let strategy = IterationStrategy::default_strategy(filter.len(), count);
+
+        Self {
+            filter,
+            count,
+            strategy,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.strategy {
+            IterationStrategy::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.strategy = IterationStrategy::Slices(slices)
+            }
+            IterationStrategy::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.strategy = IterationStrategy::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
-        0 => {
-            // return empty
-            Ok(new_empty_array(array.data_type()))
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            strategy: self.strategy,
         }
-        len if len == array.len() => {
-            // return all
-            let data = array.data().clone();
-            Ok(make_array(data))
+    }
+}
+
+/// The iteration strategy used to evaluate [`FilterPredicate`]
+#[derive(Debug)]
+enum IterationStrategy {
+    /// A lazily evaluated iterator of ranges
+    SlicesIterator,
+    /// A lazily evaluated iterator of indices
+    IndexIterator,
+    /// A precomputed list of indices
+    Indices(Vec<usize>),
+    /// A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+    /// Select all rows
+    All,
+    /// Select no rows
+    None,
+}
+
+impl IterationStrategy {
+    /// The default [`IterationStrategy`] for a filter of length `filter_length`
+    /// and selecting `filter_count` rows
+    fn default_strategy(filter_length: usize, filter_count: usize) -> Self {
+        if filter_length == 0 || filter_count == 0 {
+            return IterationStrategy::None;
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+        if filter_count == filter_length {
+            return IterationStrategy::All;
+        }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+        // Compute the selectivity of the predicate by dividing the number of true
+        // bits in the predicate by the predicate's total length
+        //
+        // This can then be used as a heuristic for the optimal iteration strategy
+        let selectivity_frac = filter_count as f64 / filter_length as f64;
+        if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            return IterationStrategy::SlicesIterator;
         }
+        IterationStrategy::IndexIterator
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
     }
+}
 
-    let num_columns = record_batch.columns().len();
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    match predicate.strategy {
+        IterationStrategy::None => Ok(new_empty_array(values.data_type())),
+        IterationStrategy::All => Ok(make_array(values.data().slice(0, predicate.count))),
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                match &predicate.strategy {
+                    IterationStrategy::Slices(slices) => {
+                        slices
+                            .iter()
+                            .for_each(|(start, end)| mutable.extend(0, *start, *end));
+                    }
+                    _ => {
+                        let iter = SlicesIterator::new(&predicate.filter);
+                        iter.for_each(|(start, end)| mutable.extend(0, start, end));

Review comment:
       Hmm, why using `SlicesIterator` covers all other cases, e.g., `Indices` here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799629375



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator

Review comment:
       here `FilterIterator` looks more like `FilterIteratorType`, as in this is no the actual iterator




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798378622



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,68 +267,573 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {

Review comment:
       The idea behind this is that we may wish to give the user more control over how the predicate is executed, by creating a builder we have an obvious extension point for doing this




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798958224



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,

Review comment:
       I think with some additions to the `GenericStringBuilder` API this would definitely be possible. In particular it needs a way to append a slice of values. Definitely something to look into :+1:




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799357447



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;

Review comment:
       I think this calculation of the selectivity fraction is important and so a comment to explain what the intention is (e.g. selectivity fraction = number of values kept / total number of values) would be useful




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799317629



##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,68 +42,140 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
-    c.bench_function("filter u8", |b| {
+    c.bench_function("filter optimize (1/2)", |b| {
+        b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize high selectivity (1023/1024)", |b| {
+        b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize low selectivity (1/1024)", |b| {
+        b.iter(|| FilterBuilder::new(&sparse_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter u8 (1/2)", |b| {
         b.iter(|| bench_filter(&data_array, &filter_array))
     });
-    c.bench_function("filter u8 high selectivity", |b| {
+    c.bench_function("filter u8 high selectivity (1023/1024)", |b| {

Review comment:
       Updated this to read (kept 1023/1024)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r795106705



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -180,37 +246,146 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
     if predicate.null_count() > 0 {
         // this greatly simplifies subsequent filtering code
         // now we only have a boolean mask to deal with
         let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+        return filter(values, &predicate);
+    }
+
+    if predicate.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.len(),
+            values.len()
+        )));
     }
 
     let filter_count = filter_count(predicate);
 
     match filter_count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {

Review comment:
       This block is copied straight from the take kernel - there is probably a way to avoid this duplication with some more layers of macros  




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798949820



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();

Review comment:
       I'll add a comment clarifying this, the effectively it is doing `nulls.len() - nulls.count_valid()` but as `nulls: Buffer` it needs to use a proxy to get the "bit length"




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798949820



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();

Review comment:
       I'll add a comment clarifying this, the effectively it is doing `nulls.len() - nulls.count_valid()` but as `nulls: Buffer` it needs to use a proxy to get the "bit length". It is explicitly handling the case where the filter is not false for a null location :sweat_smile: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025251778


   I found some time this afternoon, so bashed out porting the filter context abstraction (caching the selection vector) and fixing up the null buffer construction.
   
   Here's where we stand now, across the board about 10x performance uplift other than for high selectivity with nulls where the bottleneck on copying ranges of null bitmasks is unchanged
   
   ```
   filter u8               time:   [48.733 us 48.795 us 48.889 us]                       
                           change: [-90.138% -90.127% -90.116%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 high selectivity                                                                             
                           time:   [2.4004 us 2.4048 us 2.4099 us]
                           change: [-81.016% -80.967% -80.915%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 low selectivity                                                                             
                           time:   [1.4000 us 1.4015 us 1.4032 us]
                           change: [-67.378% -67.309% -67.246%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8       time:   [14.783 us 14.798 us 14.816 us]                               
                           change: [-95.157% -95.150% -95.143%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 high selectivity                                                                             
                           time:   [1.1631 us 1.1636 us 1.1642 us]
                           change: [-85.209% -85.201% -85.192%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 low selectivity                                                                            
                           time:   [150.47 ns 150.64 ns 150.83 ns]
                           change: [-84.434% -84.380% -84.295%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs                                                                             
                           time:   [40.762 us 40.771 us 40.781 us]
                           change: [-89.275% -89.267% -89.259%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs high selectivity                                                                             
                           time:   [7.0784 us 7.0828 us 7.0876 us]
                           change: [+2.4840% +2.5549% +2.6258%] (p = 0.00 < 0.05)
                           Performance has regressed.
   
   filter context u8 w NULLs low selectivity                                                                            
                           time:   [267.79 ns 267.96 ns 268.12 ns]
                           change: [-72.091% -72.023% -71.946%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter f32              time:   [117.24 us 117.27 us 117.32 us]                       
                           change: [-79.651% -79.637% -79.623%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32      time:   [41.474 us 41.486 us 41.499 us]                                
                           change: [-88.917% -88.911% -88.905%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32 high selectivity                                                                             
                           time:   [11.289 us 11.292 us 11.294 us]
                           change: [+3.9848% +4.0461% +4.1142%] (p = 0.00 < 0.05)
                           Performance has regressed.
   
   filter context f32 low selectivity                                                                            
                           time:   [295.75 ns 296.19 ns 296.65 ns]
                           change: [-69.321% -69.181% -69.008%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter single record batch                                                                            
                           time:   [69.716 us 69.749 us 69.783 us]
                           change: [-86.024% -86.009% -85.998%] (p = 0.00 < 0.05)
                           Performance has improved.
   ```
   
   What is interesting, at least to me, is the performance tax imposed by the packed bitmask representation of BooleanArray - even with non-trivial bit-twiddling shenanigans it still appears to be more performant to hydrate the filter to an array of indices or slices when filtering multiple arrays. 
   
   ```
   filter optimize         time:   [45.865 us 45.990 us 46.096 us]                             
   
   filter optimize high selectivity                                                                             
                           time:   [1.6900 us 1.6910 us 1.6920 us]
   
   filter optimize low selectivity                                                                             
                           time:   [1.4087 us 1.4116 us 1.4149 us]
   ```
   
   Perhaps the compiler just struggles to auto-vectorise bitmask loops or something, not sure :sweat_smile: 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800635721



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);

Review comment:
       This was actually being handled at a higher level in filter_array, I've cleaned this up https://github.com/apache/arrow-rs/pull/1248/commits/7b4a2f16c1576e90c9e226e562918ad79c883ea0




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799526637



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_end_offset: usize,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_end_offset = 64 - chunks.lead_padding();
+
+        Self {
+            current_chunk,
+            chunk_end_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some(self.chunk_end_offset + (bit_pos as usize) - 64);

Review comment:
       if `chunk_end_offset`  is initialized to just  ` - chunks.lead_padding()` (instead of `64 - chunks.lead_padding()`, then wouldn't it be unnecessary to subtract `64` here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799822846



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;

Review comment:
       Added a comment




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r795981104



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -247,65 +246,399 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used construct [`FilterPredicate`]

Review comment:
       this probably should read: `... that can be used TO construct A FilterPredicate`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798945289



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),

Review comment:
       :spaghetti: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798942622



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       NULLs within the filter itself are handled by `prep_null_mask_filter` - i.e. they are converted to false prior to any of this code seeing it




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799052565



##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,68 +42,140 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
-    c.bench_function("filter u8", |b| {
+    c.bench_function("filter optimize (1/2)", |b| {
+        b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize high selectivity (1023/1024)", |b| {
+        b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize low selectivity (1/1024)", |b| {
+        b.iter(|| FilterBuilder::new(&sparse_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter u8 (1/2)", |b| {
         b.iter(|| bench_filter(&data_array, &filter_array))
     });
-    c.bench_function("filter u8 high selectivity", |b| {
+    c.bench_function("filter u8 high selectivity (1023/1024)", |b| {

Review comment:
       does 1023/1024 mean 1023 out of 1024 kept or filtered out?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799342264



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
 
-    match filter_count {
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    // The filtered `nulls` has a length of `predicate.count` bits and
+    // therefore the null count is this minus the number of valid bits
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
+
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
         }
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
 
-    let num_columns = record_batch.columns().len();
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);

Review comment:
       I can't say I understand why, but for some reason this actually makes the performance worse?? My only guess is that the loads are speculated eliminating the cost of the range check, and there is some assumption LLVM can make about the checked load that it can't about the unchecked... Not sure :sweat_smile: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799334843



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;

Review comment:
       that's a great idea and I suspect that in order to get the best performance in more cases, it will often be necessary to choose one of multiple possible implementations at runtime in other places in the code as well; also love the reference to the research paper




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold edited a comment on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1028457980


   So I've added string and dictionary support. Strings only see a ~2x performance bump, this can almost certainly be pushed further, but I figured it was good enough and I can come back to it in subsequent PRs. Dictionaries see the full 9-10x bump that the integer types see which is :ok_hand: 
   
   I'm marking this ready for review because I think it represents a non-trivial improvement that would be super-awesome to make it into the arrow 9 release. There is definitely further refinement that could take place, and more perf that could be squeezed out, but I'd prefer to do that in subsequent PRs unless people object.
   
   For reference here are the latest numbers, down a little bit since #1228 was merged, but still pretty exciting imo
   
   ```
   filter u8               time:   [47.731 us 47.740 us 47.748 us]                       
                           change: [-85.383% -85.377% -85.371%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 high selectivity                                                                             
                           time:   [2.4163 us 2.4185 us 2.4210 us]
                           change: [-73.969% -73.939% -73.909%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 low selectivity                                                                             
                           time:   [1.3798 us 1.3814 us 1.3829 us]
                           change: [-41.966% -41.649% -41.441%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8       time:   [14.557 us 14.567 us 14.580 us]                               
                           change: [-94.178% -94.175% -94.171%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 high selectivity                                                                             
                           time:   [1.1673 us 1.1678 us 1.1684 us]
                           change: [-84.915% -84.905% -84.894%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 low selectivity                                                                            
                           time:   [150.77 ns 150.80 ns 150.83 ns]
                           change: [-82.779% -82.758% -82.735%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32              time:   [68.835 us 68.857 us 68.882 us]                       
                           change: [-78.627% -78.613% -78.598%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32 high selectivity                                                                             
                           time:   [6.1117 us 6.1135 us 6.1154 us]
                           change: [-54.814% -54.788% -54.759%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32 low selectivity                                                                             
                           time:   [1.3835 us 1.3858 us 1.3895 us]
                           change: [-41.772% -41.682% -41.570%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32      time:   [16.049 us 16.058 us 16.068 us]                                
                           change: [-93.552% -93.545% -93.535%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 high selectivity                                                                             
                           time:   [4.7336 us 4.7417 us 4.7507 us]
                           change: [-59.764% -59.459% -58.934%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 low selectivity                                                                            
                           time:   [141.69 ns 141.83 ns 141.98 ns]
                           change: [-83.970% -83.929% -83.895%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 w NULLs                                                                             
                           time:   [42.514 us 42.529 us 42.548 us]
                           change: [-86.620% -86.613% -86.606%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 w NULLs high selectivity                                                                             
                           time:   [10.785 us 10.788 us 10.790 us]
                           change: [+0.8230% +1.0819% +1.2557%] (p = 0.00 < 0.05)
                           Change within noise threshold.
   
   filter context i32 w NULLs low selectivity                                                                            
                           time:   [279.94 ns 280.04 ns 280.13 ns]
                           change: [-68.389% -68.317% -68.242%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs                                                                             
                           time:   [40.525 us 40.540 us 40.557 us]
                           change: [-87.204% -87.196% -87.187%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs high selectivity                                                                             
                           time:   [7.0863 us 7.0895 us 7.0928 us]
                           change: [+3.4115% +3.4779% +3.5553%] (p = 0.00 < 0.05)
                           Performance has regressed.
   
   filter context u8 w NULLs low selectivity                                                                            
                           time:   [270.69 ns 270.79 ns 270.89 ns]
                           change: [-67.961% -67.906% -67.849%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter f32              time:   [120.99 us 121.18 us 121.56 us]                       
                           change: [-68.315% -68.281% -68.241%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32      time:   [36.935 us 36.951 us 36.968 us]                                
                           change: [-88.072% -88.059% -88.045%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32 high selectivity                                                                             
                           time:   [10.339 us 10.342 us 10.346 us]
                           change: [-1.7648% -1.4694% -1.2565%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32 low selectivity                                                                            
                           time:   [287.59 ns 288.40 ns 289.16 ns]
                           change: [-67.322% -67.087% -66.921%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string   time:   [243.62 us 243.66 us 243.72 us]                                  
                           change: [-40.960% -40.903% -40.858%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string high selectivity                                                                            
                           time:   [75.645 us 75.664 us 75.682 us]
                           change: [-81.225% -81.211% -81.199%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string low selectivity                                                                             
                           time:   [666.91 ns 667.33 ns 667.80 ns]
                           change: [-42.717% -42.635% -42.556%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary                                                                             
                           time:   [16.348 us 16.354 us 16.359 us]
                           change: [-89.690% -89.684% -89.677%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary high selectivity                                                                              
                           time:   [4.8464 us 4.8530 us 4.8617 us]
                           change: [-83.162% -83.106% -83.056%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary low selectivity                                                                            
                           time:   [318.97 ns 319.14 ns 319.32 ns]
                           change: [-49.861% -49.819% -49.770%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs                                                                             
                           time:   [42.654 us 42.726 us 42.806 us]
                           change: [-87.775% -87.728% -87.657%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs high selectivity                                                                             
                           time:   [11.313 us 11.316 us 11.319 us]
                           change: [-68.181% -68.152% -68.130%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs low selectivity                                                                            
                           time:   [480.59 ns 480.81 ns 481.04 ns]
                           change: [-54.683% -54.630% -54.579%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter single record batch                                                                            
                           time:   [62.108 us 62.210 us 62.292 us]
                           change: [-80.890% -80.862% -80.837%] (p = 0.00 < 0.05)
                           Performance has improved.
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798380248



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,68 +267,573 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
+    }
+}
+
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
     }
+}
 
-    let filter_count = filter_count(predicate);
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
 
-    match filter_count {
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+            unsafe { collect_filtered_bits(bits, predicate.count) }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
+            unsafe { collect_filtered_bits(bits, predicate.count) }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
         }
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// Collects the provided boolean iterator into a `Buffer`
+///
+/// Largely copied from `MutableBuffer::from_trusted_len_iter_bool`
+///
+/// TODO: DRY this up
+unsafe fn collect_filtered_bits(
+    mut src: impl Iterator<Item = bool>,
+    bit_len: usize,
+) -> Buffer {
+    let aligned_len = bit_util::ceil(bit_len, 64) * 8;
+    let mut buffer = MutableBuffer::new(aligned_len);
+
+    'a: loop {
+        let mut byte_accum: u64 = 0;
+        let mut mask: u64 = 1;
+
+        while mask != 0 {
+            if let Some(value) = src.next() {
+                byte_accum |= match value {
+                    true => mask,
+                    false => 0,
+                };
+                mask <<= 1;
+            } else {
+                if mask != 1 {
+                    buffer.push_unchecked(byte_accum);
+                }
+                break 'a;
+            }
+        }
+        buffer.push_unchecked(byte_accum);
     }
 
-    let num_columns = record_batch.columns().len();
+    // Truncate to byte length - technically not necessary but cannot hurt
+    buffer.resize(bit_util::ceil(bit_len, 8), 0);
+    buffer.into()
+}
+
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let mut buffer = MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+
+    match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+        FilterIterator::Slices(slices) => {
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
         }
+        FilterIterator::IndexIterator => {
+            for idx in IndexIterator::new(&predicate.filter) {
+                unsafe { buffer.push_unchecked(values[idx]) };
+            }
+        }
+        FilterIterator::Indices(indices) => {
+            for idx in indices {
+                unsafe { buffer.push_unchecked(values[*idx]) };
+            }
+        }
+    }
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
+}
+
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {

Review comment:
       In subsequent PRs I intend to experiment with applying a similar construction to primitive arrays and null bitmasks, e.g. `FilterPrimitive` and `FilterNull` as I think it might compose nicely and potentially allow for code sharing with the take kernels.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800150479



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        // Compute the selectivity of the predicate by dividing the number of true
+        // bits in the predicate by the predicate's total length
+        //
+        // This can then be used as a heuristic for the optimal iteration strategy
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let strategy = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            IterationStrategy::SlicesIterator
+        } else {
+            IterationStrategy::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            strategy,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.strategy {
+            IterationStrategy::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.strategy = IterationStrategy::Slices(slices)
+            }
+            IterationStrategy::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.strategy = IterationStrategy::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            strategy: self.strategy,
+        }
+    }
+}
+
+/// The iteration strategy used to evaluate [`FilterPredicate`]
+#[derive(Debug)]
+enum IterationStrategy {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                match &predicate.strategy {
+                    IterationStrategy::Slices(slices) => {
+                        slices
+                            .iter()
+                            .for_each(|(start, end)| mutable.extend(0, *start, *end));
+                    }
+                    _ => {
+                        let iter = SlicesIterator::new(&predicate.filter);
+                        iter.for_each(|(start, end)| mutable.extend(0, start, end));
+                    }
+                }
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// If the predicate selected no null-rows, returns `None`, otherwise returns
+/// `Some((null_count, null_buffer))` where `null_count` is the number of nulls
+/// in the filtered output, and `null_buffer` is the filtered null buffer
+///
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    // The filtered `nulls` has a length of `predicate.count` bits and
+    // therefore the null count is this minus the number of valid bits
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.strategy {
+        IterationStrategy::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        IterationStrategy::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        IterationStrategy::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start + offset..end + offset, src)
+            }
+            builder.finish()
+        }
+        IterationStrategy::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start + offset..*end + offset, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.strategy {
+        IterationStrategy::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
+        }
+        IterationStrategy::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        IterationStrategy::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
         }
+        IterationStrategy::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let num_offsets_bytes = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut dst_offsets = MutableBuffer::new(num_offsets_bytes);
+        let dst_values = MutableBuffer::new(0);
+        let cur_offset = OffsetSize::zero();
+        dst_offsets.push(cur_offset);
+
+        Self {
+            src_offsets: array.value_offsets(),
+            src_values: &array.data().buffers()[1],
+            dst_offsets,
+            dst_values,
+            cur_offset,
+        }
+    }
+
+    /// Returns the byte offset at `idx`
+    #[inline]
+    fn get_value_offset(&self, idx: usize) -> usize {
+        self.src_offsets[idx].to_usize().expect("illegal offset")
     }
 
-    let num_columns = record_batch.columns().len();
+    /// Returns the start and end of the value at index `idx` along with its length
+    #[inline]
+    fn get_value_range(&self, idx: usize) -> (usize, usize, OffsetSize) {
+        // These can only fail if `array` contains invalid data
+        let start = self.get_value_offset(idx);
+        let end = self.get_value_offset(idx + 1);
+        let len = OffsetSize::from_usize(end - start).expect("illegal offset range");
+        (start, end, len)
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    /// Extends the in-progress array by the indexes in the provided iterator
+    fn extend_idx(&mut self, iter: impl Iterator<Item = usize>) {
+        for idx in iter {
+            let (start, end, len) = self.get_value_range(idx);
+            self.cur_offset += len;
+            self.dst_offsets.push(self.cur_offset);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+    }
+
+    /// Extends the in-progress array by the ranges in the provided iterator
+    fn extend_slices(&mut self, iter: impl Iterator<Item = (usize, usize)>) {
+        for slice in iter {

Review comment:
       ```suggestion
           for (start, end) in iter {
   ```
   Small style suggestion




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jorgecarleitao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800191885



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_offset: i64,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_offset = -(chunks.lead_padding() as i64);
+
+        Self {
+            current_chunk,
+            chunk_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some((self.chunk_offset + bit_pos as i64) as usize);
+            }
+
+            // Must panic if exhausted early as trusted length iterator
+            self.current_chunk = self.iter.next().expect("IndexIterator exhausted early");
+            self.chunk_offset += 64;
+        }
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.remaining, Some(self.remaining))
+    }
+}
+
+/// Counts the number of set bits in `filter`
 fn filter_count(filter: &BooleanArray) -> usize {

Review comment:
       not sure why this was/here here, but isn't this equal to `filter.len() - filter.null_count()`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] alamb merged pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801071624



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most

Review comment:
       I _think_ a filter with high selectivity is considered to be not very selective, i.e. it isn't very discerning about what it selects. However, this terminology seems to be a source of a lot of confusion, myself included, so I'm just going to reword it :laughing: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] viirya commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801062865



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most

Review comment:
       nvm if I read it wrongly. But doesn't "the least selective predicates" means keep only a few rows? I think here it wants to say is, the highly selective predicates should favours `SlicesIterator`, right?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030782311


   FYI @jorgecarleitao arrow-rs might be good to compare performance to arrow2 after those changes?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800227050



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);

Review comment:
       Good shout - will add :+1:




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (2aa46b0) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.00%`.
   > The diff coverage is `75.30%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.98%   -0.01%     
   ==========================================
     Files         180      180              
     Lines       52288    52561     +273     
   ==========================================
   + Hits        43396    43619     +223     
   - Misses       8892     8942      +50     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `84.64% <75.30%> (-7.86%)` | :arrow_down: |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [...ntegration-testing/src/bin/arrow-file-to-stream.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWZpbGUtdG8tc3RyZWFtLnJz) | `0.00% <0.00%> (ø)` | |
   | [...ion-testing/src/bin/arrow-json-integration-test.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWpzb24taW50ZWdyYXRpb24tdGVzdC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-client.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLWNsaWVudC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-server.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLXNlcnZlci5ycw==) | `0.00% <0.00%> (ø)` | |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `66.43% <0.00%> (+0.22%)` | :arrow_up: |
   | [arrow/src/datatypes/field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9maWVsZC5ycw==) | `54.10% <0.00%> (+0.30%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...2aa46b0](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] sunchao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
sunchao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800233631



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {

Review comment:
       I don't know either. For the chained ANDs case, maybe in future it is worth to evaluate the approach based on selection vector and only materialize the output array at the end. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030196008


   @tustvold I've read the code as carefully as I can, I've left a few minor comments but looks great overall - great work


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801080970



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,600 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let strategy = IterationStrategy::default_strategy(filter.len(), count);
+
+        Self {
+            filter,
+            count,
+            strategy,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.strategy {
+            IterationStrategy::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.strategy = IterationStrategy::Slices(slices)
+            }
+            IterationStrategy::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.strategy = IterationStrategy::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
-        0 => {
-            // return empty
-            Ok(new_empty_array(array.data_type()))
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            strategy: self.strategy,
         }
-        len if len == array.len() => {
-            // return all
-            let data = array.data().clone();
-            Ok(make_array(data))
+    }
+}
+
+/// The iteration strategy used to evaluate [`FilterPredicate`]
+#[derive(Debug)]
+enum IterationStrategy {
+    /// A lazily evaluated iterator of ranges
+    SlicesIterator,
+    /// A lazily evaluated iterator of indices
+    IndexIterator,
+    /// A precomputed list of indices
+    Indices(Vec<usize>),
+    /// A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+    /// Select all rows
+    All,
+    /// Select no rows
+    None,
+}
+
+impl IterationStrategy {
+    /// The default [`IterationStrategy`] for a filter of length `filter_length`
+    /// and selecting `filter_count` rows
+    fn default_strategy(filter_length: usize, filter_count: usize) -> Self {
+        if filter_length == 0 || filter_count == 0 {
+            return IterationStrategy::None;
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+        if filter_count == filter_length {
+            return IterationStrategy::All;
+        }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+        // Compute the selectivity of the predicate by dividing the number of true
+        // bits in the predicate by the predicate's total length
+        //
+        // This can then be used as a heuristic for the optimal iteration strategy
+        let selectivity_frac = filter_count as f64 / filter_length as f64;
+        if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            return IterationStrategy::SlicesIterator;
         }
+        IterationStrategy::IndexIterator
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
     }
+}
 
-    let num_columns = record_batch.columns().len();
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    match predicate.strategy {
+        IterationStrategy::None => Ok(new_empty_array(values.data_type())),
+        IterationStrategy::All => Ok(make_array(values.data().slice(0, predicate.count))),
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                match &predicate.strategy {
+                    IterationStrategy::Slices(slices) => {
+                        slices
+                            .iter()
+                            .for_each(|(start, end)| mutable.extend(0, *start, *end));
+                    }
+                    _ => {
+                        let iter = SlicesIterator::new(&predicate.filter);
+                        iter.for_each(|(start, end)| mutable.extend(0, start, end));

Review comment:
       In short because `MutableArrayData` doesn't support other iteration strategies and I'm not sure that calling it with "ranges" of length 1 will outperform at least attempting to call it with larger ranges.
   
   My 2 cents is that the eventual goal should be to remove this fallback, as `MutableArrayData` is a poor fit for filtering




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799041843



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       my understanding of the meaning of "highly selective filter" is that it filters out most of the data




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800475726



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);

Review comment:
       What about the same for `0` (no selected values)?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798951408



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut offsets = MutableBuffer::new(bytes_offset);
+        let values = MutableBuffer::new(0);
+        let cur_offset = OffsetSize::zero();
+        offsets.push(cur_offset);
+
+        Self {
+            src_offsets: array.value_offsets(),
+            src_values: &array.data().buffers()[1],
+            dst_offsets: offsets,
+            dst_values: values,
+            cur_offset,
+        }
+    }
+
+    /// Returns the byte offset at `idx`
+    #[inline]
+    fn get_value_offset(&self, idx: usize) -> usize {
+        self.src_offsets[idx].to_usize().expect("illegal offset")
     }
 
-    let num_columns = record_batch.columns().len();
+    /// Returns the start and end of the value at index `idx` along with its length
+    #[inline]
+    fn get_value_range(&self, idx: usize) -> (usize, usize, OffsetSize) {
+        // These can only fail if `array` contains invalid data
+        let start = self.get_value_offset(idx);
+        let end = self.get_value_offset(idx + 1);
+        let len = OffsetSize::from_usize(end - start).expect("illegal offset range");
+        (start, end, len)
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    /// Extends the in-progress array by the indexes in the provided iterator
+    fn extend_idx(&mut self, iter: impl Iterator<Item = usize>) {
+        for idx in iter {
+            let (start, end, len) = self.get_value_range(idx);
+            self.cur_offset += len;
+            self.dst_offsets.push(self.cur_offset);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+    }
+
+    /// Extends the in-progress array by the ranges in the provided iterator
+    fn extend_slices(&mut self, iter: impl Iterator<Item = (usize, usize)>) {
+        for slice in iter {
+            // These can only fail if `array` contains invalid data
+            for idx in slice.0..slice.1 {
+                let (_, _, len) = self.get_value_range(idx);
+                self.cur_offset += len;
+                self.dst_offsets.push(self.cur_offset); // push_unchecked?
+            }
+
+            let start = self.get_value_offset(slice.0);
+            let end = self.get_value_offset(slice.1);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
+    }
+}
+
+/// `filter` implementation for string arrays
+///
+/// Note: NULLs with a non-zero slot length in `array` will have the corresponding
+/// data copied across. This allows handling the null mask separately from the data

Review comment:
       `prep_null_mask_filter` handles nulls in the filter, not nulls in the array being filtered.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799382832



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();

Review comment:
       `IndexIterator` reports its size allowing it to be used with `trusted_len_iter`, `SlicesIterator` does not




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799378153



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;
+
+macro_rules! downcast_filter {
+    ($type: ty, $values: expr, $filter: expr) => {{
+        let values = $values
+            .as_any()
+            .downcast_ref::<PrimitiveArray<$type>>()
+            .expect("Unable to downcast to a primitive array");
+
+        Ok(Arc::new(filter_primitive::<$type>(&values, $filter)))
+    }};
+}
+
+macro_rules! downcast_dict_filter {
+    ($type: ty, $values: expr, $filter: expr) => {{
+        let values = $values
+            .as_any()
+            .downcast_ref::<DictionaryArray<$type>>()
+            .expect("Unable to downcast to a dictionary array");
+        Ok(Arc::new(filter_dict::<$type>(values, $filter)))
+    }};
+}
 
 /// An iterator of `(usize, usize)` each representing an interval `[start,end[` whose

Review comment:
       "[start,end[" looks like a typo, should probably be "[start,end]" (to indicate an inclusive range); I know that this line wasn't changed so far in this PR, but if changes are made in this area of the code anyway, it's an opportunity to fix / improve little things like this I think




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (437e120) into [master](https://codecov.io/gh/apache/arrow-rs/commit/aa71aeaa3d6f0345690490588640226701e1ac15?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (aa71aea) will **increase** coverage by `0.00%`.
   > The diff coverage is `82.11%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@           Coverage Diff            @@
   ##           master    #1248    +/-   ##
   ========================================
     Coverage   82.96%   82.96%            
   ========================================
     Files         178      178            
     Lines       51522    51865   +343     
   ========================================
   + Hits        42744    43029   +285     
   - Misses       8778     8836    +58     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `83.51% <72.76%> (-6.05%)` | :arrow_down: |
   | [arrow/src/util/bit\_chunk\_iterator.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYml0X2NodW5rX2l0ZXJhdG9yLnJz) | `93.47% <92.43%> (-4.33%)` | :arrow_down: |
   | [arrow/src/buffer/immutable.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2J1ZmZlci9pbW11dGFibGUucnM=) | `99.45% <100.00%> (+0.52%)` | :arrow_up: |
   | [parquet/src/arrow/bit\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYml0X3V0aWwucnM=) | `100.00% <100.00%> (ø)` | |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.64% <0.00%> (-0.13%)` | :arrow_down: |
   | [parquet/src/arrow/array\_reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYXJyYXlfcmVhZGVyLnJz) | `78.21% <0.00%> (+0.01%)` | :arrow_up: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `66.43% <0.00%> (+0.22%)` | :arrow_up: |
   | [parquet/src/arrow/arrow\_reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYXJyb3dfcmVhZGVyLnJz) | `93.57% <0.00%> (+0.38%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [aa71aea...437e120](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (4e9197c) into [master](https://codecov.io/gh/apache/arrow-rs/commit/aa71aeaa3d6f0345690490588640226701e1ac15?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (aa71aea) will **decrease** coverage by `0.00%`.
   > The diff coverage is `82.11%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.96%   82.96%   -0.01%     
   ==========================================
     Files         178      178              
     Lines       51522    51865     +343     
   ==========================================
   + Hits        42744    43028     +284     
   - Misses       8778     8837      +59     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `83.51% <72.76%> (-6.05%)` | :arrow_down: |
   | [arrow/src/util/bit\_chunk\_iterator.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYml0X2NodW5rX2l0ZXJhdG9yLnJz) | `93.47% <92.43%> (-4.33%)` | :arrow_down: |
   | [arrow/src/buffer/immutable.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2J1ZmZlci9pbW11dGFibGUucnM=) | `99.45% <100.00%> (+0.52%)` | :arrow_up: |
   | [parquet/src/arrow/bit\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYml0X3V0aWwucnM=) | `100.00% <100.00%> (ø)` | |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.51% <0.00%> (-0.26%)` | :arrow_down: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `66.21% <0.00%> (ø)` | |
   | [parquet/src/arrow/array\_reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYXJyYXlfcmVhZGVyLnJz) | `78.21% <0.00%> (+0.01%)` | :arrow_up: |
   | [parquet/src/arrow/arrow\_reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYXJyb3dfcmVhZGVyLnJz) | `93.57% <0.00%> (+0.38%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [aa71aea...4e9197c](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800193192



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_offset: i64,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_offset = -(chunks.lead_padding() as i64);
+
+        Self {
+            current_chunk,
+            chunk_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some((self.chunk_offset + bit_pos as i64) as usize);
+            }
+
+            // Must panic if exhausted early as trusted length iterator
+            self.current_chunk = self.iter.next().expect("IndexIterator exhausted early");
+            self.chunk_offset += 64;
+        }
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.remaining, Some(self.remaining))
+    }
+}
+
+/// Counts the number of set bits in `filter`
 fn filter_count(filter: &BooleanArray) -> usize {

Review comment:
       No, this is counting the number of set bits in the BooleanArray's values, not the nulls (which should not exist at this point)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] sunchao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
sunchao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800221300



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);

Review comment:
       For the special case where `count` is equal to the length of original array (i.e., all values are selected), is there a faster path?

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]

Review comment:
       `keep most / all rows`? 

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {

Review comment:
       Not quite related to this PR, but I'm wondering that in the case there are multiple predicates on the same input array, maybe it's beneficial to only materialize the final output array after all the predicates have been evaluated. It seems we don't have such API yet?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jhorstmann commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jhorstmann commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799455292



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
 
-    match filter_count {
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    // The filtered `nulls` has a length of `predicate.count` bits and
+    // therefore the null count is this minus the number of valid bits
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
+
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
         }
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
 
-    let num_columns = record_batch.columns().len();
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);

Review comment:
       I see a tiny improvement locally, but you are right, the branch should be perfectly predictable. Might not be worth any potential soundness discussions.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jorgecarleitao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800194301



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_offset: i64,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_offset = -(chunks.lead_padding() as i64);
+
+        Self {
+            current_chunk,
+            chunk_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some((self.chunk_offset + bit_pos as i64) as usize);
+            }
+
+            // Must panic if exhausted early as trusted length iterator
+            self.current_chunk = self.iter.next().expect("IndexIterator exhausted early");
+            self.chunk_offset += 64;
+        }
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.remaining, Some(self.remaining))
+    }
+}
+
+/// Counts the number of set bits in `filter`
 fn filter_count(filter: &BooleanArray) -> usize {

Review comment:
       check, thanks!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799526637



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_end_offset: usize,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_end_offset = 64 - chunks.lead_padding();
+
+        Self {
+            current_chunk,
+            chunk_end_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some(self.chunk_end_offset + (bit_pos as usize) - 64);

Review comment:
       if `chunk_end_offset`  is initialized to just  ` - chunks.lead_padding()` (instead of `64 - chunks.lead_padding()`, then wouldn't it be unnecessary to subtract `64` here? In other words at the moment we have `64 - chunks.lead_padding() + (bit_pos as usize) - 64` where the 64 is cancelled out by -64 so  `- chunks.lead_padding() + (bit_pos as usize) ` should be the same, shouldn't it? some comments might be necessary to explain why / how it works though




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799822480



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator

Review comment:
       Renamed to `IterationStrategy`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801080970



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,600 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let strategy = IterationStrategy::default_strategy(filter.len(), count);
+
+        Self {
+            filter,
+            count,
+            strategy,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.strategy {
+            IterationStrategy::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.strategy = IterationStrategy::Slices(slices)
+            }
+            IterationStrategy::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.strategy = IterationStrategy::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
-        0 => {
-            // return empty
-            Ok(new_empty_array(array.data_type()))
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            strategy: self.strategy,
         }
-        len if len == array.len() => {
-            // return all
-            let data = array.data().clone();
-            Ok(make_array(data))
+    }
+}
+
+/// The iteration strategy used to evaluate [`FilterPredicate`]
+#[derive(Debug)]
+enum IterationStrategy {
+    /// A lazily evaluated iterator of ranges
+    SlicesIterator,
+    /// A lazily evaluated iterator of indices
+    IndexIterator,
+    /// A precomputed list of indices
+    Indices(Vec<usize>),
+    /// A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+    /// Select all rows
+    All,
+    /// Select no rows
+    None,
+}
+
+impl IterationStrategy {
+    /// The default [`IterationStrategy`] for a filter of length `filter_length`
+    /// and selecting `filter_count` rows
+    fn default_strategy(filter_length: usize, filter_count: usize) -> Self {
+        if filter_length == 0 || filter_count == 0 {
+            return IterationStrategy::None;
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+        if filter_count == filter_length {
+            return IterationStrategy::All;
+        }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+        // Compute the selectivity of the predicate by dividing the number of true
+        // bits in the predicate by the predicate's total length
+        //
+        // This can then be used as a heuristic for the optimal iteration strategy
+        let selectivity_frac = filter_count as f64 / filter_length as f64;
+        if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            return IterationStrategy::SlicesIterator;
         }
+        IterationStrategy::IndexIterator
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
     }
+}
 
-    let num_columns = record_batch.columns().len();
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    match predicate.strategy {
+        IterationStrategy::None => Ok(new_empty_array(values.data_type())),
+        IterationStrategy::All => Ok(make_array(values.data().slice(0, predicate.count))),
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                match &predicate.strategy {
+                    IterationStrategy::Slices(slices) => {
+                        slices
+                            .iter()
+                            .for_each(|(start, end)| mutable.extend(0, *start, *end));
+                    }
+                    _ => {
+                        let iter = SlicesIterator::new(&predicate.filter);
+                        iter.for_each(|(start, end)| mutable.extend(0, start, end));

Review comment:
       In short because `MutableArrayData` doesn't support other iteration strategies and I'm not sure that calling it with "ranges" of length 1 will outperform at least attempting to call it with larger ranges.
   
   My 2 cents is that the eventual goal should be to remove this fallback, as `MutableArrayData` is a poor fit for filtering where the ranges are typically not substantial enough to amortize its per-range overheads effectively




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (d587a46) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.00%`.
   > The diff coverage is `75.25%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.99%   -0.01%     
   ==========================================
     Files         180      180              
     Lines       52288    52532     +244     
   ==========================================
   + Hits        43396    43598     +202     
   - Misses       8892     8934      +42     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `85.07% <74.57%> (-7.43%)` | :arrow_down: |
   | [arrow/src/buffer/mutable.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2J1ZmZlci9tdXRhYmxlLnJz) | `89.82% <100.00%> (+0.35%)` | :arrow_up: |
   | [parquet/src/encodings/encoding.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvZW5jb2RpbmdzL2VuY29kaW5nLnJz) | `93.52% <0.00%> (-0.20%)` | :arrow_down: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `66.21% <0.00%> (ø)` | |
   | [...ntegration-testing/src/bin/arrow-file-to-stream.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWZpbGUtdG8tc3RyZWFtLnJz) | `0.00% <0.00%> (ø)` | |
   | [...ion-testing/src/bin/arrow-json-integration-test.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWpzb24taW50ZWdyYXRpb24tdGVzdC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-client.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLWNsaWVudC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-server.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLXNlcnZlci5ycw==) | `0.00% <0.00%> (ø)` | |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.77% <0.00%> (+0.12%)` | :arrow_up: |
   | ... and [1 more](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...d587a46](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799381989



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();

Review comment:
       why is it necessary to pass `self.count` into the `IndexIterator` and not into the `SlicesIterator`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798959128



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -696,7 +1273,8 @@ mod tests {
             .flat_map(|(idx, v)| v.then(|| idx))
             .collect();
 
-        assert_eq!(bits, expected_bits);
+        assert_eq!(slice_bits, expected_bits);
+        assert_eq!(index_bits, expected_bits);
     }
 
     #[test]

Review comment:
       Eeh... I'm fairly confident, but more tests couldn't hurt. I'll see what I can come up with




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jhorstmann commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jhorstmann commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799333423



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
 
-    match filter_count {
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    // The filtered `nulls` has a length of `predicate.count` bits and
+    // therefore the null count is this minus the number of valid bits
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
+
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
         }
     }
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
 
-    let num_columns = record_batch.columns().len();
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);

Review comment:
       Are the indices from these iterators trusted? If so we could probably gain a few more percents of performance by using `get_unchecked` here.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799350951



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;

Review comment:
       out of curiosity though, have you measured what's the impact on performance (of choosing slices or index iterator at runtime)? I suspect that the benchmarks that filter out half of the data will be most affected / improved? possibly very selective filters (that filter out most of the data) will see improvement as well?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1026130732


   this looks very promising @tustvold - excellent performance improvement; when I made the filter kernel up to 550 times faster in https://github.com/apache/arrow-rs/commit/457ea62f9210d8a31e5fcd467f28a4ce215c99a7 I was wondering if this was the limit of performance or it could be made even faster, and I am very happy to see that you have been able to reach a new level of performance for the filter kernel


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1026166515


   > 550 times faster
   
   Damn, that's some improvement :laughing: 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jhorstmann commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jhorstmann commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1026879011


   > What is interesting, at least to me, is the performance tax imposed by the packed bitmask representation of BooleanArray - even with non-trivial bit-twiddling shenanigans it still appears to be more performant to hydrate the filter to an array of indices or slices when filtering multiple arrays.
   
   On recent intel machines there would be a much faster way using the [pext](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_pext_u64&ig_expand=5394) instruction. That basically implements a filter for 64 bits at a time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798959128



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -696,7 +1273,8 @@ mod tests {
             .flat_map(|(idx, v)| v.then(|| idx))
             .collect();
 
-        assert_eq!(bits, expected_bits);
+        assert_eq!(slice_bits, expected_bits);
+        assert_eq!(index_bits, expected_bits);
     }
 
     #[test]

Review comment:
       Eeh... I'm fairly confident, but more tests couldn't hurt. I'll see what I can come up with
   
   Edit: will do this first thing tomorrow - my brain is a little bit :fried_egg: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799021711



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -696,7 +1273,8 @@ mod tests {
             .flat_map(|(idx, v)| v.then(|| idx))
             .collect();
 
-        assert_eq!(bits, expected_bits);
+        assert_eq!(slice_bits, expected_bits);
+        assert_eq!(index_bits, expected_bits);
     }
 
     #[test]

Review comment:
       Added a fuzz test in https://github.com/apache/arrow-rs/pull/1248/commits/d587a46932412806cdcd2cd9d66c94a512b4e834




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799643564



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
+    }
+}
+
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);

Review comment:
       if the filter predicate has been optimized and contains a filter of type `Slices`, why not use that here instead of creating a new `SlicesIterator` and calculating the slices again? otherwise the optimization function is useless here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799526637



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_end_offset: usize,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_end_offset = 64 - chunks.lead_padding();
+
+        Self {
+            current_chunk,
+            chunk_end_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some(self.chunk_end_offset + (bit_pos as usize) - 64);

Review comment:
       if `chunk_end_offset`  is initialized to just  ` - chunks.lead_padding()` (instead of `64 - chunks.lead_padding()`, then wouldn't it be unnecessary to subtract `64` here? In other words at the moment we have `64 - chunks.lead_padding() + (bit_pos as usize) - 64` where the 64 is cancelled out by -64 so  `- chunks.lead_padding() + (bit_pos as usize) ` should be the same, shouldn't it?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799817101



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_end_offset: usize,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_end_offset = 64 - chunks.lead_padding();
+
+        Self {
+            current_chunk,
+            chunk_end_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some(self.chunk_end_offset + (bit_pos as usize) - 64);

Review comment:
       Yeah - this is just to avoid using signed indexes. I've benchmarked the change and it makes no difference to performance, but I would rather keep using `usize` for consistency. I'll add some clarifying comments




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800046847



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,580 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))

Review comment:
       Good shout, I double checked and the cost of "optimizing" the filter, is of a similar magnitude to that of the primitive array copies, and so it will be faster to not "optimize" for a single array. Will fix :+1:




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799822311



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
+    }
+}
+
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);

Review comment:
       Pure oversight - fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800045171



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,580 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))

Review comment:
       Does it make sense to include a condition for when we have 1 column to filter (and use the normal `filter` function instead).
   I made a PR to arrow2 some time ago as it saves some unnecessary work for when we only need to filter a single column.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jorgecarleitao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800191885



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_offset: i64,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_offset = -(chunks.lead_padding() as i64);
+
+        Self {
+            current_chunk,
+            chunk_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some((self.chunk_offset + bit_pos as i64) as usize);
+            }
+
+            // Must panic if exhausted early as trusted length iterator
+            self.current_chunk = self.iter.next().expect("IndexIterator exhausted early");
+            self.chunk_offset += 64;
+        }
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.remaining, Some(self.remaining))
+    }
+}
+
+/// Counts the number of set bits in `filter`
 fn filter_count(filter: &BooleanArray) -> usize {

Review comment:
       not sure why this was/here here, but isn't this equal to `filter.len() - filter.null_count()`?
   
   EDIT: ah, maybe this is not true because null_count does not take the offsets into account?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jorgecarleitao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800191885



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_offset: i64,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_offset = -(chunks.lead_padding() as i64);
+
+        Self {
+            current_chunk,
+            chunk_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some((self.chunk_offset + bit_pos as i64) as usize);
+            }
+
+            // Must panic if exhausted early as trusted length iterator
+            self.current_chunk = self.iter.next().expect("IndexIterator exhausted early");
+            self.chunk_offset += 64;
+        }
+        None
+    }
+
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (self.remaining, Some(self.remaining))
+    }
+}
+
+/// Counts the number of set bits in `filter`
 fn filter_count(filter: &BooleanArray) -> usize {

Review comment:
       not sure why this was here, but isn't this equal to `filter.len() - filter.null_count()`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] sunchao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
sunchao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800227777



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {

Review comment:
       Yea that's what I have in mind. I'm thinking that for a chain of ANDs maybe we don't need to evaluate them separately but rather evaluate on the result of the previous one, and skip materialization only until the last predicate (or short-circuit if all values are filtered out before that)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030909117


   > the specialization of the mutableArrayData that offers the performance
   
   Yeah, setting `FILTER_SLICES_SELECTIVITY_THRESHOLD` to `0`, i.e. forcing slice iteration, shows a roughly 2-3x performance improvement on low selectivity filters solely from moving away from `MutableArrayData`. This isn't all that surprising considering each range is 2 dynamic dispatches :sweat_smile: The remaining 5x comes from the iteration strategy. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799061079



##########
File path: arrow/src/buffer/mutable.rs
##########
@@ -475,35 +475,36 @@ impl MutableBuffer {
         let (_, upper) = iterator.size_hint();
         let upper = upper.expect("from_trusted_len_iter requires an upper limit");
 
-        let mut result = {
-            let byte_capacity: usize = upper.saturating_add(7) / 8;
-            MutableBuffer::new(byte_capacity)
-        };
+        let aligned_len = bit_util::ceil(upper, 64) * 8;
+        let mut result = MutableBuffer::new(aligned_len);
 
         'a: loop {
-            let mut byte_accum: u8 = 0;
-            let mut mask: u8 = 1;
+            let mut accum: u64 = 0;

Review comment:
       that's interesting - I am curious @tustvold  have you measured the impact just from this change (from u8 to u64 accumulator) ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799312437



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       Yeah, highly selective means low selectivity. Selectivity is a measure of how many rows it selects, selective-ness is a measure of how few rows it selects. Or something like that... This terminology is still new to me :smile: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798943316



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),

Review comment:
       This is a direct port of the existing logic, if the filter contains nulls, we convert all nulls to false, so that we don't have to worry about them




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798959128



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -696,7 +1273,8 @@ mod tests {
             .flat_map(|(idx, v)| v.then(|| idx))
             .collect();
 
-        assert_eq!(bits, expected_bits);
+        assert_eq!(slice_bits, expected_bits);
+        assert_eq!(index_bits, expected_bits);
     }
 
     #[test]

Review comment:
       Eeh... I'm fairly confident, but more tests couldn't hurt. I'll see what I can come up with




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025171943


   This is really cool and promising!. In my experience the filter kernels can be quite expensive in benchmarks. Great stuff 😃


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800226976



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]

Review comment:
       Is this a comment on the formatting?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799817101



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]
+struct IndexIterator<'a> {
+    current_chunk: u64,
+    chunk_end_offset: usize,
+    remaining: usize,
+    iter: UnalignedBitChunkIterator<'a>,
+}
+
+impl<'a> IndexIterator<'a> {
+    fn new(filter: &'a BooleanArray, len: usize) -> Self {
+        assert_eq!(filter.null_count(), 0);
+        let data = filter.data();
+        let chunks =
+            UnalignedBitChunk::new(&data.buffers()[0], data.offset(), data.len());
+        let mut iter = chunks.iter();
+
+        let current_chunk = iter.next().unwrap_or(0);
+        let chunk_end_offset = 64 - chunks.lead_padding();
+
+        Self {
+            current_chunk,
+            chunk_end_offset,
+            remaining: len,
+            iter,
+        }
+    }
+}
+
+impl<'a> Iterator for IndexIterator<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        while self.remaining != 0 {
+            if self.current_chunk != 0 {
+                let bit_pos = self.current_chunk.trailing_zeros();
+                self.current_chunk ^= 1 << bit_pos;
+                self.remaining -= 1;
+                return Some(self.chunk_end_offset + (bit_pos as usize) - 64);

Review comment:
       Yeah - this is just to avoid using signed indexes. I've benchmarked the change and it makes no difference to performance, but I would rather keep using `usize` for consistency. I'll add some clarifying comments.
   
   Edit: Scratch that - I messed up the benchmark. This yields a 5% additional bump - nice :+1: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jorgecarleitao commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jorgecarleitao commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030785901


   Last time I benched arrow2 was 5x faster than arrow ([results here](https://docs.google.com/spreadsheets/d/1hLKsqJaw_VLjtJCgQ635R9iHDNYwZscE0OT1omdZuwg/edit#gid=488524223)), so this most likely beats it :)
   
   I haven't had the time to go through this in detail, but the approach is very well though. Kudos to @tustvold !


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800229172



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {

Review comment:
       Definitely something to measure. I think it would be hard to beat a strategy that materializes very selective predicates and otherwise just evaluates predicates against already "eliminated" rows. A masked comparison kernel, I'd expect to have non-trivial branching overheads, but I honestly don't know :smile: I guess it also depends on how expensive the predicate is to evaluate, a complex cast might tip the scales... :thinking: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] jhorstmann commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
jhorstmann commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1026873475


   To get a good feeling for how close we are to the theoretical peak performance it could be useful to add throughput numbers to the criterion benchmark. When criterion know the number of bytes that are processed (size of the primitive array + size of filter bitmap), it can report the results in Gb/s, which can be compared to the max single-threaded memory bandwidth of the machine, which is usually between 10-15Gb/s. When the arrays fit into L1 cache it can be even higher.
   
   The filter benchmarks are also a bit focused on bytes, I don't know how common those are in DataFusion, but I would think that 32 or 64 bit numbers are much more common and should be the focus of benchmarking.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1026921010


   > The filter benchmarks are also a bit focused on bytes, I don't know how common those are in DataFusion, but I would think that 32 or 64 bit numbers are much more common and should be the focus of benchmarking.
   
   Yeah, the f32 benchmarks are encouraging that the performance uplift isn't just a quirk of the fact the benchmarks currently use u8 for integers.
   
   > On recent intel machines there would be a much faster way using the pext instruction. That basically implements a filter for 64 bits at a time.
   
   Yeah, I do not doubt some artisanal use of intrinsics could likely eliminate much of the cost of bitmasks, perhaps even surpassing indices. I had I guess hoped that it would be possible to get further before resorting to the arcane :laughing:  


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030385108


   Thank you all for taking the time to review this, I think I've incorporated all feedback, but let me know if I've missed something :sweat_smile: 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] viirya commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
viirya commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1031948147


   A great improvement! Looking forward to have this enhancement.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800213190



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;

Review comment:
       @tustvold have you experimented with other values of `FILTER_SLICES_SELECTIVITY_THRESHOLD` ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] Dandandan edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
Dandandan edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030782311


   FYI @jorgecarleitao might be good to compare performance to arrow2 after those changes?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] sunchao commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
sunchao commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1030895267


   Great work @tustvold ! really looking forward to this improvement!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799629375



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator

Review comment:
       here `FilterIterator` looks more like `FilterIteratorType`, as in this is not the actual iterator




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (395f6b3) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.01%`.
   > The diff coverage is `74.92%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.98%   -0.02%     
   ==========================================
     Files         180      180              
     Lines       52288    52552     +264     
   ==========================================
   + Hits        43396    43608     +212     
   - Misses       8892     8944      +52     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `84.57% <74.92%> (-7.93%)` | :arrow_down: |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `65.98% <0.00%> (-0.23%)` | :arrow_down: |
   | [parquet/src/encodings/encoding.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvZW5jb2RpbmdzL2VuY29kaW5nLnJz) | `93.52% <0.00%> (-0.20%)` | :arrow_down: |
   | [...ntegration-testing/src/bin/arrow-file-to-stream.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWZpbGUtdG8tc3RyZWFtLnJz) | `0.00% <0.00%> (ø)` | |
   | [...ion-testing/src/bin/arrow-json-integration-test.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWpzb24taW50ZWdyYXRpb24tdGVzdC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-client.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLWNsaWVudC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-server.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLXNlcnZlci5ycw==) | `0.00% <0.00%> (ø)` | |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.77% <0.00%> (+0.12%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...395f6b3](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800226817



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {

Review comment:
       If you had multiple predicates, I presume you'd evaluate them separately and then `AND` the filters together to get the final filter to "materialize". I'm not sure if this is what you had in mind?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] viirya commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801063167



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,24 +17,60 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;
+
+macro_rules! downcast_filter {
+    ($type: ty, $values: expr, $filter: expr) => {{
+        let values = $values
+            .as_any()
+            .downcast_ref::<PrimitiveArray<$type>>()
+            .expect("Unable to downcast to a primitive array");
+
+        Ok(Arc::new(filter_primitive::<$type>(&values, $filter)))
+    }};
+}
+
+macro_rules! downcast_dict_filter {
+    ($type: ty, $values: expr, $filter: expr) => {{
+        let values = $values
+            .as_any()
+            .downcast_ref::<DictionaryArray<$type>>()
+            .expect("Unable to downcast to a dictionary array");
+        Ok(Arc::new(filter_dict::<$type>(values, $filter)))
+    }};
+}
 
-/// An iterator of `(usize, usize)` each representing an interval `[start,end[` whose
-/// slots of a [BooleanArray] are true. Each interval corresponds to a contiguous region of memory to be
-/// "taken" from an array to be filtered.
+/// An iterator of `(usize, usize)` each representing an interval `[start, end]` whose
+/// slots of a [BooleanArray] are true. Each interval corresponds to a contiguous region of memory
+/// to be "taken" from an array to be filtered.
+///
+/// This is only performant for the least selective filters that copy across long contiguous runs

Review comment:
       ditto, I think this should be highly selective filters?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799648071



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
 
-    match filter_count {
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
+    }
+}
+
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => {
+                    unimplemented!("Filter not supported for dictionary key type {:?}", t)
+                }
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result

Review comment:
       I think it would be useful to explain what is the returned value if there are nulls as well e.g. (null_count, null_buffer), otherwise one has to read the code to figure that out




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] alamb commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1034189941


   Thank you to everyone who contributed reviews and comments to this PR and thank you again to @tustvold for sticking with this!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (3241417) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.00%`.
   > The diff coverage is `75.65%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.98%   -0.01%     
   ==========================================
     Files         180      180              
     Lines       52288    52545     +257     
   ==========================================
   + Hits        43396    43606     +210     
   - Misses       8892     8939      +47     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `85.40% <75.65%> (-7.10%)` | :arrow_down: |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [parquet/src/encodings/encoding.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvZW5jb2RpbmdzL2VuY29kaW5nLnJz) | `93.52% <0.00%> (-0.20%)` | :arrow_down: |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.51% <0.00%> (-0.13%)` | :arrow_down: |
   | [...ntegration-testing/src/bin/arrow-file-to-stream.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWZpbGUtdG8tc3RyZWFtLnJz) | `0.00% <0.00%> (ø)` | |
   | [...ion-testing/src/bin/arrow-json-integration-test.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWpzb24taW50ZWdyYXRpb24tdGVzdC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-client.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLWNsaWVudC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-server.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLXNlcnZlci5ycw==) | `0.00% <0.00%> (ø)` | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...3241417](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799362918



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;

Review comment:
       I have not experimented with anything more sophisticated than the basic switch based on filter selectivity, it would definitely be cool to be smarter about this though, I just lacked inspiration :smile: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r795106504



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -244,6 +419,110 @@ pub fn filter_record_batch(
     RecordBatch::try_new(record_batch.schema(), filtered_arrays)
 }
 
+fn filter_null_mask(
+    data: &ArrayData,
+    filter: &BooleanArray,
+    filter_count: usize,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), filter, filter_count);
+    let null_count = filter_count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+fn filter_bits(
+    buffer: &Buffer,
+    offset: usize,
+    filter: &BooleanArray,
+    filter_count: usize,
+) -> Buffer {
+    let src = buffer.as_slice();
+
+    // TODO: Optimise this
+    let mut buf = MutableBuffer::from_len_zeroed(bit_util::ceil(filter_count, 8));
+    let dst = buf.as_slice_mut();
+
+    for (dst_idx, src_idx) in IndexIterator::new(filter).enumerate() {
+        if bit_util::get_bit(src, src_idx + offset) {
+            bit_util::set_bit(dst, dst_idx);
+        }
+    }
+
+    buf.into()
+}
+
+/// `filter` implementation for boolean buffers
+fn filter_boolean(
+    values: &BooleanArray,
+    filter: &BooleanArray,
+    filter_count: usize,
+) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), filter, filter_count);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(filter_count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, filter, filter_count) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = builder.build().unwrap(); // TODO: unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    filter: &BooleanArray,
+    filter_count: usize,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+
+    let mut buffer = MutableBuffer::with_capacity(filter_count * T::get_byte_width());
+
+    let selectivity_frac = filter_count as f64 / filter.len() as f64;

Review comment:
       Credit to @sunchao for the inspiration behind this hybrid approach - https://github.com/apache/arrow-rs/issues/1191#issuecomment-1023658537




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799323599



##########
File path: arrow/src/buffer/mutable.rs
##########
@@ -475,35 +475,36 @@ impl MutableBuffer {
         let (_, upper) = iterator.size_hint();
         let upper = upper.expect("from_trusted_len_iter requires an upper limit");
 
-        let mut result = {
-            let byte_capacity: usize = upper.saturating_add(7) / 8;
-            MutableBuffer::new(byte_capacity)
-        };
+        let aligned_len = bit_util::ceil(upper, 64) * 8;
+        let mut result = MutableBuffer::new(aligned_len);
 
         'a: loop {
-            let mut byte_accum: u8 = 0;
-            let mut mask: u8 = 1;
+            let mut accum: u64 = 0;

Review comment:
       I was sure this improved the benchmarks at one point, but it would appear to now make them worse :scream: 
   
   Great catch :+1: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1029832055






-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] alamb commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r801998604



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -1298,4 +1347,141 @@ mod tests {
         test_slices_fuzz(32, 8, 8);
         test_slices_fuzz(32, 5, 9);
     }
+
+    /// Filters `values` by `predicate` using standard rust iterators
+    fn filter_rust<T>(values: impl IntoIterator<Item = T>, predicate: &[bool]) -> Vec<T> {
+        values
+            .into_iter()
+            .zip(predicate)
+            .filter(|(_, x)| **x)
+            .map(|(a, _)| a)
+            .collect()
+    }
+
+    /// Generates an array of length `len` with `valid_percent` non-null values
+    fn gen_primitive<T>(len: usize, valid_percent: f64) -> Vec<Option<T>>
+    where
+        Standard: Distribution<T>,
+    {
+        let mut rng = thread_rng();
+        (0..len)
+            .map(|_| rng.gen_bool(valid_percent).then(|| rng.gen()))
+            .collect()
+    }
+
+    /// Generates an array of length `len` with `valid_percent` non-null values
+    fn gen_strings(
+        len: usize,
+        valid_percent: f64,
+        str_len_range: std::ops::Range<usize>,
+    ) -> Vec<Option<String>> {
+        let mut rng = thread_rng();
+        (0..len)
+            .map(|_| {
+                rng.gen_bool(valid_percent).then(|| {
+                    let len = rng.gen_range(str_len_range.clone());
+                    (0..len)
+                        .map(|_| char::from(rng.sample(Alphanumeric)))
+                        .collect()
+                })
+            })
+            .collect()
+    }
+
+    /// Returns an iterator that calls `Option::as_deref` on each item
+    fn as_deref<T: std::ops::Deref>(
+        src: &[Option<T>],
+    ) -> impl Iterator<Item = Option<&T::Target>> {
+        src.iter().map(|x| x.as_deref())
+    }
+
+    #[test]
+    fn fuzz_filter() {
+        let mut rng = thread_rng();
+
+        for i in 0..100 {
+            let filter_percent = match i {
+                0..=4 => 1.,
+                5..=10 => 0.,

Review comment:
       TIL `match` works on ranges!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (8ed556e) into [master](https://codecov.io/gh/apache/arrow-rs/commit/aa71aeaa3d6f0345690490588640226701e1ac15?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (aa71aea) will **increase** coverage by `0.02%`.
   > The diff coverage is `87.93%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   + Coverage   82.96%   82.98%   +0.02%     
   ==========================================
     Files         178      178              
     Lines       51522    51816     +294     
   ==========================================
   + Hits        42744    43000     +256     
   - Misses       8778     8816      +38     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `86.94% <81.97%> (-2.62%)` | :arrow_down: |
   | [arrow/src/util/bit\_chunk\_iterator.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYml0X2NodW5rX2l0ZXJhdG9yLnJz) | `93.47% <92.43%> (-4.33%)` | :arrow_down: |
   | [arrow/src/buffer/immutable.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2J1ZmZlci9pbW11dGFibGUucnM=) | `99.45% <100.00%> (+0.52%)` | :arrow_up: |
   | [parquet/src/arrow/bit\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYml0X3V0aWwucnM=) | `100.00% <100.00%> (ø)` | |
   | [arrow/src/datatypes/datatype.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9kYXRhdHlwZS5ycw==) | `66.38% <0.00%> (-0.43%)` | :arrow_down: |
   | [arrow/src/datatypes/field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9maWVsZC5ycw==) | `53.79% <0.00%> (-0.31%)` | :arrow_down: |
   | [parquet/src/encodings/encoding.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvZW5jb2RpbmdzL2VuY29kaW5nLnJz) | `93.52% <0.00%> (-0.20%)` | :arrow_down: |
   | [parquet\_derive/src/parquet\_field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldF9kZXJpdmUvc3JjL3BhcnF1ZXRfZmllbGQucnM=) | `66.21% <0.00%> (ø)` | |
   | [parquet/src/arrow/array\_reader.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cGFycXVldC9zcmMvYXJyb3cvYXJyYXlfcmVhZGVyLnJz) | `78.21% <0.00%> (+0.01%)` | :arrow_up: |
   | ... and [1 more](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [aa71aea...8ed556e](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: POC: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1028457980


   So I've added string and dictionary support. Strings only see a ~2x performance bump, this can almost certainly be pushed further, but I figured it was good enough and I can come back to it in subsequent PRs. Dictionaries see the full 9-10x bump that the integer types see which is :ok_hand: 
   
   I'm marking this ready for review because I think it represents a non-trivial improvement that would be super-awesome to make it into the arrow 9 release. There is definitely further refinement that could take place, and more perf that could be squeezed out, but I'd prefer to do that in subsequent PRs unless people object.
   
   For reference here are the latest numbers, down a little bit since #1228 was merged, but still pretty exciting imo
   
   ```
   filter optimize         time:   [45.686 us 45.709 us 45.739 us]                             
                           change: [-0.2448% -0.0653% +0.1098%] (p = 0.46 > 0.05)
                           No change in performance detected.
   
   filter optimize high selectivity                                                                             
                           time:   [1.7436 us 1.7447 us 1.7459 us]
                           change: [+3.2315% +3.3341% +3.4592%] (p = 0.00 < 0.05)
                           Performance has regressed.
   
   filter optimize low selectivity                                                                             
                           time:   [1.3802 us 1.3893 us 1.3972 us]
                           change: [-3.1413% -2.7720% -2.3950%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8               time:   [47.731 us 47.740 us 47.748 us]                       
                           change: [-85.383% -85.377% -85.371%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 high selectivity                                                                             
                           time:   [2.4163 us 2.4185 us 2.4210 us]
                           change: [-73.969% -73.939% -73.909%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter u8 low selectivity                                                                             
                           time:   [1.3798 us 1.3814 us 1.3829 us]
                           change: [-41.966% -41.649% -41.441%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8       time:   [14.557 us 14.567 us 14.580 us]                               
                           change: [-94.178% -94.175% -94.171%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 high selectivity                                                                             
                           time:   [1.1673 us 1.1678 us 1.1684 us]
                           change: [-84.915% -84.905% -84.894%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 low selectivity                                                                            
                           time:   [150.77 ns 150.80 ns 150.83 ns]
                           change: [-82.779% -82.758% -82.735%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32              time:   [68.835 us 68.857 us 68.882 us]                       
                           change: [-78.627% -78.613% -78.598%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32 high selectivity                                                                             
                           time:   [6.1117 us 6.1135 us 6.1154 us]
                           change: [-54.814% -54.788% -54.759%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter i32 low selectivity                                                                             
                           time:   [1.3835 us 1.3858 us 1.3895 us]
                           change: [-41.772% -41.682% -41.570%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32      time:   [16.049 us 16.058 us 16.068 us]                                
                           change: [-93.552% -93.545% -93.535%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 high selectivity                                                                             
                           time:   [4.7336 us 4.7417 us 4.7507 us]
                           change: [-59.764% -59.459% -58.934%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 low selectivity                                                                            
                           time:   [141.69 ns 141.83 ns 141.98 ns]
                           change: [-83.970% -83.929% -83.895%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 w NULLs                                                                             
                           time:   [42.514 us 42.529 us 42.548 us]
                           change: [-86.620% -86.613% -86.606%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context i32 w NULLs high selectivity                                                                             
                           time:   [10.785 us 10.788 us 10.790 us]
                           change: [+0.8230% +1.0819% +1.2557%] (p = 0.00 < 0.05)
                           Change within noise threshold.
   
   filter context i32 w NULLs low selectivity                                                                            
                           time:   [279.94 ns 280.04 ns 280.13 ns]
                           change: [-68.389% -68.317% -68.242%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs                                                                             
                           time:   [40.525 us 40.540 us 40.557 us]
                           change: [-87.204% -87.196% -87.187%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context u8 w NULLs high selectivity                                                                             
                           time:   [7.0863 us 7.0895 us 7.0928 us]
                           change: [+3.4115% +3.4779% +3.5553%] (p = 0.00 < 0.05)
                           Performance has regressed.
   
   filter context u8 w NULLs low selectivity                                                                            
                           time:   [270.69 ns 270.79 ns 270.89 ns]
                           change: [-67.961% -67.906% -67.849%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter f32              time:   [120.99 us 121.18 us 121.56 us]                       
                           change: [-68.315% -68.281% -68.241%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32      time:   [36.935 us 36.951 us 36.968 us]                                
                           change: [-88.072% -88.059% -88.045%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32 high selectivity                                                                             
                           time:   [10.339 us 10.342 us 10.346 us]
                           change: [-1.7648% -1.4694% -1.2565%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context f32 low selectivity                                                                            
                           time:   [287.59 ns 288.40 ns 289.16 ns]
                           change: [-67.322% -67.087% -66.921%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string   time:   [243.62 us 243.66 us 243.72 us]                                  
                           change: [-40.960% -40.903% -40.858%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string high selectivity                                                                            
                           time:   [75.645 us 75.664 us 75.682 us]
                           change: [-81.225% -81.211% -81.199%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string low selectivity                                                                             
                           time:   [666.91 ns 667.33 ns 667.80 ns]
                           change: [-42.717% -42.635% -42.556%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary                                                                             
                           time:   [16.348 us 16.354 us 16.359 us]
                           change: [-89.690% -89.684% -89.677%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary high selectivity                                                                              
                           time:   [4.8464 us 4.8530 us 4.8617 us]
                           change: [-83.162% -83.106% -83.056%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary low selectivity                                                                            
                           time:   [318.97 ns 319.14 ns 319.32 ns]
                           change: [-49.861% -49.819% -49.770%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs                                                                             
                           time:   [42.654 us 42.726 us 42.806 us]
                           change: [-87.775% -87.728% -87.657%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs high selectivity                                                                             
                           time:   [11.313 us 11.316 us 11.319 us]
                           change: [-68.181% -68.152% -68.130%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter context string dictionary w NULLs low selectivity                                                                            
                           time:   [480.59 ns 480.81 ns 481.04 ns]
                           change: [-54.683% -54.630% -54.579%] (p = 0.00 < 0.05)
                           Performance has improved.
   
   filter single record batch                                                                            
                           time:   [62.108 us 62.210 us 62.292 us]
                           change: [-80.890% -80.862% -80.837%] (p = 0.00 < 0.05)
                           Performance has improved.
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] alamb commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798754727



##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,12 +42,24 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
+    c.bench_function("filter optimize", |b| {

Review comment:
       maybe we should name the default ones like 
   
   ```suggestion
       c.bench_function("filter optimize (1/2)", |b| {
   ```
   
   To make it clear?

##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,12 +42,24 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
+    c.bench_function("filter optimize", |b| {
+        b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize high selectivity", |b| {
+        b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize low selectivity", |b| {

Review comment:
       ```suggestion
       c.bench_function("filter optimize low selectivity (1/1024)", |b| {
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();

Review comment:
       this seems to assume that the filter is always `false` for any null location (e.g. `prep_null_mask_filter` has been called). Is that guaranteed? Perhaps we can use a `debug_assert_eq!` style thing to check

##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,12 +42,24 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
+    c.bench_function("filter optimize", |b| {
+        b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize high selectivity", |b| {
+        b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize low selectivity", |b| {

Review comment:
       same suggestions for the other benchmarks

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       I think "least selective" means "high selectivity", confusingly

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),

Review comment:
       ```suggestion
                   t => unimplemented!("Filter not supported for dictionary key type {:?}", t),
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.

Review comment:
       ❤️ 

##########
File path: arrow/benches/filter_kernels.rs
##########
@@ -42,12 +42,24 @@ fn add_benchmark(c: &mut Criterion) {
     let dense_filter_array = create_boolean_array(size, 0.0, 1.0 - 1.0 / 1024.0);
     let sparse_filter_array = create_boolean_array(size, 0.0, 1.0 / 1024.0);
 
-    let filter = build_filter(&filter_array).unwrap();
-    let dense_filter = build_filter(&dense_filter_array).unwrap();
-    let sparse_filter = build_filter(&sparse_filter_array).unwrap();
+    let filter = FilterBuilder::new(&filter_array).optimize().build();
+    let dense_filter = FilterBuilder::new(&dense_filter_array).optimize().build();
+    let sparse_filter = FilterBuilder::new(&sparse_filter_array).optimize().build();
 
     let data_array = create_primitive_array::<UInt8Type>(size, 0.0);
 
+    c.bench_function("filter optimize", |b| {
+        b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
+    });
+
+    c.bench_function("filter optimize high selectivity", |b| {

Review comment:
       ```suggestion
       c.bench_function("filter optimize high selectivity (1023/1024)", |b| {
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),

Review comment:
       is the idea here that by setting the filter to `false` for rows that will be null in the output, that we skip copying array values which are null?

##########
File path: arrow/src/util/bench_util.rs
##########
@@ -110,6 +110,28 @@ pub fn create_string_array<Offset: StringOffsetSizeTrait>(
         .collect()
 }
 
+/// Creates an random (but fixed-seeded) array of a given size and null density

Review comment:
       ```suggestion
   /// Creates an random (but fixed-seeded) array of a given size and null density
   /// consisting of random 4 character alphanumeric strings
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       ```suggestion
   /// This provides the best performance on all but the least selective predicates (which keep most / all rows), where the
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {

Review comment:
       I think some rationale for the `0.8` number in some comments might be justified here. Using a named symbolic constant like `FILTER_SLICES_SELECTIVTY_THRESHOLD` might also help document the rationale some more

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///

Review comment:
       I see -- this effectively materializes the list of `usize` as a `Vec` 👍 

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -696,7 +1273,8 @@ mod tests {
             .flat_map(|(idx, v)| v.then(|| idx))
             .collect();
 
-        assert_eq!(bits, expected_bits);
+        assert_eq!(slice_bits, expected_bits);
+        assert_eq!(index_bits, expected_bits);
     }
 
     #[test]

Review comment:
       Do you feel good about the level of testing in this module (specifically with filters of selectivity 0.8 and higher / lower)?

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,

Review comment:
       I wonder if using `GenericStringBuilder<>` here would be possible

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +147,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the most selective predicates, where the

Review comment:
       Is it also worth mentioning that it requires the filter to be entirely non-null?

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut offsets = MutableBuffer::new(bytes_offset);
+        let values = MutableBuffer::new(0);
+        let cur_offset = OffsetSize::zero();
+        offsets.push(cur_offset);
+
+        Self {
+            src_offsets: array.value_offsets(),
+            src_values: &array.data().buffers()[1],
+            dst_offsets: offsets,
+            dst_values: values,
+            cur_offset,
+        }
+    }
+
+    /// Returns the byte offset at `idx`
+    #[inline]
+    fn get_value_offset(&self, idx: usize) -> usize {
+        self.src_offsets[idx].to_usize().expect("illegal offset")
     }
 
-    let num_columns = record_batch.columns().len();
+    /// Returns the start and end of the value at index `idx` along with its length
+    #[inline]
+    fn get_value_range(&self, idx: usize) -> (usize, usize, OffsetSize) {
+        // These can only fail if `array` contains invalid data
+        let start = self.get_value_offset(idx);
+        let end = self.get_value_offset(idx + 1);
+        let len = OffsetSize::from_usize(end - start).expect("illegal offset range");
+        (start, end, len)
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    /// Extends the in-progress array by the indexes in the provided iterator
+    fn extend_idx(&mut self, iter: impl Iterator<Item = usize>) {
+        for idx in iter {
+            let (start, end, len) = self.get_value_range(idx);
+            self.cur_offset += len;
+            self.dst_offsets.push(self.cur_offset);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+    }
+
+    /// Extends the in-progress array by the ranges in the provided iterator
+    fn extend_slices(&mut self, iter: impl Iterator<Item = (usize, usize)>) {
+        for slice in iter {
+            // These can only fail if `array` contains invalid data
+            for idx in slice.0..slice.1 {
+                let (_, _, len) = self.get_value_range(idx);
+                self.cur_offset += len;
+                self.dst_offsets.push(self.cur_offset); // push_unchecked?
+            }
+
+            let start = self.get_value_offset(slice.0);
+            let end = self.get_value_offset(slice.1);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
+    }
+}
+
+/// `filter` implementation for string arrays
+///
+/// Note: NULLs with a non-zero slot length in `array` will have the corresponding
+/// data copied across. This allows handling the null mask separately from the data

Review comment:
       I thought this maybe was handled by applying `prep_null_mask_filter` to the filter first?

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut offsets = MutableBuffer::new(bytes_offset);

Review comment:
       ```suggestion
           let mut dst_offsets = MutableBuffer::new(bytes_offset);
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();

Review comment:
       ```suggestion
           let num_offsets = (capacity + 1) * std::mem::size_of::<OffsetSize>();
   ```

##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut offsets = MutableBuffer::new(bytes_offset);
+        let values = MutableBuffer::new(0);
+        let cur_offset = OffsetSize::zero();
+        offsets.push(cur_offset);
+
+        Self {
+            src_offsets: array.value_offsets(),
+            src_values: &array.data().buffers()[1],
+            dst_offsets: offsets,
+            dst_values: values,
+            cur_offset,
+        }
+    }
+
+    /// Returns the byte offset at `idx`
+    #[inline]
+    fn get_value_offset(&self, idx: usize) -> usize {
+        self.src_offsets[idx].to_usize().expect("illegal offset")
     }
 
-    let num_columns = record_batch.columns().len();
+    /// Returns the start and end of the value at index `idx` along with its length
+    #[inline]
+    fn get_value_range(&self, idx: usize) -> (usize, usize, OffsetSize) {
+        // These can only fail if `array` contains invalid data
+        let start = self.get_value_offset(idx);
+        let end = self.get_value_offset(idx + 1);
+        let len = OffsetSize::from_usize(end - start).expect("illegal offset range");
+        (start, end, len)
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    /// Extends the in-progress array by the indexes in the provided iterator
+    fn extend_idx(&mut self, iter: impl Iterator<Item = usize>) {
+        for idx in iter {
+            let (start, end, len) = self.get_value_range(idx);
+            self.cur_offset += len;
+            self.dst_offsets.push(self.cur_offset);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+    }
+
+    /// Extends the in-progress array by the ranges in the provided iterator
+    fn extend_slices(&mut self, iter: impl Iterator<Item = (usize, usize)>) {
+        for slice in iter {
+            // These can only fail if `array` contains invalid data
+            for idx in slice.0..slice.1 {
+                let (_, _, len) = self.get_value_range(idx);
+                self.cur_offset += len;
+                self.dst_offsets.push(self.cur_offset); // push_unchecked?
+            }
+
+            let start = self.get_value_offset(slice.0);
+            let end = self.get_value_offset(slice.1);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
+    }
+}
+
+/// `filter` implementation for string arrays
+///
+/// Note: NULLs with a non-zero slot length in `array` will have the corresponding
+/// data copied across. This allows handling the null mask separately from the data
+fn filter_string<OffsetSize>(
+    array: &GenericStringArray<OffsetSize>,
+    predicate: &FilterPredicate,
+) -> GenericStringArray<OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    let data = array.data();
+    assert_eq!(data.buffers().len(), 2);
+    assert_eq!(data.child_data().len(), 0);
+    let mut filter = FilterString::new(predicate.count, array);
+
+    match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            filter.extend_slices(SlicesIterator::new(&predicate.filter))
+        }
+        FilterIterator::Slices(slices) => filter.extend_slices(slices.iter().cloned()),
+        FilterIterator::IndexIterator => {
+            filter.extend_idx(IndexIterator::new(&predicate.filter, predicate.count))
+        }
+        FilterIterator::Indices(indices) => filter.extend_idx(indices.iter().cloned()),
+    }
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(filter.dst_offsets.into())
+        .add_buffer(filter.dst_values.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    GenericStringArray::from(data)
+}
+
+/// `filter` implementation for dictionaries
+fn filter_dict<T>(
+    array: &DictionaryArray<T>,
+    predicate: &FilterPredicate,
+) -> DictionaryArray<T>
+where
+    T: ArrowPrimitiveType,
+    T::Native: num::Num,
+{
+    let filtered_keys = filter_primitive::<T>(array.keys(), predicate);
+    let filtered_data = filtered_keys.data_ref();
+
+    let data = unsafe {
+        ArrayData::new_unchecked(
+            array.data_type().clone(),
+            filtered_keys.len(),

Review comment:
       why not use `filtered_data` `ArrayData` consistently  rather than intermixing the array and array data?
   
   ```suggestion
               filtered_data.len(),
   ```
   
   -- not that this is wrong I am just curious




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] yordan-pavlov commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
yordan-pavlov commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799350951



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -17,19 +17,55 @@
 
 //! Defines miscellaneous array kernels.
 
+use std::ops::AddAssign;
+use std::sync::Arc;
+
+use num::Zero;
+
+use TimeUnit::*;
+
 use crate::array::*;
-use crate::buffer::buffer_bin_and;
-use crate::datatypes::DataType;
-use crate::error::Result;
+use crate::buffer::{buffer_bin_and, Buffer, MutableBuffer};
+use crate::datatypes::*;
+use crate::error::{ArrowError, Result};
 use crate::record_batch::RecordBatch;
 use crate::util::bit_chunk_iterator::{UnalignedBitChunk, UnalignedBitChunkIterator};
+use crate::util::bit_util;
 
-/// Function that can filter arbitrary arrays
-pub type Filter<'a> = Box<dyn Fn(&ArrayData) -> ArrayData + 'a>;
+/// If the filter selects more than this fraction of rows, use
+/// [`SlicesIterator`] to copy ranges of values. Otherwise iterate
+/// over individual rows using [`IndexIterator`]
+///
+/// Threshold of 0.8 chosen based on <https://dl.acm.org/doi/abs/10.1145/3465998.3466009>
+///
+const FILTER_SLICES_SELECTIVITY_THRESHOLD: f64 = 0.8;

Review comment:
       out of curiosity though, have you measured what's the impact on performance (of choosing slices or index iterator at runtime)? I suspect that the benchmarks that filter out half of the data will be most affected / improved?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] codecov-commenter edited a comment on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
codecov-commenter edited a comment on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1025014491


   # [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#1248](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1e7481a) into [master](https://codecov.io/gh/apache/arrow-rs/commit/1764399f828bce3e8f73df288fd98c6ce5ca110a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (1764399) will **decrease** coverage by `0.00%`.
   > The diff coverage is `75.63%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/arrow-rs/pull/1248/graphs/tree.svg?width=650&height=150&src=pr&token=pq9V9qWZ1N&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@            Coverage Diff             @@
   ##           master    #1248      +/-   ##
   ==========================================
   - Coverage   82.99%   82.99%   -0.01%     
   ==========================================
     Files         180      180              
     Lines       52288    52549     +261     
   ==========================================
   + Hits        43396    43611     +215     
   - Misses       8892     8938      +46     
   ```
   
   
   | [Impacted Files](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [arrow/src/util/bench\_util.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL3V0aWwvYmVuY2hfdXRpbC5ycw==) | `95.23% <ø> (ø)` | |
   | [arrow/src/compute/kernels/filter.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2NvbXB1dGUva2VybmVscy9maWx0ZXIucnM=) | `85.00% <75.63%> (-7.50%)` | :arrow_down: |
   | [arrow/src/array/transform/mod.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2FycmF5L3RyYW5zZm9ybS9tb2QucnM=) | `84.64% <0.00%> (ø)` | |
   | [...ntegration-testing/src/bin/arrow-file-to-stream.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWZpbGUtdG8tc3RyZWFtLnJz) | `0.00% <0.00%> (ø)` | |
   | [...ion-testing/src/bin/arrow-json-integration-test.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2Fycm93LWpzb24taW50ZWdyYXRpb24tdGVzdC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-client.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLWNsaWVudC5ycw==) | `0.00% <0.00%> (ø)` | |
   | [...-testing/src/bin/flight-test-integration-server.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-aW50ZWdyYXRpb24tdGVzdGluZy9zcmMvYmluL2ZsaWdodC10ZXN0LWludGVncmF0aW9uLXNlcnZlci5ycw==) | `0.00% <0.00%> (ø)` | |
   | [arrow/src/datatypes/field.rs](https://codecov.io/gh/apache/arrow-rs/pull/1248/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-YXJyb3cvc3JjL2RhdGF0eXBlcy9maWVsZC5ycw==) | `54.10% <0.00%> (+0.30%)` | :arrow_up: |
   
   ------
   
   [Continue to review full report at Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   > `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
   > Powered by [Codecov](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Last update [1764399...1e7481a](https://codecov.io/gh/apache/arrow-rs/pull/1248?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] sunchao commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
sunchao commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800227877



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -119,17 +155,83 @@ impl<'a> Iterator for SlicesIterator<'a> {
     }
 }
 
+/// An iterator of `usize` whose index in [`BooleanArray`] is true
+///
+/// This provides the best performance on all but the least selective predicates (which keep most
+/// / all rows), where the benefits of copying large runs instead favours [`SlicesIterator`]

Review comment:
       Oh sorry never mind. I see what it means now :)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r800232477



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,585 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let mut filter_builder = FilterBuilder::new(predicate);
+    if record_batch.num_columns() > 1 {
+        // Only optimize if filtering more than one column
+        filter_builder = filter_builder.optimize();
+    }
+    let filter = filter_builder.build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    strategy: IterationStrategy,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);

Review comment:
       Added in https://github.com/apache/arrow-rs/pull/1248/commits/2aa46b0743871924f59df2ad9c8071771e24bafc




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798951573



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();
+                self.iterator = FilterIterator::Indices(indices)
+            }
+            _ => {}
+        }
+        self
+    }
+
+    /// Construct the final `FilterPredicate`
+    pub fn build(self) -> FilterPredicate {
+        FilterPredicate {
+            filter: self.filter,
+            count: self.count,
+            iterator: self.iterator,
+        }
     }
+}
 
-    let filter_count = filter_count(predicate);
+/// The internal iterator type of [`FilterPredicate`]
+#[derive(Debug)]
+enum FilterIterator {
+    // A lazily evaluated iterator of ranges
+    SlicesIterator,
+    // A lazily evaluated iterator of indices
+    IndexIterator,
+    // A precomputed list of indices
+    Indices(Vec<usize>),
+    // A precomputed array of ranges
+    Slices(Vec<(usize, usize)>),
+}
+
+/// A filtering predicate that can be applied to an [`Array`]
+#[derive(Debug)]
+pub struct FilterPredicate {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
 
-    match filter_count {
+impl FilterPredicate {
+    /// Selects rows from `values` based on this [`FilterPredicate`]
+    pub fn filter(&self, values: &dyn Array) -> Result<ArrayRef> {
+        filter_array(values, self)
+    }
+}
+
+fn filter_array(values: &dyn Array, predicate: &FilterPredicate) -> Result<ArrayRef> {
+    if predicate.filter.len() > values.len() {
+        return Err(ArrowError::InvalidArgumentError(format!(
+            "Filter predicate of length {} is larger than target array of length {}",
+            predicate.filter.len(),
+            values.len()
+        )));
+    }
+
+    match predicate.count {
         0 => {
             // return empty
-            Ok(new_empty_array(array.data_type()))
+            Ok(new_empty_array(values.data_type()))
         }
-        len if len == array.len() => {
+        len if len == values.len() => {
             // return all
-            let data = array.data().clone();
+            let data = values.data().clone();
             Ok(make_array(data))
         }
-        _ => {
-            // actually filter
-            let mut mutable =
-                MutableArrayData::new(vec![array.data_ref()], false, filter_count);
+        // actually filter
+        _ => match values.data_type() {
+            DataType::Boolean => {
+                let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
+                Ok(Arc::new(filter_boolean(values, predicate)))
+            }
+            DataType::Int8 => {
+                downcast_filter!(Int8Type, values, predicate)
+            }
+            DataType::Int16 => {
+                downcast_filter!(Int16Type, values, predicate)
+            }
+            DataType::Int32 => {
+                downcast_filter!(Int32Type, values, predicate)
+            }
+            DataType::Int64 => {
+                downcast_filter!(Int64Type, values, predicate)
+            }
+            DataType::UInt8 => {
+                downcast_filter!(UInt8Type, values, predicate)
+            }
+            DataType::UInt16 => {
+                downcast_filter!(UInt16Type, values, predicate)
+            }
+            DataType::UInt32 => {
+                downcast_filter!(UInt32Type, values, predicate)
+            }
+            DataType::UInt64 => {
+                downcast_filter!(UInt64Type, values, predicate)
+            }
+            DataType::Float32 => {
+                downcast_filter!(Float32Type, values, predicate)
+            }
+            DataType::Float64 => {
+                downcast_filter!(Float64Type, values, predicate)
+            }
+            DataType::Date32 => {
+                downcast_filter!(Date32Type, values, predicate)
+            }
+            DataType::Date64 => {
+                downcast_filter!(Date64Type, values, predicate)
+            }
+            DataType::Time32(Second) => {
+                downcast_filter!(Time32SecondType, values, predicate)
+            }
+            DataType::Time32(Millisecond) => {
+                downcast_filter!(Time32MillisecondType, values, predicate)
+            }
+            DataType::Time64(Microsecond) => {
+                downcast_filter!(Time64MicrosecondType, values, predicate)
+            }
+            DataType::Time64(Nanosecond) => {
+                downcast_filter!(Time64NanosecondType, values, predicate)
+            }
+            DataType::Timestamp(Second, _) => {
+                downcast_filter!(TimestampSecondType, values, predicate)
+            }
+            DataType::Timestamp(Millisecond, _) => {
+                downcast_filter!(TimestampMillisecondType, values, predicate)
+            }
+            DataType::Timestamp(Microsecond, _) => {
+                downcast_filter!(TimestampMicrosecondType, values, predicate)
+            }
+            DataType::Timestamp(Nanosecond, _) => {
+                downcast_filter!(TimestampNanosecondType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::YearMonth) => {
+                downcast_filter!(IntervalYearMonthType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::DayTime) => {
+                downcast_filter!(IntervalDayTimeType, values, predicate)
+            }
+            DataType::Interval(IntervalUnit::MonthDayNano) => {
+                downcast_filter!(IntervalMonthDayNanoType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Second) => {
+                downcast_filter!(DurationSecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Millisecond) => {
+                downcast_filter!(DurationMillisecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Microsecond) => {
+                downcast_filter!(DurationMicrosecondType, values, predicate)
+            }
+            DataType::Duration(TimeUnit::Nanosecond) => {
+                downcast_filter!(DurationNanosecondType, values, predicate)
+            }
+            DataType::Utf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i32>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i32>(values, predicate)))
+            }
+            DataType::LargeUtf8 => {
+                let values = values
+                    .as_any()
+                    .downcast_ref::<GenericStringArray<i64>>()
+                    .unwrap();
+                Ok(Arc::new(filter_string::<i64>(values, predicate)))
+            }
+            DataType::Dictionary(key_type, _) => match key_type.as_ref() {
+                DataType::Int8 => downcast_dict_filter!(Int8Type, values, predicate),
+                DataType::Int16 => downcast_dict_filter!(Int16Type, values, predicate),
+                DataType::Int32 => downcast_dict_filter!(Int32Type, values, predicate),
+                DataType::Int64 => downcast_dict_filter!(Int64Type, values, predicate),
+                DataType::UInt8 => downcast_dict_filter!(UInt8Type, values, predicate),
+                DataType::UInt16 => downcast_dict_filter!(UInt16Type, values, predicate),
+                DataType::UInt32 => downcast_dict_filter!(UInt32Type, values, predicate),
+                DataType::UInt64 => downcast_dict_filter!(UInt64Type, values, predicate),
+                t => unimplemented!("Take not supported for dictionary key type {:?}", t),
+            },
+            _ => {
+                // fallback to using MutableArrayData
+                let mut mutable = MutableArrayData::new(
+                    vec![values.data_ref()],
+                    false,
+                    predicate.count,
+                );
+
+                let iter = SlicesIterator::new(&predicate.filter);
+                iter.for_each(|(start, end)| mutable.extend(0, start, end));
+
+                let data = mutable.freeze();
+                Ok(make_array(data))
+            }
+        },
+    }
+}
+
+/// Computes a new null mask for `data` based on `predicate`
+///
+/// Returns `None` if no nulls in the result
+fn filter_null_mask(
+    data: &ArrayData,
+    predicate: &FilterPredicate,
+) -> Option<(usize, Buffer)> {
+    if data.null_count() == 0 {
+        return None;
+    }
+
+    let nulls = filter_bits(data.null_buffer()?, data.offset(), predicate);
+    let null_count = predicate.count - nulls.count_set_bits();
+
+    if null_count == 0 {
+        return None;
+    }
+
+    Some((null_count, nulls))
+}
+
+/// Filter the packed bitmask `buffer`, with `predicate` starting at bit offset `offset`
+fn filter_bits(buffer: &Buffer, offset: usize, predicate: &FilterPredicate) -> Buffer {
+    let src = buffer.as_slice();
+
+    match &predicate.iterator {
+        FilterIterator::IndexIterator => {
+            let bits = IndexIterator::new(&predicate.filter, predicate.count)
+                .map(|src_idx| bit_util::get_bit(src, src_idx + offset));
+
+            // SAFETY: `IndexIterator` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::Indices(indices) => {
+            let bits = indices
+                .iter()
+                .map(|src_idx| bit_util::get_bit(src, *src_idx + offset));
 
-            let iter = SlicesIterator::new(predicate);
-            iter.for_each(|(start, end)| mutable.extend(0, start, end));
+            // SAFETY: `Vec::iter()` reports its size correctly
+            unsafe { MutableBuffer::from_trusted_len_iter_bool(bits).into() }
+        }
+        FilterIterator::SlicesIterator => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                builder.append_packed_range(start..end, src)
+            }
+            builder.finish()
+        }
+        FilterIterator::Slices(slices) => {
+            let mut builder =
+                BooleanBufferBuilder::new(bit_util::ceil(predicate.count, 8));
+            for (start, end) in slices {
+                builder.append_packed_range(*start..*end, src)
+            }
+            builder.finish()
+        }
+    }
+}
 
-            let data = mutable.freeze();
-            Ok(make_array(data))
+/// `filter` implementation for boolean buffers
+fn filter_boolean(values: &BooleanArray, predicate: &FilterPredicate) -> BooleanArray {
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = filter_bits(&data.buffers()[0], data.offset(), predicate);
+
+    let mut builder = ArrayDataBuilder::new(DataType::Boolean)
+        .len(predicate.count)
+        .add_buffer(values);
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    BooleanArray::from(data)
+}
+
+/// `filter` implementation for primitive arrays
+fn filter_primitive<T>(
+    values: &PrimitiveArray<T>,
+    predicate: &FilterPredicate,
+) -> PrimitiveArray<T>
+where
+    T: ArrowPrimitiveType,
+{
+    let data = values.data();
+    assert_eq!(data.buffers().len(), 1);
+    assert_eq!(data.child_data().len(), 0);
+
+    let values = data.buffer::<T::Native>(0);
+    assert!(values.len() >= predicate.filter.len());
+
+    let buffer = match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in SlicesIterator::new(&predicate.filter) {
+                buffer.extend_from_slice(&values[start..end]);
+            }
+            buffer
         }
+        FilterIterator::Slices(slices) => {
+            let mut buffer =
+                MutableBuffer::with_capacity(predicate.count * T::get_byte_width());
+            for (start, end) in slices {
+                buffer.extend_from_slice(&values[*start..*end]);
+            }
+            buffer
+        }
+        FilterIterator::IndexIterator => {
+            let iter =
+                IndexIterator::new(&predicate.filter, predicate.count).map(|x| values[x]);
+
+            // SAFETY: IndexIterator is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+        FilterIterator::Indices(indices) => {
+            let iter = indices.iter().map(|x| values[*x]);
+
+            // SAFETY: `Vec::iter` is trusted length
+            unsafe { MutableBuffer::from_trusted_len_iter(iter) }
+        }
+    };
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(buffer.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
     }
+
+    let data = unsafe { builder.build_unchecked() };
+    PrimitiveArray::from(data)
 }
 
-/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
-pub fn filter_record_batch(
-    record_batch: &RecordBatch,
-    predicate: &BooleanArray,
-) -> Result<RecordBatch> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter_record_batch(record_batch, &predicate);
+/// [`FilterString`] is created from a source [`GenericStringArray`] and can be
+/// used to build a new [`GenericStringArray`] by copying values from the source
+///
+/// TODO(raphael): Could this be used for the take kernel as well?
+struct FilterString<'a, OffsetSize> {
+    src_offsets: &'a [OffsetSize],
+    src_values: &'a [u8],
+    dst_offsets: MutableBuffer,
+    dst_values: MutableBuffer,
+    cur_offset: OffsetSize,
+}
+
+impl<'a, OffsetSize> FilterString<'a, OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    fn new(capacity: usize, array: &'a GenericStringArray<OffsetSize>) -> Self {
+        let bytes_offset = (capacity + 1) * std::mem::size_of::<OffsetSize>();
+        let mut offsets = MutableBuffer::new(bytes_offset);
+        let values = MutableBuffer::new(0);
+        let cur_offset = OffsetSize::zero();
+        offsets.push(cur_offset);
+
+        Self {
+            src_offsets: array.value_offsets(),
+            src_values: &array.data().buffers()[1],
+            dst_offsets: offsets,
+            dst_values: values,
+            cur_offset,
+        }
+    }
+
+    /// Returns the byte offset at `idx`
+    #[inline]
+    fn get_value_offset(&self, idx: usize) -> usize {
+        self.src_offsets[idx].to_usize().expect("illegal offset")
     }
 
-    let num_columns = record_batch.columns().len();
+    /// Returns the start and end of the value at index `idx` along with its length
+    #[inline]
+    fn get_value_range(&self, idx: usize) -> (usize, usize, OffsetSize) {
+        // These can only fail if `array` contains invalid data
+        let start = self.get_value_offset(idx);
+        let end = self.get_value_offset(idx + 1);
+        let len = OffsetSize::from_usize(end - start).expect("illegal offset range");
+        (start, end, len)
+    }
 
-    let filtered_arrays = match num_columns {
-        1 => {
-            vec![filter(record_batch.columns()[0].as_ref(), predicate)?]
+    /// Extends the in-progress array by the indexes in the provided iterator
+    fn extend_idx(&mut self, iter: impl Iterator<Item = usize>) {
+        for idx in iter {
+            let (start, end, len) = self.get_value_range(idx);
+            self.cur_offset += len;
+            self.dst_offsets.push(self.cur_offset);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
-        _ => {
-            let filter = build_filter(predicate)?;
-            record_batch
-                .columns()
-                .iter()
-                .map(|a| make_array(filter(a.data())))
-                .collect()
+    }
+
+    /// Extends the in-progress array by the ranges in the provided iterator
+    fn extend_slices(&mut self, iter: impl Iterator<Item = (usize, usize)>) {
+        for slice in iter {
+            // These can only fail if `array` contains invalid data
+            for idx in slice.0..slice.1 {
+                let (_, _, len) = self.get_value_range(idx);
+                self.cur_offset += len;
+                self.dst_offsets.push(self.cur_offset); // push_unchecked?
+            }
+
+            let start = self.get_value_offset(slice.0);
+            let end = self.get_value_offset(slice.1);
+            self.dst_values
+                .extend_from_slice(&self.src_values[start..end]);
         }
+    }
+}
+
+/// `filter` implementation for string arrays
+///
+/// Note: NULLs with a non-zero slot length in `array` will have the corresponding
+/// data copied across. This allows handling the null mask separately from the data
+fn filter_string<OffsetSize>(
+    array: &GenericStringArray<OffsetSize>,
+    predicate: &FilterPredicate,
+) -> GenericStringArray<OffsetSize>
+where
+    OffsetSize: Zero + AddAssign + StringOffsetSizeTrait,
+{
+    let data = array.data();
+    assert_eq!(data.buffers().len(), 2);
+    assert_eq!(data.child_data().len(), 0);
+    let mut filter = FilterString::new(predicate.count, array);
+
+    match &predicate.iterator {
+        FilterIterator::SlicesIterator => {
+            filter.extend_slices(SlicesIterator::new(&predicate.filter))
+        }
+        FilterIterator::Slices(slices) => filter.extend_slices(slices.iter().cloned()),
+        FilterIterator::IndexIterator => {
+            filter.extend_idx(IndexIterator::new(&predicate.filter, predicate.count))
+        }
+        FilterIterator::Indices(indices) => filter.extend_idx(indices.iter().cloned()),
+    }
+
+    let mut builder = ArrayDataBuilder::new(data.data_type().clone())
+        .len(predicate.count)
+        .add_buffer(filter.dst_offsets.into())
+        .add_buffer(filter.dst_values.into());
+
+    if let Some((null_count, nulls)) = filter_null_mask(data, predicate) {
+        builder = builder.null_count(null_count).null_bit_buffer(nulls);
+    }
+
+    let data = unsafe { builder.build_unchecked() };
+    GenericStringArray::from(data)
+}
+
+/// `filter` implementation for dictionaries
+fn filter_dict<T>(
+    array: &DictionaryArray<T>,
+    predicate: &FilterPredicate,
+) -> DictionaryArray<T>
+where
+    T: ArrowPrimitiveType,
+    T::Native: num::Num,
+{
+    let filtered_keys = filter_primitive::<T>(array.keys(), predicate);
+    let filtered_data = filtered_keys.data_ref();
+
+    let data = unsafe {
+        ArrayData::new_unchecked(
+            array.data_type().clone(),
+            filtered_keys.len(),

Review comment:
       Copy pasta, no particular reason :laughing: 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r798953803



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +279,559 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > 0.8 {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
+    }
+
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///

Review comment:
       Yup - it is what `Filter` did before but it can materialize as `Vec<usize>` or `Vec<(usize, usize)>` instead of only the latter before




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#issuecomment-1029832055


   I've found a bug handling null masks of offset data, fixing... 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [arrow-rs] tustvold commented on a change in pull request #1248: Specialized filter kernels

Posted by GitBox <gi...@apache.org>.
tustvold commented on a change in pull request #1248:
URL: https://github.com/apache/arrow-rs/pull/1248#discussion_r799382832



##########
File path: arrow/src/compute/kernels/filter.rs
##########
@@ -185,79 +287,564 @@ pub fn prep_null_mask_filter(filter: &BooleanArray) -> BooleanArray {
 /// # Ok(())
 /// # }
 /// ```
-pub fn filter(array: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
-    if predicate.null_count() > 0 {
-        // this greatly simplifies subsequent filtering code
-        // now we only have a boolean mask to deal with
-        let predicate = prep_null_mask_filter(predicate);
-        return filter(array, &predicate);
+pub fn filter(values: &dyn Array, predicate: &BooleanArray) -> Result<ArrayRef> {
+    let predicate = FilterBuilder::new(predicate).build();
+    filter_array(values, &predicate)
+}
+
+/// Returns a new [RecordBatch] with arrays containing only values matching the filter.
+pub fn filter_record_batch(
+    record_batch: &RecordBatch,
+    predicate: &BooleanArray,
+) -> Result<RecordBatch> {
+    let filter = FilterBuilder::new(predicate).optimize().build();
+
+    let filtered_arrays = record_batch
+        .columns()
+        .iter()
+        .map(|a| filter_array(a, &filter))
+        .collect::<Result<Vec<_>>>()?;
+
+    RecordBatch::try_new(record_batch.schema(), filtered_arrays)
+}
+
+/// A builder to construct [`FilterPredicate`]
+#[derive(Debug)]
+pub struct FilterBuilder {
+    filter: BooleanArray,
+    count: usize,
+    iterator: FilterIterator,
+}
+
+impl FilterBuilder {
+    /// Create a new [`FilterBuilder`] that can be used to construct a [`FilterPredicate`]
+    pub fn new(filter: &BooleanArray) -> Self {
+        let filter = match filter.null_count() {
+            0 => BooleanArray::from(filter.data().clone()),
+            _ => prep_null_mask_filter(filter),
+        };
+
+        let count = filter_count(&filter);
+        let selectivity_frac = count as f64 / filter.len() as f64;
+        let iterator = if selectivity_frac > FILTER_SLICES_SELECTIVITY_THRESHOLD {
+            FilterIterator::SlicesIterator
+        } else {
+            FilterIterator::IndexIterator
+        };
+
+        Self {
+            filter,
+            count,
+            iterator,
+        }
     }
 
-    let filter_count = filter_count(predicate);
+    /// Compute an optimised representation of the provided `filter` mask that can be
+    /// applied to an array more quickly.
+    ///
+    /// Note: There is limited benefit to calling this to then filter a single array
+    /// Note: This will likely have a larger memory footprint than the original mask
+    pub fn optimize(mut self) -> Self {
+        match self.iterator {
+            FilterIterator::SlicesIterator => {
+                let slices = SlicesIterator::new(&self.filter).collect();
+                self.iterator = FilterIterator::Slices(slices)
+            }
+            FilterIterator::IndexIterator => {
+                let indices = IndexIterator::new(&self.filter, self.count).collect();

Review comment:
       `IndexIterator` reports its size allowing it to be used with `trusted_len_iter`, `SlicesIterator` does not. Theoretically they both could, but `SlicesIterator` is public and the benefit less as the whole purpose is to amortize the resizes by copying large ranges




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org