Repository navigation
Fix a crash on ALTER TABLE ... MATERIALIZE TTL over a non-MergeTree table - #117181
Conversation
…able `MutationsInterpreter::Source::getMergeTreeData` is a `dynamic_cast`, so it returns `nullptr` for any storage that is not a `MergeTreeData`. The `MATERIALIZE TTL` arm dereferenced it with no null check, and the dereference was the left operand of the `&&`, so `hasOnlyRowsTTL` could not short-circuit it: a column TTL alone was enough. Reading `storage_settings` off the null pointer makes `MultiVersion::get` lock a `std::mutex` at an invalid address. The fault is on the `validate` path, with `dry_run` true, and needs no non-default setting. Two storages get past the `hasAnyTTL` check at `:1410` while not being a `MergeTreeData`, and both were measured to fault at the same frame: * `StorageAlias`, which forwards `checkMutationIsPossible` to its target (`StorageAlias.h:105`) and returns the target's metadata verbatim (`StorageAlias.h:124-131`). * `StorageMaterializedView`, which forwards the same check (`StorageMaterializedView.cpp:867-871`) and can carry a TTL on a column of its own declaration. `AlterCommands::apply` re-derives the column TTL map from the column list on any alter (`AlterCommands.cpp:1806-1826`) and the view persists it (`StorageMaterializedView.cpp:803` and `:824`). The view is not reached through its target's TTL: `getInMemoryMetadataPtr` grafts only the target's virtual columns onto the view's own metadata (`StorageMaterializedView.cpp:360-381`), so the target's TTL is invisible through it. It is reached through the view's own column TTL instead. `StorageMemory` cannot hold a TTL, and `StorageEmbeddedRocksDB` accepts only UPDATE and DELETE. The guard sits at the point of use, so it covers any storage that reaches the line rather than these two specifically. Found by the AST fuzzer: check "AST fuzzer (amd_debug, targeted)", STID 2809-3ce4, `Address: 0x4c8`. Every other site in this file that can see a null `MergeTreeData` already handled it. Line 1415 was the only outlier, and the added term has the same shape as the guard at the `index_granularity_bytes` site. Defaulting the flag to false for a non-`MergeTree` source changes nothing observable. The stages computed on the `validate` path are discarded, and the real mutation is planned by a fresh `MutationsInterpreter` built from the `MergeTreeData &` constructor, where the setting is read correctly. Comparing a direct mutation against one through an alias on a `ttl_only_drop_parts` rows-TTL table gives identical rows, sums and active parts. The test asserts one completed mutation per storage rather than the value the TTL produces. An expired TTL is also applied by a background TTL merge, which creates no mutation, so asserting the value would hold even if the mutation never ran. No comment is added at the guard: the sibling guards in this function carry none, and the reasoning belongs here. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex). Two review rounds with one
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260830-180300 |
|
Workflow [PR], commit [be876bc] Summary: ✅
AI ReviewSummaryThis PR fixes the null dereference in Findings💡 Nits
Final VerdictCode change looks correct. No new inline blockers or majors from the current diff; only the changelog-category nit above remains. LLVM Coverage ReportMeasured on commit be876bc.
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260830-173200 |
|
cc @scanhex12 @davenger, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline. |
Closes: #117327
Related: #115287
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a crash when executing
ALTER TABLE ... MATERIALIZE TTLon a table that is not aMergeTreebut whose metadata carries a TTL. Two engines can be in that state:Alias, when its target has a TTL, andMaterializedView, when it declares a TTL on a column of its own. Closes #117327.Description
ALTER TABLE ... MATERIALIZE TTLterminated the server withSIGSEGV. Reachable with default settings.Found by the AST fuzzer:
AST fuzzer (amd_debug, targeted), STID2809-3ce4ALTER TABLE non_metadata_alters__fuzz_19 (MATERIALIZE TTL),Address: 0x4c8Root cause.
MutationsInterpreter::Source::getMergeTreeDatais adynamic_cast, so it returnsnullptrfor any storage that is not aMergeTreeData. TheMATERIALIZE_TTLarm dereferenced it as the left operand of the&&, sohasOnlyRowsTTLcould not short-circuit it and a column TTL alone was enough. Readingstorage_settingsoff it then makesMultiVersion::getlock a mutex at an invalid address. The fault is on the mutation validation path, before the mutation is planned.Change. One null term, leftmost so it short-circuits, matching the sibling guards in this function. It sits at the point of use, so it covers whatever reaches the line, not a list of engines. Two reach it today: an
Aliastable, which returns its target's metadata, and a materialized view declaring a TTL on a column of its own.For a non-
MergeTreesource the flag now reads false, which is not observable. The stages built on that path are discarded, and the real mutation is planned separately from the target'sMergeTreeData, where the setting is read correctly. On attl_only_drop_partsrows-TTL table, a direct mutation and one through an alias give identical rows, sums and active parts.Validation. New stateless test, one arm per affected storage plus a negative arm that still raises
INCORRECT_QUERY. The oracle is a completed mutation insystem.mutations, which a background TTL merge cannot produce; per-arm sensitivity was checked by deleting each statement in turn. The crash appears and disappears across the change on Build-ID-verified binaries, 50 randomized runs pass, and thematerialize_ttl,storage_aliasandmodify_commentfamilies all pass.Workflow [PR]
Sync PR [sync-upstream/pr/117181]
Version info
26.9.1.445(included in26.9and later)