Thanks! 
I've just tried it out, and it does work for lot of our cases.

But I've noticed that it doesn't fold expressions that could be reordered under 
associativity rules.
For example `+( $1, +( 10, 20 ) )` is folded to `+( $1, 30 )`, but the 
left-deep equivalent `+( +( $1, 10 ), 20 )` is not folded.
I assume that ReduceExpressionsRule does not attempt any kind of reordering of 
associative/commutative operators? (It could be a large search space, I know)
Are there other rules that we can/should apply before ReduceExpressionsRule 
that would allow it to fold the expression `$1 + 10 + 20` regardless of the 
nesting?

Additionally, ReduceExpressionsRule doesn't seem to handle scalar subquery 
expressions.
I think the idea is/was that scalar subquery expressions would be converted to 
joins by the *_SUB_QUERY_TO_CORRELATE CoreRules.
However, for our purposes we retain un-correlated scalar subqueries.
It seems that RexProgramBuilder.RegisterShuttle/RegisterInputShuttle don't 
override the visitSubQuery method and return a RexInputRef as it does for all 
the other expression types.
As a result, when RegisterShuttle/RegisterInputShuttle's visitSubQuery method 
is invoked on the scalar subquery expression, nothing happens, no RexInputRef 
is created for it, and it simply returns the original RexSubQuery.
Then RexProgramBuilder attempts to cast that node as a RexInputRef, and throws 
"org.apache.calcite.rex.RexSubQuery cannot be cast to 
org.apache.calcite.rex.RexLocalRef at 
org.apache.calcite.rex.RexProgramBuilder.registerInput(RexProgramBuilder.java:296)"
(At least, this is what happens in 1.26.0, I haven't checked new versions).
I figure this is something we are just gonna have to work around on our end?

Thanks!
-Ian J. Bertolacci

On 10/25/21, 10:12 PM, "Julian Hyde" <jhyde.apa...@gmail.com> wrote:

    Chunwei is correct.

    If there are expressions that you would expect to constant-folded by that 
rule that aren’t, please log a bug.

    > On Oct 25, 2021, at 7:10 PM, Chunwei Lei <chunwei.l...@gmail.com> wrote:
    > 
    > Hi, Ian.
    > 
    > ReduceExpressionsRule is always used to do constant folding.
    > 
    > 
    > Best,
    > Chunwei
    > 
    > 
    > On Tue, Oct 26, 2021 at 4:34 AM Ian Bertolacci
    > <ian.bertola...@workday.com.invalid> wrote:
    > 
    >> Howdy,
    >> Does Calcite have any mechanism for applying constant folding to RexNodes
    >> in a query?
    >> 
    >> We’ve been wondering why expressions like `1 + 2` don’t get folded into
    >> the constant `3`.
    >> We’re aware of RexSimplify, but it only does constant folding for Boolean
    >> expressions (i.e. logical and (some) comparison operations)
    >> And even with RexSimplify, we find that it still does not perform some
    >> (what we would consider trivial) optimizations on SEARCH expressions.
    >> For example `SEARCH(123:BIGINT, Sarg[123L:BIGINT]:BIGINT)` should get
    >> folded into `true`, but isn’t.
    >> (But that’s a separate conversation).
    >> 
    >> So: is constant folding on all expressions something that already exists
    >> in Calcite?
    >> If so, what should we be looking at to configure Calcite to apply 
constant
    >> folding?
    >> 
    >> Thanks!
    >> -Ian J. Bertolacci
    >> 


Reply via email to