Repository navigation
Conversation
… old names `StorageMemory` reads the columns of its stored blocks by name, and `alter` changed only the metadata. After `RENAME COLUMN` the new name read defaults, a column added later with an old name read the old data (or failed with Code 349), multi-command ALTERs mixed up columns, `UPDATE` of a renamed column failed with NOT_FOUND_COLUMN_IN_BLOCK, and dropped columns stayed in memory. `alter` now replays the commands on a copy of the metadata to get the ordered renames and drops, so the blocks follow the metadata exactly (`DROP COLUMN IF EXISTS` of a `Nested` is a no-op there). Under the lock that INSERTs commit with, it converts a copy of the blocks before `alterTable` commits the new definition, and publishes data, then metadata, after it. A block left without columns gets a default-filled column, so it keeps its row count. Such an ALTER is logged with the new metadata version, and an INSERT or a RESTORE that captured older metadata converts its blocks when it commits. A read whose metadata snapshot is older than the blocks it captured throws UNFINISHED instead of reading defaults. BACKUP takes the metadata and the blocks together, and RESTORE checks the column names of `columns.txt` against the table; a restore whose names cannot be verified fails if a rename or drop ran during it. RENAME COLUMN has been allowed for Memory since ClickHouse#56892. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A backup written by a release without the rename fix has `columns.txt` with the new names but blocks under the old ones, and a restore with `allow_different_table_def` appends columns the table does not have. Such a column was kept, so a later `ADD COLUMN` with its name read the old data again. `restoreDataImpl` now drops every restored column the table does not have, after converting the blocks for the ALTERs that ran during the restore; a block left without columns gets the fill column and keeps its rows. The fill column rule moves to `chooseFillColumn`, shared by `alter` and the restore. The fill column is compressed when the table has `compress = 1`, like the other stored columns: a block that lost all its stored columns kept an uncompressed column (100000 UInt64 rows: 800128 bytes instead of 3148). Co-Authored-By: Claude Opus 5.5 <[email protected]>
`StorageMemory::alter` updated the runtime settings only when every command of the ALTER was a settings command (`AlterCommands::isSettingsAlter` is `all_of`). `ADD COLUMN b UInt64, DROP COLUMN a, MODIFY SETTING compress = 1` therefore wrote `compress = 1` into the metadata, while the fill column built by the same ALTER and every later INSERT stayed uncompressed (100000 UInt64 rows: 800128 bytes instead of 3148), and a mixed `max_rows_to_keep` never removed rows. The settings commands are now detected with `any_of`, and the new settings and the eviction they cause are staged together with the converted blocks and published after `alterTable`, like the conversion. Publishing the eviction before it would drop rows on a mixed ALTER that `alterTable` then rejects (`DROP COLUMN` of the last physical column fails with `EMPTY_LIST_OF_COLUMNS_PASSED`). The eviction runs on the converted blocks, which is the state after the ALTER. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s empty A block that loses all its stored columns gets a fill column, and when that is an empty `Tuple()` the block keeps its rows but takes 0 allocated bytes. So after `ADD COLUMN e Tuple()` and `DROP COLUMN a`, `BACKUP` saw `totalBytes == 0` and wrote no data (a `RESTORE` brought back 0 rows), and `RESTORE` saw the table as empty and appended to it. A table created with only `Tuple()` columns had the same problem. Both checks now use the row count. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… was taken `BackupEntriesCollector::run` writes the table definitions first and then collects the data, holding only a shared table lock, which an `ALTER` does not wait for. Since `alter` now renames and drops the stored columns, an `ALTER` between the two stages gave a backup whose `CREATE` has the old columns while `columns.txt` and `data.bin` have the new ones, and the `RESTORE` column check of this change refused it with `CANNOT_RESTORE_TABLE`. A `Replicated` database replica with a lagging DDL queue does the same without any concurrent `ALTER`, because its definitions come from Keeper. The collector now keeps the definition it wrote for each data path (`getTableCreateQuery`), and `StorageMemory::backupData` throws the retryable `INCONSISTENT_METADATA_FOR_BACKUP` when the stored columns of that definition (everything except ALIAS and EPHEMERAL) differ from the metadata it backs up. The new fail point `backup_pause_before_collecting_table_data` pauses between the two stages for the test. 05296 runs a `RENAME COLUMN` in that pause, 05295 backs up and restores a table with MATERIALIZED, ALIAS, EPHEMERAL and Nested columns, and checks that an insert after a mixed `ALTER` with `MODIFY SETTING max_rows_to_keep` uses the new setting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A materialized view backs up and restores its inner table under its own data path, so the definition that `BACKUP` compared with the data of an inner `Memory` table was the view's, and `RESTORE` compared the backup's `columns.txt` with an inner table created from the view's columns. After an `ALTER` of the inner table itself, which the view follows only at the next `MODIFY QUERY`, the view could not be backed up, and a backup of it written by a release could not be restored. The same strict `RESTORE` check refused backups that a release wrote across a concurrent `ALTER`, which restore on master. The collector now keeps the definitions per storage, so an inner table has none of its own. `RESTORE` restores the blocks by name when their names differ from the table, and fails only if a column of the table was renamed or dropped. `BACKUP` also compares the column types of the definition, so a concurrent `MODIFY COLUMN` fails the backup instead of leaving a definition older than the data. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the comments on the ALTER publication order and on the RESTORE column name check, and drop one that restated the type comparison. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 11 findings over six rounds) plus my own cold review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20260929-191300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260929-173500 |
|
Workflow [PR], commit [dffc4a4] Summary: ❌
AI ReviewSummaryThis PR fixes a real cluster of Findings❌ Blockers
Final Verdict❌ Changes requested — the two blockers above still need to be fixed before this is merge-ready. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes19 object files changed (+98.35 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units36 translation units recompiled, 290 s compile time in total, 36 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…amed A RENAME COLUMN on the inner table of a materialized view alone (the view follows it only at MODIFY QUERY) leaves a backup whose blocks store the column under its new name, while RESTORE creates the inner table with the columns of the view. The restore dropped the stored column as one the table lacks and read the view's column as default values: `SELECT x` with `x` renamed to `y` in `.inner_id.*` restored 4 rows with sum(x) = 0 instead of 6. Master restores 6 only because its RENAME leaves the blocks under the old name. When the table has a column that the backup's columns.txt lacks, a stored column that columns.txt lists but the table lacks now fails the RESTORE with CANNOT_RESTORE_TABLE instead of being dropped; allow_different_table_def restores by name as before. A mismatch in one direction still restores: a column added to the inner table is dropped (the view does not have it) and a column dropped from it is restored as default values. The check looks at the stored blocks, so backups written by releases, whose blocks keep the names from before a concurrent RENAME, still restore. Co-Authored-By: Claude Opus 5.5 <[email protected]>
RESTORE read data.bin with one NativeReader, which takes its header from the first block of the index and converts every later block to it: a column the first block lacks is skipped and a type that differs throws. The blocks of a Memory table differ whenever an ALTER ran between inserts, so on 26.3 and 26.8 a column added between two inserts restored as default values (sum 5 -> 0) and a MODIFY COLUMN between them made RESTORE fail with TYPE_MISMATCH. With the renamed blocks of this PR it also dropped the rows a materialized view inserted after `RENAME COLUMN x TO y, ADD COLUMN x` on its inner table (x = [0,0,0,4] restored as [0,0,0,0]), and the check for a possibly renamed column saw only the first block's names. Each block now gets its own reader over its index entry. The Memory-specific tests are tagged memory-engine: the stress runner replaces Memory with MergeTree in a share of its runs, and tagged tests are skipped there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
When a DROP COLUMN leaves a stored block with no columns, the block keeps its rows through a fill column filled with the type's default. If every remaining physical column had a DEFAULT or MATERIALIZED expression, that is not what reading the rows means: `CREATE TABLE t (a UInt8) ENGINE = Memory; INSERT 1; ADD COLUMN b UInt8 DEFAULT 7; DROP COLUMN a` stored and read b = 0. The same fill is used for the blocks of an INSERT that commits after such an ALTER and for a RESTORE whose stored columns are all unknown to the table. In that case the block now gets all physical columns, computed with `addMissingDefaults` as an INSERT that omits them computes them, so b = 7, and the stored columns agree with each other (`t DEFAULT tuple(3)`, `m MATERIALIZED t.x + 4` store t.x = 3, m = 7; storing only one of them read the other as its type's default). The actions are built once per ALTER, commit or restore and only when a block needs them, so a default that cannot be computed fails only an ALTER that needs it, before anything is changed. Non-deterministic defaults (`now()`, `rand()`) get the values of that computation, as in an INSERT. If a physical column without a default expression exists, only it is stored, with the type's default, as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| const auto physical_columns = columns.getAllPhysical(); | ||
| auto fill_column = std::find_if(physical_columns.begin(), physical_columns.end(), | ||
| [&](const auto & column) { return !columns.hasDefault(column.name); }); | ||
| if (fill_column != physical_columns.end()) |
There was a problem hiding this comment.
setFillColumn only switches to addMissingDefaults when every surviving physical column has a default or materialized expression. That still leaves wrong results for mixed schemas where one remaining column has no default and another depends on it. A concrete repro is:
CREATE TABLE t (x UInt8) ENGINE = Memory;
INSERT INTO t VALUES (5);
ALTER TABLE t ADD COLUMN a UInt8, ADD COLUMN b UInt8 DEFAULT a + 1;
ALTER TABLE t DROP COLUMN x;
SELECT a, b FROM t;For the pre-existing row, this path stores only a = 0; ReadFromMemoryStorageStep does not evaluate b's default later, so the row reads back as (0, 0) instead of (0, 1). The same hole exists in the restore path when dropping unknown backup columns empties a block. addMissingDefaults can already fill missing non-default columns with type defaults before computing dependent defaults, so this needs to keep the full ColumnsDescription whenever any surviving physical column has a default/materialized expression, not only when all of them do.
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@groeneai merge master |
…-drop-stored-columns # Conflicts: # src/Storages/StorageMemory.cpp
|
Merged master in 00874cf. The only conflict was in |
The master style rule failpoint_tests_are_isolated requires every stateless test that enables a fail point to be tagged both no-parallel and no-fasttest. 05296 arms restore_pause_before_data_restore_tasks and backup_pause_before_collecting_table_data and was tagged no-parallel only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| const String & metadata_path_in_backup = table_info.metadata_path_in_backup; | ||
| backup_entries.emplace_back(metadata_path_in_backup, std::make_shared<BackupEntryFromMemory>(new_create_query->formatWithSecretsOneLine())); | ||
| if (table_info.storage) | ||
| create_table_queries_by_storage[table_info.storage.get()] = new_create_query; |
There was a problem hiding this comment.
create_table_queries_by_storage is only populated for table_info.storage, i.e. the outer StorageMaterializedView object. But StorageMaterializedView::backupData delegates to its inner StorageMemory, so the delegated call reaches StorageMemory::backupData with getTableCreateQuery(*this) == nullptr and skips the new definition-vs-data consistency check entirely.
That still leaves a broken backup path for materialized views whose inner Memory table was altered directly. For example, after ALTER TABLE \` MODIFY COLUMN x Nullable(UInt64), BACKUP TABLE mvwritescolumns.txt/data.binfor the inner schema whileRESTORE TABLE mvrecreates the inner table from the view definition.restoreDataFromBackuponly compares names, not types, so this path can still reintroduce the mismatched-type restore bug that the newDataTypeFactorycheck fixed for ordinaryMemorytables. The same hole also means an inner-tableALTERracing the backup is rejected only later at restore time, not with the retryableINCONSISTENT_METADATA_FOR_BACKUP` promised here.
Please either key the collector map by the delegated inner storage as well, or plumb the view's create-query down explicitly when delegating the backup, and add a regression around backing up a materialized view after changing an inner Memory column type.
The insert branch of updateBlockData is removed: a column that a stored block lacks can be stored there only once ClickHouse#122954 removes dropped and renamed columns from the stored blocks. DROP COLUMN and RENAME COLUMN leave the stored data of a Memory table in place, so a value stored for an added column would come back after DROP COLUMN and a new ADD COLUMN of the same name. Until then, such an UPDATE is rejected with NOT_FOUND_COLUMN_IN_BLOCK, as on master. The test is renamed to 05301_memory_update_after_modify_column, loses the ADD COLUMN arms and gains a MATERIALIZE COLUMN arm on a MATERIALIZED column. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@alexey-milovidov this one is too large for me to review |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #56892
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ALTER TABLE ... RENAME COLUMNandDROP COLUMNon aMemorytable: the rows inserted before theALTERwere read as default values under the new column name, and a column added later with an old name returned the data of the renamed or dropped column. AMODIFY SETTINGcombined with other commands in oneALTERof aMemorytable now applies to the table right away, not only to its stored definition.RESTOREof aMemorytable no longer loses a column added between two inserts, or fails after aMODIFY COLUMNbetween them.Description
On 26.9 (and 26.3),
RENAME COLUMN c0 TO c1on aMemorytable makesSELECT c1return the default for every row inserted before it:Memoryreads the columns of its stored blocks by name, andStorageMemory::alterchanged only the metadata. The same stale names returned the data of a dropped column when its name was added again, mixed up the columns of a multi-commandALTER, brokeUPDATEof a renamed column, and kept dropped columns in memory.Now
alterrenames and drops the columns of the stored blocks, under the lock thatINSERTs commit with, beforealterTablecommits the new definition. AnINSERTor aRESTOREthat started before theALTERconverts its blocks when it commits. A query racing such anALTERfails with the retryableUNFINISHED.BACKUPtakes the metadata and the blocks together and fails if the columns changed after it took the table definition;RESTOREreads each stored block with its own columns, drops columns the table lacks (backups of unfixed releases and inner tables of materialized views can have them), or fails if one may be renamed. Both check emptiness by rows.MODIFY SETTINGin anALTERwith other commands now changes the running table.Tests:
05295covers each shape above;05296runs anINSERT, aRESTOREand aBACKUPacross theALTER. Both fail on master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122954]