Skip to content

Fix Merges/Mutations with enabled Block Number/Offset Min-Max Index - #109281

Merged
Michicosun merged 17 commits into
masterfrom
enable_virtual_columns_minmax_index
Jul 28, 2026
Merged

Michicosun merged 17 commits into
masterfrom
enable_virtual_columns_minmax_index

Conversation

@Michicosun

@Michicosun Michicosun commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

This patch fixes the remaining problems needed for part-level min-max index for _block_number and _block_offset columns.

Version info

  • Backported to: 26.7.2.33

@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2487fce]

Summary: ✅


AI Review

Summary

This PR tries to close the remaining merge and mutation gaps around part-level min-max support for _block_number and _block_offset, but two important paths are still incomplete in the current code. I would not merge it in this state because the advertised "with_block_number_offset works by default" contract is still broken for projection-part merges and for wide/full subset mutations.

PR Metadata
  • Changelog category = Improvement is reasonable, but the current Changelog entry overstates the result: this patch does not yet fix the remaining projection-merge and subset-mutation gaps. Suggested replacement: Improve merge and full-rewrite materialization of _block_number and _block_offset for part-level min-max indexes.
Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTask.cpp:713-714, 1038-1049] Projection-part merges still fall back to partition_key_only min-max as soon as parent_part is set. Commit-order projections persist _block_number and _block_offset as real projection columns and sort by them, so their merged projection parts still need minmax__block_number / minmax__block_offset when part_minmax_index_columns = 'with_block_number_offset'. With the current enabledBlock*Column guard both flags become false for every projection merge, so the first projection-part merge silently drops block-column pruning from the projection. The added test tests/queries/0_stateless/04510_minmax_block_columns_projection_merge.sql does not cover this path because it uses a normal projection and only one inserted part. Suggested fix: derive the min-max level from the projection part schema / new part columns instead of using parent_part as a proxy.
  • [src/Storages/MergeTree/MutateTask.cpp:630-639, 1510, 2776-2823] The new READ_COLUMN injection materializes _block_number / _block_offset on wide/full subset mutations, but MutateSomePartColumnsTask still writes through MergedColumnOnlyOutputStream and then finalizeMutatedPart copies the source part's old min-max index unchanged. As a result, commands such as UPDATE, MATERIALIZE INDEX, MATERIALIZE STATISTICS, and MATERIALIZE PROJECTION can now produce parts that store the block columns but still lack minmax__block_number / minmax__block_offset, so the promised part-level pruning remains unavailable until some later full rewrite. Suggested fix: either force any mutation that newly materializes these columns onto MutateAllPartColumnsTask, or teach the subset-mutation path to rebuild and store the part-level min-max files before finalization.
Final Verdict

⚠️ Changes requested. The patch still misses two common paths that need block-column part-level min-max coverage, so the current PR does not satisfy its stated contract yet.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 3, 2026
@alesapin alesapin self-assigned this Jul 3, 2026
}

const bool has_block_columns = enabledBlockNumberColumn(global_ctx) && enabledBlockOffsetColumn(global_ctx);
global_ctx->minmax_idx_columns = MergeTreeData::getMinMaxColumns(global_ctx->metadata_snapshot->getPartitionKey(), global_ctx->data_settings, has_block_columns ? MergeTreePartMinMaxIndexColumns::WITH_BLOCK_NUMBER_OFFSET : MergeTreePartMinMaxIndexColumns::PARTITION_KEY_ONLY);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

parent_part is not a good proxy for "this merge cannot have block-column minmax". Commit-order projections already persist _block_number / _block_offset as ordinary projection columns (ProjectionIndexCommitOrder::fillProjectionDescription appends them to the projection schema), so with part_minmax_index_columns = 'with_block_number_offset' they still need minmax__block_number / minmax__block_offset on projection-part merges. With the new enabledBlock*Column check both flags become false for every projection merge, so merged commit-order projection parts are now capped to partition_key_only and lose pruning on those columns. Please key this off the actual projection-part schema / new-part columns instead of parent_part.

{
const auto data_settings = part->storage.getSettings();

if ((*data_settings)[MergeTreeSetting::enable_block_number_column] && !part_columns.has(BlockNumberColumn::name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Appending READ_COLUMN here fixes full rewrites, but it still leaves the wide/full partial-mutation path short of the advertised min-max behavior. Commands like MATERIALIZE INDEX, MATERIALIZE STATISTICS, and MATERIALIZE PROJECTION will now materialize _block_number / _block_offset on old parts, yet they still run through MutateSomePartColumnsTask; that path hardlinks the old minmax_*.idx set and MergedColumnOnlyOutputStream never writes new part-level min-max files. The mutated part ends up containing the block columns but still lacks minmax__block_number / minmax__block_offset, so part_minmax_index_columns = 'with_block_number_offset' remains ineffective until some later full rewrite. Either force these cases onto the full-rewrite path or teach the partial writer to rebuild/store min-max too.

@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.30% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

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

Full report · Diff report

@Michicosun Michicosun changed the title Enable virtual Min-Max index by default Fix Merges/Mutations with enabled Block Number/Offset Min-Max Index Jul 28, 2026
@Michicosun
Michicosun added this pull request to the merge queue Jul 28, 2026
Merged via the queue into master with commit 3d94646 Jul 28, 2026
179 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @alesapin — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently incomplete):

  • Why: The body only states 'fixes the remaining problems' without describing WHAT was broken (projection sub-part merges and mutations requesting min-max columns the part cannot store) or HOW it is fixed (capping the min-max column set to the columns the resulting part actually carries, and materializing block columns for interpreter-driven mutations).

  • Suggested addition:

    Describe the concrete failures: (1) merges of projection sub-parts requested _block_number/_block_offset min-max columns that projection parts never store; (2) mutations did not materialize block columns nor cap the min-max column set to the resulting part's columns. The fix caps getMinMaxColumns via a new up_to argument based on whether the resulting part actually contains both block columns, and forces READ_COLUMN materialization for interpreter-driven mutations.

Ignore this if the current title/body is intentional.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @alesapin — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

Revertionist pushed a commit to Revertionist/ClickHouse-fork that referenced this pull request Jul 29, 2026
…s during mutations

Setting part_minmax_index_columns = 'with_block_number_offset' put
_block_number and _block_offset into the per-part minmax index, but the
mutation read pipeline did not materialize them, so any mutation that
rewrites a part failed with

  Code: 341 ... Not found column or subcolumn _block_number in block.
  (NOT_FOUND_COLUMN_IN_BLOCK)

The engine side was fixed in ClickHouse#109281, which made mutations materialize
both columns. That PR covers merges and ALTER TABLE ... REWRITE PARTS in
04501_mutation_materialize_block_number_offset; this test pins the
interpreter-driven mutation shapes that were reported and were not
covered: ALTER UPDATE (the shape in the issue), RENAME COLUMN, DROP
COLUMN and DELETE.

Besides checking the mutations succeed, the test asserts the resulting
minmax ranges: a rewritten part keeps (1,1) / (0,8), and DELETE narrows
the _block_offset range to (1,8). Verified failing before ClickHouse#109281 and
passing after it.

Closes: ClickHouse#106780
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Jul 29, 2026
PedroTadim added a commit that referenced this pull request Jul 30, 2026
Cherry-pick of #109281 "Fix Merges/Mutations with enabled Block Number/Offset
Min-Max Index" to 26.6.

Conflicts were resolved keeping the 26.6 base text and applying only the
change of #109281 (pin `enable_block_number_column` / `enable_block_offset_column`
to 0):

- `02864_statistics_usage.sql`: 26.6 has no `default_compression_codec = 'LZ4'`
  pin, that comes from an unrelated master-only change.
- `03706_statistics_preserve_checksums_on_mutations.sh`: 26.6 uses the `minmax`
  statistics type where master uses `basic`.

Original pull-request #109281
Cherry-pick pull-request #112324

Co-Authored-By: Claude Fable 5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Jul 30, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants