Repository: pingcap/tidb (Go)

Issue:
`explain analyze` causes wrong result when txn is too large
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

Run TiDB and load data with sysbench.

```sh
sysbench --config-file=config oltp_point_select --tables=1 --table-size=500000 prepare
```

Create table with same schema and load data by `explain analyze insert select`

```sql
create table sbtest2(id int(11) NOT NULL AUTO_INCREMENT, `k` int(11) NOT NULL DEFAULT '0',`c` char(120) NOT NULL DEFAULT '',`pad` char(60) NOT NULL DEFAULT '',PRIMARY KEY (`id`),KEY `k_1` (`k`));
explain analyze insert into sbtest2 select * from sbtest1;
```

P.S. if we just run `insert select` without `explain analyze`, it'll return txn-too-large error.

```sql
MySQL [test]> insert into sbtest2 select * from sbtest1;
ERROR 8004 (HY000): Transaction is too large, size: 104857766
```

### 2. What did you expect to see? (Required)

If the statement execution succeeds, the data in `sbtest2` should be the same as `sbtest1`.

```sql
MySQL [test]> select count(1) from sbtest2;
+----------+
| count(1) |
+----------+
|   500000 |
+----------+
1 row in set (2.857 sec)

MySQL [test]> select count(1) from sbtest1;
+----------+
| count(1) |
+----------+
|   500000 |
+----------+
1 row in set (0.242 sec)
```

### 3. What did you see instead (Required)

The data in `sbtest2` is truncated.

```sql
MySQL [test]> select count(1) from sbtest2;
+----------+
| count(1) |
+----------+
|   412825 |
+----------+
1 row in set (2.857 sec)

MySQL [test]> select count(1) from sbtest1;
+----------+
| count(1) |
+----------+
|   500000 |
+----------+
1 row in set (0.242 sec)
```

The transaction details in TiDB dashboard are missing.

![image](https://user-images.githubusercontent.com/9587680/171365718-bb85cfb3-e6e7-4e26-a062-3ebc5ec244a3.png)

### 4. What is your TiDB version? (Required)

```
Release Version: v6.2.0-alpha
Edition: Community
Git Commit Hash: e2fd394fbfaf1ace2bb129c0e7863bd20f087152
Git Branch: heads/refs/tags/v6.2.0-alpha
UTC Build Time: 2022-05-31 14:58:00
GoVersion: go1.18.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
Store: tikv
```




Discussion:
A minimal reproducible case:
```sql
/* t */ drop table if exists t;
/* t */ create table t (a int primary key);
/* t */ insert into t values (2);
/* t */ set @@tidb_constraint_check_in_place=1;
/* t */ explain analyze insert into t values (1), (2), (3); -- ok
/* t */ select * from t; -- [1, 2]
```



Patch:
diff --git a/executor/adapter.go b/executor/adapter.go
index b1a759cc1be27..1ac4ecef0194d 100644
--- a/executor/adapter.go
+++ b/executor/adapter.go
@@ -469,7 +469,7 @@ func (a *ExecStmt) Exec(ctx context.Context) (_ sqlexec.RecordSet, err error) {
 		return a.handlePessimisticSelectForUpdate(ctx, e)
 	}
 
-	if handled, result, err := a.handleNoDelay(ctx, e, isPessimistic); handled {
+	if handled, result, err := a.handleNoDelay(ctx, e, isPessimistic); handled || err != nil {
 		return result, err
 	}
 


Test patch:
diff --git a/executor/explain_test.go b/executor/explain_test.go
index af3439b048b81..8ecbd3edd019b 100644
--- a/executor/explain_test.go
+++ b/executor/explain_test.go
@@ -495,3 +495,16 @@ func TestIssue35911(t *testing.T) {
 	// To be consistent with other operators, we should not aggregate the concurrency in the runtime stats.
 	require.EqualValues(t, 5, concurrency)
 }
+
+func TestIssue35105(t *testing.T) {
+	store, clean := testkit.CreateMockStore(t)
+	defer clean()
+	tk := testkit.NewTestKit(t, store)
+	tk.MustExec("use test")
+	tk.MustExec("drop table if exists t")
+	tk.MustExec("create table t (a int primary key)")
+	tk.MustExec("insert into t values (2)")
+	tk.MustExec("set @@tidb_constraint_check_in_place=1")
+	require.Error(t, tk.ExecToErr("explain analyze insert into t values (1), (2), (3)"))
+	tk.MustQuery("select * from t").Check(testkit.Rows("2"))
+}