diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java index b38e1c51db..0053fb56e3 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java @@ -276,6 +276,8 @@ public class H2Dialect extends Dialect { if ( useLocalTime ) { functionFactory.localtimeLocaltimestamp(); } + functionFactory.trunc(); + functionFactory.dateTrunc(); functionFactory.bitLength(); functionFactory.octetLength(); functionFactory.ascii(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java index d810e9f1c4..023363d064 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java @@ -216,6 +216,7 @@ public class OracleDialect extends Dialect { functionFactory.bitand(); functionFactory.lastDay(); functionFactory.toCharNumberDateTimestamp(); + functionFactory.dateTrunc_trunc(); functionFactory.ceiling_ceil(); functionFactory.concat_pipeOperator(); functionFactory.rownumRowid(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java index 4015681512..5e599063da 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java @@ -2448,4 +2448,13 @@ public class CommonFunctionFactory { .register(); } + public void dateTrunc_trunc() { + functionRegistry.patternDescriptorBuilder( "date_trunc", "trunc(?2,'?1')" ) + .setInvariantType(timestampType) + .setExactArgumentCount( 2 ) + .setParameterTypes(TEMPORAL_UNIT, TEMPORAL) + .setArgumentListSignature( "(TEMPORAL_UNIT field, TEMPORAL datetime)" ) + .register(); + } + } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java index 2447c84e72..8deeeb1cde 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java @@ -8,10 +8,16 @@ package org.hibernate.orm.test.query.hql; import org.hibernate.QueryException; import org.hibernate.dialect.CockroachDialect; +import org.hibernate.dialect.DB2Dialect; import org.hibernate.dialect.DerbyDialect; +import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.MariaDBDialect; import org.hibernate.dialect.MySQLDialect; +import org.hibernate.dialect.OracleDialect; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.dialect.SQLServerDialect; +import org.hibernate.dialect.SybaseDialect; import org.hibernate.dialect.TiDBDialect; import org.hibernate.testing.TestForIssue; @@ -473,6 +479,46 @@ public class FunctionTests { ); } + @Test + @SkipForDialect(dialectClass = MySQLDialect.class, matchSubTypes = true) + @SkipForDialect(dialectClass = SQLServerDialect.class) + @SkipForDialect(dialectClass = SybaseDialect.class, matchSubTypes = true) + @SkipForDialect(dialectClass = DerbyDialect.class) + public void testTruncFunction(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + assertThat( session.createQuery("select trunc(32.92345)").getSingleResult(), is(32d) ); + assertThat( session.createQuery("select trunc(32.92345,3)").getSingleResult(), is(32.923d) ); + } + ); + } + + @Test + @RequiresDialect(MySQLDialect.class) + @RequiresDialect(SQLServerDialect.class) + @RequiresDialect(DB2Dialect.class) + @RequiresDialect(H2Dialect.class) + public void testTruncateFunction(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + assertThat( session.createQuery("select truncate(32.92345,3)").getSingleResult(), is(32.923d) ); + } + ); + } + + @Test + @RequiresDialect(H2Dialect.class) + @RequiresDialect(DB2Dialect.class) + @RequiresDialect(OracleDialect.class) + @RequiresDialect(PostgreSQLDialect.class) + public void testDateTruncFunction(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery("select date_trunc(year,current_timestamp)").getSingleResult(); + } + ); + } + @Test public void testLowerUpperFunctions(SessionFactoryScope scope) { scope.inTransaction(