Skip to content

Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression - #124740

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-mv-source-in-expression-subqueries-124647
Oct 9, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-mv-source-in-expression-subqueries-124647

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124647
Related: #123514

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed a crash on INSERT into a MergeTree table when a materialized view over it reads the same table from a correlated subquery, IN <table> or a non-leftmost table of an IN subquery.

Description

On master, INSERT INTO src segfaults in KeyCondition::KeyCondition (from ReadFromMergeTree::buildIndexes) when a materialized view over the MergeTree table src reads src again from one of these positions:

CREATE MATERIALIZED VIEW mv TO dst AS SELECT k, toUInt64(k IN src) AS c FROM src;
INSERT INTO src VALUES (1); -- SIGSEGV

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, tryResolveTableIdentifier gives 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 on replaceStorageInQueryTree to swap the view source in after the passes. That swap walked only join trees, so references inside expressions were planned as a MergeTree read without a key expression.

Change. When the storage being swapped in is the context's view source, replaceStorageInQueryTree walks 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 as 05292_mv_source_substitution_scope documents. On master a Null source 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. Each MergeTree arm 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

  • Merged into: 26.10.1.1976-master (included in 26.10 and later)
  • Backported to: 26.9.16.12, 26.8.24.10

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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 8, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 💡 The second caller of the new walk, evaluateScalarSubqueryIfNeeded (a non-correlated scalar subquery that holds IN src), has no arm of its own in the test (evaluateScalarSubqueryIfNeeded.cpp:222) AGREE, noted, not blocking Same function and the same view-source key as the tested caller; with the new walk disabled the test fails again.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-49:20261008-203200

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [14a80a5]

Summary: ✅


AI Review

Summary

This PR broadens replaceStorageInQueryTree from a join-tree-only substitution to a full query-tree walk for materialized-view source replacement, which is the right shape to fix the INSERT-time segfault when a materialized view re-reads its source table from scalar / EXISTS / IN subqueries. I did not find a remaining code-level correctness issue in the patch, but the PR metadata under-classifies a user-visible crash fix.

PR Metadata
  • 💡 Changelog category is incorrect for this change. A fix for a user-visible INSERT crash should use Critical Bug Fix, not Not for changelog.
  • 💡 Exact replacement:
    • Changelog category: Critical Bug Fix (crash, data loss, RBAC)
    • Changelog entry: Fixes a crash on \INSERT` into `MergeTree` tables when a materialized view reads the source table from a correlated subquery, `IN `, or a non-leftmost table of an `IN` subquery.`
      Missing context / blind spots
      • ⚠️ Praktika PR and ARM build jobs were still pending during this review on 2026-10-08, so a fully green CI run would still be useful to close the remaining build/runtime coverage gap.
      Final Verdict

      ⚠️ No new inline code findings from me; the implementation looks ready pending CI, but I would fix the changelog metadata before merge.

      LLVM Coverage Report

      Measured on commit 14a80a5.

      Metric Baseline Current Δ
      Lines 88.30% 88.30% +0.00%
      Functions 92.10% 92.10% +0.00%
      Branches 79.70% 79.70% +0.00%

      Changed lines: Changed C/C++ lines covered: 24/26 (92.31%) · Uncovered code

      Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. label Oct 8, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 9, 2026
Merged via the queue into ClickHouse:master with commit 1f0a238 Oct 9, 2026
152 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 9, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 11, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #124740 to 26.9: Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #124740 to 26.8: Fix a crash on INSERT when a materialized view reads its source table from a subquery in an expression
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 12, 2026
…rialized view reads its source table from a subquery in an expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIGSEGV on INSERT: un-swapped MV source keeps block metadata over MergeTree

6 participants