Client::Insert: a column name with a backslash fails or loses the value (the backslash is not escaped in the column list)
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
- 79/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- cpp
- Bereich
- backend-api-design
Rechercherichtung
Das Issue nennt NameToQueryString in clickhouse/client.cpp als Ursache. Lies zuerst diese Funktion und die bestehenden Tests für Spaltennamen in Client::Insert(). Ergänze Tests für Namen mit Backslashes, einschließlich eines abschließenden Backslashes. Überprüfe dann, dass Inserts die vorgesehenen Spaltenwerte beibehalten, und führe den relevanten Test aus. Fertig ist es, wenn Backslashes in der generierten Spaltenliste escaped werden, ohne andere Bezeichnernamen zu beeinträchtigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
Client::Insert() fails, or loses the value with no error, when a column name of the block contains a backslash (\). The same defect was reported for clickhouse-go in https://github.com/ClickHouse/clickhouse-go/issues/2212.
- Column
a\bora\tb: the server reads\bor\tin the INSERT column list as an escape sequence. The statement names a column that the table does not have, andInsert()throwsNo such column. - Column
end\ora\`b: the backslash escapes the next backtick, and the statement does not parse (Syntax error). - The table also has the column with the decoded name (for example
a\tbanda<TAB>b):Insert()returns with no error and writes one row, but the value is not stored. Both columns have their default value.
Column names with a space, a backtick, or an unknown escape (a\qb) insert correctly. BeginInsert() with the caller's own SQL is not affected.
Steps to reproduce
- Start a ClickHouse server.
- Run the program. For each name, it creates a
Memorytable with that column and a columnplain, and inserts one row withClient::Insert(). Then it inserts 42 intoa\tbof a table that also has the columna<TAB>b, and reads the row.
Reproduction program (C++, 40 lines)
#include <clickhouse/client.h>
#include <iostream>
using namespace clickhouse;
// Shows a backspace or a tab character of a server message as <BS> or <TAB>.
std::string Show(std::string s) {
for (auto [c, m] : {std::pair{'\b', "<BS>"}, {'\t', "<TAB>"}})
for (size_t p; (p = s.find(c)) != std::string::npos;) s.replace(p, 1, m);
return s;
}
int main() {
const auto opts = ClientOptions().SetHost("localhost");
// Server column names: test id, a`b, a\qb, a\b, end\, a\tb, a\`b.
const char* ddl[] = {"`test id`", "`a\\`b`", "`a\\\\qb`", "`a\\\\b`", "`end\\\\`", "`a\\\\tb`", "`a\\\\\\`b`"};
const char* names[] = {"test id", "a`b", "a\\qb", "a\\b", "end\\", "a\\tb", "a\\`b"};
for (int i = 0; i < 7; ++i) {
Client client(opts); // a new client: a failed Insert() leaves the client in the insert state
client.Execute(std::string("CREATE OR REPLACE TABLE t (") + ddl[i] + " UInt64, plain UInt64) ENGINE = Memory");
Block block;
auto v = std::make_shared<ColumnUInt64>(); v->Append(42);
auto p = std::make_shared<ColumnUInt64>(); p->Append(1);
block.AppendColumn(names[i], v);
block.AppendColumn("plain", p);
try { client.Insert("t", block); std::cout << names[i] << " -> ok\n"; }
catch (const std::exception& e) { std::cout << names[i] << " -> " << Show(e.what()) << "\n"; }
}
// Two columns: a\tb (backslash, t) and a<TAB>b. Insert 42 into a\tb.
Client client(opts);
client.Execute("CREATE OR REPLACE TABLE t (`a\\\\tb` UInt64, `a\\tb` UInt64) ENGINE = Memory");
Block block;
auto v = std::make_shared<ColumnUInt64>(); v->Append(42);
block.AppendColumn("a\\tb", v);
client.Insert("t", block);
client.Select("SELECT `a\\\\tb`, `a\\tb` FROM t", [](const Block& b) {
for (size_t r = 0; r < b.GetRowCount(); ++r)
std::cout << "two columns -> ok, stored: a\\tb = " << b[0]->As<ColumnUInt64>()->At(r)
<< ", a<TAB>b = " << b[1]->As<ColumnUInt64>()->At(r) << "\n";
});
}
Output:
test id -> ok
a`b -> ok
a\qb -> ok
a\b -> DB::Exception: No such column a<BS> in table default.t (66961d91-abc0-4498-9a3d-82f6320bcebd)
end\ -> DB::Exception: Syntax error: failed at position 25 (plain): plain` ) VALUES. Unmatched parentheses: '(' at position 15 is never closed.
a\tb -> DB::Exception: No such column a<TAB>b in table default.t (05c65a14-a170-4cc4-9921-b8c11613c6ec)
a\`b -> DB::Exception: Syntax error: failed at position 22 (b): b`,`plain` ) VALUES. Unmatched parentheses: '(' at position 15 is never closed.
two columns -> ok, stored: a\tb = 0, a<TAB>b = 0
Expected behavior
Each insert stores 42 in the column that the block names. The server reads a backquoted identifier with the escapes of a string literal (Syntax, "Identifiers"), and writes a backslash in an identifier as \\. SHOW CREATE TABLE prints:
`a\\b` UInt64,
`end\\` UInt64,
`x\`y` UInt64
INSERT INTO t ( `a\\b`,`end\\` ) VALUES (1, 2) inserts the row. INSERT INTO t ( `a\b` ) VALUES (1), the form that the client sends, fails with No such column a.
Root cause
NameToQueryString (clickhouse/client.cpp:468) doubles each backtick of the column name, but writes a backslash as it is.
Workaround: double each backslash in the Block column name, or use BeginInsert() with your own SQL.
The fix is in NameToQueryString only. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- #560:
Insert()does not quote the table name, the other identifier of the same statement.
Environment
- clickhouse-cpp
masterat 6049285, clang 18, Linux - ClickHouse server 26.11.7.10
Classification: impact: Data integrity · area: Queries (INSERT column list quoting)
Found by automated analysis (Polyglot AI) while checking clickhouse-go#2212 against the other client repos. Verified against a live server.
- Vorherrschende Sprache
- C
- Sterne
- 388
- Forks
- 220
- Ø Merge
- 1 T. 25 Min.
- Gemergte PRs (30 T.)
- 16
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus ClickHouse/clickhouse-cpp
-
bug data-types
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
ClickHouse/clickhouse-cpp#645 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug data-types native-format
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
ClickHouse/clickhouse-cpp#643 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug connectivity native-protocol
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
ClickHouse/clickhouse-cpp#638 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Core SQL test harness: ParseSetAssignments does not skip comments inside SETEvtl. vergeben @puretechteam hat das vor 12 Tagen übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
ClickHouse/clickhouse-cpp#626 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Enum type parsing: duplicate ordinals (Enum8('a' = 1, 'b' = 1)) and duplicate labels (Enum8('a' = 1, 'a' = 2)) are silently accepted and lose a memberEvtl. vergeben @SBNREX hat das vor 19 Tagen übernommen. Offenbug data-types native-format
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
ClickHouse/clickhouse-cpp#607 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in ClickHouse/clickhouse-cpp
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
google-coral/coralnpu#175 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
Maintainer antworten meist innerhalb von 1 Tag