Skip to content

Fix missing materialized-CTE gate in lightweight UPDATE/DELETE set subqueries - #119489

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-lightweight-update-materialized-cte-gate
Sep 12, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-lightweight-update-materialized-cte-gate

Conversation

@novikd

@novikd novikd commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Closes: #119451
Related: #98884
Related: #105041
Related: #118853
Related: #118983

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 LOGICAL_ERROR Reading from materialized CTE '...' before its materialization completed - DelayedPortsProcessor gate is missing in the query plan raised by a lightweight UPDATE, or a lightweight DELETE in lightweight_update_force mode, whose predicate contains an IN subquery that defines a materialized CTE, when the set is built at run time.

Description

A lightweight UPDATE plans its IN subqueries in MutationsInterpreter::buildSubqueryPlansForSetsAndAdd, mirroring the Planner's addBuildSubqueriesForSetsStepIfNeeded, but the mutation plan never received the outer DelayedMaterializingCTEsStep that addBuildSubqueriesForMaterializedCTEsIfNeeded adds for a SELECT. DelayedCreatingSetsStep::makePlansForSets strips the safety-net step from every set plan built at run time and relies on that outer step as the single writer site, so the MemorySource readers of the CTE ran ungated and hit the fail-fast check in ReadFromMemoryStorageStep. The set built in place during primary-key analysis (populated table, IN on a key column, use_index_for_in_with_subqueries = 1) keeps its safety net, which is why only the run-time set path failed.

The helper is moved out of the anonymous namespace of Planner.cpp into CollectMaterializedCTE.cpp and called for the CTEs referenced by the set subqueries of each mutation stage. No optimizer or pipeline changes.

For mutations without materialized CTEs the change is a no-op: collectMaterializedCTEs returns nothing and addBuildSubqueriesForMaterializedCTEsIfNeeded returns before touching the plan, so no step is added.

Found by the AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118853&sha=b20ba6c83530627b14d5f12aa0a1089d7275b2a4&name_0=PR&name_1=AST%20fuzzer%20(amd_debug%2C%20targeted)

Note on #118853: with force_materialized_cte = 1 (its default) a materialized CTE in a lightweight UPDATE is rejected before planning, which also covers this shape. Whichever of the two merges second must add SET force_materialized_cte = 0 to the new test; the setting does not exist on master yet, so this PR cannot set it. ALTER TABLE ... UPDATE cannot reach this path until #118983 is fixed.

🤖 Generated with Claude Code


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

novikd and others added 5 commits September 11, 2026 16:33
… an `IN` subquery

The six run-time set variants (empty table, non-key column, index analysis for
`IN` disabled, two `IN` subqueries, a CTE depending on another CTE, lightweight
`DELETE`) fail with `LOGICAL_ERROR` before the fix; the in-place variant and
the `SELECT` analogue pass.

Related: #119451

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ner`

Move the helper out of the anonymous namespace of `Planner.cpp` into
`CollectMaterializedCTE.cpp`, next to `collectMaterializedCTEs`, so that plans
assembled outside the `Planner` can plant the materialized-CTE gate too.
No behaviour change.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…plan

`MutationsInterpreter::buildSubqueryPlansForSetsAndAdd` planned the `IN`
subqueries of a mutation with `forceMaterializeCTE` but never added the outer
`DelayedMaterializingCTEsStep` that the `Planner` adds through
`addBuildSubqueriesForMaterializedCTEsIfNeeded` for a `SELECT`. Since
`DelayedCreatingSetsStep::makePlansForSets` strips the safety-net step from
every set plan built at run time, the `MemorySource` readers of the CTE ran
ungated and hit the fail-fast check in `ReadFromMemoryStorageStep` with
`LOGICAL_ERROR`. Collect the CTEs referenced by each set subquery and plant
the step after the sets step, as the `Planner` does.

Closes: #119451
Fuzzer report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118853&sha=b20ba6c83530627b14d5f12aa0a1089d7275b2a4&name_0=PR&name_1=AST%20fuzzer%20(amd_debug%2C%20targeted)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`Processors/QueryPlan/MaterializingCTEStep.h` was needed only by
`addBuildSubqueriesForMaterializedCTEsIfNeeded`, which now lives in
`CollectMaterializedCTE.cpp`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6585b66]

Summary: ✅


AI Review

Summary

This PR fixes the missing outer DelayedMaterializingCTEsStep on the analyzer-based mutation path by reusing the existing planner-side materialized-CTE wiring for set subqueries. I reviewed the current diff, the surrounding DelayedCreatingSetsStep / DelayedMaterializingCTEsStep flow, the linked issue, and the full existing PR discussion, and I did not find a remaining correctness, concurrency, or coverage gap in the current code. Praktika is all green on commit 6585b66c51e9b59067f81b8c1d4a16d6daeb5710.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit 6585b66.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 51/53 (96.23%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Sep 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6585b66c5 with master d9e548372 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

5 object files changed (+45.87 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Planner/CollectMaterializedCTE.cpp.o 42.64 KiB 88.89 KiB +46.25 KiB (+108.46%)

716 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

9 translation units recompiled, 36 s compile time in total, 9 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Sep 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into master with commit 834f9cb Sep 12, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-lightweight-update-materialized-cte-gate branch September 12, 2026 09:56
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
novikd added a commit that referenced this pull request Sep 14, 2026
`05182_lightweight_update_materialized_cte_in_subquery` (from #119489) exercises the
mutation plan for a materialized CTE inside a lightweight `UPDATE` / `DELETE` predicate.
With the default `force_materialized_cte = 1` that statement is rejected before
planning, so the test sets the guard off explicitly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 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]>
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). pr-bugfix Pull request with bugfix, not backported by default 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.6-must-backport

Projects

None yet

4 participants