Skip to content

Fix a short read of the product quantization codebook - #112818

Merged
shankar-iyer merged 1 commit into
masterfrom
pq-codebook-whole-file-read
Aug 1, 2026
Merged

shankar-iyer merged 1 commit into
masterfrom
pq-codebook-whole-file-read

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Closes: #112804
Related: #110926

The product method of the Quantized codec stores one codebook value for the whole part. It is written once, in serializeBinaryBulkStateSuffix, after the data of all granules, so every granule's mark in the product_quantization_codebook stream points at its start and no mark delimits its end.

MergeTreeReaderStreamSingleColumn::getRightOffset assumed the opposite. For a read whose mark range ends before the end of the part it bounds the read at the offset of the first following mark with a greater compressed offset. For the codebook stream that mark is the part's final mark, which is written right after the suffix - and with an adaptive write buffer (starting at adaptive_write_buffer_initial_size, 16 KiB, and doubling) the codebook spans several compressed blocks, so the final mark points into the middle of it. For a 64 KiB codebook the marks look like this:

mark 0..9  (0, 0)          <- the data granules
mark 10    (49202, 16384)  <- the final mark, inside the codebook

so the read was cut at compressed offset 49202 and threw:

Cannot read all data of type FixedString. Bytes read:49152. String size:65536:
(while reading column vec.product_quantization_codebook)

The bound is only enforced by read buffers that implement setReadUntilPosition - object storage, encrypted or cached disks - which is why this only showed up on the s3 CI runs, and only with a write-side min_columns_to_activate_adaptive_write_buffer small enough to activate the adaptive buffer. That matches the two culprit settings the randomized-settings minimizer found in the issue.

The codebook stream holds exactly one value for the whole part, so it must be read as a whole file, like the metadata streams of Dynamic and JSON. This also repairs reads of parts that are already written.

The failing test is a plain SELECT of the subcolumn, so this was not specific to it: any read of the codebook subcolumn whose mark range stops before the part's final mark could hit it. The new test reproduces that deterministically with a WHERE on the primary key, without the range-splitting injection - on a local_blob_storage disk, because a plain local ReadBufferFromFileDescriptor ignores setReadUntilPosition and hides the bug.

CI report from the issue: Stateless tests (amd_tsan, s3 storage, parallel, 3/3) (#111093)

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed Cannot read all data of type FixedString when reading the product_quantization_codebook subcolumn of a column with a Quantized('product', ...) codec. The per-part codebook is written after the data of all granules, so it is not delimited by marks, and a read whose mark range ended before the part's final mark could read it short.

Version info

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

The `product` method of the `Quantized` codec stores one codebook value for the whole part. It is written
once, in `serializeBinaryBulkStateSuffix`, after the data of all granules, so every granule's mark in the
`product_quantization_codebook` stream points at its start and no mark delimits its end.

`MergeTreeReaderStreamSingleColumn::getRightOffset` assumed the opposite. For a read whose mark range ends
before the end of the part it bounds the read at the offset of the first following mark with a greater
compressed offset. For the codebook stream that mark is the part's final mark, which is written right after
the suffix - and with an adaptive write buffer (starting at `adaptive_write_buffer_initial_size`, 16 KiB, and
doubling) the codebook spans several compressed blocks, so the final mark points into the middle of it. For a
64 KiB codebook the marks look like this:

    mark 0..9  (0, 0)          <- the data granules
    mark 10    (49202, 16384)  <- the final mark, inside the codebook

so the read was cut at compressed offset 49202 and threw

    Cannot read all data of type FixedString. Bytes read:49152. String size:65536:
    (while reading column vec.product_quantization_codebook)

The bound is only enforced by read buffers that implement `setReadUntilPosition` - object storage, encrypted
or cached disks - which is why this only showed up on the s3 CI runs, and only with a write-side
`min_columns_to_activate_adaptive_write_buffer` small enough to activate the adaptive buffer.

The codebook stream holds exactly one value for the whole part, so it must be read as a whole file, like the
metadata streams of `Dynamic` and `JSON`. This also repairs reads of parts that are already written.

Closes: #112804
Related: #110926
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8dccc44]

Summary: ✅


AI Review

Summary

This PR fixes a real wide-part Quantized('product', ...) read-boundary bug by classifying product_quantization_codebook as a single per-part stream that must be read to EOF instead of to the next mark. I reviewed the current diff, the current code, the existing PR discussion state (there are no review threads on this PR), and the current green Praktika report, and I did not find an unresolved correctness, compatibility, or coverage issue.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 31, 2026
@shankar-iyer shankar-iyer self-assigned this Aug 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 16/16 (100.00%) · Uncovered code

Full report · Diff report

@shankar-iyer
shankar-iyer added this pull request to the merge queue Aug 1, 2026
Merged via the queue into master with commit 1b8ecef Aug 1, 2026
178 checks passed
@shankar-iyer
shankar-iyer deleted the pq-codebook-whole-file-read branch August 1, 2026 06:32
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

Flaky test: 02354_vector_search_quantize_codec_pq_multigranule

3 participants