Repository navigation
Fix ALTER COLUMN on tables with implicit min-max index over virtual columns - #109428
Michicosun merged 5 commits into
Conversation
…olumns When enable_block_number_column / enable_block_offset_column and the corresponding add_minmax_index_for_block_*_column settings are on, the table gets an implicit min-max skip index over the persistent virtual columns _block_number / _block_offset (auto_minmax_index__block_number, etc.). Any ALTER on a physical column (RENAME / ADD / MODIFY) then failed with UNKNOWN_IDENTIFIER, e.g. Cannot apply ALTER because it breaks skip index auto_minmax_index__block_number: Missing columns: '_block_number' ... available columns: 'value1' 'renamed_date1' AlterCommands::apply re-derives every secondary index from its AST during validation, but resolved them against physical columns only. The implicit indices over virtual columns could not resolve _block_number / _block_offset and threw, even though the ALTER did not touch those columns. Resolve implicitly-created indices against the persistent virtual columns as well, matching how they are created in StorageInMemoryMetadata::addImplicitIndicesForVirtualColumns. Explicit indices are unchanged and still resolved against physical columns only. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260705-043700 |
|
cc @Michicosun @CurtizJ — could you review this? It fixes |
|
Workflow [PR], commit [15a6faf] Summary: ❌
AI ReviewSummaryThis PR fixes PR Metadata
Findings💡 Nits
Final Verdict
|
Address review: the previous check matched only the index name in EXPLAIN indexes = 1 output, which prints the index name even when it reads every granule (Granules: N/N). A regression that kept the index attached but stopped pruning would still have passed. Assert on the Granules line instead: after the ADD/MODIFY/RENAME COLUMN sequence, _block_number = 1 must read 10/20 granules and _block_offset = 0 must read 2/20 (index_granularity = 1, 20 rows in two blocks). A stopped-pruning regression reads 20/20 and fails these. Also RENAME the added column to widen the ALTER coverage. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Per review feedback: - Resolve implicit indices against getSampleBlockWithVirtuals instead of building a std::optional<ColumnsDescription> and merging virtual columns by hand. Explicit indices are still resolved against the physical columns only, so the "ALTER breaks explicit index" guard is unchanged. - Restore the original one-line comment above the loop. - Simplify the test to a single table exercising RENAME / ADD / MODIFY with both implicit indices, instead of three tables. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…column set The previous fixup switched the secondary-index re-validation pool from metadata_copy.columns (+ deduped virtuals) to getSampleBlockWithVirtuals(), which is built from getAllPhysical() + virtuals appended unconditionally. That introduced two regressions: - getAllPhysical() drops ALIAS/EPHEMERAL columns, so an implicit min-max index over an ALIAS column (auto_minmax_index_<alias>) could no longer resolve its expression after an ALTER, throwing UNKNOWN_IDENTIFIER (03768_rename_alias_column_implicit_index). - Appending virtuals without a dedup produced a block with both a physical and a virtual column of the same name (e.g. a physical _part_offset plus the virtual _part_offset), yielding AMBIGUOUS_COLUMN_NAME on ALTER ADD/analysis (04040_alter_ephemeral_column_conflicts_with_virtual, 03401_normal_projection_with_part_offset). Restore the original approach: start from the full metadata_copy.columns (which includes alias/ephemeral) and add each virtual only if a column of that name is not already present (physical shadows virtual). Explicit indices are still resolved against metadata_copy.columns only, so the "ALTER breaks explicit index" guard is unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed 0bd7dcc fixing a regression the previous fixup (029ff8b, the
Root cause:
Fix: start the resolution pool from the full Verified locally against a fresh build (buildId 831ddc63): all three previously-red tests plus |
| /// Changes in columns may lead to changes in secondary indices. | ||
| /// Implicit indices may reference persistent virtual columns (e.g. `_block_number`); resolve them | ||
| /// against the full column set (incl. alias/ephemeral) plus virtuals, a physical column shadowing | ||
| /// a virtual of the same name. | ||
| ColumnsDescription columns_with_virtuals = metadata_copy.columns; | ||
| for (const auto & virtual_column : metadata_copy.virtuals.toColumnsDescription(VirtualsKind::All, VirtualsMaterializationPlace::All)) | ||
| if (!columns_with_virtuals.has(virtual_column.name)) | ||
| columns_with_virtuals.add(virtual_column); |
There was a problem hiding this comment.
introduce method StorageInMemoryMetadata::getColumnsWithVirtuals
There was a problem hiding this comment.
Done — added StorageInMemoryMetadata::getColumnsWithVirtuals() (next to getSampleBlockWithVirtuals) and the call site now just uses it.
| /// Changes in columns may lead to changes in secondary indices. | ||
| /// Implicit indices may reference persistent virtual columns (e.g. `_block_number`); resolve them | ||
| /// against the full column set (incl. alias/ephemeral) plus virtuals, a physical column shadowing | ||
| /// a virtual of the same name. |
There was a problem hiding this comment.
do not change any comments in this pr
There was a problem hiding this comment.
Restored the original comment verbatim.
| index.definition_ast, | ||
| index.isImplicitlyCreated() ? columns_with_virtuals : metadata_copy.columns, | ||
| index.isImplicitlyCreated(), index.escape_filenames, context); |
There was a problem hiding this comment.
we can always use extended columns set to simplify this
There was a problem hiding this comment.
Done — dropped the isImplicitlyCreated() ternary; all indices now resolve against the extended set. Safe because the set is the full columns (incl. alias/ephemeral) + virtuals with physical shadowing virtual, so a dropped physical column an explicit index needs is still absent (guard still throws) and there is no duplicate _part_offset. Verified: DROP COLUMN under an explicit index still throws Code 47; physical _part_offset + projection ALTER no longer ambiguous; 04401/03768/04040/03401(+no_sorting) all pass.
…uals Factor the implicit-index column resolution into a named method StorageInMemoryMetadata::getColumnsWithVirtuals and always resolve secondary indices against it, dropping the isImplicitlyCreated ternary. The extended set is the full column list (incl. alias/ephemeral) plus virtual columns, a physical column shadowing a virtual of the same name, so explicit indices still fail an ALTER that removes a column they need and no _part_offset ambiguity is introduced. Original comment restored.
|
@Michicosun addressed all three — |
| metadata_copy.sampling_key.recalculateWithNewAST(metadata_copy.sampling_key.definition_ast, metadata_copy.columns, metadata_copy.virtuals, context); | ||
|
|
||
| /// Changes in columns may lead to changes in secondary indices | ||
| const ColumnsDescription columns_with_virtuals = metadata_copy.getColumnsWithVirtuals(); |
There was a problem hiding this comment.
AlterCommands::apply only rebuilds metadata_copy.secondary_indices, so this fix covers the implicit skip-index path from add_minmax_index_for_block_*_column. The PR body/changelog currently also mention part_minmax_index_columns = 'with_block_number_offset', but that is a separate per-part min-max path in MergeTreeData::getMinMaxColumns and never reaches this loop. Please drop that setting from the release note unless you have a failing repro that goes through secondary_indices here.
There was a problem hiding this comment.
Corrected — dropped part_minmax_index_columns = 'with_block_number_offset' from the changelog entry. Confirmed the reasoning: the fix rebuilds only metadata_copy.secondary_indices, and part_minmax_index_columns resolves entirely through MergeTreeData::getMinMaxColumns (per-part min-max, used in the partition/write/merge pipeline) — it has no reference to secondary_indices and never reaches this loop. That per-part path surfaces as a separate NOT_FOUND_COLUMN_IN_BLOCK '_block_number' (Code 341), out of scope for this skip-index fix and tracked in #106780.
|
The AST fuzzer (amd_debug) |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code |
CI finish ledger — 15a6fafCI is fully finished. Every failure has an owner; none is caused by this PR.
Root cause and fixing-PR detail: see #109428 (comment) Session id: cron:our-pr-ci-monitor:20260705-193000 |
c739ffd
|
Hi — this looks like a backport candidate for |
…mplicit-minmax-virtual-index Fix ALTER COLUMN on tables with implicit min-max index over virtual columns
Related: https://github.com/ClickHouse/ClickHouse/blob/master/tests/queries/0_stateless/01378_alter_rename_with_ttl_zookeeper.sql
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
UNKNOWN_IDENTIFIERerror when runningALTER TABLE ... RENAME/ADD/MODIFY COLUMNon a MergeTree table that has an implicit min-max index over the persistent virtual columns_block_numberor_block_offset(enabled byadd_minmax_index_for_block_number_column/add_minmax_index_for_block_offset_column).Description
When
enable_block_number_column/enable_block_offset_columnare on together withadd_minmax_index_for_block_number_column/add_minmax_index_for_block_offset_column, the table gets an implicit min-max skip index over the persistent virtual columns_block_number/_block_offset(auto_minmax_index__block_number,auto_minmax_index__block_offset).Any
ALTERon a physical column (RENAME / ADD / MODIFY) then failed, e.g.:AlterCommands::applyre-derives every secondary index from its AST during validation, but resolved them against physical columns only. The implicit indices over virtual columns could not resolve_block_number/_block_offsetand threw, even though the ALTER did not touch those columns. This fix resolves implicitly-created indices against the persistent virtual columns as well, matching how they are created inStorageInMemoryMetadata::addImplicitIndicesForVirtualColumns. Explicit indices are unchanged and still resolved against physical columns only, so the guard against ALTERs that genuinely break a user-defined index is intact.Added a regression test (
04401_implicit_minmax_block_number_offset_alter_column) covering RENAME/ADD/MODIFY column with the_block_number/_block_offsetimplicit index, and asserting the index still prunes granules after the ALTER.No related open GitHub issue was found; this surfaces as flaky failures of the public stateless test
01378_alter_rename_with_ttl_zookeeperin PR runs where CI randomization enables these settings, and will become reproducible on master once the virtual min-max index is enabled by default.Version info
26.7.1.546(included in26.7and later)