Enum type parsing (HTTP): labels are decoded with Regex.Unescape, not ClickHouse escapes ('it''s', '\z', NUL + digit)

Open
#729 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
databases

Research direction

Start in ClickHouse.Driver/Types/EnumType.cs at ParseEnumMember (lines 86-102), where Regex.Unescape decodes the label. Read ClickHouse.Driver.Tcp/Types/QuotedText.cs for the server's escape rules, and check EnumColumnCodecTests.Create_LabelWithAnEscape_DecodesItLikeTheServer as the reference. Replace both Regex.Unescape call sites, including the one PR #728 adds, and keep the existing EnumTypeTests cases with \' passing. Done when the labels in the issue's table match the server hex values.

Written by the indexing model from the issue text.

Description

bug data-types
Describe the bug

The HTTP client decodes an Enum8 / Enum16 label in a type string differently from the server. The client gets a label that is not the label the server stores, or it cannot parse the type string.

Affected entry points:

  1. Type strings that the application writes: InsertOptions.ColumnTypes and {name:Type} query parameter type hints, also inside Nullable(...) / Array(...).
  2. Column types that the server declares, only for \0 followed by an octal digit. The server writes a NUL byte in a label as \0, so the label a NUL 12b arrives in the RowBinaryWithNamesAndTypes header as 'a\012b'. The other escapes that the server writes in a type name (\', \\, \n, \b, \f, \0 before a non-octal character) decode correctly.

Results on main (one table per label, Enum8(<label> = 1, 'other' = 2)):

Label in the type string Server label (hex) Client label Insert, InsertOptions.ColumnTypes {p:Enum8(...)} parameter Read, server-declared column
'it''s' 69742773 (it's) it''s (quote stays doubled) KeyNotFoundException: key it's OK OK
'a\zb', 'a\Ab', 'a\db' 615C7A62, 615C4162, 615C6462 (backslash kept) RegexParseException RegexParseException RegexParseException OK
'a\cAb', 'a\1b', 'aAb', 'a\.b', 'a\ b' backslash kept, e.g. 615C634162 escape decoded KeyNotFoundException OK OK
'a\012b' 6100313262 (a, NUL, 12, b) \012 read as one octal escape (LF) KeyNotFoundException OK wrong label 610A62; insert with the server schema also throws KeyNotFoundException

The parameter path fails only where parsing throws, because the parameter formatter does not look up labels.

Steps to reproduce
  1. Run the code example below against a ClickHouse server.
  2. Step 1 throws, although it's is the only label of the column.
  3. Step 2 throws before the query is sent.
  4. Step 3 prints False: the label read through the client is not the label the server stores.
Expected behaviour

The client decodes a label like the server does. Server output:

SELECT hex(toString(CAST(1 AS Enum8('it''s' = 1))));   -- 69742773   (it's)
SELECT hex(toString(CAST(1 AS Enum8('a\zb' = 1))));    -- 615C7A62   (a\zb)
SELECT hex(toString(CAST(1 AS Enum8('a\cAb' = 1))));   -- 615C634162 (a\cAb)
SELECT hex(toString(CAST(1 AS Enum8('a\012b' = 1))));  -- 6100313262 (a, NUL, 12, b)
SELECT toTypeName(CAST(1 AS Enum8('it''s' = 1)));      -- Enum8('it\'s' = 1)
SELECT toTypeName(CAST(1 AS Enum8('a\012b' = 1)));     -- Enum8('a\012b' = 1)

The native TCP client in this repository already decodes labels like the server: ClickHouse.Driver.Tcp/Types/QuotedText.cs, pinned by EnumColumnCodecTests.Create_LabelWithAnEscape_DecodesItLikeTheServer.

Code example
Reproduction program (C#, 22 lines)
using ClickHouse.Driver;
using ClickHouse.Driver.ADO.Parameters;
using ClickHouse.Driver.Utility;

var client = new ClickHouseClient("Host=localhost;Port=8123");

// 1. Type string written by the application: a doubled quote.
await client.ExecuteNonQueryAsync("CREATE TABLE enum_q (e Enum8('it''s' = 1)) ENGINE = Memory");
await client.InsertBinaryAsync("enum_q", new[] { "e" }, new[] { new object[] { "it's" } },
    new InsertOptions { ColumnTypes = new Dictionary<string, string> { ["e"] = "Enum8('it''s' = 1)" } });

// 2. Type string written by the application: an escape that the server keeps.
var ps = new ClickHouseParameterCollection();
ps.AddParameter("p", @"a\zb");
await client.ExecuteScalarAsync(@"SELECT {p:Enum8('a\zb' = 1)}", ps);

// 3. Column type declared by the server: a label with NUL followed by a digit.
await client.ExecuteNonQueryAsync(@"CREATE TABLE enum_nul (e Enum8('a\012b' = 1)) ENGINE = Memory");
await client.ExecuteNonQueryAsync("INSERT INTO enum_nul VALUES (1)");
var server = (string)await client.ExecuteScalarAsync("SELECT toString(e) FROM enum_nul"); // "a\0" + "12b"
var read = (string)await client.ExecuteScalarAsync("SELECT e FROM enum_nul");             // "a\nb"
Console.WriteLine(server == read);
Error log
1: ClickHouseBulkCopySerializationException: Error when serializing data
   -> KeyNotFoundException: The given key 'it's' was not present in the dictionary.
2: RegexParseException: Invalid pattern 'a\zb' at offset 3. Unrecognized escape sequence \z.
3: False   (server label hex 6100313262, client label hex 610A62)
Root cause

ClickHouse.Driver/Types/EnumType.cs:86-102 (ParseEnumMember) removes the outer quotes and passes the label to Regex.Unescape. Regex.Unescape uses .NET regex escape rules: it throws on an unknown escape of a word character, it decodes \cX, \uXXXX and up to three octal digits, it removes the backslash before any other character, and it does not know the doubled quote. The Tokenizer already keeps 'it''s' in one token, so only the decode step is wrong.

The fix is a label decoder with the server rules in the HTTP client (the two clients do not share a type tree). Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: open PR #728 (for #727) adds a second Regex.Unescape call, for a member without an ordinal.

Configuration
Environment
  • Client version: main at 45494d47; the same code is in 1.5.0
  • Language version: C# (LangVersion latest)
  • .NET version: 10.0 (net10.0)
  • 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 #727. Verified against a live server.

Dominant language
C#
Stars
102
Forks
31
Avg merge
2d 13h
Merged PRs (30d)
40

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/clickhouse-cs

All issues in ClickHouse/clickhouse-cs

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.