Skip to content

Commit c58485c

Browse files
committed
HHH-18496 Add json_arrayagg
1 parent 6b4cc28 commit c58485c

File tree

46 files changed

+1573
-16
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1573
-16
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,7 @@ The following functions deal with SQL JSON types, which are not supported on eve
16341634
| `json_value()` | Extracts a value from a JSON document by JSON path
16351635
| `json_exists()` | Checks if a JSON path exists in a JSON document
16361636
| `json_query()` | Queries non-scalar values by JSON path in a JSON document
1637+
| `json_arrayagg()` | Creates a JSON array by aggregating values
16371638
|===
16381639
16391640
@@ -1919,6 +1920,49 @@ Depending on the database, an error might still be thrown even without that, but
19191920
19201921
NOTE: The H2 emulation only supports absolute JSON paths using the dot notation.
19211922
1923+
[[hql-json-arrayagg-function]]
1924+
===== `json_arrayagg()`
1925+
1926+
Creates a JSON array by aggregating values.
1927+
1928+
[[hql-json-arrayagg-bnf]]
1929+
[source, antlrv4, indent=0]
1930+
----
1931+
include::{extrasdir}/json_arrayagg_bnf.txt[]
1932+
----
1933+
1934+
This aggregate function is similar to an <<hql-aggregate-functions-orderedset,_ordered set aggregate function_>>
1935+
since it allows to specify the order in which elements are aggregated, but uses a special syntax.
1936+
1937+
[[hql-json-arrayagg-example]]
1938+
====
1939+
[source, java, indent=0]
1940+
----
1941+
include::{json-example-dir-hql}/JsonArrayAggregateTest.java[tags=hql-json-arrayagg-example]
1942+
----
1943+
====
1944+
1945+
Although database dependent, usually `null` values are `absent` in the resulting JSON array.
1946+
To retain `null` elements, use the `null on null` clause.
1947+
1948+
[[hql-json-arrayagg-null-example]]
1949+
====
1950+
[source, java, indent=0]
1951+
----
1952+
include::{json-example-dir-hql}/JsonArrayAggregateTest.java[tags=hql-json-arrayagg-null-example]
1953+
----
1954+
====
1955+
1956+
The order in which elements are aggregated can be defined by specifying an order by clause.
1957+
1958+
[[hql-json-arrayagg-order-by-example]]
1959+
====
1960+
[source, java, indent=0]
1961+
----
1962+
include::{json-example-dir-hql}/JsonArrayAggregateTest.java[tags=hql-json-arrayagg-order-by-example]
1963+
----
1964+
====
1965+
19221966
[[hql-user-defined-functions]]
19231967
==== Native and user-defined functions
19241968
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"json_arrayagg(" expressionOrPredicate jsonNullClause? orderByClause? ")" filterClause?
2+
3+
jsonNullClause
4+
: ("absent"|"null") "on null"
5+
;

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
436436
functionFactory.jsonExists_no_passing();
437437
functionFactory.jsonObject_db2();
438438
functionFactory.jsonArray_db2();
439+
functionFactory.jsonArrayAgg_db2();
439440
}
440441
}
441442
}

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/H2LegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
406406
functionFactory.jsonValue_h2();
407407
functionFactory.jsonQuery_h2();
408408
functionFactory.jsonExists_h2();
409+
functionFactory.jsonArrayAgg_h2();
409410
}
410411
}
411412
else {

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
273273
if ( getVersion().isSameOrAfter( 2, 7 ) ) {
274274
functionFactory.jsonObject_hsqldb();
275275
functionFactory.jsonArray_hsqldb();
276+
functionFactory.jsonArrayAgg_hsqldb();
276277
}
277278

278279
//trim() requires parameters to be cast when used as trim character

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MariaDBLegacyDialect.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
9292
);
9393
commonFunctionFactory.jsonValue_mariadb();
9494
commonFunctionFactory.jsonArray_mariadb();
95+
commonFunctionFactory.jsonQuery_mariadb();
96+
commonFunctionFactory.jsonArrayAgg_mariadb();
9597
if ( getVersion().isSameOrAfter( 10, 3, 3 ) ) {
9698
commonFunctionFactory.inverseDistributionOrderedSetAggregates_windowEmulation();
9799
functionContributions.getFunctionRegistry().patternDescriptorBuilder( "median", "median(?1) over ()" )

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/MySQLLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
658658
functionFactory.jsonExists_mysql();
659659
functionFactory.jsonObject_mysql();
660660
functionFactory.jsonArray_mysql();
661+
functionFactory.jsonArrayAgg_mysql();
661662
}
662663
}
663664

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
327327
functionFactory.jsonExists_oracle();
328328
functionFactory.jsonObject_oracle();
329329
functionFactory.jsonArray_oracle();
330+
functionFactory.jsonArrayAgg_oracle();
330331
}
331332
}
332333

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/PostgreSQLLegacyDialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
638638
functionFactory.jsonExists();
639639
functionFactory.jsonObject();
640640
functionFactory.jsonArray();
641+
functionFactory.jsonArrayAgg();
641642
}
642643
else {
643644
functionFactory.jsonValue_postgresql();
@@ -646,10 +647,12 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
646647
if ( getVersion().isSameOrAfter( 16 ) ) {
647648
functionFactory.jsonObject();
648649
functionFactory.jsonArray();
650+
functionFactory.jsonArrayAgg();
649651
}
650652
else {
651653
functionFactory.jsonObject_postgresql();
652654
functionFactory.jsonArray_postgresql();
655+
functionFactory.jsonArrayAgg_postgresql();
653656
}
654657
}
655658

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
409409
}
410410
if ( getVersion().isSameOrAfter( 14 ) ) {
411411
functionFactory.listagg_stringAggWithinGroup( "varchar(max)" );
412+
functionFactory.jsonArrayAgg_sqlserver();
412413
}
413414
if ( getVersion().isSameOrAfter( 16 ) ) {
414415
functionFactory.leastGreatest();

0 commit comments

Comments
 (0)