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

Aperta Adatta ai principianti
#3,433 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
clickhouse, java
Ambito
database

Direzione di ricerca

Inizia da client-v2/src/main/java/com/clickhouse/client/api/Client.java, intorno alle righe 1846–1857, dove l’elenco delle colonne viene aggiunto all’istruzione INSERT. Controlla come vengono racchiusi tra virgolette gli identificatori altrove ed esamina il problema di compatibilità relativo ai chiamanti che passano già nomi tra virgolette. Aggiungi o aggiorna test per i nomi contenenti spazi e backtick incorporati, quindi conferma che l’istruzione generata funziona con ClickHouse.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Java
Stelle
1.6k
Fork
657
Merge medio
2g 6h
PR unite (30g)
36

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ClickHouse/clickhouse-java

Tutte le issue di ClickHouse/clickhouse-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.