Skip to content

Separate text index analysis from MergeTreeReaderTextIndex - #102255

Merged
CurtizJ merged 7 commits into
ClickHouse:masterfrom
CurtizJ:better-text-index-analysis
Apr 14, 2026
Merged

CurtizJ merged 7 commits into
ClickHouse:masterfrom
CurtizJ:better-text-index-analysis

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Apr 9, 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):

Improved analysis of multiple text indexes used simultaneously in the same query. Also, the use_skip_indexes_on_data_read setting is no longer required for the direct read optimization of text indexes.

Version info

  • Merged into: 26.4.1.910

Move Stage 1 of text index analysis (dictionary + small postings reading)
to always happen in `filterMarksUsingIndex`, alongside other skip indexes.
The pre-computed granule is captured and passed to `MergeTreeReaderTextIndex`
so it skips re-reading from streams and goes straight to Stage 2
(large postings reading + virtual column filling).

This fixes three problems:
- `canSkipMark` is only honored for the first prewhere reader, so multiple
  text indexes or mixed skip indexes lost mark filtering.
- Direct read from text index required `use_skip_indexes_on_data_read = 1`.
- Enables future distributed text index analysis.

The granule flows through `RangesInDataPartReadHints.index_granules` (Path 1:
pre-execution) or `SkipIndexReadResult.index_granules` (Path 2: data-read).
Each reader gets a `shallowCopy` of the granule for thread-safe independent
`current_range`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ce945bd]

Summary: ❌

job_name test_name status info comment
BuzzHouse (amd_tsan) failure
Logical error: '!res.empty()' (STID: 2508-452d) FAIL cidb IGNORED

AI Review

Summary

This PR refactors text-index direct-read flow by moving index analysis out of MergeTreeReaderTextIndex, propagating precomputed granules via read hints/result pools, and relaxing the use_skip_indexes_on_data_read prerequisite. High-level direction looks reasonable, but there is one blocker: existing prefetch coverage for remote filesystem text-index reads was removed without equivalent replacement.

Findings

❌ Blockers

  • [tests/queries/0_stateless/02346_text_index_prefetch.sql:1, tests/queries/0_stateless/02346_text_index_prefetch.reference:1] The PR deletes explicit coverage that RemoteFSPrefetchedReads is exercised for text-index reads on s3_cache. Newly added tests validate correctness/selectivity but do not verify prefetch behavior, so a regression in remote prefetch path can pass unnoticed.
    • Suggested fix: restore this test (or add an equivalent new test) that checks ProfileEvents['RemoteFSPrefetchedReads'] > 0 for the text-index read path.
Tests
  • ⚠️ Re-add a prefetch-focused stateless test for text-index reads on remote storage (s3_cache) that validates RemoteFSPrefetchedReads remains positive under deterministic settings.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ❌ Removed 02346_text_index_prefetch without equivalent replacement coverage.
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Missing prefetch regression coverage weakens rollout safety for remote-storage deployments.
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Restore or replace the removed prefetch test coverage for text-index reads on remote filesystem (RemoteFSPrefetchedReads).

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Apr 9, 2026
@EmeraldShift

Copy link
Copy Markdown
Contributor

Also, the use_skip_indexes_on_data_read setting is no longer required for the direct read optimization of text indexes.

Just to double check: does this mean that I can set use_skip_indexes_on_data_read = 0 to eagerly analyze the Text index, and it will still use the direct read optimization? That would be awesome! I am seeing some bad performance regression with use_skip_indexes_on_data_read = 1 (the default) because it loads all marks and then checks every granule; glad to see I could still avoid processing all rows in the matching granules with the deferred skip index disabled 😁

@CurtizJ

CurtizJ commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

Just to double check: does this mean that I can set use_skip_indexes_on_data_read = 0 to eagerly analyze the Text index, and it will still use the direct read optimization?

Yes, that's correct.

}
}

void MergeTreeReaderTextIndex::prefetchBeginOfRange(Priority priority)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unfortunately, we have to sacrifice prefetches for now. Maybe I'll add prefetches for skip indexes in the general case later.

@CurtizJ
CurtizJ marked this pull request as draft April 9, 2026 17:23
@CurtizJ
CurtizJ marked this pull request as ready for review April 9, 2026 17:23
@ClickHouse ClickHouse deleted a comment from alexey-milovidov Apr 10, 2026
@ClickHouse ClickHouse deleted a comment from alexey-milovidov Apr 10, 2026
@CurtizJ
CurtizJ requested review from Ergus, ahmadov and rschu1ze April 10, 2026 13:39
@ahmadov ahmadov self-assigned this Apr 13, 2026

@ahmadov ahmadov left a comment

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.

LGTM 👍

@Ergus Ergus left a comment

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.

I only have a question:

For remote-filesystem table policies MergeTreePrefetchedReadPool calls reader->prefetchBeginOfRange(priority) for every prewhere reader before the I/O window opens.

PrefetchIncrement watch(context->getAsyncReadCounters());
reader->prefetchBeginOfRange(priority);
};

Now every text index read becomes a synchronous, unmasked latency event (because there is not override) Is this intended?
This PR intentionally drops the prefetch on remote filesystems?

@CurtizJ

CurtizJ commented Apr 13, 2026 •

Copy link
Copy Markdown
Member Author

This PR intentionally drops the prefetch on remote filesystems?

Yes, this is intended. Prefetch in the text index only worked for the sparse index file. Now its analysis has been moved to the common code path in filterMarksUsingIndex, which does not have prefetch infrastructure yet. This is not a big problem, because reading tokens from the dictionary typically contributes more latency.

I have a prototype of prefetch support for skip indexes in general and will open a PR soon. However, we need to be careful with prefetches of skip indexes, as they can slow down queries. I also want to add prefetches for the text index dictionary later, but that requires a small refactoring.

@clickhouse-gh

clickhouse-gh Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

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

Changed lines: 97.12% (202/208) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@CurtizJ
CurtizJ added this pull request to the merge queue Apr 14, 2026
Merged via the queue into ClickHouse:master with commit 0e4c1ec Apr 14, 2026
159 of 161 checks passed
@CurtizJ
CurtizJ deleted the better-text-index-analysis branch April 14, 2026 01:04
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 14, 2026
CurtizJ added a commit to fastio/ClickHouse that referenced this pull request Apr 14, 2026
Resolve merge conflicts with PR ClickHouse#102255 (Separate text index analysis
from `MergeTreeReaderTextIndex`).

Key adaptations for the lazy posting list feature:
- Constructor: use master's `MergeTreeIndexGranulePtr` parameter and
  `initializeFallbackReader` extraction; add lazy mode validation before
  fallback reader initialization.
- `readGranule`: use master's `dynamic_pointer_cast<const MergeTreeIndexGranuleText>`
  pattern; keep `small_postings_stream` as a class member for lazy cursor
  handles; retain lazy mode codec validation after deserialization.
- `makeLazyCursorHandle`: adapt to use `granule->` direct access instead
  of `assert_cast`.
- `canSkipMark`: removed (per master's refactoring).
- `readRows`: follow master's simplified loop structure with conditional
  `readPostingsIfNeeded` skip for lazy mode.
- `fillColumn`: use `granule->` direct access for pattern tokens.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@zdyj3170101136

Copy link
Copy Markdown

please backport it to 26.2

@rschu1ze

rschu1ze commented Jul 6, 2026

Copy link
Copy Markdown
Member

@zdyj3170101136 Only bugfixes get backported, this PR is feature work.

CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Oct 10, 2026
`createReadTasksForTextIndex` added an `IndexReadTask` without columns for
every useful text index that is not read directly. The tasks used to keep
such indexes out of `MergeTreeSkipIndexReader`, but that exclusion was
removed in ClickHouse#102255, and a
task without columns never gets a `MergeTreeReaderTextIndex`, because
read steps are matched to tasks by column names. The only remaining
effect was an empty PREWHERE read step per such index.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

7 participants