From 9f97c9ac0cac3282e0ad59ba6b88bb1bfb8dd3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=B6=85?= Date: Fri, 20 Oct 2023 12:47:06 +0800 Subject: [PATCH] types: fix update unsigned column with overflow string issue (#47817) close pingcap/tidb#47816 --- pkg/executor/update_test.go | 13 +++++++++++++ pkg/sessionctx/stmtctx/stmtctx.go | 10 ---------- pkg/types/datum.go | 8 +++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/executor/update_test.go b/pkg/executor/update_test.go index 8a9916a81569a..fdbc1590e6933 100644 --- a/pkg/executor/update_test.go +++ b/pkg/executor/update_test.go @@ -520,6 +520,19 @@ func TestIssue23553(t *testing.T) { tk.MustExec(`update tt a inner join (select m0 from tt where status!=1 group by m0 having count(*)>1) b on a.m0=b.m0 set a.status=1`) } +// see issue https://github.com/pingcap/tidb/issues/47816 +func TestUpdateUnsignedWithOverflow(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("create table t1(id int, a int unsigned)") + tk.MustExec("set sql_mode=''") + tk.MustExec("insert into t1 values(1, 10), (2, 20)") + tk.MustExec("update t1 set a='-1' where id=1") + tk.MustExec("update t1 set a='1000000000000000000' where id=2") + tk.MustQuery("select id, a from t1 order by id asc").Check(testkit.Rows("1 0", "2 4294967295")) +} + func TestLockUnchangedUniqueKeys(t *testing.T) { store := testkit.CreateMockStore(t) diff --git a/pkg/sessionctx/stmtctx/stmtctx.go b/pkg/sessionctx/stmtctx/stmtctx.go index 3c97bbdc5baad..0a181e325f8f5 100644 --- a/pkg/sessionctx/stmtctx/stmtctx.go +++ b/pkg/sessionctx/stmtctx/stmtctx.go @@ -1127,16 +1127,6 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails { return details } -// ShouldIgnoreOverflowError indicates whether we should ignore the error when type conversion overflows, -// so we can leave it for further processing like clipping values less than 0 to 0 for unsigned integer types. -func (sc *StatementContext) ShouldIgnoreOverflowError() bool { - // TODO: move this function into `/types` pkg - if (sc.InInsertStmt && sc.TypeFlags().TruncateAsWarning()) || sc.InLoadDataStmt { - return true - } - return false -} - // PushDownFlags converts StatementContext to tipb.SelectRequest.Flags. func (sc *StatementContext) PushDownFlags() uint64 { var flags uint64 diff --git a/pkg/types/datum.go b/pkg/types/datum.go index 868c2f6fd39f3..ccc01697ab6f1 100644 --- a/pkg/types/datum.go +++ b/pkg/types/datum.go @@ -1200,11 +1200,9 @@ func (d *Datum) convertToUint(sc *stmtctx.StatementContext, target *FieldType) ( case KindFloat32, KindFloat64: val, err = ConvertFloatToUint(sc.TypeFlags(), d.GetFloat64(), upperBound, tp) case KindString, KindBytes: - uval, err1 := StrToUint(sc.TypeCtxOrDefault(), d.GetString(), false) - if err1 != nil && ErrOverflow.Equal(err1) && !sc.ShouldIgnoreOverflowError() { - return ret, errors.Trace(err1) - } - val, err = ConvertUintToUint(uval, upperBound, tp) + var err1 error + val, err1 = StrToUint(sc.TypeCtxOrDefault(), d.GetString(), false) + val, err = ConvertUintToUint(val, upperBound, tp) if err == nil { err = err1 }