Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,8 @@ The following functions deal with SQL array types, which are not supported on ev
| <<hql-array-slice-functions,`array_slice()`>> | Creates a sub-array of the based on lower and upper index
| <<hql-array-replace-functions,`array_replace()`>> | Creates array copy replacing a given element with another
| <<hql-array-trim-functions,`array_trim()`>> | Creates array copy trimming the last _N_ elements
| <<hql-array-reverse-functions,`array_reverse()`>> | Returns a copy of the array with elements in reverse order
| <<hql-array-sort-functions,`array_sort()`>> | Returns a sorted copy of the array
| <<hql-array-fill-functions,`array_fill()`>> | Creates array filled with the same element _N_ times
| <<hql-array-fill-functions,`array_fill_list()`>> | Like `array_fill`, but returns the result as `List<?>`
| <<hql-array-to-string-functions,`array_to_string()`>> | String representation of array
Expand Down Expand Up @@ -1596,6 +1598,46 @@ include::{array-example-dir-hql}/ArrayTrimTest.java[tags=hql-array-trim-example]
----
====

[[hql-array-reverse-functions]]
===== `array_reverse()`

Returns a copy of the array with elements in reverse order. Returns `null` if the argument is `null`.

[[hql-array-reverse-example]]
====
[source, java, indent=0]
----
include::{array-example-dir-hql}/ArrayReverseTest.java[tags=hql-array-reverse-example]
----
====

[[hql-array-sort-functions]]
===== `array_sort()`

Returns a sorted copy of the array. When called with no optional arguments, elements are sorted in ascending order with `null` elements placed last.
The optional second argument allows specifying descending order, and the optional third argument controls the position of `null` elements.
Returns `null` if the first argument is `null`.

[[hql-array-sort-example]]
====
[source, java, indent=0]
----
include::{array-example-dir-hql}/ArraySortTest.java[tags=hql-array-sort-example]
----
====

The second argument controls sort direction: `false` for ascending (default), `true` for descending.
The third argument controls `null` placement: `false` for nulls last, `true` for nulls first.
When the third argument is omitted, it defaults to the value of the second argument.

[[hql-array-sort-descending-nulls-last-example]]
====
[source, java, indent=0]
----
include::{array-example-dir-hql}/ArraySortTest.java[tags=hql-array-sort-descending-nulls-last-example]
----
====

[[hql-array-fill-functions]]
===== `array_fill()` and `array_fill_list()`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
* A {@linkplain Dialect SQL dialect} for CockroachDB.
*
* @author Gavin King
* @author Yoobin Yoon
*/
public class CockroachLegacyDialect extends Dialect {

Expand Down Expand Up @@ -485,6 +486,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_operator();
functionFactory.arrayReplace();
functionFactory.arrayTrim_unnest();
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_unnest();
functionFactory.arrayFill_cockroachdb();
functionFactory.arrayToString_postgresql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
*
* @author Thomas Mueller
* @author Jürgen Kreitler
* @author Yoobin Yoon
*/
public class H2LegacyDialect extends Dialect {

Expand Down Expand Up @@ -409,6 +410,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice();
functionFactory.arrayReplace_h2( getMaximumArraySize() );
functionFactory.arrayTrim_trim_array();
functionFactory.arrayReverse_h2( getMaximumArraySize() );
functionFactory.arraySort_h2( getMaximumArraySize() );
functionFactory.arrayFill_h2();
functionFactory.arrayToString_h2( getMaximumArraySize() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* @author Christoph Sturm
* @author Phillip Baird
* @author Fred Toussi
* @author Yoobin Yoon
*/
public class HSQLLegacyDialect extends Dialect {

Expand Down Expand Up @@ -269,6 +270,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_unnest();
functionFactory.arrayReplace_unnest();
functionFactory.arrayTrim_trim_array();
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_hsql();
functionFactory.arrayFill_hsql();
functionFactory.arrayToString_hsql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
* @author Steve Ebersole
* @author Gavin King
* @author Loïc Lefèvre
* @author Yoobin Yoon
*/
public class OracleLegacyDialect extends Dialect {

Expand Down Expand Up @@ -384,6 +385,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_oracle();
functionFactory.arrayReplace_oracle();
functionFactory.arrayTrim_oracle();
functionFactory.arrayReverse_oracle();
functionFactory.arraySort_oracle();
functionFactory.arrayFill_oracle();
functionFactory.arrayToString_oracle();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
* A {@linkplain Dialect SQL dialect} for PostgreSQL 8 and above.
*
* @author Gavin King
* @author Yoobin Yoon
*/
public class PostgreSQLLegacyDialect extends Dialect {

Expand Down Expand Up @@ -668,6 +669,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
else {
functionFactory.arrayTrim_unnest();
}
if ( getVersion().isSameOrAfter( 18 ) ) {
functionFactory.arrayReverse();
functionFactory.arraySort();
}
else {
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_unnest();
}
functionFactory.arrayFill_postgresql();
functionFactory.arrayToString_postgresql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
* A {@linkplain Dialect SQL dialect} for CockroachDB 23.1 and above.
*
* @author Gavin King
* @author Yoobin Yoon
*/
public class CockroachDialect extends Dialect {

Expand Down Expand Up @@ -477,6 +478,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_operator();
functionFactory.arrayReplace();
functionFactory.arrayTrim_unnest();
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_unnest();
functionFactory.arrayFill_cockroachdb();
functionFactory.arrayToString_postgresql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
*
* @author Thomas Mueller
* @author Jürgen Kreitler
* @author Yoobin Yoon
*/
public class H2Dialect extends Dialect {
private static final DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 2, 1, 214 );
Expand Down Expand Up @@ -342,6 +343,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice();
functionFactory.arrayReplace_h2( getMaximumArraySize() );
functionFactory.arrayTrim_trim_array();
functionFactory.arrayReverse_h2( getMaximumArraySize() );
functionFactory.arraySort_h2( getMaximumArraySize() );
functionFactory.arrayFill_h2();
functionFactory.arrayToString_h2( getMaximumArraySize() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
* @author Christoph Sturm
* @author Phillip Baird
* @author Fred Toussi
* @author Yoobin Yoon
*/
public class HSQLDialect extends Dialect {

Expand Down Expand Up @@ -214,6 +215,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_unnest();
functionFactory.arrayReplace_unnest();
functionFactory.arrayTrim_trim_array();
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_hsql();
functionFactory.arrayFill_hsql();
functionFactory.arrayToString_hsql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
* @author Steve Ebersole
* @author Gavin King
* @author Loïc Lefèvre
* @author Yoobin Yoon
*/
public class OracleDialect extends Dialect {

Expand Down Expand Up @@ -397,6 +398,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
functionFactory.arraySlice_oracle();
functionFactory.arrayReplace_oracle();
functionFactory.arrayTrim_oracle();
functionFactory.arrayReverse_oracle();
functionFactory.arraySort_oracle();
functionFactory.arrayFill_oracle();
functionFactory.arrayToString_oracle();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
* <a href="https://www.postgresql.org/docs/current/index.html">PostgreSQL documentation</a>.
*
* @author Gavin King
* @author Yoobin Yoon
*/
public class PostgreSQLDialect extends Dialect {
protected final static DatabaseVersion MINIMUM_VERSION = DatabaseVersion.make( 13 );
Expand Down Expand Up @@ -628,6 +629,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
else {
functionFactory.arrayTrim_unnest();
}
if ( getVersion().isSameOrAfter( 18 ) ) {
functionFactory.arrayReverse();
functionFactory.arraySort();
}
else {
functionFactory.arrayReverse_unnest();
functionFactory.arraySort_unnest();
}
functionFactory.arrayFill_postgresql();
functionFactory.arrayToString_postgresql();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
*
* @author Steve Ebersole
* @author Gavin King
* @author Yoobin Yoon
*/
public class CommonFunctionFactory {

Expand Down Expand Up @@ -3321,6 +3322,97 @@ public void arrayTrim_oracle() {
functionRegistry.register( "array_trim", new OracleArrayTrimFunction() );
}

/**
* CockroachDB and PostgreSQL array_reverse() function
*/
public void arrayReverse() {
functionRegistry.namedDescriptorBuilder( "array_reverse" )
.setArgumentsValidator(
StandardArgumentsValidators.composite(
StandardArgumentsValidators.exactly( 1 ),
ArrayArgumentValidator.DEFAULT_INSTANCE
) )
.setReturnTypeResolver( ArrayViaArgumentReturnTypeResolver.DEFAULT_INSTANCE )
.setArgumentTypeResolver(
StandardFunctionArgumentTypeResolvers.composite(
StandardFunctionArgumentTypeResolvers.invariant( ANY )
) )
.setArgumentListSignature( "(ARRAY array)" )
.register();
}

/**
* array_reverse() emulation for PostgreSQL versions before 18 and HSQLDB
*/
public void arrayReverse_unnest() {
functionRegistry.register( "array_reverse", new PostgreSQLArrayReverseEmulation() );
}

/**
* Oracle array_reverse() function
*/
public void arrayReverse_oracle() {
functionRegistry.register( "array_reverse", new OracleArrayReverseFunction() );
}

/**
* H2 array_reverse() function
*/
public void arrayReverse_h2(int maximumArraySize) {
functionRegistry.register( "array_reverse", new H2ArrayReverseFunction( maximumArraySize ) );
}

/**
* CockroachDB and PostgreSQL array_sort() function
*/
public void arraySort() {
functionRegistry.namedDescriptorBuilder( "array_sort" )
.setArgumentsValidator(
StandardArgumentsValidators.composite(
StandardArgumentsValidators.between( 1, 3 ),
ArrayArgumentValidator.DEFAULT_INSTANCE
)
)
.setReturnTypeResolver( ArrayViaArgumentReturnTypeResolver.DEFAULT_INSTANCE )
.setArgumentTypeResolver(
StandardFunctionArgumentTypeResolvers.composite(
StandardFunctionArgumentTypeResolvers.invariant( ANY ),
StandardFunctionArgumentTypeResolvers.IMPLIED_RESULT_TYPE,
StandardFunctionArgumentTypeResolvers.IMPLIED_RESULT_TYPE
)
)
.setArgumentListSignature( "(ARRAY array[, boolean descending[, boolean nulls_first]])" )
.register();
}

/**
* PostgreSQL array_sort() emulation for versions before 18
*/
public void arraySort_unnest() {
functionRegistry.register( "array_sort", new PostgreSQLArraySortEmulation() );
}

/**
* Oracle array_sort() function
*/
public void arraySort_oracle() {
functionRegistry.register( "array_sort", new OracleArraySortFunction() );
}

/**
* H2 array_sort() function
*/
public void arraySort_h2(int maximumArraySize) {
functionRegistry.register( "array_sort", new H2ArraySortFunction( maximumArraySize ) );
}

/**
* HSQL array_sort() function
*/
public void arraySort_hsql() {
functionRegistry.register( "array_sort", new HSQLArraySortFunction() );
}

/**
* H2 array_fill() function
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.dialect.function.array;

import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
import org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers;

import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ANY;

/**
* Encapsulates the validator, return type and argument type resolvers for the array_reverse functions.
* Subclasses only have to implement the rendering.
*/
public abstract class AbstractArrayReverseFunction extends AbstractSqmSelfRenderingFunctionDescriptor {

public AbstractArrayReverseFunction() {
super(
"array_reverse",
StandardArgumentsValidators.composite(
StandardArgumentsValidators.exactly( 1 ),
ArrayArgumentValidator.DEFAULT_INSTANCE
),
ArrayViaArgumentReturnTypeResolver.DEFAULT_INSTANCE,
StandardFunctionArgumentTypeResolvers.composite(
StandardFunctionArgumentTypeResolvers.invariant( ANY )
)
);
}
}
Loading