forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: jayzhan211 <[email protected]>
- Loading branch information
1 parent
9476c64
commit c6cbfd3
Showing
2 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use arrow::compute::SlicesIterator; | ||
use arrow_array::{ArrayRef, ArrowPrimitiveType, BooleanArray}; | ||
use arrow_buffer::bit_iterator::BitIndexIterator; | ||
use datafusion_common::Result; | ||
|
||
// 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, | ||
} | ||
|
||
/// 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) | ||
// } | ||
|
||
/// Number of rows being selected based on this [`FilterPredicate`] | ||
pub fn count(&self) -> usize { | ||
self.count | ||
} | ||
} | ||
|
||
pub trait FilterCoalescer: Send + Sync { | ||
fn append_filtered_array( | ||
&mut self, | ||
array: &ArrayRef, | ||
predicate: &FilterPredicate, | ||
) -> Result<()>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct PrimitiveFilterBuilder<T: ArrowPrimitiveType, const NULLABLE: bool> { | ||
filter_values: Vec<T::Native>, | ||
// nulls: MaybeNullBufferBuilder, | ||
} | ||
|
||
impl<T, const NULLABLE: bool> PrimitiveFilterBuilder<T, NULLABLE> | ||
where | ||
T: ArrowPrimitiveType, | ||
{ | ||
pub fn new() -> Self { | ||
Self { | ||
filter_values: vec![], | ||
} | ||
} | ||
} | ||
|
||
impl<T, const NULLABLE: bool> FilterCoalescer for PrimitiveFilterBuilder<T, NULLABLE> | ||
where | ||
T: ArrowPrimitiveType, | ||
{ | ||
fn append_filtered_array( | ||
&mut self, | ||
array: &ArrayRef, | ||
predicate: &FilterPredicate, | ||
) -> Result<()> { | ||
let arr = array.as_primitive::<T>(); | ||
let values = arr.values(); | ||
|
||
match &predicate.strategy { | ||
IterationStrategy::SlicesIterator => { | ||
for (start, end) in SlicesIterator::new(&predicate.filter) { | ||
self.filter_values.extend(&values[start..end]); | ||
} | ||
} | ||
IterationStrategy::Slices(slices) => { | ||
for (start, end) in slices { | ||
self.filter_values.extend(&values[*start..*end]); | ||
} | ||
} | ||
IterationStrategy::IndexIterator => { | ||
let iter = IndexIterator::new(&predicate.filter, predicate.count) | ||
.map(|x| values[x]); | ||
self.filter_values.extend(iter); | ||
} | ||
IterationStrategy::Indices(indices) => { | ||
let iter = indices.iter().map(|x| values[*x]); | ||
self.filter_values.extend(iter); | ||
} | ||
IterationStrategy::All | IterationStrategy::None => unreachable!(), | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// An iterator of `usize` whose index in [`BooleanArray`] is true | ||
/// | ||
/// This provides the best performance on most predicates, apart from those which keep | ||
/// large runs and therefore favour [`SlicesIterator`] | ||
struct IndexIterator<'a> { | ||
remaining: usize, | ||
iter: BitIndexIterator<'a>, | ||
} | ||
|
||
impl<'a> IndexIterator<'a> { | ||
fn new(filter: &'a BooleanArray, remaining: usize) -> Self { | ||
assert_eq!(filter.null_count(), 0); | ||
let iter = filter.values().set_indices(); | ||
Self { remaining, iter } | ||
} | ||
} | ||
|
||
impl Iterator for IndexIterator<'_> { | ||
type Item = usize; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.remaining != 0 { | ||
// Fascinatingly swapping these two lines around results in a 50% | ||
// performance regression for some benchmarks | ||
let next = self.iter.next().expect("IndexIterator exhausted early"); | ||
self.remaining -= 1; | ||
// Must panic if exhausted early as trusted length iterator | ||
return Some(next); | ||
} | ||
None | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.remaining, Some(self.remaining)) | ||
} | ||
} |