Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/expand into filter #3929

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 99 additions & 34 deletions src/graph/planner/match/MatchClausePlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ static std::unique_ptr<std::vector<storage::cpp2::EdgeProp>> genEdgeProps(const
return edgeProps;
}

static Expression* nodeId(ObjectPool* pool, const NodeInfo& node) {
return AttributeExpression::make(
pool, InputPropertyExpression::make(pool, node.alias), ConstantExpression::make(pool, kVid));
}

StatusOr<SubPlan> MatchClausePlanner::transform(CypherClauseContextBase* clauseCtx) {
if (clauseCtx->kind != CypherClauseKind::kMatch) {
return Status::Error("Not a valid context for MatchClausePlanner.");
Expand All @@ -127,11 +132,18 @@ StatusOr<SubPlan> MatchClausePlanner::transform(CypherClauseContextBase* clauseC
SubPlan subplan;
size_t startIndex = 0;
bool startFromEdge = false;
// The node alias seen in current pattern only
std::unordered_set<std::string> nodeAliasesSeenInPattern;

NG_RETURN_IF_ERROR(findStarts(
nodeInfos, edgeInfos, matchClauseCtx, nodeAliasesSeen, startFromEdge, startIndex, subplan));
NG_RETURN_IF_ERROR(
expand(nodeInfos, edgeInfos, matchClauseCtx, startFromEdge, startIndex, subplan));
NG_RETURN_IF_ERROR(expand(nodeInfos,
edgeInfos,
matchClauseCtx,
startFromEdge,
startIndex,
subplan,
nodeAliasesSeenInPattern));
NG_RETURN_IF_ERROR(
connectPathPlan(nodeInfos, matchClauseCtx, subplan, nodeAliasesSeen, matchClausePlan));
}
Expand Down Expand Up @@ -220,46 +232,62 @@ Status MatchClausePlanner::expand(const std::vector<NodeInfo>& nodeInfos,
MatchClauseContext* matchClauseCtx,
bool startFromEdge,
size_t startIndex,
SubPlan& subplan) {
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern) {
if (startFromEdge) {
return expandFromEdge(nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan);
return expandFromEdge(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan, nodeAliasesSeenInPattern);
} else {
return expandFromNode(nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan);
return expandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan, nodeAliasesSeenInPattern);
}
}

Status MatchClausePlanner::expandFromNode(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan) {
Status MatchClausePlanner::expandFromNode(
const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern) {
// Vid of the start node is known already
nodeAliasesSeenInPattern.emplace(nodeInfos[startIndex].alias);
DCHECK(!nodeInfos.empty() && startIndex < nodeInfos.size());
if (startIndex == 0) {
// Pattern: (start)-[]-...-()
return rightExpandFromNode(nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan);
return rightExpandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan, nodeAliasesSeenInPattern);
}

const auto& var = subplan.root->outputVar();
if (startIndex == nodeInfos.size() - 1) {
// Pattern: ()-[]-...-(start)
return leftExpandFromNode(nodeInfos, edgeInfos, matchClauseCtx, startIndex, var, subplan);
return leftExpandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, var, subplan, nodeAliasesSeenInPattern);
}

// Pattern: ()-[]-...-(start)-...-[]-()
NG_RETURN_IF_ERROR(
rightExpandFromNode(nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan));
NG_RETURN_IF_ERROR(leftExpandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan.root->outputVar(), subplan));
NG_RETURN_IF_ERROR(rightExpandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan, nodeAliasesSeenInPattern));
NG_RETURN_IF_ERROR(leftExpandFromNode(nodeInfos,
edgeInfos,
matchClauseCtx,
startIndex,
subplan.root->outputVar(),
subplan,
nodeAliasesSeenInPattern));

return Status::OK();
}

Status MatchClausePlanner::leftExpandFromNode(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
std::string inputVar,
SubPlan& subplan) {
Status MatchClausePlanner::leftExpandFromNode(
const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
std::string inputVar,
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern) {
Expression* nextTraverseStart = nullptr;
auto qctx = matchClauseCtx->qctx;
if (startIndex == nodeInfos.size() - 1) {
Expand All @@ -274,6 +302,11 @@ Status MatchClausePlanner::leftExpandFromNode(const std::vector<NodeInfo>& nodeI
bool reversely = true;
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);
}
auto& edge = edgeInfos[i - 1];
auto traverse = Traverse::make(qctx, subplan.root, spaceId);
traverse->setSrc(nextTraverseStart);
Expand All @@ -297,10 +330,22 @@ Status MatchClausePlanner::leftExpandFromNode(const std::vector<NodeInfo>& nodeI
subplan.root = traverse;
nextTraverseStart = genNextTraverseStart(qctx->objPool(), edge);
inputVar = traverse->outputVar();
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;
inputVar = filter->outputVar();
}
}

VLOG(1) << subplan;
auto& node = nodeInfos.front();
if (!node.anonymous) {
nodeAliasesSeenInPattern.emplace(node.alias);
}
auto appendV = AppendVertices::make(qctx, subplan.root, spaceId);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
Expand All @@ -316,18 +361,25 @@ Status MatchClausePlanner::leftExpandFromNode(const std::vector<NodeInfo>& nodeI
return Status::OK();
}

Status MatchClausePlanner::rightExpandFromNode(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan) {
Status MatchClausePlanner::rightExpandFromNode(
const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern) {
auto inputVar = subplan.root->outputVar();
auto qctx = matchClauseCtx->qctx;
auto spaceId = matchClauseCtx->space.id;
Expression* nextTraverseStart = initialExpr_;
bool reversely = false;
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);
}
auto& edge = edgeInfos[i];
auto traverse = Traverse::make(qctx, subplan.root, spaceId);
traverse->setSrc(nextTraverseStart);
Expand All @@ -345,11 +397,21 @@ Status MatchClausePlanner::rightExpandFromNode(const std::vector<NodeInfo>& node
genTraverseColNames(subplan.root->colNames(), node, edge, i != startIndex));
subplan.root = traverse;
nextTraverseStart = genNextTraverseStart(qctx->objPool(), edge);
inputVar = traverse->outputVar();
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;
inputVar = filter->outputVar();
}
}

VLOG(1) << subplan;
auto& node = nodeInfos.back();
if (!node.anonymous) {
nodeAliasesSeenInPattern.emplace(node.alias);
}
auto appendV = AppendVertices::make(qctx, subplan.root, spaceId);
auto vertexProps = SchemaUtil::getAllVertexProp(qctx, spaceId, true);
NG_RETURN_IF_ERROR(vertexProps);
Expand All @@ -365,12 +427,15 @@ Status MatchClausePlanner::rightExpandFromNode(const std::vector<NodeInfo>& node
return Status::OK();
}

Status MatchClausePlanner::expandFromEdge(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan) {
return expandFromNode(nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan);
Status MatchClausePlanner::expandFromEdge(
const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern) {
return expandFromNode(
nodeInfos, edgeInfos, matchClauseCtx, startIndex, subplan, nodeAliasesSeenInPattern);
}

Status MatchClausePlanner::projectColumnsBySymbols(MatchClauseContext* matchClauseCtx,
Expand Down
15 changes: 10 additions & 5 deletions src/graph/planner/match/MatchClausePlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ class MatchClausePlanner final : public CypherClausePlanner {
MatchClauseContext* matchClauseCtx,
bool startFromEdge,
size_t startIndex,
SubPlan& subplan);
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern);

Status expandFromNode(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan);
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern);

PlanNode* joinLeftAndRightExpandPart(QueryContext* qctx, PlanNode* left, PlanNode* right);

Expand All @@ -46,19 +48,22 @@ class MatchClausePlanner final : public CypherClausePlanner {
MatchClauseContext* matchClauseCtx,
size_t startIndex,
std::string inputVar,
SubPlan& subplan);
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern);

Status rightExpandFromNode(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan);
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern);

Status expandFromEdge(const std::vector<NodeInfo>& nodeInfos,
const std::vector<EdgeInfo>& edgeInfos,
MatchClauseContext* matchClauseCtx,
size_t startIndex,
SubPlan& subplan);
SubPlan& subplan,
std::unordered_set<std::string>& nodeAliasesSeenInPattern);

// Project all named alias.
// TODO: Might not neccessary
Expand Down
5 changes: 1 addition & 4 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ Status MatchValidator::buildNodeInfo(const MatchPath *path,
anonymous = true;
alias = vctx_->anonVarGen()->getVar();
} else {
if (!nodeAliases.emplace(alias, AliasType::kNode).second) {
return Status::SemanticError("`%s': Redefined alias in a single path pattern.",
alias.c_str());
}
nodeAliases.emplace(alias, AliasType::kNode);
}
Expression *filter = nullptr;
if (props != nullptr) {
Expand Down
17 changes: 17 additions & 0 deletions tests/data/nba_int_vid/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ schema: |
CREATE EDGE INDEX IF NOT EXISTS serve_start_end_index ON serve(start_year, end_year);
CREATE EDGE INDEX IF NOT EXISTS like_likeness_index ON like(likeness);
files:
- path: ../nba/null.csv
withHeader: true
type: vertex
vertex:
vid:
index: 0
type: string
function: hash
tags:
- name: player
props:
- name: name
type: string
index: 1
- name: age
type: int
index: 2
- path: ../nba/player.csv
withHeader: true
type: vertex
Expand Down
2 changes: 1 addition & 1 deletion tests/tck/features/match/Base.IntVid.feature
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ Feature: Basic match
Scenario: filter evaluable
When executing query:
"""
match (v:player{age: -1}) return v
match (v:player{age: -5}) return v
"""
Then the result should be, in any order, with relax comparison:
| v |
Expand Down
6 changes: 0 additions & 6 deletions tests/tck/features/match/MultiLineMultiQueryParts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,3 @@ Feature: Multi Line Multi Query Parts
RETURN m
"""
Then a SemanticError should be raised at runtime: Alias used but not defined: `m'
When executing query:
"""
USE nba;
MATCH (v:player)-[e]-(v:team) RETURN v, e
"""
Then a SemanticError should be raised at runtime: `v': Redefined alias in a single path pattern.
5 changes: 0 additions & 5 deletions tests/tck/features/match/MultiQueryParts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,3 @@ Feature: Multi Query Parts
RETURN m
"""
Then a SemanticError should be raised at runtime: Alias used but not defined: `m'
When executing query:
"""
MATCH (v:player)-[e]-(v:team) RETURN v, e
"""
Then a SemanticError should be raised at runtime: `v': Redefined alias in a single path pattern.
Loading