From d1716f658312fc61aed0e4a8d343499a337f854a Mon Sep 17 00:00:00 2001 From: jimingquan Date: Wed, 26 Jul 2023 19:13:47 +0800 Subject: [PATCH] delete push limit to shortestpath rule --- .../executor/algo/BFSShortestPathExecutor.cpp | 14 +-- .../executor/algo/BFSShortestPathExecutor.h | 2 +- .../algo/MultiShortestPathExecutor.cpp | 8 +- .../rule/PushLimitDownShortestPathRule.cpp | 102 ------------------ .../rule/PushLimitDownShortestPathRule.h | 72 ------------- 5 files changed, 13 insertions(+), 185 deletions(-) delete mode 100644 src/graph/optimizer/rule/PushLimitDownShortestPathRule.cpp delete mode 100644 src/graph/optimizer/rule/PushLimitDownShortestPathRule.h diff --git a/src/graph/executor/algo/BFSShortestPathExecutor.cpp b/src/graph/executor/algo/BFSShortestPathExecutor.cpp index 10d66fad128..532bedfd91a 100644 --- a/src/graph/executor/algo/BFSShortestPathExecutor.cpp +++ b/src/graph/executor/algo/BFSShortestPathExecutor.cpp @@ -13,6 +13,9 @@ folly::Future BFSShortestPathExecutor::execute() { // MemoryTrackerVerified SCOPED_TIMER(&execTime_); pathNode_ = asNode(node()); + if (pathNode_->limit() != -1) { + limit_ = pathNode_->limit(); + } terminateEarlyVar_ = pathNode_->terminateEarlyVar(); if (step_ == 1) { @@ -194,24 +197,23 @@ folly::Future BFSShortestPathExecutor::conjunctPath() { }); } -DataSet BFSShortestPathExecutor::doConjunct(const std::vector& meetVids, - bool oddStep) const { +DataSet BFSShortestPathExecutor::doConjunct(const std::vector& meetVids, bool oddStep) { DataSet ds; auto leftPaths = createPath(meetVids, false, oddStep); auto rightPaths = createPath(meetVids, true, oddStep); for (auto& leftPath : leftPaths) { auto range = rightPaths.equal_range(leftPath.first); for (auto& rightPath = range.first; rightPath != range.second; ++rightPath) { + cnt_.fetch_add(1, std::memory_order_relaxed); + if (cnt_.load(std::memory_order_relaxed) > limit_) { + return ds; + } Path result = leftPath.second; result.reverse(); result.append(rightPath->second); Row row; row.emplace_back(std::move(result)); ds.rows.emplace_back(std::move(row)); - cnt_.fetch_add(1, std::memory_order_relaxed); - if (cnt_.load(std::memory_order_relaxed) > limit_) { - return ds; - } } } return ds; diff --git a/src/graph/executor/algo/BFSShortestPathExecutor.h b/src/graph/executor/algo/BFSShortestPathExecutor.h index f567137a5f3..28477856d16 100644 --- a/src/graph/executor/algo/BFSShortestPathExecutor.h +++ b/src/graph/executor/algo/BFSShortestPathExecutor.h @@ -55,7 +55,7 @@ class BFSShortestPathExecutor final : public Executor { folly::Future conjunctPath(); - DataSet doConjunct(const std::vector& meetVids, bool oddStep) const; + DataSet doConjunct(const std::vector& meetVids, bool oddStep); std::unordered_multimap createPath(std::vector meetVids, bool reverse, diff --git a/src/graph/executor/algo/MultiShortestPathExecutor.cpp b/src/graph/executor/algo/MultiShortestPathExecutor.cpp index 2092bf8e566..4d4c372adcd 100644 --- a/src/graph/executor/algo/MultiShortestPathExecutor.cpp +++ b/src/graph/executor/algo/MultiShortestPathExecutor.cpp @@ -239,6 +239,10 @@ DataSet MultiShortestPathExecutor::doConjunct( [this](const std::vector& leftPaths, const std::vector& rightPaths, DataSet& ds) { for (const auto& leftPath : leftPaths) { for (const auto& rightPath : rightPaths) { + cnt_.fetch_add(1, std::memory_order_relaxed); + if (cnt_.load(std::memory_order_relaxed) > limit_) { + break; + } auto forwardPath = leftPath; auto backwardPath = rightPath; backwardPath.reverse(); @@ -246,10 +250,6 @@ DataSet MultiShortestPathExecutor::doConjunct( Row row; row.values.emplace_back(std::move(forwardPath)); ds.rows.emplace_back(std::move(row)); - cnt_.fetch_add(1, std::memory_order_relaxed); - if (cnt_.load(std::memory_order_relaxed) > limit_) { - break; - } } } }; diff --git a/src/graph/optimizer/rule/PushLimitDownShortestPathRule.cpp b/src/graph/optimizer/rule/PushLimitDownShortestPathRule.cpp deleted file mode 100644 index 0952e3b30f6..00000000000 --- a/src/graph/optimizer/rule/PushLimitDownShortestPathRule.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* Copyright (c) 2021 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License. - */ - -#include "graph/optimizer/rule/PushLimitDownShortestPathRule.h" - -#include "graph/optimizer/OptContext.h" -#include "graph/optimizer/OptGroup.h" -#include "graph/planner/plan/Algo.h" -#include "graph/planner/plan/PlanNode.h" -#include "graph/planner/plan/Query.h" - -using nebula::graph::BFSShortestPath; -using nebula::graph::Limit; -using nebula::graph::MultiShortestPath; -using nebula::graph::PlanNode; -using nebula::graph::QueryContext; - -namespace nebula { -namespace opt { -// transform Limit->DataCollect->Loop->BFS/MultiShortest -// to DataCollect->Loop->BFS/MultiShortest(limit) -std::unique_ptr PushLimitDownShortestPathRule::kInstance = - std::unique_ptr(new PushLimitDownShortestPathRule()); - -PushLimitDownShortestPathRule::PushLimitDownShortestPathRule() { - RuleSet::QueryRules().addRule(this); -} - -const Pattern &PushLimitDownShortestPathRule::pattern() const { - static Pattern pattern = Pattern::create( - graph::PlanNode::Kind::kLimit, - {Pattern::create( - graph::PlanNode::Kind::kDataCollect, - {Pattern::create(graph::PlanNode::Kind::kLoop, - {Pattern::create({graph::PlanNode::Kind::kBFSShortest, - graph::PlanNode::Kind::kMultiShortestPath})})})}); - return pattern; -} - -StatusOr PushLimitDownShortestPathRule::transform( - OptContext *ctx, const MatchedResult &matched) const { - auto qctx = ctx->qctx(); - auto limitGroupNode = matched.node; - auto dataCollectGroupNode = matched.dependencies.front().node; - auto loopGroupNode = matched.dependencies.front().dependencies.front().node; - auto pathGroupNode = matched.dependencies.front().dependencies.front().dependencies.front().node; - - const auto limit = static_cast(limitGroupNode->node()); - const auto dataCollect = static_cast(dataCollectGroupNode->node()); - const auto loop = static_cast(loopGroupNode->node()); - auto pathNode = pathGroupNode->node(); - auto pathKind = pathNode->kind(); - - int64_t pathLimit = -1; - if (pathKind == PlanNode::Kind::kBFSShortest) { - pathLimit = static_cast(pathNode)->limit(); - } else { - pathLimit = static_cast(pathNode)->limit(); - } - - int64_t limitRows = limit->offset() + limit->count(qctx); - if (pathLimit >= 0 && limitRows >= pathLimit) { - return TransformResult::noTransform(); - } - - auto newDataCollect = static_cast(dataCollect->clone()); - newDataCollect->setOutputVar(limit->outputVar()); - auto newDataCollectGroupNode = OptGroupNode::create(ctx, newDataCollect, limitGroupNode->group()); - - auto newLoop = static_cast(loop->clone()); - auto newLoopGroup = OptGroup::create(ctx); - auto newLoopGroupNode = newLoopGroup->makeGroupNode(newLoop); - - auto newPath = static_cast(pathNode->clone()); - if (pathKind == PlanNode::Kind::kBFSShortest) { - static_cast(newPath)->setLimit(limitRows); - } else { - static_cast(newPath)->setLimit(limitRows); - } - auto newPathGroup = OptGroup::create(ctx); - auto newPathGroupNode = newPathGroup->makeGroupNode(newPath); - - newDataCollectGroupNode->dependsOn(newLoopGroup); - - for (auto dep : pathGroupNode->dependencies()) { - newPathGroupNode->dependsOn(dep); - } - - TransformResult result; - result.eraseAll = true; - result.newGroupNodes.emplace_back(newPathGroupNode); - return result; -} - -std::string PushLimitDownShortestPathRule::toString() const { - return "PushLimitDownShortestPathRule"; -} - -} // namespace opt -} // namespace nebula diff --git a/src/graph/optimizer/rule/PushLimitDownShortestPathRule.h b/src/graph/optimizer/rule/PushLimitDownShortestPathRule.h deleted file mode 100644 index 28ffcdb4378..00000000000 --- a/src/graph/optimizer/rule/PushLimitDownShortestPathRule.h +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright (c) 2021 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License. - */ - -#ifndef GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNSHORTESTPATHRULE_H -#define GRAPH_OPTIMIZER_RULE_PUSHLIMITDOWNSHORTESTPATHRULE_H - -#include "graph/optimizer/OptRule.h" - -namespace nebula { -namespace opt { - -// Push [[Limit]] down [[BFSShortestPath / MulitShortestPath]] -// Required conditions: -// 1. Match the pattern -// Benefits: -// 1. Limit data early to optimize performance -// -// Tranformation: -// Before: -// -// +--------+--------+ -// | Limit | -// | (limit=3) | -// +--------+--------+ -// | -// +---------+---------+ -// | DataCollect | -// +---------+---------+ -// | -// +---------+---------+ -// | Loop | -// +---------+---------+ -// | -// +---------+---------+ -// | ShortestPath | -// +---------+---------+ -// -// After: -// -// +---------+---------+ -// | DataCollect | -// +---------+---------+ -// | -// +---------+---------+ -// | Loop | -// +---------+---------+ -// | -// +--------+--------+ -// | ShortestPaths | -// | (limit=3) | -// +--------+--------+ - -class PushLimitDownShortestPathRule final : public OptRule { - public: - const Pattern &pattern() const override; - - StatusOr transform(OptContext *ctx, - const MatchedResult &matched) const override; - - std::string toString() const override; - - private: - PushLimitDownShortestPathRule(); - - static std::unique_ptr kInstance; -}; - -} // namespace opt -} // namespace nebula -#endif