TCP type parsing: `\N`, `\=` and `\` + control character in a quoted Enum label are not decoded like the server

Open Beginner friendly
#730 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

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

bug data-types native-format
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 (labels ab and a\Nb). CanWrite / CanRead throw FormatException.
  • Enum8('a\Nb' = 1, 'ab' = 2): the server rejects the type (Duplicate names in enum). CanWrite / CanRead return true.

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
  1. Run the code example below. It does not need a server.
  2. The first call throws FormatException, but the server accepts that type.
  3. 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 switch has no case 'N';
  • '=' is not in the list of characters that drop the backslash (line 102);
  • the default arm (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

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.