Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-3031

Support for correlated ANY/SOME/ALL sub-query

    XMLWordPrintableJSON

    Details

      Description

      Repro:

      @Test public void testSelectAnyCorrelated() {
          final String sql = "select empno > any(select deptno from dept where emp.job = dept.name) from emp\n"
              ;
          checkSubQuery(sql).withLateDecorrelation(true).check();
        }
      

      Error:

      java.lang.AssertionError: correlation id $cor0 not found in correlation list []
      
      	at org.apache.calcite.util.Litmus$1.fail(Litmus.java:31)
      	at org.apache.calcite.rex.RexChecker.visitCorrelVariable(RexChecker.java:174)
      	at org.apache.calcite.rex.RexChecker.visitCorrelVariable(RexChecker.java:57)
      	at org.apache.calcite.rex.RexCorrelVariable.accept(RexCorrelVariable.java:47)
      	at org.apache.calcite.rex.RexVisitorImpl.visitFieldAccess(RexVisitorImpl.java:98)
      	at org.apache.calcite.rex.RexChecker.visitFieldAccess(RexChecker.java:149)
      	at org.apache.calcite.rex.RexChecker.visitFieldAccess(RexChecker.java:57)
      	at org.apache.calcite.rex.RexFieldAccess.accept(RexFieldAccess.java:81)
      	at org.apache.calcite.rex.RexChecker.visitCall(RexChecker.java:140)
      	at org.apache.calcite.rex.RexChecker.visitCall(RexChecker.java:57)
      	at org.apache.calcite.rex.RexCall.accept(RexCall.java:191)
      	at org.apache.calcite.rel.core.Filter.isValid(Filter.java:120)
      	at org.apache.calcite.test.SqlToRelConverterTest$RelValidityChecker.visit(SqlToRelConverterTest.java:3312)
      	at org.apache.calcite.rel.SingleRel.childrenAccept(SingleRel.java:72)
      	at org.apache.calcite.rel.RelVisitor.visit(RelVisitor.java:44)
      

      The plan after SubQueryRemoveRule is:

      LogicalProject(EXPR$0=[CAST(OR(AND(IS TRUE(>($0, $9)), <>($10, 0)), AND(>($10, $11), null, <>($10, 0), IS NOT TRUE(>($0, $9))), AND(>($0, $9), <>($10, 0), IS NOT TRUE(>($0, $9)), <=($10, $11)))):BOOLEAN NOT NULL])
        LogicalJoin(condition=[true], joinType=[inner])
          LogicalTableScan(table=[[CATALOG, SALES, EMP]])
          LogicalAggregate(group=[{}], m=[MIN($0)], c=[COUNT()], d=[COUNT($0)])
            LogicalProject(DEPTNO=[$0])
              LogicalFilter(condition=[=($cor0.JOB, $1)])
                LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
      

      It should be a Correlate, instead of a Join.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                vgarg Vineet Garg
                Reporter:
                hyuan Haisheng Yuan
              • Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 50m
                  50m