-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dylib presto changes + presto-docs + readme + examples
- Loading branch information
Showing
16 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
******************* | ||
Presto C++ Plugins | ||
******************* | ||
|
||
This chapter outlines the plugins in Presto C++ that are available for various use cases such as to load User Defined Functions (UDFs), connectors, or types. | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
plugin/function_plugin | ||
|
||
|
||
Setup | ||
----- | ||
|
||
1. Place the plugin shared libraries in the ``plugins`` directory. | ||
|
||
2. Set the ``plugin.dir`` property to the path of the ``plugins`` directory in the ``config.properties`` file of each of your workers. | ||
|
||
3. Start or restart the coordinator and workers to pick up any placed libraries. | ||
|
45 changes: 45 additions & 0 deletions
45
presto-docs/src/main/sphinx/presto_cpp/plugin/function_plugin.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
======================= | ||
Function Plugin | ||
======================= | ||
|
||
Creating a Shared Library for UDFs | ||
---------------------------------- | ||
User defined functions (UDFs) allow users to create custom functions without the need to rebuild the executable. | ||
There are many benefits to UDFs, such as: | ||
|
||
* Simplify SQL queries by creating UDFs for repetitive logic. | ||
* Implement custom logic pertaining to the specific business use cases of the users. | ||
* Once defined, easily reusable and called multiple times just like built in functions. | ||
* Shorter compile times. | ||
|
||
1. To create the UDF, create a new C++ file in the following format: | ||
|
||
.. code-block:: c++ | ||
|
||
#include "presto_cpp/main/dynamic_registry/DynamicFunctionRegistrar.h" | ||
|
||
template <typename T> | ||
struct NameOfStruct { | ||
FOLLY_ALWAYS_INLINE bool call(int64_t& result) { | ||
... | ||
} | ||
}; | ||
|
||
extern "C" { | ||
void registry() { | ||
facebook::presto::registerPrestoFunction< | ||
nameOfStruct, | ||
int64_t>("function_name"); | ||
} | ||
} | ||
|
||
Note: the ``int64_t`` return type can be changed as needed. For more examples, see the `README <https://github.com/prestodb/presto-native-execution/main/dynamic_registry/README.md>`_. | ||
|
||
2. Create a shared library which may be made using CMakeLists.txt like the following: | ||
|
||
.. code-block:: text | ||
add_library(name_of_dynamic_fn SHARED TestFunction.cpp) | ||
target_link_libraries(name_of_dynamic_fn PRIVATE fmt::fmt Folly::folly gflags::gflags) | ||
3. Place your shared libraries in the plugin directory. The path to this directory needs to be the same as ``plugin.dir`` property set in :doc:`../plugin`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
presto-native-execution/presto_cpp/main/dynamic_registry/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Licensed 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. | ||
add_subdirectory(examples) |
41 changes: 41 additions & 0 deletions
41
presto-native-execution/presto_cpp/main/dynamic_registry/DynamicFunctionRegistrar.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed 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. | ||
*/ | ||
#pragma once | ||
|
||
#include "presto_cpp/main/common/Configs.h" | ||
#include "velox/functions/Macros.h" | ||
#include "velox/functions/Registerer.h" | ||
|
||
namespace facebook::presto { | ||
template <template <class> class T, typename TReturn, typename... TArgs> | ||
void registerPrestoFunction( | ||
const char* name, | ||
const char* nameSpace = "", | ||
const std::vector<velox::exec::SignatureVariable>& constraints = {}, | ||
bool overwrite = true) { | ||
std::string cppNamespace(nameSpace); | ||
if (cppNamespace.empty()) { | ||
auto systemConfig = SystemConfig::instance(); | ||
cppNamespace = systemConfig->prestoDefaultNamespacePrefix(); | ||
} | ||
if (cppNamespace.back() != '.') { | ||
cppNamespace.append("."); | ||
} | ||
std::string cppName(cppNamespace); | ||
cppName.append(name); | ||
LOG(INFO) << "registering function: " << cppName; | ||
facebook::velox::registerFunction<T, TReturn, TArgs...>( | ||
{cppName}, constraints, overwrite); | ||
} | ||
} // namespace facebook::presto |
1 change: 1 addition & 0 deletions
1
presto-native-execution/presto_cpp/main/dynamic_registry/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Read [here](https://prestodb.io/docs/current/presto-cpp/plugin.html) on how to use the Dynamic Library Loader. |
42 changes: 42 additions & 0 deletions
42
presto-native-execution/presto_cpp/main/dynamic_registry/examples/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Licensed 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. | ||
|
||
add_library(presto_function_my_dynamic SHARED MyDynamicFunction.cpp) | ||
add_library(presto_varchar_function_my_dynamic SHARED | ||
MyDynamicVarcharFunction.cpp) | ||
add_library(presto_array_function_my_dynamic SHARED MyDynamicArrayFunction.cpp) | ||
add_library(presto_non_defualt_function_my_dynamic SHARED | ||
MyDynamicNonDefaultFunction.cpp) | ||
|
||
set(CMAKE_DYLIB_TEST_LINK_LIBRARIES fmt::fmt gflags::gflags xsimd) | ||
target_link_libraries(presto_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
target_link_libraries(presto_varchar_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
target_link_libraries(presto_array_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
target_link_libraries(presto_non_defualt_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
|
||
if(APPLE) | ||
set(COMMON_LIBRARY_LINK_OPTIONS "-Wl,-undefined,dynamic_lookup") | ||
target_link_options(presto_function_my_dynamic PRIVATE | ||
${COMMON_LIBRARY_LINK_OPTIONS}) | ||
target_link_options(presto_varchar_function_my_dynamic PRIVATE | ||
${COMMON_LIBRARY_LINK_OPTIONS}) | ||
target_link_options(presto_array_function_my_dynamic PRIVATE | ||
${COMMON_LIBRARY_LINK_OPTIONS}) | ||
target_link_options(presto_non_defualt_function_my_dynamic PRIVATE | ||
${COMMON_LIBRARY_LINK_OPTIONS}) | ||
else() | ||
set(COMMON_LIBRARY_LINK_OPTIONS "-Wl,--exclude-libs,ALL") | ||
endif() |
48 changes: 48 additions & 0 deletions
48
presto-native-execution/presto_cpp/main/dynamic_registry/examples/MyDynamicArrayFunction.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed 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 "presto_cpp/main/dynamic_registry/DynamicFunctionRegistrar.h" | ||
#include "velox/type/SimpleFunctionApi.h" | ||
|
||
// This file defines a mock function that will be dynamically linked and | ||
// registered. There are no restrictions as to how the function needs to be | ||
// defined, but the library (.so) needs to provide a `void registry()` C | ||
// function in the top-level namespace. | ||
// | ||
// (note the extern "C" directive to prevent the compiler from mangling the | ||
// symbol name). | ||
|
||
namespace facebook::velox::common::dynamicRegistry { | ||
|
||
template <typename T> | ||
struct DynamicFunction { | ||
VELOX_DEFINE_FUNCTION_TYPES(T); | ||
FOLLY_ALWAYS_INLINE bool call( | ||
int64_t& result, | ||
const arg_type<Array<int64_t>>& array) { | ||
result = array.size(); | ||
return true; | ||
} | ||
}; | ||
} // namespace facebook::velox::common::dynamicRegistry | ||
|
||
extern "C" { | ||
void registry() { | ||
facebook::presto::registerPrestoFunction< | ||
facebook::velox::common::dynamicRegistry::DynamicFunction, | ||
int64_t, | ||
facebook::velox::Array<int64_t>>({"dynamic_array"}); | ||
} | ||
} |
Oops, something went wrong.