Skip to content

Support Virtual Columns for Granule MinMax Index - #104746

Merged
Michicosun merged 11 commits into
masterfrom
virtuals_in_granule_minmax
May 15, 2026
Merged

Michicosun merged 11 commits into
masterfrom
virtuals_in_granule_minmax

Conversation

@Michicosun

@Michicosun Michicosun commented May 12, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

This patch adds the ability to create a granule-level implicit min-max index for virtual columns block number and block offset, which, combined with #103952, can help filter all data based on the virtual block number and block offset predicates.

Version info

  • Merged into: 26.5.1.692

@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [76809c4]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb

AI Review

Summary

This PR adds implicit min-max index support for persistent virtual columns _block_number and _block_offset, plus merge-time wiring and tests. The core implementation is close, but there are still correctness gaps in setting/metadata synchronization and reserved-name protection that can leave stale metadata state or create duplicate reserved index names.

Findings
❌ Blockers
  • [src/Storages/AlterCommands.cpp:980] RESET SETTING updates settings_changes but does not run the same recompute flow used in MODIFY SETTING for metadata.add_minmax_index_for_block_* and implicit virtual indices. This violates the setting contract: after RESET SETTING enable_block_number_column, stale auto_minmax_index__block_number metadata can remain active.
    Suggested fix: after the reset loop, run the same effective-settings recomputation and dropImplicitIndicesForVirtualColumns/addImplicitIndicesForVirtualColumns synchronization used in the MODIFY SETTING branch.

  • [src/Storages/MergeTree/registerStorageMergeTree.cpp:870] reserved-name protection for auto_minmax_index__* is derived from collapsed flags (add_* && enable_*). With add_minmax_index_for_block_number_column = 1 and enable_block_number_column = 0, explicit reserved-name indices are still accepted at create time. Later enabling can append an implicit index with the same reserved name and violate unique-name invariants in secondary_indices.
    Suggested fix: reserve the prefix based on raw add_* intent (not collapsed runtime-enabled state), or reject enablement when conflicting explicit reserved-name indices exist.

  • [src/Storages/AlterCommands.cpp:721] the same collapsed-flag guard exists in ADD INDEX, so the reserved-name gap is reachable through ALTER TABLE ... ADD INDEX as well (not only CREATE TABLE).
    Suggested fix: align ADD INDEX guard with the create-path fix so reserved-name protection uses raw add_* intent (or add an explicit conflict check before enabling virtual implicit indices).

Tests
  • ⚠️ Add a focused stateless test for ALTER TABLE ... RESET SETTING enable_block_number_column (and _block_offset) that verifies implicit virtual index metadata is removed/re-added consistently.
  • ⚠️ Add a focused stateless test that proves reserved-name rejection remains enforced for both CREATE TABLE ... INDEX and ALTER TABLE ... ADD INDEX when add_minmax_index_for_block_* is set but enable_block_* is currently 0.
Final Verdict

Status: ⚠️ Request changes
Minimum required actions:

  1. Synchronize implicit virtual index metadata in the RESET SETTING path.
  2. Fix reserved-name protection to be intent-based (raw add_*) in both create and alter index paths.
  3. Add regression tests for both behaviors.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 12, 2026
Comment thread src/Storages/VirtualColumnUtils.cpp Resolved
@Michicosun
Michicosun force-pushed the virtuals_in_granule_minmax branch from 2c3cba8 to 084dc9c Compare May 12, 2026 19:04
@EmeraldShift

Copy link
Copy Markdown
Contributor

How does this compare to the commit_order projection index? I suppose this is better suited to bulk filtering _block_number > X, while the other is for finding a small number of rows matching some exact block number and offset?

@Michicosun

Copy link
Copy Markdown
Member Author

How does this compare to the commit_order projection index? I suppose this is better suited to bulk filtering _block_number > X, while the other is for finding a small number of rows matching some exact block number and offset?

I created a commit-order projection to support table reading in a row commit order without resorting at runtime. It is necessary to omit sorting large data sets. Initially, I made it to support streaming queries on top of the merge tree engine.

@EmeraldShift

Copy link
Copy Markdown
Contributor

I see. I had assumed the commit_order projection index only stored _parent_part_offset similar to normal projection indexes. If it's got e.g. * in the selected columns, now I understand how it can be used for pure sorting, along with #104680 :)

@CheSema CheSema self-assigned this May 13, 2026
@CheSema

CheSema commented May 13, 2026 •

Copy link
Copy Markdown
Member

I curios about the case when we add this index on the existed table. And how it would work for old parts where block number is not set for the rows. This behavior should be in descriptoin/documentation.

@Michicosun

Copy link
Copy Markdown
Member Author

I curios about the case when we add this index on the existed table. And how it would work for old parts where block number is not set for the rows. This behavior should be in descriptoin/documentation.

When the block number column is materialized on disk, the index will be written. For parts where the block number column was not materialized, it will be materialized with the part's minimum block number and written to the index with the same value.

Comment thread src/Storages/AlterCommands.cpp Resolved
any_mt_setting = true;
}
}
if (any_mt_setting)

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.

RESET SETTING still bypasses this metadata/index resync.

Concrete path:

  1. CREATE TABLE ... SETTINGS add_minmax_index_for_block_number_column = 1, enable_block_number_column = 1 (implicit auto_minmax_index__block_number is present)
  2. ALTER TABLE ... RESET SETTING enable_block_number_column

settings_changes is updated in the RESET_SETTING branch below, but the recalculation of metadata.add_minmax_index_for_block_* and metadata.secondary_indices is not re-run there. So the stale implicit virtual index remains in metadata even though the setting was reset to default (0).

Can we run the same recompute + dropImplicitIndicesForVirtualColumns/addImplicitIndicesForVirtualColumns flow for RESET_SETTING too?


auto using_auto_minmax_index = metadata.add_minmax_index_for_numeric_columns || metadata.add_minmax_index_for_string_columns
|| metadata.add_minmax_index_for_temporal_columns;
auto using_auto_minmax_index =

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.

using_auto_minmax_index here is derived from metadata.add_minmax_index_for_block_*, which is persisted as add_* && enable_*.

That means CREATE TABLE ... SETTINGS add_minmax_index_for_block_number_column = 1, enable_block_number_column = 0 does not reserve the auto_minmax_index__block_number prefix, so users can create an explicit index with that reserved name.

Later ALTER TABLE ... MODIFY SETTING enable_block_number_column = 1 calls addImplicitIndicesForVirtualColumns, which only checks index.column_names.front() == _block_number; it does not guard against an existing explicit index with the same reserved name on another expression. This can append a second index named auto_minmax_index__block_number and break the unique-name invariant for secondary_indices.

Can we keep reserved-name protection based on raw add_* intent (or reject enabling when a reserved-name explicit index already exists)?


auto using_auto_minmax_index = metadata.add_minmax_index_for_numeric_columns || metadata.add_minmax_index_for_string_columns
|| metadata.add_minmax_index_for_temporal_columns;
auto using_auto_minmax_index =

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.

ADD INDEX still uses metadata.add_minmax_index_for_block_* (which is persisted as add_* && enable_*) to decide whether the auto_minmax_index__* prefix is reserved.

That leaves a live gap:

  1. CREATE TABLE ... SETTINGS add_minmax_index_for_block_number_column = 1, enable_block_number_column = 0
  2. ALTER TABLE ... ADD INDEX auto_minmax_index__block_number (a) TYPE minmax GRANULARITY 1 (accepted here)
  3. ALTER TABLE ... MODIFY SETTING enable_block_number_column = 1

At step 3, addImplicitIndicesForVirtualColumns can append an implicit auto_minmax_index__block_number for _block_number, so secondary_indices can end up with duplicate reserved names.

Can we reserve the prefix based on the raw add_* intent (or reject enabling when such explicit reserved-name indices already exist), same as the create-path fix?

@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.80% 90.80% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 99.03% (205/207) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants