Skip to content

Fix Cannot fold actions for projection when the join order optimizer reorders an outer join converted to INNER - #127416

Merged
nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-join-reorder-defer-inner-type-change
Nov 2, 2026
Merged

nickitat merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-join-reorder-defer-inner-type-change

Conversation

@groeneai

@groeneai groeneai commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #127331

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the Cannot fold actions for projection logical error for queries with join_use_nulls = 1 in which a LEFT or RIGHT JOIN is turned into an inner join by a WHERE condition and then reordered by the join order optimizer, for example a JOIN b ON ... LEFT JOIN c ON b.x = c.x ... WHERE c.v = 'x'. Closes #127331.

Description

On 26.8.28.14 (also 26.6, 26.7 and 26.9) this valid query is rejected with Code: 49. ... Cannot fold actions for projection. Node equals(__table2.x, __table3.x) requires input __table3.x which does not belong to projection. (LOGICAL_ERROR), with only join_use_nulls = 1 set:

SELECT a.v, b.x, c.x, c.v
FROM a JOIN b ON a.k = b.k AND a.t = b.t LEFT JOIN c ON b.x = c.x AND b.t = c.t
WHERE c.v = 'x'
SETTINGS join_use_nulls = 1;

On master it still fails with default settings for some row estimates, and with other join orders.

WHERE c.v = 'x' rejects NULLs, so the LEFT JOIN becomes INNER, but its toNullable(c.*) actions stay because the result columns must remain Nullable. The join order optimizer applied that type change at the step that joins c alone and dropped the original c columns. After the conversion nothing keeps c next to b, so it can be joined with a first (through a.t = b.t = c.t), and the later step that evaluates b.x = c.x no longer finds the original c.x.

Now the type change is applied at the step that joins the relation alone only when an outer join pads that relation with NULLs. Any other type change is applied at the last join step. The chosen join order does not change.

The new test covers the greedy, DPsize, DPhyp and DPsub algorithms (with and without conflict detectors), the RIGHT JOIN form, and a predicate synthesized from the column equivalences. Every query in it fails on master. Sweeping 5 query shapes x 6 algorithms x 201 random seeds gives no errors with the fix, the same results as without the conversion, and the same join trees as before for every order that already worked.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1120-master (included in 26.11 and later)

groeneai and others added 2 commits October 22, 2026 12:48
With `join_use_nulls = 1`, `tryConvertOuterJoinToInnerJoin` turns an outer
join into INNER but keeps its `toNullable` actions, because the join output
must stay Nullable. `chooseJoinOrder` applied those type changes at the step
that joins the relation alone and from then on replaced the relation's
original inputs. After the conversion nothing pins the relation to its ON
partners anymore, so the optimizer may join it first (through a transitive
equality or another ON edge); a later step that reads an original column of
the relation (an ON predicate or a predicate synthesized from the column
equivalences) then failed with `Cannot fold actions for projection`.

An INNER join pads nothing, so its type change is value-preserving and is not
tied to any step. Apply a type change at the step that joins the relation
alone only when an outer join NULL-pads that relation (it has an outer
`join_kinds` entry); apply every other type change at the last step, when
all predicates over the original columns have been evaluated. The chosen join
order does not change, only where the columns become Nullable.

Closes ClickHouse#127331

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge the two-line type-change comment in chooseJoinOrder into one invariant line.

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; 1 finding; bounded: 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test checks result rows only, so it would pass if join reordering were skipped (05443_join_reorder_converted_outer_join_type_change.sql) DISAGREE With the fix reverted, all 8 queries fail with Cannot fold actions for projection on master (8/8, repeated 3x; the randomized runner fails 6/6). That error only occurs when c is joined before its ON partner, so every query exercises the bad order. Equal rows with reordering off is the intended oracle. An EXPLAIN pin on the join order would go red on unrelated cost-model changes.

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

Session id: cron:clickhouse-review-slot-8:20261022-133600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: the query in the description with join_use_nulls = 1 fails on 26.8.28.14 and 26.9.19.9; each of the 8 test queries fails 3/3 on a master debug build.
b Root cause explained? Yes: after the outer join is converted to INNER, its toNullable type change was still applied at the step that joins the relation alone, and a later step read the replaced original column.
c Fix matches root cause? Yes: the type change is applied where it is valid (the padding step for an outer join, the last step otherwise); no guard, no setting, join order unchanged.
d Test intent preserved / new tests added? Yes: one SQL test, 8 queries, every one fails on master.
e Both directions demonstrated? Yes: master fails every query and in clickhouse-test; with the fix 50/50 randomized runs pass.
f Fix is general across code paths? Yes: one site for all join order algorithms (greedy, DPsize, DPhyp, DPsub with and without conflict detectors), covering both ON predicates and synthesized equalities.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes: the type change is toNullable/CAST only and value-preserving for any type; LEFT and RIGHT forms, 5 query shapes x 6 algorithms x 201 seeds.
h Backward compatible? (maintainer-approved exception only) Yes: no setting or format change; only where toNullable is computed moves for orders that join the converted side early.
i Invariants and contracts preserved? Yes: outer-join padding unchanged; the last step outputs the same Nullable columns as before; original inputs stay available until the last step.

Session id: cron:clickhouse-impl-slot-4:20261022-114200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @davenger, could you review this and add the can be tested label so CI runs? With join_use_nulls = 1, an outer join converted to INNER keeps its toNullable actions, and chooseJoinOrder applied them at the first step joining that relation, so a later ON predicate lost the original column; now only NULL-padded relations change type there and every other type change is applied at the last step, with the join order unchanged.

@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5d12b3c]

Summary: ✅


AI Review

Summary

PR #127416 defers the retained toNullable actions of outer joins converted to INNER until the final reordered join, keeping original columns available for join predicates while preserving Nullable result types. No actionable findings at commit 5d12b3cf8c34c9cc9244f01bc293059bd7d97945. The new regression test passed in Fast test.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 22, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 22, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - d9d5870

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Bugfix validation (functional tests, amd64) / Start ClickHouse Server infra, not from this PR: the job runs the master-HEAD binary with the test configs of this run's merge commit, which is older than master #125817, and that binary refuses those cache configs at startup (Code: 36, max_file_segment_size must not exceed the size of each SLRU queue) on all 3 attempts, so the regression test never ran #127524 (mine, open)
Bugfix validation (functional tests, aarch64) / Start ClickHouse Server same server start failure #127524 (mine, open)
Finish Workflow / Post Hooks new_tests_check.py reports the two Bugfix validation (functional tests, *) failures above #127524 (mine, open), the owner of those jobs
Mergeable Check, PR praktika rollups of the rows above #127524 (the owner of the rows above)

Fast test, Style check and all five Stateless tests (*, flaky check) jobs passed. The Config Workflow / Pre Hooks failures on this head are the earlier attempts stopped at the can be tested label; the current attempt passed. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261023-010008

@nickitat nickitat self-assigned this Nov 2, 2026
@nickitat
nickitat enabled auto-merge November 2, 2026 14:31
@nickitat
nickitat added this pull request to the merge queue Nov 2, 2026
Merged via the queue into ClickHouse:master with commit 20ce5b5 Nov 2, 2026
351 of 353 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 2, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Nov 5, 2026
Cherry pick #127416 to 26.10: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
robot-clickhouse added a commit that referenced this pull request Nov 5, 2026
…hen the join order optimizer reorders an outer join converted to INNER
robot-clickhouse added a commit that referenced this pull request Nov 5, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 5, 2026
Backport #127416 to 26.10: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
@nickitat nickitat removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 11, 2026
@nickitat nickitat added pr-must-backport Pull request should be backported intentionally. Use this label with great care! and removed pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 11, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 11, 2026
robot-ch-test-poll added a commit that referenced this pull request Nov 11, 2026
Cherry pick #127416 to 26.3: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
robot-clickhouse added a commit that referenced this pull request Nov 11, 2026
…en the join order optimizer reorders an outer join converted to INNER
robot-ch-test-poll added a commit that referenced this pull request Nov 11, 2026
Cherry pick #127416 to 26.8: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
robot-clickhouse added a commit that referenced this pull request Nov 11, 2026
…en the join order optimizer reorders an outer join converted to INNER
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 11, 2026
Cherry pick #127416 to 26.9: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
robot-clickhouse added a commit that referenced this pull request Nov 11, 2026
…en the join order optimizer reorders an outer join converted to INNER
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #127416 to 26.9: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
clickhouse-gh Bot added a commit that referenced this pull request Nov 12, 2026
Backport #127416 to 26.8: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
nickitat added a commit that referenced this pull request Nov 12, 2026
Backport #127416 to 26.3: Fix `Cannot fold actions for projection` when the join order optimizer reorders an outer join converted to INNER
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…en the join order optimizer reorders an outer join converted to INNER
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…en the join order optimizer reorders an outer join converted to INNER
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…en the join order optimizer reorders an outer join converted to INNER
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…hen the join order optimizer reorders an outer join converted to INNER
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-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

Logical error "Cannot fold actions for projection" with join_use_nulls, outer-to-inner JOIN conversion and join reordering

9 participants