fix: The limit_pushdown physical optimization rule removes limits in …#394
Merged
fix: The limit_pushdown physical optimization rule removes limits in …#394
Conversation
…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. -->
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
avantgardnerio
pushed a commit
that referenced
this pull request
Mar 18, 2026
avantgardnerio
pushed a commit
that referenced
this pull request
Apr 8, 2026
avantgardnerio
pushed a commit
that referenced
this pull request
Apr 20, 2026
avantgardnerio
pushed a commit
that referenced
this pull request
Apr 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…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::satisfiedfield 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::satisfiedto true. This is wrong: the limit is not satisfied because fetch doesn't support skip/offset. Instead, we should setGlobalRequirements::satisfiedto 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?