Skip to content

Commit fc072dd

Browse files
committed
Only convert BigInteger to BigDecimal when value exceeds long range
1 parent 9488fdd commit fc072dd

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/utils/StatementUtils.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@ public static long queryApproximateRowCnt(JdbcConnection jdbc, TableId tableId)
7979
}
8080

8181
// PreparedStatement#setObject method will be converted to long type when handling bigint
82-
// unsigned, which poses a data overflow issue.
83-
// Therefore, we need to handle the overflow issue by converting the bigint unsigned to
84-
// BigDecimal.
82+
// unsigned, which poses a data overflow issue for values exceeding Long.MAX_VALUE.
83+
// Therefore, we need to convert to BigDecimal when the value is outside the long range
8584
public static void setSafeObject(PreparedStatement ps, int parameterIndex, Object value)
8685
throws SQLException {
8786
if (value instanceof BigInteger) {
88-
ps.setBigDecimal(parameterIndex, new BigDecimal((BigInteger) value));
87+
BigInteger bigIntValue = (BigInteger) value;
88+
if (bigIntValue.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
89+
|| bigIntValue.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
90+
ps.setBigDecimal(parameterIndex, new BigDecimal(bigIntValue));
91+
} else {
92+
ps.setObject(parameterIndex, bigIntValue.longValueExact());
93+
}
8994
} else {
9095
ps.setObject(parameterIndex, value);
9196
}

0 commit comments

Comments
 (0)