Skip to content

Cherry pick #121010 to 26.8: Fix missing materialized-CTE gate when two CTEs of a set subquery read one shared CTE - #125503

Merged
robot-ch-test-poll merged 3 commits into
backport/26.8/121010from
cherrypick/26.8/121010
Oct 13, 2026
Merged

robot-ch-test-poll merged 3 commits into
backport/26.8/121010from
cherrypick/26.8/121010

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #121010

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 2 commits September 19, 2026 10:00
…d one shared CTE

A set subquery that declares a MATERIALIZED CTE read by two other MATERIALIZED
CTEs of the same subquery lost the materialization gate for the shared CTE, so a
reader was dispatched before its writer finished:

  Reading from materialized CTE 'c' before its materialization completed -
  DelayedPortsProcessor gate is missing in the query plan. (LOGICAL_ERROR)

inlineMaterializedCTEIfNeeded merges duplicate MATERIALIZED CTEs across UNION
branches by name and body hash. In the reported query the two branches' `c` are
identical and collapse to one MaterializedCTE, while the two `d` bodies differ
(x + 1048577 vs x + 200) and stay two, so one inner CTE ends up read by two
distinct outer CTEs.

SelectQueryOptions::subquery() copies force_materialize_cte through, so inside a
set subquery the nested Planner that builds a CTE's own plan also passed the
is_subquery && !force_materialize_cte guard in collectMaterializedCTEs and
planted a DelayedMaterializingCTEsStep for `c` - once inside each `d`'s plan.
Claiming a CTE is a single atomic exchange in makePlansForCTEs, and
resolveMaterializingCTEs runs its recursive optimizePlans pass before the claims
in this tree, so the first nested gate wins. The second nested gate and the
enclosing plan's dominating level-1 gate then both find `c` already claimed and
degenerate to a MaterializingCTEsStep holding zero CTEs, which updatePipeline
returns unchanged, so no DelayedPortsProcessor is inserted at all. The two CTE
plans run as sibling side pipelines of one step, and the ungated MemorySource for
`c` is dispatched while its writer, now buried inside the other CTE's plan, is
still running.

A CTE's plan is attached below its own gate, so it can only dominate that CTE's
readers. Gates for the CTEs its body reads belong to the plan that collected
them, which places an inner CTE at a deeper level and therefore plants a gate
above the outer CTE's. The nested gate was a dominated duplicate, so the fix is
to stop propagating force_materialize_cte into the nested CTE planner, making the
force path behave exactly like the already-correct non-force path (a plain
top-level SELECT passes force = false today, which is why the non-force path
never showed this). Writer-step counts in EXPLAIN are unchanged, confirming only
dominated duplicates were removed and no materialization work moved.

The UNION ALL is not essential. What is required is two multiply-referenced outer
CTEs reading one shared inner CTE, plus more than one reading stream, so the same
defect reproduces with a single-branch diamond and as a plain SELECT as well as a
lightweight UPDATE. The new test covers all four shapes; each failed 20 of 20
runs before this change. It also pins the number of MaterializingCTE writer steps
so a future planner change that inlines the CTEs cannot make the cases vacuous,
with a control asserting the count drops to zero without the MATERIALIZED
keyword, and asserts post-UPDATE data rather than only the absence of a throw.
05182_lightweight_update_materialized_cte_in_subquery:38 is the single-branch
chained case that already passes and is left untouched.

Closes: #120985

Co-Authored-By: Claude Opus 5 <[email protected]>
…ared-by-two-ctes

Fix missing materialized-CTE gate when two CTEs of a set subquery read one shared CTE
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Oct 12, 2026
Resolve the cherry-pick of #121010 to 26.8.

On master, #119489 moved `addBuildSubqueriesForMaterializedCTEsIfNeeded`
from `src/Planner/Planner.cpp` into `src/Planner/CollectMaterializedCTE.cpp`.
That PR is not on 26.8, so the hunk did not apply. On 26.8 the function is
still in `Planner.cpp`, and its body is identical to the one #121010
patched. The resolution adds the same three lines there, before
`Planner cte_planner(...)`, and leaves `CollectMaterializedCTE.cpp` as it
is on 26.8.

#119489 also adds the materialized-CTE gate for lightweight `UPDATE` and
`DELETE` set subqueries, which the two `UPDATE` cases of
`05230_materialized_cte_shared_by_two_ctes_in_set_subquery` depend on.
Without it they still fail on 26.8, so those two cases are removed from
the test here. The `SELECT` cases are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

This cherry-pick conflicts because #119489 is not on 26.8. That PR moved addBuildSubqueriesForMaterializedCTEsIfNeeded from Planner.cpp to CollectMaterializedCTE.cpp. It also adds the materialized-CTE gate for lightweight UPDATE/DELETE set subqueries, which the two UPDATE cases of 05230_materialized_cte_shared_by_two_ctes_in_set_subquery need.

26.8 has both bugs. The official 26.8.26.4, built from c9652db9 (the first parent of backport/26.8/121010), fails both 05230 and #119489's 05182_lightweight_update_materialized_cte_in_subquery (3/3 each) with Reading from materialized CTE 'c' before its materialization completed.

@alexey-milovidov, could you add v26.8-must-backport to #119489? Its cherry-pick to 26.8 merges cleanly. After it, this PR merges cleanly too, so the robot's retry fixes this PR and no manual resolution is needed. I built that end state (26.8 + #119489 + #121010, Debug, clang-22): the unchanged 05230 passes 50/50 (25 sequential, 25 at -j 8) and 05182 passes 3/3. With #119489 alone, 05230 still fails.

If #119489 should not go to 26.8, I pushed a resolution that ports only this fix: branch resolve-26.8-cherrypick-121010 on my fork (dd6953e7730f). Its parents are this PR's head and backport/26.8/121010, so applying it is a fast-forward. It adds the three lines to the copy of the function in Planner.cpp and removes the two UPDATE cases from 05230, because they still fail on 26.8 without #119489. That test passes 50/50 on the resolution and fails 3/3 on 26.8.26.4. Applying it turns off the robot's retry, and a later 26.8 cherry-pick of #119489 would then conflict in Planner.cpp. I have triage but not push here, so it takes:

git fetch https://github.com/groeneai/ClickHouse resolve-26.8-cherrypick-121010
git push origin FETCH_HEAD:cherrypick/26.8/121010

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai. I applied your resolution (dd6953e7730f, a fast-forward on top of this PR's head with backport/26.8/121010 as the second parent). I checked it: the 3-line fix is now in the Planner.cpp copy of addBuildSubqueriesForMaterializedCTEsIfNeeded, and 05230 no longer has the two lightweight UPDATE cases that need #119489. Whether #119489 should go to 26.8 is still a separate decision for a human. If it is backported later, its cherry-pick will conflict in Planner.cpp, and the conflict can be resolved then.

@robot-ch-test-poll
robot-ch-test-poll merged commit 45bc1dc into backport/26.8/121010 Oct 13, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/121010 branch October 13, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants