You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SortMergeJoinExec double counts join key columns in the buffered batch reservation #27322
SortMergeJoinExec charges a buffered batch twice for each join key that is a plain column. BufferedBatch::try_new adds batch.get_array_memory_size() and the size of each evaluated join key array. When the key is a column reference, the key array shares its buffers with the batch. The reservation then counts that column two times.
The effect is that a join on a wide string key reserves about twice the memory it holds. This makes the join spill or fail under a memory limit earlier than it should.
To Reproduce
Run this in datafusion-cli (commit 34eb6dc on main). Both queries buffer the same data. Only the join key differs.
SETdatafusion.execution.target_partitions =2;
SETdatafusion.optimizer.prefer_hash_join = false;
CREATETABLElASSELECT value AS id, repeat('x', 1000) || value AS s FROM generate_series(1, 20000);
CREATETABLErASSELECT value AS id, repeat('x', 1000) || value AS s FROM generate_series(1, 20000);
-- join on the wide string column
EXPLAIN ANALYZE SELECTmax(l.id), max(r.id), max(l.s), max(r.s) FROM l JOIN r ONl.s=r.s;
-- join on the integer column, same columns kept
EXPLAIN ANALYZE SELECTmax(l.id), max(r.id), max(l.s), max(r.s) FROM l JOIN r ONl.id=r.id;
Observed SortMergeJoinExec metrics:
join key
peak_mem_used
l.s = r.s
38.8 MB
l.id = r.id
19.7 MB
Expected behavior
Both queries should report about the same peak_mem_used, because the buffered batches are the same. A key array that is a column of the batch should not be charged again.
Additional context
A possible fix is to charge only the key arrays that do not share buffers with the batch (for example, skip Column keys, or count buffers by pointer once per batch).
Activity
Kristofferjoha commentedon Nov 11, 2026
@adriangb Are you planning to work on this?
adriangb commentedon Nov 11, 2026
I have no immediate plans to work on this
Kristofferjoha commentedon Nov 11, 2026
take