From f343050cf83bb3aba28b0f68b94cdf36f5316860 Mon Sep 17 00:00:00 2001 From: NEUpanning Date: Fri, 26 Jul 2024 12:11:37 +0800 Subject: [PATCH] add unit test for udf --- cpp/velox/tests/CMakeLists.txt | 3 +++ cpp/velox/tests/MyUdfTest.cc | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 cpp/velox/tests/MyUdfTest.cc diff --git a/cpp/velox/tests/CMakeLists.txt b/cpp/velox/tests/CMakeLists.txt index f3d65f127f67a..7d7c8275daab1 100644 --- a/cpp/velox/tests/CMakeLists.txt +++ b/cpp/velox/tests/CMakeLists.txt @@ -57,3 +57,6 @@ add_velox_test(spark_functions_test SOURCES SparkFunctionTest.cc add_velox_test(execution_ctx_test SOURCES RuntimeTest.cc) add_velox_test(velox_memory_test SOURCES MemoryManagerTest.cc) add_velox_test(buffer_outputstream_test SOURCES BufferOutputStreamTest.cc) +if(BUILD_EXAMPLES) + add_velox_test(MyUdfTest SOURCES MyUdfTest.cc) +endif() diff --git a/cpp/velox/tests/MyUdfTest.cc b/cpp/velox/tests/MyUdfTest.cc new file mode 100644 index 0000000000000..46898b38cfd24 --- /dev/null +++ b/cpp/velox/tests/MyUdfTest.cc @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "udf/UdfLoader.h" +#include "velox/expression/VectorFunction.h" +#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" +#include "velox/parse/TypeResolver.h" + +using namespace facebook::velox::functions::test; +using namespace facebook::velox; +class MyUdfTest : public FunctionBaseTest { + protected: + static void SetUpTestCase() { + parse::registerTypeResolver(); + auto udfLoader = gluten::UdfLoader::getInstance(); + udfLoader->loadUdfLibraries("../udf/examples/libmyudf.so"); + udfLoader->registerUdf(); + memory::MemoryManager::testingSetInstance({}); + } +}; + +TEST_F(MyUdfTest, myudf1) { + const auto myudf1 = [&](const int64_t& number) { + return evaluateOnce("myudf1(c0)", BIGINT(), std::make_optional(number)); + }; + + EXPECT_EQ(5, myudf1(0)); + EXPECT_EQ(105, myudf1(100)); + EXPECT_EQ(3147483652, myudf1(3147483647)); // int64 +} \ No newline at end of file