Skip to content

[client-v2] Reader API that decodes RowBinary values without per-value objects #3449

Backlog

Description

@Marais

Problem

RowBinaryWithNamesAndTypesFormatReader turns every value into a Java object before the caller sees it:

  • One stream call per value. BinaryStreamReader reads each value from the InputStream separately. For example, readIntLE calls readNBytes(input, buff, 0, 4) and puts the int together with shifts, so there's at least one read call per value, through CompressedBlockInputStream and its wrappers.
  • One object per value. readRecord stores each value in an Object[] (record[i] = val), and the typed getters go through it: getInteger → readNumberValue → readValue(index). That means:
    • Int32 → Integer
    • UInt64 → BigInteger
    • Date → LocalDate
    • DateTime64 → ZonedDateTime
    • String → String, or with binary_string_support a StringValue around a new byte[]

A caller that writes values into its own format, such as Spark's rows, pays for these objects and then converts them back.

Workaround in the Spark connector

ClickHouse/spark-clickhouse-connector#588 reads the RowBinary bytes itself, with its own buffered reader. It only uses client-v2 for the query, the HTTP transport, decompression and parsing the column types. In our benchmark against client-v2 0.9.5 decoding, it used 1.6–5.3× less CPU, depending on the column types. That figure covers buffering and avoiding objects together; we didn't measure them separately.

The cost is that the connector has to copy client-v2's behaviour exactly, and notice when it changes. Moving from 0.9.5 to 0.11.1 changed:

  • The name of the decompressing stream class (ClickHouseLZ4InputStream → CompressedBlockInputStream)
  • The Date conversion
  • The varint length limit
  • The wrapper around the response stream
  • The text of read errors

Proposal

  1. Buffered reads. Read the decompressed stream in large chunks into an internal buffer. Decode fixed-width values from it with a little-endian ByteBuffer view (getInt, getLong), and only call the stream when the buffer is used up. This helps every caller, including the existing object API.

  2. A cursor over the row, without per-value objects. RowBinary has no offsets, so values are read in column order, each exactly once:

    • Primitives: integers and floats as primitives, UInt64 as its raw long bits.
    • Dates and times: Date and Date32 as an epoch-day int, DateTime and DateTime64 as raw seconds or ticks (long), with the scale taken from the column.
    • Strings: String and FixedString as a buffer, offset and length, valid until the next row.
    • Nullable: a null flag before the value, at any depth.
    • Array: the length, then each element with the same accessors.
    • Map: the entry count, then key, value, key, value.
    • Tuple: the fields in order.

    JSON, Variant, Dynamic and the interval types could stay on the object API to start with; the connector sends those to client-v2 today too. The existing getters, which allow random access, could be built on the cursor.

With this, the connector would drop its own reader and its copies of client-v2's behaviour, and only map values into Spark rows.

Activity

  1. polyglotAI-bot commented on Nov 26, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — maintainer decision
    Maintainers decide whether client-v2 gets a RowBinary read API without a Java object per value, and whether the buffered reads go first as an internal change.

    • Category: Feature
    • Classification: impact: Performance · area: Data types & formats (RowBinary)
    • Status on main: Not supported on main — the reader code on main (8480ca001) works as the issue describes.
    • Breaking change: No — can be done without a break, as a new API next to the getters.
    • Duplicates / related: Related: Major performance regression in client-v2 #2516 — its first root cause is the same per-row Object[] with boxed values.

    Analysis: readRecord puts each value into an Object[] (AbstractBinaryFormatReader.java:186-197), and the typed getters that return primitives (getInteger, getLong) read from that array (AbstractBinaryFormatReader.java:396). BinaryStreamReader.readNBytes calls the stream for each value (BinaryStreamReader.java:616-626). ClickHouseBinaryFormatReader has no method that gives a value without an object.

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

  2. added
    client
    data-types
    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

    clientdata-typesenhancementperformance

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions