Repository navigation
[client-v2] Check the range of integer column values on write - #3411
polyglotAI-bot wants to merge 2 commits into
Conversation
Binary serialization narrowed Number values for integer columns with intValue()/longValue() and had no signed range check for Int128/Int256, so values that do not fit the column were written as other values (2147483648L into Int32 became -2147483648, the Int128 maximum as BigDecimal became -1). Convert the value without loss of the integer part and check the range of the column type; reject out-of-range values, NaN and infinities with IllegalArgumentException. Document that the JDBC RowBinary writer expects values of the column type. Fixes: #2619 Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Auto-approved by the AI review gate on fba1ea3. 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
|
ConnectorSec security review — head
|
convertToBigInteger called toBigInteger() before the range check. For a short BigDecimal such as 1E+500000000 this computes 10^500000000 (about 200 MB and minutes of CPU); on JDK 8, 11 and 17 a fraction-only value such as 1E-500000000 does the same through a divide. Check the number of integer digits first: zero and fraction-only values give 0, and a value with more than 78 integer digits (2^256 has 78) fits no integer column and is rejected with IllegalArgumentException. Used for BigDecimal and generic Number values. Reported in the ConnectorSec review of #3411. Fixes: #2619 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, confirmed. On JDK 17, Fixed in 33696af with your Tests in
Without the fix, 12 rows fail (11 time out). With the fix, client-v2 unit tests (1012), |
There was a problem hiding this comment.
Auto-approved by the AI review gate on 33696af. Details in the approver's comment on this pull request.
Summary
Closes #2619
The reported case already works on
main: since 860ed34 (0.10.0), aUInt64value is converted withBigInteger, sosetStringwith a value aboveLong.MAX_VALUEis written correctly. As the issue comment asks, I checked all integer types. Strings at the minimum and maximum of each type are correct. ButSerializerUtils(binary serialization in client-v2, also used by the JDBC RowBinary writer) silently changed some other values:convertToInteger/convertToLongnarrowed aNumberwithintValue()/longValue().2147483648Lwas written as-2147483648intoInt32,4294967296Las0intoInt8, andBigInteger2^63 wrapped inInt64.Int128/Int256had no signed range check. A value up to the unsigned maximum, for example theString"170141183460469231731687303715884105728", was stored as a negative number.NumberConverter.toBigIntegeruseslongValue()forBigDecimal,doubleand otherNumbertypes. TheInt128maximum asBigDecimalwas stored as-1. In-rangeBigDecimalvalues andUnsignedLong.MAX_VALUEwere rejected forUInt64,UInt128andUInt256.1.0E19dwas stored as9223372036854775807inUInt64.The integer cases now convert the value to the column type without loss of the integer part, then check the range of the column type.
Stringvalues use the same parse functions as before (Integer.parseInt,Long.parseLong,BigInteger).Integer,Long,Short,Byteand in-rangedoublevalues keep an allocation-free path.NumberConverteris not changed, because the read path uses it.Not in this PR:
Enum8/Enum16also useintValue(). Enum write validation is in [client-v2] Validate Enum8/Enum16 values on insert #3312.addBatch()fails, the bytes of the failed row can stay in the writer buffer.RowBinaryFormatSerializer.writeUInt256writes 16 bytes. I will check both separately.Compatibility
A value in the range of the column type is written as before, also a fractional value (the fraction is dropped, as before). A value out of the range,
NaNor an infinity now fails withIllegalArgumentException(JDBC:SQLException). Before, it was wrapped or written as another value. This includes(double) Long.MAX_VALUEforInt64, which was clamped to the maximum before. The server also wraps out-of-range integers in text formats (INSERT ... VALUES (2147483648)intoInt32stores-2147483648). But this client already rejected out-of-range values forInt8,Int16andUInt8toUInt256, and now all integer types do the same. No public API change (the new helper is private).Docs:
docs/integration-jdbc.mdnow tells applications to bind values of the column type when the RowBinary writer is on, and says what the writer does with other values.docs/features.mdhas one new line for the integer range check.Test
SerializerUtilsPrimitiveSerializationTests#testSerializeIntegerColumnRejectsOutOfRangeValue(unit): out-of-rangeLong,BigInteger,BigDecimal,double,NaN,UnsignedLong, genericNumberandStringvalues must fail with the range message. Onmain, 10 of 11 rows fail (the value is written)."2147483648"forInt32already failed and is the contrast case.RowBinaryFormatWriterTest#writeIntegerColumnValueTest(integration): in-rangeBigDecimal,double,float,Long,BigInteger,UnsignedLongandStringvalues forInt8toUInt256, with a fixed-width column after the value. Onmain, 7 of 14 rows fail (for example-1instead of theInt128maximum). The other rows are contrast cases: fraction truncation, values that fit, theStringmaximum.WriterStatementImplTest#testSetStringIntoIntegerColumnBounds: the issue's path (setString+addBatch()+executeBatch()) with the minimum and maximum of all 12 integer types. It also passes onmainand pins the reported behavior.WriterStatementImplTest#testSetLongOutOfIntegerColumnRangeFails:setLong(2147483648L)forInt32makesaddBatch()throwSQLException. Onmain, the value is written as-2147483648.SerializerUtilsPrimitiveSerializationTests#testSerializeIntegerColumnRejectsValueWithLargeExponentand#testSerializeIntegerColumnWritesIntegerPart(unit,timeOut = 10000): aBigDecimalor genericNumbersuch as1E+500000000or1E-500000000is rejected or written as 0 at once. The number of integer digits is checked beforetoBigInteger(), which would otherwise compute 10^500000000 (ConnectorSec review).Ran in a devbox against ClickHouse 26.10.4:
RowBinaryFormatWriterTest,DataTypeTests,InsertTests,SerializerUtilsTests.WriterStatementImplTest,PreparedStatementTest,JdbcDataTypeTests.packages/clickhouse-jdbc-allbuilds.All pass, except
JdbcDataTypeTests#testIpAddressTypes. That test resolveswww.example.com, and the test host has no DNS.Checklist
history/latest/2619.md)docs/changes_checklist.md: exception handling changed (out-of-range values fail withIllegalArgumentException, the type that the sibling range checks already throw). No new public method, no logging, config, enum or default change.docs/features.mdupdated.🤖 Generated with Claude Code
Note
Medium Risk
Behavior change on write: out-of-range or invalid numeric inputs that used to be silently corrupted now throw, affecting RowBinary inserts and POJO/binary writers; in-range paths should match prior output.
Overview
Binary serialization for all integer column types (
Int8–Int256,UInt8–UInt256) now converts inputs to the column’s integer part without silent Java narrowing, then rejects values outside the type range,NaN, or infinity withIllegalArgumentException(JDBC RowBinary inserts surface this asSQLException).SerializerUtilsreplaces unsafeintValue()/longValue()/NumberConverter.toBigIntegerpaths withconvertToInteger,convertToLong, and a newconvertToBigInteger+toIntegerPartthat drops fractions, guards extremeBigDecimalexponents (e.g.1E+500000000), and appliesClickHouseChecker.betweenforInt128/Int256and wider unsigned types.In-range behavior is unchanged (fractions still truncated); only previously wrapped or wrong out-of-range writes now fail fast. Docs and changelog note the contract; unit and integration tests cover bounds, large exponents, and JDBC
setString/setLongcases.Reviewed by Cursor Bugbot for commit 33696af. Bugbot is set up for automated code reviews on this repo. Configure here.