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
42 of 61 tasks

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

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.

2 participants