Skip to content

Commit 895ae01

Browse files
committed
Fix decimal precision in database metadata
1 parent f8253da commit 895ae01

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

jdbc/src/main/java/tech/ydb/jdbc/YdbConst.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ public final class YdbConst {
66
public static final int UNKNOWN_SQL_TYPE = Integer.MIN_VALUE;
77

88
public static final int SQL_KIND_PRIMITIVE = 10000;
9-
public static final int SQL_KIND_DECIMAL = 1 << 15; // 32768
10-
11-
// YDB does not support types with custom precision yet
12-
public static final int SQL_DECIMAL_DEFAULT_PRECISION = 22;
13-
public static final int SQL_DECIMAL_DEFAULT_SCALE = 9;
9+
public static final int SQL_KIND_DECIMAL = 1 << 14; // 16384
1410

1511
// Built-in limits
1612
public static final int MAX_PRIMARY_KEY_SIZE = 1024 * 1024; // 1 MiB per index

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import tech.ydb.table.description.TableIndex;
4040
import tech.ydb.table.result.ResultSetReader;
4141
import tech.ydb.table.settings.DescribeTableSettings;
42+
import tech.ydb.table.values.DecimalType;
4243
import tech.ydb.table.values.PrimitiveType;
4344
import tech.ydb.table.values.Type;
4445

@@ -799,7 +800,14 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
799800
nullable = columnNoNulls;
800801
}
801802

802-
int decimalDigits = type.getKind() == Type.Kind.DECIMAL ? YdbConst.SQL_DECIMAL_DEFAULT_PRECISION : 0;
803+
int decimalDigits = 0;
804+
if (type.getKind() == Type.Kind.DECIMAL) {
805+
if (type instanceof DecimalType) {
806+
decimalDigits = ((DecimalType) type).getPrecision();
807+
} else {
808+
decimalDigits = DecimalType.getDefault().getPrecision();
809+
}
810+
}
803811

804812
rs.newRow()
805813
.withTextValue("TABLE_NAME", tableName)
@@ -874,11 +882,17 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
874882
for (String key : description.getPrimaryKeys()) {
875883
TableColumn column = columnMap.get(key);
876884
Type type = column.getType();
885+
int decimalDigits = 0;
877886
if (type.getKind() == Type.Kind.OPTIONAL) {
878887
type = type.unwrapOptional();
879888
}
880-
881-
int decimalDigits = type.getKind() == Type.Kind.DECIMAL ? YdbConst.SQL_DECIMAL_DEFAULT_PRECISION : 0;
889+
if (type.getKind() == Type.Kind.DECIMAL) {
890+
if (type instanceof DecimalType) {
891+
decimalDigits = ((DecimalType) type).getPrecision();
892+
} else {
893+
decimalDigits = DecimalType.getDefault().getPrecision();
894+
}
895+
}
882896

883897
rs.newRow()
884898
.withShortValue("SCOPE", (short) scope)
@@ -962,7 +976,15 @@ public ResultSet getTypeInfo() {
962976

963977
for (Type type: YdbTypes.getAllDatabaseTypes()) {
964978
String literal = getLiteral(type);
965-
int scale = type.getKind() == Type.Kind.DECIMAL ? YdbConst.SQL_DECIMAL_DEFAULT_SCALE : 0;
979+
int scale = 0;
980+
if (type.getKind() == Type.Kind.DECIMAL) {
981+
if (type instanceof DecimalType) {
982+
scale = ((DecimalType) type).getScale();
983+
} else {
984+
scale = DecimalType.getDefault().getScale();
985+
}
986+
}
987+
966988
rs.newRow()
967989
.withTextValue("TYPE_NAME", type.toString())
968990
.withIntValue("DATA_TYPE", YdbTypes.toSqlType(type))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private int getSqlPrecisionImpl(Type type) {
245245
case OPTIONAL:
246246
return getSqlPrecisionImpl(type.unwrapOptional());
247247
case DECIMAL:
248-
return 8 + 8;
248+
return ((DecimalType) type).getPrecision();
249249
case PRIMITIVE:
250250
return getSqlPrecisionImpl((PrimitiveType) type);
251251
default:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public void getTypeInfo() throws SQLException {
424424
rs.nextRow(name.eq("Timestamp"), type.eq(Types.TIMESTAMP), precision.eq(26), unsigned.eq(false)).assertAll();
425425
rs.nextRow(name.eq("Interval"), type.eq(Types.BIGINT), precision.eq(8), unsigned.eq(false)).assertAll();
426426

427-
rs.nextRow(name.eq("Decimal(22, 9)"), type.eq(Types.DECIMAL), precision.eq(16),
427+
rs.nextRow(name.eq("Decimal(22, 9)"), type.eq(Types.DECIMAL), precision.eq(22),
428428
unsigned.eq(false), fixedPrec.eq(true), minScale.eq(9), maxScale.eq(9)).assertAll();
429429

430430
rs.assertNoRows();
@@ -616,7 +616,7 @@ public void getColumns() throws SQLException {
616616
columnSize.eq(8), ordinal.eq(21)).assertAll();
617617

618618
rs.nextRow(columnName.eq("c_Decimal"), dataType.eq(Types.DECIMAL), typeName.eq("Decimal(22, 9)"),
619-
columnSize.eq(16), ordinal.eq(22), decimalDigits.eq(22)).assertAll();
619+
columnSize.eq(22), ordinal.eq(22), decimalDigits.eq(22)).assertAll();
620620

621621
rs.assertNoRows();
622622

0 commit comments

Comments
 (0)