Skip to content

Commit 0a4bbbe

Browse files
committed
HHH-18496 Add json_arrayagg
1 parent 3d719c4 commit 0a4bbbe

File tree

46 files changed

+1572
-18
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

+1572
-18
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
@@ -437,6 +437,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
437437
functionFactory.jsonExists_no_passing();
438438
functionFactory.jsonObject_db2();
439439
functionFactory.jsonArray_db2();
440+
functionFactory.jsonArrayAgg_db2();
440441
}
441442
}
442443
}

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
@@ -407,6 +407,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
407407
functionFactory.jsonValue_h2();
408408
functionFactory.jsonQuery_h2();
409409
functionFactory.jsonExists_h2();
410+
functionFactory.jsonArrayAgg_h2();
410411
}
411412
}
412413
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
@@ -275,6 +275,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
275275
if ( getVersion().isSameOrAfter( 2, 7 ) ) {
276276
functionFactory.jsonObject_hsqldb();
277277
functionFactory.jsonArray_hsqldb();
278+
functionFactory.jsonArrayAgg_hsqldb();
278279
}
279280

280281
//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
@@ -94,6 +94,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
9494
);
9595
commonFunctionFactory.jsonValue_mariadb();
9696
commonFunctionFactory.jsonArray_mariadb();
97+
commonFunctionFactory.jsonQuery_mariadb();
98+
commonFunctionFactory.jsonArrayAgg_mariadb();
9799
if ( getVersion().isSameOrAfter( 10, 3, 3 ) ) {
98100
commonFunctionFactory.inverseDistributionOrderedSetAggregates_windowEmulation();
99101
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
@@ -660,6 +660,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
660660
functionFactory.jsonExists_mysql();
661661
functionFactory.jsonObject_mysql();
662662
functionFactory.jsonArray_mysql();
663+
functionFactory.jsonArrayAgg_mysql();
663664
}
664665
}
665666

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
@@ -329,6 +329,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
329329
functionFactory.jsonExists_oracle();
330330
functionFactory.jsonObject_oracle();
331331
functionFactory.jsonArray_oracle();
332+
functionFactory.jsonArrayAgg_oracle();
332333
}
333334
}
334335

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
@@ -629,6 +629,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
629629
functionFactory.jsonExists();
630630
functionFactory.jsonObject();
631631
functionFactory.jsonArray();
632+
functionFactory.jsonArrayAgg();
632633
}
633634
else {
634635
functionFactory.jsonValue_postgresql();
@@ -637,10 +638,12 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
637638
if ( getVersion().isSameOrAfter( 16 ) ) {
638639
functionFactory.jsonObject();
639640
functionFactory.jsonArray();
641+
functionFactory.jsonArrayAgg();
640642
}
641643
else {
642644
functionFactory.jsonObject_postgresql();
643645
functionFactory.jsonArray_postgresql();
646+
functionFactory.jsonArrayAgg_postgresql();
644647
}
645648
}
646649

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
@@ -411,6 +411,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
411411
}
412412
if ( getVersion().isSameOrAfter( 14 ) ) {
413413
functionFactory.listagg_stringAggWithinGroup( "varchar(max)" );
414+
functionFactory.jsonArrayAgg_sqlserver();
414415
}
415416
if ( getVersion().isSameOrAfter( 16 ) ) {
416417
functionFactory.leastGreatest();

0 commit comments

Comments
 (0)