From 18032087ce13077cb0404f1ffad8bd9780609992 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Thu, 5 Nov 2026 13:55:05 +0300 Subject: [PATCH 1/8] Support arbitrary Decimal precision and scale in Hibernate and Liquibase --- hibernate-dialect/CHANGELOG.md | 5 + hibernate-dialect/README.md | 20 +++ .../ydb/hibernate/dialect/YdbDialect.java | 8 +- .../dialect/types/DecimalJdbcType.java | 43 ++++- .../hibernate/types/DecimalMappingTest.java | 166 ++++++++++++++++++ .../types/DecimalPrecisionScaleTest.java | 130 ++++++++++++++ liquibase-dialect/CHANGELOG.md | 4 + liquibase-dialect/README.md | 14 ++ .../ydb/liquibase/type/DecimalTypeYdb.java | 35 +++- .../ydb/liquibase/YdbDatabaseDecimalTest.java | 101 +++++++++++ .../liquibase/type/DecimalTypeYdbTest.java | 83 +++++++++ .../changelogs/csv/decimal-insert.csv | 3 + .../changelogs/csv/decimal-upsert.csv | 3 + .../src/test/resources/changelogs/decimal.xml | 50 ++++++ 14 files changed, 654 insertions(+), 11 deletions(-) create mode 100644 hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java create mode 100644 hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java create mode 100644 liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java create mode 100644 liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java create mode 100644 liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv create mode 100644 liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv create mode 100644 liquibase-dialect/src/test/resources/changelogs/decimal.xml diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index dc9e2abd..62ec9d7a 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased ## + +- Support arbitrary YDB Decimal precision and scale through `@Column(precision = ..., scale = ...)`, including JDBC parameter binding. +- Fix Decimal JDBC type codes resolved from native query result metadata. + ## 1.7.1 ## - Fixed default type mapping fop UInt32/Int64/UInt64 diff --git a/hibernate-dialect/README.md b/hibernate-dialect/README.md index 023bcd90..4bbcbc43 100644 --- a/hibernate-dialect/README.md +++ b/hibernate-dialect/README.md @@ -74,6 +74,26 @@ Use this custom dialect just like any other Hibernate dialect. Map your entity classes to database tables and use Hibernate's session factory to perform database operations. +### Decimal precision and scale + +Use `@Column` to select the precision and scale of a `BigDecimal` field: + +```java +@Column(precision = 35, scale = 10) +private BigDecimal amount; +``` + +The dialect uses `Decimal(35,10)` for both schema generation and JDBC parameter +binding. Precision must be between 1 and 35, and scale between 0 and precision. +Without explicit parameters, `BigDecimal` defaults to `Decimal(22,9)` and +`BigInteger` to `Decimal(22,0)`. + +The existing `@JdbcTypeCode(YdbJdbcCode.DECIMAL_22_9)`, `DECIMAL_31_9`, +`DECIMAL_35_0`, and `DECIMAL_35_9` mappings remain available. An explicit +`@JdbcTypeCode` overrides automatic type selection from `@Column`. +`columnDefinition` alone changes DDL without configuring the JDBC parameter +type; use `precision` and `scale` when mapping an existing Decimal column too. + ## Integration with Spring Data JPA Configure Spring Data JPA with Hibernate to use custom YDB dialect diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java index 4cf18c24..08b22e1e 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java @@ -200,6 +200,8 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry typeContributions.contributeJdbcType(new YdbJdbcType(YdbJdbcCode.INTERVAL, Duration.class)); typeContributions.contributeJavaType(BigDecimalJavaType.INSTANCE_22_9); + typeContributions.contributeJdbcType(new DecimalJdbcType(NUMERIC)); + typeContributions.contributeJdbcType(new DecimalJdbcType(DECIMAL)); typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9)); typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_31_9)); typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_0)); @@ -255,7 +257,7 @@ public JdbcType resolveSqlTypeDescriptor( int scale, JdbcTypeRegistry jdbcTypeRegistry) { if ((jdbcTypeCode == NUMERIC || jdbcTypeCode == DECIMAL) && (precision != 0 || scale != 0)) { - int sqlCode = ydbDecimal(precision, scale); + int sqlCode = DecimalJdbcType.jdbcTypeCode(precision, scale); return DECIMAL_JDBC_TYPE_CACHE.computeIfAbsent(sqlCode, DecimalJdbcType::new); } @@ -530,8 +532,4 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() { return null; }; } - - private static int ydbDecimal(int precision, int scale) { - return 1 << 14 + (precision << 6) + (scale & 0x111111); - } } diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java index a0da6926..5215b436 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java @@ -4,18 +4,37 @@ import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.Types; +import org.hibernate.MappingException; import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.AdjustableJdbcType; +import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; /** * @author Kirill Kurdyukov */ -public class DecimalJdbcType extends org.hibernate.type.descriptor.jdbc.DecimalJdbcType { +public class DecimalJdbcType extends org.hibernate.type.descriptor.jdbc.DecimalJdbcType implements AdjustableJdbcType { private final int sqlCode; + private final int defaultSqlCode; public DecimalJdbcType(int sqlCode) { + this(sqlCode, sqlCode); + } + + private DecimalJdbcType(int sqlCode, int defaultSqlCode) { this.sqlCode = sqlCode; + this.defaultSqlCode = defaultSqlCode; + } + + public static int jdbcTypeCode(int precision, int scale) { + if (precision < 1 || precision > 35 || scale < 0 || scale > precision) { + throw new MappingException("Invalid YDB Decimal(" + precision + ", " + scale + + "): precision must be between 1 and 35 and scale between 0 and precision"); + } + return (1 << 14) + (precision << 6) + scale; } @Override @@ -23,6 +42,28 @@ public int getJdbcTypeCode() { return sqlCode; } + @Override + public int getDefaultSqlTypeCode() { + return defaultSqlCode; + } + + @Override + public JdbcType resolveIndicatedType(JdbcTypeIndicators indicators, JavaType javaType) { + if (sqlCode != Types.NUMERIC && sqlCode != Types.DECIMAL) { + return this; + } + + int precision = indicators.getColumnPrecision(); + int scale = indicators.getColumnScale(); + if (precision == JdbcTypeIndicators.NO_COLUMN_PRECISION) { + precision = javaType.getDefaultSqlPrecision(indicators.getDialect(), this); + } + if (scale == JdbcTypeIndicators.NO_COLUMN_SCALE) { + scale = javaType.getDefaultSqlScale(indicators.getDialect(), this); + } + return new DecimalJdbcType(jdbcTypeCode(precision, scale), defaultSqlCode); + } + @Override public ValueBinder getBinder(final JavaType javaType) { return new ValueBinder<>() { diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java new file mode 100644 index 00000000..315e6ee5 --- /dev/null +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java @@ -0,0 +1,166 @@ +package tech.ydb.hibernate.types; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import java.lang.reflect.Proxy; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.PreparedStatement; +import java.sql.Types; +import java.util.concurrent.atomic.AtomicReference; +import org.hibernate.MappingException; +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; +import org.hibernate.mapping.BasicValue; +import org.hibernate.type.descriptor.jdbc.JdbcType; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import tech.ydb.hibernate.dialect.YdbDialect; +import tech.ydb.hibernate.dialect.code.YdbJdbcCode; + +public class DecimalMappingTest { + private static StandardServiceRegistry registry; + private static Metadata metadata; + private static YdbDialect dialect; + + @BeforeAll + static void setUp() { + DialectResolutionInfo info = (DialectResolutionInfo) Proxy.newProxyInstance( + DecimalMappingTest.class.getClassLoader(), new Class[]{DialectResolutionInfo.class}, + (proxy, method, args) -> { + if (method.getReturnType() == int.class) { + return 1; + } + return method.getReturnType() == String.class ? "YDB" : null; + }); + dialect = new YdbDialect(info); + registry = new StandardServiceRegistryBuilder() + .applySetting("hibernate.dialect", dialect) + .applySetting("hibernate.temp.use_jdbc_metadata_defaults", false) + .build(); + metadata = new MetadataSources(registry).addAnnotatedClass(DecimalColumns.class).buildMetadata(); + } + + @AfterAll + static void tearDown() { + StandardServiceRegistryBuilder.destroy(registry); + } + + @Test + void mapsColumnPrecisionAndScale() throws Exception { + assertMapping("amount", 18, 2, Types.NUMERIC); + assertMapping("largeAmount", 35, 10, Types.NUMERIC); + assertMapping("fraction", 35, 35, Types.NUMERIC); + assertMapping("integerAmount", 35, 0, Types.NUMERIC); + } + + @Test + void preservesDefaultAndFixedMappings() throws Exception { + assertMapping("defaultDecimal", 22, 9, Types.NUMERIC); + assertMapping("defaultInteger", 22, 0, Types.NUMERIC); + assertMapping("fixed22_9", 22, 9, YdbJdbcCode.DECIMAL_22_9); + assertMapping("fixed31_9", 31, 9, YdbJdbcCode.DECIMAL_31_9); + assertMapping("fixed35_0", 35, 0, YdbJdbcCode.DECIMAL_35_0); + assertMapping("fixed35_9", 35, 9, YdbJdbcCode.DECIMAL_35_9); + } + + @Test + void rejectsInvalidColumnParameters() { + assertThrows(MappingException.class, + () -> new MetadataSources(registry).addAnnotatedClass(InvalidPrecision.class).buildMetadata()); + assertThrows(MappingException.class, + () -> new MetadataSources(registry).addAnnotatedClass(InvalidScale.class).buildMetadata()); + } + + @Test + void resolvesNativeResultMetadata() { + for (int jdbcCode : new int[]{Types.NUMERIC, Types.DECIMAL}) { + JdbcType type = dialect.resolveSqlTypeDescriptor("Decimal(35, 10)", jdbcCode, 35, 10, + ((MetadataImplementor) metadata).getTypeConfiguration().getJdbcTypeRegistry()); + assertEquals(18634, type.getJdbcTypeCode()); + } + } + + private static void assertMapping(String field, int precision, int scale, int ddlCode) throws Exception { + BasicValue value = (BasicValue) metadata.getEntityBinding(DecimalColumns.class.getName()) + .getProperty(field).getValue(); + org.hibernate.mapping.Column column = (org.hibernate.mapping.Column) value.getSelectables().get(0); + assertEquals("Decimal(" + precision + ", " + scale + ")", column.getSqlType(metadata)); + JdbcType jdbcType = value.resolve().getJdbcType(); + assertEquals(ddlCode, jdbcType.getDefaultSqlTypeCode()); + assertEquals(ddlCode, jdbcType.getDdlTypeCode()); + int decimalCode = jdbcType.getJdbcTypeCode() - (1 << 14); + assertEquals(precision, decimalCode >> 6); + assertEquals(scale, decimalCode & 63); + assertBinding(value.resolve(), BigDecimal.ZERO, jdbcType.getJdbcTypeCode()); + assertBinding(value.resolve(), null, jdbcType.getJdbcTypeCode()); + } + + private static void assertBinding(BasicValue.Resolution resolution, BigDecimal value, int jdbcCode) + throws Exception { + AtomicReference bound = new AtomicReference<>(); + PreparedStatement statement = (PreparedStatement) Proxy.newProxyInstance( + DecimalMappingTest.class.getClassLoader(), new Class[]{PreparedStatement.class}, + (proxy, method, args) -> { + if (method.getName().equals("setObject")) { + bound.set(args); + } + return null; + }); + var javaType = resolution.getDomainJavaType(); + resolution.getJdbcType().getBinder(javaType).bind(statement, javaType.wrap(value, null), 1, null); + assertEquals(1, bound.get()[0]); + assertEquals(value, bound.get()[1]); + assertEquals(jdbcCode, bound.get()[2]); + } + + @Entity + public static class DecimalColumns { + @Id + private long id; + @Column(precision = 18, scale = 2) + private BigDecimal amount; + @Column(precision = 35, scale = 10) + private BigDecimal largeAmount; + @Column(precision = 35, scale = 35) + private BigDecimal fraction; + @Column(precision = 35, scale = 0) + private BigDecimal integerAmount; + private BigDecimal defaultDecimal; + private BigInteger defaultInteger; + @JdbcTypeCode(YdbJdbcCode.DECIMAL_22_9) + private BigDecimal fixed22_9; + @Column(precision = 18, scale = 2) + @JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9) + private BigDecimal fixed31_9; + @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_0) + private BigDecimal fixed35_0; + @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_9) + private BigDecimal fixed35_9; + } + + @Entity + public static class InvalidPrecision { + @Id + private long id; + @Column(precision = 36, scale = 0) + private BigDecimal amount; + } + + @Entity + public static class InvalidScale { + @Id + private long id; + @Column(precision = 18, scale = 19) + private BigDecimal amount; + } +} diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java new file mode 100644 index 00000000..df1d55c2 --- /dev/null +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java @@ -0,0 +1,130 @@ +package tech.ydb.hibernate.types; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.math.BigDecimal; +import java.math.BigInteger; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.AvailableSettings; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import static tech.ydb.hibernate.TestUtils.basedConfiguration; +import static tech.ydb.hibernate.TestUtils.jdbcUrl; +import tech.ydb.test.junit5.YdbHelperExtension; + +public class DecimalPrecisionScaleTest { + @RegisterExtension + private static final YdbHelperExtension ydb = new YdbHelperExtension(); + + private static SessionFactory sessionFactory; + + @BeforeAll + static void setUp() { + sessionFactory = basedConfiguration().addAnnotatedClass(DecimalEntity.class) + .setProperty(AvailableSettings.URL, jdbcUrl(ydb) + "?disablePrepareDataQuery=true") + .setProperty(AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, "drop-and-create") + .setProperty(AvailableSettings.STATEMENT_BATCH_SIZE, "3") + .buildSessionFactory(); + } + + @AfterAll + static void tearDown() { + if (sessionFactory != null) { + sessionFactory.close(); + } + } + + @Test + void persistsBatchWithLargeValuesZerosAndNulls() { + DecimalEntity large = largeValues(1); + DecimalEntity zero = new DecimalEntity(2, new BigDecimal("0.00"), new BigDecimal("0.0000000000"), + new BigDecimal("0.00000000000000000000000000000000000"), BigDecimal.ZERO, + new BigDecimal("0.000000000"), BigInteger.ZERO); + DecimalEntity empty = new DecimalEntity(3, null, null, null, null, null, null); + sessionFactory.inTransaction(session -> { + session.persist(large); + session.persist(zero); + session.persist(empty); + }); + sessionFactory.inTransaction(session -> { + assertEquals(large, session.find(DecimalEntity.class, 1L)); + assertEquals(zero, session.find(DecimalEntity.class, 2L)); + assertEquals(empty, session.find(DecimalEntity.class, 3L)); + }); + } + + @Test + void updatesAndQueriesWithInferredParameters() { + DecimalEntity entity = largeValues(10); + sessionFactory.inTransaction(session -> session.persist(entity)); + entity.setLargeAmount(new BigDecimal("-1234567890123456789012345.9876543210")); + entity.setFraction(new BigDecimal("-0.12345678901234567890123456789012345")); + sessionFactory.inTransaction(session -> session.createMutationQuery( + "update DecimalEntity e set e.largeAmount = :amount, e.fraction = :fraction where e.id = :id") + .setParameter("amount", entity.getLargeAmount()) + .setParameter("fraction", entity.getFraction()) + .setParameter("id", entity.getId()) + .executeUpdate()); + sessionFactory.inTransaction(session -> assertEquals(entity, + session.createQuery("from DecimalEntity e where e.largeAmount = :amount and e.fraction = :fraction", + DecimalEntity.class) + .setParameter("amount", entity.getLargeAmount()) + .setParameter("fraction", entity.getFraction()) + .getSingleResult())); + entity.setIntegerAmount(new BigDecimal("-12345678901234567890123456789012345")); + sessionFactory.inTransaction(session -> session.merge(entity)); + sessionFactory.inTransaction(session -> assertEquals(entity, session.find(DecimalEntity.class, entity.getId()))); + } + + @Test + void readsDecimalNativeQueryMetadata() { + DecimalEntity entity = largeValues(20); + sessionFactory.inTransaction(session -> session.persist(entity)); + sessionFactory.inTransaction(session -> { + Object[] actual = session.createNativeQuery( + "SELECT amount, largeAmount, fraction, integerAmount FROM decimal_precision_scale WHERE id = :id", + Object[].class) + .setParameter("id", entity.getId()) + .getSingleResult(); + assertArrayEquals(new Object[]{entity.getAmount(), entity.getLargeAmount(), entity.getFraction(), + entity.getIntegerAmount()}, actual); + }); + } + + private static DecimalEntity largeValues(long id) { + return new DecimalEntity(id, new BigDecimal("1234567890123456.78"), + new BigDecimal("1234567890123456789012345.1234567890"), + new BigDecimal("0.12345678901234567890123456789012345"), + new BigDecimal("12345678901234567890123456789012345"), + new BigDecimal("1234567890123.123456789"), new BigInteger("1234567890123456789012")); + } + + @Entity(name = "DecimalEntity") + @Table(name = "decimal_precision_scale") + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class DecimalEntity { + @Id + private long id; + @Column(precision = 18, scale = 2) + private BigDecimal amount; + @Column(precision = 35, scale = 10) + private BigDecimal largeAmount; + @Column(precision = 35, scale = 35) + private BigDecimal fraction; + @Column(precision = 35, scale = 0) + private BigDecimal integerAmount; + private BigDecimal defaultDecimal; + private BigInteger defaultInteger; + } +} diff --git a/liquibase-dialect/CHANGELOG.md b/liquibase-dialect/CHANGELOG.md index 2ee209b6..6f21b1d0 100644 --- a/liquibase-dialect/CHANGELOG.md +++ b/liquibase-dialect/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased ## + +* Support parameterized `Decimal(precision,scale)` in schema changes and data migrations, retaining `Decimal(22,9)` as the default. + ## 1.1.2 ## * Fixed the `liquibase tag` command failing on YDB due to an unsupported generic `UPDATE` query ([#236](https://github.com/ydb-platform/ydb-java-dialects/issues/236)). diff --git a/liquibase-dialect/README.md b/liquibase-dialect/README.md index 44483b14..48f8885b 100644 --- a/liquibase-dialect/README.md +++ b/liquibase-dialect/README.md @@ -79,6 +79,20 @@ cp ydb-jdbc-driver-shaded-${version-jdbc}.jar /usr/local/opt/liquibase/internal/ Use this custom dialect just like any other Liquibase dialect. +### Decimal precision and scale + +Specify both precision and scale to use a parameterized YDB decimal: + +```xml + +``` + +YDB supports precision from 1 to 35 and scale from 0 to precision. The aliases +`java.sql.Types.DECIMAL` and `java.math.BigDecimal` accept the same parameters. +Without parameters, `decimal` retains the default `Decimal(22,9)`. +The declared parameters are preserved when creating or adding columns, generating +changelogs, inserting values, and loading or updating data from CSV files. + ## Integration with Spring Boot ```properties diff --git a/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java b/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java index f407cb9f..5182c544 100644 --- a/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java +++ b/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java @@ -4,6 +4,7 @@ import liquibase.datatype.DataTypeInfo; import liquibase.datatype.DatabaseDataType; import liquibase.datatype.LiquibaseDataType; +import liquibase.exception.UnexpectedLiquibaseException; /** * @author Kirill Kurdyukov @@ -11,7 +12,7 @@ @DataTypeInfo( name = "Decimal", aliases = {"java.sql.Types.DECIMAL", "java.math.BigDecimal"}, - minParameters = 2, + minParameters = 0, maxParameters = 2, priority = LiquibaseDataType.PRIORITY_DATABASE ) @@ -19,17 +20,41 @@ public class DecimalTypeYdb extends BaseTypeYdb { @Override public boolean validate(Database database) { - return super.validate(database) && - getParameters()[0].equals(22) && getParameters()[1].equals(9); // Fixed Decimal(22,9) + decimalType(); + return true; } @Override public DatabaseDataType toDatabaseDataType(Database database) { - return new DatabaseDataType("DECIMAL(22,9)"); + return new DatabaseDataType(decimalType()); } @Override protected String objectToSql(Object value) { - return "CAST('" + value + "' AS DECIMAL(22,9))"; + return "CAST('" + value + "' AS " + decimalType() + ")"; + } + + private String decimalType() { + Object[] parameters = getParameters(); + if (parameters.length == 0) { + return "DECIMAL(22,9)"; + } + if (parameters.length != 2) { + throw new UnexpectedLiquibaseException("YDB Decimal requires both precision and scale, or no parameters"); + } + + int precision; + int scale; + try { + precision = Integer.parseInt(parameters[0].toString()); + scale = Integer.parseInt(parameters[1].toString()); + } catch (NumberFormatException e) { + throw new UnexpectedLiquibaseException("YDB Decimal precision and scale must be integers", e); + } + if (precision < 1 || precision > 35 || scale < 0 || scale > precision) { + throw new UnexpectedLiquibaseException( + "YDB Decimal requires 1 <= precision <= 35 and 0 <= scale <= precision"); + } + return "DECIMAL(" + precision + "," + scale + ")"; } } diff --git a/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java b/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java new file mode 100644 index 00000000..ed3af9ac --- /dev/null +++ b/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java @@ -0,0 +1,101 @@ +package tech.ydb.liquibase; + +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.LinkedHashMap; +import java.util.Map; +import liquibase.Contexts; +import liquibase.Liquibase; +import liquibase.command.CommandScope; +import liquibase.database.jvm.JdbcConnection; +import liquibase.resource.DirectoryResourceAccessor; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class YdbDatabaseDecimalTest extends BaseTest { + + @Test + void migrateLoadAndRegenerateParameterizedDecimals(@TempDir Path tempDir) throws Exception { + migrateChangeFile("./changelogs/decimal.xml"); + + try (Connection connection = DriverManager.getConnection(jdbcUrl())) { + assertDecimalTypes(connection); + try (Statement statement = connection.createStatement(); + ResultSet rows = statement.executeQuery("SELECT * FROM decimal_values ORDER BY id")) { + for (int id = 1; id <= 5; id++) { + assertTrue(rows.next()); + assertEquals(id, rows.getInt("id")); + if (id == 4) { + assertNull(rows.getBigDecimal("amount")); + assertNull(rows.getBigDecimal("whole_amount")); + assertNull(rows.getBigDecimal("fraction")); + assertNull(rows.getBigDecimal("default_amount")); + assertNull(rows.getBigDecimal("small_amount")); + } else { + String sign = id == 2 || id == 3 ? "-" : ""; + assertEquals(new BigDecimal(sign + "1234567890123456789012345.1234567890"), + rows.getBigDecimal("amount")); + assertEquals(new BigDecimal(sign + "12345678901234567890123456789012345"), + rows.getBigDecimal("whole_amount")); + assertEquals(new BigDecimal(sign + "0.12345678901234567890123456789012345"), + rows.getBigDecimal("fraction")); + assertEquals(new BigDecimal(sign + "1234567890123.123456789"), + rows.getBigDecimal("default_amount")); + assertEquals(new BigDecimal(sign + "9"), rows.getBigDecimal("small_amount")); + } + } + assertFalse(rows.next()); + } + } + + Path changelogFile = tempDir.resolve("decimal-generated.xml"); + new CommandScope("generateChangelog") + .addArgumentValue("url", jdbcUrl()) + .addArgumentValue("changelogFile", changelogFile.toString()) + .addArgumentValue("includeObjects", "table:decimal_values") + .execute(); + String changelog = new String(Files.readAllBytes(changelogFile), StandardCharsets.UTF_8); + assertTrue(changelog.contains("type=\"DECIMAL(35,10)\"")); + assertTrue(changelog.contains("type=\"DECIMAL(35,0)\"")); + assertTrue(changelog.contains("type=\"DECIMAL(35,35)\"")); + assertTrue(changelog.contains("type=\"DECIMAL(22,9)\"")); + assertTrue(changelog.contains("type=\"DECIMAL(1,0)\"")); + + try (Connection connection = DriverManager.getConnection(jdbcUrl()); + Statement statement = connection.createStatement()) { + statement.execute("DROP TABLE decimal_values"); + } + try (Connection connection = DriverManager.getConnection(jdbcUrl()); + DirectoryResourceAccessor accessor = new DirectoryResourceAccessor(tempDir); + Liquibase liquibase = new Liquibase(changelogFile.getFileName().toString(), accessor, + new JdbcConnection(connection))) { + liquibase.update(new Contexts()); + assertDecimalTypes(connection); + } + } + + private static void assertDecimalTypes(Connection connection) throws Exception { + Map types = new LinkedHashMap<>(); + try (ResultSet columns = connection.getMetaData().getColumns(null, null, "decimal_values", null)) { + while (columns.next()) { + types.put(columns.getString("COLUMN_NAME"), columns.getString("TYPE_NAME")); + } + } + assertEquals("Decimal(35, 10)", types.get("amount")); + assertEquals("Decimal(35, 0)", types.get("whole_amount")); + assertEquals("Decimal(35, 35)", types.get("fraction")); + assertEquals("Decimal(22, 9)", types.get("default_amount")); + assertEquals("Decimal(1, 0)", types.get("small_amount")); + } +} diff --git a/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java b/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java new file mode 100644 index 00000000..097c6a80 --- /dev/null +++ b/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java @@ -0,0 +1,83 @@ +package tech.ydb.liquibase.type; + +import java.math.BigDecimal; +import liquibase.datatype.DataTypeFactory; +import liquibase.datatype.LiquibaseDataType; +import liquibase.exception.UnexpectedLiquibaseException; +import org.junit.jupiter.api.Test; +import tech.ydb.liquibase.database.YdbDatabase; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DecimalTypeYdbTest { + + private final YdbDatabase database = new YdbDatabase(); + + @Test + void defaultParameters() { + for (String name : new String[]{"decimal", "java.sql.Types.DECIMAL", "java.math.BigDecimal"}) { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription(name, database); + assertTrue(type.validate(database)); + assertEquals("DECIMAL(22,9)", type.toDatabaseDataType(database).toString()); + assertEquals("CAST('1.23' AS DECIMAL(22,9))", type.objectToSql(new BigDecimal("1.23"), database)); + } + } + + @Test + void explicitParametersAndAliases() { + for (String name : new String[]{"Decimal", "decimal", "java.sql.Types.DECIMAL", "java.math.BigDecimal"}) { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription(name + "(35, 10)", database); + assertTrue(type.validate(database)); + assertEquals("DECIMAL(35,10)", type.toDatabaseDataType(database).toString()); + assertEquals("CAST('1234567890123456789012345.1234567890' AS DECIMAL(35,10))", + type.objectToSql("1234567890123456789012345.1234567890", database)); + } + } + + @Test + void parameterBoundaries() { + for (String parameters : new String[]{"1,0", "1,1", "35,0", "35,35", "22,9"}) { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(" + parameters + ")", database); + assertTrue(type.validate(database)); + assertEquals("DECIMAL(" + parameters + ")", type.toDatabaseDataType(database).toString()); + } + } + + @Test + void numericParameterObjects() { + DecimalTypeYdb type = new DecimalTypeYdb(); + type.addParameter(18); + type.addParameter(2); + assertTrue(type.validate(database)); + assertEquals("DECIMAL(18,2)", type.toDatabaseDataType(database).toString()); + } + + @Test + void invalidParametersAreRejectedDuringValidationAndConversion() { + for (String parameters : new String[]{"10", "10,2,1", "0,0", "36,0", "-1,0", "10,-1", "10,11", + "1.5,0", "10,1.5", "text,0", "10,text", "2147483648,0"}) { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(" + parameters + ")", database); + assertThrows(UnexpectedLiquibaseException.class, () -> type.validate(database), parameters); + assertThrows(UnexpectedLiquibaseException.class, () -> type.toDatabaseDataType(database), parameters); + assertThrows(UnexpectedLiquibaseException.class, () -> type.objectToSql("1", database), parameters); + } + } + + @Test + void invalidNumberRetainsCause() { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(text,0)", database); + UnexpectedLiquibaseException failure = assertThrows(UnexpectedLiquibaseException.class, + () -> type.toDatabaseDataType(database)); + assertInstanceOf(NumberFormatException.class, failure.getCause()); + } + + @Test + void nullValues() { + LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(35,10)", database); + assertEquals("NULL", type.objectToSql(null, database)); + assertEquals("NULL", type.objectToSql("null", database)); + } +} diff --git a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv new file mode 100644 index 00000000..b9d25ef7 --- /dev/null +++ b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv @@ -0,0 +1,3 @@ +id,amount,whole_amount,fraction,default_amount,small_amount +3,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9 +4,null,null,null,null,null diff --git a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv new file mode 100644 index 00000000..a9587c19 --- /dev/null +++ b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv @@ -0,0 +1,3 @@ +id,amount,whole_amount,fraction,default_amount,small_amount +3,-1234567890123456789012345.1234567890,-12345678901234567890123456789012345,-0.12345678901234567890123456789012345,-1234567890123.123456789,-9 +5,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9 diff --git a/liquibase-dialect/src/test/resources/changelogs/decimal.xml b/liquibase-dialect/src/test/resources/changelogs/decimal.xml new file mode 100644 index 00000000..e8eab6f7 --- /dev/null +++ b/liquibase-dialect/src/test/resources/changelogs/decimal.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From ad251eebdc07ce2389172f70addc5d47b1548598 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Thu, 5 Nov 2026 14:38:26 +0300 Subject: [PATCH 2/8] Remove legacy Decimal JDBC codes and Unreleased sections --- hibernate-dialect/CHANGELOG.md | 5 --- hibernate-dialect/README.md | 7 +++-- .../ydb/hibernate/dialect/YdbDialect.java | 8 ----- .../ydb/hibernate/dialect/code/YdbConst.java | 2 -- .../hibernate/dialect/code/YdbJdbcCode.java | 14 --------- .../dialect/types/DecimalJdbcType.java | 2 +- .../hibernate/types/DecimalMappingTest.java | 31 +++++++++---------- .../hibernate/ydb_jdbc_code/TestEntity.java | 9 ++---- liquibase-dialect/CHANGELOG.md | 4 --- 9 files changed, 24 insertions(+), 58 deletions(-) diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index 62ec9d7a..dc9e2abd 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,8 +1,3 @@ -## Unreleased ## - -- Support arbitrary YDB Decimal precision and scale through `@Column(precision = ..., scale = ...)`, including JDBC parameter binding. -- Fix Decimal JDBC type codes resolved from native query result metadata. - ## 1.7.1 ## - Fixed default type mapping fop UInt32/Int64/UInt64 diff --git a/hibernate-dialect/README.md b/hibernate-dialect/README.md index 4bbcbc43..1db4fed2 100644 --- a/hibernate-dialect/README.md +++ b/hibernate-dialect/README.md @@ -88,8 +88,11 @@ binding. Precision must be between 1 and 35, and scale between 0 and precision. Without explicit parameters, `BigDecimal` defaults to `Decimal(22,9)` and `BigInteger` to `Decimal(22,0)`. -The existing `@JdbcTypeCode(YdbJdbcCode.DECIMAL_22_9)`, `DECIMAL_31_9`, -`DECIMAL_35_0`, and `DECIMAL_35_9` mappings remain available. An explicit +The `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9` +constants have been removed. Replace their `@JdbcTypeCode` annotations with +`@Column(precision = ..., scale = ...)` using the same precision and scale. +For example, migrate `@JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9)` to +`@Column(precision = 31, scale = 9)`. Remove the old annotation; an explicit `@JdbcTypeCode` overrides automatic type selection from `@Column`. `columnDefinition` alone changes DDL without configuring the JDBC parameter type; use `precision` and `scale` when mapping an existing Decimal column too. diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java index 08b22e1e..80f8fc51 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java @@ -202,10 +202,6 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry typeContributions.contributeJavaType(BigDecimalJavaType.INSTANCE_22_9); typeContributions.contributeJdbcType(new DecimalJdbcType(NUMERIC)); typeContributions.contributeJdbcType(new DecimalJdbcType(DECIMAL)); - typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_22_9)); - typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_31_9)); - typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_0)); - typeContributions.contributeJdbcType(new DecimalJdbcType(YdbJdbcCode.DECIMAL_35_9)); } @Override @@ -223,10 +219,6 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DATETIME_64, "Datetime64", "Datetime64", this)); ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.TIMESTAMP_64, "Timestamp64", "Timestamp64", this)); ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INTERVAL_64, "Interval64", "Interval64", this)); - ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_22_9, "Decimal(22, 9)", "Decimal(22, 9)", this)); - ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_31_9, "Decimal(31, 9)", "Decimal(31, 9)", this)); - ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_0, "Decimal(35, 0)", "Decimal(35, 0)", this)); - ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.DECIMAL_35_9, "Decimal(35, 9)", "Decimal(35, 9)", this)); ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.JSON, "Json", "Json", this)); ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.JSON_DOCUMENT, "JsonDocument", "JsonDocument", this)); ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.BOOL, "Bool", "Bool", this)); diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java index 06831caf..6d414e69 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java @@ -4,13 +4,11 @@ * That class contain custom YDB type codes * @see JDBC Driver constants * @see Primitive types - * @see Decimal type * * @author Kirill Kurdyukov */ final class YdbConst { public static final int SQL_KIND_PRIMITIVE = 10000; - public static final int SQL_KIND_DECIMAL = 1 << 14; // 16384 private YdbConst() { }; } \ No newline at end of file diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbJdbcCode.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbJdbcCode.java index 45bfa45a..cd31b277 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbJdbcCode.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbJdbcCode.java @@ -132,18 +132,4 @@ public final class YdbJdbcCode { public static final int TIMESTAMP_64 = YdbConst.SQL_KIND_PRIMITIVE + 27; public static final int INTERVAL_64 = YdbConst.SQL_KIND_PRIMITIVE + 28; - - - // WARN! Attribute value must be constant! - - /** - * link - */ - public static final int DECIMAL_22_9 = YdbConst.SQL_KIND_DECIMAL + (22 << 6) + 9; - - public static final int DECIMAL_31_9 = YdbConst.SQL_KIND_DECIMAL + (31 << 6) + 9; - - public static final int DECIMAL_35_0 = YdbConst.SQL_KIND_DECIMAL + (35 << 6); - - public static final int DECIMAL_35_9 = YdbConst.SQL_KIND_DECIMAL + (35 << 6) + 9; } diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java index 5215b436..163fce05 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java @@ -21,7 +21,7 @@ public class DecimalJdbcType extends org.hibernate.type.descriptor.jdbc.DecimalJ private final int defaultSqlCode; public DecimalJdbcType(int sqlCode) { - this(sqlCode, sqlCode); + this(sqlCode, sqlCode == Types.NUMERIC ? Types.NUMERIC : Types.DECIMAL); } private DecimalJdbcType(int sqlCode, int defaultSqlCode) { diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java index 315e6ee5..1186cf61 100644 --- a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java @@ -10,7 +10,6 @@ import java.sql.Types; import java.util.concurrent.atomic.AtomicReference; import org.hibernate.MappingException; -import org.hibernate.annotations.JdbcTypeCode; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; @@ -25,7 +24,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import tech.ydb.hibernate.dialect.YdbDialect; -import tech.ydb.hibernate.dialect.code.YdbJdbcCode; public class DecimalMappingTest { private static StandardServiceRegistry registry; @@ -61,16 +59,16 @@ void mapsColumnPrecisionAndScale() throws Exception { assertMapping("largeAmount", 35, 10, Types.NUMERIC); assertMapping("fraction", 35, 35, Types.NUMERIC); assertMapping("integerAmount", 35, 0, Types.NUMERIC); + assertMapping("decimal22_9", 22, 9, Types.NUMERIC); + assertMapping("decimal31_9", 31, 9, Types.NUMERIC); + assertMapping("decimal35_0", 35, 0, Types.NUMERIC); + assertMapping("decimal35_9", 35, 9, Types.NUMERIC); } @Test - void preservesDefaultAndFixedMappings() throws Exception { + void preservesDefaults() throws Exception { assertMapping("defaultDecimal", 22, 9, Types.NUMERIC); assertMapping("defaultInteger", 22, 0, Types.NUMERIC); - assertMapping("fixed22_9", 22, 9, YdbJdbcCode.DECIMAL_22_9); - assertMapping("fixed31_9", 31, 9, YdbJdbcCode.DECIMAL_31_9); - assertMapping("fixed35_0", 35, 0, YdbJdbcCode.DECIMAL_35_0); - assertMapping("fixed35_9", 35, 9, YdbJdbcCode.DECIMAL_35_9); } @Test @@ -87,6 +85,8 @@ void resolvesNativeResultMetadata() { JdbcType type = dialect.resolveSqlTypeDescriptor("Decimal(35, 10)", jdbcCode, 35, 10, ((MetadataImplementor) metadata).getTypeConfiguration().getJdbcTypeRegistry()); assertEquals(18634, type.getJdbcTypeCode()); + assertEquals(Types.DECIMAL, type.getDefaultSqlTypeCode()); + assertEquals(Types.DECIMAL, type.getDdlTypeCode()); } } @@ -137,15 +137,14 @@ public static class DecimalColumns { private BigDecimal integerAmount; private BigDecimal defaultDecimal; private BigInteger defaultInteger; - @JdbcTypeCode(YdbJdbcCode.DECIMAL_22_9) - private BigDecimal fixed22_9; - @Column(precision = 18, scale = 2) - @JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9) - private BigDecimal fixed31_9; - @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_0) - private BigDecimal fixed35_0; - @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_9) - private BigDecimal fixed35_9; + @Column(precision = 22, scale = 9) + private BigDecimal decimal22_9; + @Column(precision = 31, scale = 9) + private BigDecimal decimal31_9; + @Column(precision = 35, scale = 0) + private BigDecimal decimal35_0; + @Column(precision = 35, scale = 9) + private BigDecimal decimal35_9; } @Entity diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/TestEntity.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/TestEntity.java index c5c3cc03..f370fb9a 100644 --- a/hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/TestEntity.java +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/ydb_jdbc_code/TestEntity.java @@ -28,15 +28,12 @@ public class TestEntity { @Column private BigDecimal default_bigDecimal; - @Column - @JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9) + @Column(precision = 31, scale = 9) private BigDecimal bigDecimal31_9; - @Column - @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_0) + @Column(precision = 35, scale = 0) private BigDecimal bigDecimal35_0; - @Column - @JdbcTypeCode(YdbJdbcCode.DECIMAL_35_9) + @Column(precision = 35, scale = 9) private BigDecimal bigDecimal35_9; } diff --git a/liquibase-dialect/CHANGELOG.md b/liquibase-dialect/CHANGELOG.md index 6f21b1d0..2ee209b6 100644 --- a/liquibase-dialect/CHANGELOG.md +++ b/liquibase-dialect/CHANGELOG.md @@ -1,7 +1,3 @@ -## Unreleased ## - -* Support parameterized `Decimal(precision,scale)` in schema changes and data migrations, retaining `Decimal(22,9)` as the default. - ## 1.1.2 ## * Fixed the `liquibase tag` command failing on YDB due to an unsupported generic `UPDATE` query ([#236](https://github.com/ydb-platform/ydb-java-dialects/issues/236)). From 140a940f8280894245844da802faf0cf4deb6d08 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Thu, 5 Nov 2026 15:02:44 +0300 Subject: [PATCH 3/8] Fix Keycloak test bootstrap with Docker 29 --- keycloak-ydb-extension/test/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keycloak-ydb-extension/test/pom.xml b/keycloak-ydb-extension/test/pom.xml index b6ca97b6..5e02d222 100644 --- a/keycloak-ydb-extension/test/pom.xml +++ b/keycloak-ydb-extension/test/pom.xml @@ -97,7 +97,7 @@ org.testcontainers testcontainers - 1.20.6 + 2.0.3 test From 50e1728dfb540f4510745490402108a488ca8964 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Thu, 5 Nov 2026 16:57:01 +0300 Subject: [PATCH 4/8] Address Decimal review: simplify type resolution and keep integration coverage --- hibernate-dialect/CHANGELOG.md | 6 + hibernate-dialect/README.md | 23 --- .../ydb/hibernate/dialect/YdbDialect.java | 8 +- .../dialect/types/DecimalJdbcType.java | 25 +-- .../hibernate/types/DecimalMappingTest.java | 165 ------------------ .../types/DecimalPrecisionScaleTest.java | 24 +++ liquibase-dialect/CHANGELOG.md | 5 + liquibase-dialect/README.md | 14 -- .../liquibase/type/DecimalTypeYdbTest.java | 83 --------- 9 files changed, 51 insertions(+), 302 deletions(-) delete mode 100644 hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java delete mode 100644 liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index dc9e2abd..b5243e61 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,3 +1,9 @@ +## 1.7.2 ## + +- Support arbitrary YDB Decimal precision and scale through `@Column(precision = ..., scale = ...)` for schema generation and JDBC parameter binding. +- **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9`. Replace their `@JdbcTypeCode` annotations with `@Column(precision = ..., scale = ...)` using the same parameters. Default mappings remain `BigDecimal` to `Decimal(22,9)` and `BigInteger` to `Decimal(22,0)`. +- Fix Decimal JDBC type codes resolved from native query result metadata. + ## 1.7.1 ## - Fixed default type mapping fop UInt32/Int64/UInt64 diff --git a/hibernate-dialect/README.md b/hibernate-dialect/README.md index 1db4fed2..023bcd90 100644 --- a/hibernate-dialect/README.md +++ b/hibernate-dialect/README.md @@ -74,29 +74,6 @@ Use this custom dialect just like any other Hibernate dialect. Map your entity classes to database tables and use Hibernate's session factory to perform database operations. -### Decimal precision and scale - -Use `@Column` to select the precision and scale of a `BigDecimal` field: - -```java -@Column(precision = 35, scale = 10) -private BigDecimal amount; -``` - -The dialect uses `Decimal(35,10)` for both schema generation and JDBC parameter -binding. Precision must be between 1 and 35, and scale between 0 and precision. -Without explicit parameters, `BigDecimal` defaults to `Decimal(22,9)` and -`BigInteger` to `Decimal(22,0)`. - -The `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9` -constants have been removed. Replace their `@JdbcTypeCode` annotations with -`@Column(precision = ..., scale = ...)` using the same precision and scale. -For example, migrate `@JdbcTypeCode(YdbJdbcCode.DECIMAL_31_9)` to -`@Column(precision = 31, scale = 9)`. Remove the old annotation; an explicit -`@JdbcTypeCode` overrides automatic type selection from `@Column`. -`columnDefinition` alone changes DDL without configuring the JDBC parameter -type; use `precision` and `scale` when mapping an existing Decimal column too. - ## Integration with Spring Data JPA Configure Spring Data JPA with Hibernate to use custom YDB dialect diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java index 80f8fc51..681b910f 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java @@ -7,7 +7,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.ConcurrentHashMap; import org.hibernate.boot.model.FunctionContributions; import org.hibernate.boot.model.TypeContributions; import org.hibernate.dialect.Dialect; @@ -92,7 +91,6 @@ public class YdbDialect extends Dialect { @SuppressWarnings({"rawtypes"}) private static final Exporter EMPTY_EXPORTER = new EmptyExporter<>(); - private static final ConcurrentHashMap DECIMAL_JDBC_TYPE_CACHE = new ConcurrentHashMap<>(); public YdbDialect(DialectResolutionInfo dialectResolutionInfo) { super(dialectResolutionInfo); @@ -249,9 +247,9 @@ public JdbcType resolveSqlTypeDescriptor( int scale, JdbcTypeRegistry jdbcTypeRegistry) { if ((jdbcTypeCode == NUMERIC || jdbcTypeCode == DECIMAL) && (precision != 0 || scale != 0)) { - int sqlCode = DecimalJdbcType.jdbcTypeCode(precision, scale); - - return DECIMAL_JDBC_TYPE_CACHE.computeIfAbsent(sqlCode, DecimalJdbcType::new); + DecimalJdbcType type = new DecimalJdbcType(DECIMAL, precision, scale); + jdbcTypeRegistry.addDescriptorIfAbsent(type.getJdbcTypeCode(), type); + return jdbcTypeRegistry.getDescriptor(type.getJdbcTypeCode()); } return super.resolveSqlTypeDescriptor(columnTypeName, jdbcTypeCode, precision, scale, jdbcTypeRegistry); diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java index 163fce05..2d3a8593 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java @@ -21,20 +21,13 @@ public class DecimalJdbcType extends org.hibernate.type.descriptor.jdbc.DecimalJ private final int defaultSqlCode; public DecimalJdbcType(int sqlCode) { - this(sqlCode, sqlCode == Types.NUMERIC ? Types.NUMERIC : Types.DECIMAL); - } - - private DecimalJdbcType(int sqlCode, int defaultSqlCode) { this.sqlCode = sqlCode; - this.defaultSqlCode = defaultSqlCode; + this.defaultSqlCode = sqlCode; } - public static int jdbcTypeCode(int precision, int scale) { - if (precision < 1 || precision > 35 || scale < 0 || scale > precision) { - throw new MappingException("Invalid YDB Decimal(" + precision + ", " + scale - + "): precision must be between 1 and 35 and scale between 0 and precision"); - } - return (1 << 14) + (precision << 6) + scale; + public DecimalJdbcType(int defaultSqlCode, int precision, int scale) { + this.sqlCode = jdbcTypeCode(precision, scale); + this.defaultSqlCode = defaultSqlCode; } @Override @@ -61,7 +54,7 @@ public JdbcType resolveIndicatedType(JdbcTypeIndicators indicators, JavaType if (scale == JdbcTypeIndicators.NO_COLUMN_SCALE) { scale = javaType.getDefaultSqlScale(indicators.getDialect(), this); } - return new DecimalJdbcType(jdbcTypeCode(precision, scale), defaultSqlCode); + return new DecimalJdbcType(defaultSqlCode, precision, scale); } @Override @@ -78,4 +71,12 @@ public void bind(CallableStatement st, X value, String name, WrapperOptions opti } }; } + + private static int jdbcTypeCode(int precision, int scale) { + if (precision < 1 || precision > 35 || scale < 0 || scale > precision) { + throw new MappingException("Invalid YDB Decimal(" + precision + ", " + scale + + "): precision must be between 1 and 35 and scale between 0 and precision"); + } + return (1 << 14) + (precision << 6) + scale; + } } diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java deleted file mode 100644 index 1186cf61..00000000 --- a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalMappingTest.java +++ /dev/null @@ -1,165 +0,0 @@ -package tech.ydb.hibernate.types; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import java.lang.reflect.Proxy; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.sql.PreparedStatement; -import java.sql.Types; -import java.util.concurrent.atomic.AtomicReference; -import org.hibernate.MappingException; -import org.hibernate.boot.Metadata; -import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; -import org.hibernate.boot.spi.MetadataImplementor; -import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo; -import org.hibernate.mapping.BasicValue; -import org.hibernate.type.descriptor.jdbc.JdbcType; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import tech.ydb.hibernate.dialect.YdbDialect; - -public class DecimalMappingTest { - private static StandardServiceRegistry registry; - private static Metadata metadata; - private static YdbDialect dialect; - - @BeforeAll - static void setUp() { - DialectResolutionInfo info = (DialectResolutionInfo) Proxy.newProxyInstance( - DecimalMappingTest.class.getClassLoader(), new Class[]{DialectResolutionInfo.class}, - (proxy, method, args) -> { - if (method.getReturnType() == int.class) { - return 1; - } - return method.getReturnType() == String.class ? "YDB" : null; - }); - dialect = new YdbDialect(info); - registry = new StandardServiceRegistryBuilder() - .applySetting("hibernate.dialect", dialect) - .applySetting("hibernate.temp.use_jdbc_metadata_defaults", false) - .build(); - metadata = new MetadataSources(registry).addAnnotatedClass(DecimalColumns.class).buildMetadata(); - } - - @AfterAll - static void tearDown() { - StandardServiceRegistryBuilder.destroy(registry); - } - - @Test - void mapsColumnPrecisionAndScale() throws Exception { - assertMapping("amount", 18, 2, Types.NUMERIC); - assertMapping("largeAmount", 35, 10, Types.NUMERIC); - assertMapping("fraction", 35, 35, Types.NUMERIC); - assertMapping("integerAmount", 35, 0, Types.NUMERIC); - assertMapping("decimal22_9", 22, 9, Types.NUMERIC); - assertMapping("decimal31_9", 31, 9, Types.NUMERIC); - assertMapping("decimal35_0", 35, 0, Types.NUMERIC); - assertMapping("decimal35_9", 35, 9, Types.NUMERIC); - } - - @Test - void preservesDefaults() throws Exception { - assertMapping("defaultDecimal", 22, 9, Types.NUMERIC); - assertMapping("defaultInteger", 22, 0, Types.NUMERIC); - } - - @Test - void rejectsInvalidColumnParameters() { - assertThrows(MappingException.class, - () -> new MetadataSources(registry).addAnnotatedClass(InvalidPrecision.class).buildMetadata()); - assertThrows(MappingException.class, - () -> new MetadataSources(registry).addAnnotatedClass(InvalidScale.class).buildMetadata()); - } - - @Test - void resolvesNativeResultMetadata() { - for (int jdbcCode : new int[]{Types.NUMERIC, Types.DECIMAL}) { - JdbcType type = dialect.resolveSqlTypeDescriptor("Decimal(35, 10)", jdbcCode, 35, 10, - ((MetadataImplementor) metadata).getTypeConfiguration().getJdbcTypeRegistry()); - assertEquals(18634, type.getJdbcTypeCode()); - assertEquals(Types.DECIMAL, type.getDefaultSqlTypeCode()); - assertEquals(Types.DECIMAL, type.getDdlTypeCode()); - } - } - - private static void assertMapping(String field, int precision, int scale, int ddlCode) throws Exception { - BasicValue value = (BasicValue) metadata.getEntityBinding(DecimalColumns.class.getName()) - .getProperty(field).getValue(); - org.hibernate.mapping.Column column = (org.hibernate.mapping.Column) value.getSelectables().get(0); - assertEquals("Decimal(" + precision + ", " + scale + ")", column.getSqlType(metadata)); - JdbcType jdbcType = value.resolve().getJdbcType(); - assertEquals(ddlCode, jdbcType.getDefaultSqlTypeCode()); - assertEquals(ddlCode, jdbcType.getDdlTypeCode()); - int decimalCode = jdbcType.getJdbcTypeCode() - (1 << 14); - assertEquals(precision, decimalCode >> 6); - assertEquals(scale, decimalCode & 63); - assertBinding(value.resolve(), BigDecimal.ZERO, jdbcType.getJdbcTypeCode()); - assertBinding(value.resolve(), null, jdbcType.getJdbcTypeCode()); - } - - private static void assertBinding(BasicValue.Resolution resolution, BigDecimal value, int jdbcCode) - throws Exception { - AtomicReference bound = new AtomicReference<>(); - PreparedStatement statement = (PreparedStatement) Proxy.newProxyInstance( - DecimalMappingTest.class.getClassLoader(), new Class[]{PreparedStatement.class}, - (proxy, method, args) -> { - if (method.getName().equals("setObject")) { - bound.set(args); - } - return null; - }); - var javaType = resolution.getDomainJavaType(); - resolution.getJdbcType().getBinder(javaType).bind(statement, javaType.wrap(value, null), 1, null); - assertEquals(1, bound.get()[0]); - assertEquals(value, bound.get()[1]); - assertEquals(jdbcCode, bound.get()[2]); - } - - @Entity - public static class DecimalColumns { - @Id - private long id; - @Column(precision = 18, scale = 2) - private BigDecimal amount; - @Column(precision = 35, scale = 10) - private BigDecimal largeAmount; - @Column(precision = 35, scale = 35) - private BigDecimal fraction; - @Column(precision = 35, scale = 0) - private BigDecimal integerAmount; - private BigDecimal defaultDecimal; - private BigInteger defaultInteger; - @Column(precision = 22, scale = 9) - private BigDecimal decimal22_9; - @Column(precision = 31, scale = 9) - private BigDecimal decimal31_9; - @Column(precision = 35, scale = 0) - private BigDecimal decimal35_0; - @Column(precision = 35, scale = 9) - private BigDecimal decimal35_9; - } - - @Entity - public static class InvalidPrecision { - @Id - private long id; - @Column(precision = 36, scale = 0) - private BigDecimal amount; - } - - @Entity - public static class InvalidScale { - @Id - private long id; - @Column(precision = 18, scale = 19) - private BigDecimal amount; - } -} diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java index df1d55c2..4412e79b 100644 --- a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java @@ -6,6 +6,9 @@ import jakarta.persistence.Table; import java.math.BigDecimal; import java.math.BigInteger; +import java.sql.ResultSet; +import java.util.HashMap; +import java.util.Map; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -43,6 +46,27 @@ static void tearDown() { } } + @Test + void createsColumnsWithDeclaredPrecisionAndScale() { + sessionFactory.inTransaction(session -> session.doWork(connection -> { + Map types = new HashMap<>(); + try (ResultSet columns = connection.getMetaData() + .getColumns(null, null, "decimal_precision_scale", null)) { + while (columns.next()) { + types.put(columns.getString("COLUMN_NAME"), columns.getString("TYPE_NAME")); + } + } + assertEquals(Map.of( + "id", "Int64", + "amount", "Decimal(18, 2)", + "largeAmount", "Decimal(35, 10)", + "fraction", "Decimal(35, 35)", + "integerAmount", "Decimal(35, 0)", + "defaultDecimal", "Decimal(22, 9)", + "defaultInteger", "Decimal(22, 0)"), types); + })); + } + @Test void persistsBatchWithLargeValuesZerosAndNulls() { DecimalEntity large = largeValues(1); diff --git a/liquibase-dialect/CHANGELOG.md b/liquibase-dialect/CHANGELOG.md index 2ee209b6..745fab32 100644 --- a/liquibase-dialect/CHANGELOG.md +++ b/liquibase-dialect/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.3 ## + +* Support parameterized `Decimal(precision,scale)` in table creation, column addition, inserts, CSV load/upsert, and changelog generation/replay. +* **Breaking change:** explicit Decimal precision and scale are now preserved instead of being replaced with `(22,9)`. Specify both integer parameters with `1 <= precision <= 35` and `0 <= scale <= precision`; invalid or incomplete parameters are rejected. Parameterless `decimal` still defaults to `Decimal(22,9)`. + ## 1.1.2 ## * Fixed the `liquibase tag` command failing on YDB due to an unsupported generic `UPDATE` query ([#236](https://github.com/ydb-platform/ydb-java-dialects/issues/236)). diff --git a/liquibase-dialect/README.md b/liquibase-dialect/README.md index 48f8885b..44483b14 100644 --- a/liquibase-dialect/README.md +++ b/liquibase-dialect/README.md @@ -79,20 +79,6 @@ cp ydb-jdbc-driver-shaded-${version-jdbc}.jar /usr/local/opt/liquibase/internal/ Use this custom dialect just like any other Liquibase dialect. -### Decimal precision and scale - -Specify both precision and scale to use a parameterized YDB decimal: - -```xml - -``` - -YDB supports precision from 1 to 35 and scale from 0 to precision. The aliases -`java.sql.Types.DECIMAL` and `java.math.BigDecimal` accept the same parameters. -Without parameters, `decimal` retains the default `Decimal(22,9)`. -The declared parameters are preserved when creating or adding columns, generating -changelogs, inserting values, and loading or updating data from CSV files. - ## Integration with Spring Boot ```properties diff --git a/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java b/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java deleted file mode 100644 index 097c6a80..00000000 --- a/liquibase-dialect/src/test/java/tech/ydb/liquibase/type/DecimalTypeYdbTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package tech.ydb.liquibase.type; - -import java.math.BigDecimal; -import liquibase.datatype.DataTypeFactory; -import liquibase.datatype.LiquibaseDataType; -import liquibase.exception.UnexpectedLiquibaseException; -import org.junit.jupiter.api.Test; -import tech.ydb.liquibase.database.YdbDatabase; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class DecimalTypeYdbTest { - - private final YdbDatabase database = new YdbDatabase(); - - @Test - void defaultParameters() { - for (String name : new String[]{"decimal", "java.sql.Types.DECIMAL", "java.math.BigDecimal"}) { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription(name, database); - assertTrue(type.validate(database)); - assertEquals("DECIMAL(22,9)", type.toDatabaseDataType(database).toString()); - assertEquals("CAST('1.23' AS DECIMAL(22,9))", type.objectToSql(new BigDecimal("1.23"), database)); - } - } - - @Test - void explicitParametersAndAliases() { - for (String name : new String[]{"Decimal", "decimal", "java.sql.Types.DECIMAL", "java.math.BigDecimal"}) { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription(name + "(35, 10)", database); - assertTrue(type.validate(database)); - assertEquals("DECIMAL(35,10)", type.toDatabaseDataType(database).toString()); - assertEquals("CAST('1234567890123456789012345.1234567890' AS DECIMAL(35,10))", - type.objectToSql("1234567890123456789012345.1234567890", database)); - } - } - - @Test - void parameterBoundaries() { - for (String parameters : new String[]{"1,0", "1,1", "35,0", "35,35", "22,9"}) { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(" + parameters + ")", database); - assertTrue(type.validate(database)); - assertEquals("DECIMAL(" + parameters + ")", type.toDatabaseDataType(database).toString()); - } - } - - @Test - void numericParameterObjects() { - DecimalTypeYdb type = new DecimalTypeYdb(); - type.addParameter(18); - type.addParameter(2); - assertTrue(type.validate(database)); - assertEquals("DECIMAL(18,2)", type.toDatabaseDataType(database).toString()); - } - - @Test - void invalidParametersAreRejectedDuringValidationAndConversion() { - for (String parameters : new String[]{"10", "10,2,1", "0,0", "36,0", "-1,0", "10,-1", "10,11", - "1.5,0", "10,1.5", "text,0", "10,text", "2147483648,0"}) { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(" + parameters + ")", database); - assertThrows(UnexpectedLiquibaseException.class, () -> type.validate(database), parameters); - assertThrows(UnexpectedLiquibaseException.class, () -> type.toDatabaseDataType(database), parameters); - assertThrows(UnexpectedLiquibaseException.class, () -> type.objectToSql("1", database), parameters); - } - } - - @Test - void invalidNumberRetainsCause() { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(text,0)", database); - UnexpectedLiquibaseException failure = assertThrows(UnexpectedLiquibaseException.class, - () -> type.toDatabaseDataType(database)); - assertInstanceOf(NumberFormatException.class, failure.getCause()); - } - - @Test - void nullValues() { - LiquibaseDataType type = DataTypeFactory.getInstance().fromDescription("Decimal(35,10)", database); - assertEquals("NULL", type.objectToSql(null, database)); - assertEquals("NULL", type.objectToSql("null", database)); - } -} From 8094cb28954eec165d9f7d31d83996ab3158767a Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Fri, 6 Nov 2026 10:21:51 +0000 Subject: [PATCH 5/8] Remove unused Decimal resolution for native query metadata The YDB JDBC driver reports zero precision and scale in ResultSetMetaData, so the resolveSqlTypeDescriptor override never matched and native queries already used the registered DECIMAL JDBC type. Drop the dead override, fix the changelog entry, and rename the native query test to match what it checks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01My2Lh7oZ87zvCJyyomZG4G --- hibernate-dialect/CHANGELOG.md | 3 +-- .../tech/ydb/hibernate/dialect/YdbDialect.java | 18 ------------------ .../types/DecimalPrecisionScaleTest.java | 2 +- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index b5243e61..af468d11 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,8 +1,7 @@ ## 1.7.2 ## - Support arbitrary YDB Decimal precision and scale through `@Column(precision = ..., scale = ...)` for schema generation and JDBC parameter binding. -- **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9`. Replace their `@JdbcTypeCode` annotations with `@Column(precision = ..., scale = ...)` using the same parameters. Default mappings remain `BigDecimal` to `Decimal(22,9)` and `BigInteger` to `Decimal(22,0)`. -- Fix Decimal JDBC type codes resolved from native query result metadata. +- **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9`, and the custom Decimal JDBC type resolution for native query metadata. Replace their `@JdbcTypeCode` annotations with `@Column(precision = ..., scale = ...)` using the same parameters. Default mappings remain `BigDecimal` to `Decimal(22,9)` and `BigInteger` to `Decimal(22,0)`. ## 1.7.1 ## diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java index 681b910f..c517c0f0 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/YdbDialect.java @@ -63,9 +63,7 @@ import static org.hibernate.type.SqlTypes.VARCHAR; import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.descriptor.java.UUIDJavaType; -import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.UUIDJdbcType; -import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl; import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry; import org.hibernate.type.spi.TypeConfiguration; @@ -239,22 +237,6 @@ protected void registerColumnTypes(TypeContributions typeContributions, ServiceR ddlTypeRegistry.addDescriptor(new DdlTypeImpl(YdbJdbcCode.INTERVAL, "Interval", "Interval", this)); } - @Override - public JdbcType resolveSqlTypeDescriptor( - String columnTypeName, - int jdbcTypeCode, - int precision, - int scale, - JdbcTypeRegistry jdbcTypeRegistry) { - if ((jdbcTypeCode == NUMERIC || jdbcTypeCode == DECIMAL) && (precision != 0 || scale != 0)) { - DecimalJdbcType type = new DecimalJdbcType(DECIMAL, precision, scale); - jdbcTypeRegistry.addDescriptorIfAbsent(type.getJdbcTypeCode(), type); - return jdbcTypeRegistry.getDescriptor(type.getJdbcTypeCode()); - } - - return super.resolveSqlTypeDescriptor(columnTypeName, jdbcTypeCode, precision, scale, jdbcTypeRegistry); - } - @Override public int getDefaultDecimalPrecision() { return 22; diff --git a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java index 4412e79b..188ba071 100644 --- a/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java +++ b/hibernate-dialect/src/test/java/tech/ydb/hibernate/types/DecimalPrecisionScaleTest.java @@ -110,7 +110,7 @@ void updatesAndQueriesWithInferredParameters() { } @Test - void readsDecimalNativeQueryMetadata() { + void readsDecimalValuesFromNativeQuery() { DecimalEntity entity = largeValues(20); sessionFactory.inTransaction(session -> session.persist(entity)); sessionFactory.inTransaction(session -> { From 748893395d1165218c59d215e6b07c535eaf0dfb Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Fri, 6 Nov 2026 10:54:27 +0000 Subject: [PATCH 6/8] Treat Liquibase decimal(p) as Decimal(p, 0) and shorten changelogs A single Decimal parameter now means precision with scale 0, as in the SQL standard and the Hibernate dialect, instead of being rejected. Cover it in the Decimal migration test and rewrite the 1.7.2 and 1.1.3 changelog entries in shorter form. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01My2Lh7oZ87zvCJyyomZG4G --- hibernate-dialect/CHANGELOG.md | 4 ++-- liquibase-dialect/CHANGELOG.md | 4 ++-- .../main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java | 6 +++--- .../java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java | 4 ++++ .../src/test/resources/changelogs/csv/decimal-insert.csv | 6 +++--- .../src/test/resources/changelogs/csv/decimal-upsert.csv | 6 +++--- liquibase-dialect/src/test/resources/changelogs/decimal.xml | 3 +++ 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index af468d11..a050de56 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.7.2 ## -- Support arbitrary YDB Decimal precision and scale through `@Column(precision = ..., scale = ...)` for schema generation and JDBC parameter binding. -- **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0`, and `DECIMAL_35_9`, and the custom Decimal JDBC type resolution for native query metadata. Replace their `@JdbcTypeCode` annotations with `@Column(precision = ..., scale = ...)` using the same parameters. Default mappings remain `BigDecimal` to `Decimal(22,9)` and `BigInteger` to `Decimal(22,0)`. +- Support any YDB `Decimal(p, s)` through `@Column(precision = ..., scale = ...)`: values are bound with the column type. Defaults stay `Decimal(22, 9)` for `BigDecimal` and `Decimal(22, 0)` for `BigInteger`. +- **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0` and `DECIMAL_35_9`. Use `@Column(precision = ..., scale = ...)` instead. ## 1.7.1 ## diff --git a/liquibase-dialect/CHANGELOG.md b/liquibase-dialect/CHANGELOG.md index 745fab32..2d2222d8 100644 --- a/liquibase-dialect/CHANGELOG.md +++ b/liquibase-dialect/CHANGELOG.md @@ -1,7 +1,7 @@ ## 1.1.3 ## -* Support parameterized `Decimal(precision,scale)` in table creation, column addition, inserts, CSV load/upsert, and changelog generation/replay. -* **Breaking change:** explicit Decimal precision and scale are now preserved instead of being replaced with `(22,9)`. Specify both integer parameters with `1 <= precision <= 35` and `0 <= scale <= precision`; invalid or incomplete parameters are rejected. Parameterless `decimal` still defaults to `Decimal(22,9)`. +* Support explicit `Decimal(p, s)` (1 <= p <= 35, 0 <= s <= p) in schema changes, inserts, `loadData`/`loadUpdateData` and changelog generation. Previously explicit parameters were replaced with `(22, 9)`. +* `decimal(p)` is created as `Decimal(p, 0)`; `decimal` without parameters stays `Decimal(22, 9)`. ## 1.1.2 ## diff --git a/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java b/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java index 5182c544..86affc1e 100644 --- a/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java +++ b/liquibase-dialect/src/main/java/tech/ydb/liquibase/type/DecimalTypeYdb.java @@ -39,15 +39,15 @@ private String decimalType() { if (parameters.length == 0) { return "DECIMAL(22,9)"; } - if (parameters.length != 2) { - throw new UnexpectedLiquibaseException("YDB Decimal requires both precision and scale, or no parameters"); + if (parameters.length > 2) { + throw new UnexpectedLiquibaseException("YDB Decimal accepts at most precision and scale"); } int precision; int scale; try { precision = Integer.parseInt(parameters[0].toString()); - scale = Integer.parseInt(parameters[1].toString()); + scale = parameters.length == 2 ? Integer.parseInt(parameters[1].toString()) : 0; } catch (NumberFormatException e) { throw new UnexpectedLiquibaseException("YDB Decimal precision and scale must be integers", e); } diff --git a/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java b/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java index ed3af9ac..ee80abaa 100644 --- a/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java +++ b/liquibase-dialect/src/test/java/tech/ydb/liquibase/YdbDatabaseDecimalTest.java @@ -42,6 +42,7 @@ void migrateLoadAndRegenerateParameterizedDecimals(@TempDir Path tempDir) throws assertNull(rows.getBigDecimal("fraction")); assertNull(rows.getBigDecimal("default_amount")); assertNull(rows.getBigDecimal("small_amount")); + assertNull(rows.getBigDecimal("precision_only")); } else { String sign = id == 2 || id == 3 ? "-" : ""; assertEquals(new BigDecimal(sign + "1234567890123456789012345.1234567890"), @@ -53,6 +54,7 @@ void migrateLoadAndRegenerateParameterizedDecimals(@TempDir Path tempDir) throws assertEquals(new BigDecimal(sign + "1234567890123.123456789"), rows.getBigDecimal("default_amount")); assertEquals(new BigDecimal(sign + "9"), rows.getBigDecimal("small_amount")); + assertEquals(new BigDecimal(sign + "1234567890"), rows.getBigDecimal("precision_only")); } } assertFalse(rows.next()); @@ -71,6 +73,7 @@ void migrateLoadAndRegenerateParameterizedDecimals(@TempDir Path tempDir) throws assertTrue(changelog.contains("type=\"DECIMAL(35,35)\"")); assertTrue(changelog.contains("type=\"DECIMAL(22,9)\"")); assertTrue(changelog.contains("type=\"DECIMAL(1,0)\"")); + assertTrue(changelog.contains("type=\"DECIMAL(10,0)\"")); try (Connection connection = DriverManager.getConnection(jdbcUrl()); Statement statement = connection.createStatement()) { @@ -97,5 +100,6 @@ private static void assertDecimalTypes(Connection connection) throws Exception { assertEquals("Decimal(35, 35)", types.get("fraction")); assertEquals("Decimal(22, 9)", types.get("default_amount")); assertEquals("Decimal(1, 0)", types.get("small_amount")); + assertEquals("Decimal(10, 0)", types.get("precision_only")); } } diff --git a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv index b9d25ef7..be2398de 100644 --- a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv +++ b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-insert.csv @@ -1,3 +1,3 @@ -id,amount,whole_amount,fraction,default_amount,small_amount -3,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9 -4,null,null,null,null,null +id,amount,whole_amount,fraction,default_amount,small_amount,precision_only +3,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9,1234567890 +4,null,null,null,null,null,null diff --git a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv index a9587c19..2df3a0e0 100644 --- a/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv +++ b/liquibase-dialect/src/test/resources/changelogs/csv/decimal-upsert.csv @@ -1,3 +1,3 @@ -id,amount,whole_amount,fraction,default_amount,small_amount -3,-1234567890123456789012345.1234567890,-12345678901234567890123456789012345,-0.12345678901234567890123456789012345,-1234567890123.123456789,-9 -5,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9 +id,amount,whole_amount,fraction,default_amount,small_amount,precision_only +3,-1234567890123456789012345.1234567890,-12345678901234567890123456789012345,-0.12345678901234567890123456789012345,-1234567890123.123456789,-9,-1234567890 +5,1234567890123456789012345.1234567890,12345678901234567890123456789012345,0.12345678901234567890123456789012345,1234567890123.123456789,9,1234567890 diff --git a/liquibase-dialect/src/test/resources/changelogs/decimal.xml b/liquibase-dialect/src/test/resources/changelogs/decimal.xml index e8eab6f7..935be619 100644 --- a/liquibase-dialect/src/test/resources/changelogs/decimal.xml +++ b/liquibase-dialect/src/test/resources/changelogs/decimal.xml @@ -17,6 +17,7 @@ + @@ -28,6 +29,7 @@ + @@ -36,6 +38,7 @@ + From d456fd8318af006e1ade3e7fe4cc4d180853047d Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Mon, 9 Nov 2026 12:08:35 +0300 Subject: [PATCH 7/8] Document YDB Decimal JDBC code encoding in YdbConst --- .../ydb/hibernate/dialect/code/YdbConst.java | 21 ++++++++++++++++--- .../dialect/types/DecimalJdbcType.java | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java index 6d414e69..54d18fbb 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/code/YdbConst.java @@ -1,5 +1,7 @@ package tech.ydb.hibernate.dialect.code; +import org.hibernate.Internal; + /** * That class contain custom YDB type codes * @see JDBC Driver constants @@ -7,8 +9,21 @@ * * @author Kirill Kurdyukov */ -final class YdbConst { - public static final int SQL_KIND_PRIMITIVE = 10000; +@Internal +public final class YdbConst { + static final int SQL_KIND_PRIMITIVE = 10000; + private static final int SQL_KIND_DECIMAL = 1 << 14; + private static final int DECIMAL_SCALE_BITS = 6; private YdbConst() { }; -} \ No newline at end of file + + /** + * Encodes the Decimal flag, precision, and scale expected by the YDB JDBC driver. + * Scale occupies the lowest six bits, followed by precision. + * + * @see JDBC Decimal decoding + */ + public static int decimalJdbcTypeCode(int precision, int scale) { + return SQL_KIND_DECIMAL + (precision << DECIMAL_SCALE_BITS) + scale; + } +} diff --git a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java index 2d3a8593..9db2bb86 100644 --- a/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java +++ b/hibernate-dialect/src/main/java/tech/ydb/hibernate/dialect/types/DecimalJdbcType.java @@ -12,6 +12,7 @@ import org.hibernate.type.descriptor.jdbc.AdjustableJdbcType; import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; +import tech.ydb.hibernate.dialect.code.YdbConst; /** * @author Kirill Kurdyukov @@ -77,6 +78,6 @@ private static int jdbcTypeCode(int precision, int scale) { throw new MappingException("Invalid YDB Decimal(" + precision + ", " + scale + "): precision must be between 1 and 35 and scale between 0 and precision"); } - return (1 << 14) + (precision << 6) + scale; + return YdbConst.decimalJdbcTypeCode(precision, scale); } } From 00ae04b68313b91d8131d9355c4e64d4f383f684 Mon Sep 17 00:00:00 2001 From: Kirill Kurdyukov Date: Mon, 9 Nov 2026 12:27:59 +0300 Subject: [PATCH 8/8] Update CHANGELOG.md --- hibernate-dialect/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hibernate-dialect/CHANGELOG.md b/hibernate-dialect/CHANGELOG.md index a050de56..beb686c7 100644 --- a/hibernate-dialect/CHANGELOG.md +++ b/hibernate-dialect/CHANGELOG.md @@ -1,4 +1,4 @@ -## 1.7.2 ## +## 1.8.0 ## - Support any YDB `Decimal(p, s)` through `@Column(precision = ..., scale = ...)`: values are bound with the column type. Defaults stay `Decimal(22, 9)` for `BigDecimal` and `Decimal(22, 0)` for `BigInteger`. - **Breaking change:** removed `YdbJdbcCode.DECIMAL_22_9`, `DECIMAL_31_9`, `DECIMAL_35_0` and `DECIMAL_35_9`. Use `@Column(precision = ..., scale = ...)` instead.