Skip to content

Commit

Permalink
Addressing comment from Shylock-Hg & rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
wey-gu committed Jun 9, 2022
1 parent b137c07 commit bad018b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "common/expression/Expression.h"
#include "graph/optimizer/OptContext.h"
#include "graph/optimizer/OptGroup.h"
#include "graph/optimizer/rule/GetEdgesTransformRule.h"
#include "graph/optimizer/rule/GetEdgesTransformUtils.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/visitor/ExtractFilterExprVisitor.h"
Expand Down Expand Up @@ -113,15 +113,15 @@ StatusOr<OptRule::TransformResult> GetEdgesTransformAppendVerticesLimitRule::tra

newLimitGroupNode->dependsOn(newAppendVerticesGroup);

auto *newScanEdges = GetEdgesTransformRule::traverseToScanEdges(traverse, limit->count(qctx));
auto *newScanEdges = GetEdgesTransformUtils::traverseToScanEdges(traverse, limit->count(qctx));
if (newScanEdges == nullptr) {
return TransformResult::noTransform();
}
auto newScanEdgesGroup = OptGroup::create(ctx);
auto newScanEdgesGroupNode = newScanEdgesGroup->makeGroupNode(newScanEdges);

auto *newProj =
GetEdgesTransformRule::projectEdges(qctx, newScanEdges, traverse->colNames().back());
GetEdgesTransformUtils::projectEdges(qctx, newScanEdges, traverse->colNames().back());
newProj->setInputVar(newScanEdges->outputVar());
newProj->setOutputVar(newAppendVertices->inputVar());
newProj->setColNames({traverse->colNames().back()});
Expand Down
6 changes: 4 additions & 2 deletions src/graph/optimizer/rule/GetEdgesTransformRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common/expression/Expression.h"
#include "graph/optimizer/OptContext.h"
#include "graph/optimizer/OptGroup.h"
#include "graph/optimizer/rule/GetEdgesTransformUtils.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/visitor/ExtractFilterExprVisitor.h"
Expand Down Expand Up @@ -90,14 +91,15 @@ StatusOr<OptRule::TransformResult> GetEdgesTransformRule::transform(

newProjectGroupNode->dependsOn(newLimitGroup);

auto *newScanEdges = traverseToScanEdges(traverse, limit->count(qctx));
auto *newScanEdges = GetEdgesTransformUtils::traverseToScanEdges(traverse, limit->count(qctx));
if (newScanEdges == nullptr) {
return TransformResult::noTransform();
}
auto newScanEdgesGroup = OptGroup::create(ctx);
auto newScanEdgesGroupNode = newScanEdgesGroup->makeGroupNode(newScanEdges);

auto *newProj = projectEdges(qctx, newScanEdges, traverse->colNames().back());
auto *newProj =
GetEdgesTransformUtils::projectEdges(qctx, newScanEdges, traverse->colNames().back());
newProj->setInputVar(newScanEdges->outputVar());
newProj->setOutputVar(traverse->outputVar());
newProj->setColNames({traverse->colNames().back()});
Expand Down
57 changes: 7 additions & 50 deletions src/graph/optimizer/rule/GetEdgesTransformRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
#define GRAPH_OPTIMIZER_RULE_GETEDGESTRANSFORMRULE_H

#include "graph/optimizer/OptRule.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/visitor/ExtractFilterExprVisitor.h"

using nebula::graph::PlanNode;
using nebula::graph::Project;
using nebula::graph::ScanEdges;
using nebula::graph::Traverse;

namespace nebula {

namespace graph {
class ScanEdges;
class Project;
class Traverse;
class PlanNode;
} // namespace graph

namespace opt {

// Convert [[ScanVertices]] to [[ScanEdges]] in certain cases
Expand Down Expand Up @@ -72,48 +71,6 @@ class GetEdgesTransformRule final : public OptRule {

std::string toString() const override;

static graph::ScanEdges *traverseToScanEdges(const graph::Traverse *traverse,
const int64_t limit_count = -1) {
const auto *edgeProps = traverse->edgeProps();
if (edgeProps == nullptr) {
return nullptr;
}
for (std::size_t i = 0; i < edgeProps->size(); i++) {
auto type = (*edgeProps)[i].get_type();
for (std::size_t j = i + 1; j < edgeProps->size(); j++) {
if (type == -((*edgeProps)[j].get_type())) {
// Don't support to retrieve edges of the inbound/outbound together
return nullptr;
}
}
}
auto scanEdges = ScanEdges::make(
traverse->qctx(),
nullptr,
traverse->space(),
edgeProps == nullptr ? nullptr
: std::make_unique<std::vector<storage::cpp2::EdgeProp>>(*edgeProps),
nullptr,
traverse->dedup(),
limit_count,
{},
traverse->filter() == nullptr ? nullptr : traverse->filter()->clone());
return scanEdges;
}

static graph::Project *projectEdges(graph::QueryContext *qctx,
PlanNode *input,
const std::string &colName) {
auto *yieldColumns = qctx->objPool()->makeAndAdd<YieldColumns>();
auto *edgeExpr = EdgeExpression::make(qctx->objPool());
auto *listEdgeExpr = ListExpression::make(qctx->objPool());
listEdgeExpr->setItems({edgeExpr});
yieldColumns->addColumn(new YieldColumn(listEdgeExpr, colName));
auto project = Project::make(qctx, input, yieldColumns);
project->setColNames({colName});
return project;
}

private:
GetEdgesTransformRule();

Expand Down
70 changes: 70 additions & 0 deletions src/graph/optimizer/rule/GetEdgesTransformUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#ifndef GRAPH_OPTIMIZER_RULE_GETEDGESTRANSFORMUTILS_H
#define GRAPH_OPTIMIZER_RULE_GETEDGESTRANSFORMUTILS_H

#include "graph/optimizer/OptRule.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/visitor/ExtractFilterExprVisitor.h"

using nebula::graph::PlanNode;
using nebula::graph::Project;
using nebula::graph::ScanEdges;
using nebula::graph::Traverse;

namespace nebula {

namespace opt {

class GetEdgesTransformUtils final {
public:
static graph::ScanEdges *traverseToScanEdges(const graph::Traverse *traverse,
const int64_t limit_count = -1) {
const auto *edgeProps = traverse->edgeProps();
if (edgeProps == nullptr) {
return nullptr;
}
for (std::size_t i = 0; i < edgeProps->size(); i++) {
auto type = (*edgeProps)[i].get_type();
for (std::size_t j = i + 1; j < edgeProps->size(); j++) {
if (type == -((*edgeProps)[j].get_type())) {
// Don't support to retrieve edges of the inbound/outbound together
return nullptr;
}
}
}
auto scanEdges = ScanEdges::make(
traverse->qctx(),
nullptr,
traverse->space(),
edgeProps == nullptr ? nullptr
: std::make_unique<std::vector<storage::cpp2::EdgeProp>>(*edgeProps),
nullptr,
traverse->dedup(),
limit_count,
{},
traverse->filter() == nullptr ? nullptr : traverse->filter()->clone());
return scanEdges;
}

static graph::Project *projectEdges(graph::QueryContext *qctx,
PlanNode *input,
const std::string &colName) {
auto *yieldColumns = qctx->objPool()->makeAndAdd<YieldColumns>();
auto *edgeExpr = EdgeExpression::make(qctx->objPool());
auto *listEdgeExpr = ListExpression::make(qctx->objPool());
listEdgeExpr->setItems({edgeExpr});
yieldColumns->addColumn(new YieldColumn(listEdgeExpr, colName));
auto project = Project::make(qctx, input, yieldColumns);
project->setColNames({colName});
return project;
}
};

} // namespace opt
} // namespace nebula
#endif

0 comments on commit bad018b

Please sign in to comment.