Repository navigation
Fix short read of product-quantization codebook on wide parts - #110926
shankar-iyer merged 2 commits into
Conversation
The `product` quantization codec stores one codebook value per part, read once and broadcast to every granule. On wide parts the codebook stream was read at its incidental position: MergeTreeReaderWide's data getter seeks a substream to its own mark only when not prefetched and not continuing a read, so under prefetch / continued reading the single-value codebook stream could be left at a sibling subcolumn's offset and read short, e.g. "Cannot read all data of type FixedString. Bytes read:49152. String size:65536". Seek the codebook stream to the current granule's mark explicitly before reading, and wire seek_stream_to_current_mark_callback into MergeTreeReaderWide::readData (previously only wired in MergeTreeReaderCompact). Compact parts were unaffected because their getter seeks unconditionally. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-2:20260718-040555 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 distinct findings across 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-2:20260718-040555 |
|
cc @rschu1ze @Avogar — could you review this? It fixes a |
|
Workflow [PR], commit [441a8e5] Summary: ✅
AI ReviewSummaryThis PR fixes the wide-part read path for the experimental Final Verdict
|
The Quantized('product', ...) codec fixed here is an experimental feature
(allow_experimental_codecs) added this cycle in ClickHouse#108565 and not present in
any released version, so this is not a stable-release bug fix. The Bugfix
Validation gate additionally requires a regression test that fails on master
HEAD without the fix, but the failure is scheduling/prefetch dependent under
concurrency and does not reproduce deterministically from a single stateless
query. Empty commit to re-run CI after the changelog category change.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 18/21 (85.71%) · Uncovered code |
CI finish ledger — 441a8e5CI fully finished (Finish Workflow + Mergeable Check pass; 0 checks queued/in_progress; last real check completed ~35 min ago). No test failures in CIDB for this head. The prior Bugfix-validation red on
Session id: cron:our-pr-ci-monitor:20260719-013000 |
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: ClickHouse#112804
Related: ClickHouse#110926
…debook-wide-read-108565 Fix short read of product-quantization codebook on wide parts
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Fixes a short read (
Cannot read all data of type FixedString. Bytes read:49152. String size:65536) of the product-quantization codebook on wide parts, e.g. during two-stage vector search. TheQuantized('product', ...)codec is a new experimental feature (behindallow_experimental_codecs) added this cycle in #108565 and not present in any released version, so this isNot for changelograther than a stable-release bug fix. (The PR was initially filed asBug Fix; the Bugfix Validation gate then required a regression test that fails on master HEAD without the fix, but the failure is scheduling/prefetch dependent under concurrency and does not reproduce deterministically from a single stateless query, so it cannot satisfy that gate. Recategorized because the fixed feature is unreleased.)The
productquantization codec stores one codebook value (aFixedString) per part, written once at the serialize suffix.SerializationProductQuantizationCodebook::deserializeBinaryBulkWithMultipleStreamsreads that value at the codebook stream's current position, trusting the reader to have positioned it. On wide parts,MergeTreeReaderWide::readData's data getter seeks a substream to its own mark only when!was_prefetched && !continue_reading && !read_without_marks. During a multi-granule scan with prefetch (the pattern the two-stage vector search produces underReadPoolInOrder), that seek is skipped, so the single-value codebook stream is left at the offset of a sibling subcolumn (the codes streamvec.quantized, whose granule-N decompressed offset isN * bytes_per_vector * granularity). The subsequent single-block read then returns fewer bytes than the codebook size, e.g.Bytes read:49152. String size:65536.Compact parts are unaffected because their getter seeks unconditionally on every call.
Fix: the codebook read now explicitly seeks its stream to the current granule's mark (where the value always begins) via
seek_stream_to_current_mark_callback, and that callback is wired intoMergeTreeReaderWide::readData(it was previously wired only inMergeTreeReaderCompact). This mirrors howSerializationObjectSharedDatapositions its streams. The callback is inert for existing wide reads (SerializationObjectSharedDataonly consumes it on compact parts), and it is skipped inread_without_marksmode where the whole part is read from the start.Verified with a deterministic mechanism check: injecting the stale offset reproduces the exact
Bytes read:49152. String size:65536without the fix and passes with it; 50 randomized-read runs on local disk and 50 runs oncached_s3(the failing CI storage) all pass. The addedcodebook_prefetchtest case is coverage that reads the codebook across granules under the CI-failing read settings; note the original failure is scheduling/prefetch dependent (CI's rerun diagnosis was 15/19) and does not reproduce deterministically from a single stateless query.Found by the
amd_tsan, s3 storageCI run of02354_vector_search_quantize_codec_pq_multigranule(report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110606&sha=1b1d4141cc3db2906ec15f5c033603931f0a3e6b&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%203%2F3%29). The feature was added in #108565.