Repository navigation
Reject correlated MATERIALIZED CTE reused in a JOIN at analysis time - #108883
alexey-milovidov merged 6 commits into
Conversation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260630-020800 |
|
cc @novikd @vdimir could you review this? It closes the last route to the |
|
Workflow [PR], commit [fb7e0c5] Summary: ✅
AI ReviewSummaryThis PR closes a real analyzer gap for Final Verdict
LLVM Coverage ReportMeasured on commit fb7e0c5.
Changed lines: Changed C/C++ lines covered: 21/21 (100.00%) · Uncovered code |
CI finish ledger — 62abf5fEvery failure below has an owner: a fixing PR (ours or external). Only
Session id: cron:our-pr-ci-monitor:20260630-073000 |
CI finish ledger - 62abf5fEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
The analyzer change (reject correlated MATERIALIZED CTE reused in a JOIN) cannot cause a TSan shutdown deadlock; both stress failures are the chronic hung-check family across unrelated PRs and master. Session id: cron:our-pr-ci-monitor:20260630-083000 |
A MATERIALIZED CTE is materialized once and cannot depend on outer-scope columns, so its body cannot be a correlated subquery. The analyzer already rejects this on the IN-subquery path and on the first (storage-initializing) reference in the join tree, but a MATERIALIZED CTE referenced more than once is not inlined: each reference is a separate TableNode whose body subquery is resolved independently. The storage-initializing reference can resolve non-correlated while a later reference resolves correlated (for example `database` binding to the outer scope). The subsequent-reference branch in resolveQueryJoinTreeNode only verified column structure, not correlation, so the correlated clone slipped through. Its PLACEHOLDER nodes (correlated-column markers) are never decorrelated on the materialized-CTE build path and reach ExpressionActions::execute, aborting the server in debug builds with 'Trying to execute PLACEHOLDER action'. Add the correlation check to the subsequent-reference branch so every reference is rejected uniformly, matching the IN-subquery and first-reference paths. The shared check is extracted into a helper. Related: ClickHouse#105501
62abf5f to
3229484
Compare
CI finish ledger — 3229484CI fully finished (Finish Workflow pass). The only red job is the fleet-wide host-OOM window; not PR-caused.
|
CI finish ledger — 9b91125Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The earlier partial fix #111528 (pin Session id: cron:our-pr-ci-monitor:20260729-103000 |
| /// A MATERIALIZED CTE is materialized once into a temporary table, so its body cannot | ||
| /// depend on outer-scope columns: a correlated body would need a different materialization | ||
| /// per call site. A correlated body also leaves PLACEHOLDER nodes (correlated-column | ||
| /// markers) in its ActionsDAG that the materialized-CTE build path never decorrelates, | ||
| /// so they reach `ExpressionActions::execute` and throw `Trying to execute PLACEHOLDER | ||
| /// action`. Reject it at analysis time instead. The check must run for every reference, | ||
| /// because two clones of the same body can resolve differently (one non-correlated, one | ||
| /// correlated) and only the correlated clone must be rejected. |
There was a problem hiding this comment.
Done in b2da7ee - cut to two lines, the rest moved into the commit message.
Per review feedback, keep only the invariant that is not obvious at the call site and move the root-cause narrative here. A MATERIALIZED CTE is materialized once into a temporary table, so its body cannot depend on outer-scope columns: a correlated body would need a different materialization per call site. A correlated body also leaves PLACEHOLDER nodes (correlated-column markers) in its `ActionsDAG` that the materialized-CTE build path never decorrelates, so they reach `ExpressionActions::execute` and throw `Trying to execute PLACEHOLDER action`. The check therefore runs at analysis time, and it must run for every reference: when a MATERIALIZED CTE is referenced more than once it is not inlined, so each reference resolves its own clone of the body and the clones can resolve differently (one non-correlated, one correlated). Only the correlated clone must be rejected. No functional change. Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 20/20 (100.00%) · Uncovered code |
CI finish ledger - b2da7eeEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
All 174 check-runs on this commit are completed and this is the only failing check. Not caused by Session id: cron:our-pr-ci-monitor:20260730-233000 |
|
Dear @novikd, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@groeneai merge fresh master into the branch |
`ExpressionActions::execute` throws `Exception(ErrorCodes::LOGICAL_ERROR, "Trying to execute PLACEHOLDER action")`, and `Exception::Exception` only calls `abortOnFailedAssertion` for a `LOGICAL_ERROR` in a debug or sanitizer build, so the test comment that said a surviving `PLACEHOLDER` "aborts the server" claimed more than the release build does. The in-tree rulebook also asks for literal SQL keywords, class and function names and log-message excerpts in inline code blocks, and for logical errors to be called exceptions rather than crashes; `MATERIALIZED`, `TableNode`, `PLACEHOLDER`, `ExpressionActions::execute` and the message text were bare. Comments only, no functional change. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Merged master ( |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 18 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 36fb80eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
Not caused by this PR: the shard overran its wall-clock budget with every test it ran passing, and Session id: cron:our-pr-ci-monitor:20260909-000033 |
…#118851) A correlated MATERIALIZED CTE that survives on a subsequent reference keeps its PLACEHOLDER nodes, and those have two reachable tripwires, not one. The cases already in this file reach ExpressionActions::execute and throw "Trying to execute PLACEHOLDER action": their CTE bodies do not aggregate, so the plan never enters the aggregation hash-table cache-key pass. Issue ClickHouse#118851 reports the other one: with an aggregating CTE body, query-plan optimization serializes the sub-plan's ActionsDAG to hash it (setAggregationHashTableCacheKeys -> calculateHashFromStep -> ActionsDAG::serialize), and serialize has no case for ActionType::PLACEHOLDER, so it throws "Unknown node type 5" before execution is reached. Measured on a pristine master debug build, the new case aborts with "Logical error: 'Unknown node type 5'" through exactly the stack the issue reports (ActionsDAG.cpp:4614 <- calculateHashTableCacheKeys.cpp:116/354/408 <- optimizeTree.cpp:321), and on this branch it is rejected at analysis time with "Materialized CTE 'rs' cannot be correlated. (UNSUPPORTED_METHOD)". Two shape decisions are measured rather than stylistic. The existing Memory table users_04322 reaches the pass, so no new table is needed: a MergeTree variant produces an identical abort and stack. Dropping the aggregation, or turning off either collect_hash_table_stats_during_aggregation or collect_hash_table_stats_during_joins, moves the query back onto the execution tripwire the file already covers, which is why the body aggregates and the two settings are pinned at query level where randomization cannot override them. The route is not debug-only: on official 26.8.2.7 (LTS) and 26.9.1.1013 the issue's query returns Code: 49 "PLACEHOLDER action node must be removed before query plan optimization", needing one non-default setting, enable_materialized_cte = 1.
|
Recurred on Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=a6fe14f068a998079aea4a1b3e7409004726e042&name_0=MasterCI&name_1=Stress%20test%20%28amd_debug%29 ( The fatal ( SELECT count() FROM t_lwu_materialized_cte
WHERE v IN (WITH c AS MATERIALIZED (SELECT database, number AS x FROM numbers(3)) SELECT a.x FROM c AS a, c AS b)
Reduced to one statement, no table needed, only SET enable_analyzer = 1, enable_materialized_cte = 1;
WITH c AS MATERIALIZED (SELECT database, number AS x FROM numbers(3)) SELECT count() FROM c AS a, c AS b;
-- Code: 49. DB::Exception: Trying to execute PLACEHOLDER action:
-- while executing 'PLACEHOLDER __table1.database -> __table1.database String : 1'. (LOGICAL_ERROR)
Verified against this PR's own
The PR is green (156 pass / 19 skipped) and approved since 2026-07-30. Worth merging. |
|
@groeneai @novikd @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…ransformer-alias-on-asterisk One conflicted file, src/Analyzer/Resolve/QueryAnalyzer.cpp, resolved as a union of two independent additions at the same two insertion points. Anonymous namespace: master's checkMaterializedCTESubqueryIsNotCorrelated (ClickHouse#108883) and this branch's isUntupleExpansion / getTupleElementName helpers. Kept both, master's first. resolveMatcher LAMBDA branch: master's node_to_projection_name.emplace seed (ClickHouse#118825) and this branch's BAD_ARGUMENTS guard for a prefixed APPLY whose lambda body is a bare matcher. Kept both, the guard first so the rejected query does not register a projection name on its way out. The seed publishes the pre-transformer name of the matched column, which is what a later bare matcher must still see; the prefixed name is published on this chain's private clone further down, so the two writes do not compete. Verified the branch delta to that file is unchanged by the resolution: the 174 changed lines of git diff <merge-base>..HEAD are set-identical to git diff origin/master..<resolved>. Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error when a correlated
MATERIALIZEDCTE is referenced more than once inside aJOIN. Such a CTE is now rejected at analysis time withUNSUPPORTED_METHOD, consistently with the single-reference andIN-subquery paths.Description
Related: #105501
Closes: #118851
A
MATERIALIZEDCTE is materialized once and cannot depend on outer-scope columns, so its body cannot be a correlated subquery. The analyzer already rejects this on theIN-subquery path and on the first (storage-initializing) reference in the join tree.When such a CTE is referenced more than once it is not inlined, so each reference is a separate
TableNoderesolved independently, and the storage-initializing one can resolve non-correlated while a later one resolves correlated. The subsequent-reference branch inresolveQueryJoinTreeNodeonly verified column structure (verifyMaterializedCTESubqueryMatchesStorage), not correlation, so the correlated clone slipped through and kept itsPLACEHOLDERnodes (correlated-column markers), which the materialized-CTE build path never decorrelates.Two routes then hit a logical error, and rejecting the clone closes both. #105501 reaches
ExpressionActions::execute(Trying to execute PLACEHOLDER action). #118851 fails earlier when the CTE body aggregates: query-plan optimization hashes the sub-plan'sActionsDAGfor an aggregation cache key (setAggregationHashTableCacheKeys->calculateHashFromStep->ActionsDAG::serialize), which has no case forPLACEHOLDERand throwsUnknown node type 5.Not debug-only: on
26.8.2.7(LTS) and26.9.1.1013the #118851 query returnsCode: 49 ... PLACEHOLDER action node must be removed before query plan optimization, with one non-default setting,enable_materialized_cte = 1.Reproducer (STID
1611-343c):The fix runs the correlation check on the subsequent-reference branch too, so every reference is rejected uniformly; the shared check is extracted into a helper. A non-correlated materialized CTE reused in a
JOINkeeps working.Found by
Stress test (arm_debug):https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107826&sha=3f6a79763dc52cd4004e8c1c177b67e1a428e7fd&name_0=PR&name_1=Stress%20test%20%28arm_debug%29
Version info
26.9.1.1296(included in26.9and later)