InsertBinaryAsync: column names with escapes fail: UseSchemaCache does not decode pre-quoted names, and a backslash is not escaped
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 49/100
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
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 withInsertOptions.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 withColumnTypes.
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
- Start a ClickHouse 26.11 server.
- 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.Selectlooks upEncloseColumnName(name)in a dictionary whose keys are the server names, enclosed by the client (ClickHouse.Driver/Utility/SchemaResolver.cs:179, keys from:95).EncloseColumnNamereturns 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. EncloseColumnNameescapes 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 namea\bin 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 (
mainat 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
- 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