Repository navigation
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the changes that propagate eliminated-sort fetch limits through eligible operators.
No open issues 🎉
What changed in this PR
This PR lets PushdownSort pass an eliminated sort’s fetch through projections and other eligible operators, allowing custom sources to receive the limit even after LimitPushdown has run.
Changes:
- Reuses limit-pushdown logic with a statistics context while preserving global and per-partition limit semantics.
- Updates test sources to support exact sort pushdown and preserve partitioning.
- Adds eight plan tests and two execution tests covering projections, cooperative execution, offsets, partitioned sorts, and repartition barriers.
| File | Description |
|---|---|
datafusion/physical-optimizer/src/pushdown_sort.rs |
Adds optimize_with_context and pushes fetch limits down after sort elimination. |
datafusion/physical-optimizer/src/limit_pushdown.rs |
Adds the crate-visible push_down_limits helper for reuse by other rules. |
datafusion/core/tests/physical_optimizer/test_utils.rs |
Adds exact_memory_exec and updates test-source limit and partitioning behavior. |
datafusion/core/tests/physical_optimizer/pushdown_sort.rs |
Adds plan and execution coverage for fetch pushdown. |
Review effort: Balanced
|
@zhuqi-lucas I'm trying to review this but struggling to wade through refactor diff vs. change diff. For future: could I ask that you make 2 commits: a refactor commit (in this case factoring away sort pushdown into |
|
Another minor piece of feedback: I had to think a bit (and ask an agent to confirm) that this is a missed-optimization fix, not a bug fix. It was not clear from the title / description if this is fixing a bug / incorrect results or a performance improvement. |
1298bb8 to
a2952bd
Compare
adriangb
left a comment
There was a problem hiding this comment.
Thank you @zhuqi-lucas! The reuse of the LimitPushdown logic keeps this change small. I left a few small suggestions about the tests.
| // A leaf: it takes a limit as its own fetch, like `DataSourceExec`. | ||
| fn limit_behavior(&self) -> LimitBehavior { | ||
| LimitBehavior::Stops | ||
| } |
There was a problem hiding this comment.
Nit: could we remove both methods? DataSourceExec overrides neither of them. It accepts a limit through with_fetch, and TestScan keeps that method. supports_limit_pushdown tells the optimizer that a limit can pass through to the input, but a leaf has no input. The default limit_behavior is already Stops, so the pushdown_sort tests pass with this change:
- fn supports_limit_pushdown(&self) -> bool {
- self.supports_fetch
- }
-
- // A leaf: it takes a limit as its own fetch, like `DataSourceExec`.
- fn limit_behavior(&self) -> LimitBehavior {
- LimitBehavior::Stops
- }
-The LimitBehavior import is then unused.
| 03)----SortPreservingMergeExec: [group_id@0 ASC NULLS LAST, value@2 ASC NULLS LAST], fetch=5 | ||
| 04)------BufferExec: capacity=1073741824 | ||
| 05)--------ProjectionExec: expr=[group_id@0 as group_id, CAST(value@1 AS Int64) + 1 as v, value@1 as value] | ||
| 06)----------ExactSortSource: fetch=5 |
There was a problem hiding this comment.
The PR description says that each case also has an OFFSET test. Could we add the one for the source without a fetch? This passes locally.
| 06)----------ExactSortSource: fetch=5 | |
| 06)----------ExactSortSource: fetch=5 | |
| query TT | |
| EXPLAIN SELECT group_id, value + 1 AS v | |
| FROM exact_sort_source | |
| ORDER BY group_id, value | |
| LIMIT 3 OFFSET 2; | |
| ---- | |
| physical_plan | |
| 01)ProjectionExec: expr=[group_id@0 as group_id, v@1 as v] | |
| 02)--GlobalLimitExec: skip=2, fetch=3 | |
| 03)----SortPreservingMergeExec: [group_id@0 ASC NULLS LAST, value@2 ASC NULLS LAST], fetch=5 | |
| 04)------BufferExec: capacity=1073741824 | |
| 05)--------ProjectionExec: expr=[group_id@0 as group_id, CAST(value@1 AS Int64) + 1 as v, value@1 as value] | |
| 06)----------LocalLimitExec: fetch=5 | |
| 07)------------ExactSortSource |
| ---- | ||
| 0 3 | ||
| 0 4 | ||
| 0 5 |
There was a problem hiding this comment.
Could we also check the rows for the LocalLimitExec path? These queries pass locally.
| 0 5 | |
| 0 5 | |
| query II | |
| SELECT group_id, value + 1 AS v | |
| FROM exact_sort_source | |
| ORDER BY group_id, value | |
| LIMIT 3; | |
| ---- | |
| 0 1 | |
| 0 2 | |
| 0 3 | |
| query II | |
| SELECT group_id, value + 1 AS v | |
| FROM exact_sort_source | |
| ORDER BY group_id, value | |
| LIMIT 3 OFFSET 2; | |
| ---- | |
| 0 3 | |
| 0 4 | |
| 0 5 |
| // The limit of the eliminated SortExec passes the projection and goes | ||
| // into the source, instead of staying above the projection. |
There was a problem hiding this comment.
Nit: the comment can describe only the current behavior.
| // The limit of the eliminated SortExec passes the projection and goes | |
| // into the source, instead of staying above the projection. | |
| // The limit of the eliminated SortExec passes the projection and goes | |
| // into the source. |
| #[test] | ||
| fn test_sort_pushdown_exact_partitioned_pushes_fetch_through_projection() { | ||
| // SPM -> SortExec(preserve_partitioning): each partition keeps its own | ||
| // fetch, now in the source below the projection. |
There was a problem hiding this comment.
Nit: same here.
| // fetch, now in the source below the projection. | |
| // fetch, in the source below the projection. |
xudong963
left a comment
There was a problem hiding this comment.
it routes ORDER BY … LIMIT into a hole in LimitPushdown that silently drops the limit for custom leaves using the deprecated supports_limit_pushdown() hook. Can we consider this case?
Claude caught the same thing on my end and freaked out about it. I was writing this artisanal comment because I disagree with Claude on this one. IMO we could handle it, but not in this PR. It's a pre-existing issue, this PR just exposes new query patterns to it. Not just that: it only happens when deprecated methods are involved. What I propose: we file an issue for it and don't block this PR. THen in that issue we can discuss if it's worth fixing, etc. |
Thanks @xudong963 and @adriangb! Filed #27891 for the leaf case. It's pre-existing, a plain |
|
🧑🍳 👌 |
Which issue does this PR close?
None.
Rationale for this change
This is a missed optimization, not a wrong-results bug: the rows are the same either way, but the source never gets the fetch.
When
PushdownSortremoves aSortExecthat has a fetch, it hands the fetch to the node right below the sort, or puts a limit node on top of it. Nodes likeProjectionExecandCooperativeExectake no fetch, so the limit stays above them and the source never sees it.PushdownSortruns afterLimitPushdown, so nothing moves it later.A parquet scan hits this when an expression repeats, here on a table whose files
PushdownSortputs in order by their statistics. The scan absorbs the common subexpression, but not the projection that uses it twice, since that would compute it twice:With this PR the scan gets
limit=2. Custom sources that keep aProjectionExecabove them hit the same thing.What changes are included in this PR?
PushdownSortputs the limit node on top of the new input as before, then pushes it down withLimitPushdown's own logic. Each node'slimit_behaviordecides where it goes: it passes nodes that keep the row count, such asProjectionExecandCooperativeExec, stops above nodes it can't cross, such asRepartitionExec, and lands in the first node that takes it as a fetch.limit_pushdown.rsgets a crate-privatepush_down_limitsentry point for this. The first commit only moves the rule body into a function. The second is the fix, and the third adds the tests.TestScankeeps its partitioning when it accepts a sort, and reports itself as a leaf that takes a limit as its fetch. Theexact_sort_sourceused bysort_pushdown.sltgets a variant that takes a fetch,exact_sort_fetch_source.Are these changes tested?
Yes, in
pushdown_sort.rs: 8 plan tests (through projections andCooperativeExec, sources with and without fetch support, OFFSET, partitioned sorts, and a repartition the limit must not pass) and 2 tests that run the optimized plan and check the rows.In
sort_pushdown.slt, plans and results for the parquet query above, and for a custom source below a projection: the fetch ends up in the source when it takes one, and in aLocalLimitExecright above it otherwise, each also with OFFSET. Without the fix, all five plans keep the limit above the projection.Are there any user-facing changes?
No.