Skip to content

Text index: improve positions layout for phrase search - #125630

Merged
ahmadov merged 12 commits into
masterfrom
ahmadov/positions-layout
Nov 4, 2026
Merged

ahmadov merged 12 commits into
masterfrom
ahmadov/positions-layout

Conversation

@ahmadov

@ahmadov ahmadov commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

This PR contains two main changes for the positions layout:

  1. 3435335 - store the per-document frequencies of each positions block as one PFor run of freq - 1, prefixed by its size, instead of a list of (local rank, frequency) exceptions. The run is omitted when every document in the block has one position.
  2. f170cef - embed the positions of rare tokens into the dictionary block instead of .pos. A phrase with a rare token then skips one .pos read per part.

The size of the positions is reduced between ~15-35% depending on text_log (10M rows) and hackernews datasets. Also, positions decode time for hasPhrase is 10–30% lower on both datasets.

Changelog category (leave one):

  • Improvement

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

Reduce the size of the positions index size up to ~30% and make phrase search decodes faster ~10-30%. One major perf improvement is decoding positions of rare tokens is significantly faster.


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

Version info

  • Merged into: 26.11.1.1275-master (included in 26.11 and later)

@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 13, 2026
@ahmadov
ahmadov requested a review from CurtizJ October 13, 2026 16:02
@ahmadov
ahmadov marked this pull request as ready for review October 14, 2026 13:11
@CurtizJ CurtizJ self-assigned this Oct 15, 2026
@ahmadov
ahmadov force-pushed the ahmadov/positions-layout branch from 05f6bdd to 10f9489 Compare October 21, 2026 15:26
Base automatically changed from ahmadov/positions-cursor to master October 22, 2026 02:06
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [befe2d7]

Summary: ✅


AI Review

Summary

This PR compresses text-index document frequencies with PFor and embeds small position lists in dictionary blocks. Reviewed head befe2d702d022ef0fc1be1b03a67b382137044e3, including all prior discussion and the current readers, writers, merge path, and tests. No genuinely new inline findings. The previously dismissed format-compatibility issue remains: the latest decoder can silently produce incorrect phrase results from existing parts.

Findings

❌ Blockers

  • src/Storages/MergeTree/TextIndexBlockedPositionsCodec.cpp:49 — Existing position data can be silently misinterpreted. [dismissed by author -- https://github.com/Text index: improve positions layout for phrase search #125630#discussion_r4348062773] The author accepts incompatible changes because positions are experimental; the concern remains because incompatible parts are accepted as valid and can return wrong results before they are rebuilt.

    Concrete source trace: one document containing foo foo has old positions [0, 1]. Its old block payload is 01 00 02 01 00 02: an exception count/rank/frequency of 1/0/2, followed by the position lane. The new frequency decoder consumes the first three bytes as a valid one-value PFor run yielding 0, hence a frequency of one. It consumes the remaining three bytes as one position, [0], and passes the end-of-payload check. Thus indexed hasPhrase(message, 'foo foo') can return false. The merge reader uses the same decoder and can persist that missing occurrence into a newly encoded index.

    Both layouts still advertise V2_WithPositions and BlockedPfor = 1; the new EmbeddedPositions dictionary records also lack a new format discriminator. Give the changed layouts distinct identifiers. Decode existing parts correctly, or explicitly reject the obsolete experimental format with rebuild instructions, and document the required rebuild and rollout procedure. Leave the author's resolved thread closed; this is the existing finding, sharpened for the current code.

Tests
  • ⚠️ Add a regression that reads a pre-change part containing foo foo, checks phrase results and position introspection, then merges it. Assert either correct preservation or an explicit obsolete-format rejection before a merge can rewrite it. Current round-trip tests pair the new encoder with the new decoder and cannot detect this failure.
Missing context / blind spots
  • ⚠️ No local ClickHouse build or server binary was available; the counterexample above was traced through the current codec and phrase matcher, without a runtime reproduction. A cross-format regression would provide executable confirmation.
Final Verdict

❌ Block. Distinguish the incompatible formats, handle existing parts safely, and add the cross-format regression. No new inline comments or thread replies were posted because this issue already has a thread dismissed by the author.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing befe2d702 with master 88f0adf4b (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of b78377b80; 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.

Object file sizes

17 object files changed (+49.50 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeIndexText.cpp.o 819.43 KiB 839.52 KiB +20.09 KiB (+2.45%)

719 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

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

Job report

@CurtizJ CurtizJ 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.

Looks good. I've left one suggestion.

@ahmadov
ahmadov added this pull request to the merge queue Nov 3, 2026
Merged via the queue into master with commit 7a03397 Nov 4, 2026
186 checks passed
@ahmadov
ahmadov deleted the ahmadov/positions-layout branch November 4, 2026 00:54
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
thevar1able pushed a commit to actueleai/ClickHouse that referenced this pull request Nov 23, 2026
…itions

Since ClickHouse#125630 `TextIndexBlockedPositionsCodec::Encoder::finishDocument`
appended `freq - 1` of every document to a dense per-token array, which
`encodeBlock` threw away whenever every document of the block had one
position. Each token owns an encoder, so with many distinct tokens this is
one heap allocation per token and one store per token occurrence into a
buffer far from the previous one (a cache miss for most occurrences).

Record `(local rank, freq - 1)` only for documents with more than one
position, as the encoder did before ClickHouse#125630, and expand them into a
128-entry stack array in `encodeBlock` only for blocks that have any. The
values passed to PFor, and so the written bytes, are unchanged.

Analysis and patch by snelheidai in ClickHouse#133439.

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

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

3 participants