[MDEV-21553] DATE(IFNULL(XXX, 0)) is not returning same result with different XXX Created: 2020-01-22  Updated: 2020-06-16  Resolved: 2020-06-12

Status: Closed
Project: MariaDB Server
Component/s: Data types, Server
Affects Version/s: 5.5, 10.1, 10.2, 10.3, 10.4
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Eimantas Assignee: Alexander Barkov
Resolution: Not a Bug Votes: 0
Labels: upstream
Environment:

Any


Issue Links:
Relates
relates to MDEV-17736 Inconsistency between DATE('0') and T... Open

 Description   

DATE(IFNULL(XXX, 0)) is not returning same result with different XXX

mysql> SELECT DATE(0), DATE(IFNULL(NULL, 0)), DATE(IFNULL(DATE(NULL), 0)), DATE(NULL);
+------------+-----------------------+-----------------------------+------------+
| DATE(0)    | DATE(IFNULL(NULL, 0)) | DATE(IFNULL(DATE(NULL), 0)) | DATE(NULL) |
+------------+-----------------------+-----------------------------+------------+
| 0000-00-00 | 0000-00-00            | NULL                        | NULL       |
+------------+-----------------------+-----------------------------+------------+

3rd column DATE(IFNULL(DATE(NULL), 0)) expected to be 0000-00-00



 Comments   
Comment by Elena Stepanova [ 2020-01-22 ]

Reproducible on all of 5.5-10.5 and MySQL 5.6, 8.0 (didn't try other MySQL versions).

Comment by Alexander Barkov [ 2020-06-12 ]

This is not a bug. This expression:

IFNULL(DATE(NULL),0)

returns a string '0'.

Then the string '0' is converted to DATE, and gives NULL.

MariaDB [test]> SELECT IFNULL(DATE(NULL),0);
Field   1:  `IFNULL(DATE(NULL),0)`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       VAR_STRING
Collation:  utf8_general_ci (33)
Length:     30
Max_length: 1
Decimals:   39
Flags:      NOT_NULL 
 
 
+----------------------+
| IFNULL(DATE(NULL),0) |
+----------------------+
| 0                    |
+----------------------+
1 row in set (0.001 sec)

MariaDB [test]> SELECT DATE('0');
Field   1:  `DATE('0')`
Catalog:    `def`
Database:   ``
Table:      ``
Org_table:  ``
Type:       DATE
Collation:  binary (63)
Length:     10
Max_length: 0
Decimals:   0
Flags:      BINARY 
 
 
+-----------+
| DATE('0') |
+-----------+
| NULL      |
+-----------+
1 row in set, 1 warning (0.001 sec)

Comment by Eimantas [ 2020-06-12 ]

I know that DATE('0') is NULL, but there are no strings in examples provided.

Why IFNULL(DATE(NULL),0) is '0' string?
Second parameter in IFNULL is INT type.

Comment by Alexander Barkov [ 2020-06-13 ]

Historically all hybrid functions in MariaDB (and in MySQL), like:

  • IFNULL(date_arg,int_arg)
  • COALESCE(date_arg,int_arg)
  • CASE .. THEN date_arg .. THEN int_arg

resolve their return data type as VARCHAR.

VARCHAR is the common datatype that can store both DATE and INT without data loss.

The same happens with UNION:

SELECT date_arg ... UNION SELECT int_arg ...

If you need a different behaviour instead of this style automatic data type conversion, please use the explicit CAST operator.

Comment by Eimantas [ 2020-06-13 ]

Thank you! That makes everything clear.

"IFNULL(?, 0)" changing to "IFNULL(?, DATE(0))" and it works as expected:
DATE(IFNULL(DATE(NULL), DATE(0)))

Generated at Thu Feb 08 09:08:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.