List:Commits« Previous MessageNext Message »
From:Sergey Vojtovich Date:November 23 2006 4:50pm
Subject:bk commit into 4.1 tree (svoj:1.2534) BUG#21310
View as plain text  
Below is the list of changes that have just been committed into a local
4.1 repository of svoj. When svoj 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-23 19:50:37+04:00, svoj@stripped +3 -0
  BUG#21310 - Trees in SQL causing a "crashed" table with MyISAM storage engine
  
  An update that used a join of a table to itself and modified the
  table on one side of the join reported the table as crashed or
  updated wrong rows.
  
  Fixed by creating temporary table for self-joined multi update statement.

  mysql-test/r/myisam.result@stripped, 2006-11-23 19:50:35+04:00, svoj@stripped +16 -0
    A test case for BUG#21310.

  mysql-test/t/myisam.test@stripped, 2006-11-23 19:50:36+04:00, svoj@stripped +19 -0
    A test case for BUG#21310.

  sql/sql_update.cc@stripped, 2006-11-23 19:50:36+04:00, svoj@stripped +11 -23
    Disabling record cache for self-joined multi update statement is wrong.
    The join must only see the table as it was at the beginning of the statement.
    safe_update_on_fly check if it is safe to update first table on the fly, that is
    not creating temporary table. It is possible in case a row from this table is
    never read more than once. safe_update_on_fly now detect self-joined table and
    refuse to update this table on the fly.

# 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:	svoj
# Host:	april.(none)
# Root:	/home/svoj/devel/mysql/BUG21310/mysql-4.1-engines

--- 1.157/sql/sql_update.cc	2006-11-23 19:50:41 +04:00
+++ 1.158/sql/sql_update.cc	2006-11-23 19:50:41 +04:00
@@ -23,7 +23,8 @@
 #include "mysql_priv.h"
 #include "sql_select.h"
 
-static bool safe_update_on_fly(JOIN_TAB *join_tab, List<Item> *fields);
+static bool safe_update_on_fly(THD *thd, JOIN_TAB *join_tab,
+                               TABLE_LIST *update_tables, List<Item> *fields);
 
 /* Return 0 if row hasn't changed */
 
@@ -846,26 +847,6 @@ int multi_update::prepare(List<Item> &no
   for (i=0 ; i < table_count ; i++)
     set_if_bigger(max_fields, fields_for_table[i]->elements);
   copy_field= new Copy_field[max_fields];
-
-  /*
-    Mark all copies of tables that are updates to ensure that
-    init_read_record() will not try to enable a cache on them
-
-    The problem is that for queries like
-
-    UPDATE t1, t1 AS t2 SET t1.b=t2.c WHERE t1.a=t2.a;
-
-    the row buffer may contain things that doesn't match what is on disk
-    which will cause an error when reading a row.
-    (This issue is mostly relevent for MyISAM tables)
-  */
-  for (table_ref= all_tables;  table_ref; table_ref=table_ref->next)
-  {
-    TABLE *table=table_ref->table;
-    if ((tables_to_update & table->map) && 
-	mysql_lock_have_duplicate(thd, table, update_tables))
-      table->no_cache= 1;			// Disable row cache
-  }
   DBUG_RETURN(thd->is_fatal_error != 0);
 }
 
@@ -905,7 +886,7 @@ multi_update::initialize_tables(JOIN *jo
       table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
     if (table == main_table)			// First table in join
     {
-      if (safe_update_on_fly(join->join_tab, &temp_fields))
+      if (safe_update_on_fly(thd, join->join_tab, update_tables, &temp_fields))
       {
 	table_to_update= main_table;		// Update table on the fly
 	continue;
@@ -957,6 +938,7 @@ multi_update::initialize_tables(JOIN *jo
   SYNOPSIS
     safe_update_on_fly
     join_tab		How table is used in join
+    update_tables       List of tables that will be updated
     fields		Fields that are updated
 
   NOTES
@@ -970,6 +952,8 @@ multi_update::initialize_tables(JOIN *jo
     - We are doing a range scan and we don't update the scan key or
       the primary key for a clustered table handler.
 
+    - Table is not joined to itself.
+
   WARNING
     This code is a bit dependent of how make_join_readinfo() works.
 
@@ -978,9 +962,13 @@ multi_update::initialize_tables(JOIN *jo
     1		Safe to update
 */
 
-static bool safe_update_on_fly(JOIN_TAB *join_tab, List<Item> *fields)
+static bool safe_update_on_fly(THD *thd, JOIN_TAB *join_tab,
+                               TABLE_LIST *update_tables, List<Item> *fields)
 {
   TABLE *table= join_tab->table;
+  /* First check if a table is not joined to itself. */
+  if (mysql_lock_have_duplicate(thd, table, update_tables))
+    return 0;
   switch (join_tab->type) {
   case JT_SYSTEM:
   case JT_CONST:

--- 1.68/mysql-test/r/myisam.result	2006-11-23 19:50:41 +04:00
+++ 1.69/mysql-test/r/myisam.result	2006-11-23 19:50:41 +04:00
@@ -944,3 +944,19 @@ select * from t1;
 a
 42
 drop table t1;
+CREATE TABLE t1(a VARCHAR(16));
+INSERT INTO t1 VALUES('aaaaaaaa'),(NULL);
+UPDATE t1 AS ta1, t1 AS ta2 SET ta1.a='aaaaaaaaaaaaaaaa';
+SELECT * FROM t1;
+a
+aaaaaaaaaaaaaaaa
+aaaaaaaaaaaaaaaa
+DROP TABLE t1;
+CREATE TABLE t1(a INT);
+INSERT INTO t1 VALUES(1),(2);
+UPDATE t1,t1 AS t2 SET t1.a=t1.a+2 WHERE t1.a=t2.a-1;
+SELECT * FROM t1 ORDER BY a;
+a
+2
+3
+DROP TABLE t1;

--- 1.54/mysql-test/t/myisam.test	2006-11-23 19:50:41 +04:00
+++ 1.55/mysql-test/t/myisam.test	2006-11-23 19:50:41 +04:00
@@ -890,4 +890,23 @@ connection default;
 select * from t1;
 drop table t1;
 
+#
+# BUG#21310 - Trees in SQL causing a "crashed" table with MyISAM storage
+#             engine
+#
+
+# A simplified test case that reflect crashed table issue.
+CREATE TABLE t1(a VARCHAR(16));
+INSERT INTO t1 VALUES('aaaaaaaa'),(NULL);
+UPDATE t1 AS ta1, t1 AS ta2 SET ta1.a='aaaaaaaaaaaaaaaa';
+SELECT * FROM t1;
+DROP TABLE t1;
+
+# A test case that reflect wrong result set.
+CREATE TABLE t1(a INT);
+INSERT INTO t1 VALUES(1),(2);
+UPDATE t1,t1 AS t2 SET t1.a=t1.a+2 WHERE t1.a=t2.a-1;
+SELECT * FROM t1 ORDER BY a;
+DROP TABLE t1;
+
 # End of 4.1 tests
Thread
bk commit into 4.1 tree (svoj:1.2534) BUG#21310Sergey Vojtovich23 Nov