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

Add optimization to remove redundant LIMIT that is more than input row count

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.36.0
    • core

    Description

      In calcite,Limit would be represented by Sort,such as `LogicalSort[fetch = 5]`.

      When the Limit' source row number is less than the Limit's fetch,we could remove the the redundant Limit.

      For example:

      SELECT * FROM (VALUES 1,2,3,4,5,6) AS t1 LIMIT 10 

      The plan tree is :

       LogicalSort(fetch=[10])
        LogicalProject(t1=[$0])
          LogicalValues(tuples=[[{ 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }]]) 

      Because the Limit's source max row number is 6,the Limit's fetch is 10,so we could remove the redundant Limit.

      Another example is :

      SELECT count(*) FROM orders LIMIT 2 

      The plan tree is :

       LogicalSort(fetch=[2])
        LogicalAggregate(group=[{}], EXPR$0=[COUNT()])
          LogicalTableScan(table=[[tpch, ORDERS]]) 

      Because Limit's source max row number is 1,the Limit's fetch is 2, so we could remove the redundant Limit.

      The logic is same as presto's RemoveRedundantLimit rule:https://github.com/prestodb/presto/blob/50fbc07111ecca60a1a5e62755f095fa204120d0/presto-main/src/main/java/com/facebook/presto/sql/planner/iterative/rule/RemoveRedundantLimit.java#L27

      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: