ADO.NET metadata ignores Nullable inside LowCardinality and SimpleAggregateFunction: AllowDBNull is false, GetFieldType is Nullable<T>, DataTable.Load and Fill throw
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
Research direction
Start with ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs around GetFieldType and ClickHouse.Driver/Utility/SchemaDescriber.cs around DescribeSchema; compare how GetEffectiveClickHouseType unwraps types. Update metadata handling for Nullable inside LowCardinality and SimpleAggregateFunction, then check the reported GetFieldType, AllowDBNull, DataTable.Load, and adapter Fill behavior for the reproduction cases.
Written by the indexing model from the issue text.
Description
Describe the bug
When the HTTP client reports column metadata, it looks for Nullable only at the outer level of the column type. For a Nullable(T) inside LowCardinality(...) or SimpleAggregateFunction(...):
GetSchemaTable()(and soGetColumnSchema()) reportsAllowDBNull = False, but the column returnsDBNull.GetFieldType()and theDataTypeofGetSchemaTable()reportNullable<T>whenTis a value type (Nullable<ulong>,Nullable<Guid>). For a bareNullable(T)column, the reader reportsT.
Effects, with the default settings:
DataTable.Load(reader)throwsConstraintExceptionwhen aLowCardinality(Nullable(String))column has a NULL.DataTable.Load(reader)andClickHouseDataAdapter.FillthrowNotSupportedException: DataSet does not support System.Nullable<>.for aSimpleAggregateFunction(anyLast, Nullable(UInt64))orLowCardinality(Nullable(UUID))column, also when the value is not NULL.
IsDBNull and GetValue return DBNull for the NULL values. Not affected: a bare Nullable(T) column.
Workaround: cast the column to the bare type in the query, for example CAST(s AS Nullable(String)). Then AllowDBNull is True, and Load and Fill work.
Steps to reproduce
- Start a ClickHouse 26.11 server.
- Put the code example in a directory one level below a clone of this repository (for example
repro/app.cs). - Run
CLICKHOUSE_CONNECTION="Host=localhost" dotnet run app.cs.
Expected behaviour
The server reports that all four columns are nullable (curl, same table and row as the code example):
SELECT isNullable(c), isNullable(s), isNullable(n), isNullable(u), isNull(s), n, isNull(u) FROM t_nullable_meta FORMAT TSVWithNames
isNullable(c) isNullable(s) isNullable(n) isNullable(u) isNull(s) n isNull(u)
1 1 1 1 1 7 1
Each column gets the metadata of the bare Nullable(UInt64) column (line 1 of the output): AllowDBNull=True, GetFieldType = T (System.String, System.UInt64, System.Guid), and Load and Fill work.
Code example
Reproduction program (C#, 28 lines)
#:project ../ClickHouse.Driver/ClickHouse.Driver.csproj
using System.Data;
using ClickHouse.Driver.ADO;
using ClickHouse.Driver.ADO.Adapters;
using ClickHouse.Driver.Utility;
var cs = Environment.GetEnvironmentVariable("CLICKHOUSE_CONNECTION") ?? "Host=localhost";
using var connection = new ClickHouseConnection(cs);
await connection.ExecuteStatementAsync("CREATE OR REPLACE TABLE t_nullable_meta (k UInt8, c Nullable(UInt64), s LowCardinality(Nullable(String)), n SimpleAggregateFunction(anyLast, Nullable(UInt64)), u LowCardinality(Nullable(UUID))) ENGINE = MergeTree ORDER BY k");
await connection.ExecuteStatementAsync("INSERT INTO t_nullable_meta VALUES (1, NULL, NULL, 7, NULL)");
foreach (var column in new[] { "c", "s", "n", "u" })
{
var sql = $"SELECT {column} FROM t_nullable_meta";
using (var reader = await connection.ExecuteReaderAsync(sql))
Console.Write($"{reader.GetDataTypeName(0)}: GetFieldType={reader.GetFieldType(0)}, AllowDBNull={reader.GetSchemaTable().Rows[0]["AllowDBNull"]}");
using (var reader = await connection.ExecuteReaderAsync(sql))
{
try { new DataTable().Load(reader); Console.Write(", Load: ok"); }
catch (Exception ex) { Console.Write($", Load: {ex.GetType().Name}: {ex.Message}"); }
}
using var command = connection.CreateCommand();
command.CommandText = sql;
try { new ClickHouseDataAdapter { SelectCommand = command }.Fill(new DataTable()); Console.WriteLine(", Fill: ok"); }
catch (Exception ex) { Console.WriteLine($", Fill: {ex.GetType().Name}: {ex.Message}"); }
}
await connection.ExecuteStatementAsync("DROP TABLE t_nullable_meta");
Error log
Output, the same in 2 of 2 runs:
Nullable(UInt64): GetFieldType=System.UInt64, AllowDBNull=True, Load: ok, Fill: ok
LowCardinality(Nullable(String)): GetFieldType=System.String, AllowDBNull=False, Load: ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints., Fill: ok
SimpleAggregateFunction(anyLast, Nullable(UInt64)): GetFieldType=System.Nullable`1[System.UInt64], AllowDBNull=False, Load: NotSupportedException: DataSet does not support System.Nullable<>., Fill: NotSupportedException: DataSet does not support System.Nullable<>.
LowCardinality(Nullable(UUID)): GetFieldType=System.Nullable`1[System.Guid], AllowDBNull=False, Load: NotSupportedException: DataSet does not support System.Nullable<>., Fill: NotSupportedException: DataSet does not support System.Nullable<>.
Root cause
ClickHouseDataReader.GetFieldType (ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs:291) and SchemaDescriber.DescribeSchema (ClickHouse.Driver/Utility/SchemaDescriber.cs:47 for DataType, :50 for AllowDBNull) check only whether the outer type is a NullableType. LowCardinalityType.FrameworkType and SimpleAggregateFunctionType.FrameworkType return the FrameworkType of the inner NullableType, which is Nullable<T> for a value type. GetEffectiveClickHouseType (ClickHouseDataReader.cs:142) already removes these wrappers with TransparentWrapper.Unwrap; these three places do not.
The fix is in these three places of the HTTP client (the TCP client has no ADO.NET reader). Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
#518 (fixed by #769): the same wrappers, for precision, scale and GetDateTimeOffset; Nullable was not part of it.
Configuration
Environment
- Client version: 1.7.0 (
mainat c58178ed) - .NET version: .NET SDK 11.0.100, runtime .NET 11.0.0 (the same result on
net10.0in the test project) - OS: Linux (container)
ClickHouse server
- ClickHouse Server version: 26.11.7.10
- ClickHouse Server non-default settings, if any: none
Classification: impact: none · area: API & diagnostics (GetSchemaTable, GetFieldType)
Found by automated analysis (Polyglot AI) while working on clickhouse-rs#576. 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