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

xiong duan commented on CALCITE-5156:
-------------------------------------

[~julianhyde] [~Chunwei Lei] Here is the basic reason for the undesirable plan:
{code:java}
select cast(deptno as integer) IS NOT NULL  from "scott".dept;
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[CAST($t0):INTEGER NOT NULL], 
expr#4=[IS NOT NULL($t3)], EXPR$0=[$t4])
  EnumerableTableScan(table=[[scott, DEPT]])
!plan

select deptno IS NOT NULL  from "scott".dept;
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], EXPR$0=[$t3])
  EnumerableTableScan(table=[[scott, DEPT]])
!plan {code}
The Calcite can optimize `deptno IS NOT NULL` to TRUE. But  `cast(deptno as 
integer) IS NOT NULL` don't. If it is necessary, I will create another issue to 
log it.

> Support implicit number type cast for IN Sub-query
> --------------------------------------------------
>
>                 Key: CALCITE-5156
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5156
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.30.0
>            Reporter: xiong duan
>            Assignee: xiong duan
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> The SQL:
> {code:java}
> select * from dept where deptno + 20 in (select deptno from dept);{code}
> Calcite returns the wrong answer.
> but the SQL
>  
> {code:java}
> select * from dept where deptno + 20 in (select cast(deptno as integer) from 
> dept);{code}
> Calcite returns the correct answer.
> So when we generate the RelNode, we can add the type cast.
> Before the type cast:
> {noformat}
> LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2])
>   LogicalFilter(condition=[IN(+($0, 20), {
> LogicalProject(DEPTNO=[$0])
>   LogicalTableScan(table=[[scott, DEPT]])
> })])
>     LogicalTableScan(table=[[scott, DEPT]]){noformat}
> After the type cast:
> {noformat}
> LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2])
>   LogicalFilter(condition=[IN(+($0, 20), {
> LogicalProject(EXPR$0=[CAST($0):INTEGER NOT NULL])
>   LogicalTableScan(table=[[scott, DEPT]])
> })])
>     LogicalTableScan(table=[[scott, DEPT]]){noformat}
> Same SQL includes:
> {code:java}
> select *
> from dept
> where deptno in (select sal-780 from emp){code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to