From 64f02b4e09fc90fc2c5713cdc4fb270274a0c1b3 Mon Sep 17 00:00:00 2001 From: Alex Soffronow Pagonidis <237136924+alex-clickhouse@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:51:18 +0000 Subject: [PATCH 1/4] Report the driver version in the native handshake and ClientInfo The TCP client reported client version 0.0.0 in its Hello and in the ClientInfo of every query: the version fields were never set, and the patch was written as a literal 0. It also sends query_kind InitialQuery, so the server does not fill in its own version. A server refuses to send a distributed query on when the initiator version is 0.0.0, so clusterAllReplicas, remote() and Distributed tables failed with "Sending a distributed query with unknown (zero) client version". On ClickHouse Cloud this broke every system.query_log lookup in the TCP suite's cancellation tests. The client now reports this package's version (major, minor, patch) from the assembly's informational version, as clickhouse-go reports its own. A build with no usable version, or with 0.0.0 (benchmark builds), reports 1.0.0. Co-Authored-By: Claude Opus 5.5 --- .../ClientVersionIntegrationTests.cs | 78 +++++++++++++++++++ .../Protocol/ClientVersionTests.cs | 40 ++++++++++ .../Protocol/ClientHandshakeParameters.cs | 16 ++-- ClickHouse.Driver.Tcp/Protocol/ClientInfo.cs | 2 +- .../Protocol/ClientMetadata.cs | 8 +- .../Protocol/ClientVersion.cs | 47 +++++++++++ changelog.d/tcp-client-version.fixes.md | 1 + 7 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs create mode 100644 ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs create mode 100644 ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs create mode 100644 changelog.d/tcp-client-version.fixes.md diff --git a/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs b/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs new file mode 100644 index 000000000..cedcbc3eb --- /dev/null +++ b/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs @@ -0,0 +1,78 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using ClickHouse.Driver.Tcp.Protocol; +using ClickHouse.Driver.Tcp.Tests.Utilities; + +namespace ClickHouse.Driver.Tcp.Tests.Integration; + +/// +/// The client version the driver reports in its handshake and in the ClientInfo of every query. A server refuses to +/// send a distributed query on when the client that started it reports version 0.0.0, so a query that reaches another +/// replica over a connection of its own is the test that matters. +/// +[TestFixture] +[Category("Integration")] +[Category("Cloud")] +public class ClientVersionIntegrationTests +{ + private static readonly CancellationToken None = CancellationToken.None; + + [Test] + public async Task ExecuteScalarAsync_QueryThatReadsEveryCloudReplica_Succeeds() + { + if (!TcpServerFixture.IsCloud) + { + Assert.Ignore("Only a Cloud service has replicas that a query reaches over connections of their own."); + } + + await using ClickHouseTcpClient client = TcpServerFixture.CreateClient(); + + object replicas = await client.ExecuteScalarAsync( + "SELECT count() FROM clusterAllReplicas(default, system.one)", + cancellationToken: None); + + Assert.That(replicas, Is.GreaterThanOrEqualTo(1UL)); + } + + [Test] + public async Task ExecuteScalarAsync_RemoteQueryToTheSameServer_Succeeds() + { + if (TcpServerFixture.IsCloud) + { + Assert.Ignore("A Cloud service does not accept remote() to its own local port."); + } + + await using ClickHouseTcpClient client = TcpServerFixture.CreateClient(); + var parameters = new ClickHouseTcpParameterCollection + { + new ClickHouseTcpParameter("user", TcpServerFixture.Username), + new ClickHouseTcpParameter("password", TcpServerFixture.Password), + }; + + // prefer_localhost_replica = 0 makes the server open a connection to itself instead of reading locally, so + // the query takes the same path as a query to another replica. + object rows = await client.ExecuteScalarAsync( + "SELECT count() FROM remote('localhost:9000', system.one, {user:String}, {password:String}) SETTINGS prefer_localhost_replica = 0", + new ClickHouseTcpQueryOptions { Parameters = parameters }, + None); + + Assert.That(rows, Is.EqualTo(1UL)); + } + + [Test] + public async Task ExecuteAsync_AnyQuery_RecordsTheDriverVersionAsTheClientVersion() + { + await using ClickHouseTcpClient client = TcpServerFixture.CreateClient(); + string queryId = Guid.NewGuid().ToString(); + + await client.ExecuteAsync("SELECT 1", new ClickHouseTcpQueryOptions { QueryId = queryId }, None); + + object reported = await QueryLog.ScalarAsync( + client, + $"SELECT format('{{}}.{{}}.{{}}', client_version_major, client_version_minor, client_version_patch) FROM {QueryLog.Table} WHERE query_id = '{queryId}' AND type = 'QueryFinish'"); + + (int major, int minor, int patch) = ClientVersion.Current; + Assert.That(reported, Is.EqualTo($"{major}.{minor}.{patch}")); + } +} diff --git a/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs b/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs new file mode 100644 index 000000000..bfb83c8fb --- /dev/null +++ b/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs @@ -0,0 +1,40 @@ +using ClickHouse.Driver.Tcp.Protocol; + +namespace ClickHouse.Driver.Tcp.Tests.Protocol; + +[TestFixture] +public class ClientVersionTests +{ + [TestCase("1.5.0+3f2a1c9", 1, 5, 0)] + [TestCase("1.4.0-rc2+3f2a1c9", 1, 4, 0)] + [TestCase("2.10.3", 2, 10, 3)] + [TestCase("1.5", 1, 5, 0)] + public void Parse_InformationalVersion_ReadsMajorMinorAndPatch(string informationalVersion, int major, int minor, int patch) + { + Assert.That(ClientVersion.Parse(informationalVersion), Is.EqualTo((major, minor, patch))); + } + + // 0.0.0 is what a benchmark build carries, and it is the one version a server refuses for a distributed query. + [TestCase("0.0.0-baseline")] + [TestCase("0.0.0-pr.724+3f2a1c9")] + [TestCase("not a version")] + [TestCase("1")] + [TestCase("")] + [TestCase(null)] + public void Parse_NoUsableVersion_ReturnsTheFallback(string informationalVersion) + { + Assert.That(ClientVersion.Parse(informationalVersion), Is.EqualTo(ClientVersion.Fallback)); + } + + [Test] + public void ClientHandshakeParameters_Defaults_ReportTheDriverVersion() + { + var parameters = new ClientHandshakeParameters { Username = "default" }; + + Assert.Multiple(() => + { + Assert.That((parameters.VersionMajor, parameters.VersionMinor, parameters.VersionPatch), Is.EqualTo(ClientVersion.Current)); + Assert.That(ClientVersion.Current, Is.Not.EqualTo((0, 0, 0))); + }); + } +} diff --git a/ClickHouse.Driver.Tcp/Protocol/ClientHandshakeParameters.cs b/ClickHouse.Driver.Tcp/Protocol/ClientHandshakeParameters.cs index e1e36decc..12677ae16 100644 --- a/ClickHouse.Driver.Tcp/Protocol/ClientHandshakeParameters.cs +++ b/ClickHouse.Driver.Tcp/Protocol/ClientHandshakeParameters.cs @@ -16,11 +16,17 @@ internal sealed class ClientHandshakeParameters /// affect negotiation. public string ClientName => DefaultClientName; - /// Informational client major version (ClientHello field 2). - public int VersionMajor { get; init; } + /// Client major version (ClientHello field 2). Defaults to this driver's version. + public int VersionMajor { get; init; } = ClientVersion.Current.Major; - /// Informational client minor version (ClientHello field 3). - public int VersionMinor { get; init; } + /// Client minor version (ClientHello field 3). Defaults to this driver's version. + public int VersionMinor { get; init; } = ClientVersion.Current.Minor; + + /// + /// Client patch version. ClientHello has no field for it; every query's ClientInfo carries it. Defaults to this + /// driver's version. + /// + public int VersionPatch { get; init; } = ClientVersion.Current.Patch; /// Default database (ClientHello field 5). public string Database { get; init; } = "default"; @@ -38,5 +44,5 @@ internal sealed class ClientHandshakeParameters public override string ToString() => string.Create( CultureInfo.InvariantCulture, - $"{nameof(ClientHandshakeParameters)} {{ ClientName = {ClientName}, Version = {VersionMajor}.{VersionMinor}, Database = {Database}, Username = {Username}, Password = , QuotaKey = }}"); + $"{nameof(ClientHandshakeParameters)} {{ ClientName = {ClientName}, Version = {VersionMajor}.{VersionMinor}.{VersionPatch}, Database = {Database}, Username = {Username}, Password = , QuotaKey = }}"); } diff --git a/ClickHouse.Driver.Tcp/Protocol/ClientInfo.cs b/ClickHouse.Driver.Tcp/Protocol/ClientInfo.cs index a2cb29209..657784101 100644 --- a/ClickHouse.Driver.Tcp/Protocol/ClientInfo.cs +++ b/ClickHouse.Driver.Tcp/Protocol/ClientInfo.cs @@ -54,7 +54,7 @@ public static void Write(ClickHouseBinaryWriter writer, NegotiatedProtocol negot if (negotiated.Supports(ProtocolFeature.VersionPatch)) { - writer.WriteVarUInt(0); // client_version_patch + writer.WriteVarUInt((ulong)client.VersionPatch); } if (negotiated.Supports(ProtocolFeature.OpenTelemetry)) diff --git a/ClickHouse.Driver.Tcp/Protocol/ClientMetadata.cs b/ClickHouse.Driver.Tcp/Protocol/ClientMetadata.cs index 2e63be9b1..56fa078b8 100644 --- a/ClickHouse.Driver.Tcp/Protocol/ClientMetadata.cs +++ b/ClickHouse.Driver.Tcp/Protocol/ClientMetadata.cs @@ -12,12 +12,15 @@ internal sealed class ClientMetadata /// Informational client identifier, pinned to the same value the handshake reports. public string ClientName => ClientHandshakeParameters.DefaultClientName; - /// Informational client major version. + /// Client major version, as the handshake reported it. public int VersionMajor { get; init; } - /// Informational client minor version. + /// Client minor version, as the handshake reported it. public int VersionMinor { get; init; } + /// Client patch version. + public int VersionPatch { get; init; } + /// Username, echoed as the initiating user in ClientInfo. required public string Username { get; init; } @@ -31,6 +34,7 @@ internal sealed class ClientMetadata { VersionMajor = parameters.VersionMajor, VersionMinor = parameters.VersionMinor, + VersionPatch = parameters.VersionPatch, Username = parameters.Username, QuotaKey = parameters.QuotaKey, }; diff --git a/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs b/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs new file mode 100644 index 000000000..823cad8ea --- /dev/null +++ b/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs @@ -0,0 +1,47 @@ +using System; +using System.Reflection; + +namespace ClickHouse.Driver.Tcp.Protocol; + +/// +/// The client version the driver reports in its handshake and in the ClientInfo of every query: the version of this +/// package, as major, minor and patch. +/// +/// +/// A server refuses to send a distributed query (clusterAllReplicas, remote, a Distributed +/// table) on to another server when the client that started it reports 0.0.0. So a build whose version is missing, +/// cannot be read, or is 0.0.0 reports instead. +/// +internal static class ClientVersion +{ + /// The version reported when the build carries no usable one. + internal static readonly (int Major, int Minor, int Patch) Fallback = (1, 0, 0); + + /// The version of this build, read once from its informational version. + internal static (int Major, int Minor, int Patch) Current { get; } = Parse( + typeof(ClientVersion).Assembly.GetCustomAttribute()?.InformationalVersion); + + /// Reads major, minor and patch from an informational version such as 1.5.0+3f2a1c. + /// The informational version, or null. + /// The version, or when there is no usable one. + internal static (int Major, int Minor, int Patch) Parse(string informationalVersion) + { + if (string.IsNullOrEmpty(informationalVersion)) + { + return Fallback; + } + + // The build metadata after '+' and a pre-release label after '-' are not part of the number. + int end = informationalVersion.AsSpan().IndexOfAny('+', '-'); + ReadOnlySpan number = end < 0 ? informationalVersion : informationalVersion.AsSpan(0, end); + if (!Version.TryParse(number, out Version parsed)) + { + return Fallback; + } + + int patch = Math.Max(parsed.Build, 0); + return parsed.Major == 0 && parsed.Minor == 0 && patch == 0 + ? Fallback + : (parsed.Major, parsed.Minor, patch); + } +} diff --git a/changelog.d/tcp-client-version.fixes.md b/changelog.d/tcp-client-version.fixes.md new file mode 100644 index 000000000..a0acd86ee --- /dev/null +++ b/changelog.d/tcp-client-version.fixes.md @@ -0,0 +1 @@ +* Native TCP client: fixed distributed queries (`clusterAllReplicas`, `remote`, a `Distributed` table) failing with "Sending a distributed query with unknown (zero) client version". The client reported version 0.0.0 in the handshake and in every query; it now reports the driver version. From 22c06a297fabf0c11d123a2876525ef221b37da5 Mon Sep 17 00:00:00 2001 From: Alex Soffronow Pagonidis <237136924+alex-clickhouse@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:58:41 +0000 Subject: [PATCH 2/4] Name the changelog fragment after the pull request Co-Authored-By: Claude Opus 5.5 --- ...cp-client-version.fixes.md => 725-tcp-client-version.fixes.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog.d/{tcp-client-version.fixes.md => 725-tcp-client-version.fixes.md} (100%) diff --git a/changelog.d/tcp-client-version.fixes.md b/changelog.d/725-tcp-client-version.fixes.md similarity index 100% rename from changelog.d/tcp-client-version.fixes.md rename to changelog.d/725-tcp-client-version.fixes.md From 1b0c0a5a4c991d90f8b281d34492cee3387b3b8f Mon Sep 17 00:00:00 2001 From: Alex Soffronow Pagonidis <237136924+alex-clickhouse@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:02:40 +0000 Subject: [PATCH 3/4] Test the version bytes in ClientInfo, and do not assume port 9000 - A wire test writes ClientInfo with version 12.34.56 and reads the three fields back. A dev build reports 1.0.0, so a dropped minor or patch did not show in the other tests. - ClientVersion.Fallback is a property, so Current does not depend on the order of the static fields. - The remote() test asks the server for its native port. - The changelog entry names the server versions that refuse 0.0.0. Co-Authored-By: Claude Opus 5.5 --- .../ClientVersionIntegrationTests.cs | 7 +- .../Protocol/ClientVersionTests.cs | 70 +++++++++++++++++++ .../Protocol/ClientVersion.cs | 3 +- changelog.d/725-tcp-client-version.fixes.md | 2 +- 4 files changed, 79 insertions(+), 3 deletions(-) diff --git a/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs b/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs index cedcbc3eb..046e84aab 100644 --- a/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs +++ b/ClickHouse.Driver.Tcp.Tests/Integration/ClientVersionIntegrationTests.cs @@ -44,8 +44,13 @@ public async Task ExecuteScalarAsync_RemoteQueryToTheSameServer_Succeeds() } await using ClickHouseTcpClient client = TcpServerFixture.CreateClient(); + + // The server's own plaintext native port, which is not the port the suite dialled when a proxy or TLS is in + // between. + object port = await client.ExecuteScalarAsync("SELECT tcpPort()", cancellationToken: None); var parameters = new ClickHouseTcpParameterCollection { + new ClickHouseTcpParameter("address", $"localhost:{port}"), new ClickHouseTcpParameter("user", TcpServerFixture.Username), new ClickHouseTcpParameter("password", TcpServerFixture.Password), }; @@ -53,7 +58,7 @@ public async Task ExecuteScalarAsync_RemoteQueryToTheSameServer_Succeeds() // prefer_localhost_replica = 0 makes the server open a connection to itself instead of reading locally, so // the query takes the same path as a query to another replica. object rows = await client.ExecuteScalarAsync( - "SELECT count() FROM remote('localhost:9000', system.one, {user:String}, {password:String}) SETTINGS prefer_localhost_replica = 0", + "SELECT count() FROM remote({address:String}, system.one, {user:String}, {password:String}) SETTINGS prefer_localhost_replica = 0", new ClickHouseTcpQueryOptions { Parameters = parameters }, None); diff --git a/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs b/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs index bfb83c8fb..fcbbdcef3 100644 --- a/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs +++ b/ClickHouse.Driver.Tcp.Tests/Protocol/ClientVersionTests.cs @@ -1,3 +1,8 @@ +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; using ClickHouse.Driver.Tcp.Protocol; namespace ClickHouse.Driver.Tcp.Tests.Protocol; @@ -26,6 +31,39 @@ public void Parse_NoUsableVersion_ReturnsTheFallback(string informationalVersion Assert.That(ClientVersion.Parse(informationalVersion), Is.EqualTo(ClientVersion.Fallback)); } + [Test] + public async Task ClientInfoWrite_ClientVersion_WritesMajorMinorAndPatch() + { + // Distinct values, so a field written as a constant or in the wrong slot shows. A dev build reports 1.0.0, + // whose zero minor and patch cannot show that. + var client = new ClientMetadata { Username = "default", VersionMajor = 12, VersionMinor = 34, VersionPatch = 56 }; + using var ms = new MemoryStream(); + using (var writer = new ClickHouseBinaryWriter(ms)) + { + ClientInfo.Write(writer, new NegotiatedProtocol(NegotiatedProtocol.ClientTcpProtocolVersion), client); + await writer.FlushAsync(CancellationToken.None); + } + + var wire = new WireCursor(ms.ToArray()); + Assert.That(wire.Byte(), Is.EqualTo(1), "query_kind"); + wire.String(); // initial_user + wire.String(); // initial_query_id + wire.String(); // initial_address + wire.Skip(sizeof(long)); // initial_query_start_time_microseconds + Assert.That(wire.Byte(), Is.EqualTo(1), "interface"); + wire.String(); // os_user + wire.String(); // client_hostname + Assert.That(wire.String(), Is.EqualTo(ClientHandshakeParameters.DefaultClientName)); + ulong major = wire.VarUInt(); + ulong minor = wire.VarUInt(); + wire.VarUInt(); // client_tcp_protocol_version + wire.String(); // quota_key + wire.VarUInt(); // distributed_depth + ulong patch = wire.VarUInt(); + + Assert.That((major, minor, patch), Is.EqualTo((12UL, 34UL, 56UL))); + } + [Test] public void ClientHandshakeParameters_Defaults_ReportTheDriverVersion() { @@ -37,4 +75,36 @@ public void ClientHandshakeParameters_Defaults_ReportTheDriverVersion() Assert.That(ClientVersion.Current, Is.Not.EqualTo((0, 0, 0))); }); } + + // Reads the native protocol's primitive encodings from a byte array, in order. + private sealed class WireCursor(byte[] bytes) + { + private int position; + + internal byte Byte() => bytes[position++]; + + internal void Skip(int count) => position += count; + + internal ulong VarUInt() + { + ulong value = 0; + for (int shift = 0; ; shift += 7) + { + byte b = bytes[position++]; + value |= (ulong)(b & 0x7F) << shift; + if ((b & 0x80) == 0) + { + return value; + } + } + } + + internal string String() + { + int length = checked((int)VarUInt()); + string value = Encoding.UTF8.GetString(bytes, position, length); + position += length; + return value; + } + } } diff --git a/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs b/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs index 823cad8ea..acd794902 100644 --- a/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs +++ b/ClickHouse.Driver.Tcp/Protocol/ClientVersion.cs @@ -15,7 +15,8 @@ namespace ClickHouse.Driver.Tcp.Protocol; internal static class ClientVersion { /// The version reported when the build carries no usable one. - internal static readonly (int Major, int Minor, int Patch) Fallback = (1, 0, 0); + /// A property rather than a field, so can read it whatever the field order. + internal static (int Major, int Minor, int Patch) Fallback => (1, 0, 0); /// The version of this build, read once from its informational version. internal static (int Major, int Minor, int Patch) Current { get; } = Parse( diff --git a/changelog.d/725-tcp-client-version.fixes.md b/changelog.d/725-tcp-client-version.fixes.md index a0acd86ee..88284c415 100644 --- a/changelog.d/725-tcp-client-version.fixes.md +++ b/changelog.d/725-tcp-client-version.fixes.md @@ -1 +1 @@ -* Native TCP client: fixed distributed queries (`clusterAllReplicas`, `remote`, a `Distributed` table) failing with "Sending a distributed query with unknown (zero) client version". The client reported version 0.0.0 in the handshake and in every query; it now reports the driver version. +* Native TCP client: fixed distributed queries (`clusterAllReplicas`, `remote`, a `Distributed` table) failing on ClickHouse 26.8 and later with "Sending a distributed query with unknown (zero) client version". The client reported version 0.0.0; it now reports the driver version, which `system.query_log` and `system.processes` also show. From 04bc611a48c671c1df64a213294a0189ed63e528 Mon Sep 17 00:00:00 2001 From: Alex Soffronow Pagonidis <237136924+alex-clickhouse@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:40:36 +0000 Subject: [PATCH 4/4] Document the client name and version that the server records The TCP page now says which client name and version ClickHouse shows in system.query_log and system.processes, where the version comes from, and when the client reports 1.0.0. Co-Authored-By: Claude Opus 5.5 --- docs/tcp.mdx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/tcp.mdx b/docs/tcp.mdx index 1240c97a4..2dfd73163 100644 --- a/docs/tcp.mdx +++ b/docs/tcp.mdx @@ -1514,6 +1514,30 @@ The log block is borrowed and is released when the callback returns. Process it copy out values that must be retained. Keep callbacks fast and do not throw; a callback exception terminates the operation and its connection. +### Client name and version on the server {#logging-client-identity} + +The client sends its name and version in the handshake and with every query. ClickHouse shows them +in `system.query_log` and `system.processes`: + +| Column | Value | +|---|---| +| `client_name` | `clickhouse-cs-tcp` | +| `client_version_major`, `client_version_minor`, `client_version_patch` | The version of the `ClickHouse.Driver` package, for example `1.4.0` | + +Use these columns to find the queries that this client sent: + +```sql +SELECT event_time, client_version_major, client_version_minor, client_version_patch, query +FROM system.query_log +WHERE client_name = 'clickhouse-cs-tcp' AND type = 'QueryFinish' +ORDER BY event_time DESC +LIMIT 10 +``` + +When the build has no usable version (the version is missing, cannot be read, or is 0.0.0), the +client reports `1.0.0`. ClickHouse 26.8 and later do not send a distributed query (`remote`, +`clusterAllReplicas`, a `Distributed` table) to other servers when the client version is 0.0.0. + ## OpenTelemetry {#opentelemetry} The TCP client emits distributed-tracing spans through .NET's `System.Diagnostics.Activity` API.