MCPcopy
hub / github.com/pingcap/tidb / removeRow

Method removeRow

pkg/executor/insert_common.go:1339–1397  ·  view source on GitHub ↗

removeRow removes the duplicate row and cleanup its keys in the key-value map. But if the to-be-removed row equals to the to-be-added row, no remove or add things to do and return (true, nil).

(
	ctx context.Context,
	txn kv.Transaction,
	handle kv.Handle,
	r toBeCheckedRow,
	inReplace bool,
)

Source from the content-addressed store, hash-verified

1337// But if the to-be-removed row equals to the to-be-added row, no remove or add
1338// things to do and return (true, nil).
1339func (e *InsertValues) removeRow(
1340 ctx context.Context,
1341 txn kv.Transaction,
1342 handle kv.Handle,
1343 r toBeCheckedRow,
1344 inReplace bool,
1345) (bool, error) {
1346 newRow := r.row
1347 oldRow, err := getOldRow(ctx, e.Ctx(), txn, r.t, handle, e.GenExprs)
1348 if err != nil {
1349 logutil.BgLogger().Error(
1350 "get old row failed when replace",
1351 zap.String("handle", handle.String()),
1352 zap.String("toBeInsertedRow", types.DatumsToStrNoErr(r.row)),
1353 )
1354 if kv.IsErrNotFound(err) {
1355 err = errors.NotFoundf("can not be duplicated row, due to old row not found. handle %s", handle)
1356 }
1357 return false, err
1358 }
1359
1360 identical, err := e.equalDatumsAsBinary(oldRow, newRow)
1361 if err != nil {
1362 return false, err
1363 }
1364 if identical {
1365 if inReplace {
1366 e.Ctx().GetSessionVars().StmtCtx.AddAffectedRows(1)
1367 }
1368 keySet := lockRowKey
1369 if e.Ctx().GetSessionVars().LockUnchangedKeys {
1370 keySet |= lockUniqueKeys
1371 }
1372 if _, err := addUnchangedKeysForLockByRow(e.Ctx(), r.t, handle, oldRow, keySet); err != nil {
1373 return false, err
1374 }
1375 return true, nil
1376 }
1377
1378 if ph, ok := handle.(kv.PartitionHandle); ok {
1379 err = e.Table.(table.PartitionedTable).GetPartition(ph.PartitionID).RemoveRecord(e.Ctx().GetTableCtx(), txn, ph.Handle, oldRow)
1380 } else {
1381 err = r.t.RemoveRecord(e.Ctx().GetTableCtx(), txn, handle, oldRow)
1382 }
1383 if err != nil {
1384 return false, err
1385 }
1386 err = onRemoveRowForFK(e.Ctx(), oldRow, e.fkChecks, e.fkCascades, e.ignoreErr)
1387 if err != nil {
1388 return false, err
1389 }
1390 if inReplace {
1391 e.Ctx().GetSessionVars().StmtCtx.AddAffectedRows(1)
1392 } else {
1393 e.Ctx().GetSessionVars().StmtCtx.AddDeletedRows(1)
1394 }
1395
1396 return false, nil

Callers 2

handleDuplicateKeyMethod · 0.95
batchCheckAndInsertMethod · 0.95

Calls 15

equalDatumsAsBinaryMethod · 0.95
BgLoggerFunction · 0.92
DatumsToStrNoErrFunction · 0.92
IsErrNotFoundFunction · 0.92
getOldRowFunction · 0.85
onRemoveRowForFKFunction · 0.85
AddAffectedRowsMethod · 0.80
AddDeletedRowsMethod · 0.80
ErrorMethod · 0.65
StringMethod · 0.65
GetSessionVarsMethod · 0.65

Tested by

no test coverage detected