Repository navigation
Conversation
… of old parts `ALTER ... ADD COLUMN k, MODIFY ORDER BY (a, k)` is accepted because parts written before it do not store `k` and read it as the type default, a constant, so they stay sorted by `(a, k)`. `checkProperties` enforces this for the same ALTER (a newly added key column may not have a default), but a later `ALTER MODIFY COLUMN k DEFAULT|MATERIALIZED <expr>` was not checked: readers of the old parts then compute `k` per row from `<expr>`, which the parts were never sorted by. `SELECT ... ORDER BY a, k` returns unsorted rows (with and without `optimize_read_in_order`), and every merge of such a part aborts in `CheckSortedTransform` on debug and sanitizer builds (release builds write a part that is not sorted by its own key). Test 03263 builds exactly this state with `MODIFY COLUMN b DEFAULT rand64() % 100000`, which is where the Float64 `Sort order of blocks violated` stress aborts come from. `checkAlterEligibility` (initial query only) now refuses a MODIFY COLUMN that changes what a sorting key column, other than the columns every part stores since CREATE (primary key and partition key columns, and the engine's sign, version and is_deleted columns), computes from its DEFAULT or MATERIALIZED expression, unless that expression is deterministic and reads only bare primary key columns. Such an expression is constant over each run of equal primary key values, which is what keeps the old parts sorted. The engine's columns are named in the engine arguments, must exist at CREATE, cannot be dropped or renamed, and `MODIFY ORDER BY` adds only columns added by the same ALTER, so one of them in the sorting key is in every part. Float, Dynamic, Variant, Object (`comparisonCanMergeDistinctValues`, now shared from SortDescription) and Nullable primary key columns are excluded, because rows that compare equal on them can still differ. The check looks at the final metadata of the ALTER, and also fires when a column in the expression's dependency closure changes (an ALIAS, an EPHEMERAL or a DEFAULT the reader evaluates on the way), as `evaluateMissingDefaults` follows that closure. ALTERs that do not touch the closure are not refused, so a table an older version left in the bad state stays alterable, and `MODIFY COLUMN k REMOVE DEFAULT` stays allowed. Constant defaults (ClickHouse#13592's workaround) and primary-key-determined defaults (04626) stay accepted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove a comment that narrated the ALIAS-dependency filter; it follows from the ExpressionSources::columns doc. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 rounds, 3 findings) plus my own cold review.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261006-124300 |
|
Workflow [PR], commit [91eb395] Summary: ✅
AI ReviewSummaryThe PR validates changes to sorting-key column defaults so readers of old parts preserve their order. At Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block. Make the old-default exemption sound, preserve a compatibility path for safe existing migrations, and add the two focused regressions. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+46.12 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units843 translation units recompiled, 6841 s compile time in total, 843 of them have a recent master baseline. |
…fore it A column that is in the sorting key but not in the primary key may now get a DEFAULT or MATERIALIZED expression that reads the bare sorting key columns listed before it, not only the primary key columns. Rows with equal values of those columns are adjacent in every part, so such an expression is constant over them and the parts that compute the column stay sorted. With `ORDER BY (a, b, k)` where `b` and `k` were added later, `MODIFY COLUMN b DEFAULT a` and then `MODIFY COLUMN k DEFAULT b` is now accepted. The position matters: a column after it, or one outside the sorting key, is still refused even when its own default reads only primary key columns, because parts that do not store `k` can store that column with any values (`k DEFAULT x` with `x DEFAULT a` returns `(1,30), (1,10)` on master for a part written before `x` got its default). Expanding the expression through the defaults of the columns it reads would therefore accept unsorted parts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…efault-of-sorting-key-column
| for (const auto & key_column : new_metadata.getColumnsRequiredForSortingKey()) | ||
| { | ||
| const String column_name = name_in_storage(key_column); | ||
| if (stored_in_every_part.contains(column_name) || !checked.insert(column_name).second) |
There was a problem hiding this comment.
[P1] Preserve safe default changes on existing sorting-key columns
This also rejects valid changes when k has been stored in every part since CREATE TABLE. For example:
CREATE TABLE t (a UInt32, k UInt32 DEFAULT x, x UInt32)
ENGINE = MergeTree ORDER BY (a, k) PRIMARY KEY a;
INSERT INTO t (a, k, x) VALUES (1, 10, 30), (1, 20, 10);
ALTER TABLE t MODIFY COLUMN k UInt32 DEFAULT x + 1;The baseline accepts this: existing rows keep their stored k, and subsequent inserts are sorted with their newly computed values. Here, k is excluded from stored_in_every_part, its allowed inputs contain only a, and the last statement throws ALTER_OF_COLUMN_IS_FORBIDDEN. This breaks existing schema migrations without any compatibility setting or other way to retain the safe behavior. The PR description acknowledges the broader rejection, but there is no migration path for these tables. Please distinguish columns that can actually be absent, grandfather existing definitions, or provide a compatibility escape, with a focused regression test for this case.
There was a problem hiding this comment.
In 5f3ba91, a column whose previous expression already breaks the rule, like this k DEFAULT x, can change again (test arm t_s).
…p the order The check refused every change that leaves a non-primary-key sorting key column with an expression that does not keep old parts sorted. It cannot tell a column that MODIFY ORDER BY added from one that is in the sorting key since CREATE, so it also refused changes like `MODIFY COLUMN k DEFAULT x + 1` for `k DEFAULT x` in `ORDER BY (a, k) PRIMARY KEY a`, and `MODIFY COLUMN x DEFAULT 5`, which existing schemas may need. Refuse a change only when the expression kept the order before it. A column added by MODIFY ORDER BY starts without a default and every later change is checked, so its expression never stops keeping the order; one that already does not is from CREATE, where every part stores the column, or from an older version, whose parts without the column are already computed from such an expression. Checking the parts instead was considered and rejected: a part without the column can still arrive after the check (ATTACH PARTITION FROM, a detached part, another replica). Co-Authored-By: Claude Opus 5.5 <[email protected]>
ATTACH PARTITION FROM compares the sorting, primary and partition keys of the two tables but not their engines, so a part of a plain MergeTree table that does not store a column can enter a ReplacingMergeTree, CollapsingMergeTree or VersionedCollapsingMergeTree table in which that column is the version, is_deleted or sign column. A later MODIFY COLUMN v DEFAULT x then made that part unsorted, because those columns were exempt from the check. Only primary key and partition key columns are stored in every part: no ALTER adds them, and ATTACH PARTITION FROM requires the same keys. A column is no longer let through for an old expression that already breaks the rule unless the sorting key reads it only as the bare column. The rule judges the whole column, so `t DEFAULT tuple(a, x)` breaks it while the key element `t.p` stays sorted, and so does `k DEFAULT x * 2` for `k % 2`; changing them to `tuple(x, x)` and `x` was accepted and unsorted old parts. The test now stops merges while it reads computed values, so a background merge cannot store a column under an intermediate default. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@CurtizJ, one design choice here should be yours. The check only proves that an expression keeps the order, so an old expression that fails it may still keep it, like CREATE TABLE src (a UInt32, x UInt32) ENGINE = MergeTree ORDER BY a;
INSERT INTO src VALUES (1, 30), (1, 10);
ALTER TABLE src ADD COLUMN k UInt32, MODIFY ORDER BY (a, k);
CREATE TABLE dst (a UInt32, x UInt32, k UInt32 DEFAULT x - x) ENGINE = MergeTree ORDER BY (a, k) PRIMARY KEY a;
ALTER TABLE dst ATTACH PARTITION tuple() FROM src;
ALTER TABLE dst MODIFY COLUMN k UInt32 DEFAULT x;
SELECT a, k FROM dst ORDER BY a, k; -- (1,30), (1,10)Which do you prefer?
I lean to (A). |
| continue; | ||
|
|
||
| /// Refusing to change an expression that already breaks the rule, like one from CREATE, protects nothing. | ||
| if (!in_key_not_bare.contains(column_name) && !keeps_order(old_metadata, old_identifiers_before, column_name)) |
There was a problem hiding this comment.
[P1] Do not treat an unproved old default as already unsorted
keeps_order provides a sufficient condition for preserving order, so its returning false does not establish that the old parts were already unsorted. For example, DEFAULT x - x is constant but fails this predicate. I reproduced the following with the fast-test binary for 91eb39507087:
CREATE TABLE src (a UInt32, x UInt32) ENGINE = MergeTree ORDER BY a;
INSERT INTO src VALUES (1, 30), (1, 10);
ALTER TABLE src ADD COLUMN k UInt64, MODIFY ORDER BY (a, k);
CREATE TABLE dst (a UInt32, x UInt32, k UInt64 DEFAULT x - x)
ENGINE = MergeTree ORDER BY (a, k) PRIMARY KEY a;
SYSTEM STOP MERGES dst;
ALTER TABLE dst ATTACH PARTITION tuple() FROM src;
SELECT a, k FROM dst ORDER BY a, k; -- (1,0), (1,0)
ALTER TABLE dst MODIFY COLUMN k UInt64 DEFAULT x;
SELECT a, k FROM dst ORDER BY a, k; -- (1,30), (1,10)The ALTER reaches this continue, creating the unsorted-read failure from a previously ordered state. This confirms the unresolved case in the author's design question. Please remove this exemption or base it on evidence that establishes the stored-column invariant, and cover this attachment case with a regression test.
Related: #71777
Related: #121943
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER TABLE ... MODIFY COLUMNnow rejects giving a sorting key column that is not in the primary key aDEFAULTorMATERIALIZEDexpression that is not deterministic or reads more than the sorting key columns before it (a constant is fine), unless its previous expression already did. Parts written before such a column was added to the sorting key withMODIFY ORDER BYdo not store it and compute it from that expression, soSELECT ... ORDER BYreturned unsorted rows and merges of those parts failed withSort order of blocks violated.Description
On 26.3.42.3 and 26.8.18.2,
ALTER TABLE t MODIFY COLUMN k DEFAULT x + yon a column added to the sorting key byMODIFY ORDER BYmakesSELECT ... ORDER BY a, kreturn unsorted rows:MODIFY ORDER BYis accepted only because old parts read the new column as a constant, andcheckPropertiesforbids a default for it in the same ALTER, but a laterMODIFY COLUMNwas not checked.checkAlterEligibilitynow refuses changing what such a column computes from a deterministic expression of the bare sorting key columns before it that are not Float, Nullable or dynamic to anything else. Only primary and partition key columns are exempt: every part stores them.Metadata cannot tell such a column from one in the sorting key since
CREATEwith a shorterPRIMARY KEY, so a column whose expression already breaks the rule (k DEFAULT x) can still change if the key reads it as is, while one that follows it cannot get a breaking one. Checking the parts is not sound: one can arrive viaATTACH PARTITION FROM, a detached part or a replica.03263_forbid_materialize_sort_keybuilt this state, and its merges are the Float64Sort order of blocks violatedstress failures, e.g. Stress test (amd_debug).Workflow [PR]
Sync PR [sync-upstream/pr/124234]