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

When being used with Union, Order by is wrongly associated with the outer query

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: None
    • Fix Version/s: 1.4.0-incubating
    • Component/s: None
    • Labels:
      None

      Description

      select empno from emp t1 where empno = 1 
      union 
      select empno from emp t2 where empno is not null 
      group by empno 
      order by empno
      

      Since union is a set operator, the Order-By is supposed to be associated with the second subquery. However, the Rel Tree generated by Calcite is

      LogicalSort(sort0=[$0], dir0=[ASC])
        LogicalProject(EMPNO=[$0])
          LogicalUnion(all=[false])
            LogicalProject(EMPNO=[$0])
              LogicalFilter(condition=[=($0, 1)])
                LogicalTableScan(table=[[CATALOG, SALES, EMP]])
            LogicalAggregate(group=[{0}])
              LogicalProject(EMPNO=[$0])
                LogicalTableScan(table=[[CATALOG, SALES, EMP]])
      

      where LogicalSort is performed at the end.

      Union-All has the same behavior also.

        Attachments

          Activity

            People

            • Assignee:
              julianhyde Julian Hyde
              Reporter:
              seanhychu Sean Hsuan-Yi Chu
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: