Skip to content

Restore primary key pruning by the compare-chain indexHint in RIGHT JOIN ON and in subqueries of an outer JOIN ON - #126551

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-compare-chain-hint-right-join-and-on-subqueries
Oct 19, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-compare-chain-hint-right-join-and-on-subqueries

Conversation

@groeneai

@groeneai groeneai commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #126541
Related: #126170

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):

The bound that optimize_and_compare_chain derives within one table (for example indexHint(l.a < 1000) from l.a < l.b AND l.b < 1000) prunes the primary key again in RIGHT JOIN ... ON and in a subquery nested in an outer JOIN ... ON.

Description

#126170 (master only, in no release) stopped adding the derived indexHint anywhere inside an outer JOIN ON: the planner evaluates an input-less ON conjunct with the join's left side, and for LEFT/FULL that side is preserved, so full_sorting_merge refuses it. The gate, preserved_join_tables.empty(), was too coarse in two cases:

  • in RIGHT JOIN the left side is not preserved, so the hint becomes a left pre-filter;
  • a subquery nested in ON is a separate query, but the visitor kept the outer join's state there.

On the issue's table, t AS l RIGHT JOIN r ON l.k = r.k AND l.a < l.b AND l.b < 1000 reads Granules: 64/64 on master and 1/64 with this change, and so does the subquery case. Results do not change.

Change: the visitor records whether the current join's left side is preserved (LEFT/FULL, not SEMI) and gates the hint on that, and it visits a subquery or UNION child with the join state cleared. LEFT/FULL still drop the hint, so the #126170 full_sorting_merge case stays fixed. A GLOBAL RIGHT JOIN still drops it: the planner may swap it into a LEFT JOIN after this pass (tryRewriteGlobalRightJoinAsLeftJoin), so isGlobalJoin moved to Analyzer/Utils for both places to share.

Validation: in 05331_and_compare_chain_outer_join_on, the new indexHint count and Granules: 1/64 rows for both shapes fail on master and pass with the change. A GLOBAL RIGHT JOIN row over remote() under full_sorting_merge passes on master and with the change, and throws NOT_IMPLEMENTED without the GLOBAL exclusion. [GLOBAL] RIGHT [ALL|ANY|ANTI|SEMI] JOIN with seven join_algorithm values gives the same rows and refusals with the chain on and off. 50 randomized runs pass, as do parallel replicas, DBReplicated and distributed plan runs.

If #126170 is ever backported, this change has to go with it.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.191-master (included in 26.11 and later)
  • Backported to: 26.10.7.14

groeneai and others added 2 commits October 18, 2026 00:47
…s of an outer JOIN ON

ClickHouse#126170 stopped adding the within-one-source `indexHint` bound anywhere
inside an outer JOIN ON, gated on `preserved_join_tables.empty()`. The
reason for that gate is that the planner evaluates an input-less ON
conjunct with the join's left side (`concatConditions` and
`canBeEvaluatedOnSide` in JoinStepLogical.cpp), so for LEFT/FULL the hint
stays in the join and `full_sorting_merge` refuses it.

The proxy was too coarse: for RIGHT JOIN the left side is not preserved
and the hint becomes a left pre-filter (`canPushDownFromOn`), as for
INNER JOIN; and `needChildVisit` kept the outer join's state while
visiting a subquery nested in ON, which is a separate query. Both lost
primary key pruning (Granules 64/64 instead of 1/64 on the issue's
table); results were unaffected.

Track whether the current join's left side is preserved and gate the hint
on that, and visit a QueryNode/UnionNode child with the join state
cleared. LEFT/FULL keep dropping the hint, so the ClickHouse#126170 case stays
fixed. RIGHT [ALL|ANY|ANTI|SEMI] JOIN with every join algorithm gives the
same rows and refusals with the chain on and off.

Closes ClickHouse#126541

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`tryRewriteGlobalRightJoinAsLeftJoin` can swap the sides of a GLOBAL
RIGHT JOIN into a LEFT JOIN after the query tree passes ran, when its
left side is a Distributed table with at least two shards. The
input-less `indexHint` that the compare chain adds for the non-preserved
left side then lands on the new, preserved left side, stays in the join
condition, and `full_sorting_merge` refuses the query (NOT_IMPLEMENTED).

Treat the left side of a GLOBAL RIGHT JOIN as preserved for that
decision, using the same `isGlobalJoin` the planner uses, now shared
from Analyzer/Utils. A GLOBAL RIGHT JOIN that is not swapped keeps
master's behaviour (no hint); plain derived bounds carry their inputs
and follow their side across the swap, so they are unaffected.

The test fixture now has 64 granules so the pruning itself is checked
(`Granules: 1/64` for both shapes), and a GLOBAL RIGHT JOIN over
remote() runs under full_sorting_merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex), two rounds: 2 findings in round 1, 0 in round 2.

# Sev Finding Verdict Evidence / action
1 ❌ The planner rewrites some GLOBAL RIGHT JOINs to LEFT after the pass (tryRewriteGlobalRightJoinAsLeftJoin), so the input-less hint lands on the now-preserved left side and full_sorting_merge refuses the query AGREE, fixed the hint is also dropped for a RIGHT JOIN that isGlobalJoin (one shared helper); regression row with remote('127.0.0.{1,2}', ...) throws NOT_IMPLEMENTED without the exclusion
2 ⚠️ The new rows ran on a one-granule table, so they could not show pruning AGREE, fixed 64-granule fixture, Granules: 1/64 asserted for both shapes
3 💡 The subquery Granules row pins parallel_replicas_local_plan = 1, which the test runner already forces in every CI configuration that enables parallel replicas noted, not blocking harmless; it only matters for a manual run with parallel replicas and parallel_replicas_local_plan = 0

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261018-015000

@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [edd5783]

Summary: ✅


AI Review

Summary

This PR restores primary-key pruning through derived indexHint bounds in RIGHT JOIN ... ON and subqueries inside outer-join conditions, while retaining the exclusion for GLOBAL RIGHT JOIN that may be rewritten as LEFT JOIN. Reviewed head: edd5783f539d06143235904ee84f61e4d08320e0. No actionable findings. The focused regression test passes in the current-head Fast test report.

Final Verdict

Status: ✅ Approve

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @yakov-olkhovskiy @rschu1ze, could you review this and add the can be tested label so CI runs? It restores the primary key pruning that #126170 lost: the derived indexHint is now dropped only when the current join's left side is preserved (LEFT/FULL, or a GLOBAL RIGHT JOIN the planner may swap), and a subquery nested in ON no longer inherits the outer join's state.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 18, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 18, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 18, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026

/// a conjunct over these can't leave ON, and full_sorting_merge rejects it there
TableExpressionNodes preserved_join_tables;
/// the join evaluates an input-less conjunct of ON with its left side

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's an "input-less conjunct of ON" and how is it related to a preserved join left side (variable name)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It is an ON conjunct whose actions-DAG node reads no column of either input. Here that is the derived indexHint(...): the planner keeps its argument in the function's own DAG and adds the node without arguments, so JoinStepLogical treats it as a left side condition (fromLeft() || fromNone() in concatConditions and canBeEvaluatedOnSide).

That is the link to the left side: in an INNER or RIGHT JOIN the condition can be pushed into the left input, where the hint prunes granules. When the join may output left rows that fail ON (LEFT, FULL, LEFT ANTI), it can't be pushed down, stays in ON, and full_sorting_merge rejects it (the failure #126170 fixed). The flag records that property of the current join.

The comment did not say this. I rewrote it in e0bfabd and dropped the duplicate line at the use site (comments only).

Review question on the field's comment: what an input-less conjunct of ON
is and how it relates to the field name. The derived `indexHint` keeps its
argument in its own actions DAG, so its node reads no input column and
JoinStepLogical assigns it to the left side (`fromLeft() || fromNone()`).
If the join may output left rows that fail ON (LEFT/FULL, LEFT ANTI, or a
GLOBAL RIGHT JOIN the planner swaps), a left side condition cannot be
pushed into that side, stays in ON, and full_sorting_merge rejects it.
State that on the field and drop the duplicate line at the use site.
Comments only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged via the queue into ClickHouse:master with commit 3a4b224 Oct 19, 2026
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 18, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 18, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 18, 2026
Backport #126551 to 26.10: Restore primary key pruning by the compare-chain `indexHint` in `RIGHT JOIN ON` and in subqueries of an outer `JOIN ON`
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-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RIGHT JOIN ON loses indexHint pruning on left table's primary key (#126170)

6 participants