-
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.
push filter down traverse rule (#4987)
- Loading branch information
Showing
11 changed files
with
420 additions
and
36 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
152 changes: 152 additions & 0 deletions
152
src/graph/optimizer/rule/PushFilterDownTraverseRule.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,152 @@ | ||
/* Copyright (c) 2022 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#include "graph/optimizer/rule/PushFilterDownTraverseRule.h" | ||
|
||
#include "common/expression/ConstantExpression.h" | ||
#include "common/expression/Expression.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" | ||
#include "graph/visitor/ExtractFilterExprVisitor.h" | ||
|
||
using nebula::Expression; | ||
using nebula::graph::Filter; | ||
using nebula::graph::PlanNode; | ||
using nebula::graph::QueryContext; | ||
using nebula::graph::Traverse; | ||
|
||
namespace nebula { | ||
namespace opt { | ||
|
||
std::unique_ptr<OptRule> PushFilterDownTraverseRule::kInstance = | ||
std::unique_ptr<PushFilterDownTraverseRule>(new PushFilterDownTraverseRule()); | ||
|
||
PushFilterDownTraverseRule::PushFilterDownTraverseRule() { | ||
RuleSet::QueryRules().addRule(this); | ||
} | ||
|
||
const Pattern& PushFilterDownTraverseRule::pattern() const { | ||
static Pattern pattern = | ||
Pattern::create(PlanNode::Kind::kFilter, | ||
{Pattern::create(PlanNode::Kind::kAppendVertices, | ||
{Pattern::create(PlanNode::Kind::kTraverse)})}); | ||
return pattern; | ||
} | ||
|
||
bool PushFilterDownTraverseRule::match(OptContext* ctx, const MatchedResult& matched) const { | ||
if (!OptRule::match(ctx, matched)) { | ||
return false; | ||
} | ||
DCHECK_EQ(matched.dependencies[0].dependencies[0].node->node()->kind(), | ||
PlanNode::Kind::kTraverse); | ||
auto traverse = | ||
static_cast<const Traverse*>(matched.dependencies[0].dependencies[0].node->node()); | ||
return traverse->isOneStep(); | ||
} | ||
|
||
StatusOr<OptRule::TransformResult> PushFilterDownTraverseRule::transform( | ||
OptContext* ctx, const MatchedResult& matched) const { | ||
auto* filterGroupNode = matched.node; | ||
auto* filterGroup = filterGroupNode->group(); | ||
auto* filter = static_cast<graph::Filter*>(filterGroupNode->node()); | ||
auto* condition = filter->condition(); | ||
|
||
auto* avGroupNode = matched.dependencies[0].node; | ||
auto* av = static_cast<graph::AppendVertices*>(avGroupNode->node()); | ||
|
||
auto* tvGroupNode = matched.dependencies[0].dependencies[0].node; | ||
auto* tv = static_cast<graph::Traverse*>(tvGroupNode->node()); | ||
auto& edgeAlias = tv->edgeAlias(); | ||
|
||
auto qctx = ctx->qctx(); | ||
auto pool = qctx->objPool(); | ||
|
||
// Pick the expr looks like `$-.e[0].likeness | ||
auto picker = [&edgeAlias](const Expression* e) -> bool { | ||
// TODO(jie): Handle the strange exists expr. e.g. exists(e.likeness) | ||
auto exprs = graph::ExpressionUtils::collectAll(e, {Expression::Kind::kPredicate}); | ||
for (auto* expr : exprs) { | ||
if (static_cast<const PredicateExpression*>(expr)->name() == "exists") { | ||
return false; | ||
} | ||
} | ||
|
||
auto varProps = graph::ExpressionUtils::collectAll( | ||
e, {Expression::Kind::kInputProperty, Expression::Kind::kVarProperty}); | ||
if (varProps.empty()) { | ||
return false; | ||
} | ||
for (auto* expr : varProps) { | ||
DCHECK(graph::ExpressionUtils::isPropertyExpr(expr)); | ||
auto& propName = static_cast<const PropertyExpression*>(expr)->prop(); | ||
if (propName != edgeAlias) return false; | ||
} | ||
return true; | ||
}; | ||
Expression* filterPicked = nullptr; | ||
Expression* filterUnpicked = nullptr; | ||
graph::ExpressionUtils::splitFilter(condition, picker, &filterPicked, &filterUnpicked); | ||
|
||
if (!filterPicked) { | ||
return TransformResult::noTransform(); | ||
} | ||
auto* newFilterPicked = | ||
graph::ExpressionUtils::rewriteEdgePropertyFilter(pool, edgeAlias, filterPicked->clone()); | ||
|
||
Filter* newFilter = nullptr; | ||
OptGroupNode* newFilterGroupNode = nullptr; | ||
if (filterUnpicked) { | ||
newFilter = Filter::make(qctx, nullptr, filterUnpicked); | ||
newFilter->setOutputVar(filter->outputVar()); | ||
newFilter->setColNames(filter->colNames()); | ||
newFilterGroupNode = OptGroupNode::create(ctx, newFilter, filterGroup); | ||
} | ||
|
||
auto* newAv = static_cast<graph::AppendVertices*>(av->clone()); | ||
|
||
OptGroupNode* newAvGroupNode = nullptr; | ||
if (newFilterGroupNode) { | ||
auto* newAvGroup = OptGroup::create(ctx); | ||
newAvGroupNode = newAvGroup->makeGroupNode(newAv); | ||
newFilterGroupNode->dependsOn(newAvGroup); | ||
newFilter->setInputVar(newAv->outputVar()); | ||
} else { | ||
newAvGroupNode = OptGroupNode::create(ctx, newAv, filterGroup); | ||
newAv->setOutputVar(filter->outputVar()); | ||
} | ||
|
||
auto* eFilter = tv->eFilter(); | ||
Expression* newEFilter = eFilter | ||
? LogicalExpression::makeAnd(pool, newFilterPicked, eFilter->clone()) | ||
: newFilterPicked; | ||
|
||
auto* newTv = static_cast<graph::Traverse*>(tv->clone()); | ||
newAv->setInputVar(newTv->outputVar()); | ||
newTv->setEdgeFilter(newEFilter); | ||
|
||
auto* newTvGroup = OptGroup::create(ctx); | ||
newAvGroupNode->dependsOn(newTvGroup); | ||
auto* newTvGroupNode = newTvGroup->makeGroupNode(newTv); | ||
|
||
for (auto dep : tvGroupNode->dependencies()) { | ||
newTvGroupNode->dependsOn(dep); | ||
} | ||
|
||
TransformResult result; | ||
result.eraseCurr = true; | ||
result.newGroupNodes.emplace_back(newFilterGroupNode ? newFilterGroupNode : newAvGroupNode); | ||
|
||
return result; | ||
} | ||
|
||
std::string PushFilterDownTraverseRule::toString() const { | ||
return "PushFilterDownTraverseRule"; | ||
} | ||
|
||
} // 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,43 @@ | ||
/* Copyright (c) 2022 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 { | ||
|
||
/* | ||
* Before: | ||
* Filter(e.likeness > 78) | ||
* | | ||
* AppendVertices | ||
* | | ||
* Traverse | ||
* | ||
* After : | ||
* AppendVertices | ||
* | | ||
* Traverse(eFilter_: *.likeness > 78) | ||
*/ | ||
class PushFilterDownTraverseRule 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: | ||
PushFilterDownTraverseRule(); | ||
|
||
static std::unique_ptr<OptRule> kInstance; | ||
}; | ||
|
||
} // 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
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.