Skip to content

Fix logical error 'visited_views == right->visited_views' on parallel materialized view squashing - #107027

Merged
CheSema merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-visited-views-assert-parallel-mv-squash
Jun 11, 2026
Merged

CheSema merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-visited-views-assert-parallel-mv-squash

Conversation

@groeneai

@groeneai groeneai commented Jun 10, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Fixed a server crash (logical error this->visited_views == right->visited_views) on INSERT when two materialized views on the same source table write to the same target table and a dependent view reads that target, with materialized_views_squash_parallel_inserts enabled.

Description

When two materialized views on the same source table write to the same target table, and a dependent materialized view reads that target (for example via a scalar subquery), an INSERT could abort the server in debug and sanitizer builds:

Logical error: 'this->visited_views == right->visited_views'
DeduplicationInfo::mergeSelf  src/Interpreters/InsertDeduplication.cpp
DeduplicationInfo::merge
CollectionOfDerivedItems<ChunkInfo>::mergeWith
Squashing::squash
ApplySquashingTransform::onConsume

Root cause: with insert deduplication disabled, the parallel-insert squashing optimization (materialized_views_squash_parallel_inserts, enabled by default) merges chunks produced by the different views. Those chunks carry DeduplicationInfo whose visited_views chains legitimately differ, because they originated from different views. mergeSelf asserted the chains were equal.

The assertion is over-strict when deduplication is disabled. In that case visited_views is only bookkeeping: getDeduplicationHashes returns an empty set when disabled, and the deduplication retry path is unreachable. The two sibling visited_views checks in the same function are already guarded by !disabled; this change brings the equality check in line with them. When deduplication is enabled, squash_parallel_inserts is forced off, so the convergence cannot happen and the assertion stays fully active to catch real bugs.

Found by the experimental serverfuzz Stress test on master, and seen in several Stress runs across unrelated PRs. Added a regression test 04327_mv_squash_parallel_inserts_visited_views.

Version info

  • Merged into: 26.6.1.645

… MV squashing

When two materialized views on the same source table write to the same target
table, and a dependent view reads that target (e.g. via a scalar subquery), an
INSERT could abort the server with a debug/sanitizer assertion:

  Logical error: 'this->visited_views == right->visited_views'
  DeduplicationInfo::mergeSelf (src/Interpreters/InsertDeduplication.cpp)

Root cause: with insert deduplication disabled, the parallel-insert squashing
optimization (materialized_views_squash_parallel_inserts, on by default) merges
chunks produced by the different views. Those chunks carry DeduplicationInfo
whose visited_views chains legitimately differ, because they originated from
different views. mergeSelf asserted the chains were equal.

That assertion is over-strict when deduplication is disabled. visited_views is
only bookkeeping in that case: getDeduplicationHashes returns an empty set when
disabled, and the deduplication retry path is unreachable. The two sibling
visited_views checks in the same function are already guarded by !disabled;
this brings the equality check in line with them. When deduplication is enabled,
squash_parallel_inserts is forced off, so the convergence cannot happen and the
assertion stays fully active to catch real bugs.

This was observed by the experimental serverfuzz Stress test on master and in
several Stress runs across unrelated PRs.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On master HEAD (debug build): source (MergeTree), source_null (Null) with two MVs source -> source_null, plus a dependent MV mv_a reading source_null via (SELECT count() FROM source_null), then INSERT INTO source SELECT number FROM numbers(2000) SETTINGS min_insert_block_size_rows=1500, max_insert_block_size=1500 aborts the server every time.
b Root cause explained? Insert dedup disabled -> deduplicate_blocks_in_dependent_materialized_views=false -> materialized_views_squash_parallel_inserts=true and DeduplicationInfo::disabled=true. The two views to the same target produce chunks whose visited_views chains differ (different originating view). Parallel-insert squashing merges them, and mergeSelf asserted this->visited_views == right->visited_views.
c Fix matches root cause? Yes. The equality assertion is over-strict when disabled: visited_views is bookkeeping only then (getDeduplicationHashes returns {}, the retry path is unreachable). Guarded with !disabled, matching the two sibling visited_views checks in the same function already guarded the same way.
d Test intent preserved / new tests added? Added regression test 04327_mv_squash_parallel_inserts_visited_views. The assertion stays fully active when dedup is enabled (where squash_parallel_inserts is forced off and the assertion catches real bugs).
e Both directions demonstrated? Yes. Crashes on unpatched master HEAD binary; passes with the fix (insert succeeds, 2000 rows, 0 assertion hits). Verified 10+ runs including 3-views-to-same-target and varied block sizes.
f Fix is general, not a narrow patch? mergeSelf is the only DeduplicationInfo merge site, so no sibling code path has the same issue. Not a symptom guard: differing visited_views under parallel squashing is a designed valid state (the optimization is explicitly disabled when dedup-in-MVs is on), and the field is dead metadata when disabled.

Session id: cron:clickhouse-worker-slot-2:20260610-150600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CheSema @al13n321 could you review this? It relaxes the visited_views == right->visited_views assertion in DeduplicationInfo::mergeSelf to be guarded by !disabled (matching the two sibling visited_views checks). With dedup disabled, parallel-insert squashing legitimately merges chunks from different views writing to the same target, so their visited_views chains differ; the assertion was aborting the server. Repro and root-cause details are in the description and the validation-gate comment.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jun 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c6039a1]

Summary: ❌

job_name test_name status info comment
Bugfix validation (functional tests) FAIL
04327_mv_squash_parallel_inserts_visited_views FAIL cidb
Stateless tests (arm_asan_ubsan, azure, sequential) FAIL
03531_check_count_for_parquet ERROR cidb
Server died FAIL cidb
Segmentation fault (STID: 0883-1d8f) FAIL cidb
Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DatabaseReplicated, WasmEdge, parallel) ERROR
LLVM Coverage DROPPED

AI Review

Summary

This PR relaxes the DeduplicationInfo::mergeSelf visited_views equality assertion when deduplication is disabled, so parallel materialized-view squashing can merge chunks from sibling views that legitimately have different visited_views chains. The code path looks consistent: disabled DeduplicationInfo produces no deduplication hashes and cannot reach the retry path. I did not find an inline code issue to post; the only concern is PR metadata.

PR Metadata
  • The selected Bug Fix (user-visible misbehavior in an official stable release) category does not match the actual impact described by the PR. The failing guard is a chassert, which is active in debug/sanitizer builds and compiled out in release builds, so this is not a stable-release user-visible bug. Suggested replacement:
### Changelog category (leave one):
- CI Fix or Improvement (changelog entry is not required)

### Changelog entry (a [user-readable short description](https://github.com/ClickHouse/ClickHouse/blob/master/docs/changelog_entry_guidelines.md) of the changes that goes into CHANGELOG.md):
...
Missing context / blind spots
  • ⚠️ I could not run tests/queries/0_stateless/04327_mv_squash_parallel_inserts_visited_views.sql locally because this checkout has no build*/programs/clickhouse binary. GitHub shows Style check succeeded, the aggregate PR status is still pending, and Bugfix validation (functional tests) is red for this assertion-only case; a completed debug/sanitizer or stateless run would close the validation gap.
Findings
  • 💡 Nits
    • [PR description] The changelog entry currently uses server crash wording for a debug/sanitizer chassert failure. If maintainers keep a changelog entry despite using a no-entry category, reword it as a logical-error exception, for example: Fixes a logical-error exception with INSERT into materialized-view chains when parallel squashing merges chunks from sibling views and deduplication for dependent materialized views is disabled.
Final Verdict

Status: ✅ Approve

No inline code findings. The metadata cleanup above is recommended before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.20% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 4/5 (80.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@CheSema CheSema self-assigned this Jun 10, 2026
@CheSema

CheSema commented Jun 10, 2026

Copy link
Copy Markdown
Member

@groeneai I do not see deduplicate_blocks_in_dependent_materialized_views=false on the test. You described that deduplication has to be disabled to trigger the fail. How did you trigger it without materialized_views_squash_parallel_inserts=fase?

@alexey-milovidov alexey-milovidov mentioned this pull request Jun 10, 2026
1 of 21 tasks
The test relied on an implicit precondition: parallel-insert squashing
(materialized_views_squash_parallel_inserts) is force-disabled whenever
deduplication for dependent materialized views is on, so the squashing path
that triggers the assertion is only reachable when MV deduplication is off.

The original INSERT did not set deduplicate_blocks_in_dependent_materialized_views;
it happened to be effectively off because INSERT ... SELECT FROM numbers() is a
non-stable INSERT SELECT and deduplicate_insert_select=enable_when_possible
(default) auto-disables deduplication for it. That is subtle and easy to break
under settings randomization, so set the setting explicitly to make the
precondition unambiguous. Verified on a default-settings clickhouse-server: the
test still aborts the server with 'this->visited_views == right->visited_views'
on a binary without the fix, and passes with the fix.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

@CheSema good question, and you are right that the precondition was only implicit. I have now set it explicitly (pushed in c6039a1).

How the failure was triggered without setting deduplicate_blocks_in_dependent_materialized_views=false:

The INSERT is INSERT INTO source SELECT number FROM numbers(2000), i.e. a non-stable INSERT SELECT. The default deduplicate_insert_select=enable_when_possible disables deduplication for such a query (isDeduplicationEnabledForInsertSelect returns (select_query_sorted || has_user_token) && ..., both false here), and InterpreterInsertQuery then overrides deduplicate_insert to disable for this query. So deduplicate_blocks ends up false even though deduplicate_blocks_in_dependent_materialized_views keeps its default of true.

That is what unblocks the squashing path:

// InsertDependenciesBuilder.cpp
deduplicate_blocks = isDeduplicationEnabledForInsert(...);                 // false here (non-stable INSERT SELECT)
deduplicate_blocks_in_dependent_materialized_views = deduplicate_blocks && settings[...];  // false && true = false
squash_parallel_inserts = !deduplicate_blocks_in_dependent_materialized_views && settings[materialized_views_squash_parallel_inserts];  // true

With squashing on, chunks from mv_null_1 and mv_null_2 (different visited_views) get merged in DeduplicationInfo::mergeSelf and hit the assertion.

I verified this on a default-settings clickhouse-server (not clickhouse-local): the test aborts with Logical error: 'this->visited_views == right->visited_views' on a binary without the fix, and passes with it. To not rely on the INSERT SELECT auto-disable subtlety (which could change under settings randomization), the test now sets deduplicate_blocks_in_dependent_materialized_views = 0 explicitly on the INSERT.

On the Bugfix validation (functional tests) red: the guard is a chassert, which is a no-op in the release master-HEAD binary the bugfix-validation framework runs against, so the regression test cannot deterministically FAIL on master even though the bug is real in debug/sanitizer/serverfuzz builds. Same framework limitation seen on the analogous assertion-only fixes.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI summary on HEAD c6039a1 (CI fully finished). Disposition of each failure:

  • Bugfix validation (functional tests) / 04327_mv_squash_parallel_inserts_visited_views — expected. The fix guards a debug-only chassert(this->visited_views == right->visited_views). chassert is a no-op in the release binary the bugfix-validation framework builds from master HEAD, so the regression test cannot deterministically FAIL there even though the bug is real (and reproduces) in debug/sanitizer/serverfuzz builds. This is the same framework limitation discussed on PR Fix LimitTransform WITH TIES assertion on empty virtual-row chunk #105102 / Fix LOGICAL_ERROR "Column identifier is already registered" in ALTER UPDATE with EXISTS #106025. The fix and the test were validated in both directions on a debug build (without-fix → SIGABRT, with-fix → pass), as detailed above.
  • Stateless tests (arm_asan_ubsan, azure, sequential) — Server died + Segmentation fault (STID 0883-1d8f) + 03531_check_count_for_parquet ERROR — unrelated infrastructure. This matches the 2026-06-10/11 arm_asan_ubsan, azure ASan-allocator OOM outage that hit ~100 unrelated PRs and master in the same window (not specific to this PR).
  • The arm_asan_ubsan, azure, parallel batch of failures was on the previous commit 280b2f8d (same azure outage), not on the current HEAD.

No PR-caused failures on the current HEAD. Ready for review.

@CheSema
CheSema added this pull request to the merge queue Jun 11, 2026
Merged via the queue into ClickHouse:master with commit f81e540 Jun 11, 2026
162 of 166 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 Jun 11, 2026
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 pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants