Commit c6115db4 authored by Mattias Jonsson's avatar Mattias Jonsson

Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work

on non-partitioned table

Problem was that partitioning specific commands was accepted
for non partitioned tables and treated like
ANALYZE/CHECK/OPTIMIZE/REPAIR TABLE, after bug-20129 was fixed,
which changed the code path from mysql_alter_table to
mysql_admin_table.

Solution was to check if the table was partitioned before
trying to execute the admin command

mysql-test/r/partition_mgm_err.result:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Updated test result
mysql-test/t/partition_mgm_err.test:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Updated test case
sql/ha_partition.cc:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
sql/ha_partition.h:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
sql/sql_lex.h:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
  
  Removed ALTER_ANALYZE/CHECK/OPTIMIZE/REPAIR_PARTITION and
  added ALTER_ADMIN_PARTITION instead.
sql/sql_partition.cc:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
  
  Removed ALTER_ANALYZE/CHECK/OPTIMIZE/REPAIR_PARTITION and
  added ALTER_ADMIN_PARTITION instead.
sql/sql_table.cc:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Give error and return if trying partitioning admin command
  on non partitioned table.
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
  
  Removed ALTER_ANALYZE/CHECK/OPTIMIZE/REPAIR_PARTITION and
  added ALTER_ADMIN_PARTITION instead.
sql/sql_yacc.yy:
  Bug#39434: ALTER TABLE CHECK/OPTIMIZE/ANALYZE PARTITION work
  on non-partitioned table
  
  Simplified the code by using ALTER_ADMIN_PARTITION for all
  commands that go through mysql_admin_tables and is set
  for partitioning specific commands that.
  
  Removed ALTER_ANALYZE/CHECK/OPTIMIZE/REPAIR_PARTITION and
  added ALTER_ADMIN_PARTITION instead.
parent b6704027
...@@ -84,6 +84,14 @@ ALTER TABLE t1 DROP PARTITION x1; ...@@ -84,6 +84,14 @@ ALTER TABLE t1 DROP PARTITION x1;
ERROR HY000: Partition management on a not partitioned table is not possible ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 COALESCE PARTITION 1; ALTER TABLE t1 COALESCE PARTITION 1;
ERROR HY000: Partition management on a not partitioned table is not possible ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 ANALYZE PARTITION p1;
ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 CHECK PARTITION p1;
ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 OPTIMIZE PARTITION p1;
ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 REPAIR PARTITION p1;
ERROR HY000: Partition management on a not partitioned table is not possible
DROP TABLE t1; DROP TABLE t1;
CREATE TABLE t1 (a int) CREATE TABLE t1 (a int)
PARTITION BY KEY (a) PARTITION BY KEY (a)
......
...@@ -125,6 +125,18 @@ ALTER TABLE t1 DROP PARTITION x1; ...@@ -125,6 +125,18 @@ ALTER TABLE t1 DROP PARTITION x1;
--error ER_PARTITION_MGMT_ON_NONPARTITIONED --error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 COALESCE PARTITION 1; ALTER TABLE t1 COALESCE PARTITION 1;
--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 ANALYZE PARTITION p1;
--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 CHECK PARTITION p1;
--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 OPTIMIZE PARTITION p1;
--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 REPAIR PARTITION p1;
DROP TABLE t1; DROP TABLE t1;
CREATE TABLE t1 (a int) CREATE TABLE t1 (a int)
......
...@@ -876,10 +876,7 @@ int ha_partition::optimize(THD *thd, HA_CHECK_OPT *check_opt) ...@@ -876,10 +876,7 @@ int ha_partition::optimize(THD *thd, HA_CHECK_OPT *check_opt)
{ {
DBUG_ENTER("ha_partition::optimize"); DBUG_ENTER("ha_partition::optimize");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, DBUG_RETURN(handle_opt_partitions(thd, check_opt, OPTIMIZE_PARTS));
OPTIMIZE_PARTS,
thd->lex->alter_info.flags &
ALTER_OPTIMIZE_PARTITION ? FALSE : TRUE));
} }
...@@ -900,10 +897,7 @@ int ha_partition::analyze(THD *thd, HA_CHECK_OPT *check_opt) ...@@ -900,10 +897,7 @@ int ha_partition::analyze(THD *thd, HA_CHECK_OPT *check_opt)
{ {
DBUG_ENTER("ha_partition::analyze"); DBUG_ENTER("ha_partition::analyze");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, DBUG_RETURN(handle_opt_partitions(thd, check_opt, ANALYZE_PARTS));
ANALYZE_PARTS,
thd->lex->alter_info.flags &
ALTER_ANALYZE_PARTITION ? FALSE : TRUE));
} }
...@@ -924,10 +918,7 @@ int ha_partition::check(THD *thd, HA_CHECK_OPT *check_opt) ...@@ -924,10 +918,7 @@ int ha_partition::check(THD *thd, HA_CHECK_OPT *check_opt)
{ {
DBUG_ENTER("ha_partition::check"); DBUG_ENTER("ha_partition::check");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, DBUG_RETURN(handle_opt_partitions(thd, check_opt, CHECK_PARTS));
CHECK_PARTS,
thd->lex->alter_info.flags &
ALTER_CHECK_PARTITION ? FALSE : TRUE));
} }
...@@ -948,12 +939,10 @@ int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt) ...@@ -948,12 +939,10 @@ int ha_partition::repair(THD *thd, HA_CHECK_OPT *check_opt)
{ {
DBUG_ENTER("ha_partition::repair"); DBUG_ENTER("ha_partition::repair");
DBUG_RETURN(handle_opt_partitions(thd, check_opt, DBUG_RETURN(handle_opt_partitions(thd, check_opt, REPAIR_PARTS));
REPAIR_PARTS,
thd->lex->alter_info.flags &
ALTER_REPAIR_PARTITION ? FALSE : TRUE));
} }
/* /*
Handle optimize/analyze/check/repair of one partition Handle optimize/analyze/check/repair of one partition
...@@ -1056,7 +1045,6 @@ static bool print_admin_msg(THD* thd, const char* msg_type, ...@@ -1056,7 +1045,6 @@ static bool print_admin_msg(THD* thd, const char* msg_type,
thd Thread object thd Thread object
check_opt Options check_opt Options
flag Optimize/Analyze/Check/Repair flag flag Optimize/Analyze/Check/Repair flag
all_parts All partitions or only a subset
RETURN VALUE RETURN VALUE
>0 Failure >0 Failure
...@@ -1064,7 +1052,7 @@ static bool print_admin_msg(THD* thd, const char* msg_type, ...@@ -1064,7 +1052,7 @@ static bool print_admin_msg(THD* thd, const char* msg_type,
*/ */
int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
uint flag, bool all_parts) uint flag)
{ {
List_iterator<partition_element> part_it(m_part_info->partitions); List_iterator<partition_element> part_it(m_part_info->partitions);
uint no_parts= m_part_info->no_parts; uint no_parts= m_part_info->no_parts;
...@@ -1072,7 +1060,7 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, ...@@ -1072,7 +1060,7 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
uint i= 0; uint i= 0;
int error; int error;
DBUG_ENTER("ha_partition::handle_opt_partitions"); DBUG_ENTER("ha_partition::handle_opt_partitions");
DBUG_PRINT("enter", ("all_parts %u, flag= %u", all_parts, flag)); DBUG_PRINT("enter", ("flag= %u", flag));
do do
{ {
...@@ -1081,7 +1069,7 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, ...@@ -1081,7 +1069,7 @@ int ha_partition::handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt,
when ALTER TABLE <CMD> PARTITION ... when ALTER TABLE <CMD> PARTITION ...
it should only do named partitions, otherwise all partitions it should only do named partitions, otherwise all partitions
*/ */
if (all_parts || if (!(thd->lex->alter_info.flags & ALTER_ADMIN_PARTITION) ||
part_elem->part_state == PART_CHANGED) part_elem->part_state == PART_CHANGED)
{ {
if (m_is_sub_partitioned) if (m_is_sub_partitioned)
......
...@@ -988,8 +988,7 @@ public: ...@@ -988,8 +988,7 @@ public:
virtual bool is_crashed() const; virtual bool is_crashed() const;
private: private:
int handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, int handle_opt_partitions(THD *thd, HA_CHECK_OPT *check_opt, uint flags);
uint flags, bool all_parts);
public: public:
/* /*
------------------------------------------------------------------------- -------------------------------------------------------------------------
......
...@@ -842,15 +842,12 @@ inline bool st_select_lex_unit::is_union () ...@@ -842,15 +842,12 @@ inline bool st_select_lex_unit::is_union ()
#define ALTER_COALESCE_PARTITION (1L << 15) #define ALTER_COALESCE_PARTITION (1L << 15)
#define ALTER_REORGANIZE_PARTITION (1L << 16) #define ALTER_REORGANIZE_PARTITION (1L << 16)
#define ALTER_PARTITION (1L << 17) #define ALTER_PARTITION (1L << 17)
#define ALTER_OPTIMIZE_PARTITION (1L << 18) #define ALTER_ADMIN_PARTITION (1L << 18)
#define ALTER_TABLE_REORG (1L << 19) #define ALTER_TABLE_REORG (1L << 19)
#define ALTER_REBUILD_PARTITION (1L << 20) #define ALTER_REBUILD_PARTITION (1L << 20)
#define ALTER_ALL_PARTITION (1L << 21) #define ALTER_ALL_PARTITION (1L << 21)
#define ALTER_ANALYZE_PARTITION (1L << 22) #define ALTER_REMOVE_PARTITIONING (1L << 22)
#define ALTER_CHECK_PARTITION (1L << 23) #define ALTER_FOREIGN_KEY (1L << 23)
#define ALTER_REPAIR_PARTITION (1L << 24)
#define ALTER_REMOVE_PARTITIONING (1L << 25)
#define ALTER_FOREIGN_KEY (1L << 26)
enum enum_alter_table_change_level enum enum_alter_table_change_level
{ {
......
...@@ -4195,12 +4195,13 @@ uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info, ...@@ -4195,12 +4195,13 @@ uint prep_alter_part_table(THD *thd, TABLE *table, Alter_info *alter_info,
!(thd->work_part_info= thd->lex->part_info->get_clone())) !(thd->work_part_info= thd->lex->part_info->get_clone()))
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
/* ALTER_ADMIN_PARTITION is handled in mysql_admin_table */
DBUG_ASSERT(!(alter_info->flags & ALTER_ADMIN_PARTITION));
if (alter_info->flags & if (alter_info->flags &
(ALTER_ADD_PARTITION | ALTER_DROP_PARTITION | (ALTER_ADD_PARTITION | ALTER_DROP_PARTITION |
ALTER_COALESCE_PARTITION | ALTER_REORGANIZE_PARTITION | ALTER_COALESCE_PARTITION | ALTER_REORGANIZE_PARTITION |
ALTER_TABLE_REORG | ALTER_OPTIMIZE_PARTITION | ALTER_TABLE_REORG | ALTER_REBUILD_PARTITION))
ALTER_CHECK_PARTITION | ALTER_ANALYZE_PARTITION |
ALTER_REPAIR_PARTITION | ALTER_REBUILD_PARTITION))
{ {
partition_info *tab_part_info= table->part_info; partition_info *tab_part_info= table->part_info;
partition_info *alt_part_info= thd->work_part_info; partition_info *alt_part_info= thd->work_part_info;
...@@ -4592,11 +4593,7 @@ that are reorganised. ...@@ -4592,11 +4593,7 @@ that are reorganised.
} }
tab_part_info->no_parts-= no_parts_dropped; tab_part_info->no_parts-= no_parts_dropped;
} }
else if ((alter_info->flags & ALTER_OPTIMIZE_PARTITION) || else if (alter_info->flags & ALTER_REBUILD_PARTITION)
(alter_info->flags & ALTER_ANALYZE_PARTITION) ||
(alter_info->flags & ALTER_CHECK_PARTITION) ||
(alter_info->flags & ALTER_REPAIR_PARTITION) ||
(alter_info->flags & ALTER_REBUILD_PARTITION))
{ {
uint no_parts_found; uint no_parts_found;
uint no_parts_opt= alter_info->partition_names.elements; uint no_parts_opt= alter_info->partition_names.elements;
...@@ -4604,18 +4601,7 @@ that are reorganised. ...@@ -4604,18 +4601,7 @@ that are reorganised.
if (no_parts_found != no_parts_opt && if (no_parts_found != no_parts_opt &&
(!(alter_info->flags & ALTER_ALL_PARTITION))) (!(alter_info->flags & ALTER_ALL_PARTITION)))
{ {
const char *ptr; my_error(ER_DROP_PARTITION_NON_EXISTENT, MYF(0), "REBUILD");
if (alter_info->flags & ALTER_OPTIMIZE_PARTITION)
ptr= "OPTIMIZE";
else if (alter_info->flags & ALTER_ANALYZE_PARTITION)
ptr= "ANALYZE";
else if (alter_info->flags & ALTER_CHECK_PARTITION)
ptr= "CHECK";
else if (alter_info->flags & ALTER_REPAIR_PARTITION)
ptr= "REPAIR";
else
ptr= "REBUILD";
my_error(ER_DROP_PARTITION_NON_EXISTENT, MYF(0), ptr);
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
} }
if (!(*fast_alter_partition)) if (!(*fast_alter_partition))
......
...@@ -4198,7 +4198,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables, ...@@ -4198,7 +4198,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
table->next_local= save_next_local; table->next_local= save_next_local;
thd->open_options&= ~extra_open_options; thd->open_options&= ~extra_open_options;
#ifdef WITH_PARTITION_STORAGE_ENGINE #ifdef WITH_PARTITION_STORAGE_ENGINE
if (table->table && table->table->part_info) if (table->table)
{ {
/* /*
Set up which partitions that should be processed Set up which partitions that should be processed
...@@ -4206,11 +4206,13 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables, ...@@ -4206,11 +4206,13 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
*/ */
Alter_info *alter_info= &lex->alter_info; Alter_info *alter_info= &lex->alter_info;
if (alter_info->flags & ALTER_ANALYZE_PARTITION || if (alter_info->flags & ALTER_ADMIN_PARTITION)
alter_info->flags & ALTER_CHECK_PARTITION || {
alter_info->flags & ALTER_OPTIMIZE_PARTITION || if (!table->table->part_info)
alter_info->flags & ALTER_REPAIR_PARTITION)
{ {
my_error(ER_PARTITION_MGMT_ON_NONPARTITIONED, MYF(0));
DBUG_RETURN(TRUE);
}
uint no_parts_found; uint no_parts_found;
uint no_parts_opt= alter_info->partition_names.elements; uint no_parts_opt= alter_info->partition_names.elements;
no_parts_found= set_part_state(alter_info, table->table->part_info, no_parts_found= set_part_state(alter_info, table->table->part_info,
......
...@@ -5658,7 +5658,7 @@ alter_commands: ...@@ -5658,7 +5658,7 @@ alter_commands:
{ {
LEX *lex= Lex; LEX *lex= Lex;
lex->sql_command = SQLCOM_OPTIMIZE; lex->sql_command = SQLCOM_OPTIMIZE;
lex->alter_info.flags|= ALTER_OPTIMIZE_PARTITION; lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
lex->no_write_to_binlog= $3; lex->no_write_to_binlog= $3;
lex->check_opt.init(); lex->check_opt.init();
} }
...@@ -5668,7 +5668,7 @@ alter_commands: ...@@ -5668,7 +5668,7 @@ alter_commands:
{ {
LEX *lex= Lex; LEX *lex= Lex;
lex->sql_command = SQLCOM_ANALYZE; lex->sql_command = SQLCOM_ANALYZE;
lex->alter_info.flags|= ALTER_ANALYZE_PARTITION; lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
lex->no_write_to_binlog= $3; lex->no_write_to_binlog= $3;
lex->check_opt.init(); lex->check_opt.init();
} }
...@@ -5676,7 +5676,7 @@ alter_commands: ...@@ -5676,7 +5676,7 @@ alter_commands:
{ {
LEX *lex= Lex; LEX *lex= Lex;
lex->sql_command = SQLCOM_CHECK; lex->sql_command = SQLCOM_CHECK;
lex->alter_info.flags|= ALTER_CHECK_PARTITION; lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
lex->check_opt.init(); lex->check_opt.init();
} }
opt_mi_check_type opt_mi_check_type
...@@ -5685,7 +5685,7 @@ alter_commands: ...@@ -5685,7 +5685,7 @@ alter_commands:
{ {
LEX *lex= Lex; LEX *lex= Lex;
lex->sql_command = SQLCOM_REPAIR; lex->sql_command = SQLCOM_REPAIR;
lex->alter_info.flags|= ALTER_REPAIR_PARTITION; lex->alter_info.flags|= ALTER_ADMIN_PARTITION;
lex->no_write_to_binlog= $3; lex->no_write_to_binlog= $3;
lex->check_opt.init(); lex->check_opt.init();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment