Repository navigation
Description
Description
NativeFormatReader cannot read a JSON or Dynamic column. With QuerySettings.setFormat(ClickHouseFormat.Native), every shape that I tested fails:
- When another column follows the
JSONorDynamiccolumn,client.newBinaryFormatReader(response)throwsIllegalArgumentException: Non-empty typeName is required. This is not aClientException, and the message does not name the column. - When the
JSONorDynamiccolumn is the last column, the reader first returns all rows of the block with wrong values:{}for eachJSONrow,0and thennullforDynamic. ThenhasNext()fails withClientException: Failed to read block: it has 0 columns, .... A caller that processes each row when it arrives uses the wrong values before it gets the error. - In a block of 300 rows, the read can also fail with
ClientException: Failed to read value for column j(cause:Unsupported data type with tag 97).
Tested: plain JSON, JSON(a UInt32), a nested object, Dynamic, one block and more blocks (max_block_size=1). The server settings output_format_native_write_json_as_string=1 and output_format_native_use_flattened_dynamic_and_json_serialization=1 give the same failures. The same queries read correctly with RowBinaryWithNamesAndTypes (for example {a=7, b=x}).
This is separate from #3156 (nested types and Variant decoded row-wise) and #3193 (LowCardinality).
Steps to reproduce
- Create a client-v2
Clientfor an HTTP endpoint. - Run
SELECT 1::UInt8 AS k, '{"a": 7, "b": "x"}'::JSON AS j, 1.5::Float64 AS fwithQuerySettings.setFormat(ClickHouseFormat.Native), and callclient.newBinaryFormatReader(response). - Run
SELECT number AS n, number::Dynamic AS d FROM numbers(3)in the same way, and read all rows (Code Example).
Error Log or Exception StackTrace
Step 2:
java.lang.IllegalArgumentException: Non-empty typeName is required
at com.clickhouse.data.ClickHouseDataType.of(ClickHouseDataType.java:488)
at com.clickhouse.data.ClickHouseColumn.readColumn(ClickHouseColumn.java:733)
at com.clickhouse.data.ClickHouseColumn.of(ClickHouseColumn.java:782)
at com.clickhouse.client.api.data_formats.NativeFormatReader.readBlock(NativeFormatReader.java:204)
at com.clickhouse.client.api.data_formats.NativeFormatReader.<init>(NativeFormatReader.java:93)
at com.clickhouse.client.api.Client.newBinaryFormatReader(Client.java:2507)
Step 3:
n=0 d=0
n=1 d=null
n=2 d=null
com.clickhouse.client.api.ClientException: Failed to read block: it has 0 columns, the first block has 2. The reader is not at a block boundary, see the Javadoc of com.clickhouse.client.api.data_formats.NativeFormatReader
Expected Behaviour
The reader returns the values of the server, as RowBinaryWithNamesAndTypes does. Server output (FORMAT JSONEachRow):
{"k":1,"j":{"a":7,"b":"x"},"f":1.5}
{"n":0,"d":0}
{"n":1,"d":1}
{"n":2,"d":2}
In the Native format, JSON and Dynamic start each block with a serialization version, followed by a column-oriented structure (Native format spec: "Versioned / stateful types"; over HTTP the default is V1). Server bytes of SELECT '{"a": 7}'::JSON AS j FORMAT Native, after the column header:
00 00 00 00 00 00 00 00 JSON serialization version 0 (V1)
01 01 01 61 dynamic paths: 1, name "a"
01 00 00 00 00 00 00 00 Dynamic serialization version of path "a", then its types and values
Root cause
NativeFormatReader.readBlock reads a JSON or Dynamic column in its last branch (client-v2/src/main/java/com/clickhouse/client/api/data_formats/NativeFormatReader.java:275-280), with binaryStreamReader.readValue(column) once for each row. This reaches the RowBinary decoders: readJsonData (BinaryStreamReader.java:1846, a path count and path-value pairs in each row) and readDynamicData (BinaryStreamReader.java:148 and :1629, a type tag in each row). So the reader reads wrong bytes, and then reads data as the next column header.
The fix needs a maintainer decision: decode the Native layout (for example the documented flattened layout, which the server sends when the client sets output_format_native_use_flattened_dynamic_and_json_serialization=1, ClickHouse 25.6 and later), or reject JSON and Dynamic columns with a clear ClientException, as readBlock already does for the QBit shapes that it does not decode.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Code Example
try (Client client = new Client.Builder().addEndpoint("http://localhost:8123")
.setUsername("default").setPassword("").build();
QueryResponse response = client.query(
"SELECT number AS n, number::Dynamic AS d FROM numbers(3)",
new QuerySettings().setFormat(ClickHouseFormat.Native)).get()) {
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
while (reader.hasNext()) {
reader.next();
System.out.println("n=" + reader.readValue("n") + " d=" + reader.readValue("d"));
}
}
// Native: n=0 d=0, n=1 d=null, n=2 d=null, then hasNext() throws ClientException
// RowBinaryWithNamesAndTypes: n=0 d=0, n=1 d=1, n=2 d=2Configuration
Client Configuration
new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword("").build()Environment
- Cloud
- Client version:
0.12.0-rc1-SNAPSHOT(mainatf16f8e1a2) - 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 TABLEstatements for tables involved: none, the queries are self-contained- Sample data for all these tables: none
Classification: impact: none · area: Data types & formats (JSON, Dynamic, Native) · native: format
Found by automated analysis (Polyglot AI) while working on #3525. Verified against a live server.
Activity