Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Apr 26, 2022
1 parent 204b001 commit 63c2e49
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/graph/context/ast/CypherAstContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct Path final {
// "(v)-[:like]->()" in (v)-[:like]->()
std::string collectVariable;

enum PathType { kDefault, kAllShortest, kSingleShortest };
enum PathType : int8_t { kDefault, kAllShortest, kSingleShortest };
PathType pathType{PathType::kDefault};
};

Expand Down Expand Up @@ -215,8 +215,8 @@ struct NodeContext final : PatternContext {
QueryContext* qctx;
Expression* bindFilter;
GraphSpaceID spaceId;
NodeInfo* info{nullptr};
std::unordered_set<std::string>* nodeAliasesAvailable;
NodeInfo* info;
std::unordered_set<std::string>* nodeAliasesAvailable{nullptr};

// Output fields
ScanInfo scanInfo;
Expand All @@ -232,7 +232,7 @@ struct EdgeContext final : PatternContext {
QueryContext* qctx;
Expression* bindFilter;
GraphSpaceID spaceId;
EdgeInfo* info{nullptr};
EdgeInfo* info;

// Output fields
ScanInfo scanInfo;
Expand Down
12 changes: 8 additions & 4 deletions src/graph/executor/algo/ShortestPathExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace graph {
folly::Future<Status> ShortestPathExecutor::execute() {
SCOPED_TIMER(&execTime_);
single_ = pathNode_->singleShortest();
range_ = {pathNode_->stepRange()->min(), pathNode_->stepRange()->max()};
maxStep_ = pathNode_->stepRange()->max();

auto& colNames = pathNode_->colNames();
auto rowSize = buildRequestDataSet();
std::vector<folly::Future<Status>> futures;
Expand Down Expand Up @@ -51,9 +52,10 @@ size_t ShortestPathExecutor::buildRequestDataSet() {
auto start = iter->getColumn(0);
auto end = iter->getColumn(1);
if (!SchemaUtil::isValidVid(start, vidType) || !SchemaUtil::isValidVid(end, vidType)) {
LOG(ERROR) << "Mismatched vid type. start type : " << start.type()
LOG(ERROR) << "Mismatched shortestPath vid type. start type : " << start.type()
<< ", end type: " << end.type()
<< ", space vid type: " << SchemaUtil::typeToString(vidType);
rowSize--;
continue;
}
if (start == end) {
Expand Down Expand Up @@ -139,7 +141,9 @@ folly::Future<Status> ShortestPathExecutor::handleResponse(size_t rowNum, size_t
return Status::OK();
}
stepNum++;
if (stepNum * 2 >= range_.second) {
auto& leftVids = leftVids_[rowNum].rows;
auto& rightVids = rightVids_[rowNum].rows;
if (stepNum * 2 >= maxStep_ || leftVids.empty() || rightVids.empty()) {
return Status::OK();
}
return shortestPath(rowNum, stepNum);
Expand All @@ -158,7 +162,7 @@ bool ShortestPathExecutor::conjunctPath(size_t rowNum, size_t stepNum) {
buildOddPath(rowNum, meetVids);
return true;
}
if (stepNum * 2 >= range_.second) {
if (stepNum * 2 >= maxStep_) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/graph/executor/algo/ShortestPathExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define GRAPH_EXECUTOR_QUERY_SHORTESTPATHEXECUTOR_H_

#include "graph/executor/StorageAccessExecutor.h"
#include "graph/planner/plan/Query.h"
#include "graph/planner/plan/Algo.h"

using nebula::storage::StorageRpcResponse;
using nebula::storage::cpp2::GetNeighborsResponse;
Expand Down Expand Up @@ -52,7 +52,7 @@ class ShortestPathExecutor final : public StorageAccessExecutor {

private:
const ShortestPath* pathNode_{nullptr};
std::pair<size_t, size_t> range_;
size_t maxStep_;
bool single_{true};

std::vector<DataSet> resultDs_;
Expand Down
6 changes: 3 additions & 3 deletions src/graph/planner/match/MatchClausePlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ StatusOr<SubPlan> MatchClausePlanner::transform(CypherClauseContextBase* clauseC
// TODO: Maybe it is better to rebuild the graph and find all connected components.
auto& pathInfos = matchClauseCtx->paths;
for (auto iter = pathInfos.begin(); iter < pathInfos.end(); ++iter) {
auto& nodeInfos = iter->nodeInfos;
auto bindFilter = matchClauseCtx->where ? matchClauseCtx->where->filter : nullptr;
SubPlan pathPlan;
if (iter->type != PATH::Type::kDefault) {
if (iter->pathType == Path::PathType::kDefault) {
MatchPathPlanner matchPathPlanner;
auto result = matchPathPlanner.transform(matchClauseCtx->qctx,
matchClauseCtx->space.id,
bindFilter,
matchClauseCtx->aliasesAvailable,
nodeAliasesSeen,
*iter);
NG_RETURN_IF_ERROF(result);
NG_RETURN_IF_ERROR(result);
pathPlan = std::move(result).value();
} else {
ShortestPathPlanner shortestPathPlanner;
Expand All @@ -49,7 +50,6 @@ StatusOr<SubPlan> MatchClausePlanner::transform(CypherClauseContextBase* clauseC
NG_RETURN_IF_ERROR(result);
pathPlan = std::move(result).value();
}
auto& nodeInfos = iter->nodeInfos;
NG_RETURN_IF_ERROR(
connectPathPlan(nodeInfos, matchClauseCtx, pathPlan, nodeAliasesSeen, matchClausePlan));
}
Expand Down
61 changes: 44 additions & 17 deletions src/graph/planner/match/ShortestPathPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

#include "graph/planner/match/ShortestPathPlanner.h"

#include "graph/context/ast/CypherAstContext.h"
#include "graph/planner/match/MatchSolver.h"
#include "graph/planner/match/SegmentsConnector.h"
#include "graph/planner/match/StartVidFinder.h"
#include "graph/planner/plan/Algo.h"
#include "graph/planner/plan/Logic.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/SchemaUtil.h"

namespace nebula {
namespace graph {
// Match (start:tagName{propName:xxx}), (end:tagName{propName:yyy})
Expand Down Expand Up @@ -74,11 +84,11 @@ static std::unique_ptr<std::vector<storage::cpp2::EdgeProp>> genEdgeProps(const
return edgeProps;
}

static YieldColumn* buildVertexColumn(ObjectPool* pool, const std::string& alias) const {
static YieldColumn* buildVertexColumn(ObjectPool* pool, const std::string& alias) {
return new YieldColumn(InputPropertyExpression::make(pool, alias), alias);
}

static YieldColumn* buildEdgeColumn(ObjectPool* pool, EdgeInfo& edge) const {
static YieldColumn* buildEdgeColumn(ObjectPool* pool, EdgeInfo& edge) {
Expression* expr = nullptr;
if (edge.range == nullptr) {
expr = SubscriptExpression::make(
Expand All @@ -93,7 +103,7 @@ static YieldColumn* buildEdgeColumn(ObjectPool* pool, EdgeInfo& edge) const {
return new YieldColumn(expr, edge.alias);
}

static YieldColumn* buildPathColumn(Expression* pathBuild, const std::string& alias) const {
static YieldColumn* buildPathColumn(Expression* pathBuild, const std::string& alias) {
return new YieldColumn(pathBuild, alias);
}

Expand All @@ -103,14 +113,14 @@ static void buildProjectColumns(QueryContext* qctx, Path& path, SubPlan& plan) {
auto& nodeInfos = path.nodeInfos;
auto& edgeInfos = path.edgeInfos;

auto addNode = [this, columns, &colNames, qctx](auto& nodeInfo) {
auto addNode = [columns, &colNames, qctx](auto& nodeInfo) {
if (!nodeInfo.alias.empty() && !nodeInfo.anonymous) {
columns->addColumn(buildVertexColumn(qctx->objPool(), nodeInfo.alias));
colNames.emplace_back(nodeInfo.alias);
}
};

auto addEdge = [this, columns, &colNames, qctx](auto& edgeInfo) {
auto addEdge = [columns, &colNames, qctx](auto& edgeInfo) {
if (!edgeInfo.alias.empty() && !edgeInfo.anonymous) {
columns->addColumn(buildEdgeColumn(qctx->objPool(), edgeInfo));
colNames.emplace_back(edgeInfo.alias);
Expand Down Expand Up @@ -145,26 +155,42 @@ StatusOr<SubPlan> ShortestPathPlanner::transform(
const std::unordered_map<std::string, AliasType>& aliasesAvailable,
std::unordered_set<std::string> nodeAliasesSeen,
Path& path) {
UNUSED(aliasesAvailable);
UNUSED(nodeAliasesSeen);
std::unordered_set<std::string> allNodeAliasesAvailable;
allNodeAliasesAvailable.merge(nodeAliasesSeen);
std::for_each(
aliasesAvailable.begin(), aliasesAvailable.end(), [&allNodeAliasesAvailable](auto& kv) {
if (kv.second == AliasType::kNode) {
allNodeAliasesAvailable.emplace(kv.first);
}
});

SubPlan subplan;
bool singleShortest = path.pathType == MatchPath::PathType::kSingleShortest;
bool singleShortest = path.pathType == Path::PathType::kSingleShortest;
auto& nodeInfos = path.nodeInfos;
auto& edge = path.edgeInfos.front();

auto& startVidFinders = StartVidFinder::finders();
std::vector<SubPlan> plans;

for (size_t i = 0; i < nodeInfos.size(); ++i) {
for (auto& nodeInfo : nodeInfos) {
bool foundIndex = false;
for (auto& finder : startVidFinders) {
auto nodeCtx = NodeContext(qctx, bindFilter, spaceId, &nodeInfos[i]);
auto nodeCtx = NodeContext(qctx, bindFilter, spaceId, &nodeInfo);
nodeCtx.nodeAliasesAvailable = &allNodeAliasesAvailable;
auto nodeFinder = finder();
if (nodeFinder->match(&nodeCtx)) {
auto plan = nodeFinder->transform(&nodeCtx);
NG_RETURN_IF_ERROR(plan);
plans.emplace_back(std::move(plan).value());
auto status = nodeFinder->transform(&nodeCtx);
NG_RETURN_IF_ERROR(status);
auto plan = status.value();
auto start = StartNode::make(qctx);
plan.tail->setDep(0, start);
plan.tail = start;

auto initExpr = nodeCtx.initialExpr->clone();
auto columns = qctx->objPool()->makeAndAdd<YieldColumns>();
columns->addColumn(new YieldColumn(initExpr, nodeInfo.alias));
plan.root = Project::make(qctx, plan.root, columns);

plans.emplace_back(std::move(plan));
foundIndex = true;
break;
}
Expand All @@ -173,24 +199,25 @@ StatusOr<SubPlan> ShortestPathPlanner::transform(
return Status::SemanticError("Can't find index from path pattern");
}
}

auto& leftPlan = plans.front();
auto& rightPlan = plans.back();

auto cp = BiCartesianProduct::make(qctx, leftPlan.root, rightPlan.root);

auto& edge = path.edgeInfos.front();
auto shortestPath = ShortestPath::make(qctx, cp, spaceId, singleShortest);
auto vertexProp = genVertexProps(nodeInfos.front(), qctx, spaceId);
NG_RETURN_IF_ERROR(vertexProp);
shortestPath->setVertexProps(std::move(vertexProp).value());
shortestPath->setEdgeProps(genEdgeProps(edge, false, qctx, spaceId));
shortestPath->setReverseEdgeProps(getEdgeProps(edge, true, qctx, spaceId));
shortestPath->setReverseEdgeProps(genEdgeProps(edge, true, qctx, spaceId));
shortestPath->setEdgeDirection(edge.direction);
shortestPath->setStepRange(edge.range);

subplan.root = shortestPath;
subplan.tail = leftPlan.tail;

bulildProjectColumns(qctx, path, subplan);
buildProjectColumns(qctx, path, subplan);

return subplan;
}
Expand Down
3 changes: 1 addition & 2 deletions src/graph/planner/plan/Algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ std::unique_ptr<PlanNodeDescription> ShortestPath::explain() const {
}

PlanNode* ShortestPath::clone() const {
auto* path = ShortestPath::make(qctx_, nullptr, space_, singleShorest_);
auto* path = ShortestPath::make(qctx_, nullptr, space_, singleShortest_);
path->cloneMembers(*this);
return path;
}

void ShortestPath::cloneMembers(const ShortestPath& path) {
SingleInputNode::cloneMembers(path);
setSingle(path.singleShortest_);
setStepRange(path.range_);
setEdgeDirection(path.edgeDirection_);
if (path.vertexProps_) {
Expand Down
8 changes: 6 additions & 2 deletions src/graph/planner/plan/Algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ class ProduceAllPaths final : public BinaryInputNode {
std::string rightVidVar_;
};

using VertexProp = nebula::storage::cpp2::VertexProp;
using EdgeProp = nebula::storage::cpp2::EdgeProp;
using Direction = nebula::storage::cpp2::EdgeDirection;

class ShortestPath final : public SingleInputNode {
public:
static ShortestPath* make(QueryContext* qctx,
Expand Down Expand Up @@ -182,7 +186,7 @@ class ShortestPath final : public SingleInputNode {
return space_;
}

bool singleShorest() const {
bool singleShortest() const {
return singleShortest_;
}

Expand Down Expand Up @@ -210,7 +214,7 @@ class ShortestPath final : public SingleInputNode {
ShortestPath(QueryContext* qctx, PlanNode* node, GraphSpaceID space, bool singleShortest)
: SingleInputNode(qctx, Kind::kShortestPath, node),
space_(space),
singleShorest_(singleShorest) {}
singleShortest_(singleShortest) {}

void cloneMembers(const ShortestPath&);

Expand Down
6 changes: 3 additions & 3 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ Status MatchValidator::buildPathExpr(const MatchPath *path,
return Status::SemanticError(
"`shortestPath(...)' only support pattern like (start)-[edge*..hop]-(end)");
}
auto &edge = edgeInfos.front();
if (edge.range->min() != 0 || edge.range->min() != 1) {
auto min = edgeInfos.front().range->min();
if (min != 0 && min != 1) {
return Status::SemanticError(
"`shortestPath(...)' does not support a minimal length different from 0 or 1");
}
pathInfo.pathType = static_cast<Path::PathType>(pathType);
}

auto *pool = qctx_->objPool();
Expand All @@ -173,7 +174,6 @@ Status MatchValidator::buildPathExpr(const MatchPath *path,
pathInfo.pathBuild = std::move(pathBuild);
pathInfo.anonymous = false;
pathInfo.alias = *pathAlias;
pathInfo.pathType = pathType;
return Status::OK();
}

Expand Down
3 changes: 0 additions & 3 deletions src/graph/validator/MatchValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class MatchValidator final : public Validator {

Status validateFilter(const Expression *filter, WhereClauseContext &whereClauseCtx);

Status validateShortestPath(const MatchPath *path);


Status validateReturn(MatchReturn *ret,
const std::vector<QueryPart> &queryParts,
ReturnClauseContext &retClauseCtx);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/MatchPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class MatchPath final {
return edges_[i].get();
}

enum PathType { kDefault, kAllShortest, kSingleShortest };
enum PathType : int8_t { kDefault, kAllShortest, kSingleShortest };

PathType pathType() const {
return pathType_;
Expand Down

0 comments on commit 63c2e49

Please sign in to comment.