Binary insert: a bare Enum(...) type hint with an ordinal outside -128..127 is written as Enum8, which corrupts the inserted rows

Aperta Adatta ai principianti
#720 0 commenti 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
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
backend, databases

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

bug data-types
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
  1. Run the code example. It inserts 3 rows with InsertBinaryAsync and the column type hint Enum('a' = 1000, 'b' = 1001).
  2. 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: Parse maps case "Enum": to new Enum8Type(parameters) and does not look at the ordinals. The ENUM alias (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 (1000 becomes -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 Enum8Type cast.
Configuration
Environment
  • Client version: main at 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 TABLE statements 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

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-cs

Tutte le issue di ClickHouse/clickhouse-cs

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.