Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
haohuaijin committed Jun 23, 2026
commit 1d85fc792dbe4e16141c4922554f3de55d6c7f66
35 changes: 11 additions & 24 deletions datafusion/physical-plan/src/joins/hash_join/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@ impl HashJoinExec {
&self,
partition_mode: PartitionMode,
) -> Result<Arc<dyn ExecutionPlan>> {
assert_or_internal_err!(
self.dynamic_filter.is_none(),
"Cannot swap HashJoinExec inputs after dynamic filters have been constructed. \
Optimizer rules that reorder join inputs must run before optimizer rules `FilterPushdown::new_post_optimization()`"
);

let left = self.left();
let right = self.right();
let new_join = self
Expand All @@ -1095,7 +1101,6 @@ impl HashJoinExec {
self.join_type(),
))
.with_partition_mode(partition_mode)
.with_dynamic_filter(None)
.build()?;
// In case of anti / semi joins or if there is embedded projection in HashJoinExec, output column order is preserved, no need to add projection again
if matches!(
Expand Down Expand Up @@ -6612,7 +6617,7 @@ mod tests {
}

#[test]
fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {
fn test_swap_inputs_rejects_dynamic_filter() -> Result<()> {
let left = build_table(
("l_key", &vec![1]),
("l_payload", &vec![10]),
Expand Down Expand Up @@ -6642,29 +6647,11 @@ mod tests {
)?
.with_dynamic_filter_expr(dynamic_filter)?;

let original_dynamic_filter = join
.dynamic_filter_expr()
.expect("join should start with a dynamic filter");
let original_expression_id = original_dynamic_filter
.expression_id()
.expect("DynamicFilterPhysicalExpr always has an expression_id");

let swapped = join.swap_inputs(PartitionMode::CollectLeft)?;
let swapped_join = swapped
.downcast_ref::<HashJoinExec>()
.expect("swapped join should be a HashJoinExec");

// HashJoinExecBuilder preserves dynamic_filter by default. swap_inputs
// must explicitly clear it, otherwise this would still be
// `Some(original_expression_id)` after the probe side has changed.
assert_ne!(
swapped_join.dynamic_filter_expr().map(|df| {
df.expression_id()
.expect("DynamicFilterPhysicalExpr always has an expression_id")
}),
Some(original_expression_id)
let err = join.swap_inputs(PartitionMode::CollectLeft).unwrap_err();
assert_contains!(
err.to_string(),
"Cannot swap HashJoinExec inputs after dynamic filters have been constructed"
);
assert!(swapped_join.dynamic_filter_expr().is_none());
Ok(())
}

Expand Down
47 changes: 0 additions & 47 deletions datafusion/proto/tests/cases/roundtrip_physical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3666,53 +3666,6 @@ fn test_hash_join_with_dynamic_filter_roundtrip() -> Result<()> {
Ok(())
}

#[test]
fn test_hash_join_swap_clears_dynamic_filter_before_roundtrip() -> Result<()> {
let build_schema = Arc::new(Schema::new(vec![Field::new(
"build_key",
DataType::Int64,
false,
)]));
let probe_schema = Arc::new(Schema::new(vec![
Field::new("probe_payload_0", DataType::Int64, false),
Field::new("probe_payload_1", DataType::Int64, false),
Field::new("probe_key", DataType::Int64, false),
]));

let build_child = Arc::new(EmptyExec::new(Arc::clone(&build_schema)));
let probe_child = Arc::new(EmptyExec::new(Arc::clone(&probe_schema)));
let on: Vec<(Arc<dyn PhysicalExpr>, Arc<dyn PhysicalExpr>)> = vec![(
Arc::new(Column::new("build_key", 0)),
Arc::new(Column::new("probe_key", 2)),
)];

let dynamic_filter = Arc::new(DynamicFilterPhysicalExpr::new(
vec![Arc::new(Column::new("probe_key", 2))],
lit(true),
));
let join = HashJoinExec::try_new(
build_child,
probe_child,
on,
None,
&JoinType::Inner,
None,
PartitionMode::CollectLeft,
NullEquality::NullEqualsNothing,
false,
)?
.with_dynamic_filter_expr(dynamic_filter)?;

let swapped = join.swap_inputs(PartitionMode::CollectLeft)?;

// If HashJoinExec::swap_inputs keeps the old dynamic filter, the swapped
// join still serializes a filter whose child is `probe_key@2`. During
// deserialization, HashJoinExec validates that filter against the new
// right/probe schema (`build_key` only), and fails with an out-of-bounds
// column reference.
roundtrip_test(swapped)
}

/// returns a SessionContext with an empty `netflow` table registered
fn netflow_context() -> Result<SessionContext> {
let ctx = SessionContext::new();
Expand Down
Loading