Repository navigation
Add vector similarity search support for SQL and JPA - #2001
Merged
Merged
Conversation
…e 23ai) Signed-off-by: Marvin Froeder <velo.br@gmail.com>
…ply naming and plain-English fixes Signed-off-by: Marvin Froeder <velo.br@gmail.com>
Signed-off-by: Marvin Froeder <velo.br@gmail.com>
… a first-class database Signed-off-by: Marvin Froeder <velo.br@gmail.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds type-safe vector similarity search for SQL and JPA. The layout follows spatial: a shared module plus a SQL module.
Usage
With
PGvectorTemplatesthis rendersorder by (document.embedding <=> ?) asc limit ?. That operator form is what pgvector's HNSW and IVFFlat indexes need.Modules
querydsl-vectorVectorExpression/VectorPath(overfloat[]) providel2Distance,l2SquaredDistance,cosineDistance,innerProduct,negativeInnerProduct,l1Distance,dimensionCount()andl2Norm().VectorExpressions.createConstantVector(float[])turns a value into an expression, so a constant can appear on either side.FloatArrayVectorPathExtensionmapsfloat[]toVectorPathwhen the module is on the annotation processor path.HibernateVectorOperatorPatternsmaps the operators to the HQL functions thathibernate-vectorregisters.HQLTemplatesloads it reflectively, the same way it loads spatial.querydsl-sql-vectorPGvectorTemplatesuses the<->,<=>,<#>and<+>operators, plusvector_dimsandvector_norm.OracleVectorTemplatesusesVECTOR_DISTANCE(.., EUCLIDEAN|EUCLIDEAN_SQUARED|COSINE|DOT|MANHATTAN),VECTOR_DIMENSION_COUNTandVECTOR_NORM.VectorOperatorSqlPatternsexposes the operator maps, so vectors can be combined with other dialect extensions such as PostGIS.PGvectorTypebinds aPGobjecttypedvector. An untyped parameter is ambiguous in PostgreSQL:vector_dims($1)and$1 <-> $2fail as "not unique" because pgvector overloads them forhalfvec.OracleVectorTypebinds withOracleType.VECTOR_FLOAT32.SqlVectorCodegenExtensionmapsvectorcolumns tofloat[], so the SQL exporter generates them asnew VectorPath(forProperty(...))on a plainRelationalPathBase. It does not bind the entity path type, so it works alongsidequerydsl-sql-spatial(covered byVectorCodegenTest).Changes to existing code
Configurationregistered template custom types before the built-in array types, so the genericfloat[]array mapping overwrote the vector type. Custom types are explicit overrides, so they now register last. None of the existing template custom types are arrays, so their behaviour is unchanged.MetaDataSerializerused acreate<Type>factory for every customcom.querydslpath type. It now uses one only when the entity path base class actually declares it, and otherwise falls back to the genericnew XPath(forProperty(...)). Spatial output is unchanged.Database coverage
The integration tests run on PostgreSQL + pgvector and on Oracle 23ai, through both SQL and JPA/Hibernate.
pgvectorcompose service (pgvector/pgvector:0.8.1-pg16, port 5433), which keeps the PostGIS image untouched.Both modules are added to
SQL_MODULESandORACLE_MODULES, and the PostgreSQL job now also startspgvector.Left out for now:
Known limitation
In JPA, a constant vector must be compared with a vector property. Hibernate binds a standalone
float[]parameter as a plain array, socreateConstantVector(q).dimensionCount()does not work there. SQL has no such restriction.With
querydsl-vectoron the processor path, everyfloat[]property becomes aVectorPath, and under the vector templates everyfloat[]binds as a vector. Areal[]column needs an explicitArrayTyperegistration; the docs cover this.Docs
Adds
docs/tutorials/vector.mdand a row in the backend table on the landing page.