Skip to content

Commit

Permalink
This is an automated cherry-pick of #51862
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
wjhuang2016 authored and ti-chi-bot committed Sep 14, 2024
1 parent af6aa91 commit 1236873
Show file tree
Hide file tree
Showing 2 changed files with 1,181 additions and 0 deletions.
60 changes: 60 additions & 0 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,28 @@ func (e *InsertValues) collectRuntimeStatsEnabled() bool {
return false
}

func (e *InsertValues) handleDuplicateKey(ctx context.Context, txn kv.Transaction, uk *keyValueWithDupInfo, replace bool, r toBeCheckedRow) (bool, error) {
if !replace {
e.Ctx().GetSessionVars().StmtCtx.AppendWarning(uk.dupErr)
if txnCtx := e.Ctx().GetSessionVars().TxnCtx; txnCtx.IsPessimistic && e.Ctx().GetSessionVars().LockUnchangedKeys {
txnCtx.AddUnchangedKeyForLock(uk.newKey)
}
return true, nil
}
_, handle, err := tables.FetchDuplicatedHandle(ctx, uk.newKey, true, txn, e.Table.Meta().ID, uk.commonHandle)
if err != nil {
return false, err
}
if handle == nil {
return false, nil
}
_, err = e.removeRow(ctx, txn, handle, r, true)
if err != nil {
return false, err
}
return false, nil
}

// batchCheckAndInsert checks rows with duplicate errors.
// All duplicate rows will be ignored and appended as duplicate warnings.
func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.Datum,
Expand Down Expand Up @@ -1177,6 +1199,7 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
return err
}
}
<<<<<<< HEAD:executor/insert_common.go
skip := false
for _, uk := range r.uniqueKeys {
_, err := txn.Get(ctx, uk.newKey)
Expand Down Expand Up @@ -1211,8 +1234,45 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
break
}
} else if !kv.IsErrNotFound(err) {
=======

rowInserted := false
for _, uk := range r.uniqueKeys {
_, err := txn.Get(ctx, uk.newKey)
if err != nil && !kv.IsErrNotFound(err) {
>>>>>>> fb64325ee52 (executor: handle the corner case that temp index is not exist but the normal index is exist (#51862)):pkg/executor/insert_common.go
return err
}
if err == nil {
rowInserted, err = e.handleDuplicateKey(ctx, txn, uk, replace, r)
if err != nil {
return err
}
if rowInserted {
break
}
continue
}
if tablecodec.IsTempIndexKey(uk.newKey) {
tablecodec.TempIndexKey2IndexKey(uk.newKey)
_, err = txn.Get(ctx, uk.newKey)
if err != nil && !kv.IsErrNotFound(err) {
return err
}
if err == nil {
rowInserted, err = e.handleDuplicateKey(ctx, txn, uk, replace, r)
if err != nil {
return err
}
if rowInserted {
break
}
}
}
}

if rowInserted {
continue
}

// If row was checked with no duplicate keys,
Expand Down
Loading

0 comments on commit 1236873

Please sign in to comment.