|
8 | 8 | :array-example-dir-hql: {core-project-dir}/src/test/java/org/hibernate/orm/test/function/array
|
9 | 9 | :json-example-dir-hql: {core-project-dir}/src/test/java/org/hibernate/orm/test/function/json
|
10 | 10 | :xml-example-dir-hql: {core-project-dir}/src/test/java/org/hibernate/orm/test/function/xml
|
| 11 | +:srf-example-dir-hql: {core-project-dir}/src/test/java/org/hibernate/orm/test/function/srf |
11 | 12 | :extrasdir: extras
|
12 | 13 |
|
13 | 14 | This chapter describes Hibernate Query Language (HQL) and Jakarta Persistence Query Language (JPQL).
|
@@ -1197,32 +1198,33 @@ The following functions deal with SQL array types, which are not supported on ev
|
1197 | 1198 | |===
|
1198 | 1199 | | Function | Purpose
|
1199 | 1200 |
|
1200 |
| -| `array()` | Creates an array based on the passed arguments |
1201 |
| -| `array_list()` | Like `array`, but returns the result as `List<?>` |
1202 |
| -| `array_agg()` | Aggregates row values into an array |
1203 |
| -| `array_position()` | Determines the position of an element in an array |
1204 |
| -| `array_positions()` | Determines all positions of an element in an array |
1205 |
| -| `array_positions_list()` | Like `array_positions`, but returns the result as `List<Integer>` |
1206 |
| -| `array_length()` | Determines the length of an array |
1207 |
| -| `array_concat()` | Concatenates array with each other in order |
1208 |
| -| `array_prepend()` | Prepends element to array |
1209 |
| -| `array_append()` | Appends element to array |
1210 |
| -| `array_contains()` | Whether an array contains an element |
1211 |
| -| `array_contains_nullable()` | Whether an array contains an element, supporting `null` element |
1212 |
| -| `array_includes()` | Whether an array contains another array |
1213 |
| -| `array_includes_nullable()` | Whether an array contains another array, supporting `null` elements |
1214 |
| -| `array_intersects()` | Whether an array holds at least one element of another array |
1215 |
| -| `array_intersects_nullable()` | Whether an array holds at least one element of another array, supporting `null` elements |
1216 |
| -| `array_get()` | Accesses the element of an array by index |
1217 |
| -| `array_set()` | Creates array copy with given element at given index |
1218 |
| -| `array_remove()` | Creates array copy with given element removed |
1219 |
| -| `array_remove_index()` | Creates array copy with the element at the given index removed |
1220 |
| -| `array_slice()` | Creates a sub-array of the based on lower and upper index |
1221 |
| -| `array_replace()` | Creates array copy replacing a given element with another |
1222 |
| -| `array_trim()` | Creates array copy trimming the last _N_ elements |
1223 |
| -| `array_fill()` | Creates array filled with the same element _N_ times |
1224 |
| -| `array_fill_list()` | Like `array_fill`, but returns the result as `List<?>` |
1225 |
| -| `array_to_string()` | String representation of array |
| 1201 | +| <<hql-array-constructor-functions,`array()`>> | Creates an array based on the passed arguments |
| 1202 | +| <<hql-array-constructor-functions,`array_list()`>> | Like `array`, but returns the result as `List<?>` |
| 1203 | +| <<hql-array-aggregate-functions,`array_agg()`>> | Aggregates row values into an array |
| 1204 | +| <<hql-array-position-functions,`array_position()`>> | Determines the position of an element in an array |
| 1205 | +| <<hql-array-positions-functions,`array_positions()`>> | Determines all positions of an element in an array |
| 1206 | +| <<hql-array-positions-functions,`array_positions_list()`>> | Like `array_positions`, but returns the result as `List<Integer>` |
| 1207 | +| <<hql-array-length-functions,`array_length()`>> | Determines the length of an array |
| 1208 | +| <<hql-array-concat-functions,`array_concat()`>> | Concatenates array with each other in order |
| 1209 | +| <<hql-array-prepend-functions,`array_prepend()`>> | Prepends element to array |
| 1210 | +| <<hql-array-append-functions,`array_append()`>> | Appends element to array |
| 1211 | +| <<hql-array-contains-functions,`array_contains()`>> | Whether an array contains an element |
| 1212 | +| <<hql-array-contains-functions,`array_contains_nullable()`>> | Whether an array contains an element, supporting `null` element |
| 1213 | +| <<hql-array-includes-example,`array_includes()`>> | Whether an array contains another array |
| 1214 | +| <<hql-array-includes-example,`array_includes_nullable()`>> | Whether an array contains another array, supporting `null` elements |
| 1215 | +| <<hql-array-intersects-functions,`array_intersects()`>> | Whether an array holds at least one element of another array |
| 1216 | +| <<hql-array-intersects-functions,`array_intersects_nullable()`>> | Whether an array holds at least one element of another array, supporting `null` elements |
| 1217 | +| <<hql-array-get-functions,`array_get()`>> | Accesses the element of an array by index |
| 1218 | +| <<hql-array-set-functions,`array_set()`>> | Creates array copy with given element at given index |
| 1219 | +| <<hql-array-remove-functions,`array_remove()`>> | Creates array copy with given element removed |
| 1220 | +| <<hql-array-remove-index-functions,`array_remove_index()`>> | Creates array copy with the element at the given index removed |
| 1221 | +| <<hql-array-slice-functions,`array_slice()`>> | Creates a sub-array of the based on lower and upper index |
| 1222 | +| <<hql-array-replace-functions,`array_replace()`>> | Creates array copy replacing a given element with another |
| 1223 | +| <<hql-array-trim-functions,`array_trim()`>> | Creates array copy trimming the last _N_ elements |
| 1224 | +| <<hql-array-fill-functions,`array_fill()`>> | Creates array filled with the same element _N_ times |
| 1225 | +| <<hql-array-fill-functions,`array_fill_list()`>> | Like `array_fill`, but returns the result as `List<?>` |
| 1226 | +| <<hql-array-to-string-functions,`array_to_string()`>> | String representation of array |
| 1227 | +| <<hql-array-unnest,`unnest()`>> | Turns an array into rows |
1226 | 1228 | |===
|
1227 | 1229 |
|
1228 | 1230 | [[hql-array-constructor-functions]]
|
@@ -1637,6 +1639,32 @@ include::{array-example-dir-hql}/ArrayToStringTest.java[tags=hql-array-to-string
|
1637 | 1639 | ----
|
1638 | 1640 | ====
|
1639 | 1641 |
|
| 1642 | +[[hql-array-unnest]] |
| 1643 | +===== `unnest()` |
| 1644 | +
|
| 1645 | +A <<hql-from-set-returning-functions,set-returning function>>, which turns the single array argument into rows. |
| 1646 | +Returns no rows if the array argument is `null` or an empty array. |
| 1647 | +The `index()` function can be used to access the 1-based array index of an array element. |
| 1648 | +
|
| 1649 | +[[hql-array-unnest-struct-example]] |
| 1650 | +==== |
| 1651 | +[source, java, indent=0] |
| 1652 | +---- |
| 1653 | +include::{array-example-dir-hql}/ArrayUnnestStructTest.java[tags=hql-array-unnest-aggregate-with-ordinality-example] |
| 1654 | +---- |
| 1655 | +==== |
| 1656 | +
|
| 1657 | +The `lateral` keyword is mandatory if the argument refers to a from node item of the same query level. |
| 1658 | +Basic plural attributes can also be joined directly, which is syntax sugar for `lateral unnest(...)`. |
| 1659 | +
|
| 1660 | +[[hql-array-unnest-example]] |
| 1661 | +==== |
| 1662 | +[source, java, indent=0] |
| 1663 | +---- |
| 1664 | +include::{array-example-dir-hql}/ArrayUnnestTest.java[tags=hql-array-unnest-example] |
| 1665 | +---- |
| 1666 | +==== |
| 1667 | +
|
1640 | 1668 | [[hql-functions-json]]
|
1641 | 1669 | ==== Functions for dealing with JSON
|
1642 | 1670 |
|
@@ -2916,6 +2944,48 @@ The CTE name can be used for a `from` clause root or a `join`, similar to entity
|
2916 | 2944 |
|
2917 | 2945 | Refer to the <<hql-with-cte,with clause>> chapter for details about CTEs.
|
2918 | 2946 |
|
| 2947 | +[[hql-from-set-returning-functions]] |
| 2948 | +==== Set-returning functions in `from` clause |
| 2949 | +
|
| 2950 | +A set-returning function is a function that produces rows instead of a single scalar value |
| 2951 | +and is exclusively used in the `from` clause, either as root node or join target. |
| 2952 | +
|
| 2953 | +The `index()` function can be used to access the 1-based index of a returned row. |
| 2954 | +
|
| 2955 | +The following set-returning functions are available on many platforms: |
| 2956 | +
|
| 2957 | +|=== |
| 2958 | +| Function | purpose |
| 2959 | +
|
| 2960 | +| <<hql-array-unnest,`unnest()`>> | Turns an array into rows |
| 2961 | +//| `generate_series()` | Creates a series of values as rows |
| 2962 | +|=== |
| 2963 | +
|
| 2964 | +To use set returning functions defined in the database, it is required to register them in a `FunctionContributor`: |
| 2965 | +
|
| 2966 | +[[hql-from-set-returning-functions-contributor-example]] |
| 2967 | +==== |
| 2968 | +[source, java, indent=0] |
| 2969 | +---- |
| 2970 | +include::{srf-example-dir-hql}/CustomSetReturningFunctionTest.java[tags=hql-set-returning-function-contributor-example] |
| 2971 | +---- |
| 2972 | +==== |
| 2973 | +
|
| 2974 | +After that, the function can be used in the `from` clause: |
| 2975 | +
|
| 2976 | +[[hql-from-set-returning-functions-custom-example]] |
| 2977 | +==== |
| 2978 | +[source, java, indent=0] |
| 2979 | +---- |
| 2980 | +include::{srf-example-dir-hql}/CustomSetReturningFunctionTest.java[tags=hql-set-returning-function-custom-example] |
| 2981 | +---- |
| 2982 | +==== |
| 2983 | +
|
| 2984 | +NOTE: The `index()` function represents the idea of the `with ordinality` SQL syntax, |
| 2985 | +which is not supported on some databases for user defined functions. |
| 2986 | +Hibernate ORM tries to emulate this feature by wrapping invocations as lateral subqueries and using `row_number()`, |
| 2987 | +which may lead to worse performance. |
| 2988 | +
|
2919 | 2989 | [[hql-join]]
|
2920 | 2990 | === Declaring joined entities
|
2921 | 2991 |
|
@@ -3131,6 +3201,17 @@ Most databases support some flavor of `join lateral`, and Hibernate emulates the
|
3131 | 3201 | But emulation is neither very efficient, nor does it support all possible query shapes, so it's important to test on your target database.
|
3132 | 3202 | ====
|
3133 | 3203 |
|
| 3204 | +[[hql-join-set-returning-function]] |
| 3205 | +==== Set-returning functions in joins |
| 3206 | +
|
| 3207 | +A `join` clause may contain a set-returning function, either: |
| 3208 | +
|
| 3209 | +- an uncorrelated set-returning function, which is almost the same as a <<hql-from-set-returning-functions,set-returning function in the `from` clause>>, except that it may have an `on` restriction, or |
| 3210 | +- a _lateral join_, which is a correlated set-returning function, and may refer to other roots declared earlier in the same `from` clause. |
| 3211 | +
|
| 3212 | +The `lateral` keyword just distinguishes the two cases. |
| 3213 | +A lateral join may be an inner or left outer join, but not a right join, nor a full join. |
| 3214 | +
|
3134 | 3215 | [[hql-implicit-join]]
|
3135 | 3216 | ==== Implicit association joins (path expressions)
|
3136 | 3217 |
|
|
0 commit comments