Skip to content
Open
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
35 changes: 22 additions & 13 deletions cpp/path_module/algorithm/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ Path::PathHelper::PathHelper(const mgp::Map &config) {

auto value = config.At("maxHops");
if (!value.IsNull()) {
config_.max_hops = value.ValueInt();
int64_t max_hops = value.ValueInt();
if (max_hops >= 0) {
config_.max_hops = max_hops;
}
}
value = config.At("minHops");
if (!value.IsNull()) {
config_.min_hops = value.ValueInt();
int64_t min_hops = value.ValueInt();
if (min_hops >= 0) {
config_.min_hops = min_hops;
}
}

value = config.At("relationshipFilter");
Expand Down Expand Up @@ -85,8 +91,10 @@ bool Path::PathHelper::AreLabelsValid(const LabelBools &label_bools) const {
}

bool Path::PathHelper::ContinueExpanding(const LabelBools &label_bools, size_t path_size) const {
return (static_cast<int64_t>(path_size) <= config_.max_hops && !label_bools.blacklisted && !label_bools.terminated &&
(label_bools.end_node || Whitelisted(label_bools.whitelisted)));
return (static_cast<int64_t>(path_size) <= config_.max_hops &&
((!label_bools.blacklisted && !label_bools.terminated &&
(label_bools.end_node || Whitelisted(label_bools.whitelisted))) ||
(path_size == 1 && !config_.filter_start_node)));
}

bool Path::PathHelper::PathSizeOk(const int64_t path_size) const {
Expand Down Expand Up @@ -434,12 +442,16 @@ void Path::PathSubgraph::ExpandFromRelationships(const std::pair<mgp::Node, int6
std::set<std::pair<std::string_view, int64_t>> &seen) {
for (const auto relationship : relationships) {
auto next_node = outgoing ? relationship.To() : relationship.From();

if (path_data_.visited_.contains(next_node.Id().AsInt())) {
continue;
}

auto type = std::string(relationship.Type());
auto wanted_direction = path_data_.helper_.GetDirection(type);

if (path_data_.helper_.IsNotStartOrSupportsStartRel(pair.second == 0)) {
if ((wanted_direction == RelDirection::kNone && !path_data_.helper_.AnyDirected(outgoing)) ||
path_data_.visited_.contains(next_node.Id().AsInt())) {
if (path_data_.helper_.IsNotStartOrFilterStartRel(pair.second == 0)) {
if (wanted_direction == RelDirection::kNone && !path_data_.helper_.AnyDirected(outgoing)) {
continue;
}
}
Expand All @@ -463,25 +475,22 @@ void Path::PathSubgraph::ExpandFromRelationships(const std::pair<mgp::Node, int6
}

void Path::PathSubgraph::TryInsertNode(const mgp::Node &node, int64_t hop_count, LabelBools &label_bools) {
if (path_data_.helper_.IsNotStartOrSupportsStartNode(hop_count == 0)) {
if (path_data_.helper_.IsNotStartOrFiltersStartNode(hop_count == 0)) {
if (path_data_.helper_.AreLabelsValid(label_bools)) {
to_be_returned_nodes_.AppendExtend(mgp::Value(node));
}
return;
}

if (!path_data_.visited_.contains(node.Id().AsInt())) {
to_be_returned_nodes_.AppendExtend(mgp::Value(node));
}
to_be_returned_nodes_.AppendExtend(mgp::Value(node));
}

mgp::List Path::PathSubgraph::BFS() {
std::queue<std::pair<mgp::Node, int64_t>> queue;
std::unordered_set<int64_t> visited;

for (const auto &node : path_data_.start_nodes_) {
queue.push({node, 0});
visited.insert(node.Id().AsInt());
path_data_.visited_.insert(node.Id().AsInt());
}

while (!queue.empty()) {
Expand Down
8 changes: 4 additions & 4 deletions cpp/path_module/algorithm/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

#include <mgp.hpp>

#include <limits>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <limits>

namespace Path {

Expand Down Expand Up @@ -108,8 +108,8 @@ class PathHelper {
LabelBools GetLabelBools(const mgp::Node &node) const;

bool AnyDirected(bool outgoing) const { return outgoing ? config_.any_outgoing : config_.any_incoming; }
bool IsNotStartOrSupportsStartNode(bool is_start) const { return (config_.filter_start_node || !is_start); }
bool IsNotStartOrSupportsStartRel(bool is_start) const { return (config_.begin_sequence_at_start || !is_start); }
bool IsNotStartOrFiltersStartNode(bool is_start) const { return (config_.filter_start_node || !is_start); }
bool IsNotStartOrFilterStartRel(bool is_start) const { return (config_.begin_sequence_at_start || !is_start); }

bool AreLabelsValid(const LabelBools &label_bools) const;
bool ContinueExpanding(const LabelBools &label_bools, size_t path_size) const;
Expand Down
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_4/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
17 changes: 17 additions & 0 deletions e2e/path_test/test_subgraph_all_4/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
query: >
MATCH (dog:Dog)
CALL path.subgraph_all(dog, {labelFilter: ["/Mouse"], filterStartNode: false})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes:
- labels:
- Mouse
properties: {}
- labels:
- Dog
properties: {}
rels:
- label: FRIENDS_WITH
properties: {}
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_5/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
12 changes: 12 additions & 0 deletions e2e/path_test/test_subgraph_all_5/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query: >
MATCH (dog:Dog)
CALL path.subgraph_all(dog, {labelFilter: ["/Mouse"]})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes:
- labels:
- Mouse
properties: {}
rels: []
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_6/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
17 changes: 17 additions & 0 deletions e2e/path_test/test_subgraph_all_6/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
query: >
MATCH (dog:Dog)
CALL path.subgraph_all(dog, {labelFilter: ["Cat", "-Dog"], filterStartNode: false})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes:
- labels:
- Dog
properties: {}
- labels:
- Cat
properties: {}
rels:
- label: "CATCHES"
properties: {}
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_7/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_7/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
query: >
MATCH (cat:Cat)
CALL path.subgraph_all(cat, {labelFilter: ["/Human", "Dog"]})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes: []
rels: []
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_8/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
15 changes: 15 additions & 0 deletions e2e/path_test/test_subgraph_all_8/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query: >
MATCH (cat:Cat)
CALL path.subgraph_all(cat, {labelFilter: ["/Human", "Dog"], filterStartNode: false})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes:
- labels:
- Cat
properties: {}
- labels:
- Human
properties: {}
rels: []
9 changes: 9 additions & 0 deletions e2e/path_test/test_subgraph_all_9/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE (w:Wolf)-[ca:CATCHES]->(d:Dog), (c:Cat), (m:Mouse), (h:Human);
MATCH (w:Wolf), (d:Dog), (c:Cat), (m:Mouse), (h:Human)
WITH w, d, c, m, h
CREATE (d)-[:CATCHES]->(c)
CREATE (c)-[:CATCHES]->(m)
CREATE (d)-[:FRIENDS_WITH]->(m)
CREATE (h)-[:OWNS]->(d)
CREATE (h)-[:HUNTS]->(w)
CREATE (h)-[:HATES]->(m);
22 changes: 22 additions & 0 deletions e2e/path_test/test_subgraph_all_9/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
query: >
MATCH (cat:Cat)
CALL path.subgraph_all(cat, {labelFilter: ["/Human", ">Dog"], filterStartNode: false})
YIELD nodes, rels
RETURN nodes, rels

output:
- nodes:
- labels:
- Cat
properties: {}
- labels:
- Dog
properties: {}
- labels:
- Human
properties: {}
rels:
- label: "CATCHES"
properties: {}
- label: "OWNS"
properties: {}