Skip to content
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

fix(interactive): Fix support for PathExpand for multiple triplets. #3335

Merged
merged 14 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/hqps-db-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ jobs:
eval ${cmd}
done

for i in 1 2 3 4 5 6 7 8 9 10;
for i in 1 2 3 4 5 6 7 8 9 10 11 12;
do
cmd="./load_plan_and_gen.sh -e=hqps -i=../resources/queries/ic/adhoc/simple_match_${i}.cypher -w=/tmp/codgen/"
cmd=${cmd}" -o=/tmp/plugin --ir_conf=${GS_TEST_DIR}/flex/ldbc-sf01-long-date/engine_config.yaml "
Expand Down
58 changes: 51 additions & 7 deletions flex/codegen/src/hqps/hqps_edge_expand_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ static constexpr const char* EDGE_EXPAND_V_OPT_FILTER_TEMPLATE_STR =
static constexpr const char* EDGE_EXPAND_V_OPT_NO_FILTER_TEMPLATE_STR =
"auto %1% = gs::make_edge_expandv_opt(%2%, %3%, %4%);\n";

// This opt can be used by both edge expande and edge expandv.
// This opt can only be used by both edge expande, with multiple edge triplet.
static constexpr const char*
EDGE_EXPAND_E_OPT_MULTI_EDGE_NO_FILTER_TEMPLATE_STR =
"auto %1% = gs::make_edge_expand_multie_opt<%2%>(%3%, %4%, %5%);\n";

// This opt can only be used by both edge expandv, with multiplet edge triplet,
static constexpr const char*
EDGE_EXPAND_V_OPT_MULTI_EDGE_NO_FILTER_TEMPLATE_STR =
"auto %1% = gs::make_edge_expand_multiv_opt(%2%, %3%);\n";

// Expand to Edges with Filter.
// propNames, direction, edge_label, vertex_label, filter
static constexpr const char* EDGE_EXPAND_E_OPT_FILTER_TEMPLATE_STR =
Expand Down Expand Up @@ -122,6 +127,30 @@ std::string edge_label_triplet_to_array_str(
return ss.str();
}

std::string edge_label_triplet_to_vector_str(
const std::vector<std::vector<int32_t>>& edge_label_triplet) {
std::stringstream ss;
ss << "std::vector<std::array<label_id_t, 3>>{";
for (int i = 0; i < edge_label_triplet.size(); ++i) {
ss << "std::array<label_id_t, 3>{";
if (edge_label_triplet[i].size() != 3) {
throw std::runtime_error("edge label triplet size must be 3");
}
for (int j = 0; j < edge_label_triplet[i].size(); ++j) {
ss << edge_label_triplet[i][j];
if (j != edge_label_triplet[i].size() - 1) {
ss << ", ";
}
}
ss << "}";
if (i != edge_label_triplet.size() - 1) {
ss << ", ";
}
}
ss << "}";
return ss.str();
}

std::string make_prop_tuple_array(const std::vector<std::string>& prop_names,
const std::vector<std::string>& prop_types) {
std::stringstream ss;
Expand Down Expand Up @@ -370,16 +399,31 @@ static std::pair<std::string, std::string> BuildMultiLabelEdgeExpandOpt(
LOG(INFO) << "Find multiple edge triplet: " << edge_label_triplet.size();

auto func_template_str = make_edge_expand_e_func_template_str(prop_types);
auto edge_triplet_2d_array =
edge_label_triplet_to_array_str(edge_label_triplet);
auto edge_named_prop_array =
make_prop_tuple_array_tuple(prop_names, prop_types);

boost::format formater(EDGE_EXPAND_E_OPT_MULTI_EDGE_NO_FILTER_TEMPLATE_STR);
boost::format formater;
if (expand_opt ==
physical::EdgeExpand::ExpandOpt::EdgeExpand_ExpandOpt_EDGE) {
auto edge_triplet_2d_array =
edge_label_triplet_to_array_str(edge_label_triplet);
formater =
boost::format(EDGE_EXPAND_E_OPT_MULTI_EDGE_NO_FILTER_TEMPLATE_STR);
formater % opt_var_name % func_template_str %
gs::direction_pb_to_str(direction) % edge_triplet_2d_array %
edge_named_prop_array;
} else if (expand_opt ==
physical::EdgeExpand::ExpandOpt::EdgeExpand_ExpandOpt_VERTEX) {
auto edge_triplet_2d_vector =
edge_label_triplet_to_vector_str(edge_label_triplet);
formater =
boost::format(EDGE_EXPAND_V_OPT_MULTI_EDGE_NO_FILTER_TEMPLATE_STR);
formater % opt_var_name % gs::direction_pb_to_str(direction) %
edge_triplet_2d_vector;
} else {
throw std::runtime_error("Unknown expand opt");
}

formater % opt_var_name % func_template_str %
gs::direction_pb_to_str(direction) % edge_triplet_2d_array %
edge_named_prop_array;
return std::make_pair(opt_var_name, formater.str());
}

Expand Down
67 changes: 51 additions & 16 deletions flex/codegen/src/hqps/hqps_path_expand_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,93 +94,128 @@
PathExpandOpBuilder& edge_expand_opt() { return *this; }

// get the getv name and the getv code.
PathExpandOpBuilder& path_expand_opt(
const physical::EdgeExpand& edge_expand_pb,
const physical::GetV& get_v_pb,
const google::protobuf::RepeatedPtrField<physical::PhysicalOpr::MetaData>&
meta_data_pb) {
direction_ =
edge_expand_pb_2_internal_direction(edge_expand_pb.direction());

if (meta_data_pb.size() >= 1) {
CHECK(meta_data_pb.size() == 1) << "currently only support one meta_data";
auto& graph_type = meta_data_pb[0].type();
if (graph_type.type_case() == common::IrDataType::kDataType) {
// throw runtime error
throw std::runtime_error("Expect graphDataType in ir_data_type");
} else if (graph_type.type_case() == common::IrDataType::kGraphType) {
VLOG(10) << "Parse edge triplet from meta_data";
auto& act_graph_type = graph_type.graph_type();
if (act_graph_type.element_opt() !=
common::GraphDataType::GraphElementOpt::
GraphDataType_GraphElementOpt_EDGE) {
throw std::runtime_error("Expect edge graph type");
}
auto& edge_type = act_graph_type.graph_data_type();
if (edge_type.size() != 1) {
throw std::runtime_error("Expect only one edge type");
if (edge_type.size() == 0) {
throw std::runtime_error("Expect edge type size > 0");
}
std::vector<int32_t> src_labels, dst_labels;
for (auto i = 0; i < edge_type.size(); ++i) {
auto& edge_type_i = edge_type[i];
auto& edge_labels_i = edge_type_i.label();
src_labels.push_back(edge_labels_i.src_label().value());
dst_labels.push_back(edge_labels_i.dst_label().value());
}
auto& edge_type0 = edge_type[0];
auto& edge_labels = edge_type0.label();
auto src_label = edge_labels.src_label().value();
auto dst_label = edge_labels.dst_label().value();

// if find edge triplets, we clear current
VLOG(10) << "Clear current dst labels:"
<< gs::to_string(dst_vertex_labels_);
dst_vertex_labels_.clear();

if (direction_ == internal::Direction::kBoth) {
CHECK(src_label == dst_label);
dst_vertex_labels_.emplace_back(src_label);
// if direction is both, we need to check src_label == dst_label
// dedup src_labels
std::sort(src_labels.begin(), src_labels.end());
src_labels.erase(std::unique(src_labels.begin(), src_labels.end()),
src_labels.end());
// dedup dst_labels
std::sort(dst_labels.begin(), dst_labels.end());
dst_labels.erase(std::unique(dst_labels.begin(), dst_labels.end()),
dst_labels.end());
for (auto i = 0; i < src_labels.size(); ++i) {
if (src_labels[i] != dst_labels[i]) {
throw std::runtime_error(
"Expect src_label == dst_label for both direction");
}
dst_vertex_labels_.emplace_back(dst_labels[i]);
}
} else if (direction_ == internal::Direction::kOut) {
dst_vertex_labels_.emplace_back(dst_label);
for (auto i = 0; i < dst_labels.size(); ++i) {
dst_vertex_labels_.emplace_back(dst_labels[i]);
}
} else if (direction_ == internal::Direction::kIn) {
dst_vertex_labels_.emplace_back(src_label);
for (auto i = 0; i < src_labels.size(); ++i) {
dst_vertex_labels_.emplace_back(src_labels[i]);
}
} else {
throw std::runtime_error("Unknown direction");
}
} else {
throw std::runtime_error("Expect graphDataType in ir_data_type");
}
} else {
VLOG(10) << "No meta_data found";
}

{
// build get_v
auto v_opt = vopt_pb_to_internal(get_v_pb.opt());
auto& v_labels_pb = get_v_pb.params().tables();

if (dst_vertex_labels_.empty()) {
for (auto i = 0; i < v_labels_pb.size(); ++i) {
dst_vertex_labels_.push_back(
try_get_label_from_name_or_id<LabelT>(v_labels_pb[i]));
}
}
VLOG(10) << "get vertex labels:" << gs::to_string(dst_vertex_labels_);
CHECK(!get_v_pb.params().has_predicate()) << "currently don't suppport "
"getv with condition";
// std::tie(get_v_expr_call_code, get_v_opt, getv_opt_) =
// BuildGetVOpt(ctx_, get_v_pb);
std::tie(getv_opt_name_, getv_opt_code_) =
make_getv_opt_call_code(ctx_, v_opt, dst_vertex_labels_);
VLOG(10) << "Got getv_opt_name_: " << getv_opt_name_;
VLOG(10) << "Got getv_opt_code_: " << getv_opt_code_;
}

{
// build edge_expand_opt
auto& params = edge_expand_pb.params();
auto expand_opt = edge_expand_pb.expand_opt();
CHECK(dst_vertex_labels_.size() > 0) << "no dst lables found";

physical::PhysicalOpr::MetaData meta_data;
// pass an empty meta_data, since we need no meta_data for
// edge_expand_opt.
std::tie(edge_expand_opt_name_, edge_expand_opt_) =
BuildOneLabelEdgeExpandOpt(ctx_, direction_, params,
dst_vertex_labels_, expand_opt, meta_data);
if (params.tables().size() < 1) {
throw std::runtime_error("no edge labels found");
} else if (params.tables().size() == 1) {
physical::PhysicalOpr::MetaData meta_data;
// pass an empty meta_data, since we need no meta_data for
std::tie(edge_expand_opt_name_, edge_expand_opt_) =
BuildOneLabelEdgeExpandOpt(ctx_, direction_, params,
dst_vertex_labels_, expand_opt,
meta_data);

Check notice on line 207 in flex/codegen/src/hqps/hqps_path_expand_builder.h

View check run for this annotation

codefactor.io / CodeFactor

flex/codegen/src/hqps/hqps_path_expand_builder.h#L97-L207

Complex Method
} else {
// get the first meta_data
if (meta_data_pb.size() < 1) {
throw std::runtime_error("no meta_data found");
}
auto& meta_data = meta_data_pb[0];
std::tie(edge_expand_opt_name_, edge_expand_opt_) =
BuildMultiLabelEdgeExpandOpt(ctx_, direction_, params, expand_opt,
meta_data);
}

VLOG(10) << "edge_expand_opt_name_: " << edge_expand_opt_name_;
VLOG(10) << "edge_expand_opt_: " << edge_expand_opt_;
}
Expand Down
25 changes: 13 additions & 12 deletions flex/engines/hqps_db/core/operator/edge_expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ class EdgeExpand {
/// @param v_sets
/// @param edge_expand_opt
/// @return
template <typename... SELECTOR,
template <typename... SELECTOR, typename... SET_T,
typename RES_T = std::pair<vertex_set_t, std::vector<offset_t>>>
static RES_T EdgeExpandV(
const GRAPH_INTERFACE& graph,
const GeneralVertexSet<vertex_id_t, label_id_t>& cur_vertex_set,
const GeneralVertexSet<vertex_id_t, label_id_t, SET_T...>& cur_vertex_set,
Direction direction, label_id_t edge_label, label_id_t other_label,
Filter<TruePredicate, SELECTOR...>&& edge_filter,
size_t limit = INT_MAX) {
Expand Down Expand Up @@ -440,11 +440,12 @@ class EdgeExpand {
/// @param v_sets
/// @param edge_expand_opt
/// @return
template <
typename VERTEX_SET_T, size_t num_labels, typename EDGE_FILTER_T,
size_t... Is, typename std::enable_if<(num_labels != 2)>::type* = nullptr,
typename RES_T = std::pair<GeneralVertexSet<vertex_id_t, label_id_t>,
std::vector<offset_t>>>
template <typename VERTEX_SET_T, size_t num_labels, typename EDGE_FILTER_T,
typename... SET_T, size_t... Is,
typename std::enable_if<(num_labels != 2)>::type* = nullptr,
typename RES_T =
std::pair<GeneralVertexSet<vertex_id_t, label_id_t, SET_T...>,
std::vector<offset_t>>>
static RES_T EdgeExpandV(const GRAPH_INTERFACE& graph,
const VERTEX_SET_T& cur_vertex_set,
Direction direction, label_id_t edge_label,
Expand Down Expand Up @@ -499,7 +500,7 @@ class EdgeExpand {
}
CHECK(cur_ind == total_size);
auto copied_labels(other_labels);
GeneralVertexSet<vertex_id_t, label_id_t> res_set(
GeneralVertexSet<vertex_id_t, label_id_t, SET_T...> res_set(
std::move(res_vids), std::move(copied_labels), std::move(res_bitset));

return std::make_pair(std::move(res_set), std::move(res_offset));
Expand Down Expand Up @@ -602,10 +603,10 @@ class EdgeExpand {
/// @param edge_filter
/// @param limit
/// @return /
template <size_t num_pairs, typename... PropTuple>
template <size_t num_pairs, typename... PropTuple, typename... SET_T>
static auto EdgeExpandVMultiTriplet(
const GRAPH_INTERFACE& graph,
const GeneralVertexSet<vertex_id_t, label_id_t>& cur_vertex_set,
const GeneralVertexSet<vertex_id_t, label_id_t, SET_T...>& cur_vertex_set,
Direction& direction,
std::array<std::array<label_id_t, 3>, num_pairs>& edge_labels,
std::tuple<PropTupleArrayT<PropTuple>...>& prop_names,
Expand Down Expand Up @@ -833,10 +834,10 @@ class EdgeExpand {
/// @param edge_filter
/// @param limit
/// @return /
template <size_t num_pairs, typename... PropTuple>
template <size_t num_pairs, typename... PropTuple, typename... SET_T>
static auto EdgeExpandEMultiTriplet(
const GRAPH_INTERFACE& graph,
const GeneralVertexSet<vertex_id_t, label_id_t>& cur_vertex_set,
const GeneralVertexSet<vertex_id_t, label_id_t, SET_T...>& cur_vertex_set,
Direction& direction,
std::array<std::array<label_id_t, 3>, num_pairs>& edge_labels,
std::tuple<PropTupleArrayT<PropTuple>...>& prop_names,
Expand Down
Loading
Loading