[ https://issues.apache.org/jira/browse/CALCITE-5930?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17754933#comment-17754933 ]
libopeng commented on CALCITE-5930: ----------------------------------- I found that in sqlToRel, if the data type of the join condition is inconsistent, the data type will be forcibly changed. {code:java} select d.* from dept d inner join ( select count(*) c from emp where comm is null) e on e.c=d.deptno; --------------------------------------------------------------------------------------------- LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2]) LogicalJoin(condition=[=($4, $3)], joinType=[inner]) LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2], DEPTNO0=[CAST($0):BIGINT NOT NULL]) <---here has cast LogicalTableScan(table=[[scott, DEPT]]) LogicalAggregate(group=[{}], C=[COUNT()]) LogicalFilter(condition=[IS NULL($6)]) LogicalTableScan(table=[[scott, EMP]]){code} If the join is converted from in, the condition does not perform a type change. {code:java} select * from dept where deptno in ( select count(*) from emp where comm is null); --------------------------------------------------------------------------------------------- LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[$2]) LogicalJoin(condition=[=($0, $3)], joinType=[inner]) LogicalTableScan(table=[[scott, DEPT]]) <----not cast LogicalAggregate(group=[{0}]) LogicalAggregate(group=[{}], EXPR$0=[COUNT()]) LogicalFilter(condition=[IS NULL($6)]) LogicalTableScan(table=[[scott, EMP]]) {code} Therefore, I think adding 'cast' when converting in to join can solve this problem. If anyone has any other suggestions, please let me know. > When data types do not match, calcite may make a calculation error > ------------------------------------------------------------------ > > Key: CALCITE-5930 > URL: https://issues.apache.org/jira/browse/CALCITE-5930 > Project: Calcite > Issue Type: Bug > Reporter: libopeng > Priority: Major > > Wrong calculation result > {code:java} > select * > from dept > where deptno in ( > select count(*) > from emp > where comm is null); > +--------+------------+----------+ > | DEPTNO | DNAME | LOC | > +--------+------------+----------+ > +--------+------------+----------+ {code} > Correct calculation results > {code:java} > select * > from dept > where cast(deptno as bigint) in ( > select count(*) > from emp > where comm is null); > +--------+------------+----------+ > | DEPTNO | DNAME | LOC | > +--------+------------+----------+ > | 10 | ACCOUNTING | NEW YORK | > +--------+------------+----------+{code} > The above two cases can be reproduced in agg.iq. > Or look at this submission > [https://github.com/apache/calcite/commit/6cef259f29884fbda3f165d579232285998d1b46]. > > > > -- This message was sent by Atlassian Jira (v8.20.10#820010)