Repository navigation
Fix ALTER TABLE ... PARTITION for a partition key containing modulo - #116606
alexey-milovidov merged 2 commits into
Conversation
Any `... PARTITION <value>` statement against a table whose `PARTITION BY`
contains `modulo` with an unsigned left operand and a signed right operand
raised a LOGICAL_ERROR, which aborts a debug or sanitizer build:
CREATE TABLE t (c0 Int32) ENGINE = MergeTree ORDER BY tuple()
PARTITION BY (37528 % c0);
INSERT INTO t VALUES (167682982);
ALTER TABLE t DROP PARTITION 37528;
-- Parsed partition value: 37528 doesn't match partition value for an
-- existing part with the same partition ID: 37528_1_1_0
`MergeTreePartition::adjustPartitionKey` rewrites every `modulo` in the
partition key to `moduloLegacy` and re-derives the key description, so that
partition values stay reproducible across the historical change of `modulo`'s
result type. `ResultOfModuloLegacy` takes the result signedness from either
operand while `ResultOfModulo` takes it from the left operand alone, so for an
unsigned left and a signed right operand the two disagree: `37528 % c0` with
`c0 Int32` is `UInt32` under `modulo` and `Int32` under `moduloLegacy`.
A part's partition value is produced with the adjusted key, on all three of the
insert, store and load paths, but `getPartitionIDFromQuery` parsed the user's
value with the unadjusted one. `getID` renders `UInt64` and `Int64` identically,
so the partition ID matched and the existing part was found, while
`Field::operator==` is type strict, so the values compared unequal and the
consistency check fired. `Field::safeGet` is deliberately lenient between
`Int64` and `UInt64`, which is why nothing else noticed the confusion.
Parse with the adjusted key instead. `partition.getID` keeps using the
unadjusted block, exactly as `MergeTreeDataWriter` does, so no partition ID
changes.
Two further consequences of the same cause are fixed by this. Once the value no
longer fits eight bytes the ID is a hash that covers the `Field` type tag, so
the computed ID never existed and `ALTER ... DROP PARTITION` silently dropped
nothing. And when the adjusted type is narrower than the unadjusted one, a value
outside its range used to convert silently and address a partition that cannot
exist; it now raises ARGUMENT_OUT_OF_BOUND, matching
`04207_drop_partition_integer_overflow_date`.
`tryParseValueFromID` gets the same treatment. It classifies by the default
`Field` of the key type, so the unadjusted key makes it build an unsigned value
where the part holds a signed one, and its own check compares the rendered ID,
which is identical either way, so it cannot catch this.
The thrown message named the part twice: the second placeholder was the part
name while the text promised a partition value, which is why the failure read as
comparing 37528 against 37528_12_23_2. It now prints both values and the part.
`createEmptyPartInsteadOfLost` reconstructs the partition value of a lost part from its partition id when the lost part was the last active part in its partition, and that call was the second site changed in this branch. It shipped without a test on the grounds that a stateless test cannot fabricate a lost part. That reason rules out one venue rather than the scenario: the same rule routes part-fabricating scenarios to `tests/integration/`, where `test_lost_part` already reaches this branch and already ships the helper that removes a part from disk. Add `test_lost_last_part_modulo_partition_key` there. With the fix reverted at that one call site the test fails with the original logical error on `ALTER TABLE mt_mod DROP PARTITION 37528`, so the arm is load-bearing rather than decorative: the empty part enters the active set holding a `UInt64` value, `tryParseValueFromID`'s own self-check cannot notice because the rendered partition id is identical either way, and the mismatch only surfaces later when a partition manipulation parses `Int64`. Two details decide whether the test measures anything. It asserts by value rather than by id, because the by-id spelling resolves a partition without parsing a value and so never reaches the check. And it compares log line counts across the action instead of calling `contains_in_log`, which answers "has this ever appeared" and is therefore already satisfied by a previous run of the same test; measured over ten repeats, the predicate form reached the lost-part path in six of them while reporting ten passes. Also drop the wide-key table in the stateless test as soon as it is no longer needed, so that no later query in the file reads its `system.parts` row, and correct a comment that described the control arms by operand signedness when what they share is the result signedness.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex), with a separate reviewer who never
Severity: ❌ blocker / Independently re-derived rather than taken on trust: no partition ID can change, because Session id: cron:clickhouse-review-slot-9:20260826-214700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260826-200900 |
|
cc @hanfei1991 @tavplubix, could you review this? |
|
Workflow [PR], commit [cf0677a] Summary: ✅
AI ReviewSummaryThis PR fixes Missing context / blind spots
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 12/17 (70.59%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 66 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - cf0677aEvery failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link will be posted here when it opens. GitHub dispatched two
Session id: cron:our-pr-ci-monitor:20260827-033000 |
|
Fixing PR for the |
|
@groeneai Can you check that it is backward compatible and there are no part names created in older version that will not be accessible after the fix? Also check that parts after this fix will be readable once you rollback on older version. If not sure add both as integration tests. |
|
Measured both directions. Short answer: no part name changes, and nothing written after the fix becomes unreadable on rollback, because the change touches two parse sites and nothing on the write path. Binaries: official 25.8.34.11, 25.10.8.2, 26.3.30.11, 26.8.3.25, this PR's own CI release build (26.9.1.1), and the unfixed master build at the same 26.9.1.1 as a same-version control. Six partition keys: Old parts keep their names and become addressable. All six writers produce identical part names and partition IDs, and
Identical for all four releases. Rollback. For all six tables the whole part directory is byte-identical between the unfixed and the fixed 26.9.1.1 build, so there is nothing version-specific to break. A directory written by the fixed binary reads back on 26.8.3.25 and 26.3.30.11 with the same part names and rows, and rolling forward again still works. Going back as far as 25.10.8.2 or 25.8.34.11 detaches the parts as One deliberate delta. Where the adjusted type is narrower, a value outside it now raises Not measured cross-version. I left the two cross-version cases out of the suite because the part bytes are unchanged, so the test would cover general part-format compatibility rather than anything this change can regress. Happy to add both with |
ed4d31c
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
... PARTITION <value>statements against a table whosePARTITION BYcontainsmodulowith an unsigned left operand and a signed right operand, such asPARTITION BY (37528 % c0)withc0 Int32. Such a statement raisedLOGICAL_ERROR, or silently affected no partition. A value outside the range of the partition key type is now rejected withARGUMENT_OUT_OF_BOUNDinstead of silently matching nothing.Description
A
... PARTITION <value>statement against a table whosePARTITION BYcontainsmodulowith an unsigned left operand and a signed right operand raisesLOGICAL_ERROR, aborting a debug or sanitizer build:Root cause:
adjustPartitionKeyrewritesmodulotomoduloLegacy, whose result type is signed if either operand is signed rather than only the left, so37528 % c0withc0 Int32isUInt32undermoduloandInt32undermoduloLegacy. A part's value uses the adjusted key;getPartitionIDFromQueryparsed the query's with the unadjusted one.getIDrendersUInt64andInt64identically, so the ID matched and the part was found, whileField::operator==is type strict.I parse with the adjusted key instead.
partition.getIDkeeps the unadjusted block, exactly asMergeTreeDataWriterdoes, so no partition ID changes, andtryParseValueFromIDgets the same treatment. Two siblings of the same cause are fixed too: past eight bytes the ID hashes theFieldtype tag, so the statement silently dropped nothing; and where the adjusted type is narrower, an out-of-range value silently matched nothing and now raisesARGUMENT_OUT_OF_BOUND.Both tests pass with the change; the stateless one aborts on the parent commit, 50/50 randomized and 50/50 pinned, and the integration one reddens when only the
tryParseValueFromIDhunk is reverted.SELECT partition FROM system.partsstill throwsBAD_GETpast eight bytes; that read-side site needs an API change and is out of scope.Found by CI, no issue filed:
BuzzHouse (amd_debug), STID 3993-3f59, https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115630&sha=11816184dbcf3b8ffec3ae07bcad615625c4581c&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29 . STID 3993-6336 (amd_msan, 12 days earlier) is the same defect.Closes #116737
Workflow [PR]
Sync PR [sync-upstream/pr/116606]
Version info
26.9.1.867(included in26.9and later)