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

   ## 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 #20448.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is alreadya 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.  
   -->
   
   While testing, I found that `ORDER BY ... LIMIT` could panic in TopK when 
interleaving very large `Utf8` values. The problem was that TopK tried to 
interleave all selected rows in a single call, which can overflow Arrow’s i32 
string offsets in extreme cases. This PR removes that panic path so the 
operator behaves safely under large-string workloads.
   
   ## What changes are included in this PR?
   
   <!--
   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.
   -->
   
   - I changed TopK emission to interleave in bounded chunks instead of one 
unbounded interleave.
   - I added panic-safe handling around Arrow interleave so overflow panics are 
handled and surfaced as normal DataFusion errors.
   - I added adaptive chunk splitting: if a chunk overflows, I retry with a 
smaller chunk until it succeeds (or return an error for the single-row edge 
case).
   - I updated TopK compaction to use the same chunked path and keep 
row-to-batch remapping correct.
   - I removed the now-unused `TopK.batch_size` field.
   - I added a focused unit test: 
`test_topk_heap_emit_with_state_respects_batch_size`.
   
   ## Are these changes tested?
   
   <!--
   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)?
   -->
   
   Yes.
   
   - I added a new unit test in `datafusion/physical-plan/src/topk/mod.rs` for 
chunked TopK emission behavior.
   - I ran `cargo fmt --all --check`.
   - I ran `cargo clippy -p datafusion-physical-plan --lib --tests -- -D 
warnings`.
   - I ran `cargo test -p datafusion-physical-plan topk::tests:: -- 
--nocapture` three times after final changes.
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be 
updated before approving the PR.
   -->
   
   Yes. For extreme large-string cases in `ORDER BY ... LIMIT`, TopK no longer 
panics with overflow; it now follows normal error/execution handling.
   
   <!--
   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: [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