Logical bug: value changed after adding DISTINCT [v6.2.0, v6.4.0]
- 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 int);
INSERT INTO t VALUES (1);
SELECT 1%`f1` FROM (SELECT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
SELECT 1%`f1` FROM (SELECT DISTINCT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
```
### 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 0 changed to NULL 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.00 sec)
mysql> SELECT 1%`f1` FROM (SELECT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
+--------+
| 1%`f1` |
+--------+
| 0 |
+--------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT 1%`f1` FROM (SELECT DISTINCT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
+--------+
| 1%`f1` |
+--------+
| NULL |
+--------+
1 row in set, 3 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:
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:v6.2.0, it cannot be reproduced in pingcap/tidb:v6.1.3 (we can't find more intermediate versions in dockerhub):
```sql
mysql> select version();
+--------------------+
| version() |
+--------------------+
| 5.7.25-TiDB-v6.2.0 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT 1%`f1` FROM (SELECT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
+--------+
| 1%`f1` |
+--------+
| 0 |
+--------+
1 row in set, 1 warning (0.01 sec)
mysql> SELECT 1%`f1` FROM (SELECT DISTINCT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
+--------+
| 1%`f1` |
+--------+
| NULL |
+--------+
1 row in set, 3 warnings (0.00 sec)
mysql> select version();
+--------------------+
| version() |
+--------------------+
| 5.7.25-TiDB-v6.1.3 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT 1%`f1` FROM (SELECT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
Empty set, 2 warnings (0.00 sec)
mysql> SELECT 1%`f1` FROM (SELECT DISTINCT DAYNAME('2010-07-20') AS `f1` FROM t WHERE DATE_ADD(TIMEDIFF('2000-08-04 02:09:39', '2002-08-11 22:00:35'), INTERVAL 1 YEAR) NOT IN (1)) AS `t2`;
Empty set, 2 warnings (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.