pingcap / pingcap/tidb

Logical bug: value changed after adding DISTINCT [v5.3.4, v6.4.0]

Open
#40,013 0 comments 1 reaction 0 assignees View on GitHub
affects-5.3 affects-5.4 affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.6 affects-7.0 affects-7.1 affects-7.5 affects-8.1 affects-8.5 may-affects-4.0 may-affects-5.0 may-affects-5.1 may-affects-5.2 severity/major sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

### 1. Minimal reproduce step (Required)

```sql
drop table if exists t;
CREATE TABLE t (c1 FLOAT UNSIGNED);
INSERT INTO t VALUES (47),(28.1237);

SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql1
SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT DISTINCT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql2
```

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

In theory, the result of sql2(DISTINCT) ⊆ the result of sql1

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

However, the value `NULL` changed to ` 2011-12-11` after adding DISTINCT, seems like a logical bug:

```sql
mysql> select version();
+--------------------+
| version() |
+--------------------+
| 5.7.25-TiDB-v6.4.0 |
+--------------------+
1 row in set (0.01 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql1
+------+
| f1 |
+------+
| NULL |
| NULL |
| NULL |
| NULL |
+------+
4 rows in set, 11 warnings (0.00 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT DISTINCT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql2
+------------+
| f1 |
+------------+
| 2011-12-11 |
| NULL |
+------------+
2 rows in set, 5 warnings (0.00 sec)
```

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

```sql
Release Version: v6.4.0
Edition: Community
Git Commit Hash: cf36a9ce2fe1039db3cf3444d51930b887df18a1
Git Branch: heads/refs/tags/v6.4.0
UTC Build Time: 2022-11-13 05:25:30
GoVersion: go1.19.2
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: unistore
```

### 5. Hope these can be helpful for your debugging:

1. The bug cannot be reproduced after removing BIN
2. We look for the first version of the bug in all docker images (with format x.x.x) of https://hub.docker.com/r/pingcap/tidb/tags
We found that the bug first occurred in pingcap/tidb:v5.3.4, it cannot be reproduced in pingcap/tidb:v5.3.3:

```sql
mysql> select version();
+--------------------+
| version() |
+--------------------+
| 5.7.25-TiDB-v5.3.4 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql1
+------------+
| f1 |
+------------+
| NULL |
| NULL |
| NULL |
| NULL |
+------------+
4 rows in set, 11 warnings (0.00 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT DISTINCT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql2
+------------------------+
| f1 |
+------------------------+
| 0x323031312D31322D3131 |
| NULL |
+------------------------+
2 rows in set, 5 warnings (0.00 sec)

mysql> select version();
+--------------------+
| version() |
+--------------------+
| 5.7.25-TiDB-v5.3.3 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql1
+------------+
| f1 |
+------------+
| NULL |
| 2011-12-11 |
| NULL |
| 2011-12-11 |
+------------+
4 rows in set, 6 warnings (0.00 sec)

mysql> SELECT (DATE_ADD(f2, INTERVAL 1 YEAR_MONTH)) AS f1 FROM (SELECT DISTINCT 1 FROM t) AS t1 JOIN (SELECT (DATE_ADD(BIN(c1), INTERVAL 1 YEAR)) AS f2 FROM t) AS t2; -- sql2
+------------+
| f1 |
+------------+
| 2011-12-11 |
| NULL |
+------------+
2 rows in set, 4 warnings (0.00 sec)
```

Contributor guide

Open the contributing guide

Research direction

Start by running the supplied SQL reproducer against the affected and unaffected TiDB versions, focusing on the interaction between DISTINCT, BIN, and DATE_ADD. Trace the query execution and type or value handling involved, then add a regression test showing that adding DISTINCT does not change the corresponding results.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.