You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by mb...@gmail.com on 2023/06/02 20:33:16 UTC

Algebraic optimizations rule

Hello,

 

I was looking for rules that apply algebraic optimizations to RexNode
expressions, such as 0 + x = x, but I couldn't find any.

Is there such a rule?

 

Mihai


Re: Algebraic optimizations rule

Posted by Julian Hyde <jh...@gmail.com>.
Be careful when applying algebraic optimizations to computer numbers. For example,

 a + (b - c) = (a + b) - c

doesn’t hold if a, b and c are floating point numbers, a and “b - c” are small, and b and c are large. “a + (b - c)” will experience less underflow and will deliver a more accurate result.

Julian
 

> On Jun 2, 2023, at 7:15 PM, <mb...@gmail.com> <mb...@gmail.com> wrote:
> 
> I have seen the constant folding rules you mention in [2], but these seen to require all operands to be compile-time known.
> Algebraic optimization rules can work even when only some of the operands are constant, such as addition of an arbitrary expression with zero.
> 
> I will take a look at the simplification code to see whether it handles such cases.
> 
> Thank you,
> Mihai
> 
> -----Original Message-----
> From: Jiajun Xie 
> Sent: Friday, June 02, 2023 7:08 PM
> To: dev@calcite.apache.org
> Subject: Re: Algebraic optimizations rule
> 
> Hello, mbudiu:
> 
> Based on my experience, constant folding can occur in two stages.
> - Enable it by `RelBuilder$Config$withSimplify`[1] in the convert stage
> - Add `RelOptRules$CONSTANT_REDUCTION_RULES`[2] in the optimize stage
> 
> If your RexNode is not complex, you can directly use RexSimplify. For example ```[3]
>    final RexSimplify simplify =
>        new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
> RexUtil.EXECUTOR)
>            .withParanoid(true);
>    return simplify.simplifyUnknownAs(e, RexUnknownAs.UNKNOWN); ```
> 
> 
> [1]
> https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L4841
> [2]
> https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/plan/RelOptRules.java#L132
> [3]
> https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/test/java/org/apache/calcite/rex/RexProgramTestBase.java#L187
> 
> On Sat, 3 Jun 2023 at 04:33, <mb...@gmail.com> wrote:
> 
>> Hello,
>> 
>> 
>> 
>> I was looking for rules that apply algebraic optimizations to RexNode 
>> expressions, such as 0 + x = x, but I couldn't find any.
>> 
>> Is there such a rule?
>> 
>> 
>> 
>> Mihai
>> 
>> 
> 


RE: Algebraic optimizations rule

Posted by mb...@gmail.com.
I have seen the constant folding rules you mention in [2], but these seen to require all operands to be compile-time known.
Algebraic optimization rules can work even when only some of the operands are constant, such as addition of an arbitrary expression with zero.

I will take a look at the simplification code to see whether it handles such cases.

Thank you,
Mihai

-----Original Message-----
From: Jiajun Xie 
Sent: Friday, June 02, 2023 7:08 PM
To: dev@calcite.apache.org
Subject: Re: Algebraic optimizations rule

Hello, mbudiu:

Based on my experience, constant folding can occur in two stages.
- Enable it by `RelBuilder$Config$withSimplify`[1] in the convert stage
- Add `RelOptRules$CONSTANT_REDUCTION_RULES`[2] in the optimize stage

If your RexNode is not complex, you can directly use RexSimplify. For example ```[3]
    final RexSimplify simplify =
        new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
RexUtil.EXECUTOR)
            .withParanoid(true);
    return simplify.simplifyUnknownAs(e, RexUnknownAs.UNKNOWN); ```


[1]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L4841
[2]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/plan/RelOptRules.java#L132
[3]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/test/java/org/apache/calcite/rex/RexProgramTestBase.java#L187

On Sat, 3 Jun 2023 at 04:33, <mb...@gmail.com> wrote:

> Hello,
>
>
>
> I was looking for rules that apply algebraic optimizations to RexNode 
> expressions, such as 0 + x = x, but I couldn't find any.
>
> Is there such a rule?
>
>
>
> Mihai
>
>


Re: Algebraic optimizations rule

Posted by Jiajun Xie <ji...@gmail.com>.
Hello, mbudiu:

Based on my experience, constant folding can occur in two stages.
- Enable it by `RelBuilder$Config$withSimplify`[1] in the convert stage
- Add `RelOptRules$CONSTANT_REDUCTION_RULES`[2] in the optimize stage

If your RexNode is not complex, you can directly use RexSimplify. For
example
```[3]
    final RexSimplify simplify =
        new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
RexUtil.EXECUTOR)
            .withParanoid(true);
    return simplify.simplifyUnknownAs(e, RexUnknownAs.UNKNOWN);
```


[1]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L4841
[2]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/main/java/org/apache/calcite/plan/RelOptRules.java#L132
[3]
https://github.com/apache/calcite/blob/8ea4160f10e95aca6c3b0029d505bbc56975a873/core/src/test/java/org/apache/calcite/rex/RexProgramTestBase.java#L187

On Sat, 3 Jun 2023 at 04:33, <mb...@gmail.com> wrote:

> Hello,
>
>
>
> I was looking for rules that apply algebraic optimizations to RexNode
> expressions, such as 0 + x = x, but I couldn't find any.
>
> Is there such a rule?
>
>
>
> Mihai
>
>