feat(query): add VectorViewClause zero-copy path and unify validate (#478)

* feat(query): add VectorViewClause zero-copy path and unify validate

- Add VectorViewClause (string_view-based) as zero-copy counterpart to
  VectorClause; variant now holds VectorClause | VectorViewClause | FtsClause
- Add QueryTarget::get_vector_view() unified accessor via std::visit,
  returns optional<VectorViewClause> regardless of which variant is held
- Split validate_and_sanitize into QueryTarget::validate (read-only) +
  sanitize_sparse_vector (mutate); validate handles both VectorClause and
  VectorViewClause via get_vector_view()
- Collection::Query passes original request directly to sqlengine when no
  sparse sanitization is needed; only copies when sort is required
- Change build_query_info/BuildSQLInfoFromSearchQuery to take const
  SearchQuery& so VectorMatrixNode string_views point to caller's data
This commit is contained in:
egolearner
2026-06-22 11:32:26 +08:00
committed by GitHub
parent 56560b812b
commit aad5718112
18 changed files with 447 additions and 175 deletions
+2 -2
View File
@@ -533,8 +533,8 @@ Status QueryAnalyzer::check_and_convert_vector(
}
*vector_cond = std::make_shared<QueryInfo::QueryVectorCondInfo>(
vector_meta, vector_data->take_matrix(), core_data_type, dimension,
vector_data->take_sparse_indices(), vector_data->take_sparse_values(),
vector_meta, vector_data->matrix(), core_data_type, dimension,
vector_data->sparse_indices(), vector_data->sparse_values(),
vector_data->take_query_params());
return Status::OK();
} else {
+13 -12
View File
@@ -15,6 +15,7 @@
#pragma once
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
@@ -47,17 +48,17 @@ class QueryInfo {
using Ptr = std::shared_ptr<QueryVectorCondInfo>;
QueryVectorCondInfo(const FieldSchema *vector_schema,
std::string vector_term,
std::string_view vector_term,
core::IndexMeta::DataType core_data_type, int dimension,
std::string vector_sparse_indices,
std::string vector_sparse_values,
std::string_view vector_sparse_indices,
std::string_view vector_sparse_values,
QueryParams::Ptr query_params)
: vector_schema_(vector_schema),
vector_term_(std::move(vector_term)),
vector_term_(vector_term),
data_type_(core_data_type),
dimension_(dimension),
vector_sparse_indices_(std::move(vector_sparse_indices)),
vector_sparse_values_(std::move(vector_sparse_values)),
vector_sparse_indices_(vector_sparse_indices),
vector_sparse_values_(vector_sparse_values),
query_params_(std::move(query_params)) {
auto *vector_params = dynamic_cast<VectorIndexParams *>(
vector_schema_->index_params().get());
@@ -75,7 +76,7 @@ class QueryInfo {
return vector_schema_;
}
const std::string &vector_term() const {
std::string_view vector_term() const {
return vector_term_;
}
@@ -99,11 +100,11 @@ class QueryInfo {
return vector_sparse_indices_.size() / sizeof(uint32_t);
}
const std::string &vector_sparse_indices() const {
std::string_view vector_sparse_indices() const {
return vector_sparse_indices_;
}
const std::string &vector_sparse_values() const {
std::string_view vector_sparse_values() const {
return vector_sparse_values_;
}
@@ -117,11 +118,11 @@ class QueryInfo {
private:
const FieldSchema *vector_schema_{nullptr};
std::string vector_term_{""};
std::string_view vector_term_;
core::IndexMeta::DataType data_type_;
uint32_t dimension_{0};
std::string vector_sparse_indices_{""};
std::string vector_sparse_values_{""};
std::string_view vector_sparse_indices_;
std::string_view vector_sparse_values_;
QueryParams::Ptr query_params_;
bool reverse_sort_{false};
};
+3 -3
View File
@@ -202,15 +202,15 @@ class QueryVectorMatrixNode : public QueryNode {
std::string text() const override;
const std::string &matrix() const {
std::string_view matrix() const {
return node_->matrix();
}
const std::string &sparse_indices() const {
std::string_view sparse_indices() const {
return node_->sparse_indices();
}
const std::string &sparse_values() const {
std::string_view sparse_values() const {
return node_->sparse_values();
}
+13 -23
View File
@@ -16,6 +16,7 @@
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <zvec/db/query_params.h>
#include "db/sqlengine/common/generic_node.h"
@@ -158,39 +159,28 @@ class VectorMatrixNode : public Node {
public:
using Ptr = std::shared_ptr<VectorMatrixNode>;
VectorMatrixNode(std::string matrix, std::string sparse_indices,
std::string sparse_values, QueryParams::Ptr query_params)
: matrix_(std::move(matrix)),
sparse_indices_(std::move(sparse_indices)),
sparse_values_(std::move(sparse_values)),
VectorMatrixNode(std::string_view matrix, std::string_view sparse_indices,
std::string_view sparse_values,
QueryParams::Ptr query_params)
: matrix_(matrix),
sparse_indices_(sparse_indices),
sparse_values_(sparse_values),
query_params_(std::move(query_params)) {
set_op(NodeOp::T_VECTOR_MATRIX_VALUE);
}
const std::string &matrix() const {
std::string_view matrix() const {
return matrix_;
}
std::string take_matrix() {
return std::move(matrix_);
}
const std::string &sparse_indices() const {
std::string_view sparse_indices() const {
return sparse_indices_;
}
std::string take_sparse_indices() {
return std::move(sparse_indices_);
}
const std::string &sparse_values() const {
std::string_view sparse_values() const {
return sparse_values_;
}
std::string take_sparse_values() {
return std::move(sparse_values_);
}
const QueryParams::Ptr &query_params() const {
return query_params_;
}
@@ -206,9 +196,9 @@ class VectorMatrixNode : public Node {
}
private:
std::string matrix_;
std::string sparse_indices_;
std::string sparse_values_;
std::string_view matrix_;
std::string_view sparse_indices_;
std::string_view sparse_values_;
QueryParams::Ptr query_params_;
};
+10 -11
View File
@@ -26,17 +26,16 @@ namespace zvec::sqlengine {
using namespace zvec;
Node::Ptr handle_vector(SearchQuery *request) {
auto *vc = request->target_.get_vector_clause();
if (vc == nullptr) {
Node::Ptr handle_vector(const SearchQuery &request) {
auto opt_view = request.target_.get_vector_view();
if (!opt_view) {
return nullptr;
}
Node::Ptr rel_exp = std::make_shared<Node>(NodeOp::T_EQ);
rel_exp->set_left(std::make_shared<IDNode>(request->target_.field_name_));
rel_exp->set_left(std::make_shared<IDNode>(request.target_.field_name_));
rel_exp->set_right(std::make_shared<VectorMatrixNode>(
std::move(vc->query_vector_), std::move(vc->sparse_indices_),
std::move(vc->sparse_values_),
std::move(request->target_.query_params_)));
opt_view->query_vector_, opt_view->sparse_indices_,
opt_view->sparse_values_, request.target_.query_params_));
return rel_exp;
}
@@ -67,13 +66,13 @@ void handle_query_field(const SearchQuery *query, SelectInfo *selected_info) {
}
Result<sqlengine::SQLInfo::Ptr> SQLInfoHelper::BuildSQLInfoFromSearchQuery(
SearchQuery query, Node::Ptr filter_node,
const SearchQuery &query, Node::Ptr filter_node,
std::shared_ptr<GroupBy> group_by) {
Node::Ptr index_params_node_ptr = nullptr;
if (const auto *vc = query.target_.get_vector_clause();
vc != nullptr &&
if (auto vc = query.target_.get_vector_view();
vc.has_value() &&
(!vc->query_vector_.empty() || !vc->sparse_indices_.empty())) {
index_params_node_ptr = handle_vector(&query);
index_params_node_ptr = handle_vector(query);
if (index_params_node_ptr == nullptr) {
return tl::make_unexpected(Status::InvalidArgument(
"Failed to build vector condition for field: ",
+1 -3
View File
@@ -24,10 +24,8 @@ namespace zvec::sqlengine {
class SQLInfoHelper {
public:
//! Build SQLInfo from SearchQuery. Takes query by value so callers may copy
//! or move it; vector payloads can be moved while building SQLInfo.
static Result<sqlengine::SQLInfo::Ptr> BuildSQLInfoFromSearchQuery(
SearchQuery query, Node::Ptr filter_node,
const SearchQuery &query, Node::Ptr filter_node,
std::shared_ptr<GroupBy> group_by);
};
+2 -2
View File
@@ -280,7 +280,7 @@ Result<QueryInfo::Ptr> SQLEngineImpl::parse_sql_info(
}
Result<QueryInfo::Ptr> SQLEngineImpl::build_query_info(
CollectionSchema::Ptr collection, SearchQuery request,
CollectionSchema::Ptr collection, const SearchQuery &request,
std::shared_ptr<GroupBy> group_by) {
ScopedProfilerStage stage_guard(profiler_, "build_sql_info");
Node::Ptr filter_node;
@@ -317,7 +317,7 @@ Result<QueryInfo::Ptr> SQLEngineImpl::build_query_info(
}
auto sql_info = sqlengine::SQLInfoHelper::BuildSQLInfoFromSearchQuery(
std::move(request), std::move(filter_node), std::move(group_by));
request, std::move(filter_node), std::move(group_by));
if (!sql_info) {
return tl::make_unexpected(sql_info.error());
}
+1 -1
View File
@@ -36,7 +36,7 @@ class SQLEngineImpl : public SQLEngine {
//! Build analyzed query info from a structured search query.
Result<QueryInfo::Ptr> build_query_info(CollectionSchema::Ptr collection,
SearchQuery request,
const SearchQuery &request,
std::shared_ptr<GroupBy> group_by);
//! Perform search with given query_info, segments and index filter