Commit 10961504 authored by unknown's avatar unknown
Browse files

Fixed bug #20869.

The bug caused a crash of the server if a subquery with
ORDER BY DESC used the range access method.
The bug happened because the method QUICK_SELECT_DESC::reset
was not reworked after MRR interface had been introduced.


mysql-test/r/subselect.result:
  Added a test case for bug #20869.
mysql-test/t/subselect.test:
  Added a test case for bug #20869.
parent 233ff83c
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -3238,3 +3238,39 @@ i
10000000000000000000
DROP TABLE t1;
DROP TABLE t2;
CREATE TABLE t1 (
i1 int(11) NOT NULL default '0',
i2 int(11) NOT NULL default '0',
t datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY  (i1,i2,t)
);
INSERT INTO t1 VALUES 
(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
(24,2,'2005-05-27 12:40:06');
CREATE TABLE t2 (
i1 int(11) NOT NULL default '0',
i2 int(11) NOT NULL default '0',
t datetime default NULL,
PRIMARY KEY  (i1)
);
INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
EXPLAIN
SELECT * FROM t1,t2
WHERE t1.t = (SELECT t1.t FROM t1 
WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
ORDER BY t1.t DESC LIMIT 1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
1	PRIMARY	t1	index	NULL	PRIMARY	16	NULL	11	Using where; Using index
2	DEPENDENT SUBQUERY	t1	range	PRIMARY	PRIMARY	16	NULL	5	Using where; Using index
SELECT * FROM t1,t2
WHERE t1.t = (SELECT t1.t FROM t1 
WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
ORDER BY t1.t DESC LIMIT 1);
i1	i2	t	i1	i2	t
24	1	2005-05-27 12:40:30	24	1	2006-06-20 12:29:40
DROP TABLE t1, t2;
+38 −0
Original line number Diff line number Diff line
@@ -2161,3 +2161,41 @@ SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);

DROP TABLE t1;
DROP TABLE t2;

# 
# Bug#20869: subselect with range access by DESC
#

CREATE TABLE t1 (
  i1 int(11) NOT NULL default '0',
  i2 int(11) NOT NULL default '0',
  t datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (i1,i2,t)
);
INSERT INTO t1 VALUES 
(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
(24,2,'2005-05-27 12:40:06');

CREATE TABLE t2 (
  i1 int(11) NOT NULL default '0',
  i2 int(11) NOT NULL default '0',
  t datetime default NULL,
  PRIMARY KEY  (i1)
);
INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');

EXPLAIN
SELECT * FROM t1,t2
  WHERE t1.t = (SELECT t1.t FROM t1 
                  WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
                    ORDER BY t1.t DESC LIMIT 1);
SELECT * FROM t1,t2
  WHERE t1.t = (SELECT t1.t FROM t1 
                  WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
                    ORDER BY t1.t DESC LIMIT 1);

DROP TABLE t1, t2;
+1 −1
Original line number Diff line number Diff line
@@ -668,7 +668,7 @@ class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
#ifdef NOT_USED
  bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
#endif
  int reset(void) { next=0; rev_it.rewind(); return 0; }
  int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
  List<QUICK_RANGE> rev_ranges;
  List_iterator<QUICK_RANGE> rev_it;
};