github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/norm/rules/fold_constants.opt (about) 1 # ============================================================================= 2 # fold_constants.opt contains normalization rules for folding constants. 3 # ============================================================================= 4 5 # FoldNullCast discards the cast operator if it has a null input. The resulting 6 # null value has the same type as the Cast operator would have had. 7 [FoldNullCast, Normalize] 8 (Cast $input:(Null) $targetTyp:*) 9 => 10 (Null $targetTyp) 11 12 # FoldNullUnary discards any unary operator with a null input, and replaces it 13 # with a null value having the same type as the unary expression would have. 14 [FoldNullUnary, Normalize] 15 (Unary $input:(Null)) 16 => 17 (FoldNullUnary (OpName) $input) 18 19 # FoldNullBinaryLeft replaces the binary operator with null if its left input 20 # is null and it does not allow null arguments. 21 [FoldNullBinaryLeft, Normalize] 22 (Binary 23 $left:(Null) 24 $right:* & ^(AllowNullArgs (OpName) $left $right) 25 ) 26 => 27 (FoldNullBinary (OpName) $left $right) 28 29 # FoldNullBinaryRight replaces the binary operator with null if its right input 30 # is null and it does not allow null arguments. 31 [FoldNullBinaryRight, Normalize] 32 (Binary 33 $left:* 34 $right:(Null) & ^(AllowNullArgs (OpName) $left $right) 35 ) 36 => 37 (FoldNullBinary (OpName) $left $right) 38 39 # FoldNullInNonEmpty replaces the In/NotIn with null when the left input is 40 # null and the right input is not empty. Null is the unknown value, and if the 41 # set is non-empty, it is unknown whether it's in/not in the set. 42 [FoldNullInNonEmpty, Normalize] 43 (In | NotIn (Null) (Tuple ^[])) 44 => 45 (Null (BoolType)) 46 47 # FoldInEmpty replaces the In with False when the the right input is empty. Note 48 # that this is correct even if the left side is Null, since even an unknown 49 # value can't be in an empty set. 50 [FoldInEmpty, Normalize] 51 (In * (Tuple [])) 52 => 53 (False) 54 55 # FoldNotInEmpty replaces the NotIn with True when the right input is empty. 56 # Note that this is correct even if the left side is Null, since even an unknown 57 # value can't be in an empty set. 58 [FoldNotInEmpty, Normalize] 59 (NotIn * (Tuple [])) 60 => 61 (True) 62 63 # FoldArray evaluates an Array expression with constant inputs. It replaces the 64 # Array with a Const datum with type TArray. 65 [FoldArray, Normalize] 66 (Array $elems:* & (IsListOfConstants $elems) $typ:*) 67 => 68 (FoldArray $elems $typ) 69 70 # FoldBinary evaluates a binary operation over constant inputs, replacing the 71 # entire expression with a constant. The rule applies as long as the evaluation 72 # would not cause an error. Any errors should be saved for execution time, 73 # since it's possible that the given operation will not be executed. For 74 # example: 75 # 76 # SELECT CASE WHEN true THEN 42 ELSE 1/0 END 77 # 78 # In this query, the ELSE clause is not executed, so the divide-by-zero error 79 # should not be triggered. 80 [FoldBinary, Normalize] 81 (Binary 82 $left:* & (IsConstValueOrTuple $left) 83 $right:* & 84 (IsConstValueOrTuple $right) & 85 (Succeeded $result:(FoldBinary (OpName) $left $right)) 86 ) 87 => 88 $result 89 90 # FoldUnary is similar to FoldBinary, but it involves a unary operation over a 91 # single constant input. As with FoldBinary, FoldUnary applies as long as the 92 # evaluation would not cause an error. 93 [FoldUnary, Normalize] 94 (Unary 95 $input:* & 96 (IsConstValueOrTuple $input) & 97 (Succeeded $result:(FoldUnary (OpName) $input)) 98 ) 99 => 100 $result 101 102 # FoldComparison is similar to FoldBinary, but it involves a comparison 103 # operation. As with FoldBinary, FoldComparison applies as long as the 104 # evaluation would not cause an error. 105 [FoldComparison, Normalize] 106 (Comparison 107 $left:* & (IsConstValueOrTuple $left) 108 $right:* & 109 (IsConstValueOrTuple $right) & 110 (Succeeded 111 $result:(FoldComparison (OpName) $left $right) 112 ) 113 ) 114 => 115 $result 116 117 # FoldCast is similar to FoldUnary, but it involves a cast operation. As with 118 # FoldUnary, FoldCast applies as long as the evaluation would not cause an 119 # error. 120 [FoldCast, Normalize] 121 (Cast 122 $input:* 123 $typ:* & 124 (IsConstValueOrTuple $input) & 125 (Succeeded $result:(FoldCast $input $typ)) 126 ) 127 => 128 $result 129 130 # FoldIndirection eliminates a constant array indirection operator applied to an 131 # array with a statically known number of elements, like this: 132 # 133 # ARRAY[i, i+1][1] 134 # ARRAY[1, 2, 3][2] 135 # 136 # The rule replaces the indirection operator with the referenced array element. 137 [FoldIndirection, Normalize] 138 (Indirection 139 $input:* 140 $index:* & 141 (IsConstValueOrTuple $index) & 142 (Succeeded $result:(FoldIndirection $input $index)) 143 ) 144 => 145 $result 146 147 # FoldColumnAccess eliminates a column access operator applied to a tuple value 148 # that is statically constructed, like this: 149 # 150 # (((i, i+1) as foo, bar)).foo 151 # (((1, 2) as foo, bar)).bar 152 # 153 # The rule replaces the column access operator with the referenced tuple 154 # element. 155 [FoldColumnAccess, Normalize] 156 (ColumnAccess 157 $input:* 158 $idx:* & (Succeeded $result:(FoldColumnAccess $input $idx)) 159 ) 160 => 161 $result 162 163 # FoldFunction is similar to FoldBinary, but it involves a function with 164 # constant inputs. As with FoldBinary, FoldFunction applies as long as the 165 # evaluation would not cause an error. Additionally, only certain functions 166 # are safe to fold as part of normalization. Other functions rely on context 167 # that may change between runs of a prepared query. 168 [FoldFunction, Normalize] 169 (Function 170 $args:* & (IsListOfConstants $args) 171 $private:* & 172 (Succeeded $result:(FoldFunction $args $private)) 173 ) 174 => 175 $result 176 177 # FoldEqualsAnyNull, conversts a scalar ANY operation to NULL if the right-hand 178 # side tuple is NULL, e.g. x = ANY(NULL::int[]). See #42562. 179 [FoldEqualsAnyNull, Normalize] 180 (AnyScalar * (Null) *) 181 => 182 (Null (BoolType))