Skip to content

Fix crash when a materialized view reads its source table through merge with a Distributed table - #129493

Closed
antonio2368 wants to merge 2 commits into
masterfrom
fix-merge-view-source-snapshot-metadata
Closed

antonio2368 wants to merge 2 commits into
masterfrom
fix-merge-view-source-snapshot-metadata

Conversation

@antonio2368

@antonio2368 antonio2368 commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

A materialized view (or the RETURNING query of an INSERT) that reads its source table through merge crashed the server when merge also covered a Distributed table. In that case the merge child that reads the real source table got the metadata of the inserted block, which has no primary key.

Now the table of the merge child query uses the metadata of the real table, because it reads the real parts. All other references to the source table keep the metadata of the inserted block, also in nested analyses that read the block: an IN subquery inside the merge child, or the row policy of an inlined view. This also covers a source table that is altered during the insert.

Related: #122603

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix a server crash when a materialized view or INSERT ... RETURNING reads its source table through merge together with a Distributed table.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/129493]

…rge` with a `Distributed` table

A materialized view (or the `RETURNING` query of an `INSERT`) is analyzed with
the inserted block as the view source. `IdentifierResolver::tryResolveTableIdentifier`
gave every reference to the source table the metadata of the block, which has no
primary key. This is correct only for the references that `replaceStorageInQueryTree`
later replaces with the block. A nested analysis also has the view source in its
context but does not replace the reference: `ReadFromMerge::createPlanForTable`
analyzes the child query again when `merge` also covers a `Distributed` table. That
child read the real parts with key-less metadata, and the server crashed in
`KeyCondition`.

Now the analysis gets the storage that its caller replaces afterwards, and only then
does the resolver apply the metadata of the view source. The row policy filter
analysis, which replaces the view source after its own analysis, passes it too.

Related: #122603

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8f85be3]

Summary: ⏳

job_name test_name status info comment
Fast test FAIL
05508_mv_merge_distributed_sibling_view_source FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_binary) DROPPED

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Nov 5, 2026
antonio2368 added a commit that referenced this pull request Nov 5, 2026
…-snapshot-without-data

The fix (#129493) gives the view source
metadata only to the references that the caller swaps for the block. This PR takes
the snapshot without data only for those references, so nested analyses (such as the
children of `merge` with a `Distributed` sibling) keep reading the real parts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Nov 5, 2026
The previous commit gave the metadata of the inserted block only to the analyses
that got `replaced_storage`. But some nested analyses do not get it and still swap
the block in: an `IN` subquery in the child query of `merge`, and the row policy of
an inlined view (`analyzer_inline_views`). Such a subquery got the types of the
altered source table, but read the block with the old types. The `IN` case gave a
`LOGICAL_ERROR` exception in `arrayExists`, and the policy case removed rows.

Now every reference to the view source outside view inner queries gets the metadata
of the block again, as before the previous commit. The only exception is the root
query of the child query of `merge`, because its table reads the real parts:
`ReadFromMerge::createPlanForTable` marks it with
`SelectQueryOptions::root_query_reads_real_tables`. The mark is a member of
`QueryAnalyzer`, not of the context, so nested analyzers do not get it. This
removes `replaced_storage`.

Related: #129493 (comment)
Related: #122603 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@antonio2368 antonio2368 closed this Nov 5, 2026
antonio2368 added a commit that referenced this pull request Nov 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant