Skip to content

[client-v2] CompressedBlockInputStream: a stream that ends right after a block header reads as a clean end, dropping rows #3448

Backlog

Description

@Marais

What happens

CompressedBlockInputStream.refill throws EOFException("Unexpected end of stream") when a block's 25-byte header is complete but the stream ends before any body byte. If the block starts at a row boundary, AbstractBinaryFormatReader.readRecord treats that as the end of data (e instanceof EOFException && firstColumn). The read then ends cleanly with rows missing.

The chance of this is low. The stream has to end cleanly (read returns -1) exactly after a block header, at a row boundary. A dropped HTTP connection throws ConnectionClosedException instead, and that read fails with an error as it should. A stream cut inside a block's header or body also fails correctly ("Incomplete read").

Version: client-v2 0.11.1.

Repro

One Int32 column, 50 rows, LZ4 blocks of 49 bytes, so block 1 holds the header and rows 0-9 exactly.

ByteArrayOutputStream plain = new ByteArrayOutputStream();
plain.write(new byte[] {1, 1, 'x', 5, 'I', 'n', 't', '3', '2'});
for (int i = 0; i < 50; i++) plain.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(i).array());

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream lz4 = new CompressedBlockOutputStream.LZ4OutputStream(out, LZ4Factory.fastestJavaInstance().fastCompressor(), 49);
lz4.write(plain.toByteArray());
lz4.close();
byte[] compressed = out.toByteArray();
int block1 = 16 + ByteBuffer.wrap(compressed, 17, 4).order(ByteOrder.LITTLE_ENDIAN).getInt();

QuerySettings settings = new QuerySettings();
settings.setOption(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey(), Boolean.FALSE);
settings.setOption(ClientConfigProperties.USE_TIMEZONE.getKey(), TimeZone.getTimeZone("UTC"));
InputStream in = new CompressedBlockInputStream(
    new ByteArrayInputStream(Arrays.copyOf(compressed, block1 + 25)), // block 2's header, no body
    LZ4Factory.fastestJavaInstance().fastDecompressor(), 64 * 1024);
RowBinaryWithNamesAndTypesFormatReader reader =
    new RowBinaryWithNamesAndTypesFormatReader(in, settings, new BinaryStreamReader.DefaultByteBufferAllocator());
int rows = 0;
while (reader.hasNext()) { reader.next(); rows++; }
System.out.println(rows); // 10, no exception
Stream cut Result
not cut 50 rows
inside block 2's header IOException: Incomplete read: 10 of 25
inside block 2's body IOException: Incomplete read: 5 of 51
right after block 2's header 10 rows, no error

Suggested fix

  1. In refill, throw a plain IOException (or ClientException) for a block whose body is missing, as a partly read body already does.
  2. Related: when read(b, off, len) spans several blocks and a later refill throws, the bytes already copied from earlier blocks are lost. Returning them and throwing on the next call (as BufferedInputStream does) would let callers that read in large chunks fail at the same position as callers that read in small ones.

Activity

  1. polyglotAI-bot commented on Nov 26, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Review PR #3322: with its diff, the issue's steps fail with DataTransferException: Unexpected end of stream in place of a clean end after 10 rows. The PR does not link this issue, and its diff has a merge conflict with main in ClientConfigProperties.java.

    • Category: Bug
    • Classification: impact: Data integrity · area: Connectivity (compressed responses) · native: protocol
    • Status on main: Reproduced on main — the issue's steps print 10 with no error, as the issue shows (main at 8480ca001; the steps need no server).
    • Breaking change: No for a minimal fix that throws an IOException. PR Improve compression handling #3322 also changes the other compressed-read errors to DataTransferException, which its own summary calls breaking.
    • Duplicates / related: No others.

    Analysis: Confirmed: refill throws the EOFException for an empty block body (CompressedBlockInputStream.java:136-138), and readRecord takes an EOFException at the first column as the end of the data (AbstractBinaryFormatReader.java:202-205). The second point of the issue (read drops the bytes of earlier blocks when a later refill throws, CompressedBlockInputStream.java:59-63) changes only where the read fails, not whether it fails.

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  2. added
    client
    connectivity
    native-protocol
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugclientconnectivitynative-protocol

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions