HHH-15654 Add overloads to ordered-set aggregate functions without filter

This commit is contained in:
Marco Belladelli 2022-12-02 16:29:01 +01:00 committed by Christian Beikov
parent 2f1f6870b6
commit 9a980b3f93
4 changed files with 137 additions and 9 deletions

View File

@ -1046,6 +1046,19 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
*/ */
<T> JpaExpression<T> functionWithinGroup(String name, Class<T> type, JpaOrder order, Expression<?>... args); <T> JpaExpression<T> functionWithinGroup(String name, Class<T> 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<String> listagg(JpaOrder order, Expression<String> argument, Expression<String> separator);
/** /**
* Create a {@code listagg} ordered set-aggregate function expression. * Create a {@code listagg} ordered set-aggregate function expression.
* *
@ -1064,6 +1077,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
Expression<String> argument, Expression<String> argument,
Expression<String> separator); Expression<String> 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. * 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); 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<Integer> percentileCont(JpaOrder order, Expression<? extends Number> argument);
/** /**
* Create a {@code percentile_cont} ordered set-aggregate function expression. * Create a {@code percentile_cont} ordered set-aggregate function expression.
* *
@ -1090,6 +1127,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
*/ */
JpaExpression<Integer> percentileCont(JpaOrder order, JpaPredicate filter, Expression<? extends Number> argument); JpaExpression<Integer> percentileCont(JpaOrder order, JpaPredicate filter, Expression<? extends Number> 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<Integer> percentileDisc(JpaOrder order, Expression<? extends Number> argument);
/** /**
* Create a {@code percentile_disc} ordered set-aggregate function expression. * Create a {@code percentile_disc} ordered set-aggregate function expression.
* *
@ -1103,6 +1152,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
*/ */
JpaExpression<Integer> percentileDisc(JpaOrder order, JpaPredicate filter, Expression<? extends Number> argument); JpaExpression<Integer> percentileDisc(JpaOrder order, JpaPredicate filter, Expression<? extends Number> 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<Long> rank(JpaOrder order, Expression<Integer> argument);
/** /**
* Create a {@code rank} ordered set-aggregate function expression. * Create a {@code rank} ordered set-aggregate function expression.
* *
@ -1116,6 +1177,18 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
*/ */
JpaExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument); JpaExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> 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<Double> percentRank(JpaOrder order, Expression<Integer> argument);
/** /**
* Create a {@code percent_rank} ordered set-aggregate function expression. * Create a {@code percent_rank} ordered set-aggregate function expression.
* *

View File

@ -1321,6 +1321,11 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.functionWithinGroup( name, type, order, args ); return criteriaBuilder.functionWithinGroup( name, type, order, args );
} }
@Override
public JpaExpression<String> listagg(JpaOrder order, Expression<String> argument, Expression<String> separator) {
return criteriaBuilder.listagg( order, argument, separator );
}
@Override @Override
public JpaExpression<String> listagg( public JpaExpression<String> listagg(
JpaOrder order, JpaOrder order,
@ -1330,11 +1335,21 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.listagg( order, filter, argument, separator ); return criteriaBuilder.listagg( order, filter, argument, separator );
} }
@Override
public JpaExpression<?> mode(JpaOrder order, Expression<?> argument) {
return criteriaBuilder.mode( order, argument );
}
@Override @Override
public JpaExpression<?> mode(JpaOrder order, JpaPredicate filter, Expression<?> argument) { public JpaExpression<?> mode(JpaOrder order, JpaPredicate filter, Expression<?> argument) {
return criteriaBuilder.mode( order, filter, argument ); return criteriaBuilder.mode( order, filter, argument );
} }
@Override
public JpaExpression<Integer> percentileCont(JpaOrder order, Expression<? extends Number> argument) {
return criteriaBuilder.percentileCont( order, argument );
}
@Override @Override
public JpaExpression<Integer> percentileCont( public JpaExpression<Integer> percentileCont(
JpaOrder order, JpaOrder order,
@ -1343,6 +1358,11 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.percentileCont( order, filter, argument ); return criteriaBuilder.percentileCont( order, filter, argument );
} }
@Override
public JpaExpression<Integer> percentileDisc(JpaOrder order, Expression<? extends Number> argument) {
return criteriaBuilder.percentileDisc( order, argument );
}
@Override @Override
public JpaExpression<Integer> percentileDisc( public JpaExpression<Integer> percentileDisc(
JpaOrder order, JpaOrder order,
@ -1351,11 +1371,21 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.percentileDisc( order, filter, argument ); return criteriaBuilder.percentileDisc( order, filter, argument );
} }
@Override
public JpaExpression<Long> rank(JpaOrder order, Expression<Integer> argument) {
return criteriaBuilder.rank( order, argument );
}
@Override @Override
public JpaExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) { public JpaExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) {
return criteriaBuilder.rank( order, filter, argument ); return criteriaBuilder.rank( order, filter, argument );
} }
@Override
public JpaExpression<Double> percentRank(JpaOrder order, Expression<Integer> argument) {
return criteriaBuilder.percentRank( order, argument );
}
@Override @Override
public JpaExpression<Double> percentRank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) { public JpaExpression<Double> percentRank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) {
return criteriaBuilder.percentRank( order, filter, argument ); return criteriaBuilder.percentRank( order, filter, argument );

View File

@ -2669,6 +2669,11 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
return functionWithinGroup( name, type, order, null, args ); return functionWithinGroup( name, type, order, null, args );
} }
@Override
public JpaExpression<String> listagg(JpaOrder order, Expression<String> argument, Expression<String> separator) {
return listagg( order, null, argument, separator );
}
@Override @Override
public SqmExpression<String> listagg( public SqmExpression<String> listagg(
JpaOrder order, JpaOrder order,
@ -2678,11 +2683,21 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
return functionWithinGroup( "listagg", String.class, order, filter, argument, separator ); return functionWithinGroup( "listagg", String.class, order, filter, argument, separator );
} }
@Override
public JpaExpression<?> mode(JpaOrder order, Expression<?> argument) {
return mode( order, null, argument );
}
@Override @Override
public SqmExpression<?> mode(JpaOrder order, JpaPredicate filter, Expression<?> argument) { public SqmExpression<?> mode(JpaOrder order, JpaPredicate filter, Expression<?> argument) {
return functionWithinGroup( "mode", argument.getJavaType(), order, filter, argument ); return functionWithinGroup( "mode", argument.getJavaType(), order, filter, argument );
} }
@Override
public JpaExpression<Integer> percentileCont(JpaOrder order, Expression<? extends Number> argument) {
return percentileCont( order, null, argument );
}
@Override @Override
public SqmExpression<Integer> percentileCont( public SqmExpression<Integer> percentileCont(
JpaOrder order, JpaOrder order,
@ -2691,6 +2706,11 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
return functionWithinGroup( "percentile_cont", Integer.class, order, filter, argument ); return functionWithinGroup( "percentile_cont", Integer.class, order, filter, argument );
} }
@Override
public JpaExpression<Integer> percentileDisc(JpaOrder order, Expression<? extends Number> argument) {
return percentileDisc( order, null, argument );
}
@Override @Override
public SqmExpression<Integer> percentileDisc( public SqmExpression<Integer> percentileDisc(
JpaOrder order, JpaOrder order,
@ -2699,11 +2719,21 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
return functionWithinGroup( "percentile_disc", Integer.class, order, filter, argument ); return functionWithinGroup( "percentile_disc", Integer.class, order, filter, argument );
} }
@Override
public JpaExpression<Long> rank(JpaOrder order, Expression<Integer> argument) {
return rank( order, null, argument );
}
@Override @Override
public SqmExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) { public SqmExpression<Long> rank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) {
return functionWithinGroup( "rank", Long.class, order, filter, argument ); return functionWithinGroup( "rank", Long.class, order, filter, argument );
} }
@Override
public JpaExpression<Double> percentRank(JpaOrder order, Expression<Integer> argument) {
return percentRank( order, null, argument );
}
@Override @Override
public SqmExpression<Double> percentRank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) { public SqmExpression<Double> percentRank(JpaOrder order, JpaPredicate filter, Expression<Integer> argument) {
return functionWithinGroup( "percent_rank", Double.class, order, filter, argument ); return functionWithinGroup( "percent_rank", Double.class, order, filter, argument );

View File

@ -26,9 +26,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import jakarta.persistence.Tuple; import jakarta.persistence.Tuple;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Root; import jakarta.persistence.criteria.Root;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -109,7 +107,7 @@ public class CriteriaOrderedSetAggregateTest {
CriteriaQuery<String> cr = cb.createQuery( String.class ); CriteriaQuery<String> cr = cb.createQuery( String.class );
Root<EntityOfBasics> root = cr.from( EntityOfBasics.class ); Root<EntityOfBasics> root = cr.from( EntityOfBasics.class );
JpaExpression<String> function = cb.listagg( null, null, root.get( "theString" ), cb.literal( "," ) ); JpaExpression<String> function = cb.listagg( null, root.get( "theString" ), cb.literal( "," ) );
cr.select( function ); cr.select( function );
List<String> elements = Arrays.asList( session.createQuery( cr ).getSingleResult().split( "," ) ); List<String> elements = Arrays.asList( session.createQuery( cr ).getSingleResult().split( "," ) );
@ -130,7 +128,6 @@ public class CriteriaOrderedSetAggregateTest {
JpaExpression<String> function = cb.listagg( JpaExpression<String> function = cb.listagg(
cb.desc( root.get( "id" ) ), cb.desc( root.get( "id" ) ),
null,
root.get( "theString" ), root.get( "theString" ),
cb.literal( "," ) cb.literal( "," )
); );
@ -172,7 +169,6 @@ public class CriteriaOrderedSetAggregateTest {
JpaExpression<String> function = cb.listagg( JpaExpression<String> function = cb.listagg(
cb.desc( root.get( "id" ), true ), cb.desc( root.get( "id" ), true ),
null,
root.get( "theString" ), root.get( "theString" ),
cb.literal( "," ) cb.literal( "," )
); );
@ -193,7 +189,6 @@ public class CriteriaOrderedSetAggregateTest {
JpaExpression<Integer> function = cb.percentileDisc( JpaExpression<Integer> function = cb.percentileDisc(
cb.asc( root.get( "theInt" ) ), cb.asc( root.get( "theInt" ) ),
null,
cb.literal( 0.5 ) cb.literal( 0.5 )
); );
@ -211,7 +206,7 @@ public class CriteriaOrderedSetAggregateTest {
CriteriaQuery<Double> cr = cb.createQuery( Double.class ); CriteriaQuery<Double> cr = cb.createQuery( Double.class );
Root<EntityOfBasics> root = cr.from( EntityOfBasics.class ); Root<EntityOfBasics> root = cr.from( EntityOfBasics.class );
JpaExpression<Double> function = cb.percentRank( cb.asc( root.get( "theInt" ) ), null, cb.literal( 5 ) ); JpaExpression<Double> function = cb.percentRank( cb.asc( root.get( "theInt" ) ), cb.literal( 5 ) );
cr.select( function ); cr.select( function );
Double result = session.createQuery( cr ).getSingleResult(); Double result = session.createQuery( cr ).getSingleResult();
@ -227,7 +222,7 @@ public class CriteriaOrderedSetAggregateTest {
CriteriaQuery<Long> cr = cb.createQuery( Long.class ); CriteriaQuery<Long> cr = cb.createQuery( Long.class );
Root<EntityOfBasics> root = cr.from( EntityOfBasics.class ); Root<EntityOfBasics> root = cr.from( EntityOfBasics.class );
JpaExpression<Long> function = cb.rank( cb.asc( root.get( "theInt" ) ), null, cb.literal( 5 ) ); JpaExpression<Long> function = cb.rank( cb.asc( root.get( "theInt" ) ), cb.literal( 5 ) );
cr.select( function ); cr.select( function );
Long result = session.createQuery( cr ).getSingleResult(); Long result = session.createQuery( cr ).getSingleResult();
@ -244,7 +239,7 @@ public class CriteriaOrderedSetAggregateTest {
Root<EntityOfBasics> root1 = cr.from( EntityOfBasics.class ); Root<EntityOfBasics> root1 = cr.from( EntityOfBasics.class );
Root<EntityOfBasics> root2 = cr.from( EntityOfBasics.class ); Root<EntityOfBasics> root2 = cr.from( EntityOfBasics.class );
JpaExpression<Long> function = cb.rank( cb.asc( root1.get( "theInt" ) ), null, cb.literal( 5 ) ); JpaExpression<Long> function = cb.rank( cb.asc( root1.get( "theInt" ) ), cb.literal( 5 ) );
cr.multiselect( root2.get( "id" ), function ) cr.multiselect( root2.get( "id" ), function )
.groupBy( root2.get( "id" ) ).having( cb.gt( root2.get( "id" ), cb.literal( 1 ) ) ) .groupBy( root2.get( "id" ) ).having( cb.gt( root2.get( "id" ), cb.literal( 1 ) ) )