Type-name parser (HTTP) accepts unbalanced parentheses: ColumnTypes "Nullable(String" stores NULL rows with no error

Open Beginner friendly
#806 1 comment 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
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
clickhouse, csharp
Domain
backend, databases

Research direction

Start with ClickHouse.Driver/Types/Grammar/Parser.cs, where Parser.Parse builds the type tree, and inspect the stack operations and end-of-input handling described in the issue. Reproduce the malformed inputs with the public parser and check the HTTP type-name entry points listed in the report. Done means each malformed name raises an ArgumentException naming the input, with no invalid type accepted or null returned.

Written by the indexing model from the issue text.

Description

bug data-types
Describe the bug

The HTTP client's type-name parser does not check that the parentheses of a type name are balanced. The server rejects all of these names with SYNTAX_ERROR, but the client accepts some of them as a different type, and fails on others with an internal exception type.

  • A name with an unclosed ( is read as its innermost type: Nullable(String is String, Array(Int32 and Map(String, Int32 are Int32.
  • A name with an extra ) or a top-level , (Nullable(Int32)), Array(Int32),Int64) throws InvalidOperationException: Stack empty.
  • A name that ends after ( (SimpleAggregateFunction() throws NullReferenceException.

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

The other entry points that parse a caller's type name are ClickHouseDbParameter.ClickHouseType and {name:Type} hints (extra ) or ,: InvalidOperationException before the request), and the public ClickHouse.Driver.Types.Grammar.Parser.Parse. The header of a RowBinaryWithNamesAndTypes response goes through the same parser.

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 (one ) missing).
  3. Read the table.
Expected behaviour

The client rejects a malformed type name with an ArgumentException that names the input, as it does for Array[Int32] (ArgumentException: Unknown type: Array[Int32]), and writes nothing. The server rejects these names:

SELECT CAST(NULL, 'Nullable(String')
Code: 62. DB::Exception: Syntax error (data type): failed at position 16 (end of query): (String. Unmatched parentheses: '(' at position 9 is never closed ... (SYNTAX_ERROR)
SELECT CAST(NULL, 'Array(Int32))')
Code: 62. DB::Exception: Syntax error (data type): ... Unmatched parentheses: ')' at position 13 has no matching opening bracket ... (SYNTAX_ERROR)

The TCP client of this repo rejects all of these names with FormatException (ClickHouse.Driver.Tcp/Types/TypeParser.cs:11).

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 count() FROM t"));  // 12
Console.WriteLine(await client.ExecuteScalarAsync("SELECT countIf(v IS NULL) FROM t")); // 12

Parser.Parse("Array(Int32").ToString();   // "Int32"
Parser.Parse("Nullable(Int32))");         // InvalidOperationException: Stack empty.
Parser.Parse("SimpleAggregateFunction("); // null
Error log
InsertBinaryAsync, ColumnTypes "Nullable(String", values ["hello","world"] -> returned 2; stored: 12 x NULL
Parser.Parse("Array(Int32")              -> Int32
Parser.Parse("Nullable(Int32))")         -> InvalidOperationException: Stack empty.
Parser.Parse("SimpleAggregateFunction(") -> null; TypeConverter then throws NullReferenceException

Root cause: Parser.Parse (ClickHouse.Driver/Types/Grammar/Parser.cs) builds the tree on a stack and never checks it. At the end of the input it returns current also when the stack still holds open nodes (line 36). It calls stack.Pop() (line 29) and stack.Peek() (line 43) with no check for an empty stack. It returns null when the input ends after (, and TypeConverter.ExtractTypeName then reads node.Value (ClickHouse.Driver/Types/TypeConverter.cs:250).

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

Configuration
Environment
  • Client version: main at c58178ed (after 1.7.0)
  • 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)


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-rs#578. 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.