-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(native): Support custom schemas in native sidecar function registry #26236
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,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_library(presto_hive_functions HiveFunctionRegistration.cpp) | ||
target_link_libraries(presto_hive_functions velox_functions_prestosql) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 <mutex> | ||
|
||
#include "presto_cpp/main/connectors/hive/functions/InitcapFunction.h" | ||
#include "presto_cpp/main/functions/dynamic_registry/DynamicFunctionRegistrar.h" | ||
#include "velox/functions/FunctionRegistry.h" | ||
|
||
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,53 @@ | ||
/* | ||
* 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" | ||
|
||
using namespace facebook::velox; | ||
using namespace facebook::velox::functions; | ||
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); | ||
|
||
static constexpr bool is_default_ascii_behavior = true; | ||
|
||
FOLLY_ALWAYS_INLINE void call( | ||
out_type<Varchar>& result, | ||
const arg_type<Varchar>& input) { | ||
stringImpl::initcap< | ||
/*strictSpace=*/false, | ||
/*isAscii=*/false, | ||
/*turkishCasing=*/true, | ||
/*greekFinalSigma=*/true>(result, input); | ||
} | ||
|
||
FOLLY_ALWAYS_INLINE void callAscii( | ||
out_type<Varchar>& result, | ||
const arg_type<Varchar>& input) { | ||
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 |
---|---|---|
|
@@ -319,4 +319,66 @@ json getFunctionsMetadata() { | |
return j; | ||
} | ||
|
||
json getFunctionsMetadata(const std::string& catalog) { | ||
json j; | ||
|
||
// Get metadata for all registered scalar functions in velox. | ||
const auto signatures = getFunctionSignatures(); | ||
static const std::unordered_set<std::string> kBlockList = { | ||
"row_constructor", "in", "is_null"}; | ||
// Exclude aggregate companion functions (extract aggregate companion | ||
// functions are registered as vector functions). | ||
const auto aggregateFunctions = exec::aggregateFunctions().copy(); | ||
for (const auto& entry : signatures) { | ||
const auto name = entry.first; | ||
// Skip internal functions. They don't have any prefix. | ||
if (kBlockList.count(name) != 0 || | ||
name.find("$internal$") != std::string::npos || | ||
getScalarMetadata(name).companionFunction) { | ||
continue; | ||
} | ||
|
||
const auto parts = getFunctionNameParts(name); | ||
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. issue: Potential out-of-bounds access in 'parts' array. Validate that 'parts' contains at least three elements before accessing indices 1 and 2 to prevent undefined behavior. |
||
if (parts[0] != catalog) { | ||
continue; | ||
} | ||
const auto schema = parts[1]; | ||
const auto function = parts[2]; | ||
j[function] = buildScalarMetadata(name, schema, entry.second); | ||
} | ||
|
||
// Get metadata for all registered aggregate functions in velox. | ||
for (const auto& entry : aggregateFunctions) { | ||
if (!aggregateFunctions.at(entry.first).metadata.companionFunction) { | ||
const auto name = entry.first; | ||
const auto parts = getFunctionNameParts(name); | ||
if (parts[0] != catalog) { | ||
continue; | ||
} | ||
const auto schema = parts[1]; | ||
const auto function = parts[2]; | ||
j[function] = | ||
buildAggregateMetadata(name, schema, entry.second.signatures); | ||
} | ||
} | ||
|
||
// Get metadata for all registered window functions in velox. Skip aggregates | ||
// as they have been processed. | ||
const auto& functions = exec::windowFunctions(); | ||
for (const auto& entry : functions) { | ||
if (aggregateFunctions.count(entry.first) == 0) { | ||
const auto name = entry.first; | ||
const auto parts = getFunctionNameParts(entry.first); | ||
if (parts[0] != catalog) { | ||
continue; | ||
} | ||
const auto schema = parts[1]; | ||
const auto function = parts[2]; | ||
j[function] = buildWindowMetadata(name, schema, entry.second.signatures); | ||
} | ||
} | ||
|
||
return j; | ||
} | ||
|
||
} // namespace facebook::presto |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ using namespace facebook::presto; | |
using json = nlohmann::json; | ||
|
||
static const std::string kPrestoDefaultPrefix = "presto.default."; | ||
static const std::string kDefaultSchema = "default"; | ||
|
||
class FunctionMetadataTest : public ::testing::Test { | ||
protected: | ||
|
@@ -114,3 +113,46 @@ TEST_F(FunctionMetadataTest, transformKeys) { | |
TEST_F(FunctionMetadataTest, variance) { | ||
testFunction("variance", "Variance.json", 5); | ||
} | ||
|
||
TEST_F(FunctionMetadataTest, GetFunctionsMetadataWithCatalog) { | ||
// Test with the "presto" catalog that is registered in SetUpTestSuite | ||
std::string catalog = "presto"; | ||
auto metadata = getFunctionsMetadata(catalog); | ||
|
||
// The result should be a JSON object with function names as keys | ||
ASSERT_TRUE(metadata.is_object()); | ||
ASSERT_FALSE(metadata.empty()); | ||
|
||
// Verify that common functions are present | ||
ASSERT_TRUE(metadata.contains("abs")); | ||
ASSERT_TRUE(metadata.contains("mod")); | ||
|
||
// Each function should have an array of signatures | ||
for (auto it = metadata.begin(); it != metadata.end(); ++it) { | ||
ASSERT_TRUE(it.value().is_array()) << "Function: " << it.key(); | ||
ASSERT_FALSE(it.value().empty()) << "Function: " << it.key(); | ||
|
||
// Each signature should have the required fields | ||
for (const auto& signature : it.value()) { | ||
ASSERT_TRUE(signature.contains("outputType")) << "Function: " << it.key(); | ||
ASSERT_TRUE(signature.contains("paramTypes")) << "Function: " << it.key(); | ||
ASSERT_TRUE(signature.contains("schema")) << "Function: " << it.key(); | ||
ASSERT_TRUE(signature.contains("functionKind")) | ||
<< "Function: " << it.key(); | ||
|
||
// Schema should be "default" since we registered with "presto.default." | ||
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. nitpick (testing): Test assertions could be more robust for schema values. Hardcoding 'default' for the schema may cause the test to fail if registration logic changes or custom schemas are used. Parameterize the expected schema or derive it from the registration logic to improve test resilience. |
||
// prefix | ||
EXPECT_EQ(signature["schema"], "default") << "Function: " << it.key(); | ||
} | ||
} | ||
} | ||
|
||
TEST_F(FunctionMetadataTest, GetFunctionsMetadataWithNonExistentCatalog) { | ||
// Test with a catalog that doesn't exist | ||
std::string catalog = "nonexistent"; | ||
auto metadata = getFunctionsMetadata(catalog); | ||
|
||
// When no functions match, it returns a null JSON value or empty object | ||
// The default json() constructor creates a null value | ||
ASSERT_TRUE(metadata.is_null() || (metadata.is_object() && metadata.empty())); | ||
} |
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.
nitpick: Unused 'is_default_ascii_behavior' constant.
If this constant is not needed, please remove it. If it is reserved for future use or external access, add a comment explaining its purpose.