Skip to content

Commit

Permalink
Specialized filter kernels (#1248)
Browse files Browse the repository at this point in the history
* Add specialized primitive filter kernels

* Filter context

* Optimize null buffer construction

* Clippy

* Benchmark filter construction

* Review feedback

* Specialized string filter

* Specialized dictionary filter kernel

* Use trusted_len_iter

* Review feedback

* Add fuzz filter test

* Clarify selective vs selectivity confusion

* Revert change to MutableBuffer::from_trusted_len_iter_bool

* Fix filter_bits offset handling

* Review feedback

* Use i64 for chunk offset

* Only optimize filter when filtering multiple columns

* Test truncated filter

* Review feedback

* Add IterationStrategy::None

* Remove selective / selectivity docs confusion
  • Loading branch information
tustvold authored Feb 9, 2022
1 parent a049640 commit c064d53
Show file tree
Hide file tree
Showing 3 changed files with 963 additions and 99 deletions.
139 changes: 108 additions & 31 deletions arrow/benches/filter_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ extern crate arrow;

use std::sync::Arc;

use arrow::compute::{filter_record_batch, Filter};
use arrow::compute::{filter_record_batch, FilterBuilder, FilterPredicate};
use arrow::record_batch::RecordBatch;
use arrow::util::bench_util::*;

use arrow::array::*;
use arrow::compute::{build_filter, filter};
use arrow::datatypes::{Field, Float32Type, Schema, UInt8Type};
use arrow::compute::filter;
use arrow::datatypes::{Field, Float32Type, Int32Type, Schema, UInt8Type};

use criterion::{criterion_group, criterion_main, Criterion};

fn bench_filter(data_array: &dyn Array, filter_array: &BooleanArray) {
criterion::black_box(filter(data_array, filter_array).unwrap());
}

fn bench_built_filter<'a>(filter: &Filter<'a>, data: &impl Array) {
criterion::black_box(filter(data.data()));
fn bench_built_filter(filter: &FilterPredicate, array: &dyn Array) {
criterion::black_box(filter.filter(array).unwrap());
}

fn add_benchmark(c: &mut Criterion) {
Expand All @@ -42,68 +42,145 @@ 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 (kept 1/2)", |b| {
b.iter(|| FilterBuilder::new(&filter_array).optimize().build())
});

c.bench_function("filter optimize high selectivity (kept 1023/1024)", |b| {
b.iter(|| FilterBuilder::new(&dense_filter_array).optimize().build())
});

c.bench_function("filter optimize low selectivity (kept 1/1024)", |b| {
b.iter(|| FilterBuilder::new(&sparse_filter_array).optimize().build())
});

c.bench_function("filter u8 (kept 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 (kept 1023/1024)", |b| {
b.iter(|| bench_filter(&data_array, &dense_filter_array))
});
c.bench_function("filter u8 low selectivity", |b| {
c.bench_function("filter u8 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_filter(&data_array, &sparse_filter_array))
});

c.bench_function("filter context u8", |b| {
c.bench_function("filter context u8 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context u8 high selectivity", |b| {
c.bench_function("filter context u8 high selectivity (kept 1023/1024)", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context u8 low selectivity", |b| {
c.bench_function("filter context u8 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_primitive_array::<UInt8Type>(size, 0.5);
c.bench_function("filter context u8 w NULLs", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
let data_array = create_primitive_array::<Int32Type>(size, 0.0);
c.bench_function("filter i32 (kept 1/2)", |b| {
b.iter(|| bench_filter(&data_array, &filter_array))
});
c.bench_function("filter context u8 w NULLs high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
c.bench_function("filter i32 high selectivity (kept 1023/1024)", |b| {
b.iter(|| bench_filter(&data_array, &dense_filter_array))
});
c.bench_function("filter i32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_filter(&data_array, &sparse_filter_array))
});

c.bench_function("filter context i32 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context u8 w NULLs low selectivity", |b| {
c.bench_function(
"filter context i32 high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context i32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_primitive_array::<Int32Type>(size, 0.5);
c.bench_function("filter context i32 w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function(
"filter context i32 w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context i32 w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_primitive_array::<UInt8Type>(size, 0.5);
c.bench_function("filter context u8 w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function(
"filter context u8 w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context u8 w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_primitive_array::<Float32Type>(size, 0.5);
c.bench_function("filter f32", |b| {
c.bench_function("filter f32 (kept 1/2)", |b| {
b.iter(|| bench_filter(&data_array, &filter_array))
});
c.bench_function("filter context f32", |b| {
c.bench_function("filter context f32 (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context f32 high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context f32 low selectivity", |b| {
c.bench_function(
"filter context f32 high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context f32 low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_string_array::<i32>(size, 0.5);
c.bench_function("filter context string", |b| {
c.bench_function("filter context string (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function("filter context string high selectivity", |b| {
b.iter(|| bench_built_filter(&dense_filter, &data_array))
});
c.bench_function("filter context string low selectivity", |b| {
c.bench_function(
"filter context string high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function("filter context string low selectivity (kept 1/1024)", |b| {
b.iter(|| bench_built_filter(&sparse_filter, &data_array))
});

let data_array = create_string_dict_array::<Int32Type>(size, 0.0);
c.bench_function("filter context string dictionary (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function(
"filter context string dictionary high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context string dictionary low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_string_dict_array::<Int32Type>(size, 0.5);
c.bench_function("filter context string dictionary w NULLs (kept 1/2)", |b| {
b.iter(|| bench_built_filter(&filter, &data_array))
});
c.bench_function(
"filter context string dictionary w NULLs high selectivity (kept 1023/1024)",
|b| b.iter(|| bench_built_filter(&dense_filter, &data_array)),
);
c.bench_function(
"filter context string dictionary w NULLs low selectivity (kept 1/1024)",
|b| b.iter(|| bench_built_filter(&sparse_filter, &data_array)),
);

let data_array = create_primitive_array::<Float32Type>(size, 0.0);

let field = Field::new("c1", data_array.data_type().clone(), true);
Expand Down
Loading

0 comments on commit c064d53

Please sign in to comment.