Skip to content

Commit 6f7b114

Browse files
committed
Updated tests for +Inf/-Inf values of Decimal
1 parent 0238a0e commit 6f7b114

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,15 @@ private static PrimitiveValue castToTimestamp(PrimitiveType type, Object x) thro
503503
}
504504

505505
private static DecimalValue validateValue(DecimalType type, DecimalValue value, Object x) throws SQLException {
506+
if (value.isNan()) {
507+
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "NaN"));
508+
}
506509
if (value.isInf()) {
507510
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "Infinite"));
508511
}
509512
if (value.isNegativeInf()) {
510513
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "-Infinite"));
511514
}
512-
if (value.isNan()) {
513-
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "NaN"));
514-
}
515515
return value;
516516
}
517517

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tech.ydb.jdbc.impl;
22

33
import java.math.BigDecimal;
4-
import java.math.BigInteger;
54
import java.sql.Connection;
65
import java.sql.Date;
76
import java.sql.PreparedStatement;
@@ -1230,10 +1229,16 @@ private void assertNextDate(ResultSet rs, int key, LocalDate ld) throws SQLExcep
12301229
public void decimalTest(SqlQueries.JdbcQuery query) throws SQLException {
12311230
String upsert = TEST_TABLE.upsertOne(query, "c_Decimal", "Decimal(22, 9)");
12321231

1233-
// YDB partially ignores Decimal(22, 9) limit, but have hard limit to 35 digits
1234-
String maxValue = "9999999999" + "9999999999" + "9999999999" + "99999";
1235-
BigDecimal closeToInf = new BigDecimal(new BigInteger(maxValue), 9);
1236-
BigDecimal closeToNegInf = new BigDecimal(new BigInteger(maxValue).negate(), 9);
1232+
BigDecimal closeToInf = new BigDecimal("9999999999999.999999999");
1233+
BigDecimal closeToNegInf = new BigDecimal("-9999999999999.999999999");
1234+
1235+
// YDB partially ignores Decimal(22, 9) limit, but have hard limit to 35 digits
1236+
// BigDecimal inf = closeToInf.add(BigDecimal.valueOf(1, 9));
1237+
// BigDecimal negInf = closeToNegInf.subtract(BigDecimal.valueOf(1, 9));
1238+
BigDecimal inf = new BigDecimal("100000000000000000000000000.000000000");
1239+
BigDecimal negInf = new BigDecimal("-100000000000000000000000000.000000000");
1240+
BigDecimal nan = new BigDecimal("100000000000000000000000000.000000001");
1241+
12371242
try (PreparedStatement ps = jdbc.connection().prepareStatement(upsert)) {
12381243
ps.setInt(1, 1);
12391244
ps.setBigDecimal(2, BigDecimal.valueOf(1.5d));
@@ -1254,20 +1259,20 @@ public void decimalTest(SqlQueries.JdbcQuery query) throws SQLException {
12541259
ps.setInt(1, 5);
12551260
ExceptionAssert.sqlException(""
12561261
+ "Cannot cast to decimal type Decimal(22, 9): "
1257-
+ "[class java.math.BigDecimal: 100000000000000000000000000.000000000] is Infinite",
1258-
() -> ps.setBigDecimal(2, closeToInf.add(BigDecimal.valueOf(1, 9)))
1262+
+ "[class java.math.BigDecimal: " + inf + "] is Infinite",
1263+
() -> ps.setBigDecimal(2, inf)
12591264
);
12601265

12611266
ExceptionAssert.sqlException(""
12621267
+ "Cannot cast to decimal type Decimal(22, 9): "
1263-
+ "[class java.math.BigDecimal: -100000000000000000000000000.000000000] is -Infinite",
1264-
() -> ps.setBigDecimal(2, closeToNegInf.subtract(BigDecimal.valueOf(1, 9)))
1268+
+ "[class java.math.BigDecimal: " + negInf + "] is -Infinite",
1269+
() -> ps.setBigDecimal(2, negInf)
12651270
);
12661271

12671272
ExceptionAssert.sqlException(""
12681273
+ "Cannot cast to decimal type Decimal(22, 9): "
1269-
+ "[class java.math.BigDecimal: 100000000000000000000000000.000000001] is NaN",
1270-
() -> ps.setBigDecimal(2, closeToInf.add(BigDecimal.valueOf(2, 9)))
1274+
+ "[class java.math.BigDecimal: " + nan + "] is NaN",
1275+
() -> ps.setBigDecimal(2, nan)
12711276
);
12721277
}
12731278

0 commit comments

Comments
 (0)