Skip to content

Avoid eager merging of expanding-key ingestion spills - #27043

Open
brancz wants to merge 1 commit into
apache:mainfrom
polarsignals:fix/ingestion-independent-sort-runs-upstream
Open

brancz wants to merge 1 commit into
apache:mainfrom
polarsignals:fix/ingestion-independent-sort-runs-upstream

Conversation

@brancz

@brancz brancz commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Related to #26940.

Rationale for this change

Multi-key sorts can run out of memory while trying to spill. Initializing merge cursors for all buffered batches can expand dictionary keys beyond the available memory before any data is written.

What changes are included in this PR?

Spill buffered batches as independent sorted runs for large multi-key sorts with potentially expanding keys when the buffered reservation exceeds reserved merge workspace. Keep the existing path otherwise.

The workspace comparison is a heuristic, not a bound on encoded key memory.

Related to #26953 and #26987. #26953 reduces merge input batch sizes; this avoids the ingestion-time merge for selected spills, including small batches that #26953 leaves unchanged. #26987 bounds the batches written to spill files for subsequent merging.

Also complementary to #27015, which reduces dictionary data written to disk.

What is the testing strategy for this PR?

Unit tests for wide dictionary keys and reservation lifetimes during async writes, including errors and cancellation. Extended workspace tests and Clippy pass.

Are there any user-facing changes?

No API changes, just an improvement to ingestion-time spilling. The tradeoff is more spill files and potentially more merge I/O for the selected path.

@alamb @Jefffrey

Spill independent sorted runs when a large multi-key sort has potentially expanding keys and less retained merge workspace than buffered reservation. Keep fixed-width keys and better-reserved merges on the existing preparation path.

Hold shared input, per-run reservations, and merge workspace across asynchronous writes. Add an exact-output public SortExec regression and later-write lifetime coverage for success, failure, and cancellation.

Amp-Thread-ID: https://ampcode.com/threads/T-92ab84e1-4bd7-42d2-9885-8e81c665fab6
Co-authored-by: Amp <amp@ampcode.com>
@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Nov 3, 2026
@brancz brancz mentioned this pull request Nov 3, 2026
22 of 47 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant