Skip to content

Commit 3ceb4a3

Browse files
committed
HHH-19826 Add array_reverse and array_sort functions
Implement array_reverse() and array_sort() with PostgreSQL 18 semantics. Supports PostgreSQL, H2, HSQLDB, Oracle, and CockroachDB with native functions or SQL emulation as appropriate. Signed-off-by: Yoobin Yoon <[email protected]>
1 parent 79d98c2 commit 3ceb4a3

29 files changed

+1345
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,8 @@ The following functions deal with SQL array types, which are not supported on ev
12201220
| <<hql-array-slice-functions,`array_slice()`>> | Creates a sub-array of the based on lower and upper index
12211221
| <<hql-array-replace-functions,`array_replace()`>> | Creates array copy replacing a given element with another
12221222
| <<hql-array-trim-functions,`array_trim()`>> | Creates array copy trimming the last _N_ elements
1223+
| <<hql-array-reverse-functions,`array_reverse()`>> | Returns a copy of the array with elements in reverse order
1224+
| <<hql-array-sort-functions,`array_sort()`>> | Returns a sorted copy of the array
12231225
| <<hql-array-fill-functions,`array_fill()`>> | Creates array filled with the same element _N_ times
12241226
| <<hql-array-fill-functions,`array_fill_list()`>> | Like `array_fill`, but returns the result as `List<?>`
12251227
| <<hql-array-to-string-functions,`array_to_string()`>> | String representation of array
@@ -1596,6 +1598,46 @@ include::{array-example-dir-hql}/ArrayTrimTest.java[tags=hql-array-trim-example]
15961598
----
15971599
====
15981600
1601+
[[hql-array-reverse-functions]]
1602+
===== `array_reverse()`
1603+
1604+
Returns a copy of the array with elements in reverse order. Returns `null` if the argument is `null`.
1605+
1606+
[[hql-array-reverse-example]]
1607+
====
1608+
[source, java, indent=0]
1609+
----
1610+
include::{array-example-dir-hql}/ArrayReverseTest.java[tags=hql-array-reverse-example]
1611+
----
1612+
====
1613+
1614+
[[hql-array-sort-functions]]
1615+
===== `array_sort()`
1616+
1617+
Returns a sorted copy of the array. When called with no optional arguments, elements are sorted in ascending order with `null` elements placed last.
1618+
The optional second argument allows specifying descending order, and the optional third argument controls the position of `null` elements.
1619+
Returns `null` if the first argument is `null`.
1620+
1621+
[[hql-array-sort-example]]
1622+
====
1623+
[source, java, indent=0]
1624+
----
1625+
include::{array-example-dir-hql}/ArraySortTest.java[tags=hql-array-sort-example]
1626+
----
1627+
====
1628+
1629+
The second argument controls sort direction: `false` for ascending (default), `true` for descending.
1630+
The third argument controls `null` placement: `false` for nulls last, `true` for nulls first.
1631+
When the third argument is omitted, it defaults to the value of the second argument.
1632+
1633+
[[hql-array-sort-descending-nulls-last-example]]
1634+
====
1635+
[source, java, indent=0]
1636+
----
1637+
include::{array-example-dir-hql}/ArraySortTest.java[tags=hql-array-sort-descending-nulls-last-example]
1638+
----
1639+
====
1640+
15991641
[[hql-array-fill-functions]]
16001642
===== `array_fill()` and `array_fill_list()`
16011643

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
* A {@linkplain Dialect SQL dialect} for CockroachDB.
142142
*
143143
* @author Gavin King
144+
* @author Yoobin Yoon
144145
*/
145146
public class CockroachLegacyDialect extends Dialect {
146147

@@ -485,6 +486,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
485486
functionFactory.arraySlice_operator();
486487
functionFactory.arrayReplace();
487488
functionFactory.arrayTrim_unnest();
489+
functionFactory.arrayReverse_unnest();
490+
functionFactory.arraySort_unnest();
488491
functionFactory.arrayFill_cockroachdb();
489492
functionFactory.arrayToString_postgresql();
490493

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
*
129129
* @author Thomas Mueller
130130
* @author Jürgen Kreitler
131+
* @author Yoobin Yoon
131132
*/
132133
public class H2LegacyDialect extends Dialect {
133134

@@ -409,6 +410,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
409410
functionFactory.arraySlice();
410411
functionFactory.arrayReplace_h2( getMaximumArraySize() );
411412
functionFactory.arrayTrim_trim_array();
413+
functionFactory.arrayReverse_h2( getMaximumArraySize() );
414+
functionFactory.arraySort_h2( getMaximumArraySize() );
412415
functionFactory.arrayFill_h2();
413416
functionFactory.arrayToString_h2( getMaximumArraySize() );
414417

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
* @author Christoph Sturm
9696
* @author Phillip Baird
9797
* @author Fred Toussi
98+
* @author Yoobin Yoon
9899
*/
99100
public class HSQLLegacyDialect extends Dialect {
100101

@@ -269,6 +270,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
269270
functionFactory.arraySlice_unnest();
270271
functionFactory.arrayReplace_unnest();
271272
functionFactory.arrayTrim_trim_array();
273+
functionFactory.arrayReverse_unnest();
274+
functionFactory.arraySort_hsql();
272275
functionFactory.arrayFill_hsql();
273276
functionFactory.arrayToString_hsql();
274277

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
* @author Steve Ebersole
171171
* @author Gavin King
172172
* @author Loïc Lefèvre
173+
* @author Yoobin Yoon
173174
*/
174175
public class OracleLegacyDialect extends Dialect {
175176

@@ -384,6 +385,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
384385
functionFactory.arraySlice_oracle();
385386
functionFactory.arrayReplace_oracle();
386387
functionFactory.arrayTrim_oracle();
388+
functionFactory.arrayReverse_oracle();
389+
functionFactory.arraySort_oracle();
387390
functionFactory.arrayFill_oracle();
388391
functionFactory.arrayToString_oracle();
389392

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
* A {@linkplain Dialect SQL dialect} for PostgreSQL 8 and above.
172172
*
173173
* @author Gavin King
174+
* @author Yoobin Yoon
174175
*/
175176
public class PostgreSQLLegacyDialect extends Dialect {
176177

@@ -668,6 +669,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
668669
else {
669670
functionFactory.arrayTrim_unnest();
670671
}
672+
if ( getVersion().isSameOrAfter( 18 ) ) {
673+
functionFactory.arrayReverse();
674+
functionFactory.arraySort();
675+
}
676+
else {
677+
functionFactory.arrayReverse_unnest();
678+
functionFactory.arraySort_unnest();
679+
}
671680
functionFactory.arrayFill_postgresql();
672681
functionFactory.arrayToString_postgresql();
673682

hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
* A {@linkplain Dialect SQL dialect} for CockroachDB 23.1 and above.
137137
*
138138
* @author Gavin King
139+
* @author Yoobin Yoon
139140
*/
140141
public class CockroachDialect extends Dialect {
141142

@@ -477,6 +478,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
477478
functionFactory.arraySlice_operator();
478479
functionFactory.arrayReplace();
479480
functionFactory.arrayTrim_unnest();
481+
functionFactory.arrayReverse_unnest();
482+
functionFactory.arraySort_unnest();
480483
functionFactory.arrayFill_cockroachdb();
481484
functionFactory.arrayToString_postgresql();
482485

hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
*
127127
* @author Thomas Mueller
128128
* @author Jürgen Kreitler
129+
* @author Yoobin Yoon
129130
*/
130131
public class H2Dialect extends Dialect {
131132
private static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 2, 1, 214 );
@@ -342,6 +343,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
342343
functionFactory.arraySlice();
343344
functionFactory.arrayReplace_h2( getMaximumArraySize() );
344345
functionFactory.arrayTrim_trim_array();
346+
functionFactory.arrayReverse_h2( getMaximumArraySize() );
347+
functionFactory.arraySort_h2( getMaximumArraySize() );
345348
functionFactory.arrayFill_h2();
346349
functionFactory.arrayToString_h2( getMaximumArraySize() );
347350

hibernate-core/src/main/java/org/hibernate/dialect/HSQLDialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Christoph Sturm
7878
* @author Phillip Baird
7979
* @author Fred Toussi
80+
* @author Yoobin Yoon
8081
*/
8182
public class HSQLDialect extends Dialect {
8283

@@ -214,6 +215,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
214215
functionFactory.arraySlice_unnest();
215216
functionFactory.arrayReplace_unnest();
216217
functionFactory.arrayTrim_trim_array();
218+
functionFactory.arrayReverse_unnest();
219+
functionFactory.arraySort_hsql();
217220
functionFactory.arrayFill_hsql();
218221
functionFactory.arrayToString_hsql();
219222

hibernate-core/src/main/java/org/hibernate/dialect/OracleDialect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
* @author Steve Ebersole
180180
* @author Gavin King
181181
* @author Loïc Lefèvre
182+
* @author Yoobin Yoon
182183
*/
183184
public class OracleDialect extends Dialect {
184185

@@ -397,6 +398,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
397398
functionFactory.arraySlice_oracle();
398399
functionFactory.arrayReplace_oracle();
399400
functionFactory.arrayTrim_oracle();
401+
functionFactory.arrayReverse_oracle();
402+
functionFactory.arraySort_oracle();
400403
functionFactory.arrayFill_oracle();
401404
functionFactory.arrayToString_oracle();
402405

0 commit comments

Comments
 (0)