Skip to content

Add RemoveRedundantSemiJoinPass to eliminate redundant SEMI/ANTI joins - #103254

Draft
wudidapaopao wants to merge 15 commits into
ClickHouse:masterfrom
wudidapaopao:eliminate_redundant_join
Draft

wudidapaopao wants to merge 15 commits into
ClickHouse:masterfrom
wudidapaopao:eliminate_redundant_join

Conversation

@wudidapaopao

@wudidapaopao wudidapaopao commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #77400.

Adds a new analyzer pass RemoveRedundantSemiJoinPass that drops a LEFT SEMI / LEFT ANTI JOIN when its right-hand side is provably redundant against a sibling SEMI/ANTI on the same key.

Approach

For each top-level query the pass walks the join tree and collects every LEFT SEMI / LEFT ANTI whose right side is the simple shape SELECT [...] FROM table [WHERE ...] joined on a single equi-key. Each candidate is summarised by (base table, right-side join column, set of WHERE conjuncts on the right side, count of RIGHT/FULL outer joins between the candidate and the join-tree root).

Candidates that share base table and right key are then bucketed and compared pairwise:

  • SEMI: keep the side whose conjunct set is a superset; rewrite the other join to a no-op pass-through.
  • ANTI: keep the side whose conjunct set is a subset (NULL-set semantics flip the direction).
  • Skip a candidate whose right-side columns are referenced anywhere else in the query, or when removing it would cross a RIGHT/FULL barrier in the unsafe direction.

Gated by the new setting optimize_remove_redundant_semi_join (enabled by default).

Cases that are optimized

  1. SEMI subset — keep the stricter, drop the looser:
SELECT u1.uid FROM users u1
  LEFT SEMI JOIN users u2 ON u1.uid = u2.uid
  LEFT SEMI JOIN (SELECT * FROM users WHERE age = 33) u3 ON u1.uid = u3.uid;
-- → drop the join with u2: every row passing u3 already passes u2.
  1. ANTI subset — keep the looser, drop the stricter:
SELECT o.id FROM orders o
  LEFT ANTI JOIN users u  ON o.uid = u.id
  LEFT ANTI JOIN (SELECT * FROM users WHERE banned) u2 ON o.uid = u2.id;
-- → drop the join with u2: the ANTI on u already excludes everything u2 would.
  1. Mixed intermediate joins (INNER, LEFT, and RIGHT/FULL in the safe direction) between the two candidates do not block elimination.

Cases that are NOT optimized

  • Right side is anything more than SELECT [...] FROM table [WHERE ...] (GROUP BY, DISTINCT, LIMIT, aggregates, joins, PREWHERE, computed projections — all skipped).
  • Filter subsumption is purely structural; range subsumption such as age > 10 ⊇ age > 20 is not detected.
  • Any column from the redundant join's right side is referenced elsewhere — SELECT, WHERE, GROUP BY / HAVING, ORDER BY, or a sibling JOIN ... ON.
  • A RIGHT / FULL outer join between the two candidates in the unsafe direction (its NULL-padding interacts differently with SEMI and ANTI).
  • Different right base tables, different right physical columns, different left key expressions, or a SEMI sitting next to an ANTI on the same table/key.
  • The ON clause is anything other than an AND of equals, or some equals has neither side resolving to a physical column on the right base table.

Test coverage matrix lives in tests/queries/0_stateless/04099_remove_redundant_semi_join.sql (44 cases: 17 positive, 27 negative).

Changelog category (leave one):

  • Performance Improvement

Changelog entry:

Added analyzer pass RemoveRedundantSemiJoinPass that removes a LEFT SEMI / LEFT ANTI JOIN when its right-hand side is provably redundant against a sibling SEMI/ANTI on the same key. Controlled by the new setting optimize_remove_redundant_semi_join (enabled by default).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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

New analyzer pass that drops a `LEFT SEMI` / `LEFT ANTI JOIN` when its
right-hand side is provably a subset (SEMI) or superset (ANTI) of
another sibling SEMI/ANTI on the same key. Controlled by
`optimize_remove_redundant_semi_join` (enabled by default).
@clickhouse-gh

clickhouse-gh Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [18d8115]

Summary: ❌

job_name test_name status info comment
Config Workflow FAIL
can_be_tested FAIL
Dockers Build (amd) DROPPED
Dockers Build (arm) DROPPED
Dockers Build (multiplatform manifest) DROPPED
Style check DROPPED
Code Review DROPPED
Docs check (Mintlify) DROPPED
Fast test DROPPED
Fast test (arm_darwin) DROPPED
Build (arm_tidy) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Apr 21, 2026
T38: SEMI with two-key ON drops the looser one.
T39: SEMI with two-key ON, conjuncts reordered and operands flipped, still
optimises (verifies canonicalisation in `extractJoinKeys`).
T40: SEMI multi-key vs single-key on the same table fall in different
buckets and are kept.
T41: SEMI multi-key ON where one equals has an expression on the LHS,
identical between two SEMI joins, drop the looser one.

T42: SEMI multi-key ON where one equals has neither side resolving to
a right-table physical column, no elimination.
@wudidapaopao
wudidapaopao force-pushed the eliminate_redundant_join branch from f4596b3 to f682d31 Compare April 21, 2026 12:04
@wudidapaopao
wudidapaopao force-pushed the eliminate_redundant_join branch from c73b6ae to ebc0913 Compare April 21, 2026 13:44
Comment thread src/Core/Settings.cpp
DECLARE(Bool, optimize_if_transform_strings_to_enum, false, R"(
Replaces string-type arguments in If and Transform to enum. Disabled by default cause it could make inconsistent change in distributed query that would lead to its fail.
)", 0) \
DECLARE(Bool, optimize_remove_redundant_semi_join, true, R"(

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.

This PR introduces a new user-facing setting, but the PR metadata still has Documentation is written unchecked and there is no matching docs update for optimize_remove_redundant_semi_join.

Please add docs for the setting (behavior, default, and safety limitations) and then mark the documentation checkbox accordingly.

@wudidapaopao
wudidapaopao force-pushed the eliminate_redundant_join branch from a07797a to db4bbee Compare April 22, 2026 11:31
@wudidapaopao
wudidapaopao force-pushed the eliminate_redundant_join branch from db4bbee to cef8c4d Compare April 22, 2026 11:33
@clickhouse-gh

clickhouse-gh Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 95.41% (270/283) · Uncovered code

Full report · Diff report

@wudidapaopao
wudidapaopao marked this pull request as draft July 3, 2026 05:37
@abashkeev abashkeev added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Sep 4, 2026
…_join

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

- retargeted PR record [email protected] to 26.10 (settings-history check rule)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

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-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove redundant JOINs

3 participants