Repository navigation
[client-v2] Make DataTypeConverter thread-safe - #3407
polyglotAI-bot wants to merge 2 commits 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 <noreply@anthropic.com>
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
|
chernser
left a comment
There was a problem hiding this comment.
see comments - DataTypeConverter should create list or array writer for each value that should be converted. This should not be an issue for small class
Review feedback: DataTypeConverter now creates a new ListAsStringWriter or ArrayAsStringWriter for each value in arrayToString, in place of passing the column and the Appendable as method parameters to one shared writer. Each writer keeps the column in a final field and the output in the base class field, as before. BaseCollectionConverter is back to its version on main. DataTypeConverter has no writer fields and no convertAndReset, so INSTANCE keeps no conversion state and is thread-safe. The output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TriageCategory: Summary What this impacts
Concerns
Required reviewer action
|
There was a problem hiding this comment.
Auto-approved by the AI review gate on e9fc023. Details in the approver's comment on this pull request.
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 requested in the review,
arrayToStringnow creates a newListAsStringWriterorArrayAsStringWriterfor each value. Each writer keeps the column in a final field, andBaseCollectionConverterkeeps the outputAppendable, as onmain.DataTypeConverterhas no writer fields, andconvertAndReset(the save/restore) is removed, soINSTANCEkeeps no conversion state. A nested conversion creates its own writer. NoThreadLocal, no locks. The cost is one small writer object for each array value.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.
BaseCollectionConverterdoes not change. Only private members ofDataTypeConverterchange (the writer fields and the writer classes).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: no public or protected signature change; no logging, config, enum, default or exception change;docs/features.mdnot affected🤖 Generated with Claude Code
Note
Low Risk
Behavior-preserving refactor for string formatting with added concurrency coverage; no auth, protocol, or default output changes intended.
Overview
Fixes parallel
getString()on array columns by makingDataTypeConverter.INSTANCEsafe to share across threads (closes #2728).DataTypeConverterno longer keeps reusableListAsStringWriter/ArrayAsStringWriterinstances on the converter. EacharrayToStringcall allocates a writer with theClickHouseColumnfixed in the constructor and usesconvertinstead ofconvertAndReset, so concurrent conversions cannot clobber each other’s column orAppendablestate (wrong quoting, mixed values, orNullPointerException). The class javadoc now documents thread safety for the shared singleton.Tests: a unit test interleaves two conversions on
INSTANCEvia blocking array elements (list, array,ArrayValue); an integration test runs multiple readers callinggetString()on array columns in parallel. CHANGELOG entry added underhistory/latest/2728.md.Reviewed by Cursor Bugbot for commit e9fc023. Bugbot is set up for automated code reviews on this repo. Configure here.