ADO.NET metadata ignores Nullable inside LowCardinality and SimpleAggregateFunction: AllowDBNull is false, GetFieldType is Nullable<T>, DataTable.Load and Fill throw

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

Maintainers usually reply within 1 day

@bjsneto is already working on this.

Since Dec 5, 2026.

  • #816 by @bjsneto — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
clickhouse, csharp
Domain
api, database

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

api-diagnostics bug
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 so GetColumnSchema()) reports AllowDBNull = False, but the column returns DBNull.
  • GetFieldType() and the DataType of GetSchemaTable() report Nullable<T> when T is a value type (Nullable<ulong>, Nullable<Guid>). For a bare Nullable(T) column, the reader reports T.

Effects, with the default settings:

  • DataTable.Load(reader) throws ConstraintException when a LowCardinality(Nullable(String)) column has a NULL.
  • DataTable.Load(reader) and ClickHouseDataAdapter.Fill throw NotSupportedException: DataSet does not support System.Nullable<>. for a SimpleAggregateFunction(anyLast, Nullable(UInt64)) or LowCardinality(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
  1. Start a ClickHouse 26.11 server.
  2. Put the code example in a directory one level below a clone of this repository (for example repro/app.cs).
  3. 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 (main at c58178ed)
  • .NET version: .NET SDK 11.0.100, runtime .NET 11.0.0 (the same result on net10.0 in 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

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.