Repository: pingcap/tidb (Go)

Issue:
FLOAT data type: out of range data should not be inserted
## Description
```
DROP TABLE IF EXISTS t_5_1_1_1_float;
CREATE TABLE t_5_1_1_1_float ( col1 FLOAT, col2 FLOAT(10,2), col3 DOUBLE, col4 DOUBLE(10,2), col5 DECIMAL, col6  DECIMAL(10,2) ) ;
INSERT INTO t_5_1_1_1_float VALUES (-3.402823466E+68, -34028234.6611, -1.7976931348623157E+308, -17976921.34, -9999999999, -99999999.99);
```

TiDB 4.0.5
```
mysql> INSERT INTO t_5_1_1_1_float VALUES (-3.402823466E+68, -34028234.6611, -1.7976931348623157E+308, -17976921.34, -9999999999, -99999999.99);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM t_5_1_1_1_float;
+------+--------------+-------------------------+--------------+-------------+--------------+
| col1 | col2         | col3                    | col4         | col5        | col6         |
+------+--------------+-------------------------+--------------+-------------+--------------+
| -Inf | -34028236.00 | -1.7976931348623157e308 | -17976921.34 | -9999999999 | -99999999.99 |
+------+--------------+-------------------------+--------------+-------------+--------------+
1 row in set (0.00 sec)
```

MySQL 5.7.29
```
mysql> INSERT INTO t_5_1_1_1_float VALUES (-3.402823466E+68, -34028234.6611, -1.7976931348623157E+308, -17976921.34, -9999999999, -99999999.99);
ERROR 1264 (22003): Out of range value for column 'col1' at row 1
```



## SIG slack channel
[#sig-exec](https://slack.tidb.io/invite?team=tidb-community&channel=sig-exec&ref=high-performance)

## Score
- 300

## Mentor
 * @SunRunAway



Discussion:
/pick-up
Pick up success.
Integrity check:
RCA symptom trigger_condition affect_version fix_version fields are empty

Please comment /info to get template
## Please edit this comment to complete the following information

### Not a bug

1. Remove the 'type/bug' label
2. Add notes to indicate why it is not a bug

### Duplicate bug

1. Add the 'type/duplicate' label
2. Add the link to the original bug

### Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: https://github.com/pingcap/tidb/issues/20100

#### 1. Root Cause Analysis (RCA) (optional) 
<!-- Write down the reason why this bug occurs -->

#### 2. Symptom (optional)

<!-- What will the user see when this bug occurs. The error message may be in the terminal, log or monitoring -->

#### 3. All Trigger Conditions (optional)

<!-- All the user scenarios that may trigger this bug -->

#### 4. Workaround (optional)

#### 5. Affected versions

<!--
In the format of [start_version:end_version], multiple version ranges are
accepted. If the bug only affects the unreleased version, please input:
"unreleased". For example:

Notes:
  1. Do not use any white spaces in '[]'.
  2. The range in '[]' is a closed interval
  3. The version format is `v$Major.$Minor.$Patch`, the $Majoy and $Minor
     number in a version range should be the same. [v3.0.1:v3.1.2] is
     invalid because the $Minor number of the version range is different.

Example 1: [v3.0.1:v3.0.5], [v4.0.1:v4.0.5]
Example 2: unreleased
-->

#### 6. Fixed versions

<!--
The first released version that contains this fix in each minor version. If the bug's affected version has been released, the fixed version should be a detailed version number; If the bug doesn't affect any released version, the fixed version can be "master";  

Example 1: v3.0.13, v4.0.5
Example 2: master
-->
**( FixedVersions AffectedVersions )** fields are empty.




Patch:
diff --git a/types/datum.go b/types/datum.go
index 2d45642270f0b..5b4d2187bdd35 100644
--- a/types/datum.go
+++ b/types/datum.go
@@ -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


Test patch:
diff --git a/executor/insert_test.go b/executor/insert_test.go
index c907b5cf9ae6f..cf174760f5dd0 100644
--- a/executor/insert_test.go
+++ b/executor/insert_test.go
@@ -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),