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

Early leave findshortestpath loop optimization #4672

Merged
merged 7 commits into from
Sep 28, 2022
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
6 changes: 5 additions & 1 deletion src/graph/executor/algo/BFSShortestPathExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace graph {
folly::Future<Status> BFSShortestPathExecutor::execute() {
SCOPED_TIMER(&execTime_);
pathNode_ = asNode<BFSShortestPath>(node());
terminateEarlyVar_ = pathNode_->terminateEarlyVar();

if (step_ == 1) {
allRightEdges_.emplace_back();
Expand Down Expand Up @@ -99,7 +100,10 @@ Status BFSShortestPathExecutor::buildPath(bool reverse) {
// set nextVid
const auto& nextVidVar = reverse ? pathNode_->rightVidVar() : pathNode_->leftVidVar();
ectx_->setResult(nextVidVar, ResultBuilder().value(std::move(nextStepVids)).build());

if (uniqueDst.size() == 0) {
ectx_->setValue(terminateEarlyVar_, true);
return Status::OK();
}
visitedVids.insert(std::make_move_iterator(uniqueDst.begin()),
std::make_move_iterator(uniqueDst.end()));
return Status::OK();
Expand Down
1 change: 1 addition & 0 deletions src/graph/executor/algo/BFSShortestPathExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class BFSShortestPathExecutor final : public Executor {
std::vector<std::unordered_multimap<Value, Edge>> allLeftEdges_;
std::vector<std::unordered_multimap<Value, Edge>> allRightEdges_;
DataSet currentDs_;
std::string terminateEarlyVar_;
};
} // namespace graph
} // namespace nebula
Expand Down
13 changes: 10 additions & 3 deletions src/graph/planner/ngql/PathPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ PlanNode* PathPlanner::getNeighbors(PlanNode* dep, bool reverse) {
SubPlan PathPlanner::singlePairPlan(PlanNode* left, PlanNode* right) {
auto qctx = pathCtx_->qctx;
auto steps = pathCtx_->steps.steps();
auto terminateEarlyVar = qctx->vctx()->anonVarGen()->getVar();
qctx->ectx()->setValue(terminateEarlyVar, false);
auto* path = BFSShortestPath::make(qctx, left, right, steps);
path->setLeftVidVar(pathCtx_->fromVidsVar);
path->setRightVidVar(pathCtx_->toVidsVar);
path->setColNames({kPathStr});
path->setTerminateEarlyVar(terminateEarlyVar);

auto* loopCondition = singlePairLoopCondition(steps, path->outputVar());
auto* loopCondition = singlePairLoopCondition(steps, path->outputVar(), terminateEarlyVar);
auto* loop = Loop::make(qctx, nullptr, path, loopCondition);

auto* dc = DataCollect::make(qctx, DataCollect::DCKind::kBFSShortest);
Expand Down Expand Up @@ -245,16 +248,20 @@ PlanNode* PathPlanner::buildPathProp(PlanNode* dep) {

// loopSteps{0} <= ((steps + 1) / 2) && (pathVar is Empty || size(pathVar) ==
// 0)
Expression* PathPlanner::singlePairLoopCondition(uint32_t steps, const std::string& pathVar) {
Expression* PathPlanner::singlePairLoopCondition(uint32_t steps,
const std::string& pathVar,
const std::string& terminateEarlyVar) {
auto loopSteps = pathCtx_->qctx->vctx()->anonVarGen()->getVar();
pathCtx_->qctx->ectx()->setValue(loopSteps, 0);
auto* pool = pathCtx_->qctx->objPool();

auto step = ExpressionUtils::stepCondition(pool, loopSteps, ((steps + 1) / 2));
auto empty = ExpressionUtils::equalCondition(pool, pathVar, Value::kEmpty);
auto zero = ExpressionUtils::zeroCondition(pool, pathVar);
auto loopTerminateEarly = ExpressionUtils::equalCondition(pool, terminateEarlyVar, false);
auto* noFound = LogicalExpression::makeOr(pool, empty, zero);
return LogicalExpression::makeAnd(pool, step, noFound);
auto* earlyStop = LogicalExpression::makeAnd(pool, step, loopTerminateEarly);
return LogicalExpression::makeAnd(pool, earlyStop, noFound);
}

// loopSteps{0} <= (steps + 1) / 2
Expand Down
4 changes: 3 additions & 1 deletion src/graph/planner/ngql/PathPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class PathPlanner final : public Planner {

void buildStart(Starts& starts, std::string& startVidsVar, bool reverse);

Expression* singlePairLoopCondition(uint32_t steps, const std::string& pathVar);
Expression* singlePairLoopCondition(uint32_t steps,
const std::string& pathVar,
const std::string& terminateEarlyVar);

Expression* multiPairLoopCondition(uint32_t steps, const std::string& pathVar);

Expand Down
9 changes: 9 additions & 0 deletions src/graph/planner/plan/Algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class BFSShortestPath : public BinaryInputNode {
return rightVidVar_;
}

std::string terminateEarlyVar() const {
return terminateEarlyVar_;
}

void setLeftVidVar(const std::string& var) {
leftVidVar_ = var;
}
Expand All @@ -88,6 +92,10 @@ class BFSShortestPath : public BinaryInputNode {
rightVidVar_ = var;
}

void setTerminateEarlyVar(const std::string& var) {
terminateEarlyVar_ = var;
}

std::unique_ptr<PlanNodeDescription> explain() const override;

private:
Expand All @@ -98,6 +106,7 @@ class BFSShortestPath : public BinaryInputNode {
private:
std::string leftVidVar_;
std::string rightVidVar_;
std::string terminateEarlyVar_;
size_t steps_{0};
};

Expand Down