[MDEV-30253] Value changed after adding DISTINCT [10.0.15, 10.11.1] Created: 2022-12-17  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0.15, 10.11.1
Fix Version/s: 10.4, 10.5, 10.6, 10.11

Type: Bug Priority: Major
Reporter: qaqcatz Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: datetime, distinct-optimization, repeat
Environment:

ubuntu 18.04


Issue Links:
Relates
relates to MDEV-30250 Value changed after relaxing ON condi... Confirmed

 Description   

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

SELECT ~f1 FROM (SELECT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql1
SELECT ~f1 FROM (SELECT DISTINCT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql2

However, the value 18446744063608551615 changed to 18446744073709551614 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 ~f1 FROM (SELECT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql1
+----------------------+
| ~f1                  |
+----------------------+
| 18446744063608551615 |
+----------------------+
1 row in set (0.00 sec)
 
mysql> SELECT ~f1 FROM (SELECT DISTINCT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql2
+----------------------+
| ~f1                  |
+----------------------+
| 18446744073709551614 |
+----------------------+
1 row in set, 1 warning (0.00 sec)

How to repeat:

drop table if exists t;
CREATE TABLE t (c1 DECIMAL(40,20));
INSERT INTO t VALUES (1);
 
SELECT ~f1 FROM (SELECT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql1
SELECT ~f1 FROM (SELECT DISTINCT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql2

Hope these can be helpful for your debugging:
1. The bug cannot be reproduced after changing REPEAT(c1, 3) to REPEAT(c1, 2);
2. The bug cannot be reproduced after changing MINUTE_MICROSECOND to SECOND;
3. 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.0.15, it cannot be reproduced in mariadb:5.5.64 (we can't find more intermediate versions in dockerhub):

MariaDB [TEST]> select version();
+------------------------------+
| version()                    |
+------------------------------+
| 10.0.15-MariaDB-1~wheezy-log |
+------------------------------+
1 row in set (0.00 sec)
 
MariaDB [TEST]> SELECT ~f1 FROM (SELECT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql1
+----------------------+
| ~f1                  |
+----------------------+
| 18446744063608551615 |
+----------------------+
1 row in set (0.00 sec)
 
MariaDB [TEST]> SELECT ~f1 FROM (SELECT DISTINCT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql2
+----------------------+
| ~f1                  |
+----------------------+
| 18446744073709551614 |
+----------------------+
1 row in set, 1 warning (0.00 sec)
 
MariaDB [TEST]> select version();
+-------------------------+
| version()               |
+-------------------------+
| 5.5.64-MariaDB-1~trusty |
+-------------------------+
1 row in set (0.00 sec)
 
MariaDB [TEST]> SELECT ~f1 FROM (SELECT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql1
+------+
| ~f1  |
+------+
| NULL |
+------+
1 row in set, 2 warnings (0.00 sec)
 
MariaDB [TEST]> SELECT ~f1 FROM (SELECT DISTINCT (DATE_ADD(REPEAT(c1, 3), INTERVAL 1 MINUTE_MICROSECOND)) AS f1 FROM t) AS t1; -- sql2
+------+
| ~f1  |
+------+
| NULL |
+------+
1 row in set, 2 warnings (0.00 sec)



 Comments   
Comment by Sergei Golubchik [ 2023-01-09 ]

Adding f1 and ~~f1 to the SELECT list reveals that in the first query they're

0001-01-01 00:00:00.100000 10101000000

and in the second they're

0001-01-01 00:00:00.100000 1

So this is likely the same as MDEV-30250, let's retest after MDEV-30250 is fixed

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