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

[PR] Fix `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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

   ## 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 #.
   
   ## Rationale for this change
   
   #22298 updated the logical plan optimizer to apply optimization rules in place to avoid `Arc` unwrap/rewraps with a new `map_children_mut()` function.  This function unconditionally updates the children of all logical plan nodes except for `LogicalPlan::Extension` nodes.  For `Extension` nodes, it only updates the children if the mapping function returns `true` for any of the children, signifying that a child was updated.
   
   When the `EliminateLimit` rule prunes a limit that has 0 skip and fetch, it modifies the node but does not return that it modified anything.  This is fine for every case except for if the limit is underneath a `LogicalPlan::Extension` node, since it that case we ignore the output from the rule.
   
   ## What changes are included in this PR?
   
   This change adds a failing test, a fix to `EliminateLimit`, and changes `map_children_mut()` to unconditionally update the children of `LogicalPlan::Extension` nodes.  The change to `map_children_mut()` could be left out, but I wouldn't be surprised if there are other optimization rules that modify the node without returning `transformed=true`, so this is done to avoid finding and fixing all of those cases.
   
   <!--
   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.
   -->
   
   ## What is the testing strategy for this PR?
   
   I added a test that replicates the issue and fails without my fixes.
   
   <!--
   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
   
   Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'.
   
   If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it.
   
   You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised.
   -->
   
   ## Are there any user-facing changes?
   
   No
   
   <!--
   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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #25577:
URL: https://github.com/apache/datafusion/pull/25577#discussion_r4070737202


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -78,7 +78,10 @@ impl OptimizerRule for EliminateLimit {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
                     #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);

Review Comment:
   unrelated: It is a good moment to rename `_config` to `config` because obviously it is being used.



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #25577:
URL: https://github.com/apache/datafusion/pull/25577#discussion_r4071857582


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -78,7 +78,10 @@ impl OptimizerRule for EliminateLimit {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
                     #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);

Review Comment:
   Looks good to me!



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -78,7 +78,10 @@ impl OptimizerRule for EliminateLimit {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
                     #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);

Review Comment:
   I actually kind of like it like this.  It's only being used for the recursive call, so in a way it is unused.
   
   Although maybe the better way to do this is to make a `rewrite_inner()` function that doesn't have the config argument.  I will try that out



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -78,7 +78,10 @@ impl OptimizerRule for EliminateLimit {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
                     #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);

Review Comment:
   Updated in [3804933](https://github.com/apache/datafusion/pull/25577/commits/3804933cfe1d25e19dc5dde7fbb8981dbf4e5e6d), let me know what you think (I'm also happy to go with what you initially suggested)



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -77,8 +83,9 @@ impl OptimizerRule for EliminateLimit {
                 } else if matches!(limit.get_skip_type()?, SkipType::Literal(0)) {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
-                    #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);
+                    let mut res = Self::rewrite_inner(Arc::unwrap_or_clone(limit.input))?;

Review Comment:
   the idea here is to apply the limit pushdown on the inner as well here? Aka keep recursing



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -77,8 +83,9 @@ impl OptimizerRule for EliminateLimit {
                 } else if matches!(limit.get_skip_type()?, SkipType::Literal(0)) {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
-                    #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);
+                    let mut res = Self::rewrite_inner(Arc::unwrap_or_clone(limit.input))?;

Review Comment:
   Hmmm, good question.  We shouldn't need to recurse here since the mechanism that actually calls the optimizer rules should handle that.  Also, the apply order is `BottomUp` for this so it should've been called on the child already.  So I think this might be unnecessary.  Let me try removing it.



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -77,8 +83,9 @@ impl OptimizerRule for EliminateLimit {
                 } else if matches!(limit.get_skip_type()?, SkipType::Literal(0)) {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
-                    #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);
+                    let mut res = Self::rewrite_inner(Arc::unwrap_or_clone(limit.input))?;

Review Comment:
   the idea here is to apply the limit pushdown on the inner as well here?



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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


##########
datafusion/optimizer/src/eliminate_limit.rs:
##########
@@ -77,8 +83,9 @@ impl OptimizerRule for EliminateLimit {
                 } else if matches!(limit.get_skip_type()?, SkipType::Literal(0)) {
                     // If fetch is `None` and skip is 0, then Limit takes no effect and
                     // we can remove it. Its input also can be Limit, so we should apply again.
-                    #[expect(clippy::used_underscore_binding)]
-                    return self.rewrite(Arc::unwrap_or_clone(limit.input), _config);
+                    let mut res = Self::rewrite_inner(Arc::unwrap_or_clone(limit.input))?;

Review Comment:
   The tests passed on my machine without the recursive call, so I don't think this was necessary.  I removed it in [9fcc605](https://github.com/apache/datafusion/pull/25577/commits/9fcc605de088994411a1b837766e31b36b1f3b8c).



-- 
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 `EliminateLimit` not eliminating limits under `LogicalPlan::Extension` nodes. [datafusion]

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

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/25577?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `72.22222%` with `5 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 82.42%. Comparing base ([`0576a0b`](https://app.codecov.io/gh/apache/datafusion/commit/0576a0b400437ade5a6f3b102465d954a539f263?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`76a5a4c`](https://app.codecov.io/gh/apache/datafusion/commit/76a5a4c717906eb54f1287373de672f2f68c15c0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion/pull/25577?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [datafusion/optimizer/src/eliminate\_limit.rs](https://app.codecov.io/gh/apache/datafusion/pull/25577?src=pr&el=tree&filepath=datafusion%2Foptimizer%2Fsrc%2Feliminate_limit.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9vcHRpbWl6ZXIvc3JjL2VsaW1pbmF0ZV9saW1pdC5ycw==) | 76.47% | [0 Missing and 4 partials :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [datafusion/optimizer/src/optimizer.rs](https://app.codecov.io/gh/apache/datafusion/pull/25577?src=pr&el=tree&filepath=datafusion%2Foptimizer%2Fsrc%2Foptimizer.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-ZGF0YWZ1c2lvbi9vcHRpbWl6ZXIvc3JjL29wdGltaXplci5ycw==) | 0.00% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/datafusion/pull/25577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@           Coverage Diff           @@
   ##             main   #25577   +/-   ##
   =======================================
     Coverage   82.42%   82.42%           
   =======================================
     Files        1139     1139           
     Lines      435372   435386   +14     
     Branches   435372   435386   +14     
   =======================================
   + Hits       358845   358860   +15     
   + Misses      54826    54818    -8     
   - Partials    21701    21708    +7     
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/25577?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


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