[jdbc-v2] PreparedStatement setters throw ArrayIndexOutOfBoundsException, not SQLException, for an invalid parameter index

Đang mở
#3,462 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
84/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
java
Lĩnh vực
api, backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

api-diagnostics bug jdbc

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
  1. Prepare SELECT ? (one parameter), and call setInt(0, 1) or setInt(2, 1).
  2. Prepare INSERT INTO t_idx FORMAT CSV (no parameters), and call setString(1, "1,one").
  3. With beta.row_binary_for_simple_insert=true, prepare INSERT INTO t_idx (a, b) VALUES (?, ?), and call setInt(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

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: main at 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 TABLE statements 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.

Ngôn ngữ chính
Java
Star
1.6k
Fork
657
Merge trung bình
2 ngày 6 giờ
Pull request đã merge (30 ngày)
36

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ClickHouse/clickhouse-java

Tất cả issue của ClickHouse/clickhouse-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.