Skip to content

Fix stale subcolumns left behind by ALTER TABLE ... RENAME COLUMN - #122527

Merged
PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-rename-column-moves-subcolumns
Oct 4, 2026
Merged

PedroTadim merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-rename-column-moves-subcolumns

Conversation

@groeneai

@groeneai groeneai commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

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

After ALTER TABLE ... RENAME COLUMN, the subcolumns of the renamed column (for example c0.null or a.size0) stayed resolvable under the old name. Reading them through a Buffer or Merge table, or using them in OPTIMIZE ... DEDUPLICATE BY, read uninitialized memory, and once a new column reused the old name they returned wrong values instead of throwing UNKNOWN_IDENTIFIER.

Description

On 26.3.33.24, after ALTER TABLE t RENAME COLUMN c0 TO c1 and ALTER TABLE t ADD COLUMN c0 UInt64, SELECT c0.null FROM t returns 1 for a row with c0 = 42 (Memory and MergeTree) instead of UNKNOWN_IDENTIFIER.

CI failure: BuzzHouse (amd_msan), STID 3520-32eb, MemorySanitizer: use-of-uninitialized-value in ColumnsDescription::tryGetColumn (ColumnsDescription.cpp:877) called from StorageBuffer::read (job, report).

Root cause: ColumnsDescription::rename re-keyed the column but left its subcolumns registered under the old storage name. hasSubcolumn, tryGetColumn and tryGetColumnDescription find such a name and dereference the parent lookup, which is end(). When a new column reuses the old name, the stale entries (with the old types) become phantom subcolumns of it.

Fix: rename removes the column's subcolumns before re-keying it and registers them again under the new name. It is the only mutator that left the subcolumn index out of sync with the columns, so no lookup site needs a guard.

Validation: the new stateless test fails on master (both serverError lines, and MSan aborts at :877) and passes with the fix. Same-tree MSan builds are clean with the fix for the Buffer, Merge and OPTIMIZE ... DEDUPLICATE BY shapes. 50/50 randomized runs pass, and 118 other RENAME COLUMN tests plus 37 DeltaLake tests give identical results with and without the fix.

Not addressed here: the Memory engine does not rename the columns of blocks stored before a RENAME COLUMN.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1536-master (included in 26.10 and later)

groeneai and others added 2 commits September 27, 2026 10:52
ColumnsDescription::rename re-keyed the column in `columns` but left its
subcolumns registered in `subcolumns` under the old storage name. After
ALTER TABLE ... RENAME COLUMN c0 TO c1, `c0.null` stayed registered without
a parent, and hasSubcolumn, tryGetColumn and tryGetColumnDescription then
dereference columns.find("c0") == end(). MemorySanitizer reports that as
use-of-uninitialized-value (STID 3520-32eb, BuzzHouse amd_msan, reached from
StorageBuffer::read); a Merge table over the renamed table and
OPTIMIZE ... DEDUPLICATE BY reach the same lookups. When a column with the
old name is added again, the stale entries become phantom subcolumns of it:
on 26.3, SELECT c0.null returns 1 for a non-NULL UInt64 c0.

Remove the subcolumns before modify_key (column_from may refer to the
element's own name, as in the DeltaLake caller) and register them again under
the new name when the column had any, so ALIAS columns and descriptions built
with add_subcolumns = false stay unchanged. If modify_key fails, the element
is erased together with its subcolumns.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ALTER TABLE ... RENAME COLUMN re-adds every column's subcolumns after
the rename, so SQL cannot observe that rename itself moves them. Check
that the old subcolumns are gone, the renamed column has its own, and
an ALIAS column gains none, with the element's own name as column_from.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Sep 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding in the full pass, 0 in the delta recheck of the fix).

# Sev Finding Verdict Evidence / action
1 ⚠️ The SQL test cannot see the re-registration of the subcolumns under the new name, because ALTER re-adds every column's subcolumns after the rename (05265_rename_column_subcolumns.sql) AGREE, fixed @ 0d1bc6e Added ColumnsDescriptionTest.RenameMovesSubcolumns to gtest_columns_description.cpp. It fails on the pre-fix code and when the re-registration, its ALIAS condition, or the remove-before-rename order is removed.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20260927-115400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05265_rename_column_subcolumns fails on every run of a pristine master build (both serverError lines), and an MSan build aborts every time at ColumnsDescription.cpp:877.
b Root cause explained? ColumnsDescription::rename re-keyed the column and left its subcolumns registered under the old storage name. The subcolumn lookups then dereferenced columns.find(<old name>) == end(), and a column that reuses the old name inherits the stale entries as phantom subcolumns.
c Fix matches root cause? Yes. rename moves the subcolumns with the column. It is the only mutator that broke the index invariant, so no guard was added at the lookup sites.
d Test intent preserved / new tests added? New stateless test: the Buffer read (the CI shape) plus phantom-subcolumn assertions, plus a unit test for the re-registration under the new name, which SQL cannot observe. Every stateless-test assertion is red before the fix; the renamed-column line is a positive control.
e Both directions demonstrated? Runner: base [ FAIL ], fix [ OK ]. MSan: the CI binary and a same-tree base build both abort at :877, and the same-tree fix build is clean. Deleting removeSubcolumns turns the test red again. The unit test fails on the pre-fix code and under three mutations (no re-registration; re-registration without the ALIAS condition; removal after the re-key).
f Fix is general across code paths? All rename callers checked (ALTER apply/validate, MutateTask, DeltaLake). Buffer, Merge and OPTIMIZE ... DEDUPLICATE BY are clean under MSan with the fix. 118 existing tests from the RENAME COLUMN family and 37 DeltaLake tests are unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Measured for Nullable, Array, Tuple, Map, String and Variant: the renamed columns keep their subcolumns, and all 8 old names are rejected. ALIAS columns register no subcolumns and are unchanged.
h Backward compatible? (maintainer-approved exception only) Yes. The change touches the in-memory index only; there is no setting, format or on-disk metadata change.
i Invariants and contracts preserved? The invariant "every subcolumn has a live parent" is restored. A failing modify_key (element erased) leaves no stale entries. column_from may alias the element's own name, so it is not read after modify_key. The cache is invalidated last.

Session id: cron:clickhouse-impl-slot-40:20260927-092800

@groeneai
groeneai requested review from Avogar and CurtizJ September 27, 2026 12:21
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ea391a5]

Summary: ✅


AI Review

Summary

This PR keeps ColumnsDescription's subcolumns index in sync across ALTER TABLE ... RENAME COLUMN by removing a renamed column's old subcolumn registrations and re-registering them under the new storage name. After reading the current diff, the touched ALTER validation/apply paths, and the existing discussion, I do not see a remaining contract violation in the patch; the added stateless test covers both the stale-old-name and renamed-new-name behaviors that the bug report promises to fix.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit ea391a5.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 91.80% -0.30%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 8/9 (88.89%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). labels Sep 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ea391a585 with master 37a251ffa (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 0d1bc6e

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 3/6) / group_uniq_array #7 dashboard confirmed_regression, client_time 0.0697 -> 0.0856 s (+22.8%), while the shard's own report calls the query success (6 too long only). The dashboard scored it without master history: its C3, C4 and C5 modules are unavailable, so the bar was its 2% floor, and its own flakyLately record for this query holds 72 flags in both directions over 14 days. The query is groupUniqArray(toString(number % 4096)) over numbers(5000000) and reads no table, while the only product change in this PR is ColumnsDescription::rename the question for exactly this class (rows the dashboard scores without master history) is posted to its owner: #119556 (comment). Either the dashboard restores the history, or I send the in-tree fallback described there
Mergeable Check, PR rollups, red only because of the row above the same owner question as the row above: #119556 (comment)

All 172 check runs completed (130 pass, 39 not selected, 3 red: the row above and its two rollups),
Finish Workflow is green and CH Inc sync passed. CIDB has no FAIL or ERROR test row on this
head. The shard's 6 too long is informational: in master_head mode only run errors and dashboard
confirmed_regression rows fail a shard.

Session id: cron:our-pr-ci-monitor:20260927-173207

}
}

TEST_F(ColumnsDescriptionTest, RenameMovesSubcolumns)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@groeneai remove the gtest when there is a sql test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 04995668e17b. The SQL test 05265_rename_column_subcolumns stays, and I dropped the unit-test sentence from the description.

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 6b916d6

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build (amd_fuzzers) / Build ClickHouse not from this PR: the clickhouse_fuzzer link fails with relocation R_X86_64_PC32 out of range; the same job is red on master and on 166 PRs in the last 24h #123738 (external, merged 2026-10-04 01:14Z). This workflow was dispatched at 20:13Z, before that merge, so the fix is not in the tree that ran; a master merge to pick it up follows
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05218_timeseries_alp_codec not from this PR: a replica resolves the TimeSeries target as default.ts_alp32 (UNKNOWN_TABLE); 32 PRs in 24h #123763 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 03008_optimize_equal_ranges not from this PR: the test's per-step counters (sum 1 0 instead of sum 1 1) do not hold when the read is split across replicas; 29 PRs in 24h #123695 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 05257_sql_security_view_sealed not from this PR: a replica cannot run the SQL SECURITY DEFINER view's read as the definer, so the row policy check fails with ACCESS_DENIED; 88 PRs in 24h #123729 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) / 04266_text_index_tokens_cardinality_order not from this PR: the replicas' Reading tokens log lines reach the initiator and land in the output; 24 PRs in 24h #123688 (mine, open)
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) / 05237_with_expression_alias_scope_descendant not from this PR: under a Replicated database the replicated CREATE VIEW stores the nested WITH alias qualified with the database, so the view reads the same-named table (view 10 instead of 100); 180 PRs in 24h #123790 (external, open)
Upgrade check (amd_release) / Error message in clickhouse-server.log not from this PR: the 26.9 server this check upgrades from wedges its DDL queue on the KILL PART_MOVE_TO_SHARD ON CLUSTER entry of 05082, and the upgraded server replays the backlog, so a replayed CREATE TABLE ... none hits TABLE_ALREADY_EXISTS #122132 (mine, merged); it needs its 26.9 backport, requested on #122132
Stateless tests (amd_debug, distributed plan, s3 storage, parallel) infra: the runner received a shutdown signal 66 s into the job, while it was still pulling the test image, so no test ran and the job published no result a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR rollups, red only because of the rows above #123738, #123763, #123695, #123729, #123688, #123790, #122132 and the fix task above

None of these touches ColumnsDescription::rename, and 05265_rename_column_subcolumns passed everywhere it ran. The three arm_binary lanes above come from #122853, which #123789 reverted on master at 00:14Z; #123790 re-lands them. All 157 checks completed and CH Inc sync passed; CIDB has no other FAIL or ERROR on this head.

Session id: cron:our-pr-ci-monitor:20261004-013156

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 3f385b8

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Fast test / 05218_text_index_substreams_opened_lazily not from this PR: a concurrent test drops a MergeTree table in a database without table UUIDs, which clears every server cache between the test's warm-up and its measured query, so a FileOpen threshold flips; it failed on 7 PRs and 2 master runs in the last 6h #123823 (mine, open)
Mergeable Check, PR rollups, red only because of the row above #123823 (mine, open)

The Fast test failure dropped the other 102 jobs (Dropped due to previous failure), and Finish Workflow / Post Hooks is the praktika post hook reporting that; it carries no separate defect. A master merge to re-run the dropped jobs follows. CH Inc sync passed; CIDB has no other FAIL or ERROR on this head.

Session id: cron:our-pr-ci-monitor:20261004-063241

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 4, 2026
Merged via the queue into ClickHouse:master with commit 796d295 Oct 4, 2026
149 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 4, 2026
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 comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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.

3 participants