From fcc15b1256e4e33132a80e39b841a6e062c353a0 Mon Sep 17 00:00:00 2001 From: DQYuan <932087612@qq.com> Date: Tue, 16 Jul 2019 19:50:04 +0800 Subject: [PATCH] types: fix delete error when convert string to float or int (#10861) --- executor/executor_test.go | 1 + types/convert.go | 4 ++++ types/convert_test.go | 23 +++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index b68bebd4bfefb..7bd75859cdaf7 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -451,6 +451,7 @@ func checkCases(tests []testCase, ld *executor.LoadDataInfo, ctx.GetSessionVars().StmtCtx.DupKeyAsWarning = true ctx.GetSessionVars().StmtCtx.BadNullAsWarning = true ctx.GetSessionVars().StmtCtx.InLoadDataStmt = true + ctx.GetSessionVars().StmtCtx.InDeleteStmt = false data, reachLimit, err1 := ld.InsertData(context.Background(), tt.data1, tt.data2) c.Assert(err1, IsNil) c.Assert(reachLimit, IsFalse) diff --git a/types/convert.go b/types/convert.go index 58913cb2252eb..7ddfa5e1dd2b1 100644 --- a/types/convert.go +++ b/types/convert.go @@ -581,6 +581,10 @@ func ConvertJSONToDecimal(sc *stmtctx.StatementContext, j json.BinaryJSON) (*MyD // getValidFloatPrefix gets prefix of string which can be successfully parsed as float. func getValidFloatPrefix(sc *stmtctx.StatementContext, s string) (valid string, err error) { + if sc.InDeleteStmt && s == "" { + return "0", nil + } + var ( sawDot bool sawDigit bool diff --git a/types/convert_test.go b/types/convert_test.go index 47f13e99306ad..52e1b58832683 100644 --- a/types/convert_test.go +++ b/types/convert_test.go @@ -461,6 +461,29 @@ func (s *testTypeConvertSuite) TestStrToNum(c *C) { testStrToFloat(c, "1e649", math.MaxFloat64, false, nil) testStrToFloat(c, "-1e649", -math.MaxFloat64, true, ErrTruncatedWrongVal) testStrToFloat(c, "-1e649", -math.MaxFloat64, false, nil) + + // for issue #10806 + testDeleteEmptyStringError(c) +} + +func testDeleteEmptyStringError(c *C) { + sc := new(stmtctx.StatementContext) + sc.InDeleteStmt = true + + str := "" + expect := 0 + + val, err := StrToInt(sc, str) + c.Assert(err, IsNil) + c.Assert(val, Equals, int64(expect)) + + val1, err := StrToUint(sc, str) + c.Assert(err, IsNil) + c.Assert(val1, Equals, uint64(expect)) + + val2, err := StrToFloat(sc, str) + c.Assert(err, IsNil) + c.Assert(val2, Equals, float64(expect)) } func (s *testTypeConvertSuite) TestFieldTypeToStr(c *C) {