Repository navigation
[client-v2] Native reader: check each block against the first block - #3367
Conversation
The Native reader decodes some nested types (for example Array(Array(Int8)) or Map(String, Array(Nullable(Float64)))) with the RowBinary layout and reads a wrong number of bytes. It then read the next bytes as a block header, for example a block with no columns and some rows, and hasNext()/next() returned the rows of that block. NativeFormatReaderDataSetTest failed for these two types with "More rows than in the data set". All blocks of one Native result have the same columns. The reader now keeps the header of the first block and fails with ClientException if a later block has other columns. It compares the length of each name and type before it reads the bytes, so a length from wrong bytes cannot cause a large allocation. Also: - The schema is created only from the first block. Before, each block replaced the row buffers, so hasNext() at the end of a block changed the values that the getters returned for the current row. - hasNext(), readRecord(Map) and readRecord(Object[]) use one method to move to the next row. readRecord(Map) no longer throws NullPointerException for an empty result or IndexOutOfBoundsException for a block without rows. - A stream that ends inside the column count of a block now fails. Before, it was the end of the result. - Javadoc explains the stream layout and how the reader reads rows. Part of: #3365 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Docs drift check: resolved, docs now match this PR's changes. |
There was a problem hiding this comment.
Auto-approved by the AI review gate on 04fe0eb. Details in the approver's comment on this pull request.
AI review gate — auto-approved
History (latest 5 heads, newest first)
|
Client V2 CoverageCoverage Report
Class Coverage
|
JDBC V2 CoverageCoverage Report
Class Coverage
|
The Native reader now compares each block with the first block, fails with ClientException on a different block, keeps the schema of the first block, skips blocks without rows, and has an idempotent hasNext(). Add this contract to the binary format readers entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Done in ed55b05: the |
TriageCategory: Summary What this impacts
Concerns
Required reviewer action
|
There was a problem hiding this comment.
Auto-approved by the AI review gate on ed55b05. Details in the approver's comment on this pull request.
chernser
left a comment
There was a problem hiding this comment.
see comment - make corrections please
…ssage Address review on #3367: - Read the column count with BinaryStreamReader.readVarInt and end the result on EOFException, as before. No SequenceInputStream. - Remove the private string helpers. Read names and types with BinaryStreamReader.readString and compare them with the first block. - Make the block-check message short. It refers to the class Javadoc, which now explains the check, its causes, and the RowBinary workaround. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit d4e4d8e. Configure here.
AI review gate — Human approval in place of AI reviewApproved on the head commit by chernser (write access, not the author), standing in for the AI review:
What to do:
Evaluated at |
…ytes A later block that is read from wrong bytes can have a very large name or type length. Reading the string first allocated that length, so the reader could throw OutOfMemoryError instead of ClientException. The reader now keeps the name and type bytes of the first block and compares the length of each later string before it reads the bytes. The bytes are compared as the server wrote them, so a name that is not UTF-8 still matches. The commit also restores the CRLF line endings of NativeFormatReader.java, as on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The maintainer decided that the column count check is enough and that the header strings are read with BinaryStreamReader.readString. This reverts the code and the tests of the previous commit. NativeFormatReader.java keeps the CRLF line endings of main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary
Part of #3365. This PR is the first step that the maintainer asked for: it fixes the problems of the Native reader and documents how the reader works. The other readers are not changed.
Root cause. The Native reader decodes some nested types (for example
Array(Array(Int8))orMap(String, Array(Nullable(Float64)))) with the RowBinary layout, and reads a wrong number of bytes for them. This limitation is known (NATIVE_COMPOSITESinReaderKnownIssues). After that column, the reader read the next bytes as a block header. On ClickHouse 26.10 these bytes gave a block with no columns and some rows. Since #3294,hasNext()/next()return the rows of such a block, soNativeFormatReaderDataSetTestfailed for these two types withMore rows than in the data set: 12and: 17. Before #3294, the one-row read-ahead failed on the last real row, and only by accident.Fix. All blocks of one Native result have the same columns: the server writes each block with the same header. I checked this on 26.10 with
max_block_size = 2. The reader now keeps the header of the first block. If a later block has other columns, the reader fails withClientExceptionand returns no row of that block. Before it reads a name or a type, it compares the length, so a length read from wrong bytes cannot cause a large allocation.The same change also fixes these problems in the block iteration:
setSchema(), which replaced the row buffers. Sonext(); hasNext();at the end of a block madegetInteger("n")read from an empty buffer.readRecord.hasNext(),readRecord(Map)andreadRecord(Object[])now move to the next row with one method,hasRowInBlock(). It reads a block only when the current block has no row left, and it skips blocks without rows. SohasNext()is idempotent.readRecord(Map)no longer throwsNullPointerExceptionfor an empty result (the server sends 0 bytes) orIndexOutOfBoundsExceptionfor a block without rows.Changes
client-v2/.../NativeFormatReader.java:hasRowInBlock());client-v2/.../NativeFormatReaderTest.java: new tests (see below).history/latest/3365.md: change record.Test
NativeFormatReaderDataSetTest: all 76 cases pass,testKnownIssuesAreCurrentincluded. Onmain,Array(Array(Int8))andMap(String, Array(Nullable(Float64)))fail. TheNATIVE_COMPOSITESknown issues (READ_FAILURE,WRONG_VALUE) still match. No known issue was changed.max_block_size = 10, with 0, 1 and 25 rows):testHasNextIsIdempotentcallshasNext()again before and after eachnext(), then checks the map and the getters. Onmainit fails for 25 rows: the current row is lost at a block boundary.testReadRecordReadsEveryRow: onmainit fails for 0 rows with an NPE.testFailsOnBrokenBlock: a later block with no columns, another column count, another name or another type, a stream that ends inside the column count, and a stream that ends inside the values. Onmain, the first five cases do not throw.testSkipsBlocksWithoutRows: block row counts[],[0],[0,2],[2,0,1]and[2,0], read withnext()and withreadRecord(Map). Onmain,[],[2,0,1]and[2,0]fail.testReadsBlocksWithManyColumns: 130 columns, so the column count takes two bytes.mvn -pl client-v2 testpasses (961 tests);NativeFormatReaderTest,QueryTests,DataTypeTestsand all*DataSetTestpass (919 tests);javadoc:javadocpasses.Compatibility
There is no public API change. All new members are private. A valid server stream is read as before. A stream in which the reader loses its position now fails with
ClientException. Before, the reader returned extra rows (onmain) or failed with an error that did not tell the cause (in 0.11.x). TheClientExceptionmessage gives the likely causes: a column type that the Native reader does not decode, or a server error written into the stream. It also tells the user to use RowBinaryWithNamesAndTypes.docs/changes_checklist.md:ClientException, the same as the sibling QBit error in the same method.BinaryStreamReaderis not changed.Checklist
com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader#3365 (the issue stays open for the other readers)history/latest/3365.md)🤖 Generated with Claude Code
Note
Medium Risk
Changes core Native binary parsing and iteration; valid streams should behave the same, but desynchronized or partial streams now throw instead of returning extra or corrupted rows.
Overview
Fixes client-v2
NativeFormatReaderso mis-decoded columns (e.g. some nestedArray/MapNative layouts) no longer produce spurious rows from misaligned block headers. Later blocks are checked against the first block’s column count, names, and types; a mismatch raisesClientException(“not at a block boundary”) instead of returning bogus data.Refactors iteration around
hasRowInBlock(): schema is established once from the first block, empty-row blocks are skipped,hasNext()is idempotent (no read-ahead that overwrites the current row), andreadRecordavoids NPE/OOB on empty results and zero-row blocks. Truncated streams after a block’s column count now fail rather than ending cleanly.Adds Javadoc,
docs/features.mdnotes, changelog (history/latest/3365.md), and unit/integration tests for block boundaries, idempotenthasNext, andreadRecordcoverage.Reviewed by Cursor Bugbot for commit f2b8fba. Bugbot is set up for automated code reviews on this repo. Configure here.