Repository navigation
Read in order through SpillingHashJoin - #124873
Conversation
|
Workflow [PR], commit [82b5002] Summary: ✅
AI ReviewSummaryThe PR enables reading in order through spill-capable hash joins by selecting an order-preserving Findings
Tests
Missing context / blind spots
Final Verdict
|
SpillingHashJoin
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes30 object files changed (+88.68 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units534 translation units recompiled, 4917 s compile time in total, 534 of them have a recent master baseline. |
I will address this in a different PR. It is not super important, we are already spilling, the max rows in join shouldn't be a problem.
Addressed by ba674f0. It doesn't enforce the exact memory cap, but we can improve it in the future. Probably more memory is consumed on producing the result than the gathered bytes.
Addressed by b61d93e. |
diegomestre2
left a comment
There was a problem hiding this comment.
Great work on enabling this optimization! I left a few comments and can give another read after you address them. If I understand correctly, the biggest change is in the default plans: hash joins are basically SpillingHashJoin by default, and that used to block read-in-order through join. With this PR, queries like ... JOIN ... ORDER BY t1.pk will read in order by default even when nothing spills.
| if (is_inner && is_any_join) | ||
| { | ||
| inner_any_used_flags = std::make_shared<JoinStuff::JoinUsedFlags>(); | ||
| inner_any_used_flags->reinitPerRowFlags(right_blocks_row_counts); |
There was a problem hiding this comment.
[P2] Bound claim-state memory for spilled inner-any joins
This also runs after the right side has been spilled, but reinitPerRowFlags allocates an atomic_bool for every right input row across all sorted blocks. That makes the external algorithm retain memory proportional to the entire right table again, independently of the spill threshold/cache budget. Duplicate-heavy right sides are especially problematic: hundreds of millions of copies of one key require hundreds of millions of flags even though all probes claim only that key's first right row. Previously the external partial merge probe did not have this allocation, so such queries can now fail with a memory-limit exception during post-build processing before producing any result.
Please avoid allocating claim flags for duplicate right rows, or use claim state that can remain bounded/spill. Add a focused duplicate-heavy INNER ANY test with a low spill threshold and memory limit; this allocation is separate from the already-discussed per-stream gather buffers.
diegomestre2
left a comment
There was a problem hiding this comment.
Thanks for addressing the comments, once it is green you can merge it. Great work!
SpillingHashJoin disable read-in-order-through-join optimization before, therefore the the two plans of parallel replicas and local didn't differ. Now it differs, because locally we enabled it. Disable read-in-order-through-join to keep it
| @@ -792,6 +854,12 @@ void MergeJoin::mergeFlushedRightBlocks() | |||
| if (!memory_limit && rows_limit) | |||
There was a problem hiding this comment.
| if (!memory_limit && rows_limit) | |
| if (!memory_limit && rows_limit && right_blocks.row_count) |
so we make sure the division does not fail with 0 as divisor.
There was a problem hiding this comment.
That is covered by if (flushed_right_blocks.empty()): SortedBlocksWriter::finishMerge (and flushToManyFiles) returns an empty result in case there are no right blocks or only empty right blocks.
There was a problem hiding this comment.
I will a debug check in an upcoming PR, but I really would like to merge this ASAP.
diegomestre2
left a comment
There was a problem hiding this comment.
I left one comment only. Feel free to consider it or not, I am not sure if it would break but it is nice to have the check. Good work!
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The read-in-order optimization now works through joins that can spill to disk: when the query plan needs the left table's row order (e.g.
ORDER BY t1.pk LIMIT n), a hash join that crosses the spill threshold spills through a partial merge join, which preserves the order, instead of a grace hash join. join_algorithm = 'partial_merge' got an order-preserving probe and supports the optimization too.Workflow [PR]
Sync PR [sync-upstream/pr/124873]
Version info
26.11.1.1101-master(included in26.11and later)