[client-v2] insert(table, columnNames, ...) does not backtick-quote column identifiers with spaces or special characters
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 client-v2/src/main/java/com/clickhouse/client/api/Client.java 的1846–1857行附近开始,这里会将列列表添加到INSERT语句中。检查其他位置如何引用标识符,并审查调用方已经传入带引号名称时的兼容性问题。为包含空格和内嵌反引号的名称添加或更新测试,然后确认生成的语句可与ClickHouse配合使用。
由索引模型根据 Issue 内容生成。
描述
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ClickHouse/clickhouse-java 的其他 Issue
-
api-diagnostics bug jdbc
难度 2/5 1-3 小时 新手友好度 75/100
ClickHouse/clickhouse-java#3464 ·
维护者通常 1 天内回复
-
tests
难度 2/5 1-3 小时 新手友好度 76/100
ClickHouse/clickhouse-java#3452 ·
维护者通常 1 天内回复
-
bug jdbc performance queries sql-parser
难度 2/5 1-3 小时 新手友好度 77/100
ClickHouse/clickhouse-java#3451 ·
维护者通常 1 天内回复
-
bug jdbc legacy queries
难度 2/5 1-3 小时 新手友好度 70/100
ClickHouse/clickhouse-java#3444 ·
维护者通常 1 天内回复
-
api-diagnostics bug client jdbc
难度 2/5 1-3 小时 新手友好度 76/100
ClickHouse/clickhouse-java#3439 · 1 条评论 ·
维护者通常 1 天内回复
查看 ClickHouse/clickhouse-java 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 60/100
quelea-projection/Quelea#749 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 92/100
erkamyaman/rust-anything#963 ·
维护者通常 1 天内回复
-
难度 2/5 半天 新手友好度 76/100
-
Deferred
难度 2/5 1-3 小时 新手友好度 78/100
Sanne/incus-spawn#2768 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复