Skip to content

Minor refactoring of posting lists deserialization - #114317

Merged
CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:text-index-none-codec-decode
Aug 11, 2026
Merged

CurtizJ merged 3 commits into
ClickHouse:masterfrom
CurtizJ:text-index-none-codec-decode

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Aug 11, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Slightly better code which is needed for future development.

Version info

  • Merged into: 26.8.1.1190 (included in 26.8 and later)

`PostingsSerialization::deserialize` decoded posting lists marked
`IsCompressed` with the configured codec, but handled the uncompressed
roaring format inline, so `PostingListCodecNone::decode` was an unused
no-op. Implement it, and route everything except raw postings through
the codec.

The codec is now selected by the posting list header instead of the
codec configured for the part, so the resolution is extracted into
`resolveCodec`. An uncompressed posting list is always a serialized
roaring bitmap, and picking the codec by the header keeps that
independent of the lazy `Bitpacking` resolution done for pre-`WithCodec`
parts.

Adapted from ClickHouse#113856

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f7a8c30]

Summary: ❌

job_name test_name status info comment
Stress test (arm_asan_ubsan) FAIL
Sanitizer (STID: 2064-22d0) FAIL cidb IGNORED

AI Review

Summary

This PR refactors text-index posting-list deserialization to route uncompressed blocks through PostingListCodecNone, reuse a caller-owned scratch buffer, and decode some contiguous payloads in place. The previously reported buffer-reuse and contradictory-flag issues are fixed in the current head, but the new zero-copy path for bitpacked payloads is not safe for generic ReadBuffer callers, so I cannot approve the PR as-is.

Findings

❌ Blockers

  • [src/Storages/MergeTree/MergeTreeIndexTextPostingListCodec.cpp:16] readContiguousBytes now returns a direct pointer for any contiguous ReadBuffer, but the bitpacking decode path still assumes SIMD slack at the end of the input. ReadBufferFromMemory / ReadBufferFromString keep BufferBase::padded = false, while BitpackingPostingListBlockCodec::maxBlockBytes still reserves +16 bytes for that overread slack, so this refactor reintroduces out-of-bounds reads for decode-from-memory callers that used to be safe. Suggested fix: only take the zero-copy branch when in.isPadded() is true, or keep copying compressed payloads into PaddedPODArray before bitpacking decode.
Final Verdict

❌ Block
Minimum required action: make the bitpacked zero-copy path conditional on padded input, or restore the copy into padded scratch storage for compressed payloads.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.00% +0.00%

Changed lines: Changed C/C++ lines covered: 47/53 (88.68%) · 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 Aug 11, 2026
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Aug 11, 2026
`PostingListCodecNone::decode` allocated a fresh `PaddedPODArray` for
every posting list block that is not entirely in the read buffer, and
`SegmentedPostingListCodec::decode` allocated its `compressed_data`
member anew on every call, because `PostingListCodecBitpacking::decode`
constructs the codec per call. Pass the reusable `deserialization_buffer`
of `PostingsSerialization` to `IPostingListCodec::decode` instead. The
buffer cannot be kept in the codec: the codec for uncompressed posting
lists is a shared static instance and `decode` is `const`.

Also decode a bitpacked segment in place when its payload is entirely in
the read buffer, as `PostingListCodecNone::decode` already did for
roaring bitmaps. Both go through `readContiguousBytes` now. Decoding in
place is safe: `simdcomp` uses unaligned loads only, and the memory of
`ReadBuffer` has the same `PADDING_FOR_SIMD` right padding as
`PaddedPODArray`.

Follows up on
ClickHouse#114317 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
`PostingListCodecNone::decode` allocated a fresh `PaddedPODArray` for
every posting list block that is not entirely in the read buffer, and
`SegmentedPostingListCodec::decode` allocated its `compressed_data`
member anew on every call, because `PostingListCodecBitpacking::decode`
constructs the codec per call. Pass the reusable `deserialization_buffer`
of `PostingsSerialization` to `IPostingListCodec::decode` instead. The
buffer cannot be kept in the codec: the codec for uncompressed posting
lists is a shared static instance and `decode` is `const`.

Also decode a bitpacked segment in place when its payload is entirely in
the read buffer, as `PostingListCodecNone::decode` already did for
roaring bitmaps. Both go through `readContiguousBytes` now. Decoding in
place is safe: `simdcomp` uses unaligned loads only, and the memory of
`ReadBuffer` has the same `PADDING_FOR_SIMD` right padding as
`PaddedPODArray`.

Follows up on
ClickHouse#114317 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
@CurtizJ
CurtizJ force-pushed the text-index-none-codec-decode branch from 510c895 to 2562ec7 Compare August 11, 2026 14:39
`RawPostings` and `IsCompressed` are mutually exclusive: the writer always
clears `IsCompressed` on every path that sets `RawPostings`. The refactoring
of `deserialize` checks `RawPostings` first, so a corrupted header with both
bits set would be parsed as `cardinality` raw `VarUInt` row ids instead of
failing, returning bogus matches and leaving the stream misaligned for the
following posting lists. Reject such headers with `CORRUPTED_DATA`.

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

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

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 691.36 MiB 688.36 MiB -3.00 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@CurtizJ
CurtizJ added this pull request to the merge queue Aug 11, 2026
@CurtizJ CurtizJ self-assigned this Aug 11, 2026
Merged via the queue into ClickHouse:master with commit dd5389b Aug 11, 2026
178 of 180 checks passed
@CurtizJ
CurtizJ deleted the text-index-none-codec-decode branch August 11, 2026 23:30
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Aug 12, 2026
…optimizations

The conflicts were between the local iteration of `Deserialize uncompressed
posting lists via PostingListCodecNone` and the version of the same change
merged into master in ClickHouse#114317,
which additionally got two follow-ups there: a reusable scratch buffer for
`IPostingListCodec::decode`, and rejection of a posting list header that
marks the data as both raw and compressed.

Resolved in favour of master, with the row ids decode path of this branch
re-applied on top of it:

- Both overloads of `IPostingListCodec::decode` take the reusable
  `deserialization_buffer` of `PostingsSerialization`.
- `SegmentedPostingListCodec::readSegment` returns the segment header
  together with the payload span produced by `readContiguousBytes`, shared
  by both `decode` overloads. Neither of them copies the payload into
  `compressed_data` anymore, which stays an encode-only member.
- The check for the contradictory `RawPostings` and `IsCompressed` flags is
  extracted into `checkPostingListFlags` and applied to both overloads of
  `PostingsSerialization::deserialize`: the row ids overload dispatches on
  `RawPostings` first as well, so it has the same exposure to a corrupted
  header.

Co-Authored-By: Claude Opus 5 <[email protected]>
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 12, 2026
CurtizJ added a commit to CurtizJ/ClickHouse that referenced this pull request Aug 15, 2026
Resolved conflicts with `Minor refactoring of posting lists deserialization`
(ClickHouse#114317) in the text index
posting list codecs:

- `IPostingListCodec::decode` keeps the new `PaddedPODArray<char> & buffer`
  scratch parameter from master, threaded through `SegmentedPostingListCodec`,
  `PostingListCodecBitpacking` and `PostingListCodecNone`, while keeping
  `createEncoder` from this branch. `IPostingListCodec::encode` and
  `encodePostingsInBlocks` stay removed - encoding goes through
  `IPostingListEncoder`.
- `PostingsSerialization::deserialize` uses `resolveCodec` from master, so an
  uncompressed posting list is always decoded by `PostingListCodecNone` instead
  of the configured codec, and contradictory `RawPostings` / `IsCompressed`
  header flags are rejected.
- `PostingListCodecNone::decode` now reuses the shared `readContiguousBytes`
  helper and the caller-owned scratch buffer instead of a local `std::vector`.
- Kept both `TextIndexSerialization::serializeRawPostings` (this branch) and
  `TextIndexSerialization::checkTokenSize` (master).

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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