Skip to content

Run joins with several ON disjuncts on the partitioned hash join - #120595

Draft
harikrishnan94 wants to merge 22 commits into
pbnp-coveragefrom
pbnp-multi-clause
Draft

harikrishnan94 wants to merge 22 commits into
pbnp-coveragefrom
pbnp-multi-clause

Conversation

@harikrishnan94

@harikrishnan94 harikrishnan94 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Part of https://github.com/ClickHouse/ClickHouse/pull/120423. Sits on #120585. #121707 sits on this.

A join whose ON has several disjuncts (ON a = b OR c = d) builds one hash table per disjunct and probes them one after another. partitioned_hash has one table, so this shape falls back to HashJoin even when you asked for partitioned_hash.

ON a = b OR c = d

HashJoin
+-- clause 0 table
+-- clause 1 table
    probe clause 0, then clause 1

partitioned_hash, one clause only
+-- one table

Each clause now gets its own partitioned table. Each build row is routed into each clause's table. The probe walks the clauses with per-row flags, and non-joined rows are emitted from the stored blocks.

ON a = b OR c = d,  join_algorithm = partitioned_hash

+-- clause 0: its own partitioned table
+-- clause 1: its own partitioned table

Changelog category (leave one):

  • Improvement

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

partitioned_hash now runs joins whose ON clause has several disjuncts.


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

@harikrishnan94 harikrishnan94 added the pr-improvement Pull request with some product improvements label Sep 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3f8a873]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 6/6) FAIL Performance dashboard
Check Results FAIL

AI Review

Summary

The PR builds and probes a separate partitioned hash table for each ON disjunct, sharing stored rows and tracking right-side matches per row. Review of commit 3f8a873ceb7d2364b1e3b1fc86860481120fe44d leaves two unresolved resource-control issues. Both already have inline threads; no duplicate comments were posted.

Findings

⚠️ Majors

  1. Join limits do not measure the complete join. src/Interpreters/PartitionedHashJoin/PartitionedHashJoin.cpp:488–529 checks input-row counts and stored-block/route bytes, omitting the tables and arenas. With several disjuncts, input rows no longer bound the summed distinct-key count: the new regression test builds 80,000 keys from 60,000 rows, so max_rows_in_join = 70000 is incorrectly accepted. max_bytes_in_join can likewise be exceeded by the uncounted tables. The deferred build never checks its final allocation. Enforce limits against the summed keys and total join memory in both build paths; simply calling getTotalRowCount during fill is insufficient because it returns input rows until build_phase_finished. Existing inline finding.

  2. Disjuncts multiply the query's build concurrency. src/Interpreters/PartitionedHashJoin/PartitionedHashJoin.cpp:776–791 launches clauses concurrently, while postBuildPool at line 898 gives each clause up to num_threads workers. Eight disjuncts with at least 16 build blocks and max_threads = 16 can schedule 128 worker tasks plus seven clause coordinators, without a shared worker budget or query CPU slots. This is the normal several-disjunct path: GraceHashJoin::isSupported rejects that shape, so the planner passes a zero spill budget. Share the worker budget across clauses or build clauses sequentially while retaining parallelism within each. Existing inline finding.

Tests
  • ⚠️ Add focused stateless coverage for max_rows_in_join and max_bytes_in_join with several disjuncts, exercising both single-thread insertion and deferred parallel construction. Include a case where input rows fit but summed keys exceed the limit.
  • ⚠️ Verify that increasing the number of disjuncts at fixed max_threads preserves a bounded total post-build worker count.
Final Verdict

⚠️ Request changes. Enforce complete join-size accounting and a shared build-worker budget, with focused validation for both.

clause.reserveFor(build_rows_hint.value_or(keys), static_cast<double>(keys)),
accumulated_rows.load(std::memory_order_relaxed),
/*grow_at_max_fill_=*/true);
for (auto & clause : clauses)

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.

Now that this branch inserts the same build block into every clause table, the sizeLimits() check below is no longer measuring the object it is supposed to cap. accumulated_rows counts the right-side rows once and accumulated_bytes only tracks the stored blocks, but a several-disjunct build can produce more hash-table keys than input rows and can spend most of its memory in the per-clause tables and arenas.

The new gtest already demonstrates the row-count side of this: 60000 build rows become 80000 keys (getTotalRowCount()). With max_rows_in_join between those values, or with a tight max_bytes_in_join, this path will accept a join that HashJoin would reject. Please switch this check to getTotalRowCount() and getTotalByteCount() once the clause inserts have run.

@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 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

⚠️ Significant changes: object file sizes, compile time of recompiled translation units.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 787.74 MiB 787.43 MiB -322.07 KiB (-0.04%)

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.

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 ⚠️

144 object files changed (+17.57 MiB total), 14 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeLeft.cpp.o new 12.92 MiB +12.92 MiB
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeInner.cpp.o new 8.13 MiB +8.13 MiB
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeRight.cpp.o new 7.59 MiB +7.59 MiB
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeFull.cpp.o new 3.48 MiB +3.48 MiB
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/HashJoinClause.cpp.o new 1.96 MiB +1.96 MiB
src/CMakeFiles/dbms.dir/Interpreters/PartitionedHashJoin/PartitionedHashJoin.cpp.o new 1.22 MiB +1.22 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoin.cpp.o 3.36 MiB 4.23 MiB +882.78 KiB (+25.62%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinAll.cpp.o 3.57 MiB 2.85 MiB -737.27 KiB (-20.18%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAll.cpp.o 3.38 MiB 2.70 MiB -699.49 KiB (-20.21%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAnti.cpp.o 2.63 MiB 1.96 MiB -677.23 KiB (-25.19%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAny.cpp.o 2.68 MiB 2.02 MiB -673.27 KiB (-24.56%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAntiMapsAll.cpp.o 3.08 MiB 2.43 MiB -664.28 KiB (-21.07%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinRightAny.cpp.o 3.31 MiB 2.67 MiB -662.55 KiB (-19.52%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAll.cpp.o 3.47 MiB 2.82 MiB -661.44 KiB (-18.64%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinAnti.cpp.o 2.59 MiB 1.95 MiB -653.20 KiB (-24.61%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAntiMapsSet.cpp.o 2.41 MiB 1.78 MiB -648.39 KiB (-26.29%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAny.cpp.o 2.64 MiB 2.01 MiB -646.48 KiB (-23.92%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinRightAny.cpp.o 2.64 MiB 2.01 MiB -646.47 KiB (-23.92%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/FullHashJoinAny.cpp.o 3.05 MiB 2.43 MiB -639.04 KiB (-20.44%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAnyMapsAll.cpp.o 3.16 MiB 2.54 MiB -636.89 KiB (-19.66%)

720 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 ⚠️

2144 translation units recompiled, 14108 s compile time in total, 2129 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +970.4 s (+8%) in total before that adjustment.

Translation units without a recent master baseline:

  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeLeft.cpp: 54.9 s
  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeInner.cpp: 38.7 s
  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeRight.cpp: 34.1 s
  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoinProbeFull.cpp: 20.8 s
  • src/Interpreters/PartitionedHashJoin/HashJoinClause.cpp: 19.6 s
  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoin.cpp: 11.8 s
  • programs/keeper/Keeper.cpp (clickhouse-keeper): 8.6 s
  • src/Interpreters/PartitionedHashJoin/PartitionedHashJoinNotJoined.cpp: 7.9 s
  • src/Interpreters/HashJoin/SharedFixedHashTableFilter.cpp: 6.0 s
  • src/Interpreters/PartitionedHashJoin/JoinRouteHashing.cpp: 5.2 s
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse-keeper ProfileEvents::global_counters_storage 13.38 MiB 13.56 MiB +192.00 KiB (+1.40%)
programs/clickhouse ProfileEvents::global_counters_storage 13.38 MiB 13.56 MiB +192.00 KiB (+1.40%)
programs/clickhouse auto DB::HashJoinClause::insertPartitionSection(DB::HashJoinClause::PostBuildContext&, unsigned lon… new 66.93 KiB +66.93 KiB
programs/clickhouse-keeper auto DB::HashJoinClause::insertPartitionSection(DB::HashJoinClause::PostBuildContext&, unsigned lon… new 66.93 KiB +66.93 KiB

Job report

…ows`

The probe of a join with several ON disjuncts (`ON a OR b`) on `PartitionedHashJoin` walked the
whole left block before returning. When each left row matches thousands of right rows through the
clauses together, the whole result was built in one `AddedColumns`: the first query of
`03748_join_on_complex_condition_result_memory`, pinned to `partitioned_hash`, produces 18.75
million rows from 5000 and fails with `Code: 241` while allocating a 31.98 MiB chunk under its
50M limit. `hash` and `parallel_hash` pass it at 2-4 MiB.

`HashJoinMethods::joinRightColumns` over several maps stops once the result reaches
`max_joined_block_rows` and returns the rows it processed, and `PartitionedHashJoin::probeImpl`
already splits the block on that count and hands the rest back through `setNextBlock`. Do the same
here: stop the row loop at the limit, append the replication offsets instead of pre-sizing them,
and return the number of rows processed. Before this layer every disjunctive join was delegated to
`HashJoin::joinBlock`, which has that split.

The new test runs the `03748` query on `partitioned_hash`, and checks every kind and strictness of
an `ON a OR b` join against `hash` at `max_joined_block_size_rows` 1, 7, 100 and 1000000. It fails
with `Code: 241` on the parent and passes here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
harikrishnan94 and others added 3 commits September 23, 2026 15:23
Keep a dirty lane sketch for each clause and sum their table predictions. Reuse the cached summed key count for warm fills without spill. The gated prediction trace matches a full merge across clauses.

Focused unit tests, aarch64 TSan tests, and functional tests 05229 and 05227 passed.
@harikrishnan94
harikrishnan94 removed this pull request from stack #120423 October 6, 2026 07:36
@harikrishnan94
harikrishnan94 removed this pull request from stack #124166 October 7, 2026 12:21
@harikrishnan94
harikrishnan94 removed this pull request from stack #124421 October 8, 2026 09:43
@harikrishnan94
harikrishnan94 removed this pull request from stack #124582 October 9, 2026 04:32
harikrishnan94 and others added 12 commits October 14, 2026 10:31
The parent now carries the rewritten `uhj`. The conflicts were in the partitioned join, where this branch runs several ON clauses on its own tables. They are resolved on top of the interfaces the parent ported: `JoinBuildContext` for the fill lanes and the drain, `KeyGetterForType` without the offset flag, and the non-joined scan that emits the row store columns through `gatherJoinOutputColumns`. The scan over the stored blocks, for per-row used flags, is written the same way.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ined filler

The scan over the stored blocks was in the tree twice after the merge, once in the old form and once ported, and the cursor member was declared twice. The row count of a Join table reads the first clause's tables, as the other readers of `clauses` do.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… constructor

Two tests of the several-clause build compared the partitioned join with a standard `HashJoin` that was built with the thread count and layout flag of the old constructor. They pass a `HashJoinBuildLayout::oneBucket(1)` now.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the declaration of `UNSUPPORTED_JOIN_KEYS` in `PartitionedHashJoin.cpp` that the style check asks for.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the restored `isHashFamilyEnabled` checks of the planner, the switch log line of `JoinSwitcher` and the `hash` name of a Join table.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the adjusted general join planning and grace buckets tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the last two commits of `uhj`: the key and byte count of a slot insert that throws, and the rename of the `StorageJoin` count test.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the fixes of the previous layers.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
`readability-qualified-auto` rejects the plain `auto &` in the loop over the lane sketches. The lane is `const`, so the reference already was `const`. The type now says so.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the few-keys test fix.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the latest merge of master into `uhj` and the port of the spilling joins to `ISpillable`.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the continuation of the partitioned join result. This layer probes every disjunct itself, so the probe of the rest is `probeDispatch` alone.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Comment on lines +784 to +789
for (size_t clause_idx = 1; clause_idx < clauses.size(); ++clause_idx)
clause_pool->scheduleOrThrow(
[this, clause_idx, rows, &clause_unique, thread_group = CurrentThread::getGroup()]
{
ThreadGroupSwitcher switcher(thread_group, ThreadName::PARTITIONED_JOIN);
clause_unique[clause_idx] = clauses[clause_idx].postBuild(rows, *post_build_pools[clause_idx]);

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.

[P2] Share the worker budget across concurrently built clauses

Every clause launched here calls postBuildPool, which independently allocates up to num_threads workers. With eight disjuncts, at least 16 build blocks, and max_threads = 16, the partitioned build can therefore run 128 worker tasks, plus seven clause coordinators, for one join. These pools do not acquire query CPU slots or share a concurrency limit.

This path is also the normal path for several-disjunct SQL joins: both planners disable spilling for that shape and construct this join with a zero external-memory budget. Increasing the number of OR terms therefore multiplies CPU concurrency and thread demand even with a fixed max_threads, which can oversubscribe the server or exhaust its thread pool under concurrent queries. Please divide a shared worker budget among the active clauses, or build the clauses sequentially while retaining parallelism within each clause.

Brings in the merge of `uhj` that repairs the `gtest_functions_stress` build.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
harikrishnan94 and others added 4 commits October 17, 2026 06:51
Brings in the fix of the inserted row count of the single-lane build and the pinned settings of the tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the latest merge of `uhj`, the fixes of the scatter check and of the spill hand-over, and the test changes of the earlier branches.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…sing

`05229` and `05238` took more than the limit of a flaky check, so they carry the `long` tag like the other slow tests of this join. `05229` asserts that the non-joined rows of a `FULL` join come from several streams, which only `parallel_non_joined_rows_processing` allows, so it sets that setting.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Brings in the fixes of the `05227`, `05218`, `05139` and `04931` tests and the sanitizer guard of the `HashJoinTable` unit test.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@harikrishnan94
harikrishnan94 removed this pull request from stack #124822 October 21, 2026 12:32

This branch has not been deployed

No deployments
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-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant