[ 
https://issues.apache.org/jira/browse/CALCITE-5345?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18149271#comment-18149271
 ] 

Yu Xu edited comment on CALCITE-5345 at 11/22/26, 12:40 PM:
------------------------------------------------------------

CALCITE-6169  removed the scale check and replaced `longValue()` with 
`longValueExact()`—which, instead of throwing an `ArithmeticException`, now 
returns `false` for out-of-range values. A side effect of this is that 
`CAST(1.0 DECIMAL → INT)` is now folded into the literal `1`. Consequently, the 
constant map includes the entry `$0 → 1`, allowing optimization rules to hoist 
this constant. In other words, this issue should now be resolved; I will add 
further tests to verify this.


was (Author: JIRAUSER307770):
CALCITE-6169 (https://issues.apache.org/jira/browse/CALCITE-6169) removed the 
scale check and replaced `longValue()` with `longValueExact()`—which, instead 
of throwing an `ArithmeticException`, now returns `false` for out-of-range 
values. A side effect of this is that `CAST(1.0 DECIMAL → INT)` is now folded 
into the literal `1`. Consequently, the constant map includes the entry `$0 → 
1`, allowing optimization rules to hoist this constant. In other words, this 
issue should now be resolved; I will add further tests to verify this.

> UnionPullUpConstantsRule could also pull up constants requiring a cast
> ----------------------------------------------------------------------
>
>                 Key: CALCITE-5345
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5345
>             Project: Calcite
>          Issue Type: Improvement
>          Components: core
>    Affects Versions: 1.32.0
>            Reporter: Alessandro Solimando
>            Assignee: Alessandro Solimando
>            Priority: Major
>
> Consider the following SQL query:
> {code:java}
> select deptno, ename from emp where deptno = 1.0
> union all
> select deptno, ename from emp where deptno = 1.0
> {code}
> The associated plan is as follows:
> {code:java}
> LogicalUnion(all=[true])
>   LogicalProject(DEPTNO=[$1], ENAME=[$0])
>     LogicalFilter(condition=[=(CAST($1):DECIMAL(11, 1) NOT NULL, 1.0)])
>       LogicalProject(ENAME=[$1], DEPTNO=[$7])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
>   LogicalProject(DEPTNO=[$1], ENAME=[$0])
>     LogicalFilter(condition=[=(CAST($1):DECIMAL(11, 1) NOT NULL, 1.0)])
>       LogicalProject(ENAME=[$1], DEPTNO=[$7])
>         LogicalTableScan(table=[[CATALOG, SALES, EMP]]){code}
> Note that since _deptno_ is of type {_}int{_}, a cast is needed in the filter 
> ({_}i.e., LogicalFilter(condition=[=(CAST($1):DECIMAL(11, 1) NOT NULL, 
> 1.0)]){_}).
> {_}UnionPullUpConstantsRule{_}, as currently written, processes only 
> (pulled-up) predicates of the form "{_}=($i, $literal){_}", while now that 
> CALCITE-5337 is present, it could also process "{_}=(CAST($i, $type), 
> $literal){_}", because the need of a cast is recognized and the cast added in 
> the projection when the constant is pulled up (if needed).
> The aforementioned query would be optimized in this way:
> {code:java}
> LogicalProject(DEPTNO=[1], ENAME=[$0])
>   LogicalUnion(all=[true])
>     LogicalProject(ENAME=[$0])
>       LogicalFilter(condition=[=(CAST($1):DECIMAL(11, 1) NOT NULL, 1.0)])
>         LogicalProject(ENAME=[$1], DEPTNO=[$7])
>           LogicalTableScan(table=[[CATALOG, SALES, EMP]])
>     LogicalProject(ENAME=[$0])
>       LogicalFilter(condition=[=(CAST($1):DECIMAL(11, 1) NOT NULL, 1.0)])
>         LogicalProject(ENAME=[$1], DEPTNO=[$7])
>           LogicalTableScan(table=[[CATALOG, SALES, EMP]]){code}
> Without this improvement, the plan would not change after applying 
> {_}UnionPullUpConstantsRule{_}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to