[client-v2] getShortArray/getIntArray/getLongArray throw ClassCastException on an empty Array(UInt8/UInt16/UInt32)

未關閉 適合新手
#3,396 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@abdalrahmanehab 已經在處理了。

開始於 2026年11月21日。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
80/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java
領域
api, backend

研究方向

從 BinaryStreamReader.readArray() 和 BinaryStreamReaderTests 中對空陣列的處理開始,尤其是 provider emptyArrayComponentTypes。比較空的和非空的無號陣列如何選擇元件型別,接著驗證對應的型別化存取器對 UInt8、UInt16 和 UInt32 都會傳回長度為零的陣列。Issue 建議透過 queryAll() 和 newBinaryFormatReader() 涵蓋回歸情境,並在每個陣列後放置一個定寬欄位。

由索引模型根據 Issue 內容生成。

描述

bug client data-types

Description

On main and 0.11.2, getShortArray(), getIntArray() and getLongArray() fail with ClassCastException on an empty Array(UInt8), Array(UInt16) or Array(UInt32). On a non-empty array of the same column, the same call returns the correct value. So the same call works on some rows and fails on other rows.

An empty array and a non-empty array of one column get different Java component types:

Column type non-empty value empty value accessor for non-empty accessor on empty
Array(UInt8) short[] byte[] getShortArray() OK getShortArray() fails
Array(UInt16) int[] short[] getIntArray() OK getIntArray() fails
Array(UInt32) long[] int[] getLongArray() OK getLongArray() fails
Array(Int8/Int16/Int32/Int64), Array(Float32/Float64), Array(Bool) same type for both OK OK

This occurs on all read paths: queryAll() (MapBackedRecord, a bare ClassCastException), and newBinaryFormatReader() and queryRecords() (ClientException: Value cannot be converted to an array of primitives, caused by the ClassCastException). getObjectArray() and getList() work for both rows. The column after the array is read correctly, so the stream is not desynchronized.

This is a regression in 0.11.2. On 0.11.1, the empty and the non-empty array have the same component type for all three types, and each accessor that works on [1] also works on [].

Steps to reproduce
  1. Run the code example below against any server.
  2. getShortArray("a") on [1, 2] returns [1, 2].
  3. getShortArray("b") on [] of the same type throws ClassCastException.
Error Log or Exception StackTrace
non-empty: [1, 2]
Exception in thread "main" java.lang.ClassCastException: class [B cannot be cast to class [S ([B and [S are in module java.base of loader 'bootstrap')
	at com.clickhouse.client.api.data_formats.internal.MapBackedRecord.getShortArray(MapBackedRecord.java:315)
	at Min.main(Min.java:12)

With newBinaryFormatReader():

com.clickhouse.client.api.ClientException: Value cannot be converted to an array of primitives
Caused by: java.lang.ClassCastException: class [B cannot be cast to class [S
Expected Behaviour

An accessor that works on a non-empty value of a column also works on an empty value of that column, and returns a zero-length array: short[0], int[0] or long[0]. That is the 0.11.1 behavior. The server sends the same column type for both values:

$ curl ... --data-binary "SELECT toTypeName(CAST([] AS Array(UInt8))) e, toTypeName(CAST([1] AS Array(UInt8))) n, CAST([] AS Array(UInt8)) ev, CAST([1] AS Array(UInt8)) nv FORMAT TSVWithNames"
e	n	ev	nv
Array(UInt8)	Array(UInt8)	[]	[1]
Code Example
Reproduction program (Java, 15 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.query.GenericRecord;
import java.util.Arrays;

public class Min {
    public static void main(String[] args) {
        try (Client client = new Client.Builder().addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword("").build()) {
            GenericRecord r = client.queryAll(
                    "SELECT CAST([1, 2] AS Array(UInt8)) AS a, CAST([] AS Array(UInt8)) AS b").get(0);
            System.out.println("non-empty: " + Arrays.toString(r.getShortArray("a")));
            System.out.println("empty: " + Arrays.toString(r.getShortArray("b")));
        }
    }
}
Root cause

A review comment on #3343 found this (comment), and the reply was "yes, this need to be addressed, will fix". main does not have a fix.

The fix is one call in the empty-array branch of readArray(); it changes only UInt8, UInt16 and UInt32.

Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related
  • #3395: near cause, but non-empty Array(Nullable(String)) and getStringArray().
Configuration
Client Configuration
new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword("").build()
Environment
  • Cloud
  • Client version: main at 75063012b (0.12.0-rc1-SNAPSHOT), same code in 0.11.2. Not affected: 0.11.1.
  • Language version: OpenJDK 17.0.21
  • OS: Linux (Ubuntu container)
ClickHouse Server
  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: none (literal CAST expressions)
  • Sample data for all these tables: none

Classification: impact: none · area: Data types & formats (Array(UInt8), Array(UInt16), Array(UInt32))


Found by automated analysis (Polyglot AI) while working on #3395. Verified against a live server. Shortened on 2026-11-20: the edit history has the first version.

主要語言
Java
星號
1.6k
分支
657
平均合併
2 天 6 小時
30 天內合併 PR
36

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。