[MDEV-22828] Bad results near DST change: UNIX_TIMESTAMP(timestamp_column)=<integer literal> Created: 2020-06-08  Updated: 2023-09-12

Status: Open
Project: MariaDB Server
Component/s: Data types
Affects Version/s: 10.3, 10.4, 10.5
Fix Version/s: 10.4, 10.5

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

Issue Links:
Relates
relates to MDEV-13995 MAX(timestamp) returns a wrong result... Closed
relates to MDEV-16422 DST and unix_timestamp(now()) Confirmed
relates to MDEV-16423 DST and timestamp comparison Confirmed
relates to MDEV-19121 optimizer fails to optimize query on ... Confirmed
relates to MDEV-19961 MIN(timestamp_column) returns a wrong... Closed
relates to MDEV-22734 Assertion `mon > 0 && mon < 13' faile... Closed
relates to MDEV-32148 Inefficient WHERE timestamp_column=da... Closed
relates to MDEV-32152 Wrong results near DST for `WHERE ind... Open

 Description   

Before repeating the problem, make sure that you have timezone information loaded into MariaDB server, so this statement does not produce errors or warnings:

SET time_zone='Europe/Moscow';

I create and populate a table with a TIMESTAMP column as follows:

SET time_zone='Europe/Moscow';
CREATE OR REPLACE TABLE t1 (a TIMESTAMP);
SET timestamp=1288477526;  -- this is summer time
INSERT INTO t1 VALUES (NULL);
SET timestamp=1288477526+3600; -- this is winter time
INSERT INTO t1 VALUES (null);
SELECT a, UNIX_TIMESTAMP(a) FROM t1;

+---------------------+-------------------+
| a                   | UNIX_TIMESTAMP(a) |
+---------------------+-------------------+
| 2010-10-31 02:25:26 |        1288477526 |
| 2010-10-31 02:25:26 |        1288481126 |
+---------------------+-------------------+

Now I want to get the first record:

SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a='2010-10-31 02:25:26' AND UNIX_TIMESTAMP(a)=1288477526;

+---------------------+-------------------+
| a                   | UNIX_TIMESTAMP(a) |
+---------------------+-------------------+
| 2010-10-31 02:25:26 |        1288477526 |
| 2010-10-31 02:25:26 |        1288481126 |
+---------------------+-------------------+

Looks wrong. Expect to get one record.

EXPLAIN EXTENDED for this query reports that the UNIX_TIMESTAMP(a)=1288477526 was removed from the condition:

EXPLAIN EXTENDED SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a='2010-10-31 02:25:26' AND UNIX_TIMESTAMP(a)=1288477526;
SHOW WARNINGS;

+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|    1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 2    |   100.00 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+

+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                      |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a`,unix_timestamp(`test`.`t1`.`a`) AS `UNIX_TIMESTAMP(a)` from `test`.`t1` where `test`.`t1`.`a` = TIMESTAMP'2010-10-31 02:25:26' |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+

Now I want to get the second record:

SELECT a,UNIX_TIMESTAMP(a) FROM t1 WHERE a='2010-10-31 02:25:26' AND UNIX_TIMESTAMP(a)=1288481126;

Empty set (0.001 sec)

Looks wrong. Expect one record.

EXPLAIN EXTENDED for this query shows that the condition was erroneously calculated as always-FALSE:

EXPLAIN EXTENDED SELECT *, UNIX_TIMESTAMP(a) FROM t1 WHERE a='2010-10-31 02:25:26' AND UNIX_TIMESTAMP(a)=1288481126;
SHOW WARNINGS;

+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra            |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------+
|    1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Impossible WHERE |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------+

+-------+------+---------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                       |
+-------+------+---------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a`,unix_timestamp(`test`.`t1`.`a`) AS `UNIX_TIMESTAMP(a)` from `test`.`t1` where 0 |
+-------+------+---------------------------------------------------------------------------------------------------------------+


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