Skip to content

Stop reading the left side of a left semi join with an empty right side - #119218

Merged
vdimir merged 6 commits into
ClickHouse:masterfrom
raimannma:semi-join-empty-right-side
Sep 16, 2026
Merged

vdimir merged 6 commits into
ClickHouse:masterfrom
raimannma:semi-join-empty-right-side

Conversation

@raimannma

@raimannma raimannma commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

A LEFT SEMI join keeps a left row solely because it has a match on the right, so an empty right side makes the whole result empty - exactly as it does for an INNER or RIGHT join. alwaysReturnsEmptySet reported that only for the latter two, so JoiningTransform kept reading the left side of a left semi join that could not produce a single row.

GraceHashJoin gates the same check on the join kind before it looks at its buckets, and MergeJoin (join_algorithm = 'partial_merge') also accepts LEFT SEMI and runs through the same JoiningTransform early exit, so both get the same condition.

The join runtime filter already stops the left side of such a join, which is why this is not visible with the default settings. With enable_join_runtime_filters = 0, a left semi join over 50 million rows against a right side that turns out empty reads all 50 million of them, and none after this change. The test asserts that through system.query_log.read_rows, for the hash, parallel_hash, grace_hash and partial_merge algorithms.

Split out of #118230, where INTERSECT DISTINCT with an empty right arm needs it to stop as early as the set-operation step does. It stands on its own: it speeds up any left semi join whose build side turns out to be empty.

Related: #118230

Changelog category (leave one):

  • Performance Improvement

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

A LEFT SEMI join whose right side is empty no longer reads its left side.


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

Version info

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

`alwaysReturnsEmptySet` reports that a join can only produce an empty result, and `JoiningTransform` then
finishes without reading its left input. It was true for `INNER` and `RIGHT` joins only, but a `LEFT SEMI`
join keeps a left row solely because it has a match, so an empty right side empties it just the same.

`GraceHashJoin` gates the same check on the join kind before it looks at its buckets, so it needs the same
condition.

The join runtime filter already stops the left side of such a join, which is why this is not visible with
the default settings; with `enable_join_runtime_filters = 0` a left semi join over 50 million rows against
an empty right side reads all of them, and none after this change.

Co-Authored-By: Claude Opus 5 <[email protected]>
raimannma added a commit to raimannma/ClickHouse that referenced this pull request Sep 10, 2026
…ut of this PR

Both are independent of `INTERSECT`/`EXCEPT` and are reviewed on their own:

- the final `DISTINCT` on all threads, with the constant-sort gather it needs, in ClickHouse#119228
- the left semi join that stops reading its left side for an empty right side, in ClickHouse#119218

`scatterByPartition`'s limit helpers and the `SortingStep` gather stay here as well, because the partitioned
`INTERSECT`/`EXCEPT` step needs them too; they are the same change in both pull requests.

Related: ClickHouse#119228
Related: ClickHouse#119218
@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 10, 2026
@vdimir vdimir added the can be tested Allows running workflows for external contributors label Sep 10, 2026
@vdimir vdimir self-assigned this Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7dc02e8]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR extends the empty-right early exit to LEFT SEMI joins in HashJoin, GraceHashJoin, and MergeJoin, and adds stateless and performance coverage for the affected algorithms. The implementation looks consistent across the touched join paths, but the main regression test still does not prove the zero-left-read contract the PR text and test comment claim, so I would keep the review open until that assertion is tightened.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/05154_semi_join_empty_right_side.sql:61] [dismissed by author -- https://github.com/Stop reading the left side of a left semi join with an empty right side #119218#discussion_r3980838853] The test comment and PR description both claim that only the 1000-row build side is read, but the assertion still accepts anything below 1,000,000 rows. A regression that reads several probe-side blocks before the stop triggers would still pass, so the test does not pin the behavior this PR is supposed to guarantee. Suggested fix: assert the actual build-side cardinality (read_rows = 1000, or at least another bound that fails on any left-side read).
Final Verdict

Changes requested. The join-side implementation is fine, but the functional regression test is still weaker than the contract this PR promises.

LLVM Coverage Report

Measured on commit 7dc02e8.

Metric Baseline Current Δ
Lines 88.40% 88.40% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 80.60% 80.50% -0.10%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 10, 2026
Comment thread src/Interpreters/HashJoin/HashJoin.cpp Resolved
…e too

`MergeJoin` accepts `LEFT SEMI` and runs through the same `JoiningTransform` early exit as the hash joins,
but its `alwaysReturnsEmptySet` still reported an empty result for `INNER` and `RIGHT` only, so
`join_algorithm = 'partial_merge'` kept reading the whole left side. `min_max_right_blocks` stays empty when
the right side is, because `mergeRightBlocks` returns before filling it, so the same condition applies.

The test covers `partial_merge` alongside the hash-based algorithms.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit 7dc02e8c62b1511478e07741103a706eda0bd766 with master failed: RuntimeError: CI logs cluster query failed: SELECT count() AS c FROM binary_sizes W….

See the job log for details.

@raimannma

Copy link
Copy Markdown
Contributor Author

@vdimir can you give those failed tests a retry, they shouldn't be related to this PR

@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 39 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 5e4fdb3b-e694-454a-9508-445365a6d70e
  • MIRAI run: 40ba0aec-9e9d-4f50-9720-b9b8f2cbcd5c
  • PR check IDs:
    • clickbench_348250_1789503760
    • clickbench_348256_1789503760
    • clickbench_348262_1789503760
    • tpch_adapted_1_official_348269_1789503760
    • tpch_adapted_1_official_348285_1789503760
    • tpch_adapted_1_official_348322_1789503760

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 11, 2026
@vdimir

vdimir commented Sep 11, 2026

Copy link
Copy Markdown
Member

@groeneai reference the PR where test_storage_iceberg_with_spark was fixed; if not fixed, investigate.

@maxknv maxknv removed the external Author is not a member of the ClickHouse organization label Sep 11, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 11, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Fixed by #119298, the revert of #118606 "Proper partition pruning with dates", merged 2026-09-10 17:26 UTC as 03bf3a961a74. The forward version is still open: #119302 re-lands it, with the pre-1970 handling in #119292.

The run here predates that revert, so the fix is not in the tree it tested. Two of the five failures are in test_storage_iceberg_with_spark/test_partition_pruning_with_functions.py, which the revert deleted, so that file no longer exists on master. The other three, test_bucket_partition_pruning[local|azure|s3], are the same regression: on master the module had 15, 12 and 12 failing rows on the three trees measured before the revert, and 0 on the revert commit itself and on each of the 20 master trees measured since.

A plain job retry will not clear it. Another PR whose tested tree also predates the revert had Integration tests (amd_asan_ubsan, db disk, old analyzer, 3/6) re-run at 09:03 UTC today, 15 hours after the revert, and got the identical five rows. This branch is 1081 commits behind master (merge base 2026-09-09 12:09 UTC), so merging master into semi-join-empty-right-side is what picks the fix up.

@rschu1ze

Copy link
Copy Markdown
Member

@raimannma Could you please add a performance test?

raimannma and others added 3 commits September 14, 2026 22:14
The left key is hashed with `sipHash64` to give the left side a cost worth
skipping: a plain `numbers_mt` source is read in a few milliseconds.

Local comparison against master (13 runs per side, one hyperthread per
physical core), median client time in seconds:

    join_algorithm   master   branch
    hash             0.336    0.0035
    parallel_hash    0.356    0.0058
    grace_hash       0.425    0.0040
    partial_merge    0.473    0.0033

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_018vHnrNqPvYi4i6xkDih7XK
@vdimir
vdimir added this pull request to the merge queue Sep 16, 2026
Merged via the queue into ClickHouse:master with commit 2dc8de9 Sep 16, 2026
175 of 177 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 16, 2026
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 comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements 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