Skip to content

Commit d97c393

Browse files
feat: Decimal (22, 9), (35, 0), (31, 9), (35, 9)
1 parent afb6b20 commit d97c393

File tree

7 files changed

+57
-28
lines changed

7 files changed

+57
-28
lines changed

hibernate-dialect/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<log4j2.version>2.17.2</log4j2.version>
4343

4444
<ydb.sdk.version>2.3.4</ydb.sdk.version>
45-
<ydb.jdbc.version>2.3.2</ydb.jdbc.version>
45+
<ydb.jdbc.version>2.3.3</ydb.jdbc.version>
4646
</properties>
4747

4848
<licenses>

hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@
7171
import tech.ydb.hibernate.dialect.types.LocalDateJdbcType;
7272
import tech.ydb.hibernate.dialect.types.LocalDateTimeJavaType;
7373
import tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType;
74-
import static tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType.JDBC_TYPE_DATETIME_CODE;
7574
import tech.ydb.hibernate.dialect.types.Uint8JdbcType;
7675

76+
import static tech.ydb.hibernate.dialect.code.YdbJdbcCode.DECIMAL_SHIFT;
77+
import static tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType.JDBC_TYPE_DATETIME_CODE;
78+
7779
/**
7880
* @author Kirill Kurdyukov
7981
*/
8082
public class YdbDialect extends Dialect {
81-
private static final int SHIFT_DECIMAL_CODE = 11000;
8283
private static final Exporter<ForeignKey> FOREIGN_KEY_EMPTY_EXPORTER = new EmptyExporter<>();
8384
private static final Exporter<Constraint> UNIQUE_KEY_EMPTY_EXPORTER = new EmptyExporter<>();
8485
private static final List<QueryHintHandler> QUERY_HINT_HANDLERS = List.of(
@@ -125,11 +126,14 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry
125126
typeContributions.contributeJdbcType(LocalDateJdbcType.INSTANCE);
126127
typeContributions.contributeJavaType(InstantJavaType.INSTANCE);
127128
typeContributions.contributeJdbcType(InstantJdbcType.INSTANCE);
129+
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9));
130+
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_31_9));
131+
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_0));
132+
typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_9));
128133

129134
// custom jdbc codec
130135
typeContributions.contributeJdbcType(Uint8JdbcType.INSTANCE);
131136
typeContributions.contributeJavaType(BigDecimalJavaType.INSTANCE_22_9);
132-
133137
}
134138

135139
@Override
@@ -140,6 +144,10 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR
140144

141145
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DATETIME, "Datetime", "Datetime", this));
142146
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.UINT8, "Uint8", "Uint8", this));
147+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_22_9, "Decimal(22, 9)", "Decimal(22, 9)", this));
148+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_31_9, "Decimal(31, 9)", "Decimal(31, 9)", this));
149+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_0, "Decimal(35, 0)", "Decimal(35, 0)", this));
150+
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_9, "Decimal(35, 9)", "Decimal(35, 9)", this));
143151
}
144152

145153
@Override
@@ -150,7 +158,7 @@ public JdbcType resolveSqlTypeDescriptor(
150158
int scale,
151159
JdbcTypeRegistry jdbcTypeRegistry) {
152160
if ((jdbcTypeCode == NUMERIC || jdbcTypeCode == DECIMAL) && (precision != 0 || scale != 0)) {
153-
int sqlCode = SHIFT_DECIMAL_CODE + (precision + scale) * (precision + scale + 1) / 2 + precision;
161+
int sqlCode = DECIMAL_SHIFT + (precision << 6) + scale;
154162

155163
return DECIMAL_JDBC_TYPE_CACHE.computeIfAbsent(sqlCode, DecimalJdbcType::new);
156164
}
@@ -174,11 +182,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
174182

175183
functionContributions.getFunctionRegistry().register(
176184
"current_time",
177-
new CurrentFunction(
178-
"current_time",
179-
currentTime(),
180-
localDateTimeType
181-
)
185+
new CurrentFunction("current_time", currentTime(), localDateTimeType)
182186
);
183187
}
184188

hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbJdbcCode.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ public final class YdbJdbcCode {
125125
*/
126126
public static final int JSON_DOCUMENT = 10023;
127127

128-
public static final int DECIMAL_22_9 = 10024;
128+
public static final int DECIMAL_SHIFT = (1 << 14);
129129

130-
public static final int DECIMAL_31_9 = 10025;
130+
public static final int DECIMAL_22_9 = DECIMAL_SHIFT + (22 << 6) + 9;
131131

132-
public static final int DECIMAL_35_9 = 10026;
132+
public static final int DECIMAL_31_9 = DECIMAL_SHIFT + (31 << 6) + 9;
133+
134+
public static final int DECIMAL_35_0 = DECIMAL_SHIFT + (35 << 6);
135+
136+
public static final int DECIMAL_35_9 = DECIMAL_SHIFT + (35 << 6) + 9;
133137
}

hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public DecimalJdbcType(int sqlCode) {
1818
this.sqlCode = sqlCode;
1919
}
2020

21+
@Override
22+
public int getJdbcTypeCode() {
23+
return sqlCode;
24+
}
25+
2126
@Override
2227
public <X> ValueBinder<X> getBinder(final JavaType<X> javaType) {
2328
return new ValueBinder<>() {

hibernate-dialect/src/test/java/tech/ydb/hibernate/TestUtils.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@ public static Configuration basedConfiguration() {
2828
}
2929

3030
public static String jdbcUrl(YdbHelperExtension ydb) {
31-
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
32-
.append(ydb.useTls() ? "grpcs://" : "grpc://")
33-
.append(ydb.endpoint())
34-
.append(ydb.database());
35-
36-
if (ydb.authToken() != null) {
37-
jdbc.append("?").append("token=").append(ydb.authToken());
38-
}
39-
40-
return jdbc.toString();
31+
return "jdbc:ydb:" +
32+
(ydb.useTls() ? "grpcs://" : "grpc://") +
33+
ydb.endpoint() +
34+
ydb.database();
4135
}
4236

4337
public static void inTransaction(Consumer<Session> work) {

hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/TestEntity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ public class TestEntity {
2828
@Column
2929
private BigDecimal default_bigDecimal;
3030

31-
@Column(precision = 3)
32-
private BigDecimal bigDecimal3_0;
31+
@Column
32+
@JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9)
33+
private BigDecimal bigDecimal31_9;
3334

34-
@Column(precision = 35, columnDefinition = "DECIMAL (35, 0)")
35+
@Column
36+
@JdbcTypeCode(YdbJdbcCode.DECIMAL_35_0)
3537
private BigDecimal bigDecimal35_0;
38+
39+
@Column
40+
@JdbcTypeCode(YdbJdbcCode.DECIMAL_35_9)
41+
private BigDecimal bigDecimal35_9;
3642
}

hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/YdbJdbcCodeTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,26 @@ public class YdbJdbcCodeTests {
2323
public void integrationTests() {
2424
SESSION_FACTORY = basedConfiguration()
2525
.addAnnotatedClass(TestEntity.class)
26-
.setProperty(AvailableSettings.URL, jdbcUrl(ydb))
26+
.setProperty(AvailableSettings.URL, jdbcUrl(ydb) + "?disablePrepareDataQuery=true")
2727
.buildSessionFactory();
2828

29-
var testEntity = new TestEntity(1, new BigDecimal("123.000000000"), new BigDecimal(123), new BigDecimal("12345678123"));
29+
/*
30+
create table hibernate_test (
31+
default_bigDecimal Decimal(22, 9),
32+
bigDecimal31_9 Decimal(31, 9),
33+
bigDecimal35_0 Decimal(35, 0),
34+
bigDecimal35_9 Decimal(35, 9),
35+
id Uint8 not null,
36+
primary key (id)
37+
)
38+
*/
39+
var testEntity = new TestEntity(
40+
1,
41+
new BigDecimal("123.000000000"),
42+
new BigDecimal("123.000000000"),
43+
new BigDecimal("12345678123"),
44+
new BigDecimal("12345678123.000000000")
45+
);
3046

3147
inTransaction(session -> session.persist(testEntity));
3248
inTransaction(session -> assertEquals(testEntity, session.find(TestEntity.class, 1)));

0 commit comments

Comments
 (0)