mirror of https://github.com/milvus-io/milvus.git
Add ShowPlanNodeVisitor under visitor pattern
Signed-off-by: FluorineDog <guilin.gou@zilliz.com>pull/4973/head^2
parent
9c490a209b
commit
3ff3a5b659
|
@ -3,7 +3,10 @@ set(MILVUS_QUERY_SRCS
|
|||
deprecated/BinaryQuery.cpp
|
||||
generated/PlanNode.cpp
|
||||
generated/Expr.cpp
|
||||
visitors/ShowPlanNodeVisitor.cpp
|
||||
visitors/ExecPlanNodeVisitor.cpp
|
||||
Parser.cpp
|
||||
)
|
||||
add_library(milvus_query ${MILVUS_QUERY_SRCS})
|
||||
target_link_libraries(milvus_query libprotobuf)
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ struct PlanNode {
|
|||
using PlanNodePtr = std::unique_ptr<PlanNode>;
|
||||
|
||||
struct VectorPlanNode : PlanNode {
|
||||
std::optional<PlanNodePtr> child_;
|
||||
std::optional<ExprPtr> predicate_;
|
||||
int64_t num_queries_;
|
||||
int64_t dim_;
|
||||
FieldId field_id_;
|
||||
|
@ -38,7 +38,7 @@ struct VectorPlanNode : PlanNode {
|
|||
};
|
||||
|
||||
struct FloatVectorANNS : VectorPlanNode {
|
||||
std::shared_ptr<float> data;
|
||||
std::vector<float> data_;
|
||||
std::string metric_type_; // TODO: use enum
|
||||
public:
|
||||
void
|
||||
|
@ -46,7 +46,7 @@ struct FloatVectorANNS : VectorPlanNode {
|
|||
};
|
||||
|
||||
struct BinaryVectorANNS : VectorPlanNode {
|
||||
std::shared_ptr<uint8_t> data;
|
||||
std::vector<uint8_t> data_;
|
||||
std::string metric_type_; // TODO: use enum
|
||||
public:
|
||||
void
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
#error TODO: copy this file out, and modify the content.
|
||||
#include "query/generated/ExecPlanNodeVisitor.h"
|
||||
|
||||
namespace milvus::query {
|
||||
void
|
||||
ExecPlanNodeVisitor::visit(FloatVectorANNS& node) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void
|
||||
ExecPlanNodeVisitor::visit(BinaryVectorANNS& node) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
} // namespace milvus::query
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
// Generated File
|
||||
// DO NOT EDIT
|
||||
#include "PlanNodeVisitor.h"
|
||||
namespace milvus::query {
|
||||
class ExecPlanNodeVisitor : PlanNodeVisitor {
|
||||
public:
|
||||
virtual void
|
||||
visit(FloatVectorANNS& node) override;
|
||||
|
||||
virtual void
|
||||
visit(BinaryVectorANNS& node) override;
|
||||
|
||||
public:
|
||||
};
|
||||
} // namespace milvus::query
|
|
@ -5,7 +5,7 @@
|
|||
namespace milvus::query {
|
||||
class ExprVisitor {
|
||||
public:
|
||||
virtual ~ExprVisitor() = 0;
|
||||
virtual ~ExprVisitor() = default;
|
||||
|
||||
public:
|
||||
virtual void
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
namespace milvus::query {
|
||||
class PlanNodeVisitor {
|
||||
public:
|
||||
virtual ~PlanNodeVisitor() = 0;
|
||||
virtual ~PlanNodeVisitor() = default;
|
||||
|
||||
public:
|
||||
virtual void
|
||||
|
|
|
@ -12,5 +12,19 @@ class ShowPlanNodeVisitor : PlanNodeVisitor {
|
|||
visit(BinaryVectorANNS& node) override;
|
||||
|
||||
public:
|
||||
using RetType = nlohmann::json;
|
||||
|
||||
public:
|
||||
RetType
|
||||
call_child(PlanNode& node) {
|
||||
assert(!ret_.has_value());
|
||||
node.accept(*this);
|
||||
assert(ret_.has_value());
|
||||
auto ret = std::move(ret_);
|
||||
return std::move(ret.value());
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<RetType> ret_;
|
||||
};
|
||||
} // namespace milvus::query
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
#include "query/generated/ExecPlanNodeVisitor.h"
|
||||
|
||||
namespace milvus::query {
|
||||
void
|
||||
ExecPlanNodeVisitor::visit(FloatVectorANNS& node) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void
|
||||
ExecPlanNodeVisitor::visit(BinaryVectorANNS& node) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
} // namespace milvus::query
|
|
@ -0,0 +1,62 @@
|
|||
#include "utils/EasyAssert.h"
|
||||
#include "utils/Json.h"
|
||||
#include <optional>
|
||||
|
||||
#include "query/generated/ShowPlanNodeVisitor.h"
|
||||
|
||||
namespace milvus::query {
|
||||
#if 0
|
||||
// THIS CONTAINS EXTRA BODY FOR VISITOR
|
||||
// WILL BE USED BY GENERATOR
|
||||
class ShowPlanNodeVisitorImpl : PlanNodeVisitor {
|
||||
public:
|
||||
using RetType = nlohmann::json;
|
||||
|
||||
public:
|
||||
RetType
|
||||
call_child(PlanNode& node) {
|
||||
assert(!ret_.has_value());
|
||||
node.accept(*this);
|
||||
assert(ret_.has_value());
|
||||
auto ret = std::move(ret_);
|
||||
return std::move(ret.value());
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<RetType> ret_;
|
||||
};
|
||||
#endif
|
||||
|
||||
using Json = nlohmann::json;
|
||||
|
||||
static std::string
|
||||
get_indent(int indent) {
|
||||
return std::string(10, '\t');
|
||||
}
|
||||
|
||||
void
|
||||
ShowPlanNodeVisitor::visit(FloatVectorANNS& node) {
|
||||
// std::vector<float> data(node.data_.get(), node.data_.get() + node.num_queries_ * node.dim_);
|
||||
assert(!ret_);
|
||||
Json json_body{
|
||||
{"node_type", "FloatVectorANNS"}, //
|
||||
{"metric_type", node.metric_type_}, //
|
||||
{"dim", node.dim_}, //
|
||||
{"field_id_", node.field_id_}, //
|
||||
{"num_queries", node.num_queries_}, //
|
||||
{"data", node.data_}, //
|
||||
};
|
||||
if (node.predicate_.has_value()) {
|
||||
AssertInfo(false, "unimplemented");
|
||||
} else {
|
||||
json_body["predicate"] = "nullopt";
|
||||
}
|
||||
ret_ = json_body;
|
||||
}
|
||||
|
||||
void
|
||||
ShowPlanNodeVisitor::visit(BinaryVectorANNS& node) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
} // namespace milvus::query
|
|
@ -9,7 +9,6 @@ set(SEGCORE_FILES
|
|||
collection_c.cpp
|
||||
partition_c.cpp
|
||||
segment_c.cpp
|
||||
EasyAssert.cpp
|
||||
SegmentBase.cpp
|
||||
IndexingEntry.cpp
|
||||
InsertRecord.cpp
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <vector>
|
||||
#include "EasyAssert.h"
|
||||
#include "utils/EasyAssert.h"
|
||||
namespace milvus::segcore {
|
||||
|
||||
// we don't use std::array because capacity of concurrent_vector wastes too much memory
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "utils/Types.h"
|
||||
// #include "knowhere/index/Index.h"
|
||||
#include "utils/Status.h"
|
||||
#include "EasyAssert.h"
|
||||
#include "utils/EasyAssert.h"
|
||||
|
||||
namespace milvus::segcore {
|
||||
using Timestamp = uint64_t; // TODO: use TiKV-like timestamp
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#include "query/deprecated/GeneralQuery.h"
|
||||
#include "utils/Status.h"
|
||||
#include "segcore/DeletedRecord.h"
|
||||
#include "EasyAssert.h"
|
||||
#include "utils/EasyAssert.h"
|
||||
#include "InsertRecord.h"
|
||||
|
||||
namespace milvus::segcore {
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#include "query/deprecated/GeneralQuery.h"
|
||||
#include "utils/Status.h"
|
||||
#include "segcore/DeletedRecord.h"
|
||||
#include "EasyAssert.h"
|
||||
#include "utils/EasyAssert.h"
|
||||
#include "IndexingEntry.h"
|
||||
#include "InsertRecord.h"
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@
|
|||
|
||||
# aux_source_directory( ${MILVUS_ENGINE_SRC}/utils UTILS_FILES )
|
||||
set(UTILS_FILES
|
||||
Status.cpp
|
||||
Status.cpp
|
||||
EasyAssert.cpp
|
||||
)
|
||||
|
||||
add_library( utils STATIC ${UTILS_FILES} )
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "query/PlanNode.h"
|
||||
#include "query/generated/ExprVisitor.h"
|
||||
#include "query/generated/PlanNodeVisitor.h"
|
||||
#include "test_utils/DataGen.h"
|
||||
#include "query/generated/ShowPlanNodeVisitor.h"
|
||||
|
||||
TEST(Query, Naive) {
|
||||
SUCCEED();
|
||||
|
@ -44,4 +46,24 @@ TEST(Query, Naive) {
|
|||
]
|
||||
}
|
||||
})";
|
||||
}
|
||||
|
||||
TEST(Query, ShowExecutor) {
|
||||
using namespace milvus::query;
|
||||
using namespace milvus::segcore;
|
||||
auto node = std::make_unique<FloatVectorANNS>();
|
||||
auto schema = std::make_shared<Schema>();
|
||||
int64_t num_queries = 100L;
|
||||
schema->AddField("fakevec", DataType::VECTOR_FLOAT, 16);
|
||||
auto raw_data = DataGen(schema, num_queries);
|
||||
node->data_ = raw_data.get_col<float>(0);
|
||||
node->metric_type_ = "L2";
|
||||
node->num_queries_ = 10;
|
||||
node->dim_ = 16;
|
||||
node->predicate_ = std::nullopt;
|
||||
ShowPlanNodeVisitor show_visitor;
|
||||
PlanNodePtr base(node.release());
|
||||
auto res = show_visitor.call_child(*base);
|
||||
res["data"] = "...collased...";
|
||||
std::cout << res.dump(4);
|
||||
}
|
|
@ -10,9 +10,17 @@ struct GeneratedData {
|
|||
std::vector<std::vector<char>> cols_;
|
||||
void
|
||||
generate_rows(int N, SchemaPtr schema);
|
||||
template <typename T>
|
||||
auto
|
||||
get_col(int index) {
|
||||
auto& target = cols_.at(index);
|
||||
std::vector<T> ret(target.size() / sizeof(T));
|
||||
memcpy(ret.data(), target.data(), target.size());
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
inline void
|
||||
GeneratedData::generate_rows(int N, SchemaPtr schema) {
|
||||
std::vector<int> offset_infos(schema->size() + 1, 0);
|
||||
auto sizeof_infos = schema->get_sizeof_infos();
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
# from gen_node import *
|
||||
from assemble import *
|
||||
from meta_gen import *
|
||||
import re
|
||||
import os
|
||||
|
||||
def gen_file(rootfile, template, output, **kwargs):
|
||||
namespace, root_base, struct_name = meta_gen(readfile(rootfile))
|
||||
|
@ -10,6 +12,22 @@ def gen_file(rootfile, template, output, **kwargs):
|
|||
file = open(output, 'w')
|
||||
file.write(vc)
|
||||
|
||||
|
||||
def extract_extra_body(visitor_info, query_path):
|
||||
pattern = re.compile("class(.*){\n((.|\n)*?)\n};", re.MULTILINE)
|
||||
|
||||
for node, visitors in visitor_info.items():
|
||||
for visitor in visitors:
|
||||
vis_name = visitor['visitor_name']
|
||||
vis_file = query_path + "visitors/" + vis_name + ".cpp"
|
||||
body = ' public:'
|
||||
|
||||
if os.path.exists(vis_file):
|
||||
infos = pattern.findall(readfile(vis_file))
|
||||
if len(infos) == 1:
|
||||
name, body, _ = infos[0]
|
||||
visitor["ctor_and_member"] = body
|
||||
|
||||
if __name__ == "__main__":
|
||||
query_path = "../../internal/core/src/query/"
|
||||
output_path = query_path + "generated/"
|
||||
|
@ -19,15 +37,21 @@ if __name__ == "__main__":
|
|||
visitor_info = {
|
||||
'Expr': [{
|
||||
'visitor_name': "ShowExprVisitor",
|
||||
"ctor_and_member": ' public:',
|
||||
"parameter_name": 'expr',
|
||||
}],
|
||||
'PlanNode': [{
|
||||
'visitor_name': "ShowPlanNodeVisitor",
|
||||
"ctor_and_member": ' public:',
|
||||
"parameter_name": 'node',
|
||||
}]
|
||||
'PlanNode': [
|
||||
{
|
||||
'visitor_name': "ShowPlanNodeVisitor",
|
||||
"parameter_name": 'node',
|
||||
},
|
||||
{
|
||||
'visitor_name': "ExecPlanNodeVisitor",
|
||||
"parameter_name": 'node',
|
||||
},
|
||||
|
||||
]
|
||||
}
|
||||
extract_extra_body(visitor_info, query_path)
|
||||
|
||||
for name in node_names:
|
||||
rootfile = query_path + name + ".h"
|
||||
|
@ -51,4 +75,3 @@ if __name__ == "__main__":
|
|||
output = output_path + vis_name + '.cpp'
|
||||
gen_file(rootfile, template, output, **info)
|
||||
print("Done")
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
namespace @@namespace@@ {
|
||||
class @@root_base@@Visitor {
|
||||
public:
|
||||
virtual ~@@root_base@@Visitor() = 0;
|
||||
virtual ~@@root_base@@Visitor() = default;
|
||||
|
||||
public:
|
||||
@@body@@
|
||||
|
|
Loading…
Reference in New Issue