Skip to content

Commit fdaf6bb

Browse files
committed
Added test for custom decimal types +inf/-inf
1 parent e328d34 commit fdaf6bb

File tree

1 file changed

+114
-5
lines changed

1 file changed

+114
-5
lines changed

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

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,8 @@ public void decimalTest(SqlQueries.JdbcQuery query) throws SQLException {
12321232
BigDecimal closeToInf = new BigDecimal("9999999999999.999999999");
12331233
BigDecimal closeToNegInf = new BigDecimal("-9999999999999.999999999");
12341234

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");
1235+
BigDecimal inf = closeToInf.add(BigDecimal.valueOf(1, 9));
1236+
BigDecimal negInf = closeToNegInf.subtract(BigDecimal.valueOf(1, 9));
12401237
BigDecimal nan = new BigDecimal("100000000000000000000000000.000000001");
12411238

12421239
try (PreparedStatement ps = jdbc.connection().prepareStatement(upsert)) {
@@ -1299,4 +1296,116 @@ private void assertNextDecimal(ResultSet rs, int key, BigDecimal bg) throws SQLE
12991296
BigDecimal decimal = rs.getBigDecimal("c_Decimal");
13001297
Assertions.assertEquals(bg, decimal);
13011298
}
1299+
1300+
@ParameterizedTest(name = "with {0}")
1301+
@EnumSource(SqlQueries.JdbcQuery.class)
1302+
public void bankDecimalTest(SqlQueries.JdbcQuery query) throws SQLException {
1303+
String upsert = TEST_TABLE.upsertOne(query, "c_BankDecimal", "Decimal(31, 9)");
1304+
boolean castingSupported = query != SqlQueries.JdbcQuery.IN_MEMORY;
1305+
1306+
BigDecimal closeToInf = new BigDecimal("9999999999999999999999.999999999");
1307+
BigDecimal closeToNegInf = new BigDecimal("-9999999999999999999999.999999999");
1308+
1309+
BigDecimal inf = closeToInf.add(BigDecimal.valueOf(1, 9));
1310+
BigDecimal negInf = closeToNegInf.subtract(BigDecimal.valueOf(1, 9));
1311+
BigDecimal nan = new BigDecimal("100000000000000000000000000.000000001");
1312+
1313+
try (PreparedStatement ps = jdbc.connection().prepareStatement(upsert)) {
1314+
if (castingSupported) {
1315+
ps.setInt(1, 1);
1316+
ps.setBigDecimal(2, BigDecimal.valueOf(1.5d));
1317+
ps.execute();
1318+
1319+
ps.setInt(1, 2);
1320+
ps.setBigDecimal(2, BigDecimal.valueOf(-12345, 10)); // will be rounded to -0.000001234
1321+
ps.execute();
1322+
1323+
ps.setInt(1, 3);
1324+
ps.setBigDecimal(2, closeToInf);
1325+
ps.execute();
1326+
1327+
ps.setInt(1, 4);
1328+
ps.setBigDecimal(2, closeToNegInf);
1329+
ps.execute();
1330+
1331+
ps.setInt(1, 5);
1332+
ExceptionAssert.sqlException(""
1333+
+ "Cannot cast to decimal type Decimal(31, 9): "
1334+
+ "[class java.math.BigDecimal: " + inf + "] is Infinite",
1335+
() -> ps.setBigDecimal(2, inf)
1336+
);
1337+
1338+
ExceptionAssert.sqlException(""
1339+
+ "Cannot cast to decimal type Decimal(31, 9): "
1340+
+ "[class java.math.BigDecimal: " + negInf + "] is -Infinite",
1341+
() -> ps.setBigDecimal(2, negInf)
1342+
);
1343+
1344+
ExceptionAssert.sqlException(""
1345+
+ "Cannot cast to decimal type Decimal(31, 9): "
1346+
+ "[class java.math.BigDecimal: " + nan + "] is NaN",
1347+
() -> ps.setBigDecimal(2, nan)
1348+
);
1349+
} else {
1350+
int sqlType = ydbType(DecimalType.of(31, 9));
1351+
ps.setInt(1, 1);
1352+
ps.setObject(2, BigDecimal.valueOf(1.5d), sqlType);
1353+
ps.execute();
1354+
1355+
ps.setInt(1, 2);
1356+
ps.setObject(2, BigDecimal.valueOf(-12345, 10), sqlType); // will be rounded to -0.000001234
1357+
ps.execute();
1358+
1359+
ps.setInt(1, 3);
1360+
ps.setObject(2, closeToInf, sqlType);
1361+
ps.execute();
1362+
1363+
ps.setInt(1, 4);
1364+
ps.setObject(2, closeToNegInf, sqlType);
1365+
ps.execute();
1366+
1367+
ps.setInt(1, 5);
1368+
ExceptionAssert.sqlException(""
1369+
+ "Cannot cast to decimal type Decimal(31, 9): "
1370+
+ "[class java.math.BigDecimal: " + inf + "] is Infinite",
1371+
() -> ps.setObject(2, inf, sqlType)
1372+
);
1373+
1374+
ExceptionAssert.sqlException(""
1375+
+ "Cannot cast to decimal type Decimal(31, 9): "
1376+
+ "[class java.math.BigDecimal: " + negInf + "] is -Infinite",
1377+
() -> ps.setObject(2, negInf, sqlType)
1378+
);
1379+
1380+
ExceptionAssert.sqlException(""
1381+
+ "Cannot cast to decimal type Decimal(31, 9): "
1382+
+ "[class java.math.BigDecimal: " + nan + "] is NaN",
1383+
() -> ps.setObject(2, nan, sqlType)
1384+
);
1385+
}
1386+
}
1387+
1388+
try (Statement st = jdbc.connection().createStatement()) {
1389+
try (ResultSet rs = st.executeQuery(TEST_TABLE.selectColumn("c_BankDecimal"))) {
1390+
assertNextBankDecimal(rs, 1, BigDecimal.valueOf(1.5d).setScale(9));
1391+
assertNextBankDecimal(rs, 2, BigDecimal.valueOf(-1234, 9));
1392+
assertNextBankDecimal(rs, 3, closeToInf);
1393+
assertNextBankDecimal(rs, 4, closeToNegInf);
1394+
1395+
Assertions.assertFalse(rs.next());
1396+
}
1397+
}
1398+
};
1399+
1400+
private void assertNextBankDecimal(ResultSet rs, int key, BigDecimal bg) throws SQLException {
1401+
Assertions.assertTrue(rs.next());
1402+
Assertions.assertEquals(key, rs.getInt("key"));
1403+
1404+
Object obj = rs.getObject("c_BankDecimal");
1405+
Assertions.assertTrue(obj instanceof BigDecimal);
1406+
Assertions.assertEquals(bg, obj);
1407+
1408+
BigDecimal decimal = rs.getBigDecimal("c_BankDecimal");
1409+
Assertions.assertEquals(bg, decimal);
1410+
}
13021411
}

0 commit comments

Comments
 (0)