Repository navigation
Conversation
`StorageDistributed` analyzed its sharding key once, in the constructor, for the column types of that moment, and `ALTER TABLE ... MODIFY COLUMN` of a column the key reads only re-validated the key. The cached expression kept the old input types and the function overloads resolved for them: - `DistributedSink::createSelector` executed it on a block of the new type, the function returned a column of another type than the cached result type, and every multi-shard INSERT failed with the `Unexpected return type` logical error (server abort on debug and sanitizer builds; found by BuzzHouse, STID 1611-3598). - `optimize_skip_unused_shards_rewrite_in` converted each `IN` value to the old input type, so a value outside its range was removed from the `IN` list sent to every shard and its rows were lost. The shard pruning of `optimize_skip_unused_shards` casts the set to the same type. A server restart built the key from the altered metadata and fixed both. The key is now held as an immutable `DistributedShardingKey` (expression, key column name, determinism flags, unbuilt-set flag) in a `MultiVersion`. `alter` analyzes it for the new columns before the metadata is stored, so a key that cannot be analyzed fails the ALTER. Every consumer obtains it through `getAnalyzedShardingKey(columns)` with the columns of its own metadata snapshot: the cached analysis is returned when the key inputs have the same types there, otherwise the key is analyzed for the snapshot, so an INSERT or SELECT racing an ALTER never pairs a snapshot with a key analyzed for other columns. `DistributedSink` keeps the key it got for its whole lifetime instead of re-reading it per block. Every analysis after the constructor works on a clone of the stored AST, because the analysis rewrites the AST in place. Keys whose result type does not depend on the input types (a bare column, `intHash64(c0)`) route rows exactly as before. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the getAnalyzedShardingKey comment to its contract; the concurrent ALTER rationale is in the previous commit message. 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; 0 findings), plus my own cold review (2 nits).
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261027-154800 |
|
cc @devcrafter @KochetovNicolai, could you review this and add the |
|
Workflow [PR], commit [958c625] Summary: ❌
AI ReviewSummaryThe PR refreshes the analyzed Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block. Preserve correct reads and aggregation for pre-existing rows across the type change, and add the regression above. |
| DatabaseCatalog::instance().getDatabase(table_id.database_name)->alterTable(local_context, table_id, new_metadata, /*validate_new_create_query=*/true); | ||
| setInMemoryMetadata(new_metadata); | ||
| if (new_analyzed_sharding_key) | ||
| analyzed_sharding_key.set(std::move(new_analyzed_sharding_key)); |
There was a problem hiding this comment.
[P1] Preserve shard placement for rows inserted before ALTER
Publishing the re-analyzed key changes the expected placement of existing rows, even when its result type stays UInt64. Reproduced with this PR's Fast test binary and two distinct shard tables: create d(c0 Int16) with key sipHash64(c0), insert 2, then widen both backing tables and d to Int32. For 2, the hash modulo two changes from 1 to 0, but the existing row stays on the second shard:
SELECT count() FROM d WHERE c0 = 2 SETTINGS optimize_skip_unused_shards = 0; -- 1
SELECT count() FROM d WHERE c0 = 2 SETTINGS optimize_skip_unused_shards = 1; -- 0The IN (2, 3) rewrite also loses the row. Inserting 2 again puts the same value on the other shard, and GROUP BY c0 with optimize_distributed_group_by_sharding_key = 1 returns two (2, 1) groups instead of (2, 2).
Please preserve the placement contract for existing data, or handle unsafe type changes before using the new key for pruning, rewriting, and aggregation. Add a regression that inserts before ALTER and uses distinct backing shard tables; the current test inserts only afterwards and exposes the same local table through both shards.
CI finish ledger - 958c625Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first two runs of this head stopped at Session id: cron:our-pr-ci-monitor:20261028-020005 |
Related: #86015
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
INSERTinto aDistributedtable failing with theUnexpected return typelogical error, andINfilters withoptimize_skip_unused_shards_rewrite_inlosing rows, afterALTER TABLE ... MODIFY COLUMNchanged the type of a column that the sharding key reads.Description
CI:
BuzzHouse (arm_asan_ubsan)on #115356 andBuzzHouse (amd_tsan)on #119997:Logical error: Unexpected return type from modulo/multiplyinDistributedSink::createSelector(STID 1611-3598).On 26.9.19.9, after
ALTER TABLE d MODIFY COLUMN c0 Int32(wasInt16) onDistributed(..., multiply(1001, c0)),INSERT INTO d VALUES (4), (5)fails withUnexpected return type from multiply. Expected Int32. Got Int64(debug builds abort). With the keyc0and the sameALTER,SELECT c0 FROM d WHERE c0 IN (1, 2, 100000) SETTINGS optimize_skip_unused_shards = 1, optimize_skip_unused_shards_rewrite_in = 1returns only1and2.Root cause:
StorageDistributedanalyzes the sharding key once, in the constructor.alteronly re-validates it (#86015), so the cached expression keeps the old input types.Change: the analyzed key (expression, key column name, flags) is an immutable
DistributedShardingKeyheld in aMultiVersion.alteranalyzes it for the new columns before storing the metadata. Every consumer (theINSERTsink, shard pruning, theINrewrite,optimize_distributed_group_by_sharding_key, the parallelINSERT SELECTcheck) gets it fromgetAnalyzedShardingKeywith the columns of its own metadata snapshot, which analyzes the key again if a query holds columns from before or after a concurrentALTER.DistributedSinkkeeps one key for its lifetime. A key whose result type does not depend on the column types routes rows as before.Test:
05453_distributed_sharding_key_alter_modify_column; both parts fail on master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127984]