Enum type parsing (HTTP): labels are decoded with Regex.Unescape, not ClickHouse escapes ('it''s', '\z', NUL + digit)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
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
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:
- Type strings that the application writes:
InsertOptions.ColumnTypesand{name:Type}query parameter type hints, also insideNullable(...)/Array(...). - Column types that the server declares, only for
\0followed by an octal digit. The server writes a NUL byte in a label as\0, so the labelaNUL12barrives in the RowBinaryWithNamesAndTypes header as'a\012b'. The other escapes that the server writes in a type name (\',\\,\n,\b,\f,\0before 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
- Run the code example below against a ClickHouse server.
- Step 1 throws, although
it'sis the only label of the column. - Step 2 throws before the query is sent.
- 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:
mainat 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 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 #727. Verified against a live server.
- Dominant language
- C#
- Stars
- 102
- Forks
- 31
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 40
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/clickhouse-cs
-
bug documentation queries
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#815 ·
Maintainers usually reply within 1 day
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-cs#813 ·
Maintainers usually reply within 1 day
-
bug data-types documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#811 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cs#809 ·
Maintainers usually reply within 1 day
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cs#806 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-cs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xmlPossibly taken @alexeyfala claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
danipen/TextMateSharp#145 · 2 comments ·
-
good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Klyroo/klyroo-dashboard#12 ·
Maintainers usually reply within 1 day
-
CONTRIBUTING rules Node out in step 0, then tells client-side contributors to run node --test, which is what CI gates onPossibly taken @yadavraj2001 claimed this today. Opendocumentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
juandresrodca/AdminForge#73 · 1 comment ·
Maintainers usually reply within 1 day
-
[辞書] VIVANTOpen要トリアージ 辞書
Difficulty 1/5 Under an hour Newbie friendliness 70/100
Maintainers usually reply within 1 day