Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix delete error when convert string to float or int #10861

Merged
merged 5 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,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(tt.data1, tt.data2)
c.Assert(err1, IsNil)
c.Assert(reachLimit, IsFalse)
Expand Down
4 changes: 4 additions & 0 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,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 == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used in StrToUint, maybe not only delete statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition statementContext.InDeleteStmt == true will keep it only work in delete statement.

And in delete statement, empty string can always convert to 0 without error and warning.(according to behaviour of mysql 5.x and 8.x ).

But in other statement like select, tidb is consistent with mysql(all with warning), so I restrict it in delete statement context.

return "0", nil
}

var (
sawDot bool
sawDigit bool
Expand Down
23 changes: 23 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down