Skip to content

Allow ALTER of column type when its key subcolumns are unchanged - #113862

Merged
Avogar merged 3 commits into
masterfrom
alter-column-key-subcolumns-92808
Aug 12, 2026
Merged

Avogar merged 3 commits into
masterfrom
alter-column-key-subcolumns-92808

Conversation

@Avogar

@Avogar Avogar commented Aug 7, 2026

Copy link
Copy Markdown
Member

Closes: #92808
Related: #112302

ALTER MODIFY COLUMN was forbidden for any column whose subcolumns are used in the primary or partition key, even when the ALTER did not change those subcolumns (e.g. adding an unrelated typed path to a JSON column, adjusting a parameter, or SKIP-ing a path, while the key subcolumn stays the same).

This relaxes the check to mirror how top-level key columns are handled: the ALTER is allowed as long as every key subcolumn keeps an on-disk-compatible type (isSafeForKeyConversion), and is rejected only when a used key subcolumn changes unsafely. Other subcolumns of the column may change freely. A mutation does not re-sort data, so a key subcolumn must convert the same order-preserving way a top-level key column does; the mutation then rebuilds primary.idx from the new data.

Similar subcolumn-level checks are added for the lazy JSON type-hint ALTER path in #112302; this PR does the same for the regular (mutation) ALTER path and should be merged after it.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Allow ALTER TABLE ... MODIFY COLUMN of a column whose subcolumns are used in the primary or partition key, as long as the subcolumns used in the key keep an on-disk-compatible type. Previously any such ALTER was forbidden.

ALTER MODIFY COLUMN was forbidden for any column whose subcolumns are
used in the primary or partition key, even when the ALTER did not change
those subcolumns. Relax the check: allow the ALTER as long as every key
subcolumn keeps an on-disk-compatible type (isSafeForKeyConversion) - the
same rule already applied to top-level key columns - and reject it only
when a used key subcolumn changes unsafely. Other subcolumns of the
column may change freely.

Closes: #92808

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5cfc428]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Received signal 5 (signal sent by pid 0 from user 0) (STID: 4062-3832) FAIL cidb IGNORED
Stress test (arm_asan_ubsan, s3) FAIL
Logical error: 'std::exception. Code: 1001, type: std::out_of_range, e.what() = unordered_map::at: key not found (version 26.8.1.1), Stack trace: (STID: 2508-3132) FAIL cidb, issue ISSUE CREATED

AI Review

Summary

This PR relaxes ALTER TABLE ... MODIFY COLUMN for columns whose subcolumns participate in the primary or partition key, allowing the ALTER when each key subcolumn keeps an on-disk-compatible type. The earlier undefined-behavior issue around removed key subcolumns is fixed, but the relaxation is still not safe to merge: one previously reported derived-state corruption path is still open, and the same newly-allowed ALTERs can also leave stored MATERIALIZED columns stale.

Findings

❌ Blockers

  • [src/Storages/MergeTree/MergeTreeData.cpp:5443-5470; src/Interpreters/MutationsInterpreter.cpp:1295-1348; src/Interpreters/MutationsInterpreter.cpp:1560-1617; src/Storages/StorageInMemoryMetadata.cpp:408-443] The new rule says other subcolumns of the altered storage column may change freely, but the mutation code still tracks the rewritten name as t while skip-index, projection, and TTL dependency matching uses exact required names like t.b. That means a newly-allowed ALTER such as ORDER BY t.a, INDEX i t.b TYPE minmax can rewrite t and hardlink stale skip-index / projection / TTL state into the new part. Suggested fix: normalize every required subcolumn back to its storage column for READ_COLUMN dependency discovery and rebuild checks, or keep rejecting alters when a changed non-key subcolumn participates in persisted secondary state.
  • [src/Storages/MergeTree/MergeTreeData.cpp:5443-5467; src/Interpreters/MutationsInterpreter.cpp:732-804] Stored MATERIALIZED columns are only collected for recomputation when updated_columns is populated from UPDATE assignments, so a READ_COLUMN type rewrite introduced by this ALTER path never reaches that analysis. A table like ORDER BY t.a, m String MATERIALIZED toTypeName(t.b) can now accept MODIFY COLUMN t ... changing only t.b, rewrite t, and hardlink the old bytes for m, leaving the part inconsistent. Suggested fix: include READ_COLUMN alters in the materialized-dependency analysis, with the same subcolumn-to-storage-column normalization, or reject alters when a changed non-key subcolumn feeds a stored MATERIALIZED column.
Tests
  • ⚠️ The new stateless coverage proves the key-subcolumn check itself, but it does not exercise the newly allowed derived-state surfaces. Please add a focused case where the key uses t.a, a skip index / projection / TTL / stored MATERIALIZED column uses t.b, and MODIFY COLUMN t ... changes only t.b, then verify the mutation rebuilds that derived state instead of hardlinking stale files.
Final Verdict

❌ Not ready. The key-subcolumn safety check is improved, but the new allowance still leaves persisted derived state inconsistent after valid MODIFY COLUMN mutations.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.10% +0.10%

Changed lines: Changed C/C++ lines covered: 30/50 (60.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 7, 2026
…mn is unchanged

The `my.tuple` ALTER only adds an unrelated element `b` while the key subcolumn
`my.tuple.a` stays the same, which is now allowed. The JSON case above stays
forbidden because its key subcolumn `my.json.b` changes type.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
In the key-subcolumn safety check, the new subcolumn could be absent when an
ALTER removes it (e.g. drops a Tuple element). Relying on chassert made this
undefined behavior in release builds. Replace it with an explicit runtime check
that throws ALTER_OF_COLUMN_IS_FORBIDDEN, and add a regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@KochetovNicolai KochetovNicolai self-assigned this Aug 12, 2026
@KochetovNicolai

Copy link
Copy Markdown
Member

Looks good, but please check the AI Review

@Avogar

Avogar commented Aug 12, 2026 •

Copy link
Copy Markdown
Member Author

but please check the AI Review

They are not actionable in this PR. The mentioned code in mutations is fixed in #112717

@Avogar
Avogar added this pull request to the merge queue Aug 12, 2026
Merged via the queue into master with commit a4c75ae Aug 12, 2026
178 of 181 checks passed
@Avogar
Avogar deleted the alter-column-key-subcolumns-92808 branch August 12, 2026 15:29
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

Allow to modify column type if its subcolumns are used in the key expression but are not affected by this alter.

3 participants