Repository navigation
Do not rewrite a join into a shape no enabled algorithm can execute - #118315
Conversation
`query_plan_convert_any_join_to_semi_or_anti_join` turns the `ANY` join of a
decorrelated `EXISTS` - or of a user's own query - into a `SEMI` or `ANTI` join
whenever the filter above it allows. The sort-merge algorithms implement
neither, so under `join_algorithm = 'full_sorting_merge'` the conversion turned
a query that runs into one that throws:
SELECT count() FROM t AS o WHERE EXISTS (SELECT 1 FROM t AS i WHERE i.b = o.b AND i.a = 5)
SETTINGS join_algorithm = 'full_sorting_merge';
Code: 48. None of the algorithms enabled by the 'join_algorithm' setting [full_sorting_merge]
can execute this SEMI LEFT JOIN with the given right table storage type
The pass now converts only into a strictness one of the enabled algorithms can
execute. `rewrite_in_to_join` reaches the same conversion through the `EXISTS` it
builds out of `x IN (subquery)`, so that path is fixed by the same check.
The decorrelation join itself had the same problem one step earlier: it is built
as `ANY RIGHT`, which no sort-merge algorithm executes and which `partial_merge`
does not do at all beyond `INNER`/`LEFT`. As the buffered case in the same
function already does, the join now gets the hash algorithms when the
user-facing list cannot execute it - that list describes the user's joins, not
an internal decorrelation detail.
The capability question is answered in one place, `JoinCommon::canBeExecutedByEnabledAlgorithm`.
Closes: #111075
Closes: #111207
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [f17c0c7] Summary: ✅
AI ReviewSummaryThis PR closes a real optimizer contract gap: it now prevents Final Verdict
LLVM Coverage ReportMeasured on commit f17c0c7.
Changed lines: Changed C/C++ lines covered: 86/90 (95.56%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…`WHERE` The same guard fixes a second reported shape, which needs neither `rewrite_in_to_join` nor a correlated subquery: an always-false `WHERE` over a user-written `ANY LEFT JOIN` against an `ENGINE = Join` table is `FALSE` for the not matched rows, so the pass converted the join to `SEMI LEFT`, which no sort-merge algorithm can execute, and the query failed with `NOT_IMPLEMENTED` under `join_algorithm = 'full_sorting_merge'` while the unoptimized plan ran. Closes: #111877 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The correlated `EXISTS` whose decorrelated join the test is about is not supported without the analyzer, where the query fails with `Missing columns` instead. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…a hash fallback `canBeExecutedByEnabledAlgorithm` treated `PREFER_PARTIAL_MERGE` as if it only enabled `MergeJoin`, but `tryCreateJoin` in both `PlannerJoins` and `ExpressionAnalyzer` falls through to the hash algorithms whenever `MergeJoin` cannot execute the requested kind and strictness. The helper therefore reported `false` for shapes such as `ANY RIGHT` or `ANTI` that the planner would have executed, which suppressed the safe `ANY` -> `SEMI`/`ANTI` rewrites and made `PlannerCorrelatedSubqueries` override the algorithm list for nothing. Treat `prefer_partial_merge` as able to execute every kind and strictness, and cover it in `05093_any_join_to_semi_anti_algorithm_check`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
With `prefer_partial_merge` modelled as `partial_merge` plus the hash fallback, the `ANY` -> `SEMI`/`ANTI` conversion happens again under that setting. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…bility-check-111075
CI randomizes `query_plan_convert_any_join_to_semi_or_anti_join`, so both new tests could pass with the conversion pass they exercise switched off. Pin it to `1` in `05093_any_join_to_semi_anti_algorithm_check` and `05102_storage_join_any_to_semi_algorithm_check`. `05102_storage_join_any_to_semi_algorithm_check` also pins `join_use_nulls = 0`, because an `ENGINE = Join` table rejects a mismatched `join_use_nulls` with an unrelated error, the same way `03630_any_join_to_semi_storage_join_guard` does. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Commit 3e5da64 picked up the submodule pointers of another branch that happened to be checked out in the same working tree. They have nothing to do with this change, so put both back to what `master` has.
…bility-check-111075
…run it `tryCreateJoin` in both `PlannerJoins.cpp` and `ExpressionAnalyzer.cpp` passes `grace_hash` over for the next algorithm of the `join_algorithm` list when there is no spill threshold and `legacy_join_size_limits_trigger_spilling` is off. So `canBeExecutedByEnabledAlgorithm` reporting it as able to execute every kind and strictness let `join_algorithm = 'full_sorting_merge,grace_hash'` rewrite an `ANY LEFT` join into `SEMI LEFT`, which then failed with `NOT_IMPLEMENTED`, and kept the correlated-subquery fallback from replacing lists like `grace_hash,partial_merge`. The helper now takes the whole `JoinSettings` and applies the same rule. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Merged master and adopted the |
|
Investigated. This is the Cascades route of #115986, and your #119814 fixes it (merged 2026-09-25 02:50Z), so there is no new PR from me. Attribution. The abort is query Fix presence. The job built Since the merge, the only hit on a master-based tree is #122124 at 03:18Z, and that job built on master |
…lters `05257_any_join_to_semi_grace_hash_without_spill_threshold` failed in CI with `query_plan_optimize_join_order_randomize`: the join reordering swapped the `ANY LEFT` join into `ANY RIGHT`, which takes a runtime filter, and the runtime filter pass cut `join_algorithm = 'full_sorting_merge,grace_hash'` down to `grace_hash` alone. Alone in the list, `grace_hash` without a spill threshold is no longer passed over but refused with `BAD_ARGUMENTS`. The pass now keeps `grace_hash` only where the join pickers would run it. Address review: the decision is shared in `JoinSettings::canRunGraceHash`, and it no longer counts a threshold given only by `max_bytes_ratio_before_external_join`: each server resolves the ratio against its own memory limits, so a remote server without them would pass `grace_hash` over after the initiator counted it. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118315&sha=dd5ac5176cba19dccd462e71156bfbd31c47ca61&name_0=PR PR: #118315 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit lets `filterResultForNotMatchedRows` decide filters that contain an `IN (subquery)` set that is not built yet. That helper is also used by `tryConvertAnyJoinToSemiOrAntiJoin`, so an `ANY LEFT/RIGHT JOIN ... WHERE rhs_predicate AND rhs_col IN (subquery)` started to be rewritten to `SEMI`. `full_sorting_merge` cannot run `SEMI`, so such a query, which runs on master, failed with `NOT_IMPLEMENTED` (the same gap that the variant without the `IN` already has, ClickHouse#118315). Restore the bail-out on not-built sets in the `ANY` pass, so it behaves as on master; the outer-to-inner conversion keeps the new handling. The test now runs that `ANY LEFT JOIN` with `join_algorithm = 'full_sorting_merge'` instead of asserting the `SEMI` plan: it returns `2 4` on master and with this commit, and fails with code 48 without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…orage Without `tmp_path` / `tmp_policy`, the join pickers refuse `grace_hash` with `NOT_IMPLEMENTED` instead of passing it over, so `JoinSettings::canRunGraceHash` must not let the `ANY` to `SEMI`/`ANTI` conversion or the runtime filter cutdown count on it there. Also extract `Strictness: ...` from `EXPLAIN` in the new tests instead of trimming leading spaces: with parallel replicas the plan is nested deeper and the tree-drawing prefix differs. https://s3.amazonaws.com/clickhouse-test-reports/PRs/118315/3865eeee725c5c2ab278d9ed36d737550b33b488/pr/result_stateless_tests_amd_llvm_coverage_parallelreplicas_s3_storage_parallel.json Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Planner/PlannerCorrelatedSubqueries.cpp
|
🕵 Merged master. The only CI red, |
…join` The regular `IN` set ignores the totals row of its subquery, but the `rewrite_in_to_join` rewrite into a correlated `EXISTS` kept the `TotalsHaving` step on the right side of the join built by the decorrelation. That leaked the totals row into the outer query and, when the outer query had `WITH TOTALS` as well, failed with the `LOGICAL_ERROR` `Totals having transform was already added to pipeline`. Found by the AST fuzzer in ClickHouse#118315: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118315&sha=eaa8e0b5913bdc2bf50caccbefee4e1589987921&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is clear and neat. LGTM.
Since ClickHouse#125085, `max_threads = auto` is lowered to the fair share of CPU slots of `ConcurrencyControl` when other queries are running. Under the concurrent load of the test runner the read in order can be planned with a single stream, so both parts are read by one `readInOrder` call and `EXPLAIN PIPELINE` shows `ReverseTransform × 2` instead of two separate chains. Disable `use_concurrency_control_for_max_threads` in the test, as its output depends on the number of streams. Failed in the merge queue of ClickHouse#118315: https://github.com/ClickHouse/ClickHouse/actions/runs/38491344800/job/115617405459 Co-Authored-By: Claude Opus 5.5 <[email protected]>
query_plan_convert_any_join_to_semi_or_anti_jointurns theANYjoin of a decorrelatedEXISTS- or of a user's own query - into aSEMIorANTIjoin whenever the filter above it allows. The sort-merge algorithms implement neither, so underjoin_algorithm = 'full_sorting_merge'the conversion turned a query that runs into one that throws:The pass now converts only into a strictness one of the enabled algorithms can execute.
rewrite_in_to_joinreaches the same conversion through theEXISTSit builds out ofx IN (subquery), so that path is fixed by the same check.The decorrelation join itself had the same problem one step earlier: it is built as
ANY RIGHT, which no sort-merge algorithm executes and whichpartial_mergedoes not do at all beyondINNER/LEFT, so a correlated subquery underjoin_algorithm = 'partial_merge'failed even with the conversion pass off. As the buffered case in the same function already does, the join now gets the hash algorithms when the user-facing list cannot execute it - that list describes the user's joins, not an internal decorrelation detail.A third reported shape needs neither of those settings: an always-false
WHEREover a user-writtenANY LEFT JOINagainst anENGINE = Jointable isFALSEfor the rows the join does not match, so thepass converted the join to
SEMI LEFTand the query failed underjoin_algorithm = 'full_sorting_merge'while the unoptimized plan ran. The same check covers it.
The capability question is answered in one place,
JoinCommon::canBeExecutedByEnabledAlgorithm, so the sort-merge restrictions are not spelled out twice.grace_hashcounts only where the join pickers run it on every node, which is decided byJoinSettings::canRunGraceHash. That means legacy mode, an absolutemax_bytes_before_external_join, orgrace_hashlisted alone. A threshold given only as a ratio does not count, because a server without memory limits resolves it to 0. The runtime filter pass uses the same predicate. Before, it cutjoin_algorithm = 'full_sorting_merge,grace_hash'of a swappedANY RIGHTjoin down tograce_hashalone, and without a threshold that is refused withBAD_ARGUMENTS.Closes: #111075
Closes: #111207
Closes: #111877
Related: #103294
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
NOT_IMPLEMENTED("None of the algorithms enabled by the 'join_algorithm' setting ... can execute this SEMI/ANTI JOIN") for a correlatedEXISTS/NOT EXISTSsubquery, for anIN (subquery)underrewrite_in_to_join, or for anANY LEFT JOINof anENGINE = Jointable under an always-falseWHERE, whenjoin_algorithmis set tofull_sorting_mergeorpartial_merge.Workflow [PR]
Sync PR [sync-upstream/pr/118315]
Version info
26.10.1.2346-master(included in26.10and later)