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

Offen Anfängerfreundlich
#3,433 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
72/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
clickhouse, java
Bereich
database

Rechercherichtung

Beginne mit client-v2/src/main/java/com/clickhouse/client/api/Client.java, etwa in den Zeilen 1846–1857, wo die Spaltenliste zum INSERT-Statement hinzugefügt wird. Prüfe, wie Bezeichner an anderen Stellen in Anführungszeichen gesetzt werden, und untersuche die Kompatibilitätsfrage zu Aufrufern, die bereits Namen mit Anführungszeichen übergeben. Füge Tests für Namen mit Leerzeichen und eingebetteten Backticks hinzu oder aktualisiere sie. Bestätige anschließend, dass das generierte Statement mit ClickHouse funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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

Vorherrschende Sprache
Java
Sterne
1.6k
Forks
657
Ø Merge
2 T. 6 Std.
Gemergte PRs (30 T.)
36

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ClickHouse/clickhouse-java

Alle Issues in ClickHouse/clickhouse-java

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.