-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(native): Add endpoint for expression optimization in sidecar #26475
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3de8d32
feat(sidecar): Represent expression optimization result with optimize…
pramodsatya bef95a4
chore(native): Update presto protocol
pramodsatya c364169
feat(native): Add Velox to Presto expression converter
pramodsatya bfc52a9
feat(native): Add expression optimization endpoint in sidecar
pramodsatya f53088f
test(native): Add e2e tests for expression optimization endpoint
pramodsatya d4c428f
docs(native): Document v1/expressions endpoint
tdcmeehan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
1,703 changes: 49 additions & 1,654 deletions
1,703
presto-main-base/src/test/java/com/facebook/presto/sql/TestExpressionInterpreter.java
Large diffs are not rendered by default.
Oops, something went wrong.
1,711 changes: 1,711 additions & 0 deletions
1,711
.../src/test/java/com/facebook/presto/sql/expressions/AbstractTestExpressionInterpreter.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
124 changes: 124 additions & 0 deletions
124
presto-native-execution/presto_cpp/main/types/ExpressionOptimizer.cpp
This file contains hidden or 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,124 @@ | ||
| /* | ||
| * 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/types/ExpressionOptimizer.h" | ||
| #include "presto_cpp/main/common/Configs.h" | ||
| #include "presto_cpp/main/common/Exception.h" | ||
| #include "presto_cpp/main/http/HttpServer.h" | ||
| #include "presto_cpp/main/types/PrestoToVeloxExpr.h" | ||
| #include "presto_cpp/main/types/TypeParser.h" | ||
| #include "presto_cpp/main/types/VeloxToPrestoExpr.h" | ||
| #include "presto_cpp/presto_protocol/core/presto_protocol_core.h" | ||
| #include "velox/core/Expressions.h" | ||
| #include "velox/expression/Expr.h" | ||
| #include "velox/expression/ExprOptimizer.h" | ||
|
|
||
| namespace facebook::presto::expression { | ||
|
|
||
| namespace { | ||
|
|
||
| static const velox::expression::MakeFailExpr kMakeFailExpr = | ||
| [](const std::string& error, | ||
| const velox::TypePtr& type) -> velox::core::TypedExprPtr { | ||
| return std::make_shared<velox::core::CastTypedExpr>( | ||
| type, | ||
| std::vector<velox::core::TypedExprPtr>{ | ||
| std::make_shared<velox::core::CallTypedExpr>( | ||
| velox::UNKNOWN(), | ||
| std::vector<velox::core::TypedExprPtr>{ | ||
| std::make_shared<velox::core::ConstantTypedExpr>( | ||
| velox::VARCHAR(), error)}, | ||
| fmt::format( | ||
| "{}fail", | ||
pramodsatya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| SystemConfig::instance()->prestoDefaultNamespacePrefix()))}, | ||
| false); | ||
| }; | ||
|
|
||
| // Tries to evaluate `expr`, irrespective of its determinism, to a constant | ||
| // value. | ||
| velox::VectorPtr tryEvaluateToConstant( | ||
| const velox::core::TypedExprPtr& expr, | ||
| velox::core::QueryCtx* queryCtx, | ||
| velox::memory::MemoryPool* pool) { | ||
| auto data = | ||
| velox::BaseVector::create<velox::RowVector>(velox::ROW({}), 1, pool); | ||
| velox::core::ExecCtx execCtx{pool, queryCtx}; | ||
| velox::exec::ExprSet exprSet({expr}, &execCtx); | ||
| velox::exec::EvalCtx evalCtx(&execCtx, &exprSet, data.get()); | ||
|
|
||
| const velox::SelectivityVector singleRow(1); | ||
| std::vector<velox::VectorPtr> results(1); | ||
| exprSet.eval(singleRow, evalCtx, results); | ||
| return results.at(0); | ||
| } | ||
|
|
||
| protocol::RowExpressionOptimizationResult optimizeExpression( | ||
| const RowExpressionPtr& input, | ||
| OptimizerLevel& optimizerLevel, | ||
| const VeloxExprConverter& prestoToVeloxConverter, | ||
| const expression::VeloxToPrestoExprConverter& veloxToPrestoConverter, | ||
| velox::core::QueryCtx* queryCtx, | ||
| velox::memory::MemoryPool* pool) { | ||
| protocol::RowExpressionOptimizationResult result; | ||
| const auto expr = prestoToVeloxConverter.toVeloxExpr(input); | ||
| auto optimized = | ||
| velox::expression::optimize(expr, queryCtx, pool, kMakeFailExpr); | ||
|
|
||
| if (optimizerLevel == OptimizerLevel::kEvaluated) { | ||
| try { | ||
| const auto evalResult = tryEvaluateToConstant(optimized, queryCtx, pool); | ||
| optimized = std::make_shared<velox::core::ConstantTypedExpr>(evalResult); | ||
| } catch (const velox::VeloxException& e) { | ||
| result.expressionFailureInfo = | ||
| toNativeSidecarFailureInfo(translateToPrestoException(e)); | ||
| result.optimizedExpression = nullptr; | ||
| return result; | ||
| } catch (const std::exception& e) { | ||
| result.expressionFailureInfo = | ||
| toNativeSidecarFailureInfo(translateToPrestoException(e)); | ||
| result.optimizedExpression = nullptr; | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| result.optimizedExpression = | ||
| veloxToPrestoConverter.getRowExpression(optimized, input); | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::vector<protocol::RowExpressionOptimizationResult> optimizeExpressions( | ||
| const std::vector<RowExpressionPtr>& input, | ||
| const std::string& timezone, | ||
| OptimizerLevel& optimizerLevel, | ||
| velox::core::QueryCtx* queryCtx, | ||
| velox::memory::MemoryPool* pool) { | ||
| TypeParser typeParser; | ||
| const VeloxExprConverter prestoToVeloxConverter(pool, &typeParser); | ||
| const expression::VeloxToPrestoExprConverter veloxToPrestoConverter(pool); | ||
| std::vector<protocol::RowExpressionOptimizationResult> result; | ||
| for (const auto& expression : input) { | ||
| result.push_back(optimizeExpression( | ||
| expression, | ||
| optimizerLevel, | ||
| prestoToVeloxConverter, | ||
| veloxToPrestoConverter, | ||
| queryCtx, | ||
| pool)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace facebook::presto::expression | ||
55 changes: 55 additions & 0 deletions
55
presto-native-execution/presto_cpp/main/types/ExpressionOptimizer.h
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * 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/external/json/nlohmann/json.hpp" | ||
| #include "presto_cpp/presto_protocol/presto_protocol.h" | ||
| #include "velox/common/memory/MemoryPool.h" | ||
| #include "velox/core/QueryCtx.h" | ||
|
|
||
| using RowExpressionPtr = | ||
| std::shared_ptr<facebook::presto::protocol::RowExpression>; | ||
|
|
||
| namespace facebook::presto::expression { | ||
|
|
||
| /// Optimizer level, which indicates the extent to which expressions should be | ||
| /// optimized. | ||
| /// TODO: This should be obtained from Presto protocol after refactoring the | ||
| /// enum from ExpressionOptimizer in Presto SPI. | ||
| enum class OptimizerLevel { | ||
| /// Removes all redundancy in a RowExpression using the ExpressionOptimizer in | ||
| /// Velox. | ||
| kOptimized = 0, | ||
| /// Attempts to evaluate the RowExpression into a constant, even when the | ||
| /// expression is non-deterministic. | ||
| kEvaluated, | ||
| }; | ||
|
|
||
| /// Optimizes the input list of RowExpressions. For each input RowExpression, | ||
| /// the result is an optimized expression on success or failure info. | ||
| /// @param input List of RowExpressions to be optimized. | ||
| /// @param timezone Session timezone, received from Presto coordinator. | ||
| /// @param optimizerLevel Optimizer level, received from Presto coordinator. | ||
pramodsatya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// The optimizerLevel can either be OPTIMIZED or EVALUATED. OPTIMIZED removes | ||
| /// all redundancy in a RowExpression by leveraging the ExpressionOptimizer in | ||
| /// Velox, and EVALUATED attempts to evaluate the RowExpression into a constant | ||
| /// even when the expression is non-deterministic. | ||
| /// @param queryCtx Query context to be used during optimization. | ||
| /// @param pool Memory pool, required for expression evaluation. | ||
| std::vector<protocol::RowExpressionOptimizationResult> optimizeExpressions( | ||
| const std::vector<RowExpressionPtr>& input, | ||
| const std::string& timezone, | ||
| OptimizerLevel& optimizerLevel, | ||
| velox::core::QueryCtx* queryCtx, | ||
| velox::memory::MemoryPool* pool); | ||
| } // namespace facebook::presto::expression | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.