Skip to content

Preserve input order for order-sensitive insert destinations - #128833

Merged
alexbakharew merged 12 commits into
ClickHouse:masterfrom
aschoube:fix/126613-preserve-insert-order
Nov 20, 2026
Merged

alexbakharew merged 12 commits into
ClickHouse:masterfrom
aschoube:fix/126613-preserve-insert-order

Conversation

@aschoube

@aschoube aschoube commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Parallel insert sinks can commit blocks out of input order, causing order-sensitive engines to keep an older row or collapse the wrong state.

Separate physical parallel-write support from insertion-order sensitivity with IStorage::isInsertOrderSensitive. Preserve a single input stream for CollapsingMergeTree, CoalescingMergeTree, GraphiteMergeTree, ReplacingMergeTree (including replicated variants), and EmbeddedRocksDB. Versioned ReplacingMergeTree is order-sensitive too when versions tie. A SELECT with multiple output streams has no input order to retain, so these destinations can still use parallel sinks.

Keep KeeperMap single-stream for every input: parallel sinks can concurrently create the same Keeper node and fail with Node exists.

InterpreterInsertQuery::addInsertToSelectPipeline checks the original stream count before resizing. InsertDependenciesBuilder applies the same policy through materialized views, proxies, and dependent-view graphs hidden behind Alias targets. A hidden KeeperMap target prevents outer fan-out even for multistream input. Existing deduplication and separate-context restrictions remain in place.

Cover ordered and multistream sink counts, multi-block input order and final results, GraphiteMergeTree data, materialized-view and Alias destinations, replicated engines, and KeeperMap data for plain INSERT and multistream INSERT SELECT. Keep zero-match grep -c results visible in test failures and document the policy in max_insert_threads.

Closes: #126613

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 incorrect results from multi-block INSERT queries into order-sensitive table engines when max_insert_threads enables parallel writes. Plain inserts and single-stream INSERT SELECT preserve input order for CollapsingMergeTree, CoalescingMergeTree, GraphiteMergeTree, ReplacingMergeTree (including replicated variants), and EmbeddedRocksDB, including through materialized views or Alias tables. Multistream SELECT input can retain parallel sinks; KeeperMap stays single-stream for all inserts.


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

Version info

  • Merged into: 26.12.1.57-master (included in 26.12 and later)

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization labels Nov 3, 2026
@alexbakharew alexbakharew self-assigned this Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7eb6ee7]

Summary: ✅


AI Review

Summary

Reviewed PR #128833 at 7eb6ee7d587fab05c0beb2fb7922465ec5fe51ef, including the current diff and all prior discussion. The change preserves single-stream input order for order-sensitive destinations while allowing parallel writes for already-multistream input, and keeps directly reached KeeperMap destinations serial. The earlier tied-version, hidden-Alias, and UNIQUE KEY findings are addressed. No new inline findings; approve the scoped fix with the explicitly deferred issue below.

Findings

⚠️ Majors

Final Verdict

Status: ✅ Approve

No additional changes required in this PR. The remaining pre-existing issue has an explicit tracking issue and an accepted deferral; no duplicate inline comment was posted.

LLVM Coverage Report

Measured on commit 7eb6ee7.

Metric Baseline Current Δ
Lines 91.80% 91.80% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.40% 81.40% +0.00%

Changed lines: Changed C/C++ lines covered: 94/103 (91.26%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 6, 2026
@alexbakharew alexbakharew added the can be tested Allows running workflows for external contributors label Nov 9, 2026
`never` is not a value of `deduplicate_insert`, so the client rejected the setting before running the query. Use `disable`.
@clickhouse-gh

clickhouse-gh Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7eb6ee7d5 with master 971c977ac (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

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

Job report

…ias` single-stream

`ReplacingMergeTree` keeps the last inserted row among rows with equal
versions, so a versioned table depends on insertion order too. Make
`MergeTreeData::supportsParallelInsert` return false for every `Replacing`
mode and register `ReplacingMergeTree` and `ReplicatedReplacingMergeTree`
with `supports_parallel_insert = false`.

An `Alias` reports only its target's `supportsParallelInsert`, but each
`AliasSink` runs its own nested `INSERT` that also writes the target's
dependent views. Add `forwardedInsertHidesSerialDestination` and
`insertReachesSerialDestination` to `InsertDependenciesBuilder` to follow
`Alias` targets, their dependent materialized views at any depth and aliases
reached through them, and keep the insert single-stream when they reach an
order-sensitive destination.

Use tied versions for `order_versioned` in `05480` and add an alias chain
to an unversioned `ReplacingMergeTree`, directly and through a materialized
view.
@clickhouse-gh clickhouse-gh Bot added comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 9, 2026
@aschoube

Copy link
Copy Markdown
Contributor Author

@alexbakharew Can you review this PR?

@alexbakharew

Copy link
Copy Markdown
Contributor

Hi @aschoube,

the CH Inc sync check is red because of the new test 05481_ordered_insert_sinks_zookeeper: our internal CI runs the stateless tests with ReplicatedMergeTree replaced by SharedMergeTree, so grep -c "ReplicatedMergeTreeSink" finds nothing and the script exits with code 1. Could you add the no-shared-merge-tree tag to it (# Tags: zookeeper, no-shared-merge-tree) with a short note that the test checks the ReplicatedMergeTree sink name? Nothing else failed there, I'll look at the rest of the PR after that.

@aschoube

aschoube commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexbakharew I have added no-shared-merge-tree to 05481_ordered_insert_sinks_zookeeper, with a note that the test checks the ReplicatedMergeTreeSink name.

clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 16, 2026

@clickhouse-gh clickhouse-gh Bot left a comment

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.

AI review approves this commit, see the AI Review comment.

@alexbakharew

Copy link
Copy Markdown
Contributor

Hi @aschoube,
When the SELECT has several streams there is no input order to keep, so can we leave parallel sinks in that case? Not for KeeperMap though: with parallel sinks it fails with “Node exists”, so it has to stay single-stream always.

Tests: could you also add GraphiteMergeTree (the test config already has graphite_rollup) and a data check for KeeperMap, for both plain INSERT and INSERT SELECT? On master both fail with “Node exists”. Also, grep -c under set -e stops the script silently when the count is 0; || true makes the failure readable.

Separate `isInsertOrderSensitive` from physical `supportsParallelInsert` support. Keep `KeeperMap` serial for every input and follow hidden `Alias` and materialized-view destinations before fan-out.

Add stream-count, `GraphiteMergeTree`, and `KeeperMap` data regressions, and make zero-count pipeline failures visible.
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 19, 2026
@aschoube

Copy link
Copy Markdown
Contributor Author

I have separated isInsertOrderSensitive from supportsParallelInsert, and checked the original stream count in InterpreterInsertQuery::addInsertToSelectPipeline before resizing.

i.e. single-stream input keeps one sink for order-sensitive destinations; an already-multistream SELECT can retain parallel sinks. KeeperMap remains single-stream, including through Alias and materialized views.

I have added 05813_ordered_insert_select_streams for the stream counts and GraphiteMergeTree data, and 05814_keeper_map_parallel_insert_data for plain INSERT and multistream INSERT SELECT data checks.

On the previous PR revision, the multistream ordered_rmt case reports one sink instead of four.

I have added || true to the grep -c pipelines so zero counts appear in the test output.

@clickhouse-gh
clickhouse-gh Bot dismissed stale reviews from themself November 19, 2026 13:28

AI review of a later commit does not approve the PR, see the AI Review comment.

…ibuted`

A `MergeTree` table with a `UNIQUE KEY` has the `Ordinary` merging mode, so
`isInsertOrderSensitive` returned `false` and an input with one stream still
got parallel sinks. `unique_key_conflict_action` resolves a conflict by the
order in which the blocks are committed, so with four insert threads `ignore`
kept the rows of a later block instead of the first one, and `overwrite` could
keep the rows of a block that is not the last one.

`Distributed` inherited `false` as well. Each `DistributedSink` sends its
blocks to the shard in a separate insert, so an `INSERT SELECT` with one
stream into a `Distributed` table over `ReplacingMergeTree` kept an older row.

Both now report that they depend on the input order. A `SELECT` with several
streams keeps parallel sinks, as before.

`02981_insert_select_resize_to_max_insert_threads` read an empty table in its
parallel replicas case. Such a read has one stream, so the case now needs data
in the table to check the resize.
@alexbakharew alexbakharew added ci-coverage Run the LLVM coverage jobs in addition to the normal CI. The PR can merge with it. and removed ci-llvm-coverage labels Nov 19, 2026
The coverage report of this pull request showed that tests never reached
three branches of `insertReachesSerialDestination`: an `Alias` of a
materialized view, an `Alias` of a table made from a table function (a
`StorageProxy`), and the depth limit of the check.

The test counts the sinks of the outer insert for these cases. A table with
15 cascaded materialized views behind an `Alias` keeps parallel sinks; with 16
views the check reaches its depth limit and keeps one sink.

https://s3.amazonaws.com/clickhouse-test-reports/PRs/128833/fc807a9725cfcaab9abb7044ec8196310d062546/pr/llvm_coverage/print_uncovered_code/print_uncovered_code.log
`05836_unique_key_preserves_insert_order` fails in `Stateless tests
(arm_binary, ParallelReplicas, plan-based, s3 storage, parallel)`. Plan-based
parallel replicas cannot read a `UNIQUE KEY` table yet (`SUPPORT_IS_DISABLED`),
see ClickHouse#128534. Set
`parallel_replicas_plan_based = 0` for the final `SELECT` of the test, as
ClickHouse#132872 did for two other tests.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128833&sha=7016cca5a1e22feb060ace1d9ce40eba62adfe09&name_0=PR
ClickHouse#128833
@alexbakharew
alexbakharew added this pull request to the merge queue Nov 20, 2026
Merged via the queue into ClickHouse:master with commit f01df65 Nov 20, 2026
195 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 20, 2026
alexey-milovidov added a commit that referenced this pull request Nov 21, 2026
…master`

Two semantic conflicts with #128833
that the merge of `master` left behind:

`insertReachesSerialDestination` failed closed on a dependent view whose
target table is dropped. Such a view is pruned from the graph of the nested
`INSERT` under `ignore_materialized_views_with_dropped_target_table` or
`materialized_views_ignore_errors` and creates no sink there, so an `INSERT`
through an `Alias` lost its fan-out, which
`04825_insert_quorum_ignored_broken_view_fanout` pins. The probe now prunes
it the same way as `dependentViewMayWriteToReplicatedTable` does, resolving
the target through the context of the `INSERT`.

`05837_distributed_insert_select_preserves_order` expected four
`DistributedSink`s for an `INSERT SELECT` from a multi-stream source. This
branch intentionally applies the single-stream guards of a plain `INSERT`
(`insertDeduplicationNeedsSingleStream`, `serial_hidden_views`) to
`INSERT SELECT` as well, and a `Distributed` destination hides the dependent
views and the deduplication of the tables on the shards, so that case is
single-stream now. The reference and the comment of the test are updated;
the ordering half of the test is unchanged.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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 ci-coverage Run the LLVM coverage jobs in addition to the normal CI. The PR can merge with it. comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. external Author is not a member of the ClickHouse organization 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

3 participants