[jdbc-v2] Statement.execute and prepareStatement throw RuntimeException, not SQLException, for two statements in one call
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ó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 75/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend, backend-api-design
Hướng nghiên cứu
Bắt đầu với JavaCCParser.parse trong jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SqlParserFacade.java, sau đó truy vết các điểm gọi được nêu trong issue trong StatementImpl.java và ConnectionImpl.java. Tìm các bài kiểm thử jdbc-v2 cho Statement và Connection.prepareStatement, rồi bổ sung phạm vi kiểm thử cho nhiều câu lệnh với trình phân tích JavaCC mặc định. Hoàn thành nghĩa là tất cả các phương thức được liệt kê đều ném SQLException thay vì RuntimeException, đồng thời hành vi hiện có đối với một câu lệnh đơn lẻ vẫn được giữ nguyên.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
With the default SQL parser (jdbc_sql_parser=JAVACC), jdbc-v2 throws java.lang.RuntimeException: More than one SQL statement found: <sql> when one call gets two statements separated by ;. The JDBC contract of these methods is throws SQLException, so a caller, a connection pool, or a framework that catches SQLException does not catch this exception. jdbc-v2 does not allow multi-statement SQL (docs/clickhouse-docs/jdbc.mdx, "Migration Guide": "Not allowed"), so the call must fail, but with an SQLException.
Affected: Statement.execute, executeUpdate, executeLargeUpdate, executeBatch (an entry added with addBatch(String)), and Connection.prepareStatement. Not affected: Statement.executeQuery, and all calls with jdbc_sql_parser=ANTLR4 or ANTLR4_PARAMS_PARSER. These send the text to the server and throw the server error as SQLException (code 62). One statement with a trailing ; (select 1;, select 1;;, select 1; -- comment) works with all three parsers.
Steps to reproduce
- Open a jdbc-v2 connection with the default properties.
- Call
createStatement().execute("drop table if exists t_multi; create table t_multi(a Int32) engine=Memory"), orprepareStatement(...)with the same text. - Look at the type of the exception.
Error Log or Exception StackTrace
Output of the code example below:
execute: not an SQLException: java.lang.RuntimeException: More than one SQL statement found: drop table if exists t_multi; create table t_multi(a Int32) engine=Memory
prepareStatement: not an SQLException: java.lang.RuntimeException: More than one SQL statement found: drop table if exists t_multi; create table t_multi(a Int32) engine=Memory
executeQuery (contrast): SQLException Code: 62. DB::Exception: Syntax error (Multi-statements are not allowed): failed at position 9 (end of query): ; select 2. . (SYNTAX_ERROR) (version 26.11.4.11 (official build)) (queryId= ...)
Expected Behaviour
All the affected calls throw java.sql.SQLException, as executeQuery and the ANTLR4 parsers do. The server rejects the same text: curl --data-binary 'drop table if exists t_multi; create table t_multi(a Int32, b String) engine=Memory' http://localhost:8123/ returns Code: 62. DB::Exception: Syntax error (Multi-statements are not allowed).
Root cause. JavaCCParser.parse (jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SqlParserFacade.java:236) throws new RuntimeException("More than one SQL statement found: " + sql). StatementImpl.execute (StatementImpl.java:563), StatementImpl.executeLargeUpdate (StatementImpl.java:845, also called by executeUpdate and executeBatch), and ConnectionImpl.prepareStatement (ConnectionImpl.java:458) call the parser with no try/catch.
A maintainer decides which error the driver gives: an SQLException that the driver makes (and its SQL state), or the server error (code 62), as with the ANTLR4 parsers.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Code Example
Reproduction program (Java, 30 lines)
import java.sql.*;
import java.util.Properties;
public class Repro {
interface Call { void run(Connection c) throws Exception; }
public static void main(String[] args) throws Exception {
String url = "jdbc:clickhouse:" + System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123").replaceFirst("^https?:", "") + "/default";
Properties props = new Properties();
props.setProperty("user", "default");
props.setProperty("password", System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", ""));
String sql = "drop table if exists t_multi; create table t_multi(a Int32) engine=Memory";
try (Connection c = DriverManager.getConnection(url, props)) {
run("execute", c, x -> x.createStatement().execute(sql));
run("prepareStatement", c, x -> x.prepareStatement(sql));
run("executeQuery (contrast)", c, x -> x.createStatement().executeQuery("select 1; select 2"));
}
}
static void run(String label, Connection c, Call call) {
try {
call.run(c);
System.out.println(label + ": no exception");
} catch (SQLException e) {
System.out.println(label + ": SQLException " + e.getMessage().split("\n")[0]);
} catch (Exception e) {
System.out.println(label + ": not an SQLException: " + e);
}
}
}
Configuration
Client Configuration
// default properties (jdbc_sql_parser=JAVACC)
Environment
- Cloud
- Client version: artifact
clickhouse-jdbc(jdbc-v2) frommain(8480ca001, 0.12.0-rc1-SNAPSHOT). v0.11.2 has the same code. - Language version: JDK 17
- OS: Linux (Docker)
ClickHouse Server
- ClickHouse Server version: 26.11.4.11
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved: in the code example- Sample data for all these tables: not needed
Classification: impact: none · area: API & diagnostics (exception type)
Found by automated analysis (Polyglot AI) while working on #1477. 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
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ClickHouse/clickhouse-java
-
tests
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ClickHouse/clickhouse-java#3452 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug jdbc performance queries sql-parser
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
ClickHouse/clickhouse-java#3451 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug jdbc legacy queries
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
ClickHouse/clickhouse-java#3444 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
api-diagnostics bug client jdbc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ClickHouse/clickhouse-java#3439 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug client jdbc legacy queries
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ClickHouse/clickhouse-java#3438 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của ClickHouse/clickhouse-java
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
quelea-projection/Quelea#749 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
free_port can hand one port to two callers, so the fixed-port http tests fail in parallel runsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 92/100
erkamyaman/rust-anything#963 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 76/100
-
Deferred
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Sanne/incus-spawn#2768 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
morluto/rea#15041 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày