-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(search): Hnsw Vector Search Plan Operator & Executor #2434
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bee69e7
Add KnnSearch Plan Operator & Executor
Beihao-Zhou 42bec27
Merge branch 'unstable' into hnsw-execution
Beihao-Zhou 7a1a991
Add KnnScan plan executor
Beihao-Zhou 11a0f9d
Add KnnScan executor tests
Beihao-Zhou 0253e47
Fix Lint & Add Todo comment
Beihao-Zhou 4cc316a
Merge branch 'unstable' into hnsw-execution
Beihao-Zhou cd0e1e8
Add range query
Beihao-Zhou 96693cd
Merge branch 'unstable' into hnsw-execution
Beihao-Zhou d4d1db0
Refactor & Modularize HNSW test suite
Beihao-Zhou e6eda7e
Add tests
Beihao-Zhou c9a9847
Fix edge case
Beihao-Zhou 1a85e49
Merge branch 'unstable' into hnsw-execution
PragmaTwice e41cd9b
Fix clang
Beihao-Zhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/search/executors/hnsw_vector_field_knn_scan_executor.h
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,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "db_util.h" | ||
#include "encoding.h" | ||
#include "search/hnsw_indexer.h" | ||
#include "search/plan_executor.h" | ||
#include "search/search_encoding.h" | ||
#include "storage/redis_db.h" | ||
#include "storage/redis_metadata.h" | ||
#include "storage/storage.h" | ||
#include "string_util.h" | ||
|
||
namespace kqir { | ||
|
||
struct HnswVectorFieldKnnScanExecutor : ExecutorNode { | ||
HnswVectorFieldKnnScan *scan; | ||
redis::LatestSnapShot ss; | ||
bool initialized = false; | ||
|
||
IndexInfo *index; | ||
redis::SearchKey search_key; | ||
redis::HnswVectorFieldMetadata field_metadata; | ||
std::vector<std::string> row_keys; | ||
decltype(row_keys)::iterator row_keys_iter; | ||
|
||
HnswVectorFieldKnnScanExecutor(ExecutorContext *ctx, HnswVectorFieldKnnScan *scan) | ||
: ExecutorNode(ctx), | ||
scan(scan), | ||
ss(ctx->storage), | ||
index(scan->field->info->index), | ||
search_key(index->ns, index->name, scan->field->name), | ||
field_metadata(*(scan->field->info->MetadataAs<redis::HnswVectorFieldMetadata>())) {} | ||
|
||
StatusOr<Result> Next() override { | ||
if (!initialized) { | ||
// TODO(Beihao): Add DB context to improve consistency and isolation - see #2332 | ||
auto hnsw_index = redis::HnswIndex(search_key, &field_metadata, ctx->storage); | ||
row_keys = GET_OR_RET(hnsw_index.KnnSearch(scan->vector, scan->k)); | ||
row_keys_iter = row_keys.begin(); | ||
initialized = true; | ||
} | ||
|
||
if (row_keys_iter == row_keys.end()) { | ||
return end; | ||
} | ||
|
||
auto key_str = *row_keys_iter; | ||
row_keys_iter++; | ||
return RowType{key_str, {}, scan->field->info->index}; | ||
} | ||
}; | ||
|
||
} // namespace kqir |
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
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
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
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.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems here we can just keep a pointer e.g.
const redis::HnswVectorFieldMetadata *field_metadata
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this was because
HnswIndex
constructor does not supportconst HnswVectorFieldMetadata*
.HnswIndex
needs to modifyHnswVectorFieldMetadata*
in other functions, so I did copy here. Do you have ideas on how we can improve it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me think, if the metadata is modified in a certain vector query, it seems that the global metadata of the vector field (in
server->index_mgr->index_map
) will not change. Will this cause any problems?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HnswVectorFieldMetadata*
is only modified when the node is inserted/deleted in a higher layer than all other nodes, sonum_levels
inHnswVectorFieldMetadata : IndexFieldMetadata
is modified. So the affected field isserver->index_mgr->index_map[<index_key>]->fields[<field>]->metadata
.In
indexer.cc
, we doSo
*metadata
changes correspondingly.But in this PR, since we don't modify
metadata
, I guess it will not cause actual problems by copying (maybe with consistency issue but this would be fixed later aligning with #2310 , and the expensive copy because of the large size ofHnswIndex
but I can fix that right after the PR if the solution usingstatic
member variable sounds good to you <3).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean.
It's good for now, but we can enhance it later. For example, we could create a const version of HnswIndex that takes a const pointer to HnswVectorFieldMetadata.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good!
I wanted to overload with a const version, but there are some nested calls also asking for
HnswVectorFieldMetadata*
as a parameter, so eventually didn't implement it to avoid making this PR look too messy. I'll take a note in tracking issue #2426 and improve it in the future.