-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-17357 Add hibernate-types module with pgvector support
- Loading branch information
Showing
22 changed files
with
1,178 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
documentation/src/main/asciidoc/userguide/chapters/query/types/TypesModule.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
[[types-module]] | ||
== Hibernate Types module | ||
:root-project-dir: ../../../../../../../.. | ||
:types-project-dir: {root-project-dir}/hibernate-types | ||
:example-dir-types: {types-project-dir}/src/test/java/org/hibernate/types | ||
:extrasdir: extras | ||
|
||
[[types-module-overview]] | ||
=== Overview | ||
|
||
The Hibernate ORM core module tries to be as minimal as possible and only model functionality | ||
that is somewhat "standard" in the SQL space or can only be modeled as part of the core module. | ||
To avoid growing that module further unnecessarily, support for certain special SQL types or functions | ||
is separated out into the Hibernate ORM types module. | ||
|
||
[[types-module-setup]] | ||
=== Setup | ||
|
||
You need to include the `hibernate-types` dependency in your build environment. | ||
For Maven, you need to add the following dependency: | ||
|
||
[[types-module-setup-maven-example]] | ||
.Maven dependency | ||
==== | ||
[source,xml] | ||
---- | ||
<dependency> | ||
<groupId>org.hibernate.orm</groupId> | ||
<artifactId>hibernate-types</artifactId> | ||
<version>${hibernate.version}</version> | ||
</dependency> | ||
---- | ||
==== | ||
|
||
The module contains service implementations that are picked up by the Java `ServiceLoader` automatically, | ||
so no further configuration is necessary to make the features available. | ||
|
||
[[types-module-vector]] | ||
=== Vector type support | ||
|
||
The Hibernate ORM types module comes with support for a special `vector` data type that essentially represents an array of floats. | ||
|
||
So far, only the PostgreSQL extension `pgvector` is supported, but in theory, | ||
the vector specific functions could be implemented to work with every database that supports arrays. | ||
|
||
For further details, refer to the https://github.com/pgvector/pgvector#querying[pgvector documentation]. | ||
|
||
[[types-module-vector-usage]] | ||
==== Usage | ||
|
||
Annotate a persistent attribute with `@JdbcTypeCode(SqlTypes.VECTOR)` and specify the vector length with `@Array(length = ...)`. | ||
|
||
[[types-module-vector-usage-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=usage-example] | ||
---- | ||
==== | ||
|
||
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)`. | ||
|
||
[[types-module-vector-functions]] | ||
==== Functions | ||
|
||
Expressions of the vector type can be used with various vector functions. | ||
|
||
[[types-module-vector-functions-overview]] | ||
|=== | ||
| Function | Purpose | ||
|
||
| `cosine_distance()` | Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors. Maps to the `<``=``>` operator | ||
| `euclidean_distance()` | Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors. Maps to the `<``-``>` operator | ||
| `l2_distance()` | Alias for `euclidean_distance()` | ||
| `taxicab_distance()` | Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors | ||
| `l1_distance()` | Alias for `taxicab_distance()` | ||
| `inner_product()` | Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors | ||
| `negative_inner_product()` | Computes the negative inner product. Maps to the `<``#``>` operator | ||
| `vector_dims()` | Determines the dimensions of a vector | ||
| `vector_norm()` | Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector | ||
|=== | ||
|
||
In addition to these special vector functions, it is also possible to use vectors with the following builtin operators | ||
|
||
`<vector1> + <vector2> = <vector3>`:: Element-wise addition of vectors. | ||
`<vector1> - <vector2> = <vector3>`:: Element-wise subtraction of vectors. | ||
`<vector1> * <vector2> = <vector3>`:: Element-wise multiplication of vectors. | ||
`sum(<vector1>) = <vector2>`:: Aggregate function support for element-wise summation of vectors. | ||
`avg(<vector1>) = <vector2>`:: Aggregate function support for element-wise average of vectors. | ||
|
||
[[types-module-vector-functions-cosine-distance]] | ||
===== `cosine_distance()` | ||
|
||
Computes the https://en.wikipedia.org/wiki/Cosine_similarity[cosine distance] between two vectors, | ||
which is `1 - inner_product( v1, v2 ) / ( vector_norm( v1 ) * vector_norm( v2 ) )`. Maps to the `<``=``>` pgvector operator. | ||
|
||
[[types-module-vector-functions-cosine-distance-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=cosine-distance-example] | ||
---- | ||
==== | ||
|
||
[[types-module-vector-functions-euclidean-distance]] | ||
===== `euclidean_distance()` and `l2_distance()` | ||
|
||
Computes the https://en.wikipedia.org/wiki/Euclidean_distance[euclidean distance] between two vectors, | ||
which is `sqrt( sum( (v1_i - v2_i)^2 ) )`. Maps to the `<``-``>` pgvector operator. | ||
The `l2_distance()` function is an alias. | ||
|
||
[[types-module-vector-functions-euclidean-distance-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=euclidean-distance-example] | ||
---- | ||
==== | ||
|
||
[[types-module-vector-functions-taxicab-distance]] | ||
===== `taxicab_distance()` and `l1_distance()` | ||
|
||
Computes the https://en.wikipedia.org/wiki/Taxicab_geometry[taxicab distance] between two vectors, | ||
which is `vector_norm(v1) - vector_norm(v2)`. | ||
The `l1_distance()` function is an alias. | ||
|
||
[[types-module-vector-functions-taxicab-distance-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=taxicab-distance-example] | ||
---- | ||
==== | ||
|
||
[[types-module-vector-functions-inner-product]] | ||
===== `inner_product()` and `negative_inner_product()` | ||
|
||
Computes the https://en.wikipedia.org/wiki/Inner_product_space[inner product] between two vectors, | ||
which is `sum( v1_i * v2_i )`. The `negative_inner_product()` function maps to the `<``#``>` pgvector operator, | ||
and the `inner_product()` function as well, but multiplies the result time `-1`. | ||
|
||
[[types-module-vector-functions-inner-product-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=inner-product-example] | ||
---- | ||
==== | ||
|
||
[[types-module-vector-functions-vector-dims]] | ||
===== `vector_dims()` | ||
|
||
Determines the dimensions of a vector. | ||
|
||
[[types-module-vector-functions-vector-dims-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=vector-dims-example] | ||
---- | ||
==== | ||
|
||
[[types-module-vector-functions-vector-norm]] | ||
===== `vector_norm()` | ||
|
||
Computes the https://en.wikipedia.org/wiki/Euclidean_space#Euclidean_norm[Euclidean norm] of a vector, | ||
which is `sqrt( sum( v_i^2 ) )`. | ||
|
||
[[types-module-vector-functions-vector-norm-example]] | ||
==== | ||
[source, JAVA, indent=0] | ||
---- | ||
include::{example-dir-types}/vector/PGVectorTest.java[tags=vector-norm-example] | ||
---- | ||
==== | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.