From 6566ba404fcc9e3c50d623f3825b09fd17d546e7 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Sun, 4 Oct 2020 14:30:31 +0800 Subject: [PATCH] types: fix FLOAT data overflow check (#20067) (#20158) (#20251) --- executor/insert_test.go | 11 +++++++++++ types/datum.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/executor/insert_test.go b/executor/insert_test.go index 27b04d032c969..b209330dabb67 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -792,6 +792,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), diff --git a/types/datum.go b/types/datum.go index f36ffe39e7142..341f20eaeef0f 100644 --- a/types/datum.go +++ b/types/datum.go @@ -839,7 +839,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