Skip to content

Do not convert a column a pending mutation drops - #118585

Merged
alexey-milovidov merged 12 commits into
masterfrom
readd-column-after-drop-conversion-116416
Oct 11, 2026
Merged

alexey-milovidov merged 12 commits into
masterfrom
readd-column-after-drop-conversion-116416

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Dropping a column and re-adding one of the same name but another type made every query touching that column
fail with 44 ILLEGAL_COLUMN as long as a part still physically carried the old column, that is until the
drop's column-removal mutation had rewritten it:

CREATE TABLE demo (id UInt64, val UInt64, c UInt64) ENGINE = MergeTree ORDER BY id
SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO demo SELECT number, number, number FROM numbers(1000);

SYSTEM STOP MERGES demo;   -- only widens the window; it exists on its own between the DDL and the mutation
ALTER TABLE demo DROP COLUMN c SETTINGS alter_sync = 0;
ALTER TABLE demo ADD  COLUMN c UInt32 SETTINGS alter_sync = 0;

SELECT count(), sum(c) FROM demo;
-- Code: 44. DB::Exception: Illegal column UInt32 of first argument of function toUInt32:
--   while executing 'FUNCTION _CAST(c, 'UInt32')'
SELECT * FROM demo ORDER BY id LIMIT 1;         -- also raises
SELECT count(), sum(id), sum(val) FROM demo;    -- 1000, 499500, 499500: the rest of the part is readable

Both statements are metadata-only, so the part keeps the old column file while the metadata already reports
the new column. IMergeTreeReader::performRequiredConversions then saw a column whose type in the part
differs from the requested one and converted it - building the conversion for UInt64 and handing it the
UInt32 default of the new column.

A column that a pending mutation drops is not read from the part at all: the readers skip it and the value
comes from the current metadata, in the requested type, so there is nothing to convert. The conversion is now
skipped for such a column, which is what a read of the rewritten part already does, so the answer no longer
changes when the mutation materializes.

Projection parts are read without the parent part's AlterConversions, so a projection could still serve the
old column (after DROP PROJECTION p, DROP COLUMN c, ADD COLUMN c UInt32, ADD PROJECTION p): a normal
projection returned the old values, an aggregate one failed with CANNOT_CONVERT_TYPE. A part that still has a
pending RENAME COLUMN / DROP COLUMN mutation touching a column the projection holds or the query reads (for
RENAME COLUMN, the old or the new name) is now read from the parent part (with its AlterConversions) instead
of its projection part; other parts, and parts whose pending mutations touch only unrelated columns, keep using
the projection.

Closes: #116416

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 to CHANGELOG.md):

Fixed ILLEGAL_COLUMN ("Illegal column ... of first argument of function to...") raised by every query
touching a column that was dropped and re-added with a different type, for as long as a part still carried
the old column. The re-added column now reads as its default, as it does once the drop's mutation rewrites
the part. Queries served from a projection no longer return the old values of such a column.


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

Version info

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

Dropping a column and re-adding one of the same name but another type made every query touching that
column fail as long as a part still physically carried the old column, that is until the drop's mutation
had rewritten it:

    ALTER TABLE t DROP COLUMN c;        -- c UInt64 is still in the part
    ALTER TABLE t ADD COLUMN c UInt32;  -- a new column, absent from the part
    SELECT count(), sum(c) FROM t;
    -- Code: 44. Illegal column UInt32 of first argument of function toUInt32:
    --   while executing 'FUNCTION _CAST(c, 'UInt32')'

Both statements are metadata-only, so the part keeps the old column file while the metadata already
reports the new column. `performRequiredConversions` then saw a column whose type in the part differs
from the requested one and converted it - building the conversion for `UInt64` and handing it the
`UInt32` default of the new column. `SELECT *` and every query naming the column raised, while the other
columns of the same part were readable.

A column that a pending mutation drops is not read from the part at all: the readers skip it and the
value comes from the current metadata, in the requested type, so there is nothing to convert. The
conversion is now skipped for such a column, which is also what a read of the rewritten part does - the
answer no longer changes when the mutation materializes.

Closes: #116416

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [77b2fe0]

Summary: ✅


AI Review

Summary

This PR fixes the stale-read path for DROP COLUMN followed by re-adding the same name, and it narrows projection fallback from a table-wide ban to a per-part check. The DROP COLUMN path looks covered, but the new RENAME COLUMN projection fallback still does not make the advertised rename case correct when the old name is reused before the rename materializes.

Findings
  • ❌ Blockers
    • [src/Processors/QueryPlan/Optimizations/projectionsCommon.cpp:398] The new RENAME COLUMN fallback assumes that switching from the projection part to the parent part is enough, but the parent reader still only remaps new -> old names (src/Storages/MergeTree/IMergeTreeReader.cpp:411) and only suppresses stale files for DROP COLUMN (src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp:115). After DROP PROJECTION p, RENAME COLUMN c TO d SETTINGS alter_sync = 0, ADD COLUMN c UInt32 SETTINGS alter_sync = 0, ADD PROJECTION p (...), a forced-projection query falls back to the parent part and still reads the old physical c for the new c, returning stale values instead of the new column's default. The merge path already has a dedicated “re-added while the rename is pending” guard (src/Storages/MergeTree/MergeTask.cpp:810), but the read path has no matching check.
    • Suggested fix: treat a reused rename-source name as stale on the parent read path, the same way the drop/re-add case is handled today, and add a focused regression that forces the projection path across RENAME COLUMN old TO new followed by ADD COLUMN old.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: fix the parent-read handling for pending RENAME COLUMN ...; ADD COLUMN <old_name> ... so the new projection fallback returns the re-added column's default rather than the old physical data, and cover that sequence with a forced-projection regression.

LLVM Coverage Report

Measured on commit 77b2fe0.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 92.20% +0.40%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 51/52 (98.08%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Sep 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 77b2fe06b0b744e5d7116d6785463ff551f59d98 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

alexey-milovidov and others added 2 commits September 26, 2026 10:23
…on is pending

A projection part is read without the parent part's `AlterConversions`, so it
still carries a column that a pending `DROP COLUMN` removed. After
`DROP COLUMN c, ADD COLUMN c UInt32` (and the projection dropped and re-added),
a query served from a normal projection returned the old values of `c`, and an
aggregate projection failed with `CANNOT_CONVERT_TYPE`. Reject projections in
`canUseProjectionForReadingStep` while metadata mutations are pending, like the
statistics min/max shortcut already does.

Addresses the review comment on the base-table reader fix.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 AI Review blocker addressed in 91ca27d: canUseProjectionForReadingStep now rejects both normal and aggregate projections while hasMetadataMutations() is true. This covers optimizeUseNormalProjection and optimizeUseAggregateProjection. The new test 05258_readd_column_after_drop_projection goes through DROP PROJECTION → DROP COLUMN → ADD COLUMN → ADD PROJECTION. DROP COLUMN of a column that a projection references is refused, so this sequence is the reachable path. Without the fix, the normal projection returned the old rows and the aggregate one failed with CANNOT_CONVERT_TYPE.

mergeTreeProjection is intentionally left as is. It exists to read the physical projection parts, and it already ignores on-the-fly mutations: pending data mutations are not applied there either, and it rejects them only under a row policy. It is never picked implicitly. A related but separate pre-existing issue is that a stale projection part is still attached to an old part after DROP PROJECTION p + ADD PROJECTION p under the same name. That is about reusing the projection name, not about this column fix.

@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Sep 26, 2026
alexey-milovidov and others added 2 commits October 3, 2026 04:46
…arent, not its projection

The previous gate rejected all projections whenever the mutations snapshot had any metadata
mutation. Once one is pending, the snapshot holds every mutation still in
`current_mutations_by_version`, including finished ones, so projections (and the implicit
`_minmax_count_projection`) were refused even for parts the mutation had already rewritten,
breaking `force_optimize_projection` in `04546_ttl_delete_where_recalc_after_update`,
`04653_mutation_rewrite_stale_part_column_type`, `04008_skip_empty_columns_lifecycle` and
`05059_minmax_count_projection_after_alter_column`.

Decide per part instead: a part whose on-fly mutation commands contain `RENAME COLUMN` /
`DROP COLUMN` is read from the parent part (with its `AlterConversions`), other parts may still
use the projection part. The same rule applies to projection-index filtering.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118585&sha=91ca27d4e399c23428036291a338109dcf809960&name_0=PR&name_1=Fast%20test
PR: #118585

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The table-wide gate from 91ca27d broke force_optimize_projection in four Fast tests (04546, 04653, 04008, 05059). Once any DROP/RENAME COLUMN mutation is pending, the mutations snapshot also holds finished mutations, so projections were refused even for parts that had already been rewritten. cce8de5 replaces it with a per-part check in analyzeProjectionCandidate and projection-index filtering: only a part whose on-fly commands contain RENAME COLUMN / DROP COLUMN falls back to the parent read. I also merged master.

alexey-milovidov and others added 2 commits October 4, 2026 23:53
…s touching its columns

`partHasPendingMetadataMutations` rejected the projection part for any pending `DROP COLUMN` /
`RENAME COLUMN` on the part, even on a column neither the projection nor the query reads, which lost
projection pruning (and made `force_optimize_projection` fail with `PROJECTION_NOT_USED`) until the
mutation materialized. Now only mutations touching a column the projection holds or the read needs
(for `RENAME COLUMN`, both the old and the new name) send the part to the parent read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118585&sha=latest&name_0=PR&name_1=Upgrade%20check%20(amd_release) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The Upgrade check (amd_release) error is UNKNOWN_IDENTIFIER (Unknown expression identifier b) from a retried background mutation, the same as #120975. It shows up in the upgrade check of many unrelated PRs every day; #118499 is still open.

@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

This is #120975. At cce8de59 all 153 lines of upgrade_error_messages.txt are the Code: 47 from mutating all_1_1_1 of test_5.t_stale_part_type_5 (the empty DETACH PART tombstone of 04653, loaded Active after the restart), repeated from 09:00:30 to 09:01:01 until cleanup logged Will drop empty part all_1_1_1.

The fix is #118499 (mine, open). It now conflicts with master on one adjacent line in src/Storages/MergeTree/MergeTreeMutationStatus.h (against 8db4dc2); I am merging master into it.

A `RENAME COLUMN` waits for a pending `DROP COLUMN` mutation, so with merges stopped the
test hung. Check the pending drop and the pending rename of an unrelated column on two tables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Oct 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into master with commit 407fa1a Oct 11, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the readd-column-after-drop-conversion-116416 branch October 11, 2026 02:01
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 12, 2026
…ition cache

ClickHouse#119337 added `hasPendingAlterOrMetadataMutations`, which stops reading and writing the
query condition cache while an alter or metadata mutation is pending for one of the read
parts. It covers what `hasPendingMetadataMutations` was added for (a pending `DROP COLUMN`
or `RENAME COLUMN` that `KILL MUTATION` can leave unmaterialized), and it ignores the
finished mutations the snapshot keeps for `system.mutations`, which made our snapshot-level
check disable the cache after any `DROP COLUMN` until the mutation was evicted. So drop
`hasPendingMetadataMutations` from the read side and both write sides.

Its last user was `isQueryConditionCacheAllowed`, which also checked it because projection
analysis reads projection parts with an empty mutations snapshot. Since ClickHouse#118585 a part with a
pending metadata mutation on a column the projection holds or the read needs is read from the
parent part, so projection analysis does not consult the cache for it, and the getter is back
to master's form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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

3 participants