List:Commits« Previous MessageNext Message »
From:Martin Skold Date:February 27 2008 4:07pm
Subject:bk commit into 5.1 tree (mskold:1.2649) BUG#31231
View as plain text  
Below is the list of changes that have just been committed into a local
5.1 repository of mskold.  When mskold does a push these changes
will be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2008-02-27 17:07:37+01:00, mskold@stripped +5 -0
  mysql_priv.h:
    bug #31231  mysql_alter_table() tries to drop a non-existing table: added FRM_ONLY flag
  ha_ndbcluster.cc:
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY: Removed check for non-pk tables, not needed when mysql_alter_table checks apropriate flags
  ndb_alter_table3.test, ndb_alter_table3.result:
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY: added test cases
  sql_table.cc:
    bug #31231  mysql_alter_table() tries to drop a non-existing table
    Don't invoke handler for tables defined with FRM_ONLY flag.
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY
    When a table is defined without an explicit primary key
    mysql will choose the first found unique index defined over
    non-nullable fields (if such an index exists). This means
    that if such an index is added (the first) or dropped (the last)
    through an alter table, this equals adding or dropping a primary key.
    The implementation for on-line add/drop index did not consider
    this semantics. This patch ensures that only handlers with the
    correctly defined flags (see handler.h for explanation of the flags):
    HA_ONLINE_ADD_PK_INDEX
    HA_ONLINE_ADD_PK_INDEX_NO_WRITES
    HA_ONLINE_DROP_PK_INDEX
    HA_ONLINE_DROP_PK_INDEX_NO_WRITES
    are invoked for such on-line operations. All others handlers must
    perform a full (offline) alter table.

  mysql-test/suite/ndb/r/ndb_alter_table3.result@stripped, 2008-02-27 16:48:03+01:00, mskold@stripped +47 -0
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY: added test cases

  mysql-test/suite/ndb/t/ndb_alter_table3.test@stripped, 2008-02-27 16:48:03+01:00, mskold@stripped +28 -0
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY: added test cases

  sql/ha_ndbcluster.cc@stripped, 2008-02-27 16:50:13+01:00, mskold@stripped +8 -19
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY: Removed check for non-pk tables, not needed when mysql_alter_table checks apropriate flags

  sql/mysql_priv.h@stripped, 2008-02-27 16:52:30+01:00, mskold@stripped +1 -0
    bug #31231  mysql_alter_table() tries to drop a non-existing table: added FRM_ONLY flag

  sql/sql_table.cc@stripped, 2008-02-27 16:53:30+01:00, mskold@stripped +76 -12
    bug #31231  mysql_alter_table() tries to drop a non-existing table
    Don't invoke handler for tables defined with FRM_ONLY flag.
    
    bug#31233 mysql_alter_table() fails to drop UNIQUE KEY
    When a table is defined without an explicit primary key
    mysql will choose the first found unique index defined over
    non-nullable fields (if such an index exists). This means
    that if such an index is added (the first) or dropped (the last)
    through an alter table, this equals adding or dropping a primary key.
    The implementation for on-line add/drop index did not consider
    this semantics. This patch ensures that only handlers with the
    correctly defined flags (see handler.h for explanation of the flags):
    HA_ONLINE_ADD_PK_INDEX
    HA_ONLINE_ADD_PK_INDEX_NO_WRITES
    HA_ONLINE_DROP_PK_INDEX
    HA_ONLINE_DROP_PK_INDEX_NO_WRITES
    are invoked for such on-line operations. All others handlers must
    perform a full (offline) alter table.

diff -Nrup a/mysql-test/suite/ndb/r/ndb_alter_table3.result b/mysql-test/suite/ndb/r/ndb_alter_table3.result
--- a/mysql-test/suite/ndb/r/ndb_alter_table3.result	2007-06-27 14:27:28 +02:00
+++ b/mysql-test/suite/ndb/r/ndb_alter_table3.result	2008-02-27 16:48:03 +01:00
@@ -33,3 +33,50 @@ a
 drop table t4;
 show tables;
 Tables_in_test
+create table t1(a int not null) engine=ndb;
+$PK Bigunsigned PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY($PK) - UniqueHashIndex
+insert into t1 values (1),(2),(3);
+alter table t1 add primary key (a);
+a Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY(a) - UniqueHashIndex
+PRIMARY(a) - OrderedIndex
+update t1 set a = 17 where a = 1;
+select * from t1 order by a;
+a
+2
+3
+17
+alter table t1 drop primary key;
+$PK Bigunsigned PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY($PK) - UniqueHashIndex
+update t1 set a = 1 where a = 17;
+select * from t1 order by a;
+a
+1
+2
+3
+drop table t1;
+create table t1(a int not null) engine=ndb;
+$PK Bigunsigned PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY($PK) - UniqueHashIndex
+insert into t1 values (1),(2),(3);
+create unique index pk on t1(a);
+a Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY(a) - UniqueHashIndex
+update t1 set a = 17 where a = 1;
+select * from t1 order by a;
+a
+2
+3
+17
+alter table t1 drop index pk;
+$PK Bigunsigned PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
+PRIMARY KEY($PK) - UniqueHashIndex
+update t1 set a = 1 where a = 17;
+select * from t1 order by a;
+a
+1
+2
+3
+drop table t1;
diff -Nrup a/mysql-test/suite/ndb/t/ndb_alter_table3.test b/mysql-test/suite/ndb/t/ndb_alter_table3.test
--- a/mysql-test/suite/ndb/t/ndb_alter_table3.test	2007-07-04 22:06:22 +02:00
+++ b/mysql-test/suite/ndb/t/ndb_alter_table3.test	2008-02-27 16:48:03 +01:00
@@ -46,3 +46,31 @@ select * from t4;
 drop table t4;
 show tables;
 connection server1;
+
+# Check add/drop primary key (not supported on-line)
+create table t1(a int not null) engine=ndb;
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+insert into t1 values (1),(2),(3);
+alter table t1 add primary key (a);
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+update t1 set a = 17 where a = 1;
+select * from t1 order by a;
+alter table t1 drop primary key;
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+update t1 set a = 1 where a = 17;
+select * from t1 order by a;
+drop table t1;
+
+# bug#31233 mysql_alter_table() fails to drop UNIQUE KEY
+create table t1(a int not null) engine=ndb;
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+insert into t1 values (1),(2),(3);
+create unique index pk on t1(a);
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+update t1 set a = 17 where a = 1;
+select * from t1 order by a;
+alter table t1 drop index pk;
+--exec $NDB_TOOLS_DIR/ndb_desc --no-defaults -d test t1 | grep PRIMARY
+update t1 set a = 1 where a = 17;
+select * from t1 order by a;
+drop table t1;
diff -Nrup a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc
--- a/sql/ha_ndbcluster.cc	2007-12-15 14:59:38 +01:00
+++ b/sql/ha_ndbcluster.cc	2008-02-27 16:50:13 +01:00
@@ -9939,35 +9939,24 @@ bool ha_ndbcluster::check_if_incompatibl
 
   if (table_changes != IS_EQUAL_YES)
     DBUG_RETURN(COMPATIBLE_DATA_NO);
-  
-  /**
-   * Changing from/to primary key
-   *
-   * This is _not_ correct, but check_if_incompatible_data-interface
-   *   doesnt give more info, so I guess that we can't do any
-   *   online add index if not using primary key
-   *
-   *   This as mysql will handle a unique not null index as primary 
-   *     even wo/ user specifiying it... :-(
-   *   
-   */
-  if ((table_share->primary_key == MAX_KEY && pk) ||
-      (table_share->primary_key != MAX_KEY && !pk) ||
-      (table_share->primary_key == MAX_KEY && !pk && ai))
-  {
-    DBUG_RETURN(COMPATIBLE_DATA_NO);
-  }
-  
+
   /* Check that auto_increment value was not changed */
   if ((create_info->used_fields & HA_CREATE_USED_AUTO) &&
       create_info->auto_increment_value != 0)
+  {
+    DBUG_PRINT("info", ("auto_increment value changed"));
     DBUG_RETURN(COMPATIBLE_DATA_NO);
+  }
   
   /* Check that row format didn't change */
   if ((create_info->used_fields & HA_CREATE_USED_AUTO) &&
       get_row_type() != create_info->row_type)
+  {
+    DBUG_PRINT("info", ("row format changed"));
     DBUG_RETURN(COMPATIBLE_DATA_NO);
+  }
 
+  DBUG_PRINT("info", ("new table seems compatible"));
   DBUG_RETURN(COMPATIBLE_DATA_YES);
 }
 
diff -Nrup a/sql/mysql_priv.h b/sql/mysql_priv.h
--- a/sql/mysql_priv.h	2007-12-14 14:01:09 +01:00
+++ b/sql/mysql_priv.h	2008-02-27 16:52:30 +01:00
@@ -2146,6 +2146,7 @@ uint build_table_shadow_filename(char *b
 #define FN_TO_IS_TMP    (1 << 1)
 #define FN_IS_TMP       (FN_FROM_IS_TMP | FN_TO_IS_TMP)
 #define NO_FRM_RENAME   (1 << 2)
+#define FRM_ONLY     (1 << 3)
 
 /* from hostname.cc */
 struct in_addr;
diff -Nrup a/sql/sql_table.cc b/sql/sql_table.cc
--- a/sql/sql_table.cc	2007-12-17 13:39:54 +01:00
+++ b/sql/sql_table.cc	2008-02-27 16:53:30 +01:00
@@ -1831,8 +1831,9 @@ bool quick_rm_table(handlerton *base,con
   if (my_delete(path,MYF(0)))
     error= 1; /* purecov: inspected */
   path[path_length - reg_ext_length]= '\0'; // Remove reg_ext
-  DBUG_RETURN(ha_delete_table(current_thd, base, path, db, table_name, 0) ||
-              error);
+  if (!(flags & FRM_ONLY))
+    error|= ha_delete_table(current_thd, base, path, db, table_name, 0);
+  DBUG_RETURN(error);
 }
 
 /*
@@ -5038,6 +5039,7 @@ err:
       index_drop_count    OUT   The number of elements in the array.
       index_add_buffer    OUT   An array of offsets into key_info_buffer.
       index_add_count     OUT   The number of elements in the array.
+      candidate_key_count  OUT  The number of candidate keys in original table.
 
   DESCRIPTION
     'table' (first argument) contains information of the original
@@ -5068,7 +5070,8 @@ compare_tables(TABLE *table,
                enum_alter_table_change_level *need_copy_table,
                KEY **key_info_buffer,
                uint **index_drop_buffer, uint *index_drop_count,
-               uint **index_add_buffer, uint *index_add_count)
+               uint **index_add_buffer, uint *index_add_count,
+               uint *candidate_key_count)
 {
   Field **f_ptr, *field;
   uint changes= 0, tmp;
@@ -5082,6 +5085,7 @@ compare_tables(TABLE *table,
     create_info->varchar will be reset in mysql_prepare_create_table.
   */
   bool varchar= create_info->varchar;
+  bool not_nullable= true;
   DBUG_ENTER("compare_tables");
 
   {
@@ -5220,6 +5224,7 @@ compare_tables(TABLE *table,
   */
   *index_drop_count= 0;
   *index_add_count= 0;
+  *candidate_key_count= 0;
   for (table_key= table->key_info; table_key < table_key_end; table_key++)
   {
     KEY_PART_INFO *table_part;
@@ -5247,6 +5252,8 @@ compare_tables(TABLE *table,
         (table_key->key_parts != new_key->key_parts))
       goto index_changed;
 
+    
+    not_nullable= true;
     /*
       Check that the key parts remain compatible between the old and
       new tables.
@@ -5255,6 +5262,8 @@ compare_tables(TABLE *table,
          table_part < table_part_end;
          table_part++, new_part++)
     {
+      not_nullable= not_nullable && (! table_part->field->maybe_null());
+
       /*
 	Key definition has changed if we are using a different field or
 	if the used key part length is different. We know that the fields
@@ -5264,6 +5273,15 @@ compare_tables(TABLE *table,
           (table_part->fieldnr - 1 != new_part->fieldnr))
 	goto index_changed;
     }
+
+    /*
+      Count all candidate keys,
+      i.e all non-nullable unique indexes
+     */
+    if ((table_key->flags & HA_NOSAME) && not_nullable)
+      (*candidate_key_count)++;
+
+
     continue;
 
   index_changed:
@@ -5851,13 +5869,16 @@ bool mysql_alter_table(THD *thd,char *ne
   uint *index_drop_buffer;
   uint index_add_count;
   uint *index_add_buffer;
+  uint candidate_key_count;
   bool committed= 0;
+  bool no_pk;
   DBUG_ENTER("mysql_alter_table");
 
   LINT_INIT(index_add_count);
   LINT_INIT(index_drop_count);
   LINT_INIT(index_add_buffer);
   LINT_INIT(index_drop_buffer);
+  LINT_INIT(candidate_key_count);
 
   /*
     Check if we attempt to alter mysql.slow_log or
@@ -6268,7 +6289,8 @@ view_err:
                        &need_copy_table_res,
                        &key_info_buffer,
                        &index_drop_buffer, &index_drop_count,
-                       &index_add_buffer, &index_add_count))
+                       &index_add_buffer, &index_add_count,
+                       &candidate_key_count))
       goto err;
    
     if (need_copy_table == ALTER_TABLE_METADATA_ONLY)
@@ -6289,6 +6311,7 @@ view_err:
     KEY   *key;
     uint  *idx_p;
     uint  *idx_end_p;
+    bool is_candidate_key= true;
 
     if (table->s->db_type()->alter_table_flags)
       alter_flags= table->s->db_type()->alter_table_flags(alter_info->flags);
@@ -6302,10 +6325,13 @@ view_err:
       DBUG_PRINT("info", ("index dropped: '%s'", key->name));
       if (key->flags & HA_NOSAME)
       {
-        /* Unique key. Check for "PRIMARY". */
-        if (! my_strcasecmp(system_charset_info,
-                            key->name, primary_key_name))
+        /*
+           Unique key. Check for "PRIMARY"
+           or if dropping last unique key.
+        */
+        if ((uint) (key - table->key_info) ==  table->s->primary_key)
         {
+          DBUG_PRINT("info", ("Dropping primary key"));
           /* Primary key. */
           needed_online_flags|=  HA_ONLINE_DROP_PK_INDEX;
           needed_fast_flags|= HA_ONLINE_DROP_PK_INDEX_NO_WRITES;
@@ -6325,7 +6351,8 @@ view_err:
         needed_fast_flags|= HA_ONLINE_DROP_INDEX_NO_WRITES;
       }
     }
-
+    no_pk= ((table->s->primary_key == MAX_KEY) ||
+            (needed_online_flags & HA_ONLINE_DROP_PK_INDEX));
     /* Check added indexes. */
     for (idx_p= index_add_buffer, idx_end_p= idx_p + index_add_count;
          idx_p < idx_end_p;
@@ -6335,14 +6362,34 @@ view_err:
       DBUG_PRINT("info", ("index added: '%s'", key->name));
       if (key->flags & HA_NOSAME)
       {
-        /* Unique key. Check for "PRIMARY". */
-        if (! my_strcasecmp(system_charset_info,
-                            key->name, primary_key_name))
+        /*
+           Unique key. Check for "PRIMARY"
+           or if adding first unique key
+           defined on non-nullable fields
+        */
+        KEY_PART_INFO *part_end= key->key_part + key->key_parts;    
+        bool is_candidate_key= true;
+
+        /*
+          Check that all fields in key are declared
+          NOT NULL
+         */
+        for (KEY_PART_INFO *key_part= key->key_part;
+             key_part < part_end;
+             key_part++)
+          is_candidate_key=
+            (is_candidate_key && 
+             (! table->field[key_part->fieldnr]->maybe_null()));
+        if ((! my_strcasecmp(system_charset_info,
+                             key->name, primary_key_name)) ||
+            (no_pk && candidate_key_count == 0 && is_candidate_key))
         {
+          DBUG_PRINT("info", ("Adding primary key"));
           /* Primary key. */
           needed_online_flags|=  HA_ONLINE_ADD_PK_INDEX;
           needed_fast_flags|= HA_ONLINE_ADD_PK_INDEX_NO_WRITES;
           pk_changed++;
+          no_pk= false;
         }
         else
         {
@@ -6359,6 +6406,20 @@ view_err:
       }
     }
 
+    if ((candidate_key_count > 0) && 
+        (needed_online_flags & HA_ONLINE_DROP_PK_INDEX))
+    {
+      /*
+        Dropped primary key when there is some other unique 
+        not null key that should be converted to primary key
+      */
+      needed_online_flags|=  HA_ONLINE_ADD_PK_INDEX;
+      needed_fast_flags|= HA_ONLINE_ADD_PK_INDEX_NO_WRITES;
+      pk_changed= 2;
+    }
+
+    DBUG_PRINT("info", ("needed_online_flags: 0x%lx, needed_fast_flags: 0x%lx",
+                        needed_online_flags, needed_fast_flags));
     /*
       Online or fast add/drop index is possible only if
       the primary key is not added and dropped in the same statement.
@@ -6857,7 +6918,10 @@ err1:
     close_temporary_table(thd, new_table, 1, 1);
   }
   else
-    VOID(quick_rm_table(new_db_type, new_db, tmp_name, FN_IS_TMP));
+    VOID(quick_rm_table(new_db_type, new_db, tmp_name,
+                        create_info->frm_only
+                        ? FN_IS_TMP | FRM_ONLY
+                        : FN_IS_TMP));
 
 err:
   /*
Thread
bk commit into 5.1 tree (mskold:1.2649) BUG#31231Martin Skold27 Feb
  • Re: bk commit into 5.1 tree (mskold:1.2649) BUG#31231Ingo Strüwing7 Mar