Type-name parser (HTTP) accepts two type names in one place and extra arguments: ColumnTypes "Nullable String" stores NULL rows with no error

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
clickhouse, csharp, databases
Domain
backend, database

Research direction

Start with TypeConverter.ExtractTypeName in ClickHouse.Driver/Types/TypeConverter.cs and Parser.Parse in ClickHouse.Driver/Types/Grammar/Parser.cs, then inspect the named Parse methods in DecimalType.cs, DateTime64Type.cs, SimpleAggregateFunctionType.cs, and MapType.cs. Compare their handling of extra names and arguments with ArrayType and NullableType. Done means malformed type names raise ArgumentException before insertion; the issue gives examples to check.

Written by the indexing model from the issue text.

Description

bug data-types
Describe the bug

The HTTP client's type-name parser accepts three kinds of malformed type names that the server rejects, and reads each of them as another type with no error. The same defect was reported for clickhouse-cpp in https://github.com/ClickHouse/clickhouse-cpp/issues/646.

  • Two type names in one place: the second name replaces the first. Array Int32 is Int32, Nullable String is String, Nullable(Int32 String) is Nullable(String), and Map(String Int64, Int32) is Map(Int64, Int32). The same occurs in Array and LowCardinality arguments, and at the top level (Int32 Int64 is Int64).
  • A type name after the closing ): the last name replaces the type. Nullable(String) Int32 is Int32, and Decimal(9, 2) Int32 is Int32.
  • Too many arguments: the client ignores the extra arguments. Decimal(9, 2, 3) is Decimal(9, 2), DateTime64(3, 'UTC', 'Asia/Tokyo') is DateTime64(3, 'UTC'), SimpleAggregateFunction(sum, UInt64, String) is SimpleAggregateFunction(sum, UInt64), and Map(String, Int32, Int64) is Map(String, Int32).

A malformed name in InsertOptions.ColumnTypes can store wrong rows with no error: InsertBinaryAsync writes the rows with the misread type, and the server reads the bytes with the real column type. With ColumnTypes["v"] = "Nullable String" (the parentheses missing), two rows "hello" and "world" are stored as 12 NULL rows, and the call returns 2.

Steps to reproduce
  1. Create a table with a Nullable(String) column.
  2. Insert two rows with InsertBinaryAsync, with InsertOptions.ColumnTypes set to Nullable String.
  3. Read the table.
Expected behaviour

The client rejects a malformed type name with an ArgumentException that names the input, as it does for Map(String = Int32) (ArgumentException: Unknown type: String = Int32), and writes nothing. The server rejects these names (SELECT toTypeName(defaultValueOfTypeName('<name>')), messages cut):

Array Int32                                   Code: 62. Syntax error (data type): failed at position 7 (Int32): Int32. Expected end of query
Nullable String                               Code: 62. Syntax error (data type): failed at position 10 (String): String. Expected end of query
Nullable(Int32 String)                        Code: 62. Syntax error (data type): failed at position 16 (String)
Nullable(String) Int32                        Code: 62. Syntax error (data type): failed at position 18 (Int32): Int32. Expected end of query
Decimal(9, 2, 3)                              Code: 42. Decimal data type family must have precision and optional scale arguments
DateTime64(3, 'UTC', 'Asia/Tokyo')            Code: 42. DateTime64 data type can optionally have two argument - scale and time zone name
SimpleAggregateFunction(sum, UInt64, String)  Code: 42. Aggregate function sum requires single argument
Map(String, Int32, Int64)                     Code: 42. Map data type family must have two arguments: key and value types

The server accepts an extra argument of DateTime and Time64 (DateTime('UTC', 'Asia/Tokyo') is DateTime('UTC')), and the client does the same. The TCP client of this repo rejects the names of the first two kinds (NotSupportedException, FormatException).

Code example
await client.ExecuteNonQueryAsync("CREATE TABLE t (v Nullable(String)) ENGINE = MergeTree ORDER BY tuple()");
var options = new InsertOptions { ColumnTypes = new Dictionary<string, string> { ["v"] = "Nullable String" } };
var n = await client.InsertBinaryAsync("t", new[] { "v" }, new[] { new object[] { "hello" }, new object[] { "world" } }, options);
Console.WriteLine(n);                                                               // 2, no exception
Console.WriteLine(await client.ExecuteScalarAsync("SELECT countIf(v IS NULL) FROM t")); // 12

await client.ExecuteNonQueryAsync("CREATE TABLE d (v Decimal(9, 3)) ENGINE = MergeTree ORDER BY tuple()");
options = new InsertOptions { ColumnTypes = new Dictionary<string, string> { ["v"] = "Decimal(9, 2, 3)" } };
await client.InsertBinaryAsync("d", new[] { "v" }, new[] { new object[] { 1.23m }, new object[] { 4.56m } }, options);
// SELECT v FROM d gives 0.123 and 0.456
Error log
InsertBinaryAsync, column Nullable(String), ColumnTypes "Nullable String", ["hello", "world"]         -> returned 2; stored 12 rows, all NULL
InsertBinaryAsync, column Nullable(String), ColumnTypes "Nullable(String) String", ["hello", "world"] -> returned 2; stored 12 rows, all NULL
InsertBinaryAsync, column Nullable(Int32), ColumnTypes "Nullable(Int32 String)", ["hello", "world"]   -> returned 2; stored 6 rows (1818585093, 1919907589, 4 x NULL)
InsertBinaryAsync, column Decimal(9, 3), ColumnTypes "Decimal(9, 2, 3)", [1.23, 4.56]                 -> returned 2; stored 0.123, 0.456

TypeConverter.ParseClickHouseType (the parser of ColumnTypes):
Array Int32                                   -> Int32
Nullable(Int32 String)                        -> Nullable(String)
Map(String Int64, Int32)                      -> Map(Int64, Int32)
LowCardinality(String Int32)                  -> LowCardinality(Int32)
Nullable(String) Int32                        -> Int32
Decimal(9, 2, 3)                              -> Decimal32(2)
DateTime64(3, 'UTC', 'Asia/Tokyo')            -> DateTime64(3, 'UTC')
SimpleAggregateFunction(sum, UInt64, String)  -> SimpleAggregateFunction(sum, UInt64)
Map(String, Int32, Int64)                     -> Map(String, Int32)

Root cause:

  • TypeConverter.ExtractTypeName (ClickHouse.Driver/Types/TypeConverter.cs:257-266) removes the text before the first space as an element name on every node, not only on an element of Tuple, Nested or JSON.
  • Parser.Parse (ClickHouse.Driver/Types/Grammar/Parser.cs:32) replaces current with each new name, also after a ) closed the node.
  • DecimalType.Parse (DecimalType.cs:51-52), DateTime64Type.Parse (DateTime64Type.cs:29-34), SimpleAggregateFunctionType.Parse (SimpleAggregateFunctionType.cs:21-22) and MapType.Parse (MapType.cs:125-126) read the arguments by index and do not check their count, as ArrayType and NullableType do with SingleChild.

The fix is in these three places. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #806 (open) reports unbalanced parentheses in the same parser; its checks do not catch these names.

Configuration
Environment
  • Client version: 1.7.0 (main at c58178ed)
  • Language version: C# (latest)
  • .NET version: .NET 10 (net10.0, SDK 11.0.100)
  • OS: Linux
ClickHouse server
  • ClickHouse Server version: 26.11.7.10

Classification: impact: Data integrity · area: Data types & formats (type names, RowBinary insert)


Found by automated analysis (Polyglot AI) while checking clickhouse-cpp#646 against the other client repos. 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.