Repository navigation
Resolve MergeTree key types in the global context on ALTER, not in the ALTER's session - #130154
Conversation
…e ALTER's session `AlterCommands::apply` re-derived the sorting, primary and partition keys and the `minmax_count` projection in the context of the session that issued the `ALTER`, while `CREATE`, a reload of the table and the part writers derive them in the global context. A session setting that changes key analysis therefore installed in-memory keys that the table does not really have. With `transform_null_in = 1`, `in` is rewritten to `nullIn`, which renames the key column and, for a `Nullable` argument, changes its type from `Nullable(UInt8)` to `UInt8`. After such an `ALTER` (even a comment-only one): - an INSERT into a table with a Nullable key under `IN` serialized the writer's `ColumnNullable` with the stored `UInt8` serialization: `Bad cast from type DB::ColumnNullable to DB::ColumnVector<char8_t>` in debug builds, a primary index of garbage bytes in release builds; - every merge failed with `NOT_FOUND_COLUMN_IN_BLOCK: Not found column in(x, ...)`; - a partition key wrote `partition.dat` with the session type, so after a restart the parts were detached as `broken-on-start`. Derive these keys in the global context, as ClickHouse#119799 already does for skip indices in the same function, and carry `isAnalyzingBackgroundExpressions` into it so that AI functions stay rejected in keys (ClickHouse#127057). The sampling key, projections and column TTLs keep the session context: the sampling key is re-analysed in the query context at read time, and projections and TTLs are separate derivations. Found by the targeted AST fuzzer on PR ClickHouse#130066 (STID 1499-5685). Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit kept the sampling key on the ALTER's session context on the grounds that it is re-analysed in the query context at read time. That is wrong: `SAMPLE` takes the sampling universum from the stored `sampling_key.data_types[0]`. With `cast_keep_nullable = 1` in the `ALTER`'s session, `SAMPLE BY CAST(x, 'UInt32')` over a `Nullable` column became `Nullable(UInt32)`, and every `SAMPLE` query failed with `Invalid sampling column type in storage parameters: Nullable(UInt32)` until the table was reloaded. Derive it with the same key context as the other keys. The test is renamed to cover both settings, gains a sampling case, and the ReplicatedMergeTree case now has an explicit `PRIMARY KEY`, so the `getPrimaryKeyFromAST` branch is covered as well. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds, head 9bb2fc8): 2 fixed, 2 dismissed with evidence, 3 noted
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261108-131300 |
|
cc @alexey-milovidov @Algunenano, could you review this and add the |
|
Workflow [PR], commit [9bb2fc8] Summary: ✅
AI ReviewSummaryReviewed PR #130154 at Missing context / blind spots
Final Verdict✅ Approve. No code changes requested; normal CI gates still apply. No new inline findings were posted. |
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, 18 s compile time in total, 7 of them have a recent master baseline. |
59e6b4b
…401` Master now re-derives the sampling key in the global context after an `ALTER` (9bb2fc8, ClickHouse#130154), like the other keys. A session `cast_keep_nullable = 1` therefore no longer makes `CAST(x AS UInt32)` over a `Nullable` column a `Nullable(UInt32)` sampling key, and `ALTER TABLE ... MODIFY SAMPLE BY` is accepted instead of failing with `ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER`. That made `04401` fail in Fast test after the last master merge. Update the test case to the new behaviour: the `ALTER` succeeds and `SAMPLE` keeps working afterwards. Update the comment in `AlterCommand::apply` to match. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109196&sha=35f0e6feeff926f8957a9d1a9cdc72405bc472e6&name_0=PR&name_1=Fast%20test PR: ClickHouse#109196 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ble expressions Merges and mutations run in a context built from the background profile, so a function that reads the time zone when it runs (e.g. `toDate` of an `Int64`) is computed in that profile's `session_timezone`. Under `SessionTimezoneBlockerInThread` `DateLUT::instance` fell back to the global context instead, so with a background profile that sets `session_timezone` an INSERT still wrote parts sorted in another zone than merges recompute (measured: an INSERT under the same `session_timezone` as the background profile, which merged on master, aborted with `Sort order of blocks violated`). Without a background profile its settings are those of the system profile, which the global context also has, so nothing changes there. Tests: an integration case with a background profile that sets `session_timezone` (fails on master and without this change, passes with it); in 05570 the Graphite block also checks that the rollup happened, and the `transform_null_in` table, which merges since ClickHouse#130154, now gets two parts, a merge and a primary key read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #119799
Related: #109196
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed failing merges, an invalid primary index and parts detached as broken after a restart when
ALTER TABLEran withtransform_null_in = 1on a MergeTree table whose sorting or partition key appliesINto a column.ALTERnow derives the table's keys in the server's global context, asCREATEand a table reload do.Description
Found by the targeted AST fuzzer on #130066 (
Bad cast from type DB::ColumnNullable to DB::ColumnVector<char8_t>): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=130066&sha=f399ce96513dd38be28154da2a435932c87a6cf9&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29On 26.10.2.103, after
ALTER TABLE t COMMENT COLUMN x 'c' SETTINGS transform_null_in = 1: withORDER BY (x IN (1, 2))every merge fails withNOT_FOUND_COLUMN_IN_BLOCK; with aNullablesorting key, INSERTs write a primary index of garbage bytes (debug builds abort as above); withPARTITION BY (x IN (NULL, 1))on aNullablecolumn, new parts are detached asbroken-on-startafter a restart. Withcast_keep_nullable = 1, the sameALTERis rejected forORDER BY CAST(x, 'UInt32')over aNullablecolumn, and withallow_nullable_key = 1the next INSERT crashes (segmentation fault).AlterCommands::applyre-derived the keys in the issuing session's context, whileCREATE, a reload and the part writers use the global context.transform_null_inrewritesintonullIn, which renames the key column and, for aNullableargument, changes its type.Now the sorting, primary, partition and sampling keys and the
minmax_countprojection are derived in the global context, as #119799 did for skip indices, keeping the flag that rejects AI functions in keys (#127057). The sampling key moves too becauseSAMPLEreads its stored type. Projections (#116824) and TTLs are unchanged. A key function needing a session-only setting (detectLanguage) is now rejected byALTER ... MODIFY ORDER BYas byCREATE; before, such a table rejected every INSERT and could not be attached again. Complements #109196, whose pinned settings lacktransform_null_in.The new test fails on master, each case on its own, and passes with the fix (50/50 randomized runs).
Workflow [PR]
Sync PR [sync-upstream/pr/130154]
Version info
26.11.1.1704-master(included in26.11and later)