-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
add PushFilterDownInnerJoinRule #4599
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b98ce0
add PushFilterDownInnerJoinRule
caton-hpg 5284d8f
update tck
caton-hpg f06219d
Merge branch 'master' into PushFilterDownInnerJoin
czpmango 4af5890
Merge branch 'master' into PushFilterDownInnerJoin
Sophie-Xie 7cf91dc
Merge branch 'master' into PushFilterDownInnerJoin
Sophie-Xie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
134 changes: 134 additions & 0 deletions
134
src/graph/optimizer/rule/PushFilterDownInnerJoinRule.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,134 @@ | ||
/* Copyright (c) 2022 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#include "graph/optimizer/rule/PushFilterDownInnerJoinRule.h" | ||
|
||
#include "graph/optimizer/OptContext.h" | ||
#include "graph/optimizer/OptGroup.h" | ||
#include "graph/planner/plan/PlanNode.h" | ||
#include "graph/planner/plan/Query.h" | ||
#include "graph/util/ExpressionUtils.h" | ||
|
||
using nebula::graph::PlanNode; | ||
using nebula::graph::QueryContext; | ||
|
||
namespace nebula { | ||
namespace opt { | ||
|
||
std::unique_ptr<OptRule> PushFilterDownInnerJoinRule::kInstance = | ||
std::unique_ptr<PushFilterDownInnerJoinRule>(new PushFilterDownInnerJoinRule()); | ||
|
||
PushFilterDownInnerJoinRule::PushFilterDownInnerJoinRule() { | ||
RuleSet::QueryRules().addRule(this); | ||
} | ||
|
||
const Pattern& PushFilterDownInnerJoinRule::pattern() const { | ||
static Pattern pattern = Pattern::create(graph::PlanNode::Kind::kFilter, | ||
{Pattern::create(graph::PlanNode::Kind::kInnerJoin)}); | ||
return pattern; | ||
} | ||
|
||
StatusOr<OptRule::TransformResult> PushFilterDownInnerJoinRule::transform( | ||
OptContext* octx, const MatchedResult& matched) const { | ||
auto* filterGroupNode = matched.node; | ||
auto* oldFilterNode = filterGroupNode->node(); | ||
auto deps = matched.dependencies; | ||
DCHECK_EQ(deps.size(), 1); | ||
auto innerJoinGroupNode = deps.front().node; | ||
auto* innerJoinNode = innerJoinGroupNode->node(); | ||
DCHECK_EQ(oldFilterNode->kind(), PlanNode::Kind::kFilter); | ||
DCHECK_EQ(innerJoinNode->kind(), PlanNode::Kind::kInnerJoin); | ||
auto* oldInnerJoinNode = static_cast<graph::InnerJoin*>(innerJoinNode); | ||
const auto* condition = static_cast<graph::Filter*>(oldFilterNode)->condition(); | ||
DCHECK(condition); | ||
const std::pair<std::string, int64_t>& leftVar = oldInnerJoinNode->leftVar(); | ||
auto symTable = octx->qctx()->symTable(); | ||
std::vector<std::string> leftVarColNames = symTable->getVar(leftVar.first)->colNames; | ||
|
||
// split the `condition` based on whether the varPropExpr comes from the left | ||
// child | ||
auto picker = [&leftVarColNames](const Expression* e) -> bool { | ||
auto varProps = graph::ExpressionUtils::collectAll(e, {Expression::Kind::kVarProperty}); | ||
if (varProps.empty()) { | ||
return false; | ||
} | ||
std::vector<std::string> propNames; | ||
for (auto* expr : varProps) { | ||
DCHECK(expr->kind() == Expression::Kind::kVarProperty); | ||
propNames.emplace_back(static_cast<const VariablePropertyExpression*>(expr)->prop()); | ||
} | ||
for (auto prop : propNames) { | ||
auto iter = std::find_if(leftVarColNames.begin(), | ||
leftVarColNames.end(), | ||
[&prop](std::string item) { return !item.compare(prop); }); | ||
if (iter == leftVarColNames.end()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
Expression* filterPicked = nullptr; | ||
Expression* filterUnpicked = nullptr; | ||
graph::ExpressionUtils::splitFilter(condition, picker, &filterPicked, &filterUnpicked); | ||
|
||
if (!filterPicked) { | ||
return TransformResult::noTransform(); | ||
} | ||
|
||
// produce new left Filter node | ||
auto* newLeftFilterNode = | ||
graph::Filter::make(octx->qctx(), | ||
const_cast<graph::PlanNode*>(oldInnerJoinNode->dep()), | ||
graph::ExpressionUtils::rewriteInnerVar(filterPicked, leftVar.first)); | ||
newLeftFilterNode->setInputVar(leftVar.first); | ||
newLeftFilterNode->setColNames(leftVarColNames); | ||
auto newFilterGroup = OptGroup::create(octx); | ||
auto newFilterGroupNode = newFilterGroup->makeGroupNode(newLeftFilterNode); | ||
for (auto dep : innerJoinGroupNode->dependencies()) { | ||
newFilterGroupNode->dependsOn(dep); | ||
} | ||
auto newLeftFilterOutputVar = newLeftFilterNode->outputVar(); | ||
|
||
// produce new InnerJoin node | ||
auto* newInnerJoinNode = static_cast<graph::InnerJoin*>(oldInnerJoinNode->clone()); | ||
newInnerJoinNode->setLeftVar({newLeftFilterOutputVar, 0}); | ||
const std::vector<Expression*>& hashKeys = oldInnerJoinNode->hashKeys(); | ||
std::vector<Expression*> newHashKeys; | ||
for (auto* k : hashKeys) { | ||
newHashKeys.emplace_back(graph::ExpressionUtils::rewriteInnerVar(k, newLeftFilterOutputVar)); | ||
} | ||
newInnerJoinNode->setHashKeys(newHashKeys); | ||
|
||
TransformResult result; | ||
result.eraseAll = true; | ||
if (filterUnpicked) { | ||
auto* newAboveFilterNode = graph::Filter::make(octx->qctx(), newInnerJoinNode); | ||
newAboveFilterNode->setOutputVar(oldFilterNode->outputVar()); | ||
newAboveFilterNode->setCondition(filterUnpicked); | ||
auto newAboveFilterGroupNode = | ||
OptGroupNode::create(octx, newAboveFilterNode, filterGroupNode->group()); | ||
|
||
auto newInnerJoinGroup = OptGroup::create(octx); | ||
auto newInnerJoinGroupNode = newInnerJoinGroup->makeGroupNode(newInnerJoinNode); | ||
newAboveFilterGroupNode->setDeps({newInnerJoinGroup}); | ||
newInnerJoinGroupNode->setDeps({newFilterGroup}); | ||
result.newGroupNodes.emplace_back(newAboveFilterGroupNode); | ||
} else { | ||
newInnerJoinNode->setOutputVar(oldFilterNode->outputVar()); | ||
newInnerJoinNode->setColNames(oldInnerJoinNode->colNames()); | ||
auto newInnerJoinGroupNode = | ||
OptGroupNode::create(octx, newInnerJoinNode, filterGroupNode->group()); | ||
newInnerJoinGroupNode->setDeps({newFilterGroup}); | ||
result.newGroupNodes.emplace_back(newInnerJoinGroupNode); | ||
} | ||
return result; | ||
} | ||
|
||
std::string PushFilterDownInnerJoinRule::toString() const { | ||
return "PushFilterDownInnerJoinRule"; | ||
} | ||
|
||
} // 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,66 @@ | ||
/* Copyright (c) 2022 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#ifndef GRAPH_OPTIMIZER_RULE_PUSHFILTERDOWNINNERJOINRULE_H_ | ||
#define GRAPH_OPTIMIZER_RULE_PUSHFILTERDOWNINNERJOINRULE_H_ | ||
|
||
#include "graph/optimizer/OptRule.h" | ||
|
||
namespace nebula { | ||
namespace opt { | ||
|
||
// Push down the filter items from the left subplan of [[InnerJoin]] | ||
// Required conditions: | ||
// 1. Match the pattern | ||
// Benefits: | ||
// 1. Filter data early to optimize performance | ||
// | ||
// Tranformation: | ||
// Before: | ||
// | ||
// +-----------+-----------+ | ||
// | Filter | | ||
// | ($left>3 and $right<4)| | ||
// +-----------+-----------+ | ||
// | | ||
// +------+------+ | ||
// | InnerJoin | | ||
// +------+------+ | ||
// | ||
// After: | ||
// | ||
// +------+------+ | ||
// | Filter | | ||
// | ($right<4) | | ||
// +------+------+ | ||
// | | ||
// +------+------+ | ||
// | InnerJoin | | ||
// +------+------+ | ||
// / | ||
// +------+------+ | ||
// | Filter | | ||
// | ($left>3) | | ||
// +------+------+ | ||
|
||
class PushFilterDownInnerJoinRule final : public OptRule { | ||
public: | ||
const Pattern &pattern() const override; | ||
|
||
StatusOr<OptRule::TransformResult> transform(OptContext *qctx, | ||
const MatchedResult &matched) const override; | ||
|
||
std::string toString() const override; | ||
|
||
private: | ||
PushFilterDownInnerJoinRule(); | ||
|
||
static std::unique_ptr<OptRule> kInstance; | ||
}; | ||
|
||
} // namespace opt | ||
} // namespace nebula | ||
|
||
#endif // GRAPH_OPTIMIZER_RULE_PUSHFILTERDOWNINNERJOINRULE_H_ |
31 changes: 31 additions & 0 deletions
31
tests/tck/features/optimizer/PushFilterDownInnerJoinRule.feature
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,31 @@ | ||
# Copyright (c) 2021 vesoft inc. All rights reserved. | ||
# | ||
# This source code is licensed under Apache 2.0 License. | ||
Feature: Push Filter down InnerJoin rule | ||
|
||
Background: | ||
Given a graph with space named "nba" | ||
|
||
Scenario: push filter down InnerJoin | ||
When profiling query: | ||
""" | ||
LOOKUP ON player WHERE player.name == "Tony Parker" | ||
YIELD id(vertex) as id | | ||
GO FROM $-.id OVER like | ||
WHERE (like.likeness - 1) >= 0 | ||
YIELD like._src AS src_id, like._dst AS dst_id, like.likeness AS likeness | ||
""" | ||
Then the result should be, in any order: | ||
| src_id | dst_id | likeness | | ||
| "Tony Parker" | "LaMarcus Aldridge" | 90 | | ||
| "Tony Parker" | "Manu Ginobili" | 95 | | ||
| "Tony Parker" | "Tim Duncan" | 95 | | ||
And the execution plan should be: | ||
| id | name | dependencies | operator info | | ||
| 10 | Project | 15 | | | ||
| 15 | InnerJoin | 17 | | | ||
| 17 | Project | 18 | | | ||
| 18 | GetNeighbors | 3 | | | ||
| 3 | Project | 11 | | | ||
| 11 | TagIndexPrefixScan | 0 | | | ||
| 0 | Start | | | |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's also workable for LeftJoin, Product.