[MDEV-30300] Value changed after relaxing HAVING condition [10.5.4,10.11.1] Created: 2022-12-25  Updated: 2023-11-28

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

Type: Bug Priority: Major
Reporter: qaqcatz Assignee: Oleksandr Byelkin
Resolution: Unresolved Votes: 0
Labels: datetime
Environment:

ubuntu 18.04



 Description   

Description:
In theory, the result of sql1 ⊆ the result of sql2:

SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); -- sql1
SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1; -- sql2

Because the 'HAVING 1' in sql2 is always true, but the 'HAVING !(`f1`=1)' in sql1 may not be true.

However, the value `14757395258967642091` changed to `14757395258987761147` after relaxing HAVING condition, 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*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); -- sql1
+----------------------+
| f1                   |
+----------------------+
| 14757395258967642091 |
+----------------------+
1 row in set, 2 warnings (0.01 sec)
 
mysql> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1; -- sql2
+----------------------+
| f1                   |
+----------------------+
| 14757395258987761147 |
+----------------------+
1 row in set (0.00 sec)

How to repeat:

drop table if exists t;
CREATE TABLE t (c1 VARCHAR(20));
INSERT INTO t VALUES ('\n3');
 
SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); -- sql1
SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1; -- sql2

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.5.4, it cannot be reproduced in mariadb:10.5.3:

MariaDB [TEST]> select version();
+-------------------------------------+
| version()                           |
+-------------------------------------+
| 10.5.4-MariaDB-1:10.5.4+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)
 
MariaDB [TEST]> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); -- sql1
+----------------------+
| f1                   |
+----------------------+
| 14757395258967642091 |
+----------------------+
1 row in set, 2 warnings (0.000 sec)
 
MariaDB [TEST]> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1; -- sql2
+----------------------+
| f1                   |
+----------------------+
| 14757395258987761147 |
+----------------------+
1 row in set (0.000 sec)
 
MariaDB [TEST]> select version();
+-------------------------------------+
| version()                           |
+-------------------------------------+
| 10.5.3-MariaDB-1:10.5.3+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)
 
MariaDB [TEST]> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); -- sql1
+---------------------+
| f1                  |
+---------------------+
| 9223372036854775807 |
+---------------------+
1 row in set, 4 warnings (0.000 sec)
 
MariaDB [TEST]> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1; -- sql2
+---------------------+
| f1                  |
+---------------------+
| 9223372036854775807 |
+---------------------+
1 row in set, 1 warning (0.000 sec)



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

Thank you! I repeated as described.
10.5-10.11:

MariaDB [test]> SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1); 
+----------------------+
| f1                   |
+----------------------+
| 14757395258967642091 |
+----------------------+
1 row in set, 2 warnings (0,001 sec)
 
Warning (Code 1292): Truncated incorrect DECIMAL value: '2017-11-29'
Warning (Code 1292): Truncated incorrect DECIMAL value: '2017-11-29'

on 10.4:

SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING !(`f1`=1);
f1
9223372036854775807
Warnings:
Warning	1916	Got overflow when converting '14757395258967641289.6' to INT. Value truncated
Warning	1916	Got overflow when converting '14757395258967641289.6' to INT. Value truncated
SELECT (~c1*ABS(0.8)|DATE_ADD('2017-11-22', INTERVAL 1 WEEK)) AS `f1` FROM t HAVING 1;
f1
9223372036854775807
Warnings:
Warning	1916	Got overflow when converting '14757395258967641289.6' to INT. Value truncated

so maybe changes after MDEV-22715

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