[jdbc-v2] PreparedStatement setters throw ArrayIndexOutOfBoundsException, not SQLException, for an invalid parameter index
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
Start with jdbc-v2/src/main/java/com/clickhouse/jdbc/PreparedStatementImpl.java and the existing checkParamIndex logic in jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/ParameterMetaDataImpl.java. Then follow the RowBinary path through jdbc-v2/src/main/java/com/clickhouse/jdbc/WriterStatementImpl.java and client-v2/src/main/java/com/clickhouse/client/api/data_formats/RowBinaryFormatWriter.java. Add JDBC v2 tests covering zero, negative, and out-of-range parameter indexes in both the standard and row-binary writer paths, and confirm each invalid setter throws SQLException.
由索引模型根據 Issue 內容生成。
描述
Description
On jdbc-v2, a PreparedStatement setter with a parameter index that has no ? marker throws java.lang.ArrayIndexOutOfBoundsException, not SQLException. This occurs for index 0, for a negative index, for an index above the parameter count, and for any index on a statement with no parameters (for example INSERT INTO t FORMAT CSV). The setters of PreparedStatementImpl and of the RowBinary writer (WriterStatementImpl, beta.row_binary_for_simple_insert=true) have this behavior. Code that catches SQLException does not catch the exception.
Steps to reproduce
- Prepare
SELECT ?(one parameter), and callsetInt(0, 1)orsetInt(2, 1). - Prepare
INSERT INTO t_idx FORMAT CSV(no parameters), and callsetString(1, "1,one"). - With
beta.row_binary_for_simple_insert=true, prepareINSERT INTO t_idx (a, b) VALUES (?, ?), and callsetInt(3, 1).
Error Log or Exception StackTrace
Output of the program below:
0 params: setString(1) -> java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
1 param: setInt(0) -> java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 1
1 param: setInt(2) -> java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
1 param: getParameterMetaData().getParameterType(2) -> java.sql.SQLException: Parameter index out of range. [1,1]
RowBinary writer, 2 params: setInt(3) -> java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
Top frames of setInt(2) and of the writer's setInt(3):
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at com.clickhouse.jdbc.PreparedStatementImpl.setInt(PreparedStatementImpl.java:186)
java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at com.clickhouse.client.api.data_formats.RowBinaryFormatWriter.setValue(RowBinaryFormatWriter.java:82)
at com.clickhouse.client.api.data_formats.RowBinaryFormatWriter.setInteger(RowBinaryFormatWriter.java:133)
at com.clickhouse.jdbc.WriterStatementImpl.setInt(WriterStatementImpl.java:171)
Expected Behaviour
SQLException. The JDBC API specifies it for each PreparedStatement.setXXX method: "SQLException - if parameterIndex does not correspond to a parameter marker in the SQL statement". The driver already does this in ParameterMetaData (the fourth output line).
Code Example
Reproduction program (Java, 36 lines)
import java.sql.*;
import java.util.Properties;
public class Repro {
interface Call { void run(PreparedStatement ps) throws SQLException; }
static void call(PreparedStatement ps, String label, Call c) {
try { c.run(ps); System.out.println(label + " -> no exception"); }
catch (Exception e) { System.out.println(label + " -> " + e.getClass().getName() + ": " + e.getMessage()); }
}
public static void main(String[] args) throws Exception {
com.clickhouse.jdbc.Driver.load(); // jdbc-v2, the default driver of clickhouse-jdbc
String url = System.getenv().getOrDefault("CLICKHOUSE_URL", "jdbc:clickhouse://localhost:8123/default");
Properties p = new Properties();
p.setProperty("user", "default");
p.setProperty("password", System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", ""));
try (Connection c = DriverManager.getConnection(url, p); Statement s = c.createStatement()) {
s.execute("CREATE TABLE IF NOT EXISTS t_idx (a Int32, b String) ENGINE = Memory");
try (PreparedStatement ps = c.prepareStatement("INSERT INTO t_idx FORMAT CSV")) {
call(ps, "0 params: setString(1)", x -> x.setString(1, "1,one"));
}
try (PreparedStatement ps = c.prepareStatement("SELECT ?")) {
call(ps, "1 param: setInt(0)", x -> x.setInt(0, 1));
call(ps, "1 param: setInt(2)", x -> x.setInt(2, 1));
call(ps, "1 param: getParameterMetaData().getParameterType(2)",
x -> x.getParameterMetaData().getParameterType(2));
}
p.setProperty("beta.row_binary_for_simple_insert", "true");
try (Connection cw = DriverManager.getConnection(url, p);
PreparedStatement ps = cw.prepareStatement("INSERT INTO t_idx (a, b) VALUES (?, ?)")) {
call(ps, "RowBinary writer, 2 params: setInt(3)", x -> x.setInt(3, 1));
}
}
}
}
Root cause
PreparedStatementImpl: each setter writesvalues[parameterIndex - 1]with no range check, for examplesetIntat PreparedStatementImpl.java:186 andsetObjectat PreparedStatementImpl.java:302.WriterStatementImplgives the index toRowBinaryFormatWriter.setValue, which writesrow[colIndex - 1]with no range check (RowBinaryFormatWriter.java:82).
ParameterMetaDataImpl.checkParamIndex (ParameterMetaDataImpl.java:31-35) already has the check that the setters need.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: #2609 and #3052 (fixed parser bugs that counted 0 parameters) reached users as this exception.
Configuration
Client Configuration
// Default properties. The writer case adds beta.row_binary_for_simple_insert=true.
Environment
- Cloud
- Client version:
mainat 8480ca001 (0.12.0-rc1-SNAPSHOT); the setter 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 TABLEstatements for tables involved: in the program (t_idx)- Sample data for all these tables: none
Classification: impact: none · area: API & diagnostics (PreparedStatement setters)
Found by automated analysis (Polyglot AI) while working on #1977. Verified against a live server.
- 主要語言
- Java
- 星號
- 1.6k
- 分支
- 657
- 平均合併
- 2 天 6 小時
- 30 天內合併 PR
- 36
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
ClickHouse/clickhouse-java 的其他 Issue
-
api-diagnostics bug jdbc
難度 2/5 1-3 小時 新手友好度 75/100
ClickHouse/clickhouse-java#3464 ·
維護者通常 1 天內回覆
-
tests
難度 2/5 1-3 小時 新手友好度 76/100
ClickHouse/clickhouse-java#3452 ·
維護者通常 1 天內回覆
-
bug jdbc performance queries sql-parser
難度 2/5 1-3 小時 新手友好度 77/100
ClickHouse/clickhouse-java#3451 ·
維護者通常 1 天內回覆
-
bug jdbc legacy queries
難度 2/5 1-3 小時 新手友好度 70/100
ClickHouse/clickhouse-java#3444 ·
維護者通常 1 天內回覆
-
api-diagnostics bug client jdbc
難度 2/5 1-3 小時 新手友好度 76/100
ClickHouse/clickhouse-java#3439 · 1 則留言 ·
維護者通常 1 天內回覆
查看 ClickHouse/clickhouse-java 的全部 Issue
相似的 Issue
-
[BUG] Nightly Build JDK 17: latest-frameworks job red since 2026-11-28 — quarkiverse-langchain4j 2.0.0 pins Quarkus 4.0.0, which requires Java 21可能已有人在做 @uhhgxg 今天認領。 未關閉
難度 1/5 1 小時以內 新手友好度 95/100
langchain4j/langchain4j#7344 ·
維護者通常 1 天內回覆
-
documentation good first issue
難度 2/5 1-3 小時 新手友好度 75/100
cristianEncarnacion/ferroreport#149 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 60/100
quelea-projection/Quelea#749 ·
維護者通常 1 天內回覆
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runs可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 92/100
erkamyaman/rust-anything#963 ·
維護者通常 1 天內回覆
-
難度 2/5 半天 新手友好度 76/100