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

Ivan Daschinsky edited comment on CALCITE-4742 at 8/18/21, 11:23 AM:
---------------------------------------------------------------------

[~nobigo] Excuse me, but <> ANY could not be rewritten as not in. For example 
check these two queries
{code}
select * from emp where deptno <> some (select 20 union select 10);
select * from emp where deptno not in (select 20 union select 10);
{code}
 First query returns on postgres with scott dataset
{code}
7369    SMITH   CLERK   7902    1980-12-17      800.00          20
7499    ALLEN   SALESMAN        7698    1981-02-20      1600.00 300.00  30
7521    WARD    SALESMAN        7698    1981-02-22      1250.00 500.00  30
7566    JONES   MANAGER 7839    1981-02-04      2975.00         20
7654    MARTIN  SALESMAN        7698    1981-09-28      1250.00 1400.00 30
7698    BLAKE   MANAGER 7839    1981-01-05      2850.00         30
7782    CLARK   MANAGER 7839    1981-06-09      2450.00         10
7788    SCOTT   ANALYST 7566    1987-04-19      3000.00         20
7839    KING    PRESIDENT               1981-11-17      5000.00         10
7844    TURNER  SALESMAN        7698    1981-09-08      1500.00 0.00    30
7876    ADAMS   CLERK   7788    1987-05-23      1100.00         20
7900    JAMES   CLERK   7698    1981-12-03      950.00          30
7902    FORD    ANALYST 7566    1981-12-03      3000.00         20
7934    MILLER  CLERK   7782    1982-01-23      1300.00         10

{code}
Second returns
{code}
7499    ALLEN   SALESMAN        7698    1981-02-20      1600.00 300.00  30
7521    WARD    SALESMAN        7698    1981-02-22      1250.00 500.00  30
7654    MARTIN  SALESMAN        7698    1981-09-28      1250.00 1400.00 30
7698    BLAKE   MANAGER 7839    1981-01-05      2850.00         30
7844    TURNER  SALESMAN        7698    1981-09-08      1500.00 0.00    30
7900    JAMES   CLERK   7698    1981-12-03      950.00          30

{code}


was (Author: ivandasch):
[~nobigo] Excuse me, but <> ANY could not be rewritten as not in. For example 
check these two queries
{code}
select * from emp where deptno <> some (select 20 union select 10);
select * from emp where deptno not in (select 20 union select 10);
{code}
 First query returns
{code}
7369    SMITH   CLERK   7902    1980-12-17      800.00          20
7499    ALLEN   SALESMAN        7698    1981-02-20      1600.00 300.00  30
7521    WARD    SALESMAN        7698    1981-02-22      1250.00 500.00  30
7566    JONES   MANAGER 7839    1981-02-04      2975.00         20
7654    MARTIN  SALESMAN        7698    1981-09-28      1250.00 1400.00 30
7698    BLAKE   MANAGER 7839    1981-01-05      2850.00         30
7782    CLARK   MANAGER 7839    1981-06-09      2450.00         10
7788    SCOTT   ANALYST 7566    1987-04-19      3000.00         20
7839    KING    PRESIDENT               1981-11-17      5000.00         10
7844    TURNER  SALESMAN        7698    1981-09-08      1500.00 0.00    30
7876    ADAMS   CLERK   7788    1987-05-23      1100.00         20
7900    JAMES   CLERK   7698    1981-12-03      950.00          30
7902    FORD    ANALYST 7566    1981-12-03      3000.00         20
7934    MILLER  CLERK   7782    1982-01-23      1300.00         10

{code}
Second returns
{code}
7499    ALLEN   SALESMAN        7698    1981-02-20      1600.00 300.00  30
7521    WARD    SALESMAN        7698    1981-02-22      1250.00 500.00  30
7654    MARTIN  SALESMAN        7698    1981-09-28      1250.00 1400.00 30
7698    BLAKE   MANAGER 7839    1981-01-05      2850.00         30
7844    TURNER  SALESMAN        7698    1981-09-08      1500.00 0.00    30
7900    JAMES   CLERK   7698    1981-12-03      950.00          30

{code}

> Implement NOT EQUALS in some subquery
> -------------------------------------
>
>                 Key: CALCITE-4742
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4742
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Ivan Daschinsky
>            Assignee: duan xiong
>            Priority: Major
>             Fix For: 1.28.0
>
>
> When executing
> {code}
> select * from "scott".emp where empno <> any (select 10);
> {code}
> with assertions, got assertion error 
> {code}
> ---
> > java.lang.AssertionError
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule.rewriteSome(SubQueryRemoveRule.java:170)
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule.apply(SubQueryRemoveRule.java:92)
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule.matchFilter(SubQueryRemoveRule.java:637)
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule.access$100(SubQueryRemoveRule.java:71)
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule$Config.lambda$static$3(SubQueryRemoveRule.java:701)
> >     at 
> > org.apache.calcite.rel.rules.SubQueryRemoveRule.onMatch(SubQueryRemoveRule.java:82)
> >     at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> >     at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> >     at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> >     at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:282)
> >     at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleCollection.execute(HepInstruction.java:77)
> >     at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> >     at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> >     at org.apache.calcite.tools.Programs.lambda$of$0(Programs.java:176)
> >     at 
> > org.apache.calcite.tools.Programs$SequenceProgram.run(Programs.java:335)
> >     at org.apache.calcite.prepare.Prepare.optimize(Prepare.java:172)
> >     at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:306)
> >     at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:215)
> >     at 
> > org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:647)
> >     at 
> > org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:513)
> >     at 
> > org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:483)
> >     at 
> > org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:249)
> >     at 
> > org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:623)
> {code}
> This query cannot be rewritten as IN query, so it goes to  
> {{SubQueryRemoveRule.rewriteSome}}. Current logic in this method doesn't work 
> on this case and produce wrong plan. 
> This query should be rewritten to something like this:
> {code}
> select distinct(e.*)  from "scott".emp as e inner join (select 10 as empno) 
> as q on e.empno <> q.empno;
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to