Repository navigation
[client-v2] Make DataTypeConverter thread-safe - #3407
Open
polyglotAI-bot wants to merge 1 commit into
Open
polyglotAI-bot wants to merge 1 commit into
polyglotAI-bot wants to merge 1 commit into
Conversation
All binary readers and MapBackedRecord share DataTypeConverter.INSTANCE. Its ArrayAsStringWriter and ListAsStringWriter kept the current column and output Appendable in fields during each arrayToString call. Two threads that converted arrays at the same time changed each other's state: getString() returned wrong quotes or values from the other thread, or failed with a NullPointerException. BaseCollectionConverter now passes the Appendable and the ClickHouseColumn as method parameters to convert, onStart, onItem and onEnd. The writers have no mutable fields, and the convertAndReset save/restore is removed. No ThreadLocal and no new allocations; the output is unchanged. Fixes: #2728 Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Auto-approved by the AI review gate on bd8c59b. Details in the approver's comment on this pull request.
AI review gate — auto-approved
History (latest 5 heads, newest first)
|
Client V2 CoverageCoverage Report
Class Coverage
|
JDBC V2 CoverageCoverage Report
Class Coverage
|
1 task
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
Closes #2728
getString()on an array value is now safe when readers,GenericRecordobjects or JDBCResultSetobjects in different threads call it at the same time. All of them use the sharedDataTypeConverter.INSTANCE. ItsArrayAsStringWriterandListAsStringWriterkept the current column and outputAppendablein fields for eacharrayToStringcall (convertAndReset). A parallel conversion changed these fields: the result had wrong quotes or values of the other thread, or the call failed with aNullPointerException.As the issue comment asks,
BaseCollectionConverternow passes theAppendableand theClickHouseColumnas parameters toconvert,onStart,onItemandonEnd. The writers have no mutable fields, and theconvertAndResetsave/restore is removed. NoThreadLocal, no locks, no new allocations.DataTypeConverterstays an instance class, so a subclass can still change the conversion.Stringelement directly, iterateArrayValue.arraywithoutasList()). They do not change thread safety, so they can be a follow-up.Compatibility
The output does not change.
BaseCollectionConverter(packagecom.clickhouse.client.api.internal) changes its protected callback signatures, andconvert(LIST, ACC)becomesconvert(LIST, ACC, ClickHouseColumn). The repo has no other users of these methods; a subclass outside the repo does not compile.Test
DataTypeConverterTest#testArrayToStringWithSharedInstanceInParallel(List,Object[],ArrayValue): an element blocks intoString(), so two conversions onDataTypeConverter.INSTANCEinterleave in a fixed order. Both results must be correct.QueryTests#testGetStringOfArraysInParallelReaders: 4 threads read their own query result withgetString()onArray(String)andArray(UInt64)columns.On
main, the unit test fails every time ([1, '2']instead of[1, 2]), and the integration test failed in 3 of 3 runs (NullPointerExceptionor a wrong value).Checklist
history/latest/2728.md)docs/changes_checklist.md: method signature changed (internalBaseCollectionConverteronly, all callers updated); no logging, config, enum, default or exception change;docs/features.mdnot affected🤖 Generated with Claude Code
Note
Medium Risk
Touches internal array-to-string conversion used by readers and JDBC; behavior is intended unchanged but concurrent
getString()on arrays was previously corrupt or NPE-prone.Overview
Makes
DataTypeConverterand its array writers safe when multiple threads callgetString()on array values through the sharedDataTypeConverter.INSTANCE.BaseCollectionConverternow passes the accumulator andClickHouseColumnintoconvertand lifecycle callbacks (onStart,onItem,onEnd) instead of storing them on the converter instance.ArrayAsStringWriterandListAsStringWriterdrop per-call fields and theconvertAndResetsave/restore pattern; callers useconvert(..., column)directly.Unit and integration tests exercise interleaved conversions on the shared instance and parallel readers calling
getString()on array columns.Reviewed by Cursor Bugbot for commit bd8c59b. Bugbot is set up for automated code reviews on this repo. Configure here.