Skip to content

Commit

Permalink
types: fix FLOAT data overflow check (#20067) (#20158)
Browse files Browse the repository at this point in the history
  • Loading branch information
newcworld authored Sep 27, 2020
1 parent 0017a17 commit fd0a18a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,17 @@ func (s *testSuite3) TestDMLCast(c *C) {
tk.MustQuery(`select * from t`).Check(testkit.Rows())
}

func (s *testSuite3) TestInsertFloatOverflow(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t;`)
tk.MustExec("create table t(col1 FLOAT, col2 FLOAT(10,2), col3 DOUBLE, col4 DOUBLE(10,2), col5 DECIMAL, col6 DECIMAL(10,2));")
_, err := tk.Exec("insert into t values (-3.402823466E+68, -34028234.6611, -1.7976931348623157E+308, -17976921.34, -9999999999, -99999999.99);")
c.Assert(err.Error(), Equals, "[types:1264]Out of range value for column 'col1' at row 1")
_, err = tk.Exec("insert into t values (-34028234.6611, -3.402823466E+68, -1.7976931348623157E+308, -17976921.34, -9999999999, -99999999.99);")
c.Assert(err.Error(), Equals, "[types:1264]Out of range value for column 'col2' at row 1")
}

// There is a potential issue in MySQL: when the value of auto_increment_offset is greater
// than that of auto_increment_increment, the value of auto_increment_offset is ignored
// (https://dev.mysql.com/doc/refman/8.0/en/replication-options-master.html#sysvar_auto_increment_increment),
Expand Down
2 changes: 1 addition & 1 deletion types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ func ProduceFloatWithSpecifiedTp(f float64, target *FieldType, sc *stmtctx.State
return f, errors.Trace(err)
}
}
if mysql.HasUnsignedFlag(target.Flag) && f < 0 {
if (mysql.HasUnsignedFlag(target.Flag) && f < 0) || (target.Tp == mysql.TypeFloat && (f > math.MaxFloat32 || f < -math.MaxFloat32)) {
return 0, overflow(f, target.Tp)
}
return f, nil
Expand Down

0 comments on commit fd0a18a

Please sign in to comment.