-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support simplification that requires multiple applications of constant folding / simplification #1160
Comments
@rdettai I think it is still relevant (though the One (silly) example
We need to apply at least one pass twice to fully simplify
In this this case if we applied simplify first and then constant evaluation the expression would be folded completely However, for other examples if you apply simplification first that is insufficient
(would need a second call to simplify now) |
Great thanks! This is very clear now. So if I understand correctly, this problem has two solutions:
The solution 1 (similar to what @pjmore proposed in #1128) would likely be more expensive in terms of space because more state needs no be tracked in the subtrees, and more critically would result in a much more complex algorithm. So we have opted for solution 2, which will be easier to implement and test, and the time overhead of walking the tree multiple times will likely be negligible as expression trees are usually rather small (and will grow even smaller at each iteration). (I think this is already what you mentioned in #1128 (review), but I thought a recap wouldn't hurt 😄) |
👍 it is a nice summary; |
I am interested in working on this and #3770 as I have a use case for a capability to simplify deeply nested expressions. E.g. (((col - 10) + 10) *100) / 100 Simplifies to just col Also something like WHERE ((x<1 or y<2) and z<3) and false is just WHERE false Given the age of these discussions, I am wondering if any of the context or recommended approaches may have changed. @alamb any new thoughts on this? |
I think algorithms for such order dependent simplifications look something like:
Perhaps we can use the API that @peter-toth added in #8891 to check if/when the expr is unchanged. The rewrite API for a long time did not have a way to know when the expression was actually rewritten (and thus when to terminate the iteration). I think we have it now |
PR for this is ready for review #10358. Maybe as a follow up feature we should expose the maximum number of iterations as a configuration parameter? |
Is your feature request related to a problem or challenge? Please describe what you are trying to do.
The constant folding and constant evaluation in constant_folding.rs(and added to in #1153) can not fold certain types of expressions where it takes
For example there is an expression like this in #1153
It is entirely evaluateable at query time, however, as formulated in #1153 it will not be folded because it requires the constant evaluator to run after the simplifier (because the simplifier can fill in now()` and then the evaluator will fill in the rest).
However, there are other types of exprs such as
(true or false) != col
which need to have the constant evaluator run before the simplifier to be fully simplified.It is a more general problem where running either the
Simplifier
orConstEvaluator
could allow a second pass of the other to proceedDescribe the solution you'd like
In general this is typically handled with some sort of 'fixed point' algorithm where there is a loop that runs the two passes until the Expr is not changed. It may also be possible to combine the Simplifier pass with the constant rewriter.
Describe alternatives you've considered
I think there are two possible approaches I can think if:
ConstantEval
andSimplify
steps into a single passAdditional context
#1153
The text was updated successfully, but these errors were encountered: