Skip to content

[client-v2] Native format: SimpleAggregateFunction of Nullable or Array is misread (wrong values or a failed read) #3525

Backlog

Description

@polyglotAI-bot

Description

With ClickHouseFormat.Native, the client-v2 binary reader misreads a SimpleAggregateFunction(f, T) column when T is Nullable(...) or Array(...). The same T without the wrapper is read correctly, and RowBinaryWithNamesAndTypes reads both. The same defect was reported for clickhouse-js in ClickHouse/clickhouse-js#1281.

  • SimpleAggregateFunction(anyLast, Nullable(String)): wrong values, no error.
  • SimpleAggregateFunction(groupArrayArray, Array(UInt32)): IllegalArgumentException: Non-empty typeName is required.

SimpleAggregateFunction of UInt64, Float64, String, Decimal and DateTime64 is read correctly. SimpleAggregateFunction of LowCardinality(String) or of Tuple(Array, Array) fails as the plain types do (#3193; #3156, which does not include this wrapper).

Root cause: NativeFormatReader.readBlock selects the Native layout from the wrapper type. column.isArray() (NativeFormatReader.java:237) and column.isNullable() (NativeFormatReader.java:252) are false for the wrapper, so the column goes to the per-row branch (NativeFormatReader.java:278). There, BinaryStreamReader.readValue reads T with the RowBinary layout (BinaryStreamReader.java:289-290): a null marker before each value, and a length before each array.

docs/features.md and the NativeFormatReader Javadoc list layouts that the reader does not decode ("for example LowCardinality, or some nested Array, Map, and Tuple types"). This wrapper of a decoded type is not in that list.

The fix is to use the inner column of the wrapper in the layout selection of readBlock. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Steps to reproduce

  1. Run the program in "Code Example". It creates the table saf_native, inserts two rows, and reads three queries with RowBinaryWithNamesAndTypes and with Native (QuerySettings.setFormat, then Client.newBinaryFormatReader).
  2. Compare the output with the server: SELECT k, s, a, f FROM saf_native ORDER BY k FORMAT JSONEachRow.

Error Log or Exception StackTrace

SELECT k, s, f FROM saf_native ORDER BY k
  RowBinaryWithNamesAndTypes: k=1 s=x f=0.5 | k=2 s=null f=1.5 |
  Native: k=1 s=\u0001 f=0.5 | k=2 s= f=1.5 |
SELECT k, CAST(s AS Nullable(String)) AS s, f FROM saf_native ORDER BY k
  RowBinaryWithNamesAndTypes: k=1 s=x f=0.5 | k=2 s=null f=1.5 |
  Native: k=1 s=x f=0.5 | k=2 s=null f=1.5 |
SELECT k, a, f FROM saf_native ORDER BY k
  RowBinaryWithNamesAndTypes: k=1 a=[1, 2] f=0.5 | k=2 a=[3] f=1.5 |
  Native: java.lang.IllegalArgumentException: Non-empty typeName is required

Expected Behaviour

Native returns the same values as RowBinaryWithNamesAndTypes and as the server:

{"k":1,"s":"x","a":[1,2],"f":0.5}
{"k":2,"s":null,"a":[3],"f":1.5}

The Native format specification says: "SimpleAggregateFunction(func, T) is an alias for its value type T. [...] its wire form and rendering are exactly those of T". The server writes the same column data for the wrapper and for T. For SELECT a ... FORMAT Native and SELECT CAST(a AS Array(UInt32)) AS a ... FORMAT Native, only the type name differs. The data of both is:

02 00 00 00 00 00 00 00  03 00 00 00 00 00 00 00   offsets 2, 3
01 00 00 00  02 00 00 00  03 00 00 00              elements 1, 2, 3

Code Example

Reproduction program (Java, 45 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.client.api.query.QueryResponse;
import com.clickhouse.client.api.query.QuerySettings;
import com.clickhouse.data.ClickHouseFormat;

public class SafNative {
    public static void main(String[] args) throws Exception {
        try (Client client = new Client.Builder().addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword(System.getenv("CLICKHOUSE_PASSWORD")).build()) {
            client.query("DROP TABLE IF EXISTS saf_native").get().close();
            client.query("CREATE TABLE saf_native (k UInt8, s SimpleAggregateFunction(anyLast, Nullable(String)), "
                    + "a SimpleAggregateFunction(groupArrayArray, Array(UInt32)), "
                    + "f SimpleAggregateFunction(max, Float64)) ENGINE = AggregatingMergeTree ORDER BY k").get().close();
            client.query("INSERT INTO saf_native VALUES (1, 'x', [1, 2], 0.5), (2, NULL, [3], 1.5)").get().close();
            for (String sql : new String[]{"SELECT k, s, f FROM saf_native ORDER BY k",
                    "SELECT k, CAST(s AS Nullable(String)) AS s, f FROM saf_native ORDER BY k",
                    "SELECT k, a, f FROM saf_native ORDER BY k"}) {
                System.out.println(sql);
                for (ClickHouseFormat format : new ClickHouseFormat[]{ClickHouseFormat.RowBinaryWithNamesAndTypes, ClickHouseFormat.Native}) {
                    try (QueryResponse response = client.query(sql, new QuerySettings().setFormat(format)).get();
                         ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response)) {
                        StringBuilder out = new StringBuilder();
                        while (reader.hasNext()) {
                            reader.next();
                            for (int i = 1; i <= reader.getSchema().getColumns().size(); i++) {
                                Object v = reader.readValue(i);
                                if (v instanceof BinaryStreamReader.ArrayValue) {
                                    v = ((BinaryStreamReader.ArrayValue) v).asList();
                                }
                                out.append(reader.getSchema().getColumns().get(i - 1).getColumnName()).append('=')
                                        .append(v == null ? "null" : String.valueOf(v).replace("\u0001", "\\u0001")).append(' ');
                            }
                            out.append("| ");
                        }
                        System.out.println("  " + format + ": " + out);
                    } catch (Exception e) {
                        System.out.println("  " + format + ": " + e + (e.getCause() == null ? "" : " <- " + e.getCause()));
                    }
                }
            }
        }
    }
}

Configuration

Client Configuration

new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword(password).build()

Environment

  • Cloud
  • Client version: 0.12.0-rc1-SNAPSHOT (main at f16f8e1a2)
  • Language version: JDK 17.0.21
  • OS: Ubuntu 22.04 (container)

ClickHouse Server

  • ClickHouse Server version: 26.11.7.10
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in "Code Example"
  • Sample data for all these tables: the INSERT in "Code Example"

Classification: impact: Data integrity · area: Data types & formats (SimpleAggregateFunction, Native) · native: format


Found by automated analysis (Polyglot AI) while checking clickhouse-js#1281 against the other client repos. Verified against a live server.

Activity

  1. added
    client
    data-types
    native-format
    on Dec 4, 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

    bugclientdata-typesnative-format

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions