MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Patrick Galbraith Date:June 22 2006 6:39am
Subject:bk commit into 5.0 tree (patg:1.2196) BUG#19773
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of patg. When patg 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
  1.2196 06/06/21 23:39:05 patg@stripped +4 -0
  BUG #19773
  
  Post-review fixes per Monty. Please see each file's comments.

  sql/ha_federated.h
    1.25 06/06/21 23:39:01 patg@stripped +8 -6
    BUG #19773
    
    Fixed "SET " to " SET " to make sure built statements are built with 
    "UPDATE `t1` SET .." instead of "UPDATE `t1`SET"
    

  sql/ha_federated.cc
    1.61 06/06/21 23:39:01 patg@stripped +182 -134
    BUG #19773 
    
    Post-review changes, per Monty
    
    - Hash by entire connection scheme
    - Separated result set for table scan and index scan
    - Rewrote ::position and ::rnd_pos to store position - if primary key use 
      primary key, if not, use record buffer.
    - Rewrote get_share to store hash with connect string
    - delete_row added subtration of "records" by affected->rows
    - Added read_next to handle what rnd_next used to do (converting raw record
      to query and vice versa)
    - Removed many DBUG_PRINT lines
    - Removed memset initialisation since subsequent loop accomplishes
    - Removed un-necessary mysql_free_result lines

  mysql-test/t/federated.test
    1.23 06/06/21 23:39:01 patg@stripped +51 -0
    BUG #19773
    
    Test multi table update and delete. Added drop table to end of previous test.

  mysql-test/r/federated.result
    1.27 06/06/21 23:39:01 patg@stripped +88 -0
    BUG #19773
    
    Results for multi-table deletes, updates

# 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:	patg
# Host:	govinda.patg.net
# Root:	/home/patg/mysql-build/mysql-5.0

--- 1.26/mysql-test/r/federated.result	2006-02-28 02:17:35 -08:00
+++ 1.27/mysql-test/r/federated.result	2006-06-21 23:39:01 -07:00
@@ -1558,6 +1558,8 @@
 3
 4
 5
+DROP TABLE federated.t1;
+DROP TABLE federated.t1;
 DROP TABLE IF EXISTS federated.bug_17377_table;
 CREATE TABLE federated.bug_17377_table (
 `fld_cid` bigint(20) NOT NULL auto_increment,
@@ -1601,6 +1603,92 @@
 5	Torkel	0	0
 DROP TABLE federated.t1;
 DROP TABLE federated.bug_17377_table;
+create table federated.t1 (i1 int, i2 int, i3 int);
+create table federated.t2 (id int, c1 varchar(20), c2 varchar(20));
+create table federated.t1 (i1 int, i2 int, i3 int) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:9308/federated/t1';
+create table federated.t2 (id int, c1 varchar(20), c2 varchar(20)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:9308/federated/t2';
+insert into federated.t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
+insert into federated.t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
+select * from federated.t1 order by i1;
+i1	i2	i3
+1	5	10
+2	2	2
+3	7	12
+4	5	2
+9	10	15
+select * from federated.t2;
+id	c1	c2
+9	abc	def
+5	opq	lmn
+2	test t	t test
+update federated.t1,federated.t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
+select * from federated.t1 order by i1;
+i1	i2	i3
+1	5	10
+2	15	2
+3	7	12
+4	5	2
+9	15	15
+select * from federated.t2 order by id;
+id	c1	c2
+2	test t	ppc
+5	opq	lmn
+9	abc	ppc
+delete   federated.t1.*,federated.t2.* from federated.t1,federated.t2 where t1.i2=t2.id;
+select * from federated.t1 order by i1;
+i1	i2	i3
+2	15	2
+3	7	12
+9	15	15
+select * from federated.t2 order by id;
+id	c1	c2
+2	test t	ppc
+9	abc	ppc
+drop table federated.t1, federated.t2;
+drop table federated.t1, federated.t2;
+create table federated.t1 (i1 int, i2 int, i3 int, primary key (i1));
+create table federated.t2 (id int, c1 varchar(20), c2 varchar(20), primary key (id));
+create table federated.t1 (i1 int auto_increment not null, i2 int, i3 int, primary key (i1)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:9308/federated/t1';
+create table federated.t2 (id int auto_increment not null, c1 varchar(20), c2 varchar(20), primary key(id)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:9308/federated/t2';
+insert into federated.t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
+insert into federated.t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
+select * from federated.t1 order by i1;
+i1	i2	i3
+1	5	10
+2	2	2
+3	7	12
+4	5	2
+9	10	15
+select * from federated.t2 order by id;
+id	c1	c2
+2	test t	t test
+5	opq	lmn
+9	abc	def
+update federated.t1,federated.t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
+select * from federated.t1 order by i1;
+i1	i2	i3
+1	5	10
+2	15	2
+3	7	12
+4	5	2
+9	15	15
+select * from federated.t2 order by id;
+id	c1	c2
+2	test t	ppc
+5	opq	lmn
+9	abc	ppc
+delete federated.t1.*,federated.t2.* from federated.t1,federated.t2 where t1.i2=t2.id;
+select * from federated.t1 order by i1;
+i1	i2	i3
+2	15	2
+3	7	12
+9	15	15
+select * from federated.t2 order by id;
+id	c1	c2
+2	test t	ppc
+9	abc	ppc
+drop table federated.t1, federated.t2;
+drop table federated.t1, federated.t2;
 DROP TABLE IF EXISTS federated.t1;
 DROP DATABASE IF EXISTS federated;
 DROP TABLE IF EXISTS federated.t1;

--- 1.22/mysql-test/t/federated.test	2006-02-28 02:17:35 -08:00
+++ 1.23/mysql-test/t/federated.test	2006-06-21 23:39:01 -07:00
@@ -1254,6 +1254,10 @@
 INSERT INTO federated.t1 VALUES ();
 SELECT LAST_INSERT_ID();
 SELECT * FROM federated.t1;
+DROP TABLE federated.t1;
+
+connection slave;
+DROP TABLE federated.t1;
 
 #
 # Bug#17377 Federated Engine returns wrong Data, always the rows
@@ -1309,5 +1313,52 @@
 connection slave;
 DROP TABLE federated.bug_17377_table;
 
+#
+# BUG 19773 Crash when using multi-table updates, deletes
+# with federated tables
+#
+connection slave;
+create table federated.t1 (i1 int, i2 int, i3 int);
+create table federated.t2 (id int, c1 varchar(20), c2 varchar(20));
+
+connection master;
+eval create table federated.t1 (i1 int, i2 int, i3 int) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:$SLAVE_MYPORT/federated/t1';
+eval create table federated.t2 (id int, c1 varchar(20), c2 varchar(20)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:$SLAVE_MYPORT/federated/t2';
+insert into federated.t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
+insert into federated.t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
+select * from federated.t1 order by i1;
+select * from federated.t2;
+update federated.t1,federated.t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
+select * from federated.t1 order by i1;
+select * from federated.t2 order by id;
+delete   federated.t1.*,federated.t2.* from federated.t1,federated.t2 where t1.i2=t2.id;
+select * from federated.t1 order by i1;
+select * from federated.t2 order by id;
+drop table federated.t1, federated.t2;
+connection slave;
+drop table federated.t1, federated.t2;
+
+# Test multi updates and deletes with keys
+connection slave;
+create table federated.t1 (i1 int, i2 int, i3 int, primary key (i1));
+create table federated.t2 (id int, c1 varchar(20), c2 varchar(20), primary key (id));
+
+connection master;
+eval create table federated.t1 (i1 int auto_increment not null, i2 int, i3 int, primary key (i1)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:$SLAVE_MYPORT/federated/t1';
+eval create table federated.t2 (id int auto_increment not null, c1 varchar(20), c2 varchar(20), primary key(id)) ENGINE=FEDERATED CONNECTION='mysql://root@stripped:$SLAVE_MYPORT/federated/t2';
+insert into federated.t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
+insert into federated.t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
+select * from federated.t1 order by i1;
+select * from federated.t2 order by id;
+update federated.t1,federated.t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
+select * from federated.t1 order by i1;
+select * from federated.t2 order by id;
+delete federated.t1.*,federated.t2.* from federated.t1,federated.t2 where t1.i2=t2.id;
+select * from federated.t1 order by i1;
+select * from federated.t2 order by id;
+drop table federated.t1, federated.t2;
+
+connection slave;
+drop table federated.t1, federated.t2;
 
 source include/federated_cleanup.inc;

--- 1.60/sql/ha_federated.cc	2006-02-28 02:17:35 -08:00
+++ 1.61/sql/ha_federated.cc	2006-06-21 23:39:01 -07:00
@@ -395,8 +395,8 @@
 static byte *federated_get_key(FEDERATED_SHARE *share, uint *length,
                                my_bool not_used __attribute__ ((unused)))
 {
-  *length= share->table_name_length;
-  return (byte*) share->table_name;
+  *length= share->connect_string_length;
+  return (byte*) share->scheme;
 }
 
 /*
@@ -416,7 +416,7 @@
   DBUG_ENTER("federated_db_init");
   if (pthread_mutex_init(&federated_mutex, MY_MUTEX_INIT_FAST))
     goto error;
-  if (hash_init(&federated_open_tables, system_charset_info, 32, 0, 0,
+  if (hash_init(&federated_open_tables, &my_charset_bin, 32, 0, 0,
                     (hash_get_key) federated_get_key, 0, 0))
   {
     VOID(pthread_mutex_destroy(&federated_mutex));
@@ -513,6 +513,7 @@
   }
   else
   {
+    int escaped_table_name_length= 0;
     /*
       Since we do not support transactions at this version, we can let the 
       client API silently reconnect. For future versions, we will need more 
@@ -531,17 +532,18 @@
     query.append(FEDERATED_STAR);
     query.append(FEDERATED_FROM);
     query.append(FEDERATED_BTICK);
-    escape_string_for_mysql(&my_charset_bin, (char *)escaped_table_name,
+    escaped_table_name_length=
+      escape_string_for_mysql(&my_charset_bin, (char*)escaped_table_name,
                             sizeof(escaped_table_name),
                             share->table_name,
                             share->table_name_length);
-    query.append(escaped_table_name);
+    query.append(escaped_table_name, escaped_table_name_length);
     query.append(FEDERATED_BTICK);
     query.append(FEDERATED_WHERE);
     query.append(FEDERATED_FALSE);
 
-    DBUG_PRINT("info", ("check_foreign_data_source query %s", 
-                        query.c_ptr_quick()));
+    DBUG_PRINT("info", ("ha_federated SQL QUERY query '%s'",
+                        query.c_ptr_safe()));
     if (mysql_real_query(mysql, query.ptr(), query.length()))
     {
       error_code= table_create_flag ?
@@ -637,8 +639,7 @@
                                        table->s->connect_string.length,
                                        MYF(0));
 
-  // Add a null for later termination of table name
-  share->scheme[table->s->connect_string.length]= 0;
+  share->connect_string_length= table->s->connect_string.length;
   DBUG_PRINT("info",("parse_url alloced share->scheme %lx", share->scheme));
 
   /*
@@ -704,7 +705,7 @@
   share->table_name++;
 
   share->table_name_length= strlen(share->table_name);
-      
+ 
   /* make sure there's not an extra / */
   if ((strchr(share->table_name, '/')))
     goto error;
@@ -740,8 +741,7 @@
 
 ha_federated::ha_federated(TABLE *table_arg)
   :handler(&federated_hton, table_arg),
-  mysql(0), stored_result(0),
-  ref_length(sizeof(MYSQL_ROW_OFFSET)), current_position(0)
+  mysql(0), table_scan_result(0), index_scan_result(0)
 {}
 
 
@@ -752,6 +752,7 @@
     convert_row_to_internal_format()
       record    Byte pointer to record
       row       MySQL result set row from fetchrow()
+      result
 
   DESCRIPTION
     This method simply iterates through a row returned via fetchrow with
@@ -764,14 +765,15 @@
     0   After fields have had field values stored from record
  */
 
-uint ha_federated::convert_row_to_internal_format(byte *record, MYSQL_ROW row)
+uint ha_federated::convert_row_to_internal_format(byte *record,
+                                                  MYSQL_ROW row,
+                                                  MYSQL_RES *result)
 {
   ulong *lengths;
   Field **field;
   DBUG_ENTER("ha_federated::convert_row_to_internal_format");
 
-  lengths= mysql_fetch_lengths(stored_result);
-  memset(record, 0, table->s->null_bytes);
+  lengths= mysql_fetch_lengths(result);
 
   for (field= table->field; *field; field++)
   {
@@ -1299,12 +1301,11 @@
 
 static FEDERATED_SHARE *get_share(const char *table_name, TABLE *table)
 {
-  char *select_query, *tmp_table_name;
+  char *select_query;
   char query_buffer[FEDERATED_QUERY_BUFFER_SIZE];
-  uint tmp_table_name_length;
   Field **field;
   String query(query_buffer, sizeof(query_buffer), &my_charset_bin);
-  FEDERATED_SHARE *share;
+  FEDERATED_SHARE *share= NULL, tmp_share;
   /*
     In order to use this string, we must first zero it's length,
     or it will contain garbage
@@ -1312,12 +1313,14 @@
   query.length(0);
 
   pthread_mutex_lock(&federated_mutex);
-  tmp_table_name= (char *)table->s->table_name;
-  tmp_table_name_length= (uint) strlen(tmp_table_name);
 
+  if (parse_url(&tmp_share, table, 0))
+    goto error;
+
+  /* TODO: change tmp_share.scheme to LEX_STRING object */
   if (!(share= (FEDERATED_SHARE *) hash_search(&federated_open_tables,
-                                               (byte*) table_name,
-                                               strlen(table_name))))
+                                         (byte*) tmp_share.scheme,
+                                         tmp_share.connect_string_length)))
   {
     query.set_charset(system_charset_info);
     query.append(FEDERATED_SELECT);
@@ -1335,24 +1338,20 @@
     if (!(share= (FEDERATED_SHARE *)
           my_multi_malloc(MYF(MY_WME),
                           &share, sizeof(*share),
-                          &tmp_table_name, tmp_table_name_length+ 1,
                           &select_query,
                           query.length()+table->s->connect_string.length+1,
                           NullS)))
-    {
-      pthread_mutex_unlock(&federated_mutex);
-      return NULL;
-    }
-
-    if (parse_url(share, table, 0))
       goto error;
 
+    memcpy(share, &tmp_share, sizeof(tmp_share));
+
+    share->table_name_length= strlen(share->table_name);
+    /* TODO: share->table_name to LEX_STRING object */
     query.append(share->table_name, share->table_name_length);
     query.append(FEDERATED_BTICK);
     share->select_query= select_query;
     strmov(share->select_query, query.ptr());
     share->use_count= 0;
-    share->table_name_length= strlen(share->table_name);
     DBUG_PRINT("info",
                ("share->select_query %s", share->select_query));
 
@@ -1368,11 +1367,8 @@
 
 error:
   pthread_mutex_unlock(&federated_mutex);
-  if (share->scheme)
-  {
-    my_free((gptr) share->scheme, MYF(0));
-    share->scheme= 0;
-  }
+  my_free((gptr) tmp_share.scheme, MYF(MY_ALLOW_ZERO_PTR));
+  my_free((gptr) share, MYF(MY_ALLOW_ZERO_PTR));
   return NULL;
 }
 
@@ -1393,6 +1389,7 @@
     hash_delete(&federated_open_tables, (byte*) share);
     my_free((gptr) share->scheme, MYF(MY_ALLOW_ZERO_PTR));
     share->scheme= 0;
+
     if (share->socket)
     {
       my_free((gptr) share->socket, MYF(MY_ALLOW_ZERO_PTR));
@@ -1476,6 +1473,11 @@
     with transactions
   */
   mysql->reconnect= 1;
+
+  ref_length= table->s->primary_key != MAX_KEY ?
+    table->key_info[table->s->primary_key].key_length :
+    table->s->reclength;
+
   DBUG_RETURN(0);
 }
 
@@ -1497,15 +1499,22 @@
   DBUG_ENTER("ha_federated::close");
 
   /* free the result set */
-  if (stored_result)
+  if (table_scan_result)
   {
-    DBUG_PRINT("info",
-               ("mysql_free_result result at address %lx", stored_result));
-    mysql_free_result(stored_result);
-    stored_result= 0;
+    mysql_free_result(table_scan_result);
+    table_scan_result= 0;
+  }
+  if (index_scan_result)
+  {
+    mysql_free_result(index_scan_result);
+    index_scan_result= 0;
   }
-  /* Disconnect from mysql */
-  mysql_close(mysql);
+  /*
+    Disconnect from mysql. Test if "mysql" exists in case connection was lost
+  */
+  if (mysql)
+    mysql_close(mysql);
+
   retval= free_share(share);
   DBUG_RETURN(retval);
 
@@ -1695,8 +1704,6 @@
   /* add the values */
   insert_string.append(values_string);
 
-  DBUG_PRINT("info", ("insert query %s", insert_string.c_ptr_quick()));
-
   if (mysql_real_query(mysql, insert_string.ptr(), insert_string.length()))
   {
     DBUG_RETURN(stash_remote_error());
@@ -1772,7 +1779,7 @@
     query.append(FEDERATED_EXTENDED);
   if (check_opt->sql_flags & TT_USEFRM)
     query.append(FEDERATED_USE_FRM);
-      
+
   if (mysql_real_query(mysql, query.ptr(), query.length()))
   {
     DBUG_RETURN(stash_remote_error());
@@ -1924,7 +1931,7 @@
 /*
   This will delete a row. 'buf' will contain a copy of the row to be =deleted.
   The server will call this right after the current row has been called (from
-  either a previous rnd_nexT() or index call).
+  either a previous rnd_next() or index call).
   If you keep a pointer to the last row or can access a primary key it will
   make doing the deletion quite a bit easier.
   Keep in mind that the server does no guarentee consecutive deletions.
@@ -1984,6 +1991,7 @@
     DBUG_RETURN(stash_remote_error());
   }
   deleted+= mysql->affected_rows;
+  records-= mysql->affected_rows;
   DBUG_PRINT("info",
              ("rows deleted %d rows deleted for all time %d",
              int(mysql->affected_rows), deleted));
@@ -1991,7 +1999,6 @@
   DBUG_RETURN(0);
 }
 
-
 /*
   Positions an index cursor to the index specified in the handle. Fetches the
   row if available. If the key value is null, begin at the first key of the
@@ -2025,7 +2032,7 @@
   char error_buffer[FEDERATED_QUERY_BUFFER_SIZE];
   char index_value[STRING_BUFFER_USUAL_SIZE];
   char sql_query_buffer[FEDERATED_QUERY_BUFFER_SIZE];
-  String index_string(index_value, 
+  String index_string(index_value,
                       sizeof(index_value),
                       &my_charset_bin);
   String sql_query(sql_query_buffer,
@@ -2050,19 +2057,10 @@
                         NULL, 0);
   sql_query.append(index_string);
 
-  DBUG_PRINT("info",
-             ("current key %d key value %s index_string value %s length %d",
-              index, (char*) key, index_string.c_ptr_quick(),
-              index_string.length()));
-
-  DBUG_PRINT("info",
-             ("current position %d sql_query %s", current_position,
-              sql_query.c_ptr_quick()));
-
-  if (stored_result)
+  if (index_scan_result)
   {
-    mysql_free_result(stored_result);
-    stored_result= 0;
+    mysql_free_result(index_scan_result);
+    index_scan_result= 0;
   }
   if (mysql_real_query(mysql, sql_query.ptr(), sql_query.length()))
   {
@@ -2071,9 +2069,9 @@
     retval= ER_QUERY_ON_FOREIGN_DATA_SOURCE;
     goto error;
   }
-  stored_result= mysql_store_result(mysql);
+  index_scan_result= mysql_store_result(mysql);
 
-  if (!stored_result)
+  if (!index_scan_result)
   {
     retval= HA_ERR_END_OF_FILE;
     goto error;
@@ -2085,15 +2083,11 @@
  */
   table->status= 0;
 
-  retval= rnd_next(buf);
+  retval= read_next(buf, index_scan_result);
+  table->status= retval ? STATUS_NOT_FOUND : 0;
   DBUG_RETURN(retval);
 
 error:
-  if (stored_result)
-  {
-    mysql_free_result(stored_result);
-    stored_result= 0;
-  }
   table->status= STATUS_NOT_FOUND;
   my_error(retval, MYF(0), error_buffer);
   DBUG_RETURN(retval);
@@ -2126,8 +2120,7 @@
                    &my_charset_bin);
   DBUG_ENTER("ha_federated::read_range_first");
 
-  if (start_key == NULL && end_key == NULL)
-    DBUG_RETURN(0);
+  DBUG_ASSERT(!(start_key == NULL && end_key == NULL));
 
   sql_query.length(0);
   sql_query.append(share->select_query);
@@ -2142,35 +2135,28 @@
   }
   sql_query.length(0);
 
-  if (stored_result)
+  if (index_scan_result)
   {
-    DBUG_PRINT("info",
-               ("mysql_free_result address %lx", stored_result));
-    mysql_free_result(stored_result);
-    stored_result= 0;
+    mysql_free_result(index_scan_result);
+    index_scan_result= 0;
   }
-  stored_result= mysql_store_result(mysql);
+  index_scan_result= mysql_store_result(mysql);
 
-  if (!stored_result)
+  if (!index_scan_result)
   {
     retval= HA_ERR_END_OF_FILE;
     goto error;
   }
- 
+
   /* This was successful, please let it be known! */
   table->status= 0;
 
-  retval= rnd_next(table->record[0]);
+  retval= read_next(table->record[0], index_scan_result);
+  table->status= retval ? STATUS_NOT_FOUND : 0;
   DBUG_RETURN(retval);
 
 error:
     table->status= STATUS_NOT_FOUND;
-    if (stored_result)
-    {
-      DBUG_PRINT("info", ("mysql_free_result address %lx", stored_result));
-      mysql_free_result(stored_result);
-      stored_result= 0;
-    }
     DBUG_RETURN(retval);
 }
 
@@ -2178,7 +2164,8 @@
 {
   int retval;
   DBUG_ENTER("ha_federated::read_range_next");
-  retval= rnd_next(table->record[0]);
+  retval= read_next(table->record[0], index_scan_result);
+  table->status= retval ? STATUS_NOT_FOUND : 0;
   DBUG_RETURN(retval);
 }
 
@@ -2190,7 +2177,8 @@
   DBUG_ENTER("ha_federated::index_next");
   statistic_increment(table->in_use->status_var.ha_read_next_count,
 		      &LOCK_status);
-  retval= rnd_next(buf);
+  retval= read_next(table->record[0], index_scan_result);
+  table->status= retval ? STATUS_NOT_FOUND : 0;
   DBUG_RETURN(retval);
 }
 /*
@@ -2246,13 +2234,10 @@
 
   if (scan)
   {
-    DBUG_PRINT("info", ("share->select_query %s", share->select_query));
-    if (stored_result)
+    if (table_scan_result)
     {
-      DBUG_PRINT("info",
-                 ("mysql_free_result address %lx", stored_result));
-      mysql_free_result(stored_result);
-      stored_result= 0;
+      mysql_free_result(table_scan_result);
+      table_scan_result= 0;
     }
 
     if (mysql_real_query(mysql,
@@ -2260,8 +2245,8 @@
                          strlen(share->select_query)))
       goto error;
 
-    stored_result= mysql_store_result(mysql);
-    if (!stored_result)
+    table_scan_result= mysql_store_result(mysql);
+    if (!table_scan_result)
       goto error;
   }
   DBUG_RETURN(0);
@@ -2272,23 +2257,25 @@
 
 int ha_federated::rnd_end()
 {
-  int retval;
   DBUG_ENTER("ha_federated::rnd_end");
 
-  if (stored_result)
+  if (table_scan_result)
   {
-    DBUG_PRINT("info", ("mysql_free_result address %lx", stored_result));
-    mysql_free_result(stored_result);
-    stored_result= 0;
+    mysql_free_result(table_scan_result);
+    table_scan_result= 0;
   }
-  retval= index_end();
-  DBUG_RETURN(retval);
+  DBUG_RETURN(0);
 }
 
 int ha_federated::index_end(void)
 {
   DBUG_ENTER("ha_federated::index_end");
   active_index= MAX_KEY;
+  if (index_scan_result)
+  {
+    mysql_free_result(index_scan_result);
+    index_scan_result= 0;
+  }
   DBUG_RETURN(0);
 }
 
@@ -2305,26 +2292,54 @@
 int ha_federated::rnd_next(byte *buf)
 {
   int retval;
-  MYSQL_ROW row;
   DBUG_ENTER("ha_federated::rnd_next");
 
-  if (stored_result == 0)
-  {
-    /*
-      Return value of rnd_init is not always checked (see records.cc),
-      so we can get here _even_ if there is _no_ pre-fetched result-set!
-      TODO: fix it.
-      */
-    DBUG_RETURN(1);
-  }
- 
-  /* Fetch a row, insert it back in a row format. */
-  current_position= stored_result->data_cursor;
-  DBUG_PRINT("info", ("current position %d", current_position));
-  if (!(row= mysql_fetch_row(stored_result)))
+  statistic_increment(table->in_use->status_var.ha_read_rnd_next_count,
+                      &LOCK_status);
+
+  retval= read_next(buf, table_scan_result);
+  table->status= retval ? STATUS_NOT_FOUND : 0;
+  DBUG_RETURN(retval);
+}
+
+/*
+  ha_federated::read_next
+
+  reads from a result set and converts to mysql internal
+  format
+
+  SYNOPSIS
+    field_in_record_is_null()
+      buf       byte pointer to record 
+      result    mysql result set 
+
+    DESCRIPTION
+     This method is a wrapper method that reads one record from a result
+     set and converts it to the internal table format
+
+    RETURN VALUE
+      1    error
+      0    no error 
+*/
+
+int ha_federated::read_next(byte *buf, MYSQL_RES *result)
+{
+  int retval;
+  my_ulonglong num_rows;
+  MYSQL_ROW row;
+  DBUG_ENTER("ha_federated::read_next");
+
+  num_rows = mysql_num_rows(result);
+  DBUG_PRINT("info", ("num_rows %ld", (my_ulonglong) num_rows));
+  if (num_rows < 1)
     DBUG_RETURN(HA_ERR_END_OF_FILE);
 
-  retval= convert_row_to_internal_format(buf, row);
+  /* Fetch a row, insert it back in a row format. */
+  row= mysql_fetch_row(result);
+
+  if (!row) DBUG_RETURN(HA_ERR_END_OF_FILE);
+
+  retval= convert_row_to_internal_format(buf, row, result);
   DBUG_RETURN(retval);
 }
 
@@ -2334,6 +2349,7 @@
   ordered. You can do something like the following to store the position:
   my_store_ptr(ref, ref_length, current_position);
 
+
   The server uses ref to store data. ref_length in the above case is the size
   needed to store current_position. ref is just a byte array that the server
   will maintain. If you are using offsets to mark rows, then current_position
@@ -2346,8 +2362,11 @@
 void ha_federated::position(const byte *record)
 {
   DBUG_ENTER("ha_federated::position");
-  /* my_store_ptr Add seek storage */
-  *(MYSQL_ROW_OFFSET *) ref= current_position;  // ref is always aligned
+  if (table->s->primary_key != MAX_KEY)
+    key_copy(ref, (byte *)record, table->key_info + table->s->primary_key,
+             ref_length);
+  else
+    memcpy(ref, record, ref_length);
   DBUG_VOID_RETURN;
 }
 
@@ -2364,14 +2383,36 @@
 */
 int ha_federated::rnd_pos(byte *buf, byte *pos)
 {
+  int result= 0;
+  MYSQL_RES *saved_resultset;
   DBUG_ENTER("ha_federated::rnd_pos");
 
   statistic_increment(table->in_use->status_var.ha_read_rnd_count,
                       &LOCK_status);
-  memcpy_fixed(&current_position, pos, sizeof(MYSQL_ROW_OFFSET));
-  stored_result->current_row= 0;
-  stored_result->data_cursor= current_position;
-  DBUG_RETURN(rnd_next(buf));
+  /*
+    since index_read_ix uses and alters index result set, then we want to
+    make sure rnd_pos only alters table scan result set, so save the index
+    result set, set index result set to table scan result set
+  */
+  saved_resultset= index_scan_result;
+  index_scan_result= table_scan_result;
+  if (table->s->primary_key != MAX_KEY)
+    /* no primary key, so use index_read_idx to  */
+    result= index_read_idx(buf, table->s->primary_key, pos,
+                           ref_length, HA_READ_KEY_EXACT);
+  else
+    /* otherwise, get the position from ref as obtained in ::position */
+    memcpy(buf, pos, ref_length);
+  /*
+    now set table scan result set to index scan result set and set index
+    scan result set to its original result set (that we backed up above)
+  */
+  table_scan_result= index_scan_result;
+  index_scan_result= saved_resultset;
+
+  /* set table status to ensure joins work */
+  table->status= result ? STATUS_NOT_FOUND : 0;
+  DBUG_RETURN(result);
 }
 
 
@@ -2476,19 +2517,22 @@
         delete_length = ?
       */
       if (row[4] != NULL)
-        records=         (ha_rows) my_strtoll10(row[4], (char**) 0, &error);
-      if (row[5] != NULL)
-        mean_rec_length= (ha_rows) my_strtoll10(row[5], (char**) 0, &error);
+        records= (ha_rows) my_strtoll10(row[4], (char**) 0, &error);
+
+      mean_rec_length= table->s->reclength;
+      data_file_length= records * mean_rec_length;
+
       if (row[12] != NULL)
-        update_time=      (ha_rows) my_strtoll10(row[12], (char**) 0, &error);
+        update_time= (ha_rows) my_strtoll10(row[12], (char**) 0, &error);
       if (row[13] != NULL)
-        check_time=      (ha_rows) my_strtoll10(row[13], (char**) 0, &error);
-    }
-    if (flag & HA_STATUS_CONST)
-    {
-      block_size= 4096;
+        check_time= (ha_rows) my_strtoll10(row[13], (char**) 0, &error);
     }
   }
+  /* size of IO operations */
+  if (flag & HA_STATUS_CONST)
+  {
+    block_size= 4096;
+  }
 
   if (result)
     mysql_free_result(result);
@@ -2498,6 +2542,7 @@
 error:
   if (result)
     mysql_free_result(result);
+
   my_sprintf(error_buffer, (error_buffer, ": %d : %s",
                             mysql_errno(mysql), mysql_error(mysql)));
   my_error(error_code, MYF(0), error_buffer);
@@ -2534,6 +2579,8 @@
   /*
     TRUNCATE won't return anything in mysql_affected_rows
   */
+  DBUG_PRINT("info", ("ha_federated SQL QUERY query %s",
+                      query.c_ptr_quick()));
   if (mysql_real_query(mysql, query.ptr(), query.length()))
   {
     DBUG_RETURN(stash_remote_error());
@@ -2578,6 +2625,7 @@
                                          THR_LOCK_DATA **to,
                                          enum thr_lock_type lock_type)
 {
+  DBUG_ENTER("ha_federated::store_lock");
   if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
   {
     /*
@@ -2607,7 +2655,7 @@
 
   *to++= &lock;
 
-  return to;
+  DBUG_RETURN(to);
 }
 
 /*

--- 1.24/sql/ha_federated.h	2006-02-28 02:17:35 -08:00
+++ 1.25/sql/ha_federated.h	2006-06-21 23:39:01 -07:00
@@ -78,7 +78,7 @@
 #define FEDERATED_VALUES_LEN sizeof(FEDERATED_VALUES)
 #define FEDERATED_UPDATE "UPDATE "
 #define FEDERATED_UPDATE_LEN sizeof(FEDERATED_UPDATE)
-#define FEDERATED_SET "SET "
+#define FEDERATED_SET " SET "
 #define FEDERATED_SET_LEN sizeof(FEDERATED_SET)
 #define FEDERATED_AND " AND "
 #define FEDERATED_AND_LEN sizeof(FEDERATED_AND)
@@ -117,6 +117,7 @@
 #define FEDERATED_FALSE " 1=0"
 #define FEDERATED_FALSE_LEN sizeof(FEDERATED_FALSE)
 
+
 /*
   FEDERATED_SHARE is a structure that will be shared amoung all open handlers
   The example implements the minimum of what you will probably need.
@@ -130,6 +131,7 @@
     remote host info, parse_url supplies
   */
   char *scheme;
+  char *connect_string;
   char *hostname;
   char *username;
   char *password;
@@ -139,7 +141,7 @@
   char *socket;
   char *sport;
   ushort port;
-  uint table_name_length, use_count;
+  uint table_name_length, connect_string_length, use_count;
   pthread_mutex_t mutex;
   THR_LOCK lock;
 } FEDERATED_SHARE;
@@ -152,10 +154,9 @@
   THR_LOCK_DATA lock;      /* MySQL lock */
   FEDERATED_SHARE *share;    /* Shared lock info */
   MYSQL *mysql; /* MySQL connection */
-  MYSQL_RES *stored_result;
-  uint ref_length;
+  /* index result, table scan result cursors */
+  MYSQL_RES *index_scan_result, *table_scan_result;
   uint fetch_num; // stores the fetch num
-  MYSQL_ROW_OFFSET current_position;  // Current position used by ::position()
   int remote_error_number;
   char remote_error_buf[FEDERATED_QUERY_BUFFER_SIZE];
 
@@ -164,12 +165,13 @@
       return 0 on success
       return errorcode otherwise
   */
-  uint convert_row_to_internal_format(byte *buf, MYSQL_ROW row);
+  uint convert_row_to_internal_format(byte *buf, MYSQL_ROW row, MYSQL_RES *resultset);
   bool create_where_from_key(String *to, KEY *key_info, 
                              const key_range *start_key,
                              const key_range *end_key,
                              bool records_in_range);
   int stash_remote_error();
+  int read_next(byte *buf, MYSQL_RES *resultset);
 
 public:
   ha_federated(TABLE *table_arg);
Thread
bk commit into 5.0 tree (patg:1.2196) BUG#19773Patrick Galbraith22 Jun