Commit 7b79f728 authored by unknown's avatar unknown
Browse files

WL #2602, #2603, #2604

Added new syntax for partition management


mysql-test/t/partition_error.test:
  New line
sql/ha_partition.cc:
  Added support for DROP PARTITION on-line
sql/ha_partition.h:
  Added support for DROP PARTITION on-line
sql/handler.h:
  Introduced state of partition
  Introduced extra list of temporary partitions
  Removed no_full_parts
  A couple of methods to check for duplicate names of partitions
  Adaptions of default checks to be useful from ALTER TABLE
  partition management
  New method on handler to drop partitions
  New method to check for foreign keys on table
sql/lex.h:
  COALESCE and REORGANISE used by ALTER TABLE x COALESCE/REORGANISE PARTITION
sql/mysql_priv.h:
  Parser needs method to check if partition management command is ongoing
sql/share/errmsg.txt:
  A number of new error messages for partition management
sql/sql_lex.h:
  Adapted the ALTER_INFO data structure for partition management
sql/sql_partition.cc:
  Couple of new routines + adaption of existing for new
  partition management functionality
sql/sql_table.cc:
  bin log writing into separate subroutine to minimise code
  duplication.
  Lots of new code to handle partition management
sql/sql_yacc.yy:
  New syntax for partition management
  Fixed a few errors in the parser part for partitioning
parent c77f8d6e
Loading
Loading
Loading
Loading
+109 −0
Original line number Diff line number Diff line
CREATE TABLE t1 (a int, b int)
PARTITION BY RANGE (a)
(PARTITION x0 VALUES LESS THAN (2),
PARTITION x1 VALUES LESS THAN (4),
PARTITION x2 VALUES LESS THAN (6),
PARTITION x3 VALUES LESS THAN (8),
PARTITION x4 VALUES LESS THAN (10),
PARTITION x5 VALUES LESS THAN (12),
PARTITION x6 VALUES LESS THAN (14),
PARTITION x7 VALUES LESS THAN (16),
PARTITION x8 VALUES LESS THAN (18),
PARTITION x9 VALUES LESS THAN (20));
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (2),
PARTITION x11 VALUES LESS THAN (5));
ERROR HY000: The new partitions cover a bigger range then the reorganised partitions do
ALTER TABLE t1 DROP PARTITION x0, x1, x2, x3, x3;
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 DROP PARTITION x0, x1, x2, x10;
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 DROP PARTITION x10, x1, x2, x1;
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 DROP PARTITION x10, x1, x2, x3;
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 REORGANISE PARTITION x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10 INTO
(PARTITION x11 VALUES LESS THAN (22));
ERROR HY000: More partitions to reorganise than there are partitions
ALTER TABLE t1 REORGANISE PARTITION x0,x1,x2 INTO
(PARTITION x1 VALUES LESS THAN (6));
ERROR HY000: All partitions must have unique names in the table
ALTER TABLE t1 REORGANISE PARTITION x0, x2 INTO
(PARTITION x11 VALUES LESS THAN (2));
ERROR HY000: When reorganising a set of partitions they must be in consecutive order
ALTER TABLE t1 REORGANISE PARTITION x0, x1, x1 INTO
(PARTITION x11 VALUES LESS THAN (4));
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (5));
ERROR HY000: The new partitions cover a bigger range then the reorganised partitions do
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (4),
PARTITION x11 VALUES LESS THAN (2));
ERROR HY000: VALUES LESS THAN value must be strictly increasing for each partition
DROP TABLE t1;
CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
PARTITIONS 2;
ALTER TABLE t1 ADD PARTITION (PARTITION p1);
ERROR HY000: All partitions must have unique names in the table
DROP TABLE t1;
CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
(PARTITION x0, PARTITION x1, PARTITION x2, PARTITION x3, PARTITION x3);
ERROR HY000: All partitions must have unique names in the table
CREATE TABLE t1 (a int)
PARTITION BY RANGE (a)
SUBPARTITION BY KEY (a)
SUBPARTITIONS 2
(PARTITION x0 VALUES LESS THAN (4),
PARTITION x1 VALUES LESS THAN (8));
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES LESS THAN (5)
(SUBPARTITION sp0, SUBPARTITION sp1));
ERROR HY000: VALUES LESS THAN value must be strictly increasing for each partition
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES LESS THAN (12)
(SUBPARTITION sp0, SUBPARTITION sp1, SUBPARTITION sp2));
ERROR HY000: Trying to Add partition(s) with wrong number of subpartitions
DROP TABLE t1;
CREATE TABLE t1 (a int)
PARTITION BY LIST (a)
(PARTITION x0 VALUES IN (1,2,3),
PARTITION x1 VALUES IN (4,5,6));
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES IN (3,4));
ERROR HY000: Multiple definition of same constant in list partitioning
DROP TABLE t1;
CREATE TABLE t1 (a int);
ALTER TABLE t1 ADD PARTITION PARTITIONS 1;
ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 DROP PARTITION x1;
ERROR HY000: Partition management on a not partitioned table is not possible
ALTER TABLE t1 COALESCE PARTITION 1;
ERROR HY000: Partition management on a not partitioned table is not possible
DROP TABLE t1;
CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
(PARTITION x0, PARTITION x1);
ALTER TABLE t1 ADD PARTITION PARTITIONS 0;
ERROR HY000: At least one partition must be added
ALTER TABLE t1 ADD PARTITION PARTITIONS 1024;
ERROR HY000: Too many partitions were defined
ALTER TABLE t1 DROP PARTITION x0;
ERROR HY000: DROP PARTITION can only be used on RANGE/LIST partitions
ALTER TABLE t1 COALESCE PARTITION 1;
ALTER TABLE t1 COALESCE PARTITION 1;
ERROR HY000: Cannot remove all partitions, use DROP TABLE instead
DROP TABLE t1;
CREATE TABLE t1 (a int)
PARTITION BY RANGE (a)
(PARTITION x0 VALUES LESS THAN (4),
PARTITION x1 VALUES LESS THAN (8));
ALTER TABLE t1 ADD PARTITION PARTITIONS 1;
ERROR HY000: For RANGE partitions each partition must be defined
ALTER TABLE t1 DROP PARTITION x2;
ERROR HY000: Error in list of partitions to change
ALTER TABLE t1 COALESCE PARTITION 1;
ERROR HY000: COALESCE PARTITION can only be used on HASH/KEY partitions
ALTER TABLE t1 DROP PARTITION x1;
ALTER TABLE t1 DROP PARTITION x0;
ERROR HY000: Cannot remove all partitions, use DROP TABLE instead
DROP TABLE t1;
+1 −0
Original line number Diff line number Diff line
@@ -726,3 +726,4 @@ partition by list (a)
partitions 2
(partition x1 values in 4,
 partition x2 values in (5));
+160 −0
Original line number Diff line number Diff line
#
# Simple test for the erroneos create statements using the 
# partition storage engine
#
-- source include/have_partition.inc

#
# Try faulty DROP PARTITION and COALESCE PARTITION
#
CREATE TABLE t1 (a int, b int)
PARTITION BY RANGE (a)
(PARTITION x0 VALUES LESS THAN (2),
 PARTITION x1 VALUES LESS THAN (4),
 PARTITION x2 VALUES LESS THAN (6),
 PARTITION x3 VALUES LESS THAN (8),
 PARTITION x4 VALUES LESS THAN (10),
 PARTITION x5 VALUES LESS THAN (12),
 PARTITION x6 VALUES LESS THAN (14),
 PARTITION x7 VALUES LESS THAN (16),
 PARTITION x8 VALUES LESS THAN (18),
 PARTITION x9 VALUES LESS THAN (20));

--error ER_REORG_OUTSIDE_RANGE
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (2),
 PARTITION x11 VALUES LESS THAN (5));

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 DROP PARTITION x0, x1, x2, x3, x3;

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 DROP PARTITION x0, x1, x2, x10;

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 DROP PARTITION x10, x1, x2, x1;

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 DROP PARTITION x10, x1, x2, x3;

--error ER_REORG_PARTITION_NOT_EXIST
ALTER TABLE t1 REORGANISE PARTITION x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10 INTO
(PARTITION x11 VALUES LESS THAN (22));

--error ER_SAME_NAME_PARTITION
ALTER TABLE t1 REORGANISE PARTITION x0,x1,x2 INTO
(PARTITION x1 VALUES LESS THAN (6));

--error ER_CONSECUTIVE_REORG_PARTITIONS
ALTER TABLE t1 REORGANISE PARTITION x0, x2 INTO
(PARTITION x11 VALUES LESS THAN (2));

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 REORGANISE PARTITION x0, x1, x1 INTO
(PARTITION x11 VALUES LESS THAN (4));

--error ER_REORG_OUTSIDE_RANGE
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (5));

--error ER_RANGE_NOT_INCREASING_ERROR
ALTER TABLE t1 REORGANISE PARTITION x0,x1 INTO
(PARTITION x01 VALUES LESS THAN (4),
 PARTITION x11 VALUES LESS THAN (2));

DROP TABLE t1;

CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
PARTITIONS 2;

--error ER_SAME_NAME_PARTITION
ALTER TABLE t1 ADD PARTITION (PARTITION p1);

DROP TABLE t1;

--error ER_SAME_NAME_PARTITION
CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
(PARTITION x0, PARTITION x1, PARTITION x2, PARTITION x3, PARTITION x3);

CREATE TABLE t1 (a int)
PARTITION BY RANGE (a)
SUBPARTITION BY KEY (a)
SUBPARTITIONS 2
(PARTITION x0 VALUES LESS THAN (4),
 PARTITION x1 VALUES LESS THAN (8));

--error ER_RANGE_NOT_INCREASING_ERROR
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES LESS THAN (5)
(SUBPARTITION sp0, SUBPARTITION sp1));

--error ER_ADD_PARTITION_SUBPART_ERROR
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES LESS THAN (12)
(SUBPARTITION sp0, SUBPARTITION sp1, SUBPARTITION sp2));

DROP TABLE t1;

CREATE TABLE t1 (a int)
PARTITION BY LIST (a)
(PARTITION x0 VALUES IN (1,2,3),
 PARTITION x1 VALUES IN (4,5,6));

--error ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR
ALTER TABLE t1 ADD PARTITION (PARTITION x2 VALUES IN (3,4));

DROP TABLE t1;

CREATE TABLE t1 (a int);

--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 ADD PARTITION PARTITIONS 1;

--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 DROP PARTITION x1;

--error ER_PARTITION_MGMT_ON_NONPARTITIONED
ALTER TABLE t1 COALESCE PARTITION 1;

DROP TABLE t1;

CREATE TABLE t1 (a int)
PARTITION BY KEY (a)
(PARTITION x0, PARTITION x1);

--error ER_ADD_PARTITION_NO_NEW_PARTITION
ALTER TABLE t1 ADD PARTITION PARTITIONS 0;

--error ER_TOO_MANY_PARTITIONS_ERROR
ALTER TABLE t1 ADD PARTITION PARTITIONS 1024;

--error ER_ONLY_ON_RANGE_LIST_PARTITION
ALTER TABLE t1 DROP PARTITION x0;

ALTER TABLE t1 COALESCE PARTITION 1;

--error ER_DROP_LAST_PARTITION
ALTER TABLE t1 COALESCE PARTITION 1;

DROP TABLE t1;

CREATE TABLE t1 (a int)
PARTITION BY RANGE (a)
(PARTITION x0 VALUES LESS THAN (4),
 PARTITION x1 VALUES LESS THAN (8));

--error ER_PARTITIONS_MUST_BE_DEFINED_ERROR
ALTER TABLE t1 ADD PARTITION PARTITIONS 1;

--error ER_DROP_PARTITION_NON_EXISTENT
ALTER TABLE t1 DROP PARTITION x2;

--error ER_COALESCE_ONLY_ON_HASH_PARTITION
ALTER TABLE t1 COALESCE PARTITION 1;

ALTER TABLE t1 DROP PARTITION x1;

--error ER_DROP_LAST_PARTITION
ALTER TABLE t1 DROP PARTITION x0;

DROP TABLE t1;
+71 −10
Original line number Diff line number Diff line
@@ -269,6 +269,28 @@ int ha_partition::ha_initialise()
/****************************************************************************
                MODULE meta data changes
****************************************************************************/
/*
  This method is used to calculate the partition name, service routine to
  the del_ren_cre_table method.
*/

static void create_partition_name(char *out, const char *in1, const char *in2)
{
  strxmov(out, in1, "_", in2, NullS);
}

/*
  This method is used to calculate the partition name, service routine to
  the del_ren_cre_table method.
*/

static void create_subpartition_name(char *out, const char *in1,
                                     const char *in2, const char *in3)
{
  strxmov(out, in1, "_", in2, "_", in3, NullS);
}


/*
  Used to delete a table. By the time delete_table() has been called all
  opened references to this table will have been closed (and your globally
@@ -326,6 +348,12 @@ int ha_partition::rename_table(const char *from, const char *to)
int ha_partition::create_handler_files(const char *name)
{
  DBUG_ENTER("ha_partition::create_handler_files()");

  /*
    We need to update total number of parts since we might write the handler
    file as part of a partition management command
  */
  m_tot_parts= get_tot_partitions(m_part_info);
  if (create_handler_file(name))
  {
    my_error(ER_CANT_CREATE_HANDLER_FILE, MYF(0));
@@ -362,6 +390,49 @@ int ha_partition::create(const char *name, TABLE *table_arg,
  DBUG_RETURN(0);
}

int ha_partition::drop_partitions(const char *path)
{
  List_iterator<partition_element> part_it(m_part_info->partitions);
  char part_name_buff[FN_REFLEN];
  uint no_parts= m_part_info->no_parts;
  uint no_subparts= m_part_info->no_subparts, i= 0;
  int error= 1;
  DBUG_ENTER("ha_partition::drop_partitions()");

  do
  {
    partition_element *part_elem= part_it++;
    if (part_elem->part_state == PART_IS_DROPPED)
    {
      /*
        This part is to be dropped, meaning the part or all its subparts.
      */
      if (is_sub_partitioned(m_part_info))
      {
        List_iterator<partition_element> sub_it(part_elem->subpartitions);
        uint j= 0, part;
        do
        {
          partition_element *sub_elem= sub_it++;
          create_subpartition_name(part_name_buff, path,
                                   part_elem->partition_name,
                                   sub_elem->partition_name);
          part= i * no_subparts + j;
          DBUG_PRINT("info", ("Drop subpartition %s", part_name_buff));
          error= m_file[part]->delete_table((const char *) part_name_buff);
        } while (++j < no_subparts);
      }
      else
      {
        create_partition_name(part_name_buff, path,
                              part_elem->partition_name);
        DBUG_PRINT("info", ("Drop partition %s", part_name_buff));
        error= m_file[i]->delete_table((const char *) part_name_buff);
      }
    }
  } while (++i < no_parts);
  DBUG_RETURN(error);
}

void ha_partition::update_create_info(HA_CREATE_INFO *create_info)
{
@@ -375,16 +446,6 @@ char *ha_partition::update_table_comment(const char *comment)
}


/*
  This method is used to calculate the partition name, service routine to
  the del_ren_cre_table method.
*/

static void create_partition_name(char *out, const char *in1, const char *in2)
{
  strxmov(out, in1, "_", in2, NullS);
}


/*
  Common routine to handle delete_table and rename_table.
+6 −0
Original line number Diff line number Diff line
@@ -166,6 +166,7 @@ class ha_partition :public handler
  virtual int create_handler_files(const char *name);
  virtual void update_create_info(HA_CREATE_INFO * create_info);
  virtual char *update_table_comment(const char *comment);
  virtual int drop_partitions(const char *path);
private:
  /*
    delete_table, rename_table and create uses very similar logic which
@@ -633,6 +634,11 @@ class ha_partition :public handler
    index scan module.
    (NDB)
  */
  virtual ulong alter_table_flags(void) const
  {
    //return HA_ONLINE_ADD_EMPTY_PARTITION + HA_ONLINE_DROP_PARTITION;
    return HA_ONLINE_DROP_PARTITION;
  }
  virtual ulong table_flags() const
  { return m_table_flags; }
  /*
Loading