-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Resolve empty relation opt for join types #11066
Merged
jonahgao
merged 7 commits into
apache:main
from
LorrensP-2158466:resolve-empty-relation-opt-for-join-types
Jun 25, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7606cad
handle left/right anti with empty join_table & added tests for those
LorrensP-2158466 e4d655e
merge main
LorrensP-2158466 5c49235
add tests and only resolve cases 1,2,3 of issue
LorrensP-2158466 c03ebd7
forgot to change a test
LorrensP-2158466 422ce20
Update datafusion/optimizer/src/propagate_empty_relation.rs
alamb fb2e739
Merge remote-tracking branch 'apache/main' into resolve-empty-relatio…
alamb 58021bf
Merge remote-tracking branch 'apache/main' into resolve-empty-relatio…
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 |
---|---|---|
|
@@ -88,16 +88,20 @@ impl OptimizerRule for PropagateEmptyRelation { | |
|
||
LogicalPlan::Join(ref join) => { | ||
// TODO: For Join, more join type need to be careful: | ||
// For LeftAnti Join, if the right side is empty, the Join result is left side(should exclude null ??). | ||
// For RightAnti Join, if the left side is empty, the Join result is right side(should exclude null ??). | ||
// For Full Join, only both sides are empty, the Join result is empty. | ||
// For LeftOut/Full Join, if the right side is empty, the Join can be eliminated with a Projection with left side | ||
// columns + right side columns replaced with null values. | ||
// For RightOut/Full Join, if the left side is empty, the Join can be eliminated with a Projection with right side | ||
// columns + left side columns replaced with null values. | ||
let (left_empty, right_empty) = binary_plan_children_is_empty(&plan)?; | ||
|
||
match join.join_type { | ||
// For Full Join, only both sides are empty, the Join result is empty. | ||
JoinType::Full if left_empty && right_empty => Ok(Transformed::yes( | ||
LogicalPlan::EmptyRelation(EmptyRelation { | ||
produce_one_row: false, | ||
schema: join.schema.clone(), | ||
}), | ||
)), | ||
JoinType::Inner if left_empty || right_empty => Ok(Transformed::yes( | ||
LogicalPlan::EmptyRelation(EmptyRelation { | ||
produce_one_row: false, | ||
|
@@ -134,13 +138,19 @@ impl OptimizerRule for PropagateEmptyRelation { | |
schema: join.schema.clone(), | ||
}), | ||
)), | ||
JoinType::LeftAnti if right_empty => { | ||
Ok(Transformed::yes((*join.left).clone())) | ||
} | ||
JoinType::RightAnti if left_empty => { | ||
Ok(Transformed::yes((*join.right).clone())) | ||
} | ||
JoinType::RightAnti if right_empty => Ok(Transformed::yes( | ||
LogicalPlan::EmptyRelation(EmptyRelation { | ||
produce_one_row: false, | ||
schema: join.schema.clone(), | ||
}), | ||
)), | ||
_ => Ok(Transformed::no(LogicalPlan::Join(join.clone()))), | ||
_ => Ok(Transformed::no(plan)), | ||
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. 👍 |
||
} | ||
} | ||
LogicalPlan::Aggregate(ref agg) => { | ||
|
@@ -467,8 +477,39 @@ mod tests { | |
assert_together_optimized_plan(plan, expected, eq) | ||
} | ||
|
||
// TODO: fix this long name | ||
fn assert_anti_join_empty_join_table_is_base_table( | ||
anti_left_join: bool, | ||
) -> Result<()> { | ||
// if we have an anti join with an empty join table, then the result is the base_table | ||
let (left, right, join_type, expected) = if anti_left_join { | ||
let left = test_table_scan()?; | ||
let right = LogicalPlanBuilder::from(test_table_scan()?) | ||
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))? | ||
.build()?; | ||
let expected = left.display_indent().to_string(); | ||
(left, right, JoinType::LeftAnti, expected) | ||
} else { | ||
let right = test_table_scan()?; | ||
let left = LogicalPlanBuilder::from(test_table_scan()?) | ||
.filter(Expr::Literal(ScalarValue::Boolean(Some(false))))? | ||
.build()?; | ||
let expected = right.display_indent().to_string(); | ||
(left, right, JoinType::RightAnti, expected) | ||
}; | ||
|
||
let plan = LogicalPlanBuilder::from(left) | ||
.join_using(right, join_type, vec![Column::from_name("a".to_string())])? | ||
.build()?; | ||
|
||
assert_together_optimized_plan(plan, &expected, true) | ||
} | ||
|
||
#[test] | ||
fn test_join_empty_propagation_rules() -> Result<()> { | ||
// test full join with empty left and empty right | ||
assert_empty_left_empty_right_lp(true, true, JoinType::Full, true)?; | ||
|
||
// test left join with empty left | ||
assert_empty_left_empty_right_lp(true, false, JoinType::Left, true)?; | ||
|
||
|
@@ -491,7 +532,13 @@ mod tests { | |
assert_empty_left_empty_right_lp(true, false, JoinType::LeftAnti, true)?; | ||
|
||
// test right anti join empty right | ||
assert_empty_left_empty_right_lp(false, true, JoinType::RightAnti, true) | ||
assert_empty_left_empty_right_lp(false, true, JoinType::RightAnti, true)?; | ||
|
||
// test left anti join empty right | ||
assert_anti_join_empty_join_table_is_base_table(true)?; | ||
|
||
// test right anti join empty left | ||
assert_anti_join_empty_join_table_is_base_table(false) | ||
} | ||
|
||
#[test] | ||
|
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
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.
I double checked that an ANTI join (e.g. a
NOT IN
) is true when there are no tuples (aka the right side is emprt)Thus I think this is a valid rewrite
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.
Nice, thanks for the double check!