-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(native): Add support for REST based remote function #23568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0695129
feat(functions): add REST client for remote function calls
Joe-Abraham e115a30
feat(functions): implement remote function registration and execution
Joe-Abraham ce70aa7
feat(expr): integrate remote functions into expression evaluation
Joe-Abraham 9ef50d1
test(functions): add mock REST server for remote function testing
Joe-Abraham c68e673
test(functions): add example remote function handlers
Joe-Abraham 160728c
test(functions): add C++ unit tests for remote functions
Joe-Abraham 79e3fb2
build(docker): add remote function support to Docker configuration
Joe-Abraham 5bbb2d1
test(e2e): add container-based E2E tests for remote functions
Joe-Abraham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| # Ignore build directories | ||
| _build/ | ||
| cmake-build-debug/ | ||
| cmake-build-release/ | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
presto-native-execution/presto_cpp/main/functions/remote/CMakeLists.txt
This file contains hidden or 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,28 @@ | ||
| # 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_to_velox_remote_functions PrestoRestFunctionRegistration.cpp) | ||
| target_link_libraries( | ||
| presto_to_velox_remote_functions | ||
| presto_functions_remote | ||
| velox_type_fbhive | ||
| Boost::url | ||
| ) | ||
|
|
||
| add_library(presto_functions_remote RestRemoteFunction.cpp) | ||
| target_link_libraries(presto_functions_remote presto_functions_rest_client velox_functions_remote) | ||
|
|
||
| add_subdirectory(client) | ||
|
|
||
| if(${PRESTO_ENABLE_TESTING}) | ||
| add_subdirectory(tests) | ||
| endif() |
173 changes: 173 additions & 0 deletions
173
presto-native-execution/presto_cpp/main/functions/remote/PrestoRestFunctionRegistration.cpp
|
Joe-Abraham marked this conversation as resolved.
|
This file contains hidden or 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,173 @@ | ||
| /* | ||
| * 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/functions/remote/PrestoRestFunctionRegistration.h" | ||
|
|
||
| #include <boost/url/encode.hpp> | ||
| #include <boost/url/rfc/unreserved_chars.hpp> | ||
|
|
||
| #include "presto_cpp/main/common/Configs.h" | ||
| #include "presto_cpp/main/common/Utils.h" | ||
| #include "presto_cpp/main/functions/remote/RestRemoteFunction.h" | ||
| #include "presto_cpp/main/functions/remote/client/RestRemoteClient.h" | ||
|
|
||
| using facebook::velox::functions::remote::PageFormat; | ||
|
|
||
| namespace facebook::presto::functions::remote::rest { | ||
| namespace { | ||
| // Returns the serialization/deserialization format used by the remote function | ||
| // server. The format is determined by the system configuration value | ||
| // "remoteFunctionServerSerde". Supported formats: | ||
| // - "presto_page": Uses Presto page format. | ||
| // - "spark_unsafe_row": Uses Spark unsafe row format. | ||
| // @return PageFormat enum value corresponding to the configured serde format. | ||
| // @throws VeloxException if the configured format is unknown. | ||
| PageFormat getSerdeFormat() { | ||
| static const auto serdeFormat = | ||
| SystemConfig::instance()->remoteFunctionServerSerde(); | ||
| if (serdeFormat == "presto_page") { | ||
| return PageFormat::PRESTO_PAGE; | ||
| } else if (serdeFormat == "spark_unsafe_row") { | ||
| return PageFormat::SPARK_UNSAFE_ROW; | ||
| } else { | ||
| VELOX_FAIL( | ||
| "Unknown serde name for remote function server: '{}'", serdeFormat); | ||
| } | ||
| } | ||
|
|
||
| // Encodes a string for safe inclusion in a URL by escaping non-alphanumeric | ||
| // characters using percent-encoding. Alphanumeric characters and '-', '_', '.', | ||
| // '~' are left unchanged. All other characters are replaced with '%' followed | ||
| // by their two-digit hexadecimal value. | ||
| // @param value The input string to encode. | ||
| // @return The URL-encoded string. | ||
| std::string urlEncode(const std::string& value) { | ||
| return boost::urls::encode(value, boost::urls::unreserved_chars); | ||
| } | ||
|
Joe-Abraham marked this conversation as resolved.
|
||
|
|
||
| std::string getFunctionName(const protocol::SqlFunctionId& functionId) { | ||
|
Joe-Abraham marked this conversation as resolved.
|
||
| // Example: "namespace.schema.function;TYPE;TYPE". | ||
| const auto nameEnd = functionId.find(';'); | ||
| // Assuming the possibility of missing ';' if there are no function arguments. | ||
| return nameEnd != std::string::npos ? functionId.substr(0, nameEnd) | ||
| : functionId; | ||
| } | ||
|
|
||
| // Constructs a Velox function signature from a Presto function signature. This | ||
| // function translates type variable constraints, integer variable constraints, | ||
| // return type, argument types, and variable arity from the Presto signature to | ||
| // the corresponding Velox signature builder. | ||
| // @param prestoSignature The Presto function signature to convert. | ||
| // @return A pointer to the constructed Velox function signature. | ||
| velox::exec::FunctionSignaturePtr buildVeloxSignatureFromPrestoSignature( | ||
| const protocol::Signature& prestoSignature) { | ||
| velox::exec::FunctionSignatureBuilder signatureBuilder; | ||
|
|
||
| for (const auto& typeVar : prestoSignature.typeVariableConstraints) { | ||
| signatureBuilder.typeVariable(typeVar.name); | ||
| } | ||
|
|
||
| for (const auto& longVar : prestoSignature.longVariableConstraints) { | ||
| signatureBuilder.integerVariable(longVar.name); | ||
| } | ||
| signatureBuilder.returnType(prestoSignature.returnType); | ||
|
|
||
| for (const auto& argType : prestoSignature.argumentTypes) { | ||
| signatureBuilder.argumentType(argType); | ||
| } | ||
|
|
||
| if (prestoSignature.variableArity) { | ||
| signatureBuilder.variableArity(); | ||
| } | ||
| return signatureBuilder.build(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void registerRestRemoteFunction( | ||
| const protocol::RestFunctionHandle& restFunctionHandle) { | ||
| static std::mutex registrationMutex; | ||
| static std::unordered_map<std::string, std::string> registeredFunctionHandles; | ||
|
sourcery-ai[bot] marked this conversation as resolved.
|
||
| static std::unordered_map<std::string, functions::rest::RestRemoteClientPtr> | ||
| restClient; | ||
| static const std::string remoteFunctionServerRestURL = | ||
| SystemConfig::instance()->remoteFunctionServerRestURL(); | ||
|
|
||
| const std::string functionId = restFunctionHandle.functionId; | ||
|
|
||
| json functionHandleJson; | ||
| to_json(functionHandleJson, restFunctionHandle); | ||
| functionHandleJson["url"] = remoteFunctionServerRestURL; | ||
| const std::string serializedFunctionHandle = functionHandleJson.dump(); | ||
|
|
||
| // Check if already registered (read-only, no lock needed for initial check) | ||
| { | ||
| std::lock_guard<std::mutex> lock(registrationMutex); | ||
| auto it = registeredFunctionHandles.find(functionId); | ||
| if (it != registeredFunctionHandles.end() && | ||
| it->second == serializedFunctionHandle) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Get or create shared RestRemoteClient for this server URL | ||
| functions::rest::RestRemoteClientPtr remoteClient; | ||
| { | ||
| std::lock_guard<std::mutex> lock(registrationMutex); | ||
| auto clientIt = restClient.find(remoteFunctionServerRestURL); | ||
| if (clientIt == restClient.end()) { | ||
| restClient[remoteFunctionServerRestURL] = | ||
| std::make_shared<functions::rest::RestRemoteClient>( | ||
| remoteFunctionServerRestURL); | ||
| } | ||
| remoteClient = restClient[remoteFunctionServerRestURL]; | ||
| } | ||
|
|
||
| functions::rest::VeloxRemoteFunctionMetadata metadata; | ||
|
|
||
| // Extract function name parts using the utility function | ||
| const std::string functionName = | ||
| getFunctionName(restFunctionHandle.functionId); | ||
| const auto parts = util::getFunctionNameParts(functionName); | ||
| const std::string schema = parts[1]; | ||
| const std::string function = parts[2]; | ||
|
|
||
| const std::string functionLocation = fmt::format( | ||
| "{}/v1/functions/{}/{}/{}/{}", | ||
| remoteFunctionServerRestURL, | ||
| schema, | ||
| function, | ||
| urlEncode(restFunctionHandle.functionId), | ||
| restFunctionHandle.version); | ||
| metadata.location = functionLocation; | ||
| metadata.serdeFormat = getSerdeFormat(); | ||
|
|
||
| auto veloxSignature = | ||
| buildVeloxSignatureFromPrestoSignature(restFunctionHandle.signature); | ||
| std::vector<velox::exec::FunctionSignaturePtr> veloxSignatures = { | ||
| veloxSignature}; | ||
|
|
||
| functions::rest::registerVeloxRemoteFunction( | ||
| getFunctionName(restFunctionHandle.functionId), | ||
| veloxSignatures, | ||
| metadata, | ||
| remoteClient); | ||
|
|
||
| // Update registration map | ||
| { | ||
| std::lock_guard<std::mutex> lock(registrationMutex); | ||
| registeredFunctionHandles[functionId] = serializedFunctionHandle; | ||
| } | ||
| } | ||
| } // namespace facebook::presto::functions::remote::rest | ||
23 changes: 23 additions & 0 deletions
23
presto-native-execution/presto_cpp/main/functions/remote/PrestoRestFunctionRegistration.h
|
Joe-Abraham marked this conversation as resolved.
|
This file contains hidden or 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,23 @@ | ||
| /* | ||
| * 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/presto_protocol/presto_protocol.h" | ||
|
|
||
| namespace facebook::presto::functions::remote::rest { | ||
|
|
||
| void registerRestRemoteFunction( | ||
| const protocol::RestFunctionHandle& restFunctionHandle); | ||
| } // namespace facebook::presto::functions::remote::rest |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.