TCP N9: POCO row insert — InsertAsync<T> over a compiled per-column gather - #559
TCP N9: POCO row insert — InsertAsync<T> over a compiled per-column gather#559alex-clickhouse wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds the TCP client’s row-oriented insert half, complementing #557’s POCO query support.
Changes:
- Adds POCO and positional
object[]insert overloads. - Compiles cached per-column gather plans with codec-aware null/type handling.
- Preserves connections across mapping failures and adds broad unit/integration coverage.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
ClickHouse.Driver.Tcp/Types/Codecs/NullableColumnCodec.cs |
Exposes lifted writable types. |
ClickHouse.Driver.Tcp/Types/ArrayColumn.cs |
Adds pooled-buffer ownership. |
ClickHouse.Driver.Tcp/Protocol/ClickHouseTcpConnection.cs |
Adds schema-driven column factories. |
ClickHouse.Driver.Tcp/Poco/PocoWritePlan.cs |
Builds compiled POCO write plans. |
ClickHouse.Driver.Tcp/Poco/PocoWriteConversion.cs |
Resolves property-to-codec conversions. |
ClickHouse.Driver.Tcp/Poco/PocoUntypedColumns.cs |
Transposes positional rows. |
ClickHouse.Driver.Tcp/Poco/PocoTypeRegistry.cs |
Caches write plans. |
ClickHouse.Driver.Tcp/Poco/PocoTypeDescriptor.cs |
Shares mapped-column descriptions. |
ClickHouse.Driver.Tcp/Poco/PocoRowBuffer.cs |
Materializes row sources. |
ClickHouse.Driver.Tcp/Poco/PocoReadPlan.cs |
Uses shared block signatures. |
ClickHouse.Driver.Tcp/Poco/PocoColumnBuilder.cs |
Compiles per-column gathers. |
ClickHouse.Driver.Tcp/Poco/PocoBlockSignature.cs |
Centralizes plan cache keys. |
ClickHouse.Driver.Tcp/Client/IClickHouseTcpClient.cs |
Adds row-insert contracts. |
ClickHouse.Driver.Tcp/Client/ClickHouseTcpClient.cs |
Implements row inserts. |
ClickHouse.Driver.Tcp.Tests/Types/NullableColumnCodecTests.cs |
Tests lifted nullable writes. |
ClickHouse.Driver.Tcp.Tests/Types/ArrayColumnTests.cs |
Tests buffer ownership. |
ClickHouse.Driver.Tcp.Tests/Protocol/ClickHouseTcpConnectionInsertTests.cs |
Tests factory lifecycle. |
ClickHouse.Driver.Tcp.Tests/Poco/PocoWritePlanTests.cs |
Tests write-plan behavior. |
ClickHouse.Driver.Tcp.Tests/Poco/PocoUntypedColumnsTests.cs |
Tests positional transposition. |
ClickHouse.Driver.Tcp.Tests/Poco/PocoRowBufferTests.cs |
Tests row materialization. |
ClickHouse.Driver.Tcp.Tests/Integration/PocoWriteIntegrationTests.cs |
Exercises real-server round trips. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| foreach (T row in source) | ||
| { |
There was a problem hiding this comment.
Correct, and fixed in aa3978d — the check is now per row rather than at the growth points, so a counted source (which rents once to fit and never grows) is no longer drained in full. The read is a field test against a token that is usually None, so it costs nothing measurable beside the source’s own MoveNext.
Added Materialize_TokenCancelledPartWayThroughACountedSource_StopsThere, which needs a source that reports its Count (so the counted path is taken) and cancels the token part-way through yielding — a plain List<T> cannot express that.
aa3978d to
62949f6
Compare
62949f6 to
4283c67
Compare
4283c67 to
3358c02
Compare
3358c02 to
2a81c80
Compare
906b78b to
6ef885e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
6ef885e to
812c488
Compare
The write half of Branch 2 (N9), stacked on the read half. A row-oriented insert transposes the caller's rows into the columnar insert the connection already sends, in both shapes: `InsertAsync<T>(IEnumerable<T>)` gathering each property into the buffer its target column is written from, and `InsertAsync(IEnumerable<object[]>)` matching values to targets by position. The gather is the mirror of the read scatter: one compiled loop per target column, no boxing and no per-row delegate hop. It needs no conversion layer of its own — a codec already accepts the calendar types on write, so the only conversions left are the CLR-level ones a cast would do (nullable lift, enum ordinal, reference upcast). Numeric widening is declined in both directions, so every shape that inserts also reads back. Whether a row may have no value for a column is the codec's question, not the CLR's: `NullPlaceholder is null` is true exactly for the types with a NULL of their own, so a null `string` property into a plain `String` column is reported before anything is sent, naming the row, rather than faulting inside the codec part-way through a block and taking the connection with it. The target types arrive in the server's sample block, after the statement has gone out, so the connection gains an InsertColumnFactory seam that builds the columns there and owns them afterwards. A factory that throws — a mapping error, which is the caller's shape rather than the connection's — closes the row stream with no rows and reports once the connection is back to Ready, exactly as a schema mismatch does. Also lifts `Nullable`'s WritableElementTypes to its nullable surface. CanWrite has always accepted `DateTime?` for a `Nullable(DateTime)` column, but the list reported only the canonical `uint?`, which a plan choosing a write type from the list rather than probing with a column cannot see. Co-Authored-By: Claude <noreply@anthropic.com>
The check sat at the buffer's growth points, which a counted source never reaches — it rents once to fit — so a long `List<T>` was drained in full whatever the token said. Tested per row instead, next to the null-row check: the read is a field test against a token that is usually None, so it costs nothing measurable beside the source's own MoveNext. Co-Authored-By: Claude <noreply@anthropic.com>
The corpus insert test knows a Nested target cannot be gathered from rows and asserts the refusal instead of a successful insert. It recognised the shape with StartsWith, so it only caught a top-level Nested -- and the corpus also has Array(Nested(a UInt8)), Tuple(Nested(a UInt8), String) and Nested in both Map positions. Those four expected an insert that cannot work, and failed on every framework and every server version. Contains, not StartsWith: a composite can only hand its child the column shape a row yields, so a Nested inside one is exactly as ungatherable, and refuses for the same reason with the same message. This mirrors b75f932 on tcp/epic-b9-tls, which made the codec itself refuse a Nested inside a composite rather than only a top-level one. That commit is on a different epic line and never reached this branch, so the test kept the narrow check. Co-Authored-By: Claude <noreply@anthropic.com>
812c488 to
7343f2f
Compare
Stacked on #557 (
tcp/epic-n5-poco-read) — review that one first; this PR's diff is the write half only.TCP epic N9: row-oriented insert, both shapes. The read half of Branch 2 landed in #557; this is the mirror, so a POCO now round-trips through the native protocol.
What it does
InsertAsync<T>(IEnumerable<T>)— one compiled gather per target column, each pulling one property out of every row into the buffer that column is written from. Box-free (except aVariant/Dynamictarget, written fromobject), no per-row delegate hop, and for a fixed-width column the gathered buffer reaches the wire as a single blit. Names match as the read side's do: case-, then underscore-insensitive, with[ClickHouseTcpColumn]/[ClickHouseTcpNotMapped].InsertAsync(IEnumerable<object[]>)— the dynamic tier, positional by the sample block's order. Each column's CLR type comes from the first row that has a value there, so hand-writtenDateTimevalues and the raw epoch seconds the untyped read produces are both accepted, and a read-then-reinsert needs no conversion by the caller.No conversion layer of its own. The read side must ask the codec for its conversions, because a column decodes to the raw wire value. Write does not: a codec already accepts
DateTime/DateTimeOffset/TimeSpandirectly, so the only conversions left are the CLR-level ones a cast would do — nullable lift, enum ordinal, reference upcast. Numeric widening is declined in both directions, which is what keeps "inserts" and "reads back" the same set of shapes.Decisions worth a look
InsertColumnFactoryseam. A factory that throws parks its exception, closes the row stream with no rows, drains, and rethrows once the connection is back toReady— the course the schema-mismatch path already took. The columns a factory returns are the insert's to dispose; a caller's own columns are untouched, as before.NullPlaceholder is nullis true exactly forNullable, a nullableLowCardinality,VariantandDynamic. Asking the CLR instead (default(TWrite) is null) let a nullstringproperty into a plainStringcolumn through the gather, to fault insideWriteColumnpart-way through a block — which terminates the connection. Caught in review; the gather now compiles a null test for reference-typed properties too, so it fails before anything is sent, naming the row.Nullable'sWritableElementTypeswas under-reporting.CanWritehas always acceptedDateTime?for aNullable(DateTime)column, but the list reported only the canonicaluint?— invisible to a caller who probes with a column, fatal to one that picks a type from the list. Now lifted, along withNullPlaceholderAs.ResolveContext.ForWrite, so one target shape is one plan. Both plans now sharePocoBlockSignaturefor the injective length-prefixed key.Two things to weigh
InsertAsync<T>makesInsertAsync(sql, null)and a generically typed column sequence (IColumn<ulong>[]) ambiguous —IReadOnlyList<IColumn>andIEnumerable<IColumn<ulong>>are unrelated, so the "non-generic beats generic" tie-break never runs. A plainIColumn[]/List<IColumn>still binds to the columnar overload, which is every call site here and the only shape an external caller can build, the concrete column classes being internal. Kept because it mirrors the shippedQueryAsync/QueryAsync<T>pairing and the assembly is unreleased and[Experimental];InsertRowsAsyncis the escape hatch if that changes. AnIEnumerable<IColumn>(a LINQ operator over a column list) now binds to the POCO overload and gets a guard naming the columnar one.LowCardinality(DateTime)reads asDateTimebut is written only fromuint— it lifts its inner's readable types and not its writable ones, so it is the last codec whose two surfaces disagree. Pre-existing, and the columnar path has always had it, but the POCO layer is what makes it visible. Documented inPocoWriteConversionand left as a follow-up, since closing it means giving theLowCardinalitywrite path a shape per write type asNullablehas.Testing
2011 tests pass on net9 against a real server; every new
Poco/file is at 100% line coverage.InsertRoundTripCasecases are inserted asRow<T>and read back.Nestedis the one shape rows cannot fill — its writer needs its own column type — so the corpus test asserts the refusal for it rather than skipping.Readyrather than being redialled, plus the factory's ownership (dispose count) and that the factory's own exception comes back with its identity intact.No CHANGELOG/RELEASENOTES entry: no TCP epic PR has one, the assembly being unreleased and
[Experimental]. The epic gets a single entry when it ships.🤖 Generated with Claude Code