Skip to content

Do not delete the data on a table_disk root when its CREATE TABLE is rejected - #132874

Open
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-table-disk-rejected-create-keeps-root
Open

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:fix-table-disk-rejected-create-keeps-root

Conversation

@groeneai

@groeneai groeneai commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #115958 (comment)

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

A CREATE TABLE of a MergeTree table that is rejected for a column named like a virtual column (for example _row_exists) no longer deletes the data already stored at the root of a disk with table_disk = 1, such as detached parts of another table: the definition is now refused before the table is constructed.

Description

On 26.8.21.12 (and 26.3.33.24), a CREATE TABLE with table_disk = 1 that is rejected for a column named _row_exists deletes the detached parts already stored at the root of its disk.

A table_disk root may hold only detached parts of another table, and a new table may be created there to adopt them. validateStorage checks the column names against the virtual columns only after StorageFactory has built the table, and undoes a rejected CREATE with IStorage::drop(). For MergeTree that is dropAllData(), which removes every detached part under the root (and, for ReplicatedMergeTree, the replica the constructor has just registered).

The check needs nothing from the constructed table, so I moved it into throwIfColumnsConflictWithVirtuals in StorageFactory (the code is unchanged) and call it from the MergeTree factory in CREATE mode, before the constructors, next to its other CREATE-only checks. A rejected definition now constructs nothing, so there is nothing to roll back. The interpreter keeps calling the same function for the other engines and for ATTACH; for those validateStorage is unchanged. The factory also re-checks the column types there, because a columnless ReplicatedMergeTree takes its columns from Keeper after StorageFactory checked them.

The same statements are refused as before, only earlier; ATTACH, server startup and RESTART REPLICA are not affected.

Validation: the new test fails on master (the detached part and its 3 rows are gone after the rejected CREATE, for MergeTree and ReplicatedMergeTree) and passes with the fix, 50/50 with randomized settings; removing the new call or limiting it to ATTACH makes it fail again.


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

groeneai and others added 2 commits November 19, 2026 04:12
…ructing it

`validateStorage` checked the column names against the virtual columns of the
engine only after `StorageFactory` had built the table, and undid a rejected
`CREATE` with `IStorage::drop()` (DROP TABLE semantics). The MergeTree
constructors have already taken the data path by then and, for
`ReplicatedMergeTree`, registered the replica in Keeper. With `table_disk = 1`
the data path is the disk root, which the constructor accepts while it holds
only detached parts of another table (adopting them is the documented
workflow), so `dropAllData()` deleted those parts. Reproduced on 26.3.33.24,
26.8.21.12 and master.

The check needs nothing from the constructed table: its inputs (the final
columns and `MergeTreeData::createVirtuals`) are known in the factory. Move the
loop of `validateVirtualColumns` into `throwIfColumnsConflictWithVirtuals` in
`StorageFactory` and call it from `registerStorageMergeTree` in `CREATE` mode
before the constructors, like the factory's other CREATE-only checks and
`throwIfUnreplicatedTableDataNotAllowed`. The reserved-name part uses the
interpreter's predicate (`allow_reserved_column_names` of the same context, not
for replayed definitions). The factory also runs
`checkAllTypesAreAllowedInTable` on the final columns, since a columnless
`ReplicatedMergeTree` takes them from Keeper after `StorageFactory::get`
checked the empty list; that was the only other check `validateStorage` could
reject a MergeTree table with.

A rejected definition now constructs nothing, so there is nothing to roll
back. `validateStorage` is unchanged and still covers the other engines and
`ATTACH`; ATTACH, startup and `RESTART REPLICA` never reach the new call.

Related: ClickHouse#115958 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rewrite the factory check's comment as a one-line invariant without the reference to InterpreterCreateQuery.

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

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

Pre-publication review: my own cold review plus an independent model (engine: codex); 3 findings, all nits, no fix round needed.

# Sev Finding Verdict Evidence / action
1 💡 The ReplicatedMergeTree case's Keeper count() prints 0 on master too AGREE Not a discriminator, kept as a leftover-registration guard; the detached-part list and the row count fail on master for both engines
2 💡 The factory's checkAllTypesAreAllowedInTable on Keeper columns has no reachable trigger AGREE Kept: it mirrors the interpreter's re-check (same guard), so validateStorage cannot reject a table built by the MergeTree factory
3 💡 Use Critical Bug Fix for a data-loss fix DISAGREE That category is chosen only on a maintainer's request; Bug Fix holds (reproduced on 26.8.21.12 and 26.3.33.24)

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

Session id: cron:clickhouse-review-slot-11:20261119-041501

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05830_table_disk_rejected_create_keeps_root_data fails on master every run (1/1 without and 3/3 with randomized settings): after the rejected CREATE, system.detached_parts is [] and the re-attached table has 0 rows, for MergeTree and ReplicatedMergeTree.
b Root cause explained? validateStorage checks virtual column names only after the MergeTree constructor has taken the table_disk root (and registered the replica in Keeper); its rollback is IStorage::drop(), i.e. dropAllData(), which deletes the detached parts of another table stored under that root.
c Fix matches root cause? Yes. The same check runs in the MergeTree factory in CREATE mode before the constructors, so a rejected definition constructs nothing and nothing is rolled back. No guard at the deletion site.
d Test intent preserved / new tests added? New stateless test with a MergeTree and a ReplicatedMergeTree case. No existing test changed.
e Both directions demonstrated? Yes: fails on master, passes with the fix (50/50 with randomized settings). Removing the new call, or limiting it to ATTACH, makes it fail again.
f Fix is general across code paths? One factory function builds every MergeTree engine, in both syntaxes, including temporary tables. The other engines' drop() only removes data of their own fresh path; for Iceberg, a CREATE with columns over an existing path is refused before construction.
g Fix generalizes across inputs (params/datatypes/wrappers)? Measured for a persistent (_row_exists), a reserved (_part_offset) and an ephemeral (_part ... EPHEMERAL) name, for both engines: the root is unchanged. allow_reserved_column_names = 1 keeps working (05628-05630). The check is by name, so type wrappers do not matter.
h Backward compatible? (maintainer-approved exception only) Yes. CREATE mode only: ATTACH, server startup, lazy loading and RESTART REPLICA are unchanged. The same definitions are refused, earlier. No setting and no format change.
i Invariants and contracts preserved? A rejected CREATE of a MergeTree table now leaves the disk and Keeper unchanged. The factory uses the same settings and the same replay predicate as the interpreter, which still runs its own check and still rolls back the other engines.

Session id: cron:clickhouse-impl-slot-40:20261119-021200

@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b0e7229]

Summary: ✅


AI Review

Summary

Moves virtual-column and final-column-type validation before MergeTree construction, preventing a rejected CREATE TABLE from deleting existing detached parts on a table_disk root. No actionable findings. The regression test covers MergeTree and ReplicatedMergeTree; five CI executions passed on 765375dd15fc before the subsequent merge from master.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b0e7229da with master b1fcefb1a (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

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 765375d

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, 4/6) / codecs_float_insert infra, not from this PR (it changes only CREATE TABLE validation and adds 05830_table_disk_rejected_create_keeps_root_data): the shard's one run error (report) is query 12 run 4, an INSERT that stalled for about 20 s and hit the test's 15 s limit (Code: 159) while the cleanup thread removed 190 outdated hits_100m_single parts that the dataset rebuild leaves behind; the disk had 5.26 GiB unreserved at that moment (job label disk-almost-full) #131429 (mine, open)
Stateless tests (arm_binary, ParallelReplicas, plan-based, s3 storage, parallel) / 05829_autopr_skip_index_on_read_in_subquery not from this PR: a deterministic red of this test under the plan-based parallel replicas job, on 12 master commits since 2026-11-19 00:41Z (master report) and on 51 other PRs #132867 (mine, open)
PR praktika rollup of the rows above #131429, #132867 (mine, open)

The other CIDB FAIL rows on this head are Config Workflow / Pre Hooks and Finish Workflow / Post Hooks of the runs before the can be tested label. Mergeable Check is red only because of the rows above; CH Inc sync is green.

Session id: cron:our-pr-ci-monitor:20261119-080025

@clickhouse-gh clickhouse-gh Bot added comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 20, 2026

This branch has not been deployed

No deployments
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-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants