Skip to content

Commit 286d696

Browse files
committed
HHH-18496 Add json_objectagg
1 parent 1bd869f commit 286d696

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
@@ -498,9 +498,12 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
498498
functionFactory.arrayToString_postgresql();
499499

500500
functionFactory.jsonValue_cockroachdb();
501+
functionFactory.jsonQuery_cockroachdb();
502+
functionFactory.jsonExists_cockroachdb();
501503
functionFactory.jsonObject_postgresql();
502-
functionFactory.jsonExists_postgresql();
503504
functionFactory.jsonArray_postgresql();
505+
functionFactory.jsonArrayAgg_postgresql( false );
506+
functionFactory.jsonObjectAgg_postgresql( false );
504507

505508
// Postgres uses # instead of ^ for XOR
506509
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
@@ -438,6 +438,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
438438
functionFactory.jsonObject_db2();
439439
functionFactory.jsonArray_db2();
440440
functionFactory.jsonArrayAgg_db2();
441+
functionFactory.jsonObjectAgg_db2();
441442
}
442443
}
443444
}

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
@@ -408,6 +408,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
408408
functionFactory.jsonQuery_h2();
409409
functionFactory.jsonExists_h2();
410410
functionFactory.jsonArrayAgg_h2();
411+
functionFactory.jsonObjectAgg_h2();
411412
}
412413
}
413414
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
@@ -276,6 +276,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
276276
functionFactory.jsonObject_hsqldb();
277277
functionFactory.jsonArray_hsqldb();
278278
functionFactory.jsonArrayAgg_hsqldb();
279+
functionFactory.jsonObjectAgg_h2();
279280
}
280281

281282
//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
@@ -96,6 +96,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
9696
commonFunctionFactory.jsonArray_mariadb();
9797
commonFunctionFactory.jsonQuery_mariadb();
9898
commonFunctionFactory.jsonArrayAgg_mariadb();
99+
commonFunctionFactory.jsonObjectAgg_mariadb();
99100
if ( getVersion().isSameOrAfter( 10, 3, 3 ) ) {
100101
commonFunctionFactory.inverseDistributionOrderedSetAggregates_windowEmulation();
101102
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
@@ -661,6 +661,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
661661
functionFactory.jsonObject_mysql();
662662
functionFactory.jsonArray_mysql();
663663
functionFactory.jsonArrayAgg_mysql();
664+
functionFactory.jsonObjectAgg_mysql();
664665
}
665666
}
666667

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
@@ -629,7 +629,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
629629
functionFactory.jsonExists();
630630
functionFactory.jsonObject();
631631
functionFactory.jsonArray();
632-
functionFactory.jsonArrayAgg();
632+
functionFactory.jsonArrayAgg_postgresql( true );
633+
functionFactory.jsonObjectAgg_postgresql( true );
633634
}
634635
else {
635636
functionFactory.jsonValue_postgresql();
@@ -638,12 +639,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
638639
if ( getVersion().isSameOrAfter( 16 ) ) {
639640
functionFactory.jsonObject();
640641
functionFactory.jsonArray();
641-
functionFactory.jsonArrayAgg();
642+
functionFactory.jsonArrayAgg_postgresql( true );
643+
functionFactory.jsonObjectAgg_postgresql( true );
642644
}
643645
else {
644646
functionFactory.jsonObject_postgresql();
645647
functionFactory.jsonArray_postgresql();
646-
functionFactory.jsonArrayAgg_postgresql();
648+
functionFactory.jsonArrayAgg_postgresql( false );
649+
functionFactory.jsonObjectAgg_postgresql( false );
647650
}
648651
}
649652

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

0 commit comments

Comments
 (0)