Skip to content

Commit d4602ee

Browse files
authored
Implement join on text function (#287)
1 parent 5149830 commit d4602ee

File tree

11 files changed

+88
-0
lines changed

11 files changed

+88
-0
lines changed

cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,5 @@ add_subdirectory(do_module)
147147
add_subdirectory(periodic_module)
148148
add_subdirectory(map_module)
149149
add_subdirectory(collections_module)
150+
add_subdirectory(text_module)
150151
add_cugraph_subdirectory(cugraph_module)

cpp/text_module/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(text_module_src
2+
text_module.cpp
3+
algorithm/text.cpp)
4+
5+
add_query_module(text 1 "${text_module_src}")

cpp/text_module/algorithm/text.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "text.hpp"
2+
3+
void Text::Join(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
4+
mgp::memory = memory;
5+
const auto arguments = mgp::List(args);
6+
const auto record_factory = mgp::RecordFactory(result);
7+
8+
try {
9+
const auto list{arguments[0].ValueList()};
10+
const auto delimiter{arguments[1].ValueString()};
11+
12+
std::string result;
13+
if (list.Empty()) {
14+
auto record = record_factory.NewRecord();
15+
record.Insert(std::string(kResultJoin).c_str(), result);
16+
return;
17+
}
18+
19+
auto iterator = list.begin();
20+
result += (*iterator).ValueString();
21+
22+
for (++iterator; iterator != list.end(); ++iterator) {
23+
result += delimiter;
24+
result += (*iterator).ValueString();
25+
}
26+
27+
auto record = record_factory.NewRecord();
28+
record.Insert(std::string(kResultJoin).c_str(), result);
29+
30+
} catch (const std::exception &e) {
31+
record_factory.SetErrorMessage(e.what());
32+
return;
33+
}
34+
}

cpp/text_module/algorithm/text.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <mgp.hpp>
4+
5+
namespace Text {
6+
7+
/* join constants */
8+
constexpr const std::string_view kProcedureJoin = "join";
9+
constexpr const std::string_view kJoinArg1 = "strings";
10+
constexpr const std::string_view kJoinArg2 = "delimiter";
11+
constexpr const std::string_view kResultJoin = "string";
12+
13+
void Join(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
14+
15+
} // namespace Text

cpp/text_module/text_module.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <mgp.hpp>
2+
3+
#include "algorithm/text.hpp"
4+
5+
extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
6+
try {
7+
mgp::memory = memory;
8+
9+
AddProcedure(Text::Join, Text::kProcedureJoin, mgp::ProcedureType::Read,
10+
{mgp::Parameter(Text::kJoinArg1, {mgp::Type::List, mgp::Type::String}),
11+
mgp::Parameter(Text::kJoinArg2, mgp::Type::String)},
12+
{mgp::Return(Text::kResultJoin, mgp::Type::String)}, module, memory);
13+
14+
} catch (const std::exception &e) {
15+
return 1;
16+
}
17+
18+
return 0;
19+
}
20+
21+
extern "C" int mgp_shutdown_module() { return 0; }

e2e/text_test/test_join_1/input.cyp

Whitespace-only changes.

e2e/text_test/test_join_1/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: >
2+
CALL text.join(["idora", "ivan", "matija"], ", ") YIELD string RETURN string;
3+
output:
4+
- string: "idora, ivan, matija"

e2e/text_test/test_join_2/input.cyp

Whitespace-only changes.

e2e/text_test/test_join_2/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: >
2+
CALL text.join(["idora", "ivan", "matija"], "") YIELD string RETURN string;
3+
output:
4+
- string: "idoraivanmatija"

e2e/text_test/test_join_3/input.cyp

Whitespace-only changes.

0 commit comments

Comments
 (0)