From 84bb690fc4cd472cb6e0a19eddf2b009a699547c Mon Sep 17 00:00:00 2001 From: alexsehep Date: Tue, 2 Apr 2024 09:28:31 +0800 Subject: [PATCH 1/4] feat(function): support md5 --- CMakeLists.txt | 2 ++ src/common/function/FunctionManager.cpp | 23 ++++++++++++++++++- src/common/function/test/CMakeLists.txt | 3 +++ .../function/test/FunctionManagerTest.cpp | 8 +++++-- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fdb7b827d9..67f1f2e73d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,8 @@ option(ENABLE_PACKAGE_TAR "Enable package artifacts to tar." OFF) option(ENABLE_CREATE_GIT_HOOKS "Enable create git hooks." ON) option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable include-what-you-use find nouse include files" OFF) +SET(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) + add_definitions(-DNEBULA_HOME=${CMAKE_SOURCE_DIR}) if(ENABLE_STANDALONE_VERSION) diff --git a/src/common/function/FunctionManager.cpp b/src/common/function/FunctionManager.cpp index cc2393a775d..3783a5aada7 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..31981ac621b 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,9 @@ 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()); From d2b17d4bae8bf5c0ac4454520df49cb14905308a Mon Sep 17 00:00:00 2001 From: alexsehep Date: Wed, 10 Apr 2024 20:46:17 +0800 Subject: [PATCH 2/4] format code Signed-off-by: alexsehep --- src/common/function/FunctionManager.cpp | 4 ++-- src/common/function/test/FunctionManagerTest.cpp | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/common/function/FunctionManager.cpp b/src/common/function/FunctionManager.cpp index 3783a5aada7..f654ba64dd0 100644 --- a/src/common/function/FunctionManager.cpp +++ b/src/common/function/FunctionManager.cpp @@ -3012,8 +3012,8 @@ FunctionManager::FunctionManager() { return Value::kNullValue; } case Value::Type::STRING: { - std::string value(args[0].get().getStr()); - return proxygen::md5Encode(folly::StringPiece(value)); + std::string value(args[0].get().getStr()); + return proxygen::md5Encode(folly::StringPiece(value)); } default: { return Value::kNullBadType; diff --git a/src/common/function/test/FunctionManagerTest.cpp b/src/common/function/test/FunctionManagerTest.cpp index 31981ac621b..babb1ef8b4a 100644 --- a/src/common/function/test/FunctionManagerTest.cpp +++ b/src/common/function/test/FunctionManagerTest.cpp @@ -475,9 +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"); - } + { TEST_FUNCTION(md5, args_["md5"], "9fc9d606912030dca86582ed62595cf7"); } { auto result = FunctionManager::get("hash", 1); ASSERT_TRUE(result.ok()); From 993bea68399f2edf5a191c4952143b41e15b4d28 Mon Sep 17 00:00:00 2001 From: alexsehep Date: Thu, 11 Apr 2024 14:26:57 +0800 Subject: [PATCH 3/4] clean cmake option Signed-off-by: alexsehep --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67f1f2e73d5..6fdb7b827d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,6 @@ option(ENABLE_PACKAGE_TAR "Enable package artifacts to tar." OFF) option(ENABLE_CREATE_GIT_HOOKS "Enable create git hooks." ON) option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable include-what-you-use find nouse include files" OFF) -SET(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) - add_definitions(-DNEBULA_HOME=${CMAKE_SOURCE_DIR}) if(ENABLE_STANDALONE_VERSION) From e607095df4fa4d07db33ceabf903517f2c83ca97 Mon Sep 17 00:00:00 2001 From: alexsehep Date: Sat, 20 Apr 2024 18:22:33 +0800 Subject: [PATCH 4/4] fix cmake Signed-off-by: alexsehep --- src/common/datatypes/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) 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(