List:Commits« Previous MessageNext Message »
From:ahristov Date:November 27 2006 12:28pm
Subject:bk commit into 4.1 tree (andrey:1.2611) BUG#24395
View as plain text  
Below is the list of changes that have just been committed into a local
4.1 repository of andrey. When andrey 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, 2006-11-27 12:28:44+01:00, andrey@stripped +6 -0
  Fix for bug#24395:
  ALTER TABLE DISABLE KEYS doesn't work when modifying the table
  
  ENABLE|DISABLE KEYS combined with another ALTER TABLE option, different
  than RENAME TO did nothing. Also, if the table had disabled keys
  and was ALTER-ed then the end table was with enabled keys.
  
  Fixed by checking whether the table had disabled keys and enabling them
  in the copied table.

  myisam/mi_open.c@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +21 -4
    Extend mi_indexes_are_disabled to implement return value
    2 - Non-unique indexes are disabled

  mysql-test/r/alter_table.result@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +121
-0
    update test

  mysql-test/t/alter_table.test@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +97 -0
    add test for bug #24395:
    ALTER TABLE DISABLE KEYS doesn't work when modifying the table

  sql/mysql_priv.h@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +1 -0
    export at_least_one_non_unique_index_exists()

  sql/sql_table.cc@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +47 -0
    When ENABLE|DISABLE index is combined with another option
    different than RENAME TO, we should ENABLE|DISABLE the keys of
    the modified table. Also when modifying we should preserve the
    previous state of the indices.
    (This problem exists in 5.0 and 5.1 but since the codebase has
     diverged, this fix won't automerge, but the fix will be quite
     similar).

  sql/table.cc@stripped, 2006-11-27 12:28:40+01:00, andrey@stripped +29 -0
    Add a helper function for checking whether there is
    at least one unique index in a table.

# This is a BitKeeper patch.  What follows are the unified diffs for the
# set of deltas contained in the patch.  The rest of the patch, the part
# that BitKeeper cares about, is below these diffs.
# User:	andrey
# Host:	example.com
# Root:	/work/bug24395/my41

--- 1.85/myisam/mi_open.c	2006-11-27 12:28:50 +01:00
+++ 1.86/myisam/mi_open.c	2006-11-27 12:28:50 +01:00
@@ -1228,15 +1228,32 @@ int mi_enable_indexes(MI_INFO *info)
     Test if indexes are disabled.
 
   RETURN
-    0  indexes are not disabled
-    1  all indexes are disabled
-   [2  non-unique indexes are disabled - NOT YET IMPLEMENTED]
+    0  Indexes are not disabled
+    1  All indexes are disabled
+    2  Non-unique indexes are disabled
 */
 
 int mi_indexes_are_disabled(MI_INFO *info)
 {
   MYISAM_SHARE *share= info->s;
 
-  return (! share->state.key_map && share->base.keys);
+  /*
+    No keys or all are enabled. keys is the number of keys. Left shifted
+    gives us only one bit set. When decreased by one, gives us all all bits
+    up to this one set and it gets unset.
+  */
+  if (!share->base.keys ||
+      (share->state.key_map == (ULL(1) << share->base.keys) - ULL(1)))
+    return 0;
+
+  /* All are disabled */
+  if (!share->state.key_map)
+    return 1;
+
+  /*
+    We have keys. Some enabled, some disabled.
+    Don't check for any non-unique disabled but return directly 2
+  */
+  return 2;
 }
 

--- 1.382/sql/mysql_priv.h	2006-11-27 12:28:50 +01:00
+++ 1.383/sql/mysql_priv.h	2006-11-27 12:28:50 +01:00
@@ -1157,6 +1157,7 @@ void update_create_info_from_table(HA_CR
 int rename_file_ext(const char * from,const char * to,const char * ext);
 bool check_db_name(char *db);
 bool check_column_name(const char *name);
+bool at_least_one_non_unique_index_exists(const TABLE * const table);
 bool check_table_name(const char *name, uint length);
 char *get_field(MEM_ROOT *mem, Field *field);
 bool get_field(MEM_ROOT *mem, Field *field, class String *res);

--- 1.309/sql/sql_table.cc	2006-11-27 12:28:50 +01:00
+++ 1.310/sql/sql_table.cc	2006-11-27 12:28:50 +01:00
@@ -3356,6 +3356,52 @@ int mysql_alter_table(THD *thd,char *new
     goto err;
   }
 
+  switch (alter_info->keys_onoff) {
+  case ENABLE:
+    /*
+      In 4.1 we always create a copy table. Therefore all keys are enabled
+      implicitly.
+    */
+    break;
+  case LEAVE_AS_IS:
+  {
+    /*
+      The indexes of a table could have been disabled. Thus we have to
+      check for this and if it is the case to disable them because new_table
+      is a new table which when created gets it's indexes enabled implicitly.
+    */
+    bool indexes_were_disabled= table->file->indexes_are_disabled();
+    /*
+      When all indexes are disabled and there is 1+ unique indexes, or
+      there are unique indexes disabled.
+    */
+    if ((indexes_were_disabled == 1 &&
+         at_least_one_non_unique_index_exists(new_table)) ||
+        indexes_were_disabled == 2)
+      /* fall-through: disable indexes */;
+    else
+      break;
+  }
+  case DISABLE:
+    /*
+      We need to take a lock, because we have created a table which hasn't
+      been locked. enable_indexes() and disable_indexes() of myisam/heap will
+      return an error if we haven't locked the table.
+    */
+    new_table->file->external_lock(thd, F_WRLCK);
+    error= new_table->file->disable_indexes(HA_KEY_SWITCH_NONUNIQ_SAVE);
+    if (error == HA_ERR_WRONG_COMMAND)
+    {
+      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE, ER_ILLEGAL_HA,
+                          ER(ER_ILLEGAL_HA), table->table_name);
+      error= 0;
+    }
+    if (error)
+    {
+      new_table->file->print_error(error, MYF(0));
+      goto skip_copy;
+    }
+  }
 
   /* We don't want update TIMESTAMP fields during ALTER TABLE. */
   new_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
@@ -3372,6 +3418,7 @@ int mysql_alter_table(THD *thd,char *new
   thd->last_insert_id=next_insert_id;		// Needed for correct log
   thd->count_cuted_fields= CHECK_FIELD_IGNORE;
 
+skip_copy:
   if (table->tmp_table)
   {
     /* We changed a temporary table */

--- 1.140/sql/table.cc	2006-11-27 12:28:50 +01:00
+++ 1.141/sql/table.cc	2006-11-27 12:28:50 +01:00
@@ -1551,6 +1551,35 @@ db_type get_table_type(const char *name)
   DBUG_RETURN(ha_checktype((enum db_type) (uint) *(head+3)));
 }
 
+
+/*
+  Finds whether the table has at least one non-unique index
+
+  SYNOPSIS
+    at_least_one_non_unique_index_exists()
+      table  Opened table
+
+  RETURN VALUES
+    FALSE  No
+    TRUE   Yes
+*/
+
+bool at_least_one_non_unique_index_exists(const TABLE * const table)
+{
+  KEY *key_info, *key_info_end;
+  DBUG_ENTER("at_least_one_non_unique_index_exists");
+
+  key_info_end= (key_info= table->key_info) + table->keys;
+
+  for (;key_info != key_info_end ; key_info++)
+  {
+    if (!(key_info->flags & HA_NOSAME))
+      DBUG_RETURN(TRUE);
+  }
+  DBUG_RETURN(FALSE);
+}
+
+
 /*
   Cleanup this table for re-execution.
 

--- 1.49/mysql-test/r/alter_table.result	2006-11-27 12:28:50 +01:00
+++ 1.50/mysql-test/r/alter_table.result	2006-11-27 12:28:50 +01:00
@@ -528,6 +528,127 @@ create table t1 ( a timestamp );
 alter table t1 add unique ( a(1) );
 ERROR HY000: Incorrect sub part key; the used key part isn't a string, the used length is
longer than the key part, or the storage engine doesn't support unique sub keys
 drop table t1;
+drop table if exists t1;
+create table t1 (a int, key(a));
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+"this used not to disable the index"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	disabled
+alter table t1 enable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	disabled
+alter table t1 enable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 add b char(10), disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	disabled
+alter table t1 add c decimal(10,2), enable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+"this however did"
+alter table t1 disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	disabled
+desc t1;
+Field	Type	Null	Key	Default	Extra
+a	bigint(20)	YES	MUL	NULL	
+b	char(10)	YES		NULL	
+c	decimal(10,2)	YES		NULL	
+alter table t1 add d decimal(15,5);
+"The key should still be disabled"
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	1	a	1	a	A	NULL	NULL	NULL	YES	BTREE	disabled
+drop table t1;
+"Now will test with one unique index"
+create table t1(a int, b char(10), unique(a));
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 enable keys;
+"If no copy on noop change, this won't touch the data file"
+"Unique index, no change"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+"Change the type implying data copy"
+"Unique index, no change"
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 modify a bigint;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 modify a int;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+drop table t1;
+"Now will test with one unique and one non-unique index"
+create table t1(a int, b char(10), unique(a), key(b));
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	
+alter table t1 disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	disabled
+alter table t1 enable keys;
+"If no copy on noop change, this won't touch the data file"
+"The non-unique index will be disabled"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	disabled
+alter table t1 enable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	
+"Change the type implying data copy"
+"The non-unique index will be disabled"
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	disabled
+"Change again the type, but leave the indexes as_is"
+alter table t1 modify a int;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	disabled
+"Try the same. When data is no copied on similar tables, this is noop"
+alter table t1 modify a int;
+show indexes from t1;
+Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
+t1	0	a	1	a	A	NULL	NULL	NULL	YES	BTREE	
+t1	1	b	1	b	A	NULL	NULL	NULL	YES	BTREE	disabled
+drop table t1;
 create database mysqltest1;
 create table t1 (c1 int);
 alter table t1 rename mysqltest1.t1;

--- 1.39/mysql-test/t/alter_table.test	2006-11-27 12:28:50 +01:00
+++ 1.40/mysql-test/t/alter_table.test	2006-11-27 12:28:50 +01:00
@@ -362,6 +362,103 @@ alter table t1 add unique ( a(1) );
 drop table t1;
 
 #
+# Bug #24395: ALTER TABLE DISABLE KEYS doesn't work when modifying the table
+#
+# This problem happens if the data change is compatible.
+# Changing to the same type is compatible for example.
+#
+--disable_warnings
+drop table if exists t1;
+--enable_warnings
+create table t1 (a int, key(a));
+show indexes from t1;
+--echo "this used not to disable the index"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+
+alter table t1 enable keys;
+show indexes from t1;
+
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+
+alter table t1 enable keys;
+show indexes from t1;
+
+alter table t1 add b char(10), disable keys;
+show indexes from t1;
+
+alter table t1 add c decimal(10,2), enable keys;
+show indexes from t1;
+
+--echo "this however did"
+alter table t1 disable keys;
+show indexes from t1;
+
+desc t1;
+
+alter table t1 add d decimal(15,5);
+--echo "The key should still be disabled"
+show indexes from t1;
+
+drop table t1;
+
+--echo "Now will test with one unique index"
+create table t1(a int, b char(10), unique(a));
+show indexes from t1;
+alter table t1 disable keys;
+show indexes from t1;
+alter table t1 enable keys;
+
+--echo "If no copy on noop change, this won't touch the data file"
+--echo "Unique index, no change"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+
+--echo "Change the type implying data copy"
+--echo "Unique index, no change"
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+
+alter table t1 modify a bigint;
+show indexes from t1;
+
+alter table t1 modify a int;
+show indexes from t1;
+
+drop table t1;
+
+--echo "Now will test with one unique and one non-unique index"
+create table t1(a int, b char(10), unique(a), key(b));
+show indexes from t1;
+alter table t1 disable keys;
+show indexes from t1;
+alter table t1 enable keys;
+
+
+--echo "If no copy on noop change, this won't touch the data file"
+--echo "The non-unique index will be disabled"
+alter table t1 modify a int, disable keys;
+show indexes from t1;
+alter table t1 enable keys;
+show indexes from t1;
+
+--echo "Change the type implying data copy"
+--echo "The non-unique index will be disabled"
+alter table t1 modify a bigint, disable keys;
+show indexes from t1;
+
+--echo "Change again the type, but leave the indexes as_is"
+alter table t1 modify a int;
+show indexes from t1;
+--echo "Try the same. When data is no copied on similar tables, this is noop"
+alter table t1 modify a int;
+show indexes from t1;
+
+drop table t1;
+
+
+#
 # Bug#11493 - Alter table rename to default database does not work without
 #             db name qualifying
 #
Thread
bk commit into 4.1 tree (andrey:1.2611) BUG#24395ahristov27 Nov