Skip to content

Fix deduplication token attribution for materialized-view targets of partitioned tables - #112649

Merged
fm4v merged 1 commit into
masterfrom
dedup-filtertopartition-mv-drift
Aug 4, 2026
Merged

fm4v merged 1 commit into
masterfrom
dedup-filtertopartition-mv-drift

Conversation

@CheSema

@CheSema CheSema commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

Linked issues

Related: #111100
Related: #111103
Caused by: #111049

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 an out-of-bounds read (a crash in release builds) in insert deduplication when an INSERT flows through a materialized view whose inner query changes the number of rows and the deduplicating target table is partitioned. DeduplicationInfo::filterToPartition attributed each deduplication token to partitions by walking the token's source-row range over a partition selector sized to the (smaller) view-output block, reading past its bounds. It now keeps every token in every partition for any materialized-view target — where the source-row-to-partition mapping no longer exists after the row count changed — instead of the out-of-bounds walk (and instead of the NOT_IMPLEMENTED that a later guard raised for the same insert).


filterToPartition was introduced by #111049 to fix an async-insert deduplication-token bleed across partitions (#111031): for a multi-token insert split across partitions it drops, from each partition's part, the tokens whose rows did not land there. That attribution walks each token's source-row range over the partition scatter selector, which is only correct at the direct insert destination, where the deduplication info's offsets still describe exactly the block being split.

A materialized-view target can have changed the row count in its inner query (e.g. GROUP BY), so the tokens describe the source rows while the selector describes the smaller view-output block — there is no source-row-to-partition mapping. #111103 turned the resulting out-of-bounds walk into a fail-loud NOT_IMPLEMENTED, which rejects a legitimate insert. This PR instead keys off the token's own view provenance (level == Level::VIEW, set whenever the data passes through a view) and keeps every token in every partition for any view target. This cannot cause cross-partition false deduplication because the deduplication block id is prefixed with the partition_id.

Tests:

  • 04658_deduplication_mv_row_drift_partitioned — a plain materialized view (no Alias engine) with a row-count-changing GROUP BY into a partitioned deduplicating target, single-token and two-token; the insert deduplicates instead of crashing or being rejected.
  • 04621_deduplication_alias_hop_partitioned_target — the two-token case now deduplicates instead of being rejected with NOT_IMPLEMENTED.

Version info

  • Merged into: 26.8.1.734-master (included in 26.8 and later)
  • Backported to: 26.7.3.9, 26.3.39.4

…ialized-view targets

filterToPartition attributed each token to partitions by walking its source-row
range over a partition selector sized to the sink block. When a materialized view
changes the row count before a partitioned deduplicating target, the tokens describe
the source rows while the selector describes the smaller view-output block, so the
walk read out of bounds (a crash in release builds; a NOT_IMPLEMENTED rejection after
#111103's guard). Per-partition attribution is only valid at the direct insert
destination; for any materialized-view target (level == VIEW) keep every token in
every partition instead.

Related: #111100
Related: #111103
Caused by: #111049
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [47f7654]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
Server died FAIL cidb IGNORED
Logical error: Got read request from replica A for unknown stream B. (STID: 5217-5b3d) FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR fixes the out-of-bounds read in filterToPartition for row-count-changing materialized-view paths, and the new stateless tests cover the reported GROUP BY cases. I found one blocker in the new guard: the level == Level::VIEW shortcut is broader than the bug and regresses row-preserving dependent materialized views whose token-to-row mapping is still valid.

Findings

❌ Blockers

  • [src/Interpreters/InsertDeduplication.cpp:166] filterToPartition now skips partition-local token attribution for every dependent materialized view, not only the row-drifted ones. setViewID marks even a pass-through TO dst AS SELECT * FROM src as VIEW, so a partitioned target behind such a view now runs deduplicateSelf on the full token list in every partition. For async inserts, getBlockUnifiedHash normalizes SOURCE_NUMBER, so one insert split into multiple source blocks under one insert_deduplication_token can make a later partition deduplicate rows that only existed in an earlier partition. Keep the old row-based filtering whenever row_to_partition.size() == getRows(), and only fall back to cloneSelf() once the offsets no longer describe the split block.
Tests
  • ⚠️ Add a focused stateless test for a row-preserving dependent materialized view into a partitioned deduplicating target, with an async insert forced into multiple source blocks under one insert_deduplication_token. The expected result is one surviving row per partition; CI is green because nothing currently exercises the new VIEW fast path without row drift.
Final Verdict

Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 30, 2026
/// count in its inner query, so there is no mapping from the tokens' source rows to the
/// view-output selector: keep every token in every partition instead. A repeated token may
/// still be deduplicated per partition through the cached data hashes.
if (level == Level::VIEW)

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.

level == Level::VIEW is too broad here. setViewID flips that bit for every dependent materialized view, including a trivial TO dst AS SELECT * FROM src where row_to_partition.size() == getRows() and the per-partition attribution is still valid. In those row-preserving cases, returning cloneSelf() feeds the full token list into deduplicateSelf for every partition, so async/user-token duplicates from another partition now participate in the current partition's self-dedup. Because async hashes normalize SOURCE_NUMBER, a single async insert split into multiple source blocks under one insert_deduplication_token can drop the later partition entirely behind a pass-through MV: filterSelf keeps the first duplicate token globally, even if that token wrote no rows to this partition. This fallback needs to key off the mapping actually being lost, not just VIEW.

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.

After MV's select we could not think that we still have the mapping which row from what insert and which row belongs to what token. The matching rows count does not grant us to make such assumption either.

@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.40% -0.10%
Functions 91.90% 91.80% -0.10%
Branches 78.60% 78.50% -0.10%

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

Full report · Diff report

@watson-ch

watson-ch Bot commented Aug 3, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2006

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@fm4v
fm4v added this pull request to the merge queue Aug 4, 2026
@watson-ch

watson-ch Bot commented Aug 4, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2006

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

Merged via the queue into master with commit 88fffd0 Aug 4, 2026
176 of 178 checks passed
@fm4v
fm4v deleted the dedup-filtertopartition-mv-drift branch August 4, 2026 07:36
@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 Aug 4, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 4, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Aug 4, 2026
…erialized-view targets of partitioned tables
clickhouse-gh Bot added a commit that referenced this pull request Aug 4, 2026
Backport #112649 to 26.7: Fix deduplication token attribution for materialized-view targets of partitioned tables
@EmeraldShift

Copy link
Copy Markdown
Contributor

The current release of 26.7 does not contain this bug-fix, and we really need it. Is it possible to cut a new patch release of 26.7 that includes this PR?

@fm4v

fm4v commented Aug 5, 2026

Copy link
Copy Markdown
Member

@EmeraldShift I'm so sorry, we are currently improving the patch release process, and it might take us up to 1 week to release it.

@fm4v

fm4v commented Aug 6, 2026

Copy link
Copy Markdown
Member

@EmeraldShift new release v26.7.3.19-stable

@EmeraldShift

Copy link
Copy Markdown
Contributor

Thank you for the quick turnaround!

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 7, 2026
fm4v added a commit that referenced this pull request Aug 7, 2026
Backport #112649 to 26.5: Fix deduplication token attribution for materialized-view targets of partitioned tables
fm4v added a commit that referenced this pull request Aug 7, 2026
Backport #112649 to 26.6: Fix deduplication token attribution for materialized-view targets of partitioned tables
@CheSema CheSema added v26.3-must-backport v26.5-must-backport v26.6-must-backport v26.7-must-backport and removed v26.2-must-backport 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 labels Aug 10, 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 Aug 10, 2026
@watson-ch watson-ch Bot added v26.2-must-backport and removed pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Aug 10, 2026
@watson-ch

watson-ch Bot commented Aug 10, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2006

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 10, 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 Aug 10, 2026
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…on for materialized-view targets of partitioned tables
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Sep 16, 2026
…on for materialized-view targets of partitioned tables
@4ertus2

4ertus2 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Would it be backported to 26.3? It looks it is present there since 26.3.17.65 and auto-backport failed in linked PR.

@robot-ch-test-poll3 robot-ch-test-poll3 removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 1, 2026
robot-clickhouse added a commit that referenced this pull request Oct 1, 2026
Cherry pick #112649 to 26.3: Fix deduplication token attribution for materialized-view targets of partitioned tables
robot-clickhouse added a commit that referenced this pull request Oct 1, 2026
…erialized-view targets of partitioned tables
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 1, 2026
CheSema added a commit that referenced this pull request Oct 1, 2026
Backport #112649 to 26.3: Fix deduplication token attribution for materialized-view targets of partitioned tables
@RinChanNOWWW

Copy link
Copy Markdown
Contributor

It seems that this PR is not backported to 26.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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-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.2-must-backport v26.3-must-backport v26.4-must-backport v26.5-must-backport v26.6-must-backport v26.7-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.