|
| 1 | +[[types-module]] |
| 2 | +== Hibernate Types module |
| 3 | +:root-project-dir: ../../../../../../../.. |
| 4 | +:types-project-dir: {root-project-dir}/hibernate-types |
| 5 | +:example-dir-types: {types-project-dir}/src/test/java/org/hibernate/types |
| 6 | +:extrasdir: extras |
| 7 | + |
| 8 | +[[types-module-overview]] |
| 9 | +=== Overview |
| 10 | + |
| 11 | +The Hibernate ORM core module tries to be as minimal as possible and only model functionality |
| 12 | +that is somewhat "standard" in the SQL space or can only be modeled as part of the core module. |
| 13 | +To avoid growing that module further unnecessarily, support for certain special SQL types or functions |
| 14 | +is separated out into the Hibernate ORM types module. |
| 15 | + |
| 16 | +[[types-module-setup]] |
| 17 | +=== Setup |
| 18 | + |
| 19 | +You need to include the `hibernate-types` dependency in your build environment. |
| 20 | +For Maven, you need to add the following dependency: |
| 21 | + |
| 22 | +[[types-module-setup-maven-example]] |
| 23 | +.Maven dependency |
| 24 | +==== |
| 25 | +[source,xml] |
| 26 | +---- |
| 27 | +<dependency> |
| 28 | + <groupId>org.hibernate.orm</groupId> |
| 29 | + <artifactId>hibernate-types</artifactId> |
| 30 | + <version>${hibernate.version}</version> |
| 31 | +</dependency> |
| 32 | +---- |
| 33 | +==== |
| 34 | + |
| 35 | +The module contains service implementations that are picked up by the Java `ServiceLoader` automatically, |
| 36 | +so no further configuration is necessary to make the features available. |
| 37 | + |
| 38 | +[[types-module-vector]] |
| 39 | +=== Vector type support |
| 40 | + |
| 41 | +The Hibernate ORM types module comes with support for a special `vector` data type that essentially represents an array of floats. |
| 42 | + |
| 43 | +So far, only the PostgreSQL extension `pgvector` is supported, but in theory, |
| 44 | +the vector specific functions could be implemented to work with every database that supports arrays. |
| 45 | + |
| 46 | +For further details, refer to the https://github.com/pgvector/pgvector#querying[pgvector documentation]. |
| 47 | + |
| 48 | +[[types-module-vector-usage]] |
| 49 | +==== Usage |
| 50 | + |
| 51 | +Annotate a persistent attribute with `@JdbcTypeCode(SqlTypes.VECTOR)` and specify the vector length with `@Array(length = ...)`. |
| 52 | + |
| 53 | +[[types-module-vector-usage-example]] |
| 54 | +==== |
| 55 | +[source, JAVA, indent=0] |
| 56 | +---- |
| 57 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=usage-example] |
| 58 | +---- |
| 59 | +==== |
| 60 | + |
| 61 | +To cast the string representation of a vector to the vector data type, simply use an HQL cast i.e. `cast('[1,2,3]' as vector)`. |
| 62 | + |
| 63 | +[[types-module-vector-functions]] |
| 64 | +==== Functions |
| 65 | + |
| 66 | +Expressions of the vector type can be used with various vector functions. |
| 67 | + |
| 68 | +[[types-module-vector-functions-overview]] |
| 69 | +|=== |
| 70 | +| Function | Purpose |
| 71 | + |
| 72 | +| `cosine_distance()` | Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors. Maps to the `<``=``>` operator |
| 73 | +| `euclidean_distance()` | Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors. Maps to the `<``-``>` operator |
| 74 | +| `l2_distance()` | Alias for `euclidean_distance()` |
| 75 | +| `taxicab_distance()` | Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors |
| 76 | +| `l1_distance()` | Alias for `taxicab_distance()` |
| 77 | +| `inner_product()` | Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors |
| 78 | +| `negative_inner_product()` | Computes the negative inner product. Maps to the `<``#``>` operator |
| 79 | +| `vector_dims()` | Determines the dimensions of a vector |
| 80 | +| `vector_norm()` | Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector |
| 81 | +|=== |
| 82 | + |
| 83 | +In addition to these special vector functions, it is also possible to use vectors with the following builtin operators |
| 84 | + |
| 85 | +`<vector1> + <vector2> = <vector3>`:: Element-wise addition of vectors. |
| 86 | +`<vector1> - <vector2> = <vector3>`:: Element-wise subtraction of vectors. |
| 87 | +`<vector1> * <vector2> = <vector3>`:: Element-wise multiplication of vectors. |
| 88 | +`sum(<vector1>) = <vector2>`:: Aggregate function support for element-wise summation of vectors. |
| 89 | +`avg(<vector1>) = <vector2>`:: Aggregate function support for element-wise average of vectors. |
| 90 | + |
| 91 | +[[types-module-vector-functions-cosine-distance]] |
| 92 | +===== `cosine_distance()` |
| 93 | + |
| 94 | +Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors, |
| 95 | +which is `1 - inner_product( v1, v2 ) / ( vector_norm( v1 ) * vector_norm( v2 ) )`. Maps to the `<``=``>` pgvector operator. |
| 96 | + |
| 97 | +[[types-module-vector-functions-cosine-distance-example]] |
| 98 | +==== |
| 99 | +[source, JAVA, indent=0] |
| 100 | +---- |
| 101 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=cosine-distance-example] |
| 102 | +---- |
| 103 | +==== |
| 104 | + |
| 105 | +[[types-module-vector-functions-euclidean-distance]] |
| 106 | +===== `euclidean_distance()` and `l2_distance()` |
| 107 | + |
| 108 | +Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors, |
| 109 | +which is `sqrt( sum( (v1_i - v2_i)^2 ) )`. Maps to the `<``-``>` pgvector operator. |
| 110 | +The `l2_distance()` function is an alias. |
| 111 | + |
| 112 | +[[types-module-vector-functions-euclidean-distance-example]] |
| 113 | +==== |
| 114 | +[source, JAVA, indent=0] |
| 115 | +---- |
| 116 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=euclidean-distance-example] |
| 117 | +---- |
| 118 | +==== |
| 119 | + |
| 120 | +[[types-module-vector-functions-taxicab-distance]] |
| 121 | +===== `taxicab_distance()` and `l1_distance()` |
| 122 | + |
| 123 | +Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors, |
| 124 | +which is `vector_norm(v1) - vector_norm(v2)`. |
| 125 | +The `l1_distance()` function is an alias. |
| 126 | + |
| 127 | +[[types-module-vector-functions-taxicab-distance-example]] |
| 128 | +==== |
| 129 | +[source, JAVA, indent=0] |
| 130 | +---- |
| 131 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=taxicab-distance-example] |
| 132 | +---- |
| 133 | +==== |
| 134 | + |
| 135 | +[[types-module-vector-functions-inner-product]] |
| 136 | +===== `inner_product()` and `negative_inner_product()` |
| 137 | + |
| 138 | +Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors, |
| 139 | +which is `sum( v1_i * v2_i )`. The `negative_inner_product()` function maps to the `<``#``>` pgvector operator, |
| 140 | +and the `inner_product()` function as well, but multiplies the result time `-1`. |
| 141 | + |
| 142 | +[[types-module-vector-functions-inner-product-example]] |
| 143 | +==== |
| 144 | +[source, JAVA, indent=0] |
| 145 | +---- |
| 146 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=inner-product-example] |
| 147 | +---- |
| 148 | +==== |
| 149 | + |
| 150 | +[[types-module-vector-functions-vector-dims]] |
| 151 | +===== `vector_dims()` |
| 152 | + |
| 153 | +Determines the dimensions of a vector. |
| 154 | + |
| 155 | +[[types-module-vector-functions-vector-dims-example]] |
| 156 | +==== |
| 157 | +[source, JAVA, indent=0] |
| 158 | +---- |
| 159 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=vector-dims-example] |
| 160 | +---- |
| 161 | +==== |
| 162 | + |
| 163 | +[[types-module-vector-functions-vector-norm]] |
| 164 | +===== `vector_norm()` |
| 165 | + |
| 166 | +Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector, |
| 167 | +which is `sqrt( sum( v_i^2 ) )`. |
| 168 | + |
| 169 | +[[types-module-vector-functions-vector-norm-example]] |
| 170 | +==== |
| 171 | +[source, JAVA, indent=0] |
| 172 | +---- |
| 173 | +include::{example-dir-types}/vector/PGVectorTest.java[tags=vector-norm-example] |
| 174 | +---- |
| 175 | +==== |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
0 commit comments