Skip to content

Commit f530b51

Browse files
committed
Register REST Remote function
1 parent 82a44e3 commit f530b51

File tree

10 files changed

+303
-154
lines changed

10 files changed

+303
-154
lines changed

presto-native-execution/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ option(PRESTO_ENABLE_ABFS "Build ABFS support" OFF)
5757
option(PRESTO_ENABLE_PARQUET "Enable Parquet support" OFF)
5858

5959
# Forwards user input to VELOX_ENABLE_REMOTE_FUNCTIONS.
60-
option(PRESTO_ENABLE_REMOTE_FUNCTIONS "Enable remote function support" OFF)
60+
option(PRESTO_ENABLE_REMOTE_FUNCTIONS "Enable remote function support" ON)
6161

6262
option(PRESTO_ENABLE_TESTING "Enable tests" ON)
6363

presto-native-execution/presto_cpp/main/PrestoServer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,10 +1246,11 @@ void PrestoServer::registerRemoteFunctions() {
12461246
} else {
12471247
VELOX_FAIL(
12481248
"To register remote functions using a json file path you need to "
1249-
"specify the remote server location using '{}', '{}' or '{}'.",
1249+
"specify the remote server location using '{}', '{}' or '{}' or {}.",
12501250
SystemConfig::kRemoteFunctionServerThriftAddress,
12511251
SystemConfig::kRemoteFunctionServerThriftPort,
1252-
SystemConfig::kRemoteFunctionServerThriftUdsPath);
1252+
SystemConfig::kRemoteFunctionServerThriftUdsPath,
1253+
SystemConfig::kRemoteFunctionServerRestURL);
12531254
}
12541255
}
12551256
#endif

presto-native-execution/presto_cpp/main/common/Configs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@ class SystemConfig : public ConfigBase {
600600
static constexpr std::string_view kRemoteFunctionServerThriftUdsPath{
601601
"remote-function-server.thrift.uds-path"};
602602

603+
/// HTTP URL used by the remote function rest server.
604+
static constexpr std::string_view kRemoteFunctionServerRestURL{
605+
"remote-function-server.rest.url"};
606+
603607
/// Path where json files containing signatures for remote functions can be
604608
/// found.
605609
static constexpr std::string_view

presto-native-execution/presto_cpp/main/types/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ add_library(
1818
presto_types OBJECT
1919
PrestoToVeloxQueryPlan.cpp PrestoToVeloxExpr.cpp VeloxPlanValidator.cpp
2020
PrestoToVeloxSplit.cpp PrestoToVeloxConnector.cpp)
21+
2122
add_dependencies(presto_types presto_operators presto_type_converter velox_type
2223
velox_type_fbhive velox_dwio_dwrf_proto)
2324

2425
target_link_libraries(presto_types presto_type_converter velox_type_fbhive
2526
velox_hive_partition_function velox_tpch_gen)
2627

28+
if(PRESTO_ENABLE_REMOTE_FUNCTIONS)
29+
add_dependencies(presto_types velox_expression presto_server_remote_function
30+
velox_functions_remote)
31+
target_link_libraries(presto_types presto_server_remote_function
32+
velox_functions_remote)
33+
endif()
34+
2735
set_property(TARGET presto_types PROPERTY JOB_POOL_LINK presto_link_job_pool)
2836

2937
if(PRESTO_ENABLE_TESTING)

presto-native-execution/presto_cpp/main/types/PrestoToVeloxExpr.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@
1414

1515
#include "presto_cpp/main/types/PrestoToVeloxExpr.h"
1616
#include <boost/algorithm/string/case_conv.hpp>
17+
#include "presto_cpp/main/common/Configs.h"
1718
#include "presto_cpp/presto_protocol/Base64Util.h"
1819
#include "velox/common/base/Exceptions.h"
1920
#include "velox/functions/prestosql/types/JsonType.h"
2021
#include "velox/vector/ComplexVector.h"
2122
#include "velox/vector/ConstantVector.h"
2223
#include "velox/vector/FlatVector.h"
24+
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
25+
#include "presto_cpp/main/JsonSignatureParser.h"
26+
#include "velox/expression/FunctionSignature.h"
27+
#include "velox/functions/remote/client/Remote.h"
28+
#endif
2329

2430
using namespace facebook::velox::core;
31+
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
32+
using facebook::velox::functions::remote::PageFormat;
33+
#endif
2534
using facebook::velox::TypeKind;
2635

2736
namespace facebook::presto {
@@ -412,6 +421,19 @@ std::optional<TypedExprPtr> VeloxExprConverter::tryConvertLike(
412421
returnType, args, getFunctionName(signature));
413422
}
414423

424+
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
425+
PageFormat fromSerdeString(const std::string_view& serdeName) {
426+
if (serdeName == "presto_page") {
427+
return PageFormat::PRESTO_PAGE;
428+
} else if (serdeName == "spark_unsafe_row") {
429+
return PageFormat::SPARK_UNSAFE_ROW;
430+
} else {
431+
VELOX_FAIL(
432+
"Unknown serde name for remote function server: '{}'", serdeName);
433+
}
434+
}
435+
#endif
436+
415437
TypedExprPtr VeloxExprConverter::toVeloxExpr(
416438
const protocol::CallExpression& pexpr) const {
417439
if (auto builtin = std::dynamic_pointer_cast<protocol::BuiltInFunctionHandle>(
@@ -458,10 +480,43 @@ TypedExprPtr VeloxExprConverter::toVeloxExpr(
458480
pexpr.functionHandle)) {
459481
auto args = toVeloxExpr(pexpr.arguments);
460482
auto returnType = typeParser_->parse(pexpr.returnType);
483+
461484
return std::make_shared<CallTypedExpr>(
462485
returnType, args, getFunctionName(sqlFunctionHandle->functionId));
463486
}
487+
#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
488+
else if (
489+
auto RestFunctionHandle =
490+
std::dynamic_pointer_cast<protocol::RestFunctionHandle>(
491+
pexpr.functionHandle)) {
464492

493+
auto args = toVeloxExpr(pexpr.arguments);
494+
auto returnType = typeParser_->parse(pexpr.returnType);
495+
496+
const auto* systemConfig = SystemConfig::instance();
497+
498+
velox::functions::RemoteVectorFunctionMetadata metadata;
499+
metadata.serdeFormat =
500+
fromSerdeString(systemConfig->remoteFunctionServerSerde());
501+
proxygen::URL url(systemConfig->kRemoteFunctionServerRestURL);
502+
metadata.location = url;
503+
504+
json signatureJson;
505+
to_json(signatureJson, RestFunctionHandle->signature);
506+
507+
JsonSignatureParser parser(signatureJson.dump());
508+
for (const auto& [functionName, signatureItems] : parser) {
509+
for (const auto& item : signatureItems) {
510+
velox::functions::registerRemoteFunction(
511+
getFunctionName(RestFunctionHandle->functionId),
512+
{item.signature},
513+
metadata);
514+
}
515+
}
516+
return std::make_shared<CallTypedExpr>(
517+
returnType, args, getFunctionName(RestFunctionHandle->functionId));
518+
}
519+
#endif
465520
VELOX_FAIL("Unsupported function handle: {}", pexpr.functionHandle->_type);
466521
}
467522

presto-native-execution/presto_cpp/main/types/tests/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ target_link_libraries(
6969
${GFLAGS_LIBRARIES}
7070
pthread)
7171

72+
if(PRESTO_ENABLE_REMOTE_FUNCTIONS)
73+
add_dependencies(presto_expressions_test presto_server_remote_function
74+
velox_expression velox_functions_remote)
75+
76+
target_link_libraries(
77+
presto_expressions_test GTest::gmock GTest::gmock_main
78+
presto_server_remote_function velox_expression velox_functions_remote)
79+
80+
endif()
81+
7282
set_property(TARGET presto_expressions_test PROPERTY JOB_POOL_LINK
7383
presto_link_job_pool)
7484

presto-native-execution/presto_cpp/presto_protocol/presto_protocol.cpp

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

presto-native-execution/presto_cpp/presto_protocol/presto_protocol.h

Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)