jdbc-v2: ANTLR4 parsers do not accept a heredoc as an ON CLUSTER name or a column comment

Đang mở Phù hợp với người mới
#3,393 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ó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
83/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

Hướng nghiên cứu

Bắt đầu với jdbc-v2/src/main/antlr4/com/clickhouse/jdbc/internal/parser/antlr4/ClickHouseParser.g4, tập trung vào clusterClause và các quy tắc chú thích cột được nêu trong issue. Đọc quy tắc literal hiện có và các bài kiểm thử parser, sau đó thêm các trường hợp tên cluster và chú thích dạng heredoc, đồng thời giữ lại các dạng có dấu ngoặc kép. Hoàn thành khi các parser ANTLR4 trả về kết quả cập nhật như mong đợi cho những trường hợp này; issue xác định BaseSqlParserFacadeTest là điểm truy cập kiểm thử dùng chung.

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

Mô tả

bug jdbc queries sql-parser

Description

With jdbc_sql_parser=ANTLR4 or ANTLR4_PARAMS_PARSER, jdbc-v2 does not accept a heredoc ($$...$$, $tag$...$tag$) in two positions where the server accepts it:

  • the cluster name in ON CLUSTER
  • the column comment (COMMENT in a column definition, and ALTER TABLE ... COMMENT COLUMN)

In a statement with ON CLUSTER, the driver then returns the server's distributed-DDL status table as a ResultSet. Statement.execute() and PreparedStatement.execute() return true, getUpdateCount() returns -1, and getResultSet() returns 1 status row (host, port, status, error, num_hosts_remaining, num_hosts_active). With ON CLUSTER default or ON CLUSTER 'default', the same statement returns false and 0.

Seen with $$default$$ and $tag$default$tag$, with both parsers, for CREATE TABLE, ALTER TABLE ... ADD COLUMN, ALTER TABLE ... UPDATE, ALTER TABLE ... DELETE, OPTIMIZE TABLE ... FINAL, RENAME TABLE, TRUNCATE TABLE and DROP TABLE. A heredoc column comment with ON CLUSTER 'default' has the same effect: ALTER TABLE t ON CLUSTER 'default' COMMENT COLUMN a $$x$$, and CREATE TABLE t ON CLUSTER 'default' (a Int COMMENT $$x$$) ....

PreparedStatement.getMetaData() before execution sends DESC (ALTER TABLE ... ON CLUSTER $$default$$ ...). The server rejects it (Code: 62), and the driver logs a WARN with a stack trace. With the quoted form, no request is sent.

Without ON CLUSTER, the parse error also occurs, but execute() returns false and 0, because the server sends no result. The server runs all of these statements correctly in both forms.

Steps to reproduce
  1. Connect with jdbc-v2 and jdbc_sql_parser=ANTLR4 (or ANTLR4_PARAMS_PARSER) to a server that has a cluster named default.
  2. Run stmt.execute("CREATE TABLE t ON CLUSTER $$default$$ (a Int) ENGINE = MergeTree ORDER BY a").
  3. Compare with stmt.execute("CREATE TABLE t2 ON CLUSTER 'default' (a Int) ENGINE = MergeTree ORDER BY a").
Error Log or Exception StackTrace

execute() logs no error (the parse error is at DEBUG only). PreparedStatement.getMetaData() logs:

WARN PreparedStatementImpl - Failed to resolve result-set metadata for prepared statement; falling back to untyped metadata
com.clickhouse.client.api.ServerException: Code: 62. DB::Exception: Syntax error: failed at position 13 (TABLE): TABLE p3 ON CLUSTER $$default$$ UPDATE b = NULL WHERE 1). ...
Expected Behaviour

A heredoc cluster name or column comment gives the same result as the quoted form: execute() returns false with update count 0, and getMetaData() sends no DESC request.

Code Example
Properties props = new Properties();
props.setProperty("user", "default");
props.setProperty("password", "...");
props.setProperty("jdbc_sql_parser", "ANTLR4"); // or ANTLR4_PARAMS_PARSER

try (Connection conn = DriverManager.getConnection("jdbc:clickhouse://localhost:8123/default", props);
     Statement stmt = conn.createStatement()) {
    System.out.println(stmt.execute("CREATE TABLE t1 ON CLUSTER 'default' (a Int) ENGINE = MergeTree ORDER BY a"));   // false
    System.out.println(stmt.getUpdateCount());                                                                       // 0
    System.out.println(stmt.execute("CREATE TABLE t2 ON CLUSTER $$default$$ (a Int) ENGINE = MergeTree ORDER BY a")); // true
    System.out.println(stmt.getUpdateCount());                                                                       // -1
}
Root cause
  • jdbc-v2/src/main/antlr4/com/clickhouse/jdbc/internal/parser/antlr4/ClickHouseParser.g4:279 is clusterClause : ON CLUSTER (identifier | STRING_LITERAL), and the column comment rules (lines 73, 332 and 335) also name STRING_LITERAL. The lexer token HEREDOC_LITERAL (since #3031) is accepted only by literal (lines 1203-1208).
  • After a parse error, parsedStatement() and parsePreparedStatement() set hasResultSet=true (SqlParserFacade.java:254-256, 264-265). So execute() (StatementImpl.java:565, PreparedStatementImpl.java:309) uses the query path, and getMetaData() (PreparedStatementImpl.java:423) sends DESC (...).

The fix is in ClickHouseParser.g4 only: no listener reads the cluster name or the comment.

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

Related
  • #3391: same defect in the default JAVACC parser (fix in #3392).
Configuration
Client Configuration
// jdbc_sql_parser = ANTLR4 or ANTLR4_PARAMS_PARSER; other properties default
Environment
  • Cloud
  • Client version: jdbc-v2 main at 75063012b (0.12.0-rc1-SNAPSHOT)
  • Language version: Java 17.0.21
  • OS: Linux (Docker)
ClickHouse Server
  • ClickHouse Server version: 26.10.4.207
  • ClickHouse Server non-default settings, if any: one-node cluster default with Keeper (for ON CLUSTER)
  • CREATE TABLE statements for tables involved: in the examples above
  • Sample data for all these tables: not necessary

Classification: impact: none · area: Queries (ANTLR4 SQL parser)


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

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.