[client-v2] insert(table, columnNames, ...) does not backtick-quote column identifiers with spaces or special characters

Đang mở Phù hợp với người mới
#3,433 1 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
72/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ệ
clickhouse, java
Lĩnh vực
database

Hướng nghiên cứu

Bắt đầu từ client-v2/src/main/java/com/clickhouse/client/api/Client.java, khoảng dòng 1846–1857, nơi danh sách cột được thêm vào câu lệnh INSERT. Kiểm tra cách các định danh được đặt trong dấu nháy ở những nơi khác và xem xét vấn đề tương thích với các bên gọi vốn đã truyền tên có dấu nháy. Thêm hoặc cập nhật các bài kiểm thử cho tên chứa dấu cách và dấu backtick bên trong, sau đó xác nhận câu lệnh được tạo hoạt động với ClickHouse.

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

Mô tả

bug client queries

Description

Client.insert(String tableName, List<String> columnNames, ...) builds the INSERT INTO ... (cols) FORMAT ... statement by concatenating the caller-supplied column names verbatim, with no identifier quoting. Column names that require quoting in ClickHouse SQL (spaces, punctuation, etc.) produce an unparsable statement and the server rejects the insert with Code: 62 ... SYNTAX_ERROR.

client-v2/src/main/java/com/clickhouse/client/api/Client.java:1846-1857:

StringBuilder sqlStmt = new StringBuilder("INSERT INTO ").append(tableName);
if (columnNames != null && !columnNames.isEmpty()) {
    sqlStmt.append(" (");
    for (String columnName : columnNames) {
        sqlStmt.append(columnName).append(", ");
    }
    sqlStmt.deleteCharAt(sqlStmt.length() - 2);
    sqlStmt.append(")");
}
sqlStmt.append(" FORMAT ").append(format.name());

This affects all insert(...) overloads that take a List<String> columnNames (the InputStream overloads at Client.java:1720/1735 and the DataStreamWriter overloads at Client.java:1793/1810, which all funnel into the method above).

Inserts that do not pass columnNames are unaffected.

ClickHouse server version

26.11.2.4 (official build) — verified against a running server.

Reproduction

TestNG test (run in client-v2, pointed at a local server):

Client client = new Client.Builder()
        .addEndpoint("http://localhost:8123")
        .setUsername("default").setPassword("")
        .compressClientRequest(false)
        .build();

final String table = "scratch_quoting_table";
client.execute("DROP TABLE IF EXISTS " + table, new CommandSettings()).get(10, TimeUnit.SECONDS);
client.execute("CREATE TABLE " + table + " (`test id` UInt64, name String) ENGINE = Memory",
        new CommandSettings()).get(10, TimeUnit.SECONDS);

List<String> columnNames = Arrays.asList("test id", "name");
byte[] data = "1\tone\n".getBytes(StandardCharsets.UTF_8);

InsertResponse response = client.insert(table, columnNames, new ByteArrayInputStream(data),
        ClickHouseFormat.TSV, new InsertSettings()).get(30, TimeUnit.SECONDS);

Expected: the row is inserted (writtenRows == 1).

Actual: the statement sent is INSERT INTO scratch_quoting_table (test id, name) FORMAT TSV, and the call fails:

com.clickhouse.client.api.ServerException: Code: 62. DB::Exception: Syntax error: failed at position 41 (id)
(line 1, col 41): id, name ) FORMAT TSV 1 one . Expected one of: token sequence, Dot, token, Comma,
ClosingRoundBracket, end of query. (SYNTAX_ERROR) (version 26.11.2.4 (official build))

Workaround today: pre-quote the names, e.g. Arrays.asList("`test id`", "name").

Suggested fix

In Client.java:1846-1857, wrap each column name in backticks and double any embedded backtick before joining, e.g. "`" + name.replace("`", "") + "`" ``.

Note on compatibility: existing callers may already be pre-quoting as a workaround (the module's own InsertTests pre-quotes table names in a data provider), so the fix should either detect and pass through already-backticked input, or ship as a documented behavior change.

Link

Relayed from https://github.com/ClickHouse/clickhouse-js/issues/1210

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.