add test for median()

This commit is contained in:
Gavin King 2022-10-06 17:52:44 +02:00
parent 6de92c4f90
commit 4a1fe85f0d
3 changed files with 34 additions and 4 deletions

View File

@ -828,7 +828,11 @@ public abstract class Dialect implements ConversionContext {
CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine);
//aggregate functions, supported on every database
//standard aggregate functions count(), sum(), max(), min(), avg(),
//supported on every database
//Note that we don't include median() in this list, since it's difficult
//to implement on MySQL and Sybase ASE
functionFactory.aggregates( this, SqlAstNodeRenderingMode.DEFAULT );

View File

@ -34,12 +34,14 @@ import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matchers;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
@ -57,6 +59,8 @@ public class FunctionTests {
scope.inTransaction(
em -> {
EntityOfBasics entity = new EntityOfBasics();
entity.setTheInt(5);
entity.setTheDouble(1.0);
entity.setId(123);
entity.setTheDate( new Date( 74, 2, 25 ) );
entity.setTheTime( new Time( 20, 10, 8 ) );
@ -1423,6 +1427,22 @@ public class FunctionTests {
);
}
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsMedian.class)
public void testMedian(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<Object[]> list = session.createQuery("select median(e.theDouble), median(e.theInt) from EntityOfBasics e", Object[].class)
.list();
assertEquals( 1, list.size() );
Double d = (Double) list.get(0)[0];
Double i = (Double) list.get(0)[1];
assertEquals(d,1.0, 1e-5);
assertEquals(i,5.0, 1e-5);
}
);
}
@Test
public void testGrouping(SessionFactoryScope scope) {
scope.inTransaction(

View File

@ -26,8 +26,6 @@ import org.hibernate.dialect.TiDBDialect;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.sql.ast.spi.StringBuilderSqlAppender;
import org.hibernate.testing.DialectCheck;
/**
* Container class for different implementation of the {@link DialectFeatureCheck} interface.
*
@ -464,7 +462,15 @@ abstract public class DialectFeatureChecks {
return !( dialect instanceof H2Dialect
|| dialect instanceof MySQLDialect
|| dialect instanceof SybaseDialect
|| dialect instanceof DerbyDialect);
|| dialect instanceof DerbyDialect );
}
}
public static class SupportsMedian implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
return !( dialect instanceof MySQLDialect
|| dialect instanceof SybaseDialect
|| dialect instanceof DerbyDialect );
}
}
}