-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[native] Retrieve Json function metadata
- Loading branch information
1 parent
052c5d9
commit 27d780d
Showing
12 changed files
with
658 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
210 changes: 210 additions & 0 deletions
210
presto-native-execution/presto_cpp/main/types/FunctionMetadata.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* | ||
* 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 "velox/exec/Aggregate.h" | ||
#include "velox/exec/AggregateFunctionRegistry.h" | ||
#include "velox/exec/WindowFunction.h" | ||
#include "velox/functions/FunctionRegistry.h" | ||
|
||
#include "presto_cpp/main/types/FunctionMetadata.h" | ||
#include "presto_cpp/presto_protocol/presto_protocol.h" | ||
|
||
using namespace facebook::velox; | ||
using namespace facebook::velox::exec; | ||
|
||
namespace facebook::presto { | ||
|
||
namespace { | ||
|
||
constexpr char const* kDefaultSchema = "default"; | ||
|
||
// The keys in velox function maps are of the format | ||
// catalog.schema.function_name. This utility function extracts the | ||
// function_name from this string. | ||
const std::string getFunctionName(const std::string& registeredFunctionName) { | ||
std::vector<std::string> pieces; | ||
folly::split('.', registeredFunctionName, pieces, true); | ||
return pieces.back(); | ||
} | ||
|
||
const protocol::AggregationFunctionMetadata getAggregationFunctionMetadata( | ||
const std::string& name, | ||
const AggregateFunctionSignature& signature) { | ||
protocol::AggregationFunctionMetadata metadata; | ||
metadata.intermediateType = signature.intermediateType().toString(); | ||
metadata.isOrderSensitive = | ||
getAggregateFunctionEntry(name)->metadata.orderSensitive; | ||
return metadata; | ||
} | ||
|
||
const protocol::RoutineCharacteristics getRoutineCharacteristics( | ||
const FunctionSignature& signature, | ||
const std::string& name, | ||
const protocol::FunctionKind& kind) { | ||
protocol::Determinism determinism; | ||
protocol::NullCallClause nullCallClause; | ||
if (kind == protocol::FunctionKind::SCALAR) { | ||
auto metadata = getFunctionMetadata(name); | ||
VELOX_USER_CHECK(metadata.has_value()); | ||
determinism = metadata.value().deterministic | ||
? protocol::Determinism::DETERMINISTIC | ||
: protocol::Determinism::NOT_DETERMINISTIC; | ||
nullCallClause = metadata.value().defaultNullBehavior | ||
? protocol::NullCallClause::RETURNS_NULL_ON_NULL_INPUT | ||
: protocol::NullCallClause::CALLED_ON_NULL_INPUT; | ||
} else { | ||
// Default metadata values of DETERMINISTIC and CALLED_ON_NULL_INPUT for | ||
// non-scalar functions. | ||
determinism = protocol::Determinism::DETERMINISTIC; | ||
nullCallClause = protocol::NullCallClause::CALLED_ON_NULL_INPUT; | ||
} | ||
|
||
protocol::RoutineCharacteristics routineCharacteristics; | ||
routineCharacteristics.language = | ||
std::make_shared<protocol::Language>(protocol::Language({"CPP"})); | ||
routineCharacteristics.determinism = | ||
std::make_shared<protocol::Determinism>(determinism); | ||
routineCharacteristics.nullCallClause = | ||
std::make_shared<protocol::NullCallClause>(nullCallClause); | ||
return routineCharacteristics; | ||
} | ||
|
||
void updateFunctionMetadata( | ||
const std::string& name, | ||
const FunctionSignature& signature, | ||
protocol::JsonBasedUdfFunctionMetadata& jsonMetadata) { | ||
jsonMetadata.docString = name; | ||
jsonMetadata.schema = kDefaultSchema; | ||
jsonMetadata.outputType = signature.returnType().toString(); | ||
|
||
const std::vector<TypeSignature> types = signature.argumentTypes(); | ||
std::vector<std::string> paramTypes; | ||
for (const auto& type : types) { | ||
paramTypes.emplace_back(type.toString()); | ||
} | ||
jsonMetadata.paramTypes = paramTypes; | ||
} | ||
|
||
const std::vector<protocol::JsonBasedUdfFunctionMetadata> | ||
buildAggregateJsonMetadata( | ||
const std::string& name, | ||
const std::vector<AggregateFunctionSignaturePtr>& signatures) { | ||
std::vector<protocol::JsonBasedUdfFunctionMetadata> jsonMetadataList; | ||
jsonMetadataList.reserve(signatures.size()); | ||
const protocol::FunctionKind kind = protocol::FunctionKind::AGGREGATE; | ||
|
||
for (const auto& signature : signatures) { | ||
protocol::JsonBasedUdfFunctionMetadata jsonMetadata; | ||
jsonMetadata.functionKind = kind; | ||
jsonMetadata.routineCharacteristics = | ||
getRoutineCharacteristics(*signature, name, kind); | ||
jsonMetadata.aggregateMetadata = | ||
std::make_shared<protocol::AggregationFunctionMetadata>( | ||
getAggregationFunctionMetadata(name, *signature)); | ||
|
||
updateFunctionMetadata(name, *signature, jsonMetadata); | ||
jsonMetadataList.emplace_back(jsonMetadata); | ||
} | ||
return jsonMetadataList; | ||
} | ||
|
||
const std::vector<protocol::JsonBasedUdfFunctionMetadata> | ||
buildScalarJsonMetadata( | ||
const std::string& name, | ||
const std::vector<const FunctionSignature*>& signatures) { | ||
std::vector<protocol::JsonBasedUdfFunctionMetadata> jsonMetadataList; | ||
jsonMetadataList.reserve(signatures.size()); | ||
const protocol::FunctionKind kind = protocol::FunctionKind::SCALAR; | ||
|
||
for (const auto& signature : signatures) { | ||
protocol::JsonBasedUdfFunctionMetadata jsonMetadata; | ||
jsonMetadata.functionKind = kind; | ||
jsonMetadata.routineCharacteristics = | ||
getRoutineCharacteristics(*signature, name, kind); | ||
|
||
updateFunctionMetadata(name, *signature, jsonMetadata); | ||
jsonMetadataList.emplace_back(jsonMetadata); | ||
} | ||
return jsonMetadataList; | ||
} | ||
|
||
const std::vector<protocol::JsonBasedUdfFunctionMetadata> | ||
buildWindowJsonMetadata( | ||
const std::string& name, | ||
const std::vector<FunctionSignaturePtr>& signatures) { | ||
std::vector<protocol::JsonBasedUdfFunctionMetadata> jsonMetadataList; | ||
jsonMetadataList.reserve(signatures.size()); | ||
const protocol::FunctionKind kind = protocol::FunctionKind::WINDOW; | ||
|
||
for (const auto& signature : signatures) { | ||
protocol::JsonBasedUdfFunctionMetadata jsonMetadata; | ||
jsonMetadata.functionKind = kind; | ||
jsonMetadata.routineCharacteristics = | ||
getRoutineCharacteristics(*signature, name, kind); | ||
|
||
updateFunctionMetadata(name, *signature, jsonMetadata); | ||
jsonMetadataList.emplace_back(jsonMetadata); | ||
} | ||
return jsonMetadataList; | ||
} | ||
|
||
const std::vector<protocol::JsonBasedUdfFunctionMetadata> getFunctionMetadata( | ||
const std::string& name) { | ||
if (auto aggregateSignatures = getAggregateFunctionSignatures(name)) { | ||
return buildAggregateJsonMetadata(name, aggregateSignatures.value()); | ||
} else if (auto windowSignatures = getWindowFunctionSignatures(name)) { | ||
return buildWindowJsonMetadata(name, windowSignatures.value()); | ||
} else { | ||
auto signatures = getFunctionSignatures(); | ||
if (signatures.find(name) != signatures.end()) { | ||
return buildScalarJsonMetadata(name, signatures.at(name)); | ||
} | ||
} | ||
VELOX_UNREACHABLE("Function kind for {} cannot be determined", name); | ||
} | ||
|
||
} // namespace | ||
|
||
json getJsonMetadataForFunction(const std::string& name) { | ||
auto metadataList = getFunctionMetadata(name); | ||
auto functionName = getFunctionName(name); | ||
json j = json::array(); | ||
json tj; | ||
for (const auto& metadata : metadataList) { | ||
protocol::to_json(tj, metadata); | ||
j.emplace_back(tj); | ||
} | ||
return j; | ||
} | ||
|
||
json getJsonFunctionMetadata() { | ||
// Get all registered aggregate, window, and scalar function names from | ||
// FunctionRegistry. | ||
auto functions = getSortedAggregateNames(); | ||
auto scalarFunctions = getSortedScalarNames(); | ||
for (const auto& scalarFunction : scalarFunctions) { | ||
functions.emplace_back(scalarFunction); | ||
} | ||
auto windowFunctions = getSortedWindowNames(); | ||
for (const auto& windowFunction : windowFunctions) { | ||
functions.emplace_back(windowFunction); | ||
} | ||
|
||
nlohmann::json j; | ||
for (const auto& function : functions) { | ||
j[function] = getJsonMetadataForFunction(function); | ||
} | ||
return j; | ||
} | ||
|
||
} // namespace facebook::presto |
25 changes: 25 additions & 0 deletions
25
presto-native-execution/presto_cpp/main/types/FunctionMetadata.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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 "presto_cpp/external/json/nlohmann/json.hpp" | ||
|
||
namespace facebook::presto { | ||
|
||
nlohmann::json getJsonMetadataForFunction(const std::string& name); | ||
|
||
nlohmann::json getJsonFunctionMetadata(); | ||
|
||
} // namespace facebook::presto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.