Repository: pingcap/tidb (Go)

Issue:
Insert into virtual column get error in sql_mode ""
## Description
## Bug Report

Please answer these questions before submitting your issue. Thanks!

1. What did you do?
If possible, provide a recipe for reproducing the error.
```
create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1))));
set sql_mode="";
INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
```

2. What did you expect to see?
```
mysql> INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
Query OK, 1 row affected, 1 warning (0.02 sec)
```


3. What did you see instead?
```
mysql> INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
ERROR 1690 (22003): DECIMAL value is out of range in '(2, 1)'
```


4. What version of TiDB are you using (`tidb-server -V` or run `select tidb_version();` on TiDB)?
master

## SIG slack channel

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

## Score

300

## Mentor

- @lzmhhh123



Discussion:
The bug here is that TiDB is not respecting the value of the sql mode STRICT_TRANS_TABLES, and behaves as if it is always on.  Confirming I can reproduce this against master:

```sql
drop table if exists t1;
create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1))));
set sql_mode="";
INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));

..

mysql> INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
ERROR 1690 (22003): DECIMAL value is out of range in '(2, 1)'
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v4.0.0-beta.2-771-gca41972fb
Edition: Community
Git Commit Hash: ca41972fbac068c8a5de107d9075f09ac68842ac
Git Branch: master
UTC Build Time: 2020-07-14 02:41:21
GoVersion: go1.13
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set (0.00 sec)

```
## Please edit this comment or add a new 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
-->



Patch:
diff --git a/executor/insert_common.go b/executor/insert_common.go
index d32498c5648d3..88c31937cf8d3 100644
--- a/executor/insert_common.go
+++ b/executor/insert_common.go
@@ -596,7 +596,7 @@ func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue
 	for i, gCol := range gCols {
 		colIdx := gCol.ColumnInfo.Offset
 		val, err := e.GenExprs[i].Eval(chunk.MutRowFromDatums(row).ToRow())
-		if e.handleErr(gCol, &val, 0, err) != nil {
+		if e.ctx.GetSessionVars().StmtCtx.HandleTruncate(err) != nil {
 			return nil, err
 		}
 		row[colIdx], err = table.CastValue(e.ctx, val, gCol.ToInfo(), false, false)


Test patch:
diff --git a/executor/write_test.go b/executor/write_test.go
index 520d08df4542a..699e56ac46a58 100644
--- a/executor/write_test.go
+++ b/executor/write_test.go
@@ -1076,6 +1076,14 @@ func (s *testSuite2) TestGeneratedColumnForInsert(c *C) {
 	tk.MustExec(`insert into t (k) values (1), (2)`)
 	tk.MustExec(`replace into t (k) values (1), (2)`)
 	tk.MustQuery(`select * from t`).Check(testkit.Rows("2 3 1", "3 4 2"))
+
+	// For issue 14340
+	tk.MustExec(`drop table if exists t1`)
+	tk.MustExec(`create table t1(f1 json, f2 real as (cast(f1 as decimal(2,1))))`)
+	tk.MustGetErrMsg(`INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON))`, "[types:1690]DECIMAL value is out of range in '(2, 1)'")
+	tk.MustExec(`set @@sql_mode = ''`)
+	tk.MustExec(`INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON))`)
+	tk.MustQuery(`select * from t1`).Check(testkit.Rows("1000 9.9"))
 }
 
 func (s *testSuite4) TestPartitionedTableReplace(c *C) {