1
2--echo # Bug#36981 - "innodb crash when selecting for update"
3--echo #
4
5#
6# Test 1: Test based on the reproduction test case for this bug.
7#         This query resulted in a crash in InnoDB due to
8#         InnoDB changing from using the index which the push condition
9#         where for to use the clustered index due to "SELECT ... FOR UPDATE".
10#
11CREATE TABLE t1 (
12  c1 CHAR(1),
13  c2 CHAR(10),
14  KEY (c1)
15);
16
17INSERT INTO t1 VALUES ('3', null);
18
19SELECT * FROM t1 WHERE c1='3' FOR UPDATE;
20
21DROP TABLE t1;
22
23#
24# Test 2: Extended test case to test that the correct rows are returned.
25#         This test is for ensuring that if InnoDB refuses to accept
26#         the pushed index condition it is still evaluated.
27#
28
29CREATE TABLE t1 (a INT);
30INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
31
32CREATE TABLE t2 (a INT);
33INSERT INTO t2 SELECT A.a + 10*(B.a + 10*C.a) FROM t1 A, t1 B, t1 C;
34
35CREATE TABLE t3 (
36  c1 CHAR(10) NOT NULL,
37  c2 CHAR(10) NOT NULL,
38  c3 CHAR(200) NOT NULL,
39  KEY (c1)
40);
41
42INSERT INTO t3
43  SELECT CONCAT('c-',1000+t2.a,'=w'), CONCAT('c-',1000+ t2.a,'=w'), 'filler'
44  FROM t2;
45
46INSERT INTO t3
47  SELECT CONCAT('c-',1000+t2.a,'=w'), CONCAT('c-',2000+t2.a,'=w'), 'filler-1'
48  FROM t2;
49
50INSERT INTO t3
51  SELECT CONCAT('c-',1000+t2.a,'=w'), CONCAT('c-',3000+t2.a,'=w'), 'filler-2'
52  FROM t2;
53
54--sorted_result
55SELECT c1,c3 FROM t3 WHERE c1 >= 'c-1994=w' and c1 != 'c-1996=w' FOR UPDATE;
56
57DROP TABLE t1,t2,t3;
58
59--echo #
60--echo # Bug#43360 - Server crash with a simple multi-table update
61--echo #
62
63CREATE TABLE t1 (
64  a CHAR(2) NOT NULL PRIMARY KEY,
65  b VARCHAR(20) NOT NULL,
66  KEY (b)
67);
68
69CREATE TABLE t2 (
70  a CHAR(2) NOT NULL PRIMARY KEY,
71  b VARCHAR(30) NOT NULL,
72  KEY (b)
73);
74
75INSERT INTO t1 VALUES
76('AB','MySQL AB'),
77('JA','Sun Microsystems'),
78('MS','Microsoft'),
79('IB','IBM- Inc.'),
80('GO','Google Inc.');
81
82INSERT INTO t2 VALUES
83('AB','Sweden'),
84('JA','USA'),
85('MS','United States of America'),
86('IB','North America'),
87('GO','South America');
88
89UPDATE t1,t2 SET t1.b=UPPER(t1.b) WHERE t1.b LIKE 'United%';
90
91SELECT * FROM t1 ORDER BY a;
92
93SELECT * FROM t2 ORDER BY a;
94
95DROP TABLE t1,t2;
96
97--echo #
98--echo # Bug#40992 - InnoDB: Crash when engine_condition_pushdown is on
99--echo #
100
101CREATE TABLE t (
102  dummy INT PRIMARY KEY,
103  a INT UNIQUE,
104  b INT
105);
106
107INSERT INTO t VALUES (1,1,1),(3,3,3),(5,5,5);
108
109SELECT * FROM t WHERE a > 2 FOR UPDATE;
110
111DROP TABLE t;
112
113--echo #
114--echo # Bug#35080 - Innodb crash at mem_block_get_len line 72
115--echo #
116
117CREATE TABLE t1 (
118  t1_autoinc INT(11) NOT NULL AUTO_INCREMENT,
119  uuid VARCHAR(36) DEFAULT NULL,
120  PRIMARY KEY (t1_autoinc),
121  KEY k (uuid)
122);
123
124CREATE TABLE t2 (
125  t2_autoinc INT(11) NOT NULL AUTO_INCREMENT,
126  uuid VARCHAR(36) DEFAULT NULL,
127  date DATETIME DEFAULT NULL,
128  PRIMARY KEY (t2_autoinc),
129  KEY k (uuid)
130);
131
132CREATE VIEW v1 AS
133  SELECT t1_autoinc, uuid
134  FROM t1
135  WHERE (ISNULL(uuid) OR (uuid like '%-%'));
136
137CREATE VIEW v2 AS
138  SELECT t2_autoinc, uuid, date
139  FROM t2
140  WHERE (ISNULL(uuid) OR (LENGTH(uuid) = 36));
141
142CREATE PROCEDURE delete_multi (IN uuid CHAR(36))
143  DELETE v1, v2 FROM v1 INNER JOIN v2
144  ON v1.uuid = v2.uuid
145  WHERE v1.uuid = @uuid;
146
147SET @uuid = UUID();
148
149INSERT INTO v1 (uuid) VALUES (@uuid);
150INSERT INTO v2 (uuid, date) VALUES (@uuid, '2009-09-09');
151
152CALL delete_multi(@uuid);
153
154DROP procedure delete_multi;
155DROP table t1,t2;
156DROP view v1,v2;
157
158--echo #
159--echo # Bug#41996 - multi-table delete crashes server (InnoDB table)
160--echo #
161
162CREATE TABLE t1 (
163  b BIGINT,
164  i INT,
165  KEY (b)
166);
167
168INSERT INTO t1 VALUES (2, 2);
169
170DELETE t1 FROM t1 a, t1 WHERE a.i=t1.b;
171
172DROP TABLE t1;
173
174--echo #
175--echo # Bug#43448 - Server crashes on multi table delete with Innodb
176--echo #
177
178CREATE TABLE t1 (
179  id1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
180  t CHAR(12)
181);
182
183CREATE TABLE t2 (
184  id2 INT NOT NULL,
185  t CHAR(12)
186);
187
188CREATE TABLE t3(
189  id3 INT NOT NULL,
190  t CHAR(12),
191  INDEX(id3)
192);
193
194delimiter |;
195
196CREATE PROCEDURE insert_data ()
197BEGIN
198  DECLARE i1 INT DEFAULT 20;
199  DECLARE i2 INT;
200  DECLARE i3 INT;
201
202  WHILE (i1 > 0) DO
203    INSERT INTO t1(t) VALUES (i1);
204    SET i2 = 2;
205    WHILE (i2 > 0) DO
206      INSERT INTO t2(id2, t) VALUES (i1, i2);
207      SET i3 = 2;
208      WHILE (i3 > 0) DO
209        INSERT INTO t3(id3, t) VALUES (i1, i2);
210        SET i3 = i3 -1;
211      END WHILE;
212      SET i2 = i2 -1;
213    END WHILE;
214    SET i1 = i1 - 1;
215  END WHILE;
216END |
217
218delimiter ;|
219
220CALL insert_data();
221
222SELECT COUNT(*) FROM t1 WHERE id1 > 10;
223SELECT COUNT(*) FROM t2 WHERE id2 > 10;
224SELECT COUNT(*) FROM t3 WHERE id3 > 10;
225
226DELETE t1, t2, t3
227FROM t1, t2, t3
228WHERE t1.id1 = t2.id2 AND t2.id2 = t3.id3 AND t1.id1 > 3;
229
230SELECT COUNT(*) FROM t1;
231SELECT COUNT(*) FROM t2;
232SELECT COUNT(*) FROM t3;
233
234DROP PROCEDURE insert_data;
235DROP TABLE t1, t2, t3;
236
237--echo #
238--echo # Bug#42580 - Innodb's ORDER BY ..LIMIT returns no rows for
239--echo #             null-safe operator <=> NULL
240--echo #
241SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
242CREATE TABLE t1(
243  c1 DATE NOT NULL,
244  c2 DATE NULL,
245  c3 DATETIME,
246  c4 TIMESTAMP,
247  PRIMARY KEY(c1),
248  UNIQUE(c2)
249);
250
251--echo
252INSERT INTO t1 VALUES('0000-00-00', '0000-00-00', '2008-01-04', '2008-01-05');
253INSERT INTO t1 VALUES('2007-05-25', '2007-05-25', '2007-05-26', '2007-05-26');
254INSERT INTO t1 VALUES('2008-01-01', NULL        , '2008-01-02', '2008-01-03');
255INSERT INTO t1 VALUES('2008-01-17', NULL        , NULL        , '2009-01-29');
256INSERT INTO t1 VALUES('2009-01-29', '2009-01-29', '2009-01-29', '2009-01-29');
257
258--echo
259SELECT * FROM t1 WHERE c2 <=> NULL ORDER BY c1,c2;
260--echo
261SELECT * FROM t1 WHERE c2 <=> NULL ORDER BY c1,c2 LIMIT 2;
262
263--echo
264DROP TABLE t1;
265SET sql_mode = default;
266--echo #
267--echo # Bug#43617 - Innodb returns wrong results with timestamp's range value
268--echo #             in IN clause
269--echo # (Note: Fixed by patch for BUG#42580)
270--echo #
271SET sql_mode = 'NO_ENGINE_SUBSTITUTION';
272CREATE TABLE t1(
273  c1 TIMESTAMP NOT NULL,
274  c2 TIMESTAMP NULL,
275  c3 DATE,
276  c4 DATETIME,
277  PRIMARY KEY(c1),
278  UNIQUE INDEX(c2)
279);
280
281INSERT INTO t1 VALUES
282  ('0000-00-00 00:00:00','0000-00-00 00:00:00','2008-01-04','2008-01-05 00:00:00'),
283  ('1971-01-01 00:00:01','1980-01-01 00:00:01','2009-01-01','2009-01-02 00:00:00'),
284  ('1999-01-01 00:00:00','1999-01-01 00:00:00', NULL,        NULL),
285  ('2007-05-23 09:15:28','2007-05-23 09:15:28','2007-05-24','2007-05-24 09:15:28'),
286  ('2007-05-27 00:00:00','2007-05-25 00:00:00','2007-05-26','2007-05-26 00:00:00'),
287  ('2008-01-01 00:00:00', NULL,                '2008-01-02','2008-01-03 00:00:00'),
288  ('2009-01-29 11:11:27','2009-01-29 11:11:27','2009-01-29','2009-01-29 11:11:27'),
289  ('2038-01-09 03:14:07','2038-01-09 03:14:07','2009-01-05','2009-01-06 00:00:00');
290
291--echo
292SELECT *
293FROM t1
294WHERE c2 IN ('1971-01-01 00:00:01','2038-01-09 03:14:07')
295ORDER BY c2;
296
297--echo
298SELECT *
299FROM t1
300WHERE c2 IN ('1971-01-01 00:00:01','2038-01-09 03:14:07')
301ORDER BY c2 LIMIT 2;
302
303--echo
304SELECT *
305FROM t1
306WHERE c2 IN ('1971-01-01 00:00:01','2038-01-09 03:14:07')
307ORDER BY c2 DESC;
308
309--echo
310SELECT *
311FROM t1
312WHERE c2 IN ('1971-01-01 00:00:01','2038-01-09 03:14:07')
313ORDER BY c2 DESC LIMIT 2;
314
315--echo
316DROP TABLE t1;
317SET sql_mode = default;
318--echo #
319--echo # Bug#43249 - Innodb returns zero time for the time column
320--echo #             with <=> NULL order by limit
321--echo # (Note: Fixed by patch for BUG#42580)
322--echo #
323
324CREATE TABLE t1(
325  c1 TIME NOT NULL,
326  c2 TIME NULL,
327  c3 DATE,
328  PRIMARY KEY(c1),
329  UNIQUE INDEX(c2)
330);
331
332INSERT INTO t1 VALUES('8:29:45',NULL,'2009-02-01');
333
334--echo
335# First time, good results:
336SELECT * FROM t1 WHERE c2 <=> NULL ORDER BY c2 LIMIT 2;
337
338--echo
339# Second time, bad results:
340SELECT * FROM t1 WHERE c2 <=> NULL ORDER BY c2 LIMIT 2;
341
342DROP TABLE t1;
343
344--echo #
345--echo # BUG#43618: MyISAM&Maria returns wrong results with 'between'
346--echo #            on timestamp
347--echo #
348
349CREATE TABLE t1(
350   ts TIMESTAMP NOT NULL,
351   c char NULL,
352   PRIMARY KEY(ts)
353);
354
355INSERT INTO t1 VALUES
356   ('1971-01-01','a'),
357   ('2007-05-25','b'),
358   ('2008-01-01','c'),
359   ('2038-01-09','d');
360
361-- disable_query_log
362-- disable_result_log
363ANALYZE TABLE t1;
364-- enable_result_log
365-- enable_query_log
366
367--disable_warnings
368
369--echo
370--echo # Execute select with invalid timestamp, desc ordering
371SET sql_mode = '';
372SELECT *
373FROM t1
374WHERE ts BETWEEN '0000-00-00' AND '2010-00-01 00:00:00'
375ORDER BY ts DESC
376LIMIT 2;
377SET sql_mode = default;
378
379--echo
380--echo # Should use index condition
381EXPLAIN
382SELECT *
383FROM t1
384WHERE ts BETWEEN '0000-00-00' AND '2010-00-01 00:00:00'
385ORDER BY ts DESC
386LIMIT 2;
387--echo
388
389--enable_warnings
390
391DROP TABLE t1;
392
393--echo #
394--echo # BUG#49906: Assertion failed - Field_varstring::val_str in field.cc
395--echo #
396
397CREATE TABLE t1 (
398  f1 VARCHAR(1024),
399  f2 VARCHAR(10),
400  INDEX test_idx USING BTREE (f2,f1(5))
401);
402
403INSERT INTO t1 VALUES  ('a','c'), ('b','d');
404
405SELECT f1
406FROM t1
407WHERE f2 LIKE 'd'
408ORDER BY f1;
409
410DROP TABLE t1;
411
412--echo #
413--echo # Bug#52660 - "Perf. regr. using ICP for MyISAM on range queries on
414--echo #              an index containing TEXT"
415--echo #
416
417CREATE TABLE t1 (a INT);
418INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
419
420CREATE TABLE t2 (a INT);
421INSERT INTO t2 SELECT A.a + 10*(B.a) FROM t1 A, t1 B;
422
423CREATE TABLE t3 (
424  c1 TINYTEXT NOT NULL,
425  i1 INT NOT NULL,
426  KEY (c1(6),i1)
427);
428
429-- disable_query_log
430-- disable_result_log
431ANALYZE TABLE t1;
432ANALYZE TABLE t2;
433ANALYZE TABLE t3;
434-- enable_result_log
435-- enable_query_log
436
437INSERT INTO t3 SELECT CONCAT('c-',1000+t2.a,'=w'), 1 FROM t2;
438
439let query=SELECT c1 FROM t3 WHERE c1 >= 'c-1004=w' and c1 <= 'c-1006=w' and i1 > 2;
440
441eval EXPLAIN $query;
442eval $query;
443
444DROP TABLE t1, t2, t3;
445
446--echo #
447--echo # Bug#57372 "Multi-table updates and deletes fail when running with ICP
448--echo #            against InnoDB"
449--echo #
450
451CREATE TABLE t1 (
452  a INT KEY,
453  b INT
454) ENGINE = INNODB;
455
456CREATE TABLE t2 (
457  a INT KEY,
458  b INT
459) ENGINE = INNODB;
460
461INSERT INTO t1 VALUES (1, 101), (2, 102), (3, 103), (4, 104), (5, 105);
462INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);
463
464UPDATE t1, t2
465SET t1.a = t1.a + 100, t2.b = t1.a + 10
466WHERE t1.a BETWEEN 2 AND 4 AND t2.a = t1.b - 100;
467
468--sorted_result
469SELECT * FROM t1;
470--sorted_result
471SELECT * FROM t2;
472
473DROP TABLE t1, t2;
474
475--echo #
476--echo # Bug#52605 - "Adding LIMIT 1 clause to query with complex range
477--echo #              predicate causes wrong results"
478--echo #
479
480CREATE TABLE t1 (
481  pk INT NOT NULL,
482  c1 INT,
483  PRIMARY KEY (pk),
484  KEY k1 (c1)
485);
486
487INSERT INTO t1 VALUES (1,NULL);
488INSERT INTO t1 VALUES (2,6);
489INSERT INTO t1 VALUES (3,NULL);
490INSERT INTO t1 VALUES (4,6);
491INSERT INTO t1 VALUES (5,NULL);
492INSERT INTO t1 VALUES (6,NULL);
493INSERT INTO t1 VALUES (7,9);
494INSERT INTO t1 VALUES (8,0);
495
496-- disable_query_log
497-- disable_result_log
498ANALYZE TABLE t1;
499-- enable_result_log
500-- enable_query_log
501
502SELECT pk, c1
503FROM t1
504WHERE (pk BETWEEN 4 AND 5 OR pk < 2) AND c1 < 240
505ORDER BY c1
506LIMIT 1;
507
508EXPLAIN SELECT pk, c1
509FROM t1
510WHERE (pk BETWEEN 4 AND 5 OR pk < 2) AND c1 < 240
511ORDER BY c1
512LIMIT 1;
513
514DROP TABLE t1;
515
516--echo #
517--echo # Bug#42991 "invalid memory access and/or crash when using
518--echo #            index condition pushdown + InnoDB"
519--echo #
520
521# Note that you need to run with --valgrind to see the warnings
522# about invalid memory accesses.
523
524CREATE TABLE t1 (
525  c1 TINYTEXT NOT NULL,
526  c2 INT NOT NULL,
527  PRIMARY KEY (c2),
528  KEY id1 (c1(4))
529);
530
531INSERT INTO t1 VALUES ('Anastasia', 5);
532INSERT INTO t1 VALUES ('Karianne', 4);
533
534-- disable_query_log
535-- disable_result_log
536ANALYZE TABLE t1;
537-- enable_result_log
538-- enable_query_log
539
540SELECT * FROM t1 WHERE (c1 <= '6566-06-15' AND c2 <> 3);
541
542EXPLAIN SELECT * FROM t1 WHERE (c1 <= '6566-06-15' AND c2 <> 3);
543
544DROP TABLE t1;
545
546--echo #
547--echo # Bug#56529 - "Crash due to long semaphore wait in InnoDB
548--echo #              with ICP and subqueries"
549--echo #
550
551CREATE TABLE t1 (
552  col_int_nokey INTEGER,
553  col_int_key INTEGER,
554  col_varchar_key VARCHAR(1),
555
556  KEY (col_int_key),
557  KEY (col_varchar_key, col_int_key)
558) stats_persistent=0;
559
560INSERT INTO t1 VALUES (NULL,2,'w');
561INSERT INTO t1 VALUES (7,9,'m');
562INSERT INTO t1 VALUES (9,3,'m');
563INSERT INTO t1 VALUES (7,9,'k');
564INSERT INTO t1 VALUES (4,NULL,'r');
565INSERT INTO t1 VALUES (2,9,'t');
566INSERT INTO t1 VALUES (6,3,'j');
567INSERT INTO t1 VALUES (8,8,'u');
568INSERT INTO t1 VALUES (NULL,8,'h');
569INSERT INTO t1 VALUES (5,53,'o');
570INSERT INTO t1 VALUES (NULL,0,NULL);
571INSERT INTO t1 VALUES (6,5,'k');
572INSERT INTO t1 VALUES (188,166,'e');
573INSERT INTO t1 VALUES (2,3,'n');
574INSERT INTO t1 VALUES (1,0,'t');
575INSERT INTO t1 VALUES (1,1,'c');
576INSERT INTO t1 VALUES (0,9,'m');
577INSERT INTO t1 VALUES (9,5,'y');
578INSERT INTO t1 VALUES (NULL,6,'f');
579
580CREATE TABLE t2 (
581  c1 INTEGER NOT NULL
582) stats_persistent=0;
583
584let query=
585SELECT table1.col_int_nokey
586FROM t1 AS table1 STRAIGHT_JOIN (
587                  t1 AS table2 INNER JOIN t1 AS table3
588                  ON table3.col_varchar_key = table2.col_varchar_key)
589     ON table3.col_int_nokey = table1.col_int_key
590WHERE table3.col_int_key != SOME ( SELECT c1 FROM t2 );
591
592eval EXPLAIN $query;
593eval $query;
594
595DROP TABLE t1, t2;
596
597--echo #
598--echo # Bug#58243 "RQG test optimizer_subquery causes server crash
599--echo #            when running with ICP"
600--echo #
601
602# Test case 1: This test case makes item->const_item() return true
603#              in uses_index_fields_only() for an item tree
604#              containing a subquery. This triggered the subquery
605#              to be pushed down to InnoDB.
606
607CREATE TABLE t1 (
608  pk INTEGER NOT NULL,
609  c1 INTEGER NOT NULL,
610  c2 INTEGER NOT NULL,
611
612  PRIMARY KEY (pk)
613);
614
615INSERT INTO t1 VALUES (1,6,7);
616
617CREATE TABLE t2 (
618  c1 INTEGER NOT NULL
619);
620
621-- disable_query_log
622-- disable_result_log
623ANALYZE TABLE t1;
624-- enable_result_log
625-- enable_query_log
626
627let query=
628SELECT t1.c1
629FROM t1
630WHERE t1.pk < 317 AND 2 IN (SELECT COUNT(t2.c1)
631                            FROM t2)
632ORDER BY t1.c2;
633
634eval EXPLAIN $query;
635eval $query;
636
637DROP TABLE t1, t2;
638
639# Test case 2: This test case makes item->used_tables() return 0
640#              in uses_index_fields_only() for an item tree
641#              containg a subquery. This triggered the subquery
642#              to be pushed down to InnoDB.
643
644CREATE TABLE t1 (
645  i1 INTEGER NOT NULL,
646  c1 VARCHAR(1) NOT NULL
647);
648
649INSERT INTO t1 VALUES (2,'w');
650
651CREATE TABLE t2 (
652  i1 INTEGER NOT NULL,
653  c1 VARCHAR(1) NOT NULL,
654  c2 VARCHAR(1) NOT NULL,
655  KEY (c1, i1)
656);
657
658INSERT INTO t2 VALUES (8,'d','d');
659INSERT INTO t2 VALUES (4,'v','v');
660
661CREATE TABLE t3 (
662  c1 VARCHAR(1) NOT NULL
663);
664
665INSERT INTO t3 VALUES ('v');
666
667-- disable_query_log
668-- disable_result_log
669ANALYZE TABLE t1;
670ANALYZE TABLE t2;
671ANALYZE TABLE t3;
672-- enable_result_log
673-- enable_query_log
674
675let query=
676SELECT i1
677FROM t1
678WHERE EXISTS (SELECT t2.c1
679              FROM (t2 INNER JOIN t3 ON (t3.c1 = t2.c1))
680              WHERE t2.c2 != t1.c1 AND t2.c2 = (SELECT MIN(t3.c1)
681                                                FROM t3));
682
683eval EXPLAIN $query;
684eval $query;
685
686DROP TABLE t1,t2,t3;
687
688--echo #
689--echo # Bug#58015 "Assert in row_sel_field_store_in_mysql_format
690--echo #            when running innodb_mrr_icp test"
691--echo #
692
693create table t1 (a char(2) charset utf8,b double, primary key (a(1)),key (b));
694insert into t1 values ('',1);
695select 1 from t1 where b <= 1 and a <> '';
696drop table t1;
697
698--echo #
699--echo # Bug#59259 "Incorrect rows returned for a correlated subquery
700--echo #            when ICP is on"
701--echo #
702
703CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL) ENGINE=InnoDB;
704
705INSERT INTO t1 VALUES (11,0);
706INSERT INTO t1 VALUES (12,5);
707INSERT INTO t1 VALUES (15,0);
708
709CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL) ENGINE=InnoDB;
710
711INSERT INTO t2 VALUES (11,1);
712INSERT INTO t2 VALUES (12,2);
713INSERT INTO t2 VALUES (15,4);
714
715SELECT * FROM t1
716WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON it.i=it.i WHERE t1.i);
717
718DROP TABLE t1, t2;
719
720--echo #
721--echo # Bug #58816 "Extra temporary duplicate rows in result set when
722--echo #             switching ICP off"
723--echo #
724
725# Save optimizer switch setting
726set @save_optimizer_switch_bug58816= @@optimizer_switch;
727
728CREATE TABLE t1 (
729  pk INT NOT NULL,
730  c1 INT NOT NULL,
731  PRIMARY KEY (pk)
732);
733
734INSERT INTO t1 VALUES (1,9),(2,7),(3,6),(4,3),(5,1);
735
736-- disable_query_log
737-- disable_result_log
738ANALYZE TABLE t1;
739-- enable_result_log
740-- enable_query_log
741
742EXPLAIN SELECT pk, c1 FROM t1 WHERE pk <> 3;
743
744SET SESSION optimizer_switch='index_condition_pushdown=off';
745
746SELECT pk, c1 FROM t1 WHERE pk <> 3;
747
748DROP TABLE t1;
749
750# Restore optimzer switch setting
751set optimizer_switch= @save_optimizer_switch_bug58816;
752
753--echo #
754--echo # Bug#58837: ICP crash or valgrind error due to uninitialized
755--echo #            value in innobase_index_cond
756--echo #
757CREATE TABLE t1 (
758  t1_int INT,
759  t1_time TIME
760);
761
762CREATE TABLE t2 (
763  t2_int int PRIMARY KEY,
764  t2_int2 INT
765);
766
767--disable_warnings
768INSERT IGNORE INTO t2 VALUES ();
769INSERT INTO t1 VALUES ();
770--enable_warnings
771
772-- disable_query_log
773-- disable_result_log
774ANALYZE TABLE t1;
775ANALYZE TABLE t2;
776-- enable_result_log
777-- enable_query_log
778
779let $query=
780SELECT *
781FROM t1 AS t1a
782WHERE NOT EXISTS
783  (SELECT *
784   FROM t1 AS t1b
785   WHERE t1b.t1_int NOT IN
786     (SELECT t2.t2_int
787      FROM t2
788      WHERE t1b.t1_time LIKE t1b.t1_int
789      OR t1b.t1_time <> t2.t2_int2
790      AND 6=7
791 )
792)
793;
794
795--echo
796--eval $query;
797--echo
798--eval EXPLAIN $query;
799--echo
800
801DROP TABLE t1,t2;
802--echo #
803--echo # Bug#59186 Wrong results of join when ICP is enabled
804--echo #
805
806CREATE TABLE t1 (
807  pk INTEGER NOT NULL,
808  c1 VARCHAR(3) NOT NULL,
809  PRIMARY KEY (pk)
810);
811
812INSERT INTO t1 VALUES (1,'y'),(0,'or');
813
814CREATE TABLE t2 (
815  pk INTEGER NOT NULL,
816  c1 VARCHAR(3) NOT NULL,
817  c2 VARCHAR(6) NOT NULL,
818  PRIMARY KEY (pk)
819);
820
821INSERT INTO t2 VALUES (6,'y','RPOYT'),(10,'m','JINQE');
822
823-- disable_query_log
824-- disable_result_log
825ANALYZE TABLE t1;
826ANALYZE TABLE t2;
827-- enable_result_log
828-- enable_query_log
829
830let query=
831SELECT c2 FROM t1 JOIN t2 ON t1.c1 = t2.c1
832WHERE (t2.pk <= 4 AND t1.pk IN (2,1)) OR
833      (t1.pk > 1 AND t2.pk BETWEEN 6 AND 6);
834
835eval EXPLAIN $query;
836eval $query;
837
838DROP TABLE t1, t2;
839
840--echo #
841--echo # Bug#58838 "Wrong results with HAVING + LIMIT without GROUP BY when
842--echo #            ICP is enabled"
843--echo #
844
845CREATE TABLE t1 (
846  pk INT NOT NULL,
847  c1 INT,
848  PRIMARY KEY (pk),
849  KEY col_int_key (c1)
850);
851
852INSERT INTO t1 VALUES (1,37),(2,8),(3,-25),(4,NULL),(5,55);
853
854SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 0;
855SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 1;
856SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 2;
857SELECT pk FROM t1 WHERE c1 <> 1 HAVING pk = 3 ORDER BY pk LIMIT 5;
858
859DROP TABLE t1;
860
861--echo #
862--echo # Bug#59483 "Crash on INSERT/REPLACE in
863--echo #            rec_convert_dtuple_to_rec_comp with ICP on"
864--echo #
865
866CREATE TABLE t1 (
867 pk INTEGER AUTO_INCREMENT PRIMARY KEY,
868 i1 INTEGER,
869 c1 CHAR(6),
870 i2 INTEGER NOT NULL,
871 KEY (i2)
872);
873
874INSERT INTO t1 VALUES
875 (NULL, 4, 'that', 8),
876 (NULL, 1, 'she', 6),
877 (NULL, 6, 'tell', 2);
878
879SELECT * FROM t1 WHERE i2 IN (3, 6) LIMIT 2 FOR UPDATE;
880INSERT INTO t1 (i2) VALUES (1);
881
882DROP TABLE t1;
883
884--echo #
885--echo # Bug #11766678 - 59843:
886--echo # USING UNINITIALISED VALUE IN USES_INDEX_FIELDS_ONLY
887--echo #
888CREATE TABLE t1 (
889  col999 FLOAT NOT NULL,
890  COL1000 VARBINARY(179) NOT NULL,
891  col1003 DATE DEFAULT NULL,
892  KEY idx4267 (col1000, col1003)
893);
894
895INSERT IGNORE INTO t1 VALUES (),();
896SELECT col999 FROM t1 WHERE col1000 = "3" AND col1003 <=> sysdate();
897
898DROP TABLE t1;
899--echo #
900--echo # Bug#11873324 "WRONG RESULT WITH ICP AND STRAIGHT_JOIN"
901--echo #
902
903CREATE TABLE t1 (
904  pk INTEGER NOT NULL,
905  i1 INTEGER,
906  PRIMARY KEY (pk),
907  KEY col_int_key (i1)
908);
909
910INSERT INTO t1 VALUES (14,NULL), (18,133);
911
912CREATE TABLE t2 (
913   pk INTEGER NOT NULL,
914   i1 INTEGER,
915   c1 VARCHAR(1),
916   PRIMARY KEY (pk),
917   KEY col_int_key (i1)
918);
919
920INSERT INTO t2 VALUES (1,7,'f');
921
922if (`SELECT UPPER(@@default_storage_engine) = 'INNODB'`)
923{
924-- disable_query_log
925-- disable_result_log
926ANALYZE TABLE t1;
927ANALYZE TABLE t2;
928-- enable_result_log
929-- enable_query_log
930}
931
932# Bug was specific of IN->EXISTS:
933set @old_opt_switch=@@optimizer_switch;
934--disable_query_log
935if (`select locate('materialization', @@optimizer_switch) > 0`)
936{
937  set optimizer_switch='materialization=off';
938}
939--enable_query_log
940
941let query=
942SELECT t1.i1
943FROM t1
944WHERE t1.i1 NOT IN
945( SELECT STRAIGHT_JOIN subquery_t1.pk
946  FROM t1 AS subquery_t1
947    JOIN t2 AS subquery_t2
948    ON subquery_t2.i1 = subquery_t1.pk
949  WHERE subquery_t1.i1 > 0
950    OR subquery_t2.c1 = 'a'
951);
952
953eval EXPLAIN $query;
954eval $query;
955
956set @@optimizer_switch=@old_opt_switch;
957DROP TABLE t1,t2;
958
959--echo #
960--echo # Bug#11876420 "MISSING ROW IN RESULT WITH SUBQUERY + IN + XOR +
961--echo #               NULL VALUES AND ICP ENABLED"
962--echo #
963
964CREATE TABLE t1 (
965   i1 INTEGER,
966   c1 VARCHAR(1),
967   KEY col_varchar_key (c1)
968);
969
970INSERT INTO t1 VALUES (1,'j'), (0,'e'), (210,'f'), (8,'v'), (7,'x'),
971                      (5,'m'), (NULL,'c');
972
973CREATE TABLE t2 (
974   i1 INTEGER,
975   c1 VARCHAR(1),
976   KEY col_varchar_key (c1)
977);
978
979INSERT INTO t2 VALUES (8,NULL);
980
981CREATE TABLE t3 (
982   i1 INTEGER,
983   c1 VARCHAR(1),
984   KEY col_varchar_key (c1)
985) ENGINE=InnoDB;
986
987INSERT INTO t3 VALUES (NULL,'w'), (1,NULL), (2,'d');
988
989-- disable_query_log
990-- disable_result_log
991ANALYZE TABLE t1;
992ANALYZE TABLE t2;
993ANALYZE TABLE t3;
994-- enable_result_log
995-- enable_query_log
996
997# Bug was specific of IN->EXISTS:
998set @old_opt_switch=@@optimizer_switch;
999--disable_query_log
1000if (`select locate('materialization', @@optimizer_switch) > 0`)
1001{
1002  set optimizer_switch='materialization=off';
1003}
1004--enable_query_log
1005
1006let query=
1007SELECT i1
1008FROM t3
1009WHERE c1 IN
1010  ( SELECT t1.c1
1011    FROM t2 JOIN t1
1012      ON t2.i1 >= t1.i1
1013    WHERE t1.c1 > t2.c1
1014  )
1015  XOR i1;
1016
1017eval EXPLAIN $query;
1018eval $query;
1019
1020set @@optimizer_switch=@old_opt_switch;
1021DROP TABLE t1, t2, t3;
1022
1023--echo #
1024--echo # Bug#12355958 "FAILING ASSERTION: TRX->LOCK.N_ACTIVE_THRS == 1"
1025--echo #
1026
1027CREATE TABLE t1 (
1028  pk INTEGER PRIMARY KEY,
1029  a INTEGER NOT NULL,
1030  b CHAR(1),
1031  KEY(b)
1032);
1033
1034INSERT INTO t1 VALUES (23,5,'d');
1035
1036-- disable_query_log
1037-- disable_result_log
1038ANALYZE TABLE t1;
1039-- enable_result_log
1040-- enable_query_log
1041
1042let query=
1043SELECT a1.pk
1044FROM t1 AS a1 JOIN (SELECT * FROM t1 LIMIT 1) AS a2 ON a2.b = a1.b
1045WHERE a1.a = (SELECT pk FROM t1 LIMIT 1)
1046      AND (a1.a != a2.a OR a1.b IS NULL);
1047
1048eval EXPLAIN $query;
1049eval $query;
1050
1051# Re-run the same query using a view. This will test the code with
1052# an Item_ref object in the condition tree (note: in order for this
1053# to trigger the bug the patch for Bug#59696 needs to be applied first).
1054
1055CREATE VIEW v1 AS SELECT * FROM t1;
1056
1057let query=
1058SELECT a1.pk
1059FROM v1 AS a1 JOIN (SELECT * FROM v1 LIMIT 1) AS a2 ON a2.b = a1.b
1060WHERE a1.a = (SELECT pk FROM v1 LIMIT 1)
1061      AND (a1.a != a2.a OR a1.b IS NULL);
1062
1063eval EXPLAIN $query;
1064eval $query;
1065
1066DROP VIEW v1;
1067DROP TABLE t1;
1068
1069--echo #
1070--echo # BUG#12601961 "SEGFAULT IN HANDLER::COMPARE_KEY2"
1071--echo # BUG#12724899 "SELECT STRAIGHT_JOIN QUERY GIVES 2 DATES VERSUS
1072--echo #               2 WARNINGS WITH ICP ON"
1073--echo #
1074
1075CREATE TABLE t1 (
1076  pk INTEGER NOT NULL,
1077  i1 INTEGER NOT NULL,
1078  c1 VARCHAR(1) NOT NULL,
1079  PRIMARY KEY (pk)
1080);
1081
1082INSERT INTO t1 VALUES (1,3,'j'), (20,8,'e');
1083
1084-- disable_query_log
1085-- disable_result_log
1086ANALYZE TABLE t1;
1087-- enable_result_log
1088-- enable_query_log
1089
1090let query=
1091SELECT alias2.i1
1092FROM t1 AS alias1 STRAIGHT_JOIN t1 AS alias2
1093ON alias2.pk AND alias2.pk <= alias1.c1
1094WHERE alias2.pk = 1;
1095
1096eval EXPLAIN $query;
1097eval $query;
1098
1099DROP TABLE t1;
1100
1101--echo #
1102--echo # BUG#12822678 - 2 MORE ROWS WHEN ICP=ON W/ STRAIGHT_JOIN
1103--echo #
1104
1105CREATE TABLE t1 (
1106  i1 INTEGER NOT NULL,
1107  d1 DOUBLE,
1108  KEY k1 (d1)
1109);
1110
1111INSERT INTO t1 VALUES (10,1), (17,NULL), (22,NULL);
1112
1113CREATE TABLE t2 (
1114  pk INTEGER NOT NULL,
1115  i1 INTEGER NOT NULL,
1116  PRIMARY KEY (pk)
1117);
1118
1119INSERT INTO t2 VALUES (4,1);
1120
1121-- disable_query_log
1122-- disable_result_log
1123ANALYZE TABLE t1;
1124ANALYZE TABLE t2;
1125-- enable_result_log
1126-- enable_query_log
1127
1128let query=
1129SELECT t1.d1, t2.pk, t2.i1
1130FROM t1 STRAIGHT_JOIN t2 ON t2.i1
1131WHERE t2.pk <> t1.d1 AND t2.pk = 4;
1132
1133eval EXPLAIN $query;
1134eval $query;
1135
1136DROP TABLE t1, t2;
1137
1138--echo #
1139--echo # BUG#12838420 "DUPLICATE VALUES FOR GROUP-BY COLUMN WHEN JOIN
1140--echo # BUFFERING IS OFF"
1141--echo #
1142CREATE TABLE t1 (
1143  col_int_key INT,
1144  pk INT,
1145  PRIMARY KEY (pk),
1146  KEY (col_int_key)
1147);
1148INSERT INTO t1 VALUES (2,3),(3,2),(3,5),(4,6);
1149
1150CREATE TABLE t2 (
1151  col_int_key INT,
1152  pk INT,
1153  PRIMARY KEY (pk),
1154  KEY (col_int_key)
1155);
1156INSERT INTO t2 VALUES (0,9),(3,10),(4,6),(6,1),(100,3),(200,5);
1157
1158-- disable_query_log
1159-- disable_result_log
1160ANALYZE TABLE t1;
1161ANALYZE TABLE t2;
1162-- enable_result_log
1163-- enable_query_log
1164
1165let $query=
1166SELECT t2.col_int_key AS field1
1167FROM t2 USE INDEX(col_int_key) STRAIGHT_JOIN t1 ON t2.col_int_key
1168WHERE t2.pk < 7 AND t2.col_int_key <> 7
1169GROUP BY field1;
1170
1171set @old_opt_switch=@@optimizer_switch;
1172# With BNL, a temp table is used for GROUP BY, we don't want this
1173SET optimizer_switch="block_nested_loop=off";
1174eval EXPLAIN $query;
1175eval $query;
1176
1177SET @@optimizer_switch=@old_opt_switch;
1178DROP TABLE t1,t2;
1179
1180--echo #
1181--echo # Bug#12976163 "CRASH IN INDEX CONDITION PUSHDOWN CODE AGAINST
1182--echo #               A MYISAM TABLE"
1183--echo #
1184
1185CREATE TABLE t1 (
1186  i1 INTEGER NOT NULL,
1187  i2 INTEGER NOT NULL
1188);
1189
1190INSERT INTO t1 VALUES (14,1), (15,2), (16,3);
1191
1192CREATE TABLE t2 (
1193  i1 INTEGER NOT NULL,
1194  i2 INTEGER NOT NULL,
1195  c1 TINYTEXT
1196);
1197
1198INSERT INTO t2
1199SELECT i1, 10 * i2, "MySQL" FROM t1;
1200
1201DELIMITER $$;
1202
1203CREATE PROCEDURE proc1(id INTEGER)
1204BEGIN
1205
1206SELECT i2
1207  FROM (
1208       (SELECT i1, i2, NULL AS a1 FROM t1)
1209       UNION
1210       (SELECT i1, i2, c1 AS a1 FROM t2)
1211       ) u1
1212WHERE i1 = id;
1213
1214END$$
1215
1216DELIMITER ;$$
1217
1218CALL proc1(15);
1219
1220DROP PROCEDURE proc1;
1221
1222DROP TABLE t1, t2;
1223
1224--echo #
1225--echo # Bug#13655397 "CRASH IN SYNC_THREAD_LEVELS_NONEMPTY_TRX"
1226--echo #
1227
1228CREATE TABLE t1 (
1229  i1 INTEGER NOT NULL,
1230  i2 INTEGER NOT NULL,
1231  KEY (i1)
1232);
1233
1234INSERT INTO t1 VALUES (4,4), (5,5);
1235
1236CREATE TABLE t2 (
1237  pk INTEGER NOT NULL,
1238  PRIMARY KEY (pk)
1239);
1240
1241INSERT INTO t2 VALUES (1);
1242
1243-- disable_query_log
1244-- disable_result_log
1245ANALYZE TABLE t1;
1246ANALYZE TABLE t2;
1247-- enable_result_log
1248-- enable_query_log
1249
1250CREATE FUNCTION f1() RETURNS INTEGER
1251RETURN (SELECT MOD(COUNT(DISTINCT pk), 10) FROM t2);
1252
1253let query=
1254SELECT i1, i2 FROM t1 WHERE f1() = 1 AND i1 = 5;
1255
1256eval EXPLAIN $query;
1257eval $query;
1258
1259DROP FUNCTION f1;
1260DROP TABLE t1, t2;
1261