Skip to content

Text index: introduce PForDelta compression for positions - #108277

Closed
ahmadov wants to merge 31 commits into
masterfrom
ahmadov/text-index-positions-pfor
Closed

ahmadov wants to merge 31 commits into
masterfrom
ahmadov/text-index-positions-pfor

Conversation

@ahmadov

@ahmadov ahmadov commented Jun 23, 2026

Copy link
Copy Markdown
Member

A follow-up of #103172 to compression positions using an integrated PforDelta compression.

Changelog category (leave one):

  • New Feature

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

Introduce new positions_encoding parameter to compression positions further. Supported values are none or pfor.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@ahmadov ahmadov changed the title Ahmadov/text index positions pfor Text index: introduce PforDelta compression for positions Jun 23, 2026
@ahmadov ahmadov changed the title Text index: introduce PforDelta compression for positions Text index: introduce PForDelta compression for positions Jun 23, 2026
ahmadov added 4 commits June 23, 2026 11:31
The compression rate is better than simdcomp and FastPFOR while matching
the encode/decode performance. Also, it contains an infused delta
computation.
@ahmadov
ahmadov force-pushed the ahmadov/text-index-positions-pfor branch from 94e3e2c to c989988 Compare June 23, 2026 11:31
Base automatically changed from ahmadov/text-index-positions to master June 24, 2026 19:06
ahmadov added 4 commits June 24, 2026 23:18
decodeRawSoA decoded the `.pos` blob entry-by-entry. Read the whole blob
once with `readBigStrict` and de-interleave from the buffer instead.
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3066a49]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
04507_mvt_encode_state_serialize FAIL cidb
02345_implicit_transaction NOT_FAILED cidb
01010_partial_merge_join NOT_FAILED cidb
03707_statistics_cache NOT_FAILED cidb
01147_partial_merge_full_join NOT_FAILED cidb
02499_monotonicity_toUnixTimestamp64 NOT_FAILED cidb

AI Review

Summary

This PR adds a positions_codec parameter for text indexes, persists the chosen positions encoding in the index header, and introduces a new PForDelta codec plus coverage for phrase-search reads and merge re-encoding. The main functionality looks close, but two compatibility/corruption-handling issues from earlier review threads are still present in the current code, so I would not approve it yet.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/TextIndexPositionCodec.cpp:71-100,161-223; src/Storages/MergeTree/MergeTreeReaderTextIndex.cpp:911-918; src/Storages/MergeTree/TextIndexUtils.cpp:522-532] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/108277#discussion_r3578322716] The .pos decoder still validates against file_size - position_offset and still calls checkFitsAvailable after consuming the count/length varints without subtracting those header bytes from the budget. A token truncated to header + payload - 1 bytes can therefore pass the current checks, read one or more bytes from the next token's slice, and return garbage positions or rewrite merged parts instead of raising CORRUPTED_DATA.
    Suggested fix: pass each token's actual slice length (next_position_offset - position_offset, or file end for the last token) into TextIndexPositionCodec::decode, and validate the total encoded size from the token start or decrement available_bytes as each varint is consumed before the raw/PFor payload-size checks.
  • [src/Storages/MergeTree/MergeTreeIndexText.h:289-302; src/Storages/MergeTree/MergeTreeIndexText.cpp:1187-1240,1540-1557] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/108277#discussion_r3597323550] Version::WithPositions = 2 now means a different header layout, but deserializeHeaderPrefix still treats every version-2 part as [has_positions][positions_codec].... Older experimental positional parts written before this PR only contain [has_positions]..., so upgrades will misparse them, and parts whose first sparse-index token count happens to be 0 or 1 can even slip past the positions_codec <= 1 check and continue deserializing garbage instead of failing cleanly.
    Suggested fix: keep a distinct header version for the extra codec field and teach the reader both layouts, or explicitly detect the old version-2 layout and throw a dedicated incompatibility exception instead of reinterpreting its sparse-index bytes as positions_codec.
Tests

⚠️ Add a focused corruption test with two back-to-back position blobs where the first token is truncated by one byte. The new decoder should fail with CORRUPTED_DATA instead of consuming bytes from the next token.
⚠️ Add a compatibility test that feeds the new reader a pre-positions_codec version-2 positional header. That should either decode as raw positions or fail with an explicit incompatibility exception, but it should not reinterpret the old sparse-index bytes as positions_codec.

Final Verdict

Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 29, 2026
@ahmadov
ahmadov marked this pull request as ready for review June 29, 2026 16:56
@ahmadov
ahmadov marked this pull request as draft June 29, 2026 16:56
Comment thread src/Compression/PFor/block.h Resolved
Comment thread src/Compression/PFor.h Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member

Updating the branch to fix the 04001_join_reorder_through_expression test, which was broken today in master.

@ahmadov
ahmadov force-pushed the ahmadov/text-index-positions-pfor branch from 9bfe47d to 20b71a4 Compare July 7, 2026 12:32
@ahmadov
ahmadov force-pushed the ahmadov/text-index-positions-pfor branch from 20b71a4 to 8fa51f1 Compare July 7, 2026 12:42
ahmadov added 3 commits July 13, 2026 15:25
The upper bound alone was insufficient: count also comes from on-disk metadata,
so a corrupt part with position_cardinality == count == UINT32_MAX and
payload_bytes == 1 passed the guard and then scratch.doc/group/bitmap.resize(count)
attempted a ~51GB allocation before lane decode reported corruption.
Require payload_bytes >= 3 * ceil(count / BLOCK) (each of three lanes needs >= 1 byte per
block) before any resize; mirror it in decodePforSoA.
Comment thread src/Compression/PFor/vertical.h Resolved
@ahmadov
ahmadov marked this pull request as ready for review July 14, 2026 16:18
@ahmadov
ahmadov requested a review from rschu1ze July 14, 2026 16:18
ahmadov added 2 commits July 16, 2026 16:52
…WithPositions`

Since `support_phrase_search` is experimental, the `WithPositions` header
version is redefined in place instead of adding a new version: it now always
persists the positions codec byte. Positional parts written by earlier
experimental builds must be rebuilt.
@ahmadov
ahmadov requested a review from CurtizJ July 16, 2026 16:36
@clickhouse-gh

clickhouse-gh Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.10% 86.20% +6.10%
Functions 91.00% 92.10% +1.10%
Branches 72.30% 78.30% +6.00%

Changed lines: Changed C/C++ lines covered: 773/841 (91.91%) · Uncovered code

Full report · Diff report

This branch was successfully deployed

1 active deployment
staging - docs — 3066a495 Deployed Jul 20, 2026 by mintlify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants