Skip to content

Do not let use_skip_indexes = 0 disable part min-max pruning - #123403

Merged
Michicosun merged 9 commits into
ClickHouse:masterfrom
aschoube:fix-minmax-pruning-use-skip-indexes
Oct 12, 2026
Merged

Michicosun merged 9 commits into
ClickHouse:masterfrom
aschoube:fix-minmax-pruning-use-skip-indexes

Conversation

@aschoube

@aschoube aschoube commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #123363
Related: #103952, #115271, #117355

Since 26.5, use_skip_indexes = 0 also turns off the per-part min-max index on the partition-key columns. Queries that set it (for example to bypass a bloom_filter index) stop pruning parts by the partition key and read every part the partition pruner does not remove. The issue reports a production query going from about 55 to about 2,400 selected parts, with p99 latency going from ~4 s to ~48 s.

#103952 added || !use_skip_indexes to skip_analysis of the min-max KeyCondition in ReadFromMergeTree::buildPartitionPruningIndexes, and the same check to MergeTreeData::getMinMaxCountProjectionBlock. The partition pruner next to both is still gated only on use_partition_pruning. use_skip_indexes is documented as "Use data skipping indexes during query execution." The part min-max index is not a data-skipping index: it covers the partition-key columns, plus _block_number / _block_offset with part_minmax_index_columns = with_block_number_offset.

The fix removes the use_skip_indexes check from both places, so the min-max index is gated on use_partition_pruning again, as before #103952. use_partition_pruning = 0 still disables it, which 03988_setting_use_partition_pruning and 04062_use_partition_pruning_disable_minmax_count_projection cover.

One existing test relied on the old behavior. The partition_key_prunes_something cell of 04652_enum_constant_vs_string_column set use_skip_indexes = 0 so that Min-Max would stay at 5/5 and only the Partition stage would prune. Min-Max now prunes 1/5 there with the same v in ['7', '7'] condition, so the cell compares the granules left after Partition with the granules Min-Max started from. It still fails if neither stage prunes, and it passes on both the old and the new build. The partition_key_condition_uses_name cell, which reads the Partition section's condition text, is unchanged.

04328_106533_partition_pruning_nan_inversion used use_skip_indexes = 0 as its unindexed oracle for the part-level min-max checks on t_106533_partlevel. With the fix that arm prunes too (Parts: 0/1 for val > 500), so the oracle now uses use_partition_pruning = 0, use_statistics_for_part_pruning = 0, which reads every part.

In getMinMaxCountProjectionBlock the gate only cost pruning: the partition pruner still ran, so results were correct. It is removed there too, for consistency.

04947_modulo_partition_key_alter_partition used use_skip_indexes = 0 so that only the partition pruner would exclude a partition. Min-Max now excludes it first, so the table is partitioned by c1 % 2, which Min-Max cannot prune by c1 = 2 but the partition pruner can.

With part_minmax_index_columns = 'with_block_number_offset', a condition on _block_number or _block_offset alone already satisfies force_index_by_date with the default use_skip_indexes = 1. With the gate removed this now also holds for use_skip_indexes = 0. force_index_by_date is not changed in this PR.

This also fixes the reproducer in #115271. With use_skip_indexes = 0, min-max pruning did not remove the p = 1 part, so the partition pruner evaluated intDiv(1, p - 1) on it and threw Division by zero. With the fix the query returns 0, as on 26.4. This does not fix the underlying problem: the partition pruner can still throw on a part that min-max pruning does not remove. The general fix for that is #117355, so this PR does not close #115271.

Test

05326_minmax_pruning_use_skip_indexes_repro is the issue's repro: 30 daily parts, each containing every k, with auto_statistics_types = '' so the Statistics step cannot hide the bug. With use_skip_indexes = 0 it expects Min-Max to select 1/30 parts. Without the fix it selects 30/30.

Benchmark

Official Docker images, clickhouse local. The table has 51.8M rows in 226 parts (180 daily partitions), ORDER BY k with every k in every part, and no statistics. The query is SELECT count() FROM t WHERE k = 3 AND ts::Date IN (SELECT toDate('2026-03-05')). Latency is the median of 10 warm runs on local NVMe:

Version use_skip_indexes Parts Rows Marks Median latency
26.4.5.143 0 1 8,192 1 3 ms
26.4.5.143 1 1 8,192 1 3 ms
26.10.1.1238 (head) 0 226 1,843,329 226 22 ms
26.10.1.1238 (head) 1 1 8,192 1 5 ms
this PR 0 1 8,192 1 4 ms
this PR 1 1 8,192 1 4 ms

On master, use_skip_indexes = 0 reads 226× the parts and 225× the rows. Locally that is about 4× the latency. On object storage each extra part means more GET requests, which is where the issue's 60× S3 GETs and 12× p99 come from. With the fix, use_skip_indexes = 0 reads the same single part as use_skip_indexes = 1. The "this PR" rows come from a local release build of this branch (clang 21.1.8), run natively rather than in Docker.

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):

Fixed use_skip_indexes = 0 also disabling part pruning by the partition-key min-max index (since 26.5), which made such queries read every part of the table.


Workflow [PR]
Sync PR [sync-upstream/pr/123403]

Version info

  • Merged into: 26.10.1.2200-master (included in 26.10 and later)
  • Backported to: 26.9.18.11, 26.8.26.5, 26.7.29.12

ClickHouse#103952 added `|| !use_skip_indexes` to `skip_analysis` of the per-part
min-max condition, both in `ReadFromMergeTree::buildPartitionPruningIndexes`
and in `MergeTreeData::getMinMaxCountProjectionBlock`. Since 26.5, queries with
`use_skip_indexes = 0` no longer prune parts by the partition-key min-max index
and read every part whose partition is not removed by the partition pruner.

`use_skip_indexes` controls data-skipping indexes only. Gate the part min-max
index on `use_partition_pruning` again, as before ClickHouse#103952 and as the partition
pruner next to it already is.

Fixes ClickHouse#123363.
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 2, 2026
The `partition_key_prunes_something` cell relied on `use_skip_indexes = 0`
leaving the Min-Max stage inert, so that only the Partition stage pruned.
Min-Max now prunes there too, so compare the granules left after Partition
with the granules Min-Max started from instead.
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e3a8470]

Summary: ✅


AI Review

Summary

This PR restores part-level partition-key Min-Max pruning when use_skip_indexes = 0 in both the normal read path and the _minmax_count_projection path, and it updates the affected stateless tests. The implementation change itself is consistent with the documented contract, but one rewritten regression oracle is no longer independent: the new use_partition_pruning = 0 fallback in 04328 can reuse query-condition-cache exclusions from the pruned arm, so the suite can miss the exact NaN-hiding regression it is trying to pin.

Findings

❌ Blockers

  • [tests/queries/0_stateless/04328_106533_partition_pruning_nan_inversion.sql:79] Replacing the old use_skip_indexes = 0 oracle with use_partition_pruning = 0 makes these arms share the same profiled query-condition-cache key as the default arm. ReadFromMergeTree writes whole-part exclusions from index analysis into the cache, and that key is salted by the skip-index profile / use_skip_indexes, not by use_partition_pruning. If the first query wrongly prunes the mixed part, the second query can reuse that cached 0/1 verdict and return the same wrong answer, turning this regression test into a false negative.
    Suggested fix: pin use_query_condition_cache = 0 for the t_106533_partlevel oracle pairs (mirroring the later t_106533_pk_minmax block), or otherwise make the fallback arm use a different QCC profile.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: make the t_106533_partlevel oracle independent again by disabling the query-condition cache for those paired queries (or by otherwise preventing cache-key reuse across the use_partition_pruning toggle).

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 2, 2026
@samay-sharma samay-sharma added the can be tested Allows running workflows for external contributors label Oct 2, 2026
With `part_minmax_index_columns = 'with_block_number_offset'` the part
min-max index also covers `_block_number` and `_block_offset`. A condition on
them alone made the min-max condition useful, so `force_index_by_date` did not
throw `INDEX_NOT_USED` although there is no condition on the partition key.
This already happened with the default `use_skip_indexes = 1`; after the
previous commit it also happens with `use_skip_indexes = 0`.

Require the min-max condition to use at least one partition-key column. The
block number columns come after the partition-key columns in the min-max
index, so it is enough to compare the used column positions.
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e3a847083 with master 2e77cbc01 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 64 s compile time in total, 8 of them have a recent master baseline.

Job report

- 04328: `use_skip_indexes = 0` no longer disables part-level min-max
  pruning, so it is not an unindexed oracle anymore. Use
  `use_partition_pruning = 0, use_statistics_for_part_pruning = 0` instead,
  which really reads every part.
- 05317: disable the query condition cache, which ignores
  `use_skip_indexes`, and statistics on the ClickHouse#115271 table, so the `Statistics`
  step can not remove the part before the partition pruner runs.
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 3, 2026
@aschoube

aschoube commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@samay-sharma CI is green here. Could you take a look, or point it to whoever reviews ReadFromMergeTree pruning? It fixes the min-max pruning regression you reported in #123363 (55 → ~2,400 parts with use_skip_indexes = 0).

@aschoube

aschoube commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@Michicosun I see you are the assignee of the issue, can you please review this PR?

Comment on lines +744 to +760
/// `force_index_by_date` requires a condition on the partition key. The min-max index may also cover
/// `_block_number` and `_block_offset` (see `part_minmax_index_columns`), which come after the
/// partition-key columns, so a condition on them alone does not count.
auto minmax_idx_uses_partition_key = [&]
{
if (!minmax_idx_condition)
return false;
const auto & condition = minmax_idx_condition->generateUnsubstituted();
if (condition.alwaysUnknownOrTrue())
return false;
const size_t num_partition_key_columns = MergeTreeData::getMinMaxColumns(
metadata_snapshot->getPartitionKey(), data.getSettings(), MergeTreePartMinMaxIndexColumns::PARTITION_KEY_ONLY).size();
for (size_t column : condition.getUsedColumns())
if (column < num_partition_key_columns)
return true;
return false;
};

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.

Why is this change needed to fix an issue?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Without the use_skip_indexes gate, the part min-max index is also built with _block_number / _block_offset when part_minmax_index_columns = 'with_block_number_offset'. A condition on them alone then made force_index_by_date pass, which it did not with use_skip_indexes = 0 before this PR. This change counts only the partition-key columns for force_index_by_date. Covered by 05318_force_index_by_date_minmax_block_number.

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.

That is okay, let's not change this in the current pr.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted the force_index_by_date change in MergeTreeDataSelectExecutor.cpp and removed 05318_force_index_by_date_minmax_block_number. The description now notes that a condition on _block_number or _block_offset alone satisfies force_index_by_date with use_skip_indexes = 0 too, as it already does with use_skip_indexes = 1.

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.

Let's create a simple test from the issue repro.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added 05326_minmax_pruning_use_skip_indexes_repro, the repro from the issue with use_skip_indexes = 0. It expects Min-Max to select 1/30 parts, and 30/30 without the fix.

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.

There are too many tests for a simple problem, let's leave only tests/queries/0_stateless/05326_minmax_pruning_use_skip_indexes_repro.sql

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed 05317 and 05318; 05326_minmax_pruning_use_skip_indexes_repro is the only new test.

The PR still adjusts 04328, 04652 and 04947, because they used use_skip_indexes = 0 to turn off part Min-Max pruning.

aschoube and others added 4 commits October 8, 2026 06:43
…_alter_partition`

`use_skip_indexes = 0` no longer disables the part-level `Min-Max` analysis, so the `pruned plan` check
failed. Use parts whose `c1` range covers the searched value, so only the partition pruner can exclude one.

Related: ClickHouse#123403

Co-authored-by: Copilot <[email protected]>
Revert the `force_index_by_date` change in `MergeTreeDataSelectExecutor` and remove
`05317_minmax_pruning_with_use_skip_indexes_disabled` and
`05318_force_index_by_date_minmax_block_number`, so the only new test is
`05326_minmax_pruning_use_skip_indexes_repro`.

Co-authored-by: Copilot <[email protected]>
@aschoube

Copy link
Copy Markdown
Contributor Author

Merged master to pick up the arm_tidy build fix. The only failure was Build (arm_tidy, 2/4) in gtest_throttler_constraint.cpp, which this PR does not touch.

Comment on lines 79 to +85
SELECT count() FROM t_106533_partlevel WHERE NOT ((val >= 0.) AND (val <= 3.));
SELECT count() FROM t_106533_partlevel WHERE NOT ((val >= 0.) AND (val <= 3.)) SETTINGS use_skip_indexes = 0;
SELECT count() FROM t_106533_partlevel WHERE NOT ((val >= 0.) AND (val <= 3.)) SETTINGS use_partition_pruning = 0, use_statistics_for_part_pruning = 0;

-- A positive range is decided by intersection alone, which a hidden NaN cannot make true, so the mixed
-- part is pruned for val > 500 as an all-finite one would be.
SELECT count() FROM t_106533_partlevel WHERE val > 500;
SELECT count() FROM t_106533_partlevel WHERE val > 500 SETTINGS use_skip_indexes = 0;
SELECT count() FROM t_106533_partlevel WHERE val > 500 SETTINGS use_partition_pruning = 0, use_statistics_for_part_pruning = 0;

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.

Changing the oracle from use_skip_indexes = 0 to use_partition_pruning = 0 makes these two "unindexed" arms share the same query-condition-cache key as the pruned arm.

ReadFromMergeTree writes whole-part exclusions from index analysis into the QCC, and that key is salted by the skip-index profile / use_skip_indexes, not by use_partition_pruning. So if the first query wrongly prunes the mixed part, the second query can reuse that cached 0/1 verdict even though it disabled partition pruning, and the regression turns into a false negative.

Please pin use_query_condition_cache = 0 for this oracle (at least these two pairs), like the later t_106533_pk_minmax case already does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The query condition cache can't carry a wrong min-max prune into the second query. Parts are pruned by the min-max index in filterParts, before index analysis, and only ranges that the primary key or skip indexes exclude from the remaining parts are written to the cache. A part dropped by min-max pruning is never recorded. Here the primary key is on id and the table has no skip index, so nothing about val > 500 is cached, and the second query reads the part.

@aschoube

Copy link
Copy Markdown
Contributor Author

@Michicosun can you add the to merge queue ?

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

This PR changes when the part-level min-max and partition pruning analysis is skipped: setting use_skip_indexes=0 no longer turns it off. With default settings nothing changes, and the ClickBench table has no partition key, so the measured queries never run the changed code. ClickBench Q30 (+29.6%) and Q31 (+30.1%) are correctly marked not_sure because both PR medians fall inside master's recent range. CPU time fell slightly for both, and the PR runs were much noisier, consistent with the two benchmarks sharing one host, so no performance effect can be attributed to this PR.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 30 not_sure 220 284 +29.6% <0.0001 aggregation: This PR only changes partition-key min-max pruning when use_skip_indexes=0, and ClickBench neither sets that nor partitions its table. CPU time fell slightly while wall time rose, which fits a busy shared node.
⚠️ 31 not_sure 259 337 +30.1% <0.0001 aggregation: Same situation as Q30: this PR's code path is inactive here, and CPU time fell slightly while wall time rose. The PR median sits at the top edge of master's normal range.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 07c591fc-33d9-414b-af89-7de9e53e1b28
  • MIRAI run: 7b95c890-442b-454f-9853-fa1e199d8216
  • PR check IDs:
    • clickbench_1028653_1791801439
    • clickbench_1028666_1791801439
    • clickbench_1028672_1791801439
    • tpch_adapted_1_official_1028681_1791801439
    • tpch_adapted_1_official_1028710_1791801439
    • tpch_adapted_1_official_1028724_1791801439

@Michicosun
Michicosun added this pull request to the merge queue Oct 12, 2026
Merged via the queue into ClickHouse:master with commit 942a30c Oct 12, 2026
163 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 12, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 12, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 12, 2026
Cherry pick #123403 to 26.7: Do not let use_skip_indexes = 0 disable part min-max pruning
robot-clickhouse added a commit that referenced this pull request Oct 12, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 12, 2026
Cherry pick #123403 to 26.8: Do not let use_skip_indexes = 0 disable part min-max pruning
robot-clickhouse added a commit that referenced this pull request Oct 12, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Oct 12, 2026
Cherry pick #123403 to 26.9: Do not let use_skip_indexes = 0 disable part min-max pruning
robot-clickhouse added a commit that referenced this pull request Oct 12, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 12, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #123403 to 26.8: Do not let use_skip_indexes = 0 disable part min-max pruning
clickhouse-gh Bot added a commit that referenced this pull request Oct 12, 2026
Backport #123403 to 26.7: Do not let use_skip_indexes = 0 disable part min-max pruning
PedroTadim added a commit that referenced this pull request Oct 13, 2026
Backport #123403 to 26.9: Do not let use_skip_indexes = 0 disable part min-max pruning
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 17, 2026
…f the Min-Max step

`(c0 IS NULL) >= true` and `(c0 IS NOT NULL) < true` reach the same single-point chain
over a NULL partition value, so the partitioned table answered 0 instead of 2 for them.

Since ClickHouse#123403, `use_skip_indexes = 0` no longer disables the part Min-Max condition, so
the two `assumeNotNull` EXPLAIN blocks printed a pruning Min-Max step on current master.
Use `isNull` / `isZeroOrNull` predicates there instead: they report no monotonicity, so
only the Partition step can use them on any base.

Closes ClickHouse#126369

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 23, 2026
…lace-build-mutex

Conflicts resolved:
- ReadFromMergeTree.cpp, MergeTreeData.cpp: keep master's `skip_analysis_`
  (no longer tied to `use_skip_indexes`, ClickHouse#123403), the PK factory without
  `relaxRangeAtomsOverNaNHidingTupleColumns`, and the vector similarity
  factory capturing `index_helper`; add the factories' `require_ready_sets`
  argument on top.
- MergeTreeIndexText.cpp: master's `params.enable_positions` plus
  `require_ready_sets`.
- ManifestFilesPruning.cpp: take master's version (ClickHouse#123085 made
  `require_ready_sets` a constructor parameter); pass `true` at the decode
  thread caller `ManifestFileIterator::getOrCreatePruner`, as this branch did
  inside the pruner before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. external Author is not a member of the ClickHouse organization pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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

6 participants