Skip to content

Commit

Permalink
Implemented undirected Rel for multi-labelled and unlabelled queries
Browse files Browse the repository at this point in the history
  • Loading branch information
aziz-mu committed May 19, 2023
1 parent 4aae989 commit e324254
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
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));
}

void static rewriteNodeTableNameForUndirectedRel(const PatternElement& patternElement) {
// 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)
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

0 comments on commit e324254

Please sign in to comment.