You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2026/09/06 02:34:24 UTC
[PR] fix: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
jayzhan211 opened a new pull request, #24958:
URL: https://github.com/apache/datafusion/pull/24958
## Which issue does this PR close?
- Closes #.
## Rationale for this change
`ORDER BY` applied on top of a `LIMIT ... OFFSET` subquery returns too few rows.
```sql
SELECT * FROM (SELECT a FROM t1 LIMIT 4 OFFSET 3) ORDER BY a;
```
`EnforceSorting` pushes the sort below the `GlobalLimitExec` and converts it into a
TopK. The TopK's fetch was seeded from `GlobalLimitExec::fetch()`, which is the
limit's *output* row count and ignores `skip`. So the TopK kept only 4 rows, the
limit then skipped 3 of them, and the query returned a single row instead of 4.
The same seeding happens in two places in `sort_pushdown.rs`: when a root's direct
child is a `GlobalLimitExec` (`assign_initial_requirements`) and when the pushdown
descends through one (`pushdown_sorts_helper`). Both had the bug.
## What changes are included in this PR?
- Add an `input_fetch` helper in `sort_pushdown.rs` that returns `skip + fetch` for
`GlobalLimitExec` and plain `fetch()` for every other operator.
- Use it at both sites where the pushed-down fetch is derived from the plan node, so
the TopK below a `GlobalLimitExec` retains enough rows for the limit to skip and
still return `fetch` rows.
## What is the testing strategy for this PR?
- `limit.slt`: new `EXPLAIN` + result test for `LIMIT 4 OFFSET 3` under `ORDER BY`,
showing `TopK(fetch=7)` below `GlobalLimitExec: skip=3, fetch=4` and the correct
4 rows.
- `ensure_requirements.rs`: two unit tests, one per code path
(`test_sort_pushed_below_limit_with_skip_keeps_skip_rows` and
`test_parent_ordering_over_limit_with_skip_keeps_skip_rows`), asserting the
pushed sort has `fetch=15` for `skip=5, fetch=10`.
## Are there any user-facing changes?
Queries with a sort above `LIMIT ... OFFSET` now return the correct number of rows.
No API changes.
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#discussion_r3950928298
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -364,7 +386,7 @@ fn pushdown_sorts_helper(
// For operators that can take a sort pushdown, continue with updated
// requirements. If this node already outputs single partition (e.g. SPM),
// don't push SinglePartition to children.
- let current_fetch = sort_push_down.plan.fetch();
+ let current_fetch = input_fetch(&sort_push_down.plan);
Review Comment:
This make sense!
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "ryux1 (via GitHub)" <gi...@apache.org>.
ryux1 commented on code in PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#discussion_r3944295482
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -79,6 +79,22 @@ impl Default for ParentRequirements {
pub type SortPushDown = PlanContext<ParentRequirements>;
+/// Number of input rows `plan` needs from its children in order to produce
+/// its own `fetch` rows. This is `plan.fetch()` for every operator except
+/// [`GlobalLimitExec`], which discards `skip` rows first and therefore needs
+/// `skip + fetch` input rows. Using the bare `fetch` there would turn
+/// `LIMIT 10 OFFSET 5` into `TopK(10)` below the limit, i.e. 5 result rows.
+///
+/// Note this is distinct from the fetch a parent imposes on `plan`'s *output*
+/// (`ParentRequirements::fetch`), for which `plan.fetch()` is the right bound.
+fn input_fetch(plan: &Arc<dyn ExecutionPlan>) -> Option<usize> {
+ let fetch = plan.fetch()?;
+ let skip = plan
+ .downcast_ref::<GlobalLimitExec>()
+ .map_or(0, |limit| limit.skip());
+ Some(fetch + skip)
Review Comment:
`GlobalLimitExec::new` accepts arbitrary `usize` values, so this addition can overflow when `skip + fetch > usize::MAX`: debug builds panic, while optimized builds wrap and may turn the pushed sort into e.g. `TopK(fetch=0)`, producing incorrect results. DataFusion's `combine_limit` uses `saturating_add` for the analogous limit composition. Could this use `fetch.saturating_add(skip)` as well, with a unit test covering `skip = usize::MAX, fetch = 1` (or an equivalent overflow boundary)?
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#issuecomment-5572594602
Thanks for addressing, sent to merge!
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#discussion_r3949591336
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -79,6 +79,22 @@ impl Default for ParentRequirements {
pub type SortPushDown = PlanContext<ParentRequirements>;
+/// Number of input rows `plan` needs from its children in order to produce
+/// its own `fetch` rows. This is `plan.fetch()` for every operator except
+/// [`GlobalLimitExec`], which discards `skip` rows first and therefore needs
+/// `skip + fetch` input rows. Using the bare `fetch` there would turn
+/// `LIMIT 10 OFFSET 5` into `TopK(10)` below the limit, i.e. 5 result rows.
+///
+/// Note this is distinct from the fetch a parent imposes on `plan`'s *output*
+/// (`ParentRequirements::fetch`), for which `plan.fetch()` is the right bound.
+fn input_fetch(plan: &Arc<dyn ExecutionPlan>) -> Option<usize> {
+ let fetch = plan.fetch()?;
+ let skip = plan
+ .downcast_ref::<GlobalLimitExec>()
+ .map_or(0, |limit| limit.skip());
+ Some(fetch + skip)
Review Comment:
This make sense, thanks @ryux1
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb merged PR #24958:
URL: https://github.com/apache/datafusion/pull/24958
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "ryux1 (via GitHub)" <gi...@apache.org>.
ryux1 commented on code in PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#discussion_r3949756608
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -79,6 +79,22 @@ impl Default for ParentRequirements {
pub type SortPushDown = PlanContext<ParentRequirements>;
+/// Number of input rows `plan` needs from its children in order to produce
+/// its own `fetch` rows. This is `plan.fetch()` for every operator except
+/// [`GlobalLimitExec`], which discards `skip` rows first and therefore needs
+/// `skip + fetch` input rows. Using the bare `fetch` there would turn
+/// `LIMIT 10 OFFSET 5` into `TopK(10)` below the limit, i.e. 5 result rows.
+///
+/// Note this is distinct from the fetch a parent imposes on `plan`'s *output*
+/// (`ParentRequirements::fetch`), for which `plan.fetch()` is the right bound.
+fn input_fetch(plan: &Arc<dyn ExecutionPlan>) -> Option<usize> {
+ let fetch = plan.fetch()?;
+ let skip = plan
+ .downcast_ref::<GlobalLimitExec>()
+ .map_or(0, |limit| limit.skip());
+ Some(fetch + skip)
Review Comment:
Looks good—the `saturating_add` change and the `usize::MAX` regression test address the boundary. Thanks!
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#issuecomment-5589601020
🚀
--
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: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#issuecomment-5556541029
## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/24958?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 81.62%. Comparing base ([`3dfa245`](https://app.codecov.io/gh/apache/datafusion/commit/3dfa245d61dde362e2c077761783bc735931659c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`0c73442`](https://app.codecov.io/gh/apache/datafusion/commit/0c734428cf1b21e27644ca56a6bc8137e5cb22eb?dropdown=coverage&el=desc&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 #24958 +/- ##
==========================================
- Coverage 81.62% 81.62% -0.01%
==========================================
Files 1124 1124
Lines 412462 412469 +7
Branches 412462 412469 +7
==========================================
- Hits 336684 336678 -6
- Misses 55965 55973 +8
- Partials 19813 19818 +5
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/24958?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
Re: [PR] fix: sort pushed below `GlobalLimitExec` ignores `skip`, returning too few rows [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #24958:
URL: https://github.com/apache/datafusion/pull/24958#discussion_r3950720874
##########
datafusion/physical-optimizer/src/ensure_requirements/enforce_sorting/sort_pushdown.rs:
##########
@@ -364,7 +386,7 @@ fn pushdown_sorts_helper(
// For operators that can take a sort pushdown, continue with updated
// requirements. If this node already outputs single partition (e.g. SPM),
// don't push SinglePartition to children.
- let current_fetch = sort_push_down.plan.fetch();
+ let current_fetch = input_fetch(&sort_push_down.plan);
Review Comment:
This value and the `min_fetch` call below are correct only because `parent_fetch` is `None` here for a `GlobalLimitExec`. The check in `can_push_fetch_through` makes sure of this.
If `parent_fetch` is `Some`, the correct value is `min(fetch, parent_fetch) + skip`. The current code gives `min(fetch + skip, parent_fetch)`. That value is too small.
To make the code correct in all cases, give parent_fetch to input_fetch. In input_fetch, apply min_fetch(plan.fetch(), parent_fetch) first, then add skip. Then set child.data.fetch = current_fetch below and remove the second min_fetch.
A comment documenting why this is correct / how the invariant is enforced would also suffice.
--
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