Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Apr 27, 2022
1 parent b76246a commit 0377451
Show file tree
Hide file tree
Showing 11 changed files with 942 additions and 190 deletions.
41 changes: 21 additions & 20 deletions src/graph/executor/algo/ShortestPathExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace nebula {
namespace graph {
folly::Future<Status> ShortestPathExecutor::execute() {
SCOPED_TIMER(&execTime_);
single_ = pathNode_->singleShortest();
singleShortest_ = pathNode_->singleShortest();
maxStep_ = pathNode_->stepRange()->max();

auto& colNames = pathNode_->colNames();
auto rowSize = buildRequestDataSet();
std::vector<folly::Future<Status>> futures;
for (size_t rowNum = 0; rowNum < rowSize; ++rowNum) {
resultDs_[rowNum].colNames = colNames;
futures.emplace_back(shortestPath(rowNum, 0));
futures.emplace_back(shortestPath(rowNum, 1));
}
return folly::collect(futures).via(runner()).thenValue([this, &colNames](auto&& resps) {
for (auto& resp : resps) {
Expand Down Expand Up @@ -60,6 +60,7 @@ size_t ShortestPathExecutor::buildRequestDataSet() {
}
if (start == end) {
// continue or return error
rowSize--;
continue;
}
if (uniqueSet.emplace(std::pair<Value, Value>{start, end}).second) {
Expand All @@ -81,7 +82,6 @@ size_t ShortestPathExecutor::buildRequestDataSet() {
}

folly::Future<Status> ShortestPathExecutor::shortestPath(size_t rowNum, size_t stepNum) {
VLOG(1) << "current rowNum is : " << rowNum << " step is :" << stepNum;
std::vector<folly::Future<Status>> futures;
futures.emplace_back(getNeighbors(rowNum, false));
futures.emplace_back(getNeighbors(rowNum, true));
Expand All @@ -96,11 +96,6 @@ folly::Future<Status> ShortestPathExecutor::shortestPath(size_t rowNum, size_t s
}

folly::Future<Status> ShortestPathExecutor::getNeighbors(size_t rowNum, bool reverse) {
if (reverse) {
VLOG(1) << "reverse GetNeightbor input : " << rightVids_[rowNum].toString();
} else {
VLOG(1) << "GetNeightbor input : " << leftVids_[rowNum].toString();
}
StorageClient* storageClient = qctx_->getStorageClient();
time::Duration getNbrTime;
storage::StorageClient::CommonRequestParam param(pathNode_->space(),
Expand Down Expand Up @@ -140,22 +135,24 @@ folly::Future<Status> ShortestPathExecutor::handleResponse(size_t rowNum, size_t
if (conjunctPath(rowNum, stepNum)) {
return Status::OK();
}
stepNum++;
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);
return shortestPath(rowNum, ++stepNum);
}

bool ShortestPathExecutor::conjunctPath(size_t rowNum, size_t stepNum) {
const auto& leftStep = allLeftSteps_[rowNum].back();
const auto& prevRightStep = allRightSteps_[rowNum][stepNum];
const auto& prevRightStep = allRightSteps_[rowNum][stepNum - 1];
std::vector<Value> meetVids;
for (const auto& step : leftStep) {
if (prevRightStep.find(step.first) != prevRightStep.end()) {
meetVids.push_back(step.first);
if (singleShortest_) {
break;
}
}
}
if (!meetVids.empty()) {
Expand All @@ -170,6 +167,9 @@ bool ShortestPathExecutor::conjunctPath(size_t rowNum, size_t stepNum) {
for (const auto& step : leftStep) {
if (rightStep.find(step.first) != rightStep.end()) {
meetVids.push_back(step.first);
if (singleShortest_) {
break;
}
}
}
if (meetVids.empty()) {
Expand All @@ -191,11 +191,6 @@ Status ShortestPathExecutor::buildPath(size_t rowNum, RpcResponse&& resps, bool
}
list.values.emplace_back(std::move(*dataset));
}
if (reverse) {
VLOG(1) << "reverse GetNeightbor output: " << list.toString().c_str();
} else {
VLOG(1) << "GetNeightbor output: " << list.toString().c_str();
}
auto listVal = std::make_shared<Value>(std::move(list));
auto iter = std::make_unique<GetNeighborsIter>(listVal);
return doBuildPath(rowNum, iter.get(), reverse);
Expand Down Expand Up @@ -246,10 +241,8 @@ Status ShortestPathExecutor::doBuildPath(size_t rowNum, GetNeighborsIter* iter,
std::make_move_iterator(uniqueDst.end()));
if (reverse) {
rightVids_[rowNum].rows.swap(nextStepVids);
VLOG(1) << "reverse next Vid : " << rightVids_[rowNum].toString() << " rowNum" << rowNum;
} else {
leftVids_[rowNum].rows.swap(nextStepVids);
VLOG(1) << "current next Vid : " << leftVids_[rowNum].toString() << " rowNum " << rowNum;
}
return Status::OK();
}
Expand All @@ -262,8 +255,12 @@ void ShortestPathExecutor::buildOddPath(size_t rowNum, const std::vector<Value>&
for (auto& rightPath : rightPaths) {
Row path = leftPath;
auto& steps = path.values.back().mutableList().values;
steps.insert(steps.end(), rightPath.values.begin(), rightPath.values.end());
steps.insert(steps.end(), rightPath.values.begin(), rightPath.values.end() - 1);
path.emplace_back(rightPath.values.back());
resultDs_[rowNum].rows.emplace_back(std::move(path));
if (singleShortest_) {
return;
}
}
}
}
Expand All @@ -284,8 +281,12 @@ bool ShortestPathExecutor::buildEvenPath(size_t rowNum, const std::vector<Value>
Row path = leftPath;
auto& steps = path.values.back().mutableList().values;
steps.emplace_back(meetVertex);
steps.insert(steps.end(), rightPath.values.begin(), rightPath.values.end());
steps.insert(steps.end(), rightPath.values.begin(), rightPath.values.end() - 1);
path.emplace_back(rightPath.values.back());
resultDs_[rowNum].rows.emplace_back(std::move(path));
if (singleShortest_) {
return true;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/executor/algo/ShortestPathExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ShortestPathExecutor final : public StorageAccessExecutor {
private:
const ShortestPath* pathNode_{nullptr};
size_t maxStep_;
bool single_{true};
bool singleShortest_{true};

std::vector<DataSet> resultDs_;
std::vector<DataSet> leftVids_;
Expand Down
1 change: 1 addition & 0 deletions src/graph/executor/query/TraverseExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Status TraverseExecutor::buildResult() {
std::move(paths.second.begin(), paths.second.end(), std::back_inserter(result.rows));
}
}
VLOG(1) << "jmq result is " << result;

return finish(ResultBuilder().value(Value(std::move(result))).build());
}
Expand Down
19 changes: 11 additions & 8 deletions src/graph/planner/match/ShortestPathPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ static YieldColumn* buildEdgeColumn(ObjectPool* pool, EdgeInfo& edge) {
return new YieldColumn(expr, edge.alias);
}

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

static void buildProjectColumns(QueryContext* qctx, Path& path, SubPlan& plan) {
auto columns = qctx->objPool()->makeAndAdd<YieldColumns>();
auto& objPool = qctx->objPool();

auto columns = objPool->makeAndAdd<YieldColumns>();
std::vector<std::string> colNames;
auto& nodeInfos = path.nodeInfos;
auto& edgeInfos = path.edgeInfos;
Expand Down Expand Up @@ -138,7 +137,7 @@ static void buildProjectColumns(QueryContext* qctx, Path& path, SubPlan& plan) {

if (!path.anonymous) {
DCHECK(!path.alias.empty());
columns->addColumn(buildPathColumn(DCHECK_NOTNULL(path.pathBuild), path.alias));
columns->addColumn(YieldColumn(DCHECK_NOTNULL(path.pathBuild), path.alias));
colNames.emplace_back(path.alias);
}

Expand Down Expand Up @@ -167,6 +166,11 @@ StatusOr<SubPlan> ShortestPathPlanner::transform(
SubPlan subplan;
bool singleShortest = path.pathType == Path::PathType::kSingleShortest;
auto& nodeInfos = path.nodeInfos;
auto& edge = path.edgeInfos.front();
std::vector<std::string> colNames;
colNames.emplace_back(nodeInfos.front().alias);
colNames.emplace_back(edge.alias);
colNames.emplace_back(nodeInfos.back().alias);

auto& startVidFinders = StartVidFinder::finders();
std::vector<SubPlan> plans;
Expand Down Expand Up @@ -196,15 +200,14 @@ StatusOr<SubPlan> ShortestPathPlanner::transform(
}
}
if (!foundIndex) {
return Status::SemanticError("Can't find index from path pattern");
return Status::SemanticError("Can't find index from shortestPath 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);
Expand All @@ -213,12 +216,12 @@ StatusOr<SubPlan> ShortestPathPlanner::transform(
shortestPath->setReverseEdgeProps(genEdgeProps(edge, true, qctx, spaceId));
shortestPath->setEdgeDirection(edge.direction);
shortestPath->setStepRange(edge.range);
shortestPath->setColNames(std::move(colNames));

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

buildProjectColumns(qctx, path, subplan);

return subplan;
}

Expand Down
15 changes: 7 additions & 8 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ Status MatchValidator::validatePath(const MatchPath *path, Path &pathInfo) {
Status MatchValidator::buildPathExpr(const MatchPath *path,
Path &pathInfo,
std::unordered_map<std::string, AliasType> &aliasesGenerated) {
auto *pathAlias = path->alias();
if (pathAlias == nullptr) {
return Status::OK();
}
if (!aliasesGenerated.emplace(*pathAlias, AliasType::kPath).second) {
return Status::SemanticError("`%s': Redefined alias", pathAlias->c_str());
}

auto &nodeInfos = pathInfo.nodeInfos;
auto &edgeInfos = pathInfo.edgeInfos;

Expand All @@ -164,6 +156,13 @@ Status MatchValidator::buildPathExpr(const MatchPath *path,
pathInfo.pathType = static_cast<Path::PathType>(pathType);
}

auto *pathAlias = path->alias();
if (pathAlias == nullptr) {
return Status::OK();
}
if (!aliasesGenerated.emplace(*pathAlias, AliasType::kPath).second) {
return Status::SemanticError("`%s': Redefined alias", pathAlias->c_str());
}
auto *pool = qctx_->objPool();
auto pathBuild = PathBuildExpression::make(pool);
for (size_t i = 0; i < edgeInfos.size(); ++i) {
Expand Down
Loading

0 comments on commit 0377451

Please sign in to comment.