-
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
14 changed files
with
326 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. Run the coordinator and workers to load your plugins. | ||
|
38 changes: 38 additions & 0 deletions
38
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,38 @@ | ||
======================= | ||
Function Plugin | ||
======================= | ||
|
||
Creating a Shared Library for UDFs | ||
---------------------------------- | ||
|
||
1. To create the UDF, create a new C++ file which follows 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 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. `Drop your shared libraries into the plugin directory <../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
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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
/* | ||
* 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 cpp_nameSpace(nameSpace); | ||
if (cpp_nameSpace.empty()) { | ||
auto systemConfig = SystemConfig::instance(); | ||
cpp_nameSpace = systemConfig->prestoDefaultNamespacePrefix(); | ||
} | ||
std::string cpp_name(cpp_nameSpace); | ||
cpp_name.append(name); | ||
LOG(INFO) << "registering function: " << cpp_name; | ||
facebook::velox::registerFunction<T, TReturn, TArgs...>( | ||
{cpp_name}, constraints, overwrite); | ||
} | ||
} // namespace facebook::presto |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
# Dynamic Loading of Presto CPP Extensions | ||
This library adds the ability to load User Defined Functions (UDFs), connectors, or types without having to fork and build Prestissimo, through the use of shared libraries that a Prestissimo worker can access. These are loaded on launch of the Presto server. The Presto server searches for any .so or .dylib files and loads them using this library. | ||
## Getting started | ||
1. Create a cpp file for your dynamic library | ||
|
||
For dynamically loaded function registration, the format is similar to that of built-in function registration, with some noted differences. Using [MyDynamicFunction.cpp](examples/MyDynamicFunction.cpp) as an example, the function uses the extern "C" keyword to protect against name mangling. A registry() function call is also necessary here. | ||
2. Register functions dynamically by creating .dylib or .so shared libraries and dropping them in a plugin directory | ||
|
||
These shared libraries may be made using CMakeLists like the following: | ||
``` | ||
add_library(name_of_dynamic_fn SHARED TestFunction.cpp) | ||
target_link_libraries(name_of_dynamic_fn PRIVATE fmt::fmt Folly::folly gflags::gflags) | ||
``` | ||
3. In the Prestissimo worker's config.properties file, set the plugin.dir property | ||
|
||
Set the value of plugin.dir to the file path of the directory where the shared libraries are located. | ||
``` | ||
plugin.dir="User\Test\Path\plugin" | ||
``` | ||
When the worker or the sidecar process starts, it scans the plugin directory and attempts to dynamically load all shared libraries. |
24 changes: 24 additions & 0 deletions
24
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,24 @@ | ||
# 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(velox_function_my_dynamic SHARED MyDynamicFunction.cpp) | ||
add_library(velox_varchar_function_my_dynamic SHARED | ||
MyDynamicVarcharFunction.cpp) | ||
add_library(velox_array_function_my_dynamic SHARED MyDynamicArrayFunction.cpp) | ||
|
||
set(CMAKE_DYLIB_TEST_LINK_LIBRARIES fmt::fmt gflags::gflags xsimd) | ||
target_link_libraries(velox_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
target_link_libraries(velox_varchar_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) | ||
target_link_libraries(velox_array_function_my_dynamic | ||
PRIVATE ${CMAKE_DYLIB_TEST_LINK_LIBRARIES}) |
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 Dynamic123Function { | ||
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::Dynamic123Function, | ||
int64_t, | ||
facebook::velox::Array<int64_t>>({"dynamic_1"}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
presto-native-execution/presto_cpp/main/dynamic_registry/examples/MyDynamicFunction.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,44 @@ | ||
/* | ||
* 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" | ||
|
||
// 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 Dynamic123Function { | ||
FOLLY_ALWAYS_INLINE bool call(int64_t& result) { | ||
result = 123; | ||
return true; | ||
} | ||
}; | ||
|
||
} // namespace facebook::velox::common::dynamicRegistry | ||
|
||
extern "C" { | ||
// In this case, we assume that facebook::velox::registerFunction | ||
// will be available and resolve when this library gets loaded. | ||
void registry() { | ||
facebook::presto::registerPrestoFunction< | ||
facebook::velox::common::dynamicRegistry::Dynamic123Function, | ||
int64_t>("dynamic_2"); | ||
} | ||
} |
Oops, something went wrong.