List:Internals« Previous MessageNext Message »
From:Konstantin Osipov Date:September 21 2005 10:11pm
Subject:bk commit into 5.0 tree (konstantin:1.1972) BUG#9819
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of kostja. When kostja 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.1972 05/09/22 02:11:21 konstantin@stripped +30 -0
  A fix and a test case for Bug#6513 "Test Suite: Values inserted by using 
  cursor is interpreted latin1 character and Bug#9819 "Cursors: Mysql Server
  Crash while fetching from table with 5 million records."
  A fix for a possible memory leak when fetching into an SP cursor
  in a long loop.
  The patch uses a common implementation of cursors in the binary protocol and 
  in stored procedures and implements materialized cursors.
  For implementation details, see comments in sql_cursor.cc

  sql/sql_cursor.h
    1.1 05/09/22 02:11:00 konstantin@stripped +65 -0
    New BitKeeper file ``sql/sql_cursor.h'' - declarations for
    server side cursors.

  sql/sql_cursor.h
    1.0 05/09/22 02:11:00 konstantin@stripped +0 -0
    BitKeeper file /home/kostja/mysql/mysql-5.0-release/sql/sql_cursor.h

  sql/sql_cursor.cc
    1.1 05/09/22 02:10:59 konstantin@stripped +660 -0
    New BitKeeper file ``sql/sql_cursor.cc'' -- implementation of server side
    cursors

  tests/mysql_client_test.c
    1.159 05/09/22 02:10:59 konstantin@stripped +5 -2
    - if cursors are materialized, a parallel update of the table used
      in the cursor may go through: update the test.

  sql/table.h
    1.115 05/09/22 02:10:59 konstantin@stripped +3 -0
    - add declarations for auxiliary methods of st_table used to work with 
     temporary tables

  sql/table.cc
    1.191 05/09/22 02:10:59 konstantin@stripped +57 -0
    - implement auxiliary st_table methods for use with temporary tables

  sql/sql_view.cc
    1.64 05/09/22 02:10:59 konstantin@stripped +1 -1
    - adjust to the changed signature of st_select_lex_unit::prepare

  sql/sql_union.cc
    1.129 05/09/22 02:10:59 konstantin@stripped +106 -70
    - move pieces of st_select_unit::prepare to select_union and st_table
      methods to be able to reuse code in the implementation of materialized
      cursors

  sql/sql_select.h
    1.99 05/09/22 02:10:59 konstantin@stripped +6 -53
    - move the declaration of sensitive (non-materializing) cursors to 
      sql_cursor.cc

  sql/sql_cursor.cc
    1.0 05/09/22 02:10:59 konstantin@stripped +0 -0
    BitKeeper file /home/kostja/mysql/mysql-5.0-release/sql/sql_cursor.cc

  sql/sql_select.cc
    1.373 05/09/22 02:10:58 konstantin@stripped +86 -304
    - move the implementation of sensitive (non-materializing) cursors to 
      sql_cursor.cc
    - make temporary tables self-contained: the table, its record and fields
      are allocated in TABLE::mem_root. This implementation is not clean
      and resets thd->mem_root several times because of the way create_tmp_table 
      works (many additional things are done inside it).
    - adjust to the changed declaration of st_select_lex_unit::prepare

  sql/sql_prepare.cc
    1.155 05/09/22 02:10:58 konstantin@stripped +134 -122
    - split the tight coupling of cursors and prepared statements to reuse 
      the same implementation in stored procedures
    - cleanups of error processing in Prepared_statement::{prepare,execute}

  sql/sql_list.h
    1.38 05/09/22 02:10:58 konstantin@stripped +2 -0
    - add an array operator new[] to class Sql_alloc

  sql/sql_lex.h
    1.198 05/09/22 02:10:58 konstantin@stripped +4 -6
    - declarations of auxiliary methods used by materialized cursors
    - a cleanup in st_select_lex_unit interface

  sql/sql_derived.cc
    1.77 05/09/22 02:10:58 konstantin@stripped +16 -27
    - reuse bits of tmp table code in UNION, derived tables, and materialized
      cursors
    - cleanup comments

  sql/sql_class.h
    1.265 05/09/22 02:10:58 konstantin@stripped +13 -13
    - adjust to the rename Cursor -> Server_side_cursor
    - additional declarations of select_union used in materialized cursors

  sql/sql_class.cc
    1.211 05/09/22 02:10:58 konstantin@stripped +15 -13
    - disable the functionality that closes transient cursors at commit/rollback;
      transient cursors are not used in 5.0, instead we use materialized ones.
      To be enabled in a later version.

  sql/sp_rcontext.h
    1.24 05/09/22 02:10:58 konstantin@stripped +29 -14
    - reimplement sp_cursor using the new implementation of server side cursors.

  sql/sp_rcontext.cc
    1.33 05/09/22 02:10:58 konstantin@stripped +76 -133
    - reimplement sp_cursor using the new implementation of server side cursors.
    - use sp_eval_func_item to assign values of SP variables from the
      row fetched from a cursor. This should fix a possible memory leak in 
      the old implementation of sp_cursor::fetch

  sql/sp_head.h
    1.71 05/09/22 02:10:57 konstantin@stripped +9 -0
    - declare a no-op implementation for cleanup_stmt in sp_instr_cpush.
      This method is needed for non-materializing cursors, which are yet not 
      used in stored procedures.
    - declaration for sp_eval_func_item

  sql/sp_head.cc
    1.185 05/09/22 02:10:57 konstantin@stripped +50 -62
    - move juggling with Query_arena::free_list and Item::next to
      sp_eval_func_item, as this is needed in 3 places already. 

  sql/protocol.h
    1.30 05/09/22 02:10:57 konstantin@stripped +0 -24
    - drop Protocol_cursor

  sql/item_subselect.cc
    1.119 05/09/22 02:10:57 konstantin@stripped +1 -1
    - adjust to the changed declaration of st_select_lex_unit::prepare

  sql/handler.cc
    1.188 05/09/22 02:10:57 konstantin@stripped +3 -5
    - now TABLE object has its mem_root always initialized.
      Adjust the implementation handler::ha_open

  sql/Makefile.am
    1.110 05/09/22 02:10:57 konstantin@stripped +2 -2
    - add sql_cursor.h, sql_cursor.cc (a new implementation of stored procedure
    cursors) and drop protocol_cursor.cc (the old one)

  mysys/my_alloc.c
    1.32 05/09/22 02:10:57 konstantin@stripped +51 -0
    - an implementation of multi_alloc_root; this is largely a copy-paste
      from mulalloc.c, but the function is small and there is no easy way
      to reuse the existing C function.

  mysql-test/t/sp-big.test
    1.2 05/09/22 02:10:57 konstantin@stripped +49 -0
    Add a restricted test case for Bug#9819 "Cursors: Mysql Server Crash
    while fetching from table with 5 million records."

  mysql-test/t/ctype_ujis.test
    1.14 05/09/22 02:10:57 konstantin@stripped +42 -0
    Add a test case for Bug#6513 "Test Suite: Values inserted by using cursor is
     interpreted latin1 character"

  mysql-test/r/sp-big.result
    1.2 05/09/22 02:10:57 konstantin@stripped +46 -0
    - test results fixed (a test case for Bug#9819)

  mysql-test/r/ctype_ujis.result
    1.14 05/09/22 02:10:57 konstantin@stripped +30 -0
    - test results fixed (a test case for Bug#6513)

  libmysqld/Makefile.am
    1.62 05/09/22 02:10:57 konstantin@stripped +1 -1
    - drop protocol_cursor.cc, add sql_cursor.cc (replaces the old
    implementation of cursors with a new one)

  include/my_sys.h
    1.169 05/09/22 02:10:57 konstantin@stripped +1 -0
    - declaration for multi_alloc_root

# 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:	konstantin
# Host:	oak.local
# Root:	/home/kostja/mysql/mysql-5.0-release

--- 1.168/include/my_sys.h	2005-09-14 15:30:09 +04:00
+++ 1.169/include/my_sys.h	2005-09-22 02:10:57 +04:00
@@ -775,6 +775,7 @@
 extern void init_alloc_root(MEM_ROOT *mem_root, uint block_size,
 			    uint pre_alloc_size);
 extern gptr alloc_root(MEM_ROOT *mem_root,unsigned int Size);
+extern gptr multi_alloc_root(MEM_ROOT *mem_root, ...);
 extern void free_root(MEM_ROOT *root, myf MyFLAGS);
 extern void set_prealloc_root(MEM_ROOT *root, char *ptr);
 extern void reset_root_defaults(MEM_ROOT *mem_root, uint block_size,

--- 1.31/mysys/my_alloc.c	2005-09-05 14:09:31 +04:00
+++ 1.32/mysys/my_alloc.c	2005-09-22 02:10:57 +04:00
@@ -221,6 +221,57 @@
 #endif
 }
 
+
+/*
+  Allocate many pointers at the same time.
+
+  DESCRIPTION
+    ptr1, ptr2, etc all point into big allocated memory area.
+
+  SYNOPSIS
+    multi_alloc_root()
+      root               Memory root
+      ptr1, length1      Multiple arguments terminated by a NULL pointer
+      ptr2, length2      ...
+      ...
+      NULL
+
+  RETURN VALUE
+    A pointer to the beginning of the allocated memory block
+    in case of success or NULL if out of memory.
+*/
+
+gptr multi_alloc_root(MEM_ROOT *root, ...)
+{
+  va_list args;
+  char **ptr, *start, *res;
+  uint tot_length, length;
+  DBUG_ENTER("multi_alloc_root");
+
+  va_start(args, root);
+  tot_length= 0;
+  while ((ptr= va_arg(args, char **)))
+  {
+    length= va_arg(args, uint);
+    tot_length+= ALIGN_SIZE(length);
+  }
+  va_end(args);
+
+  if (!(start= (char*) alloc_root(root, tot_length)))
+    DBUG_RETURN(0);                            /* purecov: inspected */
+
+  va_start(args, root);
+  res= start;
+  while ((ptr= va_arg(args, char **)))
+  {
+    *ptr= res;
+    length= va_arg(args, uint);
+    res+= ALIGN_SIZE(length);
+  }
+  va_end(args);
+  DBUG_RETURN((gptr) start);
+}
+
 #define TRASH_MEM(X) TRASH(((char*)(X) + ((X)->size-(X)->left)), (X)->left)
 
 /* Mark all data in blocks free for reusage */

--- 1.109/sql/Makefile.am	2005-08-25 23:41:46 +04:00
+++ 1.110/sql/Makefile.am	2005-09-22 02:10:57 +04:00
@@ -61,7 +61,7 @@
                         tztime.h my_decimal.h\
 			sp_head.h sp_pcontext.h sp_rcontext.h sp.h sp_cache.h \
 			parse_file.h sql_view.h	sql_trigger.h \
-			sql_array.h \
+			sql_array.h sql_cursor.h \
 			examples/ha_example.h examples/ha_archive.h \
 			examples/ha_tina.h ha_blackhole.h  \
 			ha_federated.h
@@ -94,7 +94,7 @@
 			client.c sql_client.cc mini_client_errors.c pack.c\
 			stacktrace.c repl_failsafe.h repl_failsafe.cc \
 			sql_olap.cc sql_view.cc \
-			gstream.cc spatial.cc sql_help.cc protocol_cursor.cc \
+			gstream.cc spatial.cc sql_help.cc sql_cursor.cc \
                         tztime.cc my_time.c my_decimal.cc\
 			sp_head.cc sp_pcontext.cc  sp_rcontext.cc sp.cc \
 			sp_cache.cc parse_file.cc sql_trigger.cc \

--- 1.187/sql/handler.cc	2005-09-14 02:41:38 +04:00
+++ 1.188/sql/handler.cc	2005-09-22 02:10:57 +04:00
@@ -1339,11 +1339,9 @@
       table->db_stat|=HA_READ_ONLY;
     (void) extra(HA_EXTRA_NO_READCHECK);	// Not needed in SQL
 
-    if (!alloc_root_inited(&table->mem_root))	// If temporary table
-      ref=(byte*) sql_alloc(ALIGN_SIZE(ref_length)*2);
-    else
-      ref=(byte*) alloc_root(&table->mem_root, ALIGN_SIZE(ref_length)*2);
-    if (!ref)
+    DBUG_ASSERT(alloc_root_inited(&table->mem_root));
+
+    if (!(ref= (byte*) alloc_root(&table->mem_root, ALIGN_SIZE(ref_length)*2)))
     {
       close();
       error=HA_ERR_OUT_OF_MEM;

--- 1.210/sql/sql_class.cc	2005-09-13 17:32:34 +04:00
+++ 1.211/sql/sql_class.cc	2005-09-22 02:10:58 +04:00
@@ -1544,6 +1544,19 @@
 }
 
 
+void Query_arena::set_query_arena(Query_arena *set)
+{
+  mem_root=  set->mem_root;
+  free_list= set->free_list;
+  state= set->state;
+}
+
+
+void Query_arena::cleanup_stmt()
+{
+  DBUG_ASSERT("Query_arena::cleanup_stmt()" == "not implemented");
+}
+
 /*
   Statement functions 
 */
@@ -1601,12 +1614,6 @@
 }
 
 
-void Statement::close_cursor()
-{
-  DBUG_ASSERT("Statement::close_cursor()" == "not implemented");
-}
-
-
 void THD::end_statement()
 {
   /* Cleanup SQL processing state to resuse this statement in next query. */
@@ -1648,13 +1655,6 @@
   DBUG_VOID_RETURN;
 }
 
-void Query_arena::set_query_arena(Query_arena *set)
-{
-  mem_root=  set->mem_root;
-  free_list= set->free_list;
-  state= set->state;
-}
-
 Statement::~Statement()
 {
   /*
@@ -1723,9 +1723,11 @@
 
 void Statement_map::close_transient_cursors()
 {
+#ifdef TO_BE_IMPLEMENTED
   Statement *stmt;
   while ((stmt= transient_cursor_list.head()))
     stmt->close_cursor();                 /* deletes itself from the list */
+#endif
 }
 
 

--- 1.264/sql/sql_class.h	2005-09-13 17:32:35 +04:00
+++ 1.265/sql/sql_class.h	2005-09-22 02:10:58 +04:00
@@ -737,10 +737,12 @@
   void set_query_arena(Query_arena *set);
 
   void free_items();
+  /* Close the active state associated with execution of this statement */
+  virtual void cleanup_stmt();
 };
 
 
-class Cursor;
+class Server_side_cursor;
 
 /*
   State of a single command executed against this connection.
@@ -816,7 +818,7 @@
   */
   char *query;
   uint32 query_length;                          // current query length
-  Cursor *cursor;
+  Server_side_cursor *cursor;
 
 public:
 
@@ -833,8 +835,6 @@
   void restore_backup_statement(Statement *stmt, Statement *backup);
   /* return class type */
   virtual Type type() const;
-  /* Close the cursor open for this statement, if there is one */
-  virtual void close_cursor();
 };
 
 
@@ -886,9 +886,6 @@
     }
     hash_delete(&st_hash, (byte *) statement);
   }
-  void add_transient_cursor(Statement *stmt)
-  { transient_cursor_list.append(stmt); }
-  void erase_transient_cursor(Statement *stmt) { stmt->unlink(); }
   /*
     Close all cursors of this connection that use tables of a storage
     engine that has transaction-specific state and therefore can not
@@ -1812,18 +1809,21 @@
   }
 };
 
-class select_union :public select_result_interceptor {
- public:
-  TABLE *table;
+class select_union :public select_result_interceptor
+{
   TMP_TABLE_PARAM tmp_table_param;
+public:
+  TABLE *table;
 
-  select_union(TABLE *table_par);
-  ~select_union();
+  select_union() :table(0) {}
   int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
   bool send_data(List<Item> &items);
   bool send_eof();
   bool flush();
-  void set_table(TABLE *tbl) { table= tbl; }
+
+  bool create_result_table(THD *thd, List<Item> *column_types,
+                           bool is_distinct, ulonglong options,
+                           const char *alias);
 };
 
 /* Base subselect interface class */

--- 1.197/sql/sql_lex.h	2005-09-15 03:57:52 +04:00
+++ 1.198/sql/sql_lex.h	2005-09-22 02:10:58 +04:00
@@ -432,10 +432,6 @@
   {
     return my_reinterpret_cast(st_select_lex*)(slave);
   }
-  st_select_lex* first_select_in_union() 
-  { 
-    return my_reinterpret_cast(st_select_lex*)(slave);
-  }
   st_select_lex_unit* next_unit()
   {
     return my_reinterpret_cast(st_select_lex_unit*)(next);
@@ -445,8 +441,7 @@
   void exclude_tree();
 
   /* UNION methods */
-  bool prepare(THD *thd, select_result *result, ulong additional_options,
-               const char *tmp_table_alias);
+  bool prepare(THD *thd, select_result *result, ulong additional_options);
   bool exec();
   bool cleanup();
   inline void unclean() { cleaned= 0; }
@@ -462,7 +457,10 @@
 
   friend void lex_start(THD *thd, uchar *buf, uint length);
   friend int subselect_union_engine::exec();
+
+  List<Item> *get_unit_column_types();
 };
+
 typedef class st_select_lex_unit SELECT_LEX_UNIT;
 
 /*

--- 1.37/sql/sql_list.h	2005-08-22 19:22:51 +04:00
+++ 1.38/sql/sql_list.h	2005-09-22 02:10:58 +04:00
@@ -32,6 +32,8 @@
   {
     return (void*) sql_alloc((uint) size);
   }
+  static void *operator new[](size_t size, MEM_ROOT *mem_root)
+  { return (void*) alloc_root(mem_root, (uint) size); }
   static void *operator new(size_t size, MEM_ROOT *mem_root)
   { return (void*) alloc_root(mem_root, (uint) size); }
   static void operator delete(void *ptr, size_t size) { TRASH(ptr, size); }

--- 1.372/sql/sql_select.cc	2005-09-13 11:38:47 +04:00
+++ 1.373/sql/sql_select.cc	2005-09-22 02:10:58 +04:00
@@ -23,6 +23,7 @@
 
 #include "mysql_priv.h"
 #include "sql_select.h"
+#include "sql_cursor.h"
 
 #include <m_ctype.h>
 #include <hash.h>
@@ -107,20 +108,15 @@
 static bool open_tmp_table(TABLE *table);
 static bool create_myisam_tmp_table(TABLE *table,TMP_TABLE_PARAM *param,
 				    ulong options);
-static Next_select_func setup_end_select_func(JOIN *join);
 static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table,
 		     Procedure *proc);
 
 static enum_nested_loop_state
-sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
-static enum_nested_loop_state
 evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
                      int error, my_bool *report_error);
 static enum_nested_loop_state
 evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
 static enum_nested_loop_state
-sub_select(JOIN *join,JOIN_TAB *join_tab, bool end_of_records);
-static enum_nested_loop_state
 flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
 static enum_nested_loop_state
 end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
@@ -1716,262 +1712,6 @@
   DBUG_RETURN(error);
 }
 
-
-/************************* Cursor ******************************************/
-
-Cursor::Cursor(THD *thd)
-  :Query_arena(&main_mem_root, INITIALIZED),
-   join(0), unit(0),
-   protocol(thd),
-   close_at_commit(FALSE)
-{
-  /* We will overwrite it at open anyway. */
-  init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
-  thr_lock_owner_init(&lock_id, &thd->lock_info);
-  bzero((void*) ht_info, sizeof(ht_info));
-}
-
-
-void
-Cursor::init_from_thd(THD *thd)
-{
-  Engine_info *info;
-  /*
-    We need to save and reset thd->mem_root, otherwise it'll be freed
-    later in mysql_parse.
-
-    We can't just change the thd->mem_root here as we want to keep the
-    things that are already allocated in thd->mem_root for Cursor::fetch()
-  */
-  main_mem_root=  *thd->mem_root;
-  state= thd->stmt_arena->state;
-  /* Allocate new memory root for thd */
-  init_sql_alloc(thd->mem_root,
-                 thd->variables.query_alloc_block_size,
-                 thd->variables.query_prealloc_size);
-
-  /*
-    The same is true for open tables and lock: save tables and zero THD
-    pointers to prevent table close in close_thread_tables (This is a part
-    of the temporary solution to make cursors work with minimal changes to
-    the current source base).
-  */
-  derived_tables= thd->derived_tables;
-  open_tables=    thd->open_tables;
-  lock=           thd->lock;
-  query_id=       thd->query_id;
-  free_list=	  thd->free_list;
-  change_list=    thd->change_list;
-  reset_thd(thd);
-  /* Now we have an active cursor and can cause a deadlock */
-  thd->lock_info.n_cursors++;
-
-  close_at_commit= FALSE; /* reset in case we're reusing the cursor */
-  info= &ht_info[0];
-  for (handlerton **pht= thd->transaction.stmt.ht; *pht; pht++)
-  {
-    const handlerton *ht= *pht;
-    close_at_commit|= test(ht->flags & HTON_CLOSE_CURSORS_AT_COMMIT);
-    if (ht->create_cursor_read_view)
-    {
-      info->ht= ht;
-      info->read_view= (ht->create_cursor_read_view)();
-      ++info;
-    }
-  }
-  /*
-    XXX: thd->locked_tables is not changed.
-    What problems can we have with it if cursor is open?
-    TODO: must be fixed because of the prelocked mode.
-  */
-}
-
-
-void
-Cursor::reset_thd(THD *thd)
-{
-  thd->derived_tables= 0;
-  thd->open_tables= 0;
-  thd->lock= 0;
-  thd->free_list= 0;
-  thd->change_list.empty();
-}
-
-
-int
-Cursor::open(JOIN *join_arg)
-{
-  join= join_arg;
-  THD *thd= join->thd;
-  /* First non-constant table */
-  JOIN_TAB *join_tab= join->join_tab + join->const_tables;
-  DBUG_ENTER("Cursor::open");
-
-  /*
-    Send fields description to the client; server_status is sent
-    in 'EOF' packet, which ends send_fields().
-  */
-  thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
-  join->result->send_fields(*join->fields, Protocol::SEND_NUM_ROWS);
-  ::send_eof(thd);
-  thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
-
-  /* Prepare JOIN for reading rows. */
-  join->tmp_table= 0;
-  join->join_tab[join->tables-1].next_select= setup_end_select_func(join);
-  join->send_records= 0;
-  join->fetch_limit= join->unit->offset_limit_cnt;
-
-  /* Disable JOIN CACHE as it is not working with cursors yet */
-  for (JOIN_TAB *tab= join_tab;
-       tab != join->join_tab + join->tables - 1;
-       tab++)
-  {
-    if (tab->next_select == sub_select_cache)
-      tab->next_select= sub_select;
-  }
-
-  DBUG_ASSERT(join_tab->table->reginfo.not_exists_optimize == 0);
-  DBUG_ASSERT(join_tab->not_used_in_distinct == 0);
-  /*
-    null_row is set only if row not found and it's outer join: should never
-    happen for the first table in join_tab list
-  */
-  DBUG_ASSERT(join_tab->table->null_row == 0);
-  DBUG_RETURN(0);
-}
-
-
-/*
-  DESCRIPTION
-    Fetch next num_rows rows from the cursor and sent them to the client
-  PRECONDITION:
-    Cursor is open
-  RETURN VALUES:
-    none, this function will send error or OK to network if necessary.
-*/
-
-void
-Cursor::fetch(ulong num_rows)
-{
-  THD *thd= join->thd;
-  JOIN_TAB *join_tab= join->join_tab + join->const_tables;
-  enum_nested_loop_state error= NESTED_LOOP_OK;
-  Query_arena backup_arena;
-  Engine_info *info;
-  DBUG_ENTER("Cursor::fetch");
-  DBUG_PRINT("enter",("rows: %lu", num_rows));
-
-  DBUG_ASSERT(thd->derived_tables == 0 && thd->open_tables == 0 &&
-              thd->lock == 0);
-
-  thd->derived_tables= derived_tables;
-  thd->open_tables= open_tables;
-  thd->lock= lock;
-  thd->query_id= query_id;
-  thd->change_list= change_list;
-  /* save references to memory, allocated during fetch */
-  thd->set_n_backup_active_arena(this, &backup_arena);
-
-  for (info= ht_info; info->read_view ; info++)
-    (info->ht->set_cursor_read_view)(info->read_view);
-
-  join->fetch_limit+= num_rows;
-
-  error= sub_select(join, join_tab, 0);
-  if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
-    error= sub_select(join,join_tab,1);
-  if (error == NESTED_LOOP_QUERY_LIMIT)
-    error= NESTED_LOOP_OK;                    /* select_limit used */
-  if (error == NESTED_LOOP_CURSOR_LIMIT)
-    join->resume_nested_loop= TRUE;
-
-#ifdef USING_TRANSACTIONS
-    ha_release_temporary_latches(thd);
-#endif
-  /* Grab free_list here to correctly free it in close */
-  thd->restore_active_arena(this, &backup_arena);
-
-  for (info= ht_info; info->read_view; info++)
-    (info->ht->set_cursor_read_view)(0);
-
-  if (error == NESTED_LOOP_CURSOR_LIMIT)
-  {
-    /* Fetch limit worked, possibly more rows are there */
-    thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
-    ::send_eof(thd);
-    thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
-    change_list= thd->change_list;
-    reset_thd(thd);
-  }
-  else
-  {
-    close(TRUE);
-    if (error == NESTED_LOOP_OK)
-    {
-      thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
-      ::send_eof(thd);
-      thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
-    }
-    else if (error != NESTED_LOOP_KILLED)
-      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
-  }
-  DBUG_VOID_RETURN;
-}
-
-
-void
-Cursor::close(bool is_active)
-{
-  THD *thd= join->thd;
-  DBUG_ENTER("Cursor::close");
-
-  /*
-    In case of UNIONs JOIN is freed inside of unit->cleanup(),
-    otherwise in select_lex->cleanup().
-  */
-  if (unit)
-    (void) unit->cleanup();
-  else
-    (void) join->select_lex->cleanup();
-
-  for (Engine_info *info= ht_info; info->read_view; info++)
-  {
-    (info->ht->close_cursor_read_view)(info->read_view);
-    info->read_view= 0;
-    info->ht= 0;
-  }
-
-  if (is_active)
-    close_thread_tables(thd);
-  else
-  {
-    /* XXX: Another hack: closing tables used in the cursor */
-    DBUG_ASSERT(lock || open_tables || derived_tables);
-
-    TABLE *tmp_derived_tables= thd->derived_tables;
-    MYSQL_LOCK *tmp_lock= thd->lock;
-
-    thd->open_tables= open_tables;
-    thd->derived_tables= derived_tables;
-    thd->lock= lock;
-    close_thread_tables(thd);
-
-    thd->open_tables= tmp_derived_tables;
-    thd->derived_tables= tmp_derived_tables;
-    thd->lock= tmp_lock;
-  }
-  thd->lock_info.n_cursors--; /* Decrease the number of active cursors */
-  join= 0;
-  unit= 0;
-  free_items();
-  change_list.empty();
-  DBUG_VOID_RETURN;
-}
-
-
-/*********************************************************************/
-
 /*
   An entry point to single-unit select (a select without UNION).
 
@@ -2051,9 +1791,9 @@
       }
       else
       {
-	if (join->prepare(rref_pointer_array, tables, wild_num,
-			  conds, og_num, order, group, having, proc_param,
-			  select_lex, unit))
+        if (err= join->prepare(rref_pointer_array, tables, wild_num,
+                               conds, og_num, order, group, having, proc_param,
+                               select_lex, unit))
 	{
 	  goto err;
 	}
@@ -2068,9 +1808,9 @@
 	DBUG_RETURN(TRUE);
     thd->proc_info="init";
     thd->used_tables=0;                         // Updated by setup_fields
-    if (join->prepare(rref_pointer_array, tables, wild_num,
-		      conds, og_num, order, group, having, proc_param,
-		      select_lex, unit))
+    if (err= join->prepare(rref_pointer_array, tables, wild_num,
+                           conds, og_num, order, group, having, proc_param,
+                           select_lex, unit))
     {
       goto err;
     }
@@ -2112,7 +1852,7 @@
   if (free_join)
   {
     thd->proc_info="end";
-    err= select_lex->cleanup();
+    err|= select_lex->cleanup();
     DBUG_RETURN(err || thd->net.report_error);
   }
   DBUG_RETURN(join->error);
@@ -5976,7 +5716,7 @@
   cleanup(full);
 
   for (unit= select_lex->first_inner_unit(); unit; unit= unit->next_unit())
-    for (sl= unit->first_select_in_union(); sl; sl= sl->next_select())
+    for (sl= unit->first_select(); sl; sl= sl->next_select())
     {
       JOIN *join= sl->join;
       if (join)
@@ -8122,9 +7862,31 @@
 
 /*
   Create a temp table according to a field list.
-  Set distinct if duplicates could be removed
-  Given fields field pointers are changed to point at tmp_table
-  for send_fields
+
+  SYNOPSIS
+    create_tmp_table()
+    thd                  thread handle
+    param                a description used as input to create the table
+    fields               list of items that will be used to define
+                         column types of the table (also see NOTES)
+    group                TODO document
+    distinct             should table rows be distinct
+    save_sum_fields      see NOTES
+    select_options
+    rows_limit
+    table_alias          possible name of the temporary table that can be used
+                         for name resolving; can be "".
+
+  DESCRIPTION
+    Given field pointers are changed to point at tmp_table for
+    send_fields. The table object is self contained: it's
+    allocated in its own memory root, as well as Field objects
+    created for table columns.
+    This function will replace Item_sum items in 'fields' list with
+    corresponding Item_field items, pointing at the fields in the
+    temporary table, unless this was prohibited by TRUE
+    value of argument save_sum_fields. The Item_field objects
+    are created in THD memory root.
 */
 
 #define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
@@ -8138,6 +7900,7 @@
 		 ulonglong select_options, ha_rows rows_limit,
 		 char *table_alias)
 {
+  MEM_ROOT *mem_root_save, own_root;
   TABLE *table;
   uint	i,field_count,null_count,null_pack_length;
   uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
@@ -8202,29 +7965,33 @@
 
   field_count=param->field_count+param->func_count+param->sum_func_count;
   hidden_field_count=param->hidden_field_count;
-  if (!my_multi_malloc(MYF(MY_WME),
-		       &table,sizeof(*table),
-		       &reg_field,  sizeof(Field*)*(field_count+1),
-		       &blob_field, sizeof(uint)*(field_count+1),
-		       &from_field, sizeof(Field*)*field_count,
-		       &copy_func,sizeof(*copy_func)*(param->func_count+1),
-		       &param->keyinfo,sizeof(*param->keyinfo),
-		       &key_part_info,
-		       sizeof(*key_part_info)*(param->group_parts+1),
-		       &param->start_recinfo,
-		       sizeof(*param->recinfo)*(field_count*2+4),
-		       &tmpname,(uint) strlen(path)+1,
-		       &group_buff,group && ! using_unique_constraint ?
-		       param->group_length : 0,
-		       NullS))
+
+  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
+
+  if (!multi_alloc_root(&own_root,
+                        &table, sizeof(*table),
+                        &reg_field, sizeof(Field*) * (field_count+1),
+                        &blob_field, sizeof(uint)*(field_count+1),
+                        &from_field, sizeof(Field*)*field_count,
+                        &copy_func, sizeof(*copy_func)*(param->func_count+1),
+                        &param->keyinfo, sizeof(*param->keyinfo),
+                        &key_part_info,
+                        sizeof(*key_part_info)*(param->group_parts+1),
+                        &param->start_recinfo,
+                        sizeof(*param->recinfo)*(field_count*2+4),
+                        &tmpname, (uint) strlen(path)+1,
+                        &group_buff, group && ! using_unique_constraint ?
+                        param->group_length : 0,
+                        NullS))
   {
     bitmap_clear_bit(&temp_pool, temp_pool_slot);
     DBUG_RETURN(NULL);				/* purecov: inspected */
   }
-  if (!(param->copy_field=copy=new Copy_field[field_count]))
+  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
+  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
   {
     bitmap_clear_bit(&temp_pool, temp_pool_slot);
-    my_free((gptr) table,MYF(0));		/* purecov: inspected */
+    free_root(&own_root, MYF(0));               /* purecov: inspected */
     DBUG_RETURN(NULL);				/* purecov: inspected */
   }
   param->items_to_copy= copy_func;
@@ -8234,6 +8001,11 @@
   bzero((char*) table,sizeof(*table));
   bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
   bzero((char*) from_field,sizeof(Field*)*field_count);
+
+  table->mem_root= own_root;
+  mem_root_save= thd->mem_root;
+  thd->mem_root= &table->mem_root;
+
   table->field=reg_field;
   table->alias= table_alias;
   table->reginfo.lock_type=TL_WRITE;	/* Will be updated */
@@ -8318,7 +8090,9 @@
             string_count++;
             string_total_length+= new_field->pack_length();
           }
+          thd->mem_root= mem_root_save;
           thd->change_item_tree(argp, new Item_field(new_field));
+          thd->mem_root= &table->mem_root;
 	  if (!(new_field->flags & NOT_NULL_FLAG))
           {
 	    null_count++;
@@ -8432,7 +8206,8 @@
   {
     uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
     table->s->rec_buff_length= alloc_length;
-    if (!(table->record[0]= (byte *) my_malloc(alloc_length*3, MYF(MY_WME))))
+    if (!(table->record[0]= (byte*)
+                            alloc_root(&table->mem_root, alloc_length*3)))
       goto err;
     table->record[1]= table->record[0]+alloc_length;
     table->s->default_values= table->record[1]+alloc_length;
@@ -8618,8 +8393,10 @@
       table->s->uniques= 1;
     }
     if (!(key_part_info= (KEY_PART_INFO*)
-	  sql_calloc((keyinfo->key_parts)*sizeof(KEY_PART_INFO))))
+          alloc_root(&table->mem_root,
+                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
       goto err;
+    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
     table->key_info=keyinfo;
     keyinfo->key_part=key_part_info;
     keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
@@ -8667,10 +8444,15 @@
     if (create_myisam_tmp_table(table,param,select_options))
       goto err;
   }
-  if (!open_tmp_table(table))
-    DBUG_RETURN(table);
+  if (open_tmp_table(table))
+    goto err;
 
- err:
+  thd->mem_root= mem_root_save;
+
+  DBUG_RETURN(table);
+
+err:
+  thd->mem_root= mem_root_save;
   free_tmp_table(thd,table);                    /* purecov: inspected */
   bitmap_clear_bit(&temp_pool, temp_pool_slot);
   DBUG_RETURN(NULL);				/* purecov: inspected */
@@ -8815,11 +8597,12 @@
   if (table->s->keys)
   {						// Get keys for ni_create
     bool using_unique_constraint=0;
-    HA_KEYSEG *seg= (HA_KEYSEG*) sql_calloc(sizeof(*seg) *
-					    keyinfo->key_parts);
+    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
+                                            sizeof(*seg) * keyinfo->key_parts);
     if (!seg)
       goto err;
 
+    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
     if (keyinfo->key_length >= table->file->max_key_length() ||
 	keyinfo->key_parts > table->file->max_key_parts() ||
 	table->s->uniques)
@@ -8916,13 +8699,14 @@
 void
 free_tmp_table(THD *thd, TABLE *entry)
 {
+  MEM_ROOT own_root= entry->mem_root;
   const char *save_proc_info;
   DBUG_ENTER("free_tmp_table");
   DBUG_PRINT("enter",("table: %s",entry->alias));
 
   save_proc_info=thd->proc_info;
   thd->proc_info="removing tmp table";
-  free_blobs(entry);
+
   if (entry->file)
   {
     if (entry->db_stat)
@@ -8943,12 +8727,11 @@
   /* free blobs */
   for (Field **ptr=entry->field ; *ptr ; ptr++)
     (*ptr)->free();
-  my_free((gptr) entry->record[0],MYF(0));
   free_io_cache(entry);
 
   bitmap_clear_bit(&temp_pool, entry->temp_pool_slot);
 
-  my_free((gptr) entry,MYF(0));
+  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
   thd->proc_info=save_proc_info;
 
   DBUG_VOID_RETURN;
@@ -9063,7 +8846,7 @@
     end_select function to use. This function can't fail.
 */
 
-static Next_select_func setup_end_select_func(JOIN *join)
+Next_select_func setup_end_select_func(JOIN *join)
 {
   TABLE *table= join->tmp_table;
   Next_select_func end_select;
@@ -9218,7 +9001,7 @@
 }
 
 
-static enum_nested_loop_state
+enum_nested_loop_state
 sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 {
   enum_nested_loop_state rc;
@@ -9359,7 +9142,7 @@
     return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
 */
 
-static enum_nested_loop_state
+enum_nested_loop_state
 sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 {
   join_tab->table->null_row=0;
@@ -13782,8 +13565,7 @@
     unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
     unit->fake_select_lex->type= "UNION RESULT";
     unit->fake_select_lex->options|= SELECT_DESCRIBE;
-    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE,
-                             "")))
+    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
       res= unit->exec();
     res|= unit->cleanup();
   }

--- 1.98/sql/sql_select.h	2005-09-08 17:53:09 +04:00
+++ 1.99/sql/sql_select.h	2005-09-22 02:10:59 +04:00
@@ -101,7 +101,7 @@
 typedef enum_nested_loop_state
 (*Next_select_func)(JOIN *, struct st_join_table *, bool);
 typedef int (*Read_record_func)(struct st_join_table *tab);
-
+Next_select_func setup_end_select_func(JOIN *join);
 
 typedef struct st_join_table {
   TABLE		*table;
@@ -140,6 +140,11 @@
   void cleanup();
 } JOIN_TAB;
 
+enum_nested_loop_state sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool
+                                        end_of_records);
+enum_nested_loop_state sub_select(JOIN *join,JOIN_TAB *join_tab, bool
+                                  end_of_records);
+
 
 typedef struct st_position			/* Used in find_best */
 {
@@ -369,58 +374,6 @@
     return (unit == &thd->lex->unit && (unit->fake_select_lex == 0 ||
                                         select_lex == unit->fake_select_lex));
   }
-};
-
-
-/*
-  Server-side cursor (now stands only for basic read-only cursor)
-  See class implementation in sql_select.cc
-  A cursor has its own runtime state - list of used items and memory root of
-  used memory - which is different from Prepared statement runtime: it must
-  be different at least for the purpose of reusing the same prepared
-  statement for many cursors.
-*/
-
-class Cursor: public Sql_alloc, public Query_arena
-{
-  MEM_ROOT main_mem_root;
-  JOIN *join;
-  SELECT_LEX_UNIT *unit;
-
-  TABLE *open_tables;
-  MYSQL_LOCK *lock;
-  TABLE *derived_tables;
-  /* List of items created during execution */
-  query_id_t query_id;
-  struct Engine_info
-  {
-    const handlerton *ht;
-    void *read_view;
-  };
-  Engine_info ht_info[MAX_HA];
-public:
-  Protocol_prep protocol;
-  Item_change_list change_list;
-  select_send result;
-  THR_LOCK_OWNER lock_id;
-  my_bool close_at_commit;
-
-  /* Temporary implementation as now we replace THD state by value */
-  /* Save THD state into cursor */
-  void init_from_thd(THD *thd);
-  /* bzero cursor state in THD */
-  void reset_thd(THD *thd);
-
-  int open(JOIN *join);
-  void fetch(ulong num_rows);
-  void reset() { join= 0; }
-  bool is_open() const { return join != 0; }
-
-  void close(bool is_active);
-
-  void set_unit(SELECT_LEX_UNIT *unit_arg) { unit= unit_arg; }
-  Cursor(THD *thd);
-  ~Cursor() {}
 };
 
 

--- 1.190/sql/table.cc	2005-09-15 23:30:39 +04:00
+++ 1.191/sql/table.cc	2005-09-22 02:10:59 +04:00
@@ -1687,6 +1687,63 @@
   DBUG_RETURN(ha_checktype(thd,(enum db_type) (uint) *(head+3),0,0));
 }
 
+/*
+  Create Item_field for each column in the table.
+
+  SYNPOSIS
+    st_table::fill_item_list()
+      item_list          a pointer to an empty list used to store items
+
+  DESCRIPTION
+    Create Item_field object for each column in the table and
+    initialize it with the corresponding Field. New items are
+    created in the current THD memory root.
+
+  RETURN VALUE
+    0                    success
+    1                    out of memory
+*/
+
+bool st_table::fill_item_list(List<Item> *item_list) const
+{
+  /*
+    All Item_field's created using a direct pointer to a field
+    are fixed in Item_field constructor.
+  */
+  for (Field **ptr= field; *ptr; ptr++)
+  {
+    Item_field *item= new Item_field(*ptr);
+    if (!item || item_list->push_back(item))
+      return TRUE;
+  }
+  return FALSE;
+}
+
+/*
+  Reset an existing list of Item_field items to point to the
+  Fields of this table.
+
+  SYNPOSIS
+    st_table::fill_item_list()
+      item_list          a non-empty list with Item_fields
+
+  DESCRIPTION
+    This is a counterpart of fill_item_list used to redirect
+    Item_fields to the fields of a newly created table.
+    The caller must ensure that number of items in the item_list
+    is the same as the number of columns in the table.
+*/
+
+void st_table::reset_item_list(List<Item> *item_list) const
+{
+  List_iterator_fast<Item> it(*item_list);
+  for (Field **ptr= field; *ptr; ptr++)
+  {
+    Item_field *item_field= (Item_field*) it++;
+    DBUG_ASSERT(item_field != 0);
+    item_field->reset_field(*ptr);
+  }
+}
 
 /*
   calculate md5 of query

--- 1.114/sql/table.h	2005-09-14 12:00:31 +04:00
+++ 1.115/sql/table.h	2005-09-22 02:10:59 +04:00
@@ -267,6 +267,9 @@
   GRANT_INFO grant;
   FILESORT_INFO sort;
   TABLE_SHARE share_not_to_be_used;     /* To be deleted when true shares */
+
+  bool fill_item_list(List<Item> *item_list) const;
+  void reset_item_list(List<Item> *item_list) const;
 };
 
 

--- 1.76/sql/sql_derived.cc	2005-08-15 19:31:01 +04:00
+++ 1.77/sql/sql_derived.cc	2005-09-22 02:10:58 +04:00
@@ -27,7 +27,7 @@
 
 
 /*
-  call given derived table processor (preparing or filling tables)
+  Call given derived table processor (preparing or filling tables)
 
   SYNOPSIS
     mysql_handle_derived()
@@ -36,7 +36,6 @@
 
   RETURN
     0	ok
-    -1	Error
     1	Error and error message given
 */
 
@@ -97,14 +96,14 @@
 
   RETURN
     0	ok
-    1	Error
-    -1	Error and error message given
-  */
+    1	Error and an error message was given
+*/
 
 int mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
 {
   SELECT_LEX_UNIT *unit= orig_table_list->derived;
   int res= 0;
+  ulonglong create_options;
   DBUG_ENTER("mysql_derived_prepare");
   if (unit)
   {
@@ -118,21 +117,18 @@
     for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
       sl->context.outer_context= 0;
 
-    if (!(derived_result= new select_union(0)))
+    if (!(derived_result= new select_union))
       DBUG_RETURN(1); // out of memory
 
     // st_select_lex_unit::prepare correctly work for single select
-    if ((res= unit->prepare(thd, derived_result, 0, orig_table_list->alias)))
+    if ((res= unit->prepare(thd, derived_result, 0)))
       goto exit;
 
-    if (check_duplicate_names(unit->types, 0))
-    {
-      res= -1;
+    if ((res= check_duplicate_names(unit->types, 0)))
       goto exit;
-    }
 
-    derived_result->tmp_table_param.init();
-    derived_result->tmp_table_param.field_count= unit->types.elements;
+    create_options= (first_select->options | thd->options |
+                     TMP_TABLE_ALL_COLUMNS);
     /*
       Temp table is created so that it hounours if UNION without ALL is to be 
       processed
@@ -143,18 +139,12 @@
       !unit->union_distinct->next_select() (i.e. it is union and last distinct
       SELECT is last SELECT of UNION).
     */
-    if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param,
-				  unit->types, (ORDER*) 0,
-				  FALSE, 1,
-				  (first_select->options | thd->options |
-				   TMP_TABLE_ALL_COLUMNS),
-				  HA_POS_ERROR,
-				  orig_table_list->alias)))
-    {
-      res= -1;
+    if ((res= derived_result->create_result_table(thd, &unit->types, FALSE,
+                                                 create_options,
+                                                 orig_table_list->alias)))
       goto exit;
-    }
-    derived_result->set_table(table);
+
+    table= derived_result->table;
 
 exit:
     /* Hide "Unknown column" or "Unknown function" error */
@@ -231,9 +221,8 @@
 
   RETURN
     0	ok
-    1	Error
-    -1	Error and error message given
-  */
+    1   Error and an error message was given
+*/
 
 int mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
 {

--- 1.128/sql/sql_union.cc	2005-09-02 17:21:09 +04:00
+++ 1.129/sql/sql_union.cc	2005-09-22 02:10:59 +04:00
@@ -23,6 +23,7 @@
 
 #include "mysql_priv.h"
 #include "sql_select.h"
+#include "sql_cursor.h"
 
 bool mysql_union(THD *thd, LEX *lex, select_result *result,
                  SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
@@ -30,13 +31,9 @@
   DBUG_ENTER("mysql_union");
   bool res;
   if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
-                           setup_tables_done_option, "")))
+                           setup_tables_done_option)))
     res= unit->exec();
-  if (!res && thd->cursor && thd->cursor->is_open())
-  {
-    thd->cursor->set_unit(unit);
-  }
-  else
+  if (res || !thd->cursor || !thd->cursor->is_open())
     res|= unit->cleanup();
   DBUG_RETURN(res);
 }
@@ -46,16 +43,6 @@
 ** store records in temporary table for UNION
 ***************************************************************************/
 
-select_union::select_union(TABLE *table_par)
-  :table(table_par)
-{
-}
-
-select_union::~select_union()
-{
-}
-
-
 int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
 {
   unit= u;
@@ -103,6 +90,45 @@
   return 0;
 }
 
+/*
+  Create a temporary table to store the result of select_union.
+
+  SYNOPSIS
+    select_union::create_result_table()
+      thd                thread handle
+      column_types       a list of items used to define columns of the
+                         temporary table
+      is_union_distinct  if set, the temporary table will eliminate
+                         duplicates on insert
+      options            create options
+
+  DESCRIPTION
+    Create a temporary table that is used to store the result of a UNION,
+    derived table, or a materialized cursor.
+
+  RETURN VALUE
+    0                    The table has been created successfully.
+    1                    create_tmp_table failed.
+*/
+
+bool
+select_union::create_result_table(THD *thd, List<Item> *column_types,
+                                  bool is_union_distinct, ulonglong options,
+                                  const char *alias)
+{
+  DBUG_ASSERT(table == 0);
+  tmp_table_param.init();
+  tmp_table_param.field_count= column_types->elements;
+
+  if (! (table= create_tmp_table(thd, &tmp_table_param, *column_types,
+                                 (ORDER*) 0, is_union_distinct, 1,
+                                 options, HA_POS_ERROR, (char*) alias)))
+    return TRUE;
+  table->file->extra(HA_EXTRA_WRITE_CACHE);
+  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
+  return FALSE;
+}
+
 
 /*
   initialization procedures before fake_select_lex preparation()
@@ -133,11 +159,10 @@
 
 
 bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
-                                 ulong additional_options,
-                                 const char *tmp_table_alias)
+                                 ulong additional_options)
 {
   SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
-  SELECT_LEX *sl, *first_select;
+  SELECT_LEX *sl, *first_sl= first_select();
   select_result *tmp_result;
   bool is_union;
   TABLE *empty_table= 0;
@@ -156,7 +181,7 @@
     if (describe)
     {
       /* fast reinit for EXPLAIN */
-      for (sl= first_select_in_union(); sl; sl= sl->next_select())
+      for (sl= first_sl; sl; sl= sl->next_select())
       {
 	sl->join->result= result;
 	select_limit_cnt= HA_POS_ERROR;
@@ -175,17 +200,16 @@
   prepared= 1;
   res= FALSE;
   
-  thd_arg->lex->current_select= sl= first_select= first_select_in_union();
-  found_rows_for_union= first_select->options & OPTION_FOUND_ROWS;
-  is_union= test(first_select->next_select());
+  thd_arg->lex->current_select= sl= first_sl;
+  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
+  is_union= test(first_sl->next_select());
 
   /* Global option */
 
   if (is_union)
   {
-    if (!(tmp_result= union_result= new select_union(0)))
+    if (!(tmp_result= union_result= new select_union))
       goto err;
-    union_result->tmp_table_param.init();
     if (describe)
       tmp_result= sel_result;
   }
@@ -238,8 +262,8 @@
       information about fields lengths and exact types
     */
     if (!is_union)
-      types= first_select_in_union()->item_list;
-    else if (sl == first_select)
+      types= first_sl->item_list;
+    else if (sl == first_sl)
     {
       /*
         We need to create an empty table object. It is used
@@ -287,7 +311,6 @@
       all collations together for UNION.
     */
     List_iterator_fast<Item> tp(types);
-    Query_arena *arena= thd->stmt_arena;
     Item *type;
     ulonglong create_options;
 
@@ -301,7 +324,7 @@
       }
     }
     
-    create_options= (first_select_in_union()->options | thd_arg->options |
+    create_options= (first_sl->options | thd_arg->options |
                      TMP_TABLE_ALL_COLUMNS);
     /*
       Force the temporary table to be a MyISAM table if we're going to use
@@ -312,47 +335,35 @@
     if (global_parameters->ftfunc_list->elements)
       create_options= create_options | TMP_TABLE_FORCE_MYISAM;
 
-    union_result->tmp_table_param.field_count= types.elements;
-    if (!(table= create_tmp_table(thd_arg,
-				  &union_result->tmp_table_param, types,
-				  (ORDER*) 0, (bool) union_distinct, 1, 
-                                  create_options, HA_POS_ERROR,
-                                  (char *) tmp_table_alias)))
+    if (union_result->create_result_table(thd, &types, test(union_distinct),
+                                          create_options, ""))
       goto err;
-    table->file->extra(HA_EXTRA_WRITE_CACHE);
-    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
     bzero((char*) &result_table_list, sizeof(result_table_list));
     result_table_list.db= (char*) "";
     result_table_list.table_name= result_table_list.alias= (char*) "union";
-    result_table_list.table= table;
-    union_result->set_table(table);
+    result_table_list.table= table= union_result->table;
 
     thd_arg->lex->current_select= lex_select_save;
     if (!item_list.elements)
     {
-      Field **field;
-      Query_arena *tmp_arena,backup;
-      tmp_arena= thd->activate_stmt_arena_if_needed(&backup);
+      Query_arena *arena, backup_arena;
 
-      for (field= table->field; *field; field++)
-      {
-	Item_field *item= new Item_field(*field);
-	if (!item || item_list.push_back(item))
-	{
-          if (tmp_arena)
-	    thd->restore_active_arena(tmp_arena, &backup);
-	  DBUG_RETURN(TRUE);
-	}
-      }
-      if (tmp_arena)
-        thd->restore_active_arena(tmp_arena, &backup);
-      if (arena->is_stmt_prepare_or_first_sp_execute())
+      arena= thd->activate_stmt_arena_if_needed(&backup_arena);
+      
+      res= table->fill_item_list(&item_list);
+
+      if (arena)
+        thd->restore_active_arena(arena, &backup_arena);
+
+      if (res)
+        goto err;
+
+      if (thd->stmt_arena->is_stmt_prepare())
       {
-	/* prepare fake select to initialize it correctly */
+        /* Validate the global parameters of this union */
+
 	init_prepare_fake_select_lex(thd);
-        /*
-          Should be done only once (the only item_list per statement).
-        */
+        /* Should be done only once (the only item_list per statement) */
         DBUG_ASSERT(fake_select_lex->join == 0);
 	if (!(fake_select_lex->join= new JOIN(thd, item_list, thd->options,
 					      result)))
@@ -375,19 +386,14 @@
 	fake_select_lex->table_list.empty();
       }
     }
-    else if (!arena->is_conventional())
+    else
     {
+      DBUG_ASSERT(!thd->stmt_arena->is_conventional());
       /*
         We're in execution of a prepared statement or stored procedure:
         reset field items to point at fields from the created temporary table.
       */
-      List_iterator_fast<Item> it(item_list);
-      for (Field **field= table->field; *field; field++)
-      {
-        Item_field *item_field= (Item_field*) it++;
-        DBUG_ASSERT(item_field != 0);
-        item_field->reset_field(*field);
-      }
+      table->reset_item_list(&item_list);
     }
   }
 
@@ -404,7 +410,7 @@
 bool st_select_lex_unit::exec()
 {
   SELECT_LEX *lex_select_save= thd->lex->current_select;
-  SELECT_LEX *select_cursor=first_select_in_union();
+  SELECT_LEX *select_cursor=first_select();
   ulonglong add_rows=0;
   ha_rows examined_rows= 0;
   DBUG_ENTER("st_select_lex_unit::exec");
@@ -595,7 +601,7 @@
     table= 0; // Safety
   }
 
-  for (SELECT_LEX *sl= first_select_in_union(); sl; sl= sl->next_select())
+  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
     error|= sl->cleanup();
 
   if (fake_select_lex)
@@ -652,7 +658,7 @@
                                        select_subselect *old_result)
 {
   bool res= FALSE;
-  for (SELECT_LEX *sl= first_select_in_union(); sl; sl= sl->next_select())
+  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
   {
     if (sl->join && sl->join->result == old_result)
       if (sl->join->change_result(result))
@@ -663,6 +669,36 @@
   return (res);
 }
 
+/*
+  Get column type information for this unit.
+
+  SYNOPSIS
+    st_select_lex_unit::get_unit_column_types()
+
+  DESCRIPTION
+    For a single-select the column types are taken
+    from the list of selected items. For a union this function
+    assumes that st_select_lex_unit::prepare has been called
+    and returns the type holders that were created for unioned
+    column types of all selects.
+
+  NOTES
+    The implementation of this function should be in sync with
+    st_select_lex_unit::prepare()
+*/
+
+List<Item> *st_select_lex_unit::get_unit_column_types()
+{
+  bool is_union= test(first_select()->next_select());
+
+  if (is_union)
+  {
+    DBUG_ASSERT(prepared);
+    /* Types are generated during prepare */
+    return &types;
+  }
+  return &first_select()->item_list;
+}
 
 bool st_select_lex::cleanup()
 {

--- 1.118/sql/item_subselect.cc	2005-09-02 17:21:07 +04:00
+++ 1.119/sql/item_subselect.cc	2005-09-22 02:10:57 +04:00
@@ -1468,7 +1468,7 @@
 
 int subselect_union_engine::prepare()
 {
-  return unit->prepare(thd, result, SELECT_NO_UNLOCK, "");
+  return unit->prepare(thd, result, SELECT_NO_UNLOCK);
 }
 
 int subselect_uniquesubquery_engine::prepare()

--- 1.63/sql/sql_view.cc	2005-09-14 13:26:02 +04:00
+++ 1.64/sql/sql_view.cc	2005-09-22 02:10:59 +04:00
@@ -378,7 +378,7 @@
 
   /* prepare select to resolve all fields */
   lex->view_prepare_mode= 1;
-  if (unit->prepare(thd, 0, 0, view->view_name.str))
+  if (unit->prepare(thd, 0, 0))
   {
     /*
       some errors from prepare are reported to user, if is not then

--- 1.1/mysql-test/r/sp-big.result	2005-07-27 05:08:45 +04:00
+++ 1.2/mysql-test/r/sp-big.result	2005-09-22 02:10:57 +04:00
@@ -13,3 +13,49 @@
 3
 drop procedure test.longprocedure;
 drop table t1;
+create table t1 (f1 char(100) , f2 mediumint , f3 int , f4 real, f5 numeric);
+insert into t1 (f1, f2, f3, f4, f5) values
+("This is a test case for for Bug#9819", 1, 2, 3.0, 4.598);
+Warnings:
+Note	1265	Data truncated for column 'f5' at row 1
+create table t2 like t1;
+select count(*) from t1;
+count(*)
+256
+select count(*) from t2;
+count(*)
+0
+create procedure p1()
+begin
+declare done integer default 0;
+declare vf1 char(100) ;
+declare vf2 mediumint;
+declare vf3 int ;
+declare vf4 real ;
+declare vf5 numeric ;
+declare cur1 cursor for select f1,f2,f3,f4,f5 from t1;  
+declare continue handler for sqlstate '02000' set done = 1; 
+open cur1;
+while done <> 1 do
+fetch cur1 into vf1, vf2, vf3, vf4, vf5;
+if not done then
+insert into t2 values (vf1, vf2, vf3, vf4, vf5);
+end if;
+end while;
+close cur1;
+end|
+call p1();
+select count(*) from t1;
+count(*)
+256
+select count(*) from t2;
+count(*)
+256
+select f1 from t1 limit 1;
+f1
+This is a test case for for Bug#9819
+select f1 from t2 limit 1;
+f1
+This is a test case for for Bug#9819
+drop procedure p1;
+drop table t1, t2;

--- 1.1/mysql-test/t/sp-big.test	2005-07-27 05:08:45 +04:00
+++ 1.2/mysql-test/t/sp-big.test	2005-09-22 02:10:57 +04:00
@@ -31,3 +31,52 @@
 
 drop procedure test.longprocedure;
 drop table t1;
+#
+# Bug #9819 "Cursors: Mysql Server Crash while fetching from table with 5
+# million records.": 
+# To really test the bug, increase the number of loop iterations ($1).
+# For 4 millions set $1 to 22.
+create table t1 (f1 char(100) , f2 mediumint , f3 int , f4 real, f5 numeric);
+insert into t1 (f1, f2, f3, f4, f5) values
+("This is a test case for for Bug#9819", 1, 2, 3.0, 4.598);
+create table t2 like t1;
+let $1=8;
+--disable_query_log
+--disable_result_log
+while ($1)
+{
+  eval insert into t1 select * from t1;
+  dec $1;
+}
+--enable_result_log
+--enable_query_log
+select count(*) from t1;
+select count(*) from t2;
+delimiter |;
+create procedure p1()
+begin
+  declare done integer default 0;
+  declare vf1 char(100) ;
+  declare vf2 mediumint;
+  declare vf3 int ;
+  declare vf4 real ;
+  declare vf5 numeric ;
+  declare cur1 cursor for select f1,f2,f3,f4,f5 from t1;  
+  declare continue handler for sqlstate '02000' set done = 1; 
+  open cur1;
+  while done <> 1 do
+    fetch cur1 into vf1, vf2, vf3, vf4, vf5;
+    if not done then
+      insert into t2 values (vf1, vf2, vf3, vf4, vf5);
+    end if;
+  end while;
+  close cur1;
+end|
+delimiter ;|
+call p1();
+select count(*) from t1;
+select count(*) from t2;
+select f1 from t1 limit 1;
+select f1 from t2 limit 1;
+drop procedure p1;
+drop table t1, t2;
--- New file ---
+++ sql/sql_cursor.cc	05/09/22 02:10:59
/* Copyright (C) 2005 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation                         /* gcc class implementation */
#endif

#include "mysql_priv.h"
#include "sql_cursor.h"
#include "sql_select.h"

/****************************************************************************
  Declarations.
****************************************************************************/

/*
  Sensitive_cursor -- a sensitive non-materialized server side
  cursor An instance of this class cursor has its own runtime
  state -- list of used items and memory root for runtime memory,
  open and locked tables, change list for the changes of the
  parsed tree. This state is freed when the cursor is closed.
*/

class Sensitive_cursor: public Server_side_cursor
{
  MEM_ROOT main_mem_root;
  Query_arena *stmt_arena;
  JOIN *join;
  TABLE *open_tables;
  MYSQL_LOCK *lock;
  TABLE *derived_tables;
  /* List of items created during execution */
  query_id_t query_id;
  struct Engine_info
  {
    const handlerton *ht;
    void *read_view;
  };
  Engine_info ht_info[MAX_HA];
  Item_change_list change_list;
  my_bool close_at_commit;
  THR_LOCK_OWNER lock_id;
private:
  /* bzero cursor state in THD */
  void reset_thd(THD *thd);
public:
  Sensitive_cursor(THD *thd, select_result *result_arg);

  THR_LOCK_OWNER *get_lock_id() { return &lock_id; }
  /* Save THD state into cursor */
  void post_open(THD *thd);

  virtual bool is_open() const { return join != 0; }
  virtual int open(JOIN *join);
  virtual void fetch(ulong num_rows);
  virtual void close();
  virtual ~Sensitive_cursor();
};


/*
  Materialized_cursor -- an insensitive materialized server-side
  cursor. The result set of this cursor is saved in a temporary
  table at open. The cursor itself is simply an interface for the
  handler of the temporary table.
*/

class Materialized_cursor: public Server_side_cursor
{
  MEM_ROOT main_mem_root;
  /* A fake unit to supply to select_send when fetching */
  SELECT_LEX_UNIT fake_unit;
  TABLE *table;
  List<Item> item_list;
  ulong fetch_limit;
  ulong fetch_count;
public:
  Materialized_cursor(select_result *result, TABLE *table);

  virtual bool is_open() const { return table != 0; }
  virtual int open(JOIN *join __attribute__((unused)));
  virtual void fetch(ulong num_rows);
  virtual void close();
  virtual ~Materialized_cursor();
};


/*
  Select_materialize -- a mediator between a cursor query and the
  protocol. In case we were not able to open a non-materialzed
  cursor, it creates an internal temporary HEAP table, and insert
  all rows into it. When the table reaches max_heap_table_size,
  it's converted to a MyISAM table. Later this table is used to
  create a Materialized_cursor.
*/

class Select_materialize: public select_union
{
  select_result *result; /* the result object of the caller (PS or SP) */
public:
  Select_materialize(select_result *result_arg) :result(result_arg) {}
  virtual bool send_fields(List<Item> &list, uint flags);
};


/**************************************************************************/

/*
  Attempt to open a materialized or non-materialized cursor.

  SYNOPSIS
    mysql_open_cursor()
      thd                thread handle
      flags   [in]       create a materialized cursor or not
      result  [in]       result class of the caller used as a destination
                         for the rows fetched from the cursor
      pcursor [out]      a pointer to store a pointer to cursor in

  RETURN VALUE
    0                    the query has been successfully executed; in this
                         case pcursor may or may not contain
                         a pointer to an open cursor.
    non-zero             an error, 'pcursor' has been left intact.
*/

int mysql_open_cursor(THD *thd, uint flags, select_result *result,
                      Server_side_cursor **pcursor)
{
  Sensitive_cursor *sensitive_cursor;
  select_result *save_result;
  Select_materialize *result_materialize;
  LEX *lex= thd->lex;
  int rc;

  /*
    The lifetime of the sensitive cursor is the same or less as the
    lifetime of the runtime memory of the statement it's opened for.
  */
  if (! (result_materialize= new (thd->mem_root) Select_materialize(result)))
    return 1;

  if (! (sensitive_cursor= new (thd->mem_root) Sensitive_cursor(thd, result)))
  {
    delete result;
    return 1;
  }

  save_result= lex->result;

  lex->result= result_materialize;
  if (! (flags & (uint) ALWAYS_MATERIALIZED_CURSOR))
  {
    thd->lock_id= sensitive_cursor->get_lock_id();
    thd->cursor= sensitive_cursor;
  }

  rc= mysql_execute_command(thd);

  lex->result= save_result;
  thd->lock_id= &thd->main_lock_id;
  thd->cursor= 0;

  /*
    Possible options here:
    - a sensitive cursor is open. In this case rc is 0 and
      result_materialize->table is NULL, or
    - a materialized cursor is open. In this case rc is 0 and
      result_materialize->table is not NULL
    - an error occured during materializaton.
      result_materialize->table is not NULL, but rc != 0
    - successful completion of mysql_execute_command without
      a cursor: rc is 0, result_materialize->table is NULL,
      sensitive_cursor is not open.
      This is possible if some command writes directly to the
      network, bypassing select_result mechanism. An example of
      such command is SHOW VARIABLES or SHOW STATUS.
  */
  if (rc)
    goto err_open;

  if (sensitive_cursor->is_open())
  {
    DBUG_ASSERT(!result_materialize->table);
    /*
      It's safer if we grab THD state after mysql_execute_command
      is finished and not in Sensitive_cursor::open(), because
      currently the call to Sensitive_cursor::open is buried deep
      in JOIN::exec of the top level join.
    */
    sensitive_cursor->post_open(thd);
    *pcursor= sensitive_cursor;
    goto end;
  }
  else if (result_materialize->table)
  {
    Materialized_cursor *materialized_cursor;
    TABLE *table= result_materialize->table;
    MEM_ROOT *mem_root= &table->mem_root;

    if (!(materialized_cursor= new (mem_root)
                               Materialized_cursor(result, table)))
    {
      rc= 1;
      goto err_open;
    }

    if ((rc= materialized_cursor->open(0)))
    {
      delete materialized_cursor;
      goto err_open;
    }

    *pcursor= materialized_cursor;
    thd->stmt_arena->cleanup_stmt();
    goto end;
  }

err_open:
  DBUG_ASSERT(! (sensitive_cursor && sensitive_cursor->is_open()));
  delete sensitive_cursor;
  if (result_materialize->table)
    free_tmp_table(thd, result_materialize->table);
end:
  delete result_materialize;
  return rc;
}

/****************************************************************************
  Server_side_cursor
****************************************************************************/

Server_side_cursor::~Server_side_cursor()
{
}


void Server_side_cursor::operator delete(void *ptr, size_t size)
{
  Server_side_cursor *cursor= (Server_side_cursor*) ptr;
  MEM_ROOT own_root= *cursor->mem_root;

  DBUG_ENTER("Server_side_cursor::operator delete");
  TRASH(ptr, size);
  /*
    If this cursor has never been opened mem_root is empty. Otherwise
    mem_root points to the memory the cursor object was allocated in.
    In this case it's important to call free_root last, and free a copy
    instead of *mem_root to avoid writing into freed memory.
  */
  free_root(&own_root, MYF(0));
  DBUG_VOID_RETURN;
}

/****************************************************************************
  Sensitive_cursor
****************************************************************************/

Sensitive_cursor::Sensitive_cursor(THD *thd, select_result *result_arg)
   :Server_side_cursor(&main_mem_root, result_arg),
   stmt_arena(0),
   join(0),
   close_at_commit(FALSE)
{
  /* We will overwrite it at open anyway. */
  init_sql_alloc(&main_mem_root, ALLOC_ROOT_MIN_BLOCK_SIZE, 0);
  thr_lock_owner_init(&lock_id, &thd->lock_info);
  bzero((void*) ht_info, sizeof(ht_info));
}


void
Sensitive_cursor::post_open(THD *thd)
{
  Engine_info *info;
  /*
    We need to save and reset thd->mem_root, otherwise it'll be
    freed later in mysql_parse.

    We can't just change thd->mem_root here as we want to keep the
    things that are already allocated in thd->mem_root for
    Sensitive_cursor::fetch()
  */
  *mem_root=  *thd->mem_root;
  stmt_arena= thd->stmt_arena;
  state= stmt_arena->state;
  /* Allocate a new memory root for thd */
  init_sql_alloc(thd->mem_root,
                 thd->variables.query_alloc_block_size,
                 thd->variables.query_prealloc_size);

  /*
    Save tables and zero THD pointers to prevent table close in
    close_thread_tables.
  */
  derived_tables= thd->derived_tables;
  open_tables=    thd->open_tables;
  lock=           thd->lock;
  query_id=       thd->query_id;
  free_list=      thd->free_list;
  change_list=    thd->change_list;
  reset_thd(thd);
  /* Now we have an active cursor and can cause a deadlock */
  thd->lock_info.n_cursors++;

  close_at_commit= FALSE; /* reset in case we're reusing the cursor */
  info= &ht_info[0];
  for (handlerton **pht= thd->transaction.stmt.ht; *pht; pht++)
  {
    const handlerton *ht= *pht;
    close_at_commit|= test(ht->flags & HTON_CLOSE_CURSORS_AT_COMMIT);
    if (ht->create_cursor_read_view)
    {
      info->ht= ht;
      info->read_view= (ht->create_cursor_read_view)();
      ++info;
    }
  }
  /*
    XXX: thd->locked_tables is not changed.
    What problems can we have with it if cursor is open?
    TODO: must be fixed because of the prelocked mode.
  */
}


void
Sensitive_cursor::reset_thd(THD *thd)
{
  thd->derived_tables= 0;
  thd->open_tables= 0;
  thd->lock= 0;
  thd->free_list= 0;
  thd->change_list.empty();
}


int
Sensitive_cursor::open(JOIN *join_arg)
{
  join= join_arg;
  THD *thd= join->thd;
  /* First non-constant table */
  JOIN_TAB *join_tab= join->join_tab + join->const_tables;
  DBUG_ENTER("Sensitive_cursor::open");

  join->change_result(result);
  /*
    Send fields description to the client; server_status is sent
    in 'EOF' packet, which follows send_fields().
    We don't simply use SEND_EOF flag of send_fields because we also
    want to flush the network buffer, which is done only in a standalone
    send_eof().
  */
  result->send_fields(*join->fields, Protocol::SEND_NUM_ROWS);
  thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
  result->send_eof();
  thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;

  /* Prepare JOIN for reading rows. */
  join->tmp_table= 0;
  join->join_tab[join->tables-1].next_select= setup_end_select_func(join);
  join->send_records= 0;
  join->fetch_limit= join->unit->offset_limit_cnt;

  /* Disable JOIN CACHE as it is not working with cursors yet */
  for (JOIN_TAB *tab= join_tab;
       tab != join->join_tab + join->tables - 1;
       tab++)
  {
    if (tab->next_select == sub_select_cache)
      tab->next_select= sub_select;
  }

  DBUG_ASSERT(join_tab->table->reginfo.not_exists_optimize == 0);
  DBUG_ASSERT(join_tab->not_used_in_distinct == 0);
  /*
    null_row is set only if row not found and it's outer join: should never
    happen for the first table in join_tab list
  */
  DBUG_ASSERT(join_tab->table->null_row == 0);
  DBUG_RETURN(0);
}


/*
  SYNOPSIS
    Sensitive_cursor::fetch()
      num_rows           fetch up to this number of rows (maybe less)

  DESCRIPTION
    Fetch next num_rows rows from the cursor and send them to the client

    Precondition:
    Sensitive_cursor is open

  RETURN VALUES:
    none, this function will send OK to the clinet or set an error
    message in THD
*/

void
Sensitive_cursor::fetch(ulong num_rows)
{
  THD *thd= join->thd;
  JOIN_TAB *join_tab= join->join_tab + join->const_tables;
  enum_nested_loop_state error= NESTED_LOOP_OK;
  Query_arena backup_arena;
  Engine_info *info;
  DBUG_ENTER("Sensitive_cursor::fetch");
  DBUG_PRINT("enter",("rows: %lu", num_rows));

  DBUG_ASSERT(thd->derived_tables == 0 && thd->open_tables == 0 &&
              thd->lock == 0);

  thd->derived_tables= derived_tables;
  thd->open_tables= open_tables;
  thd->lock= lock;
  thd->query_id= query_id;
  thd->change_list= change_list;
  /* save references to memory allocated during fetch */
  thd->set_n_backup_active_arena(this, &backup_arena);

  for (info= ht_info; info->read_view ; info++)
    (info->ht->set_cursor_read_view)(info->read_view);

  join->fetch_limit+= num_rows;

  error= sub_select(join, join_tab, 0);
  if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
    error= sub_select(join,join_tab,1);
  if (error == NESTED_LOOP_QUERY_LIMIT)
    error= NESTED_LOOP_OK;                    /* select_limit used */
  if (error == NESTED_LOOP_CURSOR_LIMIT)
    join->resume_nested_loop= TRUE;

#ifdef USING_TRANSACTIONS
    ha_release_temporary_latches(thd);
#endif
  /* Grab free_list here to correctly free it in close */
  thd->restore_active_arena(this, &backup_arena);

  change_list= thd->change_list;
  reset_thd(thd);

  for (info= ht_info; info->read_view; info++)
    (info->ht->set_cursor_read_view)(0);

  if (error == NESTED_LOOP_CURSOR_LIMIT)
  {
    /* Fetch limit worked, possibly more rows are there */
    thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
    result->send_eof();
    thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
  }
  else
  {
    close();
    if (error == NESTED_LOOP_OK)
    {
      thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
      result->send_eof();
      thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
    }
    else if (error != NESTED_LOOP_KILLED)
      my_message(ER_OUT_OF_RESOURCES, ER(ER_OUT_OF_RESOURCES), MYF(0));
  }
  DBUG_VOID_RETURN;
}


void
Sensitive_cursor::close()
{
  THD *thd= join->thd;
  DBUG_ENTER("Sensitive_cursor::close");

  for (Engine_info *info= ht_info; info->read_view; info++)
  {
    (info->ht->close_cursor_read_view)(info->read_view);
    info->read_view= 0;
    info->ht= 0;
  }

  thd->change_list= change_list;
  {
    /*
      XXX: Another hack: we need to set THD state as if in a fetch to be
      able to call stmt close.
    */
    DBUG_ASSERT(lock || open_tables || derived_tables);

    TABLE *tmp_derived_tables= thd->derived_tables;
    MYSQL_LOCK *tmp_lock= thd->lock;

    thd->open_tables= open_tables;
    thd->derived_tables= derived_tables;
    thd->lock= lock;

    /* Is expected to at least close tables and empty thd->change_list */
    stmt_arena->cleanup_stmt();

    thd->open_tables= tmp_derived_tables;
    thd->derived_tables= tmp_derived_tables;
    thd->lock= tmp_lock;
  }
  thd->lock_info.n_cursors--; /* Decrease the number of active cursors */
  join= 0;
  stmt_arena= 0;
  free_items();
  change_list.empty();
  DBUG_VOID_RETURN;
}


Sensitive_cursor::~Sensitive_cursor()
{
  if (is_open())
    close();
}

/***************************************************************************
 Materialized_cursor
****************************************************************************/

Materialized_cursor::Materialized_cursor(select_result *result_arg,
                                         TABLE *table_arg)
  :Server_side_cursor(&table_arg->mem_root, result_arg),
  table(table_arg),
  fetch_limit(0),
  fetch_count(0)
{
  fake_unit.init_query();
  fake_unit.thd= table->in_use;
}


int Materialized_cursor::open(JOIN *join __attribute__((unused)))
{
  THD *thd= fake_unit.thd;
  int rc;
  Query_arena backup_arena;

  thd->set_n_backup_active_arena(this, &backup_arena);
  /* Create a list of fields and start sequential scan */
  rc= (table->fill_item_list(&item_list) ||
       result->prepare(item_list, &fake_unit) ||
       table->file->ha_rnd_init(TRUE));
  thd->restore_active_arena(this, &backup_arena);
  return rc;
}


/*
  Fetch up to the given number of rows from a materialized cursor.

  DESCRIPTION
    Precondition: the cursor is open.

    If the cursor points after the last row, the fetch will automatically
    close the cursor and not send any data (except the 'EOF' packet
    with SERVER_STATUS_LAST_ROW_SENT). This is an extra round trip
    and probably should be improved to return
    SERVER_STATUS_LAST_ROW_SENT along with the last row.

  RETURN VALUE
    none, in case of success the row is sent to the client, otherwise
    an error message is set in THD
*/

void Materialized_cursor::fetch(ulong num_rows)
{
  THD *thd= table->in_use;

  int res= 0;
  for (fetch_limit+= num_rows; fetch_count < fetch_limit; fetch_count++)
  {
    if ((res= table->file->rnd_next(table->record[0])))
      break;
    /* Send data only if the read was successful. */
    result->send_data(item_list);
  }

  switch (res) {
  case 0:
    thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
    result->send_eof();
    thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
    break;
  case HA_ERR_END_OF_FILE:
    thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
    result->send_eof();
    thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT;
    close();
    break;
  default:
    table->file->print_error(res, MYF(0));
    close();
    break;
  }
}


void Materialized_cursor::close()
{
  /* Free item_list items */
  free_items();
  (void) table->file->ha_rnd_end();
  /*
    We need to grab table->mem_root to prevent free_tmp_table from freeing:
    the cursor object was allocated in this memory.
  */
  main_mem_root= table->mem_root;
  mem_root= &main_mem_root;
  clear_alloc_root(&table->mem_root);
  free_tmp_table(table->in_use, table);
  table= 0;
}


Materialized_cursor::~Materialized_cursor()
{
  if (is_open())
    close();
}


/***************************************************************************
 Select_materialize
****************************************************************************/

bool Select_materialize::send_fields(List<Item> &list, uint flags)
{
  bool rc;
  DBUG_ASSERT(table == 0);
  if (create_result_table(unit->thd, unit->get_unit_column_types(),
                          FALSE, thd->options | TMP_TABLE_ALL_COLUMNS, ""))
    return TRUE;
  /*
    We can't simply supply SEND_EOF flag to send_fields, because send_fields
    doesn't flush the network buffer.
  */
  rc= result->send_fields(list, Protocol::SEND_NUM_ROWS);
  thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
  result->send_eof();
  thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
  return rc;
}


--- New file ---
+++ sql/sql_cursor.h	05/09/22 02:11:00
#ifndef _sql_cursor_h_
#define _sql_cursor_h_
/* Copyright (C) 2005 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifdef USE_PRAGMA_INTERFACE
#pragma interface                              /* gcc class interface */
#endif

/*
  Declarations for implementation of server side cursors. Only
  read-only non-scrollable cursors are currently implemented.
*/

/*
  Server_side_cursor -- an interface for materialized and
  sensitive (non-materialized) implementation of cursors. All
  cursors are self-contained (created in their own memory root).
  For that reason they must be deleted only using a pointer to
  Server_side_cursor, not to its base class.
*/

class Server_side_cursor: protected Query_arena, public Sql_alloc
{
protected:
  /* Row destination used for fetch */
  select_result *result;
public:
  Server_side_cursor(MEM_ROOT *mem_root_arg, select_result *result_arg)
    :Query_arena(mem_root_arg, INITIALIZED), result(result_arg)
  {}

  virtual bool is_open() const= 0;

  virtual int open(JOIN *top_level_join)= 0;
  virtual void fetch(ulong num_rows)= 0;
  virtual void close()= 0;
  virtual ~Server_side_cursor();

  static void operator delete(void *ptr, size_t size);
};


int mysql_open_cursor(THD *thd, uint flags,
                      select_result *result,
                      Server_side_cursor **res);

/* Possible values for flags */

enum { ANY_CURSOR= 1, ALWAYS_MATERIALIZED_CURSOR= 2 };

#endif /* _sql_cusor_h_ */


--- 1.13/mysql-test/r/ctype_ujis.result	2005-07-20 12:40:10 +04:00
+++ 1.14/mysql-test/r/ctype_ujis.result	2005-09-22 02:10:57 +04:00
@@ -2271,3 +2271,33 @@
 c1
 abcde111
 drop table t1;
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS sp1;
+set names ujis;
+set character_set_database = ujis;
+set character_set_server = ujis;
+CREATE TABLE t1(c1 char(2)) default charset = ujis;
+CREATE TABLE t2(c2 char(2)) default charset = ujis;
+INSERT INTO t1 VALUES(_ujis 0xA4A2);
+CREATE PROCEDURE sp1()
+BEGIN
+DECLARE a CHAR(1);
+DECLARE cur1 CURSOR FOR SELECT c1 FROM t1;
+OPEN cur1;
+FETCH cur1 INTO a;
+INSERT INTO t2 VALUES (a);
+CLOSE cur1;
+END|
+CALL sp1();
+SELECT c1,c2 FROM t1,t2;
+c1	c2
+SELECT hex(convert(_latin1 0xA4A2 using ujis)),hex(c2) FROM t1,t2;
+hex(convert(_latin1 0xA4A2 using ujis))	hex(c2)
+8FA2F0A1F1	A4A2
+DROP PROCEDURE sp1;
+DROP TABLE t1;
+DROP TABLE t2;
+set names default;
+set character_set_database=default;
+set character_set_server=default;

--- 1.13/mysql-test/t/ctype_ujis.test	2005-07-28 04:21:40 +04:00
+++ 1.14/mysql-test/t/ctype_ujis.test	2005-09-22 02:10:57 +04:00
@@ -1151,3 +1151,45 @@
 -- source include/ctype_innodb_like.inc
 
 # End of 4.1 tests
+--disable_warnings
+DROP TABLE IF EXISTS t1, t2;
+DROP PROCEDURE IF EXISTS sp1;
+--enable_warnings
+
+set names ujis;
+set character_set_database = ujis;
+set character_set_server = ujis;
+
+CREATE TABLE t1(c1 char(2)) default charset = ujis;
+CREATE TABLE t2(c2 char(2)) default charset = ujis;
+
+INSERT INTO t1 VALUES(_ujis 0xA4A2);
+
+DELIMITER |;
+CREATE PROCEDURE sp1()
+BEGIN
+  DECLARE a CHAR(1);
+  DECLARE cur1 CURSOR FOR SELECT c1 FROM t1;
+  OPEN cur1;
+  FETCH cur1 INTO a;
+  INSERT INTO t2 VALUES (a);
+  CLOSE cur1;
+END|
+DELIMITER ;|
+CALL sp1();
+
+#The data in t1 and t2 should be the same but different
+SELECT c1,c2 FROM t1,t2;
+
+#Since the result of hex(convert(_latin1 0xA4A2 using ujis))
+#equals to hex(c2), it seems that the value which was inserted 
+#by using cursor is interpreted as latin1 character set
+SELECT hex(convert(_latin1 0xA4A2 using ujis)),hex(c2) FROM t1,t2;
+
+DROP PROCEDURE sp1;
+DROP TABLE t1;
+DROP TABLE t2;
+
+set names default;
+set character_set_database=default;
+set character_set_server=default;

--- 1.29/sql/protocol.h	2005-06-30 16:17:05 +04:00
+++ 1.30/sql/protocol.h	2005-09-22 02:10:57 +04:00
@@ -150,30 +150,6 @@
   virtual bool store(Field *field);
 };
 
-class Protocol_cursor :public Protocol_simple
-{
-public:
-  MEM_ROOT *alloc;
-  MYSQL_FIELD *fields;
-  MYSQL_ROWS *data;
-  MYSQL_ROWS **prev_record;
-  ulong row_count;
-
-  Protocol_cursor() :data(NULL) {}
-  Protocol_cursor(THD *thd_arg, MEM_ROOT *ini_alloc) :Protocol_simple(thd_arg), alloc(ini_alloc), data(NULL) {}
-  bool prepare_for_send(List<Item> *item_list) 
-  {
-    row_count= 0;
-    fields= NULL;
-    data= NULL;
-    prev_record= &data;
-    return Protocol_simple::prepare_for_send(item_list);
-  }
-  bool send_fields(List<Item> *list, uint flags);
-  bool write();
-  uint get_field_count() { return field_count; }
-};
-
 void send_warning(THD *thd, uint sql_errno, const char *err=0);
 void net_printf_error(THD *thd, uint sql_errno, ...);
 void net_send_error(THD *thd, uint sql_errno=0, const char *err=0);

--- 1.61/libmysqld/Makefile.am	2005-07-06 01:56:19 +04:00
+++ 1.62/libmysqld/Makefile.am	2005-09-22 02:10:57 +04:00
@@ -60,7 +60,7 @@
 	sql_string.cc sql_table.cc sql_test.cc sql_udf.cc \
 	sql_update.cc sql_yacc.cc table.cc thr_malloc.cc time.cc \
 	unireg.cc uniques.cc stacktrace.c sql_union.cc hash_filo.cc \
-	spatial.cc gstream.cc sql_help.cc tztime.cc protocol_cursor.cc \
+	spatial.cc gstream.cc sql_help.cc tztime.cc sql_cursor.cc \
 	sp_head.cc sp_pcontext.cc sp.cc sp_cache.cc sp_rcontext.cc \
 	parse_file.cc sql_view.cc sql_trigger.cc my_decimal.cc \
 	ha_blackhole.cc

--- 1.184/sql/sp_head.cc	2005-09-15 03:57:52 +04:00
+++ 1.185/sql/sp_head.cc	2005-09-22 02:10:57 +04:00
@@ -199,11 +199,18 @@
   Item *it= sp_prepare_func_item(thd, it_addr);
   uint rsize;
   Query_arena backup_arena;
+  Item *old_item_next, *old_free_list, **p_free_list;
   DBUG_PRINT("info", ("type: %d", type));
 
   if (!it)
-  {
     DBUG_RETURN(NULL);
+
+  if (reuse)
+  {
+    old_item_next= reuse->next;
+    p_free_list= use_callers_arena ? &thd->spcont->callers_arena->free_list :
+                                     &thd->free_list;
+    old_free_list= *p_free_list;
   }
 
   switch (sp_map_result_type(type)) {
@@ -312,15 +319,23 @@
   default:
     DBUG_ASSERT(0);
   }
-  it->rsize= rsize;
-
-  DBUG_RETURN(it);
+  goto end;
 
 return_null_item:
   CREATE_ON_CALLERS_ARENA(it= new(reuse, &rsize) Item_null(),
                     use_callers_arena, &backup_arena);
+end:
   it->rsize= rsize;
 
+  if (reuse && it == reuse)
+  {
+    /*
+      The Item constructor registered itself in the arena free list,
+      while the item slot is reused, so we have to restore the list.
+    */
+    it->next= old_item_next;
+    *p_free_list= old_free_list;
+  }
   DBUG_RETURN(it);
 }
 
@@ -1358,14 +1373,6 @@
 	  uint offset= static_cast<Item_splocal *>(it)->get_offset();
 	  Item *val= nctx->get_item(i);
 	  Item *orig= octx->get_item(offset);
-	  Item *o_item_next;
-          /* we'll use callers_arena in sp_eval_func_item */
-	  Item *o_free_list= thd->spcont->callers_arena->free_list;
-
-	  LINT_INIT(o_item_next);
-
-	  if (orig)
-	    o_item_next= orig->next;
 
           /*
             We might need to allocate new item if we weren't able to
@@ -1380,15 +1387,6 @@
 	  }
 	  if (copy != orig)
 	    octx->set_item(offset, copy);
-	  if (orig && copy == orig)
-	  {
-	    /*
-              A reused item slot, where the constructor put it in the
-              free_list, so we have to restore the list.
-            */
-	    thd->spcont->callers_arena->free_list= o_free_list;
-	    copy->next= o_item_next;
-	  }
 	}
 	else
 	{
@@ -2476,6 +2474,10 @@
 int
 sp_instr_copen::execute(THD *thd, uint *nextp)
 {
+  /*
+    We don't store a pointer to the cursor in the instruction to be
+    able to reuse the same instruction among different threads in future.
+  */
   sp_cursor *c= thd->spcont->get_cursor(m_cursor);
   int res;
   DBUG_ENTER("sp_instr_copen::execute");
@@ -2484,41 +2486,33 @@
     res= -1;
   else
   {
-    sp_lex_keeper *lex_keeper= c->pre_open(thd);
-    if (!lex_keeper)                            // cursor already open or OOM
-    {
-      res= -1;
-      *nextp= m_ip+1;
-    }
-    else
-    {
-      Query_arena *old_arena= thd->stmt_arena;
+    sp_lex_keeper *lex_keeper= c->get_lex_keeper();
+    Query_arena *old_arena= thd->stmt_arena;
 
-      /*
-        Get the Query_arena from the cpush instruction, which contains
-        the free_list of the query, so new items (if any) are stored in
-        the right free_list, and we can cleanup after each open.
-      */
-      thd->stmt_arena= c->get_instr();
-      res= lex_keeper->reset_lex_and_exec_core(thd, nextp, FALSE, this);
-      /* Cleanup the query's items */
-      if (thd->stmt_arena->free_list)
-        cleanup_items(thd->stmt_arena->free_list);
-      thd->stmt_arena= old_arena;
-      /*
-        Work around the fact that errors in selects are not returned properly
-        (but instead converted into a warning), so if a condition handler
-        caught, we have lost the result code.
-      */
-      if (!res)
-      {
-        uint dummy1, dummy2;
+    /*
+      Get the Query_arena from the cpush instruction, which contains
+      the free_list of the query, so new items (if any) are stored in
+      the right free_list, and we can cleanup after each open.
+    */
+    thd->stmt_arena= c->get_instr();
+    res= lex_keeper->reset_lex_and_exec_core(thd, nextp, FALSE, this);
+    /* Cleanup the query's items */
+    if (thd->stmt_arena->free_list)
+      cleanup_items(thd->stmt_arena->free_list);
+    thd->stmt_arena= old_arena;
+    /*
+      Work around the fact that errors in selects are not returned properly
+      (but instead converted into a warning), so if a condition handler
+      caught, we have lost the result code.
+    */
+    if (!res)
+    {
+      uint dummy1, dummy2;
 
-        if (thd->spcont->found_handler(&dummy1, &dummy2))
-  	  res= -1;
-      }
-      c->post_open(thd, res ? FALSE : TRUE);
+      if (thd->spcont->found_handler(&dummy1, &dummy2))
+        res= -1;
     }
+    /* TODO: Assert here that we either have an error or a cursor */
   }
   DBUG_RETURN(res);
 }
@@ -2527,7 +2521,8 @@
 int
 sp_instr_copen::exec_core(THD *thd, uint *nextp)
 {
-  int res= mysql_execute_command(thd);
+  sp_cursor *c= thd->spcont->get_cursor(m_cursor);
+  int res= c->open(thd);
   *nextp= m_ip+1;
   return res;
 }
@@ -2582,14 +2577,7 @@
   Query_arena backup_arena;
   DBUG_ENTER("sp_instr_cfetch::execute");
 
-  if (! c)
-    res= -1;
-  else
-  {
-    thd->set_n_backup_active_arena(thd->spcont->callers_arena, &backup_arena);
-    res= c->fetch(thd, &m_varlist);
-    thd->restore_active_arena(thd->spcont->callers_arena, &backup_arena);
-  }
+  res= c ? c->fetch(thd, &m_varlist) : -1;
 
   *nextp= m_ip+1;
   DBUG_RETURN(res);

--- 1.70/sql/sp_head.h	2005-09-14 12:53:55 +04:00
+++ 1.71/sql/sp_head.h	2005-09-22 02:10:57 +04:00
@@ -860,6 +860,12 @@
 
   virtual void print(String *str);
 
+  /*
+    This call is used to cleanup the instruction when a sensitive
+    cursor is closed. For now stored procedures always use materialized
+    cursors and the call is not used.
+  */
+  virtual void cleanup_stmt() { /* no op */ }
 private:
 
   sp_lex_keeper m_lex_keeper;
@@ -1040,5 +1046,8 @@
 sp_add_to_query_tables(THD *thd, LEX *lex,
 		       const char *db, const char *name,
 		       thr_lock_type locktype);
+
+Item *sp_eval_func_item(THD *thd, Item **it, enum_field_types type,
+                        Item *reuse, bool use_callers_arena);
 
 #endif /* _SP_HEAD_H_ */

--- 1.32/sql/sp_rcontext.cc	2005-08-18 13:23:47 +04:00
+++ 1.33/sql/sp_rcontext.cc	2005-09-22 02:10:58 +04:00
@@ -25,6 +25,7 @@
 
 #include "mysql.h"
 #include "sp_head.h"
+#include "sql_cursor.h"
 #include "sp_rcontext.h"
 #include "sp_pcontext.h"
 
@@ -45,31 +46,18 @@
 sp_rcontext::set_item_eval(THD *thd, uint idx, Item **item_addr,
 			   enum_field_types type)
 {
-  extern Item *sp_eval_func_item(THD *thd, Item **it, enum_field_types type,
-				 Item *reuse, bool use_callers_arena);
   Item *it;
   Item *reuse_it;
-  Item *old_item_next;
   /* sp_eval_func_item will use callers_arena */
-  Item *old_free_list= thd->spcont->callers_arena->free_list;
   int res;
-  LINT_INIT(old_item_next);
 
-  if ((reuse_it= get_item(idx)))
-    old_item_next= reuse_it->next;
+  reuse_it= get_item(idx);
   it= sp_eval_func_item(thd, item_addr, type, reuse_it, TRUE);
   if (! it)
     res= -1;
   else
   {
     res= 0;
-    if (reuse_it && it == reuse_it)
-    {
-      // A reused item slot, where the constructor put it in the free_list,
-      // so we have to restore the list.
-      thd->spcont->callers_arena->free_list= old_free_list;
-      it->next= old_item_next;
-    }
     set_item(idx, it);
   }
 
@@ -170,7 +158,8 @@
  */
 
 sp_cursor::sp_cursor(sp_lex_keeper *lex_keeper, sp_instr_cpush *i)
-  :m_lex_keeper(lex_keeper), m_prot(NULL), m_isopen(0), m_current_row(NULL),
+  :m_lex_keeper(lex_keeper),
+   server_side_cursor(NULL),
    m_i(i)
 {
   /*
@@ -182,59 +171,37 @@
 
 
 /*
-  pre_open cursor
+  Open an SP cursor
 
   SYNOPSIS
-    pre_open()
-    THD		Thread handler
+    open()
+    THD		         Thread handler
 
-  NOTES
-    We have to open cursor in two steps to make it easy for sp_instr_copen
-    to reuse the sp_instr::exec_stmt() code.
-    If this function returns 0, post_open should not be called
 
   RETURN
-   0  ERROR
+   0 in case of success, -1 otherwise
 */
 
-sp_lex_keeper*
-sp_cursor::pre_open(THD *thd)
+int
+sp_cursor::open(THD *thd)
 {
-  if (m_isopen)
+  if (server_side_cursor)
   {
     my_message(ER_SP_CURSOR_ALREADY_OPEN, ER(ER_SP_CURSOR_ALREADY_OPEN),
                MYF(0));
-    return NULL;
+    return -1;
   }
-  init_alloc_root(&m_mem_root, MEM_ROOT_BLOCK_SIZE, MEM_ROOT_PREALLOC);
-  if ((m_prot= new Protocol_cursor(thd, &m_mem_root)) == NULL)
-    return NULL;
-
-  /* Save for execution. Will be restored in post_open */
-  m_oprot= thd->protocol;
-  m_nseof= thd->net.no_send_eof;
-
-  /* Change protocol for execution */
-  thd->protocol= m_prot;
-  thd->net.no_send_eof= TRUE;
-  return m_lex_keeper;
-}
-
-
-void
-sp_cursor::post_open(THD *thd, my_bool was_opened)
-{
-  thd->net.no_send_eof= m_nseof; // Restore the originals
-  thd->protocol= m_oprot;
-  if ((m_isopen= was_opened))
-    m_current_row= m_prot->data;
+  if (mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR, &result,
+                        &server_side_cursor))
+    return -1;
+  return 0;
 }
 
 
 int
 sp_cursor::close(THD *thd)
 {
-  if (! m_isopen)
+  if (! server_side_cursor)
   {
     my_message(ER_SP_CURSOR_NOT_OPEN, ER(ER_SP_CURSOR_NOT_OPEN), MYF(0));
     return -1;
@@ -247,106 +214,82 @@
 void
 sp_cursor::destroy()
 {
-  if (m_prot)
-  {
-    delete m_prot;
-    m_prot= NULL;
-    free_root(&m_mem_root, MYF(0));
-  }
-  m_isopen= FALSE;
+  delete server_side_cursor;
+  server_side_cursor= 0;
 }
 
+
 int
 sp_cursor::fetch(THD *thd, List<struct sp_pvar> *vars)
 {
-  List_iterator_fast<struct sp_pvar> li(*vars);
-  sp_pvar_t *pv;
-  MYSQL_ROW row;
-  uint fldcount;
-
-  if (! m_isopen)
+  if (! server_side_cursor)
   {
     my_message(ER_SP_CURSOR_NOT_OPEN, ER(ER_SP_CURSOR_NOT_OPEN), MYF(0));
     return -1;
   }
-  if (m_current_row == NULL)
+  if (vars->elements != result.get_field_count())
   {
-    my_message(ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA), MYF(0));
+    my_message(ER_SP_WRONG_NO_OF_FETCH_ARGS,
+               ER(ER_SP_WRONG_NO_OF_FETCH_ARGS), MYF(0));
     return -1;
   }
 
-  row= m_current_row->data;
-  for (fldcount= 0 ; (pv= li++) ; fldcount++)
-  {
-    Item *it;
-    Item *reuse;
-    uint rsize;
-    Item *old_item_next;
-    Item *old_free_list= thd->free_list;
-    const char *s;
-    LINT_INIT(old_item_next);
-
-    if (fldcount >= m_prot->get_field_count())
-    {
-      my_message(ER_SP_WRONG_NO_OF_FETCH_ARGS,
-                 ER(ER_SP_WRONG_NO_OF_FETCH_ARGS), MYF(0));
-      return -1;
-    }
+  result.set_spvar_list(vars);
 
-    if ((reuse= thd->spcont->get_item(pv->offset)))
-      old_item_next= reuse->next;
+  /* Attempt to fetch one row */
+  if (server_side_cursor->is_open())
+    server_side_cursor->fetch(1);
 
-    s= row[fldcount];
-    if (!s)
-      it= new(reuse, &rsize) Item_null();
-    else
-    {
-      /*
-        Length of data can be calculated as:
-        pointer_to_next_not_null_object - s -1
-        where the last -1 is to remove the end \0
-      */
-      uint len;
-      MYSQL_ROW next= row+fldcount+1;
-      while (!*next)                             // Skip nulls
-        next++;
-      len= (*next -s)-1;
-      switch (sp_map_result_type(pv->type)) {
-      case INT_RESULT:
-	it= new(reuse, &rsize) Item_int(s);
-	break;
-      case REAL_RESULT:
-        it= new(reuse, &rsize) Item_float(s, len);
-	break;
-      case DECIMAL_RESULT:
-        it= new(reuse, &rsize) Item_decimal(s, len, thd->db_charset);
-        break;
-      case STRING_RESULT:
-        /* TODO: Document why we do an extra copy of the string 's' here */
-        it= new(reuse, &rsize) Item_string(thd->strmake(s, len), len,
-					   thd->db_charset);
-        break;
-      case ROW_RESULT:
-      default:
-        DBUG_ASSERT(0);
-      }
-    }
-    it->rsize= rsize;
-    if (reuse && it == reuse)
-    {
-      // A reused item slot, where the constructor put it in the free_list,
-      // so we have to restore the list.
-      thd->free_list= old_free_list;
-      it->next= old_item_next;
-    }
-    thd->spcont->set_item(pv->offset, it);
-  }
-  if (fldcount < m_prot->get_field_count())
+  /*
+    If the cursor was pointing after the last row, the fetch will
+    close it instead of sending any rows.
+  */
+  if (! server_side_cursor->is_open())
   {
-    my_message(ER_SP_WRONG_NO_OF_FETCH_ARGS,
-               ER(ER_SP_WRONG_NO_OF_FETCH_ARGS), MYF(0));
+    my_message(ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA), MYF(0));
     return -1;
   }
-  m_current_row= m_current_row->next;
+
   return 0;
+}
+
+
+/***************************************************************************
+ Select_fetch_into_spvars
+****************************************************************************/
+
+int Select_fetch_into_spvars::prepare(List<Item> &fields, SELECT_LEX_UNIT *u)
+{
+  /*
+    Cache the number of columns in the result set in order to easily
+    return an error if column count does not match value count.
+  */
+  field_count= fields.elements;
+  return select_result_interceptor::prepare(fields, u);
+}
+
+
+bool Select_fetch_into_spvars::send_data(List<Item> &items)
+{
+  List_iterator_fast<struct sp_pvar> pv_iter(*spvar_list);
+  List_iterator_fast<Item> item_iter(items);
+  sp_pvar_t *pv;
+  Item *item;
+
+  /* Must be ensured by the caller */
+  DBUG_ASSERT(spvar_list->elements == items.elements);
+
+  /*
+    Assign the row fetched from a server side cursor to stored
+    procedure variables.
+  */
+  for (; pv= pv_iter++, item= item_iter++; )
+  {
+    Item *reuse= thd->spcont->get_item(pv->offset);
+    /* Evaluate a new item on the arena of the calling instruction */
+    Item *it= sp_eval_func_item(thd, &item, pv->type, reuse, TRUE);
+
+    thd->spcont->set_item(pv->offset, it);
+  }
+  return FALSE;
 }

--- 1.23/sql/sp_rcontext.h	2005-08-25 17:34:13 +04:00
+++ 1.24/sql/sp_rcontext.h	2005-09-22 02:10:58 +04:00
@@ -216,6 +216,27 @@
 }; // class sp_rcontext : public Sql_alloc
 
 
+/*
+  An interceptor of cursor result set used to implement
+  FETCH <cname> INTO <varlist>.
+*/
+
+class Select_fetch_into_spvars: public select_result_interceptor
+{
+  List<struct sp_pvar> *spvar_list;
+  uint field_count;
+public:
+  uint get_field_count() { return field_count; }
+  void set_spvar_list(List<struct sp_pvar> *vars) { spvar_list= vars; }
+
+  virtual bool send_eof() { return FALSE; }
+  virtual bool send_data(List<Item> &items);
+  virtual int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
+};
+
+
+/* A mediator between stored procedures and server side cursors */
+
 class sp_cursor : public Sql_alloc
 {
 public:
@@ -227,12 +248,11 @@
     destroy();
   }
 
-  // We have split this in two to make it easy for sp_instr_copen
-  // to reuse the sp_instr::exec_stmt() code.
   sp_lex_keeper *
-  pre_open(THD *thd);
-  void
-  post_open(THD *thd, my_bool was_opened);
+  get_lex_keeper() { return m_lex_keeper; }
+
+  int
+  open(THD *thd);
 
   int
   close(THD *thd);
@@ -240,7 +260,7 @@
   inline my_bool
   is_open()
   {
-    return m_isopen;
+    return test(server_side_cursor);
   }
 
   int
@@ -251,18 +271,13 @@
   {
     return m_i;
   }
-  
+
 private:
 
-  MEM_ROOT m_mem_root;		// My own mem_root
+  Select_fetch_into_spvars result;
   sp_lex_keeper *m_lex_keeper;
-  Protocol_cursor *m_prot;
-  my_bool m_isopen;
-  my_bool m_nseof;		// Original no_send_eof
-  Protocol *m_oprot;		// Original protcol
-  MYSQL_ROWS *m_current_row;
+  Server_side_cursor *server_side_cursor;
   sp_instr_cpush *m_i;		// My push instruction
-  
   void
   destroy();
 

--- 1.154/sql/sql_prepare.cc	2005-09-15 03:57:52 +04:00
+++ 1.155/sql/sql_prepare.cc	2005-09-22 02:10:58 +04:00
@@ -71,6 +71,7 @@
 
 #include "mysql_priv.h"
 #include "sql_select.h" // for JOIN
+#include "sql_cursor.h"
 #include "sp_head.h"
 #include "sp.h"
 #include "sp_cache.h"
@@ -81,6 +82,18 @@
 #include <mysql_com.h>
 #endif
 
+/* A result class used to send cursor rows using the binary protocol. */
+
+class Select_fetch_protocol_prep: public select_send
+{
+  Protocol_prep protocol;
+public:
+  Select_fetch_protocol_prep(THD *thd);
+  virtual bool send_fields(List<Item> &list, uint flags);
+  virtual bool send_data(List<Item> &items);
+  virtual bool send_eof();
+};
+
 /******************************************************************************
   Prepared_statement: a statement that can contain placeholders
 ******************************************************************************/
@@ -89,6 +102,7 @@
 {
 public:
   THD *thd;
+  Select_fetch_protocol_prep result;
   Protocol *protocol;
   Item_param **param_array;
   uint param_count;
@@ -109,8 +123,9 @@
   virtual ~Prepared_statement();
   void setup_set_params();
   virtual Query_arena::Type type() const;
-  virtual void close_cursor();
+  virtual void cleanup_stmt();
   bool set_name(LEX_STRING *name);
+  inline void close_cursor() { delete cursor; cursor= 0; }
 
   bool prepare(const char *packet, uint packet_length);
   bool execute(String *expanded_query, bool open_cursor);
@@ -140,8 +155,6 @@
   return pos[param_no/8] & (1 << (param_no & 7));
 }
 
-enum { STMT_QUERY_LOG_LENGTH= 8192 };
-
 /*
   Find a prepared statement in the statement map by id.
 
@@ -1264,7 +1277,7 @@
     It is not SELECT COMMAND for sure, so setup_tables will be called as
     usual, and we pass 0 as setup_tables_done_option
   */
-  if (unit->prepare(thd, 0, 0, ""))
+  if (unit->prepare(thd, 0, 0))
     goto error;
   if (!lex->describe && !text_protocol)
   {
@@ -1395,7 +1408,7 @@
   thd->used_tables= 0;                        // Updated by setup_fields
 
   /* Calls JOIN::prepare */
-  DBUG_RETURN(lex->unit.prepare(thd, 0, setup_tables_done_option, ""));
+  DBUG_RETURN(lex->unit.prepare(thd, 0, setup_tables_done_option));
 }
 
 /*
@@ -1785,19 +1798,6 @@
 }
 
 
-/* Cleanup PS after execute/prepare and restore THD state */
-
-static void cleanup_stmt_and_thd_after_use(Statement *stmt, THD *thd)
-{
-  DBUG_ENTER("cleanup_stmt_and_thd_after_use");
-  stmt->lex->unit.cleanup();
-  cleanup_items(stmt->free_list);
-  thd->rollback_item_tree_changes();
-  thd->cleanup_after_query();
-  DBUG_VOID_RETURN;
-}
-
-
 /*
   COM_STMT_PREPARE handler.
 
@@ -2221,16 +2221,14 @@
                     test(flags & (ulong) CURSOR_TYPE_READ_ONLY));
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(), WAIT_PRIOR);
-  if (rc)
-    goto err;
 
-  mysql_log.write(thd, COM_STMT_EXECUTE, "[%lu] %s", stmt->id, thd->query);
+  if (rc == 0)
+    mysql_log.write(thd, COM_STMT_EXECUTE, "[%lu] %s", stmt->id, thd->query);
 
   DBUG_VOID_RETURN;
 
 set_params_data_err:
   my_error(ER_WRONG_ARGUMENTS, MYF(0), "mysql_stmt_execute");
-err:
   reset_stmt_params(stmt);
   DBUG_VOID_RETURN;
 }
@@ -2285,14 +2283,16 @@
 
   if (stmt->set_params_from_vars(stmt, lex->prepared_stmt_params,
                                  &expanded_query))
-  {
-    my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE");
-    DBUG_VOID_RETURN;
-  }
+    goto set_params_data_err;
 
   (void) stmt->execute(&expanded_query, FALSE);
 
   DBUG_VOID_RETURN;
+
+set_params_data_err:
+  my_error(ER_WRONG_ARGUMENTS, MYF(0), "EXECUTE");
+  reset_stmt_params(stmt);
+  DBUG_VOID_RETURN;
 }
 
 
@@ -2313,7 +2313,7 @@
   ulong num_rows= uint4korr(packet+4);
   Prepared_statement *stmt;
   Statement stmt_backup;
-  Cursor *cursor;
+  Server_side_cursor *cursor;
   DBUG_ENTER("mysql_stmt_fetch");
 
   statistic_increment(thd->status_var.com_stmt_fetch, &LOCK_status);
@@ -2321,7 +2321,7 @@
     DBUG_VOID_RETURN;
 
   cursor= stmt->cursor;
-  if (!cursor || !cursor->is_open())
+  if (!cursor)
   {
     my_error(ER_STMT_HAS_NO_OPEN_CURSOR, MYF(0), stmt_id);
     DBUG_VOID_RETURN;
@@ -2333,25 +2333,16 @@
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(), QUERY_PRIOR);
 
-  thd->protocol= stmt->protocol;                // Switch to binary protocol
   cursor->fetch(num_rows);
-  thd->protocol= &thd->protocol_simple;         // Use normal protocol
 
   if (!(specialflag & SPECIAL_NO_PRIOR))
     my_pthread_setprio(pthread_self(), WAIT_PRIOR);
 
   if (!cursor->is_open())
   {
-    /* We're done with the fetch: reset PS for next execution */
-    cleanup_stmt_and_thd_after_use(stmt, thd);
+    stmt->close_cursor();
+    thd->cursor= 0;
     reset_stmt_params(stmt);
-    /*
-      Must be the last, as some memory is still needed for
-      the previous calls.
-    */
-    free_root(cursor->mem_root, MYF(0));
-    if (cursor->close_at_commit)
-      thd->stmt_map.erase_transient_cursor(stmt);
   }
 
   thd->restore_backup_statement(stmt, &stmt_backup);
@@ -2384,14 +2375,19 @@
   /* There is always space for 4 bytes in buffer */
   ulong stmt_id= uint4korr(packet);
   Prepared_statement *stmt;
-  Cursor *cursor;
   DBUG_ENTER("mysql_stmt_reset");
 
   statistic_increment(thd->status_var.com_stmt_reset, &LOCK_status);
   if (!(stmt= find_prepared_statement(thd, stmt_id, "mysql_stmt_reset")))
     DBUG_VOID_RETURN;
 
-  stmt->close_cursor();                    /* will reset statement params */
+  stmt->close_cursor();
+
+  /*
+    Clear parameters from data which could be set by
+    mysql_stmt_send_long_data() call.
+  */
+  reset_stmt_params(stmt);
 
   stmt->state= Query_arena::PREPARED;
 
@@ -2533,11 +2529,65 @@
 }
 
 
+/***************************************************************************
+ Select_fetch_protocol_prep
+****************************************************************************/
+
+Select_fetch_protocol_prep::Select_fetch_protocol_prep(THD *thd)
+  :protocol(thd)
+{}
+
+bool Select_fetch_protocol_prep::send_fields(List<Item> &list, uint flags)
+{
+  bool rc;
+  Protocol *save_protocol= thd->protocol;
+
+  /*
+    Protocol::send_fields caches the information about column types:
+    this information is later used to send data. Therefore, the same
+    dedicated Protocol object must be used for all operations with
+    a cursor.
+  */
+  thd->protocol= &protocol;
+  rc= select_send::send_fields(list, flags);
+  thd->protocol= save_protocol;
+
+  return rc;
+}
+
+bool Select_fetch_protocol_prep::send_eof()
+{
+  Protocol *save_protocol= thd->protocol;
+
+  thd->protocol= &protocol;
+  ::send_eof(thd);
+  thd->protocol= save_protocol;
+  return FALSE;
+}
+
+
+bool
+Select_fetch_protocol_prep::send_data(List<Item> &fields)
+{
+  Protocol *save_protocol= thd->protocol;
+  bool rc;
+
+  thd->protocol= &protocol;
+  rc= select_send::send_data(fields);
+  thd->protocol= save_protocol;
+  return rc;
+}
+
+/***************************************************************************
+ Prepared_statement
+****************************************************************************/
+
 Prepared_statement::Prepared_statement(THD *thd_arg, Protocol *protocol_arg)
   :Statement(INITIALIZED, ++thd_arg->statement_id_counter,
              thd_arg->variables.query_alloc_block_size,
              thd_arg->variables.query_prealloc_size),
   thd(thd_arg),
+  result(thd_arg),
   protocol(protocol_arg),
   param_array(0),
   param_count(0),
@@ -2585,17 +2635,7 @@
 {
   DBUG_ENTER("Prepared_statement::~Prepared_statement");
   DBUG_PRINT("enter",("stmt: %p  cursor: %p", this, cursor));
-  if (cursor)
-  {
-    if (cursor->is_open())
-    {
-      cursor->close(FALSE);
-      cleanup_items(free_list);
-      thd->rollback_item_tree_changes();
-      free_root(cursor->mem_root, MYF(0));
-    }
-    cursor->Cursor::~Cursor();
-  }
+  delete cursor;
   /*
     We have to call free on the items even if cleanup is called as some items,
     like Item_param, don't free everything until free_items()
@@ -2612,25 +2652,18 @@
 }
 
 
-void Prepared_statement::close_cursor()
+void Prepared_statement::cleanup_stmt()
 {
-  DBUG_ENTER("Prepared_statement::close_cursor");
+  DBUG_ENTER("Prepared_statement::cleanup_stmt");
   DBUG_PRINT("enter",("stmt: %p", this));
 
-  if (cursor && cursor->is_open())
-  {
-    thd->change_list= cursor->change_list;
-    cursor->close(FALSE);
-    cleanup_stmt_and_thd_after_use(this, thd);
-    free_root(cursor->mem_root, MYF(0));
-    if (cursor->close_at_commit)
-      thd->stmt_map.erase_transient_cursor(this);
-  }
-  /*
-    Clear parameters from data which could be set by
-    mysql_stmt_send_long_data() call.
-  */
-  reset_stmt_params(this);
+  /* The order is important */
+  lex->unit.cleanup();
+  cleanup_items(free_list);
+  thd->cleanup_after_query();
+  close_thread_tables(thd);
+  thd->rollback_item_tree_changes();
+
   DBUG_VOID_RETURN;
 }
 
@@ -2734,14 +2767,13 @@
   if (rc == 0)
     rc= check_prepared_statement(this, name.str != 0);
 
-  if (rc && thd->lex->sphead)
+  if (rc && lex->sphead)
   {
-    delete thd->lex->sphead;
-    thd->lex->sphead= NULL;
+    delete lex->sphead;
+    lex->sphead= NULL;
   }
   lex_end(lex);
-  close_thread_tables(thd);
-  cleanup_stmt_and_thd_after_use(this, thd);
+  cleanup_stmt();
   thd->restore_backup_statement(this, &stmt_backup);
   thd->stmt_arena= old_stmt_arena;
 
@@ -2781,7 +2813,7 @@
   Statement stmt_backup;
   Query_arena *old_stmt_arena;
   Item *old_free_list;
-  bool rc= 1;
+  bool rc= TRUE;
 
   statistic_increment(thd->status_var.com_stmt_execute, &LOCK_status);
 
@@ -2789,18 +2821,35 @@
   if (state == Query_arena::ERROR)
   {
     my_message(last_errno, last_error, MYF(0));
-    return 1;
+    return TRUE;
   }
   if (flags & IS_IN_USE)
   {
     my_error(ER_PS_NO_RECURSION, MYF(0));
-    return 1;
+    return TRUE;
   }
+
+  /*
+    For SHOW VARIABLES lex->result is NULL, as it's a non-SELECT
+    command. For such queries we don't return an error and don't
+    open a cursor -- the client library will recognize this case and
+    materialize the result set.
+    For SELECT statements lex->result is created in
+    check_prepared_statement. lex->result->simple_select() is FALSE
+    in INSERT ... SELECT and similar commands.
+  */
+
+  if (open_cursor && lex->result && !lex->result->simple_select())
+  {
+    DBUG_PRINT("info",("Cursor asked for not SELECT stmt"));
+    my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
+    return TRUE;
+  }
+
   /* In case the command has a call to SP which re-uses this statement name */
   flags|= IS_IN_USE;
 
-  if (cursor && cursor->is_open())
-    close_cursor();
+  close_cursor();
 
   /*
     If the free_list is not empty, we'll wrongly free some externally
@@ -2808,32 +2857,6 @@
   */
   DBUG_ASSERT(thd->change_list.is_empty());
   DBUG_ASSERT(thd->free_list == NULL);
-  if (open_cursor)
-  {
-    if (!lex->result || !lex->result->simple_select())
-    {
-      DBUG_PRINT("info",("Cursor asked for not SELECT stmt"));
-      /*
-        If lex->result is set in the parser, this is not a SELECT
-        statement: we can't open a cursor for it.
-      */
-      my_error(ER_SP_BAD_CURSOR_QUERY, MYF(0));
-      goto error;
-    }
-
-    DBUG_PRINT("info",("Using READ_ONLY cursor"));
-    if (!cursor && !(cursor= new (mem_root) Cursor(thd)))
-      goto error;
-    /* If lex->result is set, mysql_execute_command will use it */
-    lex->result= &cursor->result;
-    protocol= &cursor->protocol;
-    thd->lock_id= &cursor->lock_id;
-    /*
-      Currently cursors can be used only from C API, so
-      we don't have to create an own memory root for them:
-      the one in THD is clean and can be used.
-    */
-  }
   thd->set_n_backup_statement(this, &stmt_backup);
   if (expanded_query->length() &&
       alloc_query(thd, (char*) expanded_query->ptr(),
@@ -2862,38 +2885,27 @@
   reinit_stmt_before_use(thd, lex);
 
   thd->protocol= protocol;                      /* activate stmt protocol */
-  mysql_execute_command(thd);
+  rc= open_cursor ? mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR,
+                                      &result, &cursor) :
+                    mysql_execute_command(thd);
   thd->protocol= &thd->protocol_simple;         /* use normal protocol */
 
-  if (cursor && cursor->is_open())
-  {
-    /*
-      It's safer if we grab THD state after mysql_execute_command is
-      finished and not in Cursor::open(), because currently the call to
-      Cursor::open is buried deep in JOIN::exec of the top level join.
-    */
-    cursor->init_from_thd(thd);
+  /* Assert that if an error, no cursor is open */
+  DBUG_ASSERT(! (rc && cursor));
 
-    if (cursor->close_at_commit)
-      thd->stmt_map.add_transient_cursor(this);
-  }
-  else
+  if (! cursor)
   {
-    close_thread_tables(thd);
-    cleanup_stmt_and_thd_after_use(this, thd);
+    cleanup_stmt();
     reset_stmt_params(this);
   }
 
   thd->set_statement(&stmt_backup);
-  thd->lock_id= &thd->main_lock_id;
   thd->stmt_arena= old_stmt_arena;
 
   if (state == Query_arena::PREPARED)
     state= Query_arena::EXECUTED;
 
-  rc= 0;
 error:
-  thd->lock_id= &thd->main_lock_id;
   flags&= ~IS_IN_USE;
   return rc;
 }

--- 1.158/tests/mysql_client_test.c	2005-09-09 15:29:20 +04:00
+++ 1.159/tests/mysql_client_test.c	2005-09-22 02:10:59 +04:00
@@ -13828,8 +13828,11 @@
   rc= mysql_stmt_execute(stmt);
   check_execute(stmt, rc);
   rc= mysql_query(mysql, "update t1 set id=id+100");
-  DIE_UNLESS(rc);
-  if (!opt_silent)
+  /*
+    If cursors are not materialized, the update will return an error;
+    we mainly test that it won't deadlock.
+  */
+  if (rc && !opt_silent)
     printf("Got error (as expected): %s\n", mysql_error(mysql));
   /*
     2: check that MyISAM tables used in cursors survive
Thread
bk commit into 5.0 tree (konstantin:1.1972) BUG#9819Konstantin Osipov22 Sep