forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchPathPlanner.cpp
347 lines (307 loc) · 12.9 KB
/
MatchPathPlanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2022 vesoft inc. All rights reserved.
//
// This source code is licensed under Apache 2.0 License.
#include "graph/planner/match/MatchPathPlanner.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/match/WhereClausePlanner.h"
#include "graph/planner/plan/Algo.h"
#include "graph/planner/plan/Logic.h"
#include "graph/planner/plan/PlanNode.h"
#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/SchemaUtil.h"
#include "graph/visitor/RewriteVisitor.h"
namespace nebula {
namespace graph {
MatchPathPlanner::MatchPathPlanner(CypherClauseContextBase* ctx, const Path& path)
: ctx_(DCHECK_NOTNULL(ctx)), path_(path) {}
static std::vector<std::string> genTraverseColNames(const std::vector<std::string>& inputCols,
const NodeInfo& node,
const EdgeInfo& edge,
bool trackPrev,
bool genPath = false) {
std::vector<std::string> cols;
if (trackPrev) {
cols = inputCols;
}
cols.emplace_back(node.alias);
cols.emplace_back(edge.alias);
if (genPath) {
cols.emplace_back(edge.innerAlias);
}
return cols;
}
static std::vector<std::string> genAppendVColNames(const std::vector<std::string>& inputCols,
const NodeInfo& node,
bool trackPrev) {
std::vector<std::string> cols;
if (trackPrev) {
cols = inputCols;
}
cols.emplace_back(node.alias);
return cols;
}
static Expression* genNextTraverseStart(ObjectPool* pool,
const EdgeInfo& edge,
const NodeInfo& node) {
auto args = ArgumentList::make(pool);
args->addArgument(InputPropertyExpression::make(pool, edge.alias));
args->addArgument(InputPropertyExpression::make(pool, node.alias));
return FunctionCallExpression::make(pool, "none_direct_dst", args);
}
static Expression* genVertexFilter(const NodeInfo& node) {
return node.filter;
}
static Expression* genEdgeFilter(const EdgeInfo& edge) {
return edge.filter;
}
static Expression* nodeId(ObjectPool* pool, const NodeInfo& node) {
return AttributeExpression::make(
pool, InputPropertyExpression::make(pool, node.alias), ConstantExpression::make(pool, kVid));
}
StatusOr<SubPlan> MatchPathPlanner::transform(WhereClauseContext* bindWhere,
std::unordered_set<std::string> nodeAliasesSeen) {
// All nodes ever seen in current match clause
// TODO: Maybe it is better to rebuild the graph and find all connected components.
SubPlan subplan;
size_t startIndex = 0;
bool startFromEdge = false;
NG_RETURN_IF_ERROR(findStarts(bindWhere, nodeAliasesSeen, startFromEdge, startIndex, subplan));
NG_RETURN_IF_ERROR(expand(startFromEdge, startIndex, subplan));
// No need to actually build path if the path is just a predicate
if (!path_.isPred) {
MatchSolver::buildProjectColumns(ctx_->qctx, path_, subplan);
}
return subplan;
}
Status MatchPathPlanner::findStarts(WhereClauseContext* bindWhereClause,
std::unordered_set<std::string> nodeAliasesSeen,
bool& startFromEdge,
size_t& startIndex,
SubPlan& matchClausePlan) {
auto& startVidFinders = StartVidFinder::finders();
bool foundStart = false;
std::for_each(
ctx_->aliasesAvailable.begin(), ctx_->aliasesAvailable.end(), [&nodeAliasesSeen](auto& kv) {
// if (kv.second == AliasType::kNode) {
nodeAliasesSeen.emplace(kv.first);
// }
});
auto spaceId = ctx_->space.id;
auto* qctx = ctx_->qctx;
const auto& nodeInfos = path_.nodeInfos;
const auto& edgeInfos = path_.edgeInfos;
// Find the start plan node
for (auto& finder : startVidFinders) {
for (size_t i = 0; i < nodeInfos.size() && !foundStart; ++i) {
NodeContext nodeCtx(qctx, bindWhereClause, spaceId, &nodeInfos[i]);
nodeCtx.aliasesAvailable = &nodeAliasesSeen;
auto nodeFinder = finder();
if (nodeFinder->match(&nodeCtx)) {
auto plan = nodeFinder->transform(&nodeCtx);
NG_RETURN_IF_ERROR(plan);
matchClausePlan = std::move(plan).value();
startIndex = i;
foundStart = true;
initialExpr_ = nodeCtx.initialExpr->clone();
VLOG(1) << "Find starts: " << startIndex << ", Pattern has " << edgeInfos.size()
<< " edges, root: " << matchClausePlan.root->outputVar()
<< ", colNames: " << folly::join(",", matchClausePlan.root->colNames());
break;
}
if (i != nodeInfos.size() - 1) {
EdgeContext edgeCtx(qctx, bindWhereClause, spaceId, &edgeInfos[i]);
auto edgeFinder = finder();
if (edgeFinder->match(&edgeCtx)) {
auto plan = edgeFinder->transform(&edgeCtx);
NG_RETURN_IF_ERROR(plan);
matchClausePlan = std::move(plan).value();
startFromEdge = true;
startIndex = i;
foundStart = true;
initialExpr_ = edgeCtx.initialExpr->clone();
break;
}
}
}
if (foundStart) {
break;
}
}
if (!foundStart) {
return Status::SemanticError("Can't solve the start vids from the sentence.");
}
// Both StartNode and Argument are leaf plan nodes
matchClausePlan.appendStartNode(qctx);
return Status::OK();
}
Status MatchPathPlanner::expand(bool startFromEdge, size_t startIndex, SubPlan& subplan) {
if (startFromEdge) {
return expandFromEdge(startIndex, subplan);
} else {
return expandFromNode(startIndex, subplan);
}
}
Status MatchPathPlanner::expandFromNode(size_t startIndex, SubPlan& subplan) {
const auto& nodeInfos = path_.nodeInfos;
DCHECK_LT(startIndex, nodeInfos.size());
// Vid of the start node is known already
nodeAliasesSeenInPattern_.emplace(nodeInfos[startIndex].alias);
if (startIndex == 0) {
// Pattern: (start)-[]-...-()
return rightExpandFromNode(startIndex, subplan);
}
if (startIndex == nodeInfos.size() - 1) {
// Pattern: ()-[]-...-(start)
return leftExpandFromNode(startIndex, subplan);
}
// Pattern: ()-[]-...-(start)-...-[]-()
NG_RETURN_IF_ERROR(rightExpandFromNode(startIndex, subplan));
NG_RETURN_IF_ERROR(leftExpandFromNode(startIndex, subplan));
return Status::OK();
}
Status MatchPathPlanner::leftExpandFromNode(size_t startIndex, SubPlan& subplan) {
const auto& nodeInfos = path_.nodeInfos;
const auto& edgeInfos = path_.edgeInfos;
Expression* nextTraverseStart = nullptr;
if (startIndex == nodeInfos.size() - 1) {
nextTraverseStart = initialExpr_;
} else {
auto* pool = ctx_->qctx->objPool();
auto args = ArgumentList::make(pool);
args->addArgument(InputPropertyExpression::make(pool, nodeInfos[startIndex].alias));
nextTraverseStart = FunctionCallExpression::make(pool, "_joinkey", args);
}
bool reversely = true;
auto qctx = ctx_->qctx;
auto spaceId = ctx_->space.id;
for (size_t i = startIndex; i > 0; --i) {
auto& node = nodeInfos[i];
auto& dst = nodeInfos[i - 1];
addNodeAlias(node);
bool expandInto = isExpandInto(dst.alias);
auto& edge = edgeInfos[i - 1];
MatchStepRange stepRange(1, 1);
if (edge.range != nullptr) {
stepRange = *edge.range;
}
auto traverse = Traverse::make(qctx, subplan.root, spaceId);
traverse->setSrc(nextTraverseStart);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
traverse->setVertexProps(std::move(vertexProps).value());
traverse->setEdgeProps(SchemaUtil::getEdgeProps(edge, reversely, qctx, spaceId));
traverse->setVertexFilter(genVertexFilter(node));
traverse->setTagFilter(genVertexFilter(node));
traverse->setEdgeFilter(genEdgeFilter(edge));
traverse->setEdgeDirection(edge.direction);
traverse->setStepRange(stepRange);
traverse->setDedup();
traverse->setGenPath(path_.genPath);
// If start from end of the path pattern, the first traverse would not
// track the previous path, otherwise, it should.
bool trackPrevPath = (startIndex + 1 == nodeInfos.size() ? i != startIndex : true);
traverse->setTrackPrevPath(trackPrevPath);
traverse->setColNames(
genTraverseColNames(subplan.root->colNames(), node, edge, trackPrevPath, path_.genPath));
subplan.root = traverse;
nextTraverseStart = genNextTraverseStart(qctx->objPool(), edge, node);
if (expandInto) {
// TODO(shylock) optimize to embed filter to Traverse
auto* startVid = nodeId(qctx->objPool(), dst);
auto* endVid = nextTraverseStart;
auto* filterExpr = RelationalExpression::makeEQ(qctx->objPool(), startVid, endVid);
auto* filter = Filter::make(qctx, traverse, filterExpr, false);
subplan.root = filter;
}
}
auto& lastNode = nodeInfos.front();
bool duppedLastAlias = isExpandInto(lastNode.alias) && nodeAliasesSeenInPattern_.size() > 1;
// If the the last alias has been presented in the pattern, we could emit the AppendVertices node
// because the same alias always presents in the same entity.
if (duppedLastAlias) {
return Status::OK();
}
auto appendV = AppendVertices::make(qctx, subplan.root, spaceId);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
appendV->setVertexProps(std::move(vertexProps).value());
appendV->setSrc(nextTraverseStart);
appendV->setVertexFilter(genVertexFilter(lastNode));
appendV->setDedup();
appendV->setTrackPrevPath(!edgeInfos.empty());
appendV->setColNames(genAppendVColNames(subplan.root->colNames(), lastNode, !edgeInfos.empty()));
subplan.root = appendV;
return Status::OK();
}
Status MatchPathPlanner::rightExpandFromNode(size_t startIndex, SubPlan& subplan) {
const auto& nodeInfos = path_.nodeInfos;
const auto& edgeInfos = path_.edgeInfos;
Expression* nextTraverseStart = initialExpr_;
bool reversely = false;
auto qctx = ctx_->qctx;
auto spaceId = ctx_->space.id;
for (size_t i = startIndex; i < edgeInfos.size(); ++i) {
auto& node = nodeInfos[i];
auto& dst = nodeInfos[i + 1];
addNodeAlias(node);
bool expandInto = isExpandInto(dst.alias);
auto& edge = edgeInfos[i];
MatchStepRange stepRange(1, 1);
if (edge.range != nullptr) {
stepRange = *edge.range;
}
auto traverse = Traverse::make(qctx, subplan.root, spaceId);
traverse->setSrc(nextTraverseStart);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
traverse->setVertexProps(std::move(vertexProps).value());
traverse->setEdgeProps(SchemaUtil::getEdgeProps(edge, reversely, qctx, spaceId));
traverse->setVertexFilter(genVertexFilter(node));
traverse->setTagFilter(genVertexFilter(node));
traverse->setEdgeFilter(genEdgeFilter(edge));
traverse->setEdgeDirection(edge.direction);
traverse->setStepRange(stepRange);
traverse->setDedup();
traverse->setTrackPrevPath(i != startIndex);
traverse->setGenPath(path_.genPath);
traverse->setColNames(
genTraverseColNames(subplan.root->colNames(), node, edge, i != startIndex, path_.genPath));
subplan.root = traverse;
nextTraverseStart = genNextTraverseStart(qctx->objPool(), edge, node);
if (expandInto) {
auto* startVid = nodeId(qctx->objPool(), dst);
auto* endVid = nextTraverseStart;
auto* filterExpr = RelationalExpression::makeEQ(qctx->objPool(), startVid, endVid);
auto* filter = Filter::make(qctx, traverse, filterExpr, false);
subplan.root = filter;
}
}
auto& lastNode = nodeInfos.back();
bool duppedLastAlias = isExpandInto(lastNode.alias) && nodeAliasesSeenInPattern_.size() > 1;
addNodeAlias(lastNode);
// If the the last alias has been presented in the pattern, we could emit the AppendVertices node
// because the same alias always presents in the same entity.
if (duppedLastAlias) {
return Status::OK();
}
auto appendV = AppendVertices::make(qctx, subplan.root, spaceId);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
appendV->setVertexProps(std::move(vertexProps).value());
appendV->setSrc(nextTraverseStart);
appendV->setVertexFilter(genVertexFilter(lastNode));
appendV->setDedup();
appendV->setTrackPrevPath(!edgeInfos.empty());
appendV->setColNames(genAppendVColNames(subplan.root->colNames(), lastNode, !edgeInfos.empty()));
subplan.root = appendV;
return Status::OK();
}
Status MatchPathPlanner::expandFromEdge(size_t startIndex, SubPlan& subplan) {
return expandFromNode(startIndex, subplan);
}
} // namespace graph
} // namespace nebula