Repository navigation
Run the post-build phase of the wrapped join in SpillingHashJoin - #111972
Conversation
`SpillingHashJoin` overrode neither `hasPostBuildPhase` nor `runPostBuildPhase`, so it inherited `IJoin`'s `false`/no-op pair. `FillingRightJoinSideTransform` only schedules the post-build phase when `hasPostBuildPhase` is true, so as soon as an auto-spill threshold is configured - and `max_bytes_ratio_before_external_join` defaults to `0.5`, which wraps every hash join - an in-memory `HashJoin` silently lost `tryRerangeRightTableData`, `tryConvertToFixedHashMap` and `publishSharedRuntimeFilters`. Forward both to `chosen_join`, the way `JoinSwitcher` already does. This is safe because `hasPostBuildPhase` is queried right after `onBuildPhaseFinish` has set `chosen_join`. `GraceHashJoin` reports no post-build phase and runs it per bucket itself, so the spilled state keeps behaving as before.
|
Workflow [PR], commit [932ff90] Summary: ✅
AI ReviewSummaryThis PR forwards Tests
Final VerdictNeeds a stronger regression test before merge. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
|
A question: What is this PR doing exactly? The PR message is barely understandable. |
|
@rschu1ze Sorry — I've rewritten the description to be clearer. The short version: A hash join has a post-build phase: a step that runs once after the right table is fully built, applying Since 26.5 the planner no longer hands a bare
The fix is five lines: forward both methods to |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
|
This is already covered by #111244 |
|
Reopening because #111244 was not merged yet. |
|
This PR is clearly labeled as a bug fix. While the other PR is labeled as a performance improvement. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The fix LGTM.
|
Fixing bugs obviously has a higher priority than performance improvements. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units10 translation units recompiled, 38 s compile time in total, 9 of them have a recent master baseline. |
The comment claimed that `GraceHashJoin` "runs it per bucket itself". It does not: `GraceHashJoin::getDelayedBlocks` only calls `HashJoin::onBuildPhaseFinish` for a delayed bucket, and the single `runPostBuildPhase` call in `GraceHashJoin::joinBlock` is guarded by `getNumBuckets() <= 1`. Scope the comment to what the code actually does; the behavior of this pull request is unchanged.
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111972&sha=ab4c8d7574d7e0320756842165806efdc6668e10&name_0=PR&name_1=Stress%20test%20%28arm_release%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. It is unrelated to this pull request (a join change). The server terminates on an uncaught exception inside the So committing a mutation of a packed part does lazy on-disk size accounting, which performs a real asynchronous read; under the stress test's thread-pool fault injection the read cannot be scheduled and the exception escapes a SELECT toStartOfDay(check_start_time) AS d, count(), groupUniqArray(pull_request_number)
FROM checks
WHERE check_start_time > now() - INTERVAL 30 DAY AND test_status IN ('FAIL','ERROR')
AND test_name LIKE 'Received signal 6%' AND test_context_raw LIKE '%MergeTreeData::Transaction::commit%'
GROUP BY d ORDER BY d DESC12 occurrences over 12 different days, including |
| -- The post-build phase ran at all (it is skipped entirely when the wrapper does not | ||
| -- report having one), and the join stayed in memory rather than spilling. | ||
| SELECT | ||
| ProfileEvents['JoinBuildPostProcessingMicroseconds'] > 0 AS post_build_phase_ran, |
There was a problem hiding this comment.
This assertion is weaker than the contract this PR is fixing. A plain HashJoin also gives JoinBuildPostProcessingMicroseconds > 0 and spills = 0, so the test still passes if the planner stops wrapping the query in SpillingHashJoin. That wrapper is only chosen when max_bytes_before_external_join > 0, getTempDataOnDisk() is available, and GraceHashJoin::isSupported() holds. Please add an EXPLAIN PLAN assertion (for example Algorithm: SpillingHashJoin(HashJoin), as in 03915_spilling_hash_join) or another direct check that this query really took the wrapped path.
|
The fix is in progress: #112691 "Do not read part sizes while committing under the parts lock". Open, assigned to Avogar, no review yet. Your reading of the mechanism matches mine; the fix invalidates the size aggregates for a part whose sizes are not computed yet instead of reading them, so nothing is read inside the That build predates it: the regression test One precision on the census: the signature splits over two sub hashes of the same two frames. 3563-2928 covers the PR side (3 hits, 3 PRs, all |
Requested in review: 03753 is not the test of the runtime filter built from the fixed hash table, so the setting belongs in the file's settings section rather than only in Q4. Measured that the wider scope costs no coverage. With the setting pinned to 0 for the whole file and with it absent, Q1/Q2/Q3/Q5/Q6/Q7 produce identical RuntimeFilter counters (Q1 6/98, Q2 9/195, Q3 11/293, Q5 8/197, Q6 5/100, Q7 104/0, RuntimeFiltersCreated=1 throughout); only Q4 differs, 0/302 with the pin against 12/290 without it. 04241 remains the dedicated test of the shared fixed hash table filter. The onset is ClickHouse#111972, also identified in review. Verified by A/B on one host with one test file and one server configuration: with the two SpillingHashJoin post-build overrides removed, the unpinned test passes 8 of 8 runs in one process; with them present it passes the first run and then fails 7 of 7 with RuntimeFilterBlocksProcessed=12, byte-identical to CI. max_bytes_ratio_before_external_join defaults to 0.5, so SpillingHashJoin wraps the join by default and previously inherited hasPostBuildPhase() = false, which kept publishSharedRuntimeFilters from displacing the fully disabled bloom filter. 40 of 40 runs pass with the new form (20 randomized, 20 not). Removing the pin reddens the test 5 of 5.
Hash joins have a "post-build phase": a step that runs once, after the right table has been fully built, and applies optimizations to the finished hash table —
tryRerangeRightTableData,tryConvertToFixedHashMapandpublishSharedRuntimeFilters.Since 26.5 the planner does not hand a bare
HashJointo the pipeline. It wraps it inSpillingHashJoin, which keeps the join in memory while it fits and switches toGraceHashJoinonly when it grows past the spill threshold. This wrapping happens for essentially every hash join, because the threshold settingmax_bytes_ratio_before_external_joindefaults to0.5.SpillingHashJoinoverrode neitherhasPostBuildPhasenorrunPostBuildPhase, so it inheritedIJoin'sfalse/no-op pair.FillingRightJoinSideTransformschedules the post-build phase only whenhasPostBuildPhasereturns true, so it saw "no post-build phase" and skipped it. The result: a join that never spilled, and therefore ran entirely as an ordinary in-memoryHashJoin, silently lost all three of those optimizations.The fix is to forward both methods to
chosen_join— the joinSpillingHashJoinactually settled on — the same wayJoinSwitcheralready does. This is safe becausehasPostBuildPhaseis queried right afteronBuildPhaseFinishhas setchosen_join. In the spilled state,chosen_joinis aGraceHashJoin, which does not override these two methods, so forwarding leaves the spilled path exactly as it is today.To be precise about that path, since the wording above was misleading earlier:
GraceHashJoinruns the post-build phase only when the right table ended up in a single bucket (GraceHashJoin::joinBlockcalls it undergetNumBuckets() <= 1). A multi-bucket spill skips it, because a hash table that holds only one bucket cannot produce a runtime filter that is valid for the whole right table. That is pre-existing behavior and is intentionally not changed here; this pull request only restores the phase for the wrapped-but-never-spilled in-memoryHashJoin, which is the common case.Measured on 26.7.1.1315, the same 200k-row join on the same data, varying only the threshold:
JoinBuildPostProcessingMicrosecondsmax_bytes_before_external_join = 100000000000(wrapped)0max_bytes_before_external_join = 0(not wrapped)12371The server log confirms the wrapped case takes the affected path:
SpillingHashJoin: All blocks fit in memory (17577600 bytes, 200000 rows), promoting HashJoin.The impact is limited to the single-threaded
HashJoinpath:HashJoin::hasPostBuildPhaserequires akey32/key64map whose range is within 4x the key count, andConcurrentHashJoindoes not implement a post-build phase at all. GivingConcurrentHashJoinone is a separate question, not addressed here.Related: #111248
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed hash joins losing their post-build optimizations (conversion to a fixed hash table, the shared runtime filter, right table reranging) whenever an automatic spill-to-disk threshold was configured, which is the default since 26.5.
Version info
26.8.1.1143(included in26.8and later)