Skip to content

Commit 3767a71

Browse files
committed
feat(native): support custom schemas in native sidecar function registry
1 parent 3837f97 commit 3767a71

File tree

14 files changed

+346
-9
lines changed

14 files changed

+346
-9
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,18 @@ void PrestoServer::registerSidecarEndpoints() {
16691669
proxygen::ResponseHandler* downstream) {
16701670
http::sendOkResponse(downstream, getFunctionsMetadata());
16711671
});
1672+
httpServer_->registerGet(
1673+
R"(/v1/functions/([^/]+))",
1674+
[](proxygen::HTTPMessage* /*message*/,
1675+
const std::vector<std::string>& pathMatch) {
1676+
return new http::CallbackRequestHandler(
1677+
[catalog = pathMatch[1]](
1678+
proxygen::HTTPMessage* /*message*/,
1679+
std::vector<std::unique_ptr<folly::IOBuf>>& /*body*/,
1680+
proxygen::ResponseHandler* downstream) {
1681+
http::sendOkResponse(downstream, getFunctionsMetadata(catalog));
1682+
});
1683+
});
16721684
httpServer_->registerPost(
16731685
"/v1/velox/plan",
16741686
[server = this](

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ if(PRESTO_ENABLE_ARROW_FLIGHT_CONNECTOR)
2020
endif()
2121

2222
target_link_libraries(presto_connectors presto_velox_expr_conversion
23-
velox_type_fbhive)
23+
velox_type_fbhive presto_hive_functions)
24+
25+
add_subdirectory(hive)

presto-native-execution/presto_cpp/main/connectors/PrestoToVeloxConnector.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
*/
1414

1515
#include "presto_cpp/main/connectors/PrestoToVeloxConnector.h"
16+
#include <mutex>
17+
#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
18+
#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h"
19+
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h"
1620
#include "presto_cpp/main/types/PrestoToVeloxExpr.h"
1721
#include "presto_cpp/main/types/TypeParser.h"
1822
#include "presto_cpp/presto_protocol/connector/hive/HiveConnectorProtocol.h"
1923
#include "presto_cpp/presto_protocol/connector/iceberg/IcebergConnectorProtocol.h"
2024
#include "presto_cpp/presto_protocol/connector/tpch/TpchConnectorProtocol.h"
25+
#include "velox/functions/FunctionRegistry.h"
2126

2227
#include <velox/type/fbhive/HiveTypeParser.h>
2328
#include "velox/connectors/hive/HiveConnector.h"
@@ -52,6 +57,14 @@ void registerPrestoToVeloxConnector(
5257
connectorName);
5358
protocol::registerConnectorProtocol(
5459
connectorName, std::move(connectorProtocol));
60+
61+
// Register hive-specific functions when hive catalog is detected.
62+
// Delegate to generic Hive native function registrar which is idempotent.
63+
if (connectorName ==
64+
velox::connector::hive::HiveConnectorFactory::kHiveConnectorName ||
65+
connectorName == std::string("hive-hadoop2")) {
66+
hive::functions::registerHiveNativeFunctions();
67+
}
5568
}
5669

5770
void unregisterPrestoToVeloxConnector(const std::string& connectorName) {
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: 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+
add_library(presto_hive_functions HiveFunctionRegistration.cpp)
13+
target_link_libraries(presto_hive_functions velox_functions_prestosql)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 <mutex>
18+
19+
#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h"
20+
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h"
21+
#include "velox/functions/FunctionRegistry.h"
22+
23+
namespace facebook::presto::hive::functions {
24+
25+
namespace {
26+
void registerHiveFunctions() {
27+
// Register functions under the 'hive.default' namespace.
28+
facebook::presto::registerPrestoFunction<InitCapFunction, Varchar, Varchar>(
29+
"initcap", "hive.default");
30+
}
31+
} // namespace
32+
33+
void registerHiveNativeFunctions() {
34+
static std::once_flag once;
35+
std::call_once(once, []() { registerHiveFunctions(); });
36+
}
37+
38+
} // 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+
using namespace facebook::velox;
21+
using namespace facebook::velox::functions;
22+
namespace facebook::presto::hive::functions {
23+
24+
/// The InitCapFunction capitalizes the first character of each word in a
25+
/// string, and lowercases the rest.
26+
template <typename T>
27+
struct InitCapFunction {
28+
VELOX_DEFINE_FUNCTION_TYPES(T);
29+
30+
static constexpr bool is_default_ascii_behavior = true;
31+
32+
FOLLY_ALWAYS_INLINE void call(
33+
out_type<Varchar>& result,
34+
const arg_type<Varchar>& input) {
35+
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<Varchar>& result,
44+
const arg_type<Varchar>& input) {
45+
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

presto-native-execution/presto_cpp/main/functions/FunctionMetadata.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,66 @@ json getFunctionsMetadata() {
319319
return j;
320320
}
321321

322+
json getFunctionsMetadata(const std::string& catalog) {
323+
json j;
324+
325+
// Get metadata for all registered scalar functions in velox.
326+
const auto signatures = getFunctionSignatures();
327+
static const std::unordered_set<std::string> kBlockList = {
328+
"row_constructor", "in", "is_null"};
329+
// Exclude aggregate companion functions (extract aggregate companion
330+
// functions are registered as vector functions).
331+
const auto aggregateFunctions = exec::aggregateFunctions().copy();
332+
for (const auto& entry : signatures) {
333+
const auto name = entry.first;
334+
// Skip internal functions. They don't have any prefix.
335+
if (kBlockList.count(name) != 0 ||
336+
name.find("$internal$") != std::string::npos ||
337+
getScalarMetadata(name).companionFunction) {
338+
continue;
339+
}
340+
341+
const auto parts = getFunctionNameParts(name);
342+
if (parts[0] != catalog) {
343+
continue;
344+
}
345+
const auto schema = parts[1];
346+
const auto function = parts[2];
347+
j[function] = buildScalarMetadata(name, schema, entry.second);
348+
}
349+
350+
// Get metadata for all registered aggregate functions in velox.
351+
for (const auto& entry : aggregateFunctions) {
352+
if (!aggregateFunctions.at(entry.first).metadata.companionFunction) {
353+
const auto name = entry.first;
354+
const auto parts = getFunctionNameParts(name);
355+
if (parts[0] != catalog) {
356+
continue;
357+
}
358+
const auto schema = parts[1];
359+
const auto function = parts[2];
360+
j[function] =
361+
buildAggregateMetadata(name, schema, entry.second.signatures);
362+
}
363+
}
364+
365+
// Get metadata for all registered window functions in velox. Skip aggregates
366+
// as they have been processed.
367+
const auto& functions = exec::windowFunctions();
368+
for (const auto& entry : functions) {
369+
if (aggregateFunctions.count(entry.first) == 0) {
370+
const auto name = entry.first;
371+
const auto parts = getFunctionNameParts(entry.first);
372+
if (parts[0] != catalog) {
373+
continue;
374+
}
375+
const auto schema = parts[1];
376+
const auto function = parts[2];
377+
j[function] = buildWindowMetadata(name, schema, entry.second.signatures);
378+
}
379+
}
380+
381+
return j;
382+
}
383+
322384
} // namespace facebook::presto

presto-native-execution/presto_cpp/main/functions/FunctionMetadata.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ namespace facebook::presto {
2121
// Returns metadata for all registered functions as json.
2222
nlohmann::json getFunctionsMetadata();
2323

24+
// Returns metadata for all registered functions filtered by catalog as json.
25+
nlohmann::json getFunctionsMetadata(const std::string& catalog);
26+
2427
} // namespace facebook::presto

0 commit comments

Comments
 (0)