Skip to content

Commit a7dd24c

Browse files
committed
MDEV-8299: MyISAM or Aria table gets corrupted after EXPLAIN INSERT and INSERT
[EXPLAIN] INSERT .. SELECT creates a select_insert object. select_insert calls handler->start_bulk_insert() during initialization. For MyISAM/Aria this requires that a matching call to handler->end_bulk_insert() call is made. Regular INSERT .. SELECT accomplishes this by calling either select_result->send_eof() or select_result->abort_result_set(). EXPLAIN INSERT ... SELECT didn't call either, which resulted in improper de-initializaiton of handler object. Make it call abort_result_set(), which invokes handler->end_bulk_insert().
1 parent bb22eb5 commit a7dd24c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

mysql-test/r/explain_non_select.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,18 @@ id select_type table type possible_keys key key_len ref rows Extra
232232
1 SIMPLE user index NULL PRIMARY 420 NULL 4 Using index
233233
DROP TABLE t1;
234234
DROP VIEW v1;
235+
#
236+
# MDEV-8299: MyISAM or Aria table gets corrupted after EXPLAIN INSERT and INSERT
237+
#
238+
CREATE TABLE t1 (pk INT NOT NULL AUTO_INCREMENT PRIMARY KEY, i INT, KEY(i)) ENGINE=MyISAM;
239+
INSERT INTO t1 (i) VALUES (100),(200);
240+
CREATE TABLE t2 (j INT) ENGINE=MyISAM;
241+
INSERT INTO t2 VALUES (10),(20);
242+
EXPLAIN INSERT INTO t1 (i) SELECT j FROM t2;
243+
id select_type table type possible_keys key key_len ref rows Extra
244+
1 SIMPLE t2 ALL NULL NULL NULL NULL 2
245+
INSERT INTO t1 (i) VALUES (300);
246+
CHECK TABLE t1;
247+
Table Op Msg_type Msg_text
248+
test.t1 check status OK
249+
DROP TABLE t1, t2;

mysql-test/t/explain_non_select.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,19 @@ INSERT INTO t1 VALUES (1),(2);
208208
EXPLAIN UPDATE v1, mysql.user SET v1.a = v1.a + 1;
209209
DROP TABLE t1;
210210
DROP VIEW v1;
211+
212+
--echo #
213+
--echo # MDEV-8299: MyISAM or Aria table gets corrupted after EXPLAIN INSERT and INSERT
214+
--echo #
215+
CREATE TABLE t1 (pk INT NOT NULL AUTO_INCREMENT PRIMARY KEY, i INT, KEY(i)) ENGINE=MyISAM;
216+
INSERT INTO t1 (i) VALUES (100),(200);
217+
218+
CREATE TABLE t2 (j INT) ENGINE=MyISAM;
219+
INSERT INTO t2 VALUES (10),(20);
220+
221+
EXPLAIN INSERT INTO t1 (i) SELECT j FROM t2;
222+
INSERT INTO t1 (i) VALUES (300);
223+
CHECK TABLE t1;
224+
225+
DROP TABLE t1, t2;
226+

sql/sql_parse.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,16 @@ case SQLCOM_PREPARE:
35493549
query_cache_invalidate3(thd, first_table, 1);
35503550
first_table->next_local= save_table;
35513551
}
3552+
if (explain)
3553+
{
3554+
/*
3555+
sel_result needs to be cleaned up properly.
3556+
INSERT... SELECT statement will call either send_eof() or
3557+
abort_result_set(). EXPLAIN doesn't call either, so we need
3558+
to cleanup manually.
3559+
*/
3560+
sel_result->abort_result_set();
3561+
}
35523562
delete sel_result;
35533563
}
35543564

0 commit comments

Comments
 (0)