Repository navigation
[client-v2] Reject a Dynamic value that contains itself instead of hanging - #3377
Open
Indian-boult wants to merge 1 commit into
Open
Indian-boult wants to merge 1 commit into
Indian-boult wants to merge 1 commit into
Conversation
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
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.
Summary
Writing a value that contains itself into a
Dynamiccolumn hung the insert or threwStackOverflowError. It now fails at once withClientException.SerializerUtils.valueToColumnForDynamicTypewalks a value to find its column type.listValue2Columnwalks nested arrays and lists with an explicit stack, and for anObject[]orListthat holds itself it pushed the same container again forever. TheMapbranch recursed into the first entry and threwStackOverflowErrorfor aMapthat holds itself. Against ClickHouse 26.10, later requests of the sameClientthen failed withConnection pool shut down.The fix detects a real cycle. Each stack entry of
listValue2Columnlinks to the entry of the container that holds it, and theMapbranch passes the chain of maps that hold the value to its nested calls. When a container appears again among its own holders, the client throwsClientExceptionwithCannot 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 exampleArray(Array(Int32)). I chose this over a depth limit because the server has no fixed type depth, it uses a stack check andinput_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
StackOverflowErrornow getClientException.Tests.
SerializerUtilsTesthas 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 withArrays.deepToString, which overflows on two maps that hold each other. The other covers four shared values without a cycle and asserts the exact type. Onmainall seven cycle rows fail, four by timeout and three withStackOverflowError, 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
StackOverflowErrorduring type inference.SerializerUtils.valueToColumnForDynamicTypenow tracks ancestor containers on the walk: map handling passes an enclosing-map chain, andlistValue2Columnlinks each stack frame to its parent. When the same container reappears on that path, the client throwsClientExceptionwith 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.