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

Undirected query for multi-labelled and unlabelled nodes. #1552

Merged
merged 1 commit into from
May 20, 2023
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
26 changes: 26 additions & 0 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,35 @@ Binder::bindGraphPattern(const std::vector<std::unique_ptr<PatternElement>>& gra
return std::make_pair(std::move(queryGraphCollection), std::move(propertyCollection));
}

// For undirected pattern (A)-[R]-(B), we need to match R in both FWD and BWD direction.
// Since computation always starts from one node, we need to rewrite the node table names to be
// the union of both node table names, i.e. (A|B)-[R]-(A|B)
void static rewriteNodeTableNameForUndirectedRel(const PatternElement& patternElement) {
auto leftNode = patternElement.getFirstNodePattern();
for (auto i = 0u; i < patternElement.getNumPatternElementChains(); ++i) {
auto patternElementChain = patternElement.getPatternElementChain(i);
auto rightNode = patternElementChain->getNodePattern();
if (patternElementChain->getRelPattern()->getDirection() == ArrowDirection::BOTH) {
std::vector<std::string> tableNameUnion = {};
auto leftTableNames = leftNode->getTableNames();
auto rightTableNames = rightNode->getTableNames();
if (!leftTableNames.empty() && !rightTableNames.empty()) {
tableNameUnion.insert(
tableNameUnion.end(), leftTableNames.begin(), leftTableNames.end());
tableNameUnion.insert(
tableNameUnion.end(), rightTableNames.begin(), rightTableNames.end());
}
leftNode->setTableNames(tableNameUnion);
rightNode->setTableNames(tableNameUnion);
}
leftNode = rightNode;
}
}

// Grammar ensures pattern element is always connected and thus can be bound as a query graph.
std::unique_ptr<QueryGraph> Binder::bindPatternElement(
const PatternElement& patternElement, PropertyKeyValCollection& collection) {
rewriteNodeTableNameForUndirectedRel(patternElement);
auto queryGraph = std::make_unique<QueryGraph>();
auto leftNode = bindQueryNode(*patternElement.getFirstNodePattern(), *queryGraph, collection);
for (auto i = 0u; i < patternElement.getNumPatternElementChains(); ++i) {
Expand Down
4 changes: 4 additions & 0 deletions src/include/parser/query/graph_pattern/node_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class NodePattern {
propertyKeyValPairs[idx].first, propertyKeyValPairs[idx].second.get());
}

void setTableNames(std::vector<std::string> otherTableNames) {
tableNames = std::move(otherTableNames);
}

protected:
std::string variableName;
std::vector<std::string> tableNames;
Expand Down
17 changes: 17 additions & 0 deletions test/test_files/demo_db/demo_db.test
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,20 @@ Karissa|30
Zhang|30
Zhang|40
Noura|50

-NAME Undir2
-QUERY MATCH (a:User)-[:LivesIn]-(c:City) RETURN a.name, c.name;
---- 8
Adam|Waterloo
Karissa|Waterloo
Zhang|Kitchener
Noura|Guelph
Waterloo|Karissa
Waterloo|Adam
Kitchener|Zhang
Guelph|Noura

-NAME Undir3
-QUERY MATCH ()-[]-() RETURN COUNT(*);
---- 1
16
38 changes: 38 additions & 0 deletions test/test_files/tinysnb/match/undirected.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,41 @@ Alice|Dan
Carol|Dan
Alice|Dan
Carol|Dan

-NAME UndirMultiLabel1
-QUERY MATCH (a:person:organisation)-[:meets|:marries|:workAt]-(b:person:organisation) RETURN COUNT(*);
---- 1
26

-NAME UndirMultiLabel2
-QUERY MATCH (a:person)-[:studyAt|:meets]-(b:person:organisation) RETURN COUNT(*);
---- 1
20

-NAME UndirMultiLabel3
-QUERY MATCH (a:person)-[:meets|:marries|:knows]-(b:person)-[:knows|:meets]-(c:person) WHERE c.fName = "Farooq" AND a.fName <> "Farooq" RETURN a.fName, b.fName;
---- 13
Carol|Elizabeth
Alice|Carol
Bob|Carol
Dan|Carol
Elizabeth|Carol
Greg|Carol
Greg|Elizabeth
Carol|Elizabeth
Alice|Carol
Bob|Carol
Dan|Carol
Elizabeth|Carol
Dan|Carol

-NAME UndirUnlabelled
-QUERY MATCH (a:person)-[]-() RETURN COUNT(*);
---- 1
60

-NAME UndirPattern
-QUERY MATCH ()-[:studyAt]-(a)-[:meets]-()-[:workAt]-() RETURN a.fName;
---- 2
Farooq
Bob