pingcap / pingcap/tidb

different index order will affect ON DUPLICATE KEY's result when there're conflicts

Open
#34,813 1 comment 2 reactions 0 assignees View on GitHub
severity/minor sig/execution type/bug type/compatibility
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

1. The first kind of table schema:
```sql
use test;
drop table if exists t2;
CREATE TABLE t2 (
c1 char(10) COLLATE utf8mb4_unicode_ci NOT NULL,
c2 enum('alice','bob','charlie','david'),
c3 varchar(10),
UNIQUE KEY idx2 ( c2 , c3 ),
UNIQUE KEY idx1 ( c1)
);
INSERT INTO t2 VALUES ('','charlie',''), ('m','alice','i');
insert into t2 values ('m','charlie','') on duplicate key update c2 = 'charlie';
select * from t2 order by c1, c2, c3;
```
and the final select result is:

```sql
test> select * from t2 order by c1, c2, c3;
+----+---------+----+
| c1 | c2 | c3 |
+----+---------+----+
| | charlie | |
| m | alice | i |
+----+---------+----+
```

2. Use another table schema, I just change the index order definition:

```sql
use test;
drop table if exists t2;
CREATE TABLE t2 (
c1 char(10) COLLATE utf8mb4_unicode_ci NOT NULL,
c2 enum('alice','bob','charlie','david'),
c3 varchar(10),
UNIQUE KEY idx1 ( c1), -- the only different is define idx1 before idx2.
UNIQUE KEY idx2 ( c2 , c3 )
);
INSERT INTO t2 VALUES ('','charlie',''), ('m','alice','i');
insert into t2 values ('m','charlie','') on duplicate key update c2 = 'charlie';
select * from t2 order by c1, c2, c3;
```

and the final select result is:

```sql
test> select * from t2 order by c1, c2, c3;
+----+---------+----+
| c1 | c2 | c3 |
+----+---------+----+
| | charlie | |
| m | charlie | i |
+----+---------+----+
```

As you can see, the final result is different!!!

### 2. What did you expect to see? (Required)

The final select result should be same, the result should be (as same with mysql):

```sql
test> select * from t2 order by c1, c2, c3;
+----+---------+----+
| c1 | c2 | c3 |
+----+---------+----+
| | charlie | |
| m | charlie | i |
+----+---------+----+
```

### 3. What did you see instead (Required)

different index order will affect DML result.

### 4. What is your TiDB version? (Required)

```sql
***************************[ 1. row ]***************************
tidb_version() | Release Version: v6.1.0-alpha-432-gd75b06ec97
Edition: Community
Git Commit Hash: d75b06ec97e54ef9f5a15630e7dfee30d22f047e
Git Branch: master
UTC Build Time: 2022-05-17 07:29:42
GoVersion: go1.18
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.