Skip to content

Refactor runtime filter hierarchy - #109620

Merged
cv4g merged 35 commits into
masterfrom
refactor-runtime-filter-hierarchy
Sep 10, 2026
Merged

cv4g merged 35 commits into
masterfrom
refactor-runtime-filter-hierarchy

Conversation

@cv4g

@cv4g cv4g commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

This PR intends to refactor join runtime-filter internals for better separation of concerns.

The IRuntimeFilter class hierarchy is decomposed into a RuntimeFilter upper layer and concrete filter implementations on the lower layer.

Runtime Filter configuration, build state and evaluation state are factored out and the ApproximateRuntimeFilter is split into a bloom-filter only ApproximateSetRuntimeFilter and an AdaptiveSetRuntimeFilter.

AdaptiveSetRuntimeFilter delegates to an ExactSetRuntimeFilter in the beginning and then conditionally switches to ApproximateSetRuntimeFilter when full.

The change also co-locates per-key runtime-filter construction in joinRuntimeFilter and carries the join-key common_type into shared fixed-hash-table publication. This lets HashJoin::publishSharedRuntimeFilters decide whether to replace the Set/BloomFilter using the same type used by BuildRuntimeFilterStep and __applyFilter. Before this type was obtained via the runtime filter indirectly.

No functional changes.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Not for changelog.


Workflow [PR]
Sync PR [sync-upstream/pr/109620]

Version info

  • Merged into: 26.9.1.1107 (included in 26.9 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4f099f4]

Summary: ✅


AI Review

Summary

This PR refactors the join runtime-filter implementation into a RuntimeFilter wrapper plus concrete filter implementations, adds MergeLock for reciprocal merge safety, and retunes the shared fixed-hash-table publication path to distinguish lookup readiness from probe readiness. After reviewing the current diff, the existing PR discussion, and the added test coverage, I did not find a current correctness, concurrency, or compatibility issue that warrants an inline review comment.

Missing context / blind spots
  • ⚠️ Full CI was still in progress at review time, so this review is based on the current code, existing tests, and the discussion history rather than completed build/integration results.
Final Verdict

✅ No new blocking or major findings in the current revision.

LLVM Coverage Report

Measured on commit 4f099f4.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 994/1017 (97.74%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 7, 2026
@clickhouse-gh

This comment was marked as outdated.

@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4f099f4ff with master 4b3f269d1 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

46 object files changed (+202.50 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/RuntimeFilterLookup.cpp.o 476.86 KiB 609.40 KiB +132.54 KiB (+27.79%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoin.cpp.o 4.27 MiB 4.30 MiB +35.22 KiB (+0.81%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

210 translation units recompiled, 2359 s compile time in total, 210 of them have a recent master baseline.

Job report

cv4g added 2 commits August 16, 2026 09:31
HashJoin::publishSharedRuntimeFilters() can replace a lookup entry with a
prebuilt SharedFixedHashTableRuntimeFilter as soon as the last build-side
port is closed, while BuildRuntimeFilterTransform::finish() (which reaches
RuntimeFilter::merge via IRuntimeFilterLookup::add) only runs afterwards in
prepare(). The refactor turned that previously tolerated late registration
(SharedFixedHashTableRuntimeFilter::merge was a no-op) into a LOGICAL_ERROR
because the variant arms differ.

Restore the no-op behavior when the destination is the shared prebuilt
filter: it probes the complete build-side hash table, a superset of anything
a late set/bloom filter could contribute, so dropping the merge is correct.
Add a regression test simulating the replace-then-late-add interleaving.
Before the refactor ApproximateRuntimeFilter::findImpl fed updateStats with
the match count returned by BloomFilter::findHashPairs while filling the
mask. The refactored generic RuntimeFilter::find recomputed the count with
countPassedStats(result), adding a second full scan over the mask for every
approximate probe block - the hot path once the exact set overflows.

Thread an optional rows_passed out-parameter through the concrete filters'
find: the bloom probe reports its in-loop count, the exact/shared paths
leave it unset and keep the previous countPassedStats fallback.
@diegomestre2 diegomestre2 self-assigned this Aug 18, 2026
insertIntoBloomFilter(getValuesColumn());
ColumnPtr RuntimeFilter::find(const ColumnWithTypeAndName & values) const
{
return data.accessReadOnly(

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.

Why is the shared lock necessary in this case, will there be any write during this process?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

find reads the protected filter state, but other threads can still enter writer paths. For example, a late registration can call finishInsert, which writes the non-atomic inserts_are_finished flag that find reads in assertCanFind

Comment thread src/Common/MutexProtected.h Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 4, 2026
Comment thread src/Interpreters/HashJoin/HashJoin.cpp Resolved
@cv4g
cv4g requested a review from diegomestre2 September 8, 2026 20:43

@diegomestre2 diegomestre2 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.

No further remarks or requests. Great work with the refactoring.

@cv4g

cv4g commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Resolved conflicts with #118336

@cv4g
cv4g added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit c445b2e Sep 10, 2026
175 checks passed
@cv4g
cv4g deleted the refactor-runtime-filter-hierarchy branch September 10, 2026 08:28
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@cv4g @diegomestre2 — title/description suggestions:

Description (currently misleading):

  • Why: Two concrete inaccuracies. (a) The body says "Locking/synchronization is implemented in a new MutexProtected class", but no MutexProtected exists anywhere in src/ (0 hits; the same grep finds MergeLock in 4 files). The class the diff actually adds is MergeLock in src/Common/MergeLock.h, and the per-filter locking is a plain SharedMutex + TSA_GUARDED_BY inside RuntimeFilter. (b) "No functional changes" is not accurate for two paths: FunctionApplyFilter.cpp:108 adds || !filter->isReady(), turning a registered-but-unfinished filter from a LOGICAL_ERROR into pass-all, and RuntimeFilterLookup.cpp:811 adds a build_state.isFinished() precondition to getRecordedKeyValues, so left-side index analysis now fails open instead of pruning from a partial key set. Both are improvements, but a reviewer scanning for "refactor only" would skip them.

  • Suggested addition:

    Corrections to the summary above:

    • The new locking helper is MergeLock (src/Common/MergeLock.h), not MutexProtected. Per-filter state is guarded by a SharedMutex inside RuntimeFilter; MergeLock only exists to take source-shared + destination-exclusive in mutex address order during RuntimeFilter::merge.
    • Not strictly behaviour-preserving, in two places, both deliberately more conservative on the build/probe race:
      • __applyFilter now checks RuntimeFilter::isReady() and passes all rows for a filter that is registered but has pending merges. Previously find threw LOGICAL_ERROR.
      • RuntimeFilter::getRecordedKeyValues / getRecordedKeyRanges now require the build to be finished, so HashJoin::publishSharedRuntimeFilters and buildRuntimeRangePredicate see complete metadata or none. Previously a filter with outstanding merges could hand out a partial exact key set, which index analysis would turn into an IN-set predicate and prune granules the join still needs.

Ignore if intentional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants