[client-v1] External tables: column names are not quoted in the structure parameter
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- java
調査の方向性
getStructure() が列リストを組み立てる clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseExternalTable.java の216~224行付近を読みます。各列名をバッククォートで囲んだ識別子にし、名前に含まれるバックスラッシュとバッククォートをエスケープします。空白、カンマ、括弧、バッククォート、型のようなテキストを含む名前のテストを追加します。各名前から有効な構造が生成され、テーブルの列数が変わらないことを完了条件とします。
索引モデルが issue の本文から書いたものです。
説明
Description
ClickHouseExternalTable builds the <table>_structure value by concatenating the raw column name and the type, with no backtick quoting (clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseExternalTable.java:216-224):
for (ClickHouseColumn c : columns) {
list.add(c);
builder.append(c.getColumnName()).append(' ').append(c.getOriginalTypeName()).append(',');
}
That string is written verbatim into the multipart <table>_structure part by ClickHouseHttpConnection.postData() (clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java:374-375). The server parses the value as a column list, so any name that is not a bare identifier breaks the query:
- a name with a space, a comma, parentheses, a backtick, or a non-ASCII letter gives
Code: 36 ... Error while parsing table structure: Syntax error; - a name that contains a type declaration (
x UInt8, y) changes the number of columns of the external table, so the data no longer matches the structure (Code: 27), or silently reads different columns when the data happens to parse.
The server accepts these names when they are backtick-quoted, so today the caller has to add the backticks:
$ printf '42\n' > ext.tsv
$ curl -F '[email protected]' 'http://localhost:8123/?query=SELECT+*+FROM+ext+FORMAT+TSVWithNames&ext_format=TSV&ext_structure=%60test%20id%60%20UInt64'
test id
42
ClickHouseExternalTable.getStructure() is also used by other callers (for example the clickhouse-jdbc external-data path), so the escaping belongs in getStructure() and not in the HTTP connection.
ClickHouse server version
26.11.2.4 (local server, anonymous access). Verified against this running server.
Reproduction
Scratch test in clickhouse-http-client (TestNG), run with mvn -Dj8 -pl clickhouse-http-client -am test -Dtest=ExtTableQuotingTest:
package com.clickhouse.client.http;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseProtocol;
import com.clickhouse.client.ClickHouseResponse;
import com.clickhouse.data.ClickHouseExternalTable;
import com.clickhouse.data.ClickHouseFormat;
import com.clickhouse.data.ClickHouseRecord;
import org.testng.annotations.Test;
public class ExtTableQuotingTest {
private void run(String colName) {
ClickHouseNode server = ClickHouseNode.builder().host("localhost").port(ClickHouseProtocol.HTTP, 8123).build();
ClickHouseExternalTable ext = ClickHouseExternalTable.builder().name("ext")
.addColumn(colName, "UInt64")
.format(ClickHouseFormat.TabSeparated)
.content(new ByteArrayInputStream("42\n".getBytes(StandardCharsets.UTF_8))).build();
System.out.println("structure=[" + ext.getStructure() + "]");
try (ClickHouseClient client = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP);
ClickHouseResponse resp = client.read(server).compressServerResponse(false)
.query("SELECT * FROM ext").external(ext).executeAndWait()) {
StringBuilder sb = new StringBuilder();
for (ClickHouseRecord r : resp.records()) {
for (int i = 0; i < r.size(); i++) {
sb.append(r.getValue(i).asString()).append(' ');
}
}
System.out.println("RESULT name=[" + colName + "] -> " + sb);
} catch (Exception e) {
System.out.println("RESULT name=[" + colName + "] -> ERROR: " + e.getMessage());
}
}
@Test(groups = { "unit" })
public void testNames() {
run("plain");
run("test id");
run("a,b");
run("`test id`");
run("x UInt8, y");
}
}
Actual output:
structure=[plain UInt64]
RESULT name=[plain] -> 42
structure=[test id UInt64]
RESULT name=[test id] -> ERROR: Code: 36. DB::Exception: Error while parsing table structure: Syntax error: failed at position 9 (UInt64): UInt64. Expected one of: token, Comma, end of query. (BAD_ARGUMENTS) (version 26.11.2.4 (official build))
structure=[a,b UInt64]
RESULT name=[a,b] -> ERROR: Code: 36. DB::Exception: Error while parsing table structure: Syntax error: failed at position 2 (,): ,b UInt64. Expected one of: data type, identifier. (BAD_ARGUMENTS) (version 26.11.2.4 (official build))
structure=[`test id` UInt64]
RESULT name=[`test id`] -> 42
structure=[x UInt8, y UInt64]
RESULT name=[x UInt8, y] -> ERROR: Code: 27. DB::Exception: Cannot parse input: expected '\t' before: '\n': (at row 1)
Expected output: every name gives the single value 42, the same as the already backtick-quoted name gives today. The caller must not have to add the backticks, and a name that contains , or a type keyword must not change the shape of the external table.
Suggested fix
In ClickHouseExternalTable (clickhouse-data/src/main/java/com/clickhouse/data/ClickHouseExternalTable.java, around line 221), write the name as a backtick-quoted identifier (escaping \ and ` inside the name) instead of the raw string.
Note: client-v2 has no external-table API, so only the v1 stack (clickhouse-data + clickhouse-http-client, and clickhouse-jdbc on top of it) is affected.
Link
Found while checking whether ClickHouse/clickhouse-go#2170 ("HTTP external tables: column names are not quoted in the structure parameter") also applies here. It does.
- 主要言語
- Java
- スター
- 1.6k
- フォーク
- 657
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 36
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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
morluto/rea#15041 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信