Skip to content

Commit

Permalink
Cleanup match planner (#4978)
Browse files Browse the repository at this point in the history
* Cleanup match planner implementation

* Restore segment connector return type
  • Loading branch information
yixinglu authored Dec 3, 2022
1 parent 9058610 commit 56d8089
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 342 deletions.
14 changes: 6 additions & 8 deletions src/graph/context/ast/CypherAstContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct CypherClauseContextBase : AstContext {
// Input column names of current clause
// Now used by unwind clause planner
std::vector<std::string> inputColNames;

std::unordered_map<std::string, AliasType> aliasesAvailable;
};

struct WhereClauseContext final : CypherClauseContextBase {
Expand All @@ -104,7 +106,6 @@ struct WhereClauseContext final : CypherClauseContextBase {
std::vector<Path> paths;

Expression* filter{nullptr};
std::unordered_map<std::string, AliasType> aliasesAvailable;
};

struct OrderByClauseContext final : CypherClauseContextBase {
Expand Down Expand Up @@ -134,7 +135,6 @@ struct YieldClauseContext final : CypherClauseContextBase {

bool distinct{false};
const YieldColumns* yieldColumns{nullptr};
std::unordered_map<std::string, AliasType> aliasesAvailable;

bool hasAgg_{false};
bool needGenProject_{false};
Expand Down Expand Up @@ -169,7 +169,6 @@ struct MatchClauseContext final : CypherClauseContextBase {
bool isOptional{false};
std::vector<Path> paths;
std::unique_ptr<WhereClauseContext> where;
std::unordered_map<std::string, AliasType> aliasesAvailable;
std::unordered_map<std::string, AliasType> aliasesGenerated;
};

Expand All @@ -182,7 +181,6 @@ struct UnwindClauseContext final : CypherClauseContextBase {
Expression* unwindExpr{nullptr};
std::string alias;

std::unordered_map<std::string, AliasType> aliasesAvailable;
std::unordered_map<std::string, AliasType> aliasesGenerated;
};

Expand All @@ -209,13 +207,13 @@ struct PatternContext {
};

struct NodeContext final : PatternContext {
NodeContext(QueryContext* q, WhereClauseContext* b, GraphSpaceID g, NodeInfo* i)
NodeContext(QueryContext* q, WhereClauseContext* b, GraphSpaceID g, const NodeInfo* i)
: PatternContext(PatternKind::kNode), qctx(q), bindWhereClause(b), spaceId(g), info(i) {}

QueryContext* qctx;
WhereClauseContext* bindWhereClause;
GraphSpaceID spaceId;
NodeInfo* info;
const NodeInfo* info;
std::unordered_set<std::string>* nodeAliasesAvailable{nullptr};

// Output fields
Expand All @@ -226,13 +224,13 @@ struct NodeContext final : PatternContext {
};

struct EdgeContext final : PatternContext {
EdgeContext(QueryContext* q, WhereClauseContext* b, GraphSpaceID g, EdgeInfo* i)
EdgeContext(QueryContext* q, WhereClauseContext* b, GraphSpaceID g, const EdgeInfo* i)
: PatternContext(PatternKind::kEdge), qctx(q), bindWhereClause(b), spaceId(g), info(i) {}

QueryContext* qctx;
WhereClauseContext* bindWhereClause;
GraphSpaceID spaceId;
EdgeInfo* info;
const EdgeInfo* info;

// Output fields
ScanInfo scanInfo;
Expand Down
49 changes: 19 additions & 30 deletions src/graph/planner/match/MatchClausePlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,13 @@ StatusOr<SubPlan> MatchClausePlanner::transform(CypherClauseContextBase* clauseC
auto& nodeInfos = iter->nodeInfos;
SubPlan pathPlan;
if (iter->pathType == Path::PathType::kDefault) {
MatchPathPlanner matchPathPlanner;
auto result = matchPathPlanner.transform(matchClauseCtx->qctx,
matchClauseCtx->space.id,
matchClauseCtx->where.get(),
matchClauseCtx->aliasesAvailable,
nodeAliasesSeen,
*iter);
MatchPathPlanner matchPathPlanner(matchClauseCtx, *iter);
auto result = matchPathPlanner.transform(matchClauseCtx->where.get(), nodeAliasesSeen);
NG_RETURN_IF_ERROR(result);
pathPlan = std::move(result).value();
} else {
ShortestPathPlanner shortestPathPlanner;
auto result = shortestPathPlanner.transform(matchClauseCtx->qctx,
matchClauseCtx->space.id,
matchClauseCtx->where.get(),
matchClauseCtx->aliasesAvailable,
nodeAliasesSeen,
*iter);
ShortestPathPlanner shortestPathPlanner(matchClauseCtx, *iter);
auto result = shortestPathPlanner.transform(matchClauseCtx->where.get(), nodeAliasesSeen);
NG_RETURN_IF_ERROR(result);
pathPlan = std::move(result).value();
}
Expand All @@ -61,28 +51,27 @@ Status MatchClausePlanner::connectPathPlan(const std::vector<NodeInfo>& nodeInfo
std::unordered_set<std::string>& nodeAliasesSeen,
SubPlan& matchClausePlan) {
std::unordered_set<std::string> intersectedAliases;
std::for_each(
nodeInfos.begin(), nodeInfos.end(), [&intersectedAliases, &nodeAliasesSeen](auto& info) {
if (nodeAliasesSeen.find(info.alias) != nodeAliasesSeen.end()) {
intersectedAliases.emplace(info.alias);
}
});
std::for_each(nodeInfos.begin(), nodeInfos.end(), [&nodeAliasesSeen](auto& info) {
for (auto& info : nodeInfos) {
if (nodeAliasesSeen.find(info.alias) != nodeAliasesSeen.end()) {
intersectedAliases.emplace(info.alias);
}
if (!info.anonymous) {
nodeAliasesSeen.emplace(info.alias);
}
});
}

if (matchClausePlan.root == nullptr) {
matchClausePlan = subplan;
return Status::OK();
}

if (intersectedAliases.empty()) {
matchClausePlan =
SegmentsConnector::cartesianProduct(matchClauseCtx->qctx, matchClausePlan, subplan);
} else {
if (intersectedAliases.empty()) {
matchClausePlan =
SegmentsConnector::cartesianProduct(matchClauseCtx->qctx, matchClausePlan, subplan);
} else {
// TODO: Actually a natural join would be much easy use.
matchClausePlan = SegmentsConnector::innerJoin(
matchClauseCtx->qctx, matchClausePlan, subplan, intersectedAliases);
}
// TODO: Actually a natural join would be much easy use.
matchClausePlan = SegmentsConnector::innerJoin(
matchClauseCtx->qctx, matchClausePlan, subplan, intersectedAliases);
}
return Status::OK();
}
Expand Down
Loading

0 comments on commit 56d8089

Please sign in to comment.