Skip to content

Fix multi-block inserts to Alias with insert_deduplication_token - #103246

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
eclbg:fix-alias-deduplication
May 19, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
eclbg:fix-alias-deduplication

Conversation

@eclbg

@eclbg eclbg commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Keep a single InterpreterInsertQuery pipeline across all chunks so they get a different source_block_number

Closes #103118

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 multi-block inserts to Alias with insert_deduplication_token so all blocks are kept

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.832

@eclbg

eclbg commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor Author

FYI @nauu and @jkartseva as you've been involved in other Alias PRs

I'm trying to learn the CH codebase and as I was already working on Alias and deduplication I tried to fix this bug reported by @clickgapai. I'll be responsive to feedback so lmk if you have any

@clickgapai

Copy link
Copy Markdown
Contributor

Thanks for the suggestion about PR #103246. I investigated this approach, but unfortunately it wouldn't work for our case - that PR modifies ClickHouse source code (StorageAlias.cpp), while test fixes need to stay within the test framework itself. I also tested it locally and encountered timeouts, indicating the approach has some issues.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [42d7236]

Summary: ✅


AI Review

Summary

This PR fixes insert_deduplication_token handling for multi-block inserts through Alias by preserving per-chunk deduplication metadata across the forwarded insert pipeline, and it hardens missing-target Alias behavior (including Merge planning paths) with focused regression tests. I reviewed the current diff and prior discussion threads and did not find unresolved correctness, safety, or performance issues in the current head.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 24, 2026
@eclbg
eclbg marked this pull request as draft April 27, 2026 10:28
@eclbg

eclbg commented Apr 27, 2026

Copy link
Copy Markdown
Contributor Author

I'm reviewing flakiness in the proposed approach

alexey-milovidov added a commit to eclbg/ClickHouse that referenced this pull request May 10, 2026
In `AliasSink`, each sink instance builds its own `InterpreterInsertQuery`
pipeline. That pipeline contains an `AddDeduplicationInfoTransform` whose
`block_number` counter starts at zero. With `max_insert_threads > 1`,
multiple `AliasSink` instances are created in parallel and each produces
deduplication tokens like `<user_token>_0`, `<user_token>_1`, ... starting
from zero. Tokens collide across sinks and parts are deduplicated against
each other, losing rows.

Test `04102_storage_alias_insert_dedup_token` failed under the flaky check
which randomizes settings (e.g. `--max_insert_threads 2` or `3`),
producing 6 rows instead of 10.

Override `supportsParallelInsert` to return `false` for `StorageAlias` so
that only a single sink is created.

Failing CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103246&sha=1eea22708c3752717263f69ad86608b72d9eb3ba&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29

PR: ClickHouse#103246

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@eclbg

eclbg commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov I thought it was a shame to disable parallel inserts completely for Alias and wanted to review if the issue is only when insert_deduplication_token is set. I'll have another look during the day today. Sorry for leaving this for a while without progress!

@eclbg

eclbg commented May 11, 2026

Copy link
Copy Markdown
Contributor Author

I've made some progress on this today. I'm trying to assign a source_block_number before the fan-out in parallel inserts, and then respecting it if it's already set in DeduplicationTokenTransforms. Still running some of the test suite locally to try to detect any side-effects, as I'm not so familiar with this part of the codebase. Will push or declare failure tomorrow morning CEST.

@eclbg
eclbg force-pushed the fix-alias-deduplication branch from 7474b36 to 5cef624 Compare May 12, 2026 15:46
@eclbg

eclbg commented May 12, 2026 •

Copy link
Copy Markdown
Contributor Author

I think I've managed to keep parallel insert for Alias and the expected deduplication behaviour. The main change is that the DeduplicationInfo that sets the source block number before the parallel fan-out. I've updated max_insert_threads in the test to exercise this instead of just being flaky.

Fast tests passed on my machine. Hope this doesn't break anything else.

(sorry for the force push reverting the parallel insert disable. I'll get it back if this approach doesn't work)

Update: I see there's a bunch of failures. Hopefully all for the same reason. I'll tackle them tomorrow
Update2: Failures addressed. Let's see what CI says 🍿

@eclbg
eclbg force-pushed the fix-alias-deduplication branch from 5cef624 to 77c16c9 Compare May 13, 2026 13:56
Comment thread src/Storages/StorageAlias.cpp Resolved
@eclbg
eclbg marked this pull request as ready for review May 13, 2026 18:04
@eclbg
eclbg force-pushed the fix-alias-deduplication branch from 58a0db4 to 0ca5dab Compare May 13, 2026 20:32
@eclbg

eclbg commented May 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Just a couple flaky test failures. Rebased and pushed

Update: the stress test is not flaky :D Fixing the logical error that makes it fail.

@eclbg
eclbg force-pushed the fix-alias-deduplication branch from 0ca5dab to 1281af5 Compare May 14, 2026 05:26
@eclbg
eclbg marked this pull request as draft May 14, 2026 10:10
Comment thread src/Storages/StorageAlias.h Resolved
@eclbg

eclbg commented May 14, 2026

Copy link
Copy Markdown
Contributor Author

Whew, we got very lucky with the stress tests and they surfaced a bug that was not specifically exercised in tests. Essentially:

  • Dropping an Alias table whose target doesn't exist failed in replicated databases (not tested until now)
  • The stress tests used a replicated database but didn't check the result of previous test runs. So an Alias table called alias... was left accidentally.
  • Then another test tried to create a Merge(currentDatabase(), 'a') table that accidentally used the leftover alias from a previous test as it reused the test database
  • Then StorageAlias::getInMemoryMetadataPtr returned empty fallback metadata and this triggered the Logical Error

What I've done:

  • Add narrow tests for what wasn't working:
    • Dropping an Alias table whose target no longer exists in a replicated database should work
    • Using an Alias whose target doesn't exist in Merge should throw UNKNOWN_TABLE
  • And generally fixed how methods on StorageAlias were handling this case.

@alexey-milovidov

Copy link
Copy Markdown
Member

This was fixed by #105146. Let's update the branch.

@eclbg

eclbg commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

Been AFK on the weekend but I'm inclined to have supportsSampling() raise UNKNOWN_TABLE, as that's what StorageMaterializedView does. And I'll do the same for the rest of supports... methods. I'll push the changes tomorrow. I'm aware that this is blocking #103488 so I'm keen to get it over the line

eclbg and others added 4 commits May 17, 2026 18:52
Keep a single `InterpreterInsertQuery` pipeline across all chunks so
they get a different `source_block_number`
The reference file incorrectly contained the test's SQL statements as if
they were echoed in the test output. `clickhouse-test` does not echo
queries from `.sql` tests; only the `SELECT` outputs are captured. The
test consistently failed on Fast test because the reference contained
many lines that were not present in the actual output.

Strip the reference down to the three `SELECT` outputs the test
produces.

Failing CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103246&sha=2e04445c1f6d5bbdfb5f3181f4300d8029893cf5&name_0=PR&name_1=Fast%20test
This fixes multi-block parallel inserts through Alias.

AliasSink now forwards incoming Chunk infos to the nested target insert
pipeline. AddDeduplicationInfoTransform then reuses an existing source
identity in DeduplicationInfo, while still updating the current insert
dependencies and original block.
@eclbg
eclbg force-pushed the fix-alias-deduplication branch from b3f794a to 9e43c4e Compare May 18, 2026 17:36
@eclbg

eclbg commented May 18, 2026

Copy link
Copy Markdown
Contributor Author

I'm feeling quite good about this branch now. Hope CI agrees. The last run only had a failure that I deemed as unrelated and that has failed in other PRs recently (cidb)

I'll mark as ready to review if no jobs have failed before my day ends even if some jobs are is still going.

@eclbg
eclbg marked this pull request as ready for review May 18, 2026 20:07

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks plausible.

@alexey-milovidov alexey-milovidov self-assigned this May 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 59.09% (65/110) | lost baseline coverage: 2 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit 552f7df May 19, 2026
165 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
@alexey-milovidov alexey-milovidov mentioned this pull request Jun 12, 2026
1 task done
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.

AliasSink::consume resets dedup block counter per chunk, causing silent data loss with insert_deduplication_token

5 participants