Repository navigation
GH-3712: Fail instead of writing CapacityByteArrayOutputStream content that does not match its size - #3965
Open
raviston wants to merge 1 commit into
Open
GH-3712: Fail instead of writing CapacityByteArrayOutputStream content that does not match its size#3965raviston wants to merge 1 commit into
raviston wants to merge 1 commit into
Conversation
…content that does not match its size The corrupt pages reported in apacheGH-3712 have a length prefix and a page header that match the size() of the level stream, but a serialized payload that is shorter: a full slab was serialized with a lower position than the bytes written to it. The writer then silently produced a file that no reader can decode. - writeTo and writeInto now check that the slabs hold exactly size() bytes and throw an IllegalStateException otherwise, so the write fails instead of producing a corrupt file. - writeInto and the direct buffer path of writeTo read the slabs through duplicates, so serializing no longer changes the position and limit of the slabs. - setByte located bytes by the limit of the slabs, assuming that every slab but the last is full. Since writeInt and writeLong were added, a slab may be followed by a new one before it is full, so setByte now uses the position. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
In the corrupt pages reported in #3712, the length prefix of the repetition levels and the page header both match the
size()of the level stream, but the serialized payload is shorter. @edgarRd's analysis shows that a contiguous range ending exactly at a slab boundary ofCapacityByteArrayOutputStreamis missing. The analysis reproduces the stored page byte for byte when the position of an already full slab is lowered before serialization.size()comes from the independentbytesUsedcounter, whilewriteTo()writesslab.position()bytes per slab, and nothing checks the two against each other. So the writer silently produced files that no reader can decode.This PR does not identify what lowered the slab position. It makes that state fail the write instead of corrupting the file, and it removes the code paths in this class that change the position of a slab after it was written.
What changes are included in this PR?
writeToandwriteIntocheck that the slabs hold exactlysize()bytes before they write anything, and throw anIllegalStateExceptionotherwise. The check is one pass over the slab list.writeIntoand the direct buffer path ofwriteToread the slabs throughduplicate(). Before, they calledflip()on the slabs themselves, so serializing changed the slabs' position and limit.setByteusedslab.limit()to find the slab holding a byte, which assumes that every slab but the last is full. SincewriteInt/writeLongwere added (GH-3530: Optimize PLAIN encoding and decoding with direct ByteBuffer I/O #3565), a slab can be followed by a new one before it is full, and thensetBytepatched the wrong byte. It now usesslab.position(). Today the only caller ofsetByteisRunLengthBitPackingHybridEncoder, which never callswriteInt/writeLong, so no current code path reaches this. It is fixed because it is the same kind of size/content mismatch.Are these changes tested?
Yes. The new
TestCapacityByteArrayOutputStreamruns with heap and direct allocators:setByteafter a partially filled slab. Fails without this change.writeTo,writeIntoandBytesInput.writeTo,writeIntoandBytesInput.toByteArray()throw, and nothing is written to the output stream. Fails without this change.The existing tests of parquet-common (346), parquet-encoding (34) and parquet-column (973) pass, as do the parquet-hadoop writer tests (
TestParquetWriter*,TestColumnChunkPageWriteStore,TestParquetFileWriter,TestDataPageChecksums,TestStatistics,TestDirectCodecFactory).Are there any user-facing changes?
A write that would have produced a corrupt page now fails with an
IllegalStateExceptionwhose message gives both sizes.Refs #3712. This does not close the issue, because the root cause is still unknown.
🤖 Generated with Claude Code