Skip to content

fix: The limit_pushdown physical optimization rule removes limits in …#394

Merged
masonh22 merged 1 commit intotp_46from
limit-pushdown-v46
Feb 2, 2026
Merged

fix: The limit_pushdown physical optimization rule removes limits in …#394
masonh22 merged 1 commit intotp_46from
limit-pushdown-v46

Conversation

@masonh22
Copy link
Copy Markdown

…some cases leading to incorrect results (apache#20048)

None

Bug 1: When pushing down limits, we recurse down the physical plan accumulating limits until we reach a node where we can't push the limit down further. At this point, we insert another limit executor (or push it into the current node, if that node supports it). After this, we continue recursing to try to find more limits to push down. If we do find another, we remove it, but we don't set the
GlobalRequirements::satisfied field back to false, meaning we don't always re-insert this limit.

Bug 2: When we're pushing down a limit with a skip/offset and no fetch/limit and we run into a node that supports fetch, we set GlobalRequirements::satisfied to true. This is wrong: the limit is not satisfied because fetch doesn't support skip/offset. Instead, we should set GlobalRequirements::satisfied to true if skip/offset is 0.

This includes a one-line change to the push down limit logic that fixes the issue.

I added a test that replicates the issue and fails without this change.

No

Which issue does this PR close?

Closes #.

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

…some cases leading to incorrect results (apache#20048)

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

None

<!--
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.
-->
Bug 1: When pushing down limits, we recurse down the physical plan
accumulating limits until we reach a node where we can't push the limit
down further. At this point, we insert another limit executor (or push
it into the current node, if that node supports it). After this, we
continue recursing to try to find more limits to push down. If we do
find another, we remove it, but we don't set the
`GlobalRequirements::satisfied` field back to false, meaning we don't
always re-insert this limit.

Bug 2: When we're pushing down a limit with a skip/offset and no
fetch/limit and we run into a node that supports fetch, we set
`GlobalRequirements::satisfied` to true. This is wrong: the limit is not
satisfied because fetch doesn't support skip/offset. Instead, we should
set `GlobalRequirements::satisfied` to true if skip/offset is 0.

<!--
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.
-->
This includes a one-line change to the push down limit logic that fixes
the issue.

<!--
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)?
-->

I added a test that replicates the issue and fails without this change.

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
@github-actions github-actions Bot added the core label Jan 30, 2026
@masonh22 masonh22 merged commit dbb753f into tp_46 Feb 2, 2026
23 checks passed
@masonh22 masonh22 deleted the limit-pushdown-v46 branch February 2, 2026 15:10
dispanser pushed a commit that referenced this pull request Feb 3, 2026
…some cases leading to incorrect results (apache#20048) (#394)

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

None

<!--
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.
-->
Bug 1: When pushing down limits, we recurse down the physical plan
accumulating limits until we reach a node where we can't push the limit
down further. At this point, we insert another limit executor (or push
it into the current node, if that node supports it). After this, we
continue recursing to try to find more limits to push down. If we do
find another, we remove it, but we don't set the
`GlobalRequirements::satisfied` field back to false, meaning we don't
always re-insert this limit.

Bug 2: When we're pushing down a limit with a skip/offset and no
fetch/limit and we run into a node that supports fetch, we set
`GlobalRequirements::satisfied` to true. This is wrong: the limit is not
satisfied because fetch doesn't support skip/offset. Instead, we should
set `GlobalRequirements::satisfied` to true if skip/offset is 0.

<!--
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.
-->
This includes a one-line change to the push down limit logic that fixes
the issue.

<!--
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)?
-->

I added a test that replicates the issue and fails without this change.

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
dispanser pushed a commit that referenced this pull request Feb 19, 2026
…some cases leading to incorrect results (apache#20048) (#394)

---
[Cherry-pick summary: v46→v47]
Source commit: 9e4cda9 (fix: limit_pushdown removes limits incorrectly (apache#20048) (#394))
Strategy: cherry-picked cleanly
Upstream PR: apache#20048 (not in v47)
Test coverage: adequate (adds 2 regression tests for the two bugs fixed)
Tests: cargo nextest run -p datafusion-physical-optimizer passed
dispanser pushed a commit that referenced this pull request Feb 26, 2026
…some cases leading to incorrect results (apache#20048) (#394)

---
[Cherry-pick summary: v46→v47]
Source commit: 9e4cda9 (fix: limit_pushdown removes limits incorrectly (apache#20048) (#394))
Strategy: cherry-picked cleanly
Upstream PR: apache#20048 (not in v47)
Test coverage: adequate (adds 2 regression tests for the two bugs fixed)
Tests: cargo nextest run -p datafusion-physical-optimizer passed
dispanser pushed a commit that referenced this pull request Feb 26, 2026
…some cases leading to incorrect results (apache#20048) (#394)

---
[Cherry-pick summary: v46→v47]
Source commit: 9e4cda9 (fix: limit_pushdown removes limits incorrectly (apache#20048) (#394))
Strategy: cherry-picked cleanly
Upstream PR: apache#20048 (not in v47)
Test coverage: adequate (adds 2 regression tests for the two bugs fixed)
Tests: cargo nextest run -p datafusion-physical-optimizer passed
dispanser pushed a commit that referenced this pull request Feb 26, 2026
…some cases leading to incorrect results (apache#20048) (#394)

---
[Cherry-pick summary: v46→v47]
Source commit: 9e4cda9 (fix: limit_pushdown removes limits incorrectly (apache#20048) (#394))
Strategy: cherry-picked cleanly
Upstream PR: apache#20048 (not in v47)
Test coverage: adequate (adds 2 regression tests for the two bugs fixed)
Tests: cargo nextest run -p datafusion-physical-optimizer passed
dispanser pushed a commit that referenced this pull request Feb 26, 2026
…some cases leading to incorrect results (apache#20048) (#394)

---
[Cherry-pick summary: v46→v47]
Source commit: 9e4cda9 (fix: limit_pushdown removes limits incorrectly (apache#20048) (#394))
Strategy: cherry-picked cleanly
Upstream PR: apache#20048 (not in v47)
Test coverage: adequate (adds 2 regression tests for the two bugs fixed)
Tests: cargo nextest run -p datafusion-physical-optimizer passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant