[MDEV-9707] MAX(timestamp(6) column) in correlated sub-query returns non-existent row data in original table Created: 2016-03-10  Updated: 2016-04-20  Resolved: 2016-04-20

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Subquery, Temporal Types
Affects Version/s: 10.1.10, 5.5, 10.0, 10.1
Fix Version/s: 5.5.49, 10.0.25, 10.1.14

Type: Bug Priority: Major
Reporter: Dylan Su Assignee: Sergei Golubchik
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Problem/Incident
is caused by MDEV-4284 Assertion `cmp_items[(uint)cmp_type]'... Closed

 Description   

Recreate:
=====

CREATE TABLE maria_bug (
  id INT NOT NULL,
  update_time TIMESTAMP(6) NOT NULL
);
INSERT INTO maria_bug VALUES(1, '2001-01-01 00:00:00.200000');
INSERT INTO maria_bug VALUES(1, '2001-01-01 00:00:00.100000');
SELECT * FROM maria_bug;
SELECT *, (SELECT MAX(m2.update_time) FROM maria_bug m2 WHERE m1.id <> 0 ) max_update FROM maria_bug m1;

Output:
=====

mysql> CREATE TABLE maria_bug (
    ->   id INT NOT NULL,
    ->   update_time TIMESTAMP(6) NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)
 
mysql> INSERT INTO maria_bug VALUES(1, '2001-01-01 00:00:00.200000');
NSERT INTO mQuery OK, 1 row affected (0.00 sec)
 
mysql> INSERT INTO maria_bug VALUES(1, '2001-01-01 00:00:00.100000');
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT * FROM maria_bug;
+----+----------------------------+
| id | update_time                |
+----+----------------------------+
|  1 | 2001-01-01 00:00:00.200000 |
|  1 | 2001-01-01 00:00:00.100000 |
+----+----------------------------+
2 rows in set (0.00 sec)
 
mysql> SELECT *, (SELECT MAX(m2.update_time) FROM maria_bug m2 WHERE m1.id <> 0 ) max_update FROM maria_bug m1;
+----+----------------------------+----------------------------+
| id | update_time                | max_update                 |
+----+----------------------------+----------------------------+
|  1 | 2001-01-01 00:00:00.200000 | 2001-01-01 00:00:00.200000 |
|  1 | 2001-01-01 00:00:00.100000 | 2001-01-01 00:00:00.000000 |
+----+----------------------------+----------------------------+
2 rows in set (0.00 sec)
 
mysql> select version();
+-----------------+
| version()       |
+-----------------+
| 10.1.10-MariaDB |
+-----------------+
1 row in set (0.00 sec)

Problem:
=====
MAX() result '2001-01-01 00:00:00.000000' is not in the table.



 Comments   
Comment by Elena Stepanova [ 2016-03-10 ]

Thanks for the report and the test case.

Reproducible on current and older versions of 5.5, 10.0, 10.1. Not reproducible on MySQL 5.6.

The problem was introduced by this commit:

commit 19fd5dcb159414ea54329b0d3fec6c6d0dc5c0d7
Author: Sergei Golubchik <sergii@pisem.net>
Date:   Sun Mar 17 11:41:25 2013 +0100
 
    MDEV-4284 Assertion `cmp_items[(uint)cmp_type]' fails in sql/item_cmpfunc.cc
    
    Flip the switch and create Item_cache based on the argument's cmp_type, not argument's result_type().
    Fix subselect_engine to calculate cmp_type correctly
    
    sql/item_subselect.h:
      mdev:4284

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