AggregateFunction: GetDataTypeName, GetFieldType, GetSchemaTable and GetSchema("Columns") throw, and the message suggests a query that the server rejects

Open
#805 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
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
clickhouse, csharp, databases
Domain
api, database

Research direction

Start with AggregateFunctionType.cs, then trace the metadata calls in ClickHouseDataReader.cs and SchemaDescriber.cs, plus header parsing in TupleType.cs. Review the AggregateFunction type parsing and the existing FrameworkType behavior to determine how metadata should represent this column. Done means metadata calls succeed with the server's type name, value reads still throw, and the suggested merge query preserves the function name and parameters.

Written by the indexing model from the issue text.

Description

api-diagnostics bug
Describe the bug

In the HTTP client, the metadata of an AggregateFunction column cannot be read. These calls throw AggregateFunctionException, the same exception as a read of a value:

  • ClickHouseDataReader.GetDataTypeName, GetFieldType, and GetSchemaTable()
  • connection.GetSchema("Columns", ...) when a table in its scope has an AggregateFunction column. So the column list of a database, or of the whole server, fails because of one AggregatingMergeTree table.

GetName works. A query that returns a Tuple with an AggregateFunction element fails already in ExecuteReaderAsync.

The message also suggests a query that the server rejects:

  • From server 26.10, the server writes the state version of uniq in the type name (AggregateFunction(1, uniq, UInt64)), also for a column created as AggregateFunction(uniq, UInt64). The message then names AggregateFunction(1) and 1Merge().
  • For a function with parameters, the message names quantilesMerge() with no parameters.

The docs say that the values cannot be read ("AggregateFunction columns" in docs/http.mdx). This issue is about the metadata and the message. Not affected: the TCP client, whose message skips the version and keeps the parameters.

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 metadata calls describe the column with the type name of the server, and only a read of a value throws. The server reports (curl, same table as the code example):

SELECT name, type FROM system.columns WHERE database = 'default' AND table = 't_agg'
k	UInt8
u	AggregateFunction(1, uniq, UInt64)
q	AggregateFunction(quantiles(0.5, 0.9), Float64)

The ClickHouse docs: "A versioned aggregate function spells a non-zero state version out as the first argument of the type string", and "The function name may carry parameters" (Native format).

The message names a query that works. The two suggested queries fail on the server:

SELECT 1Merge(u) FROM t_agg
Code: 46. DB::Exception: Function with name `1Merge` does not exist. ... (UNKNOWN_FUNCTION)
SELECT quantilesMerge(q) FROM t_agg
Code: 42. DB::Exception: Aggregate function for calculation of multiple quantiles require at least one parameter. (NUMBER_OF_ARGUMENTS_DOESNT_MATCH)
SELECT uniqMerge(u), quantilesMerge(0.5, 0.9)(q) FROM t_agg
1	[1,1]
Code example
Reproduction program (C#, 26 lines)
#:project ../ClickHouse.Driver/ClickHouse.Driver.csproj
using ClickHouse.Driver.ADO;
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_agg (k UInt8, u AggregateFunction(uniq, UInt64), q AggregateFunction(quantiles(0.5, 0.9), Float64)) ENGINE = AggregatingMergeTree ORDER BY k");

void Try(string label, Func<object> f)
{
    try { Console.WriteLine($"{label}: {f()}"); }
    catch (Exception e) { Console.WriteLine($"{label}: {e.GetType().Name}: {e.Message}"); }
}

using (var reader = await connection.ExecuteReaderAsync("SELECT k, u, q FROM t_agg"))
{
    Try("GetName(1)", () => reader.GetName(1));
    Try("GetDataTypeName(1)", () => reader.GetDataTypeName(1));
    Try("GetDataTypeName(2)", () => reader.GetDataTypeName(2));
    Try("GetFieldType(1)", () => reader.GetFieldType(1));
    Try("GetSchemaTable()", () => reader.GetSchemaTable()!.Rows.Count);
}
Try("GetSchema(Columns, [default, t_agg])", () => connection.GetSchema("Columns", new[] { "default", "t_agg" }).Rows.Count);
Try("GetSchema(Columns, [default])", () => connection.GetSchema("Columns", new[] { "default" }).Rows.Count);
await connection.ExecuteStatementAsync("DROP TABLE t_agg");
Try("GetSchema(Columns, [default]) after DROP", () => connection.GetSchema("Columns", new[] { "default" }).Rows.Count);
Error log
GetName(1): u
GetDataTypeName(1): AggregateFunctionException: Unable to directly query column with type AggregateFunction(1). Use 1Merge() function to query this value
GetDataTypeName(2): AggregateFunctionException: Unable to directly query column with type AggregateFunction(quantiles). Use quantilesMerge() function to query this value
GetFieldType(1): AggregateFunctionException: Unable to directly query column with type AggregateFunction(1). Use 1Merge() function to query this value
GetSchemaTable(): AggregateFunctionException: Unable to directly query column with type AggregateFunction(1). Use 1Merge() function to query this value
GetSchema(Columns, [default, t_agg]): AggregateFunctionException: Unable to directly query column with type AggregateFunction(1). Use 1Merge() function to query this value
GetSchema(Columns, [default]): AggregateFunctionException: Unable to directly query column with type AggregateFunction(1). Use 1Merge() function to query this value
GetSchema(Columns, [default]) after DROP: 0
Root cause

AggregateFunctionType.ToString() and FrameworkType throw (ClickHouse.Driver/Types/AggregateFunctionType.cs:23 and :14). GetDataTypeName (ClickHouse.Driver/ADO/Readers/ClickHouseDataReader.cs:254), GetSchemaTable() (ClickHouse.Driver/Utility/SchemaDescriber.cs:47) and GetSchema("Columns") (SchemaDescriber.cs:132) call them, and TupleType reads FrameworkType when it parses the header (ClickHouse.Driver/Types/TupleType.cs:51). Parse keeps only the value of the first argument as the function (AggregateFunctionType.cs:18): for a versioned state that is the version, and the parameters are lost.

The fix needs a decision: which .NET type GetFieldType and the DataType of the schema rows give for such a column, because FrameworkType throws on purpose (ClickHouse.Driver/ADO/Readers/ColumnSlotFactory.cs:84). Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related

#804 (open): SimpleAggregateFunction drops the function parameters from the type name.
#506 (open): builds AggregateFunctionType from the binary type encoding with the function name only.

Configuration
Environment
  • Client version: 1.7.0 (main at c58178ed)
  • .NET version: .NET SDK 11.0.100, runtime .NET 11.0.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: API & diagnostics (ADO.NET metadata)


Found by automated analysis (Polyglot AI) while working on clickhouse-cs#804. 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.