Repository: pingcap/tidb (Go)

Issue:
bug of replace into table with generated column 
## Bug Report

Please answer these questions before submitting your issue. Thanks!

```sql
mysql>create table t1 (a int, b int as (a + 1) virtual not null, unique index idx(b));
Query OK, 0 rows affected
Time: 0.065s
mysql>REPLACE INTO `generate_column`.`t1` (`a`) VALUES (2);
Query OK, 1 row affected
Time: 0.011s
mysql>REPLACE INTO `generate_column`.`t1` (`a`) VALUES (2);
(1105, u'Miss column')  -- should execute successful
```

<!-- a step by step guide for reproducing the bug. -->

### 2. What did you expect to see? (Required)

### 3. Affected version (Required)

mysql>select tidb_version();
+-------------------------------------------------------------------+
| tidb_version()                                                    |
+-------------------------------------------------------------------+
| Release Version: v4.0.0-beta.2-468-gbcbc25db6                     |
| Edition: Community                                                |
| Git Commit Hash: bcbc25db68a4dd719f02ffdf75e74e59b05a231d         |
| Git Branch: HEAD                                                  |
| UTC Build Time: 2020-05-18 08:58:16                               |
| GoVersion: go1.13                                                 |
| Race Enabled: false                                               |
| TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306 |
| Check Table Before Drop: false                                    |
+-------------------------------------------------------------------+
1 row in set

### 4. Root Cause Analysis

<!-- should be filled by the investigator before it's closed -->



Discussion:

Patch:
diff --git a/table/tables/tables.go b/table/tables/tables.go
index 6919cc9e671a7..734db261e6e36 100644
--- a/table/tables/tables.go
+++ b/table/tables/tables.go
@@ -791,6 +791,9 @@ func DecodeRawRowData(ctx sessionctx.Context, meta *model.TableInfo, h kv.Handle
 			v[i] = ri
 			continue
 		}
+		if col.IsGenerated() && !col.GeneratedStored {
+			continue
+		}
 		v[i], err = GetColDefaultValue(ctx, col, defaultVals)
 		if err != nil {
 			return nil, rowMap, err


Test patch:
diff --git a/executor/executor_test.go b/executor/executor_test.go
index 9abc81ec2f612..cc561e90a1ab2 100644
--- a/executor/executor_test.go
+++ b/executor/executor_test.go
@@ -5823,3 +5823,16 @@ func (s *testSuite1) TestUpdateGivenPartitionSet(c *C) {
 	tk.MustExec("update t2 partition(p0) set a = 3 where a = 2")
 	tk.MustExec("update t2 partition(p0, p3) set a = 33 where a = 1")
 }
+
+// For issue 17256
+func (s *testSuite) TestGenerateColumnReplace(c *C) {
+	tk := testkit.NewTestKit(c, s.store)
+	tk.MustExec("use test;")
+	tk.MustExec("drop table if exists t1")
+	tk.MustExec("create table t1 (a int, b int as (a + 1) virtual not null, unique index idx(b));")
+	tk.MustExec("REPLACE INTO `t1` (`a`) VALUES (2);")
+	tk.MustExec("REPLACE INTO `t1` (`a`) VALUES (2);")
+	tk.MustQuery("select * from t1").Check(testkit.Rows("2 3"))
+	tk.MustExec("insert into `t1` (`a`) VALUES (2) on duplicate key update a = 3;")
+	tk.MustQuery("select * from t1").Check(testkit.Rows("3 4"))
+}