Skip to content

Rewrite INTERSECT ALL and EXCEPT ALL to JOIN - #129530

Draft
m-selmi wants to merge 3 commits into
intersect-except-distinct-as-joinfrom
intersect-except-all-as-join
Draft

m-selmi wants to merge 3 commits into
intersect-except-distinct-as-joinfrom
intersect-except-all-as-join

Conversation

@m-selmi

@m-selmi m-selmi commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Second part of: #115106
Follow-up to: #129089
Related: #120479
Related: #114550

This change builds on some of the ideas in #120479, thanks to @raimannma.

Execute INTERSECT ALL and EXCEPT ALL as a hash join on all columns instead of the single-threaded IntersectOrExcept step. This builds on the DISTINCT rewrite in #129089.

How it works

The hash table stores a count of rows per key instead of the rows themselves.

INTERSECT ALL runs as INNER ANY. Each left row takes one copy of its key from the count, so a key that appears a times on the left and b times on the right is output min(a, b) times. The output columns are always taken from the probe side, so the hash table stays a count without RowRefs.

EXCEPT ALL runs as LEFT ANTI. A left row is output only once the right copies of its key are used up, so the key is output a − b times.

When the join order optimizer builds the left side, EXCEPT ALL runs as RIGHT ANTI. Each cell keeps one RowRef together with the count, and the copies that were not taken are output after the probe. Only the blocks that hold such a row are kept.

The probe streams lower the counts under one lock per bucket of the hash table, 256 in total, without atomics. Before that, the rows of a block are grouped by key, so all rows with one key take their copies with a single lookup, and a thread that finds a bucket locked continues with the next free one. The rows are grouped with a small hash table of at most 4096 keys per probe stream. A sample of 4096 rows decides whether the grouping pays off for the block. When it does not, a skip budget shared by all probe streams turns it off for the next rows. JoinProbePreAggregationRowsChecked and JoinProbePreAggregationGroups show how many rows were grouped and how many groups they formed.

ll rows of a group are equal, so the result lists one row of each group as many times as the group is in the result, instead of keeping track of every row. ScatteredBlock::filter accepts such a list of repeated rows.

The runtime filter is disabled for EXCEPT ALL as LEFT ANTI, because a left row whose key is on the right is still output once the right copies are used up.

When the join spills and must keep the left order, INTERSECT ALL does not fall back to MergeJoin, which outputs one row per key instead of counting duplicates.

With extremes = 1, the extremes of INTERSECT ALL and EXCEPT ALL are now computed on the result, instead of combining the extremes of the arms.

Changes to the join code shared with other joins

IJoin::joinBlock takes a JoinProbeContext with the index of the probe stream, in the same way as JoinBuildContext on the build side. Every join type takes it, and only the joins on count maps use it.

RuntimeFilterSkipBudget is moved to Common/RowsSkipBudget.h and shared with the grouping above.

The matched rows of EXPLAIN ANALYZE are counted from the list of output rows instead of the filter. For other joins both give the same number.

Behavior shared with other joins

As a join, the set operations now share some limitations of joins.

With a very small spill threshold a spilling hash join can exceed the memory limit.

A WHERE condition over the result can be pushed into both arms, like any filter over a join, so an expression that throws for some rows can be evaluated on rows it did not see before.

With query_plan_optimize_join_order_algorithm set to only dphyp or dpsize, EXCEPT ALL fails with Failed to find a valid join order.

Performance, the new performance test intersect_except_all_as_join

intersect_except_all_results

Changelog category (leave one):

  • Performance Improvement

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

INTERSECT ALL and EXCEPT ALL are now executed as a hash join, which makes them 2 to 10 times faster. The previous execution can be restored with the setting rewrite_intersect_except_all_to_join = 0.


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

@m-selmi m-selmi added comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... can be tested Allows running workflows for external contributors labels Nov 5, 2026
@m-selmi
m-selmi added this pull request to stack #129531 November 5, 2026 16:02
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4f22308]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py FAIL

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 5, 2026
@m-selmi
m-selmi force-pushed the intersect-except-all-as-join branch from 20c00a6 to d60dd9d Compare November 5, 2026 16:53
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing deccc87c5 with master dcf6e7b2a (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.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 828.50 MiB 828.01 MiB -500.75 KiB (-0.06%)

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

123 object files changed (+9.00 MiB total), 6 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/LeftHashJoinAntiMapsCount.cpp.o new 1.58 MiB +1.58 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAnyMapsCount.cpp.o new 1.58 MiB +1.58 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAntiMapsCountWithRef.cpp.o new 1.58 MiB +1.58 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/RightHashJoinAntiMapsOne.cpp.o new 1.37 MiB +1.37 MiB
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoin.cpp.o 4.21 MiB 5.58 MiB +1.37 MiB (+32.45%)
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/InnerHashJoinAnyMapsSet.cpp.o new 1.21 MiB +1.21 MiB
src/CMakeFiles/dbms.dir/Storages/StorageJoin.cpp.o 2.60 MiB 2.67 MiB +70.57 KiB (+2.65%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/JoinStep.cpp.o 271.97 KiB 312.87 KiB +40.89 KiB (+15.04%)
src/CMakeFiles/dbms.dir/Planner/Planner.cpp.o 845.34 KiB 881.89 KiB +36.56 KiB (+4.32%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/JoinStepLogical.cpp.o 1.05 MiB 1.08 MiB +23.85 KiB (+2.21%)
src/CMakeFiles/dbms.dir/Processors/Transforms/JoiningTransform.cpp.o 270.29 KiB 291.81 KiB +21.53 KiB (+7.96%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/skipDeduplicationAfterSetOperationJoin.c… new 18.34 KiB +18.34 KiB

711 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

1915 translation units recompiled, 12038 s compile time in total, 1909 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Interpreters/HashJoin/InnerHashJoinAnyMapsCount.cpp: 13.1 s
  • src/Interpreters/HashJoin/LeftHashJoinAntiMapsCount.cpp: 13.0 s
  • src/Interpreters/HashJoin/RightHashJoinAntiMapsCountWithRef.cpp: 13.0 s
  • src/Interpreters/HashJoin/RightHashJoinAntiMapsOne.cpp: 11.7 s
  • src/Interpreters/HashJoin/InnerHashJoinAnyMapsSet.cpp: 11.5 s
Symbol sizes
Binary Symbol Master PR Δ
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)2, DB::HashJoin::MapsTemplat… new 151.50 KiB +151.50 KiB
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)0, (DB::JoinStrictness)2, DB::HashJoin::MapsTemplat… new 151.50 KiB +151.50 KiB
programs/clickhouse-keeper unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… new 147.95 KiB +147.95 KiB
programs/clickhouse unsigned long DB::HashJoinMethods<(DB::JoinKind)2, (DB::JoinStrictness)6, DB::HashJoin::MapsTemplat… new 147.95 KiB +147.95 KiB

Job report

@m-selmi
m-selmi force-pushed the intersect-except-all-as-join branch 2 times, most recently from 7873235 to deccc87 Compare November 13, 2026 21:30
@m-selmi m-selmi added the ci-relocation-headroom-ok Accept an expected decrease of the relocation headroom in the build check label Nov 16, 2026
@m-selmi
m-selmi force-pushed the intersect-except-all-as-join branch from c6e78e8 to 726bfc4 Compare November 16, 2026 15:10
@m-selmi
m-selmi force-pushed the intersect-except-all-as-join branch from 726bfc4 to 4f22308 Compare November 16, 2026 16:39

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

can be tested Allows running workflows for external contributors ci-relocation-headroom-ok Accept an expected decrease of the relocation headroom in the build check comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant