-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bec96a9
commit 5929ab3
Showing
13 changed files
with
259 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/graph/optimizer/rule/PushLimitDownShortestPathRule.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* 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<OptRule> PushLimitDownShortestPathRule::kInstance = | ||
std::unique_ptr<PushLimitDownShortestPathRule>(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<OptRule::TransformResult> 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<const Limit *>(limitGroupNode->node()); | ||
const auto dataCollect = static_cast<const DataCollect *>(dataCollectGroupNode->node()); | ||
const auto loop = static_cast<const Loop *>(loopGroupNode->node()); | ||
auto pathNode = pathGroupNode->node(); | ||
auto pathKind = pathNode->kind(); | ||
|
||
int64_t pathLimit = -1; | ||
if (pathKind == PlanNode::Kind::kBFSShortest) { | ||
pathLimit = static_cast<const BFSShortestPath *>(pathNode)->limit(); | ||
} else { | ||
pathLimit = static_cast<const MultiShortestPath *>(pathNode)->limit(); | ||
} | ||
|
||
int64_t limitRows = limit->offset() + limit->count(qctx); | ||
if (pathLimit >= 0 && limitRows >= pathLimit) { | ||
return TransformResult::noTransform(); | ||
} | ||
|
||
auto newDataCollect = static_cast<dataCollect *>(dataCollect->clone()); | ||
newDataCollect->setOutputVar(limit->outputVar()); | ||
auto newDataCollectGroupNode = OptGroupNode::create(ctx, newDataCollect, limitGroupNode->group()); | ||
|
||
auto newLoop = static_cast<Loop *>(loop->clone()); | ||
auto newLoopGroup = OptGroup::create(ctx); | ||
auto newLoopGroupNode = newLoopGroup->makeGroupNode(newLoop); | ||
|
||
auto newPath = static_cast<decltype(pathNode)>(pathNode->clone()); | ||
if (pathKind == PlanNode::Kind::kBFSShortest) { | ||
static_cast<BFSShortestPath *>(newPath)->setLimit(limitRows); | ||
} else { | ||
static_cast<MultiShortestPath *>(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* 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<OptRule::TransformResult> transform(OptContext *ctx, | ||
const MatchedResult &matched) const override; | ||
|
||
std::string toString() const override; | ||
|
||
private: | ||
PushLimitDownShortestPathRule(); | ||
|
||
static std::unique_ptr<OptRule> kInstance; | ||
}; | ||
|
||
} // namespace opt | ||
} // namespace nebula | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.