Enum type parsing: duplicate ordinals (Enum8('a' = 1, 'b' = 1)) and duplicate labels (Enum8('a' = 1, 'a' = 2)) are silently accepted and lose a member
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 75/100
Rechercherichtung
Der Fix gehört in den Konstruktor EnumType::EnumType in clickhouse/types/types.cpp (etwa Zeilen 357-362), wo name_to_value_ und value_to_name_ aus der Item-Liste gefüllt werden. Prüfe, ob insert ein bereits vorhandenes Label meldet und ob value_to_name_ bereits den Ordinalwert enthält, und wirf dann einen Validierungsfehler, der den Typ nennt. Fertig ist es, wenn CreateColumnByType sowohl Enum8('a' = 1, 'b' = 1) als auch Enum8('a' = 1, 'a' = 2) ablehnt und bestehende Enum-Tests weiterhin bestehen. clickhouse/columns/factory.cpp (Zeilen 205-218) zeigt, wie die Items in diesen Pfad gelangen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
CreateColumnByType() / Type::CreateEnum8() accept Enum type names that the server rejects:
- Duplicate ordinal —
Enum8('a' = 1, 'b' = 1). Server:Code: 62 ... Duplicate values in enum: 'b' = 1 and 'a' (SYNTAX_ERROR). - Duplicate label —
Enum8('a' = 1, 'a' = 2). Server:Code: 62 ... Duplicate names in enum: 'a' = 1 and 2 (SYNTAX_ERROR).
Neither is rejected by the client, and both produce an EnumType whose two lookup maps disagree with each other and with the type name the application wrote:
clickhouse/types/types.cpp:357-362
EnumType::EnumType(Type::Code type, const std::vector<EnumItem>& items) : Type(type) {
for (const auto& item : items) {
auto result = name_to_value_.insert(item);
value_to_name_[item.second] = result.first->first;
}
}
- Duplicate ordinal:
name_to_value_keeps both labels pointing at the same ordinal, whilevalue_to_name_[ordinal]is overwritten by the last label. The first label is lost — it disappears fromGetName(), fromNameAt(), and from iteration viaBeginValueToName()/EndValueToName().Enum8('a' = 1, 'b' = 1)renders asEnum8('b' = 1). - Duplicate label:
std::map::insertdoes not overwrite, soname_to_value_keeps only the first ordinal, whilevalue_to_name_gains an entry for each ordinal.GetName()reproduces the invalid two-member name,HasEnumValue(2)istrue, butGetEnumValue("a")is1.
clickhouse/columns/factory.cpp:205-218 builds the EnumItem vector from the AST without any check either, so every type string that reaches CreateColumnByType() goes through this path.
Types coming from the server over the Native protocol always have distinct ordinals and labels, so this is reachable only through type names the application supplies (CreateColumnByType(), Type::CreateEnum8()/CreateEnum16(), hand-built Block columns for INSERT) — but on that path it is silent data corruption, not just a missing diagnostic: with a duplicate ordinal, two different labels appended by name are written as the same ordinal, and if the collapsed type name happens to match the destination column, the server accepts the INSERT and stores the wrong label.
The equivalent check already exists in some other clients (e.g. the .NET TCP client throws FormatException: ... declares the ordinal {n} more than once / ... declares the label '{label}' more than once).
ClickHouse server version
26.10.3.7 (official build), used for the end-to-end part of the reproduction below.
Reproduction
Built against the in-tree static lib (g++ -std=c++17 -I. -Icontrib repro.cpp build/clickhouse/libclickhouse-cpp-lib.a build/clickhouse/cityhash/libcityhash.a build/contrib/lz4/lz4/liblz4.a build/contrib/zstd/zstd/libzstdstatic.a -lssl -lcrypto):
#include <clickhouse/columns/factory.h>
#include <clickhouse/columns/enum.h>
#include <clickhouse/client.h>
#include <iostream>
using namespace clickhouse;
int main() {
// 1) duplicate ordinal
auto col = CreateColumnByType("Enum8('a' = 1, 'b' = 1)"); // server rejects this type
std::cout << "accepted, GetName() = " << col->Type()->GetName() << "\n";
auto e = col->As<ColumnEnum8>();
e->Append(std::string("a"));
e->Append(std::string("b"));
std::cout << " 'a' -> " << (int)e->At(0) << ", 'b' -> " << (int)e->At(1) << "\n";
std::cout << " NameAt(0)=" << e->NameAt(0) << " NameAt(1)=" << e->NameAt(1) << "\n";
// 2) duplicate label
auto col2 = CreateColumnByType("Enum8('a' = 1, 'a' = 2)"); // server rejects this type
std::cout << "accepted, GetName() = " << col2->Type()->GetName() << "\n";
// 3) end-to-end: the collapsed name matches the destination column, so the INSERT succeeds
Client client(ClientOptions().SetHost("localhost").SetPort(9000));
client.Execute("DROP TABLE IF EXISTS t");
client.Execute("CREATE TABLE t (id UInt8, e Enum8('b' = 1)) ENGINE = Memory");
Block block;
auto ids = std::make_shared<ColumnUInt8>();
ids->Append(1); ids->Append(2);
block.AppendColumn("id", ids);
block.AppendColumn("e", col);
client.Insert("t", block); // no exception
client.Select("SELECT id, e, CAST(e AS Int8) FROM t ORDER BY id", [](const Block& b) {
for (size_t i = 0; i < b.GetRowCount(); ++i)
std::cout << " row: " << (int)b[0]->As<ColumnUInt8>()->At(i) << " "
<< b[1]->As<ColumnEnum8>()->NameAt(i) << " "
<< (int)b[2]->As<ColumnInt8>()->At(i) << "\n";
});
client.Execute("DROP TABLE IF EXISTS t");
}
Expected (what the server does, and what EnumColumnCodec in the .NET TCP client does): both CreateColumnByType() calls throw, naming the offending type.
Actual:
accepted, GetName() = Enum8('b' = 1) <- member 'a' silently dropped
'a' -> 1, 'b' -> 1 <- both labels write ordinal 1
NameAt(0)=b NameAt(1)=b <- 'a' is unreadable
accepted, GetName() = Enum8('a' = 1, 'a' = 2)
row: 1 b 1 <- appended as 'a', stored as 'b'
row: 2 b 1
The INSERT completed without any error and the first row was silently written with the wrong label.
For reference, with a destination column of Enum8('a' = 1, 'b' = 2) the server does catch the mismatch, because the collapsed name no longer matches:
DB::Exception: Enum conversion changes value for element 'b' from 1 to 2:
while converting column "e" from type Enum8('b' = 1) to type Enum8('a' = 1, 'b' = 2)
so whether the corruption reaches the table depends only on which labels happen to collide.
Suggested fix
Validate the member list when the EnumType is built — in EnumType::EnumType (clickhouse/types/types.cpp:357-362), so every entry point (CreateColumnByType, Type::CreateEnum8/CreateEnum16, direct construction) is covered: throw ValidationError/UnimplementedError naming the type if name_to_value_.insert(item) reports the label was already present, or if value_to_name_ already holds the ordinal.
Note this must not be turned into a range check on explicitly numbered members: 26.3 accepts an explicit out-of-range ordinal and wraps it (Enum8('a' = 200, ...) -> 'a' = -56), and only 26.10+ rejects it.
Related but distinct, both still open: #604 (members without explicit numbers) and #602 (appending undeclared ordinals).
Link
Relayed from ClickHouse/clickhouse-cs#731 — the same defect in the .NET HTTP client's EnumType.Parse.
- 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 queries
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
ClickHouse/clickhouse-cpp#644 ·
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
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