Repository navigation
Do not prune a part by statistics while an alter mutation is pending - #119337
Conversation
A pending `ALTER MODIFY COLUMN` is applied on the fly, so a read already returns the converted values,
while the part's statistics still describe the values as they were written. Statistics-based part
pruning compared the predicate against those stale statistics and dropped the part, so
CREATE TABLE t (x Float64 STATISTICS(basic), s String) ENGINE = MergeTree ORDER BY tuple();
SYSTEM STOP MERGES t;
INSERT INTO t VALUES (10.5, 'a'), (11.5, 'b');
ALTER TABLE t MODIFY COLUMN x Int64 SETTINGS alter_sync = 0;
SELECT x FROM t; -- 10, 11: the read is already converted
SELECT count() FROM t WHERE x = 10; -- 0, the part is pruned by the range [10.5, 11.5]
returned no rows for the whole lifetime of the pending mutation, at default settings and with no error.
`filterPartsByStatistics` gated on `hasDataMutations` and `hasPatchParts`, but an `ALTER MODIFY COLUMN`
is neither: it is a `READ_COLUMN` alter mutation. Add `hasAlterMutations` to the gate, which is
exactly what the neighbouring gate for the top-k minmax index (`partHasStaleTopKIndex`) already checks.
The gate stays as coarse as the two conditions next to it - any pending alter mutation disables
statistics pruning for the query, rather than only one that touches a column with statistics.
Closes: #116627
|
Workflow [PR], commit [86dccbc] Summary: ✅
AI ReviewSummaryPR #119337 prevents statistics, skip indexes, and the query condition cache from pruning rows using values invalidated by pending column mutations. It also guards projection reads under row policies and preserves statistics optimizations after completed mutations and representation-preserving type changes. Reviewed head: Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes29 object files changed (+50.59 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units389 translation units recompiled, 4068 s compile time in total, 389 of them have a recent master baseline. |
|
Related: #110518 |
…ing-pending-alter-116627
… mutations A pending `DROP COLUMN` is a metadata mutation, and a column with the dropped name can be added again right away. `AlterConversions` then makes a read return the new column's default, while `IMergeTreeDataPart::loadStatistics` still loads the dropped column's statistics under that name, so `WHERE b = 7` on the re-added `b` pruned the old part by the stale `[1000, 1099]` range and returned 0 rows. Add `hasMetadataMutations` to the gate in `filterPartsByStatistics`. `partHasStaleTopKIndex` had the same blind spot: after `DROP INDEX`, `DROP COLUMN`, `ADD COLUMN ... DEFAULT`, `ADD INDEX` with the same names, the part still holds the minmax index built over the dropped values, and `ORDER BY b DESC LIMIT 1` picked the wrong part. Include metadata mutations in the gate and treat the index as stale when one of its columns is dropped by a pending mutation (`isColumnDropped`); a plain `RENAME COLUMN` keeps the index valid. Both scenarios are added to `05200_statistics_pruning_pending_alter` and were reproduced on the previous head of this branch before the fix. Addresses the review thread on #119337 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ing-pending-alter-116627
…ending metadata mutations Two gaps left by the previous fence, both from the `clickhouse-gh` review: - The query condition cache is keyed by the part name, which a pending `ALTER MODIFY COLUMN` (alter mutation) or `DROP COLUMN` / `RENAME COLUMN` (metadata mutation) does not change. `filterPartsByQueryConditionCache` only bailed out on data mutations and patch parts, so an entry recorded for a dropped column pruned marks of a re-added column with the same name. Fence the read side on alter and metadata mutations too, and keep the write sides in `ReadFromMergeTree` symmetric. - `canUseIndex` only knows about columns updated on the fly, so every skip-index path (index analysis, skip indexes on data read, direct read from a text index, the top-k minmax) used an index file built over a column that is dropped or renamed by a pending mutation, after a new column with the same name and an index with the same name were added. Add `AlterConversions::getColumnsInvalidatingIndexes` (updated columns plus every name touched by a pending drop or rename) and pass it to `canUseIndex` from all per-part callers; this replaces the top-k-only `isColumnDropped` check. Extend `05200_statistics_pruning_pending_alter` with the regular skip-index case and the query condition cache case. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ing-pending-alter-116627
…nmax on pending metadata mutations `IndexReadRangesRefiner` (with `use_indexes_refiner_in_read_pools = 1`) still built the skip-index result from `getAllUpdatedColumns`, and `MergeTreeSkipIndexReader::read` built the minmax for the read-time TopK threshold without any staleness check. Both could use an index file built over a column that a pending `DROP COLUMN` / `RENAME COLUMN` rebound to a new one. Pass `getColumnsInvalidatingIndexes` to the refiner and check `canUseIndex` for the TopK minmax. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… part has pending alter mutations `mergeTreeProjection` reads clear the mutations snapshot, so pending alter and metadata mutations (`MODIFY COLUMN`, `DROP COLUMN`, `RENAME COLUMN`), which the parent read applies on the fly via `AlterConversions`, are not applied to the projection. Under a row policy the projection could show stale values. Refuse the read for parts that still have on-the-fly mutation commands. The check is per part, because the snapshot counters (`hasAlterMutations`) also include already finished mutations. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp
|
🕵 @groeneai, investigate the failure: |
|
Acknowledged. I'm investigating the |
…ROP COLUMN` Cover the metadata-mutation arm of `getColumnsUpdatedOnFly` in `optimizeDirectReadFromTextIndex.cpp`: after `DROP INDEX; DROP COLUMN s; ADD COLUMN s; ADD INDEX` with the drop still pending, `hasToken` on the re-added column must not be answered from the postings built over the dropped one. Without that arm the test returns 0 rows instead of 10. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Fix: #125345 Root cause: the MergeTree randomizer drew |
…umnsInvalidatingIndexes` `addMutationCommand` normalizes `rename_map` and `dropped_columns`: chained renames `A -> B; B -> C` collapse into one `A -> C` entry, and a drop after a rename is recorded under the original name. Rebuilding the invalidating set from them lost the intermediate name `B`. Collect every name used by a pending `RENAME COLUMN` or `DROP COLUMN` command separately, as written in the command. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The only CI failure, |
|
🕵 All 22 red checks on 3c53e90 come from one failure: |
|
🕵 Merged |
|
🕵 Merged |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Looks like a clean bugfix.
#119337 landed on master with a broader fix for the same class of bugs: statistics pruning, skip indexes (including the top-k minmax index), the query condition cache and direct reads from text indexes are now disabled for a part with any pending alter or metadata mutation (`partHasPendingAlterOrMetadataMutations`, `AlterConversions::getColumnsInvalidatingIndexes`). The conflicts are resolved in favour of master, and the per-column variants of these checks from this branch (`getColumnsWithStaleIndexes`, `changesColumnIdentity`, `partChangesColumnIdentity`) are dropped as subsumed. The fix for the trivial `count` from sparse-column defaultness statistics is kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts against the release branch: `makeBlockWithMinMaxFromStatistics` has no `context` parameter there, `filterPartsByStatistics` loads statistics in a plain loop (no `loadStatisticsOfPartsInParallel`), and `partHasStaleTopKIndex` has no masking-policy gate. The per-part pending-mutation and representation-preserving-conversion checks of #119337 are applied in the release-branch shape. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #119337 to 26.10: Do not prune a part by statistics while an alter mutation is pending
`Bugfix validation (functional tests)` requires every new test to fail on `master`. The tests for statistics-based part pruning, skip indexes, the query condition cache (including top-k) and direct reads from text indexes pass there, because those paths were fixed on `master` by #119337, which has its own tests. They are removed from this PR, which is now only about `optimize_trivial_count_with_sparsity_filter`. The positive case of `05361_trivial_count_sparsity_pending_rename_other_column` (a pending metadata mutation of another column keeps the rewrite) is moved into `05227_trivial_count_sparsity_pending_rename`, which fails on `master`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117814&sha=latest&name_0=PR&name_1=Bugfix%20validation%20(functional%20tests%2C%20amd64) #117814 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ition cache ClickHouse#119337 added `hasPendingAlterOrMetadataMutations`, which stops reading and writing the query condition cache while an alter or metadata mutation is pending for one of the read parts. It covers what `hasPendingMetadataMutations` was added for (a pending `DROP COLUMN` or `RENAME COLUMN` that `KILL MUTATION` can leave unmaterialized), and it ignores the finished mutations the snapshot keeps for `system.mutations`, which made our snapshot-level check disable the cache after any `DROP COLUMN` until the mutation was evicted. So drop `hasPendingMetadataMutations` from the read side and both write sides. Its last user was `isQueryConditionCacheAllowed`, which also checked it because projection analysis reads projection parts with an empty mutations snapshot. Since ClickHouse#118585 a part with a pending metadata mutation on a column the projection holds or the read needs is read from the parent part, so projection analysis does not consult the cache for it, and the getter is back to master's form. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… in 05458 Since ClickHouse#119337 the query condition cache is skipped while an alter mutation is pending for a part, so the existing `DEFAULT now64 for NULL` cases pass on master. After `KILL MUTATION` of the `MODIFY COLUMN` the part keeps its `Nullable` column, nothing is pending, and every read still replaces the NULLs with `now64(3)`: on master the cached query returns 0 rows instead of 100000. The new case fails when `IMergeTreeReader::performRequiredConversions` does not record the non-deterministic conversion output. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix silently missing rows when a query filters on a column whose
ALTER MODIFY COLUMNorDROP COLUMNis still a pending on-the-fly mutation: statistics-based part pruning, skip indexes (including the top-k minmax index) and the query condition cache described the values as they were written, and dropped parts or granules the query had to read.Documentation entry for user-facing changes
...
A pending
ALTER MODIFY COLUMNis applied on the fly, so a read already returns the converted values, while the part's statistics still describe the values as they were written. Statistics-based part pruning compared the predicate against those stale statistics and dropped the part, soreturned no rows for the whole lifetime of the pending mutation, at default settings (
use_statistics_for_part_pruning = 1since 26.4) and with no error.filterPartsByStatisticsgated onhasDataMutationsandhasPatchParts, but anALTER MODIFY COLUMNis neither: it is aREAD_COLUMNalter mutation. AddhasAlterMutationsto the gate, which is exactly what the neighbouring gate for the top-k minmax index (partHasStaleTopKIndex) already checks. The alter (and metadata, see below) mutations are checked per part, through the commands newer than the part (MergeTreeDataSelectExecutor::partHasPendingAlterOrMetadataMutations): the snapshot counters also include finished mutations kept forsystem.mutations, which must not disable pruning for the parts they were already applied to. A part with such a pending mutation is not pruned by statistics, whichever column the mutation touches. The query condition cache gates use the same check over the selected parts.The review found a second blind spot of the same kind: a pending
DROP COLUMNis a metadata mutation, and a column with the dropped name can be added again right away. A read then returns the new column's default, while the part's statistics are still loaded under that name for the dropped column, soWHERE b = 7on the re-addedbpruned the part and returned 0 rows. The statistics gate now also checkshasMetadataMutations.partHasStaleTopKIndexhad the same problem afterDROP INDEX,DROP COLUMN,ADD COLUMN ... DEFAULT,ADD INDEXwith the same names: the part keeps the minmax index built over the dropped values, andORDER BY b DESC LIMIT 1picked the wrong part. It now includes metadata mutations in its gate.A second review pass found two more paths of the same kind, both fixed:
canUseIndex, which knows about columns updated on the fly but not about metadata mutations, so afterDROP INDEX idx; DROP COLUMN b; ADD COLUMN b ...; ADD INDEX idx baWHEREonbwas pruned by the index file built over the droppedb(0 rows instead of 11 withuse_skip_indexes_on_data_read). The newAlterConversions::getColumnsInvalidatingIndexesreturns the updated columns plus every name touched by a pendingDROP COLUMNorRENAME COLUMN, and all per-partcanUseIndexcallers pass it. An index on a column renamed by a still-pending mutation is therefore not used for that part until the rename is materialized (conservative: which column a part's index file was built over is not recorded).filterPartsByQueryConditionCachenow also bails out on alter and metadata mutations, and the write sides inReadFromMergeTreeare gated the same way.A pending
DROP INDEX idxfollowed byADD INDEX idxon another column (no column mutation involved) is a separate, pre-existing bug and is not fixed here.Related: #121171
New test:
05200_statistics_pruning_pending_alter, covering all of these scenarios. Verified in both directions, and a differential run of thestatistic/on_fly/mutation/alter_modify/prunestateless tests (341 tests) shows no new failures.Closes: #116627
Closes: #117323
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119337]
Version info
26.11.1.922-master(included in26.11and later)