Repository navigation
Fix CompressedReadBufferFromFile::seek taking a block left by readBig for the current position - #125446
Conversation
…BufferFromFile::seek` `readBig` decompresses a block that fits the caller's buffer straight into it and sets `size_compressed = 0`, leaving the previous, exhausted block in `working_buffer`. In that state `seek(P, S)` treated "`working_buffer` is not empty and `offset() == S`" as "already at the required position", so when `S` happened to equal the size of the stale block the seek was a no-op and the next read started at offset 0 of block `P`. In Compact parts the substreams of a column with a type-specific codec land in separate blocks (the sizes of an `Array` with `CODEC(Delta, ZSTD)` get only `ZSTD`, the elements `Delta, ZSTD`), and a following substream mark can point into a shared block at a non-zero offset. With default settings `SELECT a, c.y` returned the values of `c.x`, and with `compress_per_column_in_compact_parts = 0` mutations failed with `CANNOT_READ_ALL_DATA` (ClickHouse#118560). Without a block only the pending lazy offset (`nextimpl_working_buffer_offset`) says where the next read starts, so the shortcut now checks only that. A seek that used to return early now takes the lazy-seek branch to the position `file_in` is already at; the block is decompressed once either way. Closes ClickHouse#118560 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding) plus my own review.
Severity: ❌ blocker / |
|
Workflow [PR], commit [7b4fafb] Summary: ✅
AI ReviewSummaryReviewed #125446 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
CI finish ledger - ecef54bEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261013-003012 |
|
@groeneai 🕵️ CachedCompressedReadBuffer::seek (lines 102-105) still has the same !buffer().empty() && offset() == offset_in_decompressed_block clause. I agree it can't be reached today: without owned_cell the buffer is empty, and the class has no readBig. Could you still drop it here too, so that the two seeks keep the same shortcut? As it stands the clause is dead code that would bring this bug back if someone later adds a readBig that decompresses into the caller's memory, the way the one in CompressedReadBufferFromFile does. |
…eek` Without `owned_cell` the working buffer of `CachedCompressedReadBuffer` is always empty, so the `!buffer().empty() && offset() == ...` arm of its seek shortcut is unreachable. Remove it anyway so that both compressed read buffers use the same shortcut, and a future `readBig` that decompresses into the caller's memory cannot bring back the no-op seek fixed in the previous commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in 69e1fb7: |
| void CompressedReadBufferFromFile::seek(size_t offset_in_compressed_file, size_t offset_in_decompressed_block) | ||
| { | ||
| /// Nothing to do if we already at required position | ||
| /// Nothing to do if we already at required position. |
There was a problem hiding this comment.
The fix is correct, but it leaves the stale block in working_buffer and relies on an unchecked property instead of removing it. Think about these three
- Reset the buffer in
readBigwhere it zeroessize_compressed:
bytes += offset();
resetWorkingBuffer();
size_compressed = 0;and drop the two later bytes += offset() lines. Then no stale block exists and seek is correct by construction.
2. Header invariant: size_compressed = 0 and working_buffer is empty.
3. At the top of seek: chassert(size_compressed || working_buffer.empty());, so a future path that zeroes size_compressed with bytes pending fails in debug builds instead of returning wrong data.
There was a problem hiding this comment.
Done in b58c6f8, all three points. One change from the suggestion: the reset runs just before readCompressedData instead of after it, because with CODEC(NONE) working_buffer points into compressed storage that the next read may reallocate. The buffer arm of the seek shortcut is dead under the invariant, so it stays removed. On a debug build the chassert held across about 500 related stateless tests.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 8 s compile time in total, 8 of them have a recent master baseline. |
Before `readBig` reads the next compressed block, which moves `file_in` past the block held in `working_buffer`, account the bytes of that block and reset the buffer where `size_compressed` is zeroed. State (a) of the class invariant (`size_compressed == 0`) then always has an empty `working_buffer`, so no stale block is left for `seek` to take for the current position. The two later `bytes += offset()` become no-ops and are removed. The reset runs before `readCompressedData`, not after it: with `CODEC(NONE)` `working_buffer` points into the compressed storage, which the next read may reallocate, so `offset()` must be taken first. This is also the order of `ReadBuffer::next`. Document the invariant in the header and check it at the top of `seek`, so a future path that zeroes `size_compressed` with bytes pending fails in debug builds instead of returning wrong data. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - b58c6f8Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261015-210016 |
|
Another CI failure caused by this bug: With 7952 the last granule has 4096 rows, so its A smaller case that needs only a ZSTD default codec: on 26.3.55.2-lts, Both are correct with b58c6f8 (checked with this PR's |
|
This PR is the fix. Both cases are the same stale-block seek and give the right result with b58c6f8. There is no separate issue: in CIDB this |
rienath
left a comment
There was a problem hiding this comment.
Merge master. The flakes were fixed
|
Merged master in 1c379a9 (no conflicts, the diff against master is unchanged). Today's master still reproduces the bug; the merged build returns the right values, and |
CI finish ledger - 1c379a9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261022-150010 |
|
Sorry to break master, it's now fixed |
Closes: #118560
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed reading wrong data from a Compact part when a column whose substreams use different codecs (for example an
Arraycolumn withCODEC(Delta, ZSTD)) is followed by a read that starts inside a shared compressed block:SELECT a, c.ycould return the values ofc.x, a mutation reading such a subcolumn could write them into the new part, and withcompress_per_column_in_compact_parts = 0mutations could fail withCANNOT_READ_ALL_DATA. Closes #118560.Description
On 26.9.11.2 (also 26.3 and 26.8), with default settings,
SELECT a, c.yfrom a Compact part ofCREATE TABLE t (a Array(UInt32) CODEC(Delta, ZSTD), c Tuple(x UInt64, y UInt64)) ENGINE = MergeTree ORDER BY tuple()returns the values ofc.x; on 26.8 and 26.9, with an extra columnz UInt64,ALTER TABLE t UPDATE z = c.y + length(a) WHERE 1writes them into the new part. Withcompress_per_column_in_compact_parts = 0the same defect fails mutations withCANNOT_READ_ALL_DATA, as in CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=106734&sha=382decb770b0f39b864ac6a8248d66f80505d7c7&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29readBigdecompresses a block that fits the caller's buffer straight into it, and it left the previous, exhausted block inworking_bufferwithsize_compressed = 0.seek(P, S)treated "the buffer is not empty andoffset() == S" as "already positioned", so when the stale block had exactlySbytes the seek was a no-op and the next read started at offset 0 of blockP. In Compact parts the sizes ofaget onlyZSTDand are a separate block from its elements, and the substream mark ofc.ypoints 8 bytes into the block ofc.readBignow accounts and resets the exhausted block before it reads the next one, sosize_compressed = 0always means an emptyworking_buffer. The header states this invariant andseekchecks it with achassert. Under the invariant the buffer arm of theseekshortcut is dead, so it is removed, also fromCachedCompressedReadBuffer::seek, and both seeks use the same shortcut. A seek that used to return early takes the lazy-seek branch to the position the file buffer is already at; the block is decompressed once either way. The change is in the buffer, so every reader of it is covered, Wide parts included.The new test fails on master (20/20 randomized runs) and passes with the change (30/30).
Workflow [PR]
Sync PR [sync-upstream/pr/125446]
Version info
26.11.1.1769-master(included in26.11and later)