Bug #109353 Extra line caused by DISTINCT and a certain number of '0'
Submitted: 13 Dec 2022 5:24 Modified: 13 Dec 2022 7:10
Reporter: ZongYin Hao Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0.22, 8.0.31 OS:Any
Assigned to: CPU Architecture:Any
Tags: distinct, regression, zero

[13 Dec 2022 5:24] ZongYin Hao
Description:
In theory, the result of sql2(DISTINCT) ⊆ the result of sql1:

SELECT 1 FROM (SELECT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1)); -- sql1
SELECT 1 FROM (SELECT DISTINCT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1)); -- sql2

However, there is an extra line in the result of sql2, seems like a logical bug:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.31    |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT 1 FROM (SELECT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));
Empty set (0.00 sec)

mysql> SELECT 1 FROM (SELECT DISTINCT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)

How to repeat:
drop table if exists t;
CREATE TABLE t (c1 FLOAT UNSIGNED,c2 CHAR(20));
INSERT INTO t VALUES (0,'0'),(0,'0'),(0,'0'),(0,'0'),(0,'0'),(0,'0'),(0,'-0');

SELECT 1 FROM (SELECT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));
SELECT 1 FROM (SELECT DISTINCT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));

Suggested fix:
This is a very strange bug, hope the following list can be helpful for your debugging:
1. The bug cannot be reproduced after deleting any (0,'0') or (0,'-0') in CREATE;
2. The bug cannot be reproduced after deleting any conditions in WHERE, you cannot even delete any '0' or '0.1' in IN;
3. We look for the first version of the bug in all docker images (with format x.x.x) of https://hub.docker.com/_/mysql/tags 
We found that the bug first occurred in mysql:8.0.22, it cannot be reproduced in mysql:8.0.21:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.21    |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT 1 FROM (SELECT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));
Empty set (0.00 sec)

mysql> SELECT 1 FROM (SELECT DISTINCT c2 FROM t) AS `t1` WHERE (c2 IN (SELECT c1 FROM t)) AND (c2 NOT IN ('0','0',0.1));
Empty set (0.00 sec)
[13 Dec 2022 7:10] MySQL Verification Team
Hello ZongYin Hao,

Thank you for the report and test case.

regards,
Umesh