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

     1  # =============================================================================
     2  # inline.opt contains normalization patterns that replace a variable reference
     3  # with the expression to which that variable is bound. For example:
     4  #
     5  #   SELECT x2+1 FROM (SELECT x+1 AS x2 FROM a)
     6  #
     7  # becomes:
     8  #
     9  #   SELECT (x+1)+1 FROM a
    10  #
    11  # Inlining variables can result in the simplification or even complete
    12  # elimination of operators, or at least in the ability to more freely reorder
    13  # them within the larger relational expression tree. This allows pushing filters
    14  # further down the tree, as well as with pulling them up in the decorrelation
    15  # case.
    16  # =============================================================================
    17  
    18  # InlineConstVar inlines variables which are restricted to be constant, as in
    19  #   SELECT * FROM foo WHERE a = 4 AND a IN (1, 2, 3, 4).
    20  # =>
    21  #   SELECT * FROM foo WHERE a = 4 AND 4 IN (1, 2, 3, 4).
    22  # Note that a single iteration of this rule might not be sufficient to inline
    23  # all variables, in which case it will trigger itself again.
    24  #
    25  # This rule is high priority so that it runs before filter pushdown.
    26  [InlineConstVar, Normalize, HighPriority]
    27  (Select $input:* $filters:* & (CanInlineConstVar $filters))
    28  =>
    29  (Select $input (InlineConstVar $filters))
    30  
    31  # InlineProjectConstants finds variable references in Projections expressions
    32  # that refer to constant input values, and then inlines those constant values
    33  # in place of the corresponding variable references. This sometimes allows
    34  # further simplifications such as constant folding or Project merging.
    35  [InlineProjectConstants, Normalize]
    36  (Project
    37      $input:* &
    38          ^(ColsAreEmpty
    39              $constCols:(FindInlinableConstants $input)
    40          )
    41      $projections:[
    42          ...
    43          $item:* & (ColsIntersect (OuterCols $item) $constCols)
    44          ...
    45      ]
    46      $passthrough:*
    47  )
    48  =>
    49  (Project
    50      $input
    51      (InlineProjectionConstants $projections $input $constCols)
    52      $passthrough
    53  )
    54  
    55  # InlineSelectConstants finds variable references in Filters expressions that
    56  # refer to constant input values, and then inlines those constant values in
    57  # place of the corresponding variable references. This sometimes allows further
    58  # simplifications such as constant folding or generation of constrained scans.
    59  [InlineSelectConstants, Normalize]
    60  (Select
    61      $input:* &
    62          ^(ColsAreEmpty
    63              $constCols:(FindInlinableConstants $input)
    64          )
    65      $filters:[
    66          ...
    67          $item:* & (ColsIntersect (OuterCols $item) $constCols)
    68          ...
    69      ]
    70  )
    71  =>
    72  (Select
    73      $input
    74      (InlineFilterConstants $filters $input $constCols)
    75  )
    76  
    77  # InlineJoinConstantsLeft finds variable references in a join condition that
    78  # refers to constant values projected by the left input. It then inlines those
    79  # constant values in place of the corresponding variable references. This
    80  # sometimes allows further simplifications such as constant folding or filter
    81  # pushdown.
    82  [InlineJoinConstantsLeft, Normalize]
    83  (Join
    84      $left:* &
    85          ^(ColsAreEmpty $constCols:(FindInlinableConstants $left))
    86      $right:*
    87      $on:[
    88          ...
    89          $item:* & (ColsIntersect (OuterCols $item) $constCols)
    90          ...
    91      ]
    92      $private:*
    93  )
    94  =>
    95  ((OpName)
    96      $left
    97      $right
    98      (InlineFilterConstants $on $left $constCols)
    99      $private
   100  )
   101  
   102  # InlineJoinConstantsRight finds variable references in a join condition that
   103  # refers to constant values projected by the right input. It then inlines those
   104  # constant values in place of the corresponding variable references. This
   105  # sometimes allows further simplifications such as constant folding or filter
   106  # pushdown.
   107  [InlineJoinConstantsRight, Normalize]
   108  (Join
   109      $left:*
   110      $right:* &
   111          ^(ColsAreEmpty
   112              $constCols:(FindInlinableConstants $right)
   113          )
   114      $on:[
   115          ...
   116          $item:* & (ColsIntersect (OuterCols $item) $constCols)
   117          ...
   118      ]
   119      $private:*
   120  )
   121  =>
   122  ((OpName)
   123      $left
   124      $right
   125      (InlineFilterConstants $on $right $constCols)
   126      $private
   127  )
   128  
   129  # PushSelectIntoInlinableProject pushes the Select operator into a Project, even
   130  # though the filter references it. This is made possible by inlining the
   131  # references to projected columns so that the Select becomes independent of the
   132  # Project, and therefore can be reordered. This normalization is important for
   133  # enabling Any filter conditions to be pushed down into scans.
   134  #
   135  # This rule is low priority so that it runs after the PushSelectIntoProject
   136  # and MergeProjectProject rules, since those rules are cheaper to match and
   137  # replace.
   138  #
   139  # Example:
   140  #   SELECT * FROM (SELECT x+1 AS x2 FROM xy) WHERE x2=10
   141  #   =>
   142  #   SELECT x+1 AS x2 FROM (SELECT * FROM xy WHERE (x+1)=10)
   143  #
   144  [PushSelectIntoInlinableProject, Normalize, LowPriority]
   145  (Select
   146      (Project
   147          $input:*
   148          $projections:* & (CanInlineProjections $projections)
   149          $passthrough:*
   150      )
   151      $filters:* & ^(FilterHasCorrelatedSubquery $filters)
   152  )
   153  =>
   154  (Project
   155      (Select $input (InlineSelectProject $filters $projections))
   156      $projections
   157      $passthrough
   158  )
   159  
   160  # InlineProjectInProject folds an inner Project operator into an outer Project
   161  # that references each inner synthesized column no more than one time. If there
   162  # are no duplicate references, then there's no benefit to keeping the multiple
   163  # nested projections. This rule simplifies the relational expression tree and
   164  # makes it more likely that other normalization rules will match.
   165  #
   166  # This rule is low priority so that it runs after the MergeProjects rule, since
   167  # that rule is cheaper to match and replace.
   168  #
   169  # Example:
   170  #   SELECT x2*2 FROM (SELECT x+1 AS x2 FROM xy)
   171  #   =>
   172  #   SELECT (x+1)*2 FROM xy
   173  #
   174  [InlineProjectInProject, Normalize, LowPriority]
   175  (Project
   176      $input:(Project * $innerProjections:*)
   177      $projections:*
   178      $passthrough:* &
   179          ^(HasDuplicateRefs
   180              $projections
   181              $passthrough
   182              (ProjectionCols $innerProjections)
   183          )
   184  )
   185  =>
   186  (InlineProjectProject $input $projections $passthrough)