Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ static int checkedCastToInt(BigInteger val) throws SQLException {

/** Cast value and throw {@link SQLException} if out-of-range. */
static float checkedCastToFloat(double val) throws SQLException {
if (val > Float.MAX_VALUE || val < -Float.MAX_VALUE) {
if (Double.isFinite(val) && (val > Float.MAX_VALUE || val < -Float.MAX_VALUE)) {
throw JdbcSqlExceptionFactory.of(
String.format(OUT_OF_RANGE_MSG, "float", val), com.google.rpc.Code.OUT_OF_RANGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public void testCheckedCastFromBigIntegerToLong() {
}

@Test
public void testCheckedCastToFloat() {
public void testCheckedCastToFloat() throws SQLException {
final CheckedCastChecker<Double> checker =
new CheckedCastChecker<>(AbstractJdbcWrapper::checkedCastToFloat);
assertThat(checker.cast(0D)).isTrue();
Expand All @@ -268,6 +268,16 @@ public void testCheckedCastToFloat() {
assertThat(checker.cast((double) Float.MIN_VALUE)).isTrue();
assertThat(checker.cast(-Float.MAX_VALUE * 2d)).isFalse();
assertThat(checker.cast(-Double.MAX_VALUE)).isFalse();

assertEquals(
Float.POSITIVE_INFINITY,
AbstractJdbcWrapper.checkedCastToFloat(Double.POSITIVE_INFINITY),
0.0d);
assertEquals(
Float.NEGATIVE_INFINITY,
AbstractJdbcWrapper.checkedCastToFloat(Double.NEGATIVE_INFINITY),
0.0d);
assertEquals(Float.NaN, AbstractJdbcWrapper.checkedCastToFloat(Double.NaN), 0.0d);
}

@Test
Expand Down
Loading