[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 摘要。