Repository navigation
Fix NOT_IMPLEMENTED for outer JOIN with full_sorting_merge when the compare chain derives a bound the join evaluates on its preserved side - #126170
Conversation
…e keys With `optimize_and_compare_chain`, the chain derives `expr op const` from `expr = y AND y < 2` and appends it to the `AND`. In an outer join `ON` a conjunct over the preserved side cannot be pushed down, so it stays in `ON` and `full_sorting_merge` rejects the join with `NOT_IMPLEMENTED`. ClickHouse#124233 guarded this with `getExpressionSource(expr).first`, which is `nullptr` for an expression over several table expressions, so the guard missed `a.number + b.number` over an inner join, the `firstNonDefault` key of `FULL JOIN ... USING`, and an `ARRAY JOIN` alias mixed with a column. Test every column's source against the preserved tables instead. The `ALWAYS_FALSE` branch also appended a preserved-side contradiction unguarded. A bound the guard skipped was still recorded, so a later bound on the same expression conflicted with it (`l.number < 2` skipped, then `l.number > 5` appended), and a type-impossible bound (`UInt8 > 300`) was appended on its own; with `optimize_redundant_comparisons = 0` nothing folds them and the join is rejected. Derive a preserved-side bound only when the user already wrote a conjunct on that expression: the derivation then still exposes a contradiction the next pass folds to `false`, and that `ON` already holds a preserved-side conjunct of the user's. Closes ClickHouse#125291 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he preserved side The planner skips lambdas when it splits `ON` by side, but the check rejected them as an unknown node, so a lambda key such as `arraySum(x -> x * 2, a.arr) + b.number` still got its bound appended. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ds only the preserved side The join's side split reads the columns a lambda captures, so a key like `arraySum(x -> x + a.number + b.number, [0])` reads only the preserved side and its derived bound must not be appended to ON. Lambda arguments are skipped, as they are not table columns. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The join evaluates a conjunct without table columns with its left side, the preserved side of LEFT and FULL joins, so a derived `materialize(0) < 2` must not be appended to ON. For a RIGHT JOIN this only skips a bound the user's ON already implies. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…und in outer JOIN ON The planner builds `indexHint` with no inputs, so the join evaluates it, and any conjunct whose columns sit only under it, with its left side: the preserved side of LEFT and FULL joins, which full_sorting_merge refuses. - `readsOnlyPreservedJoinTables` counted the columns under `indexHint`, so `l.number + indexHint(r.number < 2) < 2` was taken as reading the other side and appended. It now skips `indexHint` arguments. - The `indexHint` wrapper the pass adds for a bound derived within one source became a left pre-filter even when the bound reads only the other side (`r.a < r.b AND r.b < 2` -> `indexHint(r.a < 2)`). It is no longer added inside an outer join ON. For a RIGHT join this also drops a hint the join could push to the left input; the derived bound is implied by the user's ON there, so the rows never change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)Reviewed heads
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261015-213200 |
|
Workflow [PR], commit [e2974b7] Summary: ✅
AI ReviewSummaryThis PR fixes outer Final VerdictStatus: ✅ Approve. |
|
cc @yariks5s @yakov-olkhovskiy, could you review this and add the |
…IN ON When the join builds its ActionsDAG it folds `x IN <set>` to a constant once the constant set turns out empty (`FunctionIn::getConstantResultForNonConstArguments`), and with it every expression the folded value feeds, e.g. `AND`. The folded node loses its children, so `l.number + (r.k IN [])` reads only the preserved side, and a bound derived over it is a left pre-filter that `full_sorting_merge` refuses with NOT_IMPLEMENTED. The query tree cannot tell whether the set will be empty: `[]`, `()`, `(NULL)` and `(-1)` for an unsigned key all fold. So a key holding an `IN` over a constant set now counts as preserved, which costs only the derived bound for such keys. The walk no longer stops at the first column of another table, because the `IN` can sit after it, as in `(r.k IN []) AND r.k > 0`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… in outer JOIN ON The join's ActionsDAG folds calls the query tree still holds unfolded, e.g. the `if(1, 0, r.k)` that `multiIf` rewriting leaves, an `IN` over an empty constant set, or `and(0, ...)` over them, and a folded node loses its inputs. So the key `l.number + multiIf(1, 0, r.k)` reads only the preserved side in the join, and the bound derived over it becomes a left pre-filter that `full_sorting_merge` refuses with NOT_IMPLEMENTED. Folding only drops inputs, so any key with a column of the preserved side may end up there, and it now counts as preserved. A bound over a mixed key gains nothing anyway: it can't leave ON, and `full_sorting_merge` rejects mixed conditions. This subsumes the special case for `IN` over a constant set, which is removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/DataTypes/TypeFunctions.cpp.o |
new | 1.00 MiB | +1.00 MiB |
src/CMakeFiles/dbms.dir/Interpreters/Aggregator.cpp.o |
25.56 MiB | 26.43 MiB | +895.89 KiB (+3.42%) |
src/CMakeFiles/dbms.dir/DataTypes/TypeMatchers.cpp.o |
new | 692.78 KiB | +692.78 KiB |
src/CMakeFiles/dbms.dir/DataTypes/FunctionSignature.cpp.o |
new | 417.14 KiB | +417.14 KiB |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDotProduct.cpp.o |
1.75 MiB | 1.55 MiB | -201.52 KiB (-11.24%) |
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl2.cpp.o |
1.31 MiB | 1.14 MiB | -174.78 KiB (-13.07%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsBitmap.cpp.o |
2.58 MiB | 2.41 MiB | -167.40 KiB (-6.35%) |
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl1.cpp.o |
1.12 MiB | 986.07 KiB | -160.78 KiB (-14.02%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/vectorFunctions.cpp.o |
3.29 MiB | 3.43 MiB | +148.59 KiB (+4.41%) |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayCumSumNonNegative.cpp.o |
603.16 KiB | 466.82 KiB | -136.34 KiB (-22.60%) |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayCumSum.cpp.o |
673.15 KiB | 538.16 KiB | -134.99 KiB (-20.05%) |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayAggregation.cpp.o |
1.29 MiB | 1.16 MiB | -134.86 KiB (-10.22%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsCodingIP.cpp.o |
1.27 MiB | 1.13 MiB | -133.67 KiB (-10.32%) |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDifference.cpp.o |
592.61 KiB | 478.04 KiB | -114.56 KiB (-19.33%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsJSON.cpp.o |
4.26 MiB | 4.37 MiB | +111.80 KiB (+2.57%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsHashingMisc.cpp.o |
5.53 MiB | 5.61 MiB | +83.25 KiB (+1.47%) |
src/Functions/array/CMakeFiles/clickhouse_functions_array.dir/arrayDistance.cpp.o |
10.19 MiB | 10.11 MiB | -78.98 KiB (-0.76%) |
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/FunctionsConversion_reg.cpp.o |
6.83 MiB | 6.75 MiB | -78.39 KiB (-1.12%) |
src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl3.cpp.o |
1.87 MiB | 1.80 MiB | -76.37 KiB (-3.98%) |
src/CMakeFiles/dbms.dir/Functions/IFunction.cpp.o |
555.11 KiB | 631.16 KiB | +76.05 KiB (+13.70%) |
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
2854 translation units recompiled, 15964 s compile time in total, 2849 of them have a recent master baseline.
Translation units without a recent master baseline:
programs/keeper/Keeper.cpp (clickhouse-keeper): 8.2 ssrc/DataTypes/TypeFunctions.cpp: 6.4 s
|
@groeneai @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…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]>
Backport #126170 to 26.10: Fix `NOT_IMPLEMENTED` for outer `JOIN` with `full_sorting_merge` when the compare chain derives a bound the join evaluates on its preserved side
…h `full_sorting_merge` when the compare chain derives a bound the join evaluates on its preserved side
Closes: #125291
Related: #124233
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed outer
JOINwithjoin_algorithm = 'full_sorting_merge'failing withNOT_IMPLEMENTEDwhenoptimize_and_compare_chainderived a bound fromONthat the join evaluates on its preserved side: for a key over several tables (for examplea.number + b.numberafter an inner join, aFULL JOIN ... USINGkey, anARRAY JOINalias combined with a column, or a key with a lambda), for a bound derived within the other side (fromr.a < r.b AND r.b < 2), and, withoptimize_redundant_comparisons = 0, for a derived bound that contradicts another one or cannot hold for the column type.Description
On 26.8.18.2,
SELECT a.number, c.number FROM numbers(3) AS a INNER JOIN numbers(3) AS b ON a.number = b.number LEFT JOIN numbers(3) AS c ON a.number + b.number = c.number AND c.number < 2 SETTINGS join_algorithm = 'full_sorting_merge'fails withNOT_IMPLEMENTED; withoptimize_and_compare_chain = 0it returns three rows. Master still fails after #124233.The compare chain appends the derived
a.number + b.number < 2toON. It reads only the preserved side, so it cannot be pushed down, andfull_sorting_mergerefuses a single-sideONfilter. #124233 skips such bounds, but testsgetExpressionSource(expr).first, which isnullptrfor a key over several tables. The same happens for aFULL JOIN ... USINGkey, anARRAY JOINalias plus a column, keys with lambdas orindexHint, a column-less key, evaluated with the left side, and keys whose other-side columns the join folds away (multiIf(1, 0, r.k),IN []). The chain's ownindexHint(r.a < 2)for a bound within the other side reads no input either. TheALWAYS_FALSEbranch appended a preserved-side contradiction unchecked, and a skipped bound was still recorded, sol.number < 2(skipped) thenl.number > 5got appended; withoptimize_redundant_comparisons = 0nothing folds it.Change: a bound is preserved-side when it reads any column of the preserved side (folding can drop the others; lambda captures count,
indexHintarguments do not) or none, and it is derived only whenONalready has a conjunct on that expression, to expose contradictions. NoindexHintbound is added to an outerON. A preserved-side bound that cannot hold for the type (UInt8 > 300) no longer foldsONtofalse; the rows are the same.Validation: fourteen rows and two
EXPLAINrows in05331_and_compare_chain_outer_join_on; thirteen fail on master and pass with the fix; 50 randomized runs pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126170]
Version info
26.11.1.128-master(included in26.11and later)