From 9a980b3f936be7d465070893670c8c595655cdfd Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Fri, 2 Dec 2022 16:29:01 +0100 Subject: [PATCH] HHH-15654 Add overloads to ordered-set aggregate functions without filter --- .../criteria/HibernateCriteriaBuilder.java | 73 +++++++++++++++++++ .../spi/HibernateCriteriaBuilderDelegate.java | 30 ++++++++ .../sqm/internal/SqmCriteriaNodeBuilder.java | 30 ++++++++ .../CriteriaOrderedSetAggregateTest.java | 13 +--- 4 files changed, 137 insertions(+), 9 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/HibernateCriteriaBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/HibernateCriteriaBuilder.java index 2141aee674..4afa9302a6 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/HibernateCriteriaBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/HibernateCriteriaBuilder.java @@ -1046,6 +1046,19 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { */ JpaExpression functionWithinGroup(String name, Class type, JpaOrder order, Expression... args); + /** + * Create a {@code listagg} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument values to join + * @param separator the separator used to join the values + * + * @return ordered set-aggregate expression + * + * @see #listagg(JpaOrder, JpaPredicate, Expression, Expression) listagg + */ + JpaExpression listagg(JpaOrder order, Expression argument, Expression separator); + /** * Create a {@code listagg} ordered set-aggregate function expression. * @@ -1064,6 +1077,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { Expression argument, Expression separator); + /** + * Create a {@code mode} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument argument to the function + * + * @return ordered set-aggregate expression + * + * @see #mode(JpaOrder, JpaPredicate, Expression) mode + */ + JpaExpression mode(JpaOrder order, Expression argument); + /** * Create a {@code mode} ordered set-aggregate function expression. * @@ -1077,6 +1102,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { */ JpaExpression mode(JpaOrder order, JpaPredicate filter, Expression argument); + /** + * Create a {@code percentile_cont} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument argument to the function + * + * @return ordered set-aggregate expression + * + * @see #percentileCont(JpaOrder, JpaPredicate, Expression) percentileCont + */ + JpaExpression percentileCont(JpaOrder order, Expression argument); + /** * Create a {@code percentile_cont} ordered set-aggregate function expression. * @@ -1090,6 +1127,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { */ JpaExpression percentileCont(JpaOrder order, JpaPredicate filter, Expression argument); + /** + * Create a {@code percentile_disc} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument argument to the function + * + * @return ordered set-aggregate expression + * + * @see #percentileDisc(JpaOrder, JpaPredicate, Expression) percentileDisc + */ + JpaExpression percentileDisc(JpaOrder order, Expression argument); + /** * Create a {@code percentile_disc} ordered set-aggregate function expression. * @@ -1103,6 +1152,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { */ JpaExpression percentileDisc(JpaOrder order, JpaPredicate filter, Expression argument); + /** + * Create a {@code rank} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument argument to the function + * + * @return ordered set-aggregate expression + * + * @see #rank(JpaOrder, JpaPredicate, Expression) rank + */ + JpaExpression rank(JpaOrder order, Expression argument); + /** * Create a {@code rank} ordered set-aggregate function expression. * @@ -1116,6 +1177,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder { */ JpaExpression rank(JpaOrder order, JpaPredicate filter, Expression argument); + /** + * Create a {@code percent_rank} ordered set-aggregate function expression. + * + * @param order order by clause used in within group + * @param argument argument to the function + * + * @return ordered set-aggregate expression + * + * @see #percentRank(JpaOrder, JpaPredicate, Expression) percentRank + */ + JpaExpression percentRank(JpaOrder order, Expression argument); + /** * Create a {@code percent_rank} ordered set-aggregate function expression. * diff --git a/hibernate-core/src/main/java/org/hibernate/query/criteria/spi/HibernateCriteriaBuilderDelegate.java b/hibernate-core/src/main/java/org/hibernate/query/criteria/spi/HibernateCriteriaBuilderDelegate.java index b4940c9761..b649161e6c 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/criteria/spi/HibernateCriteriaBuilderDelegate.java +++ b/hibernate-core/src/main/java/org/hibernate/query/criteria/spi/HibernateCriteriaBuilderDelegate.java @@ -1321,6 +1321,11 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde return criteriaBuilder.functionWithinGroup( name, type, order, args ); } + @Override + public JpaExpression listagg(JpaOrder order, Expression argument, Expression separator) { + return criteriaBuilder.listagg( order, argument, separator ); + } + @Override public JpaExpression listagg( JpaOrder order, @@ -1330,11 +1335,21 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde return criteriaBuilder.listagg( order, filter, argument, separator ); } + @Override + public JpaExpression mode(JpaOrder order, Expression argument) { + return criteriaBuilder.mode( order, argument ); + } + @Override public JpaExpression mode(JpaOrder order, JpaPredicate filter, Expression argument) { return criteriaBuilder.mode( order, filter, argument ); } + @Override + public JpaExpression percentileCont(JpaOrder order, Expression argument) { + return criteriaBuilder.percentileCont( order, argument ); + } + @Override public JpaExpression percentileCont( JpaOrder order, @@ -1343,6 +1358,11 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde return criteriaBuilder.percentileCont( order, filter, argument ); } + @Override + public JpaExpression percentileDisc(JpaOrder order, Expression argument) { + return criteriaBuilder.percentileDisc( order, argument ); + } + @Override public JpaExpression percentileDisc( JpaOrder order, @@ -1351,11 +1371,21 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde return criteriaBuilder.percentileDisc( order, filter, argument ); } + @Override + public JpaExpression rank(JpaOrder order, Expression argument) { + return criteriaBuilder.rank( order, argument ); + } + @Override public JpaExpression rank(JpaOrder order, JpaPredicate filter, Expression argument) { return criteriaBuilder.rank( order, filter, argument ); } + @Override + public JpaExpression percentRank(JpaOrder order, Expression argument) { + return criteriaBuilder.percentRank( order, argument ); + } + @Override public JpaExpression percentRank(JpaOrder order, JpaPredicate filter, Expression argument) { return criteriaBuilder.percentRank( order, filter, argument ); diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java index 8eba8c55bf..0f501d2968 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java @@ -2669,6 +2669,11 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return functionWithinGroup( name, type, order, null, args ); } + @Override + public JpaExpression listagg(JpaOrder order, Expression argument, Expression separator) { + return listagg( order, null, argument, separator ); + } + @Override public SqmExpression listagg( JpaOrder order, @@ -2678,11 +2683,21 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return functionWithinGroup( "listagg", String.class, order, filter, argument, separator ); } + @Override + public JpaExpression mode(JpaOrder order, Expression argument) { + return mode( order, null, argument ); + } + @Override public SqmExpression mode(JpaOrder order, JpaPredicate filter, Expression argument) { return functionWithinGroup( "mode", argument.getJavaType(), order, filter, argument ); } + @Override + public JpaExpression percentileCont(JpaOrder order, Expression argument) { + return percentileCont( order, null, argument ); + } + @Override public SqmExpression percentileCont( JpaOrder order, @@ -2691,6 +2706,11 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return functionWithinGroup( "percentile_cont", Integer.class, order, filter, argument ); } + @Override + public JpaExpression percentileDisc(JpaOrder order, Expression argument) { + return percentileDisc( order, null, argument ); + } + @Override public SqmExpression percentileDisc( JpaOrder order, @@ -2699,11 +2719,21 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, return functionWithinGroup( "percentile_disc", Integer.class, order, filter, argument ); } + @Override + public JpaExpression rank(JpaOrder order, Expression argument) { + return rank( order, null, argument ); + } + @Override public SqmExpression rank(JpaOrder order, JpaPredicate filter, Expression argument) { return functionWithinGroup( "rank", Long.class, order, filter, argument ); } + @Override + public JpaExpression percentRank(JpaOrder order, Expression argument) { + return percentRank( order, null, argument ); + } + @Override public SqmExpression percentRank(JpaOrder order, JpaPredicate filter, Expression argument) { return functionWithinGroup( "percent_rank", Double.class, order, filter, argument ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaOrderedSetAggregateTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaOrderedSetAggregateTest.java index 6625867594..27ad620469 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaOrderedSetAggregateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/criteria/CriteriaOrderedSetAggregateTest.java @@ -26,9 +26,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import jakarta.persistence.Tuple; -import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.Join; import jakarta.persistence.criteria.Root; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -109,7 +107,7 @@ public class CriteriaOrderedSetAggregateTest { CriteriaQuery cr = cb.createQuery( String.class ); Root root = cr.from( EntityOfBasics.class ); - JpaExpression function = cb.listagg( null, null, root.get( "theString" ), cb.literal( "," ) ); + JpaExpression function = cb.listagg( null, root.get( "theString" ), cb.literal( "," ) ); cr.select( function ); List elements = Arrays.asList( session.createQuery( cr ).getSingleResult().split( "," ) ); @@ -130,7 +128,6 @@ public class CriteriaOrderedSetAggregateTest { JpaExpression function = cb.listagg( cb.desc( root.get( "id" ) ), - null, root.get( "theString" ), cb.literal( "," ) ); @@ -172,7 +169,6 @@ public class CriteriaOrderedSetAggregateTest { JpaExpression function = cb.listagg( cb.desc( root.get( "id" ), true ), - null, root.get( "theString" ), cb.literal( "," ) ); @@ -193,7 +189,6 @@ public class CriteriaOrderedSetAggregateTest { JpaExpression function = cb.percentileDisc( cb.asc( root.get( "theInt" ) ), - null, cb.literal( 0.5 ) ); @@ -211,7 +206,7 @@ public class CriteriaOrderedSetAggregateTest { CriteriaQuery cr = cb.createQuery( Double.class ); Root root = cr.from( EntityOfBasics.class ); - JpaExpression function = cb.percentRank( cb.asc( root.get( "theInt" ) ), null, cb.literal( 5 ) ); + JpaExpression function = cb.percentRank( cb.asc( root.get( "theInt" ) ), cb.literal( 5 ) ); cr.select( function ); Double result = session.createQuery( cr ).getSingleResult(); @@ -227,7 +222,7 @@ public class CriteriaOrderedSetAggregateTest { CriteriaQuery cr = cb.createQuery( Long.class ); Root root = cr.from( EntityOfBasics.class ); - JpaExpression function = cb.rank( cb.asc( root.get( "theInt" ) ), null, cb.literal( 5 ) ); + JpaExpression function = cb.rank( cb.asc( root.get( "theInt" ) ), cb.literal( 5 ) ); cr.select( function ); Long result = session.createQuery( cr ).getSingleResult(); @@ -244,7 +239,7 @@ public class CriteriaOrderedSetAggregateTest { Root root1 = cr.from( EntityOfBasics.class ); Root root2 = cr.from( EntityOfBasics.class ); - JpaExpression function = cb.rank( cb.asc( root1.get( "theInt" ) ), null, cb.literal( 5 ) ); + JpaExpression function = cb.rank( cb.asc( root1.get( "theInt" ) ), cb.literal( 5 ) ); cr.multiselect( root2.get( "id" ), function ) .groupBy( root2.get( "id" ) ).having( cb.gt( root2.get( "id" ), cb.literal( 1 ) ) )