[MDEV-11333] Expect "Impossible where condition" for WHERE timestamp_field>=DATE_ADD(TIMESTAMP'9999-01-01 00:00:00',INTERVAL 1000 YEAR) Created: 2016-11-22  Updated: 2017-04-07  Resolved: 2016-11-25

Status: Closed
Project: MariaDB Server
Component/s: Optimizer, Temporal Types
Affects Version/s: 10.0, 10.1, 10.2, 10.3
Fix Version/s: 10.3.0

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: datatype

Issue Links:
Blocks
blocks MDEV-4912 Data type plugin API version 1 Closed
blocks MDEV-11337 Split Item::save_in_field() into virt... Closed

 Description   

This query returns NULL with a warning about datetime overflow.

SELECT DATE_ADD(TIMESTAMP'9999-01-01 00:00:00',INTERVAL 1000 YEAR);

If I use now this expression in a WHERE condition on a TIMESTAMP field with DEFAULT CURRENT_TIMESTAMP:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, KEY(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
INSERT INTO t1 VALUES('2001-01-01'),('2002-02-02'),('2003-03-03');
EXPLAIN SELECT * FROM t1 WHERE a >= DATE_ADD(TIMESTAMP'9999-01-01 00:00:00',INTERVAL 1000 YEAR);

it reports that one row will be checked:

+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| id   | select_type | table | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|    1 | SIMPLE      | t1    | range | a             | a    | 4       | NULL |    1 | Using where; Using index |
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+

This is wrong. It should report "Impossible where".

Note, if I change the condition wrapping DATE_ADD to COALESCE:

EXPLAIN SELECT * FROM t1 WHERE a >= COALESCE(DATE_ADD(TIMESTAMP'9999-01-01 00:00:00',INTERVAL 1000 YEAR));

it starts to report "Impossible where" correctly:

+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                               |
+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
|    1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE noticed after reading const tables |
+------+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+



 Comments   
Comment by Alexander Barkov [ 2016-11-25 ]

Fixed by a join patch for MDEV-11337.

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