Repository navigation
Fix parts silently pruned after ALTER MODIFY COLUMN ... AFTER on a partition-key column - #120862
Conversation
…t the table column order MergeTreeData::getMinMaxColumns took the min-max index column list from partition_key.expression->getRequiredColumnsWithTypes(), which is the table's column definition order. ALTER TABLE ... MODIFY COLUMN <c> <type> AFTER <other> on a column used in PARTITION BY rewrites that order, and nothing on the ALTER path refreshes already materialized state: checkPartitionKeyAndInitMinMax is called only from the MergeTreeData constructor, so minmax_idx_date_column_pos and minmax_idx_time_column_pos keep their pre-ALTER values, and no code rebuilds a loaded part's hyperrectangle. Since every consumer addresses the hyperrectangle by integer position, a part written before the ALTER had its ranges read out of the wrong slots. Observed on master, all silent: CREATE TABLE t (a UInt32, b UInt32) ENGINE = MergeTree PARTITION BY (a, b) ORDER BY tuple(); INSERT INTO t VALUES (1, 100); ALTER TABLE t MODIFY COLUMN a UInt32 AFTER b; SELECT count() FROM t WHERE a = 1; -- 0, the row is there DELETE FROM t WHERE a = 1 SETTINGS mutations_sync = 2; -- completes, deletes nothing plus a wrong system.parts.min_time for a part written after the ALTER, or LOGICAL_ERROR "Part minmax index by time is neither DateTime or DateTime64" when the stale slot holds an incompatible Field kind (issue ClickHouse#92834's reproducer). The order is now a function of the partition key's column-name set alone, which an existing table cannot change: RENAME and DROP on a key column are rejected, PARTITION BY cannot be altered, MODIFY COLUMN on a key column only changes the type, and the key cannot reference an ALIAS. So a metadata mutation can no longer desynchronize the index, rather than the ALTER path having to repair it. The implicit _minmax_count_projection derives its min(c)/max(c) pair order from the same table order through a different call, and getMinMaxCountProjectionBlock pairs the i-th projection column with hyperrectangle[i], so both derivations are made canonical. Sorting only getMinMaxColumns would have broken min()/max() answered from that projection for every table whose column order is not sorted by name, with no ALTER involved. Sorting only the projection would have left the index untouched. Both are needed and each one guards the other. Nothing persisted or transferred depends on the order: files are minmax_<column>.idx, checksums.txt is keyed by file name, no metadata file lists the columns positionally, and parts are fetched as files, so there is no format change and a mixed-version replica set is unaffected. Measured in both directions: a data directory written by master and opened by this build reads correctly, and the reverse too; on the upgrade path a stale min_time is repaired on load, because both the load order and the cached positions are recomputed from the metadata. Two things this does not do. It does not repair bounds a merge already wrote while the order was desynchronized: a merge unions the source hyperrectangles by position and then stores them by name, so those bounds are on disk and survive a restart. Re-inserting the data repairs such a table; an ordinary merge does not, OPTIMIZE ... FINAL included, because merge_may_reduce_rows is false for it, so the merged index is the union of the stored source ranges instead of a recomputation from the data, and a union only ever widens. OPTIMIZE ... FINAL DEDUPLICATE does repair it in place, because deduplicate does set that flag - at the cost of dropping duplicate rows. And it leaves ReplicatedMergeTreeTableMetadata alone, which indexes the required-column list with the cached date position: it runs only for format_version below MERGE_TREE_DATA_MIN_FORMAT_VERSION_WITH_CUSTOM_PARTITIONING, whose partition key is toYYYYMM(<the single date column>), so the list has one element and every order is the same order. The new test asserts the projection-on and projection-off answers are equal rather than hard-coding a triple, so a future change of the projection defaults cannot satisfy it vacuously. Its last case has no ALTER at all: it exists because a table declared (b, a) is the shape that detects a desynchronized projection order. Closes: ClickHouse#92834 Related: ClickHouse#93111 Co-Authored-By: Claude Opus 5 <[email protected]>
…test The T7 and T8 cases compare min()/max() answered from the implicit _minmax_count_projection against the same query with projections off, but optimize_use_implicit_projections = 1 only grants permission: if optimizeUseAggregateProjection declines, both sides are computed from the base table and the comparison passes on any binary, including one built without the ProjectionDescription::getMinMaxCountProjection sort. Since reverting only that sort reddens exactly one line, T8 "projection enabled", that assertion is the whole guard for half the fix and it must not be able to pass vacuously. Add a projection-use assertion next to each of the two cases, in the shape already used by 04950_minmax_count_projection_unusable_bound. The two assertions read the initiator's own plan, which under parallel replicas holds the projection only when the initiator builds a local plan and aggregation-in-order is off. Both are randomized per run by clickhouse-test, and the ParallelReplicas configuration enables parallel replicas for every query, so without a pin the assertions failed about half the time there (16/30, 13/50, 10/20 runs; the runner's own --diagnose-random-settings named optimize_aggregation_in_order). The answers stay correct in every combination, only the plan choice moves, so a per-setting "Random settings limits" line pins both settings to the values they already have by default rather than tagging the test out of randomization. Co-Authored-By: Claude Opus 5 <[email protected]>
The Nullable/DateTime case asserted count() FROM system.parts, which proves only that the query completed, and it let c2 default to the DateTime epoch. A value oracle over that row would have been no better: min_time = max_time = 0 is also what an empty range and an unset cached position produce, so the expected output was reachable without the corrected slot order. Insert a non-epoch c2 and assert min_time / max_time, which subsumes the no-throw property since printing the row requires the query to finish. Measured on the fixed build: with c2 at its epoch default the row reads 1970-01-01 00:00:00, with '2024-06-15 12:00:00' it reads that value back. On master the same case still aborts with "Part minmax index by time is neither DateTime or DateTime64", so the case keeps the failure it was written for. The file passes with each of the six session timezones clickhouse-test randomizes, including the three that are non-deterministic around 1970. No mixed-NULL Nullable(DateTime) case is added: a Nullable date or time key leaves the cached position unset, because checkPartitionKeyAndInitMinMax classifies with raw TypeIndex tests, so getMinMaxTime returns an empty result before it reads a bound and such an oracle would be vacuous too. Supporting min_time / max_time for a Nullable date or time key is separate work. Also pin the setting that actually governs the lightweight DELETE case: InterpreterDeleteQuery overwrites mutations_sync in the query context with lightweight_deletes_sync, so the old pin was inert and the case was deterministic only by that default. The heavyweight ALTER ... DELETE case keeps mutations_sync, which is its setting. Renumbered to 05229 because 05227 and 05228 landed on master meanwhile. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review over four rounds. An independent model reviewed each head (engine: codex;
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20260918-151100 |
Pre-PR validation gate (click to expand)
Also verified: 50 runs with the default randomized settings and 50 with the pruning settings pinned, |
|
Workflow [PR], commit [8b9f6a9] Summary: ✅
AI ReviewSummaryThis PR fixes the wrong min-max slot ordering that appears after Final Verdict✅ No new findings. LLVM Coverage ReportMeasured on commit 8b9f6a9.
Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code |
CI finish ledger - 5c218d4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
None of the three is reachable from this diff, which changes partition-key handling for Session id: cron:our-pr-ci-monitor:20260919-020058 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 47 s compile time in total, 8 of them have a recent master baseline. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120862&sha=d56ca535ff38d5a7f2d8a9a2eba8ecbbfbe35f70&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The SELECT * FROM file(concat(repeat('{"a":null,"b":true,"c":false}', 100000), ',', repeat('}', 100000)))reached at The other red, Merged |
|
Investigated. Your attribution holds, this diff is not on the path, and the owner is your own #118467. One correction: the memory is not going where the report puts it. What I measured from the job's artifacts: Where the memory actually went. The The same function is the entire stack of Why Does #118467 cover it. Yes, outright: 100,000 groups against Seed. This targeted job prepared its corpus from 12 test files, one of which is your Breadth (CIDB, AST fuzzer jobs). This branch's record. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120862&sha=1035f95698ee5d722dce82e0e5e9bbb91739c302&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The
Merged |
|
Investigated 1. What actually failed the check. In The bar for that verdict is the dashboard's own adaptive threshold, and for this query it was 10.3% ( 5.2% is the wrong number for this query. On That tier logic lives in the dashboard service, not in this repository, so it is not fixable by a PR here. 2. This shard also lost its learned thresholds. So all 206,016 rows of The fix is already in flight, and it is a pair:
Both are open, so I am not opening a third PR for this. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120862&sha=ce46017022c2706b5b7538329364b78c8a62646c&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. Status of the two reds on
|
|
Both are still open, so there is nothing to link as merged yet, and I will post the merge links here. One thing worth flagging about the order they land in: #121189 is itself red on That message carries no "N slower" phrase, and #121090 is green on all six perf shards with |
|
🕵 Status 2026-09-22: merged |
|
Neither #121189 nor #121090 will land: @ fm4v closed both unmerged on 2026-09-23 in favor of #121847, which merged as |
Closes: #92834
Related: #93111
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results after
ALTER TABLE ... MODIFY COLUMN <column> <type> AFTER <other>on a column used inPARTITION BY. The reorder changed the order of the part min-max index columns, which are addressed by position, so a part written before theALTERwas silently pruned from queries it belongs to,DELETEremoved nothing,system.parts.min_timereported another column's value or raisedLOGICAL_ERROR: Part minmax index by time is neither DateTime or DateTime64, and the implicit_minmax_count_projectionansweredmin()/max()from another column. The order now comes from the partition key's column names, which an existing table cannot change. Closes #92834.Description
MergeTreeData::getMinMaxColumnstook the min-max index column list frompartition_key.expression->getRequiredColumnsWithTypes(), the table's column definition order.MODIFY COLUMN ... AFTERrewrites that order, and nothing on theALTERpath refreshes materializedstate:
checkPartitionKeyAndInitMinMaxruns only in theMergeTreeDataconstructor,so
minmax_idx_{date,time}_column_poskeep their old values, and nothing rebuilds a loaded part'shyperrectangle. Every consumer addresses it by position, so a part written before theALTERreadits ranges from the wrong slots.
The implicit
_minmax_count_projectionderives itsmin(c)/max(c)pair order from the same tableorder through a different call and is not rebuilt either, so both derivations are now sorted by
name; sorting one alone would desynchronize the other. The name set cannot change for an existing
table:
RENAME/DROPon a key column are rejected,PARTITION BYcannot be altered, and a keycolumn cannot become an
ALIAS.Nothing persisted is positional (files are
minmax_<column>.idx, checksums keyed by name), sothere is no format change and mixed-version replicas are unaffected. Measured both ways: a
directory written by master reads correctly here, and the reverse.
mergeTreeIndex(..., with_minmax = 1)lists itsminmax_<column>columns in that order too, so itsSELECT *order changes for a partition key that is not name-sorted; no test depends on it.Not repaired: a merge that ran while the order was desynchronized already wrote mixed bounds into
minmax_<column>.idx(unioned by position, stored by name), which survive a restart. A re-insertfixes such a table, as does
OPTIMIZE ... FINAL DEDUPLICATE, which recomputes them from the databut drops duplicate rows; a plain
OPTIMIZE ... FINALonly unions the stored ranges, so itdoes not.
Workflow [PR]
Sync PR [sync-upstream/pr/120862]
Version info
26.10.1.435-master(included in26.10and later)