Skip to content

Commit

Permalink
Reusing functions in GetEdgesTransformRule
Browse files Browse the repository at this point in the history
  • Loading branch information
wey-gu committed Jun 9, 2022
1 parent bf5839b commit 4927b79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 101 deletions.
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/GetEdgesTransformRule.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/visitor/ExtractFilterExprVisitor.h"
Expand Down Expand Up @@ -112,14 +113,15 @@ StatusOr<OptRule::TransformResult> GetEdgesTransformAppendVerticesLimitRule::tra

newLimitGroupNode->dependsOn(newAppendVerticesGroup);

auto *newScanEdges = traverseToScanEdges(traverse, limit->count(qctx));
auto *newScanEdges = GetEdgesTransformRule::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 =
GetEdgesTransformRule::projectEdges(qctx, newScanEdges, traverse->colNames().back());
newProj->setInputVar(newScanEdges->outputVar());
newProj->setOutputVar(newAppendVertices->inputVar());
newProj->setColNames({traverse->colNames().back()});
Expand All @@ -142,46 +144,5 @@ std::string GetEdgesTransformAppendVerticesLimitRule::toString() const {
return "GetEdgesTransformAppendVerticesLimitRule";
}

/*static*/ graph::ScanEdges *GetEdgesTransformAppendVerticesLimitRule::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 *GetEdgesTransformAppendVerticesLimitRule::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
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ class GetEdgesTransformAppendVerticesLimitRule final : public OptRule {
private:
GetEdgesTransformAppendVerticesLimitRule();

static graph::ScanEdges *traverseToScanEdges(const graph::Traverse *traverse,
const int64_t limit_count);

static graph::Project *projectEdges(graph::QueryContext *qctx,
graph::PlanNode *input,
const std::string &colName);
Expand Down
42 changes: 0 additions & 42 deletions src/graph/optimizer/rule/GetEdgesTransformRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,47 +118,5 @@ std::string GetEdgesTransformRule::toString() const {
return "GetEdgesTransformRule";
}

/*static*/ graph::ScanEdges *GetEdgesTransformRule::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 *GetEdgesTransformRule::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
62 changes: 49 additions & 13 deletions src/graph/optimizer/rule/GetEdgesTransformRule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +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"

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

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

namespace opt {

Expand Down Expand Up @@ -71,15 +72,50 @@ class GetEdgesTransformRule final : public OptRule {

std::string toString() const override;

private:
GetEdgesTransformRule();

static graph::ScanEdges *traverseToScanEdges(const graph::Traverse *traverse,
const int64_t limit_count);
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,
graph::PlanNode *input,
const std::string &colName);
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();

static std::unique_ptr<OptRule> kInstance;
};
Expand Down

0 comments on commit 4927b79

Please sign in to comment.