Skip to content

Fix wrong column type in part after compound RENAME COLUMN swap - #107064

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-103247-compound-rename-swap-type
Jun 18, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-103247-compound-rename-swap-type

Conversation

@groeneai

@groeneai groeneai commented Jun 10, 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):

Fix a compound ALTER TABLE ... RENAME COLUMN a TO b, RENAME COLUMN c TO a that reuses a freed column name (a "swap") between columns of different types. The materialized part recorded the wrong column type, so a later SELECT failed with Conversion between numeric types and IPv6 is not supported (or aborted on part load in debug builds).

Description

Reported in #103247 (cc @ den-crane). Reproducer:

CREATE TABLE ipv6_repro(id UInt32, ip UInt32)
ENGINE = MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 0
AS SELECT * FROM values((0, 3628718300), (1, 0));

ALTER TABLE ipv6_repro ADD COLUMN ip_v6 IPv6 AFTER ip SETTINGS mutations_sync = 2;
ALTER TABLE ipv6_repro UPDATE ip_v6 = toIPv6(IPv4NumToString(ip)) WHERE ip != 0 SETTINGS mutations_sync = 2;
ALTER TABLE ipv6_repro RENAME COLUMN ip TO ip_v4, RENAME COLUMN ip_v6 TO ip SETTINGS mutations_sync = 2;

SELECT * FROM ipv6_repro WHERE ip_v4 = 0 ORDER BY id;
-- Code: 48. Conversion between numeric types and IPv6 is not supported.

Root cause is in getColumnsForNewDataPart (src/Storages/MergeTree/MutateTask.cpp). When building the new part's column list, the target column ip (from RENAME ip_v6 TO ip) still finds a same-named source column ip (the old one being renamed away to ip_v4) and took its type (UInt32) instead of the renamed-from column ip_v6 type (IPv6). The .bin data files are renamed correctly, so the part ends up with the IPv6 column declared as UInt32.

On read this produces _CAST(UInt32, 'IPv6') (Code 48) on release builds, or an assertion on part load in debug builds (Column ip has rows count 8 ... but data part has 2 rows, because the 16-byte IPv6 values are read as 4-byte UInt32).

The kept-column branch previously special-cased only DROP a, RENAME c TO a (was_removed). The fix generalizes it to any column renamed into this name (renamed_columns_to_from), which also covers the swap, so both the type and the substreams come from the renamed-from source column. The table metadata was already correct; only the part's columns.txt type was wrong.

Added regression test 03990_alter_rename_column_swap_different_types covering the swap on both compact and wide parts, plus the existing DROP + RENAME into case to guard against regressing it. The existing 01278_alter_rename_combination test exercises the swap pattern but with all-Int32 columns, so the wrong type was coincidentally identical and the bug was invisible.

Version info

  • Merged into: 26.6.1.953-master (included in 26.6 and later)
  • Backported to: 26.3.33.146

A compound ALTER that reuses a freed column name across columns of
different types, e.g.

    RENAME COLUMN ip TO ip_v4, RENAME COLUMN ip_v6 TO ip

wrote the wrong type into the materialized part's columns.txt. In
getColumnsForNewDataPart (MutateTask.cpp), the new column "ip" (target
of "ip_v6 TO ip") found a same-named source column "ip" - the old one
being renamed away to "ip_v4" - and took its type (UInt32) instead of
the renamed-from column "ip_v6" type (IPv6). The data files were
renamed correctly, so the part ended up with the IPv6 column declared
as UInt32.

At read time this produces CAST UInt32 -> IPv6 (Code 48, "Conversion
between numeric types and IPv6 is not supported") on release builds, or
an assertion on part load in debug builds ("Column `ip` has rows count
8 ... but data part has 2 rows", because 32 IPv6 bytes are interpreted
as 8 UInt32 values).

The kept-column branch only special-cased DROP xxx + RENAME yyy TO xxx
(was_removed). Generalize it to any column renamed into this name
(renamed_columns_to_from), which also covers the swap, so the type and
substreams come from the renamed-from source column.

Closes: ClickHouse#103247

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. The exact reproducer from #103247 fails 100% of the time: after the compound RENAME ip TO ip_v4, RENAME ip_v6 TO ip (mutations_sync=2), any read of the part fails. On a debug build it aborts on part load (Column ip has rows count 8 ... but data part has 2 rows); on release it raises Code: 48 Conversion between numeric types and IPv6.
b Root cause explained? Yes. In getColumnsForNewDataPart the new column ip (target of RENAME ip_v6 TO ip) finds a same-named source column ip (the old UInt32 one being renamed away to ip_v4) and took its type. So the part's columns.txt recorded ip UInt32 while ip.bin actually holds the renamed IPv6 data. Reader then sees on-disk UInt32 vs metadata IPv6 and casts UInt32 to IPv6 (or, in debug, the 16-byte IPv6 values are counted as 4-byte UInt32 -> 8 rows != 2, assertion).
c Fix matches root cause? Yes. The kept-column branch only special-cased DROP a, RENAME b TO a (was_removed). The fix generalizes the condition to "any column renamed into this name" (renamed_columns_to_from.contains(it->name)), so type and substreams are taken from the renamed-from source column for the swap too. No symptom guard.
d Test intent preserved / new tests added? New test 03990_alter_rename_column_swap_different_types added: the swap on compact and wide parts (UInt32 <-> IPv6), plus the DROP a + RENAME b TO a case to guard the branch I generalized. Existing 01278_alter_rename_combination still passes (it used all-Int32 columns, so the wrong type was coincidentally identical and never caught this).
e Both directions demonstrated? Yes. Built debug at master fc73f46fae65. New test runs [ OK ] through clickhouse-test with the fix; the unfixed binary aborts (SIGABRT) on the same test SQL.
f Fix is general, not a narrow patch? Yes. The on-the-fly read path (AlterConversions) already handles the swap correctly, so the materialization path in getColumnsForNewDataPart was the only affected site. The fix covers all "rename into a freed name" shapes (DROP+rename and swap) in one branch rather than patching the IPv6 case specifically.

Session id: cron:clickhouse-worker-slot-5:20260610-191053

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @alesapin could you review this? It fixes a data-corruption bug in mutation part writing: a compound RENAME that swaps a column name between columns of different types (RENAME a TO b, RENAME c TO a) made getColumnsForNewDataPart record the renamed-away column's type for the new column, so the part's columns.txt ended up with the wrong type (IPv6 declared as UInt32), failing SELECT with Code 48 / aborting on part load. Closes #103247.

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fb9bbc7]

Summary: ✅


AI Review

Summary

This PR fixes getColumnsForNewDataPart so a compound ALTER TABLE ... RENAME COLUMN that reuses a freed name in MergeTree parts records the type and substreams from the renamed-from column instead of the same-named source column being renamed away. I found no remaining correctness issues in the current diff; the prior unmaterialized-column concern is addressed by the existing map invariant and the added wide/compact regression coverage.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 10, 2026
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
Adds regression coverage for the case raised in code review: ADD COLUMN
ip_v6 (never materialized in existing parts) followed by the compound
swap RENAME ip TO ip_v4, RENAME ip_v6 TO ip. Covered both as a
metadata-only rename and with a non-key UPDATE in the same ALTER that
forces the part to be rewritten by the mutation, so
getColumnsForNewDataPart actually processes the part while the
renamed-from column has no data.

In every case the new ip is correctly dropped from the part (the reader
default-fills it) and the old ip becomes ip_v4. These pass on the
current fix, confirming the unmaterialized swap does not regress.

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (updated for fb9bbc7)

This update is test-only (extends 03990_alter_rename_column_swap_different_types); the source fix in 360529a5 is unchanged.

# Question Answer
a Deterministic repro? Yes. Original swap reproducer from #103247 fails 100% on the old binary (read-time CAST UInt32->IPv6 Code 48 on release, part-load assertion on debug); fixed binary returns correct data. The new test scenarios (unmaterialized swap, with and without a part-rewriting UPDATE) run deterministically.
b Root cause explained? Yes. getColumnsForNewDataPart recorded the wrong type for the target of RENAME ip_v6 TO ip: it took the type of the same-named source column ip being renamed away (UInt32) instead of the renamed-from column ip_v6 (IPv6). Files were renamed correctly so columns.txt declared IPv6 data as UInt32.
c Fix matches root cause? Yes. The kept-column branch is generalized from was_removed (DROP a + RENAME b TO a) to renamed_columns_to_from.contains(it->name), so the swap also takes type and substreams from the renamed-from source column.
d Test intent preserved / new tests added? Yes. New test 03990 asserts wide+compact swaps with different types. This update adds the review-requested coverage: unmaterialized-column swaps (wide and compact) and a wide unmaterialized swap with a non-key UPDATE in the same ALTER that forces getColumnsForNewDataPart to rewrite the part with no ip_v6 data. All pass.
e Both directions demonstrated? Yes. Old binary: aborts/CASTs on the swap reproducer. New binary: correct results. The unmaterialized scenarios pass on the current fix and were verified on a real debug server.
f Fix is general, not a narrow patch? Yes. The condition covers DROP-into and swap renames uniformly. I also verified the review-raised "unmaterialized renamed-from column" case: the throw in that branch is not reachable for it (renamed_columns_to_from only maps to columns present in the source part; an unmaterialized column is erased by the pre-existing was_renamed branch). No source change was needed there; tests document the behavior. The on-the-fly read path (AlterConversions) already handled the swap, so the write path was the only buggy site.

Session id: cron:clickhouse-worker-slot-16:20260611-001100

@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.70% +0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 4/4 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Jun 18, 2026
@alexey-milovidov
alexey-milovidov merged commit 113564d into ClickHouse:master Jun 18, 2026
165 of 166 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 18, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Fix wrong column type in part after compound RENAME COLUMN swap

ClickGap verified: reproduced it with the script below on 26.5; tested 6 version(s) — 5 affected, 1 clean (see Affects); long-standing (not a recent regression); introducing PR not yet pinned.

Reproduction

-- WIDE parts required (min_bytes_for_wide_part = 0); compact parts read back fine.
CREATE TABLE ipv6_repro (id UInt32, ip UInt32)
ENGINE = MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 0
AS SELECT * FROM values((0, 3628718300), (1, 0));

ALTER TABLE ipv6_repro ADD COLUMN ip_v6 IPv6 AFTER ip SETTINGS mutations_sync = 2;
ALTER TABLE ipv6_repro UPDATE ip_v6 = toIPv6(IPv4NumToString(ip)) WHERE ip != 0 SETTINGS mutations_sync = 2;
ALTER TABLE ipv6_repro RENAME COLUMN ip TO ip_v4, RENAME COLUMN ip_v6 TO ip SETTINGS mutations_sync = 2;

SELECT id, ip_v4, ip FROM ipv6_repro WHERE ip_v4 = 0 ORDER BY id;
-- Buggy: Code: 48. Conversion between numeric types and IPv6 is not supported.
-- Fixed: 1	0	::

▶ Run on ClickHouse Fiddle

Bisect

  • Reproduces on every release line tested down to v24.1.8.22 (the oldest with a downloadable binary for this repro). The introducing change predates v24.1.8.22, so it can't be pinned from release binaries — this is a long-standing issue, not a recent regression. A maintainer with full git history can git blame the relevant file/line to find the exact change.

Affects

  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.5, 26.4, 26.3, 26.2, 26.1
  • Does NOT reproduce on: master
  • Backport needed: 26.5, 26.4, 26.3, 26.2, 26.1

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-mutations
Severity: P2

cc fix PR author: @groeneai


ClickGap · Finding: phase_d_pr107064

Triage metadata

@Diskein Diskein added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 24, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 24, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 24, 2026
PedroTadim added a commit that referenced this pull request Sep 25, 2026
Backport #107064 to 26.3: Fix wrong column type in part after compound RENAME COLUMN swap
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

8 participants