Skip to content

Commit 84bb690

Browse files
committed
feat(function): support md5
1 parent 594ed69 commit 84bb690

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ option(ENABLE_PACKAGE_TAR "Enable package artifacts to tar." OFF)
3030
option(ENABLE_CREATE_GIT_HOOKS "Enable create git hooks." ON)
3131
option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable include-what-you-use find nouse include files" OFF)
3232

33+
SET(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
34+
3335
add_definitions(-DNEBULA_HOME=${CMAKE_SOURCE_DIR})
3436

3537
if(ENABLE_STANDALONE_VERSION)

src/common/function/FunctionManager.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <float.h>
99
#include <folly/json.h>
10+
#include <proxygen/lib/utils/CryptUtil.h>
1011

1112
#include <boost/algorithm/string.hpp>
1213
#include <boost/algorithm/string/replace.hpp>
@@ -442,9 +443,9 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
442443
{TypeSignature({Value::Type::STRING}, Value::Type::MAP),
443444
TypeSignature({Value::Type::STRING}, Value::Type::NULLVALUE)}},
444445
{"score", {TypeSignature({}, Value::Type::__EMPTY__)}},
446+
{"md5", {TypeSignature({Value::Type::STRING}, Value::Type::STRING)}},
445447
};
446448

447-
// static
448449
StatusOr<Value::Type> FunctionManager::getReturnType(const std::string &funcName,
449450
const std::vector<Value::Type> &argsType) {
450451
auto func = funcName;
@@ -3000,6 +3001,26 @@ FunctionManager::FunctionManager() {
30003001
return Value::kNullValue;
30013002
};
30023003
}
3004+
{
3005+
auto &attr = functions_["md5"];
3006+
attr.minArity_ = 1;
3007+
attr.maxArity_ = 1;
3008+
attr.isAlwaysPure_ = true;
3009+
attr.body_ = [](const auto &args) -> Value {
3010+
switch (args[0].get().type()) {
3011+
case Value::Type::NULLVALUE: {
3012+
return Value::kNullValue;
3013+
}
3014+
case Value::Type::STRING: {
3015+
std::string value(args[0].get().getStr());
3016+
return proxygen::md5Encode(folly::StringPiece(value));
3017+
}
3018+
default: {
3019+
return Value::kNullBadType;
3020+
}
3021+
}
3022+
};
3023+
}
30033024
} // NOLINT
30043025

30053026
// static

src/common/function/test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ nebula_add_test(
1919
LIBRARIES
2020
gtest
2121
gtest_main
22+
${PROXYGEN_LIBRARIES}
2223
)
2324

2425
nebula_add_test(
@@ -37,6 +38,7 @@ nebula_add_test(
3738
$<TARGET_OBJECTS:fs_obj>
3839
LIBRARIES
3940
gtest
41+
${PROXYGEN_LIBRARIES}
4042
)
4143

4244
nebula_add_test(
@@ -52,5 +54,6 @@ nebula_add_test(
5254
LIBRARIES
5355
gtest
5456
gtest_main
57+
${PROXYGEN_LIBRARIES}
5558
)
5659

src/common/function/test/FunctionManagerTest.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ std::unordered_map<std::string, std::vector<Value>> FunctionManagerTest::args_ =
170170
{"json_extract1", {"{\"a\": 1, \"b\": 0.2, \"c\": {\"d\": true}}"}},
171171
{"json_extract2", {"_"}},
172172
{"json_extract3", {"{a: 1, \"b\": 0.2}"}},
173-
{"json_extract4", {"{\"a\": \"foo\", \"b\": 0.2, \"c\": {\"d\": {\"e\": 0.1}}}"}}};
174-
173+
{"json_extract4", {"{\"a\": \"foo\", \"b\": 0.2, \"c\": {\"d\": {\"e\": 0.1}}}"}},
174+
{"md5", {"abcdefghijkl"}}};
175175
#define TEST_FUNCTION(expr, ...) \
176176
do { \
177177
EXPECT_TRUE(testFunction(#expr, __VA_ARGS__)); \
@@ -248,6 +248,7 @@ TEST_F(FunctionManagerTest, testNull) {
248248
TEST_FUNCTION(concat, args_["nullvalue"], Value::kNullValue);
249249
TEST_FUNCTION(concat_ws, std::vector<Value>({Value::kNullValue, 1, 2}), Value::kNullValue);
250250
TEST_FUNCTION(concat_ws, std::vector<Value>({1, 1, 2}), Value::kNullValue);
251+
TEST_FUNCTION(md5, args_["nullvalue"], Value::kNullValue);
251252
}
252253

253254
TEST_F(FunctionManagerTest, functionCall) {
@@ -474,6 +475,9 @@ TEST_F(FunctionManagerTest, functionCall) {
474475
args_["json_extract4"],
475476
Value(Map({{"a", Value("foo")}, {"b", Value(0.2)}, {"c", Value(Map())}})));
476477
}
478+
{
479+
TEST_FUNCTION(md5, args_["md5"], "9fc9d606912030dca86582ed62595cf7");
480+
}
477481
{
478482
auto result = FunctionManager::get("hash", 1);
479483
ASSERT_TRUE(result.ok());

0 commit comments

Comments
 (0)