Skip to content

Apache DataFusion SQL Query Engine

Improve filter pushdown optimizer rule performance (#15959)

Adrian Garcia Badaracco48331e6044a2
diff --git a/Cargo.lock b/Cargo.lock
index fcac5b1f75..7077563a44 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1082,9 +1082,9 @@ dependencies = [
[[package]]
name = "bitflags"
-version = "1.3.2"
+version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bitflags"
@@ -1437,7 +1437,7 @@ version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
- "bitflags 1.3.2",
+ "bitflags 1.2.1",
"textwrap",
"unicode-width 0.1.14",
]
@@ -5154,7 +5154,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
- "bitflags 1.3.2",
+ "bitflags 1.2.1",
]
[[package]]
diff --git a/datafusion/core/benches/push_down_filter.rs b/datafusion/core/benches/push_down_filter.rs
index 92de1711a9..139fb12c30 100644
--- a/datafusion/core/benches/push_down_filter.rs
+++ b/datafusion/core/benches/push_down_filter.rs
@@ -18,7 +18,7 @@
use arrow::array::RecordBatch;
use arrow::datatypes::{DataType, Field, Schema};
use bytes::{BufMut, BytesMut};
-use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
+use criterion::{criterion_group, criterion_main, Criterion};
use datafusion::config::ConfigOptions;
use datafusion::prelude::{ParquetReadOptions, SessionContext};
use datafusion_execution::object_store::ObjectStoreUrl;
@@ -105,20 +105,19 @@ fn bench_push_down_filter(c: &mut Criterion) {
let mut config = ConfigOptions::default();
config.execution.parquet.pushdown_filters = true;
let plan = BenchmarkPlan { plan, config };
+ let optimizer = FilterPushdown::new();
- c.bench_with_input(
- BenchmarkId::new("push_down_filter", plan.clone()),
- &plan,
- |b, plan| {
- b.iter(|| {
- let optimizer = FilterPushdown::new();
- optimizer
- .optimize(Arc::clone(&plan.plan), &plan.config)
- .unwrap();
- });
- },
- );
+ c.bench_function("push_down_filter", |b| {
+ b.iter(|| {
+ optimizer
+ .optimize(Arc::clone(&plan.plan), &plan.config)
+ .unwrap();
+ });
+ });
}
+// It's a bit absurd that it's this complicated but to generate a flamegraph you can run:
+// `cargo flamegraph -p datafusion --bench push_down_filter --flamechart --root --profile profiling --freq 1000 -- --bench`
+// See https://github.com/flamegraph-rs/flamegraph
criterion_group!(benches, bench_push_down_filter);
criterion_main!(benches);
diff --git a/datafusion/physical-optimizer/src/filter_pushdown.rs b/datafusion/physical-optimizer/src/filter_pushdown.rs
index 6c445458b5..8d23bfbda6 100644
--- a/datafusion/physical-optimizer/src/filter_pushdown.rs
+++ b/datafusion/physical-optimizer/src/filter_pushdown.rs
@@ -414,7 +414,7 @@ fn push_down_filters(
let mut parent_predicates_pushdown_states =
vec![ParentPredicateStates::NoChildren; parent_predicates.len()];
let mut self_filters_pushdown_supports = vec![];
- let mut new_children = vec![];
+ let mut new_children = Vec::with_capacity(node.children().len());
let children = node.children();
let filter_description =
diff --git a/datafusion/physical-plan/src/filter.rs b/datafusion/physical-plan/src/filter.rs
index 9f5d9dc298..928bc1b389 100644
--- a/datafusion/physical-plan/src/filter.rs
+++ b/datafusion/physical-plan/src/filter.rs
@@ -538,7 +538,7 @@ impl ExecutionPlan for FilterExec {
// If we have unhandled filters, we need to create a new FilterExec
let filter_input = Arc::clone(self.input());
let new_predicate = conjunction(unhandled_filters);
- let new_exec = if new_predicate.eq(&lit(true)) {
+ let updated_node = if new_predicate.eq(&lit(true)) {
// FilterExec is no longer needed, but we may need to leave a projection in place
match self.projection() {
Some(projection_indices) => {
@@ -554,25 +554,37 @@ impl ExecutionPlan for FilterExec {
)
})
.collect::<Vec<_>>();
- Arc::new(ProjectionExec::try_new(proj_exprs, filter_input)?)
- as Arc<dyn ExecutionPlan>
+ Some(Arc::new(ProjectionExec::try_new(proj_exprs, filter_input)?)
+ as Arc<dyn ExecutionPlan>)
}
None => {
// No projection needed, just return the input
- filter_input
+ Some(filter_input)
}
}
+ } else if new_predicate.eq(&self.predicate) {
+ // The new predicate is the same as our current predicate
+ None
} else {
// Create a new FilterExec with the new predicate
- Arc::new(
- FilterExec::try_new(new_predicate, filter_input)?
- .with_default_selectivity(self.default_selectivity())?
- .with_projection(self.projection().cloned())?,
- )
+ let new = FilterExec {
+ predicate: Arc::clone(&new_predicate),
+ input: Arc::clone(&filter_input),
+ metrics: self.metrics.clone(),
+ default_selectivity: self.default_selectivity,
+ cache: Self::compute_properties(
+ &filter_input,
+ &new_predicate,
+ self.default_selectivity,
+ self.projection.as_ref(),
+ )?,
+ projection: None,
+ };
+ Some(Arc::new(new) as _)
};
Ok(FilterPushdownPropagation {
filters: child_pushdown_result.parent_filters.make_supported(),
- updated_node: Some(new_exec),
+ updated_node,
})
}
}