Skip to content

Commit 3f2502c

Browse files
committed
Updates tests: added Uuid, Decimal(35, 0), Decimal(31, 9)
1 parent 8225fae commit 3f2502c

19 files changed

+1104
-434
lines changed

jdbc/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<environmentVariables>
7878
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
7979
<YDB_DOCKER_IMAGE>ydbplatform/local-ydb:trunk</YDB_DOCKER_IMAGE>
80+
<YDB_DOCKER_FEATURE_FLAGS>enable_parameterized_decimal</YDB_DOCKER_FEATURE_FLAGS>
8081
</environmentVariables>
8182
<systemPropertyVariables>
8283
<java.util.logging.config.file>src/test/resources/logging.properties</java.util.logging.config.file>

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ static Getters buildGetters(Type type) {
6464
castToBooleanNotSupported(clazz),
6565
castToByteNotSupported(clazz),
6666
castToShortNotSupported(clazz),
67-
value -> value.getDecimal().toBigInteger().intValue(),
68-
value -> value.getDecimal().toBigInteger().longValue(),
67+
value -> safeDecimalInt(value.getDecimal()),
68+
value -> safeDecimalLong(value.getDecimal()),
6969
value -> value.getDecimal().toBigDecimal().floatValue(),
7070
value -> value.getDecimal().toBigDecimal().doubleValue(),
7171
castToBytesNotSupported(clazz),
@@ -235,6 +235,28 @@ private static ValueToBoolean valueToBoolean(PrimitiveType id) {
235235
}
236236
}
237237

238+
private static int safeDecimalInt(DecimalValue value) throws SQLException {
239+
if (value.isInf() || value.isNegativeInf() || value.isNan()) {
240+
throw cannotConvert(value.getType(), int.class, value.toString());
241+
}
242+
try {
243+
return value.toBigDecimal().intValueExact();
244+
} catch (ArithmeticException ex) {
245+
throw cannotConvert(value.getType(), int.class, value.toString());
246+
}
247+
}
248+
249+
private static long safeDecimalLong(DecimalValue value) throws SQLException {
250+
if (value.isInf() || value.isNegativeInf() || value.isNan()) {
251+
throw cannotConvert(value.getType(), long.class, value.toString());
252+
}
253+
try {
254+
return value.toBigDecimal().longValueExact();
255+
} catch (ArithmeticException ex) {
256+
throw cannotConvert(value.getType(), long.class, value.toString());
257+
}
258+
}
259+
238260
private static byte checkByteValue(PrimitiveType id, int value) throws SQLException {
239261
int ch = value >= 0 ? value : ~value;
240262
if ((ch & 0x7F) != ch) {
@@ -574,8 +596,9 @@ private static SqlType buildPrimitiveType(int sqlType, PrimitiveType id) {
574596
case Text:
575597
case Json:
576598
case JsonDocument:
577-
case Uuid:
578599
return new SqlType(sqlType, String.class);
600+
case Uuid:
601+
return new SqlType(sqlType, UUID.class);
579602
case Bytes:
580603
case Yson:
581604
return new SqlType(sqlType, byte[].class);
@@ -857,7 +880,7 @@ private static ValueToClass valueToClass(PrimitiveType id) {
857880
}
858881
}
859882

860-
private static SQLException cannotConvert(PrimitiveType type, Class<?> javaType, Object value) {
883+
private static SQLException cannotConvert(Type type, Class<?> javaType, Object value) {
861884
return new SQLException(String.format(YdbConst.UNABLE_TO_CONVERT, type, value, javaType));
862885
}
863886

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbTypes.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17+
import java.util.UUID;
1718

1819
import tech.ydb.jdbc.YdbConst;
1920
import tech.ydb.table.values.DecimalType;
@@ -112,6 +113,7 @@ private YdbTypes() {
112113
typeByClass.put(String.class, PrimitiveType.Text);
113114
typeByClass.put(byte[].class, PrimitiveType.Bytes);
114115

116+
typeByClass.put(UUID.class, PrimitiveType.Uuid);
115117
typeByClass.put(java.sql.Date.class, PrimitiveType.Date);
116118
typeByClass.put(LocalDate.class, PrimitiveType.Date);
117119
typeByClass.put(LocalDateTime.class, PrimitiveType.Datetime);
@@ -211,11 +213,7 @@ private int toSqlTypeImpl(Type type) {
211213
case OPTIONAL:
212214
return toSqlTypeImpl(type.unwrapOptional());
213215
case DECIMAL:
214-
DecimalType decimal = (DecimalType) type;
215-
if (DecimalType.getDefault().equals(type)) {
216-
return Types.DECIMAL;
217-
}
218-
return YdbConst.SQL_KIND_DECIMAL + decimal.getPrecision() << 5 + decimal.getScale();
216+
return Types.DECIMAL;
219217
case STRUCT:
220218
return Types.STRUCT;
221219
case LIST:

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,17 +606,24 @@ public void getColumns() throws SQLException {
606606
rs.nextRow(columnName.eq("c_Yson"), dataType.eq(Types.BINARY), typeName.eq("Yson"),
607607
columnSize.eq(YdbConst.MAX_COLUMN_SIZE), ordinal.eq(17)).assertAll();
608608

609+
rs.nextRow(columnName.eq("c_Uuid"), dataType.eq(Types.VARCHAR), typeName.eq("Uuid"),
610+
columnSize.eq(16), ordinal.eq(18)).assertAll();
611+
609612
rs.nextRow(columnName.eq("c_Date"), dataType.eq(Types.DATE), typeName.eq("Date"),
610-
columnSize.eq(10), ordinal.eq(18)).assertAll();
613+
columnSize.eq(10), ordinal.eq(19)).assertAll();
611614
rs.nextRow(columnName.eq("c_Datetime"), dataType.eq(Types.TIMESTAMP), typeName.eq("Datetime"),
612-
columnSize.eq(19), ordinal.eq(19)).assertAll();
615+
columnSize.eq(19), ordinal.eq(20)).assertAll();
613616
rs.nextRow(columnName.eq("c_Timestamp"), dataType.eq(Types.TIMESTAMP), typeName.eq("Timestamp"),
614-
columnSize.eq(26), ordinal.eq(20)).assertAll();
617+
columnSize.eq(26), ordinal.eq(21)).assertAll();
615618
rs.nextRow(columnName.eq("c_Interval"), dataType.eq(Types.BIGINT), typeName.eq("Interval"),
616-
columnSize.eq(8), ordinal.eq(21)).assertAll();
619+
columnSize.eq(8), ordinal.eq(22)).assertAll();
617620

618621
rs.nextRow(columnName.eq("c_Decimal"), dataType.eq(Types.DECIMAL), typeName.eq("Decimal(22, 9)"),
619-
columnSize.eq(22), ordinal.eq(22), decimalDigits.eq(22)).assertAll();
622+
columnSize.eq(22), ordinal.eq(23), decimalDigits.eq(22)).assertAll();
623+
rs.nextRow(columnName.eq("c_BigDecimal"), dataType.eq(Types.DECIMAL), typeName.eq("Decimal(35, 0)"),
624+
columnSize.eq(35), ordinal.eq(24), decimalDigits.eq(35)).assertAll();
625+
rs.nextRow(columnName.eq("c_BankDecimal"), dataType.eq(Types.DECIMAL), typeName.eq("Decimal(31, 9)"),
626+
columnSize.eq(31), ordinal.eq(25), decimalDigits.eq(31)).assertAll();
620627

621628
rs.assertNoRows();
622629

0 commit comments

Comments
 (0)