Skip to content

Fix EliminateLimit not eliminating limits under LogicalPlan::Extension nodes. (#25577) - #436

Merged
masonh22 merged 1 commit into
v55from
take-limit-fix
Sep 28, 2026
Merged

masonh22 merged 1 commit into
v55from
take-limit-fix

Conversation

@masonh22

Copy link
Copy Markdown

Which issue does this PR close?

  • Closes #.

Rationale for this change

apache#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.

What is the testing strategy for this PR?

I added a test that replicates the issue and fails without my fixes.

Are there any user-facing changes?

No

…sion` nodes. (apache#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

apache#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.
-->
@masonh22
masonh22 merged commit 713da15 into v55 Sep 28, 2026
12 checks passed
@masonh22
masonh22 deleted the take-limit-fix branch September 28, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants