Skip to content
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

[native] Add row expression optimizer #22927

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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 @@ -51,6 +51,7 @@ target_link_libraries(
presto_http
presto_operators
presto_velox_conversion
presto_expression_optimizer
velox_abfs
velox_aggregates
velox_caching
Expand Down
28 changes: 28 additions & 0 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,13 @@ void PrestoServer::registerSidecarEndpoints() {
proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, getFunctionsMetadata());
});
httpServer_->registerPost(
"/v1/expressions",
[&](proxygen::HTTPMessage* message,
const std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream) {
optimizeExpressions(*message, body, downstream);
});
httpServer_->registerPost(
"/v1/velox/plan",
[server = this](
Expand Down Expand Up @@ -1594,4 +1601,25 @@ protocol::NodeStatus PrestoServer::fetchNodeStatus() {
return nodeStatus;
}

void PrestoServer::optimizeExpressions(
const proxygen::HTTPMessage& message,
const std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream) {
json::array_t inputRowExpressions =
json::parse(util::extractMessageBody(body));
auto rowExpressionOptimizer =
std::make_unique<expression::RowExpressionOptimizer>(
nativeWorkerPool_.get());
auto result = rowExpressionOptimizer->optimize(
message.getHeaders(), inputRowExpressions);
if (result.second) {
VELOX_USER_CHECK(
result.first.is_array(),
"Output json should be an array of row expressions");
http::sendOkResponse(downstream, result.first);
} else {
http::sendErrorResponse(downstream, result.first);
}
}

} // namespace facebook::presto
6 changes: 6 additions & 0 deletions presto-native-execution/presto_cpp/main/PrestoServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "presto_cpp/main/PeriodicHeartbeatManager.h"
#include "presto_cpp/main/PrestoExchangeSource.h"
#include "presto_cpp/main/PrestoServerOperations.h"
#include "presto_cpp/main/types/RowExpressionOptimizer.h"
#include "presto_cpp/main/types/VeloxPlanValidator.h"
#include "velox/common/caching/AsyncDataCache.h"
#include "velox/common/memory/MemoryAllocator.h"
Expand Down Expand Up @@ -217,6 +218,11 @@ class PrestoServer {

protocol::NodeStatus fetchNodeStatus();

void optimizeExpressions(
const proxygen::HTTPMessage& message,
const std::vector<std::unique_ptr<folly::IOBuf>>& body,
proxygen::ResponseHandler* downstream);

void populateMemAndCPUInfo();

// Periodically yield tasks if there are tasks queued.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ add_library(presto_velox_conversion OBJECT VeloxPlanConversion.cpp)

target_link_libraries(presto_velox_conversion velox_type)

add_library(presto_expression_converter RowExpressionConverter.cpp)

target_link_libraries(presto_expression_converter presto_type_converter
presto_types presto_protocol)

add_library(presto_expression_optimizer RowExpressionOptimizer.cpp)

target_link_libraries(presto_expression_optimizer presto_expression_converter)

if(PRESTO_ENABLE_TESTING)
add_subdirectory(tests)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,10 @@ std::string toJsonString(const T& value) {
}

std::string mapScalarFunction(const std::string& name) {
static const std::unordered_map<std::string, std::string> kFunctionNames = {
// Operator overrides: com.facebook.presto.common.function.OperatorType
{"presto.default.$operator$add", "presto.default.plus"},
{"presto.default.$operator$between", "presto.default.between"},
{"presto.default.$operator$divide", "presto.default.divide"},
{"presto.default.$operator$equal", "presto.default.eq"},
{"presto.default.$operator$greater_than", "presto.default.gt"},
{"presto.default.$operator$greater_than_or_equal", "presto.default.gte"},
{"presto.default.$operator$is_distinct_from",
"presto.default.distinct_from"},
{"presto.default.$operator$less_than", "presto.default.lt"},
{"presto.default.$operator$less_than_or_equal", "presto.default.lte"},
{"presto.default.$operator$modulus", "presto.default.mod"},
{"presto.default.$operator$multiply", "presto.default.multiply"},
{"presto.default.$operator$negation", "presto.default.negate"},
{"presto.default.$operator$not_equal", "presto.default.neq"},
{"presto.default.$operator$subtract", "presto.default.minus"},
{"presto.default.$operator$subscript", "presto.default.subscript"},
// Special form function overrides.
{"presto.default.in", "in"},
};

std::string lowerCaseName = boost::to_lower_copy(name);

auto it = kFunctionNames.find(lowerCaseName);
if (it != kFunctionNames.end()) {
auto it = kPrestoOperatorMap.find(lowerCaseName);
if (it != kPrestoOperatorMap.end()) {
return it->second;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@

namespace facebook::presto {

static const std::unordered_map<std::string, std::string> kPrestoOperatorMap = {
// Operator overrides: com.facebook.presto.common.function.OperatorType
{"presto.default.$operator$add", "presto.default.plus"},
{"presto.default.$operator$between", "presto.default.between"},
{"presto.default.$operator$divide", "presto.default.divide"},
{"presto.default.$operator$equal", "presto.default.eq"},
{"presto.default.$operator$greater_than", "presto.default.gt"},
{"presto.default.$operator$greater_than_or_equal", "presto.default.gte"},
{"presto.default.$operator$is_distinct_from",
"presto.default.distinct_from"},
{"presto.default.$operator$less_than", "presto.default.lt"},
{"presto.default.$operator$less_than_or_equal", "presto.default.lte"},
{"presto.default.$operator$modulus", "presto.default.mod"},
{"presto.default.$operator$multiply", "presto.default.multiply"},
{"presto.default.$operator$negation", "presto.default.negate"},
{"presto.default.$operator$not_equal", "presto.default.neq"},
{"presto.default.$operator$subtract", "presto.default.minus"},
{"presto.default.$operator$subscript", "presto.default.subscript"},
// Special form function overrides.
{"presto.default.in", "in"},
};

class VeloxExprConverter {
public:
VeloxExprConverter(velox::memory::MemoryPool* pool, TypeParser* typeParser)
Expand Down
Loading
Loading