Skip to content

Commit 6d3b641

Browse files
authored
feat(native): Support custom schemas in native sidecar function registry (#26236)
## Description Fixes - #25429 - Add hive variant of initcap to `hive.default.` namespace ## Motivation and Context Add support for custom schemas in native sidecar function registry ## Impact Enables functions to be registered in connector specific or custom namespaces. ## Test Plan Included with this PR. ``` == RELEASE NOTES == Prestissimo (Native Execution) Changes * Add support for custom schemas in native sidecar function registry. ```
1 parent 84af5a1 commit 6d3b641

File tree

19 files changed

+640
-10
lines changed

19 files changed

+640
-10
lines changed

presto-docs/src/main/sphinx/presto_cpp/sidecar.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ The following HTTP endpoints are implemented by the Presto C++ sidecar.
2424
Presto C++ worker. Each function's metadata is serialized to JSON in
2525
format ``JsonBasedUdfFunctionMetadata``.
2626

27+
.. function:: GET /v1/functions/{catalog}
28+
29+
Returns a list of function metadata for all functions registered in the
30+
Presto C++ worker that belong to the specified catalog. Each function's
31+
metadata is serialized to JSON in format ``JsonBasedUdfFunctionMetadata``.
32+
This endpoint allows filtering functions by catalog to support namespace
33+
separation.
34+
2735
.. function:: POST /v1/velox/plan
2836

2937
Converts a Presto plan fragment to its corresponding Velox plan and

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ target_link_libraries(
6666
presto_operators
6767
presto_session_properties
6868
presto_velox_plan_conversion
69+
presto_hive_functions
6970
velox_abfs
7071
velox_aggregates
7172
velox_caching

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "presto_cpp/main/common/Utils.h"
2929
#include "presto_cpp/main/connectors/Registration.h"
3030
#include "presto_cpp/main/connectors/SystemConnector.h"
31+
#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
3132
#include "presto_cpp/main/functions/FunctionMetadata.h"
3233
#include "presto_cpp/main/http/HttpConstants.h"
3334
#include "presto_cpp/main/http/filters/AccessLogFilter.h"
@@ -50,6 +51,7 @@
5051
#include "velox/common/file/FileSystems.h"
5152
#include "velox/common/memory/SharedArbitrator.h"
5253
#include "velox/connectors/Connector.h"
54+
#include "velox/connectors/hive/HiveConnector.h"
5355
#include "velox/connectors/hive/storage_adapters/abfs/RegisterAbfsFileSystem.h"
5456
#include "velox/connectors/hive/storage_adapters/gcs/RegisterGcsFileSystem.h"
5557
#include "velox/connectors/hive/storage_adapters/hdfs/RegisterHdfsFileSystem.h"
@@ -1359,6 +1361,12 @@ void PrestoServer::registerFunctions() {
13591361
prestoBuiltinFunctionPrefix_);
13601362
velox::window::prestosql::registerAllWindowFunctions(
13611363
prestoBuiltinFunctionPrefix_);
1364+
1365+
if (velox::connector::hasConnector(
1366+
velox::connector::hive::HiveConnectorFactory::kHiveConnectorName) ||
1367+
velox::connector::hasConnector("hive-hadoop2")) {
1368+
hive::functions::registerHiveNativeFunctions();
1369+
}
13621370
}
13631371

13641372
void PrestoServer::registerRemoteFunctions() {
@@ -1691,6 +1699,18 @@ void PrestoServer::registerSidecarEndpoints() {
16911699
proxygen::ResponseHandler* downstream) {
16921700
http::sendOkResponse(downstream, getFunctionsMetadata());
16931701
});
1702+
httpServer_->registerGet(
1703+
R"(/v1/functions/([^/]+))",
1704+
[](proxygen::HTTPMessage* /*message*/,
1705+
const std::vector<std::string>& pathMatch) {
1706+
return new http::CallbackRequestHandler(
1707+
[catalog = pathMatch[1]](
1708+
proxygen::HTTPMessage* /*message*/,
1709+
std::vector<std::unique_ptr<folly::IOBuf>>& /*body*/,
1710+
proxygen::ResponseHandler* downstream) {
1711+
http::sendOkResponse(downstream, getFunctionsMetadata(catalog));
1712+
});
1713+
});
16941714
httpServer_->registerPost(
16951715
"/v1/velox/plan",
16961716
[server = this](

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ target_link_libraries(
3535
velox_type_fbhive
3636
velox_tpcds_connector
3737
)
38+
39+
add_subdirectory(hive)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
add_subdirectory(functions)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
add_library(presto_hive_functions HiveFunctionRegistration.cpp)
14+
target_link_libraries(
15+
presto_hive_functions
16+
presto_dynamic_function_registrar
17+
velox_functions_string
18+
)
19+
20+
if(PRESTO_ENABLE_TESTING)
21+
add_subdirectory(tests)
22+
endif()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
16+
17+
#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h"
18+
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h"
19+
20+
using namespace facebook::velox;
21+
namespace facebook::presto::hive::functions {
22+
23+
namespace {
24+
void registerHiveFunctions() {
25+
// Register functions under the 'hive.default' namespace.
26+
facebook::presto::registerPrestoFunction<InitCapFunction, Varchar, Varchar>(
27+
"initcap", "hive.default");
28+
}
29+
} // namespace
30+
31+
void registerHiveNativeFunctions() {
32+
static std::once_flag once;
33+
std::call_once(once, []() { registerHiveFunctions(); });
34+
}
35+
36+
} // namespace facebook::presto::hive::functions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
#pragma once
15+
16+
namespace facebook::presto::hive::functions {
17+
18+
// Registers Hive-specific native functions into the 'hive.default' namespace.
19+
// This method is safe to call multiple times; it performs one-time registration
20+
// guarded by an internal call_once.
21+
void registerHiveNativeFunctions();
22+
23+
} // namespace facebook::presto::hive::functions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
#pragma once
16+
17+
#include "velox/functions/Macros.h"
18+
#include "velox/functions/lib/string/StringImpl.h"
19+
20+
namespace facebook::presto::hive::functions {
21+
22+
/// The InitCapFunction capitalizes the first character of each word in a
23+
/// string, and lowercases the rest.
24+
template <typename T>
25+
struct InitCapFunction {
26+
VELOX_DEFINE_FUNCTION_TYPES(T);
27+
28+
// ASCII input always produces ASCII result. This is required for ASCII fast
29+
// path
30+
static constexpr bool is_default_ascii_behavior = true;
31+
32+
FOLLY_ALWAYS_INLINE void call(
33+
out_type<velox::Varchar>& result,
34+
const arg_type<velox::Varchar>& input) {
35+
velox::functions::stringImpl::initcap<
36+
/*strictSpace=*/false,
37+
/*isAscii=*/false,
38+
/*turkishCasing=*/true,
39+
/*greekFinalSigma=*/true>(result, input);
40+
}
41+
42+
FOLLY_ALWAYS_INLINE void callAscii(
43+
out_type<velox::Varchar>& result,
44+
const arg_type<velox::Varchar>& input) {
45+
velox::functions::stringImpl::initcap<
46+
/*strictSpace=*/false,
47+
/*isAscii=*/true,
48+
/*turkishCasing=*/true,
49+
/*greekFinalSigma=*/true>(result, input);
50+
}
51+
};
52+
53+
} // namespace facebook::presto::hive::functions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
add_executable(presto_hive_functions_test InitcapTest.cpp)
14+
15+
add_test(
16+
NAME presto_hive_functions_test
17+
COMMAND presto_hive_functions_test
18+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
19+
)
20+
21+
target_link_libraries(
22+
presto_hive_functions_test
23+
presto_hive_functions
24+
presto_common
25+
velox_functions_test_lib
26+
GTest::gtest
27+
GTest::gtest_main
28+
)

0 commit comments

Comments
 (0)