@@ -72,20 +72,26 @@ type updateRun struct {
7272 regionLocalInfo regionLocalInfoType
7373}
7474
75+ func (r * updateRun ) initRowContainer (params runParams , columns colinfo.ResultColumns ) {
76+ if ! r .rowsNeeded {
77+ return
78+ }
79+ r .tu .rows = rowcontainer .NewRowContainer (
80+ params .p .Mon ().MakeBoundAccount (),
81+ colinfo .ColTypeInfoFromResCols (columns ),
82+ )
83+ r .resultRowBuffer = make ([]tree.Datum , len (columns ))
84+ for i := range r .resultRowBuffer {
85+ r .resultRowBuffer [i ] = tree .DNull
86+ }
87+ }
88+
7589func (u * updateNode ) startExec (params runParams ) error {
7690 // cache traceKV during execution, to avoid re-evaluating it for every row.
7791 u .run .traceKV = params .p .ExtendedEvalContext ().Tracing .KVTracingEnabled ()
7892
79- if u .run .rowsNeeded {
80- u .run .tu .rows = rowcontainer .NewRowContainer (
81- params .p .Mon ().MakeBoundAccount (),
82- colinfo .ColTypeInfoFromResCols (u .columns ),
83- )
84- u .run .resultRowBuffer = make ([]tree.Datum , len (u .columns ))
85- for i := range u .run .resultRowBuffer {
86- u .run .resultRowBuffer [i ] = tree .DNull
87- }
88- }
93+ u .run .initRowContainer (params , u .columns )
94+
8995 return u .run .tu .init (params .ctx , params .p .txn , params .EvalContext ())
9096}
9197
@@ -126,7 +132,7 @@ func (u *updateNode) BatchedNext(params runParams) (bool, error) {
126132
127133 // Process the update for the current input row, potentially
128134 // accumulating the result row for later.
129- if err := u .processSourceRow (params , u .input .Values ()); err != nil {
135+ if err := u .run . processSourceRow (params , u .input .Values ()); err != nil {
130136 return false , err
131137 }
132138
@@ -164,7 +170,7 @@ func (u *updateNode) BatchedNext(params runParams) (bool, error) {
164170
165171// processSourceRow processes one row from the source for update and, if
166172// result rows are needed, saves it in the result row container.
167- func (u * updateNode ) processSourceRow (params runParams , sourceVals tree.Datums ) error {
173+ func (r * updateRun ) processSourceRow (params runParams , sourceVals tree.Datums ) error {
168174 // sourceVals contains values for the columns from the table, in the order of the
169175 // table descriptor. (One per column in u.tw.ru.FetchCols)
170176 //
@@ -174,42 +180,42 @@ func (u *updateNode) processSourceRow(params runParams, sourceVals tree.Datums)
174180 // oldValues is the prefix of sourceVals that corresponds to real
175181 // stored columns in the table, that is, excluding the RHS assignment
176182 // expressions.
177- oldValues := sourceVals [:len (u . run .tu .ru .FetchCols )]
183+ oldValues := sourceVals [:len (r .tu .ru .FetchCols )]
178184 sourceVals = sourceVals [len (oldValues ):]
179185
180186 // The update values follow the fetch values and their order corresponds to the order of ru.UpdateCols.
181- updateValues := sourceVals [:len (u . run .tu .ru .UpdateCols )]
187+ updateValues := sourceVals [:len (r .tu .ru .UpdateCols )]
182188 sourceVals = sourceVals [len (updateValues ):]
183189
184190 // The passthrough values follow the update values.
185- passthroughValues := sourceVals [:u . run .numPassthrough ]
191+ passthroughValues := sourceVals [:r .numPassthrough ]
186192 sourceVals = sourceVals [len (passthroughValues ):]
187193
188194 // Verify the schema constraints. For consistency with INSERT/UPSERT
189195 // and compatibility with PostgreSQL, we must do this before
190196 // processing the CHECK constraints.
191- if err := enforceNotNullConstraints (updateValues , u . run .tu .ru .UpdateCols ); err != nil {
197+ if err := enforceNotNullConstraints (updateValues , r .tu .ru .UpdateCols ); err != nil {
192198 return err
193199 }
194200
195201 // Run the CHECK constraints, if any. CheckHelper will either evaluate the
196202 // constraints itself, or else inspect boolean columns from the input that
197203 // contain the results of evaluation.
198- if ! u . run .checkOrds .Empty () {
204+ if ! r .checkOrds .Empty () {
199205 if err := checkMutationInput (
200206 params .ctx , params .EvalContext (), & params .p .semaCtx , params .p .SessionData (),
201- u . run . tu .tableDesc (), u . run . checkOrds , sourceVals [:u . run .checkOrds .Len ()],
207+ r . tu .tableDesc (), r . checkOrds , sourceVals [:r .checkOrds .Len ()],
202208 ); err != nil {
203209 return err
204210 }
205- sourceVals = sourceVals [u . run .checkOrds .Len ():]
211+ sourceVals = sourceVals [r .checkOrds .Len ():]
206212 }
207213
208214 // Create a set of partial index IDs to not add entries or remove entries
209215 // from. Put values are followed by del values.
210216 var pm row.PartialIndexUpdateHelper
211- if n := len (u . run .tu .tableDesc ().PartialIndexes ()); n > 0 {
212- err := pm .Init (sourceVals [:n ], sourceVals [n :n * 2 ], u . run .tu .tableDesc ())
217+ if n := len (r .tu .tableDesc ().PartialIndexes ()); n > 0 {
218+ err := pm .Init (sourceVals [:n ], sourceVals [n :n * 2 ], r .tu .tableDesc ())
213219 if err != nil {
214220 return err
215221 }
@@ -221,53 +227,53 @@ func (u *updateNode) processSourceRow(params runParams, sourceVals tree.Datums)
221227 // Order of column values is put partitions, quantized vectors, followed by
222228 // del partitions
223229 var vh row.VectorIndexUpdateHelper
224- if n := len (u . run .tu .tableDesc ().VectorIndexes ()); n > 0 {
225- vh .InitForPut (sourceVals [:n ], sourceVals [n :n * 2 ], u . run .tu .tableDesc ())
226- vh .InitForDel (sourceVals [n * 2 :n * 3 ], u . run .tu .tableDesc ())
230+ if n := len (r .tu .tableDesc ().VectorIndexes ()); n > 0 {
231+ vh .InitForPut (sourceVals [:n ], sourceVals [n :n * 2 ], r .tu .tableDesc ())
232+ vh .InitForDel (sourceVals [n * 2 :n * 3 ], r .tu .tableDesc ())
227233 }
228234
229235 // Error out the update if the enforce_home_region session setting is on and
230236 // the row's locality doesn't match the gateway region.
231- if err := u . run .regionLocalInfo .checkHomeRegion (updateValues ); err != nil {
237+ if err := r .regionLocalInfo .checkHomeRegion (updateValues ); err != nil {
232238 return err
233239 }
234240
235241 // Queue the insert in the KV batch.
236- newValues , err := u . run .tu .rowForUpdate (
237- params .ctx , oldValues , updateValues , pm , vh , false /* mustValidateOldPKValues */ , u . run .traceKV ,
242+ newValues , err := r .tu .rowForUpdate (
243+ params .ctx , oldValues , updateValues , pm , vh , false /* mustValidateOldPKValues */ , r .traceKV ,
238244 )
239245 if err != nil {
240246 return err
241247 }
242248
243249 // If result rows need to be accumulated, do it.
244- if u . run .tu .rows != nil {
250+ if r .tu .rows != nil {
245251 // The new values can include all columns, so the values may contain
246252 // additional columns for every newly added column not yet visible. We do
247253 // not want them to be available for RETURNING.
248254 //
249255 // MakeUpdater guarantees that the first columns of the new values
250256 // are those specified u.columns.
251257 largestRetIdx := - 1
252- for i := range u . run .rowIdxToRetIdx {
253- retIdx := u . run .rowIdxToRetIdx [i ]
258+ for i := range r .rowIdxToRetIdx {
259+ retIdx := r .rowIdxToRetIdx [i ]
254260 if retIdx >= 0 {
255261 if retIdx >= largestRetIdx {
256262 largestRetIdx = retIdx
257263 }
258- u . run .resultRowBuffer [retIdx ] = newValues [i ]
264+ r .resultRowBuffer [retIdx ] = newValues [i ]
259265 }
260266 }
261267
262268 // At this point we've extracted all the RETURNING values that are part
263269 // of the target table. We must now extract the columns in the RETURNING
264270 // clause that refer to other tables (from the FROM clause of the update).
265- for i := 0 ; i < u . run .numPassthrough ; i ++ {
271+ for i := 0 ; i < r .numPassthrough ; i ++ {
266272 largestRetIdx ++
267- u . run .resultRowBuffer [largestRetIdx ] = passthroughValues [i ]
273+ r .resultRowBuffer [largestRetIdx ] = passthroughValues [i ]
268274 }
269275
270- if _ , err := u . run . tu .rows .AddRow (params .ctx , u . run .resultRowBuffer ); err != nil {
276+ if _ , err := r . tu .rows .AddRow (params .ctx , r .resultRowBuffer ); err != nil {
271277 return err
272278 }
273279 }
0 commit comments