-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
[native] Add support for REST based remote function #23568
Open
Joe-Abraham
wants to merge
1
commit into
prestodb:master
Choose a base branch
from
Joe-Abraham:remote
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,16 @@ | |
#include "velox/vector/ComplexVector.h" | ||
#include "velox/vector/ConstantVector.h" | ||
#include "velox/vector/FlatVector.h" | ||
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS | ||
#include "presto_cpp/main/JsonSignatureParser.h" | ||
#include "velox/expression/FunctionSignature.h" | ||
#include "velox/functions/remote/client/Remote.h" | ||
#endif | ||
|
||
using namespace facebook::velox::core; | ||
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS | ||
using facebook::velox::functions::remote::PageFormat; | ||
#endif | ||
using facebook::velox::TypeKind; | ||
|
||
namespace facebook::presto { | ||
|
@@ -131,6 +139,114 @@ std::string getFunctionName(const protocol::SqlFunctionId& functionId) { | |
: functionId; | ||
} | ||
|
||
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS | ||
std::string getSchemaName(const protocol::SqlFunctionId& functionId) { | ||
// Example: "json.x4.eq;INTEGER;INTEGER". | ||
const auto nameEnd = functionId.find(';'); | ||
std::string functionName = (nameEnd != std::string::npos) | ||
? functionId.substr(0, nameEnd) | ||
: functionId; | ||
|
||
const auto firstDot = functionName.find('.'); | ||
const auto secondDot = functionName.find('.', firstDot + 1); | ||
if (firstDot != std::string::npos && secondDot != std::string::npos) { | ||
return functionName.substr(firstDot + 1, secondDot - firstDot - 1); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
std::string extractFunctionName(const std::string& input) { | ||
size_t lastDot = input.find_last_of('.'); | ||
if (lastDot != std::string::npos) { | ||
return input.substr(lastDot + 1); | ||
} | ||
return input; | ||
} | ||
|
||
std::string urlEncode(const std::string& value) { | ||
std::ostringstream escaped; | ||
escaped.fill('0'); | ||
escaped << std::hex; | ||
for (char c : value) { | ||
if (isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || | ||
c == '.' || c == '~') { | ||
escaped << c; | ||
} else { | ||
escaped << '%' << std::setw(2) << int(static_cast<unsigned char>(c)); | ||
} | ||
} | ||
return escaped.str(); | ||
} | ||
|
||
TypedExprPtr registerRestRemoteFunction( | ||
const protocol::RestFunctionHandle& restFunctionHandle, | ||
const std::vector<TypedExprPtr>& args, | ||
const velox::TypePtr& returnType) { | ||
const auto* systemConfig = SystemConfig::instance(); | ||
|
||
velox::functions::RemoteVectorFunctionMetadata metadata; | ||
const auto& serdeName = systemConfig->remoteFunctionServerSerde(); | ||
if (serdeName == "presto_page") { | ||
metadata.serdeFormat = PageFormat::PRESTO_PAGE; | ||
} else { | ||
VELOX_FAIL( | ||
"presto_page serde is expected by remote function server but got : '{}'", | ||
serdeName); | ||
} | ||
metadata.functionId = restFunctionHandle.functionId; | ||
metadata.version = restFunctionHandle.version; | ||
metadata.schema = getSchemaName(restFunctionHandle.functionId); | ||
|
||
const std::string location = fmt::format( | ||
"{}/v1/functions/{}/{}/{}/{}", | ||
systemConfig->remoteFunctionServerRestURL(), | ||
metadata.schema.value_or("default"), | ||
extractFunctionName(getFunctionName(restFunctionHandle.functionId)), | ||
urlEncode(restFunctionHandle.functionId), | ||
restFunctionHandle.version); | ||
metadata.location = location; | ||
|
||
const auto& prestoSignature = restFunctionHandle.signature; | ||
// parseTypeSignature | ||
velox::exec::FunctionSignatureBuilder signatureBuilder; | ||
// Handle type variable constraints | ||
for (const auto& typeVar : prestoSignature.typeVariableConstraints) { | ||
signatureBuilder.typeVariable(typeVar.name); | ||
} | ||
|
||
// Handle long variable constraints (for integer variables) | ||
for (const auto& longVar : prestoSignature.longVariableConstraints) { | ||
signatureBuilder.integerVariable(longVar.name); | ||
} | ||
|
||
// Handle return type | ||
signatureBuilder.returnType(prestoSignature.returnType); | ||
|
||
// Handle argument types | ||
for (const auto& argType : prestoSignature.argumentTypes) { | ||
signatureBuilder.argumentType(argType); | ||
} | ||
|
||
// Handle variable arity | ||
if (prestoSignature.variableArity) { | ||
signatureBuilder.variableArity(); | ||
} | ||
|
||
auto signature = signatureBuilder.build(); | ||
std::vector<velox::exec::FunctionSignaturePtr> veloxSignatures = {signature}; | ||
|
||
velox::functions::registerRemoteFunction( | ||
getFunctionName(restFunctionHandle.functionId), | ||
veloxSignatures, | ||
metadata, | ||
false); | ||
|
||
return std::make_shared<CallTypedExpr>( | ||
returnType, args, getFunctionName(restFunctionHandle.functionId)); | ||
} | ||
#endif | ||
|
||
} // namespace | ||
|
||
velox::variant VeloxExprConverter::getConstantValue( | ||
|
@@ -513,6 +629,17 @@ TypedExprPtr VeloxExprConverter::toVeloxExpr( | |
return std::make_shared<CallTypedExpr>( | ||
returnType, args, getFunctionName(sqlFunctionHandle->functionId)); | ||
} | ||
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this logic to a separate function, say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback, I have made the improvements |
||
else if ( | ||
auto restFunctionHandle = | ||
std::dynamic_pointer_cast<protocol::RestFunctionHandle>( | ||
pexpr.functionHandle)) { | ||
auto args = toVeloxExpr(pexpr.arguments); | ||
auto returnType = typeParser_->parse(pexpr.returnType); | ||
|
||
return registerRestRemoteFunction(*restFunctionHandle, args, returnType); | ||
} | ||
#endif | ||
|
||
VELOX_FAIL("Unsupported function handle: {}", pexpr.functionHandle->_type); | ||
} | ||
|
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
123 changes: 123 additions & 0 deletions
123
presto-native-execution/presto_cpp/main/types/tests/PrestoToVeloxExprTest.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,123 @@ | ||
/* | ||
* 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/types/PrestoToVeloxExpr.h" | ||
#include <gtest/gtest.h> | ||
#include "velox/common/base/tests/GTestUtils.h" | ||
#include "velox/functions/remote/client/Remote.h" | ||
#include "velox/functions/remote/server/RemoteFunctionService.h" | ||
|
||
using namespace facebook::presto; | ||
using namespace facebook::velox; | ||
|
||
class RemoteFunctionTest : public ::testing::Test { | ||
protected: | ||
static void SetUpTestCase() { | ||
memory::MemoryManager::testingSetInstance({}); | ||
} | ||
|
||
void SetUp() override { | ||
memoryPool_ = memory::MemoryManager::getInstance()->addLeafPool(); | ||
converter_ = | ||
std::make_unique<VeloxExprConverter>(memoryPool_.get(), &typeParser_); | ||
|
||
functionHandle = std::make_shared<protocol::RestFunctionHandle>(); | ||
functionHandle->functionId = "remote.testSchema.testFunction;BIGINT;BIGINT"; | ||
functionHandle->version = "v1"; | ||
functionHandle->signature.name = "testFunction"; | ||
functionHandle->signature.returnType = "bigint"; | ||
functionHandle->signature.argumentTypes = {"bigint", "bigint"}; | ||
functionHandle->signature.typeVariableConstraints = {}; | ||
functionHandle->signature.longVariableConstraints = {}; | ||
functionHandle->signature.variableArity = false; | ||
|
||
expectedMetadata.serdeFormat = functions::remote::PageFormat::PRESTO_PAGE; | ||
expectedMetadata.functionId = functionHandle->functionId; | ||
expectedMetadata.version = functionHandle->version; | ||
expectedMetadata.schema = "testSchema"; | ||
|
||
testExpr.functionHandle = functionHandle; | ||
testExpr.returnType = "bigint"; | ||
testExpr.displayName = "testFunction"; | ||
auto cexpr = std::make_shared<protocol::ConstantExpression>(); | ||
cexpr->type = "bigint"; | ||
cexpr->valueBlock.data = "CgAAAExPTkdfQVJSQVkBAAAAAAEAAAAAAAAA"; | ||
testExpr.arguments.push_back(cexpr); | ||
|
||
auto cexpr2 = std::make_shared<protocol::ConstantExpression>(); | ||
cexpr2->type = "bigint"; | ||
cexpr2->valueBlock.data = "CgAAAExPTkdfQVJSQVkBAAAAAAEAAAAAAAAA"; | ||
testExpr.arguments.push_back(cexpr2); | ||
} | ||
|
||
std::unique_ptr<config::ConfigBase> restSystemConfig( | ||
const std::unordered_map<std::string, std::string> configOverride = {}) | ||
const { | ||
std::unordered_map<std::string, std::string> systemConfig{ | ||
{std::string(SystemConfig::kRemoteFunctionServerSerde), | ||
std::string("presto_page")}, | ||
{std::string(SystemConfig::kRemoteFunctionServerRestURL), | ||
std::string("http://localhost:8080")}}; | ||
|
||
for (const auto& [configName, configValue] : configOverride) { | ||
systemConfig[configName] = configValue; | ||
} | ||
return std::make_unique<config::ConfigBase>(std::move(systemConfig), true); | ||
} | ||
|
||
std::shared_ptr<protocol::RestFunctionHandle> functionHandle; | ||
protocol::CallExpression testExpr; | ||
functions::RemoteVectorFunctionMetadata expectedMetadata; | ||
std::shared_ptr<memory::MemoryPool> memoryPool_; | ||
TypeParser typeParser_; | ||
std::unique_ptr<VeloxExprConverter> converter_; | ||
}; | ||
|
||
TEST_F(RemoteFunctionTest, HandlesRestFunctionCorrectly) { | ||
try { | ||
auto restConfig = restSystemConfig(); | ||
auto systemConfig = SystemConfig::instance(); | ||
systemConfig->initialize(std::move(restConfig)); | ||
auto expr = converter_->toVeloxExpr(testExpr); | ||
auto callExpr = std::dynamic_pointer_cast<const core::CallTypedExpr>(expr); | ||
ASSERT_NE(callExpr, nullptr); | ||
EXPECT_EQ(callExpr->name(), "remote.testSchema.testFunction"); | ||
|
||
EXPECT_EQ(callExpr->inputs().size(), 2); | ||
auto arg0 = std::dynamic_pointer_cast<const core::ConstantTypedExpr>( | ||
callExpr->inputs()[0]); | ||
auto arg1 = std::dynamic_pointer_cast<const core::ConstantTypedExpr>( | ||
callExpr->inputs()[1]); | ||
ASSERT_NE(arg0, nullptr); | ||
ASSERT_NE(arg1, nullptr); | ||
EXPECT_EQ(arg0->type()->kind(), TypeKind::BIGINT); | ||
EXPECT_EQ(arg1->type()->kind(), TypeKind::BIGINT); | ||
|
||
} catch (const std::exception& e) { | ||
FAIL() << "Exception: " << e.what(); | ||
} | ||
} | ||
|
||
TEST_F(RemoteFunctionTest, UnsupportedSerdeFormat) { | ||
std::unordered_map<std::string, std::string> restConfigOverride{ | ||
{std::string(SystemConfig::kRemoteFunctionServerSerde), | ||
std::string("spark_unsafe_rows")}}; | ||
auto restConfig = restSystemConfig(restConfigOverride); | ||
auto systemConfig = SystemConfig::instance(); | ||
systemConfig->initialize(std::move(restConfig)); | ||
|
||
VELOX_ASSERT_THROW( | ||
converter_->toVeloxExpr(testExpr), | ||
"presto_page serde is expected by remote function server but got : 'spark_unsafe_rows'"); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please revert this formatting change.