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

Eliminte the useless row collect. #3363

Merged
merged 15 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/graph/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ nebula_add_library(
rule/PushLimitDownEdgeIndexPrefixScanRule.cpp
rule/PushLimitDownEdgeIndexRangeScanRule.cpp
rule/PushLimitDownProjectRule.cpp
rule/EliminateRowCollectRule.cpp
)

nebula_add_subdirectory(test)
69 changes: 69 additions & 0 deletions src/graph/optimizer/rule/EliminateRowCollectRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "graph/optimizer/rule/EliminateRowCollectRule.h"

#include "graph/optimizer/OptContext.h"
#include "graph/optimizer/OptGroup.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"

using nebula::graph::DataCollect;
using nebula::graph::PlanNode;
using nebula::graph::Project;
using nebula::graph::QueryContext;

namespace nebula {
namespace opt {

std::unique_ptr<OptRule> EliminateRowCollectRule::kInstance =
std::unique_ptr<EliminateRowCollectRule>(new EliminateRowCollectRule());

EliminateRowCollectRule::EliminateRowCollectRule() { RuleSet::QueryRules().addRule(this); }

// TODO match DataCollect->(Any Node with real result)
const Pattern& EliminateRowCollectRule::pattern() const {
static Pattern pattern = Pattern::create(graph::PlanNode::Kind::kDataCollect,
{Pattern::create(graph::PlanNode::Kind::kProject)});
return pattern;
}

bool EliminateRowCollectRule::match(OptContext* octx, const MatchedResult& matched) const {
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
if (!OptRule::match(octx, matched)) {
return false;
}
const auto* collectNode = static_cast<const DataCollect*>(matched.node->node());
if (collectNode->kind() != DataCollect::DCKind::kRowBasedMove) {
return false;
}
return true;
}

StatusOr<OptRule::TransformResult> EliminateRowCollectRule::transform(
OptContext* octx, const MatchedResult& matched) const {
auto dataCollectGroupNode = matched.node;
auto projGroupNode = matched.dependencies.front().node;

const auto dataCollect = static_cast<const DataCollect*>(dataCollectGroupNode->node());
const auto proj = static_cast<const Project*>(projGroupNode->node());

auto newProj = static_cast<Project*>(proj->clone());
newProj->setOutputVar(dataCollect->outputVar());
auto newProjGroupNode = OptGroupNode::create(octx, newProj, dataCollectGroupNode->group());

for (auto dep : projGroupNode->dependencies()) {
newProjGroupNode->dependsOn(dep);
}

TransformResult result;
result.eraseAll = true;
result.newGroupNodes.emplace_back(newProjGroupNode);
return result;
}

std::string EliminateRowCollectRule::toString() const { return "EliminateRowCollectRule"; }

} // namespace opt
} // namespace nebula
30 changes: 30 additions & 0 deletions src/graph/optimizer/rule/EliminateRowCollectRule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#pragma once

#include "graph/optimizer/OptRule.h"

namespace nebula {
namespace opt {

class EliminateRowCollectRule final : public OptRule {
public:
const Pattern &pattern() const override;

bool match(OptContext *ctx, const MatchedResult &matched) const override;

StatusOr<TransformResult> transform(OptContext *ctx, const MatchedResult &matched) const override;

std::string toString() const override;

private:
EliminateRowCollectRule();

static std::unique_ptr<OptRule> kInstance;
};

} // namespace opt
} // namespace nebula
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Feature: Push Limit down rule
| "Luka Doncic" |
And the execution plan should be:
| id | name | dependencies | operator info |
| 4 | DataCollect | 5 | |
| 5 | Project | 6 | |
| 6 | Limit | 7 | |
| 7 | GetNeighbors | 0 | {"limit": "2"} |
Expand All @@ -39,7 +38,6 @@ Feature: Push Limit down rule
| 1998 |
And the execution plan should be:
| id | name | dependencies | operator info |
| 0 | DataCollect | 1 | |
| 1 | Project | 2 | |
| 2 | Limit | 3 | |
| 3 | GetNeighbors | 4 | {"limit": "7"} |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Feature: Push Limit down project rule
| <("Tim Duncan" :bachelor{name: "Tim Duncan", speciality: "psychology"} :player{age: 42, name: "Tim Duncan"})-[:like@0 {likeness: 95}]->("Tony Parker" :player{age: 36, name: "Tony Parker"})> |
And the execution plan should be:
| id | name | dependencies | operator info |
| 9 | DataCollect | 19 | |
| 19 | Project | 16 | |
| 16 | Limit | 11 | |
| 11 | Filter | 4 | |
Expand Down