Skip to content

Commit 59ae75b

Browse files
committed
HHH-18496 Add json_objectagg
1 parent c58485c commit 59ae75b

File tree

47 files changed

+1607
-71
lines changed

Some content is hidden

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

47 files changed

+1607
-71
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,7 @@ The following functions deal with SQL JSON types, which are not supported on eve
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
16371637
| `json_arrayagg()` | Creates a JSON array by aggregating values
1638+
| `json_objectagg()` | Creates a JSON object by aggregating values
16381639
|===
16391640
16401641
@@ -1963,6 +1964,52 @@ include::{json-example-dir-hql}/JsonArrayAggregateTest.java[tags=hql-json-arraya
19631964
----
19641965
====
19651966
1967+
[[hql-json-objectagg-function]]
1968+
===== `json_objectagg()`
1969+
1970+
Creates a JSON object by aggregating values.
1971+
1972+
[[hql-json-arrayagg-bnf]]
1973+
[source, antlrv4, indent=0]
1974+
----
1975+
include::{extrasdir}/json_objectagg_bnf.txt[]
1976+
----
1977+
1978+
The arguments represent the key and the value to be aggregated to the JSON object,
1979+
separated by the `value` keyword or a `:` (colon).
1980+
1981+
[[hql-json-objectagg-example]]
1982+
====
1983+
[source, java, indent=0]
1984+
----
1985+
include::{json-example-dir-hql}/JsonObjectAggregateTest.java[tags=hql-json-objectagg-example]
1986+
----
1987+
====
1988+
1989+
Although database dependent, usually `null` values are `absent` in the resulting JSON array.
1990+
To retain `null` elements, use the `null on null` clause.
1991+
1992+
[[hql-json-objectagg-null-example]]
1993+
====
1994+
[source, java, indent=0]
1995+
----
1996+
include::{json-example-dir-hql}/JsonObjectAggregateTest.java[tags=hql-json-objectagg-null-example]
1997+
----
1998+
====
1999+
2000+
Duplicate keys usually are retained in the resulting string.
2001+
Use `with unique keys` to specify that the encounter of a duplicate key should cause an error.
2002+
2003+
[[hql-json-objectagg-unique-keys-example]]
2004+
====
2005+
[source, java, indent=0]
2006+
----
2007+
include::{json-example-dir-hql}/JsonObjectAggregateTest.java[tags=hql-json-objectagg-unique-keys-example]
2008+
----
2009+
====
2010+
2011+
WARNING: Some databases like e.g. MySQL, SAP HANA, DB2 and SQL Server do not support raising an error on duplicate keys.
2012+
19662013
[[hql-user-defined-functions]]
19672014
==== Native and user-defined functions
19682015
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"json_objectagg(" expressionOrPredicate ("value"|":") expressionOrPredicate jsonNullClause? uniqueKeysClause? ")" filterClause?
2+
3+
jsonNullClause
4+
: ("absent"|"null") "on null"
5+
;
6+
7+
uniqueKeysClause
8+
: ("with"|"without") "unique keys"
9+
;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,12 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
502502
functionFactory.arrayToString_postgresql();
503503

504504
functionFactory.jsonValue_cockroachdb();
505+
functionFactory.jsonQuery_cockroachdb();
506+
functionFactory.jsonExists_cockroachdb();
505507
functionFactory.jsonObject_postgresql();
506-
functionFactory.jsonExists_postgresql();
507508
functionFactory.jsonArray_postgresql();
509+
functionFactory.jsonArrayAgg_postgresql( false );
510+
functionFactory.jsonObjectAgg_postgresql( false );
508511

509512
// Postgres uses # instead of ^ for XOR
510513
functionContributions.getFunctionRegistry().patternDescriptorBuilder( "bitxor", "(?1#?2)" )

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.jsonObject_db2();
438438
functionFactory.jsonArray_db2();
439439
functionFactory.jsonArrayAgg_db2();
440+
functionFactory.jsonObjectAgg_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.jsonQuery_h2();
408408
functionFactory.jsonExists_h2();
409409
functionFactory.jsonArrayAgg_h2();
410+
functionFactory.jsonObjectAgg_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
@@ -274,6 +274,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
274274
functionFactory.jsonObject_hsqldb();
275275
functionFactory.jsonArray_hsqldb();
276276
functionFactory.jsonArrayAgg_hsqldb();
277+
functionFactory.jsonObjectAgg_h2();
277278
}
278279

279280
//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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
9494
commonFunctionFactory.jsonArray_mariadb();
9595
commonFunctionFactory.jsonQuery_mariadb();
9696
commonFunctionFactory.jsonArrayAgg_mariadb();
97+
commonFunctionFactory.jsonObjectAgg_mariadb();
9798
if ( getVersion().isSameOrAfter( 10, 3, 3 ) ) {
9899
commonFunctionFactory.inverseDistributionOrderedSetAggregates_windowEmulation();
99100
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
@@ -659,6 +659,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
659659
functionFactory.jsonObject_mysql();
660660
functionFactory.jsonArray_mysql();
661661
functionFactory.jsonArrayAgg_mysql();
662+
functionFactory.jsonObjectAgg_mysql();
662663
}
663664
}
664665

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
638638
functionFactory.jsonExists();
639639
functionFactory.jsonObject();
640640
functionFactory.jsonArray();
641-
functionFactory.jsonArrayAgg();
641+
functionFactory.jsonArrayAgg_postgresql( true );
642+
functionFactory.jsonObjectAgg_postgresql( true );
642643
}
643644
else {
644645
functionFactory.jsonValue_postgresql();
@@ -647,12 +648,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
647648
if ( getVersion().isSameOrAfter( 16 ) ) {
648649
functionFactory.jsonObject();
649650
functionFactory.jsonArray();
650-
functionFactory.jsonArrayAgg();
651+
functionFactory.jsonArrayAgg_postgresql( true );
652+
functionFactory.jsonObjectAgg_postgresql( true );
651653
}
652654
else {
653655
functionFactory.jsonObject_postgresql();
654656
functionFactory.jsonArray_postgresql();
655-
functionFactory.jsonArrayAgg_postgresql();
657+
functionFactory.jsonArrayAgg_postgresql( false );
658+
functionFactory.jsonObjectAgg_postgresql( false );
656659
}
657660
}
658661

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

0 commit comments

Comments
 (0)