@@ -254,10 +254,12 @@ Status MatchPathPlanner::leftExpandFromNode(
254
254
for (size_t i = startIndex; i > 0 ; --i) {
255
255
auto & node = nodeInfos[i];
256
256
auto & dst = nodeInfos[i - 1 ];
257
- bool expandInto = nodeAliasesSeenInPattern. find (dst. alias ) != nodeAliasesSeenInPattern. end ();
257
+
258
258
if (!node.anonymous ) {
259
259
nodeAliasesSeenInPattern.emplace (node.alias );
260
260
}
261
+ bool expandInto = nodeAliasesSeenInPattern.find (dst.alias ) != nodeAliasesSeenInPattern.end ();
262
+
261
263
auto & edge = edgeInfos[i - 1 ];
262
264
auto traverse = Traverse::make (qctx, subplan.root , spaceId);
263
265
traverse->setSrc (nextTraverseStart);
@@ -292,19 +294,26 @@ Status MatchPathPlanner::leftExpandFromNode(
292
294
}
293
295
}
294
296
295
- auto & node = nodeInfos.front ();
296
- if (!node.anonymous ) {
297
- nodeAliasesSeenInPattern.emplace (node.alias );
297
+ auto & lastNode = nodeInfos.front ();
298
+
299
+ bool duppedLastAlias =
300
+ nodeAliasesSeenInPattern.find (lastNode.alias ) != nodeAliasesSeenInPattern.end () &&
301
+ nodeAliasesSeenInPattern.size () > 1 ;
302
+ // If the the last alias has been presented in the pattern, we could emit the AppendVertices node
303
+ // because the same alias always presents in the same entity.
304
+ if (duppedLastAlias) {
305
+ return Status::OK ();
298
306
}
307
+
299
308
auto appendV = AppendVertices::make (qctx, subplan.root , spaceId);
300
309
auto vertexProps = SchemaUtil::getAllVertexProp (qctx, spaceId, true );
301
310
NG_RETURN_IF_ERROR (vertexProps);
302
311
appendV->setVertexProps (std::move (vertexProps).value ());
303
312
appendV->setSrc (nextTraverseStart);
304
- appendV->setVertexFilter (genVertexFilter (node ));
313
+ appendV->setVertexFilter (genVertexFilter (lastNode ));
305
314
appendV->setDedup ();
306
315
appendV->setTrackPrevPath (!edgeInfos.empty ());
307
- appendV->setColNames (genAppendVColNames (subplan.root ->colNames (), node , !edgeInfos.empty ()));
316
+ appendV->setColNames (genAppendVColNames (subplan.root ->colNames (), lastNode , !edgeInfos.empty ()));
308
317
subplan.root = appendV;
309
318
310
319
return Status::OK ();
@@ -324,10 +333,12 @@ Status MatchPathPlanner::rightExpandFromNode(
324
333
for (size_t i = startIndex; i < edgeInfos.size (); ++i) {
325
334
auto & node = nodeInfos[i];
326
335
auto & dst = nodeInfos[i + 1 ];
327
- bool expandInto = nodeAliasesSeenInPattern. find (dst. alias ) != nodeAliasesSeenInPattern. end ();
336
+
328
337
if (!node.anonymous ) {
329
338
nodeAliasesSeenInPattern.emplace (node.alias );
330
339
}
340
+ bool expandInto = nodeAliasesSeenInPattern.find (dst.alias ) != nodeAliasesSeenInPattern.end ();
341
+
331
342
auto & edge = edgeInfos[i];
332
343
auto traverse = Traverse::make (qctx, subplan.root , spaceId);
333
344
traverse->setSrc (nextTraverseStart);
@@ -355,19 +366,31 @@ Status MatchPathPlanner::rightExpandFromNode(
355
366
}
356
367
}
357
368
358
- auto & node = nodeInfos.back ();
359
- if (!node.anonymous ) {
360
- nodeAliasesSeenInPattern.emplace (node.alias );
369
+ auto & lastNode = nodeInfos.back ();
370
+
371
+ bool duppedLastAlias =
372
+ nodeAliasesSeenInPattern.find (lastNode.alias ) != nodeAliasesSeenInPattern.end () &&
373
+ nodeAliasesSeenInPattern.size () > 1 ;
374
+
375
+ if (!lastNode.anonymous ) {
376
+ nodeAliasesSeenInPattern.emplace (lastNode.alias );
361
377
}
378
+
379
+ // If the the last alias has been presented in the pattern, we could emit the AppendVertices node
380
+ // because the same alias always presents in the same entity.
381
+ if (duppedLastAlias) {
382
+ return Status::OK ();
383
+ }
384
+
362
385
auto appendV = AppendVertices::make (qctx, subplan.root , spaceId);
363
386
auto vertexProps = SchemaUtil::getAllVertexProp (qctx, spaceId, true );
364
387
NG_RETURN_IF_ERROR (vertexProps);
365
388
appendV->setVertexProps (std::move (vertexProps).value ());
366
389
appendV->setSrc (nextTraverseStart);
367
- appendV->setVertexFilter (genVertexFilter (node ));
390
+ appendV->setVertexFilter (genVertexFilter (lastNode ));
368
391
appendV->setDedup ();
369
392
appendV->setTrackPrevPath (!edgeInfos.empty ());
370
- appendV->setColNames (genAppendVColNames (subplan.root ->colNames (), node , !edgeInfos.empty ()));
393
+ appendV->setColNames (genAppendVColNames (subplan.root ->colNames (), lastNode , !edgeInfos.empty ()));
371
394
subplan.root = appendV;
372
395
373
396
return Status::OK ();
0 commit comments