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_aggregates
velox_caching
velox_common_base
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 @@ -1546,6 +1546,26 @@ void PrestoServer::registerSidecarEndpoints() {
proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, getFunctionsMetadata());
});
rowExpressionOptimizer_ =
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be better to have a single global function for RowExpressionOptimizer->optimize for registration here (It could construct a RowExpressionOptimizer internally though). Lets avoid constructing the object here.

std::make_unique<expression::RowExpressionOptimizer>();
httpServer_->registerPost(
"/v1/expressions",
[&](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 result =
rowExpressionOptimizer_->optimize(message, 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);
}
});
httpServer_->registerPost(
"/v1/velox/plan",
[server = this](
Expand Down
2 changes: 2 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 @@ -291,6 +292,7 @@ class PrestoServer {
std::string address_;
std::string nodeLocation_;
folly::SSLContextPtr sslContext_;
std::unique_ptr<expression::RowExpressionOptimizer> rowExpressionOptimizer_;
};

} // namespace facebook::presto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ add_library(presto_velox_conversion OBJECT VeloxPlanConversion.cpp)

target_link_libraries(presto_velox_conversion velox_type)

add_library(presto_expression_optimizer RowExpressionConverter.cpp
RowExpressionOptimizer.cpp)

target_link_libraries(presto_expression_optimizer presto_type_converter
presto_types presto_protocol)

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 Expand Up @@ -102,6 +80,15 @@ std::string getFunctionName(const protocol::SqlFunctionId& functionId) {

} // namespace

const std::unordered_map<std::string, std::string> veloxToPrestoOperatorMap() {
std::unordered_map<std::string, std::string> veloxToPrestoOperatorMap;
for (const auto& entry : kPrestoOperatorMap) {
veloxToPrestoOperatorMap[entry.second] = entry.first;
}
veloxToPrestoOperatorMap.insert({"cast", "presto.default.$operator$cast"});
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't follow why this is separated and added in this function. Please can you elaborate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed for the cast e2e tests, it was added separately so the existing map kPrestoOperatorMap is not modified. It is also needed only in this function veloxToPrestoOperatorMap() which returns an inverse map, and is not needed in the original kPrestoOperatorMap.
Please let me know if this is fine or whether it should also be added in kPrestoOperatorMap.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this is for specfic test functions, then it might be better to make this change in the test function logic instead of here. Its not particularly server side logic then.

return veloxToPrestoOperatorMap;
}

velox::variant VeloxExprConverter::getConstantValue(
const velox::TypePtr& type,
const protocol::Block& block) const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@

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"},
};

const std::unordered_map<std::string, std::string> veloxToPrestoOperatorMap();

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