Repository navigation
Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression - #124740
Conversation
Since ClickHouse#123514, `tryResolveTableIdentifier` resolves every reference to the source table of a materialized view, outside view inner queries, to the catalog storage with the view source metadata (columns only, no primary key), relying on `replaceStorageInQueryTree` to swap the view source in after the passes. That swap only walked the join trees, so a reference in a correlated subquery, as the right side of `IN` (`k IN src`), or in a non-leftmost table of an `IN` subquery was planned as a `MergeTree` read with that metadata, and `KeyCondition` dereferenced the missing primary key expression: an INSERT into the source table crashed the server. When the storage being swapped in is the context's view source, walk the whole query tree, including subqueries in expressions, still stopping at view inner queries and table function arguments. These references read the pushed block again, as with ClickHouse#122345 and the old analyzer. Other callers (Buffer, header computation) keep the join-tree walk. Closes ClickHouse#124647 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; 0 findings), plus my own review before it (1 nit on the code, 1 on the PR text, applied).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-49:20261008-203200 |
|
Workflow [PR], commit [14a80a5] Summary: ✅
AI ReviewSummaryThis PR broadens PR Metadata
|
Backport #124740 to 26.9: Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression
Backport #124740 to 26.8: Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression
…rialized view reads its source table from a subquery in an expression
Closes: #124647
Related: #123514
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash on
INSERTinto aMergeTreetable when a materialized view over it reads the same table from a correlated subquery,IN <table>or a non-leftmost table of anINsubquery.Description
On master,
INSERT INTO srcsegfaults inKeyCondition::KeyCondition(fromReadFromMergeTree::buildIndexes) when a materialized view over theMergeTreetablesrcreadssrcagain from one of these positions:It is not in a release yet: the #123514 backports to 26.9 and 26.8 are merged, but the latest tags (
v26.9.13.15-stable,v26.8.21.10-lts) precede them, so this fix needs to follow them there.Root cause. Since #123514,
tryResolveTableIdentifiergives every reference to the view source table outside view inner queries the catalog storage with the view source metadata (columns only, no primary key), and relies onreplaceStorageInQueryTreeto swap the view source in after the passes. That swap walked only join trees, so references inside expressions were planned as aMergeTreeread without a key expression.Change. When the storage being swapped in is the context's view source,
replaceStorageInQueryTreewalks the whole query tree, still stopping at view inner queries and table function arguments. These references read the pushed block again, as before #123514 (#122345, released 26.x) and as05292_mv_source_substitution_scopedocuments. On master aNullsource in these positions read the empty table instead of the block. Other callers (StorageBuffer, header computation) keep the join-tree walk.Validation. New test: five views over a pre-populated source, then one
INSERT. EachMergeTreearm alone crashes on master; all pass with the fix, 50/50 randomized. 05241, 05291-05294 and the other materialized view stateless tests give the same results as master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124740]
Version info
26.10.1.1976-master(included in26.10and later)26.9.16.12,26.8.24.10