From f5a5990d033f98372d960668183f37e33cb2ac4d Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sat, 8 Oct 2022 19:03:30 +0200 Subject: [PATCH] try to make var_xxx() and sdtev_xxx() more portable --- .../test/java/org/hibernate/userguide/hql/HQLTest.java | 2 +- .../hibernate/community/dialect/DB2LegacyDialect.java | 3 ++- .../main/java/org/hibernate/dialect/DB2Dialect.java | 3 ++- .../main/java/org/hibernate/dialect/SybaseDialect.java | 7 +++++++ .../dialect/function/CommonFunctionFactory.java | 7 +------ .../hibernate/orm/test/query/hql/FunctionTests.java | 10 ++++++++++ 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/documentation/src/test/java/org/hibernate/userguide/hql/HQLTest.java b/documentation/src/test/java/org/hibernate/userguide/hql/HQLTest.java index 4263fe7e14..9302086663 100644 --- a/documentation/src/test/java/org/hibernate/userguide/hql/HQLTest.java +++ b/documentation/src/test/java/org/hibernate/userguide/hql/HQLTest.java @@ -1795,7 +1795,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase { } @Test - @RequiresDialect({MySQLDialect.class, PostgreSQLDialect.class, H2Dialect.class, DerbyDialect.class, OracleDialect.class}) +// @RequiresDialect({MySQLDialect.class, PostgreSQLDialect.class, H2Dialect.class, DerbyDialect.class, OracleDialect.class}) public void test_var_function_example() { doInJPA(this::entityManagerFactory, entityManager -> { //tag::hql-native-function-example[] diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java index 2cad60a8ef..452b7e3224 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java @@ -277,9 +277,10 @@ public class DB2LegacyDialect extends Dialect { functionFactory.median(); functionFactory.stddev(); functionFactory.stddevPopSamp(); + functionFactory.varPopSamp(); functionFactory.regrLinearRegressionAggregates(); functionFactory.variance(); - functionFactory.stdevVarianceSamp(); + functionFactory.varianceSamp(); functionFactory.addYearsMonthsDaysHoursMinutesSeconds(); functionFactory.yearsMonthsDaysHoursMinutesSecondsBetween(); functionFactory.dateTrunc(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java index 55ae36c533..0419b72512 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java @@ -259,9 +259,10 @@ public class DB2Dialect extends Dialect { functionFactory.median(); functionFactory.stddev(); functionFactory.stddevPopSamp(); + functionFactory.varPopSamp(); functionFactory.regrLinearRegressionAggregates(); functionFactory.variance(); - functionFactory.stdevVarianceSamp(); + functionFactory.varianceSamp(); functionFactory.addYearsMonthsDaysHoursMinutesSeconds(); functionFactory.yearsMonthsDaysHoursMinutesSecondsBetween(); functionFactory.dateTrunc(); diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java index 4db81fbfc9..7980a5e49a 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SybaseDialect.java @@ -208,6 +208,13 @@ public class SybaseDialect extends AbstractTransactSQLDialect { CommonFunctionFactory functionFactory = new CommonFunctionFactory(queryEngine); + functionFactory.stddev(); + functionFactory.variance(); + functionFactory.stddevPopSamp_stdevp(); + functionFactory.varPopSamp_varp(); + functionFactory.stddevPopSamp(); + functionFactory.varPopSamp(); + // For SQL-Server we need to cast certain arguments to varchar(16384) to be able to concat them queryEngine.getSqmFunctionRegistry().register( "count", 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 311c177db5..da9840ac09 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 @@ -419,12 +419,7 @@ public class CommonFunctionFactory { /** * DB2 */ - public void stdevVarianceSamp() { - functionRegistry.namedAggregateDescriptorBuilder( "stddev_samp" ) - .setInvariantType(doubleType) - .setExactArgumentCount( 1 ) - .setParameterTypes(NUMERIC) - .register(); + public void varianceSamp() { functionRegistry.namedAggregateDescriptorBuilder( "variance_samp" ) .setInvariantType(doubleType) .setExactArgumentCount( 1 ) 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 3cb3bf4952..81c0c0971d 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 @@ -926,6 +926,16 @@ public class FunctionTests { ); } + @Test + public void testStatisticalFunctions(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery("select var_samp(e.theDouble), var_pop(abs(e.theDouble)), stddev_samp(e.theDouble), stddev_pop(e.theDouble) from EntityOfBasics e") + .list(); + } + ); + } + @Test public void testCurrentDateTimeFunctions(SessionFactoryScope scope) { scope.inTransaction(