TCP type parsing: `\N`, `\=` and `\` + control character in a quoted Enum label are not decoded like the server
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
Research direction
The escape handling is in AppendEscape in ClickHouse.Driver.Tcp/Types/QuotedText.cs (around lines 91-105), which both EnumColumnCodec.ParseMember and NamedElementParser.SplitElement call through QuotedText.TryRead. Start by reproducing the two CanWrite calls in the issue, then add the \N, \= and backslash-plus-control-character cases. Done when those labels decode like the server, other unknown escapes such as \z keep their backslash, and the regression test in EnumColumnCodecTests passes.
Written by the indexing model from the issue text.
Description
Describe the bug
The native TCP client (ClickHouse.Driver.Tcp) decodes three escapes in a quoted Enum label (and in a backtick-quoted Tuple/Nested element name) of a type string differently from the server:
| Escape in a quoted label | Server | TCP client on main |
|---|---|---|
\N |
empty string | \N (backslash kept) |
\= |
= |
\= (backslash kept) |
\ + an ASCII control character 0x00–0x1F (for example a literal TAB) |
the control character | \ + the control character |
Impact is low. The public entry points that decode a type string that the application writes are ClickHouseTcpTypes.CanWrite and ClickHouseTcpTypes.CanRead. There a label changes the result only through the duplicate-label check:
Enum8('a\Nb' = 1, 'a\\Nb' = 2): the server accepts the type (labelsabanda\Nb).CanWrite/CanReadthrowFormatException.Enum8('a\Nb' = 1, 'ab' = 2): the server rejects the type (Duplicate names in enum).CanWrite/CanReadreturntrue.
The same occurs for \=, and also inside Nullable(...), Array(...), Enum16(...) and a bare Enum(...).
Reads, inserts and {name:Type} parameters are not affected (checked on main). The server never writes these escapes in a type name: it sends Enum8('ab' = 1, 'a\\Nb' = 2). The TCP insert schema comes from the server, and the parameter path does not decode labels.
Steps to reproduce
- Run the code example below. It does not need a server.
- The first call throws
FormatException, but the server accepts that type. - The second call prints
True, but the server rejects that type.
Expected behaviour
The client decodes a quoted label like the server does. Server output:
SELECT hex(toString(CAST(1 AS Enum8('a\Nb' = 1)))); -- 6162 (ab)
SELECT toTypeName(CAST(1 AS Enum8('a\Nb' = 1))); -- Enum8('ab' = 1)
SELECT hex(toString(CAST(1 AS Enum8('a\=b' = 1)))); -- 613D62 (a=b)
SELECT hex('a\<TAB>b'); -- with a literal TAB (0x09) after the backslash: 610962
SELECT hex('a\<DEL>b'); -- with a literal DEL (0x7F) after the backslash: 615C7F62 (backslash kept)
SELECT toTypeName(CAST(1 AS Enum8('a\Nb' = 1, 'a\\Nb' = 2))); -- Enum8('ab' = 1, 'a\\Nb' = 2)
SELECT toTypeName(CAST(1 AS Enum8('a\Nb' = 1, 'ab' = 2))); -- Code: 62. DB::Exception: Duplicate names in enum: 'ab' = 1 and 2
Server source: parseComplexEscapeSequence in src/IO/ReadHelpers.cpp. \N gives no characters. For a single-character escape, the backslash is dropped when the decoded character is one of \ ' " ` / = or isControlASCII (<= 31). The server applies the same rule to backtick-quoted identifiers: SELECT toTypeName(CAST((1, 2) AS Tuple(`a\Nb` Int8, c Int8))) gives Tuple(ab Int8, c Int8).
Code example
using ClickHouse.Driver.Tcp;
// The server accepts this type: its labels are 'ab' and 'a\Nb'.
try
{
Console.WriteLine(ClickHouseTcpTypes.CanWrite(@"Enum8('a\Nb' = 1, 'a\\Nb' = 2)", typeof(string)));
}
catch (FormatException e)
{
Console.WriteLine(e.Message); // ... declares the label 'a\Nb' more than once.
}
// The server rejects this type: Duplicate names in enum: 'ab' = 1 and 2.
Console.WriteLine(ClickHouseTcpTypes.CanWrite(@"Enum8('a\Nb' = 1, 'ab' = 2)", typeof(string))); // True
Error log
System.FormatException: Enum type 'Enum8('a\Nb' = 1, 'a\\Nb' = 2)' declares the label 'a\Nb' more than once.
Root cause
QuotedText.AppendEscape (ClickHouse.Driver.Tcp/Types/QuotedText.cs:91-105):
- the
switchhas nocase 'N'; '='is not in the list of characters that drop the backslash (line 102);- the
defaultarm (line 105) keeps the backslash for every other character, ASCII control characters included.
EnumColumnCodec.ParseMember (EnumColumnCodec.cs:234) and NamedElementParser.SplitElement (NamedElementParser.cs:39) both use QuotedText.TryRead.
The fix is three small changes in QuotedText.AppendEscape. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related: #729 (HTTP client) names QuotedText as its reference decoder, without these three cases.
Configuration
Environment
- Client version:
main@ 45494d47 (after 1.5.0), native TCP client (ClickHouse.Driver.Tcp) - Language version: C#
- .NET version: .NET SDK 10.0.404, tests run on net10.0
- OS: Linux (Docker)
ClickHouse server
- ClickHouse Server version: 26.9.1.1629
- ClickHouse Server non-default settings, if any: none
Classification: impact: none · area: Data types & formats (Enum8, Enum16) · native: format
Found by automated analysis (Polyglot AI) while working on #729. 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