Binary insert: a bare Enum(...) type hint with an ordinal outside -128..127 is written as Enum8, which corrupts the inserted rows
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
- 76/100
Direzione di ricerca
Inizia da ClickHouse.Driver/Types/EnumType.cs, dove l’issue dice che un Enum senza specificazione viene analizzato come Enum8; confronta la gestione del relativo intervallo degli ordinali con ClickHouse.Driver.Tcp/Types/Codecs/EnumColumnCodec.cs. Controlla i test esistenti per l’analisi dei tipi e aggiungi casi per ordinali inferiori a -128 e superiori a 127. Verifica che il comportamento di Enum8 e Enum16 espliciti resti invariato. Il lavoro è completato quando un Enum senza specificazione seleziona la larghezza usata dal server per tali intervalli e gli inserimenti binari preservano tutte le righe.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
The HTTP client parses a bare Enum(...) type string as Enum8, whatever the ordinals are. The server types a bare Enum as Enum16 when an ordinal is outside -128..127.
A binary insert gets such a type from the user through InsertOptions.ColumnTypes, or through [ClickHouseColumn(Type = "...")] on a POCO property. The client then writes 1 byte per value where the Enum16 column reads 2:
- With an odd row count, the insert fails:
Cannot read all data. Bytes read: 1. Bytes expected: 2. - With an even row count, the insert succeeds. The client reports all rows, but the server stores half of them, with undeclared ordinals. Text reads of the column then fail with
UNKNOWN_ELEMENT_OF_ENUM. The values of the other columns in the row also go wrong.
An explicit Enum16(...) hint works correctly. The read path and server-probed schemas are not affected: the server always reports Enum8/Enum16 explicitly, also inside Array, Tuple and typed JSON paths.
Steps to reproduce
- Run the code example. It inserts 3 rows with
InsertBinaryAsyncand the column type hintEnum('a' = 1000, 'b' = 1001). SELECT toInt16(e), id FROM t
Expected behaviour
The client sizes a bare Enum the same way as the server:
SELECT toTypeName(CAST(1000, 'Enum(\'a\' = 1000)')) -- Enum16('a' = 1000)
SELECT toTypeName(CAST(128, 'Enum(\'a\' = 128)')) -- Enum16('a' = 128)
SELECT toTypeName(CAST(-129, 'Enum(\'a\' = -129)')) -- Enum16('a' = -129)
SELECT toTypeName(CAST(127, 'Enum(\'a\' = 127)')) -- Enum8('a' = 127)
The 3 rows are stored as inserted: (1000, 1), (1001, 2), (1000, 3).
Actual: the insert reports 3 rows, and the server stores 2 rows: (488, 233) and (-6142, 3). It reads the bytes E8 01 | E9 02 | E8 03 as Enum16, UInt8 pairs.
TypeConverter.ParseClickHouseType("Enum('a' = 1000)", TypeSettings.Default) returns an Enum8Type that renders as Enum8('a' = 1000). The same applies to Enum('a' = 128) and Enum('a' = -129).
Code example
Reproduction program (C#, 16 lines)
await client.ExecuteNonQueryAsync(
"CREATE TABLE t (e Enum16('a' = 1000, 'b' = 1001), id UInt8) ENGINE = Memory");
var reported = await client.InsertBinaryAsync(
"t",
new[] { "e", "id" },
new List<object[]> { new object[] { "a", (byte)1 }, new object[] { "b", (byte)2 }, new object[] { "a", (byte)3 } },
new InsertOptions
{
ColumnTypes = new Dictionary<string, string>
{
["e"] = "Enum('a' = 1000, 'b' = 1001)",
["id"] = "UInt8",
},
});
// reported == 3; SELECT toInt16(e), id FROM t returns (488, 233), (-6142, 3)
The POCO path does the same: a property with [ClickHouseColumn(Type = "Enum('a' = 1000, 'b' = 1001)")] and the values "a", "b" reports 2 rows and stores 1 row with the ordinal -5656.
Results, single column Enum16('a' = 1000, 'b' = 1001) unless stated:
| Hint | Rows inserted (label a) |
Result |
|---|---|---|
Enum('a' = 1000, 'b' = 1001) |
1 | Insert fails, Code 33 |
Enum('a' = 1000, 'b' = 1001) |
2 | Reported 2, stored 1 row, ordinal -5912 |
Enum('a' = 1000, 'b' = 1001) |
4 | Reported 4, stored 2 rows, ordinal -5912 |
Enum('a' = 128, 'b' = 1) into Enum16('a' = 128, 'b' = 1) |
2 | Reported 2, stored 1 row, ordinal -32640 |
Enum16('a' = 1000, 'b' = 1001) |
2 | Correct: 2 rows, ordinal 1000 |
Enum('a' = 1000, 'b' = 1001), numeric value 1000 |
1 | Fails on the client: OverflowException from Convert.ToSByte |
Error log
Text read of the stored rows:
Code: 691. DB::Exception: Unexpected value -5912 in enum: while executing 'FUNCTION toString(__table1.e :: 1) -> toString(__table1.e) String : 0'. (UNKNOWN_ELEMENT_OF_ENUM) (version 26.9.1.1629 (official build))
Odd row count:
Code: 33. DB::Exception: Cannot read all data. Bytes read: 1. Bytes expected: 2: (at row 1)
Root cause
ClickHouse.Driver/Types/EnumType.cs:48-50:Parsemapscase "Enum":tonew Enum8Type(parameters)and does not look at the ordinals. TheENUMalias (TypeConverter.cs:46) goes the same way.ClickHouse.Driver/Types/Enum8Type.cs:30: the label path writes(sbyte)Lookup(enumStr)with an unchecked cast, so an ordinal outside -128..127 is truncated silently (1000becomes-24).- The server stores RowBinary enum ordinals without a membership check (see #718), so it stores the misaligned bytes and does not reject them.
The fix is one condition in EnumType.Parse.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- #719, the fix of #718, does not change the
Enum8Typecast.
Configuration
Environment
- Client version:
mainat 8ef4d956 (after 1.5.0) - Language version: C# (test project,
net10.0) - .NET version: SDK 10.0.203
- OS: Linux (Docker)
ClickHouse server
- ClickHouse Server version: 26.9.1.1629
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved: in the code example- Sample data for all these tables: in the code example
Classification: impact: Data integrity · area: Data types & formats (Enum8, Enum16)
Found by automated analysis (Polyglot AI) while working on #718. Verified against a live server.
- Lingua principale
- C#
- Stelle
- 101
- Fork
- 28
- Merge medio
- 2g 11h
- PR unite (30g)
- 30
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ClickHouse/clickhouse-cs
-
AcceptEncodingWireTests fail on Windows, 15 of 16, with a connection resetForse già presa @enigmatic-postulate l’ha presa oggi. Apertabug tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
ClickHouse/clickhouse-cs#779 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
ClickHouse/clickhouse-cs#754 ·
I maintainer di solito rispondono entro 1 giorno
-
bug data-types native-format
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
ClickHouse/clickhouse-cs#730 ·
I maintainer di solito rispondono entro 1 giorno
-
ClickHouseDataSource: DisposeAsync() (and await using) does not dispose the owned ClickHouseClient/HttpClientForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaapi-diagnostics bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
ClickHouse/clickhouse-cs#689 ·
I maintainer di solito rispondono entro 1 giorno
-
Every query fails with Code 164 (READONLY) for a readonly = 1 user: the driver always sends enable_http_compression and input_format_binary_read_json_as_string as settingsForse già presa @polyglotAI-bot l’ha presa 52 giorni fa. Apertabug configuration
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
ClickHouse/clickhouse-cs#659 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di ClickHouse/clickhouse-cs
Issue simili
-
agentic-workflows
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
dotnet/maui#40788 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue help wanted
Difficoltà 2/5 1-3 ore Idoneità per principianti 92/100
erkamyaman/rust-anything#943 ·
I maintainer di solito rispondono entro 1 giorno
-
priority:P2
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
SixLabors/ImageSharp.Drawing#438 · 1 commento ·
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100