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

Remove 'ORDER BY ... LIMIT n' when input has at most one row, n >= 1, and there is no 'OFFSET' clause

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.36.0
    • core

    Description

      When a Sort represents 'Order By x Limit n',and its input is guaranteed that is at most one row,we could remove this Sort.

      For example, the sql:

      SELECT count(*) FROM orders ORDER BY 1 LIMIT 10 

      because the SELECT count(*) FROM orders row count is 1, it is at most one row, then we could remove ORDER BY 1 LIMIT 10; after the optimization, the sql is:

      SELECT count(*) FROM orders  

      Above logic are same as Presto/Trino's RemoveRedundantTopN rule:

      This JIRA is enhancement for  CALCITE-5994; in 5994, we restrict Sort must be pure Order(Offset and Limit is null), but after this jira, this optimization could be applied to 'Order By x' Or 'Order By x Limit n'.

      Attachments

        Issue Links

          Activity

            People

              shenlang LakeShen
              shenlang LakeShen
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: