neilconway commented on code in PR #23566:
URL: https://github.com/apache/datafusion/pull/23566#discussion_r3596758392
##########
datafusion/optimizer/src/eliminate_join.rs:
##########
@@ -394,6 +406,30 @@ fn rewrite_join(
let (visible_left, visible_right) = split_join_output_columns(&join, live);
+ // A LEFT JOIN preserves every left row, so when nothing above the join
+ // references the right side's columns and the right side cannot multiply
+ // left rows (the ancestors are duplicate-insensitive, or the right side
+ // is unique on the join keys), the join has no observable effect and can
+ // be replaced by its left input. A join filter cannot prevent this: it
+ // only decides whether a left row is matched or null-padded, and either
+ // way the row is emitted.
+ if join.join_type == JoinType::Left
+ && visible_right.is_empty()
+ && (duplicate_insensitive
+ || side_unique_on_join(
+ join.right.schema(),
+ join.on.iter().map(|(_, right)| right),
+ join.null_equality,
+ ))
Review Comment:
Consider refactoring this into a helper, so we can use it here and also in
`rewritten_join_type`?
##########
datafusion/optimizer/src/eliminate_join.rs:
##########
@@ -394,6 +406,30 @@ fn rewrite_join(
let (visible_left, visible_right) = split_join_output_columns(&join, live);
+ // A LEFT JOIN preserves every left row, so when nothing above the join
+ // references the right side's columns and the right side cannot multiply
+ // left rows (the ancestors are duplicate-insensitive, or the right side
+ // is unique on the join keys), the join has no observable effect and can
+ // be replaced by its left input. A join filter cannot prevent this: it
+ // only decides whether a left row is matched or null-padded, and either
+ // way the row is emitted.
+ if join.join_type == JoinType::Left
Review Comment:
What about supporting `RIGHT JOIN`s as well?
##########
datafusion/sqllogictest/test_files/joins.slt:
##########
@@ -5608,3 +5608,316 @@ set datafusion.execution.target_partitions = 4;
statement ok
reset datafusion.execution.batch_size;
+
+##########
+# Eliminate unused LEFT JOINs (`EliminateJoin` rule)
Review Comment:
Can we add test cases for `LIMIT` and `LEFT JOIN LATERAL`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]