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] Handling Velox constant folding errors in Presto C++ sidecar #24591

Open
pramodsatya opened this issue Feb 18, 2025 · 0 comments
Open
Assignees

Comments

@pramodsatya
Copy link
Contributor

pramodsatya commented Feb 18, 2025

Background

The interface ExpressionOptimizerFactory in Presto SPI allows customization of expression optimization. NativeExpressionOptimizerFactory implements this interface to provide a NativeExpressionOptimizer in this PR, this allows Presto C++ to constant fold expressions in the native execution engine Velox. NativeExpressionOptimizer uses helper class NativeSidecarExpressionInterpreter to collect all input RowExpressions from the query to be optimized (by constant folding) and delegates expression optimization to the Presto C++ sidecar via a http request to the endpoint v1/expressions. The Presto C++ sidecar receives an array of Presto protocol RowExpressions to be optimized in the body of this http request and returns a list of optimized RowExpressions to the Presto coordinator (via the native sidecar plugin).

Description

For each input RowExpression received by the Presto C++ sidecar, helper class RowExpressionOptimizer from the PR is used to optimize it with the following steps:

  1. If the input RowExpression is an instance of SpecialFormExpression, it is optimized using rules borrowed from function visitSpecialForm in RowExpressionInterpreter.java.
  2. The optimized RowExpression from 1 is now the input RowExpression which is to be constant folded in Velox (the optimization rules from 1 will subsequently be moved to Velox)
  3. The input protocol::RowExpression is converted to a Velox typed expression using VeloxExprConverter
  4. This typed expression is compiled in Velox with constant folding enabled to get a Velox exec::Expr
  5. The constant folded Velox expression is converted back to a Presto protocol RowExpression using the helper class RowExpressionConverter (to be added in PR)

During expression compilation in Velox with the constant folding optimization enabled (in step 4), any error encountered during evaluation of the constant expression (such as with expression divide(0,0)) is suppressed with a try/catch block in the function tryFoldIfConstant, and the input expression is returned as-is.

In Presto java, RowExpressions are constant folded during planning in RowExpressionInterpreter.java and any exceptions encountered during constant folding are replaced by a FailFunction (instance of Presto protocol CallExpression) in the function processWithExceptionHandling.

In order to support the same behavior in Presto C++, Velox's expression compiler needs to indicate any errors encountered during constant folding with a suitable error code (such as StandardErrorCode in presto-spi). The Velox change is prototyped in this PR. With this change in Velox's expression compiler, RowExpressionConverter (to be added in PR) could convert the Velox FailFunction to Presto FailFunction (instance of protocol CallExpression) appropriately. This would help achieve consistency between Presto and Presto C++ for constant folding.

Opening this issue to discuss approaches for handling exceptions during constant folding of expressions using the native execution engine.

cc: @aditi-pandit @tdcmeehan @majetideepak @czentgr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant