Skip to content

Add FastPFOR codec for text index posting lists - #107408

Closed
fastio wants to merge 7 commits into
ClickHouse:masterfrom
fastio:integration_codec_for_text_index
Closed

fastio wants to merge 7 commits into
ClickHouse:masterfrom
fastio:integration_codec_for_text_index

Conversation

@fastio

@fastio fastio commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

This pull request adds a new fastpfor posting list codec for the (experimental) text index, selectable via posting_list_codec = 'fastpfor' alongside the existing none and bitpacking.

Implementation.

  • Refactor the bitpacking-only PostingListCodecBitpackingImpl into a generic SegmentedPostingListCodecImpl that keeps the shared segment / block / Index Section layout and delegates only the per-block payload to a pluggable IPostingListBlockCodec (bitpacking or fastpfor). The lazy posting cursor keeps random per-block access unchanged.
  • Add FastPFORBlockCodec, a pimpl wrapper around FastPFOR's CompositeCodec<SIMDFastPFor<4>, VariableByte>. The header stays free of SIMD/FastPFOR includes so it is include-safe on builds without the library.
  • Each segment header now records its codec type, so reads dispatch to the matching block codec.
  • FastPFOR is added as a contrib submodule, built as ch_contrib::fastpfor on x86-64-v2+ (SSE4.2) and AArch64 (NEON). On builds without FastPFOR, creating or reading a fastpfor posting list fails with SUPPORT_IS_DISABLED; lists written with fastpfor cannot be read on such builds.

Changelog category (leave one):

  • New Feature

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

Add a new fastpfor option for the text index posting_list_codec parameter. It applies delta coding followed by FastPFOR (patched frame-of-reference) compression per block, compressing better than bitpacking for bursty or sparse posting lists.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Reopen: #95517

@fastio
fastio marked this pull request as draft June 13, 2026 16:25
@fastio
fastio marked this pull request as ready for review June 15, 2026 09:40
@CurtizJ CurtizJ added the can be tested Allows running workflows for external contributors label Jun 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [90d833c]

Summary: ❌

job_name test_name status info comment
AST fuzzer (arm_asan_ubsan) FAIL
Logical error: Block structure mismatch in A stream: different columns: (STID: 0993-27f0) FAIL cidb, issue
Stress test (arm_msan) ERROR

AI Review

Summary

This PR adds a fastpfor posting-list codec for text indexes, including a pluggable block-codec refactor, lazy cursor support, tests, docs, and benchmarks. The FastPFOR decoder fixes the earlier bounded-tail and SIMD-page validation concerns, but the new codec is still not wired into the table-level MergeTree setting path.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/IPostingListCodec.cpp:45] The feature is only reachable through the explicit index argument path. text_index_posting_list_codec is backed by TextIndexPostingListCodec, and src/Core/SettingsEnums.h / src/Core/SettingsEnums.cpp still only declare and map none and bitpacking, so CREATE TABLE ... SETTINGS text_index_posting_list_codec = 'fastpfor' is rejected before it reaches this factory. This contradicts the documented table-level setting behavior and leaves one advertised configuration path unusable.
    Suggested fix: add FastPFOR / "fastpfor" to TextIndexPostingListCodec and extend the stateless coverage to create or alter a table with text_index_posting_list_codec = 'fastpfor'.
Final Verdict

⚠️ Changes requested: please wire fastpfor into the TextIndexPostingListCodec setting enum and cover the table-setting path.

@clickhouse-gh clickhouse-gh Bot added pr-feature Pull request with new product feature submodule changed At least one submodule changed in this PR. manual approve Manual approve required to run CI labels Jun 15, 2026
Optional parameter `posting_list_codec` (default: `none`) specifies the codec for posting list:
- `none` - the posting lists are stored without additional compression.
- `bitpacking` - apply [differential (delta) coding](https://en.wikipedia.org/wiki/Delta_encoding), followed by [bit-packing](https://dev.to/madhav_baby_giraffe/bit-packing-the-secret-to-optimizing-data-storage-and-transmission-m70) (each within blocks of fixed-size). Slows down SELECT queries, not recommended at the moment.
- `fastpfor` - apply differential (delta) coding, followed by [FastPFOR](https://github.com/fast-pack/FastPFOR) (patched frame-of-reference) coding within blocks of fixed size. Compared to `bitpacking` it compresses better when most deltas are small but a few are large (bursty or sparse posting lists), because the large outliers are coded as exceptions rather than widening every value. `fastpfor` is only available when ClickHouse is built with the FastPFOR library (`x86-64-v2` or higher, and `AArch64`); on builds without it, both creating and reading a `fastpfor` posting list fail with `SUPPORT_IS_DISABLED`. Posting lists written with `fastpfor` therefore cannot be read on a build that lacks FastPFOR.

@murphy-4o murphy-4o Jun 16, 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.

I'm curious whether the hypothesis holds true for this scenario: can the posting list guarantee a small delta?
Additionally, please include the benchmark results for this optimization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Benchmark (mixed 2M-row corpus: bursty + uniform-stride + random-sparse tokens): total text-index size is fastpfor 10.84 MiB vs bitpacking 12.52 MiB vs none 11.59 MiB — fastpfor is the smallest overall, and on a bursty-only token set it is 813 KiB vs bitpacking 2.72 MiB (~3.3x). The hypothesis isn't that the posting list guarantees small deltas: fastpfor codes the few large gaps as exceptions instead of widening every value, so it beats bitpacking precisely on bursty/sparse lists; versus uncompressed none the win is data-dependent. Query latency was comparable across the three codecs in these runs.

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.

Sorry if I'm misunderstanding something, but why are the index sizes between none and either compressed codec so similar? I would have guessed any compression would make the index significantly smaller (e.g. the data from the PR that introduced bitpacking). Can you help me understand how these work?

@rschu1ze

Copy link
Copy Markdown
Member

@fastio Please split the commits into smaller pieces: One that refactors the existing infrastructure and one that adds fastpfor as a codec (right now, it's one huge commit).

@fastio

fastio commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@fastio Please split the commits into smaller pieces: One that refactors the existing infrastructure and one that adds fastpfor as a codec (right now, it's one huge commit).

Hi @rschu1ze, thanks for the suggestion, I'll split this into 2 PRs.

@fastio
fastio marked this pull request as draft June 18, 2026 13:54
fastio added 2 commits June 22, 2026 17:46
Generalize the bitpacking-only segment codec into a pluggable per-block
framework (IPostingListBlockCodec); no behavior change.
Add the fastpfor posting_list_codec (delta + FastPFOR per block); decode is
bounded so corrupted .pst input raises CORRUPTED_DATA instead of reading OOB.
@fastio
fastio force-pushed the integration_codec_for_text_index branch from 302ab17 to 490f30f Compare June 22, 2026 08:19
@fastio

fastio commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

@rschu1ze Done — split into two commits: one that refactors the infrastructure into a pluggable per-block codec framework, and one that adds the fastpfor codec. I rebased the branch onto the latest master so the split is linear.

@ahmadov

ahmadov commented Jun 22, 2026

Copy link
Copy Markdown
Member

Hi @fastio, thanks for this PR. I'm currently working on a similar pfor based compression for follow-up of #103172. I suppose we should use the same integer compression for both posting list and positions since that new pfor compression performs better that FastPfor in my benchmarks. I'll push it soon and let you know.

@fastio

fastio commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ahmadov, Thanks for the heads-up! The codec here is intentionally pluggable. Once you merge, I can build a posting list codec on top of the new PFOR -- hoping it gives us better compression.

return createPostingListCodec(IPostingListCodec::Type::Bitpacking);

#if USE_FASTPFOR
if (codec_name == PostingListCodecFastPFOR::getName())

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.

The table-level text_index_posting_list_codec = 'fastpfor' path is still rejected. This branch handles a literal posting_list_codec = 'fastpfor' index argument, but the table setting goes through TextIndexPostingListCodec (settings[...].toString() in MergeTreeIndexText.cpp), and src/Core/SettingsEnums.h / src/Core/SettingsEnums.cpp still only declare and map none and bitpacking. As a result, CREATE TABLE ... SETTINGS text_index_posting_list_codec = 'fastpfor' fails before it reaches this factory, even though the docs say these advanced parameters can be set at table level. Please add FastPFOR / "fastpfor" to the setting enum and cover the table-setting path in the stateless test.

@clickhouse-gh

clickhouse-gh Bot commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.50% -0.10%
Branches 77.60% 77.50% -0.10%

Changed lines: Changed C/C++ lines covered by tests: 430/487 (88.30%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@fastio
fastio marked this pull request as ready for review July 15, 2026 09:20
`master` moved the documentation from the Docusaurus tree under
`docs/en/**/*.md` to the Mintlify tree at `docs/**/*.mdx`, deleting every
file this branch edits. The edits are replayed at the new paths:

- `docs/en/engines/table-engines/mergetree-family/textindexes.md` -> `docs/reference/engines/table-engines/mergetree-family/textindexes.mdx`
@abashkeev abashkeev added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Sep 4, 2026
@rschu1ze

Copy link
Copy Markdown
Member

Implemented in #84435

@rschu1ze rschu1ze closed this Oct 15, 2026
@ahmadov

ahmadov commented Oct 15, 2026

Copy link
Copy Markdown
Member

Implemented in #84435

It should be #118123. #84435 is the old attempt :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... manual approve Manual approve required to run CI pr-feature Pull request with new product feature submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants