Skip to content

Add part-level aggregation for minmax skip indexes - #93318

Closed
zoomxi wants to merge 2 commits into
ClickHouse:masterfrom
zoomxi:skip-index-part-aggregation
Closed

zoomxi wants to merge 2 commits into
ClickHouse:masterfrom
zoomxi:skip-index-part-aggregation

Conversation

@zoomxi

@zoomxi zoomxi commented Jan 2, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Add part-level aggregation for MergeTree minmax skip indexes to enable part pruning before granule-level filtering, as discussed in #65413 and #64210.

Documentation entry for user-facing changes

  • [*] Documentation is written (mandatory for new features)

Details

  • Add allow_experimental_skip_index_part_aggregation setting to enable transparent part-level index aggregation;
  • Generate .pidx files during data insertion, merge operations, and MATERIALIZE INDEX;
  • Support fallback computation from existing .idx files for backward compatibility;
  • Integrate part-level pruning into query execution before granule-level filtering.

@EmeraldShift

Copy link
Copy Markdown
Contributor

What happens if one part with a materialized index and one part without it are merged?

@zoomxi

zoomxi commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

What happens if one part with a materialized index and one part without it are merged?

For a part with a materialized index, the .pidx file will be read directly. For a part without a .pidx file, it will fallback to compute the part-level aggregation from the granule-level .idx file at query time, and the computed result will be cached in memory. After the merge, a new part along with its .pidx file will be generated.

@EmeraldShift

Copy link
Copy Markdown
Contributor

This is out of scope for this PR, but I wonder if an additional optimization is possible: if a part is not pruned, and the KeyCondition cannot be false for the whole part, can we disable analysis for that index in that part? For example:

  • Index: minmax by timestamp
  • Part: min=123, max=456
  • Query: WHERE timestamp > 100

We are sure before reading the part that every granule will match this condition, because all values must be in the range 123..456, which all satisfies >100. So it would be nice to save the work of comparing every mark within the part when the solution is trivial.

(This applies more generally too: we can also skip reading the timestamp column if it's not used elsewhere, and the technique also applies at a partition, PK, and granule level)

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

clickhouse-gh Bot commented Jan 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [10790d8]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, parallel) failure
01287_max_execution_speed FAIL cidb, issue
Integration tests (amd_asan, db disk, old analyzer, 5/6) failure
test_keeper_snapshots/test.py::test_invalid_snapshot FAIL cidb
test_keeper_snapshots/test.py::test_snapshot_size FAIL cidb
Integration tests (amd_binary, 2/5) failure
test_keeper_invalid_digest/test.py::test_keeper_invalid_digest FAIL cidb
Integration tests (amd_binary, 3/5) failure
test_crash_log/test.py::test_crash_log_synchronous FAIL cidb, issue
test_crash_log/test.py::test_pkill_query_log FAIL cidb
Integration tests (amd_binary, 4/5) failure
test_projection_rebuild_with_required_columns/test.py::test_projection_rebuild_uses_only_required_columns FAIL cidb, issue
Integration tests (amd_binary, 5/5) failure
test_keeper_snapshots/test.py::test_invalid_snapshot FAIL cidb
test_keeper_snapshots/test.py::test_snapshot_size FAIL cidb
Integration tests (arm_binary, distributed plan, 1/4) failure
test_backup_restore_on_cluster/test_concurrency.py::test_create_or_drop_tables_during_backup[Replicated-ReplicatedMergeTree] FAIL cidb, issue
test_backup_restore_on_cluster/test_concurrency.py::test_create_or_drop_tables_during_backup[Memory-MergeTree] FAIL cidb, issue
test_backup_restore_on_cluster/test_concurrency.py::test_create_or_drop_tables_during_backup[Lazy-Log] FAIL cidb, issue
test_backup_restore_on_cluster/test_concurrency.py::test_kill_mutation_during_backup FAIL cidb, issue
Integration tests (arm_binary, distributed plan, 3/4) failure
test_keeper_snapshots/test.py::test_invalid_snapshot FAIL cidb
test_keeper_snapshots/test.py::test_snapshot_size FAIL cidb
test_keeper_invalid_digest/test.py::test_keeper_invalid_digest FAIL cidb
Integration tests (amd_tsan, 2/6) failure
test_backup_restore_on_cluster/test_disallow_concurrency.py::test_concurrent_backups_on_different_nodes FAIL cidb, issue
test_backup_restore_on_cluster/test_disallow_concurrency.py::test_concurrent_restores_on_same_node FAIL cidb, issue
test_backup_restore_on_cluster/test_disallow_concurrency.py::test_concurrent_restores_on_different_node FAIL cidb, issue
Integration tests (amd_tsan, 5/6) failure
test_storage_s3_queue/test_6.py::test_ordered_mode_with_hive[AzureQueue-1-1-2] FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jan 2, 2026
@EmeraldShift

EmeraldShift commented Jan 2, 2026 •

Copy link
Copy Markdown
Contributor

Based on the output of 01786_explain_merge_tree, it looks like the aggregated skip index is applied before even partition pruning. My intuition would be that partition pruning first is cheaper, since data within a partition doesn't need to be read, while in the worst case a skip index may need to be fully read to derive the aggregated value. Maybe I'm missing something?

EDIT: actually the partition-key-based MinMax check also happens before checking the partition key, huh. Makes we wonder if this could be merged with that and if both could be configurable as before or after partition pruning in cases where the partition filter is very selective (e.g. date = ...)

@zoomxi
zoomxi force-pushed the skip-index-part-aggregation branch from 5f00b48 to 73a4c4e Compare January 4, 2026 03:03
@zoomxi

zoomxi commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

Based on the output of 01786_explain_merge_tree, it looks like the aggregated skip index is applied before even partition pruning. My intuition would be that partition pruning first is cheaper, since data within a partition doesn't need to be read, while in the worst case a skip index may need to be fully read to derive the aggregated value. Maybe I'm missing something?

At pruning time, all three are simple in-memory comparisons with similar costs. The current order follows existing pattern.

@zoomxi

zoomxi commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

The CI failure seems unrelated to my changes.

MergeTreeIndexSubstreams getSubstreams() const override { return {{MergeTreeIndexSubstream::Type::Regular, "", ".idx2"}}; }
MergeTreeIndexFormat getDeserializedFormat(const MergeTreeDataPartChecksums & checksums, const std::string & path_prefix) const override; /// NOLINT

bool supportsPartLevelAggregation() const override { return true; }

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.

Two questions:

  1. In the current implementation, is there a reason why part-level indexes are limited to min-max indexes? I think we can merge at least also SET indexes. Merging bloom filter indexes isn't feasible (without saturating the index, i.e. making it useless)

  2. That brings me to the more important point. Please don't beat me for saying that but the merging mechanism is a crutch. It optimizes for the wrong goal. Its advantage is that it can re-use existing metadata at part write time to save some CPU. The disadvantage is that it is limited to min-max (and set) indexes. Imagine a another mechanism where the part-level index is built from scratch. Advantage: we could use arbitrary data structures as metadata Disadvantage: slightly longer construction time but that's fine in ClickHouse. The thing is that just that already exists: https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#column-statistics, there is even a min-max type of statistics. I would suggest that we rather use this. We plan to enable the statistics by default for numeric types soon, then it would be more ubiquitous than today.

@rschu1ze rschu1ze Jan 8, 2026 •

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.

This PR has another advantage which I forgot: Min max indexes can be build on top of arbitrary expressions, statistics only on columns. But that's not a great loss, IMHO.

@rschu1ze

rschu1ze commented Jan 8, 2026

Copy link
Copy Markdown
Member

@hanfei1991 FYI

@zoomxi

zoomxi commented Jan 12, 2026

Copy link
Copy Markdown
Contributor Author

@rschu1ze Thanks for your valuable feedback! I agree that Statistics should be the primary source for part-level MinMax metadata.

I'm considering introducing a new abstraction layer called ZoneMap to unify Statistics and Skip Index part-level aggregation, providing part pruning and aggregate query optimization.

ZoneMap data sources:

  1. MinMax: Prioritized in this order:

    • Column Statistics
    • Skip Index aggregation (for expressions not covered by Statistics)
  2. Set: Skip Index aggregation (limited by max rows threshold)

  3. Hypothesis: Skip Index aggregation

Separation of concerns:

  • Statistics → Query Optimizer (selectivity estimation)
  • ZoneMap → I/O optimization (part pruning + aggregate queries like SELECT MIN(col))

This aligns with how other OLAP systems handle this.

The ZoneMap abstraction layer is also extensible - in the future, it could integrate additional metadata such as null counts or other metrics.

Does this direction make sense? I'd appreciate your suggestions.

@nickitat

Copy link
Copy Markdown
Member

We briefly discussed this proposal with hanfei1991@. I'd agree that re-using the existing Statistics code looks preferable. The biggest missing part for now is that we don't apply statistics to parts pruning. So, IMO:

  1. We need to implement a new logic that will apply [certain types of] statistics to pruning parts
  2. The users will need to either:
  • enable auto_statistics_type='minmax' and forget about it (at some point it will already be enabled for them automatically)
  • or declare Statistics(MinMax) for each selected column, if they want only a limited subset of columns to be covered
  1. The decision of creating a skip index for any of those columns will be completely orthogonal, and up to them also. I.e., remains as it is today.

@zoomxi

zoomxi commented Jan 13, 2026 •

Copy link
Copy Markdown
Contributor Author

@nickitat Thanks for your reply. I understand now that reusing the statistics data is enough. I will continue working on implementing statistics application for parts pruning.

@zoomxi zoomxi closed this Jan 13, 2026
@zoomxi zoomxi reopened this Jan 13, 2026
@zoomxi zoomxi closed this Jan 13, 2026
@rschu1ze

Copy link
Copy Markdown
Member

By the way, using statistics for pruning as @nickitat proposed introduces an interesting requirement: Statistic must not "lie" about the data, otherwise we'll prune too much and get wrong results. This is different from join reordering where lying statistics are okay.

@zoomxi
zoomxi deleted the skip-index-part-aggregation branch May 12, 2026 02:43
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-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants