[MDEV-33307] Wrong query results of ADDDATE Function Created: 2024-01-25  Updated: 2024-01-30

Status: Open
Project: MariaDB Server
Component/s: Optimizer, Storage Engine - InnoDB
Affects Version/s: 10.4, 10.5, 10.6, 10.11, 11.0, 11.1, 11.2, 11.3, 11.4.0
Fix Version/s: 10.4, 10.5, 10.6, 10.11, 11.0, 11.1, 11.2, 11.3

Type: Bug Priority: Major
Reporter: Shiyang Ye Assignee: Alexander Barkov
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Windows 11 CLI



 Description   

Description

In theory, sql1 and sql2 should return the same result, however,sql1 return -2025 while sql2 return -20240103

SQL1:

SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1 FROM t0) as t WHERE f1;

SQL2:

SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1, (t0.c0 - ADDDATE('2024-01-01', 1)) IS TRUE AS flag FROM t0) as t WHERE flag=1;

How to repeat

DROP DATABASE IF EXISTS database0;
CREATE DATABASE database0;
USE database0;
CREATE TABLE t0(c0 DECIMAL NULL, c1 DECIMAL) ;
INSERT IGNORE INTO t0(c0, c1) VALUES('-1', 1);

version

 
SELECT version();
 
+----------------+
| version()      |
+----------------+
| 11.4.0-MariaDB |
+----------------+
1 row in set (0.00 sec)

SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1 FROM t0) as t WHERE f1; --sql1
 
+-------+
| f1    |
+-------+
| -2025 |
+-------+

SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1, (t0.c0 - ADDDATE('2024-01-01', 1)) IS TRUE AS flag FROM t0) as t WHERE flag=1; --sql2
 
+-----------+
| f1        |
+-----------+
| -20240103 |
+-----------+



 Comments   
Comment by Alice Sherepa [ 2024-01-26 ]

THanks! I repeated on 10.4-11.4 versions, with InnoDB, the reason - types conversion

MariaDB [test]> explain extended SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1, (t0.c0 - ADDDATE('2024-01-01', 1)) IS TRUE AS flag FROM t0) as t WHERE flag=1;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|    1 | SIMPLE      | t0    | ALL  | NULL          | NULL | NULL    | NULL | 1    |   100.00 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0,001 sec)
 
Note (Code 1003): select `test`.`t0`.`c0` - ('2024-01-01' + interval 1 day) AS `f1` from `test`.`t0` where `test`.`t0`.`c0` - <cache>('2024-01-01' + interval 1 day) is true = 1
MariaDB [test]>  SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1, (t0.c0 - ADDDATE('2024-01-01', 1)) IS TRUE AS flag FROM t0) as t WHERE flag=1;
+-----------+
| f1        |
+-----------+
| -20240103 |
+-----------+
1 row in set, 1 warning (0,001 sec)
 
Warning (Code 1292): Truncated incorrect DOUBLE value: '2024-01-02'

MariaDB [test]> explain extended SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1 FROM t0) as t WHERE f1;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|    1 | SIMPLE      | t0    | ALL  | NULL          | NULL | NULL    | NULL | 1    |   100.00 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0,001 sec)
 
Note (Code 1003): select `test`.`t0`.`c0` - <cache>('2024-01-01' + interval 1 day) AS `f1` from `test`.`t0` where `test`.`t0`.`c0` - <cache>('2024-01-01' + interval 1 day) <> 0
MariaDB [test]>  SELECT f1 FROM (SELECT (t0.c0 - ADDDATE('2024-01-01', 1)) AS f1 FROM t0) as t WHERE f1;
+-------+
| f1    |
+-------+
| -2025 |
+-------+
1 row in set, 2 warnings (0,001 sec)
 
Warning (Code 1292): Truncated incorrect DOUBLE value: '2024-01-02'
Warning (Code 1292): Truncated incorrect DOUBLE value: '2024-01-02'

Comment by Shiyang Ye [ 2024-01-30 ]

Dear devps, when would you confirm the bug and fix that?

Comment by Sergei Golubchik [ 2024-01-30 ]

The bug is confirmed, see the previous comment.
As for the fix — there's no specific date, when we'll get to it.

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