github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ops/mutation.opt (about) 1 # mutation.opt contains Optgen language definitions for the mutation statement 2 # operator (Insert, Upsert, Update, Delete). 3 4 # Insert evaluates a relational input expression, and inserts values from it 5 # into a target table. The input may be an arbitrarily complex expression: 6 # 7 # INSERT INTO ab SELECT x, y+1 FROM xy ORDER BY y 8 # 9 # It can also be a simple VALUES clause: 10 # 11 # INSERT INTO ab VALUES (1, 2) 12 # 13 # It may also return rows, which can be further composed: 14 # 15 # SELECT a + b FROM [INSERT INTO ab VALUES (1, 2) RETURNING a, b] 16 # 17 # The Insert operator is capable of inserting values into computed columns and 18 # mutation columns, which are not writable (or even visible in the case of 19 # mutation columns) by SQL users. 20 [Relational, Mutation] 21 define Insert { 22 Input RelExpr 23 Checks FKChecksExpr 24 _ MutationPrivate 25 } 26 27 [Private] 28 define MutationPrivate { 29 # Table identifies the table which is being mutated. It is an id that can be 30 # passed to the Metadata.Table method in order to fetch cat.Table metadata. 31 Table TableID 32 33 # InsertCols are columns from the Input expression that will be inserted into 34 # the target table. They must be a subset of the Input expression's output 35 # columns. The count and order of columns corresponds to the count and order 36 # of the target table's columns, including in-progress schema mutation 37 # columns. If any column ID is zero, then that column will not be part of 38 # the insert operation (e.g. delete-only mutation column). Column values are 39 # read from the input columns and are then inserted into the corresponding 40 # table columns. For example: 41 # 42 # INSERT INTO ab VALUES (1, 2) 43 # 44 # If there is a delete-only mutation column "c", then InsertCols would contain 45 # [a_colid, b_colid, 0]. 46 InsertCols ColList 47 48 # FetchCols are columns from the Input expression that will be fetched from 49 # the target table. They must be a subset of the Input expression's output 50 # columns. The count and order of columns corresponds to the count and order 51 # of the target table's columns, including in-progress schema mutation 52 # columns. If any column ID is zero, then that column will not take part in 53 # the update operation (e.g. columns in unreferenced column family). 54 # 55 # Fetch columns are referenced by update, computed, and constraint 56 # expressions. They're also needed to formulate the final key/value pairs; 57 # updating even one column in a family requires the entire value to be 58 # reformulated. For example: 59 # 60 # CREATE TABLE abcd ( 61 # a INT PRIMARY KEY, b INT, c INT, d INT, e INT, 62 # FAMILY (a, b), FAMILY (c, d), FAMILY (e)) 63 # UPDATE ab SET c=c+1 64 # 65 # The (a, c, d) columns need to be fetched from the store in order to satisfy 66 # the UPDATE query. The "a" column is needed because it's in the primary key. 67 # The "c" column is needed because its value is used as part of computing an 68 # updated value, and the "d" column is needed because it's in the same family 69 # as "c". Taking all this into account, FetchCols would contain this list: 70 # [a_colid, 0, c_colid, d_colid, 0]. 71 FetchCols ColList 72 73 # UpdateCols are columns from the Input expression that contain updated values 74 # for columns of the target table. They must be a subset of the Input 75 # expression's output columns. The count and order of columns corresponds to 76 # the count and order of the target table's columns, including in-progress 77 # schema mutation columns. If any column ID is zero, then that column will not 78 # take part in the update operation (e.g. columns that are not updated). 79 # Updated column values are read from the input columns and are then inserted 80 # into the corresponding table columns. For example: 81 # 82 # CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT AS (b+1) AS STORED) 83 # UPDATE abc SET b=1 84 # 85 # Since column "b" is updated, and "c" is a computed column dependent on "b", 86 # then UpdateCols would contain [0, b_colid, c_colid]. 87 UpdateCols ColList 88 89 # CheckCols are columns from the Input expression containing the results of 90 # evaluating the check constraints from the target table. Evaluating a check 91 # check constraint expression produces a boolean value which is projected as 92 # a column and then checked by the mutation operator. Check columns must be 93 # a subset of the Input expression's output columns. The count and order of 94 # columns corresponds to the count and order of the target table's Check 95 # collection (see the opt.Table.CheckCount and opt.Table.Check methods). If 96 # any column ID is zero, then that check will not be performed (i.e. because 97 # it's been statically proved to be true). For example: 98 # 99 # CREATE TABLE abc (a INT CHECK (a > 0), b INT, c INT CHECK (c <> 0)) 100 # UPDATE abc SET a=1, b=b+1 101 # 102 # Since the check constraint for column "a" can be statically proven to be 103 # true, CheckCols would contain [0, b_colid]. 104 # TODO(radu): we don't actually implement this optimization currently. 105 CheckCols ColList 106 107 # IndexPredicateCols are columns from the Input expression containing the 108 # results of evaluating each partial index predicate from the target table 109 # for the mutation. Evaluating a partial index predicate produces a boolean 110 # value which is projected as a column and used during execution to 111 # determine whether or not to write a row to the partial index. The count 112 # and order of columns corresponds to the count and order of the target 113 # table's partial indexes. For example: 114 # 115 # CREATE TABLE abc ( 116 # a INT, b INT, c INT, 117 # INDEX (a) WHERE a > 0, 118 # INDEX (b), 119 # INDEX (c) WHERE c > 5 120 # ) 121 # 122 # In this case there are two columns. The first is the result of evaluating 123 # the predicate expression of the index on a. The second is the result of 124 # evaluating the predicate of the index on c. The index on b is not a 125 # partial index, because it has no predicate, so it is not included in 126 # IndexPredicateCols. 127 IndexPredicateCols ColList 128 129 # CanaryCol is used only with the Upsert operator. It identifies the column 130 # that the execution engine uses to decide whether to insert or to update. 131 # If the canary column value is null for a particular input row, then a new 132 # row is inserted into the table. Otherwise, the existing row is updated. 133 # While CanaryCol is 0 for all non-Upsert operators, it is also 0 for the 134 # "blind" Upsert case in which a "Put" KV operator inserts a new row or 135 # overwrites an existing row. 136 CanaryCol ColumnID 137 138 # ReturnCols are the set of columns returned by the mutation operator when 139 # the RETURNING clause has been specified. By default, the return columns 140 # include all columns in the table, including hidden columns, but not 141 # including any columns that are undergoing mutation (being added or dropped 142 # as part of online schema change). If no RETURNING clause was specified, 143 # then ReturnCols is nil. 144 ReturnCols ColList 145 146 # PassthroughCols are columns that the mutation needs to passthrough from 147 # its input. It's similar to the passthrough columns in projections. This 148 # is useful for `UPDATE .. FROM` mutations where the `RETURNING` clause 149 # references columns from tables in the `FROM` clause. When this happens 150 # the update will need to pass through those refenced columns from its input. 151 PassthroughCols ColList 152 153 # Mutation operators can act similarly to a With operator: they buffer their 154 # input, making it accessible to FK queries. If this is not required, WithID 155 # is zero. 156 WithID WithID 157 158 # FKCascades stores metadata necessary for building cascading queries. 159 FKCascades FKCascades 160 161 # FKFallback is true if we need to fall back to the legacy path for FK 162 # checks / cascades. 163 FKFallback bool 164 } 165 166 # Update evaluates a relational input expression that fetches existing rows from 167 # a target table and computes new values for one or more columns. Arbitrary 168 # subsets of rows can be selected from the target table and processed in order, 169 # as with this example: 170 # 171 # UPDATE abc SET b=10 WHERE a>0 ORDER BY b+c LIMIT 10 172 # 173 # The Update operator will also update any computed columns, including mutation 174 # columns that are computed. 175 [Relational, Mutation] 176 define Update { 177 Input RelExpr 178 Checks FKChecksExpr 179 _ MutationPrivate 180 } 181 182 # Upsert evaluates a relational input expression that tries to insert a new row 183 # into a target table. If a conflicting row already exists, then Upsert will 184 # instead update the existing row. The Upsert operator is used for all of these 185 # syntactic variants: 186 # 187 # INSERT..ON CONFLICT DO UPDATE 188 # INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT (a) DO UPDATE SET b=10 189 # 190 # INSERT..ON CONFLICT DO NOTHING 191 # INSERT INTO abc VALUES (1, 2, 3) ON CONFLICT DO NOTHING 192 # 193 # UPSERT 194 # UPSERT INTO abc VALUES (1, 2, 3) 195 # 196 # The Update operator will also insert/update any computed columns, including 197 # mutation columns that are computed. 198 [Relational, Mutation] 199 define Upsert { 200 Input RelExpr 201 Checks FKChecksExpr 202 _ MutationPrivate 203 } 204 205 # Delete is an operator used to delete all rows that are selected by a 206 # relational input expression: 207 # 208 # DELETE FROM abc WHERE a>0 ORDER BY b LIMIT 10 209 # 210 [Relational, Mutation] 211 define Delete { 212 Input RelExpr 213 Checks FKChecksExpr 214 _ MutationPrivate 215 } 216 217 # FKChecks is a list of foreign key check queries, to be run after the main 218 # query. 219 [Scalar, List] 220 define FKChecks { 221 } 222 223 # FKChecksItem is a foreign key check query, to be run after the main query. 224 # An execution error will be generated if the query returns any results. 225 [Scalar, ListItem] 226 define FKChecksItem { 227 Check RelExpr 228 _ FKChecksItemPrivate 229 } 230 231 [Private] 232 define FKChecksItemPrivate { 233 OriginTable TableID 234 ReferencedTable TableID 235 236 # If FKOutbound is true: this item checks that a new value in the origin 237 # table has a valid reference. The FK constraint is 238 # OutboundForeignKey(FKOrdinal) on the origin table. 239 # 240 # If FKOutbound is false: this item checks that a removed value from the 241 # referenced table doesn't orphan references to it from the origin table. 242 # The FK constraint is InboundForeignKey(FKOrdinal) on the referenced table. 243 FKOutbound bool 244 FKOrdinal int 245 246 # KeyCols are the columns in the Check query that form the value tuple shown 247 # in the error message. 248 KeyCols ColList 249 250 # OpName is the name that should be used for this check in error messages. 251 OpName string 252 }