clickhouse-cs / ClickHouse.Driver.Tcp
AGENTS.md102 starsChanged 2 months agoMITView on GitHub
These rules add to the root AGENTS.md. They apply to ClickHouse.Driver.Tcp/ and ClickHouse.Driver.Tcp.Tests/. The read path uses a linear ReadBuffer (Protocol/ReadBuffer.cs, which documents its design). A ring buffer was considered and rejected: this access pattern rarely pays the compaction cost that a ring buffer removes, and a ring buffer adds a scratch copy on every read that crosses the end, split reads from the socket, and more edge cases.
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 35,117 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository12 other files it gives its agents.
- AGENTS.mdAGENTS.md
- ClickHouse.Driver/AGENTS.mdAGENTS.md
- examples/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ClickHouse.Driver/CLAUDE.mdCLAUDE.md
- ClickHouse.Driver.Tcp/CLAUDE.mdCLAUDE.md
- examples/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- reviewSkill
- issue-classificationSkill
- issue-prioritySkill
- reviewSkill
The file
We split this AGENTS.md into the rules it gives an agent
9 rules. About 0 are common, 2 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 44
Only ClickHouse-native {name:Type} placeholders are supported.
- Very specific: line 48
Every version-conditional read or write asks NegotiatedProtocol.Supports(ProtocolFeature.X) (Protocol/).
- Distinctive: line 53
When you add or change a type, consider every path that touches it:
- Very specific: line 65
Prefer pooled buffers, Span/Memory, and zero-copy access.
- Distinctive: line 118
Do not set it to a version other than that of the server you point the suite at.
- Very specific: line 127
Read the summary of TcpServerFixture before you write a test that runs on Cloud.
- Very specific: line 153
Add a bare T case *and* a Nullable(T) case, with nulls between present values, for every type that Nullable accepts.
- Very specific: line 190
Only a non-zero start shows that the codec rebases its child offsets, run starts and element ranges.
- Very specific: line 201
Put the slice after earlier values of the same type, so the expected child offset is not 0.
≈ “Common” is an estimate: at least 1 in 5 of the 45,344 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Native TCP client (ClickHouse.Driver.Tcp)
These rules add to the root AGENTS.md. They apply to ClickHouse.Driver.Tcp/ and
ClickHouse.Driver.Tcp.Tests/.
Project Context
- Primary API:
ClickHouseTcpClient. It speaks the ClickHouse native protocol, and data moves as columnar Native-format blocks, not as RowBinary rows. - Experimental: the entry points (client, data source, session, operations interfaces, and the
dependency-injection extensions) carry
[Experimental("CHTCP0001")]. A new entry point of that kind carries it too. The test, benchmark and examples projects suppress the warning. - Targets:
net8.0,net9.0,net10.0. There is nonet6.0build. - Standalone: this project references
ClickHouse.Driver.Commononly, neverClickHouse.Driver.ClickHouse.Driverpacks this assembly into its NuGet package. - Reference sources: the ClickHouse server source is the authority for wire behavior. clickhouse-go already implements the native protocol and is a useful second reference.
Structure
ClickHouse.Driver.Tcp/
├── Client/ # ClickHouseTcpClient, options, data source, sessions, connection pool
├── Protocol/ # Connection, handshake, packets, ReadBuffer, binary reader/writer
├── Format/ # Native-format blocks: BlockReader, BlockWriter
├── Types/ # Column types; Types/Codecs/ has one codec per ClickHouse type
├── Poco/ # POCO read/write mapping
├── Parameters/ # Query parameter type inference and formatting
├── Compression/ # Compressed frames (the codecs are in ClickHouse.Driver.Common)
├── Diagnostic/, Logging/ # OpenTelemetry and ILogger integration
├── DependencyInjection/ # IServiceCollection extensions
├── Exceptions/, Numerics/
└── PublicAPI/ # Public API files, checked by an analyzerDevelopment Guidelines
Configuration & Parameters
- Client configuration:
ClickHouseTcpClientOptions, or a connection string throughClickHouseTcpConnectionStringBuilder. - Per-call options:
ClickHouseTcpQueryOptionsandClickHouseTcpInsertOptions. - Parameters:
ClickHouseTcpParameterCollection, passed inClickHouseTcpQueryOptions.Parameters. Only ClickHouse-native{name:Type}placeholders are supported. The client does not rewrite ADO.NET-style@nameplaceholders.
Protocol versions
- Every version-conditional read or write asks
NegotiatedProtocol.Supports(ProtocolFeature.X)(Protocol/). Do not compare against an inline revision number.ProtocolFeaturemirrors the server'sDBMS_MIN_REVISION_WITH_*constants; add a member there for a new version-gated field.
Types
When you add or change a type, consider every path that touches it:
- the codec's read (
ReadColumnAsync) and write (WriteColumn), including the dense write path that re-sends a column read from the server; Nullable(T): its null map and the codec'sNullPlaceholder;- POCO mapping in
Poco/; - parameter formatting in
Parameters/TcpParameterFormatter.cs. It duplicates the HTTP client'sHttpParameterFormatter, because the two assemblies cannot share a type tree. Keep the shared cases aligned.
Performance
This library is highly performance-sensitive. Minimize allocations and copies, most of all on the
hot paths: wire reading and writing (Protocol/, Format/) and column (de)serialization (Types/).
Prefer pooled buffers, Span/Memory, and zero-copy access. Do not bulk-copy an array where a
better data structure avoids it.
The read path uses a linear ReadBuffer (Protocol/ReadBuffer.cs, which documents its design). A
ring buffer was considered and rejected: this access pattern rarely pays the compaction cost that a
ring buffer removes, and a ring buffer adds a scratch copy on every read that crosses the end, split
reads from the socket, and more edge cases.
Public API surface
- Default new types to
internal, especially wire plumbing (buffers, readers/writers, packet/block encoders, connection internals). Make somethingpubliconly when it is part of the user-facing contract: the client, options, exceptions, and the CLR value types users receive (such asInt256/UInt256). It is much easier to widen accessibility later than to take back a public API. - The test project sees internals through
InternalsVisibleToinAssemblyInfo.cs. The assembly is strong-named, so the declaration carries the full public key. Microsoft.CodeAnalysis.PublicApiAnalyzerschecks the surface. Add each new public symbol toPublicAPI/PublicAPI.Unshipped.txt..editorconfigmakes RS0016, RS0017, RS0024 and RS0048 errors, but--property WarningLevel=0hides them. To check the surface, build without it:dotnet build ClickHouse.Driver.Tcp --framework net9.0 2>&1 | grep -E '(error|warning) RS[0-9]+'- The analyzer reads exactly one
PublicAPI.Shipped.txt+PublicAPI.Unshipped.txtpair. Do not add a second pair (for example a per-framework folder): the checks then stop without an error.
Testing
Running tests
dotnet test ClickHouse.Driver.Tcp.Tests --framework net9.0 --property WarningLevel=0Tests outside the Integration namespace need no server. Integration/TcpServerFixture.cs finds a
server for the integration tests, in this order:
CLICKHOUSE_TCP_CONNECTION: a whole native connection string (TLS options included).CLICKHOUSE_TCP_HOST, withCLICKHOUSE_TCP_PORT(default 9000),CLICKHOUSE_TCP_USER(defaultdefault) andCLICKHOUSE_TCP_PASSWORD(defaultclickhouse). A local server usually has an empty password:CLICKHOUSE_TCP_HOST=localhost CLICKHOUSE_TCP_PASSWORD="" dotnet test ClickHouse.Driver.Tcp.Tests --framework net9.0 --property WarningLevel=0- A Testcontainers container, which needs Docker.
CLICKHOUSE_VERSIONselects the image tag.
CLICKHOUSE_VERSION is also the version the version gates use (Utilities/TcpServerFeatures.cs).
The suite asks the server for its version only when the variable does not hold a version number.
Do not set it to a version other than that of the server you point the suite at.
ClickHouse Cloud
CLICKHOUSE_TEST_ENVIRONMENT=cloudmarks a Cloud run (TcpServerFixture.IsCloud). Without it,SkipIfCloudLocksASettingdoes nothing.- The Cloud workflow runs only fixtures marked
[Category("Cloud")]. The standard matrix runs them too. The category is for behavior that a Cloud service changes (a load balancer, several replicas, TLS, locked settings), not for coverage. Mark a new fixture with it if it tests such behavior. - Read the summary of
TcpServerFixturebefore you write a test that runs on Cloud.Memorytables are local to one replica, so some test shapes read an empty table there. - Cloud refuses some query settings. Call
TcpServerFixture.SkipIfCloudLocksASettingfor a test that sets them. The refusal also ends the connection, so the tests after it fail withObjectDisposedException. Look at the first failure.
Suite conventions
- Table names: give each test its own name with a
Guidsuffix, as the fixtures'UniqueTableName()helpers do ($"tcp_insert_test_{Guid.NewGuid():N}"), and drop it in afinally. - Server version gating:
[RequiresServerFeature(TcpFeature.X)].TcpFeaturemirrors the HTTP client'sClickHouse.Driver.ADO.Feature; keep the shared versions aligned. system.query_log:Utilities/QueryLog.cs(QueryLog.ScalarAsync). In the lookup SQL, writeQueryLog.Tablein place ofsystem.query_log. Each replica keeps its own log, so on a server with a cluster (Cloud and the fixture's container) it reads every replica.
Codec test layering
Per-type coverage is split between two layers. A test in the wrong layer passes while it proves nothing the other layer did not already prove.
InsertRoundTripCase owns per-type values. Adding a type means adding cases to
InsertRoundTripCase.Cases(): each creates a one-column table, inserts the column, selects it back,
and compares. That exercises the real WriteColumn → server → ReadColumnAsync path, so it
subsumes any unit test that writes an ergonomic column and reads it back.
Add a bare T case and a Nullable(T) case, with nulls between present values, for every type
that Nullable accepts. Nullable has its own write path (the null map plus a per-type placeholder)
that the bare type never exercises. If the type needs an enabling setting or a non-default
placeholder, the Nullable case is where that gets proven.
A codec unit test should only assert what no server round-trip can reach:
- exact wire bytes: encodings, offsets, discriminators, dictionary key widths, state prefixes;
- error paths: type-resolution
FormatException/NotSupportedException, and exceptions thrown by a directWriteColumncall; - API surface:
CanWrite(especially rejections),ElementType,WritableElementTypes,NullPlaceholderAs,TypeName, field/label metadata; internalcolumn shapes no integration case constructs;- zero-row columns, bounds behavior past
RowCount, andDispose/pooling; - anything the server normalizes away, so a round-trip could not observe it (duplicate map keys, client-side dictionary construction, which precision or key width the client chose).
Two subtleties decide whether an accessor needs unit coverage:
.Valuessometimes needs it, sometimes not. It depends on whetherGetValuedelegates to it. ForArrayColumn<T>,GetValue(row) => Values[row], so the per-row comparison in integration already covers it. For theNullable,LowCardinality,Map,Nested, andTuplecolumns,GetValuereads its own state directly, andValuesis a separate pooled cache that integration never touches. Cover those.- Dense write paths are covered by integration.
InsertAsync_DenseReadbackReinserted_RoundTripsThroughSelectre-inserts the dense read-back of every case, so a unit test for "the dense column writes without rebuilding" is redundant unless it builds a shape the server cannot produce.
VariantColumnCodecTests is the reference: no RoundTripAsync calls at all, wire bytes and error
paths only.
Slices: always test a non-zero start
An insert larger than maxRowsPerBlock (default ClickHouseTcpConnection.DefaultMaxRowsPerBlock)
goes out as several blocks, so the codec writes a slice. A slice at start = 0 does not exercise
the slice logic: every child run starts at offset 0, so a codec that ignores start still passes.
Only a non-zero start shows that the codec rebases its child offsets, run starts and element
ranges.
- Ergonomic write path:
InsertSlicingIntegrationTestsinserts everyInsertRoundTripCaseone row per block, so a new case gets this coverage. - Dense write path: it computes the slice differently, and no matrix test slices it. A codec with
its own dense path needs an integration test that inserts with
maxRowsPerBlockless than the row count and reads back every row. FollowInsertAsync_DenseVariantColumnSplitAcrossBlocks_RoundTripsEveryRow. Add anidcolumn to pin the read-back order; theMemoryengine does not guarantee it across blocks. - Codec unit test: for each write path, write a slice with
start > 0and assert the exact bytes. Put the slice after earlier values of the same type, so the expected child offset is not 0. FollowVariantColumnCodecTests.WriteColumn_DenseColumnSliceAfterEarlierValues_StartsEachRunAtItsSliceOffset.
Documentation
User-facing behavior of this client is documented in docs/tcp.mdx. The TCP examples are in
examples/Tcp/.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

