[MDEV-30255] 0 changed to 0.0 caused by DISTINCT and UNION ALL [10.1.37, 10.11.1] Created: 2022-12-17  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1.37, 10.3, 10.4, 10.11.1, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11
Fix Version/s: 10.4, 10.5, 10.6

Type: Bug Priority: Minor
Reporter: qaqcatz Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: distinct-optimization, innodb, union, upstream
Environment:

ubuntu 18.04



 Description   

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

(SELECT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1'); -- sql1
(SELECT DISTINCT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1'); -- sql2

However, the value 0 changed to 0.0 after adding DISTINCT, seems like a logical bug:

mysql> select version();
+-----------------------------------------+
| version()                               |
+-----------------------------------------+
| 10.11.1-MariaDB-1:10.11.1+maria~ubu2204 |
+-----------------------------------------+
1 row in set (0.00 sec)
 
mysql> (SELECT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0              |
| 1              |
+----------------+
2 rows in set (0.00 sec)
 
mysql> (SELECT DISTINCT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0.0            |
| 1              |
+----------------+
2 rows in set (0.00 sec)

How to repeat:

drop table if exists t;
create table t (c1 double);
insert into t values (0.1);
 
(SELECT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
(SELECT DISTINCT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');

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/_/mariadb/tags
We found that the bug first occurred in mariadb:10.1.37, it cannot be reproduced in mariadb:10.1.36:

MariaDB [TEST]> select version();
+--------------------------+
| version()                |
+--------------------------+
| 10.1.37-MariaDB-1~bionic |
+--------------------------+
1 row in set (0.00 sec)
 
MariaDB [TEST]> (SELECT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0              |
| 1              |
+----------------+
2 rows in set (0.00 sec)
 
MariaDB [TEST]> (SELECT DISTINCT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0.0            |
| 1              |
+----------------+
2 rows in set (0.00 sec)
 
MariaDB [TEST]> select version();
+--------------------------+
| version()                |
+--------------------------+
| 10.1.36-MariaDB-1~bionic |
+--------------------------+
1 row in set (0.00 sec)
 
MariaDB [TEST]> (SELECT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0.0            |
| 1              |
+----------------+
2 rows in set (0.00 sec)
 
MariaDB [TEST]> (SELECT DISTINCT (c1 DIV 1)*0.1 FROM t) UNION ALL (SELECT '1');
+----------------+
| (c1 DIV 1)*0.1 |
+----------------+
| 0.0            |
| 1              |
+----------------+
2 rows in set (0.00 sec)



 Comments   
Comment by Alice Sherepa [ 2022-12-19 ]

Thank you!
I repeated as described, on 10.3-10.11 with Innodb engine, also on Mysql (5.7.38, 8.0.30).
MyIsam and Aria returned the expected results.

Generated at Thu Feb 08 10:14:51 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.