MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Konstantin Osipov Date:February 15 2010 2:19pm
Subject:bzr commit into mysql-5.5-next-mr branch (kostja:3100) Bug#47648
View as plain text  
#At file:///opt/local/work/next-4284/ based on revid:dlenev@stripped

 3100 Konstantin Osipov	2010-02-15
      A fix and a test case for Bug#47648 "main.merge fails sporadically".
      If a prepared statement used both a MyISAMMRG table and a stored 
      function or trigger, execution could fail with "No such table"
      error. 
      The error would come from a failure of the MyISAMMRG engine
      to meet the expectations of the prelocking algorithm, 
      in particular maintain lex->query_tables_own_last pointer
      in sync with lex->query_tables_last pointer/the contents
      of lex->query_tables. When adding merge children, the merge
      engine would extend the table list. Then, when adding 
      prelocked tables, the prelocking algorithm would use a pointer
      to the last merge child to assign to lex->query_tables_own_last.
      Then, when merge children were removed at the end of
      open_tables(), lex->query_tables_own_last
      was not updated, and kept pointing
      to a removed merge child.
      
      The fix ensures that query_tables_own_last is always in
      sync with lex->query_talbes_last.
     @ mysql-test/r/merge.result
        Update results (Bug#47648).
     @ mysql-test/t/merge.test
        Add a test case for Bug#47648.
        Update the result file to reflect a fix of another bug
        in MyISAMMRG code: not maintaining lex->query_tables_own_last
        allowed a stored function or trigger to modify a merge table
        which was already updated by the main statement.
        It is not allowed for other storage engines, and should
        not be allowed for MyISAMMRG.
     @ storage/myisammrg/ha_myisammrg.cc
        When adding children to the list of tables to open,
        make sure that we properly set lex->query_tables_own_last.
        When removing the children, update lex->query_tables_own_last
        if necessary.

    modified:
      mysql-test/r/merge.result
      mysql-test/t/merge.test
      storage/myisammrg/ha_myisammrg.cc
=== modified file 'mysql-test/r/merge.result'
--- a/mysql-test/r/merge.result	2009-12-11 12:24:23 +0000
+++ b/mysql-test/r/merge.result	2010-02-15 14:19:39 +0000
@@ -2475,19 +2475,18 @@ CREATE TRIGGER tm1_ai AFTER INSERT ON tm
 FOR EACH ROW INSERT INTO t1 VALUES(11);
 LOCK TABLE tm1 WRITE, t1 WRITE;
 INSERT INTO tm1 VALUES (1);
+ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
 SELECT * FROM tm1;
 c1
 1
-11
 UNLOCK TABLES;
 LOCK TABLE t1 WRITE, tm1 WRITE;
 INSERT INTO tm1 VALUES (1);
+ERROR HY000: Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
 SELECT * FROM tm1;
 c1
 1
-11
 1
-11
 UNLOCK TABLES;
 DROP TRIGGER tm1_ai;
 DROP TABLE tm1, t1;
@@ -2512,12 +2511,12 @@ UPDATE t2, tm1 SET t2.c1=f1();
 FLUSH TABLES;
 FLUSH TABLES;
 UNLOCK TABLES;
+ERROR HY000: Can't update table 't3' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
 SELECT * FROM tm1;
 c1
 1
 4
 3
-33
 4
 5
 DROP TRIGGER t2_au;
@@ -2553,4 +2552,29 @@ ERROR HY000: Unable to open underlying t
 DROP TABLE t1, t3;
 # Connection default.
 # Disconnecting con1, all mdl_tickets must have been released.
+#
+# A test case for Bug#47648   main.merge fails sporadically
+#
+# Make sure we correctly maintain lex->query_tables_last_own.
+#
+create table t1 (c1 int not null);
+create table t2 (c1 int not null);
+create table t3 (c1 int not null);
+create function f1 () returns int return (select max(c1) from t3);
+create table t4 (c1 int not null) engine=merge union=(t1,t2) insert_method=last ;
+select * from t4 where c1 < f1();
+c1
+prepare stmt from "select * from t4 where c1 < f1()";
+execute stmt;
+c1
+execute stmt;
+c1
+execute stmt;
+c1
+drop function f1;
+execute stmt;
+ERROR 42000: FUNCTION test.f1 does not exist
+execute stmt;
+ERROR 42000: FUNCTION test.f1 does not exist
+drop table t4, t3, t2, t1;
 End of 6.0 tests

=== modified file 'mysql-test/t/merge.test'
--- a/mysql-test/t/merge.test	2009-12-11 12:24:23 +0000
+++ b/mysql-test/t/merge.test	2010-02-15 14:19:39 +0000
@@ -1949,10 +1949,12 @@ CREATE TABLE tm1 (c1 INT) ENGINE=MRG_MYI
 CREATE TRIGGER tm1_ai AFTER INSERT ON tm1
   FOR EACH ROW INSERT INTO t1 VALUES(11);
 LOCK TABLE tm1 WRITE, t1 WRITE;
+--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
 INSERT INTO tm1 VALUES (1);
 SELECT * FROM tm1;
 UNLOCK TABLES;
 LOCK TABLE t1 WRITE, tm1 WRITE;
+--error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
 INSERT INTO tm1 VALUES (1);
 SELECT * FROM tm1;
 UNLOCK TABLES;
@@ -1990,6 +1992,7 @@ FLUSH TABLES;
 #sleep 1;
 UNLOCK TABLES;
     connection con1;
+    --error ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG
     reap;
     disconnect con1;
 connection default;
@@ -2037,4 +2040,30 @@ connection default;
 --echo # Disconnecting con1, all mdl_tickets must have been released.
 disconnect con1;
 
+--echo #
+--echo # A test case for Bug#47648   main.merge fails sporadically
+--echo #
+--echo # Make sure we correctly maintain lex->query_tables_last_own.
+--echo #
+create table t1 (c1 int not null);
+create table t2 (c1 int not null);
+create table t3 (c1 int not null);
+
+create function f1 () returns int return (select max(c1) from t3);
+
+create table t4 (c1 int not null) engine=merge union=(t1,t2) insert_method=last ;
+
+select * from t4 where c1 < f1();
+prepare stmt from "select * from t4 where c1 < f1()";
+execute stmt;
+execute stmt;
+execute stmt;
+drop function f1;
+--error ER_SP_DOES_NOT_EXIST
+execute stmt;
+--error ER_SP_DOES_NOT_EXIST
+execute stmt;
+drop table t4, t3, t2, t1;
+
 --echo End of 6.0 tests
+

=== modified file 'storage/myisammrg/ha_myisammrg.cc'
--- a/storage/myisammrg/ha_myisammrg.cc	2010-02-06 10:28:06 +0000
+++ b/storage/myisammrg/ha_myisammrg.cc	2010-02-15 14:19:39 +0000
@@ -458,6 +458,16 @@ int ha_myisammrg::add_children_list(void
   */
   if (thd->lex->query_tables_last == &parent_l->next_global)
     thd->lex->query_tables_last= this->children_last_l;
+  /*
+    The branch below works only when re-executing a prepared
+    statement or a stored procedure statement:
+    We've just modified query_tables_last. Keep it in sync with
+    query_tables_last_own, if it was set by the prelocking code.
+    This currently is not used anywhere, but done for the sake of
+    symmetry with detach_children_list().
+  */
+  if (thd->lex->query_tables_own_last == &parent_l->next_global)
+    thd->lex->query_tables_own_last= this->children_last_l;
 
 end:
   DBUG_RETURN(0);
@@ -888,6 +898,15 @@ int ha_myisammrg::detach_children(void)
     if (thd->lex->query_tables_last == this->children_last_l)
       thd->lex->query_tables_last= this->children_l->prev_global;
 
+    /*
+      If the statement requires prelocking, and prelocked
+      tables were added right after merge children, modify the
+      last own table pointer to point at prev_global of the merge
+      parent.
+    */
+    if (thd->lex->query_tables_own_last == this->children_last_l)
+      thd->lex->query_tables_own_last= this->children_l->prev_global;
+
     /* Terminate child list. So it cannot be tried to remove again. */
     *this->children_last_l= NULL;
     this->children_l->prev_global= NULL;


Attachment: [text/bzr-bundle] bzr/kostja@sun.com-20100215141939-6r0ckd8pnte2qggu.bundle
Thread
bzr commit into mysql-5.5-next-mr branch (kostja:3100) Bug#47648Konstantin Osipov15 Feb