Repository navigation
Fix the crash of merge, loop and executable reading a materialized view's source table on INSERT - #129338
Conversation
… and `executable` under a materialized view Inside a materialized view query, `tryResolveTableIdentifier` (ClickHouse#123514) types every reference to the view source table by the view source metadata (`StorageValues`, no primary key) while keeping the catalog storage, and relies on `replaceStorageInQueryTree` to swap the view source in after the passes. That swap only happens for an interpreter given the view source as its storage. Three reads re-analyze an AST under a copy of the push context without one: a `Merge` child planned by a nested interpreter (a `Distributed` sibling raises the read above `FetchColumns`), the inner query of `loop`, and the input queries of `executable`. The catalog `MergeTree` was then planned with key-less metadata and `KeyCondition` dereferenced a null primary key expression (SIGSEGV, STID 1288-3bd5). The same holds for the `APPEND` refresh of a refreshable view into the source table. Pass the view source as the interpreter's storage at these three sites, as `interpretSubquery` and `ExecuteScalarSubqueriesVisitor` already do. The nested re-analysis reads the inserted block again, as before ClickHouse#123514 and as the old interpreter did. The context is unchanged, so row-policy subqueries of a `Merge` child keep reading the inserted block (05321), the local shard of a `Distributed` read keeps reading the table (05294) and ordinary view inner queries are unaffected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261104-165700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261104-155800 |
|
cc @PedroTadim, could you review this and add the |
|
Workflow [PR], commit [d7987d7] Summary: ✅
AI ReviewSummaryThe PR fixes null primary-key dereferences when nested Missing context / blind spots
Final VerdictStatus: ✅ Approve for the scoped crash fix. No code changes requested; latest-head CI remains pending. |
|
🕵️ @groeneai The
Children planned at
|
| InterpreterSelectQueryAnalyzer interpreter(select_ast, context, options, column_names); | ||
| /// The analyzer substitutes the inserted block of a materialized view for its source table only when given it. | ||
| InterpreterSelectQueryAnalyzer interpreter( | ||
| select_ast, context, options, context->isViewInnerQuery() ? nullptr : context->getViewSource(), column_names); |
There was a problem hiding this comment.
❌ [P1] Keep these block-dependent reads out of the query-global scalar cache
Passing the view source makes the nested read depend on the current inserted block, but subtreeHasViewSource in evaluateScalarSubqueryIfNeeded.cpp recognizes only direct TableNode references. A scalar over loop, merge, or executable hides that dependency behind the table function, so can_use_global_scalars stays true and subsequent blocks reuse the first block's answer.
Reproduced with this commit's CI binary: a view selecting x AS inserted and (SELECT max(x) FROM (SELECT x FROM loop(db, src) WHERE x > 0 LIMIT 2)) AS seen, followed by INSERT INTO src SELECT number + 1 FROM numbers(3) SETTINGS max_threads=1, max_block_size=1, min_insert_block_size_rows=0, min_insert_block_size_bytes=0, min_insert_block_size_rows_for_materialized_views=0, min_insert_block_size_bytes_for_materialized_views=0, stores (1,1), (2,1), (3,1) instead of (1,1), (2,2), (3,3). The merge and executable scalar subqueries from the added test have the same failure; a direct source-table scalar returns the correct values.
Please propagate the block dependency to the scalar-cache decision, or otherwise prevent these results from being shared across inserted blocks, and add multi-block regression coverage for all three paths.
There was a problem hiding this comment.
Acknowledged. I'll open a follow-up PR that keeps these reads out of the query-wide scalar cache, with multi-block tests for loop, merge and executable, and post its link here.
There was a problem hiding this comment.
The follow-up is #129497. Could you add the can be tested label there so its CI runs?
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+60.05 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units9 translation units recompiled, 36 s compile time in total, 9 of them have a recent master baseline. |
…w source A `Merge` child is planned by a nested interpreter only when a sibling such as `Distributed` raises the stage above `FetchColumns`. Passing the view source to it made the source table read the inserted block there and the table at `FetchColumns`, so the same view read different data depending on which other tables the regexp matched. When the child is the source table itself, clear the view source instead, as `DistributedCreateLocalPlan` does for the local shard: the resolver then types the child by its own metadata, which has the primary key, and the child reads the table, as do subqueries in its own query. Other children keep the view source, so their row-policy and `IN` subqueries keep reading the inserted block, as they do at `FetchColumns`; clearing it for every child made a row policy reading the source table (the 05321 shape with a `Distributed` sibling) read the table instead. `loop` and `executable` keep reading the inserted block. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. Changed in d7987d7:
The description now states this behavior. |
|
🕵️ @groeneai Thanks, d7987d7 addresses my point: the source child of One nit in |
|
Thanks. I measured it and would keep the string comparison here, because the resolver ( Source
The second row shows the collision already exists on master for a direct reference, and the |
Cherry pick #129338 to 26.9: Fix the crash of `merge`, `loop` and `executable` reading a materialized view's source table on INSERT
Cherry pick #129338 to 26.8: Fix the crash of `merge`, `loop` and `executable` reading a materialized view's source table on INSERT
…table` reading a materialized view's source table on INSERT
…table` reading a materialized view's source table on INSERT
Backport #129338 to 26.10: Fix the crash of `merge`, `loop` and `executable` reading a materialized view's source table on INSERT
Backport #129338 to 26.8: Fix the crash of `merge`, `loop` and `executable` reading a materialized view's source table on INSERT
Backport #129338 to 26.9: Fix the crash of `merge`, `loop` and `executable` reading a materialized view's source table on INSERT
…ough `merge` with a `Distributed` table" This reverts commit 9696a8c, which came from #129493. Its rule gave the metadata of the inserted block only to analyses that swap it in, but nested analyses that swap in subqueries got the catalog types: #122603 (comment) The crash is fixed in master by #129338. Co-Authored-By: Claude Opus 5.5 <[email protected]>
After ClickHouse#129338, a `merge` child that is the materialized view's source table and is planned by the nested interpreter (a `Distributed` sibling raises the stage) cleared the view source, so the IN subqueries of the view query pushed into that child read the table, while at `FetchColumns` they read the inserted block. With the analyzer typing the source table by the view source only when given it as storage, passing no storage is enough for the child to read the table, and the view source stays for the subqueries (`view_source_in_subqueries`). `merge(db, '^(src|dist)$') WHERE x NOT IN (SELECT ... FROM src)` now gives the same rows as `merge(db, '^src$')`. The `clearViewSource` in `createChildrenPlans` is dropped: `loop` and `executable` now get the view source as storage, so no analysis that reads a `Merge` table carries the view source without `view_source_in_subqueries`. The test keeps only the shapes that differ from master: the `Buffer` over `Distributed` crash (a table on the right of NOT IN, a JOIN) and the `merge` source child, each also through a refreshable view appending to the source table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…table` reading a materialized view's source table on INSERT
…table` reading a materialized view's source table on INSERT
…utable` reading a materialized view's source table on INSERT
…w source Since ClickHouse#129338, `loop(db, src)` inside a materialized view of `src` reads the inserted block, but `StorageLoop::mayReadViewSource` only forwarded to the inner table, so a scalar subquery over it went to the query-wide scalar cache and every later block got the first block's value. This was the first case of the AI review thread on `ReadFromLoopStep.cpp`; the PR dropped it in 4a1429d when the crash fix was split out, and ClickHouse#129497 later picked it up. Count the inner table being the view source, as ClickHouse#129497 does, and carry its test (without the `loop` table arms: master no longer allows `CREATE TABLE ... AS loop(...)`). The scalar form now equals the CROSS JOIN form, `(1,1),(2,2),(3,3)`, where the head CI binary (5e36832) gave `(1,1),(2,1),(3,1)`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #125023
Related: #123514
Related: #125990
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash on
INSERTinto the source table of a materialized view whose query reads that table again throughmerge(with aDistributedtable among its tables),looporexecutable, including theAPPENDrefresh of a refreshable materialized view into that table. Closes #125023.Description
On v26.10.2.103-stable, v26.9.25.15-stable and v26.8.34.13-lts, this
INSERTsegfaults:executablecrashes the same way; on master, so doesmergewith aDistributedtable among its tables, also on theAPPENDrefresh of a refreshable view.CI: Stress test (arm_debug) on 26.8 (STID 1288-3bd5), Stress test (amd_msan) (STID 2395-4d91).
Root cause. Since #123514,
tryResolveTableIdentifiertypes the source table by the view source's key-less metadata and relies onreplaceStorageInQueryTree, run only by an interpreter given the view source as storage, to swap it in. The nested interpreters ofmerge(with aDistributedsibling),loopandexecutableare not given it, so the catalogMergeTreeis planned without its primary key.Change. The nested interpreters of
loop,executableandmergegetisViewInnerQuery() ? nullptr : getViewSource()as storage, as ininterpretSubquery, soloopandexecutableread the inserted block, as v26.8.18.2 did. Amergechild that is the source table clears the view source instead, like the local shard inDistributedCreateLocalPlan, so it reads the table whatever its siblings are, as atFetchColumns, and so do subqueries in its own query; other children's row-policy andINsubqueries keep reading the block.As on v26.8.18.2, a scalar subquery over
looporexecutablekeeps the first block's value for the wholeINSERT. ABufferover aDistributedtable read withx IN srcstill crashes; #125990, on top of this PR, covers it.Validation. New test, one arm per site, a row policy on another
mergechild, refreshable views appending to and readingsrc: master crashes on it, reverting any one site crashes its arm, 30/30 randomized.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129338]
Version info
26.11.1.1418-master(included in26.11and later)