Bug #79015 When function DATEDIFF and NULLIF work together, incorrect result is returned
Submitted: 29 Oct 2015 9:03 Modified: 29 Oct 2015 9:15
Reporter: Su Dylan Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.7.8, 5.6.27, 5.6.28, 5.7.9 OS:Any
Assigned to: CPU Architecture:Any
Tags: regression

[29 Oct 2015 9:03] Su Dylan
Description:
Output:
=======
mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                   NULL | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

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

Problem:
========
The following sql is expected to return 1:

select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01'))

How to repeat:
select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');

Suggested fix:
The following sql returns 1:

select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01'))
[29 Oct 2015 9:15] MySQL Verification Team
Hello Su Dylan,

Thank you for the report and test case.
Observed that 5.6/5.7 latest builds are affected.

Thanks,
Umesh
[29 Oct 2015 9:16] MySQL Verification Team
// 5.6.27, 5.6.28, 5.7.9 affected

[root@cluster-repo ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.6.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                   NULL | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

// 5.6.28

[umshastr@hod03]/export/umesh/server/binaries/mysql-advanced-5.6.28: bin/mysql -uroot -S /tmp/mysql_ushastry.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.28-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                   NULL | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

// 5.7.9

[umshastr@hod03]/export/umesh/server/binaries/mysql-5.7.9: bin/mysql -uroot -S /tmp/mysql_ushastry.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                   NULL | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
[29 Oct 2015 9:17] MySQL Verification Team
// 5.1.77/5.5.47

[umshastr@hod03]/export/umesh/server/binaries/mysql-5.1.77: bin/mysql -uroot -S /tmp/mysql_ushastry.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.77 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                      1 | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

// 5.5.47 

[umshastr@hod03]/export/umesh/server/binaries/mysql-5.5.47: bin/mysql -uroot -S /tmp/mysql_ushastry.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.47 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')), NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01');
+----------------------------------------------------------------------------------------+------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01')) | NULLIF('2015-01-01 01:01:01', '1999-01-01 01:01:01') |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
|                                                                                      1 | 2015-01-01 01:01:01                                  |
+----------------------------------------------------------------------------------------+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01');
+---------------------------------------------------------+
| DATEDIFF( '2015-01-02 01:01:01', '2015-01-01 01:01:01') |
+---------------------------------------------------------+
|                                                       1 |
+---------------------------------------------------------+
1 row in set (0.00 sec)