You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/05/26 08:20:31 UTC
[PR] fix: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
kumarUjjawal opened a new pull request, #22525:
URL: https://github.com/apache/datafusion/pull/22525
## 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 #22489.
## Rationale for this change
`LimitPushdown` carries `GlobalRequirements` while walking the physical plan.
In the bad plan shape from #22489, an outer `OFFSET` was already handled above
a sort barrier, but its `skip` still remained in the state when recursion
continued into the child subtree. That stale `skip` then merged with an inner
`LIMIT` and reduced its fetch incorrectly, which caused a grouped row to be
dropped.
The fix is to clear `skip` once the limit requirement has already been handled,
while keeping `fetch` so valid limit pushdown into child sorts still happens.
<!--
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?
- Clear `skip` before recursing into children when the limit requirement is already handled.
- Keep `fetch` unchanged so valid TopK-style pushdown still works.
- Add a physical optimizer regression test for the exact outer `OFFSET` / sort barrier / inner `LIMIT` shape.
- Add an end-to-end sqllogictest for the SQL reproducer from #22489.
<!--
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?
Yes
<!--
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?
No API Change
<!--
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: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #22525:
URL: https://github.com/apache/datafusion/pull/22525#discussion_r3308958410
##########
datafusion/sqllogictest/test_files/limit.slt:
##########
@@ -989,3 +989,34 @@ c-4
statement ok
DROP TABLE t21176;
+
+# Regression test for https://github.com/apache/datafusion/issues/22489
Review Comment:
Thanks you for this
--
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: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #22525:
URL: https://github.com/apache/datafusion/pull/22525#discussion_r3306834780
##########
datafusion/core/tests/physical_optimizer/limit_pushdown.rs:
##########
@@ -714,3 +714,69 @@ fn no_limit_preserves_plan_identity() -> Result<()> {
Ok(())
}
+
+#[test]
+fn outer_offset_does_not_leak_through_sort_into_inner_limit() -> Result<()> {
+ // Regression test for https://github.com/apache/datafusion/issues/22489
+ //
+ // When an outer OFFSET is separated from an inner LIMIT by a SortExec
+ // with different sort keys, the outer skip must not reduce the inner
+ // fetch. Before the fix, combine_limit merged them, producing
+ // GlobalLimitExec(skip=1, fetch=7) instead of preserving the inner
+ // LIMIT 8.
+ //
+ // Plan structure:
+ // GlobalLimitExec: skip=1, fetch=None (outer OFFSET 1)
+ // SortExec: [c1 DESC] (outer sort — different key)
+ // GlobalLimitExec: skip=0, fetch=8 (inner LIMIT 8)
+ // SortExec: [c2 ASC] (inner sort — different key)
+ // EmptyExec
+ let schema = create_schema();
+ let empty = empty_exec(Arc::clone(&schema));
+
+ let inner_ordering: LexOrdering = [PhysicalSortExpr {
+ expr: col("c2", &schema)?,
+ options: SortOptions::default(),
+ }]
+ .into();
+ let inner_sort = sort_exec(inner_ordering, empty);
+ let inner_limit = global_limit_exec(inner_sort, 0, Some(8));
+
+ let outer_ordering: LexOrdering = [PhysicalSortExpr {
+ expr: col("c1", &schema)?,
+ options: SortOptions {
+ descending: true,
+ nulls_first: false,
Review Comment:
I think we should also add a test (or confirm one already exists) where the two sorts **DO** have the same sort and ensure the limit is still pushed
##########
datafusion/core/tests/physical_optimizer/limit_pushdown.rs:
##########
@@ -714,3 +714,69 @@ fn no_limit_preserves_plan_identity() -> Result<()> {
Ok(())
}
+
+#[test]
+fn outer_offset_does_not_leak_through_sort_into_inner_limit() -> Result<()> {
+ // Regression test for https://github.com/apache/datafusion/issues/22489
+ //
+ // When an outer OFFSET is separated from an inner LIMIT by a SortExec
+ // with different sort keys, the outer skip must not reduce the inner
+ // fetch. Before the fix, combine_limit merged them, producing
+ // GlobalLimitExec(skip=1, fetch=7) instead of preserving the inner
+ // LIMIT 8.
+ //
+ // Plan structure:
+ // GlobalLimitExec: skip=1, fetch=None (outer OFFSET 1)
+ // SortExec: [c1 DESC] (outer sort — different key)
+ // GlobalLimitExec: skip=0, fetch=8 (inner LIMIT 8)
+ // SortExec: [c2 ASC] (inner sort — different key)
+ // EmptyExec
+ let schema = create_schema();
+ let empty = empty_exec(Arc::clone(&schema));
+
+ let inner_ordering: LexOrdering = [PhysicalSortExpr {
+ expr: col("c2", &schema)?,
+ options: SortOptions::default(),
+ }]
+ .into();
+ let inner_sort = sort_exec(inner_ordering, empty);
+ let inner_limit = global_limit_exec(inner_sort, 0, Some(8));
+
+ let outer_ordering: LexOrdering = [PhysicalSortExpr {
+ expr: col("c1", &schema)?,
+ options: SortOptions {
+ descending: true,
+ nulls_first: false,
Review Comment:
maybe worth calling out that this is a different sort order
##########
datafusion/physical-optimizer/src/limit_pushdown.rs:
##########
@@ -375,6 +375,14 @@ pub(crate) fn pushdown_limits(
(new_node, global_state) = pushdown_limit_helper(new_node.data, global_state)?;
}
+ // Once a limit has been materialized above the current node, child
+ // subtrees should not inherit its `skip`. Keep `fetch`, but clear
+ // `skip` before recursing so child-local limits are not merged with
+ // an `OFFSET` that has already been applied.
Review Comment:
I am surprised this doesn't need to check for the actual sort keys too 🤔
--
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: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #22525:
URL: https://github.com/apache/datafusion/pull/22525
--
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: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #22525:
URL: https://github.com/apache/datafusion/pull/22525#discussion_r3306660564
##########
datafusion/sqllogictest/test_files/limit.slt:
##########
@@ -989,3 +989,34 @@ c-4
statement ok
DROP TABLE t21176;
+
+# Regression test for https://github.com/apache/datafusion/issues/22489
Review Comment:
I don't think this guards against the issue:
I reverted the code change locally
```diff
diff --git a/datafusion/physical-optimizer/src/limit_pushdown.rs b/datafusion/physical-optimizer/src/limit_pushdown.rs
index 63c4f21bd9..6164d86e53 100644
--- a/datafusion/physical-optimizer/src/limit_pushdown.rs
+++ b/datafusion/physical-optimizer/src/limit_pushdown.rs
@@ -375,14 +375,6 @@ pub(crate) fn pushdown_limits(
(new_node, global_state) = pushdown_limit_helper(new_node.data, global_state)?;
}
- // Once a limit has been materialized above the current node, child
- // subtrees should not inherit its `skip`. Keep `fetch`, but clear
- // `skip` before recursing so child-local limits are not merged with
- // an `OFFSET` that has already been applied.
- if global_state.satisfied {
- global_state.skip = 0;
- }
-
// Apply pushdown limits in children
let children = new_node.data.children();
let mut changed = false;
```
And then I ran the tests:
```shell
cargo test --profile=ci --test sqllogictests
...
Running with 16 test threads (available parallelism: 16)
Completed 472 test files in 9 seconds
```
--
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: clear handled OFFSET before child recursion in LimitPushdown [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #22525:
URL: https://github.com/apache/datafusion/pull/22525#discussion_r3306745292
##########
datafusion/sqllogictest/test_files/limit.slt:
##########
@@ -989,3 +989,34 @@ c-4
statement ok
DROP TABLE t21176;
+
+# Regression test for https://github.com/apache/datafusion/issues/22489
Review Comment:
Here is a proposed fix:
- https://github.com/kumarUjjawal/datafusion/pull/1
--
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