New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ARROW-10540 [Rust] Improve filtering #8630
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,128 +14,141 @@ | |
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
extern crate arrow; | ||
|
||
use arrow::{compute::Filter, util::test_util::seedable_rng}; | ||
use rand::{ | ||
distributions::{Alphanumeric, Standard}, | ||
prelude::Distribution, | ||
Rng, | ||
}; | ||
|
||
use arrow::array::*; | ||
use arrow::compute::{filter, FilterContext}; | ||
use arrow::compute::{build_filter, filter}; | ||
use arrow::datatypes::ArrowNumericType; | ||
use arrow::datatypes::{Float32Type, UInt8Type}; | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
|
||
fn create_primitive_array<T, F>(size: usize, value_fn: F) -> PrimitiveArray<T> | ||
fn create_primitive_array<T>(size: usize, null_density: f32) -> PrimitiveArray<T> | ||
where | ||
T: ArrowNumericType, | ||
F: Fn(usize) -> T::Native, | ||
Standard: Distribution<T::Native>, | ||
{ | ||
// use random numbers to avoid spurious compiler optimizations wrt to branching | ||
let mut rng = seedable_rng(); | ||
let mut builder = PrimitiveArray::<T>::builder(size); | ||
for i in 0..size { | ||
builder.append_value(value_fn(i)).unwrap(); | ||
|
||
for _ in 0..size { | ||
if rng.gen::<f32>() < null_density { | ||
builder.append_null().unwrap(); | ||
} else { | ||
builder.append_value(rng.gen()).unwrap(); | ||
} | ||
} | ||
builder.finish() | ||
} | ||
|
||
fn create_u8_array_with_nulls(size: usize) -> UInt8Array { | ||
let mut builder = UInt8Builder::new(size); | ||
for i in 0..size { | ||
if i % 2 == 0 { | ||
builder.append_value(1).unwrap(); | ||
} else { | ||
fn create_string_array(size: usize, null_density: f32) -> StringArray { | ||
// use random numbers to avoid spurious compiler optimizations wrt to branching | ||
let mut rng = seedable_rng(); | ||
let mut builder = StringBuilder::new(size); | ||
|
||
for _ in 0..size { | ||
if rng.gen::<f32>() < null_density { | ||
builder.append_null().unwrap(); | ||
} else { | ||
let value = (&mut rng) | ||
.sample_iter(&Alphanumeric) | ||
.take(10) | ||
.collect::<String>(); | ||
builder.append_value(&value).unwrap(); | ||
} | ||
} | ||
builder.finish() | ||
} | ||
|
||
fn create_bool_array<F>(size: usize, value_fn: F) -> BooleanArray | ||
where | ||
F: Fn(usize) -> bool, | ||
{ | ||
fn create_bool_array(size: usize, trues_density: f32) -> BooleanArray { | ||
let mut rng = seedable_rng(); | ||
let mut builder = BooleanBuilder::new(size); | ||
for i in 0..size { | ||
builder.append_value(value_fn(i)).unwrap(); | ||
for _ in 0..size { | ||
let value = rng.gen::<f32>() < trues_density; | ||
builder.append_value(value).unwrap(); | ||
} | ||
builder.finish() | ||
} | ||
|
||
fn bench_filter_u8(data_array: &UInt8Array, filter_array: &BooleanArray) { | ||
filter( | ||
criterion::black_box(data_array), | ||
criterion::black_box(filter_array), | ||
) | ||
.unwrap(); | ||
fn bench_filter(data_array: &UInt8Array, filter_array: &BooleanArray) { | ||
criterion::black_box(filter(data_array, filter_array).unwrap()); | ||
} | ||
|
||
// fn bench_filter_f32(data_array: &Float32Array, filter_array: &BooleanArray) { | ||
// filter(criterion::black_box(data_array), criterion::black_box(filter_array)).unwrap(); | ||
// } | ||
|
||
fn bench_filter_context_u8(data_array: &UInt8Array, filter_context: &FilterContext) { | ||
filter_context | ||
.filter(criterion::black_box(data_array)) | ||
.unwrap(); | ||
} | ||
|
||
fn bench_filter_context_f32(data_array: &Float32Array, filter_context: &FilterContext) { | ||
filter_context | ||
.filter(criterion::black_box(data_array)) | ||
.unwrap(); | ||
fn bench_built_filter<'a>(filter: &Filter<'a>, data: &impl Array) { | ||
criterion::black_box(filter(&data.data())); | ||
} | ||
|
||
fn add_benchmark(c: &mut Criterion) { | ||
let size = 65536; | ||
let filter_array = create_bool_array(size, |i| matches!(i % 2, 0)); | ||
let sparse_filter_array = create_bool_array(size, |i| matches!(i % 8000, 0)); | ||
let dense_filter_array = create_bool_array(size, |i| !matches!(i % 8000, 0)); | ||
let filter_array = create_bool_array(size, 0.5); | ||
let sparse_filter_array = create_bool_array(size, 1.0 - 1.0 / 8000.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the names of the |
||
let dense_filter_array = create_bool_array(size, 1.0 / 8000.0); | ||
|
||
let filter_context = FilterContext::new(&filter_array).unwrap(); | ||
let sparse_filter_context = FilterContext::new(&sparse_filter_array).unwrap(); | ||
let dense_filter_context = FilterContext::new(&dense_filter_array).unwrap(); | ||
let filter = build_filter(&filter_array).unwrap(); | ||
let sparse_filter = build_filter(&sparse_filter_array).unwrap(); | ||
let dense_filter = build_filter(&dense_filter_array).unwrap(); | ||
|
||
let data_array = create_primitive_array::<UInt8Type>(size, 0.0); | ||
|
||
let data_array = create_primitive_array(size, |i| match i % 2 { | ||
0 => 1, | ||
_ => 0, | ||
}); | ||
c.bench_function("filter u8 low selectivity", |b| { | ||
b.iter(|| bench_filter_u8(&data_array, &filter_array)) | ||
b.iter(|| bench_filter(&data_array, &filter_array)) | ||
}); | ||
c.bench_function("filter u8 high selectivity", |b| { | ||
b.iter(|| bench_filter_u8(&data_array, &sparse_filter_array)) | ||
b.iter(|| bench_filter(&data_array, &sparse_filter_array)) | ||
}); | ||
c.bench_function("filter u8 very low selectivity", |b| { | ||
b.iter(|| bench_filter_u8(&data_array, &dense_filter_array)) | ||
b.iter(|| bench_filter(&data_array, &dense_filter_array)) | ||
}); | ||
|
||
c.bench_function("filter context u8 low selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &filter_context)) | ||
b.iter(|| bench_built_filter(&filter, &data_array)) | ||
}); | ||
c.bench_function("filter context u8 high selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &sparse_filter_context)) | ||
b.iter(|| bench_built_filter(&sparse_filter, &data_array)) | ||
}); | ||
c.bench_function("filter context u8 very low selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &dense_filter_context)) | ||
b.iter(|| bench_built_filter(&dense_filter, &data_array)) | ||
}); | ||
|
||
let data_array = create_u8_array_with_nulls(size); | ||
let data_array = create_primitive_array::<UInt8Type>(size, 0.5); | ||
c.bench_function("filter context u8 w NULLs low selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &filter_context)) | ||
b.iter(|| bench_built_filter(&filter, &data_array)) | ||
}); | ||
c.bench_function("filter context u8 w NULLs high selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &sparse_filter_context)) | ||
b.iter(|| bench_built_filter(&sparse_filter, &data_array)) | ||
}); | ||
c.bench_function("filter context u8 w NULLs very low selectivity", |b| { | ||
b.iter(|| bench_filter_context_u8(&data_array, &dense_filter_context)) | ||
b.iter(|| bench_built_filter(&dense_filter, &data_array)) | ||
}); | ||
|
||
let data_array = create_primitive_array(size, |i| match i % 2 { | ||
0 => 1.0, | ||
_ => 0.0, | ||
}); | ||
let data_array = create_primitive_array::<Float32Type>(size, 0.5); | ||
c.bench_function("filter context f32 low selectivity", |b| { | ||
b.iter(|| bench_filter_context_f32(&data_array, &filter_context)) | ||
b.iter(|| bench_built_filter(&filter, &data_array)) | ||
}); | ||
c.bench_function("filter context f32 high selectivity", |b| { | ||
b.iter(|| bench_filter_context_f32(&data_array, &sparse_filter_context)) | ||
b.iter(|| bench_built_filter(&sparse_filter, &data_array)) | ||
}); | ||
c.bench_function("filter context f32 very low selectivity", |b| { | ||
b.iter(|| bench_filter_context_f32(&data_array, &dense_filter_context)) | ||
b.iter(|| bench_built_filter(&dense_filter, &data_array)) | ||
}); | ||
|
||
let data_array = create_string_array(size, 0.5); | ||
c.bench_function("filter context string low selectivity", |b| { | ||
b.iter(|| bench_built_filter(&filter, &data_array)) | ||
}); | ||
c.bench_function("filter context string high selectivity", |b| { | ||
b.iter(|| bench_built_filter(&sparse_filter, &data_array)) | ||
}); | ||
c.bench_function("filter context string very low selectivity", |b| { | ||
b.iter(|| bench_built_filter(&dense_filter, &data_array)) | ||
}); | ||
} | ||
|
||
|
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,40 @@ | ||
// 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 crate::array::ArrayData; | ||
|
||
use super::{ | ||
Extend, _MutableArrayData, | ||
utils::{reserve_for_bits, set_bits}, | ||
}; | ||
|
||
pub(super) fn build_extend(array: &ArrayData) -> Extend { | ||
let values = array.buffers()[0].data(); | ||
Box::new( | ||
move |mutable: &mut _MutableArrayData, start: usize, len: usize| { | ||
let buffer = &mut mutable.buffers[0]; | ||
reserve_for_bits(buffer, mutable.len + len); | ||
set_bits( | ||
&mut buffer.data_mut(), | ||
values, | ||
mutable.len, | ||
array.offset() + start, | ||
len, | ||
); | ||
}, | ||
) | ||
} |
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,75 @@ | ||
// 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 crate::{ | ||
array::{ArrayData, OffsetSizeTrait}, | ||
datatypes::ToByteSlice, | ||
}; | ||
|
||
use super::{Extend, _MutableArrayData, utils::extend_offsets}; | ||
|
||
pub(super) fn build_extend<T: OffsetSizeTrait>(array: &ArrayData) -> Extend { | ||
let offsets = array.buffer::<T>(0); | ||
if array.null_count() == 0 { | ||
// fast case where we can copy regions without nullability checks | ||
Box::new( | ||
move |mutable: &mut _MutableArrayData, start: usize, len: usize| { | ||
let mutable_offsets = mutable.buffer::<T>(0); | ||
let last_offset = mutable_offsets[mutable_offsets.len() - 1]; | ||
// offsets | ||
extend_offsets::<T>( | ||
&mut mutable.buffers[0], | ||
last_offset, | ||
&offsets[start..start + len + 1], | ||
); | ||
|
||
mutable.child_data[0].extend( | ||
offsets[start].to_usize().unwrap(), | ||
offsets[start + len].to_usize().unwrap(), | ||
) | ||
}, | ||
) | ||
} else { | ||
// nulls present: append item by item, ignoring null entries | ||
Box::new( | ||
move |mutable: &mut _MutableArrayData, start: usize, len: usize| { | ||
let mutable_offsets = mutable.buffer::<T>(0); | ||
let mut last_offset = mutable_offsets[mutable_offsets.len() - 1]; | ||
|
||
let buffer = &mut mutable.buffers[0]; | ||
let delta_len = array.len() - array.null_count(); | ||
buffer.reserve(buffer.len() + delta_len * std::mem::size_of::<T>()); | ||
|
||
let child = &mut mutable.child_data[0]; | ||
(start..start + len).for_each(|i| { | ||
if array.is_valid(i) { | ||
// compute the new offset | ||
last_offset = last_offset + offsets[i + 1] - offsets[i]; | ||
|
||
// append value | ||
child.extend( | ||
offsets[i].to_usize().unwrap(), | ||
offsets[i + 1].to_usize().unwrap(), | ||
); | ||
} | ||
// append offset | ||
buffer.extend_from_slice(last_offset.to_byte_slice()); | ||
}) | ||
}, | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using random numbers to generate the filter arrays makes it difficult to control filter selectivity; also doesn't that make each benchmark run unique which I think is the opposite of what we want - we want consistent benchmarks with stable, repeatable conditions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this is the reason for the inconsistency in the benchmark results - usually a highly selective filter, where most filter bits are 0s and only a small number of values are selected / copied to the output array will always have the best performance because most batches of filter bits can be skipped quickly and only a few values are copied to the output array;
this relationship can clearly be seen in the benchmark results listed in this earlier PR #7798;
however in the benchmark results listed in the description of this PR in many cases the opposite is true - low selectivity filter benchmarks achieve much better performance than their high selectivity counterparts; I wonder what's the reason for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your comments and for going through the code.
Could you elaborate? Isn't the selectivity controlled above? AFAIK we just do not make it on regularly-spaced intervals (
i % X
) but according to some seed/randomness.I agree. Would freezing it with a seed address the concern? My main concern with
if i % 2 == 0
and the like is that these are highly predictable patterns and unlikely in real world situations. This predictability can make our benchmarks not very informative as they are benchmarking speculative execution and other optimizations, not the code (and again, these patterns are unlikely in real-world).Another way of looking at it is that our benchmarks need entropy to represent the lack of information that we possess about the underlying distribution of the data (nulls, selectivity, values, etc). The patterns
i % 2
and the like are super informative (almost no entropy).Sorry, it is just notation: I used "highly selectivity" in the text above to mean a lot of 1s (i.e. many items are selected => high selectivity), not a lot of 0s. But I see that you use the opposite meaning.
In the benchmarks above:
low selectivity
-> select 50%high selectivity
-> do not select 1 on every 8000 entriesvery low selectivity
-> select 1 on every 8000 entriesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yordan-pavlov Thanks for reminding me that, it was on my plate for a while, now it is addressed in #8635
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thread id is xored with the seed, thread_rng doesn't fit reproducible benchmarks point of view, so check out the pr I've opened @jorgecarleitao . tell me what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @vertexclique mentions, he has provided a good PR related to this conversation #8635
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorgecarleitao the filter benchmarks may not simulate real-world use cases, but they are designed to test the code under specific conditions such as the worst case scenario with alternating 1s and 0s where no batch can be skipped and all selected values have to be copied individually; how can this scenario be achieved with a randomly generated filter array?
the other scenarios which test mostly 0s (best performance because most filter batches can be skipped and only a small number of selected values have to be copied) and mostly 1s (which is not as fast, but still faster than worst case because filter batches can be checked quickly and most values are copied in slices) should be easier to achieve with random filter arrays but are they going to be repeatable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorgecarleitao once the approach to filter benchmarks has been finalized would it be possible to rearrange the commits so that the same benchmark code can be used to benchmark both the old and new implementations so that we can do a direct comparison?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yordan-pavlov , that is the case already: :) I put any changes to benchmarks on the first commit (as I need to benchmark them myself).