Skip to content

Commit

Permalink
Updated tests for +Inf/-Inf values of Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 committed Dec 24, 2024
1 parent 0238a0e commit 6f7b114
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
6 changes: 3 additions & 3 deletions jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,15 @@ private static PrimitiveValue castToTimestamp(PrimitiveType type, Object x) thro
}

private static DecimalValue validateValue(DecimalType type, DecimalValue value, Object x) throws SQLException {
if (value.isNan()) {
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "NaN"));
}
if (value.isInf()) {
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "Infinite"));
}
if (value.isNegativeInf()) {
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "-Infinite"));
}
if (value.isNan()) {
throw new SQLException(String.format(YdbConst.UNABLE_TO_CAST_TO_DECIMAL, type, toString(x), "NaN"));
}
return value;
}

Expand Down
27 changes: 16 additions & 11 deletions jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tech.ydb.jdbc.impl;

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

// YDB partially ignores Decimal(22, 9) limit, but have hard limit to 35 digits
String maxValue = "9999999999" + "9999999999" + "9999999999" + "99999";
BigDecimal closeToInf = new BigDecimal(new BigInteger(maxValue), 9);
BigDecimal closeToNegInf = new BigDecimal(new BigInteger(maxValue).negate(), 9);
BigDecimal closeToInf = new BigDecimal("9999999999999.999999999");
BigDecimal closeToNegInf = new BigDecimal("-9999999999999.999999999");

// YDB partially ignores Decimal(22, 9) limit, but have hard limit to 35 digits
// BigDecimal inf = closeToInf.add(BigDecimal.valueOf(1, 9));
// BigDecimal negInf = closeToNegInf.subtract(BigDecimal.valueOf(1, 9));
BigDecimal inf = new BigDecimal("100000000000000000000000000.000000000");
BigDecimal negInf = new BigDecimal("-100000000000000000000000000.000000000");
BigDecimal nan = new BigDecimal("100000000000000000000000000.000000001");

try (PreparedStatement ps = jdbc.connection().prepareStatement(upsert)) {
ps.setInt(1, 1);
ps.setBigDecimal(2, BigDecimal.valueOf(1.5d));
Expand All @@ -1254,20 +1259,20 @@ public void decimalTest(SqlQueries.JdbcQuery query) throws SQLException {
ps.setInt(1, 5);
ExceptionAssert.sqlException(""
+ "Cannot cast to decimal type Decimal(22, 9): "
+ "[class java.math.BigDecimal: 100000000000000000000000000.000000000] is Infinite",
() -> ps.setBigDecimal(2, closeToInf.add(BigDecimal.valueOf(1, 9)))
+ "[class java.math.BigDecimal: " + inf + "] is Infinite",
() -> ps.setBigDecimal(2, inf)
);

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

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

Expand Down

0 comments on commit 6f7b114

Please sign in to comment.