Skip to content

Throw BAD_ARGUMENTS, not LOGICAL_ERROR, for incompatible granularity in MOVE/REPLACE PARTITION - #103881

Merged
hanfei1991 merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-stid-4063-3b45-move-partition-bad-args
Jul 14, 2026
Merged

hanfei1991 merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-stid-4063-3b45-move-partition-bad-args

Conversation

@groeneai

@groeneai groeneai commented May 1, 2026 •

Copy link
Copy Markdown
Collaborator

This fix addresses a BuzzHouse fuzzer finding (STID 4063-3b45) where a
MOVE PARTITION between two MergeTree tables with incompatible granularity
settings throws LOGICAL_ERROR instead of a user-error code. LOGICAL_ERROR
aborts the server in debug / sanitizer builds and surfaces in operator alerts
as a critical incident, which is wrong for a user-reachable input mismatch.

MergeTreeData::canReplacePartition rejects MOVE/REPLACE/ATTACH PARTITION
between two tables when one is on adaptive granularity and the other is on
non-adaptive granularity (i.e. when the destination has
enable_mixed_granularity_parts = false or index_granularity_bytes = 0,
and the source part's mark type does not match the destination's
canUseAdaptiveGranularity). Out of the four call sites that throw on this
condition, three threw LOGICAL_ERROR:

  • StorageMergeTree::movePartitionToTable
  • StorageReplicatedMergeTree::replacePartitionFromImpl
  • StorageReplicatedMergeTree::movePartitionToTable

The fourth (StorageMergeTree::replacePartitionFrom) already used BAD_ARGUMENTS.

This PR aligns all four sites on BAD_ARGUMENTS and adds a regression test
covering all four code paths (plain MergeTree MOVE/REPLACE, ReplicatedMergeTree
MOVE/REPLACE).

Reproducer:

CREATE TABLE src (a UInt64, b UInt64) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity_bytes = 0, enable_mixed_granularity_parts = 0;
CREATE TABLE dst (a UInt64, b UInt64) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity_bytes = 10485760, enable_mixed_granularity_parts = 0;
INSERT INTO src SELECT number, number FROM numbers(1000);

ALTER TABLE src MOVE PARTITION tuple() TO TABLE dst;
-- Before: LOGICAL_ERROR (server abort in debug/sanitizer builds)
-- After: BAD_ARGUMENTS

Originally hit by BuzzHouse (amd_tsan) on 2026-05-01:

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

Throw BAD_ARGUMENTS instead of LOGICAL_ERROR when ALTER TABLE ... MOVE/REPLACE/ATTACH PARTITION is run between two MergeTree tables with incompatible granularity settings (one adaptive, one non-adaptive).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.899 (included in 26.7 and later)

…rity in MOVE/REPLACE PARTITION

`MergeTreeData::canReplacePartition` rejects MOVE/REPLACE/ATTACH PARTITION
between two MergeTree tables when one is on adaptive granularity and the other
is on non-adaptive granularity (i.e. when the destination has
`enable_mixed_granularity_parts = false` or `index_granularity_bytes = 0`,
and the source part's mark type does not match the destination's
`canUseAdaptiveGranularity`). This is a user-reachable condition: a user can
create two MergeTree tables with intentionally different granularity settings,
insert data, and then run `ALTER TABLE ... MOVE/REPLACE PARTITION ...`.

Out of the four call sites that throw on this condition, three threw
`LOGICAL_ERROR`:

* `StorageMergeTree::movePartitionToTable`             (was: `LOGICAL_ERROR`)
* `StorageReplicatedMergeTree::replacePartitionFromImpl` (was: `LOGICAL_ERROR`)
* `StorageReplicatedMergeTree::movePartitionToTable`     (was: `LOGICAL_ERROR`)

The fourth (`StorageMergeTree::replacePartitionFrom`) already used
`BAD_ARGUMENTS`. `LOGICAL_ERROR` is reserved for assertion failures and
internal-bug conditions: it aborts the server in debug / sanitizer builds and
surfaces in operator alerts as a critical incident. It is the wrong error code
for a user-reachable input mismatch.

This commit aligns all four sites on `BAD_ARGUMENTS` and adds a regression test
covering all four code paths.

The bug was found by BuzzHouse on `amd_tsan` (STID 4063-3b45) on 2026-05-01.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103862&sha=3bec163c6ba8dcb12ed1144c8ac75c261863cc81&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
@groeneai

groeneai commented May 1, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (TASK.md Phase 4)

Session: cron:clickhouse-ci-task-worker:20260501-201500

(a) Deterministic repro — yes:

CREATE TABLE src (a UInt64, b UInt64) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity_bytes = 0, enable_mixed_granularity_parts = 0;
CREATE TABLE dst (a UInt64, b UInt64) ENGINE = MergeTree ORDER BY a
SETTINGS index_granularity_bytes = 10485760, enable_mixed_granularity_parts = 0;
INSERT INTO src SELECT number, number FROM numbers(1000);
ALTER TABLE src MOVE PARTITION tuple() TO TABLE dst;
-- Before fix: LOGICAL_ERROR (server abort in debug/sanitizer)
-- After fix: BAD_ARGUMENTS

(b) Root cause explained — MergeTreeData::canReplacePartition (src/Storages/MergeTree/MergeTreeData.cpp:9468) returns false when the destination table's enable_mixed_granularity_parts = false and/or index_granularity_bytes = 0, AND the source part's mark_type.adaptive does not match the destination's canUseAdaptiveGranularity(). This is reachable from any user query that creates two MergeTree tables with intentionally different granularity settings and runs ALTER TABLE ... MOVE/REPLACE/ATTACH PARTITION ... between them. Three of the four call sites (StorageMergeTree::movePartitionToTable, StorageReplicatedMergeTree::replacePartitionFromImpl, StorageReplicatedMergeTree::movePartitionToTable) threw LOGICAL_ERROR for this user-reachable condition, which aborts the server in debug/sanitizer builds and shows up as an internal-bug critical alert in operator dashboards.

(c) Fix matches root cause — yes. The fix changes LOGICAL_ERROR → BAD_ARGUMENTS at the three affected sites. This directly addresses the misclassification (user-reachable condition deserves a user-error code, not an internal-bug code). The fourth site (StorageMergeTree::replacePartitionFrom, line 2601) was already BAD_ARGUMENTS and is exercised by the new test to lock in the consistent behaviour.

(d) Test intent preserved / new tests added — tests/queries/0_stateless/04150_move_partition_inconsistent_granularity.sql is new. It covers all four call sites: plain MergeTree MOVE, plain MergeTree REPLACE, ReplicatedMergeTree REPLACE, ReplicatedMergeTree MOVE, asserting BAD_ARGUMENTS consistently. A future regression at any site will be caught.

(e) Demonstrated in both directions — yes:

  • Before fix: BuzzHouse CI run (PR Add max_bytes_ratio_before_external_join setting #103862) and my local debug-build reproduction both throw LOGICAL_ERROR (CI report linked in PR body).
  • After fix: rebuilt local binary throws BAD_ARGUMENTS for all 4 paths (verified via direct queries and via clickhouse-test --test-runs 20 — 20/20 passes with full randomization).

(f) Fix is general, not a narrow patch — yes:

  • "Does the same bug pattern exist in other code paths?" Searched all callers of canReplacePartition (grep across src/): exactly 4 throw sites, all updated/verified. Searched for the message inconsistent granularity across the codebase: same 4 sites only. No other code path shares this bug.
  • "Am I fixing the root cause or just guarding against the symptom?" The fix is at the throw site, not a defensive check elsewhere. The condition itself (incompatible granularities) is legitimate user input, so the correct fix is to classify it correctly as BAD_ARGUMENTS. No upstream value is being silently corrupted; we are simply renaming the error code.

@groeneai

groeneai commented May 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alesapin @CurtizJ — could you review this? It changes LOGICAL_ERROR → BAD_ARGUMENTS at three MOVE/REPLACE PARTITION sites where two MergeTree tables have incompatible granularity settings (one adaptive, one non-adaptive). The condition is user-reachable, so LOGICAL_ERROR (which aborts in debug/sanitizer builds) is the wrong code. BuzzHouse fuzzer hit this on amd_tsan (STID 4063-3b45).

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

clickhouse-gh Bot commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0ba2ebb]

Summary: ✅


AI Review

Summary

This PR fixes the user-facing error classification for incompatible-granularity MOVE/REPLACE PARTITION in MergeTree and ReplicatedMergeTree. The three changed throw sites now match the existing BAD_ARGUMENTS behavior at the fourth caller, the new regression test covers the affected helpers in both engines, and ATTACH PARTITION FROM still goes through the same replacePartitionFrom helpers, so I did not find a remaining correctness or evidence gap in the current diff.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 1, 2026
@groeneai

groeneai commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI ledger — covered SHA: 5710a3af91c3573f95ca155fcfba903df6774695

CI fully finished (Mergeable Check completed; 0 pending checks).

Check Test Disposition
Stateless tests (amd_tsan, parallel, 1/2) 00096_obfuscator_save_load flaky chronic trunk — task 2026-06-02-ci-00096obfuscatorsaveload-chronic-f
Stateless tests (amd_tsan, parallel, 1/2) 00175_obfuscator_schema_inference flaky chronic trunk — task 2026-06-02-ci-00175obfuscatorschemainference-ab
Unit tests (asan_ubsan) (check-level) infra/chronic UBSan in UntrackedMemoryCounter — task 2026-06-03-ci-p1-chronic-ubsan-in-untrackedmemoryc

No PR-caused failures. Awaiting human review.

Session: cron:our-pr-ci-monitor:20260604-213000

@hanfei1991 hanfei1991 self-assigned this Jun 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Dear @hanfei1991, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@hanfei1991 hanfei1991 self-assigned this Jul 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

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

Full report · Diff report

@hanfei1991
hanfei1991 added this pull request to the merge queue Jul 14, 2026
Merged via the queue into ClickHouse:master with commit b3df6de Jul 14, 2026
176 of 177 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 14, 2026
@Algunenano

Copy link
Copy Markdown
Member

This broke the private CI. Reverting. @groeneai @hanfei1991 Please reintroduce but check private CI before merging

@groeneai

Copy link
Copy Markdown
Collaborator Author

Reintroduced in #110535. Likely private-CI cause: the regression test's ReplicatedMergeTree assertions expect BAD_ARGUMENTS, but under the Cloud SharedMergeTree substitution partition moves take a different code path that doesn't hit this canReplacePartition granularity check, so those assertions don't hold. I've tagged the test no-shared-merge-tree (verified it now skips under --replace-replicated-with-shared); the plain MergeTree cases still cover the fix. @Algunenano @hanfei1991 could you run private CI on #110535 before merging to confirm?

pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Jul 24, 2026
…in MOVE/REPLACE PARTITION

Reintroduces ClickHouse#103881 (reverted in ClickHouse#110526 because it broke the private CI).
The regression test now carries the no-shared-merge-tree tag so its
ReplicatedMergeTree assertions are not run under the Cloud SharedMergeTree
substitution, which routes partition moves through a different code path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alexey-milovidov pushed a commit to amosbird/ClickHouse that referenced this pull request Sep 2, 2026
…ranularity for non-adaptive parts

A non-adaptive data part stores no per-mark row counts on disk: its mark
file holds only the two offsets per mark, so the rows per granule have to
be reconstructed when the part is loaded, and they are reconstructed from
the READING table's `index_granularity`.

`MergeTreeData::canReplacePartition` is the only compatibility gate on the
partition-manipulation path, and it compared mark adaptivity only. With
both tables non-adaptive (`index_granularity_bytes = 0`) neither adaptivity
branch fires, so a part written at `index_granularity = N` was admitted
into a table declared with `index_granularity = M != N`.
`MergeTreeDataPartWide::loadIndexGranularityImpl` then paired the source's
mark offsets with the destination's rows per mark, mapping every mark to
the wrong row range.

Observable consequences, both measured on 8627fa2:

- Release build: the part attaches and the table disagrees with itself.
  For 18 rows moved from `index_granularity = 8` into
  `index_granularity = 4`, `SELECT count() FROM t WHERE <primary key
  condition>` returned 10; in the other direction (4 into 8) it returned
  24, while `SELECT count() FROM t` returned the correct 18.
- Debug and sanitizer builds: the consistency check in
  `IMergeTreeDataPart::loadRowsCount` throws `LOGICAL_ERROR`, which those
  builds turn into a server abort.

The new condition is placed outside the `enable_mixed_granularity_parts`
block on purpose: a destination that accepts mixed granularity still
reinterprets a non-adaptive part, because `changeGranularityIfRequired`
clears `mark_type.adaptive` and `index_granularity_bytes` from the
filesystem but leaves `fixed_index_granularity` at the destination's
value. That case was measured to abort on master too, and it is the only
arm of the new test that distinguishes this placement from putting the
check inside the block.

Comparing against the source part's own `fixed_index_granularity` is
sound because `index_granularity` is read-only after table creation
(`MergeTreeSettings::isReadonlySetting`, enforced as `READONLY_SETTING`
in `MergeTreeData::checkAlterIsPossible`), so for any part the source
table wrote itself the setting is the value the part was written with.

`MergeTreeDataPartCompact` needs no equivalent guard: a Compact part is
always adaptive and its marks record per-granule row counts, so nothing is
reconstructed. Verified that a non-adaptive table cannot write a Compact
part even with `min_bytes_for_wide_part` and `min_rows_for_wide_part` set
to 1e9.

The condition only ever adds rejections, so it cannot make a previously
rejected operation succeed. Previously accepted DDL now errors with
`BAD_ARGUMENTS`; the accepted cases produced a part whose marks map to the
wrong rows unless the partition fitted in a single granule.

The new test arms live in the existing
`04150_move_partition_inconsistent_granularity`, which carries
`no-shared-merge-tree`: in Cloud `ReplicatedMergeTree` is substituted by
`SharedMergeTree`, whose partition-move path does not call
`canReplacePartition`. Dropping that tag is what got ClickHouse#103881 reverted.

The arms cover both directions of the mismatch and all of
`REPLACE`/`MOVE`/`ATTACH PARTITION FROM`, so a one-sided comparison does
not satisfy them. `r_src_nonadaptive` pins `index_granularity = 8192`,
its default, because `clickhouse-test` otherwise injects a random value
as the CREATE TABLE default and a draw equal to the destination's 4096
would remove that arm's expected rejection.

One arm asserts an acceptance instead: an adaptive source part at a
different `index_granularity` is still admitted, because its marks carry
per-granule row counts and the destination's setting never enters their
interpretation. It pins the scope of the new condition, so dropping the
`!adaptive` conjunct would be caught by the test rather than silently
rejecting a valid transfer. That arm pins its source part to the `Wide`
format, which is the format whose granularity load path holds the defect,
and without which restating the conjunct as a `Wide` part-type test would
also pass.

Reported and root-caused by Pedro Ferreira.

Found by the server-side AST fuzzer under Stress test (arm_debug) on
ClickHouse#116783 :
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116783&sha=dd8310bb0d928a3aa50822f052cc835cf86efac0&name_0=PR&name_1=Stress%20test%20%28arm_debug%29

Closes ClickHouse#117524
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…in MOVE/REPLACE PARTITION

Reintroduces ClickHouse#103881 (reverted in ClickHouse#110526 because it broke the private CI).
The regression test now carries the no-shared-merge-tree tag so its
ReplicatedMergeTree assertions are not run under the Cloud SharedMergeTree
substitution, which routes partition moves through a different code path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…4063-3b45-move-partition-bad-args

Throw BAD_ARGUMENTS, not LOGICAL_ERROR, for incompatible granularity in MOVE/REPLACE PARTITION
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.

5 participants