Skip to content

Commit c535e80

Browse files
crazycs520hawkingrei
authored andcommitted
executor: fix issue that update ignore stmt return error when meet incorrect timestamp value error (pingcap#54878)
close pingcap#50308
1 parent 71dbe95 commit c535e80

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pkg/executor/test/executor/executor_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -2986,3 +2986,19 @@ func TestIssue48756(t *testing.T) {
29862986
"Warning 1105 ",
29872987
))
29882988
}
2989+
2990+
func TestIssue50308(t *testing.T) {
2991+
store := testkit.CreateMockStore(t)
2992+
tk := testkit.NewTestKit(t, store)
2993+
tk.MustExec("use test")
2994+
tk.MustExec("create table t(a timestamp);")
2995+
tk.MustExec("insert ignore into t values(cast('2099-01-01' as date));")
2996+
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning 1292 Incorrect timestamp value: '2099-01-01' for column 'a' at row 1"))
2997+
tk.MustQuery("select * from t;").Check(testkit.Rows("0000-00-00 00:00:00"))
2998+
tk.MustExec("delete from t")
2999+
tk.MustExec("insert into t values('2000-01-01');")
3000+
tk.MustGetErrMsg("update t set a=cast('2099-01-01' as date)", "[types:1292]Incorrect timestamp value: '2099-01-01'")
3001+
tk.MustExec("update ignore t set a=cast('2099-01-01' as date);")
3002+
tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning 1292 Incorrect timestamp value: '2099-01-01'"))
3003+
tk.MustQuery("select * from t;").Check(testkit.Rows("0000-00-00 00:00:00"))
3004+
}

pkg/executor/update.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (e *UpdateExec) updateRows(ctx context.Context) (int, error) {
339339
return totalNumRows, nil
340340
}
341341

342-
func (*UpdateExec) handleErr(colName model.CIStr, rowIdx int, err error) error {
342+
func (e *UpdateExec) handleErr(colName model.CIStr, col *table.Column, rowIdx int, err error) error {
343343
if err == nil {
344344
return nil
345345
}
@@ -351,7 +351,10 @@ func (*UpdateExec) handleErr(colName model.CIStr, rowIdx int, err error) error {
351351
if types.ErrOverflow.Equal(err) {
352352
return types.ErrWarnDataOutOfRange.GenWithStackByArgs(colName.O, rowIdx+1)
353353
}
354-
354+
if types.ErrTruncatedWrongVal.Equal(err) && col != nil && col.ColumnInfo != nil && col.ColumnInfo.GetType() == mysql.TypeTimestamp {
355+
ec := e.Ctx().GetSessionVars().StmtCtx.ErrCtx()
356+
return errors.AddStack(ec.HandleErrorWithAlias(kv.ErrKeyExists, err, err))
357+
}
355358
return err
356359
}
357360

@@ -364,15 +367,15 @@ func (e *UpdateExec) fastComposeNewRow(rowIdx int, oldRow []types.Datum, cols []
364367
}
365368
con := assign.Expr.(*expression.Constant)
366369
val, err := con.Eval(e.Ctx().GetExprCtx().GetEvalCtx(), emptyRow)
367-
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
370+
if err = e.handleErr(assign.ColName, cols[assign.Col.Index], rowIdx, err); err != nil {
368371
return nil, err
369372
}
370373

371374
// info of `_tidb_rowid` column is nil.
372375
// No need to cast `_tidb_rowid` column value.
373376
if cols[assign.Col.Index] != nil {
374377
val, err = table.CastValue(e.Ctx(), val, cols[assign.Col.Index].ColumnInfo, false, false)
375-
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
378+
if err = e.handleErr(assign.ColName, cols[assign.Col.Index], rowIdx, err); err != nil {
376379
return nil, err
377380
}
378381
}
@@ -399,7 +402,7 @@ func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum, cols []*tab
399402
// No need to cast `_tidb_rowid` column value.
400403
if cols[assign.Col.Index] != nil {
401404
val, err = table.CastValue(e.Ctx(), val, cols[assign.Col.Index].ColumnInfo, false, false)
402-
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
405+
if err = e.handleErr(assign.ColName, cols[assign.Col.Index], rowIdx, err); err != nil {
403406
return nil, err
404407
}
405408
}
@@ -420,15 +423,15 @@ func (e *UpdateExec) composeGeneratedColumns(rowIdx int, newRowData []types.Datu
420423
continue
421424
}
422425
val, err := assign.Expr.Eval(e.Ctx().GetExprCtx().GetEvalCtx(), e.evalBuffer.ToRow())
423-
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
426+
if err = e.handleErr(assign.ColName, cols[assign.Col.Index], rowIdx, err); err != nil {
424427
return nil, err
425428
}
426429

427430
// info of `_tidb_rowid` column is nil.
428431
// No need to cast `_tidb_rowid` column value.
429432
if cols[assign.Col.Index] != nil {
430433
val, err = table.CastValue(e.Ctx(), val, cols[assign.Col.Index].ColumnInfo, false, false)
431-
if err = e.handleErr(assign.ColName, rowIdx, err); err != nil {
434+
if err = e.handleErr(assign.ColName, cols[assign.Col.Index], rowIdx, err); err != nil {
432435
return nil, err
433436
}
434437
}

0 commit comments

Comments
 (0)