Skip to content

Apache DataFusion SQL Query Engine

Enable physical filter pushdown for hash joins (#16954)

Adrian Garcia Badaraccob10f453e7b6b
diff --git a/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs b/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
index 2506f270c0..c3512d6d9e 100644
--- a/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
+++ b/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
@@ -159,6 +159,269 @@ fn test_pushdown_into_scan_with_config_options() {
);
}
+#[tokio::test]
+async fn test_dynamic_filter_pushdown_through_hash_join_with_topk() {
+ use datafusion_common::JoinType;
+ use datafusion_physical_plan::joins::{HashJoinExec, PartitionMode};
+
+ // Create build side with limited values
+ let build_batches = vec![record_batch!(
+ ("a", Utf8, ["aa", "ab"]),
+ ("b", Utf8, ["ba", "bb"]),
+ ("c", Float64, [1.0, 2.0])
+ )
+ .unwrap()];
+ let build_side_schema = Arc::new(Schema::new(vec![
+ Field::new("a", DataType::Utf8, false),
+ Field::new("b", DataType::Utf8, false),
+ Field::new("c", DataType::Float64, false),
+ ]));
+ let build_scan = TestScanBuilder::new(Arc::clone(&build_side_schema))
+ .with_support(true)
+ .with_batches(build_batches)
+ .build();
+
+ // Create probe side with more values
+ let probe_batches = vec![record_batch!(
+ ("d", Utf8, ["aa", "ab", "ac", "ad"]),
+ ("e", Utf8, ["ba", "bb", "bc", "bd"]),
+ ("f", Float64, [1.0, 2.0, 3.0, 4.0])
+ )
+ .unwrap()];
+ let probe_side_schema = Arc::new(Schema::new(vec![
+ Field::new("d", DataType::Utf8, false),
+ Field::new("e", DataType::Utf8, false),
+ Field::new("f", DataType::Float64, false),
+ ]));
+ let probe_scan = TestScanBuilder::new(Arc::clone(&probe_side_schema))
+ .with_support(true)
+ .with_batches(probe_batches)
+ .build();
+
+ // Create HashJoinExec
+ let on = vec![(
+ col("a", &build_side_schema).unwrap(),
+ col("d", &probe_side_schema).unwrap(),
+ )];
+ let join = Arc::new(
+ HashJoinExec::try_new(
+ build_scan,
+ probe_scan,
+ on,
+ None,
+ &JoinType::Inner,
+ None,
+ PartitionMode::Partitioned,
+ datafusion_common::NullEquality::NullEqualsNothing,
+ )
+ .unwrap(),
+ );
+
+ let join_schema = join.schema();
+
+ // Finally let's add a SortExec on the outside to test pushdown of dynamic filters
+ let sort_expr =
+ PhysicalSortExpr::new(col("e", &join_schema).unwrap(), SortOptions::default());
+ let plan = Arc::new(
+ SortExec::new(LexOrdering::new(vec![sort_expr]).unwrap(), join)
+ .with_fetch(Some(2)),
+ ) as Arc<dyn ExecutionPlan>;
+
+ let mut config = ConfigOptions::default();
+ config.optimizer.enable_dynamic_filter_pushdown = true;
+ config.execution.parquet.pushdown_filters = true;
+
+ // Appy the FilterPushdown optimizer rule
+ let plan = FilterPushdown::new_post_optimization()
+ .optimize(Arc::clone(&plan), &config)
+ .unwrap();
+
+ // Test that filters are pushed down correctly to each side of the join
+ insta::assert_snapshot!(
+ format_plan_for_test(&plan),
+ @r"
+ - SortExec: TopK(fetch=2), expr=[e@4 ASC], preserve_partitioning=[false]
+ - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true, predicate=DynamicFilterPhysicalExpr [ true ]
+ "
+ );
+
+ // Put some data through the plan to check that the filter is updated to reflect the TopK state
+ let session_ctx = SessionContext::new_with_config(SessionConfig::new());
+ session_ctx.register_object_store(
+ ObjectStoreUrl::parse("test://").unwrap().as_ref(),
+ Arc::new(InMemory::new()),
+ );
+ let state = session_ctx.state();
+ let task_ctx = state.task_ctx();
+ let mut stream = plan.execute(0, Arc::clone(&task_ctx)).unwrap();
+ // Iterate one batch
+ stream.next().await.unwrap().unwrap();
+
+ // Test that filters are pushed down correctly to each side of the join
+ insta::assert_snapshot!(
+ format_plan_for_test(&plan),
+ @r"
+ - SortExec: TopK(fetch=2), expr=[e@4 ASC], preserve_partitioning=[false], filter=[e@4 IS NULL OR e@4 < bb]
+ - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true, predicate=DynamicFilterPhysicalExpr [ e@1 IS NULL OR e@1 < bb ]
+ "
+ );
+}
+
+// Test both static and dynamic filter pushdown in HashJoinExec.
+// Note that static filter pushdown is rare: it should have already happened in the logical optimizer phase.
+// However users may manually construct plans that could result in a FilterExec -> HashJoinExec -> Scan setup.
+// Dynamic filters arise in cases such as nested inner joins or TopK -> HashJoinExec -> Scan setups.
+#[tokio::test]
+async fn test_static_filter_pushdown_through_hash_join() {
+ use datafusion_common::JoinType;
+ use datafusion_physical_plan::joins::{HashJoinExec, PartitionMode};
+
+ // Create build side with limited values
+ let build_batches = vec![record_batch!(
+ ("a", Utf8, ["aa", "ab"]),
+ ("b", Utf8, ["ba", "bb"]),
+ ("c", Float64, [1.0, 2.0])
+ )
+ .unwrap()];
+ let build_side_schema = Arc::new(Schema::new(vec![
+ Field::new("a", DataType::Utf8, false),
+ Field::new("b", DataType::Utf8, false),
+ Field::new("c", DataType::Float64, false),
+ ]));
+ let build_scan = TestScanBuilder::new(Arc::clone(&build_side_schema))
+ .with_support(true)
+ .with_batches(build_batches)
+ .build();
+
+ // Create probe side with more values
+ let probe_batches = vec![record_batch!(
+ ("d", Utf8, ["aa", "ab", "ac", "ad"]),
+ ("e", Utf8, ["ba", "bb", "bc", "bd"]),
+ ("f", Float64, [1.0, 2.0, 3.0, 4.0])
+ )
+ .unwrap()];
+ let probe_side_schema = Arc::new(Schema::new(vec![
+ Field::new("d", DataType::Utf8, false),
+ Field::new("e", DataType::Utf8, false),
+ Field::new("f", DataType::Float64, false),
+ ]));
+ let probe_scan = TestScanBuilder::new(Arc::clone(&probe_side_schema))
+ .with_support(true)
+ .with_batches(probe_batches)
+ .build();
+
+ // Create HashJoinExec
+ let on = vec![(
+ col("a", &build_side_schema).unwrap(),
+ col("d", &probe_side_schema).unwrap(),
+ )];
+ let join = Arc::new(
+ HashJoinExec::try_new(
+ build_scan,
+ probe_scan,
+ on,
+ None,
+ &JoinType::Inner,
+ None,
+ PartitionMode::Partitioned,
+ datafusion_common::NullEquality::NullEqualsNothing,
+ )
+ .unwrap(),
+ );
+
+ // Create filters that can be pushed down to different sides
+ // We need to create filters in the context of the join output schema
+ let join_schema = join.schema();
+
+ // Filter on build side column: a = 'aa'
+ let left_filter = col_lit_predicate("a", "aa", &join_schema);
+ // Filter on probe side column: e = 'ba'
+ let right_filter = col_lit_predicate("e", "ba", &join_schema);
+ // Filter that references both sides: a = d (should not be pushed down)
+ let cross_filter = Arc::new(BinaryExpr::new(
+ col("a", &join_schema).unwrap(),
+ Operator::Eq,
+ col("d", &join_schema).unwrap(),
+ )) as Arc<dyn PhysicalExpr>;
+
+ let filter =
+ Arc::new(FilterExec::try_new(left_filter, Arc::clone(&join) as _).unwrap());
+ let filter = Arc::new(FilterExec::try_new(right_filter, filter).unwrap());
+ let plan = Arc::new(FilterExec::try_new(cross_filter, filter).unwrap())
+ as Arc<dyn ExecutionPlan>;
+
+ // Test that filters are pushed down correctly to each side of the join
+ insta::assert_snapshot!(
+ OptimizationTest::new(Arc::clone(&plan), FilterPushdown::new(), true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = d@3
+ - FilterExec: e@4 = ba
+ - FilterExec: a@0 = aa
+ - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - FilterExec: a@0 = d@3
+ - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = aa
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true, predicate=e@1 = ba
+ "
+ );
+
+ // Test left join - filters should NOT be pushed down
+ let join = Arc::new(
+ HashJoinExec::try_new(
+ TestScanBuilder::new(Arc::clone(&build_side_schema))
+ .with_support(true)
+ .build(),
+ TestScanBuilder::new(Arc::clone(&probe_side_schema))
+ .with_support(true)
+ .build(),
+ vec![(
+ col("a", &build_side_schema).unwrap(),
+ col("d", &probe_side_schema).unwrap(),
+ )],
+ None,
+ &JoinType::Left,
+ None,
+ PartitionMode::Partitioned,
+ datafusion_common::NullEquality::NullEqualsNothing,
+ )
+ .unwrap(),
+ );
+
+ let join_schema = join.schema();
+ let filter = col_lit_predicate("a", "aa", &join_schema);
+ let plan =
+ Arc::new(FilterExec::try_new(filter, join).unwrap()) as Arc<dyn ExecutionPlan>;
+
+ // Test that filters are NOT pushed down for left join
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown::new(), true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = aa
+ - HashJoinExec: mode=Partitioned, join_type=Left, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - FilterExec: a@0 = aa
+ - HashJoinExec: mode=Partitioned, join_type=Left, on=[(a@0, d@0)]
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ - DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[d, e, f], file_type=test, pushdown_supported=true
+ "
+ );
+}
+
#[test]
fn test_filter_collapse() {
// filter should be pushed down into the parquet scan with two filters
diff --git a/datafusion/physical-plan/src/execution_plan.rs b/datafusion/physical-plan/src/execution_plan.rs
index 6d51bf195d..6b402528c1 100644
--- a/datafusion/physical-plan/src/execution_plan.rs
+++ b/datafusion/physical-plan/src/execution_plan.rs
@@ -17,8 +17,8 @@
pub use crate::display::{DefaultDisplay, DisplayAs, DisplayFormatType, VerboseDisplay};
use crate::filter_pushdown::{
- ChildFilterDescription, ChildPushdownResult, FilterDescription, FilterPushdownPhase,
- FilterPushdownPropagation, PushedDownPredicate,
+ ChildPushdownResult, FilterDescription, FilterPushdownPhase,
+ FilterPushdownPropagation,
};
pub use crate::metrics::Metric;
pub use crate::ordering::InputOrderMode;
@@ -33,7 +33,6 @@ pub use datafusion_physical_expr::window::WindowExpr;
pub use datafusion_physical_expr::{
expressions, Distribution, Partitioning, PhysicalExpr,
};
-use itertools::Itertools;
use std::any::Any;
use std::fmt::Debug;
@@ -521,19 +520,10 @@ pub trait ExecutionPlan: Debug + DisplayAs + Send + Sync {
parent_filters: Vec<Arc<dyn PhysicalExpr>>,
_config: &ConfigOptions,
) -> Result<FilterDescription> {
- // Default implementation: mark all filters as unsupported for all children
- let mut desc = FilterDescription::new();
- let child_filters = parent_filters
- .iter()
- .map(|f| PushedDownPredicate::unsupported(Arc::clone(f)))
- .collect_vec();
- for _ in 0..self.children().len() {
- desc = desc.with_child(ChildFilterDescription {
- parent_filters: child_filters.clone(),
- self_filters: vec![],
- });
- }
- Ok(desc)
+ Ok(FilterDescription::all_unsupported(
+ &parent_filters,
+ &self.children(),
+ ))
}
/// Handle the result of a child pushdown.
diff --git a/datafusion/physical-plan/src/filter_pushdown.rs b/datafusion/physical-plan/src/filter_pushdown.rs
index a3e94a75c8..d10bd7aea3 100644
--- a/datafusion/physical-plan/src/filter_pushdown.rs
+++ b/datafusion/physical-plan/src/filter_pushdown.rs
@@ -40,8 +40,9 @@ use std::sync::Arc;
use datafusion_common::Result;
use datafusion_physical_expr::utils::{collect_columns, reassign_predicate_columns};
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
+use itertools::Itertools;
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterPushdownPhase {
/// Pushdown that happens before most other optimizations.
/// This pushdown allows static filters that do not reference any [`ExecutionPlan`]s to be pushed down.
@@ -257,6 +258,19 @@ impl<T> FilterPushdownPropagation<T> {
}
}
+ /// Create a new [`FilterPushdownPropagation`] that tells the parent node that no filters were pushed down regardless of the child results.
+ pub fn all_unsupported(child_pushdown_result: ChildPushdownResult) -> Self {
+ let filters = child_pushdown_result
+ .parent_filters
+ .into_iter()
+ .map(|_| PushedDown::No)
+ .collect();
+ Self {
+ filters,
+ updated_node: None,
+ }
+ }
+
/// Create a new [`FilterPushdownPropagation`] with the specified filter support.
/// This transmits up to our parent node what the result of pushing down the filters into our node and possibly our subtree was.
pub fn with_parent_pushdown_result(filters: Vec<PushedDown>) -> Self {
@@ -413,6 +427,25 @@ impl FilterDescription {
Ok(desc)
}
+ /// Mark all parent filters as unsupported for all children.
+ pub fn all_unsupported(
+ parent_filters: &[Arc<dyn PhysicalExpr>],
+ children: &[&Arc<dyn crate::ExecutionPlan>],
+ ) -> Self {
+ let mut desc = Self::new();
+ let child_filters = parent_filters
+ .iter()
+ .map(|f| PushedDownPredicate::unsupported(Arc::clone(f)))
+ .collect_vec();
+ for _ in 0..children.len() {
+ desc = desc.with_child(ChildFilterDescription {
+ parent_filters: child_filters.clone(),
+ self_filters: vec![],
+ });
+ }
+ desc
+ }
+
pub fn parent_filters(&self) -> Vec<Vec<PushedDownPredicate>> {
self.child_filter_descriptions
.iter()
diff --git a/datafusion/physical-plan/src/joins/hash_join.rs b/datafusion/physical-plan/src/joins/hash_join.rs
index a7f28ede44..0a26039462 100644
--- a/datafusion/physical-plan/src/joins/hash_join.rs
+++ b/datafusion/physical-plan/src/joins/hash_join.rs
@@ -34,6 +34,10 @@ use super::{
};
use super::{JoinOn, JoinOnRef};
use crate::execution_plan::{boundedness_from_children, EmissionType};
+use crate::filter_pushdown::{
+ ChildPushdownResult, FilterDescription, FilterPushdownPhase,
+ FilterPushdownPropagation,
+};
use crate::joins::join_hash_map::{JoinHashMapU32, JoinHashMapU64};
use crate::projection::{
try_embed_projection, try_pushdown_through_join, EmbeddedProjection, JoinData,
@@ -68,6 +72,7 @@ use arrow::datatypes::{Schema, SchemaRef};
use arrow::error::ArrowError;
use arrow::record_batch::RecordBatch;
use arrow::util::bit_util;
+use datafusion_common::config::ConfigOptions;
use datafusion_common::utils::memory::estimate_memory_size;
use datafusion_common::{
internal_datafusion_err, internal_err, plan_err, project_schema, JoinSide, JoinType,
@@ -79,7 +84,7 @@ use datafusion_expr::Operator;
use datafusion_physical_expr::equivalence::{
join_equivalence_properties, ProjectionMapping,
};
-use datafusion_physical_expr::PhysicalExprRef;
+use datafusion_physical_expr::{PhysicalExpr, PhysicalExprRef};
use datafusion_physical_expr_common::datum::compare_op_for_nested;
use ahash::RandomState;
@@ -944,6 +949,47 @@ impl ExecutionPlan for HashJoinExec {
try_embed_projection(projection, self)
}
}
+
+ fn gather_filters_for_pushdown(
+ &self,
+ _phase: FilterPushdownPhase,
+ parent_filters: Vec<Arc<dyn PhysicalExpr>>,
+ _config: &ConfigOptions,
+ ) -> Result<FilterDescription> {
+ // Other types of joins can support *some* filters, but restrictions are complex and error prone.
+ // For now we don't support them.
+ // See the logical optimizer rules for more details: datafusion/optimizer/src/push_down_filter.rs
+ // See https://github.com/apache/datafusion/issues/16973 for tracking.
+ if self.join_type != JoinType::Inner {
+ return Ok(FilterDescription::all_unsupported(
+ &parent_filters,
+ &self.children(),
+ ));
+ }
+ FilterDescription::from_children(parent_filters, &self.children())
+ // TODO: push down our self filters to children in the post optimization phase
+ }
+
+ fn handle_child_pushdown_result(
+ &self,
+ _phase: FilterPushdownPhase,
+ child_pushdown_result: ChildPushdownResult,
+ _config: &ConfigOptions,
+ ) -> Result<FilterPushdownPropagation<Arc<dyn ExecutionPlan>>> {
+ // Note: this check shouldn't be necessary because we already marked all parent filters as unsupported for
+ // non-inner joins in `gather_filters_for_pushdown`.
+ // However it's a cheap check and serves to inform future devs touching this function that they need to be really
+ // careful pushing down filters through non-inner joins.
+ if self.join_type != JoinType::Inner {
+ // Other types of joins can support *some* filters, but restrictions are complex and error prone.
+ // For now we don't support them.
+ // See the logical optimizer rules for more details: datafusion/optimizer/src/push_down_filter.rs
+ return Ok(FilterPushdownPropagation::all_unsupported(
+ child_pushdown_result,
+ ));
+ }
+ Ok(FilterPushdownPropagation::if_any(child_pushdown_result))
+ }
}
/// Reads the left (build) side of the input, buffering it in memory, to build a