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

Optimizer should remove ORDER BY in sub-query, provided it has no LIMIT or OFFSET

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.18.0
    • 1.19.0
    • core
    • None

    Description

      The following SQL performs sort twice, however inner sort can be eliminated

      select * from (
        select * from "emps" 
      order by "emps"."deptno"
      ) order by 1 desc

      The same goes for (window calculation will sort on its own)

      select row_number() over (order by "emps"."deptno")  from (
        select * from "emps" 
      order by "emps"."deptno" desc
      )

      The same goes for SetOp (union, minus):

      select * from (
        select * from "emps" 
      order by "emps"."deptno"
      ) union select * from (
        select * from "emps" 
      order by "emps"."deptno" desc
      )

      There might be other cases like that (e.g. Aggregate, Join, Exchange, SortExchange)

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              vladimirsitnikov Vladimir Sitnikov
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: