Add jsonb_to_vector and jsonb_to_halfvec cast functions #944
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.
This commit adds the ability to cast JSONB arrays directly into vector and halfvec vector types, for example:
There are several TODOs if the overall idea of the patch is acceptable:
sparsevec(and possibly bit vectors, but maybe a separate discussion givenbitis a core type).vectortojsonb, one must cast to an array (to_jsonb(vector::real[])) otherwise the value is stored as a string. It may be helpful to have avector_to_jsonbfunction similar to how PostgreSQL hasarray_to_json.Motivation
There are several vector data sources that come from JSON documents, whether the embeddings are contained in them or they're being transformed from other sources, such as from data lake files. Additionally, some cases prefer not to duplicate the vector data between the JSON file and a separate vector column, though are fine to use the vector as an expression in a HNSW/IVFFlat index. The previous discussion concluded that the cast from
jsonb::text::vectorwould work, but for cases with bulk imports or transformations, this adds nontrivial overhead.Testing
The tests show how the
jsonb_to_vectorfunctions performs compared tojsonb::text::vectorcast. This was executed ask=10exact-nearest neighbor queries on a dataset of 100,000 vectors that all fit into memory. Each test was run until 50 or 500 transactions was completed, and the average of these transactions taken. The times are in milliseconds.I went through a few different variations of the tests including:
The below show the results from the last two tests
jsonb::text::vectorjsonb_to_vector(similar forjsonb_to_halfvec)Results
Most tests showed a direct jsonb to vector cast had close to a 20% speedup over the
jsonb::text::vectormethod.vector
jsonb::text::vectorjsonb_to_vectorhalfvec