Repository navigation
Fix LOGICAL_ERROR on ALTER RENAME COLUMN combined with another command in one statement - #116063
Conversation
An implicit minmax index's name and column_names are both derived from its definition_ast by getIndexFromAST, and AlterCommands::apply re-derives them only after every command in the statement has been applied. The RENAME COLUMN branch replaced only the AST, so within one statement the record still described the old column on both keys, and the commands are applied in order against a single accumulating metadata copy. A following MODIFY SETTING therefore missed on its name-keyed dropImplicitIndicesForColumn and created a second index via its column_names-keyed addImplicitIndicesForColumn. Both records re-derived to the same name, and MergeTreeData::checkProperties rejected the duplicate with LOGICAL_ERROR: an exception in release builds, an abort in debug and sanitizer builds. Found by BuzzHouse (arm_asan_ubsan), STID 3219-3899, on an unrelated carrier PR: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114054&sha=052eff9243e13d2a88492f793dba153e14f8c119&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29 Assign the two derived keys in that branch. The name is the reserved prefix plus the new column name by construction. column_names is skipped for ALIAS columns: getIndexFromAST expands an ALIAS column's expression, gated on ColumnsDescription::hasAlias, so such an index's column_names holds the expansion's output name, which never mentions the alias and is therefore unaffected by the rename. Writing the new column name there instead would claim the index covers a column it does not, and stop it suppressing a same-named column's own implicit index. Two further symptoms of the same stale keys are fixed. (RENAME a TO b), (DROP COLUMN b) returned UNKNOWN_IDENTIFIER quoting auto_minmax_index_a, an index that no longer existed, because the name-keyed drop missed and the surviving index outlived its column; it now succeeds, matching the contract that implicit indices must not prevent ALTERs. (RENAME a TO b), (ADD COLUMN a) silently left the new column without an implicit index until the table was reloaded. Three pre-existing defects are deliberately left unchanged and pinned at baseline by the validation matrix, since none of them forms a duplicate name and each would require repairing an explicit index's cached keys mid-statement: an explicit minmax index on the renamed column still leaves a following ADD COLUMN of the old name unindexed until a reload; a statement mixing MODIFY SETTING with another command still resurrects an implicit index dropped by an earlier statement; and that refresh still drops an implicit index whose name a later column's ALIAS index shadows. Validated with 44 base-versus-fix probes covering every intra-statement reader, all six column kinds, ten type wrappers and the replicated path, plus three mutation controls showing each part of the change is load-bearing. Regression arms were added to 03748_default_minmax_indices_alter, which aborts the server without this change.
Internal second-model review (click to expand)Two independent cold review passes ran against this diff before publication. Both raised one
The first finding did falsify one clause of my draft changelog, which had claimed the Session id: cron:clickhouse-review-slot-9:20260824-063800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260824-054900 |
|
Workflow [PR], commit [11945f2] Summary: ✅
AI ReviewSummaryThis PR repairs the renamed column's own implicit minmax index inside Findings❌ Blockers
Tests
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
|
cc @al13n321 @Algunenano, could you review this? An implicit minmax index derives its |
| index.name = IMPLICITLY_ADDED_MINMAX_INDEX_PREFIX + rename_to; | ||
| /// For an ALIAS column the index covers the columns its expression expands to, | ||
| /// which the column's own name never appears in, so a rename does not affect them. | ||
| if (!metadata.columns.hasAlias(rename_to)) |
There was a problem hiding this comment.
This still leaves stale column_names on other implicit indices that expand through column_name. A concrete case is:
CREATE TABLE t
(
`value` Int32,
`al` Int32 ALIAS value > 0
)
ENGINE = MergeTree
ORDER BY tuple()
SETTINGS add_minmax_index_for_numeric_columns = 1;
ALTER TABLE t
(RENAME COLUMN value TO value2),
(ADD COLUMN value Int32);After the rename, auto_minmax_index_value is repaired here, but auto_minmax_index_al still carries column_names = {'value'} until the final reparse because the else branch only rewrites its definition_ast. StorageInMemoryMetadata::addImplicitIndicesForColumn uses index.column_names.front() == column.name to detect an existing minmax index, so the later ADD COLUMN value arm suppresses the new column's implicit index and the table finishes without auto_minmax_index_value.
To make the ADD COLUMN part of the fix hold for plain-column renames, the rename path needs to refresh any implicit index whose cached column_names still mention column_name, not only the renamed column's own index. A focused regression arm for the case above would catch it.
There was a problem hiding this comment.
I ran your fixture on this head and on pristine master. The outcome you predict happens on master, not here.
After the ALTER, with both implicit indices confirmed created first:
master cc7cb8c3 |
this head 7d9ccfa5 |
reload ground truth | |
|---|---|---|---|
auto_minmax_index_al |
value2 > 0 |
value2 > 0 |
value2 > 0 |
auto_minmax_index_value |
absent | value |
value |
auto_minmax_index_value2 |
value2 |
value2 |
value2 |
So the new value does get its implicit index, and the in-memory list already matches DETACH/ATTACH.
auto_minmax_index_al is not the suppressor. Delete the ALIAS column and run the same ALTER on (value Int32) alone: master still leaves the new value unindexed. The colliding key belongs to the renamed column's own index, which is the one this patch assigns.
The premise about the alias index's cached key is also inverted. Add a column named greater(value, 0) instead and it is the one that goes unindexed on both builds, because ReplaceAliasByExpressionMatcher runs before the sample_block that column_names is derived from, so an ALIAS expression index caches the expansion's output name rather than value. It cannot suppress a column called value. The in-code comment at StorageInMemoryMetadata.cpp:975-979 says otherwise and is wrong on that point.
Causally, removing just the column_names = {rename_to} line from this patch reproduces your predicted output exactly, while removing either the name assignment or the hasAlias guard leaves this arm green. That line is the repair.
I also tried the sharpest version of the general claim, al Int32 ALIAS value, where the expansion output name genuinely is value: master leaves the new column unindexed, this head indexes it.
Coverage exists: arm 5 of 03748_default_minmax_indices_alter.sql is (RENAME COLUMN c2 TO c13), (ADD COLUMN c2 Int32), the same reader, without the ALIAS column that plays no causal role.
On the suggested remedy, refreshing every implicit index whose cached column_names mention the old name was measured during design and dropped. Re-keying an index this branch does not own either mis-keys multi-column and alias indices, or re-analyses an ALIAS expression against intermediate column state and fails a valid statement with Code: 43.
No change here.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 11945f2CI is fully finished at this commit and no check failed, so no ledger row is owed. Evidence: 175 unique check runs (156 success, 19 skipped, 0 failures) with pagination complete Session id: cron:our-pr-ci-monitor:20260824-133010 |
d659d17
…ated lanes 05060_database_max_tables_rename_inner_tables, 03748_default_minmax_indices_alter and 04757_alter_modify_projection_settings fail the main pass of Stateless tests (amd_llvm_coverage, s3 storage, DBReplicated, parallel, 2/3) on every master commit, and the check still reports OK: 1023 whitewashed rows over 7 days (341 each, 100% of their rows on that lane, 0 red). The lane's DIAGNOSTICS stage reruns a failed test without --replicated-database, the rerun passes, the test is labelled flaky, and llvm_coverage jobs force flaky to OK. Two intended DatabaseReplicated refusals are involved. DatabaseReplicated::renameTable throws NOT_IMPLEMENTED (48) for a cross-database RENAME before any existence check, so all 7 assertion arms of 05060 get 48 where they expect 724 TOO_MANY_TABLES. validateReplicatedDatabaseSegments refuses one ALTER that mixes replicated and non-replicated command kinds with QUERY_IS_PROHIBITED (392), and it runs before every storage-level check, which is why 04757's mixed arm reports 392 instead of the 344 SUPPORT_IS_DISABLED it asserts. Applying the tag-vs-split rule of ClickHouse#121863, which tags a whole file when the refused statement is the file's subject and splits the refused minority out otherwise: - 05060: whole-file no-replicated-database tag. 7 of 7 arms are refused and the cross-database RENAME of a table group is the file's entire subject. Its siblings 04498_database_max_tables and 05042_rename_full_database_error_order are tagged in ClickHouse#121863 with the same reason string. - 03748: 4 of 14 assertion blocks are refused, split into 05244_default_minmax_indices_alter_rename_with_setting. A whole-file tag would have made a coverage regression permanent, because the file had 1303 clean rows on this lane until the week those four arms were added. - 04757: 1 of 8 error arms is refused, split into 05245_alter_modify_projection_fixed_granularity_guard. The settings-only arm stays in the parent: a lone MODIFY SETTING makes areNonReplicatedAlterCommands true, so it is not refused and reaches the storage guard that raises the SUPPORT_IS_DISABLED it expects. Every moved statement is carried over verbatim, so no assertion is weakened. A fixture rewrite was rejected for 03748 because the combined (RENAME COLUMN), (MODIFY SETTING) shape is itself the regression under test from ClickHouse#116063: a following settings ALTER is what re-derives the implicit index's name-keyed entries, so splitting the statement in two would have silently deleted that coverage. Both changed references were regenerated with clickhouse-test --record. The 03748 reference loses exactly the 19 lines of the moved blocks and nothing else; 04757's reference is byte-identical; 05245's is empty because its only statement is a serverError arm. Validated on a single-node Replicated rig with the master snapshot binary 26.10.1.1: before the change each test fails deterministically under --replicated-database (48, 392, 392) and passes without it; after it, the three tagged tests report SKIPPED with reason replicated-database while 03748 and 04757 pass on the lane for the first time. Misspelling a tag makes the test run and fail again with the original code, which proves the skip comes from the tag. 50 randomized runs of each test without the flag and 20 with it produced no failure other than the local Code: 36 Invalid time zone environment class. Related: ClickHouse#121863 Co-Authored-By: Claude Opus 5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
LOGICAL_ERROR: Index with name auto_minmax_index_<column> already existswhen a singleALTER TABLEstatement combinedRENAME COLUMNwith another command, such asMODIFY SETTING, on a table with implicit minmax indices (add_minmax_index_for_numeric_columnsand friends). Renaming a plain column also no longer leaves a followingDROP COLUMNfailing withUNKNOWN_IDENTIFIER, nor a followingADD COLUMNwithout its implicit index.Description
ALTER TABLE t (RENAME COLUMN a TO b), (MODIFY SETTING fsync_part_directory = 1)raised aLOGICAL_ERRORon any table with an implicit minmax index fora: an exception in release builds, an abort under debug and sanitizers. No related open issue; surfaced byBuzzHouse (arm_asan_ubsan), STID 3219-3899 (report).Root cause: an implicit index's
nameandcolumn_namesare derived from itsdefinition_ast, but theRENAME COLUMNbranch ofAlterCommands::applyreplaced only the AST, and both are re-derived only at the end of the statement. Commands apply in order against one accumulating metadata copy, so a later command read the stale record: theMODIFY SETTINGrefresh missed its name-keyed drop and created a second index on itscolumn_names-keyed add. Both re-derived to one name, whichcheckPropertiesrejects.The fix assigns those two fields where they go stale.
nameisauto_minmax_index_plus the new column name by construction.column_namesis skipped for ALIAS columns, whose index covers what the expression expands to and is unaffected by the rename.Two further symptoms are fixed for plain columns:
(RENAME a TO b), (DROP COLUMN b)returnedUNKNOWN_IDENTIFIERnaming an index that no longer existed, and(RENAME a TO b), (ADD COLUMN a)leftawithout one.Pre-existing defects here are unchanged and pinned at baseline, since each needs a cached key repaired on an index this branch does not own: an EXPLICIT minmax index on the renamed column, or an ALIAS column whose expression names its own output, leave a following
ADD COLUMNof that name unindexed until a reload; and mixingMODIFY SETTINGwith another command can resurrect an index dropped earlier, or drop one a later ALIAS index shadows.Regression arms were added to
03748_default_minmax_indices_alter, and validated with 44 base-versus-fix probes covering every intra-statement reader, all column kinds, ten type wrappers and replication.Workflow [PR]
Sync PR [sync-upstream/pr/116063]