Repository navigation
Restore primary key pruning by the compare-chain indexHint in RIGHT JOIN ON and in subqueries of an outer JOIN ON - #126551
Conversation
…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]>
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.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261018-015000 |
|
Workflow [PR], commit [edd5783] Summary: ✅
AI ReviewSummaryThis PR restores primary-key pruning through derived Final VerdictStatus: ✅ Approve |
|
cc @yakov-olkhovskiy @rschu1ze, could you review this and add the |
|
|
||
| /// 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 |
There was a problem hiding this comment.
What's an "input-less conjunct of ON" and how is it related to a preserved join left side (variable name)?
There was a problem hiding this comment.
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]>
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`
Closes: #126541
Related: #126170
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The bound that
optimize_and_compare_chainderives within one table (for exampleindexHint(l.a < 1000)froml.a < l.b AND l.b < 1000) prunes the primary key again inRIGHT JOIN ... ONand in a subquery nested in an outerJOIN ... ON.Description
#126170 (master only, in no release) stopped adding the derived
indexHintanywhere inside an outerJOIN ON: the planner evaluates an input-lessONconjunct with the join's left side, and forLEFT/FULLthat side is preserved, sofull_sorting_mergerefuses it. The gate,preserved_join_tables.empty(), was too coarse in two cases:RIGHT JOINthe left side is not preserved, so the hint becomes a left pre-filter;ONis 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 < 1000readsGranules: 64/64on master and1/64with 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, notSEMI) and gates the hint on that, and it visits a subquery orUNIONchild with the join state cleared.LEFT/FULLstill drop the hint, so the #126170full_sorting_mergecase stays fixed. AGLOBAL RIGHT JOINstill drops it: the planner may swap it into aLEFT JOINafter this pass (tryRewriteGlobalRightJoinAsLeftJoin), soisGlobalJoinmoved toAnalyzer/Utilsfor both places to share.Validation: in
05331_and_compare_chain_outer_join_on, the newindexHintcount andGranules: 1/64rows for both shapes fail on master and pass with the change. AGLOBAL RIGHT JOINrow overremote()underfull_sorting_mergepasses on master and with the change, and throwsNOT_IMPLEMENTEDwithout theGLOBALexclusion.[GLOBAL] RIGHT [ALL|ANY|ANTI|SEMI] JOINwith sevenjoin_algorithmvalues gives the same rows and refusals with the chain on and off. 50 randomized runs pass, as do parallel replicas,DBReplicatedand 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
26.11.1.191-master(included in26.11and later)26.10.7.14