Skip to content

Commit

Permalink
Merge pull request #304 from memgraph/T510-MAGE-implement-label
Browse files Browse the repository at this point in the history
[main < T510] Implement label.exists
  • Loading branch information
antepusic authored Aug 16, 2023
2 parents 57b76f0 + 07ed16d commit 158919a
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,7 @@ add_subdirectory(do_module)
add_subdirectory(periodic_module)
add_subdirectory(map_module)
add_subdirectory(collections_module)
add_subdirectory(label_module)
add_subdirectory(text_module)

add_cugraph_subdirectory(cugraph_module)
5 changes: 5 additions & 0 deletions cpp/label_module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(label_module_src
label_module.cpp
algorithm/label.cpp)

add_query_module(label 1 "${label_module_src}")
23 changes: 23 additions & 0 deletions cpp/label_module/algorithm/label.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "label.hpp"

void Label::Exists(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::memory = memory;
const auto arguments = mgp::List(args);
const auto record_factory = mgp::RecordFactory(result);
try {
bool exists = false;

const auto label = arguments[1].ValueString();
if (arguments[0].IsNode()) {
const auto node = arguments[0].ValueNode();
exists = node.HasLabel(label);
}

auto record = record_factory.NewRecord();
record.Insert(std::string(kResultExists).c_str(), std::move(exists));

} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
return;
}
}
16 changes: 16 additions & 0 deletions cpp/label_module/algorithm/label.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <mgp.hpp>

namespace Label {

/* exists constants */
constexpr std::string_view kReturnExists = "exists";
constexpr std::string_view kProcedureExists = "exists";
constexpr std::string_view kArgumentsNode = "node";
constexpr std::string_view kArgumentsLabel = "label";
constexpr std::string_view kResultExists = "exists";

void Exists(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);

} // namespace Label
20 changes: 20 additions & 0 deletions cpp/label_module/label_module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <mgp.hpp>

#include "algorithm/label.hpp"

extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
try {
mgp::memory = memory;
AddProcedure(Label::Exists, Label::kProcedureExists, mgp::ProcedureType::Read,
{mgp::Parameter(Label::kArgumentsNode, mgp::Type::Any),
mgp::Parameter(Label::kArgumentsLabel, mgp::Type::String)},
{mgp::Return(Label::kReturnExists, mgp::Type::Bool)}, module, memory);

} catch (const std::exception &e) {
return 1;
}

return 0;
}

extern "C" int mgp_shutdown_module() { return 0; }
1 change: 1 addition & 0 deletions e2e/label_test/test_exists_false/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE (s1:Student {name: 'Ana'});
7 changes: 7 additions & 0 deletions e2e/label_test/test_exists_false/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query: >
MATCH (s1:Student {name: 'Ana'})
CALL label.exists(s1, "Teacher") YIELD exists
RETURN exists
output:
- exists: False
1 change: 1 addition & 0 deletions e2e/label_test/test_exists_not_node/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MERGE (s1:Student {name: 'Ana'}) MERGE (s2:Student {name: 'Marija'}) CREATE (s1)-[k:KNOWS]->(s2);
7 changes: 7 additions & 0 deletions e2e/label_test/test_exists_not_node/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query: >
MATCH (:Student)-[k:KNOWS]->(:Student)
CALL label.exists(k, "Knows") YIELD exists
RETURN exists
output:
- exists: False
1 change: 1 addition & 0 deletions e2e/label_test/test_exists_true/input.cyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE (s1:Student {name: 'Ana'});
7 changes: 7 additions & 0 deletions e2e/label_test/test_exists_true/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query: >
MATCH (s1:Student {name: 'Ana'})
CALL label.exists(s1, "Student") YIELD exists
RETURN exists
output:
- exists: True

0 comments on commit 158919a

Please sign in to comment.