This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename planner to plan and add planners dir. * Add planner framework. * Implement SequentialPlanner. * Add ast context. * Refactor the toPlan and address comment. * Refactor: planners could be instantiated. * Fix initialization-order-fiasco. * Update namings.
- Loading branch information
Showing
13 changed files
with
276 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#ifndef CONTEXT_ASTCONTEXT_H_ | ||
#define CONTEXT_ASTCONTEXT_H_ | ||
|
||
#include "context/QueryContext.h" | ||
#include "parser/Sentence.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
struct AstContext { | ||
QueryContext* qctx; | ||
Sentence* sentence; | ||
}; | ||
} // namespace graph | ||
} // namespace nebula | ||
#endif // CONTEXT_ASTCONTEXT_H_ |
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 |
---|---|---|
|
@@ -12,4 +12,6 @@ nebula_add_library( | |
Query.cpp | ||
Mutate.cpp | ||
Maintain.cpp | ||
Planner.cpp | ||
planners/SequentialPlanner.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,32 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#include "planner/Planner.h" | ||
|
||
#include "validator/Validator.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
|
||
StatusOr<SubPlan> Planner::toPlan(AstContext* astCtx) { | ||
if (astCtx == nullptr) { | ||
return Status::Error("AstContext nullptr."); | ||
} | ||
const auto* sentence = astCtx->sentence; | ||
DCHECK(sentence != nullptr); | ||
auto planners = plannersMap().find(sentence->kind()); | ||
if (planners == plannersMap().end()) { | ||
return Status::Error("No planners for sentence: %s", sentence->toString().c_str()); | ||
} | ||
for (auto& planner : planners->second) { | ||
if (planner.match(astCtx)) { | ||
return planner.instantiate()->transform(astCtx); | ||
} | ||
} | ||
return Status::Error("No planner matches sentence: %s", sentence->toString().c_str()); | ||
} | ||
} // namespace graph | ||
} // 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,51 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#ifndef PLANNER_PLANNERS_PLANNER_H_ | ||
#define PLANNER_PLANNERS_PLANNER_H_ | ||
|
||
#include "common/base/Base.h" | ||
#include "planner/PlanNode.h" | ||
#include "context/AstContext.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
class Planner; | ||
|
||
struct SubPlan { | ||
// root and tail of a subplan. | ||
PlanNode* root{nullptr}; | ||
PlanNode* tail{nullptr}; | ||
}; | ||
|
||
using MatchFunc = std::function<bool(AstContext* astContext)>; | ||
using PlannerInstantiateFunc = std::function<std::unique_ptr<Planner>()>; | ||
struct MatchAndInstantiate { | ||
MatchAndInstantiate(MatchFunc m, PlannerInstantiateFunc p) | ||
: match(std::move(m)), instantiate(std::move(p)) {} | ||
MatchFunc match; | ||
PlannerInstantiateFunc instantiate; | ||
}; | ||
|
||
class Planner { | ||
public: | ||
virtual ~Planner() = default; | ||
|
||
static auto& plannersMap() { | ||
static std::unordered_map<Sentence::Kind, std::vector<MatchAndInstantiate>> plannersMap; | ||
return plannersMap; | ||
} | ||
|
||
static StatusOr<SubPlan> toPlan(AstContext* astCtx); | ||
|
||
virtual StatusOr<SubPlan> transform(AstContext* astCtx) = 0; | ||
|
||
protected: | ||
Planner() = default; | ||
}; | ||
} // namespace graph | ||
} // namespace nebula | ||
#endif // PLANNER_PLANNERS_PLANNER_H_ |
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,64 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#include "planner/planners/SequentialPlanner.h" | ||
|
||
#include "parser/Sentence.h" | ||
#include "planner/Logic.h" | ||
#include "planner/Query.h" | ||
#include "validator/SequentialValidator.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
SequentialPlannerRegister SequentialPlannerRegister::instance_; | ||
|
||
bool SequentialPlanner::match(AstContext* astCtx) { | ||
if (astCtx->sentence->kind() == Sentence::Kind::kSequential) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
StatusOr<SubPlan> SequentialPlanner::transform(AstContext* astCtx) { | ||
SubPlan subPlan; | ||
auto* seqCtx = static_cast<SequentialAstContext*>(astCtx); | ||
auto* qctx = seqCtx->qctx; | ||
const auto& validators = seqCtx->validators; | ||
subPlan.root = validators.back()->root(); | ||
ifBuildDataCollect(subPlan, qctx); | ||
for (auto iter = validators.begin(); iter < validators.end() - 1; ++iter) { | ||
NG_RETURN_IF_ERROR((iter + 1)->get()->appendPlan(iter->get()->root())); | ||
} | ||
subPlan.tail = StartNode::make(qctx); | ||
NG_RETURN_IF_ERROR(validators.front()->appendPlan(subPlan.tail)); | ||
VLOG(1) << "root: " << subPlan.root->kind() << " tail: " << subPlan.tail->kind(); | ||
return subPlan; | ||
} | ||
|
||
void SequentialPlanner::ifBuildDataCollect(SubPlan& subPlan, QueryContext* qctx) { | ||
switch (subPlan.root->kind()) { | ||
case PlanNode::Kind::kSort: | ||
case PlanNode::Kind::kLimit: | ||
case PlanNode::Kind::kDedup: | ||
case PlanNode::Kind::kUnion: | ||
case PlanNode::Kind::kIntersect: | ||
case PlanNode::Kind::kMinus: | ||
case PlanNode::Kind::kFilter: { | ||
auto* dc = DataCollect::make(qctx, | ||
subPlan.root, | ||
DataCollect::CollectKind::kRowBasedMove, | ||
{subPlan.root->outputVar()}); | ||
dc->setColNames(subPlan.root->colNames()); | ||
subPlan.root = dc; | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} | ||
} // namespace graph | ||
} // 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,46 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#ifndef PLANNER_PLANNERS_SEQUENTIALPLANNER_H_ | ||
#define PLANNER_PLANNERS_SEQUENTIALPLANNER_H_ | ||
|
||
#include "planner/Planner.h" | ||
#include "context/QueryContext.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
class SequentialPlanner final : public Planner { | ||
public: | ||
static std::unique_ptr<SequentialPlanner> make() { | ||
return std::unique_ptr<SequentialPlanner>(new SequentialPlanner()); | ||
} | ||
|
||
static bool match(AstContext* astCtx); | ||
|
||
/** | ||
* Each sentence would be converted to a sub-plan, and they would | ||
* be cascaded together into a complete execution plan. | ||
*/ | ||
StatusOr<SubPlan> transform(AstContext* astCtx) override; | ||
|
||
void ifBuildDataCollect(SubPlan& subPlan, QueryContext* qctx); | ||
|
||
private: | ||
SequentialPlanner() = default; | ||
}; | ||
|
||
class SequentialPlannerRegister final { | ||
private: | ||
SequentialPlannerRegister() { | ||
auto& planners = Planner::plannersMap()[Sentence::Kind::kSequential]; | ||
planners.emplace_back(&SequentialPlanner::match, &SequentialPlanner::make); | ||
} | ||
|
||
static SequentialPlannerRegister instance_; | ||
}; | ||
} // namespace graph | ||
} // namespace nebula | ||
#endif // PLANNER_PLANNERS_SEQUENTIALPLANNER_H_ |
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
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.