From 0b00aa53f8ca229df0fd32851312bf7f45085978 Mon Sep 17 00:00:00 2001 From: Aiee <18348405+Aiee@users.noreply.github.com> Date: Fri, 2 Dec 2022 00:28:12 +0800 Subject: [PATCH] Fix edge case --- src/graph/planner/match/MatchPathPlanner.cpp | 39 ++++++++----------- .../validator/test/MatchValidatorTest.cpp | 1 - .../features/bugfix/DupAliasInMatch.feature | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/graph/planner/match/MatchPathPlanner.cpp b/src/graph/planner/match/MatchPathPlanner.cpp index ce13b9eebe6..3c534cb6eb4 100644 --- a/src/graph/planner/match/MatchPathPlanner.cpp +++ b/src/graph/planner/match/MatchPathPlanner.cpp @@ -254,10 +254,12 @@ Status MatchPathPlanner::leftExpandFromNode( for (size_t i = startIndex; i > 0; --i) { auto& node = nodeInfos[i]; auto& dst = nodeInfos[i - 1]; - bool expandInto = nodeAliasesSeenInPattern.find(dst.alias) != nodeAliasesSeenInPattern.end(); + if (!node.anonymous) { nodeAliasesSeenInPattern.emplace(node.alias); } + bool expandInto = nodeAliasesSeenInPattern.find(dst.alias) != nodeAliasesSeenInPattern.end(); + auto& edge = edgeInfos[i - 1]; auto traverse = Traverse::make(qctx, subplan.root, spaceId); traverse->setSrc(nextTraverseStart); @@ -297,9 +299,10 @@ Status MatchPathPlanner::leftExpandFromNode( bool duppedLastAlias = nodeAliasesSeenInPattern.find(lastNode.alias) != nodeAliasesSeenInPattern.end() && nodeAliasesSeenInPattern.size() > 1; - - if (!lastNode.anonymous) { - nodeAliasesSeenInPattern.emplace(lastNode.alias); + // 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); @@ -313,15 +316,6 @@ Status MatchPathPlanner::leftExpandFromNode( appendV->setColNames(genAppendVColNames(subplan.root->colNames(), lastNode, !edgeInfos.empty())); subplan.root = appendV; - if (duppedLastAlias) { - auto* startVid = nodeId(qctx->objPool(), lastNode); - auto* endVid = nextTraverseStart; - auto* filterExpr = RelationalExpression::makeEQ(qctx->objPool(), startVid, endVid); - auto* filter = Filter::make(qctx, appendV, filterExpr, false); - subplan.root = filter; - inputVar = filter->outputVar(); - } - return Status::OK(); } @@ -339,10 +333,12 @@ Status MatchPathPlanner::rightExpandFromNode( for (size_t i = startIndex; i < edgeInfos.size(); ++i) { auto& node = nodeInfos[i]; auto& dst = nodeInfos[i + 1]; - bool expandInto = nodeAliasesSeenInPattern.find(dst.alias) != nodeAliasesSeenInPattern.end(); + if (!node.anonymous) { nodeAliasesSeenInPattern.emplace(node.alias); } + bool expandInto = nodeAliasesSeenInPattern.find(dst.alias) != nodeAliasesSeenInPattern.end(); + auto& edge = edgeInfos[i]; auto traverse = Traverse::make(qctx, subplan.root, spaceId); traverse->setSrc(nextTraverseStart); @@ -380,6 +376,12 @@ Status MatchPathPlanner::rightExpandFromNode( nodeAliasesSeenInPattern.emplace(lastNode.alias); } + // 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); @@ -391,15 +393,6 @@ Status MatchPathPlanner::rightExpandFromNode( appendV->setColNames(genAppendVColNames(subplan.root->colNames(), lastNode, !edgeInfos.empty())); subplan.root = appendV; - if (duppedLastAlias) { - auto* startVid = nodeId(qctx->objPool(), lastNode); - auto* endVid = nextTraverseStart; - auto* filterExpr = RelationalExpression::makeEQ(qctx->objPool(), startVid, endVid); - auto* filter = Filter::make(qctx, appendV, filterExpr, false); - subplan.root = filter; - inputVar = filter->outputVar(); - } - return Status::OK(); } diff --git a/src/graph/validator/test/MatchValidatorTest.cpp b/src/graph/validator/test/MatchValidatorTest.cpp index f80acede978..694fc4701d4 100644 --- a/src/graph/validator/test/MatchValidatorTest.cpp +++ b/src/graph/validator/test/MatchValidatorTest.cpp @@ -541,7 +541,6 @@ TEST_F(MatchValidatorTest, validateAlias) { std::vector expected = {PlanNode::Kind::kProject, PlanNode::Kind::kProject, PlanNode::Kind::kFilter, - PlanNode::Kind::kAppendVertices, PlanNode::Kind::kTraverse, PlanNode::Kind::kTraverse, PlanNode::Kind::kIndexScan, diff --git a/tests/tck/features/bugfix/DupAliasInMatch.feature b/tests/tck/features/bugfix/DupAliasInMatch.feature index b6e065dd072..ac5b4ed19b2 100644 --- a/tests/tck/features/bugfix/DupAliasInMatch.feature +++ b/tests/tck/features/bugfix/DupAliasInMatch.feature @@ -36,7 +36,7 @@ Feature: Duplicate alias in MATCH | n1 | Scenario: Duplicate node alias expand both direction - # expand from left to right + # expand from middle to both sides When executing query: """ MATCH (n1)-[]->(n0)-[]->(n1)-[]->(n1)-[]->(n1) WHERE (id(n0) == "Tim Duncan") RETURN n1