Skip to content

Support skip indexes in ranges refiner - #112296

Merged
CurtizJ merged 18 commits into
ClickHouse:masterfrom
CurtizJ:skip-indexes-in-ranges-refiner
Aug 8, 2026
Merged

CurtizJ merged 18 commits into
ClickHouse:masterfrom
CurtizJ:skip-indexes-in-ranges-refiner

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Additional context

Added support for refining MergeTree ranges using the analysis result of skip indexes, in addition to the result of the projection index introduced in #110291. PR is not for the changelog, because the changelog should have one entry from #110291.

Version info

  • Merged into: 26.8.1.999 (included in 26.8 and later)

CurtizJ and others added 3 commits July 28, 2026 13:33
Generalize `ProjectionIndexReadRangesRefiner` into `IndexReadRangesRefiner`:
mark ranges fully filtered out by indexes evaluated at data-read time are
dropped in MergeTree read pools before a read task is created for them.
Previously only the projection index bitmap was applied this way; now the
skip index read result (`use_skip_indexes_on_data_read`), including the
top-k threshold check and runtime filters from JOIN, is applied as well.

The granule-skipping predicate is factored into
`MergeTreeIndexReadResult::canSkipMark`, shared between the refiner and
`MergeTreeReaderIndex`, so task-cut time and read time decisions cannot
diverge. Applying the top-k threshold earlier is sound because
`TopKThresholdTracker` only tightens monotonically.

Replace the setting `use_projection_index_in_read_pools` (introduced in
the same unreleased version) with a single common setting
`use_indexes_refiner_in_read_pools` that covers both index kinds.

Follow-up to: ClickHouse#110291

Co-Authored-By: Claude Fable 5 <[email protected]>
With a `set` index over ~350k granules the index granule read dominates
the query (~1s measured via `FilteringMarksWithSecondaryKeysMicroseconds`)
and hides the refiner effect below the report threshold (~3%). A `minmax`
index prunes the same granules (`needle` sorts above every `hay_*` value)
but is much cheaper to read, making the refiner win visible: ~29% on both
the default and the prefetched read pool in local runs.

Co-Authored-By: Claude Fable 5 <[email protected]>
Numeric min/max comparisons are cheaper and more representative for a
minmax index than strings, and the pruning logic is easier to see: hay
granules cover [0, 63] while the needle value 9999 lies outside every
such range. Pruning is unchanged: 196 of 351563 marks selected, the
rest dropped by the ranges refiner at task-cut time.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3212298]

Summary: ❌

job_name test_name status info comment
Stress test (arm_release) FAIL
Logical error: Not-ready Set is passed as the second argument for function 'A (STID: 0250-3e49) FAIL cidb IGNORED

AI Review

Summary

This PR generalizes the MergeTree read-pool ranges refiner from projection-index bitmaps to read-time skip indexes and adds regression coverage for runtime filters, ParallelReplicas, and S3/object-storage reads. The execution-path changes look consistent, but the setting rename is not propagated to the autogenerated settings docs, so the published docs would still advertise only the removed name.

Findings

⚠️ Majors

  • [src/Core/Settings.cpp:5694] Introducing use_indexes_refiner_in_read_pools without regenerating the autogenerated settings docs leaves user-facing docs and search out of sync. docs/reference/settings/session-settings/use.mdx:183 and docs/reference/settings/session-settings/manifest.json:1244 still expose only use_projection_index_in_read_pools, so users looking up the new setting on the docs site will not find it.
    Suggested fix: regenerate the settings docs/manifest (and the legacy-route asset they feed) after the rename so the new setting name is published.
Final Verdict

Needs changes: regenerate the autogenerated settings documentation for the renamed setting before merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.60% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 97/99 (97.98%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 28, 2026
CurtizJ and others added 2 commits July 28, 2026 14:46
The CI test profile sets `max_rows_to_read` (tests/config/users.d/limits.yaml),
and with the default `read_overflow_mode = 'throw'` this disables applying skip
indexes at data-read time entirely (`supportsSkipIndexesOnDataRead`): the skip
index is applied during index analysis instead and the ranges refiner has
nothing to drop, so the ProfileEvents assertions returned 0. Pin
`max_rows_to_read = 0` in the test.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=a9232d5bc1c5a71896a1e4abbaa67211636508f1&name_0=PR&name_1=Fast%20test
PR: ClickHouse#112296

Co-Authored-By: Claude Fable 5 <[email protected]>
@KochetovNicolai KochetovNicolai self-assigned this Jul 28, 2026
CurtizJ and others added 3 commits July 30, 2026 21:52
The skip index reader applies JOIN runtime filters with a once-per-part,
fail-open snapshot. The refiner builds the per-part index read result at
task-cut time, possibly before the build side has published the filters,
which would cache the result without runtime pruning for the whole part.
Do not create the refiner when runtime filters can prune through the
primary key or dynamic skip indexes, so the result is built at reader
initialization as before.

Resolves review comment:
ClickHouse#112296 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
The setting was already present in the SettingsRandomizer dict; the
second key silently overrode the first.

Co-Authored-By: Claude Fable 5 <[email protected]>
@CurtizJ

CurtizJ commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

I've tested this PR on S3 and noticed that some queries may slow down due to increased fragmentation of read ranges and a higher number of GET requests. I've fixed it in 381c5ff.

Examples of queries:

With text index INDEX idx_message (message) TYPE text(tokenizer = 'splitByNonAlpha', preprocessor = lower(message), posting_list_codec = 'bitpacking')

With refiner:

SELECT message
FROM text_log_large_indexed_v3
WHERE hasAllTokens(message, 'key=2046511834937189626')
SETTINGS enable_filesystem_cache = 0, enable_parallel_replicas = 0, use_indexes_refiner_in_read_pools = 1
0%		0.116 sec.
50%		0.134 sec.
90%		0.156 sec.

Without refiner:

SELECT message
FROM text_log_large_indexed_v3
WHERE hasAllTokens(message, 'key=2046511834937189626')
SETTINGS enable_filesystem_cache = 0, enable_parallel_replicas = 0, use_indexes_refiner_in_read_pools = 0
0%		0.283 sec.
50%		0.303 sec.
90%		0.333 sec.

With set index INDEX ix_pr pull_request_number TYPE set(100) GRANULARITY 1. It was 3 times slower without the fix from 2de0d16.

With refiner:

SELECT count()
FROM text_log_large_indexed_v3
WHERE pull_request_number = 56944
SETTINGS enable_filesystem_cache = 0, enable_parallel_replicas = 1, use_indexes_refiner_in_read_pools = 1
0%		1.073 sec.
50%		1.107 sec.
90%		1.580 sec.

Without refiner:

SELECT count()
FROM text_log_large_indexed_v3
WHERE pull_request_number = 56944
SETTINGS enable_filesystem_cache = 0, enable_parallel_replicas = 1, use_indexes_refiner_in_read_pools = 1
0%		1.465 sec.
50%		1.650 sec.
90%		1.748 sec.

`04653_join_runtime_filters_index_analysis_refiner` asserts that
`RuntimeFilterGranulesConsidered` and `RuntimeFilterGranulesDropped` are
non-zero, but its probe side (`refiner_fact`) has only 2000 rows, while
`clickhouse-test` randomizes `join_runtime_filter_min_probe_rows`
uniformly over 1..10000. Whenever the draw exceeds 2000 no runtime filter
is built at all and both counters collapse to zero, so the test prints
`0 0` instead of `1 1`. All 14 failing runs in CI drew a value above 2000,
the smallest being 2036.

Pin `join_runtime_filter_min_probe_rows = 0`, and also pin
`query_plan_optimize_join_order_randomize = 0` because the asserted
granule-pruning counts depend on which side of the join builds the filter.
These are the same two pins that `04490`, `04611` and `04612` already
carry; the test was modelled on `04611` before they were added there.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=381c5ffae13e6bc74d8cdf677b773e55395ec25c&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29
Related: ClickHouse#112296
Related: ClickHouse#111691

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 101/105 (96.19%) · Uncovered code

Full report · Diff report

CurtizJ and others added 5 commits August 2, 2026 15:29
`merge_tree_read_split_ranges_into_intersecting_and_non_intersecting_injection_probability`
is randomized by the test harness and drawn per query. When it fires,
`splitPartsWithRangesByPrimaryKey` replaces the read pool with in-order
layer reads and the mark on a layer boundary is read by both layers, so
`read_rows` grows by one granule while `SelectedMarks` and the runtime
filter pruning counters stay identical. The two runs of the test flip the
coin independently, which breaks the `read_rows` parity assertion.

The effect needs `num_streams > 1`, so it only shows up on remote storage:
on a local disk the small part collapses to a single stream and the draw
is inert. Hence the failure was exclusive to the s3 storage job.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=e865dc2ac565051199b4226e12835ebf67a4745e&name_0=PR
ClickHouse#112296

Co-Authored-By: Claude Opus 5 <[email protected]>
The merge of `master` into this branch kept both the renamed history
entry `use_indexes_refiner_in_read_pools` and the old
`use_projection_index_in_read_pools` entry that master still carries
from ClickHouse#110291, while
`Settings.cpp` merged cleanly and declares only the new name.

`SettingsImpl::applyCompatibilitySetting` calls `getTier` for every
history entry newer than the requested version, and `getTier` throws
`UNKNOWN_SETTING` for a name that is not declared. A dangling entry
therefore makes every `SET compatibility = ...` fail, which broke 25
otherwise unrelated fast tests.

The old name was introduced in the same unreleased 26.8 cycle and never
shipped, so its history entry is simply dropped.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=c3a5f3209fa04b588d5c4cfc51921e9bdeae8ad8&name_0=PR&name_1=Fast%20test
Related: ClickHouse#112296

Co-Authored-By: Claude Opus 5 <[email protected]>
`use_projection_index_in_read_pools` was renamed to
`use_indexes_refiner_in_read_pools` in this pull request. The previous
commit dropped its settings history entry, which the style check
`settings_changes_history` rejects: a removed record is
indistinguishable, from the diff alone, from a default change that was
reverted without being recorded, and the check's only exemption is a
change touching `src/Core/SettingsChangesHistory.cpp` alone - which a
rename can never be, since it edits `src/Core/Settings.cpp` too.

Declare the old name in `OBSOLETE_SETTINGS` and restore its history
entry. `applyCompatibilitySetting` skips entries whose tier is
`OBSOLETE`, so `SET compatibility` no longer throws `UNKNOWN_SETTING`,
and `03999_stateless_settings_history` ignores obsolete settings. It
also keeps configurations written against master, where the old name has
been present since
ClickHouse#110291, from failing to
load.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=318ff84832ea55bcc8c68f33e8860a4df16d744a&name_0=PR&name_1=Style%20check
Related: ClickHouse#112296

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 3212298 - the build was skipped, reused from cache, or predates profile upload.

@CurtizJ

CurtizJ commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

@KochetovNicolai, could you review the latest commits, please?

grantholly-clickhouse pushed a commit to grantholly-clickhouse/ClickHouse that referenced this pull request Aug 7, 2026
The parameter is redundant since commit fbacf3a, which keeps
`IMergeTreeReader::all_mark_ranges` in sync with the current task's
mark ranges on the reader-reuse path (`MergeTreeReadPoolBase::createTask`).
At every call site the passed value was equal to
`getLastMark(reader->all_mark_ranges)`, so the right bound of ranged
read requests on remote disks is now derived inside the reader from its
own `all_mark_ranges` (cached in the `last_mark_to_read` member).

This also fixes multiplied S3 read requests when a step of the readers
chain reads over fragmented mark ranges: `continueReadingChain` computed
the bound from the ranges started by the current read block only, not
from the whole task, so with fragmented task ranges (e.g. a skip index
applied at index analysis time plus `PREWHERE`) the bound advanced on
every read block, and every advance cancelled the buffer's prefetch and
issued a new small read request.

Patch readers are now refreshed with their per-task mark ranges on the
reader-reuse path (`MergeTreeReadTask::Readers::updateAllMarkRanges`),
because their right bound now also comes from their own
`all_mark_ranges`.

Related: ClickHouse#112296
Related: ClickHouse#112296 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@CurtizJ
CurtizJ force-pushed the skip-indexes-in-ranges-refiner branch from 6c239f5 to 3042e31 Compare August 7, 2026 17:24
@CurtizJ
CurtizJ force-pushed the skip-indexes-in-ranges-refiner branch from 3042e31 to c8fcfdd Compare August 7, 2026 17:26
CurtizJ and others added 2 commits August 7, 2026 17:55
Test `04812_prewhere_fragmented_ranges_s3_read_requests` now runs the same
`PREWHERE` query twice: with the mark ranges of a read task fragmented by
index analysis, and with them fragmented by the ranges refiner in read pools
(`use_indexes_refiner_in_read_pools`). Both cases assert that the number of
S3 read requests stays low; the refiner case also asserts that the refiner
did drop marks, so it cannot silently degrade into the analysis path.

Checked against the release build of the last commit without the fix for read
fragmentation (`2de0d16b63b`): the analysis case issues 99 S3 read requests
and the refiner case 189 (with 3375 marks dropped by the refiner), versus 7
and 3 with the fix.

Test `04654_skip_indexes_refiner_remote_disk` is removed. It asserted the
shrink-only refinement of ranges of parts on remote disks, which was dropped
in `c8fcfdd09cc` because the right bound of ranged read requests now spans
the whole read task.

Build report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112296&sha=2de0d16b63bf36975d10da3068a0d9fc025c7825&name_0=PR&name_1=Build+%28amd_release%29
Related: ClickHouse#112296
Related: ClickHouse#113584

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR renames the read-pool projection-index setting to use_indexes_refiner_in_read_pools and extends the read-pool mark-range refiner to also cover skip indexes, with a guard so JOIN runtime filters aren't snapshotted early. The refiner is gated behind a setting that defaults off, and everything on the default path is an identical-logic refactor (canSkipMark/canSkipAnyMark moved onto the index read result). ClickBench Q16 and Q17 are plain aggregation scans that don't enable the refiner, so neither flagged delta (Q17 +19.0%, Q16 +11.6% and already within master's variance band) can be attributed to this change — both read as run-to-run variance. Q17 is downgraded to not_sure accordingly.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 16 not_sure 733 818 +11.6% <0.0001 aggregation: Q16 is a plain GROUP BY aggregation scan; the +11.6% sits inside master's variance band and the changed read-pool index refiner (default off) isn't exercised here, so this is run-to-run variance.
⚠️ 17 not_sure 474 564 +19.0% <0.0001 aggregation: The +19.0% can't come from this PR: the new read-pool index refiner is an opt-in setting that defaults off, and the rest is an identical-logic refactor a plain GROUP BY scan never touches. Reads as run-to-run variance.

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: ebdccca8-fd90-45fc-813e-7ae07dc13ba6
  • MIRAI run: 7fa493cd-b88f-40e0-9da0-577b769c70f6
  • PR check IDs:
    • clickbench_320340_1786195966
    • clickbench_320346_1786195966
    • clickbench_320357_1786195966
    • tpch_adapted_1_official_320374_1786195966
    • tpch_adapted_1_official_320384_1786195966
    • tpch_adapted_1_official_320411_1786195966

@CurtizJ
CurtizJ added this pull request to the merge queue Aug 8, 2026
Merged via the queue into ClickHouse:master with commit 4641beb Aug 8, 2026
179 of 181 checks passed
@CurtizJ
CurtizJ deleted the skip-indexes-in-ranges-refiner branch August 8, 2026 19:12
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 8, 2026
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.

3 participants