-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathupdate.go
304 lines (272 loc) · 8.72 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"context"
"fmt"
"github.com/pingcap/errors"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
// UpdateExec represents a new update executor.
type UpdateExec struct {
baseExecutor
OrderedList []*expression.Assignment
// updatedRowKeys is a map for unique (Table, handle) pair.
// The value is true if the row is changed, or false otherwise
updatedRowKeys map[int64]map[int64]bool
tblID2table map[int64]table.Table
rows [][]types.Datum // The rows fetched from TableExec.
newRowsData [][]types.Datum // The new values to be set.
fetched bool
cursor int
matched uint64 // a counter of matched rows during update
// tblColPosInfos stores relationship between column ordinal to its table handle.
// the columns ordinals is present in ordinal range format, @see plannercore.TblColPosInfos
tblColPosInfos plannercore.TblColPosInfoSlice
evalBuffer chunk.MutRow
allAssignmentsAreConstant bool
}
func (e *UpdateExec) exec(ctx context.Context, schema *expression.Schema) ([]types.Datum, error) {
assignFlag, err := e.getUpdateColumns(e.ctx, schema.Len())
if err != nil {
return nil, err
}
if e.cursor >= len(e.rows) {
return nil, nil
}
if e.updatedRowKeys == nil {
e.updatedRowKeys = make(map[int64]map[int64]bool)
}
row := e.rows[e.cursor]
newData := e.newRowsData[e.cursor]
for _, content := range e.tblColPosInfos {
tbl := e.tblID2table[content.TblID]
if e.updatedRowKeys[content.TblID] == nil {
e.updatedRowKeys[content.TblID] = make(map[int64]bool)
}
handleDatum := row[content.HandleOrdinal]
if e.canNotUpdate(handleDatum) {
continue
}
handle := row[content.HandleOrdinal].GetInt64()
oldData := row[content.Start:content.End]
newTableData := newData[content.Start:content.End]
updatable := false
flags := assignFlag[content.Start:content.End]
for _, flag := range flags {
if flag {
updatable = true
break
}
}
if !updatable {
// If there's nothing to update, we can just skip current row
continue
}
changed, ok := e.updatedRowKeys[content.TblID][handle]
if !ok {
// Row is matched for the first time, increment `matched` counter
e.matched++
}
if changed {
// Each matched row is updated once, even if it matches the conditions multiple times.
continue
}
// Update row
changed, _, _, err1 := updateRecord(ctx, e.ctx, handle, oldData, newTableData, flags, tbl, false)
if err1 == nil {
e.updatedRowKeys[content.TblID][handle] = changed
continue
}
sc := e.ctx.GetSessionVars().StmtCtx
if kv.ErrKeyExists.Equal(err1) && sc.DupKeyAsWarning {
sc.AppendWarning(err1)
continue
}
return nil, err1
}
e.cursor++
return []types.Datum{}, nil
}
// canNotUpdate checks the handle of a record to decide whether that record
// can not be updated. The handle is NULL only when it is the inner side of an
// outer join: the outer row can not match any inner rows, and in this scenario
// the inner handle field is filled with a NULL value.
//
// This fixes: https://github.com/pingcap/tidb/issues/7176.
func (e *UpdateExec) canNotUpdate(handle types.Datum) bool {
return handle.IsNull()
}
// Next implements the Executor Next interface.
func (e *UpdateExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.Reset()
if !e.fetched {
err := e.fetchChunkRows(ctx)
if err != nil {
return err
}
e.fetched = true
e.ctx.GetSessionVars().StmtCtx.AddRecordRows(uint64(len(e.rows)))
for {
row, err := e.exec(ctx, e.children[0].Schema())
if err != nil {
return err
}
// once "row == nil" there is no more data waiting to be updated,
// the execution of UpdateExec is finished.
if row == nil {
break
}
}
}
return nil
}
func (e *UpdateExec) fetchChunkRows(ctx context.Context) error {
fields := retTypes(e.children[0])
colsInfo := make([]*table.Column, len(fields))
for _, content := range e.tblColPosInfos {
tbl := e.tblID2table[content.TblID]
for i, c := range tbl.WritableCols() {
colsInfo[content.Start+i] = c
}
}
globalRowIdx := 0
chk := newFirstChunk(e.children[0])
if !e.allAssignmentsAreConstant {
e.evalBuffer = chunk.MutRowFromTypes(fields)
}
composeFunc := e.fastComposeNewRow
if !e.allAssignmentsAreConstant {
composeFunc = e.composeNewRow
}
for {
err := Next(ctx, e.children[0], chk)
if err != nil {
return err
}
if chk.NumRows() == 0 {
break
}
for rowIdx := 0; rowIdx < chk.NumRows(); rowIdx++ {
chunkRow := chk.GetRow(rowIdx)
datumRow := chunkRow.GetDatumRow(fields)
newRow, err1 := composeFunc(globalRowIdx, datumRow, colsInfo)
if err1 != nil {
return err1
}
e.rows = append(e.rows, datumRow)
e.newRowsData = append(e.newRowsData, newRow)
globalRowIdx++
}
chk = chunk.Renew(chk, e.maxChunkSize)
}
return nil
}
func (e *UpdateExec) handleErr(colName model.CIStr, rowIdx int, err error) error {
if err == nil {
return nil
}
if types.ErrDataTooLong.Equal(err) {
return resetErrDataTooLong(colName.O, rowIdx+1, err)
}
if types.ErrOverflow.Equal(err) {
return types.ErrWarnDataOutOfRange.GenWithStackByArgs(colName.O, rowIdx+1)
}
return err
}
func (e *UpdateExec) fastComposeNewRow(rowIdx int, oldRow []types.Datum, cols []*table.Column) ([]types.Datum, error) {
newRowData := types.CloneRow(oldRow)
for _, assign := range e.OrderedList {
handleIdx, handleFound := e.tblColPosInfos.FindHandle(assign.Col.Index)
if handleFound && e.canNotUpdate(oldRow[handleIdx]) {
continue
}
con := assign.Expr.(*expression.Constant)
val, err := con.Eval(emptyRow)
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
return nil, err
}
// info of `_tidb_rowid` column is nil.
// No need to cast `_tidb_rowid` column value.
if cols[assign.Col.Index] != nil {
val, err = table.CastValue(e.ctx, val, cols[assign.Col.Index].ColumnInfo)
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
return nil, err
}
}
newRowData[assign.Col.Index] = *val.Copy()
}
return newRowData, nil
}
func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum, cols []*table.Column) ([]types.Datum, error) {
newRowData := types.CloneRow(oldRow)
e.evalBuffer.SetDatums(newRowData...)
for _, assign := range e.OrderedList {
handleIdx, handleFound := e.tblColPosInfos.FindHandle(assign.Col.Index)
if handleFound && e.canNotUpdate(oldRow[handleIdx]) {
continue
}
val, err := assign.Expr.Eval(e.evalBuffer.ToRow())
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
return nil, err
}
// info of `_tidb_rowid` column is nil.
// No need to cast `_tidb_rowid` column value.
if cols[assign.Col.Index] != nil {
val, err = table.CastValue(e.ctx, val, cols[assign.Col.Index].ColumnInfo)
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
return nil, err
}
}
newRowData[assign.Col.Index] = *val.Copy()
e.evalBuffer.SetDatum(assign.Col.Index, val)
}
return newRowData, nil
}
// Close implements the Executor Close interface.
func (e *UpdateExec) Close() error {
e.setMessage()
return e.children[0].Close()
}
// Open implements the Executor Open interface.
func (e *UpdateExec) Open(ctx context.Context) error {
return e.children[0].Open(ctx)
}
func (e *UpdateExec) getUpdateColumns(ctx sessionctx.Context, schemaLen int) ([]bool, error) {
assignFlag := make([]bool, schemaLen)
for _, v := range e.OrderedList {
if !ctx.GetSessionVars().AllowWriteRowID && v.Col.ID == model.ExtraHandleID {
return nil, errors.Errorf("insert, update and replace statements for _tidb_rowid are not supported.")
}
idx := v.Col.Index
assignFlag[idx] = true
}
return assignFlag, nil
}
// setMessage sets info message(ERR_UPDATE_INFO) generated by UPDATE statement
func (e *UpdateExec) setMessage() {
stmtCtx := e.ctx.GetSessionVars().StmtCtx
numMatched := e.matched
numChanged := stmtCtx.UpdatedRows()
numWarnings := stmtCtx.WarningCount()
msg := fmt.Sprintf(mysql.MySQLErrName[mysql.ErrUpdateInfo], numMatched, numChanged, numWarnings)
stmtCtx.SetMessage(msg)
}