You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "haohuaijin (via GitHub)" <gi...@apache.org> on 2026/06/22 08:39:21 UTC

[PR] fix: drop dynamic filter in hash-join when swap inputs [datafusion]

haohuaijin opened a new pull request, #23078:
URL: https://github.com/apache/datafusion/pull/23078

   ## 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 #23077 
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## 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?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456625871


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   I opened a separate issue to discuss the broader public API contract of `HashJoinExec::swap_inputs()` for downstream optimizer usage: #23105
   
   For this PR, should we keep it scoped to making `swap_inputs()` drop the stale dynamic filter and documenting that behavior, or would you prefer this PR only add an assertion/precondition for now?



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456575915


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   yes, we directly call the `swap_inputs()` after the dynamic filter pushdown rule(by add a new physical optmizer rule via `SessionStateBuilder::with_physical_optimizer_rule`), it will not trigger by the datafusion.



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456575915


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   yes, we directly call the `swap_inputs()` after the dynamic filter pushdown rule(by add a new physical optmizer rule via `SessionStateBuilder::with_physical_optimizer_rule`).



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456469412


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   i try it, but not easy to reproduce with the sql. this bug is trigger when ser/de the physical plan [fc93043](https://github.com/apache/datafusion/pull/23078/commits/fc93043a4740aea76b0016f7a6cc019ba3cbd6da) (you can check this test case for more detail). i will give more try to reproduce this by sql.



-- 
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: add assert to `HashJoinExec::swap_inputs` [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#issuecomment-4777460109

   Thanks for your review @2010YOUY01 @Dandandan 


-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456455131


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   Could we add an end-to-end test that explains why this behavior is desired β€” that `swap_inputs()` should drop the dynamic filter? It’s a bit hard to infer the test goal from this unit test alone.
   
   It should be some SQL queries that used to have bug in results, but enforcing this fix make them correct.



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3457756502


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   I think for now we should only assert that the dynamic filter is `None` when calling `swap_inputs()`.
   
   If your use case can follow the existing restriction, it should be both simpler and safer: it is very natural to first decide the join order and then construct dynamic filters, rather than build some dynamic filters -> swap the join order -> build the remaining dynamic filters.
   
   This likely means ensuring extension physical optimizer rules follow the same restriction: dynamic filter construction rules should be placed after rules that swap join input orders.
   
   If there are cases that cannot be solved this way, we can discuss them separately.



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456561811


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   I'm wondering: are you using `swap_inputs()` directly downstream? πŸ€”
   
   I think the root cause is that `swap_inputs()` has not been clearly specified. It implicitly assumes a bunch of preconditions, so it is only mostly safe to use internally when we strictly follow the optimizer rule order.
   
   However, it is now a public API, so downstream usages may trigger subtle bugs if users are unaware of those preconditions.
   
   See the optimizer rule order below. Internally, `swap_inputs()` is only called inside the optimizer rule above, so we can ensure it is called only when `dynamic_filter` is `None`:
   
   https://github.com/apache/datafusion/blob/46b508eb4019868a1d4ee676f6604f298cb3a862/datafusion/physical-optimizer/src/optimizer.rs#L161
   
   I tried adding `assert!(self.dynamic_filter.is_none())` at the beginning of `HashJoinExec::swap_inputs()`, and the internal tests still pass.
   
   So I suggest we discuss separately what extra contract you need for downstream usage, and then extend `swap_inputs()` accordingly. (and also add the above assertions to prevent similar issues)



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3458174573


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   apply suggestion in [1d85fc7](https://github.com/apache/datafusion/pull/23078/commits/1d85fc792dbe4e16141c4922554f3de55d6c7f66)
   
   follow the existing restriction will solve the issue, but i must copy the below code and insert my optimizer rule into itπŸ˜‚.
   
   https://github.com/apache/datafusion/blob/46b508eb4019868a1d4ee676f6604f298cb3a862/datafusion/physical-optimizer/src/optimizer.rs#L144-L250



-- 
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: add assert to `HashJoinExec::swap_inputs` [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 merged PR #23078:
URL: https://github.com/apache/datafusion/pull/23078


-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456469412


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   i try it, but not easy to reproduce with the sql. this bug is trigger when ser/de the physical plan [fc93043](https://github.com/apache/datafusion/pull/23078/commits/fc93043a4740aea76b0016f7a6cc019ba3cbd6da),
   i will give more try to reproduce this by sql.



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3456561811


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   I'm wondering: are you using `swap_inputs()` directly downstream? πŸ€”
   
   I think the root cause is that `swap_inputs()` has not been clearly specified. It implicitly assumes a bunch of preconditions, so it is only mostly safe to use internally when we strictly follow the optimizer rule order.
   
   However, it is now a public API, so downstream usages may trigger subtle bugs if users are unaware of those preconditions.
   
   See the optimizer rule order below. Internally, `swap_inputs()` is only called inside that optimizer rule, so we can ensure it is called only when `dynamic_filter` is `None`:
   
   https://github.com/apache/datafusion/blob/46b508eb4019868a1d4ee676f6604f298cb3a862/datafusion/physical-optimizer/src/optimizer.rs#L161
   
   I tried adding `assert!(self.dynamic_filter.is_none())` at the beginning of `HashJoinExec::swap_inputs()`, and the internal tests still pass.
   
   So I suggest we discuss separately what extra contract you need for downstream usage, and then extend `swap_inputs()` accordingly. (and also add the above assertions to prevent similar issues)



-- 
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: add assert to `HashJoinExec::swap_inputs` [datafusion]

Posted by "haohuaijin (via GitHub)" <gi...@apache.org>.
haohuaijin commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3458351353


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   > There are lots of implicit assumptions like 'rule A must be run after rule B', so if your extension rule might require certain properties, it's a good idea to add more tests and assertion guards to DataFusion core.
   
   Good idea



-- 
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: drop dynamic filter in hash-join when swap inputs [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #23078:
URL: https://github.com/apache/datafusion/pull/23078#discussion_r3458282642


##########
datafusion/physical-plan/src/joins/hash_join/exec.rs:
##########
@@ -6610,6 +6611,63 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_swap_inputs_clears_dynamic_filter() -> Result<()> {

Review Comment:
   Yes I agree, adding custom rule is quite tricky.
   
   There are lots of implicit assumptions like 'rule A must be run after rule B', so if your extension rule might require certain properties, it's a good idea to add more tests and assertion guards to DataFusion core.



-- 
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