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

未關閉 適合新手
#3,433 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
clickhouse, java
領域
database

研究方向

從 client-v2/src/main/java/com/clickhouse/client/api/Client.java 的1846–1857行附近開始,這裡會將欄位清單加入INSERT陳述式。檢查其他地方如何引用識別子,並檢視呼叫端已傳入帶引號名稱時的相容性問題。為包含空格和內嵌反引號的名稱新增或更新測試,然後確認產生的陳述式可與ClickHouse搭配使用。

由索引模型根據 Issue 內容生成。

描述

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

主要語言
Java
星號
1.6k
分支
657
平均合併
2 天 6 小時
30 天內合併 PR
36

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。