Repository navigation
Do not read a JOIN input that cannot contribute under a WHERE no row passes - #123829
Conversation
…passes Since the constant-conjunct guard of ClickHouse#107084, a no-input conjunct of a filter above a JOIN is kept off the side the filter cannot be pushed to (the non-preserved side of an OUTER join, or a side none of whose columns is used above the join). A UNION ALL branch made false by an outer WHERE on its constant column (`materialize(k4)` from the UNION conversion) therefore read, aggregated and hash-built that side in full (issue ClickHouse#123702). When the filter above a JoinStepLogical is false for every row, replace each input it cannot be pushed to with ReadNothing, using the helper extracted from the constant-false ON short-circuit. A `FULL JOIN` under such a `WHERE` has both inputs replaced. The helper keeps an input that carries more than rows: a JoinStepLogicalLookup, also below single-child wrappers, a subtree that can produce totals (TotalsHavingStep, a remote read, or a query result cache read), since a join joins its inputs' totals, or a subtree holding one half of a shared subplan (`CommonSubplanStep` / `CommonSubplanReferenceStep`, which decorrelation pairs across a join). The totals case also restores the totals row the ON short-circuit dropped since 26.8. Join-order estimation now treats ReadNothing as an exact empty relation, as it already does for an exact empty MergeTree read, so a replaced input keeps its exact zero estimate. `04516_join_reorder_duplicate_names_merge_expression` now ignores the `[0]` row estimate an empty table gets in its EXPLAIN label. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the JoinStepLogicalLookup comment to one line and drop the comment above carriesMoreThanRows, which restated its own doc. 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; 5 findings over 3 rounds) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261004-054000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261004-044100 |
|
Workflow [PR], commit [57a85fb] Summary: ✅
AI ReviewSummaryThis PR narrows the Final Verdict
LLVM Coverage ReportMeasured on commit 57a85fb.
Changed lines: Changed C/C++ lines covered: 27/28 (96.43%) · Uncovered code |
CI finish ledger - 9d0b68dEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The Fast test failure dropped the remaining jobs, so this head has no other test signal yet. CIDB has no other Session id: cron:our-pr-ci-monitor:20261004-090143 |
|
@groeneai check CI failures |
…ut-under-false-where Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The only failed test was I merged master, which includes #123823, at 3808f74, and full CI is running again. On a local build of the merged head, this PR's test |
|
📊 Cloud Performance Report ✅ AI verdict: 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
|
…07084 Review on ClickHouse#123702 asked to keep the fix local to ClickHouse#107084, so the plan-level ReadNothing rule is replaced by an exception in that guard. The guard keeps a conjunct that reads no column off a join input that push-down is disabled for: pushing it there and dropping it from the post-join filter changes the result when it is NULL (04335) or true but pruning (`indexHint`). When no row passes the filter and the join kind allows filtering one input, that input receives the conjunct and the join output is empty whatever the other input holds, so the guard is skipped and the disabled input is filtered too, as before 26.7. "No row passes" is `filterResultForNotMatchedRows` with no input defaulted, which the outer join conversions already use, and the filter must not be sensitive to its evaluation count. This also drops the ReadNothingStep row estimate and the oracle change in 04516_join_reorder_duplicate_names_merge_expression, which only the ReadNothing rule needed. The WITH TOTALS row lost by the constant-false ON short-circuit is unrelated to ClickHouse#107084 and goes to a separate PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units1239 translation units recompiled, 8850 s compile time in total, 1239 of them have a recent master baseline. |
Cherry pick #123829 to 26.8: Do not read a JOIN input that cannot contribute under a WHERE no row passes
…bute under a WHERE no row passes
| /// With no input given only the conjuncts that read no column are evaluated, so FALSE means no row passes. | ||
| const bool filter_is_always_false | ||
| = (left_stream_filter_push_down_input_columns_available || right_stream_filter_push_down_input_columns_available) | ||
| && !isSensitiveToEvaluationCount(filter->getExpression()) |
There was a problem hiding this comment.
Why do we use isSensitiveToEvaluationCount but now dagContainsNonDeterministicFunction for example?
There was a problem hiding this comment.
dagContainsNonDeterministicFunction would be a no-op here: filterResultForNotMatchedRows already returns UNKNOWN for such a DAG (Utils.cpp:175), and a filter with a stateful function returns from tryPushDownFilter before it reaches the join (filterPushDown.cpp:1175). So at this spot isSensitiveToEvaluationCount only adds hasObservableSideEffects, i.e. sleep/sleepEachRow: with the flag set, the conjunct is also evaluated on every block of the side #107084 keeps it off. It is the predicate the merge gate in optimizeJoin.cpp uses for the same situation (an expression evaluated in an extra place).
The effect is narrow. With default settings, the outer-to-inner conversion and use_join_disjunctions_push_down already push such a sleep into the right input. With both off:
CREATE TABLE l (k UInt64) ENGINE = MergeTree ORDER BY k;
CREATE TABLE r (k UInt64) ENGINE = MergeTree ORDER BY k;
INSERT INTO l SELECT number FROM numbers(1000);
INSERT INTO r SELECT number FROM numbers(1000000);
SELECT count() FROM l LEFT JOIN r ON l.k = r.k WHERE sleep(0.0001) = 1
SETTINGS query_plan_join_swap_table = 'false', query_plan_convert_outer_join_to_inner_join = 0, use_join_disjunctions_push_down = 0;SleepFunctionCalls: 19 on 26.6.2.158, 1 on 26.8.2.7 and on this PR (18 or 19 on all three with default settings). Dropping the term would only affect sleep/sleepEachRow in an always-false filter.
For the backports, removing it is the simplest fix: 26.8 has neither IFunctionBase::hasObservableSideEffects nor allNodeFunctions (the errors on 34f42da), and sleep carries no flag there, so nothing is lost. On 26.9 the error on 13578f3 is the old static copy at optimizeJoin.cpp:771; it has the same body, so deleting it is enough.
The previous commit declares `isSensitiveToEvaluationCount` in
`Optimizations/Utils.h`, but the file-static copy with the same body is
still in `optimizeJoin.cpp`, so that file does not compile ("static
declaration follows non-static declaration"). Remove the copy and keep
its comment for `canMergeExpressionIntoJoinGraph`, as #120510 does on
master.
Related: #123829
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…123829 The previous commit copies master's body, which uses `allNodeFunctions` and `IFunctionBase::hasObservableSideEffects`; 26.8 has neither, so `Utils.cpp` does not compile. Check the same properties with what 26.8 has: a function that is non-deterministic within the query or stateful, `sleep` and `sleepEachRow` by name (the only functions master reports as having observable side effects), and any lambda, since on 26.8 neither this check nor `dagContainsNonDeterministicFunction` looks into a lambda body. Related: #123829 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport #123829 to 26.9: Do not read a JOIN input that cannot contribute under a WHERE no row passes
Backport #123829 to 26.8: Do not read a JOIN input that cannot contribute under a WHERE no row passes
…not contribute under a WHERE no row passes
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
WHEREthat no row passes is pushed to everyJOINinput again, including the non-preserved side of anOUTER JOINand a side with no column used above the join, so that input is no longer read and hash-built for nothing. For example, aUNION ALLbranch whose constant column fails the outerWHEREis skipped as in 26.6. Closes #123702, closes #123878.Description
Since #107084 (26.7) a filter conjunct that reads no column is kept off a join input that push-down is disabled for: pushing it there and dropping it from the post-join filter changes the result when it is
NULL(04335) or true but pruning (indexHint). In #123702 such a conjunct comes from aUNION ALLbranch whose constant column fails the outerWHERE. It is false for every row, yet the right side of theLEFT JOINin that branch is read, aggregated and hash-built.Change, local to that guard:
tryPushDownOverJoinSteptellssplitActionsForJOINFilterPushDownwhen no row passes the filter (filterResultForNotMatchedRowswith no input defaulted, as the OUTER JOIN conversions use, and the filter is not sensitive to its evaluation count) and the join kind allows filtering one input. That input receives the conjunct, so the join output is empty whatever the other input holds, and the guard is skipped: the disabled input is filtered too, as before 26.7.Validation, rows read on a debug build: the issue query with
compatibility = '24.6'reads 190,104 instead of 22,084,104; the issue'sLEFT JOINview andINNER JOINbranch reproducers read 0 instead of 10,000,000 and 20,000,080, like 26.6.05320fails on master and passes 50/50 randomized; itsindexHintarm returns 958 instead of 150 when the guard is removed. An A/B over 2,185 join, filter, union and EXPLAIN+JOIN tests changes no reference.The earlier version of this PR also restored a
WITH TOTALSrow lost by the constant-falseONshort-circuit; that is unrelated to #107084 and goes to a separate PR.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123829]
Version info
26.10.1.1661-master(included in26.10and later)26.9.13.10,26.8.20.12