1# t/innodb_mysql.test
2#
3# Last update:
4# 2006-07-26 ML test refactored (MySQL 5.1)
5#               main testing code t/innodb_mysql.test -> include/mix1.inc
6#
7
8#Want to skip this test from daily Valgrind execution.
9--source include/no_valgrind_without_big.inc
10# Adding big test option for this test.
11--source include/big_test.inc
12
13-- source include/have_innodb.inc
14let $engine_type= InnoDB;
15let $other_engine_type= MEMORY;
16# InnoDB does support FOREIGN KEYs
17let $test_foreign_keys= 1;
18set global innodb_large_prefix=off;
19--source include/mix1.inc
20set global innodb_large_prefix=default;
21
22--disable_warnings
23drop table if exists t1, t2, t3;
24--enable_warnings
25--echo #
26--echo # BUG#35850: Performance regression in 5.1.23/5.1.24
27--echo #
28create table t1(a int);
29insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
30create table t2 (a int, b int, pk int, key(a,b), primary key(pk)) engine=innodb;
31insert into t2 select @a:=A.a+10*(B.a + 10*C.a),@a, @a from t1 A, t1 B, t1 C;
32--echo # this must use key 'a', not PRIMARY:
33--replace_column 10 #
34explain select a from t2 where a=b;
35drop table t1, t2;
36
37--echo #
38--echo # Bug #40360: Binlog related errors with binlog off
39--echo #
40# This bug is triggered when the binlog format is STATEMENT and the
41# binary log is turned off. In this case, no error should be shown for
42# the statement since there are no replication issues.
43
44SET SESSION BINLOG_FORMAT=STATEMENT;
45SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
46query_vertical select @@session.sql_log_bin, @@session.binlog_format, @@session.tx_isolation;
47CREATE TABLE t1 ( a INT ) ENGINE=InnoDB;
48INSERT INTO t1 VALUES(1);
49DROP TABLE t1;
50
51--echo #
52--echo # Bug#37284 Crash in Field_string::type()
53--echo #
54--disable_warnings
55DROP TABLE IF EXISTS t1;
56--enable_warnings
57CREATE TABLE t1 (a char(50)) ENGINE=InnoDB;
58CREATE INDEX i1 on t1 (a(3));
59SELECT * FROM t1 WHERE a = 'abcde';
60DROP TABLE t1;
61
62--echo #
63--echo # Bug #37742: HA_EXTRA_KEYREAD flag is set when key contains only prefix of
64--echo # requested column
65--echo #
66
67CREATE TABLE foo (a int, b int, c char(10),
68                  PRIMARY KEY (c(3)),
69                  KEY b (b)
70) engine=innodb;
71
72CREATE TABLE foo2 (a int, b int, c char(10),
73                  PRIMARY KEY (c),
74                  KEY b (b)
75) engine=innodb;
76
77CREATE TABLE bar (a int, b int, c char(10),
78                  PRIMARY KEY (c(3)),
79                  KEY b (b)
80) engine=myisam;
81
82INSERT INTO foo VALUES
83   (1,2,'abcdefghij'), (2,3,''), (3,4,'klmnopqrst'),
84   (4,5,'uvwxyz'), (5,6,'meotnsyglt'), (4,5,'asfdewe');
85
86INSERT INTO bar SELECT * FROM foo;
87INSERT INTO foo2 SELECT * FROM foo;
88
89-- disable_result_log
90ANALYZE TABLE bar;
91ANALYZE TABLE foo;
92ANALYZE TABLE foo2;
93-- enable_result_log
94
95--query_vertical EXPLAIN SELECT c FROM bar WHERE b>2;
96--query_vertical EXPLAIN SELECT c FROM foo WHERE b>2;
97--query_vertical EXPLAIN SELECT c FROM foo2 WHERE b>2;
98
99--query_vertical EXPLAIN SELECT c FROM bar WHERE c>2;
100--query_vertical EXPLAIN SELECT c FROM foo WHERE c>2;
101--query_vertical EXPLAIN SELECT c FROM foo2 WHERE c>2;
102
103DROP TABLE foo, bar, foo2;
104
105
106--echo #
107--echo # Bug#41348: INSERT INTO tbl SELECT * FROM temp_tbl overwrites locking type of temp table
108--echo #
109
110--disable_warnings
111DROP TABLE IF EXISTS t1,t3,t2;
112DROP FUNCTION IF EXISTS f1;
113--enable_warnings
114
115DELIMITER |;
116CREATE FUNCTION f1() RETURNS VARCHAR(250)
117 BEGIN
118     return 'hhhhhhh' ;
119 END|
120DELIMITER ;|
121
122CREATE TABLE t1 (a VARCHAR(20), b VARCHAR(20), c VARCHAR(20)) ENGINE=INNODB;
123
124BEGIN WORK;
125
126CREATE TEMPORARY TABLE t2 (a VARCHAR(20), b VARCHAR(20), c varchar(20)) ENGINE=INNODB;
127CREATE TEMPORARY TABLE t3 LIKE t2;
128
129INSERT INTO t1 VALUES ('a','b',NULL),('c','d',NULL),('e','f',NULL);
130
131SET @stmt := CONCAT('INSERT INTO t2 SELECT tbl.a, tbl.b, f1()',' FROM t1 tbl');
132PREPARE stmt1 FROM @stmt;
133
134SET @stmt := CONCAT('INSERT INTO t3', ' SELECT * FROM t2');
135PREPARE stmt3 FROM @stmt;
136
137EXECUTE stmt1;
138
139COMMIT;
140
141DEALLOCATE PREPARE stmt1;
142DEALLOCATE PREPARE stmt3;
143
144DROP TABLE t1,t3,t2;
145DROP FUNCTION f1;
146
147--echo #
148--echo # Bug#37016: TRUNCATE TABLE removes some rows but not all
149--echo #
150
151--disable_warnings
152DROP TABLE IF EXISTS t1,t2;
153--enable_warnings
154
155CREATE TABLE t1 (id INT NOT NULL, PRIMARY KEY (id)) ENGINE=INNODB;
156CREATE TABLE t2 (id INT PRIMARY KEY,
157                 t1_id INT, INDEX par_ind (t1_id),
158                 FOREIGN KEY (t1_id) REFERENCES t1(id)) ENGINE=INNODB;
159INSERT INTO t1 VALUES (1),(2);
160INSERT INTO t2 VALUES (3,2);
161
162SET AUTOCOMMIT = 0;
163
164START TRANSACTION;
165--error ER_TRUNCATE_ILLEGAL_FK
166TRUNCATE TABLE t1;
167SELECT * FROM t1;
168COMMIT;
169SELECT * FROM t1;
170
171START TRANSACTION;
172--error ER_TRUNCATE_ILLEGAL_FK
173TRUNCATE TABLE t1;
174SELECT * FROM t1;
175ROLLBACK;
176SELECT * FROM t1;
177
178SET AUTOCOMMIT = 1;
179
180START TRANSACTION;
181SELECT * FROM t1;
182COMMIT;
183
184--error ER_TRUNCATE_ILLEGAL_FK
185TRUNCATE TABLE t1;
186SELECT * FROM t1;
187DELETE FROM t2 WHERE id = 3;
188
189START TRANSACTION;
190SELECT * FROM t1;
191--error ER_TRUNCATE_ILLEGAL_FK
192TRUNCATE TABLE t1;
193ROLLBACK;
194SELECT * FROM t1;
195TRUNCATE TABLE t2;
196
197DROP TABLE t2;
198DROP TABLE t1;
199
200--echo #
201--echo # Bug#40127 Multiple table DELETE IGNORE hangs on foreign key constraint violation on 5.0
202--echo #
203CREATE TABLE t1 (
204        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
205        PRIMARY KEY (id)
206) ENGINE=InnoDB;
207
208CREATE TABLE t2 (
209        id INT UNSIGNED NOT NULL AUTO_INCREMENT,
210        aid INT UNSIGNED NOT NULL,
211        PRIMARY KEY (id),
212        FOREIGN KEY (aid) REFERENCES t1 (id)
213) ENGINE=InnoDB;
214
215CREATE TABLE t3 (
216        bid INT UNSIGNED NOT NULL,
217        FOREIGN KEY (bid) REFERENCES t2 (id)
218) ENGINE=InnoDB;
219
220CREATE TABLE t4 (
221  a INT
222) ENGINE=InnoDB;
223
224CREATE TABLE t5 (
225  a INT
226) ENGINE=InnoDB;
227
228INSERT INTO t1 (id) VALUES (1);
229INSERT INTO t2 (id, aid) VALUES (1, 1),(2,1),(3,1),(4,1);
230INSERT INTO t3 (bid) VALUES (1);
231
232INSERT INTO t4 VALUES (1),(2),(3),(4),(5);
233INSERT INTO t5 VALUES (1);
234
235DELETE t5 FROM t4 LEFT JOIN t5 ON t4.a= t5.a;
236
237--error ER_ROW_IS_REFERENCED_2
238DELETE t2, t1 FROM t2 INNER JOIN t1 ON (t2.aid = t1.id) WHERE t2.id = 1;
239--error ER_ROW_IS_REFERENCED_2
240DELETE t2, t1 FROM t2 INNER JOIN t1 ON (t2.aid = t1.id) WHERE t2.id = 1;
241
242DELETE IGNORE t2, t1 FROM t2 INNER JOIN t1 ON (t2.aid = t1.id) WHERE t2.id = 1;
243
244DROP TABLE t3;
245DROP TABLE t2;
246DROP TABLE t1;
247DROP TABLES t4,t5;
248
249--echo # Bug#40127 Multiple table DELETE IGNORE hangs on foreign key constraint violation on 5.0
250--echo # Testing for any side effects of IGNORE on AFTER DELETE triggers used with
251--echo # transactional tables.
252--echo #
253CREATE TABLE t1 (i INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
254CREATE TABLE t2 (a VARCHAR(100)) ENGINE=InnoDB;
255CREATE TABLE t3 (i INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
256CREATE TABLE t4 (i INT NOT NULL PRIMARY KEY, t1i INT,
257  FOREIGN KEY (t1i) REFERENCES t1(i))
258  ENGINE=InnoDB;
259delimiter ||;
260CREATE TRIGGER trg AFTER DELETE ON t1 FOR EACH ROW
261BEGIN
262  SET @b:='EXECUTED TRIGGER';
263  INSERT INTO t2 VALUES (@b);
264  SET @a:= error_happens_here;
265END||
266delimiter ;||
267
268SET @b:="";
269SET @a:="";
270INSERT INTO t1 VALUES (1),(2),(3),(4);
271INSERT INTO t3 SELECT * FROM t1;
272--echo ** An error in a trigger causes rollback of the statement.
273--error ER_BAD_FIELD_ERROR
274DELETE t1 FROM t3 LEFT JOIN t1 ON t1.i=t3.i;
275SELECT @a,@b;
276SELECT * FROM t2;
277SELECT * FROM t1 LEFT JOIN t3 ON t1.i=t3.i;
278
279--echo ** Same happens with the IGNORE option
280--error ER_BAD_FIELD_ERROR
281DELETE IGNORE t1 FROM t3 LEFT JOIN t1 ON t1.i=t3.i;
282SELECT * FROM t2;
283SELECT * FROM t1 LEFT JOIN t3 ON t1.i=t3.i;
284
285--echo **
286--echo ** The following is an attempt to demonstrate
287--echo ** error handling inside a row iteration.
288--echo **
289DROP TRIGGER trg;
290DELETE FROM t1;
291DELETE FROM t2;
292DELETE FROM t3;
293
294INSERT INTO t1 VALUES (1),(2),(3),(4);
295INSERT INTO t3 VALUES (1),(2),(3),(4);
296INSERT INTO t4 VALUES (3,3),(4,4);
297
298delimiter ||;
299CREATE TRIGGER trg AFTER DELETE ON t1 FOR EACH ROW
300BEGIN
301  SET @b:= CONCAT('EXECUTED TRIGGER FOR ROW ',CAST(OLD.i AS CHAR));
302  INSERT INTO t2 VALUES (@b);
303END||
304delimiter ;||
305
306--echo ** DELETE is prevented by foreign key constrains but errors are silenced.
307--echo ** The AFTER trigger isn't fired.
308DELETE IGNORE t1 FROM t3 LEFT JOIN t1 ON t1.i=t3.i;
309--echo ** Tables are modified by best effort:
310SELECT * FROM t1 LEFT JOIN t3 ON t1.i=t3.i;
311--echo ** The AFTER trigger was only executed on successful rows:
312SELECT * FROM t2;
313
314DROP TRIGGER trg;
315
316--echo **
317--echo ** Induce an error midway through an AFTER-trigger
318--echo **
319DELETE FROM t4;
320DELETE FROM t1;
321DELETE FROM t3;
322INSERT INTO t1 VALUES (1),(2),(3),(4);
323INSERT INTO t3 VALUES (1),(2),(3),(4);
324delimiter ||;
325CREATE TRIGGER trg AFTER DELETE ON t1 FOR EACH ROW
326BEGIN
327  SET @a:= @a+1;
328  IF @a > 2 THEN
329    INSERT INTO t4 VALUES (5,5);
330  END IF;
331END||
332delimiter ;||
333
334SET @a:=0;
335--echo ** Errors in the trigger causes the statement to abort.
336--error ER_NO_REFERENCED_ROW_2
337DELETE IGNORE t1 FROM t3 LEFT JOIN t1 ON t1.i=t3.i;
338SELECT * FROM t1 LEFT JOIN t3 ON t1.i=t3.i;
339SELECT * FROM t4;
340
341DROP TRIGGER trg;
342DROP TABLE t4;
343DROP TABLE t1;
344DROP TABLE t2;
345DROP TABLE t3;
346
347--echo #
348--echo # Bug#43580: Issue with Innodb on multi-table update
349--echo #
350CREATE TABLE t1 (a INT, b INT, KEY (a)) ENGINE = INNODB;
351CREATE TABLE t2 (a INT KEY, b INT, KEY (b)) ENGINE = INNODB;
352
353CREATE TABLE t3 (a INT, b INT KEY, KEY (a)) ENGINE = INNODB;
354CREATE TABLE t4 (a INT KEY, b INT, KEY (b)) ENGINE = INNODB;
355
356INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6);
357INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);
358
359INSERT INTO t3 VALUES (1, 101), (2, 102), (3, 103), (4, 104), (5, 105), (6, 106);
360INSERT INTO t4 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);
361
362# Because t1.a changes and t2.b changes based on t1.a, the result
363# depends on join order, so STRAIGHT_JOIN is used to have it repeatable.
364UPDATE t2 straight_join t1 SET t1.a = t1.a + 100, t2.b = t1.a + 10
365WHERE t1.a BETWEEN 2 AND 4 AND t2.a = t1.b;
366--sorted_result
367SELECT * FROM t2;
368
369# Because t1.a changes and t2.b changes based on t1.a, the result
370# depends on join order, so STRAIGHT_JOIN is used to have it repeatable.
371UPDATE t4 straight_join t3 SET t3.a = t3.a + 100, t4.b = t3.a + 10
372WHERE t3.a BETWEEN 2 AND 4 AND t4.a = t3.b - 100;
373--sorted_result
374SELECT * FROM t4;
375
376DROP TABLE t1, t2, t3, t4;
377
378--echo #
379--echo # Bug#44886: SIGSEGV in test_if_skip_sort_order() -
380--echo #            uninitialized variable used as subscript
381--echo #
382
383CREATE TABLE t1 (a INT, b INT, c INT, d INT, PRIMARY KEY (b), KEY (a,c))
384  ENGINE=InnoDB;
385INSERT INTO t1 VALUES (1,1,1,0);
386
387CREATE TABLE t2 (a INT, b INT, e INT, KEY (e)) ENGINE=InnoDB;
388INSERT INTO t2 VALUES (1,1,2);
389
390CREATE TABLE t3 (a INT, b INT) ENGINE=MyISAM;
391INSERT INTO t3 VALUES (1, 1);
392# Legacy queries below need to turn off ONLY_FULL_GROUP_BY and STRICT mode.
393SET sql_mode='NO_ENGINE_SUBSTITUTION';
394SELECT * FROM t1, t2, t3
395  WHERE t1.a = t3.a AND (t1.b = t3.b OR t1.d) AND t2.b = t1.b AND t2.e = 2
396  GROUP BY t1.b;
397SET sql_mode=default;
398DROP TABLE t1, t2, t3;
399
400--echo #
401--echo # Bug #45828: Optimizer won't use partial primary key if another
402--echo # index can prevent filesort
403--echo #
404
405# Create the table
406CREATE TABLE `t1` (
407  c1 int NOT NULL,
408  c2 int NOT NULL,
409  c3 int NOT NULL,
410  PRIMARY KEY (c1,c2),
411  KEY  (c3)
412) ENGINE=InnoDB;
413
414# populate with data
415INSERT INTO t1 VALUES (5,2,1246276747);
416INSERT INTO t1 VALUES (2,1,1246281721);
417INSERT INTO t1 VALUES (7,3,1246281756);
418INSERT INTO t1 VALUES (4,2,1246282139);
419INSERT INTO t1 VALUES (3,1,1246282230);
420INSERT INTO t1 VALUES (1,0,1246282712);
421INSERT INTO t1 VALUES (8,3,1246282765);
422INSERT INTO t1 SELECT c1+10,c2+10,c3+10 FROM t1;
423INSERT INTO t1 SELECT c1+100,c2+100,c3+100 from t1;
424INSERT INTO t1 SELECT c1+1000,c2+1000,c3+1000 from t1;
425INSERT INTO t1 SELECT c1+10000,c2+10000,c3+10000 from t1;
426INSERT INTO t1 SELECT c1+100000,c2+100000,c3+100000 from t1;
427INSERT INTO t1 SELECT c1+1000000,c2+1000000,c3+1000000 from t1;
428ANALYZE TABLE t1;
429# query and no rows will match the c1 condition, whereas all will match c3
430SELECT * FROM t1 WHERE c1 = 99999999 AND c3 > 1 ORDER BY c3;
431
432# SHOULD use the pk.
433# index on c3 will be used instead of primary key
434EXPLAIN SELECT * FROM t1 WHERE c1 = 99999999 AND c3 > 1 ORDER BY c3;
435
436# if we force the primary key, we can see the estimate is 1
437EXPLAIN SELECT * FROM t1 FORCE INDEX (PRIMARY) WHERE c1 = 99999999 AND c3 > 1 ORDER BY c3;
438
439
440CREATE TABLE t2 (
441  c1 int NOT NULL,
442  c2 int NOT NULL,
443  c3 int NOT NULL,
444  KEY (c1,c2),
445  KEY (c3)
446) ENGINE=InnoDB;
447
448# SHOULD use the pk.
449# if we switch it from a primary key to a regular index, it works correctly as well
450explain SELECT * FROM t2 WHERE c1 = 99999999 AND c3 > 1 ORDER BY c3;
451
452DROP TABLE t1,t2;
453
454
455--echo #
456--echo # 36259: Optimizing with ORDER BY
457--echo #
458
459CREATE TABLE t1 (
460  a INT NOT NULL AUTO_INCREMENT,
461  b INT NOT NULL,
462  c INT NOT NULL,
463  d VARCHAR(5),
464  e INT NOT NULL,
465  PRIMARY KEY (a), KEY i2 (b,c,d)
466) ENGINE=InnoDB;
467
468INSERT INTO t1 (b,c,d,e) VALUES (1,1,'a',1), (2,2,'b',2);
469INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
470INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
471INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
472INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
473INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
474INSERT INTO t1 (b,c,d,e) SELECT RAND()*10000, RAND()*10000, d, e FROM t1;
475-- disable_result_log
476ANALYZE TABLE t1;
477-- enable_result_log
478EXPLAIN SELECT * FROM t1 WHERE b=1 AND c=1 ORDER BY a;
479EXPLAIN SELECT * FROM t1 FORCE INDEX(i2) WHERE b=1 and c=1 ORDER BY a;
480# With 4k pages, the 'rows' column in the output below is either 120 or 138,
481# not 128 as it is with 8k and 16k. Bug#12602606
482--replace_column 10 # 11 #
483EXPLAIN SELECT * FROM t1 FORCE INDEX(PRIMARY) WHERE b=1 AND c=1 ORDER BY a;
484
485DROP TABLE t1;
486
487--echo #
488--echo # Bug #47963: Wrong results when index is used
489--echo #
490CREATE TABLE t1(
491  a VARCHAR(5) NOT NULL,
492  b VARCHAR(5) NOT NULL,
493  c DATETIME NOT NULL,
494  KEY (c)
495) ENGINE=InnoDB;
496INSERT INTO t1 VALUES('TEST', 'TEST', '2009-10-09 00:00:00');
497SELECT * FROM t1 WHERE a = 'TEST' AND
498  c >= '2009-10-09 00:00:00' AND c <= '2009-10-09 00:00:00';
499SELECT * FROM t1 WHERE a = 'TEST' AND
500  c >= '2009-10-09 00:00:00.0' AND c <= '2009-10-09 00:00:00.0';
501SELECT * FROM t1 WHERE a = 'TEST' AND
502  c >= '2009-10-09 00:00:00.0' AND c <= '2009-10-09 00:00:00';
503SELECT * FROM t1 WHERE a = 'TEST' AND
504  c >= '2009-10-09 00:00:00' AND c <= '2009-10-09 00:00:00.0';
505SELECT * FROM t1 WHERE a = 'TEST' AND
506  c >= '2009-10-09 00:00:00.000' AND c <= '2009-10-09 00:00:00.000';
507SELECT * FROM t1 WHERE a = 'TEST' AND
508  c >= '2009-10-09 00:00:00.00' AND c <= '2009-10-09 00:00:00.001';
509SELECT * FROM t1 WHERE a = 'TEST' AND
510  c >= '2009-10-09 00:00:00.001' AND c <= '2009-10-09 00:00:00.00';
511EXPLAIN SELECT * FROM t1 WHERE a = 'TEST' AND
512  c >= '2009-10-09 00:00:00.001' AND c <= '2009-10-09 00:00:00.00';
513DROP TABLE t1;
514
515--echo #
516--echo # Bug #46175: NULL read_view and consistent read assertion
517--echo #
518
519CREATE TABLE t1(a CHAR(13),KEY(a)) ENGINE=innodb;
520CREATE TABLE t2(b DATETIME,KEY(b)) ENGINE=innodb;
521INSERT INTO t1 VALUES (),();
522INSERT INTO t2 VALUES (),();
523CREATE OR REPLACE VIEW v1 AS SELECT 1 FROM t2
524  WHERE b =(SELECT a FROM t1 LIMIT 1);
525
526CONNECT (con1, localhost, root,,);
527CONNECTION default;
528
529DELIMITER |;
530CREATE PROCEDURE p1(num INT)
531BEGIN
532  DECLARE i INT DEFAULT 0;
533  REPEAT
534    SHOW CREATE VIEW v1;
535    SET i:=i+1;
536  UNTIL i>num END REPEAT;
537END|
538DELIMITER ;|
539
540--echo # Should not crash
541--disable_query_log
542--disable_result_log
543--send CALL p1(1000)
544CONNECTION con1;
545--echo # Should not crash
546CALL p1(1000);
547
548CONNECTION default;
549--reap
550--enable_query_log
551--enable_result_log
552
553DISCONNECT con1;
554DROP PROCEDURE p1;
555DROP VIEW v1;
556DROP TABLE t1,t2;
557
558
559--echo #
560--echo # Bug #49324: more valgrind errors in test_if_skip_sort_order
561--echo #
562CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=innodb ;
563--echo # should not cause valgrind warnings
564SELECT 1 FROM t1 JOIN t1 a USING(a) GROUP BY t1.a,t1.a;
565DROP TABLE t1;
566
567--echo #
568--echo # Bug#50843: Filesort used instead of clustered index led to
569--echo #            performance degradation.
570--echo #
571create table t1(f1 int not null primary key, f2 int) engine=innodb;
572create table t2(f1 int not null, key (f1)) engine=innodb;
573insert into t1 values (1,1),(2,2),(3,3);
574insert into t2 values (1),(2),(3);
575-- disable_result_log
576analyze table t1;
577analyze table t2;
578-- enable_result_log
579explain select t1.* from t1 left join t2 using(f1) group by t1.f1;
580drop table t1,t2;
581--echo #
582
583
584--echo #
585--echo # Bug #39653: find_shortest_key in sql_select.cc does not consider
586--echo #             clustered primary keys
587--echo #
588
589CREATE TABLE t1 (a INT PRIMARY KEY, b INT, c INT, d INT, e INT, f INT,
590                 KEY (b,c)) ENGINE=INNODB;
591
592INSERT INTO t1 VALUES (1,1,1,1,1,1), (2,2,2,2,2,2), (3,3,3,3,3,3),
593                      (4,4,4,4,4,4), (5,5,5,5,5,5), (6,6,6,6,6,6),
594		      (7,7,7,7,7,7), (8,8,8,8,8,8), (9,9,9,9,9,9),
595		      (11,11,11,11,11,11);
596
597ANALYZE TABLE t1;
598
599--query_vertical EXPLAIN SELECT COUNT(*) FROM t1
600
601DROP TABLE t1;
602
603--echo #
604--echo # Bug #49838: DROP INDEX and ADD UNIQUE INDEX for same index may
605--echo #   corrupt definition at engine
606--echo #
607
608CREATE TABLE t1 (a INT NOT NULL, b INT NOT NULL, KEY k (a,b))
609  ENGINE=InnoDB;
610
611ALTER TABLE t1 DROP INDEX k, ADD UNIQUE INDEX k (a,b);
612
613--query_vertical SHOW INDEXES FROM t1;
614
615DROP TABLE t1;
616
617
618--echo #
619--echo # Bug #47453: InnoDB incorrectly changes TIMESTAMP columns when
620--echo #  JOINed during an UPDATE
621--echo #
622
623CREATE TABLE t1 (d INT) ENGINE=InnoDB;
624CREATE TABLE t2 (a INT, b INT,
625  c TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
626  ON UPDATE CURRENT_TIMESTAMP) ENGINE=InnoDB;
627
628--echo # set up our data elements
629INSERT INTO t1 (d) VALUES (1);
630INSERT INTO t2 (a,b) VALUES (1,1);
631SELECT SECOND(c) INTO @bug47453 FROM t2;
632
633SELECT SECOND(c)-@bug47453 FROM t1 JOIN t2 ON d=a;
634UPDATE t1 JOIN t2 ON d=a SET b=1 WHERE a=1;
635SELECT SECOND(c)-@bug47453 FROM t1 JOIN t2 ON d=a;
636
637SELECT SLEEP(1);
638
639UPDATE t1 JOIN t2 ON d=a SET b=1 WHERE a=1;
640
641--echo # should be 0
642SELECT SECOND(c)-@bug47453 FROM t1 JOIN t2 ON d=a;
643
644DROP TABLE t1, t2;
645
646--echo #
647--echo # Bug #53334: wrong result for outer join with impossible ON condition
648--echo # (see the same test case for MyISAM in join.test)
649--echo #
650
651CREATE TABLE t1 (id INT PRIMARY KEY);
652CREATE TABLE t2 (id INT);
653
654INSERT INTO t1 VALUES (75);
655INSERT INTO t1 VALUES (79);
656INSERT INTO t1 VALUES (78);
657INSERT INTO t1 VALUES (77);
658REPLACE INTO t1 VALUES (76);
659REPLACE INTO t1 VALUES (76);
660INSERT INTO t1 VALUES (104);
661INSERT INTO t1 VALUES (103);
662INSERT INTO t1 VALUES (102);
663INSERT INTO t1 VALUES (101);
664INSERT INTO t1 VALUES (105);
665INSERT INTO t1 VALUES (106);
666INSERT INTO t1 VALUES (107);
667
668INSERT INTO t2 VALUES (107),(75),(1000);
669
670SELECT t1.id,t2.id FROM t2 LEFT JOIN t1 ON t1.id>=74 AND t1.id<=0
671  WHERE t2.id=75 AND t1.id IS NULL;
672EXPLAIN SELECT t1.id,t2.id FROM t2 LEFT JOIN t1 ON t1.id>=74 AND t1.id<=0
673  WHERE t2.id=75 AND t1.id IS NULL;
674
675DROP TABLE t1,t2;
676
677--echo #
678--echo # Bug#38999 valgrind warnings for update statement in function compare_record()
679--echo #
680
681CREATE TABLE t1 (a INT PRIMARY KEY) ENGINE=InnoDB;
682CREATE TABLE t2 (a INT PRIMARY KEY) ENGINE=InnoDB;
683INSERT INTO t1 values (1),(2),(3),(4),(5);
684INSERT INTO t2 values (1);
685
686SELECT * FROM t1 WHERE a = 2;
687UPDATE t1,t2 SET t1.a = t1.a + 100 WHERE t1.a = 1;
688
689DROP TABLE t1,t2;
690
691--echo #
692--echo # Bug #53830: !table || (!table->read_set || bitmap_is_set(table->read_set, field_index))
693--echo #
694
695CREATE TABLE t1 (a INT, b INT, c INT, d INT,
696                 PRIMARY KEY(a,b,c), KEY(b,d))
697                 ENGINE=InnoDB;
698INSERT INTO t1 VALUES (0, 77, 1, 3);
699
700UPDATE t1 SET d = 0 WHERE b = 77 AND c = 25;
701
702DROP TABLE t1;
703
704--echo #
705--echo # Bug#50389 Using intersect does not return all rows
706--echo #
707
708CREATE TABLE t1 (
709  f1 INT(10) NOT NULL,
710  f2 INT(10),
711  f3 INT(10),
712  f4 TINYINT(4),
713  f5 VARCHAR(50),
714  PRIMARY KEY (f1),
715  KEY idx1 (f2,f5,f4),
716  KEY idx2 (f2,f4)
717) ENGINE=InnoDB;
718
719LOAD DATA INFILE '../../std_data/intersect-bug50389.tsv' INTO TABLE t1;
720
721-- disable_result_log
722ANALYZE TABLE t1;
723-- enable_result_log
724
725SELECT * FROM t1 WHERE f1 IN
726(3305028,3353871,3772880,3346860,4228206,3336022,
727 3470988,3305175,3329875,3817277,3856380,3796193,
728 3784744,4180925,4559596,3963734,3856391,4494153)
729AND f5 = 'abcdefghijklmnopwrst' AND f2 = 1221457 AND f4 = 0 ;
730
731EXPLAIN SELECT * FROM t1 WHERE f1 IN
732(3305028,3353871,3772880,3346860,4228206,3336022,
733 3470988,3305175,3329875,3817277,3856380,3796193,
734 3784744,4180925,4559596,3963734,3856391,4494153)
735AND f5 = 'abcdefghijklmnopwrst' AND f2 = 1221457 AND f4 = 0 ;
736
737DROP TABLE t1;
738
739--echo #
740--echo # Bug#51431 Wrong sort order after import of dump file
741--echo #
742
743CREATE TABLE t1 (
744  f1 INT(11) NOT NULL,
745  f2 int(11) NOT NULL,
746  f3 int(11) NOT NULL,
747  f4 tinyint(1) NOT NULL,
748  PRIMARY KEY (f1),
749  UNIQUE KEY (f2, f3),
750  KEY (f4)
751) ENGINE=InnoDB STATS_PERSISTENT=0;
752
753INSERT INTO t1 VALUES
754(1,1,991,1), (2,1,992,1), (3,1,993,1), (4,1,994,1), (5,1,995,1),
755(6,1,996,1), (7,1,997,1), (8,1,998,1), (10,1,999,1), (11,1,9910,1),
756(16,1,9911,1), (17,1,9912,1), (18,1,9913,1), (19,1,9914,1), (20,1,9915,1),
757(21,1,9916,1), (22,1,9917,1), (23,1,9918,1), (24,1,9919,1), (25,1,9920,1),
758(26,1,9921,1), (27,1,9922,1);
759
760FLUSH TABLES;
761
762SELECT * FROM t1 WHERE f2 = 1 AND f4 = TRUE
763ORDER BY f1 DESC LIMIT 5;
764EXPLAIN SELECT * FROM t1 WHERE f2 = 1 AND f4 = TRUE
765ORDER BY f1 DESC LIMIT 5;
766
767DROP TABLE t1;
768
769
770--echo #
771--echo # Bug#54117 crash in thr_multi_unlock, temporary table
772--echo #
773
774CREATE TEMPORARY TABLE t1(a INT) ENGINE = InnoDB;
775
776LOCK TABLES t1 READ;
777ALTER TABLE t1 COMMENT 'test';
778UNLOCK TABLES;
779
780DROP TABLE t1;
781
782--echo #
783--echo # Bug#55656: mysqldump can be slower after bug #39653 fix
784--echo #
785
786CREATE TABLE t1 (a INT , b INT, c INT, d INT,
787  KEY (b), PRIMARY KEY (a,b)) ENGINE=INNODB STATS_PERSISTENT=0;
788INSERT INTO t1 VALUES (1,1,1,1), (2,2,2,2), (3,3,3,3);
789--query_vertical EXPLAIN SELECT COUNT(*) FROM t1
790
791DROP INDEX b ON t1;
792CREATE INDEX b ON t1(a,b);
793--query_vertical EXPLAIN SELECT COUNT(*) FROM t1
794
795DROP INDEX b ON t1;
796CREATE INDEX b ON t1(a,b,c);
797--query_vertical EXPLAIN SELECT COUNT(*) FROM t1
798
799DROP INDEX b ON t1;
800CREATE INDEX b ON t1(a,b,c,d);
801--query_vertical EXPLAIN SELECT COUNT(*) FROM t1
802
803DROP TABLE t1;
804
805--echo #
806--echo # Bug#55826: create table .. select crashes with when KILL_BAD_DATA
807--echo #  is returned
808--echo #
809
810CREATE TABLE t1(a INT) ENGINE=innodb;
811INSERT INTO t1 VALUES (0);
812--error ER_TRUNCATED_WRONG_VALUE
813CREATE TABLE t2
814  SELECT LEAST((SELECT '' FROM t1),NOW()) FROM `t1`;
815DROP TABLE t1;
816
817--echo #
818--echo # Bug#56862 Moved to innodb_16k.test
819--echo #
820--echo #
821--echo # Test for bug #39932 "create table fails if column for FK is in different
822--echo #                      case than in corr index".
823--echo #
824--disable_warnings
825drop tables if exists t1, t2;
826--enable_warnings
827create table t1 (pk int primary key) engine=InnoDB;
828# Even although the below statement uses uppercased field names in
829# foreign key definition it still should be able to find explicitly
830# created supporting index. So it should succeed and should not
831# create any additional supporting indexes.
832create table t2 (fk int, key x (fk),
833                 constraint x foreign key (FK) references t1 (PK)) engine=InnoDB;
834show create table t2;
835drop table t2, t1;
836
837
838--echo #
839--echo # Test for bug #11762012 - "54553: INNODB ASSERTS IN HA_INNOBASE::
840--echo #                           UPDATE_ROW, TEMPORARY TABLE, TABLE LOCK".
841--echo #
842--disable_warnings
843DROP TABLE IF EXISTS t1;
844--enable_warnings
845CREATE TEMPORARY TABLE t1 (c int) ENGINE = InnoDB;
846INSERT INTO t1 VALUES (1);
847LOCK TABLES t1 READ;
848--echo # Even though temporary table was locked for READ we
849--echo # still allow writes to it to be compatible with MyISAM.
850--echo # This is possible since due to fact that temporary tables
851--echo # are specific to connection and therefore locking for them
852--echo # is irrelevant.
853UPDATE t1 SET c = 5;
854UNLOCK TABLES;
855DROP TEMPORARY TABLE t1;
856
857--echo # End of 5.1 tests
858
859
860--echo #
861--echo # Bug#49604 "6.0 processing compound WHERE clause incorrectly
862--echo #            with Innodb - extra rows"
863--echo #
864
865CREATE TABLE t1 (
866  c1 INT NOT NULL,
867  c2 INT,
868  PRIMARY KEY (c1),
869  KEY k1 (c2)
870) ENGINE=InnoDB;
871
872INSERT INTO t1 VALUES (12,1);
873INSERT INTO t1 VALUES (15,1);
874INSERT INTO t1 VALUES (16,1);
875INSERT INTO t1 VALUES (22,1);
876INSERT INTO t1 VALUES (20,2);
877
878CREATE TABLE t2 (
879  c1 INT NOT NULL,
880  c2 INT,
881  PRIMARY KEY (c1)
882) ENGINE=InnoDB;
883
884INSERT INTO t2 VALUES (1,2);
885INSERT INTO t2 VALUES (2,9);
886
887SELECT STRAIGHT_JOIN t2.c2, t1.c2, t2.c1
888FROM t1 JOIN t2 ON t1.c2 = t2.c1
889WHERE t2.c1 IN (2, 1, 6) OR t2.c1 NOT IN (1);
890
891DROP TABLE t1, t2;
892
893
894--echo #
895--echo # Bug#44613 SELECT statement inside FUNCTION takes a shared lock
896--echo #
897
898--disable_warnings
899DROP TABLE IF EXISTS t1;
900DROP FUNCTION IF EXISTS f1;
901--enable_warnings
902
903CREATE TABLE t1(x INT PRIMARY KEY, y INT) ENGINE=innodb;
904INSERT INTO t1 VALUES (1, 0), (2, 0);
905
906CREATE FUNCTION f1(z INT) RETURNS INT READS SQL DATA
907  RETURN (SELECT x FROM t1 WHERE x = z);
908
909--echo # Connection default
910START TRANSACTION;
911SELECT f1(1);
912
913--echo # Connection con2
914--disable_query_log
915connect (con2, localhost, root);
916--enable_query_log
917START TRANSACTION;
918SELECT f1(1);
919# This next statement used to block.
920UPDATE t1 SET y = 1 WHERE x = 1;
921
922COMMIT;
923
924disconnect con2;
925--source include/wait_until_disconnected.inc
926--echo # Connection default
927connection default;
928COMMIT;
929DROP TABLE t1;
930DROP FUNCTION f1;
931--echo #
932--echo # Bug#42744: Crash when using a join buffer to join a table with a blob
933--echo # column and an additional column used for duplicate elimination.
934--echo #
935
936CREATE TABLE t1 (a tinyblob) ENGINE=InnoDB;
937CREATE TABLE t2 (a int PRIMARY KEY,  b tinyblob) ENGINE=InnoDB;
938INSERT INTO t1 VALUES ('1'), (NULL);
939INSERT INTO t2 VALUES (1, '1');
940
941EXPLAIN
942SELECT t2.b FROM t1,t2 WHERE t1.a IN (SELECT 1 FROM t2);
943
944SELECT t2.b FROM t1,t2 WHERE t1.a IN (SELECT 1 FROM t2);
945
946DROP TABLE t1,t2;
947
948--echo #
949--echo # Bug#48093: 6.0 Server not processing equivalent IN clauses properly
950--echo #            with Innodb tables
951--echo #
952
953CREATE TABLE t1 (
954  i int(11) DEFAULT NULL,
955  v1 varchar(1) DEFAULT NULL,
956  v2 varchar(20) DEFAULT NULL,
957  KEY i (i),
958  KEY v (v1,i)
959) ENGINE=innodb;
960
961INSERT INTO t1 VALUES (1,'f','no');
962INSERT INTO t1 VALUES (2,'u','yes-u');
963INSERT INTO t1 VALUES (2,'h','yes-h');
964INSERT INTO t1 VALUES (3,'d','no');
965
966--echo
967SELECT v2
968FROM t1
969WHERE v1  IN  ('f', 'd', 'h', 'u' ) AND i  =  2;
970
971--echo
972--echo # Should not use index_merge
973EXPLAIN
974SELECT v2
975FROM t1
976WHERE v1  IN  ('f', 'd', 'h', 'u' ) AND i  =  2;
977
978DROP TABLE t1;
979
980--echo #
981--echo # Bug#54606 innodb fast alter table + pack_keys=0
982--echo #           prevents adding new indexes
983--echo #
984
985--disable_warnings
986DROP TABLE IF EXISTS t1;
987--enable_warnings
988
989CREATE TABLE t1 (a INT, b CHAR(9), c INT, key(b))
990  ENGINE=InnoDB
991  PACK_KEYS=0;
992CREATE INDEX a ON t1 (a);
993CREATE INDEX c on t1 (c);
994
995DROP TABLE t1;
996
997
998--echo #
999--echo # Additional coverage for refactoring which is made as part
1000--echo # of fix for Bug#27480 "Extend CREATE TEMPORARY TABLES privilege
1001--echo # to allow temp table operations".
1002--echo #
1003--echo # Check that OPTIMIZE table works for temporary InnoDB tables.
1004--disable_warnings
1005DROP TABLE IF EXISTS t1;
1006--enable_warnings
1007CREATE TEMPORARY TABLE t1 (a INT) ENGINE=InnoDB;
1008OPTIMIZE TABLE t1;
1009DROP TABLE t1;
1010
1011
1012--echo #
1013--echo # Bug#11762345 54927: DROPPING AND ADDING AN INDEX IN ONE
1014--echo #              COMMAND CAN FAIL IN INNODB PLUGIN 1.0
1015--echo #
1016
1017--disable_warnings
1018DROP TABLE IF EXISTS t1;
1019--enable_warnings
1020
1021CREATE TABLE t1 (id int, a  int, b  int, PRIMARY KEY (id),
1022                 INDEX a (a)) ENGINE=innodb;
1023
1024ALTER TABLE t1 DROP INDEX a, ADD INDEX a (b, a);
1025# This used to fail
1026ALTER TABLE t1 DROP INDEX a, ADD INDEX (a, b);
1027
1028DROP TABLE t1;
1029
1030
1031--echo End of 6.0 tests
1032