Skip to content

Commit 1fd84f9

Browse files
committed
MDEV-16760 CREATE OR REPLACE TABLE never updates statistical tables
If the command CREATE OR REPLACE TABLE really replaces a table then it should remove all data on this table from all statistical tables.
1 parent c89bb15 commit 1fd84f9

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

mysql-test/r/stat_tables.result

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,28 @@ pk
552552
2
553553
DROP TABLE t1;
554554
set use_stat_tables=@save_use_stat_tables;
555+
#
556+
# MDEV-16760: CREATE OR REPLACE TABLE after ANALYZE TABLE
557+
#
558+
SET use_stat_tables= PREFERABLY;
559+
CREATE TABLE t1 (pk int PRIMARY KEY, c varchar(32));
560+
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
561+
ANALYZE TABLE t1;
562+
Table Op Msg_type Msg_text
563+
test.t1 analyze status Engine-independent statistics collected
564+
test.t1 analyze status OK
565+
SELECT * FROM t1;
566+
pk c
567+
1 foo
568+
2 bar
569+
SELECT * FROM mysql.column_stats;
570+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
571+
test t1 pk 1 2 0.0000 4.0000 1.0000 0 NULL NULL
572+
test t1 c bar foo 0.0000 3.0000 1.0000 0 NULL NULL
573+
CREATE OR REPLACE TABLE t1 (pk int PRIMARY KEY, a char(7));
574+
SELECT * FROM t1;
575+
pk a
576+
SELECT * FROM mysql.column_stats;
577+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
578+
DROP TABLE t1;
579+
set use_stat_tables=@save_use_stat_tables;

mysql-test/r/stat_tables_innodb.result

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,5 +579,30 @@ pk
579579
2
580580
DROP TABLE t1;
581581
set use_stat_tables=@save_use_stat_tables;
582+
#
583+
# MDEV-16760: CREATE OR REPLACE TABLE after ANALYZE TABLE
584+
#
585+
SET use_stat_tables= PREFERABLY;
586+
CREATE TABLE t1 (pk int PRIMARY KEY, c varchar(32));
587+
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
588+
ANALYZE TABLE t1;
589+
Table Op Msg_type Msg_text
590+
test.t1 analyze status Engine-independent statistics collected
591+
test.t1 analyze status OK
592+
SELECT * FROM t1;
593+
pk c
594+
1 foo
595+
2 bar
596+
SELECT * FROM mysql.column_stats;
597+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
598+
test t1 pk 1 2 0.0000 4.0000 1.0000 0 NULL NULL
599+
test t1 c bar foo 0.0000 3.0000 1.0000 0 NULL NULL
600+
CREATE OR REPLACE TABLE t1 (pk int PRIMARY KEY, a char(7));
601+
SELECT * FROM t1;
602+
pk a
603+
SELECT * FROM mysql.column_stats;
604+
db_name table_name column_name min_value max_value nulls_ratio avg_length avg_frequency hist_size hist_type histogram
605+
DROP TABLE t1;
606+
set use_stat_tables=@save_use_stat_tables;
582607
set optimizer_switch=@save_optimizer_switch_for_stat_tables_test;
583608
SET SESSION STORAGE_ENGINE=DEFAULT;

mysql-test/t/stat_tables.test

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,23 @@ SELECT pk FROM t1;
336336
DROP TABLE t1;
337337

338338
set use_stat_tables=@save_use_stat_tables;
339+
340+
--echo #
341+
--echo # MDEV-16760: CREATE OR REPLACE TABLE after ANALYZE TABLE
342+
--echo #
343+
344+
SET use_stat_tables= PREFERABLY;
345+
346+
CREATE TABLE t1 (pk int PRIMARY KEY, c varchar(32));
347+
INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
348+
ANALYZE TABLE t1;
349+
SELECT * FROM t1;
350+
SELECT * FROM mysql.column_stats;
351+
352+
CREATE OR REPLACE TABLE t1 (pk int PRIMARY KEY, a char(7));
353+
SELECT * FROM t1;
354+
SELECT * FROM mysql.column_stats;
355+
356+
DROP TABLE t1;
357+
358+
set use_stat_tables=@save_use_stat_tables;

sql/sql_table.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,10 @@ int create_table_impl(THD *thd,
47624762
{
47634763
if (create_info->options & HA_LEX_CREATE_REPLACE)
47644764
{
4765+
LEX_STRING db_name= {(char *) db, strlen(db)};
4766+
LEX_STRING tab_name= {(char *) table_name, strlen(table_name)};
4767+
(void) delete_statistics_for_table(thd, &db_name, &tab_name);
4768+
47654769
TABLE_LIST table_list;
47664770
table_list.init_one_table(db, strlen(db), table_name,
47674771
strlen(table_name), table_name,

0 commit comments

Comments
 (0)