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

     1  # =============================================================================
     2  # numeric.opt contains normalization rules for numeric operators.
     3  # =============================================================================
     4  
     5  # FoldPlusZero folds $left + 0 for numeric types.
     6  #
     7  # Note: It is necessary to cast $left to the column type of the binary
     8  # operation since the type of $left may not match the column type. For example,
     9  # 1::int + 0::decimal should result in 1::decimal, not 1::int. The execution
    10  # engine panics when it expects one type but receives another, so this cast is
    11  # essential. If $left is already of the correct type, the cast will be removed
    12  # by the EliminateCast rule. Otherwise, if $left is a constant, the cast will
    13  # be folded away by the FoldCast rule.
    14  [FoldPlusZero, Normalize]
    15  (Plus $left:* $right:(Const 0))
    16  =>
    17  (Cast $left (BinaryType Plus $left $right))
    18  
    19  # FoldZeroPlus folds 0 + $right for numeric types.
    20  [FoldZeroPlus, Normalize]
    21  (Plus $left:(Const 0) $right:*)
    22  =>
    23  (Cast $right (BinaryType Plus $left $right))
    24  
    25  # FoldMinusZero folds $left - 0 for numeric types. This rule requires a check
    26  # that $left is numeric because JSON - INT is valid and is not a no-op with a
    27  # zero value.
    28  [FoldMinusZero, Normalize]
    29  (Minus $left:(IsAdditiveType (TypeOf $left)) $right:(Const 0))
    30  =>
    31  (Cast $left (BinaryType Minus $left $right))
    32  
    33  # FoldMultOne folds $left * 1 for numeric types.
    34  [FoldMultOne, Normalize]
    35  (Mult $left:* $right:(Const 1))
    36  =>
    37  (Cast $left (BinaryType Mult $left $right))
    38  
    39  # FoldOneMult folds 1 * $right for numeric types.
    40  [FoldOneMult, Normalize]
    41  (Mult $left:(Const 1) $right:*)
    42  =>
    43  (Cast $right (BinaryType Mult $left $right))
    44  
    45  # FoldDivOne folds $left / 1 for numeric types.
    46  [FoldDivOne, Normalize]
    47  (Div | FloorDiv $left:* $right:(Const 1))
    48  =>
    49  (Cast $left (BinaryType (OpName) $left $right))
    50  
    51  # InvertMinus rewrites -(a - b) to (b - a) if the operand types allow it.
    52  [InvertMinus, Normalize]
    53  (UnaryMinus
    54      (Minus $left:* $right:*) &
    55          (CanConstructBinary Minus $right $left)
    56  )
    57  =>
    58  (Minus $right $left)
    59  
    60  # EliminateUnaryMinus discards a doubled UnaryMinus operator.
    61  [EliminateUnaryMinus, Normalize]
    62  (UnaryMinus (UnaryMinus $input:*))
    63  =>
    64  $input