Skip to content

Commit

Permalink
format fetchvertices (#2572)
Browse files Browse the repository at this point in the history
* format fetchvertices

* fix compiler error

* fix gv colnames

* add test case

* fix test error

* fix error

* address comment
  • Loading branch information
nevermore3 authored Sep 24, 2021
1 parent c2032e4 commit fd2e949
Show file tree
Hide file tree
Showing 40 changed files with 690 additions and 507 deletions.
10 changes: 10 additions & 0 deletions src/graph/context/ast/QueryAstContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ struct SubgraphContext final : public AstContext {
bool getEdgeProp{false};
};

struct FetchVerticesContext final : public AstContext {
Starts from;
bool distinct{false};
YieldColumns* yieldExpr{nullptr};
ExpressionProps exprProps;

// store the result of the previous sentence
std::string inputVarName;
};

} // namespace graph
} // namespace nebula
#endif // GRAPH_CONTEXT_AST_QUERYASTCONTEXT_H_
1 change: 1 addition & 0 deletions src/graph/planner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ nebula_add_library(
ngql/GoPlanner.cpp
ngql/SubgraphPlanner.cpp
ngql/LookupPlanner.cpp
ngql/FetchVerticesPlanner.cpp
)
5 changes: 5 additions & 0 deletions src/graph/planner/PlannersRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "graph/planner/match/PropIndexSeek.h"
#include "graph/planner/match/StartVidFinder.h"
#include "graph/planner/match/VertexIdSeek.h"
#include "graph/planner/ngql/FetchVerticesPlanner.h"
#include "graph/planner/ngql/GoPlanner.h"
#include "graph/planner/ngql/LookupPlanner.h"
#include "graph/planner/ngql/PathPlanner.h"
Expand Down Expand Up @@ -47,6 +48,10 @@ void PlannersRegister::registSequential() {
auto& planners = Planner::plannersMap()[Sentence::Kind::kGetSubgraph];
planners.emplace_back(&SubgraphPlanner::match, &SubgraphPlanner::make);
}
{
auto& planners = Planner::plannersMap()[Sentence::Kind::kFetchVertices];
planners.emplace_back(&FetchVerticesPlanner::match, &FetchVerticesPlanner::make);
}
}

void PlannersRegister::registMatch() {
Expand Down
69 changes: 69 additions & 0 deletions src/graph/planner/ngql/FetchVerticesPlanner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2021 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 "graph/planner/ngql/FetchVerticesPlanner.h"

#include "graph/planner/plan/Query.h"
#include "graph/util/PlannerUtil.h"

namespace nebula {
namespace graph {

std::unique_ptr<FetchVerticesPlanner::VertexProps> FetchVerticesPlanner::buildVertexProps(
const ExpressionProps::TagIDPropsMap& propsMap) {
if (propsMap.empty()) {
return nullptr;
}
auto vertexProps = std::make_unique<VertexProps>(propsMap.size());
auto fun = [](auto& tag) {
VertexProp vp;
vp.set_tag(tag.first);
std::vector<std::string> props(tag.second.begin(), tag.second.end());
vp.set_props(std::move(props));
return vp;
};
std::transform(propsMap.begin(), propsMap.end(), vertexProps->begin(), fun);
return vertexProps;
}

StatusOr<SubPlan> FetchVerticesPlanner::transform(AstContext* astCtx) {
fetchCtx_ = static_cast<FetchVerticesContext*>(astCtx);
auto qctx = fetchCtx_->qctx;
auto space = fetchCtx_->space;
auto& starts = fetchCtx_->from;

std::string vidsVar;
if (!starts.vids.empty() && starts.originalSrc == nullptr) {
PlannerUtil::buildConstantInput(qctx, starts, vidsVar);
} else {
starts.src = starts.originalSrc;
if (starts.fromType == kVariable) {
vidsVar = starts.userDefinedVarName;
} else {
vidsVar = fetchCtx_->inputVarName;
}
}

SubPlan subPlan;
auto* getVertices = GetVertices::make(qctx,
nullptr,
space.id,
starts.src,
buildVertexProps(fetchCtx_->exprProps.tagProps()),
{},
fetchCtx_->distinct);
getVertices->setInputVar(vidsVar);

subPlan.root = Project::make(qctx, getVertices, fetchCtx_->yieldExpr);
if (fetchCtx_->distinct) {
subPlan.root = Dedup::make(qctx, subPlan.root);
}
subPlan.tail = getVertices;
return subPlan;
}

} // namespace graph
} // namespace nebula
43 changes: 43 additions & 0 deletions src/graph/planner/ngql/FetchVerticesPlanner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Copyright (c) 2021 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 GRAPH_PLANNER_NGQL_FETCH_VERTICES_PLANNER_H_
#define GRAPH_PLANNER_NGQL_FETCH_VERTICES_PLANNER_H_

#include "common/base/Base.h"
#include "graph/context/ast/QueryAstContext.h"
#include "graph/planner/Planner.h"
#include "graph/planner/plan/PlanNode.h"

namespace nebula {
namespace graph {
class FetchVerticesPlanner final : public Planner {
public:
using VertexProp = nebula::storage::cpp2::VertexProp;
using VertexProps = std::vector<VertexProp>;

static std::unique_ptr<FetchVerticesPlanner> make() {
return std::unique_ptr<FetchVerticesPlanner>(new FetchVerticesPlanner());
}

static bool match(AstContext* astCtx) {
return astCtx->sentence->kind() == Sentence::Kind::kFetchVertices;
}

StatusOr<SubPlan> transform(AstContext* astCtx) override;

private:
std::unique_ptr<VertexProps> buildVertexProps(const ExpressionProps::TagIDPropsMap& propsMap);

private:
FetchVerticesPlanner() = default;

FetchVerticesContext* fetchCtx_{nullptr};
};
} // namespace graph
} // namespace nebula

#endif // GRAPH_PLANNER_NGQL_FETCH_VERTICES_PLANNER_H
11 changes: 4 additions & 7 deletions src/graph/planner/ngql/GoPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

#include "graph/planner/ngql/GoPlanner.h"

#include "graph/planner/plan/Algo.h"
#include "graph/planner/plan/Logic.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/QueryUtil.h"
#include "graph/util/SchemaUtil.h"
#include "graph/validator/Validator.h"
#include "graph/util/PlannerUtil.h"

namespace nebula {
namespace graph {
Expand Down Expand Up @@ -395,7 +392,7 @@ SubPlan GoPlanner::nStepsPlan(SubPlan& startVidPlan) {
gn->setEdgeProps(buildEdgeProps(true));
gn->setInputVar(goCtx_->vidsVar);

auto* getDst = QueryUtil::extractDstFromGN(qctx, gn, goCtx_->vidsVar);
auto* getDst = PlannerUtil::extractDstFromGN(qctx, gn, goCtx_->vidsVar);

PlanNode* loopBody = getDst;
PlanNode* loopDep = nullptr;
Expand Down Expand Up @@ -429,7 +426,7 @@ SubPlan GoPlanner::mToNStepsPlan(SubPlan& startVidPlan) {
gn->setEdgeProps(buildEdgeProps(false));
gn->setInputVar(goCtx_->vidsVar);

auto* getDst = QueryUtil::extractDstFromGN(qctx, gn, goCtx_->vidsVar);
auto* getDst = PlannerUtil::extractDstFromGN(qctx, gn, goCtx_->vidsVar);

auto* loopBody = getDst;
auto* loopDep = startVidPlan.root;
Expand Down Expand Up @@ -487,7 +484,7 @@ StatusOr<SubPlan> GoPlanner::transform(AstContext* astCtx) {
goCtx_->joinInput = goCtx_->from.fromType != FromType::kInstantExpr;
goCtx_->joinDst = !goCtx_->exprProps.dstTagProps().empty();

SubPlan startPlan = QueryUtil::buildStart(qctx, goCtx_->from, goCtx_->vidsVar);
SubPlan startPlan = PlannerUtil::buildStart(qctx, goCtx_->from, goCtx_->vidsVar);

auto& steps = goCtx_->steps;
if (steps.isMToN()) {
Expand Down
1 change: 0 additions & 1 deletion src/graph/planner/ngql/GoPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "graph/planner/Planner.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"

namespace nebula {
namespace graph {
Expand Down
8 changes: 4 additions & 4 deletions src/graph/planner/ngql/PathPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "graph/planner/plan/Algo.h"
#include "graph/planner/plan/Logic.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/QueryUtil.h"
#include "graph/util/PlannerUtil.h"
#include "graph/util/SchemaUtil.h"
#include "graph/validator/Validator.h"

Expand Down Expand Up @@ -62,15 +62,15 @@ void PathPlanner::doBuildEdgeProps(std::unique_ptr<std::vector<EdgeProp>>& edgeP
void PathPlanner::buildStart(Starts& starts, std::string& vidsVar, bool reverse) {
auto qctx = pathCtx_->qctx;
if (!starts.vids.empty() && starts.originalSrc == nullptr) {
QueryUtil::buildConstantInput(qctx, starts, vidsVar);
PlannerUtil::buildConstantInput(qctx, starts, vidsVar);
} else {
if (reverse) {
auto subPlan = QueryUtil::buildRuntimeInput(qctx, starts);
auto subPlan = PlannerUtil::buildRuntimeInput(qctx, starts);
pathCtx_->runtimeToProject = subPlan.tail;
pathCtx_->runtimeToDedup = subPlan.root;
vidsVar = pathCtx_->runtimeToDedup->outputVar();
} else {
auto subPlan = QueryUtil::buildRuntimeInput(qctx, starts);
auto subPlan = PlannerUtil::buildRuntimeInput(qctx, starts);
pathCtx_->runtimeFromProject = subPlan.tail;
pathCtx_->runtimeFromDedup = subPlan.root;
vidsVar = pathCtx_->runtimeFromDedup->outputVar();
Expand Down
4 changes: 2 additions & 2 deletions src/graph/planner/ngql/SubgraphPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "graph/planner/plan/Algo.h"
#include "graph/planner/plan/Logic.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/QueryUtil.h"
#include "graph/util/PlannerUtil.h"
#include "graph/util/SchemaUtil.h"
#include "graph/validator/Validator.h"

Expand Down Expand Up @@ -122,7 +122,7 @@ StatusOr<SubPlan> SubgraphPlanner::transform(AstContext* astCtx) {
auto qctx = subgraphCtx_->qctx;
std::string vidsVar;

SubPlan startPlan = QueryUtil::buildStart(qctx, subgraphCtx_->from, vidsVar);
SubPlan startPlan = PlannerUtil::buildStart(qctx, subgraphCtx_->from, vidsVar);
if (subgraphCtx_->steps.steps() == 0) {
return zeroStep(startPlan, vidsVar);
}
Expand Down
3 changes: 2 additions & 1 deletion src/graph/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ nebula_add_library(
ZoneUtil.cpp
ToJson.cpp
ParserUtil.cpp
QueryUtil.cpp
PlannerUtil.cpp
ValidateUtil.cpp
)

nebula_add_library(
Expand Down
13 changes: 8 additions & 5 deletions src/graph/util/QueryUtil.cpp → src/graph/util/PlannerUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#include "graph/util/QueryUtil.h"
#include "graph/util/PlannerUtil.h"

#include "common/base/Base.h"
#include "common/expression/ColumnExpression.h"
Expand All @@ -17,7 +17,7 @@ namespace nebula {
namespace graph {

// static
void QueryUtil::buildConstantInput(QueryContext* qctx, Starts& starts, std::string& vidsVar) {
void PlannerUtil::buildConstantInput(QueryContext* qctx, Starts& starts, std::string& vidsVar) {
vidsVar = qctx->vctx()->anonVarGen()->getVar();
DataSet ds;
ds.colNames.emplace_back(kVid);
Expand All @@ -33,7 +33,7 @@ void QueryUtil::buildConstantInput(QueryContext* qctx, Starts& starts, std::stri
}

// static
SubPlan QueryUtil::buildRuntimeInput(QueryContext* qctx, Starts& starts) {
SubPlan PlannerUtil::buildRuntimeInput(QueryContext* qctx, Starts& starts) {
auto pool = qctx->objPool();
auto* columns = pool->add(new YieldColumns());
auto* column = new YieldColumn(starts.originalSrc->clone(), kVid);
Expand All @@ -54,7 +54,7 @@ SubPlan QueryUtil::buildRuntimeInput(QueryContext* qctx, Starts& starts) {
}

// static
SubPlan QueryUtil::buildStart(QueryContext* qctx, Starts& starts, std::string& vidsVar) {
SubPlan PlannerUtil::buildStart(QueryContext* qctx, Starts& starts, std::string& vidsVar) {
SubPlan subPlan;
if (!starts.vids.empty() && starts.originalSrc == nullptr) {
buildConstantInput(qctx, starts, vidsVar);
Expand All @@ -65,7 +65,9 @@ SubPlan QueryUtil::buildStart(QueryContext* qctx, Starts& starts, std::string& v
return subPlan;
}

PlanNode* QueryUtil::extractDstFromGN(QueryContext* qctx, PlanNode* gn, const std::string& output) {
PlanNode* PlannerUtil::extractDstFromGN(QueryContext* qctx,
PlanNode* gn,
const std::string& output) {
auto pool = qctx->objPool();
auto* columns = pool->add(new YieldColumns());
auto* column = new YieldColumn(EdgePropertyExpression::make(pool, "*", kDst), kVid);
Expand All @@ -77,5 +79,6 @@ PlanNode* QueryUtil::extractDstFromGN(QueryContext* qctx, PlanNode* gn, const st
dedup->setOutputVar(output);
return dedup;
}

} // namespace graph
} // namespace nebula
10 changes: 5 additions & 5 deletions src/graph/util/QueryUtil.h → src/graph/util/PlannerUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/

#ifndef GRAPH_UTIL_QUERYUTIL_H_
#define GRAPH_UTIL_QUERYUTIL_H_
#ifndef GRAPH_UTIL_PLANNER_UTIL_H_
#define GRAPH_UTIL_PLANNER_UTIL_H_
#include "common/base/Base.h"

namespace nebula {
Expand All @@ -14,9 +14,9 @@ class QueryContext;
struct Starts;
struct SubPlan;
class PlanNode;
class QueryUtil final {
class PlannerUtil final {
public:
QueryUtil() = delete;
PlannerUtil() = delete;

static void buildConstantInput(QueryContext* qctx, Starts& starts, std::string& vidsVar);

Expand All @@ -29,4 +29,4 @@ class QueryUtil final {

} // namespace graph
} // namespace nebula
#endif // GRAPH_UTIL_ZONEUTIL_H_
#endif // GRAPH_UTIL_PLANNER_UTIL_H_
Loading

0 comments on commit fd2e949

Please sign in to comment.