Skip to content

Commit f2e6ad6

Browse files
committed
HHH-17355 Add array_agg documentation and add it to NodeBuilder
1 parent eeca530 commit f2e6ad6

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ The following functions deal with SQL array types, which are not supported on ev
11181118
| Function | Purpose
11191119
11201120
| `array()` | Creates an array based on the passed arguments
1121+
| `array_agg()` | Aggregates row values into an array
11211122
| `array_position()` | Determines the position of an element in an array
11221123
| `array_length()` | Determines the length of an array
11231124
| `array_concat()` | Concatenates array with each other in order
@@ -1147,6 +1148,18 @@ include::{array-example-dir-hql}/ArrayConstructorTest.java[tags=hql-array-exampl
11471148
----
11481149
====
11491150
1151+
===== `array_agg()`
1152+
1153+
An <<hql-aggregate-functions-orderedset,ordered set aggregate function>> that aggregates values to an array.
1154+
1155+
[[hql-array-agg-example]]
1156+
====
1157+
[source, JAVA, indent=0]
1158+
----
1159+
include::{array-example-dir-hql}/ArrayAggregateTest.java[tags=hql-array-agg-example]
1160+
----
1161+
====
1162+
11501163
[[hql-array-position-functions]]
11511164
===== `array_position()`
11521165

hibernate-core/src/main/java/org/hibernate/query/sqm/NodeBuilder.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Map;
1818
import java.util.Set;
1919

20+
import org.hibernate.Incubating;
2021
import org.hibernate.engine.spi.SessionFactoryImplementor;
2122
import org.hibernate.metamodel.model.domain.JpaMetamodel;
2223
import org.hibernate.query.NullPrecedence;
@@ -25,10 +26,13 @@
2526
import org.hibernate.query.criteria.JpaCoalesce;
2627
import org.hibernate.query.criteria.JpaCompoundSelection;
2728
import org.hibernate.query.criteria.JpaExpression;
29+
import org.hibernate.query.criteria.JpaOrder;
2830
import org.hibernate.query.criteria.JpaParameterExpression;
31+
import org.hibernate.query.criteria.JpaPredicate;
2932
import org.hibernate.query.criteria.JpaSearchedCase;
3033
import org.hibernate.query.criteria.JpaSelection;
3134
import org.hibernate.query.criteria.JpaSimpleCase;
35+
import org.hibernate.query.criteria.JpaWindow;
3236
import org.hibernate.query.spi.QueryEngine;
3337
import org.hibernate.query.sqm.tree.delete.SqmDeleteStatement;
3438
import org.hibernate.query.sqm.tree.domain.SqmBagJoin;
@@ -100,6 +104,43 @@ <R> SqmTuple<R> tuple(
100104
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101105
// Array functions for array types
102106

107+
/**
108+
* @see #arrayAgg(JpaOrder, JpaPredicate, JpaWindow, Expression)
109+
* @since 6.4
110+
*/
111+
<T> JpaExpression<T[]> arrayAgg(JpaOrder order, Expression<? extends T> argument);
112+
113+
/**
114+
* @see #arrayAgg(JpaOrder, JpaPredicate, JpaWindow, Expression)
115+
* @since 6.4
116+
*/
117+
<T> JpaExpression<T[]> arrayAgg(JpaOrder order, JpaPredicate filter, Expression<? extends T> argument);
118+
119+
/**
120+
* @see #arrayAgg(JpaOrder, JpaPredicate, JpaWindow, Expression)
121+
* @since 6.4
122+
*/
123+
<T> JpaExpression<T[]> arrayAgg(JpaOrder order, JpaWindow window, Expression<? extends T> argument);
124+
125+
/**
126+
* Create a {@code array_agg} ordered set-aggregate function expression.
127+
*
128+
* @param order order by clause used in within group
129+
* @param filter optional filter clause
130+
* @param window optional window over which to apply the function
131+
* @param argument values to aggregate
132+
*
133+
* @return ordered set-aggregate expression
134+
*
135+
* @see #functionWithinGroup(String, Class, JpaOrder, JpaPredicate, JpaWindow, Expression...)
136+
* @since 6.4
137+
*/
138+
<T> JpaExpression<T[]> arrayAgg(
139+
JpaOrder order,
140+
JpaPredicate filter,
141+
JpaWindow window,
142+
Expression<? extends T> argument);
143+
103144
/**
104145
* Creates an array literal with the {@code array} constructor function.
105146
*

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,30 @@ public SqmExpression<Double> percentRank(
36323632
return functionWithinGroup( "percent_rank", Double.class, order, filter, window, arguments );
36333633
}
36343634

3635+
@Override
3636+
public <T> JpaExpression<T[]> arrayAgg(JpaOrder order, Expression<? extends T> argument) {
3637+
return arrayAgg( order, null, null, argument );
3638+
}
3639+
3640+
@Override
3641+
public <T> JpaExpression<T[]> arrayAgg(JpaOrder order, JpaPredicate filter, Expression<? extends T> argument) {
3642+
return arrayAgg( order, filter, null, argument );
3643+
}
3644+
3645+
@Override
3646+
public <T> JpaExpression<T[]> arrayAgg(JpaOrder order, JpaWindow window, Expression<? extends T> argument) {
3647+
return arrayAgg( order, null, window, argument );
3648+
}
3649+
3650+
@Override
3651+
public <T> JpaExpression<T[]> arrayAgg(
3652+
JpaOrder order,
3653+
JpaPredicate filter,
3654+
JpaWindow window,
3655+
Expression<? extends T> argument) {
3656+
return functionWithinGroup( "array_agg", null, order, filter, window, argument );
3657+
}
3658+
36353659
@Override
36363660
public <T> SqmExpression<T[]> arrayLiteral(T... elements) {
36373661
return getFunctionDescriptor( "array" ).generateSqmExpression(

hibernate-core/src/test/java/org/hibernate/orm/test/function/array/ArrayAggregateTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ public void cleanup(SessionFactoryScope scope) {
109109
@Test
110110
public void testEmpty(SessionFactoryScope scope) {
111111
scope.inSession( em -> {
112+
//tag::hql-array-agg-example[]
112113
List<String[]> results = em.createQuery( "select array_agg(e.data) within group (order by e.id) from BasicEntity e", String[].class )
113114
.getResultList();
115+
//end::hql-array-agg-example[]
114116
assertEquals( 1, results.size() );
115117
assertNull( results.get( 0 ) );
116118
} );

0 commit comments

Comments
 (0)