Skip to content

[client-v2] Reading a Dynamic value that holds a tuple without element names fails or returns wrong data #3384

Open

Description

@Indian-boult

Description

client-v2 cannot read a Dynamic value when its type has a tuple without element names, such as Tuple(Int32, String), Array(Tuple(Int32, String)) or Map(String, Tuple(Int32, String)). Tuples with element names work. The same happens in jdbc-v2, because it uses the client-v2 reader.

The server writes the type of every Dynamic value in the binary type encoding. An unnamed tuple is 0x1F<var_uint_number_of_elements><nested_type_encoding_1>...<nested_type_encoding_N> and a named tuple is 0x20 with a name before each element type. BinaryStreamReader.readDynamicData looks the tag up in ClickHouseDataType.binTag2Type first. 0x1F is the tag of ClickHouseDataType.Tuple, so the lookup finds it and the switch goes to its default branch. That branch returns a bare Tuple column and leaves the element count and the element types in the stream. The tuple branch that reads them only runs for 0x20.

A bare Tuple inside another type gives the type name Array(Tuple), which fails with Missing nested data type. At the top level the tuple is read as an empty Object[] and the next values are read from the element type bytes. The result then depends on the bytes that follow. It can be Unsupported data type with tag 97, an EOFException, wrong values in the next columns and extra rows, or an empty result with no error.

These are the results on origin/main at 7506301 against ClickHouse 26.10.4.207. Client#queryAll, Client#newBinaryFormatReader and jdbc-v2 ResultSet#getObject fail the same way. The binary format reader and jdbc-v2 return the first wrong rows before they fail.

Query Result
SELECT [(1, 'a')]::Array(Tuple(Int32, String))::Dynamic AS d, 7::Int64 AS tail IllegalArgumentException: Missing nested data type
SELECT (1, 'a')::Tuple(Int32, String)::Dynamic AS d, 7::Int64 AS tail ClientException: Unsupported data type with tag 97
SELECT ((1, 'a'), 2)::Tuple(Tuple(Int32, String), Int8)::Dynamic AS d, 7::Int8 AS tail ClientException: Unsupported data type with tag 97
SELECT ((1, 'a'), 2)::Tuple(Tuple(Int32, String), Int8)::Dynamic AS d, 7::Int64 AS tail d is an empty Object[], tail is 289261903552258, then EOFException
SELECT ((1, 'a'), 2)::Tuple(Tuple(Int32, String), Int8)::Dynamic AS d, 7::Int32 AS tail no error, 2 rows, in the first row d is an empty Object[] and tail is 151133954
SELECT (1, 2)::Tuple(Nullable(Int32), Int8)::Dynamic AS d, 7::Int64 AS tail d is an empty Object[], tail is 1099629667074, then EOFException
SELECT (1, 2)::Tuple(Nullable(Int32), Int8)::Dynamic AS d, 7::Int8 AS tail no error, 3 rows, d is an empty Object[] in the first row
SELECT map('k', (1, 'a'))::Map(String, Tuple(Int32, String))::Dynamic AS d, 7::Int64 AS tail Missing nested data type
SELECT [[(1, 'a')], [(2, 'b'), (3, 'c')]]::Array(Array(Tuple(Int32, String)))::Dynamic AS d, 7::Int64 AS tail Missing nested data type
SELECT [(1, 'a'), 'x']::Array(Variant(String, Tuple(Int32, String)))::Dynamic AS d, 7::Int64 AS tail Missing nested data type
SELECT ((1, 'a'), 2)::Tuple(a Tuple(Int32, String), b Int8)::Dynamic AS d, 7::Int64 AS tail Missing nested data type
SELECT '{"a": [1, "x"]}'::JSON(a Tuple(Int32, String))::Dynamic AS d, 7::Int64 AS tail no error and no rows

The same queries with named tuples, such as Tuple(x Int32, y String), return the right values. ClickHouse 25.8.33.6 sends the same bytes.

Steps to reproduce

  1. Start ClickHouse 26.10.
  2. Run SELECT [(1, 'a')]::Array(Tuple(Int32, String))::Dynamic AS d, 7::Int64 AS tail with Client#queryAll.
  3. The query fails with Missing nested data type.

Error Log or Exception StackTrace

com.clickhouse.client.api.ClientException: Failed to get query response
	at com.clickhouse.client.api.Client.queryAll(Client.java:2252)
	at com.clickhouse.client.api.Client.queryAll(Client.java:2265)
Caused by: java.lang.IllegalArgumentException: Missing nested data type
	at com.clickhouse.data.ClickHouseColumn.readColumn(ClickHouseColumn.java:553)
	at com.clickhouse.data.ClickHouseColumn.readColumn(ClickHouseColumn.java:499)
	at com.clickhouse.data.ClickHouseColumn.of(ClickHouseColumn.java:782)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readDynamicData(BinaryStreamReader.java:1663)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:148)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:135)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:117)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:101)
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.readRecord(AbstractBinaryFormatReader.java:168)
	at com.clickhouse.client.api.Client.queryAll(Client.java:2244)
com.clickhouse.client.api.ClientException: Failed to get query response
	at com.clickhouse.client.api.Client.queryAll(Client.java:2252)
	at com.clickhouse.client.api.Client.queryAll(Client.java:2265)
Caused by: com.clickhouse.client.api.ClientException: Unsupported data type with tag 97
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readDynamicData(BinaryStreamReader.java:1657)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:148)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:135)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:117)
	at com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.readValue(BinaryStreamReader.java:101)
	at com.clickhouse.client.api.data_formats.internal.AbstractBinaryFormatReader.readRecord(AbstractBinaryFormatReader.java:168)
	at com.clickhouse.client.api.Client.queryAll(Client.java:2244)

The bytes of SELECT ((1, 'a'), 2)::Tuple(Tuple(Int32, String), Int8)::Dynamic AS d, 7::Int64 AS tail FORMAT RowBinary show the element types that are not read.

1f 02 1f 02 09 15 07   type Tuple(Tuple(Int32, String), Int8)
01 00 00 00 01 61 02   value ((1, 'a'), 2)
07 00 00 00 00 00 00 00   tail

Expected Behaviour

The reader reads the element count and the element types of an unnamed tuple, the same as for a named tuple. The first query returns one row with d equal to [[1, a]] and tail equal to 7, and the other columns stay intact.

Code Example

try (Client client = new Client.Builder()
        .addEndpoint("http://localhost:8123")
        .setUsername("default")
        .setPassword("")
        .build()) {
    List<GenericRecord> rows = client.queryAll(
            "SELECT [(1, 'a')]::Array(Tuple(Int32, String))::Dynamic AS d, 7::Int64 AS tail");
    System.out.println(rows.get(0).getObject("d"));
}

Configuration

Client Configuration

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

Environment

  • Cloud
  • Client version: client-v2 and jdbc-v2 0.12.0-rc1-SNAPSHOT, origin/main at 7506301
  • Language version: Java 21
  • OS: macOS

ClickHouse Server

  • ClickHouse Server version: 26.10.4.207, and 25.8.33.6 sends the same bytes
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none, the queries use literals
  • Sample data for all these tables, use clickhouse-obfuscator if necessary: the literals in the queries above

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions