add tests for trunc() and date_trunc(), even though they're non-"standard"

see https://hibernate.atlassian.net/browse/HHH-15855
This commit is contained in:
Gavin 2022-12-20 22:32:12 +01:00 committed by Gavin King
parent b704e3154e
commit f5f92b852a
4 changed files with 58 additions and 0 deletions

View File

@ -276,6 +276,8 @@ public class H2Dialect extends Dialect {
if ( useLocalTime ) { if ( useLocalTime ) {
functionFactory.localtimeLocaltimestamp(); functionFactory.localtimeLocaltimestamp();
} }
functionFactory.trunc();
functionFactory.dateTrunc();
functionFactory.bitLength(); functionFactory.bitLength();
functionFactory.octetLength(); functionFactory.octetLength();
functionFactory.ascii(); functionFactory.ascii();

View File

@ -216,6 +216,7 @@ public class OracleDialect extends Dialect {
functionFactory.bitand(); functionFactory.bitand();
functionFactory.lastDay(); functionFactory.lastDay();
functionFactory.toCharNumberDateTimestamp(); functionFactory.toCharNumberDateTimestamp();
functionFactory.dateTrunc_trunc();
functionFactory.ceiling_ceil(); functionFactory.ceiling_ceil();
functionFactory.concat_pipeOperator(); functionFactory.concat_pipeOperator();
functionFactory.rownumRowid(); functionFactory.rownumRowid();

View File

@ -2448,4 +2448,13 @@ public class CommonFunctionFactory {
.register(); .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();
}
} }

View File

@ -8,10 +8,16 @@ package org.hibernate.orm.test.query.hql;
import org.hibernate.QueryException; import org.hibernate.QueryException;
import org.hibernate.dialect.CockroachDialect; import org.hibernate.dialect.CockroachDialect;
import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.DerbyDialect; import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MariaDBDialect; import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect; 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.dialect.TiDBDialect;
import org.hibernate.testing.TestForIssue; 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 @Test
public void testLowerUpperFunctions(SessionFactoryScope scope) { public void testLowerUpperFunctions(SessionFactoryScope scope) {
scope.inTransaction( scope.inTransaction(