Repository navigation
Conversation
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>
kosiew
left a comment
There was a problem hiding this comment.
@brancz, thanks for addressing the eager merge memory exhaustion. Independent spilling resolves the reproduced wide-dictionary failures while preserving ordering and memory accounting.
I'm requesting changes because the fallback also admits short UTF-8 keys, causing a previously successful sort to exceed its temporary disk quota. Please narrow the admission policy or otherwise prevent this regression.
| )), | ||
| ) | ||
| })?; | ||
| if has_expanding_keys |
There was a problem hiding this comment.
The gate also admits short Utf8 keys, where per-batch spill files can exhaust the temp-disk quota through repeated metadata and extra merge passes. A 4,096-row sort succeeded under a 300 KiB quota without this branch but now fails. Please narrow admission or avoid this disk overhead, and add a regression test.
| "in_mem_batches must not be empty when attempting to sort and spill" | ||
| ); | ||
|
|
||
| let has_expanding_keys = self.expr.iter().try_fold(false, |found, expr| { |
There was a problem hiding this comment.
Consider checking key count, batch count, and reservation thresholds before querying expression types. This avoids unnecessary data_type calls when independent spilling is already ruled out.
| }) | ||
| .collect::<std::result::Result<Vec<_>, _>>()?; | ||
| let actual = concat_batches(&decoded[0].schema(), &decoded)?; | ||
| let key_index = |
There was a problem hiding this comment.
Consider varying the second dictionary key independently among equal first keys across runs. The current test cannot detect a merge that ignores later dictionary keys. Verify ordering by (first_key, second_key, id).
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