InsertBinaryAsync: column names with escapes fail: UseSchemaCache does not decode pre-quoted names, and a backslash is not escaped

Open
#807 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
49/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
backend, database

Research direction

Read ClickHouse.Driver/Utility/SchemaResolver.cs and ClickHouse.Driver/Utility/StringExtensions.cs, then trace the INSERT column list in ClickHouse.Driver/ClickHouseClient.cs. Reproduce the reported spellings in probe, cache, and ColumnTypes modes; inspect issue #316 and PR #679 because support for pre-quoted names is unresolved. Done when the expected column receives the value across the reported modes.

Written by the indexing model from the issue text.

Description

bug queries
Describe the bug

InsertBinaryAsync (the object[] and the POCO overload) fails for column names that need an escape:

  • With InsertOptions.UseSchemaCache = true, a pre-quoted column name with an escape ( `ab``, ``a\b``, ``a\tb``, but not ``a`b``) throwsArgumentException: Column '...' not found in table schema. The same name works with the default schema probe and with InsertOptions.ColumnTypes`.
  • A column whose name contains a backslash cannot be inserted with its bare name in any schema mode. Its pre-quoted name `a\\b` works only with ColumnTypes.

The TCP client (ClickHouseTcpClient.InsertAsync) inserts into all of these columns.

The same defect was reported for clickhouse-go in https://github.com/ClickHouse/clickhouse-go/issues/2212.

Steps to reproduce
  1. Start a ClickHouse 26.11 server.
  2. Run the code example. It inserts 7 with each column spelling and each schema mode, and prints the column that got the value.
Expected behaviour

The server reads a quoted identifier with the escapes of a string literal (Syntax): system.columns gives the names a`b, a\b and a<TAB>b for the table of the example. Each spelling inserts into its column in each schema mode: `ab`` and ``a`b `` into ``ab, a\\b and the bare `a\b` into `a\b`, a\tb `` into a<TAB>b.

Code example
Reproduction program (C#, 31 lines)
using ClickHouse.Driver;

const string cs = "Host=clickhouse;Port=8123";
using var client = new ClickHouseClient(cs);
// Server column names: a`b, a\b, a<TAB>b, plain.
const string ddl = "(`a``b` Int64, `a\\\\b` Int64, `a\\tb` Int64, plain Int64) ENGINE = Memory";
const string where = "SELECT arrayStringConcat(arrayFilter(x -> x != '', [if(sum(`a``b`) = 7, 'a`b', ''), if(sum(`a\\\\b`) = 7, 'a\\\\b', ''), if(sum(`a\\tb`) = 7, 'a<TAB>b', '')]), ',') FROM t";

string[] columns = ["`a``b`", "`a\\`b`", "`a\\\\b`", "`a\\tb`", "a`b", "a\\b", "a\tb"];
foreach (var mode in new[] { "probe", "cache", "types" })
{
    Console.WriteLine($"== {mode}");
    foreach (var column in columns)
    {
        await client.ExecuteNonQueryAsync($"CREATE OR REPLACE TABLE t {ddl}");
        var options = mode switch
        {
            "probe" => new InsertOptions(),
            "cache" => new InsertOptions { UseSchemaCache = true },
            _ => new InsertOptions { ColumnTypes = new Dictionary<string, string> { [column] = "Int64", ["plain"] = "Int64" } },
        };
        string result;
        try
        {
            await client.InsertBinaryAsync("t", [column, "plain"], [new object[] { 7L, 1L }], options);
            result = $"ok, value in [{await client.ExecuteScalarAsync(where)}]";
        }
        catch (Exception ex) { result = $"{ex.GetType().Name}: {ex.Message.Split('\n')[0].Split(" (")[0]}"; }
        Console.WriteLine($"  {column.Replace("\t", "<TAB>"),-9} -> {result}");
    }
}
Error log

Output on main (c58178ed):

== probe
  `a``b`    -> ok, value in [a`b]
  `a\`b`    -> ok, value in [a`b]
  `a\\b`    -> ClickHouseServerException: Code: 16. DB::Exception: No such column a in table default.t
  `a\tb`    -> ok, value in [a<TAB>b]
  a`b       -> ok, value in [a`b]
  a\b       -> ClickHouseServerException: Code: 47. DB::Exception: Unknown expression identifier `a\b`. In scope SELECT `a\b`, plain FROM t WHERE 1 = 0.
  a<TAB>b   -> ok, value in [a<TAB>b]
== cache
  `a``b`    -> ArgumentException: Column '`a``b`' not found in table schema
  `a\`b`    -> ok, value in [a`b]
  `a\\b`    -> ArgumentException: Column '`a\\b`' not found in table schema
  `a\tb`    -> ArgumentException: Column '`a\tb`' not found in table schema
  a`b       -> ok, value in [a`b]
  a\b       -> ClickHouseServerException: Code: 16. DB::Exception: No such column a in table default.t
  a<TAB>b   -> ok, value in [a<TAB>b]
== types
  `a``b`    -> ok, value in [a`b]
  `a\`b`    -> ok, value in [a`b]
  `a\\b`    -> ok, value in [a\b]
  `a\tb`    -> ok, value in [a<TAB>b]
  a`b       -> ok, value in [a`b]
  a\b       -> ClickHouseServerException: Code: 16. DB::Exception: No such column a in table default.t
  a<TAB>b   -> ok, value in [a<TAB>b]

In "No such column a", a backspace follows the a: the server reads \b as an escape.

Root cause
  • With the schema cache, CachedSchema.Select looks up EncloseColumnName(name) in a dictionary whose keys are the server names, enclosed by the client (ClickHouse.Driver/Utility/SchemaResolver.cs:179, keys from :95). EncloseColumnName returns a pre-quoted name unchanged (ClickHouse.Driver/Utility/StringExtensions.cs:38), so the lookup compares the caller's spelling as text and does not decode its escapes.
  • EncloseColumnName escapes the backtick but not the backslash (StringExtensions.cs:42), so the probe query and the INSERT column list (ClickHouse.Driver/ClickHouseClient.cs:741) contain `a\b`. The probe mode encloses the server name a\b in the same way (SchemaResolver.cs:95), so the pre-quoted `a\\b` fails there too.

The fix depends on the open decision in #316: whether pre-quoted column names stay accepted. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related
  • #316 and its open PR #679: #679 escapes the backslash and stops the support of pre-quoted names, so these spellings then fail in every mode.
Configuration
Environment
  • Client version: 1.7.0 (main at c58178ed)
  • .NET version: .NET SDK 11.0.100, program target net10.0
  • OS: Linux (container)
ClickHouse server
  • ClickHouse Server version: 26.11.7.10
  • ClickHouse Server non-default settings, if any: none

Classification: impact: none · area: Queries (INSERT column list quoting)


Found by automated analysis (Polyglot AI) while checking clickhouse-go#2212 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.