You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "jonahgao (via GitHub)" <gi...@apache.org> on 2024/07/28 12:09:10 UTC
[PR] Fix bug in `remove_join_expressions` [datafusion]
jonahgao opened a new pull request, #11693:
URL: https://github.com/apache/datafusion/pull/11693
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #11621.
## Rationale for this change
Given an example query:
```sql
SELECT * FROM t1 JOIN t2 ON t1.v1 = t2.v1 WHERE (t1.v1 == t2.v1) OR t1.v1
```
When removing the join predicate `t1.v1 = t2.v1` from the parent filter `(t1.v1 == t2.v1) OR t1.v1`, this process can be considered as replacing the join predicate with `true`. The result should be `TRUE OR t1.v1` and further simplified to empty, but currently, it is `t1.v1`. This will lead to incorrect join results.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
Yes
## Are there any user-facing changes?
No
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#discussion_r1694234395
##########
datafusion/optimizer/src/eliminate_cross_join.rs:
##########
@@ -1057,7 +1071,7 @@ mod tests {
"Filter: (t4.c < UInt32(15) OR t4.c = UInt32(688)) AND (t4.c < UInt32(15) OR t3.c = UInt32(688) OR t3.b = t4.b) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
" Inner Join: t3.a = t4.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
" Inner Join: t1.a = t3.a [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
- " Filter: t2.c < UInt32(15) AND t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
+ " Filter: t2.c = UInt32(688) [a:UInt32, b:UInt32, c:UInt32, a:UInt32, b:UInt32, c:UInt32]",
Review Comment:
Remove `t1.a = t2.a` from `(t1.a = t2.a OR t2.c < 15) AND (t1.a = t2.a AND tc.2 = 688)`, and the result should be ` tc.2 = 688`
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#issuecomment-2257370231
Thanks for the review @alamb
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#discussion_r1695999958
##########
datafusion/optimizer/src/eliminate_cross_join.rs:
##########
@@ -402,7 +400,18 @@ fn remove_join_expressions(expr: Expr, join_keys: &JoinKeySet) -> Option<Expr> {
_ => None,
}
}
-
+ Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == Operator::Or => {
+ let l = remove_join_expressions(*left, join_keys);
+ let r = remove_join_expressions(*right, join_keys);
+ match (l, r) {
+ (Some(ll), Some(rr)) => Some(Expr::BinaryExpr(BinaryExpr::new(
+ Box::new(ll),
+ op,
+ Box::new(rr),
+ ))),
+ _ => None,
Review Comment:
That might be useful to encode as a comment:
```suggestion
// When either `left` or `right` is empty, it means they are `true`
// so OR'ing anything with them will also be true
_ => None,
```
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#discussion_r1694234151
##########
datafusion/sqllogictest/test_files/join.slt:
##########
@@ -1046,6 +1046,19 @@ true false
true NULL
true NULL
+# Test issue: https://github.com/apache/datafusion/issues/11621
+query BB
+SELECT * FROM t1 JOIN t2 ON t1.v1 = t2.v1 WHERE (t1.v1 == t2.v1) OR t1.v1;
Review Comment:
This query returns an empty result on the current main.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#issuecomment-2254503374
The failure of clippy is unrelated to the current changes.
> error: use of deprecated method `arrow_schema::Schema::all_fields`: Use `flattened_fields` instead
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #11693:
URL: https://github.com/apache/datafusion/pull/11693#discussion_r1694234031
##########
datafusion/optimizer/src/eliminate_cross_join.rs:
##########
@@ -402,7 +400,18 @@ fn remove_join_expressions(expr: Expr, join_keys: &JoinKeySet) -> Option<Expr> {
_ => None,
}
}
-
+ Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == Operator::Or => {
+ let l = remove_join_expressions(*left, join_keys);
+ let r = remove_join_expressions(*right, join_keys);
+ match (l, r) {
+ (Some(ll), Some(rr)) => Some(Expr::BinaryExpr(BinaryExpr::new(
+ Box::new(ll),
+ op,
+ Box::new(rr),
+ ))),
+ _ => None,
Review Comment:
When either `left` or `right` is empty, it means they are `true`.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Fix bug in `remove_join_expressions` [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao merged PR #11693:
URL: https://github.com/apache/datafusion/pull/11693
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org