Repository navigation
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]>
…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]>
…s the source through `loop`, `merge` or `executable` Under a materialized view each inserted block is the view source. A scalar subquery is kept out of the query-wide scalar cache only when `subtreeHasViewSource` finds a `TableNode` of the source table in it. The nested queries planned by `loop`, by the input queries of `executable` and by `Merge` children are given the view source, so they read the inserted block, but none of them shows the source as a `TableNode`: `loop(db, src)` is a table function node, `executable`'s input queries are not analyzed, and `merge`'s children are not in the tree. The first block's value was then stored in the query context and reused for every later block of the same INSERT. Add `IStorage::mayReadViewSource`, consulted by `subtreeHasViewSource` for the storage of every table and table function node of the scalar. `Loop` answers true when its inner table is the view source or may read it, `Executable` when it has input queries, `Merge` when a child may read it, and the `Proxy` and `Alias` wrappers forward to the table they read. Only scalars under a view source that read through these engines lose the query-wide cache. Stacked on ClickHouse#129338, which gives these nested reads the inserted block. 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; 5 findings over two passes on the same head).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261105-132400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261105-113500 |
|
cc @yakov-olkhovskiy, could you review this and add the |
|
Workflow [PR], commit [b05c7e8] Summary: ✅
AI ReviewSummaryReviewed head Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
…f publishing it in the query context A scalar subquery that reads the view source is evaluated per inserted block, but an `Array`, `Tuple` (and the other `useless_literal_types`) result was still published under the subquery hash in the INSERT-wide query context and bound through `__getScalar`, which reads that shared entry. With parallel view branches (`parallel_view_processing = 1`, `max_insert_threads > 1`) one block can overwrite the entry before another block binds it, so the row stores another block's value. Measured with 10000 one-row blocks, 8 insert threads, a `Null` source and a `Memory` target: `(SELECT groupArray(x) FROM (SELECT x FROM loop(db, src) LIMIT 1))` 477 wrong rows and a two-column (`Tuple`) scalar 429 on the previous head; a scalar reading the source directly (`(SELECT groupArray(x) FROM db.src)`) 466 on master, 584 on v26.8.34.13 and 539 on v26.3.60.2; 0 for all with `enable_scalar_subquery_optimization = 0` and 0 with this change. Such a scalar is now bound as a `ConstantNode`, as it already is when the optimization is disabled. The same change is part of ClickHouse#125610. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units724 translation units recompiled, 5879 s compile time in total, 724 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
The flaky check runs nproc-1 copies of a new test at once with the thread fuzzer on, and there the 1000-block INSERT made every run take 215-279 s against the 180 s limit. It was 97% of the test's server CPU (6.2 of 6.4 CPU-s per run, debug build). With 200 blocks the binary without the Array/Tuple folding still fails the test in 60 of 60 runs (10 of 10 through clickhouse-test with randomized settings), and the per-run cost under matched concurrency (16 CPUs, 15 workers, 30 runs) drops from a 7.45 s to a 2.64 s median. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…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]>
Related: #129338
Related: #125610
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a materialized view storing the first inserted block's value of a scalar subquery for every block of an
INSERTwhen the subquery reads the view's source table throughlooporexecutable, directly or through aMerge,Aliasor table function table, and anArrayorTuplescalar subquery over the source table storing another block's value withparallel_view_processing.Description
Stacked on #129338; follow-up of this review finding.
On v26.8.18.2-lts and v26.3.42.3-lts a materialized view with
(SELECT max(x) FROM (SELECT x FROM loop(db, src) LIMIT 10)) AS seenstores(1,1),(2,1),(3,1)for three one-row blocks inserted intosrcinstead of(1,1),(2,2),(3,3).executablewith an input query oversrcbehaves the same, and on v26.8.18.2-lts so domerge,AliasandCREATE TABLE ... AS loop(...)tables. Withparallel_view_processing = 1, anArrayorTuplescalar oversrc(direct or throughloop) stores another block's value for about 5% of the blocks (v26.8.34.13, v26.3.60.2, master).Root cause. Under a view source, a scalar subquery skips the query-wide scalar cache only if
subtreeHasViewSourcefinds aTableNodeof the source. The nested queries ofloop,executableinput queries andMergechildren read the inserted block without one, so the first block's value is kept. AnArrayorTuplescalar is bound through__getScalar, which reads a query context entry shared by all blocks.Change. New
IStorage::mayReadViewSource, consulted bysubtreeHasViewSourcefor each table and table function node of the scalar:Loopwhen its inner table is the view source or may read it,Executablewhen it has input queries,Mergewhen a child may;StorageProxyandAliasforward. Only such scalars lose the query-wide cache; they are bound as constants. Price: anexecutablescalar with an input query runs once per block even if its input query does not read the source, and amergescalar enumerates its children once per block. A row policy reading the source hides the same read; that case is #125610.Validation. New test (multi-block
INSERT,APPENDrefreshable view intosrc, refreshable view readingsrc, all six shapes; parallelINSERTwithArrayandTuplescalars): fails on #129338's head; reverting any override or the constant binding reddens its arms; 50/50 randomized.Workflow [PR]
Sync PR [sync-upstream/pr/129497]