Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions presto-docs/src/main/sphinx/presto_cpp/sidecar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ The following HTTP endpoints are implemented by the Presto C++ sidecar.
Presto C++ worker. Each function's metadata is serialized to JSON in
format ``JsonBasedUdfFunctionMetadata``.

.. function:: GET /v1/functions/{catalog}

Returns a list of function metadata for all functions registered in the
Presto C++ worker that belong to the specified catalog. Each function's
metadata is serialized to JSON in format ``JsonBasedUdfFunctionMetadata``.
This endpoint allows filtering functions by catalog to support namespace
separation.

.. function:: POST /v1/velox/plan

Converts a Presto plan fragment to its corresponding Velox plan and
Expand Down
1 change: 1 addition & 0 deletions presto-native-execution/presto_cpp/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ target_link_libraries(
presto_operators
presto_session_properties
presto_velox_plan_conversion
presto_hive_functions
velox_abfs
velox_aggregates
velox_caching
Expand Down
20 changes: 20 additions & 0 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "presto_cpp/main/common/Utils.h"
#include "presto_cpp/main/connectors/Registration.h"
#include "presto_cpp/main/connectors/SystemConnector.h"
#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
#include "presto_cpp/main/functions/FunctionMetadata.h"
#include "presto_cpp/main/http/HttpConstants.h"
#include "presto_cpp/main/http/filters/AccessLogFilter.h"
Expand All @@ -50,6 +51,7 @@
#include "velox/common/file/FileSystems.h"
#include "velox/common/memory/SharedArbitrator.h"
#include "velox/connectors/Connector.h"
#include "velox/connectors/hive/HiveConnector.h"
#include "velox/connectors/hive/storage_adapters/abfs/RegisterAbfsFileSystem.h"
#include "velox/connectors/hive/storage_adapters/gcs/RegisterGcsFileSystem.h"
#include "velox/connectors/hive/storage_adapters/hdfs/RegisterHdfsFileSystem.h"
Expand Down Expand Up @@ -1359,6 +1361,12 @@ void PrestoServer::registerFunctions() {
prestoBuiltinFunctionPrefix_);
velox::window::prestosql::registerAllWindowFunctions(
prestoBuiltinFunctionPrefix_);

if (velox::connector::hasConnector(
velox::connector::hive::HiveConnectorFactory::kHiveConnectorName) ||
velox::connector::hasConnector("hive-hadoop2")) {
hive::functions::registerHiveNativeFunctions();
}
}

void PrestoServer::registerRemoteFunctions() {
Expand Down Expand Up @@ -1691,6 +1699,18 @@ void PrestoServer::registerSidecarEndpoints() {
proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, getFunctionsMetadata());
});
httpServer_->registerGet(
R"(/v1/functions/([^/]+))",
[](proxygen::HTTPMessage* /*message*/,
const std::vector<std::string>& pathMatch) {
return new http::CallbackRequestHandler(
[catalog = pathMatch[1]](
proxygen::HTTPMessage* /*message*/,
std::vector<std::unique_ptr<folly::IOBuf>>& /*body*/,
proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, getFunctionsMetadata(catalog));
});
});
httpServer_->registerPost(
"/v1/velox/plan",
[server = this](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ endif()

target_link_libraries(presto_connectors presto_velox_expr_conversion
velox_type_fbhive velox_tpcds_connector)

add_subdirectory(hive)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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_subdirectory(functions)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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_hive_functions HiveFunctionRegistration.cpp)
target_link_libraries(presto_hive_functions presto_dynamic_function_registrar
velox_functions_string)

if(PRESTO_ENABLE_TESTING)
add_subdirectory(tests)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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/connectors/hive/functions/HiveFunctionRegistration.h"

#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h"
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h"

using namespace facebook::velox;
namespace facebook::presto::hive::functions {

namespace {
void registerHiveFunctions() {
// Register functions under the 'hive.default' namespace.
facebook::presto::registerPrestoFunction<InitCapFunction, Varchar, Varchar>(
"initcap", "hive.default");
}
} // namespace

void registerHiveNativeFunctions() {
static std::once_flag once;
std::call_once(once, []() { registerHiveFunctions(); });
}

} // namespace facebook::presto::hive::functions
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

namespace facebook::presto::hive::functions {

// Registers Hive-specific native functions into the 'hive.default' namespace.
// This method is safe to call multiple times; it performs one-time registration
// guarded by an internal call_once.
void registerHiveNativeFunctions();

} // namespace facebook::presto::hive::functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 "velox/functions/Macros.h"
#include "velox/functions/lib/string/StringImpl.h"

namespace facebook::presto::hive::functions {

/// The InitCapFunction capitalizes the first character of each word in a
/// string, and lowercases the rest.
template <typename T>
struct InitCapFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

// ASCII input always produces ASCII result.
static constexpr bool is_default_ascii_behavior = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this unused variable.


FOLLY_ALWAYS_INLINE void call(
out_type<velox::Varchar>& result,
const arg_type<velox::Varchar>& input) {
velox::functions::stringImpl::initcap<
/*strictSpace=*/false,
/*isAscii=*/false,
/*turkishCasing=*/true,
/*greekFinalSigma=*/true>(result, input);
}

FOLLY_ALWAYS_INLINE void callAscii(
out_type<velox::Varchar>& result,
const arg_type<velox::Varchar>& input) {
velox::functions::stringImpl::initcap<
/*strictSpace=*/false,
/*isAscii=*/true,
/*turkishCasing=*/true,
/*greekFinalSigma=*/true>(result, input);
}
};

} // namespace facebook::presto::hive::functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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_executable(presto_hive_functions_test InitcapTest.cpp)

add_test(
NAME presto_hive_functions_test
COMMAND presto_hive_functions_test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(
presto_hive_functions_test presto_hive_functions presto_common
velox_functions_test_lib GTest::gtest GTest::gtest_main)
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 <gtest/gtest.h>

#include "presto_cpp/main/connectors/hive/functions/HiveFunctionRegistration.h"
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"

namespace facebook::presto::functions::test {
class InitcapTest : public velox::functions::test::FunctionBaseTest {
protected:
static void SetUpTestCase() {
velox::functions::test::FunctionBaseTest::SetUpTestCase();
facebook::presto::hive::functions::registerHiveNativeFunctions();
}
};

TEST_F(InitcapTest, initcap) {
const auto initcap = [&](const std::optional<std::string>& value) {
return evaluateOnce<std::string>("\"hive.default.initcap\"(c0)", value);
};

// Unicode only.
EXPECT_EQ(
initcap("àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ"),
"Àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ");
EXPECT_EQ(initcap("αβγδεζηθικλμνξοπρςστυφχψ"), "Αβγδεζηθικλμνξοπρςστυφχψ");
// Mix of ascii and unicode.
EXPECT_EQ(initcap("αβγδεζ world"), "Αβγδεζ World");
EXPECT_EQ(initcap("αfoo wβ"), "Αfoo Wβ");
// Ascii only.
EXPECT_EQ(initcap("hello world"), "Hello World");
EXPECT_EQ(initcap("HELLO WORLD"), "Hello World");
EXPECT_EQ(initcap("1234"), "1234");
EXPECT_EQ(initcap("a b c d"), "A B C D");
EXPECT_EQ(initcap("abcd"), "Abcd");
// Numbers.
EXPECT_EQ(initcap("123"), "123");
EXPECT_EQ(initcap("1abc"), "1abc");
// Edge cases.
EXPECT_EQ(initcap(""), "");
EXPECT_EQ(initcap(std::nullopt), std::nullopt);

// Test with various whitespace characters
EXPECT_EQ(initcap("YQ\tY"), "Yq\tY");
EXPECT_EQ(initcap("YQ\nY"), "Yq\nY");
EXPECT_EQ(initcap("YQ\rY"), "Yq\rY");
EXPECT_EQ(initcap("hello\tworld\ntest"), "Hello\tWorld\nTest");
EXPECT_EQ(initcap("foo\r\nbar"), "Foo\r\nBar");

// Test with multiple consecutive whitespaces
EXPECT_EQ(initcap("hello world"), "Hello World");
EXPECT_EQ(initcap("a b c"), "A B C");
EXPECT_EQ(initcap("test\t\tvalue"), "Test\t\tValue");
EXPECT_EQ(initcap("line\n\n\nbreak"), "Line\n\n\nBreak");

// Test with leading and trailing whitespaces
EXPECT_EQ(initcap(" hello"), " Hello");
EXPECT_EQ(initcap("world "), "World ");
EXPECT_EQ(initcap(" spaces "), " Spaces ");
EXPECT_EQ(initcap("\thello"), "\tHello");
EXPECT_EQ(initcap("\nworld"), "\nWorld");
EXPECT_EQ(initcap("test\n"), "Test\n");

// Test with mixed whitespace types
EXPECT_EQ(initcap("hello \t\nworld"), "Hello \t\nWorld");
EXPECT_EQ(initcap("a\tb\nc\rd"), "A\tB\nC\rD");
EXPECT_EQ(initcap(" \t\n "), " \t\n ");
}
} // namespace facebook::presto::functions::test
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ json buildWindowMetadata(

} // namespace

json getFunctionsMetadata() {
json getFunctionsMetadata(const std::optional<std::string>& catalog) {
json j;

// Get metadata for all registered scalar functions in velox.
Expand All @@ -285,6 +285,10 @@ json getFunctionsMetadata() {
}

const auto parts = getFunctionNameParts(name);
// Skip if catalog filter is specified and doesn't match
if (catalog.has_value() && parts[0] != catalog.value()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abstract a lambda for this check.

continue;
}
const auto schema = parts[1];
const auto function = parts[2];
j[function] = buildScalarMetadata(name, schema, entry.second);
Expand All @@ -295,6 +299,10 @@ json getFunctionsMetadata() {
if (!aggregateFunctions.at(entry.first).metadata.companionFunction) {
const auto name = entry.first;
const auto parts = getFunctionNameParts(name);
// Skip if catalog filter is specified and doesn't match
if (catalog.has_value() && parts[0] != catalog.value()) {
continue;
}
const auto schema = parts[1];
const auto function = parts[2];
j[function] =
Expand All @@ -309,6 +317,10 @@ json getFunctionsMetadata() {
if (aggregateFunctions.count(entry.first) == 0) {
const auto name = entry.first;
const auto parts = getFunctionNameParts(entry.first);
// Skip if catalog filter is specified and doesn't match
if (catalog.has_value() && parts[0] != catalog.value()) {
continue;
}
const auto schema = parts[1];
const auto function = parts[2];
j[function] = buildWindowMetadata(name, schema, entry.second.signatures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

#pragma once

#include <optional>
#include "presto_cpp/external/json/nlohmann/json.hpp"

namespace facebook::presto {

// Returns metadata for all registered functions as json.
nlohmann::json getFunctionsMetadata();
nlohmann::json getFunctionsMetadata(
const std::optional<std::string>& catalog = std::nullopt);

} // namespace facebook::presto
Loading
Loading