diff --git a/src/common/datatypes/test/CMakeLists.txt b/src/common/datatypes/test/CMakeLists.txt index f31731cf58b..4042938f81f 100644 --- a/src/common/datatypes/test/CMakeLists.txt +++ b/src/common/datatypes/test/CMakeLists.txt @@ -73,6 +73,7 @@ nebula_add_test( LIBRARIES gtest ${THRIFT_LIBRARIES} + ${PROXYGEN_LIBRARIES} ) nebula_add_test( diff --git a/src/common/function/FunctionManager.cpp b/src/common/function/FunctionManager.cpp index cc2393a775d..f654ba64dd0 100644 --- a/src/common/function/FunctionManager.cpp +++ b/src/common/function/FunctionManager.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -442,9 +443,9 @@ std::unordered_map> FunctionManager::typ {TypeSignature({Value::Type::STRING}, Value::Type::MAP), TypeSignature({Value::Type::STRING}, Value::Type::NULLVALUE)}}, {"score", {TypeSignature({}, Value::Type::__EMPTY__)}}, + {"md5", {TypeSignature({Value::Type::STRING}, Value::Type::STRING)}}, }; -// static StatusOr FunctionManager::getReturnType(const std::string &funcName, const std::vector &argsType) { auto func = funcName; @@ -3000,6 +3001,26 @@ FunctionManager::FunctionManager() { return Value::kNullValue; }; } + { + auto &attr = functions_["md5"]; + attr.minArity_ = 1; + attr.maxArity_ = 1; + attr.isAlwaysPure_ = true; + attr.body_ = [](const auto &args) -> Value { + switch (args[0].get().type()) { + case Value::Type::NULLVALUE: { + return Value::kNullValue; + } + case Value::Type::STRING: { + std::string value(args[0].get().getStr()); + return proxygen::md5Encode(folly::StringPiece(value)); + } + default: { + return Value::kNullBadType; + } + } + }; + } } // NOLINT // static diff --git a/src/common/function/test/CMakeLists.txt b/src/common/function/test/CMakeLists.txt index ab547cf44a6..c0410a30c22 100644 --- a/src/common/function/test/CMakeLists.txt +++ b/src/common/function/test/CMakeLists.txt @@ -19,6 +19,7 @@ nebula_add_test( LIBRARIES gtest gtest_main + ${PROXYGEN_LIBRARIES} ) nebula_add_test( @@ -37,6 +38,7 @@ nebula_add_test( $ LIBRARIES gtest + ${PROXYGEN_LIBRARIES} ) nebula_add_test( @@ -52,5 +54,6 @@ nebula_add_test( LIBRARIES gtest gtest_main + ${PROXYGEN_LIBRARIES} ) diff --git a/src/common/function/test/FunctionManagerTest.cpp b/src/common/function/test/FunctionManagerTest.cpp index 88ff49888df..babb1ef8b4a 100644 --- a/src/common/function/test/FunctionManagerTest.cpp +++ b/src/common/function/test/FunctionManagerTest.cpp @@ -170,8 +170,8 @@ std::unordered_map> FunctionManagerTest::args_ = {"json_extract1", {"{\"a\": 1, \"b\": 0.2, \"c\": {\"d\": true}}"}}, {"json_extract2", {"_"}}, {"json_extract3", {"{a: 1, \"b\": 0.2}"}}, - {"json_extract4", {"{\"a\": \"foo\", \"b\": 0.2, \"c\": {\"d\": {\"e\": 0.1}}}"}}}; - + {"json_extract4", {"{\"a\": \"foo\", \"b\": 0.2, \"c\": {\"d\": {\"e\": 0.1}}}"}}, + {"md5", {"abcdefghijkl"}}}; #define TEST_FUNCTION(expr, ...) \ do { \ EXPECT_TRUE(testFunction(#expr, __VA_ARGS__)); \ @@ -248,6 +248,7 @@ TEST_F(FunctionManagerTest, testNull) { TEST_FUNCTION(concat, args_["nullvalue"], Value::kNullValue); TEST_FUNCTION(concat_ws, std::vector({Value::kNullValue, 1, 2}), Value::kNullValue); TEST_FUNCTION(concat_ws, std::vector({1, 1, 2}), Value::kNullValue); + TEST_FUNCTION(md5, args_["nullvalue"], Value::kNullValue); } TEST_F(FunctionManagerTest, functionCall) { @@ -474,6 +475,7 @@ TEST_F(FunctionManagerTest, functionCall) { args_["json_extract4"], Value(Map({{"a", Value("foo")}, {"b", Value(0.2)}, {"c", Value(Map())}}))); } + { TEST_FUNCTION(md5, args_["md5"], "9fc9d606912030dca86582ed62595cf7"); } { auto result = FunctionManager::get("hash", 1); ASSERT_TRUE(result.ok());