Repository navigation
Cherry pick #121010 to 26.8: Fix missing materialized-CTE gate when two CTEs of a set subquery read one shared CTE - #125503
Conversation
…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
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]>
|
This cherry-pick conflicts because #119489 is not on 26.8. That PR moved 26.8 has both bugs. The official 26.8.26.4, built from @alexey-milovidov, could you add If #119489 should not go to 26.8, I pushed a resolution that ports only this fix: branch |
|
🕵 Thanks @groeneai. I applied your resolution ( |
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job