Skip to content

Fix mv source engine checks - #123514

Merged
fm4v merged 3 commits into
ClickHouse:masterfrom
Ergus:fix-mv-source-engine-checks
Oct 8, 2026
Merged

fm4v merged 3 commits into
ClickHouse:masterfrom
Ergus:fix-mv-source-engine-checks

Conversation

@Ergus

@Ergus Ergus commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Closes: #123270
Caused by: #122345

#122345 resolved the source table of a materialized view directly to the StorageValues holding the pushed block, so that the view query is typed by the block. That node also served every engine check of the analysis: FINAL/SAMPLE were validated against StorageValues, which supports neither, and _shard_num over a Distributed source was no longer rewritten.

The two roles are now separate: the catalog table stays the storage of the TableNode, and only the metadata (column types) is taken from the view source. The existing post-pass replaceStorageInQueryTree still swaps the view source in for execution, as before #122345.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix INSERT failing with ILLEGAL_FINAL or SAMPLING_NOT_SUPPORTED when a materialized view applies FINAL or SAMPLE to its own source table inside a subquery. Regression in #122345.


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

Version info

  • Merged into: 26.10.1.1832-master (included in 26.10 and later)
  • Backported to: 26.9.13.18, 26.8.21.12

@Ergus Ergus added pr-must-backport Pull request should be backported intentionally. Use this label with great care! v26.8-must-backport v26.9-must-backport labels Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dbabf63]

Summary: ✅


AI Review

Summary

This PR separates materialized-view source typing from engine-aware source-table checks by keeping the catalog storage in tryResolveTableIdentifier while borrowing column types from view_source. That fixes the direct FINAL/SAMPLE regression covered by the new stateless test, but one analyzer path still swaps the source table back to StorageValues too early, so the _shard_num part of the promised contract is still broken for IN (subquery).

Findings
  • ❌ Blockers
    • [src/Analyzer/Resolve/resolveFunction.cpp:2436] The PR description promises that engine-sensitive analysis sees the real source table again, including _shard_num rewriting for Distributed sources. But the IN (subquery) rewrite still calls updateStorage(storage, scope.context) on the matching TableNode, so the right-hand side is converted back to view_source during resolve. ShardNumColumnToFunctionPass runs later and only rewrites _shard_num for remote storages, so a materialized-view self-reference such as ... IN (SELECT _shard_num FROM dist_src) still misses the rewrite and keeps the regression this PR says it fixes.
      Suggested fix: make this path mirror tryResolveTableIdentifier by preserving the catalog storage and transplanting only the view-source metadata snapshot until replaceStorageInQueryTree, then add a focused regression test for _shard_num inside IN (subquery) over a Distributed source.
Final Verdict
  • Status: ❌ Block
  • Minimum required action: keep the catalog storage through the IN (subquery) view-source rewrite as well, and cover _shard_num there with a regression test.

LLVM Coverage Report

Measured on commit dbabf63.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.60% 79.50% -0.10%

Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 2, 2026
if (auto view_source = context->getViewSource();
view_source && !context->isViewInnerQuery()
&& view_source->getStorageID().getFullNameNotQuoted() == storage_id.getFullNameNotQuoted())
metadata_snapshot = view_source->getInMemoryMetadataPtr(context, false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tryResolveTableIdentifier now keeps the catalog storage here, but the IN (subquery) rewrite still swaps matching source-table references back to view_source during the resolve pass in src/Analyzer/Resolve/resolveFunction.cpp:2434. ShardNumColumnToFunctionPass runs after resolve, so a materialized-view self-reference like ... IN (SELECT _shard_num FROM dist_src) is still seen as StorageValues there and _shard_num will not be rewritten for a Distributed source. If the intent is that analyzer passes see the real table again, this path needs the same treatment as the one in this patch: keep the catalog storage until replaceStorageInQueryTree, or transplant only the metadata snapshot instead of calling updateStorage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The IN rewrite with updateStorage(view_source) predates #122345 and is untouched by both PRs, so _shard_num inside IN (SELECT … FROM dist_src) behaves exactly as before; it is not part of #123270.

Types are consistent there (the subquery resolves with the block metadata before the storage is replaced by the same StorageValues). Deferring that swap to replaceStorageInQueryTree is not a drop-in change, because the post-pass swap does not visit IN subqueries; it would turn IN into a read of the whole table.

If _shard_num in IN over a Distributed MV source matters, it should be a separate issue.

@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dbabf63f7 with master f45b61d3c (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline.

Job report

@Ergus
Ergus force-pushed the fix-mv-source-engine-checks branch from 78f684e to 82eabe3 Compare October 2, 2026 17:25
@Ergus
Ergus force-pushed the fix-mv-source-engine-checks branch from 82eabe3 to dbabf63 Compare October 2, 2026 19:33
@alexbakharew alexbakharew self-assigned this Oct 7, 2026
@fm4v
fm4v added this pull request to the merge queue Oct 8, 2026
Merged via the queue into ClickHouse:master with commit c9d4edb Oct 8, 2026
294 of 295 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 8, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 8, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2026
Backport #123514 to 26.9: Fix mv source engine checks
clickhouse-gh Bot added a commit that referenced this pull request Oct 8, 2026
Backport #123514 to 26.8: Fix mv source engine checks
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
alexey-milovidov pushed a commit to fastio/ClickHouse that referenced this pull request Oct 10, 2026
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]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 18, 2026
tuanpach pushed a commit to tuanpach/ClickHouse that referenced this pull request Oct 19, 2026
…w's source table

Since ClickHouse#123514 `tryResolveTableIdentifier` resolves the source table of a
materialized view (outside view inner queries) to the catalog storage with the
view source's metadata, and relies on `replaceStorageInQueryTree` to swap the
inserted block in after the passes. `buildFilterQueryTree`, which builds the
query tree of every row policy, `additional_table_filters`,
`parallel_replicas_custom_key` and `additional_result_filter`, never did the
swap. A filter like `x IN src` (or an `IN` subquery reading `src` as a
non-leftmost table) under an MV push therefore planned a read of the real
`MergeTree` with key-less metadata, and `KeyCondition` dereferenced the missing
primary key expression (SIGSEGV). With a source engine without index analysis
(`Null`) the filter read the empty table instead of the block, and the view
wrote no rows.

Do the swap in `buildFilterQueryTree`, with the same guard as the analysis-time
scalar subquery path: the inner query of an ordinary view reads the table, not
the block (05345).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
bdeitte pushed a commit to bdeitte/ClickHouse that referenced this pull request Nov 5, 2026
… 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]>
lazerg pushed a commit to lazerg/ClickHouse that referenced this pull request Nov 12, 2026
… 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).

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]>
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-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.3-must-backport v26.7-must-backport v26.8-must-backport v26.9-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FINAL/SAMPLE on an MV's own source in a subquery breaks INSERT

6 participants