github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/norm/rules/ordering.opt (about)

     1  # =============================================================================
     2  # ordering.opt contains normalization patterns that try to simplify ordering
     3  # directives by removing redundant columns. For example:
     4  #
     5  #   SELECT * FROM abc ORDER BY a, b
     6  #
     7  # If (a) is a key for the table, then its value is unique in every row. Since
     8  # subsequent sort key columns are only used when there are duplicate values in
     9  # previous sort key columns, the (b) sort key column will never be used.
    10  #
    11  # This is one example, but there are several ways to simplify based on
    12  # functional dependence theory (see [6]) that take into account constant
    13  # columns, equivalent columns, and the functional relationships between columns.
    14  # See OrderingChoice.Simplify and FuncDepSet for more details.
    15  #
    16  # Citations: [6]
    17  # =============================================================================
    18  
    19  # SimplifyLimitOrdering removes redundant columns from the Limit operator's
    20  # input ordering.
    21  [SimplifyLimitOrdering, Normalize]
    22  (Limit
    23      $input:*
    24      $limit:*
    25      $ordering:* &
    26          (CanSimplifyLimitOffsetOrdering $input $ordering)
    27  )
    28  =>
    29  (Limit
    30      $input
    31      $limit
    32      (SimplifyLimitOffsetOrdering $input $ordering)
    33  )
    34  
    35  # SimplifyOffsetOrdering removes redundant columns from the Offset operator's
    36  # input ordering.
    37  [SimplifyOffsetOrdering, Normalize]
    38  (Offset
    39      $input:*
    40      $offset:*
    41      $ordering:* &
    42          (CanSimplifyLimitOffsetOrdering $input $ordering)
    43  )
    44  =>
    45  (Offset
    46      $input
    47      $offset
    48      (SimplifyLimitOffsetOrdering $input $ordering)
    49  )
    50  
    51  # SimplifyGroupByOrdering removes redundant columns from the GroupBy operators'
    52  # input ordering.
    53  #
    54  # Note: Doesn't match EnsureDistinctOn because test cases were too difficult to
    55  # find. If a test case for EnsureDistinctOn is found, it should be added to the
    56  # match pattern.
    57  [SimplifyGroupByOrdering, Normalize]
    58  (GroupBy | ScalarGroupBy | DistinctOn
    59      $input:*
    60      $aggregations:*
    61      $groupingPrivate:* &
    62          (CanSimplifyGroupingOrdering $input $groupingPrivate)
    63  )
    64  =>
    65  ((OpName)
    66      $input
    67      $aggregations
    68      (SimplifyGroupingOrdering $input $groupingPrivate)
    69  )
    70  
    71  # SimplifyOrdinalityOrdering removes redundant columns from the Ordinality
    72  # operator's input ordering.
    73  [SimplifyOrdinalityOrdering, Normalize]
    74  (Ordinality
    75      $input:*
    76      $ordinalityPrivate:* &
    77          (CanSimplifyOrdinalityOrdering $input $ordinalityPrivate)
    78  )
    79  =>
    80  (Ordinality
    81      $input
    82      (SimplifyOrdinalityOrdering $input $ordinalityPrivate)
    83  )
    84  
    85  # SimplifyExplainOrdering removes redundant columns from the Explain operator's
    86  # input ordering.
    87  [SimplifyExplainOrdering, Normalize]
    88  (Explain
    89      $input:*
    90      $explainPrivate:* &
    91          (CanSimplifyExplainOrdering $input $explainPrivate)
    92  )
    93  =>
    94  (Explain $input (SimplifyExplainOrdering $input $explainPrivate))