galaxytemple opened a new pull request, #27006:
URL: https://github.com/apache/datafusion/pull/27006

   ## Which issue does this PR close?
   - Closes #26690.
   
   ## Rationale for this change
   On a pre-sorted, multi-partition table, an `ORDER BY ... LIMIT` query over 
an `OFFSET` subquery returns fewer rows than requested:
   
   ```sql
   SELECT * FROM (SELECT * FROM t OFFSET 5) ORDER BY a LIMIT 10;
   ```
   In this scenario, replace_with_order_preserving_variants drops the 
unnecessary SortExec and replaces CoalescePartitionsExec with 
SortPreservingMergeExec.
   
   When descending through the `GlobalLimitExec(skip=5)`, it forwarded 
`fetch=10` without adding `skip`.
   So `SortPreservingMergeExec` only kept 10 rows.
   Then `GlobalLimitExec` skipped 5 of them, returning only 5 rows.
   
   ## What changes are included in this PR?
   - Move `input_fetch` to `enforce_sorting/mod.rs` as `pub(crate)` to share it 
across sorting rules, computing required input rows using `RowBound` (#26660).
   - In `plan_with_order_preserving_variants`, pass 
`input_fetch(&sort_input.plan, fetch)` down to children ensuring 
`SortPreservingMergeExec` receives `skip + fetch` rows.
   
   ## What is the testing strategy for this PR?
   - `ensure_requirements.rs`: unit test asserting `SortExec(fetch=10)` over 
`GlobalLimitExec(skip=5)` replaces `CoalescePartitionsExec` with 
`SortPreservingMergeExec(fetch=15)`.
   - `limit.slt`: added the reproducer queries from #26690, verifying `EXPLAIN` 
(`SortPreservingMergeExec` with `fetch=15`) and query results.
   
   ## Are there any user-facing changes?
   No API changes. Bug fix for queries with ORDER BY ... LIMIT over an OFFSET 
subquery returning fewer rows than expected.
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to