Repository navigation
Keep the JOIN runtime filter usable when its switch to a Bloom filter hits the memory limit - #133269
Conversation
… hits the memory limit `AdaptiveSetRuntimeFilter::switchToApproximateFilter` emplaced the Bloom filter into the variant directly. `std::variant::emplace` destroys the exact set first and then runs the constructor, which allocates the Bloom filter's buffer through the memory tracker. When the memory limit refuses it, the variant is left valueless. `BuildRuntimeFilterTransform` is an `ISimpleTransform`, which passes the exception downstream and keeps pulling blocks into the same filter, so the next block threw LOGICAL_ERROR "Unexpected state of AdaptiveSetRuntimeFilter" and aborted debug and sanitizer builds. The merge path (`mergeFrom` with a Bloom filter source) went through the same function. Build and fill the Bloom filter in a local and only then move it into the variant, as `switchToDenseBitset` already does; the move is nothrow (static_assert), so a refused allocation now leaves the exact set in place. A full exact set on the next insert then means the previous switch threw; it still holds every key (`ExactSetRuntimeFilter::insert` inserts the block before setting `is_full`), so retry the switch instead of throwing. The exception still reaches the user unchanged; only the state the filter is left in changes. The transient peak during the switch grows by the exact set's own buffers, bounded by `exact_bytes_limit`. ClickHouse#129186 had this fix (9a9df77 + 9583f6a); the scope cut af3c523 dropped it together with the dense-bitset memory handling. Related: ClickHouse#129092 Related: ClickHouse#129186 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The new gtests now assert that the probe is checked, not skipped: a variant that dropped the key set on a refused allocation also passed their all-true masks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261120-161000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261120-130803 |
|
Workflow [PR], commit [5d2260a] Summary: ✅
AI ReviewSummaryReviewed PR #133269 at Missing context / blind spots
Final VerdictStatus: ✅ Approve |
There was a problem hiding this comment.
@groeneai Thanks for restoring the exception-safety fix. Keeping the exact set usable until the replacement Bloom filter is ready makes sense. What seems avoidable is how large the exact set can grow before we start that conversion.
Could you change AdaptiveSetRuntimeFilter::insert to switch during insertion rather than after the entire block? For example, if the set is at its distinct-key limit, duplicates can still go through the exact path. At the first new key, we already know a switch is needed. Growing the hash table for the rest of the block increases peak memory without improving Bloom sizing—the Bloom size comes from the configured geometry and optional statistics hint, not from the final distinct count.
One option is to return the number of input rows consumed, seed the replacement from the bounded exact set, and feed it the remaining rows starting with that first new key. Please preserve the existing dense-bitset choice and make the failure behavior explicit: if allocating the replacement throws mid-block, the exact set contains only the consumed prefix. A test with duplicates at the limit followed by new keys would exercise that boundary, both on success and with a refused allocation.
Please implement this as a follow-up in order to not block the LOGICAL_ERROR fix as of commit a84704c. The goal is to avoid block-sized overshoot, not to give up exception safety or slow down bulk insertion for duplicate-heavy inputs. Start working on it, when this PR is merged and tag me on the PR when it's open, please.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 9 s compile time in total, 7 of them have a recent master baseline. |
|
Acknowledged. Once this PR is merged, I'll open the follow-up that makes |
CI finish ledger - a84704cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Apart from the first attempt, which stopped at the Session id: cron:our-pr-ci-monitor:20261120-203023 |
|
@groeneai Please merge the master branch. In CI run https://github.com/ClickHouse/ClickHouse/actions/runs/44780580104/job/136504729438?pr=133269 we got a failure that is tracked in #133246 and seems to be related to #132527 and #133270. |
|
📊 Cloud Performance Report
PR verdict: not_sure. 1 measured difference(s) not attributable to this PR. clickbench Q31: The measured difference cannot be attributed to this PR from the available source evidence. In the tested merge e0405cad, the patch against its first parent 6a2b727a changes only two things: AdaptiveSetRuntimeFilter::insert, which now retries switchFromExactFilter when the exact set is full, and AdaptiveSetRuntimeFilter::switchToApproximateFilter, which now builds the Bloom filter before emplacing it. Both are in src/Processors/QueryPlan/RuntimeFilterLookup.cpp. The rest of the patch is gtests in src/Processors/QueryPlan/tests/gtest_runtime_filter_lookup.cpp. Runtime filters are only built on … clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
The follow-up is open: #133383. |
…y past the limit `AdaptiveSetRuntimeFilter::insert` inserted the whole block into the exact set and only then checked `exact_values_limit`, so a block with many distinct keys grew the exact set's hash table and set elements to the block's distinct-key count before the switch to the dense bitset or Bloom filter, whose size does not depend on that count. `Set::insertFromColumns` gets an optional `max_keys`: once the set holds that many keys, rows with keys it already has are still taken and the first row with a new key stops the insert. The capped loop is a separate out-of-line function chosen once per block, only when the block can reach the cap, so `IN` sets run the unchanged bulk loop. The adaptive filter inserts with `max_keys = exact_values_limit` and switches when the insert stopped (or the set is full by bytes); the replacement is filled locally from the exact keys and the whole block, then moved in, so a refused allocation leaves the exact set with the rows before the first new key past the limit. ClickHouse#133269's retry of a full exact set folds into the same condition. Tests: duplicates at the limit then new keys, on success (dense bitset) and with a refused Bloom allocation; ClickHouse#133269's retry test no longer expects the refused key in the filter. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t loop The out-of-line capped loop was taken for practically every block of a runtime filter build (the default limit is 10000 keys, blocks are 65409 rows), and its per-row bookkeeping made duplicate-heavy filter builds 1.29-1.35x slower. It is removed. Set::insertFromBlockImplCase now checks the cap only right after a key was newly inserted and stops after the row whose key makes the set hold more than the cap. Rows whose key is already in the set run the same code as before, and IN sets, which pass no cap, pay one compare per new key. The exact set holds at most exact_values_limit + 1 keys, which is the existing "full" threshold, so the retry after a refused switch from ClickHouse#133269 and its test stay as they are; the replacement takes the exact keys plus the whole block. If its allocation is refused, the exact set holds the rows up to and including the first key past the limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #129092
Related: #129186
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
When the memory limit refuses the Bloom filter that
AdaptiveSetRuntimeFilterswitches to once its exact set overflows, the next block of the same build stream throwsLOGICAL_ERROR"Unexpected state of AdaptiveSetRuntimeFilter", which aborts debug and sanitizer builds. Release builds returnMEMORY_LIMIT_EXCEEDEDeither way, so there is no user-visible change. BuzzHouse randomizes all three ingredients (join_runtime_bloom_filter_bytes,join_runtime_filter_exact_values_limit,max_memory_usage).Root cause:
switchToApproximateFilteremplaced the Bloom filter into the variant, sostd::variant::emplacedestroyed the exact set before the constructor allocated the buffer; the refusal left the variant valueless.BuildRuntimeFilterTransformis anISimpleTransform, which passes the exception downstream and keeps feeding blocks into the same filter. ThemergeFrompath (a Bloom filter source) uses the same function.Change: build and fill the Bloom filter in a local, then move it in (nothrow, as
switchToDenseBitsetalready does), so a refused allocation keeps the exact set. A full exact set on the next insert now retries the switch; the full set still holds every key. No exception is caught. The exact set is now freed only after the Bloom filter is filled, so the switch briefly holds both; the exact set is capped by the exact-set limits (for a local filter, about the Bloom filter's size).#129186 had this fix; the scope cut in af3c523 dropped it along with the dense-bitset memory handling.
Validation: two gtests (insert path, merge path) abort on master and pass with the fix; each half of the change alone leaves the insert test red. A SQL repro with
max_threads = 32, max_block_size = 64, max_memory_usage = 80000000and a 16 MiB Bloom filter aborted a master debug build 14/200 times and the fixed build 0/200.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/133269]
Version info
26.12.1.110-master(included in26.12and later)