Skip to content

Commit

Permalink
types: fix update unsigned column with overflow string issue (#47817)
Browse files Browse the repository at this point in the history
close #47816
  • Loading branch information
lcwangchao authored Oct 20, 2023
1 parent 5e3210b commit 9f97c9a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
13 changes: 13 additions & 0 deletions pkg/executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 0 additions & 10 deletions pkg/sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions pkg/types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 9f97c9a

Please sign in to comment.