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

Property pruner #3750

Merged
merged 28 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b6b1ecc
property pruner
jievince Jan 17, 2022
d82606f
postprocess works.
jievince Jan 18, 2022
c453057
add Travese::pruneProperties
jievince Jan 18, 2022
4bbf081
make vertexprops of travese be null when node alias is not used
jievince Jan 18, 2022
180bcbf
fix extractPropsFromExps for tagPropExpr and edgePropexpr, vFilter an…
jievince Jan 18, 2022
e3e7248
add PropertyTracker::update for project node
jievince Jan 19, 2022
0f36c8d
add DeduceMatchPropsVisitor and FLAGS_enable_opt_collapse_project_rule
jievince Jan 20, 2022
481bcbb
add Filter::pruneProperties and only 3 tck cases not passed
jievince Jan 20, 2022
6173b72
Do not do propsUsed.colsSet.erase(it)
jievince Jan 20, 2022
8c78e8c
revert labeltagpropexpr and pass all tck
jievince Jan 20, 2022
792d0f1
add hasAlias, add deduce types for id, src, dst func
jievince Jan 21, 2022
7d4f9dc
rename visitor
jievince Jan 21, 2022
93ec6c5
ingore func id, src, dst, type, typeid, rank, hash in property tracke…
jievince Jan 23, 2022
bcae49b
add Aggregate::pruneProperties and store the alias of id/src/dst... t…
jievince Jan 23, 2022
97071d6
format tck
jievince Jan 23, 2022
c41e056
remove some unusedless headers
jievince Jan 23, 2022
b761996
add tck for plan
jievince Jan 24, 2022
ab9f684
rename has1, has2, has3
jievince Jan 24, 2022
cde0eb3
add flag_enable_optimizer_property_pruner_rule and support prune edge…
jievince Feb 7, 2022
cfc0275
add kUnknownEdgeType
jievince Feb 10, 2022
72b0c22
add PrunePropertiesVisitor
jievince Feb 10, 2022
f60ca32
remove PlanNode::pruneProperties, and move PropertyTracker to Propert…
jievince Feb 10, 2022
4f80bc0
add PrunePropertiesRule.feature
jievince Feb 10, 2022
b8a96af
add markDeleted interface
jievince Feb 10, 2022
cb33f65
remove unused code
jievince Feb 10, 2022
679ff67
fix header macro
jievince Feb 11, 2022
1e936ea
update tck
jievince Feb 11, 2022
a04dc00
Merge branch 'master' into property-pruner
czpmango Feb 11, 2022
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
8 changes: 4 additions & 4 deletions src/common/expression/PropertyExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ class LabelTagPropertyExpression final : public PropertyExpression {
}

private:
LabelTagPropertyExpression(ObjectPool* pool,
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "")
explicit LabelTagPropertyExpression(ObjectPool* pool,
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "")
: PropertyExpression(pool, Kind::kLabelTagProperty, "", tag, prop), label_(label) {}

void writeTo(Encoder& encoder) const override;
Expand Down
2 changes: 2 additions & 0 deletions src/daemons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ nebula_add_executable(
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:optimizer_obj>
$<TARGET_OBJECTS:plan_node_visitor_obj>
$<TARGET_OBJECTS:planner_obj>
$<TARGET_OBJECTS:plan_obj>
$<TARGET_OBJECTS:executor_obj>
Expand Down Expand Up @@ -211,6 +212,7 @@ nebula_add_executable(
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:optimizer_obj>
$<TARGET_OBJECTS:plan_node_visitor_obj>
$<TARGET_OBJECTS:planner_obj>
$<TARGET_OBJECTS:plan_obj>
$<TARGET_OBJECTS:executor_obj>
Expand Down
31 changes: 27 additions & 4 deletions src/graph/optimizer/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "graph/planner/plan/ExecutionPlan.h"
#include "graph/planner/plan/Logic.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/visitor/PrunePropertiesVisitor.h"

using nebula::graph::BinaryInputNode;
using nebula::graph::Loop;
Expand All @@ -20,6 +21,8 @@ using nebula::graph::QueryContext;
using nebula::graph::Select;
using nebula::graph::SingleDependencyNode;

DEFINE_bool(enable_optimizer_property_pruner_rule, true, "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should reuse enable_optimizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could enable_optimizer be able to control whether to enable a single rule?

Copy link
Contributor

@Shylock-Hg Shylock-Hg Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we don't provide a way to control single optimization. And in my mind, enable_optimizer must control all optimizations include this of course.


namespace nebula {
namespace opt {

Expand All @@ -30,12 +33,32 @@ StatusOr<const PlanNode *> Optimizer::findBestPlan(QueryContext *qctx) {
auto optCtx = std::make_unique<OptContext>(qctx);

auto root = qctx->plan()->root();
auto status = prepare(optCtx.get(), root);
NG_RETURN_IF_ERROR(status);
auto rootGroup = std::move(status).value();
auto spaceID = qctx->rctx()->session()->space().id;

auto ret = prepare(optCtx.get(), root);
NG_RETURN_IF_ERROR(ret);
auto rootGroup = std::move(ret).value();

NG_RETURN_IF_ERROR(doExploration(optCtx.get(), rootGroup));
return rootGroup->getPlan();
auto *newRoot = rootGroup->getPlan();

auto status2 = postprocess(const_cast<PlanNode *>(newRoot), qctx, spaceID);
czpmango marked this conversation as resolved.
Show resolved Hide resolved
if (!status2.ok()) {
LOG(ERROR) << "Failed to postprocess plan: " << status2;
}
return newRoot;
}

Status Optimizer::postprocess(PlanNode *root, graph::QueryContext *qctx, GraphSpaceID spaceID) {
if (FLAGS_enable_optimizer_property_pruner_rule) {
graph::PropertyTracker propsUsed;
graph::PrunePropertiesVisitor visitor(propsUsed, qctx, spaceID);
root->accept(&visitor);
if (!visitor.ok()) {
return visitor.status();
}
}
return Status::OK();
}

StatusOr<OptGroup *> Optimizer::prepare(OptContext *ctx, PlanNode *root) {
Expand Down
6 changes: 6 additions & 0 deletions src/graph/optimizer/Optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include "common/base/Base.h"
#include "common/base/StatusOr.h"
#include "common/thrift/ThriftTypes.h"

DECLARE_bool(enable_optimizer_property_pruner_rule);

namespace nebula {
namespace graph {
Expand All @@ -30,7 +33,10 @@ class Optimizer final {
StatusOr<const graph::PlanNode *> findBestPlan(graph::QueryContext *qctx);

private:
Status postprocess(graph::PlanNode *root, graph::QueryContext *qctx, GraphSpaceID spaceID);

StatusOr<OptGroup *> prepare(OptContext *ctx, graph::PlanNode *root);

Status doExploration(OptContext *octx, OptGroup *rootGroup);

OptGroup *convertToGroup(OptContext *ctx,
Expand Down
5 changes: 5 additions & 0 deletions src/graph/optimizer/rule/CollapseProjectRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"

DEFINE_bool(enable_optimizer_collapse_project_rule, true, "");

using nebula::graph::PlanNode;
using nebula::graph::QueryContext;

Expand Down Expand Up @@ -110,6 +112,9 @@ StatusOr<OptRule::TransformResult> CollapseProjectRule::transform(
}

bool CollapseProjectRule::match(OptContext* octx, const MatchedResult& matched) const {
if (!FLAGS_enable_optimizer_collapse_project_rule) {
return false;
}
return OptRule::match(octx, matched);
}

Expand Down
2 changes: 2 additions & 0 deletions src/graph/optimizer/rule/CollapseProjectRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "graph/optimizer/OptRule.h"

DECLARE_bool(enable_optimizer_collapse_project_rule);

namespace nebula {
namespace opt {

Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(OPTIMIZER_TEST_LIB
$<TARGET_OBJECTS:graph_context_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:optimizer_obj>
$<TARGET_OBJECTS:plan_node_visitor_obj>
$<TARGET_OBJECTS:ssl_obj>
$<TARGET_OBJECTS:memory_obj>
$<TARGET_OBJECTS:geo_index_obj>
Expand Down
5 changes: 5 additions & 0 deletions src/graph/planner/plan/PlanNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "common/graph/Response.h"
#include "graph/context/QueryContext.h"
#include "graph/planner/plan/PlanNodeVisitor.h"
#include "graph/util/ToJson.h"

namespace nebula {
Expand Down Expand Up @@ -360,6 +361,10 @@ std::unique_ptr<PlanNodeDescription> PlanNode::explain() const {
return desc;
}

void PlanNode::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

void PlanNode::releaseSymbols() {
auto symTbl = qctx_->symTable();
for (auto in : inputVars_) {
Expand Down
9 changes: 9 additions & 0 deletions src/graph/planner/plan/PlanNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace nebula {
namespace graph {

class PlanNodeVisitor;

/**
* PlanNode is an abstraction of nodes in an execution plan which
* is a kind of directed cyclic graph.
Expand Down Expand Up @@ -189,6 +191,12 @@ class PlanNode {
// Describe plan node
virtual std::unique_ptr<PlanNodeDescription> explain() const;

virtual void accept(PlanNodeVisitor* visitor);

void markDeleted() {
deleted_ = true;
}

virtual PlanNode* clone() const = 0;

virtual void calcCost();
Expand Down Expand Up @@ -311,6 +319,7 @@ class PlanNode {
std::vector<const PlanNode*> dependencies_;
std::vector<Variable*> inputVars_;
std::vector<Variable*> outputVars_;
bool deleted_{false};
};

std::ostream& operator<<(std::ostream& os, PlanNode::Kind kind);
Expand Down
31 changes: 31 additions & 0 deletions src/graph/planner/plan/PlanNodeVisitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#ifndef PLAN_PLANNODEVISITOR_H_
#define PLAN_PLANNODEVISITOR_H_

#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"

namespace nebula {
namespace graph {

class PlanNodeVisitor {
public:
virtual ~PlanNodeVisitor() = default;

virtual void visit(PlanNode *node) = 0;
virtual void visit(Filter *node) = 0;
virtual void visit(Project *node) = 0;
virtual void visit(Aggregate *node) = 0;
virtual void visit(Traverse *node) = 0;
virtual void visit(AppendVertices *node) = 0;
virtual void visit(BiJoin *node) = 0;
};

} // namespace graph
} // namespace nebula

#endif // PLAN_PLANNODEVISITOR_H_
25 changes: 25 additions & 0 deletions src/graph/planner/plan/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <folly/json.h>
#include <thrift/lib/cpp/util/EnumUtils.h>

#include "graph/planner/plan/PlanNodeVisitor.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/ToJson.h"

Expand Down Expand Up @@ -269,6 +270,10 @@ std::unique_ptr<PlanNodeDescription> Filter::explain() const {
return desc;
}

void Filter::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

PlanNode* Filter::clone() const {
auto* newFilter = Filter::make(qctx_, nullptr);
newFilter->cloneMembers(*this);
Expand Down Expand Up @@ -336,6 +341,10 @@ std::unique_ptr<PlanNodeDescription> Project::explain() const {
return desc;
}

void Project::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

PlanNode* Project::clone() const {
auto* newProj = Project::make(qctx_, nullptr);
newProj->cloneMembers(*this);
Expand Down Expand Up @@ -490,6 +499,10 @@ std::unique_ptr<PlanNodeDescription> Aggregate::explain() const {
return desc;
}

void Aggregate::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

PlanNode* Aggregate::clone() const {
auto* newAggregate = Aggregate::make(qctx_, nullptr);
newAggregate->cloneMembers(*this);
Expand Down Expand Up @@ -734,6 +747,10 @@ std::unique_ptr<PlanNodeDescription> Traverse::explain() const {
return desc;
}

void Traverse::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

AppendVertices* AppendVertices::clone() const {
auto newAV = AppendVertices::make(qctx_, nullptr, space_);
newAV->cloneMembers(*this);
Expand All @@ -760,13 +777,21 @@ std::unique_ptr<PlanNodeDescription> AppendVertices::explain() const {
return desc;
}

void AppendVertices::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

std::unique_ptr<PlanNodeDescription> BiJoin::explain() const {
auto desc = BinaryInputNode::explain();
addDescription("hashKeys", folly::toJson(util::toJson(hashKeys_)), desc.get());
addDescription("probeKeys", folly::toJson(util::toJson(probeKeys_)), desc.get());
return desc;
}

void BiJoin::accept(PlanNodeVisitor* visitor) {
visitor->visit(this);
}

void BiJoin::cloneMembers(const BiJoin& j) {
BinaryInputNode::cloneMembers(j);

Expand Down
12 changes: 12 additions & 0 deletions src/graph/planner/plan/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ class Filter final : public SingleInputNode {
PlanNode* clone() const override;
std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

private:
Filter(QueryContext* qctx, PlanNode* input, Expression* condition, bool needStableFilter);
void cloneMembers(const Filter&);
Expand Down Expand Up @@ -826,6 +828,8 @@ class Project final : public SingleInputNode {
PlanNode* clone() const override;
std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

private:
Project(QueryContext* qctx, PlanNode* input, YieldColumns* cols);

Expand Down Expand Up @@ -1114,6 +1118,8 @@ class Aggregate final : public SingleInputNode {
PlanNode* clone() const override;
std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

private:
Aggregate(QueryContext* qctx,
PlanNode* input,
Expand Down Expand Up @@ -1469,6 +1475,8 @@ class Traverse final : public GetNeighbors {

std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

Traverse* clone() const override;

MatchStepRange* stepRange() const {
Expand Down Expand Up @@ -1526,6 +1534,8 @@ class AppendVertices final : public GetVertices {

std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

AppendVertices* clone() const override;

Expression* vFilter() const {
Expand Down Expand Up @@ -1585,6 +1595,8 @@ class BiJoin : public BinaryInputNode {

std::unique_ptr<PlanNodeDescription> explain() const override;

void accept(PlanNodeVisitor* visitor) override;

protected:
BiJoin(QueryContext* qctx,
Kind kind,
Expand Down
1 change: 1 addition & 0 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "graph/context/QueryContext.h"
#include "graph/context/QueryExpressionContext.h"
#include "graph/visitor/FoldConstantExprVisitor.h"
#include "graph/visitor/PropertyTrackerVisitor.h"

DEFINE_int32(max_expression_depth, 512, "Max depth of expression tree.");

Expand Down
6 changes: 6 additions & 0 deletions src/graph/visitor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ nebula_add_library(
expr_visitor_obj OBJECT
ExprVisitorImpl.cpp
DeducePropsVisitor.cpp
PropertyTrackerVisitor.cpp
DeduceTypeVisitor.cpp
ExtractPropExprVisitor.cpp
ExtractFilterExprVisitor.cpp
Expand All @@ -17,4 +18,9 @@ nebula_add_library(
EvaluableExprVisitor.cpp
)

nebula_add_library(
plan_node_visitor_obj OBJECT
PrunePropertiesVisitor.cpp
)

nebula_add_subdirectory(test)
Loading