[jdbc-v1] PreparedStatement insert silently cuts a FixedString(N) value that is longer than N bytes

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

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

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

研究方向

此 issue 指出 clickhouse-data/src/main/java/com/clickhouse/data/format/BinaryDataProcessor.java 中的 FixedBytesSerDe.serialize,以及它在 ClickHouseRowBinaryProcessor 中的用法。閱讀引用行中的序列化器,並新增檢查以拒絕長度超過固定長度的值。確認重現的 insert 會報錯且不會儲存任何資料列,以驗證已完成。

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

描述

bug data-types jdbc legacy

Description

With the v1 JDBC driver (clickhouse.jdbc.v1=true), a PreparedStatement insert of a value that is longer than N bytes into a FixedString(N) column returns success and stores only the first N bytes. The rest of the value is lost, with no error. This occurs with setString and setBytes, for INSERT INTO t VALUES (?, ?) and INSERT INTO t, and for the column types FixedString(16), Nullable(FixedString(16)), LowCardinality(FixedString(16)) and Array(FixedString(16)). jdbc-v2 and client-v2 (POJO insert, RowBinaryFormatWriter) reject the same value with an error. The same defect was reported for clickhouse-go in https://github.com/ClickHouse/clickhouse-go/issues/2181.

Steps to reproduce
  1. Connect with clickhouse.jdbc.v1=true.
  2. CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id
  3. Prepare INSERT INTO t VALUES (?, ?), call setInt(1, 1) and setString(2, "this is NOT the correct length.") (31 bytes), then executeUpdate().
  4. SELECT x FROM t
Error Log or Exception StackTrace

No exception. Output of the program below (3 of 3 runs):

executeUpdate: 1
stored: [this is NOT the ]
Expected Behaviour

An error, and no row stored. The server rejects the same value in the text formats:

INSERT INTO t VALUES (1, 'this is NOT the correct length.')
Code: 131. DB::Exception: String too long for type FixedString(16): ... (TOO_LARGE_STRING_SIZE)
INSERT INTO t FORMAT TSV   -- row: 1<TAB>this is NOT the correct length.
Code: 131. DB::Exception: Too large value for FixedString(16): (at row 1) ... (TOO_LARGE_STRING_SIZE)

The same program without clickhouse.jdbc.v1=true (jdbc-v2) gets this server error, and a client-v2 POJO insert throws IllegalArgumentException: length of byte array value is 31, which should NOT longer than 16.

Code Example
Reproduction program (Java, 22 lines)
import java.sql.*;

public class Repro {
    public static void main(String[] args) throws Exception {
        // compress=0: the v1 driver cannot read compressed responses from server 26.9+ (#3107)
        String url = "jdbc:clickhouse://localhost:8123/default?clickhouse.jdbc.v1=true&compress=0";
        try (Connection c = DriverManager.getConnection(url, "default", "<password>");
             Statement st = c.createStatement()) {
            st.execute("DROP TABLE IF EXISTS t");
            st.execute("CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id");
            try (PreparedStatement ps = c.prepareStatement("INSERT INTO t VALUES (?, ?)")) {
                ps.setInt(1, 1);
                ps.setString(2, "this is NOT the correct length."); // 31 bytes
                System.out.println("executeUpdate: " + ps.executeUpdate());
            }
            try (ResultSet rs = st.executeQuery("SELECT x FROM t")) {
                rs.next();
                System.out.println("stored: [" + rs.getString(1) + "]");
            }
        }
    }
}
Root cause

The v1 RowBinary serializer of FixedString (FixedBytesSerDe.serialize, which FixedStringSerDe inherits) copies Math.min(bytes.length, length) bytes into a buffer of N bytes, so a longer value is cut and not rejected (BinaryDataProcessor.java:485-488). ClickHouseRowBinaryProcessor uses it for each FixedString column (ClickHouseRowBinaryProcessor.java:619-621).

The fix is a length check in FixedBytesSerDe.serialize. A v1 caller that relies on the cut would then get an error, as on jdbc-v2 and client-v2.

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

Related: #2223 asks for an opt-in cut in client-v2; it is a feature request, not this defect.

Configuration
Client Configuration
// URL parameters: clickhouse.jdbc.v1=true&compress=0
Environment
  • Cloud
  • Client version: main at 8480ca001 (0.12.0-rc1-SNAPSHOT); the serializer code is the same in v0.11.2
  • Language version: OpenJDK 17.0.21
  • OS: Linux
ClickHouse Server
  • ClickHouse Server version: 26.11.4.11
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: CREATE TABLE t (id UInt8, x FixedString(16)) ENGINE = MergeTree ORDER BY id
  • Sample data for all these tables: one row, the 31-byte value this is NOT the correct length.

Classification: impact: Data integrity · area: Data types & formats (FixedString)


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

主要語言
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 摘要。