Skip to content

Text index: remove left-over roaringish from the positions merge path - #121976

Merged
CurtizJ merged 7 commits into
masterfrom
ahmadov/remove-left-over-roaringish
Oct 6, 2026
Merged

CurtizJ merged 7 commits into
masterfrom
ahmadov/remove-left-over-roaringish

Conversation

@ahmadov

@ahmadov ahmadov commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

The roaringish based positions support was dropped by #111550, however merge path decoded every positional source whole into roaringish entries, remapped their doc ids, sorted all of them and re-encoded.

With this PR, left-over roaringish is completely removed from the code base and merge postings segment by segment for every source. The merge queue reports which source each emitted row id came from, and that source's next position lists are streamed block by block into the positions encoder in merged order. Nothing is sorted or materialized per token. The build side streams per-document lists into the same encoder.

The on-disk format is unchanged, and merging interleaved positional parts should be faster.

Changelog category (leave one):

  • Performance Improvement

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

Remove left-over roaringish from the code base.


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

Version info

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

@ahmadov ahmadov added comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... hold Skip for auto-updating. labels Sep 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5785d2d]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 5/6) FAIL Performance dashboard
sort_low_cardinality #5 FAIL dashboard IGNORED
sort_low_cardinality #3 slower query history IGNORED

AI Review

Summary

This PR replaces the leftover roaringish positions accumulation with direct blocked-position re-encoding on both the build and merge paths. The on-disk layout still looks consistent and CI is all green, but I do not think the review is clean yet: the new per-source interleaving branch in PostingsMergeQueue still has no focused stateless coverage, and the removal of on-the-fly roaringish compaction still leaves a realistic dense-token memory regression on the build path.

PR Metadata
  • Changelog category: Performance Improvement is the right bucket.
  • Changelog entry: required and present, but it is too implementation-internal. Remove left-over roaringish from the code base. does not tell users what improves.
  • Suggested replacement: Improve merge performance for text indexes with \support_phrase_search = 1` by removing a legacy positions-conversion step.`
Findings

⚠️ Majors

  • [src/Storages/MergeTree/TextIndexUtils.cpp:791] The new window_sources bookkeeping is only exercised when remapped row ids from different parts alternate within one token, but the existing merge-path phrase tests still use disjoint id ranges (tests/queries/0_stateless/02346_text_index_phrase_search.sql:111, tests/queries/0_stateless/02346_text_index_partially_materialized.sql:228). That leaves the core branch that decides which source each emitted posting rank came from effectively untested, so a wrong source assignment can silently corrupt phrase positions after OPTIMIZE TABLE ... FINAL without any current stateless test failing. Suggested fix: add a focused stateless test that inserts odd/even ids into separate parts, forces OPTIMIZE TABLE ... FINAL, and checks hasPhrase before and after the merge.
  • [src/Storages/MergeTree/TextIndexBlockedPositionsCodec.h:125] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/121976#discussion_r4127789838] The new build-side PositionListBuilder appends every occurrence of the open document into raw UInt32 storage and only delta-encodes/compresses it in Encoder::finishDocument (src/Storages/MergeTree/TextIndexBlockedPositionsCodec.cpp:147). The removed roaringish builder compacted dense runs on the fly, so dense single-row workloads such as ngrams(3) over long repeated strings still regress sharply in peak memory even after the later “keep one raw copy” tweak. I still consider this real because the current code keeps the full open-document position list materialized until the row ends, which is exactly the path that can trip MEMORY_LIMIT_EXCEEDED during index build.
Final Verdict

⚠️ Not ready to merge as-is. I would want the interleaving merge path covered by a dedicated stateless test, and I still think the dense-token build-memory regression needs either a code fix or an explicit decision to accept the regression.

LLVM Coverage Report

Measured on commit 5785d2d.

Metric Baseline Current Δ
Lines 88.30% 88.20% -0.10%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 192/205 (93.66%) · 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 Sep 24, 2026
Previously, the merge decoded every positional source whole into roaringish
entries, remapped their doc ids, sorted all of them and re-encoded.

With this PR, left-over roaringish is completely removed from the code base
and merge postings segment by segment for every source.
The merge queue reports which source each emitted row id came from, and
that source's next position lists are streamed block by block into the
positions encoder in merged order. Nothing is sorted or materialized per
token. The build side streams per-document lists into the same encoder.

The on-disk format is unchanged, and merging interleaved positional
parts should be faster.
@ahmadov
ahmadov force-pushed the ahmadov/remove-left-over-roaringish branch from 7bfef8f to 7e972d5 Compare September 24, 2026 12:45
@ahmadov
ahmadov requested a review from CurtizJ September 24, 2026 12:45
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5785d2d90 with master cf63c917b (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 85c3e1d61; 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

6 object files changed (-25.39 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeIndexText.cpp.o 826.95 KiB 808.81 KiB -18.14 KiB (-2.19%)

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

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

Median compile-time ratio to the baselines is ×0.94 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -19.7 s (-7%) in total before that adjustment.

Job report

@ahmadov

ahmadov commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Removing the extra Roaringish positions conversion on the merge path improved the perf:
image

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Oct 2, 2026

if (positions)
{
if (run_length != 0 && window_sources[bit] != run_source)

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.

This source-tracking branch only gets stressed when postings from different parts actually interleave after the row-id remap. The existing merge coverage in tests/queries/0_stateless/02346_text_index_phrase_search.sql merges disjoint id ranges, so window_sources never has to alternate within the same token and this bookkeeping can regress unnoticed.

Please add a focused stateless test that inserts odd/even ids into separate parts (or any other layout that forces interleaving after OPTIMIZE FINAL) and validates hasPhrase before and after the merge.

@CurtizJ CurtizJ self-assigned this Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: b8b3cb3b-0c15-4bc8-bf96-381d005a1d5c
  • MIRAI run: 2fbcde7e-eeb6-4d27-97bc-7f7092fe19f0
  • PR check IDs:
    • clickbench_3423571_1791232426
    • clickbench_3423584_1791232426
    • clickbench_3423590_1791232426
    • tpch_adapted_1_official_3423601_1791232426
    • tpch_adapted_1_official_3423627_1791232426
    • tpch_adapted_1_official_3423641_1791232426

@ahmadov ahmadov assigned rschu1ze and unassigned rschu1ze Oct 2, 2026

@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 in general.

if (!sorted)
{
std::sort(first, last);
last = std::unique(first, last);

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 can positions be non-unique?

@ahmadov ahmadov Oct 5, 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.

mostly because of array and map containers that reset position to 0 at each element. e.g. ['hello world', 'hello']. hello comes at position 0 twice.

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 looks strange, because it may give false positives. Also, it looks like hasPhrase does not support arrays, and positions cannot be used anyhow. I also see PR #121300, but we will need to properly support positions at the edges of tokens from adjacent array elements to avoid false positives. Maybe it's simpler just to disable positions with arrays.

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.

Ah, I see, the support was added in #120859 with a hint mode.

@CurtizJ CurtizJ Oct 5, 2026 •

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.

It looks like we need something like this: https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/position-increment-gap. Then we can answer hasPhrase queries on top of arrays with exact direct reads and the slop parameter value up to the fake gap value.

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.

yes, we support hasPhrase in hint mode for array inputs, but we can implement "position increment gap" similar for enabling the exact mode. It would be really helpful.

So far, there shouldn't be any false-position with the current implementation. I'll have a look into implementing "position increment gap" in follow-ups to enable the exact mode.

Comment on lines +201 to +203
size_t offset = writeVarUIntTo(block_begin, num_exceptions);
for (UInt32 value : exceptions)
offset += writeVarUIntTo(block_begin + offset, value);

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.

For the follow-up: maybe it's better to just save frequencies as pfor block. I remember that when I checked the sizes of the frequency arrays for BM25 compressed with pfor and the sizes of the position exceptions, the plain pfor block was smaller even for blocks with a few exceptions. Probably the decode speed will also be better.

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 6, 2026
Merged via the queue into master with commit e703172 Oct 6, 2026
147 of 149 checks passed
@CurtizJ
CurtizJ deleted the ahmadov/remove-left-over-roaringish branch October 6, 2026 13:50
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 6, 2026
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Oct 6, 2026
Resolve conflicts with ClickHouse#121976 (streaming merge of text index positions):
take `PositionsMerge` from master and drop the per-source capture of pre-remap row ids
(`captureRowIdsForPositions`, `readAndAppendPositions`, `flushPositions`); keep the BM25 term-frequency
path of `PostingsMergeQueue` alongside the positions source tracking.

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

Copy link
Copy Markdown
Contributor

@ahmadov @CurtizJ — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

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... hold Skip for auto-updating. pr-performance Pull request with some performance 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.

5 participants