[MDEV-17632] Inconsistent behavior of conversion to DATETIME for comparison Created: 2018-11-07  Updated: 2019-11-18

Status: Open
Project: MariaDB Server
Component/s: Data types, Temporal Types
Affects Version/s: 10.0, 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4

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

Issue Links:
Relates
relates to MDEV-17563 Different results using table or view... Closed
relates to MDEV-17625 Different warnings when comparing a g... Closed
relates to MDEV-17634 Regression: TIME(0)=TIME('z') returns... Closed
Epic Link: Data type cleanups

 Description   

Conversion to DATETIME for comparison from other data types works differently, both in terms of the result and in terms of warnings generated.

Summary:

  • Implicit conversion never returns warnings
  • Explicit conversion always returns a warning
  • Implicit conversion always returns zero datetime
  • Explicit conversion mostly returns zero datetime, but TIME-to-DATETIME returns NULL

Implicit conversion from TIME to DATETIME - zero datetime without warnings

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SET old_mode=ZERO_DATE_TIME_CAST;
SELECT TIMESTAMP'2001-01-01 00:00:00' > TIME'00:00:00';

+-------------------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > TIME'00:00:00' |
+-------------------------------------------------+
|                                               1 |
+-------------------------------------------------+
1 row in set (0.00 sec)

Implicit conversion from VARCHAR to DATETIME - zero datetime without warnings

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SET old_mode=ZERO_DATE_TIME_CAST;
SELECT TIMESTAMP'2001-01-01 00:00:00' > '00:00:00';

+---------------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > '00:00:00' |
+---------------------------------------------+
|                                           1 |
+---------------------------------------------+
1 row in set (0.00 sec)

Implicit conversion from INT to DATETIME - zero datetime without warnings

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SELECT TIMESTAMP'2001-01-01 00:00:00' > 0;

+------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > 0 |
+------------------------------------+
|                                  1 |
+------------------------------------+
1 row in set (0.00 sec)

Explicit conversion from TIME to DATETIME - NULL with a warning

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SET old_mode=ZERO_DATE_TIME_CAST;
SELECT TIMESTAMP'2001-01-01 00:00:00' > CAST(TIME'00:00:00' AS DATETIME);

+-------------------------------------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > CAST(TIME'00:00:00' AS DATETIME) |
+-------------------------------------------------------------------+
|                                                              NULL |
+-------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

Explicit conversion from VARCHAR to DATETIME - zero datetime with a warning

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SET old_mode=ZERO_DATE_TIME_CAST;
SELECT TIMESTAMP'2001-01-01 00:00:00' > CAST('00:00:00' AS DATETIME);

+---------------------------------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > CAST('00:00:00' AS DATETIME) |
+---------------------------------------------------------------+
|                                                             1 |
+---------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

Explicit conversion from INT to DATETIME - zero datetime with a warning

SET sql_mode='NO_ZERO_IN_DATE,NO_ZERO_DATE';
SELECT TIMESTAMP'2001-01-01 00:00:00' > CAST(0 AS DATETIME);

+------------------------------------------------------+
| TIMESTAMP'2001-01-01 00:00:00' > CAST(0 AS DATETIME) |
+------------------------------------------------------+
|                                                    1 |
+------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)



 Comments   
Comment by Sergei Golubchik [ 2018-11-07 ]

I think first four queries are fine, but the last two aren't: when sql_mode='NO_ZERO_DATE' then CAST(0 AS DATETIME) should be an invalid date, returned NULL, no matter what exactly zero value was there (same result for TIME'00:00:00', '00:00:00', and 0).

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