Skip to content

Minor text index refactoring extracted from the BM25 PR - #124595

Merged
CurtizJ merged 10 commits into
ClickHouse:masterfrom
CurtizJ:text-index-refactoring-for-bm25
Oct 11, 2026
Merged

CurtizJ merged 10 commits into
ClickHouse:masterfrom
CurtizJ:text-index-refactoring-for-bm25

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Related: #117519

The changes of #117519 that do not depend on BM25 scoring, so that PR gets smaller.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

  • Merged into: 26.10.1.2125-master (included in 26.10 and later)

CurtizJ and others added 2 commits October 8, 2026 02:09
Extracted from ClickHouse#117519, which builds BM25 scoring on top of it.

Mechanical changes:
- Rename `MergeTreeIndexTextPostingListCursor.{h,cpp}` to `PostingListCursor.{h,cpp}` and `FlatPostingsPtr` to `PaddedPODArrayPtr`, defined in `IPostingListCodec.h`.
- Replace `MergeTreeIndexTextParams::positions` with the bool `enable_positions`.
- Replace the `skip_postings` arguments of `deserializeTokenInfo` and `deserializeDictionaryBlock` with `with_postings` without a default,
  and `MergeTreeIndexDeserializationState::skip_postings_deserialization` with `text_index_read_postings`.
- Make `loadHeader` and `matchTokens` free functions, extract `makeIndexIdForCaches`, `skipVarUInts` and `readVarUInts`,
  and make `serializeTokensAndPostings` a plain function.
- Take `std::span<const uint32_t>` in the posting list block codecs.
- Capture the index in the condition factory of the vector similarity index instead of a raw pointer to it.
- Remove the unused `IndexReadTask::is_final`.

Keep the search queries of the virtual columns of a text index in `IndexReadTask` instead of the condition:
`MergeTreeIndexConditionText` is no longer modified after construction. `replaceToVirtualColumn` becomes the const
`tryGetVirtualColumnName`, `getSearchQueryForVirtualColumn` is removed, and `MergeTreeReaderTextIndex` and
`optimizeTrivialCountFromTextIndex` take the queries from the read task, which `createMergeTreeReaderIndex` now receives whole.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A part may hold a substream of a skip index that the current definition does not write, e.g. the `.pos` of a text index
created with `support_phrase_search = 1` and redefined without it while the part was detached. `getAllSubstreamsInPart`
probed only `getSubstreams`, so a mutation that rebuilds the index did not see the stale `.pos` and hardlinked it into
the new part. It now probes `getPotentialSubstreams`, which also covers the legacy minmax `.idx`, so the override of
`MergeTreeIndexMinMax` is removed and the function is no longer virtual.

Extracted from ClickHouse#117519.

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

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7828b0d]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, s3 storage, DBReplicated, parallel, 1/2) FAIL
05339_parquet_prewhere_subcolumn_read_through_column FAIL cidb IGNORED
05339_parquet_prewhere_subcolumn_read_through_column FAIL cidb IGNORED
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) FAIL
05210_lightweight_update_materialized_cte_in_ignore FAIL cidb IGNORED
01513_optimize_aggregation_in_order_memory_long NOT_FAILED cidb IGNORED
02884_create_view_with_sql_security_option NOT_FAILED cidb IGNORED

AI Review

Summary

This PR extracts text-index refactoring from the BM25 work: it moves virtual-column/query state out of MergeTreeIndexConditionText, carries query/default-expression data in IndexReadTask, and broadens on-disk substream discovery so mutations can clean up stale legacy and text-index side files correctly. I did not find a new blocker or major correctness issue in the current revision, and the earlier orphan-substream review point is addressed in the current MutateTask logic.

Missing context / blind spots
  • ⚠️ No local build artifacts were present in the workspace, so this pass is static-only. Running 05343_text_index_stale_positions_after_redefinition together with the affected gtest_index_potential_substreams and gtest_posting_list_cursor targets would close that gap.
Final Verdict

✅ No new inline findings for the current PR revision.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 8, 2026
@CurtizJ CurtizJ changed the title Text index refactoring extracted from the BM25 scoring PR Minor text index refactoring extracted from the BM25 PR Oct 8, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Oct 8, 2026
`IndexReadTask` and `IndexReadColumns` kept the search query of each virtual
column in a separate map keyed by column name, so `createReadTasksForTextIndex`
and the text index reader had to join them back and throw a `LOGICAL_ERROR`
when the lookup failed.

Replace both with a single `IndexReadTask::Column` that holds the name, type,
search query and default expression. `createReadTasksForTextIndex` now builds
the `VirtualColumnDescription` from it, so the read tasks, which are copied for
every part, do not carry a `ColumnDescription` that clones its ASTs on copy.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
CurtizJ and others added 2 commits October 8, 2026 14:54
`hasAnyIndexFileOnDisk` and the orphan collector in `updateIndicesToRecalculateAndDrop`
walked `getSubstreams`, so a checksum-less `.pos` of a text index redefined without
`support_phrase_search` stayed invisible and was hardlinked into the mutated part.
Walk `getPotentialSubstreams` instead; it also covers minmax's legacy `.idx`, so the
hand-coded `.idx2` -> `.idx` probe is removed.

ClickHouse#124595 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`TextIndexDAGReplacer` carried the same virtual column information through
four structs: `SelectedCondition`, `AddedVirtualColumn`, `NodeReplacement`
and `ResultReplacement`.

`SelectedCondition` now holds an `IndexReadTask::Column` instead of a separate
search query and column name, so adding the virtual column only sets its
default expression. Added columns go straight into an `IndexReadColumns`
member, pruned by `replace` after `removeUnusedActions`, which removes
`AddedVirtualColumn`. The rewrite functions return the replacement node
and the virtual column input cache is a member, which removes
`NodeReplacement`. `replace` returns `std::optional<ResultReplacement>`
instead of the `is_dag_rewritten` flag.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
CurtizJ and others added 3 commits October 10, 2026 13:01
`TextIndexDAGReplacer` already knows the analyzed index of every virtual
column it adds, so it now collects `IndexReadTasks` instead of the
intermediate `IndexReadColumns` map. `createReadTasksForTextIndex` takes
the tasks as is and no longer looks the indexes up by name.

Also add `IndexReadTask::getNamesAndTypesList` and minor cleanups of
comments and formatting.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…er_redefinition`

On a case-insensitive filesystem (the macOS fast test runner) `replaceFileNameToHashIfNeeded`
stores every stream of a part under the hash of its name, so the `skp_idx_idx.pos.*` glob
matched nothing and the test failed on `cp`. Probe the plain name and then the hashed one.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124595&sha=80456c877d038e80ca9a4d1d25f3e9ca285073a1&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29
ClickHouse#124595

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`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]>
Comment on lines -251 to -252
/// Mapping from virtual column (optimized for direct read from text index) to search query.
std::unordered_map<String, TextSearchQueryPtr> virtual_column_to_search_query;

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.

The main point of refactoring: avoid filling shared fields after creation of MergeTreeIndexConditionText. Currently, it works by accident, but breaks with BM25 scoring.

struct ResultReplacement
{
IndexReadColumns added_columns;
IndexReadTasks index_read_tasks;

@CurtizJ CurtizJ Oct 10, 2026 •

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.

Just reducing the number of temporary structs. It makes the code more neat.

/// index this same mutation drops, so it is absent from the post-drop metadata.
const String file_name = index_ptr->getFileName();
for (const auto & index_substream : index_ptr->getSubstreams())
for (const auto & index_substream : index_ptr->getPotentialSubstreams())

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.

A fix for the corner case with chain DETACH PART -> DROP INDEX -> ADD INDEX -> ATTACH PART, when the index holds substreams incompatible with the actual index definition and is not being rebuilt during mutations.

CurtizJ and others added 2 commits October 10, 2026 20:22
The test copied the positions substream files into a part directory by hand to
reproduce a part left by the ClickHouse#109595 bug. Stateless tests should not modify the
filesystem of a part. The reachable case is covered by
`05343_text_index_stale_positions_after_redefinition`, and `getPotentialSubstreams`
by `gtest_index_potential_substreams`.

ClickHouse#124595

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…oring-for-bm25

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

clickhouse-gh Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

47 object files changed (+232.62 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/PostingListCursor.cpp.o new 258.61 KiB +258.61 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp.o 487.05 KiB 462.11 KiB -24.94 KiB (-5.12%)

720 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

297 translation units recompiled, 2948 s compile time in total, 296 of them have a recent master baseline.

Translation units without a recent master baseline:

  • src/Storages/MergeTree/PostingListCursor.cpp: 7.1 s

Job report

@CurtizJ

CurtizJ commented Oct 11, 2026

Copy link
Copy Markdown
Member Author

Unrelated failures:

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 11, 2026
@CurtizJ CurtizJ self-assigned this Oct 11, 2026
Merged via the queue into ClickHouse:master with commit 1b9d291 Oct 11, 2026
160 of 163 checks passed
@CurtizJ
CurtizJ deleted the text-index-refactoring-for-bm25 branch October 11, 2026 13:26
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Oct 11, 2026
Resolve conflicts with ClickHouse#124595 and adapt the BM25 code to its refactoring:
`IndexReadTask` keeps the search query in `IndexReadTask::Column`, `TextIndexDAGReplacer` builds `IndexReadTasks` directly
(including the score columns), and `createReadTasksForTextIndex` takes the tasks.

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

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... 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.

2 participants