Skip to content

Commit b7f3119

Browse files
MDEV-40388: sequence.simple fails on replay
When recording is enabled for the query such as, explain select * from seq_1_to_10; it recorded the table context having a DDL definition as: - CREATE TABLE `seq_1_to_10` ( -> `seq` bigint(20) unsigned NOT NULL, -> PRIMARY KEY (`seq`) -> ) ENGINE=SEQUENCE DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci; Now, when that context is replayed, the DDL statement is executed. But, we cannot create such a table, and instead it errors out saying ERROR 1050 (42S01): Table 'seq_1_to_10' already exists. Solution is to not record a DDL statement or any stats for sequence tables such as seq_1_to_10.
1 parent 97e8c62 commit b7f3119

5 files changed

Lines changed: 88 additions & 4 deletions

File tree

mysql-test/main/opt_context_store_ddls.result

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,39 @@ CREATE TABLE `t1` (
542542

543543

544544
drop table t1, t2;
545+
#
546+
# MDEV-40388: sequence.simple fails on replay
547+
#
548+
create sequence s1;
549+
# context result should have the ddl
550+
explain select * from s1;
551+
id select_type table type possible_keys key key_len ref rows Extra
552+
1 SIMPLE s1 system NULL NULL NULL NULL 1
553+
# == Optimizer Context Tables
554+
name
555+
# === Optimizer Context DDLs
556+
@ddls
557+
CREATE TABLE `s1` (
558+
`next_not_cached_value` bigint(21) NOT NULL,
559+
`minimum_value` bigint(21) NOT NULL,
560+
`maximum_value` bigint(21) NOT NULL,
561+
`start_value` bigint(21) NOT NULL COMMENT 'start value when sequences is created or value if RESTART is used',
562+
`increment` bigint(21) NOT NULL COMMENT 'increment value',
563+
`cache_size` bigint(21) unsigned NOT NULL,
564+
`cycle_option` tinyint(1) unsigned NOT NULL COMMENT '0 if no cycles are allowed, 1 if the sequence should begin a new cycle when maximum_value is passed',
565+
`cycle_count` bigint(21) NOT NULL COMMENT 'How many cycles have been done'
566+
) ENGINE=MyISAM SEQUENCE=1;
567+
568+
569+
drop table s1;
570+
# no ddl should be captured here
571+
explain select * from seq_1_to_10;
572+
id select_type table type possible_keys key key_len ref rows Extra
573+
1 SIMPLE seq_1_to_10 index NULL PRIMARY 8 NULL 10 Using index
574+
# == Optimizer Context Tables
575+
name
576+
# === Optimizer Context DDLs
577+
@ddls
578+
579+
# End of 13.1 tests
545580
drop database db1;

mysql-test/main/opt_context_store_ddls.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,19 @@ delete t1.*, t2.* from t1, t2 where t1.id1 = t2.id2;
312312

313313
drop table t1, t2;
314314

315+
--echo #
316+
--echo # MDEV-40388: sequence.simple fails on replay
317+
--echo #
318+
create sequence s1;
319+
--echo # context result should have the ddl
320+
explain select * from s1;
321+
--source include/opt_context_list_tables_ddls.inc
322+
drop table s1;
323+
324+
--echo # no ddl should be captured here
325+
explain select * from seq_1_to_10;
326+
--source include/opt_context_list_tables_ddls.inc
327+
328+
--echo # End of 13.1 tests
329+
315330
drop database db1;

mysql-test/main/opt_context_store_stats.result

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,19 @@ table_name file_stat_records index_name rec_per_key
591591
index_name ranges num_rows max_index_blocks max_row_blocks
592592
# == End of optimizer context
593593
drop table s1;
594+
#
595+
# MDEV-40388: sequence.simple fails on replay
596+
# Table context should *not* be recorded for seq
597+
#
598+
set optimizer_record_context=ON;
599+
explain select * from seq_1_to_10;
600+
id select_type table type possible_keys key key_len ref rows Extra
601+
1 SIMPLE seq_1_to_10 index NULL PRIMARY 8 NULL 10 Using index
602+
# == Optimizer Context
603+
# === Tables
604+
# Tables in the context
605+
table_name file_stat_records index_name rec_per_key
606+
# === Range accesses
607+
index_name ranges num_rows max_index_blocks max_row_blocks
608+
# == End of optimizer context
609+
# End of 13.1 tests

mysql-test/main/opt_context_store_stats.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,14 @@ EXPLAIN select * from s1;
432432

433433
--source include/opt_context_list_tables_and_ranges.inc
434434
drop table s1;
435+
436+
--echo #
437+
--echo # MDEV-40388: sequence.simple fails on replay
438+
--echo # Table context should *not* be recorded for seq
439+
--echo #
440+
set optimizer_record_context=ON;
441+
explain select * from seq_1_to_10;
442+
443+
--source include/opt_context_list_tables_and_ranges.inc
444+
445+
--echo # End of 13.1 tests

sql/opt_context_store_replay.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,10 +756,14 @@ bool Optimizer_context_recorder::dump_sql_script(THD* thd, String &sql_script)
756756
append_table_or_view_name(tbl, &full_tbl_name);
757757

758758
/*
759-
Sequence table doesn't need CREATE TABLE or contain any stats
759+
Sequence table such as seq_1_to_10 is a virtual table and so
760+
doesn't need CREATE TABLE STATEMENT, and stats.
760761
*/
761-
if (tbl->table && tbl->table->s && tbl->table->s->sequence)
762+
if (!tbl->is_view() && tbl->table &&
763+
tbl->table->s->db_type()->discover_table)
764+
{
762765
continue;
766+
}
763767

764768
/*
765769
A query can use the same table multiple times. Do not dump the
@@ -809,8 +813,11 @@ bool Optimizer_context_recorder::dump_sql_script(THD* thd, String &sql_script)
809813
qry_ctx_script.append(ddl);
810814
qry_ctx_script.append(STRING_WITH_LEN(";\n\n"));
811815

812-
/* If this is a VIEW, we've stored its DDL and we're done. */
813-
if (tbl->is_view())
816+
/* If this is a VIEW or a SEQUENCE table as
817+
CREATE SEQUENCE s1;
818+
explain SELECT * FROM s1;
819+
we've stored its DDL and we're done. */
820+
if (tbl->is_view() || (tbl->table && tbl->table->s->sequence))
814821
continue;
815822

816823
/* No, it's a base table */

0 commit comments

Comments
 (0)