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 insert error when convert string to float (#13716) #14011

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,24 @@ func (s *testSuite3) TestJiraIssue5366(c *C) {
tk.MustExec(` insert into bug select ifnull(JSON_UNQUOTE(JSON_EXTRACT('[{"amount":2000,"feeAmount":0,"merchantNo":"20190430140319679394","shareBizCode":"20160311162_SECOND"}]', '$[0].merchantNo')),'') merchant_no union SELECT '20180531557' merchant_no;`)
tk.MustQuery(`select * from bug`).Sort().Check(testkit.Rows("20180531557", "20190430140319679394"))
}

func (s *testSuite3) TestDMLCast(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, b double)`)
tk.MustExec(`insert into t values (ifnull('',0)+0, 0)`)
tk.MustExec(`insert into t values (0, ifnull('',0)+0)`)
tk.MustQuery(`select * from t`).Check(testkit.Rows("0 0", "0 0"))
_, err := tk.Exec(`insert into t values ('', 0)`)
c.Assert(err, NotNil)
_, err = tk.Exec(`insert into t values (0, '')`)
c.Assert(err, NotNil)
_, err = tk.Exec(`update t set a = ''`)
c.Assert(err, NotNil)
_, err = tk.Exec(`update t set b = ''`)
c.Assert(err, NotNil)
tk.MustExec("update t set a = ifnull('',0)+0")
tk.MustExec("update t set b = ifnull('',0)+0")
tk.MustExec("delete from t where a = ''")
tk.MustQuery(`select * from t`).Check(testkit.Rows())
}
2 changes: 1 addition & 1 deletion executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ func (s *testSuite) TestUpdate(c *C) {
tk.MustExec("update t set a = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set b = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 <nil>"))
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 0000-00-00 00:00:00"))
tk.MustExec("set @@sql_mode=@orig_sql_mode;")

tk.MustExec("create view v as select * from t")
Expand Down
8 changes: 8 additions & 0 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,10 @@ func (b *builtinCastStringAsIntSig) evalInt(row chunk.Row) (res int64, isNull bo
if len(val) > 1 && val[0] == '-' { // negative number
isNegative = true
}
sctx := b.ctx.GetSessionVars().StmtCtx
if val == "" && (sctx.InInsertStmt || sctx.InUpdateStmt) {
return 0, false, nil
}

var ures uint64
sc := b.ctx.GetSessionVars().StmtCtx
Expand Down Expand Up @@ -1137,6 +1141,10 @@ func (b *builtinCastStringAsRealSig) evalReal(row chunk.Row) (res float64, isNul
if isNull || err != nil {
return res, isNull, err
}
sctx := b.ctx.GetSessionVars().StmtCtx
if val == "" && (sctx.InInsertStmt || sctx.InUpdateStmt) {
return 0, false, nil
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.StrToFloat(sc, val)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,6 @@ func (b *PlanBuilder) buildUpdateLists(ctx context.Context, tableList []*ast.Tab
if err != nil {
return nil, nil, err
}
newExpr = expression.BuildCastFunction(b.ctx, newExpr, col.GetType())
p = np
newList = append(newList, &expression.Assignment{Col: col, Expr: newExpr})
}
Expand Down
2 changes: 1 addition & 1 deletion types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ 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 || sc.InSelectStmt || sc.InUpdateStmt) && s == "" {
if (sc.InDeleteStmt || sc.InSelectStmt) && s == "" {
return "0", nil
}
var (
Expand Down
7 changes: 2 additions & 5 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,17 +469,14 @@ func (s *testTypeConvertSuite) TestStrToNum(c *C) {
func testSelectUpdateDeleteEmptyStringError(c *C) {
testCases := []struct {
inSelect bool
inUpdate bool
inDelete bool
}{
{true, false, false},
{false, true, false},
{false, false, true},
{true, false},
{false, true},
}
sc := new(stmtctx.StatementContext)
for _, tc := range testCases {
sc.InSelectStmt = tc.inSelect
sc.InUpdateStmt = tc.inUpdate
sc.InDeleteStmt = tc.inDelete

str := ""
Expand Down