Fix JSON: a path holding a composite value, and Dynamic(max_types=N) - #628
Open
polyglotAI-bot wants to merge 2 commits into
Open
polyglotAI-bot wants to merge 2 commits into
polyglotAI-bot wants to merge 2 commits into
Conversation
…s=N) JsonType.ReadJsonNode dispatched only on Array, Map and FixedString, so a Tuple, Nested, Dynamic, Variant or SimpleAggregateFunction path was read as one value and handed to JsonValue.Create, which throws on an object or an array element: the whole row was unreadable. Each of those types is now dispatched on what it actually holds — a tuple element by element, a Nested as a repeated tuple, and a Dynamic or Variant on the type its value carries — so the document matches the one the server renders. Dynamic also accepts a max_types argument now. It bounds only the set of types the server tracks, not the wire layout, so it is kept in the type name and otherwise ignored, as the TCP codec already does. Fixes: #626
polyglotAI-bot
requested review from
alex-clickhouse and
mzitnik
as code owners
September 25, 2026 15:28
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Feature gating gaps and nullable composite JSON rendering remain unresolved.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
What changed in this PR
Fixes JSON deserialization for composite values and adds support for Dynamic(max_types=N).
Changes:
- Adds recursive JSON handling for tuples, nested values, dynamics, variants, and aggregate wrappers.
- Preserves tuple and nested field names.
- Adds parsing, regression tests, and changelog documentation.
| File | Description |
|---|---|
ClickHouse.Driver/Types/VariantType.cs |
Names the null discriminator |
ClickHouse.Driver/Types/TypeConverter.cs |
Registers parameterized Dynamic |
ClickHouse.Driver/Types/TupleType.cs |
Tracks tuple element names |
ClickHouse.Driver/Types/NestedType.cs |
Tracks nested element names |
ClickHouse.Driver/Types/JsonType.cs |
Implements composite JSON decoding |
ClickHouse.Driver/Types/DynamicType.cs |
Parses max_types |
ClickHouse.Driver/Types/BinaryTypeDecoder.cs |
Preserves decoded names |
ClickHouse.Driver.Tests/Types/JsonStringAsByteArrayTests.cs |
Updates wrapper rendering tests |
ClickHouse.Driver.Tests/Types/JsonCompositeValueReadTests.cs |
Adds composite JSON coverage |
ClickHouse.Driver.Tests/Types/DynamicTests.cs |
Tests Dynamic(max_types=N) |
ClickHouse.Driver.Tests/Types/BinaryTypeDecoderTests.cs |
Tests decoded tuple names |
changelog.d/626-json-composite-values.fixes.md |
Documents the fixes |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
ReadJsonNode dispatched Tuple, Nested, Dynamic, Variant and SimpleAggregateFunction, but a Nullable path still read its value whole, so a Nullable(Tuple(...)) was serialized from the CLR System.Tuple and a named tuple came out with Item1/Item2 properties instead of the server's object. Nullable only decides whether a value is present: consume its marker and dispatch again on the type it wraps.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Fixes #626.
JsonType.ReadJsonNodedecided how to materialize a path from its ClickHouse type, but onlyArray,MapandFixedStringhad an arm. Every other type was read as one value and handed toReadJsonValue, whose default arm callsJsonValue.Create(JsonSerializer.SerializeToElement(...))— and
JsonValue.Createthrows on an element which is an object or an array. So a path holding aTuplethrewInvalidOperationException: The element cannot be an object or arrayand the wholerow became unreadable, with no way for the caller to get the value. The reported shape reaches this
through a heterogeneous JSON array:
'{"a": ["template", "x", ["macro", 20], "y"]}'::JSONtypespath
aasArray(Dynamic), whose third element is aDynamicholdingTuple(String, Int64).The dispatch is now complete for every type through which a composite value can arrive:
Tupleis read element by element, and rendered as the server renders it — a named tuple as anobject, an unnamed one as an array;
Nestedis a repeated tuple (a length, then that many tuples), so it renders as an array ofobjects. It derives from
TupleType, so it is matched before the tuple arm — reading it as asingle tuple would consume the wrong bytes and desync the reader;
DynamicandVariantcarry the type of the value itself, so the value's own type is read anddispatched again, instead of reading the value whole and losing its structure;
SimpleAggregateFunctionreads exactly as the type it wraps, so the wrapper is resolved beforethe value is read.
As a defence in depth the default arm now uses
JsonSerializer.SerializeToNode, which renders acomposite instead of throwing.
The second defect in the issue is separate:
Dynamic(max_types = N)failed in the column-headerparser before a single value was read (
ArgumentException: Unknown type: Dynamic(max_types=0)).max_typesbounds only the set of types the server tracks; it does not change the wire layout,which is self-describing per value.
DynamicTypeis now aParameterizedTypethat accepts theargument, keeps it in the type name and otherwise ignores it — the same thing
DynamicColumnCodec.TryParseMaxTypesalready does on the TCP side. Any other argument is rejectedwith a message naming it.
Changes
Types/JsonType.cs:Nested,Tuple,SimpleAggregateFunction,DynamicandVariantarms inReadJsonNode, withReadJsonNested/ReadJsonTuple/ReadJsonVariant; the default arm ofReadJsonValuerenders composites instead of throwing.Types/TupleType.cs,Types/NestedType.cs: anElementNamesproperty, read off thename Typechildren. A multi-word type alias (
BIGINT UNSIGNED) also holds a space, so an alias is ruled outbefore the name is taken; an element without a name makes the whole tuple unnamed.
Types/BinaryTypeDecoder.cs: the named-tuple and Nested decoders keep the field names theyalready read instead of discarding them.
Types/DynamicType.cs,Types/TypeConverter.cs:Dynamicis registered as a parameterized typeand parses
max_types=N;TypeConverter.IsTypeAliasexposes the alias check, andRegisteredTypesis de-duplicated now thatDynamicis in both registries.Types/VariantType.cs: the0xFFnull discriminator is a named constant, now that the JSON readpath needs it too.
changelog.d/626-json-composite-values.fixes.md.Test
Tests/Types/JsonCompositeValueReadTests.cs(new, 14 cases): each case selects the value andthe server's own
toJSONStringof the same expression, and asserts both against the expecteddocument, so the driver cannot drift from the server. It covers the reported shape (with and
without
max_dynamic_types = 0), a hinted named and unnamed tuple, a tuple insideArrayandMap,Nested,SimpleAggregateFunction(anyLast, Tuple(...)), aDynamicpath holding a tuple,and a
Variantpath holding an array and a tuple. Three contrast cases pin the modes the fix doesnot cover — a scalar string under
Dynamic, a nullDynamicand a nullVariant— and passunchanged before and after.
Tests/Types/DynamicTests.cs:Dynamic,Dynamic(max_types=0)andDynamic(max_types=3)parseand keep their declared name; an unsupported argument throws; and the value reads back over a live
server for each spelling.
Tests/Types/BinaryTypeDecoderTests.cs: a named-tuple header keeps its element names, an unnamedone has none.
main; the wholeClickHouse.Driver.Testssuite passes on this branch (11023 passed / 142 skipped, net10.0,ClickHouse 26.8).
Existing tests changed, and why
JsonStringAsByteArrayTests.ReadJson_WithNonTextByteArrayPath_IsNotDecodedAsTextasserted thatArray(UInt8)under aVariantorSimpleAggregateFunctionpath renders as base64 ("AQI=").That was the output of reading the array whole as a
byte[]; the server renders[1,2]for thesame value, as does this driver for the same array without the wrapper. The four cases now assert
the array, through
ToJsonString()so the exact JSON is pinned rather than a string value. ADynamic-hinted string was added toStringBearingJsonShapes: underReadStringsAsByteArraysitused to come back base64, because a
Dynamicnever reached the text-decoding arm.Related
Reading a
Dynamic/Variant-hinted path that holds an array, and a string under aDynamichintcoming back base64 — #530 — are the same dispatch
defect and are fixed by the same change.
Pre-PR validation gate
main, passes here)AGENTS.md(integration tests against a real server,TestCaseSourceparametrization,CreateTableNamenot needed — no test creates a table,changelog fragment instead of
CHANGELOG.md, no public API change)