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

Correlated Queries in ON clause do not work when referring to left side

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • core

    Description

      Changes from CALCITE-4210 does not robustly support correlated queries in ON clauses.

      For the short term I am going revert the logic for supporting ON clause and throw an exception when correlated queries occur in ON clause.

      Then I will follow up with a fix that will support single subqueries in on clauses. This will require fixing the offset in Blackboard.register, correctly detect if correlated variable is on the being used and adding a rule RelDecorrelator joined decorrelations.

      Given:

      SELECT *
      FROM dept
      LEFT JOIN bonus ON bonus.job = (
        SELECT emp.job
        FROM  emp
        WHERE  emp.deptno = dept.deptno
      )
      

      Should generate

      LogicalProject(DEPTNO=[$0], NAME=[$1], ENAME=[$2], JOB=[$3], SAL=[$4], COMM=[$5])
        LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{0}]) //requiring dept.DEPTNO as the correlated value
          LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
          LogicalFilter(condition=[=($1, $4)]) //This is comparing bonus.job = emp.job
            LogicalJoin(condition=[true], joinType=[left])
              LogicalTableScan(table=[[CATALOG, SALES, BONUS]])
              LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
                LogicalProject(JOB=[$2])
                  LogicalFilter(condition=[=($7, $cor0.DEPTNO)]) //all employees for a given department 
                    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
      

      But instead generates:

      LogicalProject(DEPTNO=[$0], NAME=[$1], ENAME=[$2], JOB=[$3], SAL=[$4], COMM=[$5])
        LogicalJoin(condition=[=($3, $0)], joinType=[left])
          LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
          LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{0}])
            LogicalTableScan(table=[[CATALOG, SALES, BONUS]])
            LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
              LogicalProject(JOB=[$2])
                LogicalFilter(condition=[=($7, $cor0.DEPTNO)])
                  LogicalTableScan(table=[[CATALOG, SALES, EMP]])
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jamesstarr James Starr
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:

                Time Tracking

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