Skip to content

Fix Min-Max index file-order hint after part_minmax_index_columns downgrade - #111511

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/minmax-getprobablywrittenfiles-42701
Jul 31, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix/minmax-getprobablywrittenfiles-42701

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a LOGICAL_ERROR ("Part level Min-Max index was constructed from unexpected columns set") that could abort a background merge in debug/sanitizer builds after part_minmax_index_columns was lowered (for example from with_block_number_offset back to partition_key_only).

Description

Found by the Stress test (arm_asan_ubsan) on PR #42701 (unrelated to that PR):
report https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=42701&sha=3f6eab14d9d3cac7d7f3add30f551a43820e4ddb&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 , STID 5721-5ce7.

MinMaxIndex::getProbablyWrittenFiles (reached from getPreferredFileOrder during merge/mutation finalize) accepted the in-memory hyperrectangle only when its size exactly matched one of two column sets derived from the current part_minmax_index_columns setting, and threw a LOGICAL_ERROR otherwise.

The min-max index has upgrade semantics: a part written under a wider part_minmax_index_columns keeps its extra hyperrectangle entries (partition key plus _block_number/_block_offset) until a merge or mutation re-materializes it. After the setting is lowered, such a part's index is legitimately larger than the current setting prescribes, so the exact-size check failed and the merge aborted.

getProbablyWrittenFiles only produces a best-effort file-ordering hint for the packed-part archive layout, and store() already writes just the leading min(current column count, hyperrectangle size) min-max files. The hint now mirrors that same clamp instead of requiring an exact match, so a valid transitional index no longer aborts the merge. On-disk output is unchanged.

Reproduced deterministically (see the new stateless test 04626_minmax_block_number_downgrade_merge): insert parts under part_minmax_index_columns='with_block_number_offset', load their min-max index into memory, ALTER the setting down to partition_key_only, then merge. The test fails on the current server (merge aborts) and passes with this change. No related open issue was found.

…ngrade

getProbablyWrittenFiles threw a LOGICAL_ERROR ("Part level Min-Max index was
constructed from unexpected columns set") during a background merge when a part's
in-memory min-max hyperrectangle was larger than the current part_minmax_index_columns
setting prescribes. This is a legitimate transitional state: a part written (or cached
in memory) under with_block_number_offset keeps its _block_number/_block_offset entries
after the setting is lowered to partition_key_only, until it is dropped by a reload or
rebuilt by a merge. store() already handles this by writing only
min(current column count, hyperrectangle size) files; the file-order hint now mirrors
that same clamp instead of demanding an exact size match and aborting.

The result of getProbablyWrittenFiles is only a best-effort file-ordering hint for the
packed-part archive layout, so it must not abort on a valid transitional index.

Reproduced deterministically (new stateless test): insert compact parts under
with_block_number_offset, cache their min-max index, ALTER the setting down to
partition_key_only, then merge.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Insert compact packed parts under part_minmax_index_columns='with_block_number_offset', run a _block_number/_block_offset query to cache the index, ALTER ... MODIFY SETTING part_minmax_index_columns='partition_key_only', then OPTIMIZE ... FINAL. Aborts every time on the current server.
b Root cause explained? Yes. The min-max index can hold more columns than the current setting prescribes (a wider index not yet re-materialized). getProbablyWrittenFiles accepted the hyperrectangle only if its size exactly equalled one of two current-setting column sets and threw LOGICAL_ERROR otherwise, so a valid transitional (larger) index aborted the merge.
c Fix matches root cause? Yes. The function only builds a best-effort file-order hint; it now emits the leading min(current column count, hyperrectangle size) files (exactly what store writes) instead of requiring an exact match.
d Test intent preserved / new test added? New stateless test 04626_minmax_block_number_downgrade_merge. No existing test weakened; the removed throw was over-strict for the hint.
e Both directions demonstrated? Yes. Test fails on the current server (merge aborts, server dies) and passes with the fix (server survives, correct results).
f General, not a narrow patch? Yes. getProbablyWrittenFiles is the single choke point for both MergeTreeDataPartCompact and MergeTreeDataPartWide getPreferredFileOrder; covered both setting directions, empty vs non-empty partition key, and packed storage.
g Generalizes across inputs? Yes. Verified across the matrix: empty partition key (size 2 -> 0), non-empty (size 3 -> 1), upgrade direction, and packed compact parts. _block_number reads still return correct rows.
h Backward compatible? Yes. No setting/format/wire change. On-disk min-max files are unchanged (store behavior untouched); only the in-memory file-order hint computation changed.
i Invariants preserved? Yes. getMinMaxColumns orders partition-key columns first, then _block_number/_block_offset, so the clamp keeps the correct leading prefix, exactly the files store writes. The hint and store now use the same column list and clamp, so they cannot disagree.

Session id: cron:clickhouse-author-slot-6:20260723-010500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review — adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 findings across 1 full pass + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ Test proves merge completion but not that the file-order hint is produced/consumed; forcing Compact controls part type, not storage type (default is Full storage whose setPreferredFileOrder is a no-op), so an empty-return regression would still pass AGREE — fixed Added min_bytes_for_full_part_storage = '1G' so the part uses Packed storage, which actually consumes the hint; the test now asserts part_storage_type = Packed and reads the data back (a broken archive would fail the read). Both directions re-verified.
2 💡 Comment inaccurately said merges "re-materialize" the wider index; ordinary merges reuse source indices via MinMaxIndex::merge; use store not store() per convention AGREE — fixed Comment tightened to the local invariant only (motivation moved to the commit message per repo convention).
3 💡 no-shared-merge-tree tag excludes sibling coverage for a rationale that cannot affect the oracle DISAGREE Retained: sibling tests 04146 (the crashing test) and 04148/04149 carry it for the same block-number machinery; the "exactly one active part after OPTIMIZE FINAL" oracle is unreliable under SharedMergeTree (different merge assignment / part naming / block-number allocation). Keeping it avoids SMT flakiness; the path is already covered by the non-SMT run.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-author-slot-6:20260723-010500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @CheSema — could you review this? It fixes a LOGICAL_ERROR in MinMaxIndex::getProbablyWrittenFiles that aborts a merge when a packed compact part still carries a wider min-max index (with _block_number/_block_offset) after part_minmax_index_columns was lowered to partition_key_only. The function builds only a best-effort packed-archive file-order hint, so it now emits the same leading prefix store writes instead of requiring an exact size match.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [285a27f]

Summary: ✅


AI Review

Summary

This PR fixes MinMaxIndex::getProbablyWrittenFiles so the packed-part file-order hint follows the same leading-prefix behavior that store already uses after part_minmax_index_columns changes. I reviewed the changed MergeTree paths and the new regression test, and I did not find correctness, compatibility, or evidence gaps in the current version of the patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.20% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.30% 78.30% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 285a27f

CI fully finished: 161 checks green, 0 failures. No red lines to own; the PR is CI-clean and ready for review.

Session id: cron:our-pr-ci-monitor:20260723-213000

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Jul 31, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 31, 2026
Merged via the queue into ClickHouse:master with commit cc710ac Jul 31, 2026
180 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants