Skip to content

[client-v2] Reject a Dynamic value that contains itself instead of hanging - #3377

Open
Indian-boult wants to merge 1 commit into
ClickHouse:mainfrom
Indian-boult:fix/dynamic-self-reference
Open

Indian-boult wants to merge 1 commit into
ClickHouse:mainfrom
Indian-boult:fix/dynamic-self-reference

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 18, 2026 •

Copy link
Copy Markdown

Summary

Writing a value that contains itself into a Dynamic column hung the insert or threw StackOverflowError. It now fails at once with ClientException.

SerializerUtils.valueToColumnForDynamicType walks a value to find its column type. listValue2Column walks nested arrays and lists with an explicit stack, and for an Object[] or List that holds itself it pushed the same container again forever. The Map branch recursed into the first entry and threw StackOverflowError for a Map that holds itself. Against ClickHouse 26.10, later requests of the same Client then failed with Connection pool shut down.

The fix detects a real cycle. Each stack entry of listValue2Column links to the entry of the container that holds it, and the Map branch passes the chain of maps that hold the value to its nested calls. When a container appears again among its own holders, the client throws ClientException with Cannot serialize <class> into a Dynamic column because it contains itself. Only containers on the path are compared, so a value that holds the same array, list or map twice without a cycle keeps its type, for example Array(Array(Int32)). I chose this over a depth limit because the server has no fixed type depth, it uses a stack check and input_format_binary_max_type_complexity. The check allocates no set and adds no object for a flat array or list.

Compatibility. The public signature does not change and values without a cycle get the same type as before. Only values that hung or threw StackOverflowError now get ClientException.

Tests. SerializerUtilsTest has two new data provider tests. One covers seven cycles, through arrays, lists, maps and mixes of them, and asserts the exception and message with a 10 s timeout so a regression fails instead of hanging the build. The values come from suppliers because the TestNG reporter prints parameters with Arrays.deepToString, which overflows on two maps that hold each other. The other covers four shared values without a cycle and asserts the exact type. On main all seven cycle rows fail, four by timeout and three with StackOverflowError, and with the fix all pass. A wrong fix that rejects any container seen twice fails a shared row. The unit tests of clickhouse-data, client-v2 and jdbc-v2 pass. I also ran the insert path by hand against ClickHouse 26.10.4.207, where all four cyclic inserts now fail within 3 ms and the client keeps working.

Closes #3177

Checklist


Note

Low Risk
Localized change to Dynamic type inference with unchanged public API; only previously broken cyclic inputs fail fast instead of hanging or overflowing the stack.

Overview
Fixes client-v2 Dynamic column writes where self-referential or cyclic Java values (array/list/map containing itself or a multi-container cycle) previously hung or threw StackOverflowError during type inference.

SerializerUtils.valueToColumnForDynamicType now tracks ancestor containers on the walk: map handling passes an enclosing-map chain, and listValue2Column links each stack frame to its parent. When the same container reappears on that path, the client throws ClientException with Cannot serialize … because it contains itself. Shared containers referenced multiple times without a cycle still infer the same types as before.

Tests add cycle cases (with timeout) and shared-container cases; CHANGELOG documents the fix for #3177.

Reviewed by Cursor Bugbot for commit 0a6ba13. Bugbot is set up for automated code reviews on this repo. Configure here.

valueToColumnForDynamicType walks a value to find its column type.
For an Object[] or List that holds itself, listValue2Column pushed the
same container again one level deeper on every pass, so the call never
returned. For a Map that holds itself, the Map branch called itself
until the stack overflowed.

Each stack entry of listValue2Column now also links to the entry of
the array or list that holds it. The Map branch passes the chain of
maps that hold the value to its nested calls. A container that is
found again among its own holders is a cycle, and the client throws
ClientException with a message that names its class. Only containers
on the path from the outer value are compared, so a value that holds
the same array, list or map twice without a cycle keeps its type.

Closes ClickHouse#3177
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

client-v2: a self-referential collection in a Dynamic column hangs (array/List) or throws StackOverflowError (Map) instead of a clean exception

1 participant