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

add optimizer comments #3910

Merged
merged 33 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ac95393
small format
czpmango Feb 17, 2022
6712bd7
add comments for EliminateRowCollectRule
czpmango Feb 17, 2022
24da62b
add comments for MergeGetNbrsAndDedupRule
czpmango Feb 17, 2022
56659c0
add comments for MergeGetNbrsAndDedupRule
czpmango Feb 17, 2022
4495ffb
add comments for MergeGetNbrsAndProjectRule
czpmango Feb 17, 2022
71f0b66
add comments for MergeGetVerticesAndDedupRule
czpmango Feb 17, 2022
d6d6889
add comments for MergeGetVerticesAndProjectRule
czpmango Feb 17, 2022
1777bf4
add comments for PushFilterDownAggregateRule
czpmango Feb 21, 2022
965a797
add comments for PushFilterDownGetNbrsRule
czpmango Feb 21, 2022
e064e82
add comments for PushFilterDownLeftJoinRule
czpmango Feb 21, 2022
60b8857
add comments for PushFilterDownProjectRule
czpmango Feb 21, 2022
e99c283
add comments for PushFilterDownScanVerticesRule
czpmango Feb 25, 2022
3874a67
add comments for PushFilterDownGetNeighborsRule
czpmango Feb 25, 2022
9aa183b
add comments for PushLimitDownGetNeighborsRule
czpmango Feb 25, 2022
8a2d540
add comments for PushLimitDownIndexScanRule
czpmango Feb 25, 2022
d92dd50
tmp
czpmango Mar 4, 2022
023a0f2
add comments for PushLimitDownScanAppendVerticesRule
czpmango Mar 10, 2022
4a062cb
add comments for PushLimitDownScanEdgesAppendVerticesRule
czpmango Mar 10, 2022
e0a53eb
PushStepLimitDownGetNeighborsRule
czpmango Mar 10, 2022
2635b8c
add comments for PushStepSampleDownGetNeighborsRule
czpmango Mar 10, 2022
14bcf07
add comments for TopNrule
czpmango Mar 14, 2022
d92489c
add comments for PushTopNDownIndexScanRule
czpmango Mar 14, 2022
5c71fe9
add comments for PushVFilterDownScanVerticesRule
czpmango Mar 14, 2022
58a579d
add comments for RemoveNoopProjectRule
czpmango Mar 14, 2022
4ff8ab7
small comments
czpmango Mar 14, 2022
a15abd9
add comments for getEdgesTransformRule
czpmango Mar 14, 2022
b7e1b43
add comments for IndexScanRule
czpmango Mar 14, 2022
099a7c4
small comments
czpmango Mar 14, 2022
54500b4
add comments for memo operations
czpmango Mar 14, 2022
dc3a40f
small delete
czpmango Mar 14, 2022
e6cab7a
format
czpmango Mar 14, 2022
13a081a
Merge branch 'master' into opt-comments
yixinglu Mar 15, 2022
d1443b1
Merge branch 'master' into opt-comments
Sophie-Xie Mar 15, 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
2 changes: 2 additions & 0 deletions src/graph/optimizer/OptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ class OptContext final : private boost::noncopyable, private cpp::NonMovable {
const OptGroupNode *findOptGroupNodeByPlanNodeId(int64_t planNodeId) const;

private:
// A global flag to record whether this iteration caused a change to the plan
bool changed_{true};
graph::QueryContext *qctx_{nullptr};
// Memo memory management in the Optimizer phase
Copy link
Contributor

Choose a reason for hiding this comment

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

What is memo?

Aiee marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this?

std::unique_ptr<ObjectPool> objPool_;
std::unordered_map<int64_t, const OptGroupNode *> planNodeToOptGroupNodeMap_;
};
Expand Down
6 changes: 6 additions & 0 deletions src/graph/optimizer/Optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace opt {

Optimizer::Optimizer(std::vector<const RuleSet *> ruleSets) : ruleSets_(std::move(ruleSets)) {}

// Optimizer entrance
StatusOr<const PlanNode *> Optimizer::findBestPlan(QueryContext *qctx) {
DCHECK(qctx != nullptr);
auto optCtx = std::make_unique<OptContext>(qctx);
Expand All @@ -49,6 +50,7 @@ StatusOr<const PlanNode *> Optimizer::findBestPlan(QueryContext *qctx) {
return newRoot;
}

// Just for Properties Pruning
Status Optimizer::postprocess(PlanNode *root, graph::QueryContext *qctx, GraphSpaceID spaceID) {
if (FLAGS_enable_optimizer_property_pruner_rule) {
graph::PropertyTracker propsUsed;
Expand All @@ -67,12 +69,15 @@ StatusOr<OptGroup *> Optimizer::prepare(OptContext *ctx, PlanNode *root) {
}

Status Optimizer::doExploration(OptContext *octx, OptGroup *rootGroup) {
// Terminate when the maximum number of iterations(RuleSets) is reached or the execution plan is
// unchanged
int8_t appliedTimes = kMaxIterationRound;
while (octx->changed()) {
if (--appliedTimes < 0) break;
octx->setChanged(false);
for (auto ruleSet : ruleSets_) {
for (auto rule : ruleSet->rules()) {
// Explore until the maximum number of iterations(Rules) is reached
NG_RETURN_IF_ERROR(rootGroup->exploreUntilMaxRound(rule));
rootGroup->setUnexplored(rule);
}
Expand All @@ -81,6 +86,7 @@ Status Optimizer::doExploration(OptContext *octx, OptGroup *rootGroup) {
return Status::OK();
}

// Create Memo structure
OptGroup *Optimizer::convertToGroup(OptContext *ctx,
PlanNode *node,
std::unordered_map<int64_t, OptGroup *> *visited) {
Expand Down
37 changes: 28 additions & 9 deletions src/graph/optimizer/rule/CollapseProjectRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@ DECLARE_bool(enable_optimizer_collapse_project_rule);
namespace nebula {
namespace opt {

/**
* Combines two [[Project]] nodes into one
* Required conditions:
* 1. Match the pattern
* 2. Expressions between nodes cannot be referenced more than once
* Benefits:
* 1. reduce the copy of memory between nodes
* 2. reduces expression overhead in some cases(similar to column pruning)
*/
// Combines two [[Project]] nodes into one
// Required conditions:
// 1. Match the pattern
// 2. Expressions between nodes cannot be referenced more than once
// Benefits:
// 1. reduce the copy of memory between nodes
// 2. reduces expression overhead in some cases(similar to column pruning)
//
// Tranformation:
// Before:
//
// +------------+------------+
// | Project |
// | ($A1+1 AS A2,$B1 AS B2) |
// +------------+------------+
// |
// +-------------------+-------------------+
// | Project |
// | ($v.age+1 AS A1,$v AS B1,$n.age AS C1)|
// +-------------------+-------------------+
//
// After:
//
// +--------------+--------------+
// | Project |
// | ($v.age+1+1 AS A2,$v AS B2) |
// +--------------+--------------+
//

class CollapseProjectRule final : public OptRule {
public:
Expand Down
24 changes: 24 additions & 0 deletions src/graph/optimizer/rule/CombineFilterRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
namespace nebula {
namespace opt {

// Combines two [[Filter]] nodes into one and connect the filter expressions with `LogicalAnd`
// Required conditions:
// 1. Match the pattern
// Benefits:
// 1. reduces the expression iterated times
//
// Tranformation:
// Before:
//
// +-----+-----+
// | Filter(A) |
// +-----+-----+
// |
// +-----+-----+
// | Filter(B) |
// +-----+-----+
//
// After:
//
// +--------+--------+
// | Filter(A and B) |
// +--------+--------+
//

class CombineFilterRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/EdgeIndexFullScanRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace nebula {
namespace opt {

// Apply the transformation of base class(IndexFullScanBaseRule::transform)
class EdgeIndexFullScanRule final : public IndexFullScanBaseRule {
public:
const Pattern& pattern() const override;
Expand Down
24 changes: 24 additions & 0 deletions src/graph/optimizer/rule/EliminateRowCollectRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@
namespace nebula {
namespace opt {

// Eliminate useless [[DataCollect]] node
// Required conditions:
// 1. Match the pattern
// 2. DataCollect::DCKind is `kRowBasedMove`
// Benefits:
// 1. Delete unnecessary node
//
// Tranformation:
// Before:
//
// +------+------+
// | DataCollect |
// +------+------+
// |
// +------+------+
// | Project |
// +------+------+
//
// After:
//
// +------+------+
// | Project |
// +------+------+

class EliminateRowCollectRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/GeoPredicateEdgeIndexScanRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace nebula {
namespace opt {

// Apply the transformation of base class(GeoPredicateIndexScanBaseRule::transform)
class GeoPredicateEdgeIndexScanRule final : public GeoPredicateIndexScanBaseRule {
public:
const Pattern &pattern() const override;
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/GeoPredicateTagIndexScanRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace nebula {
namespace opt {

// Apply the transformation of base class(GeoPredicateIndexScanBaseRule::transform)
class GeoPredicateTagIndexScanRule final : public GeoPredicateIndexScanBaseRule {
public:
const Pattern &pattern() const override;
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/GetEdgesTransformRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ bool GetEdgesTransformRule::match(OptContext *ctx, const MatchedResult &matched)
const auto &colNames = traverse->colNames();
auto colSize = colNames.size();
DCHECK_GE(colSize, 2);
// TODO: Poor readability for optimizer, is there any other way to do the same thing?
if (colNames[colSize - 2][0] != '_') { // src
return false;
}
Expand Down
39 changes: 37 additions & 2 deletions src/graph/optimizer/rule/GetEdgesTransformRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,43 @@ class PlanNode;

namespace opt {

// e.g. match ()-[e]->(?) return e
// Optimize to get edges directly
// Convert [[ScanVertices]] to [[ScanEdges]] in certain cases
// Required conditions:
// 1. Match the pattern
// Benefits:
// 1. Avoid doing Traverse to optimize performance
// Quey example:
// 1. match ()-[e]->() return e limit 3
//
// Tranformation:
// Before:
//
// +---------+---------+
// | AppendVertices |
// +---------+---------+
// |
// +---------+---------+
// | Traverse |
// +---------+---------+
// |
// +---------+---------+
// | ScanVertices |
// +---------+---------+
//
// After:
//
// +---------+---------+
// | AppendVertices |
// +---------+---------+
// |
// +---------+---------+
// | Project |
// +---------+---------+
// |
// +---------+---------+
// | ScanEdges |
// +---------+---------+

class GetEdgesTransformRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
5 changes: 4 additions & 1 deletion src/graph/optimizer/rule/IndexScanRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ StatusOr<OptRule::TransformResult> IndexScanRule::transform(OptContext* ctx,
NG_RETURN_IF_ERROR(analyzeExpression(newFilter, &items, &kind, isEdge(groupNode), qctx));
auto status = createIndexQueryCtx(iqctx, kind, items, qctx, groupNode);
if (!status.ok()) {
// Degenerate back to tag lookup
NG_RETURN_IF_ERROR(createIndexQueryCtx(iqctx, qctx, groupNode));
}
}
Expand Down Expand Up @@ -118,6 +119,7 @@ Status IndexScanRule::createIndexQueryCtx(IndexQueryCtx& iqctx,
return Status::OK();
}

// Single ColumnHints item
Status IndexScanRule::createSingleIQC(IndexQueryCtx& iqctx,
const FilterItems& items,
graph::QueryContext* qctx,
Expand All @@ -132,6 +134,7 @@ Status IndexScanRule::createSingleIQC(IndexQueryCtx& iqctx,
return appendIQCtx(index, items, iqctx, newFilter);
}

// Multiple ColumnHints items
Status IndexScanRule::createMultipleIQC(IndexQueryCtx& iqctx,
const FilterItems& items,
graph::QueryContext* qctx,
Expand Down Expand Up @@ -237,7 +240,6 @@ inline bool verifyType(const Value& val) {
Status IndexScanRule::appendColHint(std::vector<IndexColumnHint>& hints,
const FilterItems& items,
const meta::cpp2::ColumnDef& col) const {
// CHECK(false);
IndexColumnHint hint;
std::pair<Value, bool> begin, end;
bool isRangeScan = true;
Expand Down Expand Up @@ -324,6 +326,7 @@ GraphSpaceID IndexScanRule::spaceId(const OptGroupNode* groupNode) const {
return in->space();
}

// TODO: Delete similar interfaces and get the filter from PlanNode
Expression* IndexScanRule::filterExpr(const OptGroupNode* groupNode) const {
auto in = static_cast<const IndexScan*>(groupNode->node());
const auto& qct = in->queryContext();
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/IndexScanRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using IndexItem = std::shared_ptr<meta::cpp2::IndexItem>;

class OptContext;

// Select the optimal index according to the preset filter inside [[IndexScan]]
class IndexScanRule final : public OptRule {
FRIEND_TEST(IndexScanRuleTest, BoundValueTest);
FRIEND_TEST(IndexScanRuleTest, IQCtxTest);
Expand Down
24 changes: 24 additions & 0 deletions src/graph/optimizer/rule/MergeGetNbrsAndDedupRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
namespace nebula {
namespace opt {

// Merge [[Dedup]] and [[GetNeighbors]] node
// Required conditions:
// 1. Match the pattern
// Benefits:
// 1. Delete unnecessary node
//
// Tranformation:
// Before:
//
// +------+-------+
// | GetNeighbors |
// +------+-------+
// |
// +------+------+
// | Dedup |
// +------+------+
//
// After:
//
// +------+-------+
// | GetNeighbors |
// | (dedup=true) |
// +------+-------+

class MergeGetNbrsAndDedupRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
27 changes: 27 additions & 0 deletions src/graph/optimizer/rule/MergeGetNbrsAndProjectRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@
namespace nebula {
namespace opt {

// Merge [[Project]] and [[GetNeighbors]] node
// Required conditions:
// 1. Match the pattern
// 2. The projection must project only one column which expression will be assigned to the src
// expression of GetNeighbors Benefits:
// 1. Delete unnecessary node
//
// Tranformation:
// Before:
//
// +------+-------+
// | GetNeighbors |
// | (src:$-.vage)|
// +------+-------+
// |
// +-------+-------+
// | Project |
// |(v.age AS vage)|
// +-------+-------+
//
// After:
//
// +------+-------+
// | GetNeighbors |
// | (src:v.age) |
// +------+-------+

class MergeGetNbrsAndProjectRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
24 changes: 24 additions & 0 deletions src/graph/optimizer/rule/MergeGetVerticesAndDedupRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
namespace nebula {
namespace opt {

// Merge [[Dedup]] and [[GetVertices]] node
// Required conditions:
// 1. Match the pattern
// Benefits:
// 1. Delete unnecessary node
//
// Tranformation:
// Before:
//
// +------+-------+
// | GetVertices |
// +------+-------+
// |
// +------+------+
// | Dedup |
// +------+------+
//
// After:
//
// +------+-------+
// | GetVertices |
// | (dedup=true) |
// +------+-------+

class MergeGetVerticesAndDedupRule final : public OptRule {
public:
const Pattern &pattern() const override;
Expand Down
Loading