List:Commits« Previous MessageNext Message »
From:Christopher Powers Date:April 1 2011 11:31pm
Subject:bzr push into mysql-trunk branch (chris.powers:3274 to 3276)
View as plain text  
 3276 cpowers	2011-04-01 [merge]
      merge with mysql-trunk

    removed:
      include/my_handler.h
      mysys/my_gethostbyname.c
      mysys/my_handler.c
      mysys/my_port.c
      storage/innobase/include/thr0loc.h
      storage/innobase/include/thr0loc.ic
      storage/innobase/thr/
      storage/innobase/thr/thr0loc.c
    added:
      include/my_compare.h
      mysys/my_compare.c
    modified:
      client/mysqlbinlog.cc
      extra/perror.c
      extra/yassl/include/cert_wrapper.hpp
      extra/yassl/include/openssl/prefix_ssl.h
      extra/yassl/include/openssl/ssl.h
      extra/yassl/include/yassl_int.hpp
      extra/yassl/src/cert_wrapper.cpp
      extra/yassl/src/ssl.cpp
      extra/yassl/src/yassl_int.cpp
      extra/yassl/taocrypt/include/asn.hpp
      extra/yassl/taocrypt/src/asn.cpp
      include/heap.h
      include/my_global.h
      include/my_net.h
      include/myisam.h
      mysql-test/extra/rpl_tests/rpl_deadlock.test
      mysql-test/extra/rpl_tests/rpl_get_master_version_and_clock.test
      mysql-test/extra/rpl_tests/rpl_stop_middle_group.test
      mysql-test/include/subquery_sj.inc
      mysql-test/r/func_time.result
      mysql-test/r/innodb_icp_all.result
      mysql-test/r/join_cache_jcl5.result
      mysql-test/r/join_cache_jcl6.result
      mysql-test/r/join_cache_jcl7.result
      mysql-test/r/join_cache_jcl8.result
      mysql-test/r/myisam_icp_all.result
      mysql-test/r/order_by_icp_mrr.result
      mysql-test/r/order_by_none.result
      mysql-test/r/ssl.result
      mysql-test/r/subquery_mat_all.result
      mysql-test/r/subquery_sj_all.result
      mysql-test/r/subquery_sj_all_jcl6.result
      mysql-test/r/subquery_sj_all_jcl7.result
      mysql-test/r/subquery_sj_dupsweed.result
      mysql-test/r/subquery_sj_dupsweed_jcl6.result
      mysql-test/r/subquery_sj_dupsweed_jcl7.result
      mysql-test/r/subquery_sj_firstmatch.result
      mysql-test/r/subquery_sj_firstmatch_jcl6.result
      mysql-test/r/subquery_sj_firstmatch_jcl7.result
      mysql-test/r/subquery_sj_innodb_all.result
      mysql-test/r/subquery_sj_innodb_all_jcl6.result
      mysql-test/r/subquery_sj_innodb_all_jcl7.result
      mysql-test/r/subquery_sj_loosescan.result
      mysql-test/r/subquery_sj_loosescan_jcl6.result
      mysql-test/r/subquery_sj_loosescan_jcl7.result
      mysql-test/r/subquery_sj_mat.result
      mysql-test/r/subquery_sj_mat_jcl6.result
      mysql-test/r/subquery_sj_mat_jcl7.result
      mysql-test/r/subquery_sj_mat_nosj.result
      mysql-test/r/subquery_sj_none.result
      mysql-test/r/subquery_sj_none_jcl6.result
      mysql-test/r/subquery_sj_none_jcl7.result
      mysql-test/suite/innodb/include/innodb_stats.inc
      mysql-test/suite/innodb/r/innodb_stats.result
      mysql-test/suite/rpl/r/rpl_corruption.result
      mysql-test/suite/rpl/r/rpl_deadlock_innodb.result
      mysql-test/suite/rpl/r/rpl_init_slave_errors.result
      mysql-test/suite/rpl/r/rpl_stm_stop_middle_group.result
      mysql-test/suite/rpl/t/rpl_corruption.test
      mysql-test/suite/rpl/t/rpl_get_master_version_and_clock.test
      mysql-test/suite/rpl/t/rpl_init_slave_errors.test
      mysql-test/suite/rpl/t/rpl_show_slave_running.test
      mysql-test/t/func_time.test
      mysql-test/t/ssl.test
      mysys/CMakeLists.txt
      mysys/my_handler_errors.h
      sql/handler.h
      sql/item.cc
      sql/item_sum.cc
      sql/item_timefunc.h
      sql/mysqld.cc
      sql/opt_range.cc
      sql/opt_range.h
      sql/rpl_reporting.cc
      sql/rpl_reporting.h
      sql/rpl_slave.cc
      sql/sql_lex.cc
      sql/sql_parse.cc
      sql/sql_select.cc
      sql/sql_table.cc
      sql/sql_update.cc
      sql/table.h
      storage/innobase/CMakeLists.txt
      storage/innobase/btr/btr0cur.c
      storage/innobase/buf/buf0buddy.c
      storage/innobase/buf/buf0buf.c
      storage/innobase/buf/buf0flu.c
      storage/innobase/buf/buf0rea.c
      storage/innobase/data/data0data.c
      storage/innobase/dict/dict0boot.c
      storage/innobase/dict/dict0crea.c
      storage/innobase/dict/dict0dict.c
      storage/innobase/dict/dict0load.c
      storage/innobase/dict/dict0mem.c
      storage/innobase/dict/dict0stats.c
      storage/innobase/fil/fil0fil.c
      storage/innobase/fsp/fsp0fsp.c
      storage/innobase/handler/ha_innodb.cc
      storage/innobase/handler/i_s.cc
      storage/innobase/ibuf/ibuf0ibuf.c
      storage/innobase/include/btr0pcur.h
      storage/innobase/include/btr0pcur.ic
      storage/innobase/include/buf0buf.h
      storage/innobase/include/buf0buf.ic
      storage/innobase/include/buf0flu.ic
      storage/innobase/include/buf0rea.h
      storage/innobase/include/dict0dict.ic
      storage/innobase/include/dict0mem.h
      storage/innobase/include/ibuf0ibuf.h
      storage/innobase/include/ibuf0ibuf.ic
      storage/innobase/include/mtr0mtr.h
      storage/innobase/include/mtr0mtr.ic
      storage/innobase/include/page0page.h
      storage/innobase/include/page0types.h
      storage/innobase/include/page0zip.ic
      storage/innobase/include/rem0rec.h
      storage/innobase/include/rem0rec.ic
      storage/innobase/include/srv0srv.h
      storage/innobase/include/sync0sync.h
      storage/innobase/include/univ.i
      storage/innobase/lock/lock0lock.c
      storage/innobase/mtr/mtr0log.c
      storage/innobase/mtr/mtr0mtr.c
      storage/innobase/page/page0page.c
      storage/innobase/page/page0zip.c
      storage/innobase/pars/pars0pars.c
      storage/innobase/row/row0ext.c
      storage/innobase/row/row0merge.c
      storage/innobase/row/row0mysql.c
      storage/innobase/row/row0row.c
      storage/innobase/row/row0sel.c
      storage/innobase/row/row0umod.c
      storage/innobase/row/row0undo.c
      storage/innobase/row/row0upd.c
      storage/innobase/srv/srv0srv.c
      storage/innobase/srv/srv0start.c
      storage/innobase/trx/trx0i_s.c
      storage/innobase/trx/trx0rec.c
      storage/innobase/trx/trx0sys.c
      storage/innobase/trx/trx0trx.c
      storage/myisam/ft_stopwords.c
      storage/myisam/mi_check.c
      storage/myisam/mi_range.c
 3275 cpowers	2011-04-01
      WL#4896 "Performance Schema Net IO"
      
      - Implemented deferred aggregation for sockets
      - Updated doxygen documentation
      - Fixed regression tests

    modified:
      include/mysql/psi/mysql_socket.h
      mysql-test/r/mysqld--help-win.result
      mysql-test/suite/perfschema/r/aggregate.result
      storage/perfschema/pfs.cc
      storage/perfschema/pfs_instr.cc
      storage/perfschema/pfs_instr.h
      storage/perfschema/pfs_instr_class.cc
      storage/perfschema/pfs_instr_class.h
      storage/perfschema/pfs_stat.h
      storage/perfschema/pfs_visitor.cc
      storage/perfschema/pfs_visitor.h
      storage/perfschema/table_events_waits_summary.cc
      storage/perfschema/table_events_waits_summary.h
      storage/perfschema/table_ews_by_thread_by_event_name.cc
      storage/perfschema/table_socket_instances.cc
 3274 Christopher Powers	2011-03-28
      WL#4896 "Performance Schema Net IO"
      
      - Updates for code review II. Intermediate version.

    modified:
      include/mysql/psi/psi_abi_v1.h.pp
      include/mysql/psi/psi_abi_v2.h.pp
      mysql-test/r/mysqld--help-notwin.result
      mysql-test/suite/perfschema/r/aggregate.result
      mysql-test/suite/perfschema/r/pfs_upgrade_lc0.result
      mysql-test/suite/perfschema/r/start_server_innodb.result
      mysql-test/suite/perfschema/r/start_server_no_stage_class.result
      mysql-test/suite/perfschema/r/start_server_no_statement_class.result
      storage/perfschema/pfs.cc
=== modified file 'client/mysqlbinlog.cc'
--- a/client/mysqlbinlog.cc	2011-03-25 15:20:25 +0000
+++ b/client/mysqlbinlog.cc	2011-03-29 13:56:27 +0000
@@ -1681,8 +1681,7 @@ static Exit_status dump_remote_log_entri
       */
       ev->register_temp_buf((char *) net->read_pos + 1);
     }
-    if (glob_description_event->binlog_version >= 3 ||
-        (type != LOAD_EVENT && type != CREATE_FILE_EVENT))
+    if (raw_mode || (type != LOAD_EVENT))
     {
       /*
         If this is a Rotate event, maybe it's the end of the requested binlog;
@@ -1771,6 +1770,14 @@ static Exit_status dump_remote_log_entri
           ev= 0;
         }
       }
+      
+      if (type == LOAD_EVENT)
+      {
+        DBUG_ASSERT(raw_mode);
+        warning("Attempting to load a remote pre-4.0 binary log that contains "
+                "LOAD DATA INFILE statements. The file will not be copied from "
+                "the remote server. ");
+      }
 
       if (raw_mode)
       {

=== modified file 'extra/perror.c'
--- a/extra/perror.c	2011-03-28 08:49:43 +0000
+++ b/extra/perror.c	2011-03-29 12:43:49 +0000
@@ -32,7 +32,7 @@ static my_bool verbose, print_all_codes;
 
 #include "../include/my_base.h"
 #include "../mysys/my_handler_errors.h"
-#include "../include/my_handler.h"
+// #include "../include/my_compare.h"
 
 #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
 static my_bool ndb_code;
@@ -240,7 +240,39 @@ static my_bool print_win_error_msg(DWORD
 }
 #endif
 
+/*
+  Register handler error messages for usage with my_error()
 
+  NOTES
+    This is safe to call multiple times as my_error_register()
+    will ignore calls to register already registered error numbers.
+*/
+
+static const char **get_handler_error_messages()
+{
+  return handler_error_messages;
+}
+
+void my_handler_error_register(void)
+{
+  /*
+    If you got compilation error here about compile_time_assert array, check
+    that every HA_ERR_xxx constant has a corresponding error message in
+    handler_error_messages[] list (check mysys/ma_handler_errors.h and
+    include/my_base.h).
+  */
+  compile_time_assert(HA_ERR_FIRST + array_elements(handler_error_messages) ==
+                      HA_ERR_LAST + 1);
+  my_error_register(get_handler_error_messages, HA_ERR_FIRST,
+                    HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
+}
+
+
+void my_handler_error_unregister(void)
+{
+  my_error_unregister(HA_ERR_FIRST,
+                      HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
+}
 
 int main(int argc,char *argv[])
 {

=== modified file 'extra/yassl/include/cert_wrapper.hpp'
--- a/extra/yassl/include/cert_wrapper.hpp	2008-11-18 16:45:44 +0000
+++ b/extra/yassl/include/cert_wrapper.hpp	2011-03-29 08:01:07 +0000
@@ -78,6 +78,7 @@ class CertManager {
     CertList     peerList_;             // peer
     input_buffer peerPublicKey_;
     X509*        peerX509_;             // peer's openSSL X509
+    X509*        selfX509_;             // our own openSSL X509
 
     SignatureAlgorithm keyType_;        // self   key type
     SignatureAlgorithm peerKeyType_;    // peer's key type
@@ -104,6 +105,7 @@ public:
     const opaque*      get_peerKey()     const;
     const opaque*      get_privateKey()  const;
           X509*        get_peerX509()    const;
+          X509*        get_selfX509()    const;
     SignatureAlgorithm get_keyType()     const;
     SignatureAlgorithm get_peerKeyType() const;
 

=== modified file 'extra/yassl/include/openssl/prefix_ssl.h'
--- a/extra/yassl/include/openssl/prefix_ssl.h	2010-12-28 23:47:05 +0000
+++ b/extra/yassl/include/openssl/prefix_ssl.h	2011-03-29 08:01:07 +0000
@@ -178,6 +178,7 @@
 #define SSL_get1_session yaSSL_get1_session
 #define X509_get_notBefore yaX509_get_notBefore
 #define X509_get_notAfter yaX509_get_notAfter
+#define yaSSL_ASN1_TIME_to_string ya_SSL_ASN1_TIME_to_string
 #define MD4_Init yaMD4_Init
 #define MD4_Update yaMD4_Update
 #define MD4_Final yaMD4_Final

=== modified file 'extra/yassl/include/openssl/ssl.h'
--- a/extra/yassl/include/openssl/ssl.h	2008-12-08 17:45:48 +0000
+++ b/extra/yassl/include/openssl/ssl.h	2011-03-29 08:01:07 +0000
@@ -541,6 +541,8 @@ void MD5_Final(unsigned char*, MD5_CTX*)
 
 /* yaSSL adds */
 int SSL_set_compression(SSL*);   /* turn on yaSSL zlib compression */
+char *yaSSL_ASN1_TIME_to_string(ASN1_TIME *time, char *buf, size_t len);
+
 
 
 

=== modified file 'extra/yassl/include/yassl_int.hpp'
--- a/extra/yassl/include/yassl_int.hpp	2010-07-15 11:13:30 +0000
+++ b/extra/yassl/include/yassl_int.hpp	2011-03-29 08:01:07 +0000
@@ -187,7 +187,7 @@ private:
 class StringHolder {
     ASN1_STRING  asnString_;
 public:
-    StringHolder(const char* str, int sz);
+    StringHolder(const char* str, int sz, byte type= 0);
     ~StringHolder();
 
     ASN1_STRING* GetString();
@@ -205,7 +205,7 @@ class X509 {
     StringHolder afterDate_;    // not valid after
 public:
     X509(const char* i, size_t, const char* s, size_t,
-         const char* b, int, const char* a, int);
+         ASN1_STRING *b, ASN1_STRING *a);
     ~X509() {}
 
     X509_NAME* GetIssuer();

=== modified file 'extra/yassl/src/cert_wrapper.cpp'
--- a/extra/yassl/src/cert_wrapper.cpp	2009-02-13 16:41:47 +0000
+++ b/extra/yassl/src/cert_wrapper.cpp	2011-03-29 08:01:07 +0000
@@ -90,7 +90,7 @@ opaque* x509::use_buffer()
 
 //CertManager
 CertManager::CertManager()
-    : peerX509_(0), verifyPeer_(false), verifyNone_(false), failNoCert_(false),
+    : peerX509_(0), selfX509_(0), verifyPeer_(false), verifyNone_(false), failNoCert_(false),
       sendVerify_(false), verifyCallback_(0)
 {}
 
@@ -98,6 +98,7 @@ CertManager::CertManager()
 CertManager::~CertManager()
 {
     ysDelete(peerX509_);
+    ysDelete(selfX509_);
 
     STL::for_each(signers_.begin(), signers_.end(), del_ptr_zero()) ;
 
@@ -209,6 +210,12 @@ X509* CertManager::get_peerX509() const
 }
 
 
+X509* CertManager::get_selfX509() const
+{
+    return selfX509_;
+}
+
+
 SignatureAlgorithm CertManager::get_peerKeyType() const
 {
     return peerKeyType_;
@@ -279,11 +286,15 @@ int CertManager::Validate()
 
         size_t iSz = strlen(cert.GetIssuer()) + 1;
         size_t sSz = strlen(cert.GetCommonName()) + 1;
-        int bSz = (int)strlen(cert.GetBeforeDate()) + 1;
-        int aSz = (int)strlen(cert.GetAfterDate()) + 1;
+        ASN1_STRING beforeDate, afterDate;
+        beforeDate.data= (unsigned char *) cert.GetBeforeDate();
+        beforeDate.type= cert.GetBeforeDateType();
+        beforeDate.length= strlen((char *) beforeDate.data) + 1;
+        afterDate.data= (unsigned char *) cert.GetAfterDate();
+        afterDate.type= cert.GetAfterDateType();
+        afterDate.length= strlen((char *) afterDate.data) + 1;
         peerX509_ = NEW_YS X509(cert.GetIssuer(), iSz, cert.GetCommonName(),
-                                sSz, cert.GetBeforeDate(), bSz,
-                                cert.GetAfterDate(), aSz);
+                                sSz, &beforeDate, &afterDate);
 
         if (err == TaoCrypt::SIG_OTHER_E && verifyCallback_) {
             X509_STORE_CTX store;
@@ -318,6 +329,18 @@ int CertManager::SetPrivateKey(const x50
             keyType_ = rsa_sa_algo;
         else
             keyType_ = dsa_sa_algo;
+
+        size_t iSz = strlen(cd.GetIssuer()) + 1;
+        size_t sSz = strlen(cd.GetCommonName()) + 1;
+        ASN1_STRING beforeDate, afterDate;
+        beforeDate.data= (unsigned char *) cd.GetBeforeDate();
+        beforeDate.type= cd.GetBeforeDateType();
+        beforeDate.length= strlen((char *) beforeDate.data) + 1;
+        afterDate.data= (unsigned char *) cd.GetAfterDate();
+        afterDate.type= cd.GetAfterDateType();
+        afterDate.length= strlen((char *) afterDate.data) + 1;
+        selfX509_ = NEW_YS X509(cd.GetIssuer(), iSz, cd.GetCommonName(),
+                                sSz, &beforeDate, &afterDate);
     }
     return 0;
 }
@@ -335,8 +358,7 @@ void CertManager::setPeerX509(X509* x)
     ASN1_STRING* after  = x->GetAfter();
 
     peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
-        subject->GetName(), subject->GetLength(), (const char*) before->data,
-        before->length, (const char*) after->data, after->length);
+        subject->GetName(), subject->GetLength(), before, after);
 }
 
 

=== modified file 'extra/yassl/src/ssl.cpp'
--- a/extra/yassl/src/ssl.cpp	2010-02-22 13:23:47 +0000
+++ b/extra/yassl/src/ssl.cpp	2011-03-29 12:52:02 +0000
@@ -1174,8 +1174,7 @@ void SSL_CTX_set_default_passwd_cb_userd
 
 X509* SSL_get_certificate(SSL* ssl)
 {
-    // only used to pass to get_privatekey which isn't used
-    return 0;
+    return ssl->getCrypto().get_certManager().get_selfX509();
 }
 
 
@@ -1667,6 +1666,25 @@ unsigned long ERR_get_error()
 
     // end stunnel needs
 
+    char *yaSSL_ASN1_TIME_to_string(ASN1_TIME *time, char *buf, size_t len)
+    {
+      tm t;
+      static const char *month_names[12]=
+      {
+        "Jan","Feb","Mar","Apr","May","Jun",
+        "Jul","Aug","Sep","Oct","Nov","Dec"
+      };
+
+      TaoCrypt::ASN1_TIME_extract(time->data, time->type, &t);
+      snprintf(buf, len, "%s %2d %02d:%02d:%02d %d GMT",
+               month_names[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min, 
+               t.tm_sec, t.tm_year + 1900);
+      return buf;
+    }
+
+
+
+
 
 } // extern "C"
 } // namespace

=== modified file 'extra/yassl/src/yassl_int.cpp'
--- a/extra/yassl/src/yassl_int.cpp	2011-03-11 10:46:34 +0000
+++ b/extra/yassl/src/yassl_int.cpp	2011-03-29 08:01:07 +0000
@@ -1440,12 +1440,12 @@ void SSL_SESSION::CopyX509(X509* x)
 
     X509_NAME* issuer   = x->GetIssuer();
     X509_NAME* subject  = x->GetSubject();
-    ASN1_STRING* before = x->GetBefore();
-    ASN1_STRING* after  = x->GetAfter();
+    ASN1_TIME* before = x->GetBefore();
+    ASN1_TIME* after  = x->GetAfter();
 
     peerX509_ = NEW_YS X509(issuer->GetName(), issuer->GetLength(),
-        subject->GetName(), subject->GetLength(), (const char*) before->data,
-        before->length, (const char*) after->data, after->length);
+        subject->GetName(), subject->GetLength(),
+        before, after);
 }
 
 
@@ -2378,9 +2378,10 @@ size_t X509_NAME::GetLength() const
 
 
 X509::X509(const char* i, size_t iSz, const char* s, size_t sSz,
-           const char* b, int bSz, const char* a, int aSz)
+           ASN1_STRING *b, ASN1_STRING *a)
     : issuer_(i, iSz), subject_(s, sSz),
-      beforeDate_(b, bSz), afterDate_(a, aSz)
+      beforeDate_((char *) b->data, b->length, b->type),
+      afterDate_((char *) a->data, a->length, a->type)
 {}
 
 
@@ -2396,13 +2397,13 @@ X509_NAME* X509::GetSubject()
 }
 
 
-ASN1_STRING* X509::GetBefore()
+ASN1_TIME* X509::GetBefore()
 {
     return beforeDate_.GetString();
 }
 
 
-ASN1_STRING* X509::GetAfter()
+ASN1_TIME* X509::GetAfter()
 {
     return afterDate_.GetString();
 }
@@ -2430,12 +2431,12 @@ ASN1_STRING* X509_NAME::GetEntry(int i)
 }
 
 
-StringHolder::StringHolder(const char* str, int sz)
+StringHolder::StringHolder(const char* str, int sz, byte type)
 {
     asnString_.length = sz;
     asnString_.data = NEW_YS byte[sz + 1];
     memcpy(asnString_.data, str, sz);
-    asnString_.type = 0;  // not used for now
+    asnString_.type = type;
 }
 
 

=== modified file 'extra/yassl/taocrypt/include/asn.hpp'
--- a/extra/yassl/taocrypt/include/asn.hpp	2010-02-20 17:08:35 +0000
+++ b/extra/yassl/taocrypt/include/asn.hpp	2011-03-29 12:52:02 +0000
@@ -23,7 +23,7 @@
 #ifndef TAO_CRYPT_ASN_HPP
 #define TAO_CRYPT_ASN_HPP
 
-
+#include <time.h>
 #include "misc.hpp"
 #include "block.hpp"
 #include "error.hpp"
@@ -278,7 +278,9 @@ public:
     const char*      GetCommonName() const { return subject_; }
     const byte*      GetHash()       const { return subjectHash_; }
     const char*      GetBeforeDate() const { return beforeDate_; }
+    byte             GetBeforeDateType() const { return beforeDateType_; }
     const char*      GetAfterDate()  const { return afterDate_; }
+    byte             GetAfterDateType() const { return afterDateType_; }
 
     void DecodeToKey();
 private:
@@ -294,7 +296,9 @@ private:
     char      issuer_[ASN_NAME_MAX];    // Names
     char      subject_[ASN_NAME_MAX];   // Names
     char      beforeDate_[MAX_DATE_SZ]; // valid before date
+    byte      beforeDateType_;          // beforeDate time type
     char      afterDate_[MAX_DATE_SZ];  // valid after date
+    byte      afterDateType_;           // afterDate time type
     bool      verify_;                  // Default to yes, but could be off
 
     void   ReadHeader();
@@ -367,6 +371,9 @@ int GetCert(Source&);
 // Get Cert in PEM format from pkcs12 file
 int GetPKCS_Cert(const char* password, Source&);
 
+void ASN1_TIME_extract(const unsigned char* date, unsigned char format,
+                       tm *parsed_time);
+
 } // namespace
 
 

=== modified file 'extra/yassl/taocrypt/src/asn.cpp'
--- a/extra/yassl/taocrypt/src/asn.cpp	2010-02-26 13:16:46 +0000
+++ b/extra/yassl/taocrypt/src/asn.cpp	2011-03-29 08:01:07 +0000
@@ -36,6 +36,51 @@
 
 namespace TaoCrypt {
 
+// like atoi but only use first byte
+word32 btoi(byte b)
+{
+    return b - 0x30;
+}
+
+
+// two byte date/time, add to value
+void GetTime(int *value, const byte* date, int& i)
+{
+    *value += btoi(date[i++]) * 10;
+    *value += btoi(date[i++]);
+}
+
+
+void ASN1_TIME_extract(const unsigned char* date, unsigned char format,
+                       tm *t)
+{
+  int i = 0;
+  memset(t, 0, sizeof (tm));
+
+  assert(format == UTC_TIME || format == GENERALIZED_TIME);
+
+  if (format == UTC_TIME) {
+    if (btoi(date[0]) >= 5)
+      t->tm_year = 1900;
+    else
+      t->tm_year = 2000;
+  }
+  else  { // format == GENERALIZED_TIME
+    t->tm_year += btoi(date[i++]) * 1000;
+    t->tm_year += btoi(date[i++]) * 100;
+  }
+
+  GetTime(&t->tm_year, date, i);     t->tm_year -= 1900; // adjust
+  GetTime(&t->tm_mon,  date, i);     t->tm_mon  -= 1;    // adjust
+  GetTime(&t->tm_mday, date, i);
+  GetTime(&t->tm_hour, date, i);
+  GetTime(&t->tm_min,  date, i);
+  GetTime(&t->tm_sec,  date, i);
+
+  assert(date[i] == 'Z');     // only Zulu supported for this profile
+}
+
+
 namespace { // locals
 
 
@@ -70,51 +115,15 @@ bool operator<(tm& a, tm&b)
 }
 
 
-// like atoi but only use first byte
-word32 btoi(byte b)
-{
-    return b - 0x30;
-}
-
-
-// two byte date/time, add to value
-void GetTime(int& value, const byte* date, int& i)
-{
-    value += btoi(date[i++]) * 10;
-    value += btoi(date[i++]);
-}
-
-
 // Make sure before and after dates are valid
 bool ValidateDate(const byte* date, byte format, CertDecoder::DateType dt)
 {
     tm certTime;
-    memset(&certTime, 0, sizeof(certTime));
-    int i = 0;
-
-    if (format == UTC_TIME) {
-        if (btoi(date[0]) >= 5)
-            certTime.tm_year = 1900;
-        else
-            certTime.tm_year = 2000;
-    }
-    else  { // format == GENERALIZED_TIME
-        certTime.tm_year += btoi(date[i++]) * 1000;
-        certTime.tm_year += btoi(date[i++]) * 100;
-    }
-
-    GetTime(certTime.tm_year, date, i);     certTime.tm_year -= 1900; // adjust
-    GetTime(certTime.tm_mon,  date, i);     certTime.tm_mon  -= 1;    // adjust
-    GetTime(certTime.tm_mday, date, i);
-    GetTime(certTime.tm_hour, date, i); 
-    GetTime(certTime.tm_min,  date, i); 
-    GetTime(certTime.tm_sec,  date, i); 
-
-    assert(date[i] == 'Z');     // only Zulu supported for this profile
-
     time_t ltime = time(0);
     tm* localTime = gmtime(&ltime);
 
+    ASN1_TIME_extract(date, format, &certTime);
+
     if (dt == CertDecoder::BEFORE) {
         if (*localTime < certTime)
             return false;
@@ -805,10 +814,12 @@ void CertDecoder::GetDate(DateType dt)
     if (dt == BEFORE) {
         memcpy(beforeDate_, date, length);
         beforeDate_[length] = 0;
+        beforeDateType_= b;
     }
     else {  // after
         memcpy(afterDate_, date, length);
         afterDate_[length] = 0;
+        afterDateType_= b;
     }       
 }
 

=== modified file 'include/heap.h'
--- a/include/heap.h	2011-03-28 08:51:35 +0000
+++ b/include/heap.h	2011-03-29 12:56:34 +0000
@@ -30,7 +30,7 @@ extern "C" {
 #include <my_pthread.h>
 #include <thr_lock.h>
 
-#include "my_handler.h"
+#include "my_compare.h"
 #include "my_tree.h"
 
 	/* defines used by heap-funktions */

=== added file 'include/my_compare.h'
--- a/include/my_compare.h	1970-01-01 00:00:00 +0000
+++ b/include/my_compare.h	2011-03-29 12:56:34 +0000
@@ -0,0 +1,147 @@
+/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+
+   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; version 2 of the License.
+
+   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 */
+
+#ifndef _my_compare_h
+#define _my_compare_h
+
+#include "myisampack.h"
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+#include "m_ctype.h"                            /* CHARSET_INFO */
+
+/*
+  There is a hard limit for the maximum number of keys as there are only
+  8 bits in the index file header for the number of keys in a table.
+  This means that 0..255 keys can exist for a table. The idea of
+  HA_MAX_POSSIBLE_KEY is to ensure that one can use myisamchk & tools on
+  a MyISAM table for which one has more keys than MyISAM is normally
+  compiled for. If you don't have this, you will get a core dump when
+  running myisamchk compiled for 128 keys on a table with 255 keys.
+*/
+
+#define HA_MAX_POSSIBLE_KEY         255         /* For myisamchk */
+/*
+  The following defines can be increased if necessary.
+  But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and HA_MAX_KEY_LENGTH.
+*/
+
+#define HA_MAX_KEY_LENGTH           1000        /* Max length in bytes */
+#define HA_MAX_KEY_SEG              16          /* Max segments for key */
+
+#define HA_MAX_POSSIBLE_KEY_BUFF    (HA_MAX_KEY_LENGTH + 24+ 6+6)
+#define HA_MAX_KEY_BUFF  (HA_MAX_KEY_LENGTH+HA_MAX_KEY_SEG*6+8+8)
+
+typedef struct st_HA_KEYSEG		/* Key-portion */
+{
+  const CHARSET_INFO *charset;
+  uint32 start;				/* Start of key in record */
+  uint32 null_pos;			/* position to NULL indicator */
+  uint16 bit_pos;                       /* Position to bit part */
+  uint16 flag;
+  uint16 length;			/* Keylength */
+  uint16 language;
+  uint8  type;				/* Type of key (for sort) */
+  uint8  null_bit;			/* bitmask to test for NULL */
+  uint8  bit_start,bit_end;		/* if bit field */
+  uint8  bit_length;                    /* Length of bit part */
+} HA_KEYSEG;
+
+#define get_key_length(length,key) \
+{ if (*(uchar*) (key) != 255) \
+    length= (uint) *(uchar*) ((key)++); \
+  else \
+  { length= mi_uint2korr((key)+1); (key)+=3; } \
+}
+
+#define get_key_length_rdonly(length,key) \
+{ if (*(uchar*) (key) != 255) \
+    length= ((uint) *(uchar*) ((key))); \
+  else \
+  { length= mi_uint2korr((key)+1); } \
+}
+
+#define get_key_pack_length(length,length_pack,key) \
+{ if (*(uchar*) (key) != 255) \
+  { length= (uint) *(uchar*) ((key)++); length_pack= 1; }\
+  else \
+  { length=mi_uint2korr((key)+1); (key)+= 3; length_pack= 3; } \
+}
+
+#define store_key_length_inc(key,length) \
+{ if ((length) < 255) \
+  { *(key)++= (length); } \
+  else \
+  { *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
+}
+
+#define size_to_store_key_length(length) ((length) < 255 ? 1 : 3)
+
+#define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
+  (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
+   ((1 << (bit_len)) - 1))
+
+#define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
+{ \
+  (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
+                ((bits) << (bit_ofs)); \
+  if ((bit_ofs) + (bit_len) > 8) \
+    (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
+                  ((bits) >> (8 - (bit_ofs))); \
+}
+
+#define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
+  set_rec_bits(0, bit_ptr, bit_ofs, bit_len)
+
+extern int ha_compare_text(const CHARSET_INFO *, uchar *, uint, uchar *, uint ,
+			   my_bool, my_bool);
+extern int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
+		      register uchar *b, uint key_length, uint nextflag,
+		      uint *diff_pos);
+
+/*
+  Inside an in-memory data record, memory pointers to pieces of the
+  record (like BLOBs) are stored in their native byte order and in
+  this amount of bytes.
+*/
+#define portable_sizeof_char_ptr 8
+
+
+/**
+  Return values of index_cond_func_xxx functions.
+
+  0=ICP_NO_MATCH  - index tuple doesn't satisfy the pushed index condition (the
+                engine should discard the tuple and go to the next one)
+  1=ICP_MATCH     - index tuple satisfies the pushed index condition (the engine
+                should fetch and return the record)
+  2=ICP_OUT_OF_RANGE - index tuple is out range that we're scanning, e.g. this
+                   if we're scanning "t.key BETWEEN 10 AND 20" and got a
+                   "t.key=21" tuple (the engine should stop scanning and return
+                   HA_ERR_END_OF_FILE right away).
+*/
+
+typedef enum icp_result {
+  ICP_NO_MATCH,
+  ICP_MATCH,
+  ICP_OUT_OF_RANGE
+} ICP_RESULT;
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif /* _my_compare_h */

=== modified file 'include/my_global.h'
--- a/include/my_global.h	2011-03-28 08:51:35 +0000
+++ b/include/my_global.h	2011-03-29 12:56:34 +0000
@@ -280,7 +280,7 @@ C_MODE_END
 #define ulonglong2double(A) my_ulonglong2double(A)
 #define my_off_t2double(A)  my_ulonglong2double(A)
 C_MODE_START
-double my_ulonglong2double(unsigned long long A);
+inline double my_ulonglong2double(unsigned long long A) { return (double A); }
 C_MODE_END
 #endif /* _AIX */
 

=== removed file 'include/my_handler.h'
--- a/include/my_handler.h	2011-03-09 20:54:55 +0000
+++ b/include/my_handler.h	1970-01-01 00:00:00 +0000
@@ -1,150 +0,0 @@
-/* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public
-   License as published by the Free Software Foundation; version 2
-   of the License.
-
-   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
-   Library General Public License for more details.
-
-   You should have received a copy of the GNU Library General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
-
-#ifndef _my_handler_h
-#define _my_handler_h
-
-#include "myisampack.h"
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-#include "m_ctype.h"                            /* CHARSET_INFO */
-
-/*
-  There is a hard limit for the maximum number of keys as there are only
-  8 bits in the index file header for the number of keys in a table.
-  This means that 0..255 keys can exist for a table. The idea of
-  HA_MAX_POSSIBLE_KEY is to ensure that one can use myisamchk & tools on
-  a MyISAM table for which one has more keys than MyISAM is normally
-  compiled for. If you don't have this, you will get a core dump when
-  running myisamchk compiled for 128 keys on a table with 255 keys.
-*/
-
-#define HA_MAX_POSSIBLE_KEY         255         /* For myisamchk */
-/*
-  The following defines can be increased if necessary.
-  But beware the dependency of MI_MAX_POSSIBLE_KEY_BUFF and HA_MAX_KEY_LENGTH.
-*/
-
-#define HA_MAX_KEY_LENGTH           1000        /* Max length in bytes */
-#define HA_MAX_KEY_SEG              16          /* Max segments for key */
-
-#define HA_MAX_POSSIBLE_KEY_BUFF    (HA_MAX_KEY_LENGTH + 24+ 6+6)
-#define HA_MAX_KEY_BUFF  (HA_MAX_KEY_LENGTH+HA_MAX_KEY_SEG*6+8+8)
-
-typedef struct st_HA_KEYSEG		/* Key-portion */
-{
-  const CHARSET_INFO *charset;
-  uint32 start;				/* Start of key in record */
-  uint32 null_pos;			/* position to NULL indicator */
-  uint16 bit_pos;                       /* Position to bit part */
-  uint16 flag;
-  uint16 length;			/* Keylength */
-  uint16 language;
-  uint8  type;				/* Type of key (for sort) */
-  uint8  null_bit;			/* bitmask to test for NULL */
-  uint8  bit_start,bit_end;		/* if bit field */
-  uint8  bit_length;                    /* Length of bit part */
-} HA_KEYSEG;
-
-#define get_key_length(length,key) \
-{ if (*(uchar*) (key) != 255) \
-    length= (uint) *(uchar*) ((key)++); \
-  else \
-  { length= mi_uint2korr((key)+1); (key)+=3; } \
-}
-
-#define get_key_length_rdonly(length,key) \
-{ if (*(uchar*) (key) != 255) \
-    length= ((uint) *(uchar*) ((key))); \
-  else \
-  { length= mi_uint2korr((key)+1); } \
-}
-
-#define get_key_pack_length(length,length_pack,key) \
-{ if (*(uchar*) (key) != 255) \
-  { length= (uint) *(uchar*) ((key)++); length_pack= 1; }\
-  else \
-  { length=mi_uint2korr((key)+1); (key)+= 3; length_pack= 3; } \
-}
-
-#define store_key_length_inc(key,length) \
-{ if ((length) < 255) \
-  { *(key)++= (length); } \
-  else \
-  { *(key)=255; mi_int2store((key)+1,(length)); (key)+=3; } \
-}
-
-#define size_to_store_key_length(length) ((length) < 255 ? 1 : 3)
-
-#define get_rec_bits(bit_ptr, bit_ofs, bit_len) \
-  (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \
-   ((1 << (bit_len)) - 1))
-
-#define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \
-{ \
-  (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \
-                ((bits) << (bit_ofs)); \
-  if ((bit_ofs) + (bit_len) > 8) \
-    (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \
-                  ((bits) >> (8 - (bit_ofs))); \
-}
-
-#define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \
-  set_rec_bits(0, bit_ptr, bit_ofs, bit_len)
-
-extern int ha_compare_text(const CHARSET_INFO *, uchar *, uint, uchar *,
-                           uint, my_bool, my_bool);
-extern int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
-		      register uchar *b, uint key_length, uint nextflag,
-		      uint *diff_pos);
-
-extern HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a);
-extern void my_handler_error_register(void);
-extern void my_handler_error_unregister(void);
-/*
-  Inside an in-memory data record, memory pointers to pieces of the
-  record (like BLOBs) are stored in their native byte order and in
-  this amount of bytes.
-*/
-#define portable_sizeof_char_ptr 8
-
-/**
-  Return values of index_cond_func_xxx functions.
-
-  0=ICP_NO_MATCH  - index tuple doesn't satisfy the pushed index condition (the
-                engine should discard the tuple and go to the next one)
-  1=ICP_MATCH     - index tuple satisfies the pushed index condition (the engine
-                should fetch and return the record)
-  2=ICP_OUT_OF_RANGE - index tuple is out range that we're scanning, e.g. this
-                   if we're scanning "t.key BETWEEN 10 AND 20" and got a
-                   "t.key=21" tuple (the engine should stop scanning and return
-                   HA_ERR_END_OF_FILE right away).
-*/
-
-typedef enum icp_result {
-  ICP_NO_MATCH,
-  ICP_MATCH,
-  ICP_OUT_OF_RANGE
-} ICP_RESULT;
-
-#ifdef	__cplusplus
-}
-#endif
-
-
-#endif /* _my_handler_h */

=== modified file 'include/my_net.h'
--- a/include/my_net.h	2010-07-23 20:14:35 +0000
+++ b/include/my_net.h	2011-03-29 12:43:49 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000 MySQL AB
+/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
    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
@@ -73,38 +73,6 @@ C_MODE_START
 #define in_addr_t uint32
 #endif
 
-/*
-  Handling of gethostbyname_r()
-*/
-
-#if !defined(HAVE_GETHOSTBYNAME_R)
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop);
-void my_gethostbyname_r_free();
-#elif defined(HAVE_PTHREAD_ATTR_CREATE) || defined(_AIX) || defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE)
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop);
-#define my_gethostbyname_r_free()
-#if !defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE) && !defined(HPUX10)
-#define GETHOSTBYNAME_BUFF_SIZE sizeof(struct hostent_data)
-#endif /* !defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE) */
-
-#elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT)
-#define GETHOSTBYNAME_BUFF_SIZE sizeof(struct hostent_data)
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop);
-#define my_gethostbyname_r_free()
-#else
-#define my_gethostbyname_r(A,B,C,D,E) gethostbyname_r((A),(B),(C),(D),(E))
-#define my_gethostbyname_r_free()
-#endif /* !defined(HAVE_GETHOSTBYNAME_R) */
-
-#ifndef GETHOSTBYNAME_BUFF_SIZE
-#define GETHOSTBYNAME_BUFF_SIZE 2048
-#endif
 
 C_MODE_END
 #endif

=== modified file 'include/myisam.h'
--- a/include/myisam.h	2011-03-28 08:51:35 +0000
+++ b/include/myisam.h	2011-03-29 12:56:34 +0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 2000 MySQL AB, 2008-2009 Sun Microsystems, Inc
+/* Copyright (C) 2000, 2011, Oracle and/or its affiliates. All rights 
+   reserved
 
    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
@@ -30,7 +31,7 @@ extern "C" {
 #ifndef _keycache_h
 #include "keycache.h"
 #endif
-#include "my_handler.h"
+#include "my_compare.h"
 #include <mysql/plugin.h>
 
 /*

=== modified file 'include/mysql/psi/mysql_socket.h'
--- a/include/mysql/psi/mysql_socket.h	2011-03-28 23:37:10 +0000
+++ b/include/mysql/psi/mysql_socket.h	2011-04-01 22:04:26 +0000
@@ -75,7 +75,6 @@ typedef struct st_mysql_socket MYSQL_SOC
 #define MYSQL_INVALID_SOCKET mysql_socket_invalid()
 
 /**
-  @def mysql_socket_invalid
   MYSQL_SOCKET helper. Initialize instrumented socket.
   @sa mysql_socket_getfd
   @sa mysql_socket_setfd
@@ -103,7 +102,6 @@ mysql_socket_set_state(MYSQL_SOCKET sock
 }
 
 /**
-  @def mysql_socket_set_address
   Set socket descriptor and address.
   @param socket nstrumented socket
   @param fd socket descriptor
@@ -122,7 +120,6 @@ mysql_socket_set_address(MYSQL_SOCKET so
 }
 
 /**
-  @def mysql_socket_set_address
   Set socket descriptor and address.
   @param socket instrumented socket
   @param thread instrumented owning thread
@@ -137,7 +134,6 @@ mysql_socket_set_thread_owner(MYSQL_SOCK
 }
 
 /**
-  @def mysql_socket_getfd
   MYSQL_SOCKET helper. Get socket descriptor.
   @param mysql_socket Instrumented socket
   @sa mysql_socket_setfd
@@ -149,7 +145,6 @@ mysql_socket_getfd(MYSQL_SOCKET mysql_so
 }
 
 /**
-  @def mysql_socket_setfd
   MYSQL_SOCKET helper. Set socket descriptor.
   @param mysql_socket Instrumented socket
   @param fd Socket descriptor
@@ -260,7 +255,7 @@ inline_mysql_end_socket_wait(struct PSI_
   @c mysql_socket_socket is a replacement for @c socket.
   @param K PSI_socket_key for this instrumented socket
   @param D Socket domain
-  @param T Socket type
+  @param T Protocol type
   @param P Transport protocol
 */
 
@@ -274,10 +269,10 @@ inline_mysql_end_socket_wait(struct PSI_
 
 /**
   @def mysql_socket_bind(FD, AP, L)
-  Assign socket to address.
+  Bind a socket to a local port number and IP address
   @c mysql_socket_bind is a replacement for @c bind.
-  @param FD Instrumented socket descriptor
-  @param AP Pointer to sockaddr structure
+  @param FD Instrumented socket descriptor returned by socket()
+  @param AP Pointer to local port number and IP address in sockaddr structure
   @param L  Length of sockaddr structure
 */
 #ifdef HAVE_PSI_INTERFACE
@@ -290,10 +285,10 @@ inline_mysql_end_socket_wait(struct PSI_
 
 /**
   @def mysql_socket_getsockname(FD, AP, LP)
-  Get locally-bound name of a socket.
+  Return port number and IP address of the local host
   @c mysql_socket_getsockname is a replacement for @c getsockname.
-  @param FD Instrumented socket descriptor
-  @param A  Pointer to sockaddr structure
+  @param FD Instrumented socket descriptor returned by socket()
+  @param A  Pointer to returned address of local host in sockaddr structure
   @param L  Pointer to length of sockaddr structure
 */
 #ifdef HAVE_PSI_INTERFACE
@@ -306,10 +301,10 @@ inline_mysql_end_socket_wait(struct PSI_
 
 /**
   @def mysql_socket_connect(FD, AP, L)
-  Connect a socket to a remote host.
+  Establish a connection to a remote host.
   @c mysql_socket_connect is a replacement for @c connect.
-  @param FD Instrumented socket descriptor
-  @param AP Pointer to sockaddr structure specifying remote address
+  @param FD Instrumented socket descriptor returned by socket()
+  @param AP Pointer to target address in sockaddr structure
   @param L  Length of sockaddr structure
 */
 #ifdef HAVE_PSI_INTERFACE
@@ -322,20 +317,29 @@ inline_mysql_end_socket_wait(struct PSI_
 
 /**
   @def mysql_socket_getpeername(FD, AP, LP)
-  Get the peer address of a socket.
+  Get port number and IP address of remote host that a socket is connected to.
   @c mysql_socket_getpeername is a replacement for @c getpeername.
-  @param FD Instrumented socket descriptor
-  @param AP Pointer to sockaddr structure
+  @param FD Instrumented socket descriptor returned by socket() or accept()
+  @param AP Pointer to returned address of remote host in sockaddr structure
   @param LP Pointer to length of sockaddr structure
 */
 #ifdef HAVE_PSI_INTERFACE
-  #define mysql_socket_getpeername(FD, A, LP) \
-    inline_mysql_socket_getpeername(__FILE__, __LINE__, FD, A, LP)
+  #define mysql_socket_getpeername(FD, AP, LP) \
+    inline_mysql_socket_getpeername(__FILE__, __LINE__, FD, AP, LP)
 #else
-  #define mysql_socket_getpeername(FD, A, LP) \
-    inline_mysql_socket_getpeername(FD, A, LP)
+  #define mysql_socket_getpeername(FD, AP, LP) \
+    inline_mysql_socket_getpeername(FD, AP, LP)
 #endif
 
+/**
+  @def mysql_socket_send(FD, B, N, FL)
+  Send data from the buffer, B, to a connected socket.
+  @c mysql_socket_send is a replacement for @c send.
+  @param FD Instrumented socket descriptor returned by socket() or accept()
+  @param B  Buffer to send
+  @param N  Number of bytes to send
+  @param FL Control flags
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_send(FD, B, N, FL) \
     inline_mysql_socket_send(__FILE__, __LINE__, FD, B, N, FL)
@@ -344,6 +348,15 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_send(FD, B, N, FL)
 #endif
 
+/**
+  @def mysql_socket_recv(FD, B, N, FL)
+  Receive data from a connected socket.
+  @c mysql_socket_recv is a replacement for @c recv.
+  @param FD Instrumented socket descriptor returned by socket() or accept()
+  @param B  Buffer to receive to
+  @param N  Maximum bytes to receive
+  @param FL Control flags
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_recv(FD, B, N, FL) \
     inline_mysql_socket_recv(__FILE__, __LINE__, FD, B, N, FL)
@@ -352,22 +365,54 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_recv(FD, B, N, FL)
 #endif
 
+/**
+  @def mysql_socket_sendto(FD, B, N, FL, AP, L)
+  Send data to a socket at the specified address.
+  @c mysql_socket_sendto is a replacement for @c sendto.
+  @param FD Instrumented socket descriptor returned by socket()
+  @param B  Buffer to send
+  @param N  Number of bytes to send
+  @param FL Control flags
+  @param AP Pointer to destination sockaddr structure
+  @param L  Size of sockaddr structure
+*/
 #ifdef HAVE_PSI_INTERFACE
-  #define mysql_socket_sendto(FD, B, N, FL, A, L) \
-    inline_mysql_socket_sendto(__FILE__, __LINE__, FD, B, N, FL, A, L)
+  #define mysql_socket_sendto(FD, B, N, FL, AP, L) \
+    inline_mysql_socket_sendto(__FILE__, __LINE__, FD, B, N, FL, AP, L)
 #else
-  #define mysql_socket_sendto(FD, B, N, FL, A, L) \
-    inline_mysql_socket_sendto(FD, B, N, FL, A, L)
+  #define mysql_socket_sendto(FD, B, N, FL, AP, L) \
+    inline_mysql_socket_sendto(FD, B, N, FL, AP, L)
 #endif
 
+/**
+  @def mysql_socket_recvfrom(FD, B, N, FL, AP, L)
+  Receive data from a socket and return source address information
+  @c mysql_socket_recvfrom is a replacement for @c recvfrom.
+  @param FD Instrumented socket descriptor returned by socket()
+  @param B  Buffer to receive to
+  @param N  Maximum bytes to receive
+  @param FL Control flags
+  @param AP Pointer to source address in sockaddr_storage structure
+  @param L  Size of sockaddr_storage structure
+*/
 #ifdef HAVE_PSI_INTERFACE
-  #define mysql_socket_recvfrom(FD, B, N, FL, A, LP) \
-    inline_mysql_socket_recvfrom(__FILE__, __LINE__, FD, B, N, FL, A, LP)
+  #define mysql_socket_recvfrom(FD, B, N, FL, AP, LP) \
+    inline_mysql_socket_recvfrom(__FILE__, __LINE__, FD, B, N, FL, AP, LP)
 #else
-  #define mysql_socket_recvfrom(FD, B, N, FL, A, LP) \
-    inline_mysql_socket_recvfrom(FD, B, N, FL, A, LP)
+  #define mysql_socket_recvfrom(FD, B, N, FL, AP, LP) \
+    inline_mysql_socket_recvfrom(FD, B, N, FL, AP, LP)
 #endif
 
+/**
+  @def mysql_socket_getsockopt(FD, LV, ON, OP, OL)
+  Get a socket option for the specified socket.
+  @c mysql_socket_getsockopt is a replacement for @c getsockopt.
+  @param FD Instrumented socket descriptor returned by socket()
+  @param LV Protocol level
+  @param ON Option to query
+  @param OP Buffer which will contain the value for the requested option
+  @param OL Pointer to length of OP
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_getsockopt(FD, LV, ON, OP, OL) \
     inline_mysql_socket_getsockopt(__FILE__, __LINE__, FD, LV, ON, OP, OL)
@@ -376,6 +421,16 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_getsockopt(FD, LV, ON, OP, OL)
 #endif
 
+/**
+  @def mysql_socket_setsockopt(FD, LV, ON, OP, OL)
+  Set a socket option for the specified socket.
+  @c mysql_socket_setsockopt is a replacement for @c setsockopt.
+  @param FD Instrumented socket descriptor returned by socket()
+  @param LV Protocol level
+  @param ON Option to modify
+  @param OP Buffer containing the value for the specified option
+  @param OL Pointer to length of OP
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_setsockopt(FD, LV, ON, OP, OL) \
     inline_mysql_socket_setsockopt(__FILE__, __LINE__, FD, LV, ON, OP, OL)
@@ -384,6 +439,13 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_setsockopt(FD, LV, ON, OP, OL)
 #endif
 
+/**
+  @def mysql_socket_listen(FD, N)
+  Set socket state to listen for an incoming connection.
+  @c mysql_socket_listen is a replacement for @c listen.
+  @param FD Instrumented socket descriptor, bound and connected
+  @param N  Maximum number of pending connections allowed.
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_listen(FD, N) \
     inline_mysql_socket_listen(__FILE__, __LINE__, FD, N)
@@ -392,14 +454,29 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_listen(FD, N)
 #endif
 
+/**
+  @def mysql_socket_accept(K, FD, AP, LP)
+  Accept a connection from any remote host; TCP only.
+  @c mysql_socket_accept is a replacement for @c accept.
+  @param K PSI_socket_key for this instrumented socket
+  @param FD Instrumented socket descriptor, bound and placed in a listen state
+  @param AP Pointer to sockaddr structure with returned IP address and port of connected host
+  @param LP Pointer to length of valid information in AP
+*/
 #ifdef HAVE_PSI_INTERFACE
-  #define mysql_socket_accept(K, FD, A, LP) \
-    inline_mysql_socket_accept(K, /*__FILE__, __LINE__,*/ FD, A, LP)
+  #define mysql_socket_accept(K, FD, AP, LP) \
+    inline_mysql_socket_accept(K, /*__FILE__, __LINE__,*/ FD, AP, LP)
 #else
-  #define mysql_socket_accept(FD, A, LP) \
-    inline_mysql_socket_accept(FD, A, LP)
+  #define mysql_socket_accept(FD, AP, LP) \
+    inline_mysql_socket_accept(FD, AP, LP)
 #endif
 
+/**
+  @def mysql_socket_close(FD)
+  Close a socket and sever any connections.
+  @c mysql_socket_close is a replacement for @c close.
+  @param FD Instrumented socket descriptor returned by socket() or accept()
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_close(FD) \
     inline_mysql_socket_close(__FILE__, __LINE__, FD)
@@ -408,6 +485,13 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_close(FD)
 #endif
 
+/**
+  @def mysql_socket_shutdown(FD, H)
+  Disable receives and/or sends on a socket.
+  @c mysql_socket_shutdown is a replacement for @c shutdown.
+  @param FD Instrumented socket descriptor returned by socket() or accept()
+  @param H  Specifies which operations to shutdown
+*/
 #ifdef HAVE_PSI_INTERFACE
   #define mysql_socket_shutdown(FD, H) \
     inline_mysql_socket_shutdown(__FILE__, __LINE__, FD, H)
@@ -416,49 +500,6 @@ inline_mysql_end_socket_wait(struct PSI_
     inline_mysql_socket_shutdown(FD, H)
 #endif
 
-/** Not supported by Winsock */
-#ifdef __WIN__
-
-  #define mysql_socket_sendmsg(FD, M, FL)
-  #define mysql_socket_recvmsg(FD, M, FL)
-  #define mysql_socket_sockatmark(FD)
-  #define mysql_socket_socketpair(K, D, T, P, FDS)
-
-#else
-
-  #ifdef HAVE_PSI_INTERFACE
-	  #define mysql_socket_sendmsg(FD, M, FL) \
-	  inline_mysql_socket_sendmsg(__FILE__, __LINE__, FD, M, FL)
-  #else
-	  #define mysql_socket_sendmsg(FD, M, FL) \
-	  inline_mysql_socket_sendmsg(FD, M, FL)
-  #endif
-
-  #ifdef HAVE_PSI_INTERFACE
-	  #define mysql_socket_recvmsg(FD, M, FL) \
-	  inline_mysql_socket_recvmsg(__FILE__, __LINE__, FD, M, FL)
-  #else
-	  #define_mysql_socket_recvmsg(FD, M, FL) \
-	  inline_mysql_socket_recvmsg(FD, M, FL)
-  #endif
-
-  #ifdef HAVE_PSI_INTERFACE
-	  #define mysql_socket_sockatmark(FD) \
-	  inline_mysql_socket_sockatmark(__FILE__, __LINE__, FD)
-  #else
-	  #define mysql_socket_sockatmark(FD) \
-	  inline_mysql_socket_sockatmark(FD)
-  #endif
-
-  #ifdef HAVE_PSI_INTERFACE
-	  #define mysql_socket_socketpair(K, D, T, P, FDS) \
-	  inline_mysql_socket_socketpair(K, D, T, P, FDS)
-  #else
-	  #define mysql_socket_socketpair(K, D, T, P, FDS) \
-	  inline_mysql_socket_socketpair(D, T, P, FDS)
-  #endif
-
-#endif //__WIN__
 
 /** mysql_socket_socket */
 
@@ -866,7 +907,7 @@ inline_mysql_socket_accept
 #endif
   MYSQL_SOCKET socket_listen, struct sockaddr *addr, socklen_t *addr_len)
 {
-  MYSQL_SOCKET socket_accept = MYSQL_INVALID_SOCKET;
+  MYSQL_SOCKET socket_accept= MYSQL_INVALID_SOCKET;
   socklen_t addr_length= (addr_len != NULL) ? *addr_len : 0;
 
   socket_accept.fd= accept(socket_listen.fd, addr, addr_len);
@@ -948,148 +989,6 @@ inline_mysql_socket_shutdown
   return result;
 }
 
-/** Not supported by Winsock */
-
-#ifndef __WIN__
-
-/** mysql_socket_sendmsg */
-
-static inline ssize_t
-inline_mysql_socket_sendmsg
-(
-#ifdef HAVE_PSI_INTERFACE
-  const char *src_file, uint src_line,
-#endif
- MYSQL_SOCKET mysql_socket, const struct msghdr *message, int flags)
-{
-  ssize_t result;
-#ifdef HAVE_PSI_INTERFACE
-  struct PSI_socket_locker *locker= NULL;
-  PSI_socket_locker_state state;
-
-  if (likely(PSI_server != NULL && mysql_socket.m_psi != NULL))
-  {
-    locker= PSI_server->get_thread_socket_locker(&state, mysql_socket.m_psi, PSI_SOCKET_SEND);
-    if (likely(locker !=NULL))
-      PSI_server->start_socket_wait(locker, (size_t)0, src_file, src_line);
-  }
-#endif
-  
-  result= sendmsg(mysql_socket.fd, message, flags);
-
-#ifdef HAVE_PSI_INTERFACE
-  if (likely(locker != NULL))
-  {
-    size_t bytes_written = (result > -1) ? result : 0;
-    PSI_server->end_socket_wait(locker, bytes_written);
-  }
-#endif
-  return result;
-}
-
-/** mysql_socket_recvmsg */
-
-static inline ssize_t
-inline_mysql_socket_recvmsg
-(
-#ifdef HAVE_PSI_INTERFACE
-  const char *src_file, uint src_line,
-#endif
- MYSQL_SOCKET mysql_socket, struct msghdr *message, int flags)
-{
-  ssize_t result;
-#ifdef HAVE_PSI_INTERFACE
-  struct PSI_socket_locker *locker= NULL;
-  PSI_socket_locker_state state;
-
-  if (likely(PSI_server != NULL && mysql_socket.m_psi != NULL))
-  {
-    locker= PSI_server->get_thread_socket_locker(&state, mysql_socket.m_psi, PSI_SOCKET_RECV);
-    if (likely(locker !=NULL))
-      PSI_server->start_socket_wait(locker, (size_t)0, src_file, src_line);
-  }
-#endif
-  
-  result= recvmsg(mysql_socket.fd, message, flags);
-
-#ifdef HAVE_PSI_INTERFACE
-  if (likely(locker != NULL))
-  {
-    size_t bytes_written = (result > -1) ? result : 0;
-    PSI_server->end_socket_wait(locker, bytes_written);
-  }
-#endif
-  return result;
-}
-
-/** mysql_socket_sockatmark */
-
-static inline int
-inline_mysql_socket_sockatmark
-(
-#ifdef HAVE_PSI_INTERFACE
-  const char *src_file, uint src_line,
-#endif
-  MYSQL_SOCKET mysql_socket)
-{
-  int result;
-#ifdef HAVE_PSI_INTERFACE
-  struct PSI_socket_locker *locker= NULL;
-  PSI_socket_locker_state state;
-
-  if (likely(PSI_server != NULL && mysql_socket.m_psi != NULL))
-  {
-    locker= PSI_server->get_thread_socket_locker(&state, mysql_socket.m_psi, PSI_SOCKET_STAT);
-    if (likely(locker !=NULL))
-      PSI_server->start_socket_wait(locker, (size_t)0, src_file, src_line);
-  }
-#endif
-  
-  result= sockatmark(mysql_socket.fd);
-
-#ifdef HAVE_PSI_INTERFACE
-  if (likely(locker != NULL))
-    PSI_server->end_socket_wait(locker, (size_t)0);
-#endif
-  return result;
-}
-
-/** mysql_socket_socketpair */
-
-static inline int
-inline_mysql_socket_socketpair
-(
-#ifdef HAVE_PSI_INTERFACE
-  PSI_socket_key key,
-#endif
-  int domain, int type, int protocol, MYSQL_SOCKET mysql_socket[2])
-{
-  int result= 0;
-  int fds[2]= {0, 0};
-
-  mysql_socket[0].m_psi= PSI_server ? PSI_server->init_socket(key, &mysql_socket[0].fd)
-                                    : NULL;
-  mysql_socket[1].m_psi= PSI_server ? PSI_server->init_socket(key, &mysql_socket[1].fd)
-                                    : NULL;
-
-  result= socketpair(domain, type, protocol, fds);
-
-  mysql_socket[0].fd = fds[0];
-  mysql_socket[1].fd = fds[1];
-
-#ifdef HAVE_PSI_INTERFACE
-  if (likely(PSI_server != NULL && mysql_socket[0].m_psi != NULL
-             && mysql_socket[1].m_psi != NULL))
-  {
-    PSI_server->set_socket_info(mysql_socket[0].m_psi, &fds[0], NULL, 0);
-    PSI_server->set_socket_info(mysql_socket[1].m_psi, &fds[1], NULL, 0);
-  }
-#endif
-  return result;
-}
-
-#endif // __WIN__
-
 /** @} (end of group Socket_instrumentation) */
 
 #endif

=== modified file 'mysql-test/extra/rpl_tests/rpl_deadlock.test'
--- a/mysql-test/extra/rpl_tests/rpl_deadlock.test	2010-12-19 17:07:28 +0000
+++ b/mysql-test/extra/rpl_tests/rpl_deadlock.test	2011-03-27 18:16:32 +0000
@@ -123,6 +123,57 @@ SELECT * FROM t3;
 source include/check_slave_is_running.inc;
 --echo
 
+#
+# bug#11748510/36524 incident of deadlock on slave is overdramatized
+#
+# Observe that the slave stopped when the number of transation retries 
+# exceeds @@global.slave_transaction_retries
+#
+connection master;
+
+--echo *** Test the deadlock warning to be escalated into the error ***
+
+delete from t1;
+delete from t2;
+delete from t3;
+
+sync_slave_with_master;
+
+# make sure slave's unilateral row gone as well
+delete from t1;
+delete from t2;
+delete from t3;
+
+# the first attempt to run a deadlock scenario of p 1) leads to the error
+set @save.slave_transaction_retries= @@global.slave_transaction_retries;
+set @@global.slave_transaction_retries= 0;
+source include/stop_slave.inc;
+
+connection master;
+
+BEGIN;
+INSERT INTO t1 VALUES (1);
+# We make a long transaction here
+INSERT INTO t2 VALUES (2), (2), (2), (2), (2), (2), (2), (2), (2), (2);
+INSERT INTO t3 VALUES (3);
+COMMIT;
+
+connection slave;
+BEGIN;
+SELECT count(*) as zero FROM t1 FOR UPDATE;
+
+start slave;
+
+--echo *** Now the slave must be stopped due to timeout ***
+
+let $slave_sql_errno= 1205; # ER_LOCK_TIMEOUT
+let $show_slave_sql_error= 0;
+source include/wait_for_slave_sql_error.inc;
+
+rollback;
+
+set @@global.slave_transaction_retries= @save.slave_transaction_retries;
+source include/start_slave.inc;
 # Clean up
 --echo *** Clean up ***
 connection master;

=== modified file 'mysql-test/extra/rpl_tests/rpl_get_master_version_and_clock.test'
--- a/mysql-test/extra/rpl_tests/rpl_get_master_version_and_clock.test	2010-12-19 17:22:30 +0000
+++ b/mysql-test/extra/rpl_tests/rpl_get_master_version_and_clock.test	2011-03-17 18:13:10 +0000
@@ -71,6 +71,13 @@ source include/wait_for_slave_io_error.i
 # now to avoid restarting IO-thread to re-enter it.
 # There will be a new IO thread forked out with its @@session.debug
 # unset.
+
+#
+# Note, due to # Bug#11765758 - 58754
+# make sure the slave threads stand still (SQL thread in this context)
+# while @@global.debug is being updated.
+#
+
 eval set @@global.debug = "-d,$dbug_sync_point";
 
 --let $rpl_server_number= 1

=== modified file 'mysql-test/extra/rpl_tests/rpl_stop_middle_group.test'
--- a/mysql-test/extra/rpl_tests/rpl_stop_middle_group.test	2011-03-17 13:20:36 +0000
+++ b/mysql-test/extra/rpl_tests/rpl_stop_middle_group.test	2011-03-29 13:44:23 +0000
@@ -47,6 +47,7 @@ source include/wait_for_slave_sql_to_sto
 --let $assert_cond= [SELECT COUNT(*) AS Val FROM ti, Val, 1] = 1
 --source include/assert.inc
 
+--source include/stop_slave.inc
 set @@global.debug="-d";
 
 #
@@ -104,12 +105,15 @@ let $slave_sql_errno= 1593; # ER_SLAVE_F
 --let $assert_cond= [SELECT COUNT(*) AS Val FROM ti, Val, 1] = 0
 --source include/assert.inc
 
+--source include/stop_slave.inc
 set @@global.debug="-d";
 
 #
 # The mixed multi-table update
 #
-stop slave;
+
+# above tests for Bug#11765758 - 58754 made sure slave is down
+
 truncate table tm;
 source include/start_slave.inc;
 
@@ -146,6 +150,7 @@ let $slave_sql_errno= 1593; # ER_SLAVE_F
 --let $assert_cond= [SELECT MAX(a) AS Val FROM ti, Val, 1] = 1
 --source include/assert.inc
 
+--source include/stop_slave.inc
 set @@global.debug="-d";
 
 #

=== modified file 'mysql-test/include/subquery_sj.inc'
--- a/mysql-test/include/subquery_sj.inc	2011-02-21 10:36:31 +0000
+++ b/mysql-test/include/subquery_sj.inc	2011-03-17 08:00:10 +0000
@@ -3506,9 +3506,29 @@ DROP TABLE ot1, ot2, ot3, it1;
 --echo # End of the test for bug#57623.
 
 --echo #
---echo # Bug#11766642: crash in Item_field::register_field_in_read_map with view
+--echo # Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
 --echo #
 
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+
+let $query=
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+             FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+eval EXPLAIN $query;
+eval $query;
+
+DROP TABLE t1, t2;
+
+--echo # End of the test for bug#11766739.
+
+--echo #
+--echo # Bug#11766642: crash in Item_field::register_field_in_read_map with view
+--echo #
 CREATE TABLE t1(a INT);
 CREATE VIEW v1 AS SELECT a FROM t1;
 

=== modified file 'mysql-test/r/func_time.result'
--- a/mysql-test/r/func_time.result	2011-03-28 13:38:03 +0000
+++ b/mysql-test/r/func_time.result	2011-03-30 07:42:03 +0000
@@ -1365,6 +1365,18 @@ SET GLOBAL SQL_MODE=DEFAULT;
 SELECT FORMAT(YEAR(STR_TO_DATE('',GET_FORMAT(TIME,''))),1);
 FORMAT(YEAR(STR_TO_DATE('',GET_FORMAT(TIME,''))),1)
 NULL
+#
+# Bug#11766126  59166: ANOTHER DATETIME VALGRIND UNINITIALIZED WARNING
+#
+SELECT CAST((MONTH(FROM_UNIXTIME(@@GLOBAL.SQL_MODE))) AS BINARY(1025));
+CAST((MONTH(FROM_UNIXTIME(@@GLOBAL.SQL_MODE))) AS BINARY(1025))
+NULL
+#
+# Bug#11766124  59164: VALGRIND: UNINITIALIZED VALUE IN NUMBER_TO_DATETIME
+#
+SELECT ADDDATE(MONTH(FROM_UNIXTIME(NULL)),INTERVAL 1 HOUR);
+ADDDATE(MONTH(FROM_UNIXTIME(NULL)),INTERVAL 1 HOUR)
+NULL
 End of 5.1 tests
 #
 # BUG#43578 "MyISAM&Maria gives wrong rows with range access

=== modified file 'mysql-test/r/innodb_icp_all.result'
--- a/mysql-test/r/innodb_icp_all.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/innodb_icp_all.result	2011-03-21 07:13:12 +0000
@@ -759,5 +759,22 @@ pk	i1	c1	i2
 2	1	she	6
 INSERT INTO t1 (i2) VALUES (1);
 DROP TABLE t1;
+#
+# Bug #11766678 - 59843:
+# USING UNINITIALISED VALUE IN USES_INDEX_FIELDS_ONLY
+#
+CREATE TABLE t1 (
+col999 FLOAT NOT NULL,
+COL1000 VARBINARY(179) NOT NULL,
+col1003 DATE DEFAULT NULL,
+KEY idx4267 (col1000, col1003)
+);
+INSERT INTO t1 VALUES (),();
+Warnings:
+Warning	1364	Field 'col999' doesn't have a default value
+Warning	1364	Field 'COL1000' doesn't have a default value
+SELECT col999 FROM t1 WHERE col1000 = "3" AND col1003 <=> sysdate();
+col999
+DROP TABLE t1;
 set default_storage_engine= @save_storage_engine;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/join_cache_jcl5.result'
--- a/mysql-test/r/join_cache_jcl5.result	2010-11-30 13:55:22 +0000
+++ b/mysql-test/r/join_cache_jcl5.result	2011-03-17 11:23:06 +0000
@@ -1311,11 +1311,11 @@ id	select_type	table	type	possible_keys
 1	SIMPLE	t5	eq_ref	PRIMARY,t5_formattypeid	PRIMARY	4	test.t4.formatclassid	1	Using where; Using join buffer (BKA, regular buffers)
 1	SIMPLE	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using join buffer (BKA, regular buffers)
 1	SIMPLE	t7	ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using index
-1	SIMPLE	t3	ref	t3_metaid,t3_formatid,t3_metaidformatid	t3_metaid	4	test.t1.metaid	2	Using where; Using join buffer (BKA, regular buffers)
 1	SIMPLE	t8	eq_ref	PRIMARY	PRIMARY	4	test.t7.artistid	1	Using join buffer (BKA, regular buffers)
-1	SIMPLE	t9	index	PRIMARY,t9_subgenreid,t9_metaid	PRIMARY	8	NULL	2	Using where; Using index; Using join buffer (BNL, regular buffers)
+1	SIMPLE	t9	ref	PRIMARY,t9_subgenreid,t9_metaid	t9_metaid	4	test.t7.metaid	2	Using index condition; Using join buffer (BKA, regular buffers)
 1	SIMPLE	t10	eq_ref	PRIMARY,t10_genreid	PRIMARY	4	test.t9.subgenreid	1	Using join buffer (BKA, regular buffers)
 1	SIMPLE	t11	eq_ref	PRIMARY	PRIMARY	4	test.t10.genreid	1	Using join buffer (BKA, regular buffers)
+1	SIMPLE	t3	ALL	t3_metaid,t3_formatid,t3_metaidformatid	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
 SELECT t1.uniquekey, t1.xml AS affiliateXml,
 t8.name AS artistName, t8.artistid, 
 t11.name AS genreName, t11.genreid, t11.priority AS genrePriority,

=== modified file 'mysql-test/r/join_cache_jcl6.result'
--- a/mysql-test/r/join_cache_jcl6.result	2010-11-30 13:55:22 +0000
+++ b/mysql-test/r/join_cache_jcl6.result	2011-03-17 11:23:06 +0000
@@ -1311,11 +1311,11 @@ id	select_type	table	type	possible_keys
 1	SIMPLE	t5	eq_ref	PRIMARY,t5_formattypeid	PRIMARY	4	test.t4.formatclassid	1	Using where; Using join buffer (BKA, incremental buffers)
 1	SIMPLE	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using join buffer (BKA, incremental buffers)
 1	SIMPLE	t7	ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using index
-1	SIMPLE	t3	ref	t3_metaid,t3_formatid,t3_metaidformatid	t3_metaid	4	test.t1.metaid	2	Using where; Using join buffer (BKA, incremental buffers)
 1	SIMPLE	t8	eq_ref	PRIMARY	PRIMARY	4	test.t7.artistid	1	Using join buffer (BKA, incremental buffers)
-1	SIMPLE	t9	index	PRIMARY,t9_subgenreid,t9_metaid	PRIMARY	8	NULL	2	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	SIMPLE	t9	ref	PRIMARY,t9_subgenreid,t9_metaid	t9_metaid	4	test.t7.metaid	2	Using index condition; Using join buffer (BKA, incremental buffers)
 1	SIMPLE	t10	eq_ref	PRIMARY,t10_genreid	PRIMARY	4	test.t9.subgenreid	1	Using join buffer (BKA, incremental buffers)
 1	SIMPLE	t11	eq_ref	PRIMARY	PRIMARY	4	test.t10.genreid	1	Using join buffer (BKA, incremental buffers)
+1	SIMPLE	t3	ALL	t3_metaid,t3_formatid,t3_metaidformatid	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT t1.uniquekey, t1.xml AS affiliateXml,
 t8.name AS artistName, t8.artistid, 
 t11.name AS genreName, t11.genreid, t11.priority AS genrePriority,

=== modified file 'mysql-test/r/join_cache_jcl7.result'
--- a/mysql-test/r/join_cache_jcl7.result	2010-11-30 13:55:22 +0000
+++ b/mysql-test/r/join_cache_jcl7.result	2011-03-17 11:23:06 +0000
@@ -1311,11 +1311,11 @@ id	select_type	table	type	possible_keys
 1	SIMPLE	t5	eq_ref	PRIMARY,t5_formattypeid	PRIMARY	4	test.t4.formatclassid	1	Using where; Using join buffer (BKA_UNIQUE, regular buffers)
 1	SIMPLE	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using join buffer (BKA_UNIQUE, regular buffers)
 1	SIMPLE	t7	ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using index
-1	SIMPLE	t3	ref	t3_metaid,t3_formatid,t3_metaidformatid	t3_metaid	4	test.t1.metaid	2	Using where; Using join buffer (BKA_UNIQUE, regular buffers)
 1	SIMPLE	t8	eq_ref	PRIMARY	PRIMARY	4	test.t7.artistid	1	Using join buffer (BKA_UNIQUE, regular buffers)
-1	SIMPLE	t9	index	PRIMARY,t9_subgenreid,t9_metaid	PRIMARY	8	NULL	2	Using where; Using index; Using join buffer (BNL, regular buffers)
+1	SIMPLE	t9	ref	PRIMARY,t9_subgenreid,t9_metaid	t9_metaid	4	test.t7.metaid	2	Using index condition; Using join buffer (BKA_UNIQUE, regular buffers)
 1	SIMPLE	t10	eq_ref	PRIMARY,t10_genreid	PRIMARY	4	test.t9.subgenreid	1	Using join buffer (BKA_UNIQUE, regular buffers)
 1	SIMPLE	t11	eq_ref	PRIMARY	PRIMARY	4	test.t10.genreid	1	Using join buffer (BKA_UNIQUE, regular buffers)
+1	SIMPLE	t3	ALL	t3_metaid,t3_formatid,t3_metaidformatid	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
 SELECT t1.uniquekey, t1.xml AS affiliateXml,
 t8.name AS artistName, t8.artistid, 
 t11.name AS genreName, t11.genreid, t11.priority AS genrePriority,

=== modified file 'mysql-test/r/join_cache_jcl8.result'
--- a/mysql-test/r/join_cache_jcl8.result	2010-11-30 13:55:22 +0000
+++ b/mysql-test/r/join_cache_jcl8.result	2011-03-17 11:23:06 +0000
@@ -1311,11 +1311,11 @@ id	select_type	table	type	possible_keys
 1	SIMPLE	t5	eq_ref	PRIMARY,t5_formattypeid	PRIMARY	4	test.t4.formatclassid	1	Using where; Using join buffer (BKA_UNIQUE, incremental buffers)
 1	SIMPLE	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using join buffer (BKA_UNIQUE, incremental buffers)
 1	SIMPLE	t7	ref	PRIMARY	PRIMARY	4	test.t1.metaid	1	Using index
-1	SIMPLE	t3	ref	t3_metaid,t3_formatid,t3_metaidformatid	t3_metaid	4	test.t1.metaid	2	Using where; Using join buffer (BKA_UNIQUE, incremental buffers)
 1	SIMPLE	t8	eq_ref	PRIMARY	PRIMARY	4	test.t7.artistid	1	Using join buffer (BKA_UNIQUE, incremental buffers)
-1	SIMPLE	t9	index	PRIMARY,t9_subgenreid,t9_metaid	PRIMARY	8	NULL	2	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	SIMPLE	t9	ref	PRIMARY,t9_subgenreid,t9_metaid	t9_metaid	4	test.t7.metaid	2	Using index condition; Using join buffer (BKA_UNIQUE, incremental buffers)
 1	SIMPLE	t10	eq_ref	PRIMARY,t10_genreid	PRIMARY	4	test.t9.subgenreid	1	Using join buffer (BKA_UNIQUE, incremental buffers)
 1	SIMPLE	t11	eq_ref	PRIMARY	PRIMARY	4	test.t10.genreid	1	Using join buffer (BKA_UNIQUE, incremental buffers)
+1	SIMPLE	t3	ALL	t3_metaid,t3_formatid,t3_metaidformatid	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT t1.uniquekey, t1.xml AS affiliateXml,
 t8.name AS artistName, t8.artistid, 
 t11.name AS genreName, t11.genreid, t11.priority AS genrePriority,

=== modified file 'mysql-test/r/myisam_icp_all.result'
--- a/mysql-test/r/myisam_icp_all.result	2011-02-10 10:19:19 +0000
+++ b/mysql-test/r/myisam_icp_all.result	2011-03-21 07:13:12 +0000
@@ -757,4 +757,21 @@ pk	i1	c1	i2
 2	1	she	6
 INSERT INTO t1 (i2) VALUES (1);
 DROP TABLE t1;
+#
+# Bug #11766678 - 59843:
+# USING UNINITIALISED VALUE IN USES_INDEX_FIELDS_ONLY
+#
+CREATE TABLE t1 (
+col999 FLOAT NOT NULL,
+COL1000 VARBINARY(179) NOT NULL,
+col1003 DATE DEFAULT NULL,
+KEY idx4267 (col1000, col1003)
+);
+INSERT INTO t1 VALUES (),();
+Warnings:
+Warning	1364	Field 'col999' doesn't have a default value
+Warning	1364	Field 'COL1000' doesn't have a default value
+SELECT col999 FROM t1 WHERE col1000 = "3" AND col1003 <=> sysdate();
+col999
+DROP TABLE t1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/mysqld--help-win.result'
--- a/mysql-test/r/mysqld--help-win.result	2011-03-28 20:15:26 +0000
+++ b/mysql-test/r/mysqld--help-win.result	2011-04-01 23:01:38 +0000
@@ -924,8 +924,8 @@ performance-schema-max-mutex-classes 200
 performance-schema-max-mutex-instances 1000000
 performance-schema-max-rwlock-classes 30
 performance-schema-max-rwlock-instances 1000000
-performance-schema-max-socket-classes 50
-performance-schema-max-socket-instances 10000
+performance-schema-max-socket-classes 10
+performance-schema-max-socket-instances 1000
 performance-schema-max-stage-classes 100
 performance-schema-max-statement-classes 165
 performance-schema-max-table-handles 10000

=== modified file 'mysql-test/r/order_by_icp_mrr.result'
--- a/mysql-test/r/order_by_icp_mrr.result	2011-02-07 09:46:53 +0000
+++ b/mysql-test/r/order_by_icp_mrr.result	2011-04-01 14:04:52 +0000
@@ -2536,7 +2536,7 @@ CREATE TABLE t1 (a INT,KEY (a));
 INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
 EXPLAIN SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	a	a	5	NULL	10	Using where; Using index; Using filesort
+1	SIMPLE	t1	index	a	a	5	NULL	10	Using where; Using index
 SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
 a	1
 10	1

=== modified file 'mysql-test/r/order_by_none.result'
--- a/mysql-test/r/order_by_none.result	2011-02-07 09:46:53 +0000
+++ b/mysql-test/r/order_by_none.result	2011-04-01 14:04:52 +0000
@@ -2535,7 +2535,7 @@ CREATE TABLE t1 (a INT,KEY (a));
 INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
 EXPLAIN SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	index	a	a	5	NULL	10	Using where; Using index; Using filesort
+1	SIMPLE	t1	index	a	a	5	NULL	10	Using where; Using index
 SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
 a	1
 10	1

=== modified file 'mysql-test/r/ssl.result'
--- a/mysql-test/r/ssl.result	2010-11-30 13:37:51 +0000
+++ b/mysql-test/r/ssl.result	2011-03-29 08:01:07 +0000
@@ -1,6 +1,12 @@
 SHOW STATUS LIKE 'Ssl_cipher';
 Variable_name	Value
 Ssl_cipher	DHE-RSA-AES256-SHA
+SHOW STATUS LIKE 'Ssl_server_not_before';
+Variable_name	Value
+Ssl_server_not_before	Jan 29 11:56:49 2010 GMT
+SHOW STATUS LIKE 'Ssl_server_not_after';
+Variable_name	Value
+Ssl_server_not_after	Jan 28 11:56:49 2015 GMT
 drop table if exists t1,t2,t3,t4;
 CREATE TABLE t1 (
 Period smallint(4) unsigned zerofill DEFAULT '0000' NOT NULL,

=== modified file 'mysql-test/r/subquery_mat_all.result'
--- a/mysql-test/r/subquery_mat_all.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_mat_all.result	2011-03-17 11:23:06 +0000
@@ -262,12 +262,12 @@ where (a1, a2) in (select b1, b2 from t2
 (a1, a2) in (select c1, c2 from t3i
 where (c1, c2) in (select b1, b2 from t2i where b2 > '0'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1i	index	it1i1,it1i2,it1i3	it1i3	18	NULL	3	100.00	Using index
-1	PRIMARY	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	100.00	Using where; Using index; Materialize
-1	PRIMARY	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	100.00	Using where; Using index; Start materialize
-1	PRIMARY	t3i	index	it3i1,it3i2,it3i3	it3i3	18	NULL	4	75.00	Using where; Using index; End materialize
+1	PRIMARY	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	50.00	Using where; Using index; LooseScan
+1	PRIMARY	t1i	ref	it1i1,it1i2,it1i3	it1i1	9	test.t2i.b1	1	100.00	Using where
+1	PRIMARY	t3i	ref	it3i1,it3i2,it3i3	it3i1	9	test.t2i.b1	1	100.00	Using where
+1	PRIMARY	t2i	ref	it2i1,it2i2,it2i3	it2i3	18	test.t2i.b1,test.t2i.b2	2	100.00	Using index; FirstMatch(t1i)
 Warnings:
-Note	1003	select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) semi join (`test`.`t2i` join `test`.`t3i`) where ((`test`.`t2i`.`b2` = `test`.`t1i`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t1i`.`a2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b1` = `test`.`t1i`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t1i`.`a1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0') and (`test`.`t2i`.`b2` > '0'))
+Note	1003	select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) semi join (`test`.`t2i` join `test`.`t3i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0') and (`test`.`t2i`.`b2` > '0'))
 select * from t1i
 where (a1, a2) in (select b1, b2 from t2i where b1 >  '0') and
 (a1, a2) in (select c1, c2 from t3i
@@ -345,13 +345,13 @@ id	select_type	table	type	possible_keys
 2	SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; Using temporary; Using filesort
 4	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
 3	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
-7	UNION	t1i	index	it1i1,it1i2,it1i3	it1i3	18	NULL	3	100.00	Using index
-7	UNION	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	100.00	Using where; Using index; Materialize
-7	UNION	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	100.00	Using where; Using index; Start materialize
-7	UNION	t3i	index	it3i1,it3i2,it3i3	it3i3	18	NULL	4	75.00	Using where; Using index; End materialize
+7	UNION	t2i	index	it2i1,it2i2,it2i3	it2i3	18	NULL	5	50.00	Using where; Using index; LooseScan
+7	UNION	t1i	ref	it1i1,it1i2,it1i3	it1i1	9	test.t2i.b1	1	100.00	Using where
+7	UNION	t3i	ref	it3i1,it3i2,it3i3	it3i1	9	test.t2i.b1	1	100.00	Using where
+7	UNION	t2i	ref	it2i1,it2i2,it2i3	it2i3	18	test.t2i.b1,test.t2i.b2	2	100.00	Using index; FirstMatch(t1i)
 NULL	UNION RESULT	<union1,7>	ALL	NULL	NULL	NULL	NULL	NULL	NULL	
 Warnings:
-Note	1003	(select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) where ((`test`.`t2i`.`b2` = `test`.`t1`.`a2`) and (`test`.`t3`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b1` = `test`.`t1`.`a1`) and (`test`.`t3`.`c1` = `test`.`t1`.`a1`) and <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),(`test`.`t1`.`a1`,`test`.`t1`.`a2`) in ( <materialize> (select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (<in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%02') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on distinct_key where ((`test`.`t2`.`b2` = `materialized subselect`.`c2`))))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on distinct_
 key where ((`test`.`t2`.`b2` = `materialized subselect`.`c2`)))))) group by `test`.`t2`.`b1`,`test`.`t2`.`b2` ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`) and (`test`.`t1`.`a2` = `materialized subselect`.`b2`))))) and (`test`.`t1`.`a2` > '0'))) union (select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) semi join (`test`.`t2i` join `test`.`t3i`) where ((`test`.`t2i`.`b2` = `test`.`t1i`.`a2`) and (`test`.`t2i`.`b2` = `test`.`t1i`.`a2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b1` = `test`.`t1i`.`a1`) and (`test`.`t2i`.`b1` = `test`.`t1i`.`a1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0') and (`test`.`t2i`.`b2` > '0')))
+Note	1003	(select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t2i` join `test`.`t3`) where ((`test`.`t2i`.`b2` = `test`.`t1`.`a2`) and (`test`.`t3`.`c2` = `test`.`t1`.`a2`) and (`test`.`t2i`.`b1` = `test`.`t1`.`a1`) and (`test`.`t3`.`c1` = `test`.`t1`.`a1`) and <in_optimizer>((`test`.`t1`.`a1`,`test`.`t1`.`a2`),(`test`.`t1`.`a1`,`test`.`t1`.`a2`) in ( <materialize> (select `test`.`t2`.`b1`,`test`.`t2`.`b2` from `test`.`t2` where (<in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%02') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on distinct_key where ((`test`.`t2`.`b2` = `materialized subselect`.`c2`))))) or <in_optimizer>(`test`.`t2`.`b2`,`test`.`t2`.`b2` in ( <materialize> (select `test`.`t3`.`c2` from `test`.`t3` where (`test`.`t3`.`c2` like '%03') ), <primary_index_lookup>(`test`.`t2`.`b2` in <temporary table> on distinct_
 key where ((`test`.`t2`.`b2` = `materialized subselect`.`c2`)))))) group by `test`.`t2`.`b1`,`test`.`t2`.`b2` ), <primary_index_lookup>(`test`.`t1`.`a1` in <temporary table> on distinct_key where ((`test`.`t1`.`a1` = `materialized subselect`.`b1`) and (`test`.`t1`.`a2` = `materialized subselect`.`b2`))))) and (`test`.`t1`.`a2` > '0'))) union (select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` semi join (`test`.`t2i`) semi join (`test`.`t2i` join `test`.`t3i`) where ((`test`.`t1i`.`a2` = `test`.`t2i`.`b2`) and (`test`.`t3i`.`c2` = `test`.`t2i`.`b2`) and (`test`.`t2i`.`b2` = `test`.`t2i`.`b2`) and (`test`.`t1i`.`a1` = `test`.`t2i`.`b1`) and (`test`.`t3i`.`c1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` = `test`.`t2i`.`b1`) and (`test`.`t2i`.`b1` > '0') and (`test`.`t2i`.`b2` > '0')))
 (select * from t1
 where (a1, a2) in (select b1, b2 from t2
 where b2 in (select c2 from t3 where c2 LIKE '%02') or

=== modified file 'mysql-test/r/subquery_sj_all.result'
--- a/mysql-test/r/subquery_sj_all.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_all.result	2011-03-17 11:23:06 +0000
@@ -1696,9 +1696,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1823,9 +1823,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1843,9 +1843,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1915,9 +1915,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1953,9 +1953,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2110,8 +2110,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2302,8 +2302,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2443,9 +2443,9 @@ insert into t2 select * from t2;
 explain select * from t1 where (a,b,c) in (select X.a, Y.a, Z.a from t2 X, t2 Y, t2 Z where X.b=33);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	
-1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	End materialize
+1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	Using where; FirstMatch(t1)
 drop table t0,t1,t2;
 set @save_join_buffer_size = @@join_buffer_size;
 set join_buffer_size= 8192;
@@ -2497,7 +2497,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Using where; FirstMatch(ot); Using join buffer (BNL, incremental buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -2563,7 +2563,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Using where; FirstMatch(ot); Using join buffer (BNL, incremental buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -3504,8 +3504,8 @@ SELECT t1_1 .varchar_key
 FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1 .int_key
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, incremental buffers)
 SELECT t0.int_key  
 FROM t0, t2
@@ -3525,8 +3525,8 @@ FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t0, t1, t2;
 # End of bug#46550
@@ -3908,8 +3908,8 @@ WHERE PNUM IN
 (SELECT PNUM  FROM PROJ));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	STAFF	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	Start materialize
-1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	
+1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(STAFF)
 SELECT EMPNUM, EMPNAME
 FROM STAFF
 WHERE EMPNUM IN
@@ -3980,9 +3980,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4206,8 +4206,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4220,13 +4220,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4241,8 +4241,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4255,13 +4255,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4275,8 +4275,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4289,13 +4289,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1, t2, t3;
 #
@@ -4349,9 +4349,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4388,9 +4388,9 @@ WHERE 1 IN(SELECT 1
 FROM t4)));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	End materialize
+1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	FirstMatch(t1)
 SELECT *
 FROM t1
 WHERE 1 IN(SELECT 1
@@ -4531,9 +4531,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where; End temporary
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -4846,8 +4846,8 @@ alter table t3 add primary key(id), add
 The following must use loose index scan over t3, key a:
 explain select count(a) from t2 where a in ( SELECT  a FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	1000	Using index
-1	PRIMARY	t3	index	a	a	5	NULL	30000	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	1000	Using where; Using index
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	30	Using index; FirstMatch(t2)
 select count(a) from t2 where a in ( SELECT  a FROM t3);
 count(a)
 1000
@@ -5106,9 +5106,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5265,8 +5265,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5291,8 +5291,8 @@ FROM t2 JOIN t3
 WHERE t2.i + t3.i = 5);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; FirstMatch(t1)
 DROP TABLE t1,t2,t3;
 # End of the test for bug#50019.
 #
@@ -5393,4 +5393,40 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_all_jcl6.result'
--- a/mysql-test/r/subquery_sj_all_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_all_jcl6.result	2011-03-17 11:23:06 +0000
@@ -1700,9 +1700,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1827,9 +1827,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1847,9 +1847,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1919,9 +1919,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1957,9 +1957,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2114,8 +2114,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2306,8 +2306,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	Using join buffer (BKA, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary; Using join buffer (BKA, incremental buffers)
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2447,9 +2447,9 @@ insert into t2 select * from t2;
 explain select * from t1 where (a,b,c) in (select X.a, Y.a, Z.a from t2 X, t2 Y, t2 Z where X.b=33);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	
-1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	End materialize
+1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	Using where; FirstMatch(t1)
 drop table t0,t1,t2;
 set @save_join_buffer_size = @@join_buffer_size;
 set join_buffer_size= 8192;
@@ -2501,7 +2501,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Using where; FirstMatch(ot); Using join buffer (BNL, incremental buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -2567,7 +2567,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Using where; FirstMatch(ot); Using join buffer (BNL, incremental buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -3508,8 +3508,8 @@ SELECT t1_1 .varchar_key
 FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1 .int_key
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, incremental buffers)
 SELECT t0.int_key  
 FROM t0, t2
@@ -3529,8 +3529,8 @@ FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t0, t1, t2;
 # End of bug#46550
@@ -3912,8 +3912,8 @@ WHERE PNUM IN
 (SELECT PNUM  FROM PROJ));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	STAFF	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	Start materialize
-1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	
+1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(STAFF)
 SELECT EMPNUM, EMPNAME
 FROM STAFF
 WHERE EMPNUM IN
@@ -3984,9 +3984,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where; Using join buffer (BKA, incremental buffers)
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4210,8 +4210,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4224,13 +4224,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4245,8 +4245,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4259,13 +4259,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4279,8 +4279,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4293,13 +4293,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1, t2, t3;
 #
@@ -4353,9 +4353,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4392,9 +4392,9 @@ WHERE 1 IN(SELECT 1
 FROM t4)));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	End materialize
+1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	FirstMatch(t1)
 SELECT *
 FROM t1
 WHERE 1 IN(SELECT 1
@@ -4535,9 +4535,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where; Using join buffer (BKA, incremental buffers)
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where; Using join buffer (BKA, incremental buffers)
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where; End temporary; Using join buffer (BKA, incremental buffers)
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -4850,8 +4850,8 @@ alter table t3 add primary key(id), add
 The following must use loose index scan over t3, key a:
 explain select count(a) from t2 where a in ( SELECT  a FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	1000	Using index
-1	PRIMARY	t3	index	a	a	5	NULL	30000	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	1000	Using where; Using index
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	30	Using index; FirstMatch(t2)
 select count(a) from t2 where a in ( SELECT  a FROM t3);
 count(a)
 1000
@@ -5110,9 +5110,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5269,8 +5269,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5295,8 +5295,8 @@ FROM t2 JOIN t3
 WHERE t2.i + t3.i = 5);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; FirstMatch(t1)
 DROP TABLE t1,t2,t3;
 # End of the test for bug#50019.
 #
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_all_jcl7.result'
--- a/mysql-test/r/subquery_sj_all_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_all_jcl7.result	2011-03-17 11:23:06 +0000
@@ -1700,9 +1700,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1827,9 +1827,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1847,9 +1847,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1919,9 +1919,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1957,9 +1957,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2114,8 +2114,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2306,8 +2306,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	Using join buffer (BKA_UNIQUE, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary; Using join buffer (BKA_UNIQUE, regular buffers)
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2447,9 +2447,9 @@ insert into t2 select * from t2;
 explain select * from t1 where (a,b,c) in (select X.a, Y.a, Z.a from t2 X, t2 Y, t2 Z where X.b=33);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	
-1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	End materialize
+1	PRIMARY	X	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Y	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	Z	ALL	NULL	NULL	NULL	NULL	6	Using where; FirstMatch(t1)
 drop table t0,t1,t2;
 set @save_join_buffer_size = @@join_buffer_size;
 set join_buffer_size= 8192;
@@ -2501,7 +2501,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	32	Using where; FirstMatch(ot); Using join buffer (BNL, regular buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -2567,7 +2567,7 @@ a, mid(filler1, 1,10), length(filler1)=l
 from t2 ot where a in (select a from t1 it);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot	ALL	NULL	NULL	NULL	NULL	22	
-1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Materialize
+1	PRIMARY	it	ALL	NULL	NULL	NULL	NULL	52	Using where; FirstMatch(ot); Using join buffer (BNL, regular buffers)
 select 
 a, mid(filler1, 1,10), length(filler1)=length(filler2) 
 from t2 ot where a in (select a from t1 it);
@@ -3508,8 +3508,8 @@ SELECT t1_1 .varchar_key
 FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1 .int_key
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, regular buffers)
 SELECT t0.int_key  
 FROM t0, t2
@@ -3529,8 +3529,8 @@ FROM t1 AS t1_1  JOIN t1 AS t1_2 ON t1_1
 );
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; Start materialize; Scan
-1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; End materialize
+1	PRIMARY	t1_1	index	varchar_key	varchar_key	9	NULL	2	Using where; Using index; LooseScan
+1	PRIMARY	t1_2	index	NULL	int_key	5	NULL	2	Using index; FirstMatch(t1_1)
 1	PRIMARY	t0	ALL	NULL	NULL	NULL	NULL	20	Using where; Using join buffer (BNL, regular buffers)
 DROP TABLE t0, t1, t2;
 # End of bug#46550
@@ -3912,8 +3912,8 @@ WHERE PNUM IN
 (SELECT PNUM  FROM PROJ));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	STAFF	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	Start materialize
-1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	PROJ	ALL	NULL	NULL	NULL	NULL	6	
+1	PRIMARY	WORKS	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(STAFF)
 SELECT EMPNUM, EMPNAME
 FROM STAFF
 WHERE EMPNUM IN
@@ -3984,9 +3984,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, regular buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where; Using join buffer (BKA_UNIQUE, regular buffers)
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4210,8 +4210,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4224,13 +4224,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4245,8 +4245,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4259,13 +4259,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4279,8 +4279,8 @@ FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4293,13 +4293,13 @@ WHERE EMPNUM IN
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
-1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where
+1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; FirstMatch(t1)
 DEALLOCATE PREPARE stmt;
 DROP TABLE t1, t2, t3;
 #
@@ -4353,9 +4353,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4392,9 +4392,9 @@ WHERE 1 IN(SELECT 1
 FROM t4)));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	9	
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	9	
-1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	End materialize
+1	PRIMARY	t4	ALL	NULL	NULL	NULL	NULL	9	FirstMatch(t1)
 SELECT *
 FROM t1
 WHERE 1 IN(SELECT 1
@@ -4535,9 +4535,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using index condition; Using MRR; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where; Using join buffer (BKA_UNIQUE, regular buffers)
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where; Using join buffer (BKA_UNIQUE, regular buffers)
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using index condition; Using where; End temporary; Using join buffer (BKA_UNIQUE, regular buffers)
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -4850,8 +4850,8 @@ alter table t3 add primary key(id), add
 The following must use loose index scan over t3, key a:
 explain select count(a) from t2 where a in ( SELECT  a FROM t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	1000	Using index
-1	PRIMARY	t3	index	a	a	5	NULL	30000	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	1000	Using where; Using index
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	30	Using index; FirstMatch(t2)
 select count(a) from t2 where a in ( SELECT  a FROM t3);
 count(a)
 1000
@@ -5110,9 +5110,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5269,8 +5269,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, regular buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5295,8 +5295,8 @@ FROM t2 JOIN t3
 WHERE t2.i + t3.i = 5);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	10	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	10	Using where
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	10	Using where; FirstMatch(t1)
 DROP TABLE t1,t2,t3;
 # End of the test for bug#50019.
 #
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed.result'
--- a/mysql-test/r/subquery_sj_dupsweed.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed.result	2011-03-17 11:23:06 +0000
@@ -1541,8 +1541,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1718,8 +1718,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1745,8 +1745,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2162,8 +2162,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2735,8 +2735,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -4084,8 +4084,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4094,15 +4094,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Start temporary
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -5392,4 +5392,40 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_jcl6.result'
--- a/mysql-test/r/subquery_sj_dupsweed_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_jcl6.result	2011-03-17 11:23:06 +0000
@@ -1545,8 +1545,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1722,8 +1722,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1749,8 +1749,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2166,8 +2166,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2739,8 +2739,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -4088,8 +4088,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4098,15 +4098,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Start temporary
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -5396,5 +5396,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_dupsweed_jcl7.result'
--- a/mysql-test/r/subquery_sj_dupsweed_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_dupsweed_jcl7.result	2011-03-17 11:23:06 +0000
@@ -1545,8 +1545,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1722,8 +1722,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1749,8 +1749,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2166,8 +2166,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, regular buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2739,8 +2739,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -4088,8 +4088,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4098,15 +4098,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Start temporary
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Start temporary
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; End temporary; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Start temporary
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -5396,5 +5396,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_firstmatch.result'
--- a/mysql-test/r/subquery_sj_firstmatch.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch.result	2011-03-17 11:23:06 +0000
@@ -5394,6 +5394,42 @@ deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
 #
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	FirstMatch(t2)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
+#
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for
 #           certain query plans
 #

=== modified file 'mysql-test/r/subquery_sj_firstmatch_jcl6.result'
--- a/mysql-test/r/subquery_sj_firstmatch_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_jcl6.result	2011-03-17 11:23:06 +0000
@@ -5398,6 +5398,42 @@ deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
 #
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	FirstMatch(t2)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
+#
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for
 #           certain query plans
 #

=== modified file 'mysql-test/r/subquery_sj_firstmatch_jcl7.result'
--- a/mysql-test/r/subquery_sj_firstmatch_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_firstmatch_jcl7.result	2011-03-17 11:23:06 +0000
@@ -5398,6 +5398,42 @@ deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
 #
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	FirstMatch(t2)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
+#
 # Bug#51457 Firstmatch semijoin strategy gives wrong results for
 #           certain query plans
 #

=== modified file 'mysql-test/r/subquery_sj_innodb_all.result'
--- a/mysql-test/r/subquery_sj_innodb_all.result	2011-01-24 11:56:54 +0000
+++ b/mysql-test/r/subquery_sj_innodb_all.result	2011-03-17 11:23:06 +0000
@@ -31,8 +31,8 @@ a	b
 9	5
 explain select * from t2 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	End temporary
 select * from t2 where b in (select a from t1);
 a	b
 1	1
@@ -49,8 +49,8 @@ primary key(pk1, pk2, pk3)
 insert into t3 select a,a, a,a,a from t0;
 explain select * from t3 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	End temporary
 select * from t3 where b in (select a from t1);
 a	b	pk1	pk2	pk3
 1	1	1	1	1
@@ -90,8 +90,8 @@ set join_buffer_size= @save_join_buffer_
 set max_heap_table_size= @save_max_heap_table_size;
 explain select * from t1 where a in (select b from t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	t2	index	b	b	5	NULL	10	Using index; Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	Using index; FirstMatch(t1)
 select * from t1;
 a	b
 1	1

=== modified file 'mysql-test/r/subquery_sj_innodb_all_jcl6.result'
--- a/mysql-test/r/subquery_sj_innodb_all_jcl6.result	2011-01-24 11:56:54 +0000
+++ b/mysql-test/r/subquery_sj_innodb_all_jcl6.result	2011-03-17 11:23:06 +0000
@@ -35,8 +35,8 @@ a	b
 9	5
 explain select * from t2 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	Using join buffer (BKA, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	End temporary; Using join buffer (BKA, incremental buffers)
 select * from t2 where b in (select a from t1);
 a	b
 1	1
@@ -53,8 +53,8 @@ primary key(pk1, pk2, pk3)
 insert into t3 select a,a, a,a,a from t0;
 explain select * from t3 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	Using join buffer (BKA, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	End temporary; Using join buffer (BKA, incremental buffers)
 select * from t3 where b in (select a from t1);
 a	b	pk1	pk2	pk3
 1	1	1	1	1
@@ -94,8 +94,8 @@ set join_buffer_size= @save_join_buffer_
 set max_heap_table_size= @save_max_heap_table_size;
 explain select * from t1 where a in (select b from t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	t2	index	b	b	5	NULL	10	Using index; Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	5	Using index; FirstMatch(t1)
 select * from t1;
 a	b
 1	1

=== modified file 'mysql-test/r/subquery_sj_innodb_all_jcl7.result'
--- a/mysql-test/r/subquery_sj_innodb_all_jcl7.result	2011-01-24 11:56:54 +0000
+++ b/mysql-test/r/subquery_sj_innodb_all_jcl7.result	2011-03-17 11:23:06 +0000
@@ -35,8 +35,8 @@ a	b
 9	5
 explain select * from t2 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	Using join buffer (BKA_UNIQUE, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	1	End temporary; Using join buffer (BKA_UNIQUE, regular buffers)
 select * from t2 where b in (select a from t1);
 a	b
 1	1
@@ -53,8 +53,8 @@ primary key(pk1, pk2, pk3)
 insert into t3 select a,a, a,a,a from t0;
 explain select * from t3 where b in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	Using join buffer (BKA_UNIQUE, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where; Start temporary
+1	PRIMARY	t3	ref	b	b	5	test.t1.a	1	End temporary; Using join buffer (BKA_UNIQUE, regular buffers)
 select * from t3 where b in (select a from t1);
 a	b	pk1	pk2	pk3
 1	1	1	1	1
@@ -94,8 +94,8 @@ set join_buffer_size= @save_join_buffer_
 set max_heap_table_size= @save_max_heap_table_size;
 explain select * from t1 where a in (select b from t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
-1	PRIMARY	t2	index	b	b	5	NULL	10	Using index; Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
+1	PRIMARY	t2	ref	b	b	5	test.t1.a	5	Using index; FirstMatch(t1)
 select * from t1;
 a	b
 1	1

=== modified file 'mysql-test/r/subquery_sj_loosescan.result'
--- a/mysql-test/r/subquery_sj_loosescan.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_loosescan.result	2011-03-17 11:23:06 +0000
@@ -1542,8 +1542,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1719,8 +1719,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1746,8 +1746,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2163,8 +2163,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2736,8 +2736,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -5393,4 +5393,40 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_jcl6.result'
--- a/mysql-test/r/subquery_sj_loosescan_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_jcl6.result	2011-03-17 11:23:06 +0000
@@ -1546,8 +1546,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1723,8 +1723,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1750,8 +1750,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2167,8 +2167,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2740,8 +2740,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_loosescan_jcl7.result'
--- a/mysql-test/r/subquery_sj_loosescan_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_loosescan_jcl7.result	2011-03-17 11:23:06 +0000
@@ -1546,8 +1546,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1723,8 +1723,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1750,8 +1750,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2167,8 +2167,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Start temporary
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Using where; End temporary; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Start temporary
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; End temporary; Using join buffer (BNL, regular buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2740,8 +2740,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End temporary
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_mat.result'
--- a/mysql-test/r/subquery_sj_mat.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_mat.result	2011-03-17 11:23:06 +0000
@@ -56,8 +56,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using index; Start temporary; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
@@ -66,8 +66,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; Start temporary; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
@@ -93,8 +93,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10005	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; Start temporary; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 drop table t0, t1, t2, t3;
@@ -938,7 +938,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -969,7 +969,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1112,7 +1112,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1121,8 +1121,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1132,7 +1132,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1142,7 +1142,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1159,8 +1159,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1169,7 +1169,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1244,8 +1244,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1255,7 +1255,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1264,8 +1264,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1274,8 +1274,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1299,8 +1299,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1314,8 +1314,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1324,8 +1324,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1334,8 +1334,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1344,8 +1344,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1362,8 +1362,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1371,8 +1371,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1542,8 +1542,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1696,9 +1696,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1719,8 +1719,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1736,9 +1736,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1746,8 +1746,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1823,9 +1823,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1843,9 +1843,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1853,9 +1853,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1878,9 +1878,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1915,9 +1915,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1953,9 +1953,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2110,8 +2110,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2163,8 +2163,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize; Scan
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2176,8 +2176,8 @@ create table t3 (a int);
 insert into t3 select A.a + 10*B.a from t0 A, t0 B;
 explain select * from t3 where a in (select kp1 from t1 where kp1<20);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20);
 a
 0
@@ -2202,8 +2202,8 @@ a
 19
 explain select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 a
 0
@@ -2231,9 +2231,9 @@ insert into t4 select a from t3;
 explain select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize; Scan
 1	PRIMARY	t4	eq_ref	PRIMARY	PRIMARY	4	test.t1.c	1	Using index; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 a
@@ -2302,8 +2302,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2393,12 +2393,12 @@ insert into t1 select A.a, B.a, 'filler'
 create table t2 as select * from t1;
 explain select * from t2 where a in (select b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 explain select * from t2 where (b,a) in (select a,b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 drop table t1,t2;
 create table t1 (a int, b int);
 insert into t1 select a,a from t0;
@@ -2736,8 +2736,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -3980,9 +3980,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4085,8 +4085,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4095,15 +4095,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -4205,9 +4205,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4219,14 +4219,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4240,9 +4240,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4254,14 +4254,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4349,9 +4349,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4531,9 +4531,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where; End temporary
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -5041,8 +5041,8 @@ INSERT INTO t2 VALUES (1, 0), (1, 1), (2
 EXPLAIN
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize
+1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 i
 1
@@ -5106,9 +5106,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5265,8 +5265,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5393,4 +5393,40 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_mat_jcl6.result'
--- a/mysql-test/r/subquery_sj_mat_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_mat_jcl6.result	2011-03-17 11:23:06 +0000
@@ -60,8 +60,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
@@ -70,8 +70,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
@@ -97,8 +97,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10005	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 drop table t0, t1, t2, t3;
@@ -942,7 +942,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -973,7 +973,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1116,7 +1116,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1125,8 +1125,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1136,7 +1136,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1146,7 +1146,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1163,8 +1163,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1173,7 +1173,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1248,8 +1248,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1259,7 +1259,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1268,8 +1268,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1278,8 +1278,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1303,8 +1303,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1318,8 +1318,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1328,8 +1328,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1338,8 +1338,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1348,8 +1348,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1366,8 +1366,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1375,8 +1375,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1546,8 +1546,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1700,9 +1700,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1723,8 +1723,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1740,9 +1740,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1750,8 +1750,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1827,9 +1827,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1847,9 +1847,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1857,9 +1857,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1882,9 +1882,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1919,9 +1919,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1957,9 +1957,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2114,8 +2114,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, incremental buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2167,8 +2167,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize; Scan
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; Using join buffer (BNL, incremental buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2180,8 +2180,8 @@ create table t3 (a int);
 insert into t3 select A.a + 10*B.a from t0 A, t0 B;
 explain select * from t3 where a in (select kp1 from t1 where kp1<20);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20);
 a
 0
@@ -2206,8 +2206,8 @@ a
 19
 explain select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 a
 0
@@ -2235,9 +2235,9 @@ insert into t4 select a from t3;
 explain select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize; Scan
 1	PRIMARY	t4	eq_ref	PRIMARY	PRIMARY	4	test.t1.c	1	Using index; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 a
@@ -2306,8 +2306,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2397,12 +2397,12 @@ insert into t1 select A.a, B.a, 'filler'
 create table t2 as select * from t1;
 explain select * from t2 where a in (select b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 explain select * from t2 where (b,a) in (select a,b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, incremental buffers)
 drop table t1,t2;
 create table t1 (a int, b int);
 insert into t1 select a,a from t0;
@@ -2740,8 +2740,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, incremental buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -3984,9 +3984,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4089,8 +4089,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4099,15 +4099,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, incremental buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -4209,9 +4209,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4223,14 +4223,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4244,9 +4244,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4258,14 +4258,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4353,9 +4353,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4535,9 +4535,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where; End temporary
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -5045,8 +5045,8 @@ INSERT INTO t2 VALUES (1, 0), (1, 1), (2
 EXPLAIN
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize
+1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, incremental buffers)
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 i
 1
@@ -5110,9 +5110,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, incremental buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, incremental buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5269,8 +5269,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, incremental buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_mat_jcl7.result'
--- a/mysql-test/r/subquery_sj_mat_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_mat_jcl7.result	2011-03-17 11:23:06 +0000
@@ -60,8 +60,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
@@ -70,8 +70,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10004	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
@@ -97,8 +97,8 @@ a
 4
 explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using index
-1	PRIMARY	t1	index	a	a	10	NULL	10005	100.00	Using where; Using index; Materialize
+1	PRIMARY	t2	index	a	a	5	NULL	4	100.00	Using where; Using index; Start temporary
+1	PRIMARY	t1	ref	a	a	5	test.t2.a	101	100.00	Using where; Using index; End temporary
 Warnings:
 Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 drop table t0, t1, t2, t3;
@@ -942,7 +942,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -973,7 +973,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1116,7 +1116,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1125,8 +1125,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1136,7 +1136,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1146,7 +1146,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1163,8 +1163,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1173,7 +1173,7 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1248,8 +1248,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1259,7 +1259,7 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
 1	PRIMARY	t3	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary; End temporary
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1268,8 +1268,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1278,8 +1278,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1303,8 +1303,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1318,8 +1318,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1328,8 +1328,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1338,8 +1338,8 @@ EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1348,8 +1348,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1366,8 +1366,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1375,8 +1375,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1546,8 +1546,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1700,9 +1700,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1723,8 +1723,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1740,9 +1740,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 RIGHT JOIN t3 ON t2.i=t3.i);
 i
@@ -1750,8 +1750,8 @@ EXPLAIN SELECT * FROM t1 WHERE (11) IN
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	1	
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1827,9 +1827,9 @@ SELECT (SELECT COUNT(*) from t1),
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1847,9 +1847,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize; Scan
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1857,9 +1857,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 LEFT JOIN t3 ON t2.i=t3.i);
 i
@@ -1882,9 +1882,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	1	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1919,9 +1919,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -1957,9 +1957,9 @@ i
 EXPLAIN SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	Using where; End temporary; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (11) IN 
 (SELECT t3.i FROM t2 STRAIGHT_JOIN t3);
 i
@@ -2114,8 +2114,8 @@ t22.a in (select t12.a from t11, t12 whe
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t11	ALL	NULL	NULL	NULL	NULL	8	Using where; Using temporary; Using filesort; Start materialize; Scan
 1	PRIMARY	t12	ALL	NULL	NULL	NULL	NULL	8	Using where; End materialize
-1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t21	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t22	ALL	NULL	NULL	NULL	NULL	26	Using where; Using join buffer (BNL, regular buffers)
 select t21.* from t21,t22 where t21.a = t22.a and 
 t22.a in (select t12.a from t11, t12 where t11.a in(255,256) and t11.a = t12.a and t11.c is null) and t22.c is null order by t21.a;
 a	b	c
@@ -2167,8 +2167,8 @@ a
 19
 explain select * from (select a from t0) X where a in (select a from t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	20	Materialize; Scan
+1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	11	Using where; Using join buffer (BNL, regular buffers)
 2	DERIVED	t0	ALL	NULL	NULL	NULL	NULL	11	
 drop table t0, t1;
 create table t0 (a int);
@@ -2180,8 +2180,8 @@ create table t3 (a int);
 insert into t3 select A.a + 10*B.a from t0 A, t0 B;
 explain select * from t3 where a in (select kp1 from t1 where kp1<20);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, regular buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20);
 a
 0
@@ -2206,8 +2206,8 @@ a
 19
 explain select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Using index; Materialize; Scan
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, regular buffers)
 select * from t3 where a in (select kp1 from t1 where kp1<20) and a<20;
 a
 0
@@ -2235,9 +2235,9 @@ insert into t4 select a from t3;
 explain select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	
-1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize
+1	PRIMARY	t1	range	kp1	kp1	5	NULL	48	Using where; Start materialize; Scan
 1	PRIMARY	t4	eq_ref	PRIMARY	PRIMARY	4	test.t1.c	1	Using index; End materialize
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, regular buffers)
 select * from t3 where a in 
 (select t1.kp1 from t1,t4 where kp1<20 and t4.pk=t1.c);
 a
@@ -2306,8 +2306,8 @@ create table t3 ( a int , filler char(10
 insert into t3 select A.a + 10*B.a, 'filler' from t0 A, t0 B;
 explain select * from t3 where a in (select a from t2) and (a > 5 or a < 10);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Materialize; Scan
-1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where; Start temporary
+1	PRIMARY	t3	ref	a	a	5	test.t2.a	1	End temporary
 select * from t3 where a in (select a from t2);
 a	filler
 1	filler
@@ -2397,12 +2397,12 @@ insert into t1 select A.a, B.a, 'filler'
 create table t2 as select * from t1;
 explain select * from t2 where a in (select b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, regular buffers)
 explain select * from t2 where (b,a) in (select a,b from t1 where a=3);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where
-1	PRIMARY	t1	ref	a	a	10	const,test.t2.a	8	Using index; Materialize
+1	PRIMARY	t1	ref	a	a	5	const	8	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	100	Using where; Using join buffer (BNL, regular buffers)
 drop table t1,t2;
 create table t1 (a int, b int);
 insert into t1 select a,a from t0;
@@ -2740,8 +2740,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Start temporary
 1	PRIMARY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where; Using join buffer (BNL, regular buffers)
 1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2_16` join `test`.`t2` join `test`.`t1_16`) where ((`test`.`t2_16`.`b2` = `test`.`t1_16`.`a2`) and (`test`.`t2_16`.`b1` = `test`.`t1_16`.`a1`) and (`test`.`t2`.`b1` = `test`.`t3`.`c1`) and (`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and (`test`.`t3`.`c2` > '0') and (concat(`test`.`t1`.`a1`,'x') = left(`test`.`t1_16`.`a1`,8)))
@@ -3984,9 +3984,9 @@ INNER JOIN t2 c ON c.idContact=cona.idCo
 WHERE cona.postalStripped='T2H3B2'
 	);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start materialize; Scan
-1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	End materialize
-1	PRIMARY	a	index	PRIMARY	PRIMARY	4	NULL	2	100.00	Using where; Using index; Using join buffer (BNL, regular buffers)
+1	PRIMARY	cona	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Start temporary
+1	PRIMARY	c	eq_ref	PRIMARY	PRIMARY	4	test.cona.idContact	1	100.00	Using where
+1	PRIMARY	a	eq_ref	PRIMARY	PRIMARY	4	test.c.idObj	1	100.00	Using index; End temporary
 Warnings:
 Note	1003	select `test`.`a`.`idIndividual` AS `idIndividual` from `test`.`t1` `a` semi join (`test`.`t3` `cona` join `test`.`t2` `c`) where ((`test`.`c`.`idContact` = `test`.`cona`.`idContact`) and (`test`.`a`.`idIndividual` = `test`.`c`.`idObj`) and (`test`.`cona`.`postalStripped` = 'T2H3B2'))
 drop table t1,t2,t3;
@@ -4089,8 +4089,8 @@ CREATE INDEX I1 ON t1 (a);
 CREATE INDEX I2 ON t1 (b);
 EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 a	b
 CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
@@ -4099,15 +4099,15 @@ CREATE INDEX I1 ON t2 (a);
 CREATE INDEX I2 ON t2 (b);
 EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize
+1	PRIMARY	t2	index	I1	I1	4	NULL	2	Using index; Materialize; Scan
+1	PRIMARY	t2	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 a	b
 EXPLAIN
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	
-1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize
+1	PRIMARY	t1	index	I1	I1	2	NULL	2	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	I2	NULL	NULL	NULL	2	Using where; Using join buffer (BNL, regular buffers)
 SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 a	b
 DROP TABLE t1,t2;
@@ -4209,9 +4209,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4223,14 +4223,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 CREATE INDEX t1_IDX ON t1(EMPNUM);
@@ -4244,9 +4244,9 @@ WHERE PNUM IN
 FROM t2
 WHERE PTYPE = 'Design'));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
 FROM t1
 WHERE EMPNUM IN
@@ -4258,14 +4258,14 @@ WHERE EMPNUM IN
         WHERE PTYPE = 'Design'))";
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	PRIMARY	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 EXECUTE stmt;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	
-1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize
+1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	6	Using where; Start materialize; Scan
 1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	12	Using where; End materialize
+1	SIMPLE	t1	ALL	t1_IDX	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 DEALLOCATE PREPARE stmt;
 DROP INDEX t1_IDX ON t1;
 EXPLAIN SELECT EMPNAME
@@ -4353,9 +4353,9 @@ FROM t2
 WHERE 1 IN(SELECT 1
 FROM t3));
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End materialize
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	100.00	Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`id` AS `id` from `test`.`t1` semi join (`test`.`t3` join `test`.`t2`) where 1
 delete from t2;
@@ -4535,9 +4535,9 @@ t2.Code IN (SELECT Country FROM t3
 WHERE Language='English' AND Percentage > 10 AND
 t2.Population > 100000);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Materialize; Scan
+1	PRIMARY	t1	range	Population,Country	Population	4	NULL	1	Using where; Start temporary
 1	PRIMARY	t2	eq_ref	PRIMARY,Population	PRIMARY	3	test.t1.Country	1	Using where
-1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where
+1	PRIMARY	t3	eq_ref	PRIMARY,Percentage	PRIMARY	33	test.t2.Code,const	1	Using where; End temporary
 DROP TABLE t1,t2,t3;
 CREATE TABLE t1 (
 Code char(3) NOT NULL DEFAULT '',
@@ -5045,8 +5045,8 @@ INSERT INTO t2 VALUES (1, 0), (1, 1), (2
 EXPLAIN
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
-1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize
+1	PRIMARY	t2	index	k	k	10	NULL	4	Using where; Using index; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where; Using join buffer (BNL, regular buffers)
 SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
 i
 1
@@ -5110,9 +5110,9 @@ INSERT INTO t3 VALUES (1), (2);
 explain extended SELECT * FROM t1 WHERE (t1.i) IN 
 (SELECT t3.i FROM t3 LEFT JOIN t2 ON t2.i=t3.i);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
-1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start materialize; Scan
-1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; End materialize
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t3	ALL	NULL	NULL	NULL	NULL	2	100.00	Start temporary
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	1	100.00	Using where; Using join buffer (BNL, regular buffers)
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	2	100.00	Using where; End temporary; Using join buffer (BNL, regular buffers)
 Warnings:
 Note	1003	select `test`.`t1`.`i` AS `i` from `test`.`t1` semi join (`test`.`t3` left join `test`.`t2` on((`test`.`t2`.`i` = `test`.`t3`.`i`))) where (`test`.`t1`.`i` = `test`.`t3`.`i`)
 SELECT * FROM t1 WHERE (t1.i) IN 
@@ -5269,8 +5269,8 @@ WHERE (varchar_nokey, varchar_nokey) IN
 FROM t2  
 WHERE varchar_nokey < 'n' XOR pk);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
-1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	
-1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize
+1	PRIMARY	t2	ALL	varchar_key	NULL	NULL	NULL	15	Using where; Materialize; Scan
+1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	18	Using where; Using join buffer (BNL, regular buffers)
 DROP TABLE t1, t2;
 # End of the test for bug#45174.
 #
@@ -5397,5 +5397,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	1	
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+1	PRIMARY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where; Start materialize
+1	PRIMARY	b2	ALL	NULL	NULL	NULL	NULL	2	End materialize
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_mat_nosj.result'
--- a/mysql-test/r/subquery_sj_mat_nosj.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_mat_nosj.result	2011-03-17 11:23:06 +0000
@@ -2951,8 +2951,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
 2	DEPENDENT SUBQUERY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
-3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where
-3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
+3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; Using join buffer (BNL, incremental buffers)
 4	SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(concat(`test`.`t1`.`a1`,'x'),<exists>(select 1 from `test`.`t1_16` where (<in_optimizer>((`test`.`t1_16`.`a1`,`test`.`t1_16`.`a2`),<exists>(select `test`.`t2_16`.`b1`,`test`.`t2_16`.`b2` from `test`.`t2_16` join `test`.`t2` where ((`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and <in_optimizer>(`test`.`t2`.`b1`,`test`.`t2`.`b1` in ( <materialize> (select `test`.`t3`.`c1` from `test`.`t3` where (`test`.`t3`.`c2` > '0') ), <primary_index_lookup>(`test`.`t2`.`b1` in <temporary table> on distinct_key where ((`test`.`t2`.`b1` = `materialized subselect`.`c1`))))) and (<cache>(`test`.`t1_16`.`a1`) = `test`.`t2_16`.`b1`) and (<cache>(`test`.`t1_16`.`a2`) = `test`.`t2_16`.`b2`)))) and (<cache>(concat(`test`.`t1`.`a1`,'x')) = left(`test`.`t1_16`.`a1`,8)))))
@@ -5615,4 +5615,40 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	
+2	SUBQUERY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	SUBQUERY	b2	ALL	NULL	NULL	NULL	NULL	2	
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;

=== modified file 'mysql-test/r/subquery_sj_none.result'
--- a/mysql-test/r/subquery_sj_none.result	2011-02-22 14:24:33 +0000
+++ b/mysql-test/r/subquery_sj_none.result	2011-03-17 08:00:10 +0000
@@ -5539,6 +5539,29 @@ deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
 #
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	
+2	DEPENDENT SUBQUERY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	b2	ALL	NULL	NULL	NULL	NULL	2	
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
 # Bug#11766642: crash in Item_field::register_field_in_read_map with view
 #
 CREATE TABLE t1(a INT);

=== modified file 'mysql-test/r/subquery_sj_none_jcl6.result'
--- a/mysql-test/r/subquery_sj_none_jcl6.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_none_jcl6.result	2011-03-17 11:23:06 +0000
@@ -80,10 +80,10 @@ a
 explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
-2	DEPENDENT SUBQUERY	t1	ref	a	a	5	func	1001	100.00	Using index
-2	DEPENDENT SUBQUERY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index; Using join buffer (BNL, incremental buffers)
+2	DEPENDENT SUBQUERY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index
+2	DEPENDENT SUBQUERY	t1	ref	a	a	10	func,test.t3.a	1167	100.00	Using index
 Warnings:
-Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 from `test`.`t1` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
+Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 from `test`.`t1` join `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
 insert into t1 values (3,31);
 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 a
@@ -2885,8 +2885,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
 2	DEPENDENT SUBQUERY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
-3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where
-3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, incremental buffers)
+3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
+3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; Using join buffer (BNL, incremental buffers)
 4	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(concat(`test`.`t1`.`a1`,'x'),<exists>(select 1 from `test`.`t1_16` where (<in_optimizer>((`test`.`t1_16`.`a1`,`test`.`t1_16`.`a2`),<exists>(select `test`.`t2_16`.`b1`,`test`.`t2_16`.`b2` from `test`.`t2_16` join `test`.`t2` where ((`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and <in_optimizer>(`test`.`t2`.`b1`,<exists>(select 1 from `test`.`t3` where ((`test`.`t3`.`c2` > '0') and (<cache>(`test`.`t2`.`b1`) = `test`.`t3`.`c1`)))) and (<cache>(`test`.`t1_16`.`a1`) = `test`.`t2_16`.`b1`) and (<cache>(`test`.`t1_16`.`a2`) = `test`.`t2_16`.`b2`)))) and (<cache>(concat(`test`.`t1`.`a1`,'x')) = left(`test`.`t1_16`.`a1`,8)))))
@@ -3956,8 +3956,8 @@ WHERE int_nokey IN (SELECT it2.int_key
 FROM it1 LEFT JOIN it2 ON it2.datetime_key);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot1	ALL	NULL	NULL	NULL	NULL	20	Using where
+2	DEPENDENT SUBQUERY	it1	index	NULL	int_key	4	NULL	2	Using index
 2	DEPENDENT SUBQUERY	it2	ref	int_key	int_key	4	func	3	Using where
-2	DEPENDENT SUBQUERY	it1	index	NULL	int_key	4	NULL	2	Using index; Using join buffer (BNL, incremental buffers)
 DROP TABLE ot1, it1, it2;
 # End of BUG#38075
 # 
@@ -5225,8 +5225,8 @@ and t2.uid=t1.fid;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	11	Using where
 1	PRIMARY	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.fid	1	
+2	DEPENDENT SUBQUERY	t4	eq_ref	PRIMARY	PRIMARY	4	func	1	Using index
 2	DEPENDENT SUBQUERY	t3	ref	uid	uid	5	const	4	Using where
-2	DEPENDENT SUBQUERY	t4	eq_ref	PRIMARY	PRIMARY	4	func	1	Using where; Using index
 select name from t2, t1 
 where t1.uid in (select t4.uid from t4, t3 where t3.uid=1 and t4.uid=t3.fid)
 and t2.uid=t1.fid;
@@ -5542,5 +5542,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	
+2	DEPENDENT SUBQUERY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	b2	ALL	NULL	NULL	NULL	NULL	2	
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/r/subquery_sj_none_jcl7.result'
--- a/mysql-test/r/subquery_sj_none_jcl7.result	2011-02-14 11:21:26 +0000
+++ b/mysql-test/r/subquery_sj_none_jcl7.result	2011-03-17 11:23:06 +0000
@@ -80,10 +80,10 @@ a
 explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t2	index	NULL	a	5	NULL	4	100.00	Using where; Using index
-2	DEPENDENT SUBQUERY	t1	ref	a	a	5	func	1001	100.00	Using index
-2	DEPENDENT SUBQUERY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index; Using join buffer (BNL, regular buffers)
+2	DEPENDENT SUBQUERY	t3	index	a	a	5	NULL	3	100.00	Using where; Using index
+2	DEPENDENT SUBQUERY	t1	ref	a	a	10	func,test.t3.a	1167	100.00	Using index
 Warnings:
-Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 from `test`.`t1` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
+Note	1003	select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 from `test`.`t1` join `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
 insert into t1 values (3,31);
 select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 a
@@ -2885,8 +2885,8 @@ t2.b1 IN (select c1 from t3 where c2 > '
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	filtered	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
 2	DEPENDENT SUBQUERY	t1_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
-3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where
-3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where; Using join buffer (BNL, regular buffers)
+3	DEPENDENT SUBQUERY	t2_16	ALL	NULL	NULL	NULL	NULL	3	100.00	Using where
+3	DEPENDENT SUBQUERY	t2	ALL	NULL	NULL	NULL	NULL	5	100.00	Using where; Using join buffer (BNL, regular buffers)
 4	DEPENDENT SUBQUERY	t3	ALL	NULL	NULL	NULL	NULL	4	100.00	Using where
 Warnings:
 Note	1003	select `test`.`t1`.`a1` AS `a1`,`test`.`t1`.`a2` AS `a2` from `test`.`t1` where <in_optimizer>(concat(`test`.`t1`.`a1`,'x'),<exists>(select 1 from `test`.`t1_16` where (<in_optimizer>((`test`.`t1_16`.`a1`,`test`.`t1_16`.`a2`),<exists>(select `test`.`t2_16`.`b1`,`test`.`t2_16`.`b2` from `test`.`t2_16` join `test`.`t2` where ((`test`.`t2`.`b2` = substr(`test`.`t2_16`.`b2`,1,6)) and <in_optimizer>(`test`.`t2`.`b1`,<exists>(select 1 from `test`.`t3` where ((`test`.`t3`.`c2` > '0') and (<cache>(`test`.`t2`.`b1`) = `test`.`t3`.`c1`)))) and (<cache>(`test`.`t1_16`.`a1`) = `test`.`t2_16`.`b1`) and (<cache>(`test`.`t1_16`.`a2`) = `test`.`t2_16`.`b2`)))) and (<cache>(concat(`test`.`t1`.`a1`,'x')) = left(`test`.`t1_16`.`a1`,8)))))
@@ -3956,8 +3956,8 @@ WHERE int_nokey IN (SELECT it2.int_key
 FROM it1 LEFT JOIN it2 ON it2.datetime_key);
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	ot1	ALL	NULL	NULL	NULL	NULL	20	Using where
+2	DEPENDENT SUBQUERY	it1	index	NULL	int_key	4	NULL	2	Using index
 2	DEPENDENT SUBQUERY	it2	ref	int_key	int_key	4	func	3	Using where
-2	DEPENDENT SUBQUERY	it1	index	NULL	int_key	4	NULL	2	Using index; Using join buffer (BNL, regular buffers)
 DROP TABLE ot1, it1, it2;
 # End of BUG#38075
 # 
@@ -5225,8 +5225,8 @@ and t2.uid=t1.fid;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	11	Using where
 1	PRIMARY	t2	eq_ref	PRIMARY	PRIMARY	4	test.t1.fid	1	
+2	DEPENDENT SUBQUERY	t4	eq_ref	PRIMARY	PRIMARY	4	func	1	Using index
 2	DEPENDENT SUBQUERY	t3	ref	uid	uid	5	const	4	Using where
-2	DEPENDENT SUBQUERY	t4	eq_ref	PRIMARY	PRIMARY	4	func	1	Using where; Using index
 select name from t2, t1 
 where t1.uid in (select t4.uid from t4, t3 where t3.uid=1 and t4.uid=t3.fid)
 and t2.uid=t1.fid;
@@ -5542,5 +5542,41 @@ a	a	a
 deallocate prepare s;
 DROP TABLE ot1, ot2, ot3, it1;
 # End of the test for bug#57623.
+#
+# Bug#11766739: Crash in tmp_table_param::init() with semijoin=on
+#
+CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
+CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (1,1), (2,1);
+EXPLAIN SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
+1	PRIMARY	t2	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	t1	system	NULL	NULL	NULL	NULL	1	
+2	DEPENDENT SUBQUERY	b1	ALL	NULL	NULL	NULL	NULL	2	Using where
+2	DEPENDENT SUBQUERY	b2	ALL	NULL	NULL	NULL	NULL	2	
+SELECT * FROM t2
+WHERE f2 IN (SELECT t1.f1
+FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
+f1	f2
+1	1
+2	1
+DROP TABLE t1, t2;
+# End of the test for bug#11766739.
+#
+# Bug#11766642: crash in Item_field::register_field_in_read_map with view
+#
+CREATE TABLE t1(a INT);
+CREATE VIEW v1 AS SELECT a FROM t1;
+INSERT INTO t1 VALUES (0),(1),(2);
+SELECT a FROM t1 WHERE a IN 
+(SELECT a XOR a FROM v1)
+ORDER BY a;
+a
+0
+DROP TABLE t1;
+DROP VIEW v1;
 set optimizer_switch=default;
 set optimizer_join_cache_level=default;

=== modified file 'mysql-test/suite/innodb/include/innodb_stats.inc'
--- a/mysql-test/suite/innodb/include/innodb_stats.inc	2010-04-21 15:24:51 +0000
+++ b/mysql-test/suite/innodb/include/innodb_stats.inc	2011-03-18 12:49:38 +0000
@@ -16,3 +16,11 @@ table_name = 'test_innodb_stats' AND
 index_name = 'a_key' AND
 stat_name IN ('n_diff_pfx01', 'n_diff_pfx02', 'n_leaf_pages', 'size')
 ORDER BY stat_name;
+
+# provoke reading from persistent storage from
+# row_update_statistics_if_needed()
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+
+# see what has been read
+query_vertical SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';

=== modified file 'mysql-test/suite/innodb/r/innodb_stats.result'
--- a/mysql-test/suite/innodb/r/innodb_stats.result	2010-04-21 15:24:51 +0000
+++ b/mysql-test/suite/innodb/r/innodb_stats.result	2011-03-18 12:49:38 +0000
@@ -33,6 +33,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	0
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1);
 ANALYZE TABLE test_innodb_stats;
@@ -66,6 +85,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	1
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (1);
 ANALYZE TABLE test_innodb_stats;
@@ -99,6 +137,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	2
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (1), (1);
 ANALYZE TABLE test_innodb_stats;
@@ -132,6 +189,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	3
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (1), (1), (1), (1), (1), (1), (1), (1), (1);
 ANALYZE TABLE test_innodb_stats;
@@ -165,6 +241,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	2
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (2);
 ANALYZE TABLE test_innodb_stats;
@@ -198,6 +293,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	2
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (1), (2);
 ANALYZE TABLE test_innodb_stats;
@@ -231,6 +345,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	3
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (2), (3);
 ANALYZE TABLE test_innodb_stats;
@@ -264,6 +397,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	3
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (1), (2), (3), (3);
 ANALYZE TABLE test_innodb_stats;
@@ -297,6 +449,25 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	5
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	
 DELETE FROM test_innodb_stats;
 INSERT INTO test_innodb_stats (a) VALUES (1), (2), (3), (4), (5), (1), (2), (3), (4), (5);
 ANALYZE TABLE test_innodb_stats;
@@ -330,3 +501,22 @@ stat_name	size
 stat_value	1
 sample_size	NULL
 stat_description	Number of pages in the index
+INSERT INTO test_innodb_stats SELECT * FROM test_innodb_stats;
+SELECT * FROM information_schema.statistics
+WHERE table_name = 'test_innodb_stats';
+TABLE_CATALOG	def
+TABLE_SCHEMA	test
+TABLE_NAME	test_innodb_stats
+NON_UNIQUE	1
+INDEX_SCHEMA	test
+INDEX_NAME	a_key
+SEQ_IN_INDEX	1
+COLUMN_NAME	a
+COLLATION	A
+CARDINALITY	10
+SUB_PART	NULL
+PACKED	NULL
+NULLABLE	YES
+INDEX_TYPE	BTREE
+COMMENT	
+INDEX_COMMENT	

=== modified file 'mysql-test/suite/perfschema/r/aggregate.result'
--- a/mysql-test/suite/perfschema/r/aggregate.result	2011-03-29 00:46:15 +0000
+++ b/mysql-test/suite/perfschema/r/aggregate.result	2011-04-01 22:04:26 +0000
@@ -71,7 +71,6 @@ GROUP BY EVENT_NAME
 HAVING (e.SUM_TIMER_WAIT < SUM(i.SUM_TIMER_WAIT))
 OR @dump_all;
 EVENT_NAME	SUM_TIMER_WAIT	SUM(i.SUM_TIMER_WAIT)
-wait/io/socket/com/client_connection	0	16887000
 SELECT EVENT_NAME, e.MIN_TIMER_WAIT, MIN(i.MIN_TIMER_WAIT)
 FROM performance_schema.events_waits_summary_global_by_event_name AS e
 JOIN performance_schema.events_waits_summary_by_instance AS i USING (EVENT_NAME)
@@ -87,7 +86,6 @@ GROUP BY EVENT_NAME
 HAVING (e.MAX_TIMER_WAIT < MAX(i.MAX_TIMER_WAIT))
 OR @dump_all;
 EVENT_NAME	MAX_TIMER_WAIT	MAX(i.MAX_TIMER_WAIT)
-wait/io/socket/com/client_connection	0	10950000
 "Verifying waits aggregate consistency (thread)"
 SELECT EVENT_NAME, e.SUM_TIMER_WAIT, SUM(t.SUM_TIMER_WAIT)
 FROM performance_schema.events_waits_summary_global_by_event_name AS e
@@ -97,7 +95,6 @@ GROUP BY EVENT_NAME
 HAVING (e.SUM_TIMER_WAIT < SUM(t.SUM_TIMER_WAIT))
 OR @dump_all;
 EVENT_NAME	SUM_TIMER_WAIT	SUM(t.SUM_TIMER_WAIT)
-wait/io/socket/com/client_connection	0	16887000
 SELECT EVENT_NAME, e.MIN_TIMER_WAIT, MIN(t.MIN_TIMER_WAIT)
 FROM performance_schema.events_waits_summary_global_by_event_name AS e
 JOIN performance_schema.events_waits_summary_by_thread_by_event_name AS t
@@ -115,7 +112,6 @@ GROUP BY EVENT_NAME
 HAVING (e.MAX_TIMER_WAIT < MAX(t.MAX_TIMER_WAIT))
 OR @dump_all;
 EVENT_NAME	MAX_TIMER_WAIT	MAX(t.MAX_TIMER_WAIT)
-wait/io/socket/com/client_connection	0	10950000
 update performance_schema.setup_consumers set enabled = 'YES';
 update performance_schema.setup_instruments
 set enabled = 'YES', timed = 'YES';

=== modified file 'mysql-test/suite/rpl/r/rpl_corruption.result'
--- a/mysql-test/suite/rpl/r/rpl_corruption.result	2011-03-13 19:56:40 +0000
+++ b/mysql-test/suite/rpl/r/rpl_corruption.result	2011-03-17 18:13:10 +0000
@@ -12,32 +12,32 @@ SET @old_master_verify_checksum = @@mast
 CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY, b VARCHAR(10), c VARCHAR(100));
 include/stop_slave.inc
 # 2. Corruption in master binlog and SHOW BINLOG EVENTS
-SET GLOBAL debug="d,corrupt_read_log_event_char";
+SET GLOBAL debug="+d,corrupt_read_log_event_char";
 SHOW BINLOG EVENTS;
 ERROR HY000: Error when executing command SHOW BINLOG EVENTS: Wrong offset or I/O error
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event_char";
 # 3. Master read a corrupted event from binlog and send the error to slave
-SET GLOBAL debug="d,corrupt_read_log_event";
+SET GLOBAL debug="+d,corrupt_read_log_event";
 START SLAVE IO_THREAD;
 include/wait_for_slave_io_error.inc [errno=1236]
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event";
 # 4. Master read a corrupted event from binlog and send it to slave
 SET GLOBAL master_verify_checksum=0;
-SET GLOBAL debug="d,corrupt_read_log_event";
+SET GLOBAL debug="+d,corrupt_read_log_event";
 START SLAVE IO_THREAD;
 include/wait_for_slave_io_error.inc [errno=1595,1722]
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event";
 SET GLOBAL master_verify_checksum=1;
 # 5. Slave. Corruption in network
-SET GLOBAL debug="d,corrupt_queue_event";
+SET GLOBAL debug="+d,corrupt_queue_event";
 START SLAVE IO_THREAD;
 include/wait_for_slave_io_error.inc [errno=1595,1722]
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_queue_event";
 # 6. Slave. Corruption in relay log
-SET GLOBAL debug="d,corrupt_read_log_event_char";
-START SLAVE;
+SET GLOBAL debug="+d,corrupt_read_log_event_char";
+START SLAVE SQL_THREAD;
 include/wait_for_slave_sql_error.inc [errno=1593]
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event_char";
 # 7. Seek diff for tables on master and slave
 include/start_slave.inc
 include/diff_tables.inc [master:t1, slave:t1]

=== modified file 'mysql-test/suite/rpl/r/rpl_deadlock_innodb.result'
--- a/mysql-test/suite/rpl/r/rpl_deadlock_innodb.result	2010-12-19 17:22:30 +0000
+++ b/mysql-test/suite/rpl/r/rpl_deadlock_innodb.result	2011-03-27 18:16:32 +0000
@@ -103,6 +103,31 @@ a
 3
 include/check_slave_is_running.inc
 
+*** Test the deadlock warning to be escalated into the error ***
+delete from t1;
+delete from t2;
+delete from t3;
+delete from t1;
+delete from t2;
+delete from t3;
+set @save.slave_transaction_retries= @@global.slave_transaction_retries;
+set @@global.slave_transaction_retries= 0;
+include/stop_slave.inc
+BEGIN;
+INSERT INTO t1 VALUES (1);
+INSERT INTO t2 VALUES (2), (2), (2), (2), (2), (2), (2), (2), (2), (2);
+INSERT INTO t3 VALUES (3);
+COMMIT;
+BEGIN;
+SELECT count(*) as zero FROM t1 FOR UPDATE;
+zero
+0
+start slave;
+*** Now the slave must be stopped due to timeout ***
+include/wait_for_slave_sql_error.inc [errno=1205]
+rollback;
+set @@global.slave_transaction_retries= @save.slave_transaction_retries;
+include/start_slave.inc
 *** Clean up ***
 DROP TABLE t1,t2,t3;
 SET global max_relay_log_size= @my_max_relay_log_size;

=== modified file 'mysql-test/suite/rpl/r/rpl_init_slave_errors.result'
--- a/mysql-test/suite/rpl/r/rpl_init_slave_errors.result	2011-02-23 09:31:37 +0000
+++ b/mysql-test/suite/rpl/r/rpl_init_slave_errors.result	2011-03-21 16:04:11 +0000
@@ -7,6 +7,7 @@ start slave;
 include/wait_for_slave_sql_error.inc [errno=1593]
 Last_SQL_Error = 'Failed during slave thread initialization'
 call mtr.add_suppression("Failed during slave.* thread initialization");
+# Asserted this: SHOW SLAVE STATUS should return No IO thread running status
 SET GLOBAL debug= "";
 reset slave;
 SET GLOBAL init_slave= "garbage";

=== modified file 'mysql-test/suite/rpl/r/rpl_stm_stop_middle_group.result'
--- a/mysql-test/suite/rpl/r/rpl_stm_stop_middle_group.result	2011-03-17 13:20:36 +0000
+++ b/mysql-test/suite/rpl/r/rpl_stm_stop_middle_group.result	2011-03-29 13:44:23 +0000
@@ -18,6 +18,7 @@ include/check_slave_no_error.inc
 include/assert.inc [Everything that was read, was executed]
 include/assert.inc [There is one row in table tm]
 include/assert.inc [There is one row in table ti]
+include/stop_slave.inc
 set @@global.debug="-d";
 include/start_slave.inc
 truncate table tm;
@@ -35,8 +36,8 @@ Last_SQL_Error = 'Fatal error: ... Slave
 include/assert.inc [Not everything that was read, was executed]
 include/assert.inc [There is one row in table tm]
 include/assert.inc [There is no row in table ti]
+include/stop_slave.inc
 set @@global.debug="-d";
-stop slave;
 truncate table tm;
 include/start_slave.inc
 set @@global.debug="+d,stop_slave_middle_group";
@@ -47,6 +48,7 @@ Last_SQL_Error = 'Fatal error: ... Slave
 include/assert.inc [Not everything that was read, was executed]
 include/assert.inc [The max value for field 'a' is 2]
 include/assert.inc [The max value for field 'a' is 1]
+include/stop_slave.inc
 set @@global.debug="-d";
 include/rpl_reset.inc
 drop table tm, ti;

=== modified file 'mysql-test/suite/rpl/t/rpl_corruption.test'
--- a/mysql-test/suite/rpl/t/rpl_corruption.test	2011-03-13 19:56:40 +0000
+++ b/mysql-test/suite/rpl/t/rpl_corruption.test	2011-03-17 18:13:10 +0000
@@ -7,6 +7,14 @@
 #  - in relay log
 ############################################################
 
+#
+# The tests intensively utilize @@global.debug. Note,
+# Bug#11765758 - 58754,
+# @@global.debug is read by the slave threads through dbug-interface. 
+# Hence, before a client thread set @@global.debug we have to ensure that:
+# (a) the slave threads are stopped, or (b) the slave threads are in
+# sync and waiting.
+
 --source include/have_debug.inc
 --source include/master-slave.inc
 
@@ -56,17 +64,10 @@ while ($i) {
 }
 --enable_query_log
 
-#
-# Bug #58630 shows `+d' syntax has a side effect Andrei changed it to
-# unsigned `d' version which does not affect the tests logics.
-# todo:
-# restore *all* following `SET GLOBAL debug' back to `+|-d' when Bug
-# #58630, OBug#11765758 sorted out
-#
 
 # Emulate corruption in binlog file when SHOW BINLOG EVENTS is executing
 --echo # 2. Corruption in master binlog and SHOW BINLOG EVENTS
-SET GLOBAL debug="d,corrupt_read_log_event_char";
+SET GLOBAL debug="+d,corrupt_read_log_event_char";
 --echo SHOW BINLOG EVENTS;
 --disable_query_log
 send_eval SHOW BINLOG EVENTS FROM $pos;
@@ -74,50 +75,50 @@ send_eval SHOW BINLOG EVENTS FROM $pos;
 --error ER_ERROR_WHEN_EXECUTING_COMMAND
 reap;
 
-# see above comments on `+d' syntax
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event_char";
 
 # Emulate corruption on master with crc checking on master
 --echo # 3. Master read a corrupted event from binlog and send the error to slave
-SET GLOBAL debug="d,corrupt_read_log_event";
+SET GLOBAL debug="+d,corrupt_read_log_event";
 --connection slave
 START SLAVE IO_THREAD;
 let $slave_io_errno= 1236;
 --source include/wait_for_slave_io_error.inc
 --connection master
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event";
 
 # Emulate corruption on master without crc checking on master
 --echo # 4. Master read a corrupted event from binlog and send it to slave
 --connection master
 SET GLOBAL master_verify_checksum=0;
-SET GLOBAL debug="d,corrupt_read_log_event";
+SET GLOBAL debug="+d,corrupt_read_log_event";
 --connection slave
 START SLAVE IO_THREAD;
 let $slave_io_errno= 1595,1722;
 --source include/wait_for_slave_io_error.inc
 --connection master
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event";
 SET GLOBAL master_verify_checksum=1;
 
 # Emulate corruption in network
 --echo # 5. Slave. Corruption in network
 --connection slave
-SET GLOBAL debug="d,corrupt_queue_event";
+SET GLOBAL debug="+d,corrupt_queue_event";
 START SLAVE IO_THREAD;
 let $slave_io_errno= 1595,1722;
 --source include/wait_for_slave_io_error.inc
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_queue_event";
 
 # Emulate corruption in relay log
 --echo # 6. Slave. Corruption in relay log
 
-SET GLOBAL debug="d,corrupt_read_log_event_char";
-START SLAVE;
+SET GLOBAL debug="+d,corrupt_read_log_event_char";
+
+START SLAVE SQL_THREAD;
 let $slave_sql_errno= 1593;
 --source include/wait_for_slave_sql_error.inc
 
-SET GLOBAL debug="";
+SET GLOBAL debug="-d,corrupt_read_log_event_char";
 
 # Start normal replication and compare same table on master
 # and slave

=== modified file 'mysql-test/suite/rpl/t/rpl_get_master_version_and_clock.test'
--- a/mysql-test/suite/rpl/t/rpl_get_master_version_and_clock.test	2010-12-19 17:22:30 +0000
+++ b/mysql-test/suite/rpl/t/rpl_get_master_version_and_clock.test	2011-03-17 18:13:10 +0000
@@ -47,6 +47,11 @@ let $dbug_sync_point= 'dbug.before_get_M
 let $debug_sync_action= 'now SIGNAL signal.get_master_uuid';
 source extra/rpl_tests/rpl_get_master_version_and_clock.test;
 
+#
+# Note, due to # Bug#11765758 - 58754
+# make sure the slave threads stand still (SQL thread in this context)
+# while @@global.debug is being updated.
+#
 eval set global debug= '$debug_saved';
 
 #Test case 4: This test checks that the slave I/O thread refuses to start

=== modified file 'mysql-test/suite/rpl/t/rpl_init_slave_errors.test'
--- a/mysql-test/suite/rpl/t/rpl_init_slave_errors.test	2011-02-23 09:31:37 +0000
+++ b/mysql-test/suite/rpl/t/rpl_init_slave_errors.test	2011-03-21 16:04:11 +0000
@@ -60,6 +60,14 @@ start slave;
 
 call mtr.add_suppression("Failed during slave.* thread initialization");
 
+# Bug#11765758 - 58754
+# above tests part logics make sure the IO thread is down to allow 
+# safe updating to @@global.debug
+
+--let $assert_text= SHOW SLAVE STATUS should return No IO thread running status
+--let $assert_cond= "[SHOW SLAVE STATUS, Slave_IO_Running, 1]" like "No"
+--source include/rpl_assert.inc
+
 SET GLOBAL debug= "";
 
 ######################################################################

=== modified file 'mysql-test/suite/rpl/t/rpl_show_slave_running.test'
--- a/mysql-test/suite/rpl/t/rpl_show_slave_running.test	2010-12-19 17:22:30 +0000
+++ b/mysql-test/suite/rpl/t/rpl_show_slave_running.test	2011-03-17 18:13:10 +0000
@@ -76,9 +76,17 @@ echo Slave_SQL_Running= $status;
 
 # cleanup
 
-connection slave;
+connection master;
+
+# Bug#11765758 - 58754
+# @@global.debug is read by the slave threads through dbug-interface. 
+# Hence, before a client thread set @@global.debug we have to ensure that:
+# (a) the slave threads are stopped, or (b) the slave threads are in
+# sync and waiting.
+sync_slave_with_master;
 
 eval set global debug= '$debug_saved';
+
 SET DEBUG_SYNC= 'RESET';
 --echo End of tests
 --source include/rpl_end.inc

=== modified file 'mysql-test/t/func_time.test'
--- a/mysql-test/t/func_time.test	2011-03-28 13:38:03 +0000
+++ b/mysql-test/t/func_time.test	2011-03-30 07:42:03 +0000
@@ -882,6 +882,18 @@ SET GLOBAL SQL_MODE=DEFAULT;
 
 SELECT FORMAT(YEAR(STR_TO_DATE('',GET_FORMAT(TIME,''))),1);
 
+--echo #
+--echo # Bug#11766126  59166: ANOTHER DATETIME VALGRIND UNINITIALIZED WARNING
+--echo #
+
+SELECT CAST((MONTH(FROM_UNIXTIME(@@GLOBAL.SQL_MODE))) AS BINARY(1025));
+
+--echo #
+--echo # Bug#11766124  59164: VALGRIND: UNINITIALIZED VALUE IN NUMBER_TO_DATETIME
+--echo #
+
+SELECT ADDDATE(MONTH(FROM_UNIXTIME(NULL)),INTERVAL 1 HOUR);
+
 --echo End of 5.1 tests
 
 --echo #

=== modified file 'mysql-test/t/ssl.test'
--- a/mysql-test/t/ssl.test	2010-04-13 15:04:45 +0000
+++ b/mysql-test/t/ssl.test	2011-03-29 08:01:07 +0000
@@ -11,6 +11,10 @@ connect (ssl_con,localhost,root,,,,,SSL)
 # Check ssl turned on
 SHOW STATUS LIKE 'Ssl_cipher';
 
+# Check ssl expiration
+SHOW STATUS LIKE 'Ssl_server_not_before';
+SHOW STATUS LIKE 'Ssl_server_not_after';
+
 # Source select test case
 -- source include/common-tests.inc
 

=== modified file 'mysys/CMakeLists.txt'
--- a/mysys/CMakeLists.txt	2011-03-28 08:51:35 +0000
+++ b/mysys/CMakeLists.txt	2011-03-29 12:56:34 +0000
@@ -23,8 +23,8 @@ SET(MYSYS_SOURCES  array.c charset-def.c
 				mf_tempfile.c mf_unixpath.c mf_wcomp.c mulalloc.c my_access.c
 				my_aes.c my_alloc.c my_bit.c my_bitmap.c my_chsize.c
 				my_compress.c my_copy.c  my_create.c my_delete.c
-				my_div.c my_error.c my_file.c my_fopen.c my_fstream.c my_gethostbyname.c 
-				my_gethwaddr.c my_getopt.c my_getsystime.c my_getwd.c my_handler.c my_init.c
+				my_div.c my_error.c my_file.c my_fopen.c my_fstream.c 
+				my_gethwaddr.c my_getopt.c my_getsystime.c my_getwd.c my_compare.c my_init.c
 				my_lib.c my_lock.c my_malloc.c my_mess.c
 				my_mkdir.c my_mmap.c my_once.c my_open.c my_pread.c my_pthread.c
 				my_quick.c my_read.c my_redel.c my_rename.c my_seek.c my_sleep.c
@@ -64,11 +64,6 @@ IF(HAVE_MLOCK)
  SET(MYSYS_SOURCES ${MYSYS_SOURCES} my_lockmem.c)
 ENDIF()
 
-IF(UNIX)
- # some workarounds
- SET(MYSYS_SOURCES ${MYSYS_SOURCES} my_port.c)
-ENDIF()
-
 ADD_CONVENIENCE_LIBRARY(mysys ${MYSYS_SOURCES})
 TARGET_LINK_LIBRARIES(mysys dbug strings ${ZLIB_LIBRARY} 
  ${LIBNSL} ${LIBM} ${LIBRT})

=== added file 'mysys/my_compare.c'
--- a/mysys/my_compare.c	1970-01-01 00:00:00 +0000
+++ b/mysys/my_compare.c	2011-03-29 12:56:34 +0000
@@ -0,0 +1,476 @@
+/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+
+   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; version 2 of the License.
+
+   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 */
+   
+#include <my_global.h>
+#include <m_ctype.h>
+#include <my_base.h>
+#include <my_compare.h>
+#include <my_sys.h>
+
+#define CMP_NUM(a,b)    (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
+
+int ha_compare_text(const CHARSET_INFO *charset_info, uchar *a, uint a_length,
+		    uchar *b, uint b_length, my_bool part_key,
+		    my_bool skip_end_space)
+{
+  if (!part_key)
+    return charset_info->coll->strnncollsp(charset_info, a, a_length,
+                                           b, b_length, (my_bool)!skip_end_space);
+  return charset_info->coll->strnncoll(charset_info, a, a_length,
+                                       b, b_length, part_key);
+}
+
+
+static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
+                       my_bool part_key, my_bool skip_end_space)
+{
+  uint length= min(a_length,b_length);
+  uchar *end= a+ length;
+  int flag;
+
+  while (a < end)
+    if ((flag= (int) *a++ - (int) *b++))
+      return flag;
+  if (part_key && b_length < a_length)
+    return 0;
+  if (skip_end_space && a_length != b_length)
+  {
+    int swap= 1;
+    /*
+      We are using space compression. We have to check if longer key
+      has next character < ' ', in which case it's less than the shorter
+      key that has an implicite space afterwards.
+
+      This code is identical to the one in
+      strings/ctype-simple.c:my_strnncollsp_simple
+    */
+    if (a_length < b_length)
+    {
+      /* put shorter key in a */
+      a_length= b_length;
+      a= b;
+      swap= -1;					/* swap sign of result */
+    }
+    for (end= a + a_length-length; a < end ; a++)
+    {
+      if (*a != ' ')
+	return (*a < ' ') ? -swap : swap;
+    }
+    return 0;
+  }
+  return (int) (a_length-b_length);
+}
+
+
+/*
+  Compare two keys
+
+  SYNOPSIS
+    ha_key_cmp()
+    keyseg	Array of key segments of key to compare
+    a		First key to compare, in format from _mi_pack_key()
+		This is normally key specified by user
+    b		Second key to compare.  This is always from a row
+    key_length	Length of key to compare.  This can be shorter than
+		a to just compare sub keys
+    next_flag	How keys should be compared
+		If bit SEARCH_FIND is not set the keys includes the row
+		position and this should also be compared
+    diff_pos    OUT Number of first keypart where values differ, counting 
+                from one.
+    diff_pos[1] OUT  (b + diff_pos[1]) points to first value in tuple b
+                      that is different from corresponding value in tuple a.
+  
+  EXAMPLES 
+   Example1: if the function is called for tuples
+     ('aaa','bbb') and ('eee','fff'), then
+     diff_pos[0] = 1 (as 'aaa' != 'eee')
+     diff_pos[1] = 0 (offset from beggining of tuple b to 'eee' keypart).
+
+   Example2: if the index function is called for tuples
+     ('aaa','bbb') and ('aaa','fff'),
+     diff_pos[0] = 2 (as 'aaa' != 'eee')
+     diff_pos[1] = 3 (offset from beggining of tuple b to 'fff' keypart,
+                      here we assume that first key part is CHAR(3) NOT NULL)
+
+  NOTES
+    Number-keys can't be splited
+
+  RETURN VALUES
+    <0	If a < b
+    0	If a == b
+    >0	If a > b
+*/
+
+#define FCMP(A,B) ((int) (A) - (int) (B))
+
+int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
+	       register uchar *b, uint key_length, uint nextflag,
+	       uint *diff_pos)
+{
+  int flag;
+  int16 s_1,s_2;
+  int32 l_1,l_2;
+  uint32 u_1,u_2;
+  float f_1,f_2;
+  double d_1,d_2;
+  uint next_key_length;
+  uchar *orig_b= b;
+
+  *diff_pos=0;
+  for ( ; (int) key_length >0 ; key_length=next_key_length, keyseg++)
+  {
+    uchar *end;
+    uint piks=! (keyseg->flag & HA_NO_SORT);
+    (*diff_pos)++;
+    diff_pos[1]= (uint)(b - orig_b);
+
+    /* Handle NULL part */
+    if (keyseg->null_bit)
+    {
+      key_length--;
+      if (*a != *b && piks)
+      {
+        flag = (int) *a - (int) *b;
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      }
+      b++;
+      if (!*a++)                                /* If key was NULL */
+      {
+        if (nextflag == (SEARCH_FIND | SEARCH_UPDATE))
+          nextflag=SEARCH_SAME;                 /* Allow duplicate keys */
+  	else if (nextflag & SEARCH_NULL_ARE_NOT_EQUAL)
+	{
+	  /*
+	    This is only used from mi_check() to calculate cardinality.
+	    It can't be used when searching for a key as this would cause
+	    compare of (a,b) and (b,a) to return the same value.
+	  */
+	  return -1;
+	}
+        next_key_length=key_length;
+        continue;                               /* To next key part */
+      }
+    }
+    end= a+ min(keyseg->length,key_length);
+    next_key_length=key_length-keyseg->length;
+
+    switch ((enum ha_base_keytype) keyseg->type) {
+    case HA_KEYTYPE_TEXT:                       /* Ascii; Key is converted */
+      if (keyseg->flag & HA_SPACE_PACK)
+      {
+        int a_length,b_length,pack_length;
+        get_key_length(a_length,a);
+        get_key_pack_length(b_length,pack_length,b);
+        next_key_length=key_length-b_length-pack_length;
+
+        if (piks &&
+            (flag=ha_compare_text(keyseg->charset,a,a_length,b,b_length,
+				  (my_bool) ((nextflag & SEARCH_PREFIX) &&
+					     next_key_length <= 0),
+				  (my_bool)!(nextflag & SEARCH_PREFIX))))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a+=a_length;
+        b+=b_length;
+        break;
+      }
+      else
+      {
+	uint length=(uint) (end-a), a_length=length, b_length=length;
+        if (piks &&
+            (flag= ha_compare_text(keyseg->charset, a, a_length, b, b_length,
+				   (my_bool) ((nextflag & SEARCH_PREFIX) &&
+					      next_key_length <= 0),
+				   (my_bool)!(nextflag & SEARCH_PREFIX))))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a=end;
+        b+=length;
+      }
+      break;
+    case HA_KEYTYPE_BINARY:
+    case HA_KEYTYPE_BIT:
+      if (keyseg->flag & HA_SPACE_PACK)
+      {
+        int a_length,b_length,pack_length;
+        get_key_length(a_length,a);
+        get_key_pack_length(b_length,pack_length,b);
+        next_key_length=key_length-b_length-pack_length;
+
+        if (piks &&
+	    (flag=compare_bin(a,a_length,b,b_length,
+                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
+                                         next_key_length <= 0),1)))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a+=a_length;
+        b+=b_length;
+        break;
+      }
+      else
+      {
+        uint length=keyseg->length;
+        if (piks &&
+	    (flag=compare_bin(a,length,b,length,
+                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
+                                         next_key_length <= 0),0)))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a+=length;
+        b+=length;
+      }
+      break;
+    case HA_KEYTYPE_VARTEXT1:
+    case HA_KEYTYPE_VARTEXT2:
+      {
+        int a_length,b_length,pack_length;
+        get_key_length(a_length,a);
+        get_key_pack_length(b_length,pack_length,b);
+        next_key_length=key_length-b_length-pack_length;
+
+        if (piks &&
+	    (flag= ha_compare_text(keyseg->charset,a,a_length,b,b_length,
+                                   (my_bool) ((nextflag & SEARCH_PREFIX) &&
+                                              next_key_length <= 0),
+				   (my_bool) ((nextflag & (SEARCH_FIND |
+							   SEARCH_UPDATE)) ==
+					      SEARCH_FIND &&
+                                              ! (keyseg->flag &
+                                                 HA_END_SPACE_ARE_EQUAL)))))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a+= a_length;
+        b+= b_length;
+        break;
+      }
+      break;
+    case HA_KEYTYPE_VARBINARY1:
+    case HA_KEYTYPE_VARBINARY2:
+      {
+        int a_length,b_length,pack_length;
+        get_key_length(a_length,a);
+        get_key_pack_length(b_length,pack_length,b);
+        next_key_length=key_length-b_length-pack_length;
+
+        if (piks &&
+	    (flag=compare_bin(a,a_length,b,b_length,
+                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
+                                         next_key_length <= 0), 0)))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a+=a_length;
+        b+=b_length;
+      }
+      break;
+    case HA_KEYTYPE_INT8:
+    {
+      int i_1= (int) *((signed char*) a);
+      int i_2= (int) *((signed char*) b);
+      if (piks && (flag = CMP_NUM(i_1,i_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a= end;
+      b++;
+      break;
+    }
+    case HA_KEYTYPE_SHORT_INT:
+      s_1= mi_sint2korr(a);
+      s_2= mi_sint2korr(b);
+      if (piks && (flag = CMP_NUM(s_1,s_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 2; /* sizeof(short int); */
+      break;
+    case HA_KEYTYPE_USHORT_INT:
+      {
+        uint16 us_1,us_2;
+        us_1= mi_sint2korr(a);
+        us_2= mi_sint2korr(b);
+        if (piks && (flag = CMP_NUM(us_1,us_2)))
+          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+        a=  end;
+        b+=2; /* sizeof(short int); */
+        break;
+      }
+    case HA_KEYTYPE_LONG_INT:
+      l_1= mi_sint4korr(a);
+      l_2= mi_sint4korr(b);
+      if (piks && (flag = CMP_NUM(l_1,l_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 4; /* sizeof(long int); */
+      break;
+    case HA_KEYTYPE_ULONG_INT:
+      u_1= mi_sint4korr(a);
+      u_2= mi_sint4korr(b);
+      if (piks && (flag = CMP_NUM(u_1,u_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 4; /* sizeof(long int); */
+      break;
+    case HA_KEYTYPE_INT24:
+      l_1=mi_sint3korr(a);
+      l_2=mi_sint3korr(b);
+      if (piks && (flag = CMP_NUM(l_1,l_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 3;
+      break;
+    case HA_KEYTYPE_UINT24:
+      l_1=mi_uint3korr(a);
+      l_2=mi_uint3korr(b);
+      if (piks && (flag = CMP_NUM(l_1,l_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 3;
+      break;
+    case HA_KEYTYPE_FLOAT:
+      mi_float4get(f_1,a);
+      mi_float4get(f_2,b);
+      /*
+        The following may give a compiler warning about floating point
+        comparison not being safe, but this is ok in this context as
+        we are bascily doing sorting
+      */
+      if (piks && (flag = CMP_NUM(f_1,f_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 4; /* sizeof(float); */
+      break;
+    case HA_KEYTYPE_DOUBLE:
+      mi_float8get(d_1,a);
+      mi_float8get(d_2,b);
+      /*
+        The following may give a compiler warning about floating point
+        comparison not being safe, but this is ok in this context as
+        we are bascily doing sorting
+      */
+      if (piks && (flag = CMP_NUM(d_1,d_2)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 8;  /* sizeof(double); */
+      break;
+    case HA_KEYTYPE_NUM:                                /* Numeric key */
+    {
+      int swap_flag= 0;
+      int alength,blength;
+
+      if (keyseg->flag & HA_REVERSE_SORT)
+      {
+        swap_variables(uchar*, a, b);
+        swap_flag=1;                            /* Remember swap of a & b */
+        end= a+ (int) (end-b);
+      }
+      if (keyseg->flag & HA_SPACE_PACK)
+      {
+        alength= *a++; blength= *b++;
+        end=a+alength;
+        next_key_length=key_length-blength-1;
+      }
+      else
+      {
+        alength= (int) (end-a);
+        blength=keyseg->length;
+        /* remove pre space from keys */
+        for ( ; alength && *a == ' ' ; a++, alength--) ;
+        for ( ; blength && *b == ' ' ; b++, blength--) ;
+      }
+      if (piks)
+      {
+	if (*a == '-')
+	{
+	  if (*b != '-')
+	    return -1;
+	  a++; b++;
+	  swap_variables(uchar*, a, b);
+	  swap_variables(int, alength, blength);
+	  swap_flag=1-swap_flag;
+	  alength--; blength--;
+	  end=a+alength;
+	}
+	else if (*b == '-')
+	  return 1;
+	while (alength && (*a == '+' || *a == '0'))
+	{
+	  a++; alength--;
+	}
+	while (blength && (*b == '+' || *b == '0'))
+	{
+	  b++; blength--;
+	}
+	if (alength != blength)
+	  return (alength < blength) ? -1 : 1;
+	while (a < end)
+	  if (*a++ !=  *b++)
+	    return ((int) a[-1] - (int) b[-1]);
+      }
+      else
+      {
+        b+=(end-a);
+        a=end;
+      }
+
+      if (swap_flag)                            /* Restore pointers */
+        swap_variables(uchar*, a, b);
+      break;
+    }
+#ifdef HAVE_LONG_LONG
+    case HA_KEYTYPE_LONGLONG:
+    {
+      longlong ll_a,ll_b;
+      ll_a= mi_sint8korr(a);
+      ll_b= mi_sint8korr(b);
+      if (piks && (flag = CMP_NUM(ll_a,ll_b)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 8;
+      break;
+    }
+    case HA_KEYTYPE_ULONGLONG:
+    {
+      ulonglong ll_a,ll_b;
+      ll_a= mi_uint8korr(a);
+      ll_b= mi_uint8korr(b);
+      if (piks && (flag = CMP_NUM(ll_a,ll_b)))
+        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
+      a=  end;
+      b+= 8;
+      break;
+    }
+#endif
+    case HA_KEYTYPE_END:                        /* Ready */
+      goto end;                                 /* diff_pos is incremented */
+    }
+  }
+  (*diff_pos)++;
+end:
+  if (!(nextflag & SEARCH_FIND))
+  {
+    uint i;
+    if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST)) /* Find record after key */
+      return (nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
+    flag=0;
+    for (i=keyseg->length ; i-- > 0 ; )
+    {
+      if (*a++ != *b++)
+      {
+        flag= FCMP(a[-1],b[-1]);
+        break;
+      }
+    }
+    if (nextflag & SEARCH_SAME)
+      return (flag);                            /* read same */
+    if (nextflag & SEARCH_BIGGER)
+      return (flag <= 0 ? -1 : 1);              /* read next */
+    return (flag < 0 ? -1 : 1);                 /* read previous */
+  }
+  return 0;
+} /* ha_key_cmp */
+
+

=== removed file 'mysys/my_gethostbyname.c'
--- a/mysys/my_gethostbyname.c	2011-01-11 09:09:21 +0000
+++ b/mysys/my_gethostbyname.c	1970-01-01 00:00:00 +0000
@@ -1,111 +0,0 @@
-/* Copyright (C) 2002, 2004 MySQL AB, 2008-2009 Sun Microsystems, Inc
-   
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public
-   License as published by the Free Software Foundation; version 2
-   of the License.
-   
-   This library 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
-   Library General Public License for more details.
-   
-   You should have received a copy of the GNU Library General Public
-   License along with this library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA */
-
-/* Thread safe version of gethostbyname_r() */
-
-#include "mysys_priv.h"
-#if !defined(__WIN__)
-#include <netdb.h>
-#endif
-#include <my_net.h>
-
-/* This file is not needed if my_gethostbyname_r is a macro */
-#if !defined(my_gethostbyname_r)
-
-/*
-  Emulate SOLARIS style calls, not because it's better, but just to make the
-  usage of getbostbyname_r simpler.
-*/
-
-#if defined(HAVE_GETHOSTBYNAME_R)
-
-#if defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE)
-
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop)
-{
-  struct hostent *hp;
-  DBUG_ASSERT((size_t) buflen >= sizeof(*result));
-  if (gethostbyname_r(name,result, buffer, (size_t) buflen, &hp, h_errnop))
-    return 0;
-  return hp;
-}
-
-#elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT)
-
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop)
-{
-  if (gethostbyname_r(name,result,(struct hostent_data *) buffer) == -1)
-  {
-    *h_errnop= errno;
-    return 0;
-  }
-  return result;
-}
-
-#else
-
-/* gethostbyname_r with similar interface as gethostbyname() */
-
-struct hostent *my_gethostbyname_r(const char *name,
-				   struct hostent *result, char *buffer,
-				   int buflen, int *h_errnop)
-{
-  struct hostent *hp;
-  DBUG_ASSERT(buflen >= sizeof(struct hostent_data));
-  hp= gethostbyname_r(name,result,(struct hostent_data *) buffer);
-  *h_errnop= errno;
-  return hp;
-}
-#endif /* GLIBC2_STYLE_GETHOSTBYNAME_R */
-
-#else /* !HAVE_GETHOSTBYNAME_R */
-
-extern mysql_mutex_t LOCK_gethostbyname_r;
-
-/*
-  No gethostbyname_r() function exists.
-  In this case we have to keep a mutex over the call to ensure that no
-  other thread is going to reuse the internal memory.
-
-  The user is responsible to call my_gethostbyname_r_free() when he
-  is finished with the structure.
-*/
-
-struct hostent *my_gethostbyname_r(const char *name,
-                                   struct hostent *res __attribute__((unused)),
-                                   char *buffer __attribute__((unused)),
-                                   int buflen __attribute__((unused)),
-                                   int *h_errnop)
-{
-  struct hostent *hp;
-  mysql_mutex_lock(&LOCK_gethostbyname_r);
-  hp= gethostbyname(name);
-  *h_errnop= h_errno;
-  return hp;
-}
-
-void my_gethostbyname_r_free()
-{
-  mysql_mutex_unlock(&LOCK_gethostbyname_r);
-}
-
-#endif /* !HAVE_GETHOSTBYNAME_R */
-#endif /* !my_gethostbyname_r */

=== removed file 'mysys/my_handler.c'
--- a/mysys/my_handler.c	2011-03-09 20:54:55 +0000
+++ b/mysys/my_handler.c	1970-01-01 00:00:00 +0000
@@ -1,602 +0,0 @@
-/* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
-
-   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; version 2 of the License.
-
-   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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
-
-#include <my_global.h>
-#include <m_ctype.h>
-#include <my_base.h>
-#include <my_handler.h>
-#include <my_sys.h>
-
-#include "my_handler_errors.h"
-
-#define CMP_NUM(a,b)    (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
-
-int ha_compare_text(const CHARSET_INFO *charset_info, uchar *a, uint a_length,
-		    uchar *b, uint b_length, my_bool part_key,
-		    my_bool skip_end_space)
-{
-  if (!part_key)
-    return charset_info->coll->strnncollsp(charset_info, a, a_length,
-                                           b, b_length, (my_bool)!skip_end_space);
-  return charset_info->coll->strnncoll(charset_info, a, a_length,
-                                       b, b_length, part_key);
-}
-
-
-static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
-                       my_bool part_key, my_bool skip_end_space)
-{
-  uint length= min(a_length,b_length);
-  uchar *end= a+ length;
-  int flag;
-
-  while (a < end)
-    if ((flag= (int) *a++ - (int) *b++))
-      return flag;
-  if (part_key && b_length < a_length)
-    return 0;
-  if (skip_end_space && a_length != b_length)
-  {
-    int swap= 1;
-    /*
-      We are using space compression. We have to check if longer key
-      has next character < ' ', in which case it's less than the shorter
-      key that has an implicite space afterwards.
-
-      This code is identical to the one in
-      strings/ctype-simple.c:my_strnncollsp_simple
-    */
-    if (a_length < b_length)
-    {
-      /* put shorter key in a */
-      a_length= b_length;
-      a= b;
-      swap= -1;					/* swap sign of result */
-    }
-    for (end= a + a_length-length; a < end ; a++)
-    {
-      if (*a != ' ')
-	return (*a < ' ') ? -swap : swap;
-    }
-    return 0;
-  }
-  return (int) (a_length-b_length);
-}
-
-
-/*
-  Compare two keys
-
-  SYNOPSIS
-    ha_key_cmp()
-    keyseg	Array of key segments of key to compare
-    a		First key to compare, in format from _mi_pack_key()
-		This is normally key specified by user
-    b		Second key to compare.  This is always from a row
-    key_length	Length of key to compare.  This can be shorter than
-		a to just compare sub keys
-    next_flag	How keys should be compared
-		If bit SEARCH_FIND is not set the keys includes the row
-		position and this should also be compared
-    diff_pos    OUT Number of first keypart where values differ, counting 
-                from one.
-    diff_pos[1] OUT  (b + diff_pos[1]) points to first value in tuple b
-                      that is different from corresponding value in tuple a.
-  
-  EXAMPLES 
-   Example1: if the function is called for tuples
-     ('aaa','bbb') and ('eee','fff'), then
-     diff_pos[0] = 1 (as 'aaa' != 'eee')
-     diff_pos[1] = 0 (offset from beggining of tuple b to 'eee' keypart).
-
-   Example2: if the index function is called for tuples
-     ('aaa','bbb') and ('aaa','fff'),
-     diff_pos[0] = 2 (as 'aaa' != 'eee')
-     diff_pos[1] = 3 (offset from beggining of tuple b to 'fff' keypart,
-                      here we assume that first key part is CHAR(3) NOT NULL)
-
-  NOTES
-    Number-keys can't be splited
-
-  RETURN VALUES
-    <0	If a < b
-    0	If a == b
-    >0	If a > b
-*/
-
-#define FCMP(A,B) ((int) (A) - (int) (B))
-
-int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
-	       register uchar *b, uint key_length, uint nextflag,
-	       uint *diff_pos)
-{
-  int flag;
-  int16 s_1,s_2;
-  int32 l_1,l_2;
-  uint32 u_1,u_2;
-  float f_1,f_2;
-  double d_1,d_2;
-  uint next_key_length;
-  uchar *orig_b= b;
-
-  *diff_pos=0;
-  for ( ; (int) key_length >0 ; key_length=next_key_length, keyseg++)
-  {
-    uchar *end;
-    uint piks=! (keyseg->flag & HA_NO_SORT);
-    (*diff_pos)++;
-    diff_pos[1]= (uint)(b - orig_b);
-
-    /* Handle NULL part */
-    if (keyseg->null_bit)
-    {
-      key_length--;
-      if (*a != *b && piks)
-      {
-        flag = (int) *a - (int) *b;
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      }
-      b++;
-      if (!*a++)                                /* If key was NULL */
-      {
-        if (nextflag == (SEARCH_FIND | SEARCH_UPDATE))
-          nextflag=SEARCH_SAME;                 /* Allow duplicate keys */
-  	else if (nextflag & SEARCH_NULL_ARE_NOT_EQUAL)
-	{
-	  /*
-	    This is only used from mi_check() to calculate cardinality.
-	    It can't be used when searching for a key as this would cause
-	    compare of (a,b) and (b,a) to return the same value.
-	  */
-	  return -1;
-	}
-        next_key_length=key_length;
-        continue;                               /* To next key part */
-      }
-    }
-    end= a+ min(keyseg->length,key_length);
-    next_key_length=key_length-keyseg->length;
-
-    switch ((enum ha_base_keytype) keyseg->type) {
-    case HA_KEYTYPE_TEXT:                       /* Ascii; Key is converted */
-      if (keyseg->flag & HA_SPACE_PACK)
-      {
-        int a_length,b_length,pack_length;
-        get_key_length(a_length,a);
-        get_key_pack_length(b_length,pack_length,b);
-        next_key_length=key_length-b_length-pack_length;
-
-        if (piks &&
-            (flag=ha_compare_text(keyseg->charset,a,a_length,b,b_length,
-				  (my_bool) ((nextflag & SEARCH_PREFIX) &&
-					     next_key_length <= 0),
-				  (my_bool)!(nextflag & SEARCH_PREFIX))))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a+=a_length;
-        b+=b_length;
-        break;
-      }
-      else
-      {
-	uint length=(uint) (end-a), a_length=length, b_length=length;
-        if (piks &&
-            (flag= ha_compare_text(keyseg->charset, a, a_length, b, b_length,
-				   (my_bool) ((nextflag & SEARCH_PREFIX) &&
-					      next_key_length <= 0),
-				   (my_bool)!(nextflag & SEARCH_PREFIX))))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a=end;
-        b+=length;
-      }
-      break;
-    case HA_KEYTYPE_BINARY:
-    case HA_KEYTYPE_BIT:
-      if (keyseg->flag & HA_SPACE_PACK)
-      {
-        int a_length,b_length,pack_length;
-        get_key_length(a_length,a);
-        get_key_pack_length(b_length,pack_length,b);
-        next_key_length=key_length-b_length-pack_length;
-
-        if (piks &&
-	    (flag=compare_bin(a,a_length,b,b_length,
-                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
-                                         next_key_length <= 0),1)))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a+=a_length;
-        b+=b_length;
-        break;
-      }
-      else
-      {
-        uint length=keyseg->length;
-        if (piks &&
-	    (flag=compare_bin(a,length,b,length,
-                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
-                                         next_key_length <= 0),0)))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a+=length;
-        b+=length;
-      }
-      break;
-    case HA_KEYTYPE_VARTEXT1:
-    case HA_KEYTYPE_VARTEXT2:
-      {
-        int a_length,b_length,pack_length;
-        get_key_length(a_length,a);
-        get_key_pack_length(b_length,pack_length,b);
-        next_key_length=key_length-b_length-pack_length;
-
-        if (piks &&
-	    (flag= ha_compare_text(keyseg->charset,a,a_length,b,b_length,
-                                   (my_bool) ((nextflag & SEARCH_PREFIX) &&
-                                              next_key_length <= 0),
-				   (my_bool) ((nextflag & (SEARCH_FIND |
-							   SEARCH_UPDATE)) ==
-					      SEARCH_FIND &&
-                                              ! (keyseg->flag &
-                                                 HA_END_SPACE_ARE_EQUAL)))))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a+= a_length;
-        b+= b_length;
-        break;
-      }
-      break;
-    case HA_KEYTYPE_VARBINARY1:
-    case HA_KEYTYPE_VARBINARY2:
-      {
-        int a_length,b_length,pack_length;
-        get_key_length(a_length,a);
-        get_key_pack_length(b_length,pack_length,b);
-        next_key_length=key_length-b_length-pack_length;
-
-        if (piks &&
-	    (flag=compare_bin(a,a_length,b,b_length,
-                              (my_bool) ((nextflag & SEARCH_PREFIX) &&
-                                         next_key_length <= 0), 0)))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a+=a_length;
-        b+=b_length;
-      }
-      break;
-    case HA_KEYTYPE_INT8:
-    {
-      int i_1= (int) *((signed char*) a);
-      int i_2= (int) *((signed char*) b);
-      if (piks && (flag = CMP_NUM(i_1,i_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a= end;
-      b++;
-      break;
-    }
-    case HA_KEYTYPE_SHORT_INT:
-      s_1= mi_sint2korr(a);
-      s_2= mi_sint2korr(b);
-      if (piks && (flag = CMP_NUM(s_1,s_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 2; /* sizeof(short int); */
-      break;
-    case HA_KEYTYPE_USHORT_INT:
-      {
-        uint16 us_1,us_2;
-        us_1= mi_sint2korr(a);
-        us_2= mi_sint2korr(b);
-        if (piks && (flag = CMP_NUM(us_1,us_2)))
-          return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-        a=  end;
-        b+=2; /* sizeof(short int); */
-        break;
-      }
-    case HA_KEYTYPE_LONG_INT:
-      l_1= mi_sint4korr(a);
-      l_2= mi_sint4korr(b);
-      if (piks && (flag = CMP_NUM(l_1,l_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 4; /* sizeof(long int); */
-      break;
-    case HA_KEYTYPE_ULONG_INT:
-      u_1= mi_sint4korr(a);
-      u_2= mi_sint4korr(b);
-      if (piks && (flag = CMP_NUM(u_1,u_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 4; /* sizeof(long int); */
-      break;
-    case HA_KEYTYPE_INT24:
-      l_1=mi_sint3korr(a);
-      l_2=mi_sint3korr(b);
-      if (piks && (flag = CMP_NUM(l_1,l_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 3;
-      break;
-    case HA_KEYTYPE_UINT24:
-      l_1=mi_uint3korr(a);
-      l_2=mi_uint3korr(b);
-      if (piks && (flag = CMP_NUM(l_1,l_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 3;
-      break;
-    case HA_KEYTYPE_FLOAT:
-      mi_float4get(f_1,a);
-      mi_float4get(f_2,b);
-      /*
-        The following may give a compiler warning about floating point
-        comparison not being safe, but this is ok in this context as
-        we are bascily doing sorting
-      */
-      if (piks && (flag = CMP_NUM(f_1,f_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 4; /* sizeof(float); */
-      break;
-    case HA_KEYTYPE_DOUBLE:
-      mi_float8get(d_1,a);
-      mi_float8get(d_2,b);
-      /*
-        The following may give a compiler warning about floating point
-        comparison not being safe, but this is ok in this context as
-        we are bascily doing sorting
-      */
-      if (piks && (flag = CMP_NUM(d_1,d_2)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 8;  /* sizeof(double); */
-      break;
-    case HA_KEYTYPE_NUM:                                /* Numeric key */
-    {
-      int swap_flag= 0;
-      int alength,blength;
-
-      if (keyseg->flag & HA_REVERSE_SORT)
-      {
-        swap_variables(uchar*, a, b);
-        swap_flag=1;                            /* Remember swap of a & b */
-        end= a+ (int) (end-b);
-      }
-      if (keyseg->flag & HA_SPACE_PACK)
-      {
-        alength= *a++; blength= *b++;
-        end=a+alength;
-        next_key_length=key_length-blength-1;
-      }
-      else
-      {
-        alength= (int) (end-a);
-        blength=keyseg->length;
-        /* remove pre space from keys */
-        for ( ; alength && *a == ' ' ; a++, alength--) ;
-        for ( ; blength && *b == ' ' ; b++, blength--) ;
-      }
-      if (piks)
-      {
-	if (*a == '-')
-	{
-	  if (*b != '-')
-	    return -1;
-	  a++; b++;
-	  swap_variables(uchar*, a, b);
-	  swap_variables(int, alength, blength);
-	  swap_flag=1-swap_flag;
-	  alength--; blength--;
-	  end=a+alength;
-	}
-	else if (*b == '-')
-	  return 1;
-	while (alength && (*a == '+' || *a == '0'))
-	{
-	  a++; alength--;
-	}
-	while (blength && (*b == '+' || *b == '0'))
-	{
-	  b++; blength--;
-	}
-	if (alength != blength)
-	  return (alength < blength) ? -1 : 1;
-	while (a < end)
-	  if (*a++ !=  *b++)
-	    return ((int) a[-1] - (int) b[-1]);
-      }
-      else
-      {
-        b+=(end-a);
-        a=end;
-      }
-
-      if (swap_flag)                            /* Restore pointers */
-        swap_variables(uchar*, a, b);
-      break;
-    }
-#ifdef HAVE_LONG_LONG
-    case HA_KEYTYPE_LONGLONG:
-    {
-      longlong ll_a,ll_b;
-      ll_a= mi_sint8korr(a);
-      ll_b= mi_sint8korr(b);
-      if (piks && (flag = CMP_NUM(ll_a,ll_b)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 8;
-      break;
-    }
-    case HA_KEYTYPE_ULONGLONG:
-    {
-      ulonglong ll_a,ll_b;
-      ll_a= mi_uint8korr(a);
-      ll_b= mi_uint8korr(b);
-      if (piks && (flag = CMP_NUM(ll_a,ll_b)))
-        return ((keyseg->flag & HA_REVERSE_SORT) ? -flag : flag);
-      a=  end;
-      b+= 8;
-      break;
-    }
-#endif
-    case HA_KEYTYPE_END:                        /* Ready */
-      goto end;                                 /* diff_pos is incremented */
-    }
-  }
-  (*diff_pos)++;
-end:
-  if (!(nextflag & SEARCH_FIND))
-  {
-    uint i;
-    if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST)) /* Find record after key */
-      return (nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
-    flag=0;
-    for (i=keyseg->length ; i-- > 0 ; )
-    {
-      if (*a++ != *b++)
-      {
-        flag= FCMP(a[-1],b[-1]);
-        break;
-      }
-    }
-    if (nextflag & SEARCH_SAME)
-      return (flag);                            /* read same */
-    if (nextflag & SEARCH_BIGGER)
-      return (flag <= 0 ? -1 : 1);              /* read next */
-    return (flag < 0 ? -1 : 1);                 /* read previous */
-  }
-  return 0;
-} /* ha_key_cmp */
-
-
-/*
-  Find the first NULL value in index-suffix values tuple
-
-  SYNOPSIS
-    ha_find_null()
-      keyseg     Array of keyparts for key suffix
-      a          Key suffix value tuple
-
-  DESCRIPTION
-    Find the first NULL value in index-suffix values tuple.
-
-  TODO
-    Consider optimizing this function or its use so we don't search for
-    NULL values in completely NOT NULL index suffixes.
-
-  RETURN
-    First key part that has NULL as value in values tuple, or the last key
-    part (with keyseg->type==HA_TYPE_END) if values tuple doesn't contain
-    NULLs.
-*/
-
-HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a)
-{
-  for (; (enum ha_base_keytype) keyseg->type != HA_KEYTYPE_END; keyseg++)
-  {
-    uchar *end;
-    if (keyseg->null_bit)
-    {
-      if (!*a++)
-        return keyseg;
-    }
-    end= a+ keyseg->length;
-
-    switch ((enum ha_base_keytype) keyseg->type) {
-    case HA_KEYTYPE_TEXT:
-    case HA_KEYTYPE_BINARY:
-    case HA_KEYTYPE_BIT:
-      if (keyseg->flag & HA_SPACE_PACK)
-      {
-        int a_length;
-        get_key_length(a_length, a);
-        a += a_length;
-        break;
-      }
-      else
-        a= end;
-      break;
-    case HA_KEYTYPE_VARTEXT1:
-    case HA_KEYTYPE_VARTEXT2:
-    case HA_KEYTYPE_VARBINARY1:
-    case HA_KEYTYPE_VARBINARY2:
-      {
-        int a_length;
-        get_key_length(a_length, a);
-        a+= a_length;
-        break;
-      }
-    case HA_KEYTYPE_NUM:
-      if (keyseg->flag & HA_SPACE_PACK)
-      {
-        int alength= *a++;
-        end= a+alength;
-      }
-      a= end;
-      break;
-    case HA_KEYTYPE_INT8:
-    case HA_KEYTYPE_SHORT_INT:
-    case HA_KEYTYPE_USHORT_INT:
-    case HA_KEYTYPE_LONG_INT:
-    case HA_KEYTYPE_ULONG_INT:
-    case HA_KEYTYPE_INT24:
-    case HA_KEYTYPE_UINT24:
-#ifdef HAVE_LONG_LONG
-    case HA_KEYTYPE_LONGLONG:
-    case HA_KEYTYPE_ULONGLONG:
-#endif
-    case HA_KEYTYPE_FLOAT:
-    case HA_KEYTYPE_DOUBLE:
-      a= end;
-      break;
-    case HA_KEYTYPE_END:                        /* purecov: inspected */
-      /* keep compiler happy */
-      DBUG_ASSERT(0);
-      break;
-    }
-  }
-  return keyseg;
-}
-
-
-
-/*
-  Register handler error messages for usage with my_error()
-
-  NOTES
-    This is safe to call multiple times as my_error_register()
-    will ignore calls to register already registered error numbers.
-*/
-
-static const char **get_handler_error_messages()
-{
-  return handler_error_messages;
-}
-
-void my_handler_error_register(void)
-{
-  /*
-    If you got compilation error here about compile_time_assert array, check
-    that every HA_ERR_xxx constant has a corresponding error message in
-    handler_error_messages[] list (check mysys/my_handler_errors.h and
-    include/my_base.h).
-  */
-  compile_time_assert(HA_ERR_FIRST + array_elements(handler_error_messages) ==
-                      HA_ERR_LAST + 1);
-  my_error_register(get_handler_error_messages, HA_ERR_FIRST,
-                    HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
-}
-
-
-void my_handler_error_unregister(void)
-{
-  my_error_unregister(HA_ERR_FIRST,
-                      HA_ERR_FIRST+ array_elements(handler_error_messages)-1);
-}

=== modified file 'mysys/my_handler_errors.h'
--- a/mysys/my_handler_errors.h	2011-02-02 22:02:29 +0000
+++ b/mysys/my_handler_errors.h	2011-03-29 12:56:34 +0000
@@ -84,4 +84,8 @@ static const char *handler_error_message
   "Record not matching the given partition set"
 };
 
+extern void my_handler_error_register(void);
+extern void my_handler_error_unregister(void);
+
+
 #endif /* MYSYS_MY_HANDLER_ERRORS_INCLUDED */

=== removed file 'mysys/my_port.c'
--- a/mysys/my_port.c	2006-12-31 00:02:27 +0000
+++ b/mysys/my_port.c	1970-01-01 00:00:00 +0000
@@ -1,40 +0,0 @@
-/* Copyright (C) 2002 MySQL AB
-   
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public
-   License as published by the Free Software Foundation; version 2
-   of the License.
-   
-   This library 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
-   Library General Public License for more details.
-   
-   You should have received a copy of the GNU Library General Public
-   License along with this library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA */
-
-/* 
-   Small functions to make code portable
-*/
-
-#include "mysys_priv.h"
-
-#ifdef _AIX
-
-/*
-  On AIX, at least with gcc 3.1, the expression
-  '(double) (ulonglong) var' doesn't always work for big unsigned
-  integers like '18446744073709551615'.  The end result is that the
-  high bit is simply dropped. (probably bug in gcc optimizations)
-  Handling the conversion in a sub function seems to work.
-*/
-
-
-
-double my_ulonglong2double(unsigned long long nr)
-{
-  return (double) nr;
-}
-#endif /* _AIX */

=== modified file 'sql/handler.h'
--- a/sql/handler.h	2011-03-28 08:51:35 +0000
+++ b/sql/handler.h	2011-03-29 12:56:34 +0000
@@ -26,7 +26,7 @@
 #include "structs.h"                            /* SHOW_COMP_OPTION */
 
 #include <my_global.h>
-#include <my_handler.h>
+#include <my_compare.h>
 #include <ft_global.h>
 #include <keycache.h>
 

=== modified file 'sql/item.cc'
--- a/sql/item.cc	2011-03-22 11:44:40 +0000
+++ b/sql/item.cc	2011-03-30 07:42:03 +0000
@@ -1006,8 +1006,12 @@ bool Item::get_date(MYSQL_TIME *ltime,ui
   }
   else
   {
-    longlong value= val_int();
     int was_cut;
+    longlong value= val_int();
+
+    if (null_value)
+      goto err;
+
     if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == LL(-1))
     {
       char buff[22], *end;

=== modified file 'sql/item_sum.cc'
--- a/sql/item_sum.cc	2011-03-22 11:44:40 +0000
+++ b/sql/item_sum.cc	2011-04-01 14:04:52 +0000
@@ -2891,7 +2891,8 @@ int group_concat_key_cmp_with_order(cons
       uint offset= (field->offset(field->table->record[0]) -
                     table->s->null_bytes);
       if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
-        return (*order_item)->asc ? res : -res;
+        return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
+          
     }
   }
   /*
@@ -3432,7 +3433,7 @@ void Item_func_group_concat::print(Strin
       if (i)
         str->append(',');
       orig_args[i + arg_count_field]->print(str, query_type);
-      if (order[i]->asc)
+      if (order[i]->direction == ORDER::ORDER_ASC)
         str->append(STRING_WITH_LEN(" ASC"));
       else
         str->append(STRING_WITH_LEN(" DESC"));

=== modified file 'sql/item_timefunc.h'
--- a/sql/item_timefunc.h	2011-03-22 11:44:40 +0000
+++ b/sql/item_timefunc.h	2011-03-30 07:42:03 +0000
@@ -140,8 +140,11 @@ public:
   { DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); }
   String *val_str(String *str) 
   {
-    str->set(val_int(), collation.collation);
-    return null_value ? 0 : str;
+    longlong nr= val_int();
+    if (null_value)
+      return 0;
+    str->set(nr, collation.collation);
+    return str;
   }
   const char *func_name() const { return "month"; }
   enum Item_result result_type () const { return INT_RESULT; }

=== modified file 'sql/mysqld.cc'
--- a/sql/mysqld.cc	2011-03-28 23:37:10 +0000
+++ b/sql/mysqld.cc	2011-04-01 23:01:38 +0000
@@ -6781,6 +6781,110 @@ static int show_ssl_get_cipher_list(THD
   return 0;
 }
 
+
+#ifdef HAVE_YASSL
+
+static char *
+my_asn1_time_to_string(ASN1_TIME *time, char *buf, size_t len)
+{
+  return yaSSL_ASN1_TIME_to_string(time, buf, len);
+}
+
+#else /* openssl */
+
+static char *
+my_asn1_time_to_string(ASN1_TIME *time, char *buf, size_t len)
+{
+  int n_read;
+  char *res= NULL;
+  BIO *bio= BIO_new(BIO_s_mem());
+
+  if (bio == NULL)
+    return NULL;
+
+  if (!ASN1_TIME_print(bio, time))
+    goto end;
+
+  n_read= BIO_read(bio, buf, (int) (len - 1));
+
+  if (n_read > 0)
+  {
+    buf[n_read]= 0;
+    res= buf;
+  }
+
+end:
+  BIO_free(bio);
+  return res;
+}
+
+#endif
+
+
+/**
+  Handler function for the 'ssl_get_server_not_before' variable
+
+  @param      thd  the mysql thread structure
+  @param      var  the data for the variable
+  @param[out] buf  the string to put the value of the variable into
+
+  @return          status
+  @retval     0    success
+*/
+
+static int
+show_ssl_get_server_not_before(THD *thd, SHOW_VAR *var, char *buff)
+{
+  var->type= SHOW_CHAR;
+  if(thd->vio_ok() && thd->net.vio->ssl_arg)
+  {
+    SSL *ssl= (SSL*) thd->net.vio->ssl_arg;
+    X509 *cert= SSL_get_certificate(ssl);
+    ASN1_TIME *not_before= X509_get_notBefore(cert);
+
+    var->value= my_asn1_time_to_string(not_before, buff,
+                                       SHOW_VAR_FUNC_BUFF_SIZE);
+    if (!var->value)
+      return 1;
+    var->value= buff;
+  }
+  else
+    var->value= empty_c_string;
+  return 0;
+}
+
+
+/**
+  Handler function for the 'ssl_get_server_not_after' variable
+
+  @param      thd  the mysql thread structure
+  @param      var  the data for the variable
+  @param[out] buf  the string to put the value of the variable into
+
+  @return          status
+  @retval     0    success
+*/
+
+static int
+show_ssl_get_server_not_after(THD *thd, SHOW_VAR *var, char *buff)
+{
+  var->type= SHOW_CHAR;
+  if(thd->vio_ok() && thd->net.vio->ssl_arg)
+  {
+    SSL *ssl= (SSL*) thd->net.vio->ssl_arg;
+    X509 *cert= SSL_get_certificate(ssl);
+    ASN1_TIME *not_after= X509_get_notAfter(cert);
+
+    var->value= my_asn1_time_to_string(not_after, buff,
+                                       SHOW_VAR_FUNC_BUFF_SIZE);
+    if (!var->value)
+      return 1;
+  }
+  else
+    var->value= empty_c_string;
+  return 0;
+}
+
 #endif /* HAVE_OPENSSL && !EMBEDDED_LIBRARY */
 
 
@@ -6899,6 +7003,10 @@ SHOW_VAR status_vars[]= {
   {"Ssl_verify_depth",         (char*) &show_ssl_get_verify_depth, SHOW_FUNC},
   {"Ssl_verify_mode",          (char*) &show_ssl_get_verify_mode, SHOW_FUNC},
   {"Ssl_version",              (char*) &show_ssl_get_version, SHOW_FUNC},
+  {"Ssl_server_not_before",    (char*) &show_ssl_get_server_not_before,
+    SHOW_FUNC},
+  {"Ssl_server_not_after",     (char*) &show_ssl_get_server_not_after,
+    SHOW_FUNC},
 #endif
 #endif /* HAVE_OPENSSL */
   {"Table_locks_immediate",    (char*) &locks_immediate,        SHOW_LONG},

=== modified file 'sql/opt_range.cc'
--- a/sql/opt_range.cc	2011-03-22 11:44:40 +0000
+++ b/sql/opt_range.cc	2011-04-01 14:04:52 +0000
@@ -742,6 +742,12 @@ public:
   bool is_ror_scan;
   /* Number of ranges in the last checked tree->key */
   uint n_ranges;
+
+  /* 
+     The sort order the range access method must be able
+     to provide. Three-value logic: asc/desc/don't care
+  */
+  ORDER::enum_order order_direction;
 };
 
 class TABLE_READ_PLAN;
@@ -2142,6 +2148,8 @@ static int fill_used_fields_bitmap(PARAM
       limit             Query limit
       force_quick_range Prefer to use range (instead of full table scan) even
                         if it is more expensive.
+      interesting_order The sort order the range access method must be able
+                        to provide. Three-value logic: asc/desc/don't care
 
   NOTES
     Updates the following in the select parameter:
@@ -2197,9 +2205,9 @@ static int fill_used_fields_bitmap(PARAM
 */
 
 int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
-				  table_map prev_tables,
-				  ha_rows limit, bool force_quick_range, 
-                                  bool ordered_output)
+                                  table_map prev_tables,
+                                  ha_rows limit, bool force_quick_range, 
+                                  const ORDER::enum_order interesting_order)
 {
   uint idx;
   double scan_time;
@@ -2260,7 +2268,8 @@ int SQL_SELECT::test_quick_select(THD *t
     param.imerge_cost_buff_size= 0;
     param.using_real_indexes= TRUE;
     param.remove_jump_scans= TRUE;
-    param.force_default_mrr= ordered_output;
+    param.force_default_mrr= (interesting_order != ORDER::ORDER_NOT_RELEVANT);
+    param.order_direction= interesting_order;
 
     thd->no_errors=1;				// Don't warn about NULL
     init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
@@ -2386,10 +2395,12 @@ int SQL_SELECT::test_quick_select(THD *t
         /*
           Simultaneous key scans and row deletes on several handler
           objects are not allowed so don't use ROR-intersection for
-          table deletes.
+          table deletes. Also, ROR-intersection cannot return rows in
+          descending order
         */
         if ((thd->lex->sql_command != SQLCOM_DELETE) && 
-             thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE))
+            thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE) &&
+            interesting_order != ORDER::ORDER_DESC)
         {
           /*
             Get best non-covering ROR-intersection plan and prepare data for
@@ -2413,7 +2424,9 @@ int SQL_SELECT::test_quick_select(THD *t
       }
       else
       {
-        if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE))
+        // Cannot return rows in descending order.
+        if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE) &&
+            interesting_order != ORDER::ORDER_DESC)
         {
           /* Try creating index_merge/ROR-union scan. */
           SEL_IMERGE *imerge;
@@ -4576,6 +4589,9 @@ TRP_ROR_INTERSECT *get_best_ror_intersec
       !param->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
     DBUG_RETURN(NULL);
 
+  if (param->order_direction == ORDER::ORDER_DESC)
+    DBUG_RETURN(NULL);
+
   /*
     Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of 
     them. Also find and save clustered PK scan if there is one.
@@ -9653,6 +9669,9 @@ get_best_group_min_max(PARAM *param, SEL
     DBUG_RETURN(NULL);
   if (table->s->keys == 0)        /* There are no indexes to use. */
     DBUG_RETURN(NULL);
+  /* Cannot do reverse ordering */
+  if (param->order_direction == ORDER::ORDER_DESC)
+    DBUG_RETURN(NULL);
 
   /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
   if (join->make_sum_func_list(join->all_fields, join->fields_list, 1))

=== modified file 'sql/opt_range.h'
--- a/sql/opt_range.h	2011-03-22 11:44:40 +0000
+++ b/sql/opt_range.h	2011-04-01 14:04:52 +0000
@@ -276,7 +276,15 @@ public:
   /* Range end should be called when we have looped over the whole index */
   virtual void range_end() {}
 
-  virtual bool reverse_sorted() = 0;
+  /** 
+    Whether the range access method returns records in reverse order.
+  */
+  virtual bool reverse_sorted() const = 0;
+  /** 
+    Whether the range access method is capable of returning records 
+    in reverse order.
+  */
+  virtual bool reverse_sort_possible() const = 0;
   virtual bool unique_key_range() { return false; }
   virtual bool clustered_pk_range() { return false; }
   
@@ -472,7 +480,8 @@ public:
   void range_end();
   int get_next_prefix(uint prefix_length, uint group_key_parts, 
                       uchar *cur_prefix);
-  bool reverse_sorted() { return 0; }
+  bool reverse_sorted() const { return false; }
+  bool reverse_sort_possible() const { return true; }
   bool unique_key_range();
   int init_ror_merged_scan(bool reuse_handler);
   void save_last_pos()
@@ -572,7 +581,8 @@ public:
   void need_sorted_output(bool sort) { DBUG_ASSERT(!sort); /* Can't do it */ }
   int  reset(void);
   int  get_next();
-  bool reverse_sorted() { return false; }
+  bool reverse_sorted() const { return false; }
+  bool reverse_sort_possible() const { return false; }
   bool unique_key_range() { return false; }
   int get_type() { return QS_TYPE_INDEX_MERGE; }
   void add_keys_and_lengths(String *key_names, String *used_lengths);
@@ -650,7 +660,8 @@ public:
   void need_sorted_output(bool sort) { DBUG_ASSERT(!sort); /* Can't do it */ }
   int  reset(void);
   int  get_next();
-  bool reverse_sorted() { return false; }
+  bool reverse_sorted() const { return false; }
+  bool reverse_sort_possible() const { return false; }
   bool unique_key_range() { return false; }
   int get_type() { return QS_TYPE_ROR_INTERSECT; }
   void add_keys_and_lengths(String *key_names, String *used_lengths);
@@ -721,7 +732,8 @@ public:
   void need_sorted_output(bool sort) { DBUG_ASSERT(!sort); /* Can't do it */ }
   int  reset(void);
   int  get_next();
-  bool reverse_sorted() { return false; }
+  bool reverse_sorted() const { return false; }
+  bool reverse_sort_possible() const { return false; }
   bool unique_key_range() { return false; }
   int get_type() { return QS_TYPE_ROR_UNION; }
   void add_keys_and_lengths(String *key_names, String *used_lengths);
@@ -863,7 +875,8 @@ public:
   void need_sorted_output(bool sort) { /* always do it */ }
   int reset();
   int get_next();
-  bool reverse_sorted() { return false; }
+  bool reverse_sorted() const { return false; }
+  bool reverse_sort_possible() const { return false; }
   bool unique_key_range() { return false; }
   int get_type() { return QS_TYPE_GROUP_MIN_MAX; }
   void add_keys_and_lengths(String *key_names, String *used_lengths);
@@ -885,7 +898,8 @@ public:
   QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts, 
                     bool *create_err);
   int get_next();
-  bool reverse_sorted() { return 1; }
+  bool reverse_sorted() const { return true; }
+  bool reverse_sort_possible() const { return true; }
   int get_type() { return QS_TYPE_RANGE_DESC; }
   QUICK_SELECT_I *make_reverse(uint used_key_parts_arg)
   {
@@ -921,7 +935,8 @@ class SQL_SELECT :public Sql_alloc {
   bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
   {
     key_map tmp(key_map::ALL_BITS);
-    return test_quick_select(thd, tmp, 0, limit, force_quick_range, FALSE) < 0;
+    return test_quick_select(thd, tmp, 0, limit, force_quick_range,
+                             ORDER::ORDER_NOT_RELEVANT) < 0;
   }
   inline bool skip_record(THD *thd, bool *skip_record)
   {
@@ -929,8 +944,8 @@ class SQL_SELECT :public Sql_alloc {
     return thd->is_error();
   }
   int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
-			ha_rows limit, bool force_quick_range, 
-                        bool ordered_output);
+                        ha_rows limit, bool force_quick_range,
+                        const ORDER::enum_order interesting_order);
 };
 
 

=== modified file 'sql/rpl_reporting.cc'
--- a/sql/rpl_reporting.cc	2010-08-05 17:45:25 +0000
+++ b/sql/rpl_reporting.cc	2011-03-28 13:19:08 +0000
@@ -17,6 +17,7 @@
 #include "rpl_reporting.h"
 #include "log.h" // sql_print_error, sql_print_warning,
                  // sql_print_information
+#include "rpl_slave.h"
 
 Slave_reporting_capability::Slave_reporting_capability(char const *thread_name)
   : m_thread_name(thread_name)
@@ -25,15 +26,94 @@ Slave_reporting_capability::Slave_report
                    &err_lock, MY_MUTEX_INIT_FAST);
 }
 
+#if !defined(EMBEDDED_LIBRARY)
+/**
+  Check if the current error is of temporary nature or not.
+  Some errors are temporary in nature, such as
+  ER_LOCK_DEADLOCK and ER_LOCK_WAIT_TIMEOUT.  Ndb also signals
+  that the error is temporary by pushing a warning with the error code
+  ER_GET_TEMPORARY_ERRMSG, if the originating error is temporary.
+
+  @param      thd  a THD instance, typically of the slave SQL thread's.
+  @error_arg  the error code for assessment. 
+              defaults to zero which makes the function check the top
+              of the reported errors stack.
+
+  @return 1 as the positive and 0 as the negative verdict
+*/
+int Slave_reporting_capability::has_temporary_error(THD *thd, uint error_arg) const
+{
+  uint error;
+  DBUG_ENTER("has_temporary_error");
+
+  DBUG_EXECUTE_IF("all_errors_are_temporary_errors",
+                  if (thd->stmt_da->is_error())
+                  {
+                    thd->clear_error();
+                    my_error(ER_LOCK_DEADLOCK, MYF(0));
+                  });
+
+  /*
+    The state of the slave thread can't be regarded as
+    experiencing a temporary failure in cases of @c is_slave_error was set TRUE,
+    or if there is no message in THD, we can't say if it's a temporary
+    error or not. This is currently the case for Incident_log_event,
+    which sets no message.
+  */
+  if (thd->is_fatal_error || !thd->is_error())
+    DBUG_RETURN(0);
+
+  error= (error_arg == 0)? thd->stmt_da->sql_errno() : error_arg;
+
+  /*
+    Temporary error codes:
+    currently, InnoDB deadlock detected by InnoDB or lock
+    wait timeout (innodb_lock_wait_timeout exceeded).
+    Notice, the temporary error requires slave_trans_retries != 0)
+  */
+  if (slave_trans_retries &&
+      (error == ER_LOCK_DEADLOCK || error == ER_LOCK_WAIT_TIMEOUT))
+    DBUG_RETURN(1);
+
+#ifdef HAVE_NDB_BINLOG
+  /*
+    currently temporary error set in ndbcluster
+  */
+  List_iterator_fast<MYSQL_ERROR> it(thd->warning_info->warn_list());
+  MYSQL_ERROR *err;
+  while ((err= it++))
+  {
+    DBUG_PRINT("info", ("has condition %d %s", err->get_sql_errno(),
+                        err->get_message_text()));
+    switch (err->get_sql_errno())
+    {
+    case ER_GET_TEMPORARY_ERRMSG:
+      DBUG_RETURN(1);
+    default:
+      break;
+    }
+  }
+#endif
+  DBUG_RETURN(0);
+}
+#endif // EMBEDDED_LIBRARY
+
+
 void
 Slave_reporting_capability::report(loglevel level, int err_code,
                                    const char *msg, ...) const
 {
+#if !defined(EMBEDDED_LIBRARY)
+  THD *thd= current_thd;
   void (*report_function)(const char *, ...);
   char buff[MAX_SLAVE_ERRMSG];
   char *pbuff= buff;
   uint pbuffsize= sizeof(buff);
   va_list args;
+
+  if (level == ERROR_LEVEL && has_temporary_error(thd, err_code))
+    level= WARNING_LEVEL;
+
   va_start(args, msg);
 
   mysql_mutex_lock(&err_lock);
@@ -51,10 +131,12 @@ Slave_reporting_capability::report(logle
     report_function= sql_print_error;
     break;
   case WARNING_LEVEL:
-    report_function= sql_print_warning;
+    report_function= global_system_variables.log_warnings?
+      sql_print_warning : NULL;
     break;
   case INFORMATION_LEVEL:
-    report_function= sql_print_information;
+    report_function= global_system_variables.log_warnings?
+      sql_print_information : NULL;
     break;
   default:
     DBUG_ASSERT(0);                            // should not come here
@@ -71,6 +153,7 @@ Slave_reporting_capability::report(logle
                   m_thread_name, pbuff,
                   (pbuff[0] && *(strend(pbuff)-1) == '.') ? "" : ",",
                   err_code);
+#endif  
 }
 
 Slave_reporting_capability::~Slave_reporting_capability()

=== modified file 'sql/rpl_reporting.h'
--- a/sql/rpl_reporting.h	2010-08-05 17:45:25 +0000
+++ b/sql/rpl_reporting.h	2011-03-28 13:19:08 +0000
@@ -23,6 +23,11 @@
  */
 #define MAX_SLAVE_ERRMSG      1024
 
+// todo: consider to remove rpl_reporting.cc,h from building embedded
+#if !defined(EMBEDDED_LIBRARY)
+class THD;
+#endif
+
 /**
    Mix-in to handle the message logging and reporting for relay log
    info and master log info structures.
@@ -65,6 +70,13 @@ public:
     mysql_mutex_unlock(&err_lock);
   }
 
+#if !defined(EMBEDDED_LIBRARY)
+  /**
+     Check if the current error is of temporary nature or not.
+  */
+  int has_temporary_error(THD *thd, uint error_arg= 0) const;
+#endif // EMBEDDED_LIBRARY
+  
   /**
      Error information structure.
    */

=== modified file 'sql/rpl_slave.cc'
--- a/sql/rpl_slave.cc	2011-03-17 17:39:31 +0000
+++ b/sql/rpl_slave.cc	2011-03-29 14:56:01 +0000
@@ -2518,63 +2518,6 @@ static ulong read_event(MYSQL* mysql, Ma
   DBUG_RETURN(len - 1);
 }
 
-/*
-  Check if the current error is of temporary nature of not.
-  Some errors are temporary in nature, such as
-  ER_LOCK_DEADLOCK and ER_LOCK_WAIT_TIMEOUT.  Ndb also signals
-  that the error is temporary by pushing a warning with the error code
-  ER_GET_TEMPORARY_ERRMSG, if the originating error is temporary.
-*/
-static int has_temporary_error(THD *thd)
-{
-  DBUG_ENTER("has_temporary_error");
-
-  DBUG_EXECUTE_IF("all_errors_are_temporary_errors",
-                  if (thd->stmt_da->is_error())
-                  {
-                    thd->clear_error();
-                    my_error(ER_LOCK_DEADLOCK, MYF(0));
-                  });
-
-  /*
-    If there is no message in THD, we can't say if it's a temporary
-    error or not. This is currently the case for Incident_log_event,
-    which sets no message. Return FALSE.
-  */
-  if (!thd->is_error())
-    DBUG_RETURN(0);
-
-  /*
-    Temporary error codes:
-    currently, InnoDB deadlock detected by InnoDB or lock
-    wait timeout (innodb_lock_wait_timeout exceeded
-  */
-  if (thd->stmt_da->sql_errno() == ER_LOCK_DEADLOCK ||
-      thd->stmt_da->sql_errno() == ER_LOCK_WAIT_TIMEOUT)
-    DBUG_RETURN(1);
-
-#ifdef HAVE_NDB_BINLOG
-  /*
-    currently temporary error set in ndbcluster
-  */
-  List_iterator_fast<MYSQL_ERROR> it(thd->warning_info->warn_list());
-  MYSQL_ERROR *err;
-  while ((err= it++))
-  {
-    DBUG_PRINT("info", ("has condition %d %s", err->get_sql_errno(),
-                        err->get_message_text()));
-    switch (err->get_sql_errno())
-    {
-    case ER_GET_TEMPORARY_ERRMSG:
-      DBUG_RETURN(1);
-    default:
-      break;
-    }
-  }
-#endif
-  DBUG_RETURN(0);
-}
-
 
 /**
   If this is a lagging slave (specified with CHANGE MASTER TO MASTER_DELAY = X), delays accordingly. Also unlocks rli->data_lock.
@@ -2965,7 +2908,7 @@ static int exec_relay_log_event(THD* thd
     if (slave_trans_retries)
     {
       int UNINIT_VAR(temp_err);
-      if (exec_res && (temp_err= has_temporary_error(thd)))
+      if (exec_res && (temp_err= rli->has_temporary_error(thd)))
       {
         const char *errmsg;
         /*
@@ -3011,10 +2954,13 @@ static int exec_relay_log_event(THD* thd
           }
         }
         else
-          sql_print_error("Slave SQL thread retried transaction %lu time(s) "
-                          "in vain, giving up. Consider raising the value of "
-                          "the slave_transaction_retries variable.",
-                          slave_trans_retries);
+        {
+          thd->is_fatal_error= 1;
+          rli->report(ERROR_LEVEL, thd->stmt_da->sql_errno(),
+                      "Slave SQL thread retried transaction %lu time(s) "
+                      "in vain, giving up. Consider raising the value of "
+                      "the slave_transaction_retries variable.", rli->trans_retries);
+        }
       }
       else if ((exec_res && !temp_err) ||
                (opt_using_transactions &&
@@ -4376,7 +4322,7 @@ static int queue_event(Master_info* mi,c
       int debug_cor_pos = rand() % (event_len - BINLOG_CHECKSUM_LEN);
       debug_event_buf_c[debug_cor_pos] =~ debug_event_buf_c[debug_cor_pos];
       DBUG_PRINT("info", ("Corrupt the event at queue_event: byte on position %d", debug_cor_pos));
-      DBUG_SET("");
+      DBUG_SET("-d,corrupt_queue_event");
     }
   );
                                               

=== modified file 'sql/sql_lex.cc'
--- a/sql/sql_lex.cc	2011-03-11 09:35:38 +0000
+++ b/sql/sql_lex.cc	2011-04-01 14:04:52 +0000
@@ -2196,7 +2196,7 @@ void st_select_lex::print_order(String *
     }
     else
       (*order->item)->print(str, query_type);
-    if (!order->asc)
+    if (order->direction == ORDER::ORDER_DESC)
       str->append(STRING_WITH_LEN(" desc"));
     if (order->next)
       str->append(',');

=== modified file 'sql/sql_parse.cc'
--- a/sql/sql_parse.cc	2011-03-28 20:15:26 +0000
+++ b/sql/sql_parse.cc	2011-04-01 23:01:38 +0000
@@ -5812,7 +5812,7 @@ bool add_to_list(THD *thd, SQL_I_List<OR
     DBUG_RETURN(1);
   order->item_ptr= item;
   order->item= &order->item_ptr;
-  order->asc = asc;
+  order->direction= (asc ? ORDER::ORDER_ASC : ORDER::ORDER_DESC);
   order->free_me=0;
   order->used=0;
   order->counter_used= 0;

=== modified file 'sql/sql_select.cc'
--- a/sql/sql_select.cc	2011-03-24 13:16:36 +0000
+++ b/sql/sql_select.cc	2011-04-01 14:04:52 +0000
@@ -4516,7 +4516,6 @@ static ha_rows get_quick_record_count(TH
 				      TABLE *table,
 				      const key_map *keys,ha_rows limit)
 {
-  int error;
   DBUG_ENTER("get_quick_record_count");
   uchar buff[STACK_BUFF_ALLOC];
   if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
@@ -4524,8 +4523,13 @@ static ha_rows get_quick_record_count(TH
   if (select)
   {
     select->head=table;
-    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
-                                          limit, 0, FALSE)) == 1)
+    int error= select->test_quick_select(thd, 
+                                         *keys, 
+                                         0,      //empty table_map
+                                         limit, 
+                                         false,  //don't force quick range
+                                         ORDER::ORDER_NOT_RELEVANT);
+    if (error == 1)
       DBUG_RETURN(select->quick->records);
     if (error == -1)
     {
@@ -9895,13 +9899,14 @@ static bool make_join_select(JOIN *join,
 	    if (sel->cond && !sel->cond->fixed)
 	      sel->cond->quick_fix_field();
 
-	    if (sel->test_quick_select(thd, tab->keys,
-				       used_tables & ~ current_map,
-				       (join->select_options &
-					OPTION_FOUND_ROWS ?
-					HA_POS_ERROR :
-					join->unit->select_limit_cnt), 0,
-                                        FALSE) < 0)
+            if (sel->test_quick_select(thd, tab->keys,
+                                       used_tables & ~ current_map,
+                                       (join->select_options &
+                                        OPTION_FOUND_ROWS ?
+                                        HA_POS_ERROR :
+                                        join->unit->select_limit_cnt),
+                                       false,   // don't force quick range
+                                       ORDER::ORDER_NOT_RELEVANT) < 0)
             {
 	      /*
 		Before reporting "Impossible WHERE" for the whole query
@@ -9914,8 +9919,9 @@ static bool make_join_select(JOIN *join,
                                          (join->select_options &
                                           OPTION_FOUND_ROWS ?
                                           HA_POS_ERROR :
-                                          join->unit->select_limit_cnt),0,
-                                          FALSE) < 0)
+                                          join->unit->select_limit_cnt),
+                                         false,   //don't force quick range
+                                         ORDER::ORDER_NOT_RELEVANT) < 0)
 		DBUG_RETURN(1);			// Impossible WHERE
             }
             else
@@ -10980,7 +10986,7 @@ bool setup_sj_materialization(JOIN_TAB *
 {
   uint i;
   DBUG_ENTER("setup_sj_materialization");
-  TABLE_LIST *emb_sj_nest= tab->table->pos_in_table_list->embedding;
+  TABLE_LIST *emb_sj_nest= tab->emb_sj_nest;
   Semijoin_mat_exec *sjm= emb_sj_nest->sj_mat_exec;
   THD *thd= tab->join->thd;
   /* First the calls come to the materialization function */
@@ -18424,9 +18430,12 @@ test_if_quick_select(JOIN_TAB *tab)
 {
   delete tab->select->quick;
   tab->select->quick=0;
-  return tab->select->test_quick_select(tab->join->thd, tab->keys,
-					(table_map) 0, HA_POS_ERROR, 0,
-                                        FALSE);
+  return tab->select->test_quick_select(tab->join->thd, 
+                                        tab->keys,
+                                        0,          // empty table map
+                                        HA_POS_ERROR, 
+                                        false,      // don't force quick range
+                                        ORDER::ORDER_NOT_RELEVANT);
 }
 
 
@@ -19651,9 +19660,11 @@ static int test_if_order_by_key(ORDER *o
     if (key_part->field != field || !field->part_of_sortkey.is_set(idx))
       DBUG_RETURN(0);
 
+    const ORDER::enum_order keypart_order= 
+      (key_part->key_part_flag & HA_REVERSE_SORT) ? 
+      ORDER::ORDER_DESC : ORDER::ORDER_ASC;
     /* set flag to 1 if we can use read-next on key, else to -1 */
-    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
-           1 : -1);
+    flag= (order->direction == keypart_order) ? 1 : -1;
     if (reverse && flag != reverse)
       DBUG_RETURN(0);
     reverse=flag;				// Remember if reverse
@@ -20110,13 +20121,15 @@ test_if_skip_sort_order(JOIN_TAB *tab,OR
           new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
 
           select->quick= 0;
-          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
+          if (select->test_quick_select(tab->join->thd, 
+                                        new_ref_key_map, 
+                                        0,       // empty table_map
                                         (tab->join->select_options &
                                          OPTION_FOUND_ROWS) ?
                                         HA_POS_ERROR :
-                                        tab->join->unit->select_limit_cnt,0,
-                                        TRUE) <=
-              0)
+                                        tab->join->unit->select_limit_cnt,
+                                        false,   // don't force quick range
+                                        order->direction) <= 0)
             goto use_filesort;
 	}
         ref_key= new_ref_key;
@@ -20161,11 +20174,14 @@ test_if_skip_sort_order(JOIN_TAB *tab,OR
         key_map map;           // Force the creation of quick select
         map.set_bit(best_key); // only best_key.
         select->quick= 0;
-        select->test_quick_select(join->thd, map, 0,
+        select->test_quick_select(join->thd, 
+                                  map, 
+                                  0,        // empty table_map
                                   join->select_options & OPTION_FOUND_ROWS ?
                                   HA_POS_ERROR :
                                   join->unit->select_limit_cnt,
-                                  TRUE, FALSE);
+                                  true,     // force quick range
+                                  order->direction);
       }
       order_direction= best_key_direction;
       /*
@@ -20195,18 +20211,13 @@ check_reverse_order:
       */
       if (select->quick->reverse_sorted())
         goto skipped_filesort;
-      else
-      {
-        int quick_type= select->quick->get_type();
-        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
-            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
-            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
-            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
-        {
-          tab->limit= 0;
-          goto use_filesort;                   // Use filesort
-        }
-      }
+
+      /*
+        test_quick_select() should not create a quick that cannot do
+        reverse ordering
+      */
+      DBUG_ASSERT((select->quick == save_quick) ||
+                  select->quick->reverse_sort_possible());
     }
   }
 
@@ -20316,7 +20327,7 @@ check_reverse_order:
   /*
     Cleanup:
     We may have both a 'select->quick' and 'save_quick' (original)
-    at this point. Delete the one that we wan't use.
+    at this point. Delete the one that we won't use.
   */
 
 skipped_filesort:
@@ -20823,7 +20834,7 @@ SORT_FIELD *make_unireg_sortorder(ORDER
     }
     else
       pos->item= *order->item;
-    pos->reverse=! order->asc;
+    pos->reverse= (order->direction == ORDER::ORDER_DESC);
     DBUG_ASSERT(pos->field != NULL || pos->item != NULL);
   }
   *length=count;
@@ -21336,7 +21347,7 @@ create_distinct_group(THD *thd, Item **r
         */
         ord->item= ref_pointer_array;
       }
-      ord->asc=1;
+      ord->direction= ORDER::ORDER_ASC;
       *prev=ord;
       prev= &ord->next;
     }
@@ -21413,7 +21424,7 @@ test_if_subpart(ORDER *a,ORDER *b)
   for (; a && b; a=a->next,b=b->next)
   {
     if ((*a->item)->eq(*b->item,1))
-      a->asc=b->asc;
+      a->direction= b->direction;
     else
       return 0;
   }

=== modified file 'sql/sql_table.cc'
--- a/sql/sql_table.cc	2011-03-28 08:10:39 +0000
+++ b/sql/sql_table.cc	2011-03-30 11:43:32 +0000
@@ -2394,10 +2394,6 @@ int mysql_rm_table_no_locks(THD *thd, TA
     else
     {
       char *end;
-      /*
-        Cannot use the db_type from the table, since that might have changed
-        while waiting for the exclusive name lock.
-      */
       if (frm_db_type == DB_TYPE_UNKNOWN)
       {
         dd_frm_type(thd, path, &frm_db_type);

=== modified file 'sql/sql_update.cc'
--- a/sql/sql_update.cc	2011-02-25 16:41:57 +0000
+++ b/sql/sql_update.cc	2011-04-01 14:04:52 +0000
@@ -1732,7 +1732,7 @@ loop_end:
 
     /* Make an unique key over the first field to avoid duplicated updates */
     bzero((char*) &group, sizeof(group));
-    group.asc= 1;
+    group.direction= ORDER::ORDER_ASC;
     group.item= (Item**) temp_fields.head_ref();
 
     tmp_param->quick_group=1;

=== modified file 'sql/table.h'
--- a/sql/table.h	2011-03-25 13:28:19 +0000
+++ b/sql/table.h	2011-04-01 14:04:52 +0000
@@ -190,16 +190,22 @@ private:
 
 typedef struct st_order {
   struct st_order *next;
-  Item	 **item;			/* Point at item in select fields */
-  Item	 *item_ptr;			/* Storage for initial item */
+  Item   **item;                        /* Point at item in select fields */
+  Item   *item_ptr;                     /* Storage for initial item */
   int    counter;                       /* position in SELECT list, correct
-                                           only if counter_used is true*/
-  bool	 asc;				/* true if ascending */
-  bool	 free_me;			/* true if item isn't shared  */
-  bool	 in_field_list;			/* true if in select field list */
+                                           only if counter_used is true */
+  enum enum_order {
+    ORDER_NOT_RELEVANT,
+    ORDER_ASC,
+    ORDER_DESC
+  };
+
+  enum_order direction;                 /* Requested direction of ordering */
+  bool   free_me;                       /* true if item isn't shared  */
+  bool   in_field_list;                 /* true if in select field list */
   bool   counter_used;                  /* parameter was counter of columns */
-  Field  *field;			/* If tmp-table group */
-  char	 *buff;				/* If tmp-table group */
+  Field  *field;                        /* If tmp-table group */
+  char   *buff;                         /* If tmp-table group */
   table_map used, depend_map;
 } ORDER;
 
@@ -1664,7 +1670,6 @@ struct TABLE_LIST
   bool          check_option_processed;
   /* FRMTYPE_ERROR if any type is acceptable */
   enum frm_type_enum required_type;
-  handlerton	*db_type;		/* table_type for handler */
   char		timestamp_buffer[20];	/* buffer for timestamp (19+1) */
   /*
     This TABLE_LIST object is just placeholder for prelocking, it will be
@@ -1684,9 +1689,6 @@ struct TABLE_LIST
     /* Don't associate a table share. */
     OPEN_STUB
   } open_strategy;
-  /* For transactional locking. */
-  int           lock_timeout;           /* NOWAIT or WAIT [X]               */
-  bool          lock_transactional;     /* If transactional lock requested. */
   bool          internal_tmp_table;
   /** TRUE if an alias for this table was specified in the SQL. */
   bool          is_alias;

=== modified file 'storage/innobase/CMakeLists.txt'
--- a/storage/innobase/CMakeLists.txt	2011-03-15 16:17:31 +0000
+++ b/storage/innobase/CMakeLists.txt	2011-03-24 12:24:08 +0000
@@ -247,7 +247,6 @@ SET(INNOBASE_SOURCES	btr/btr0btr.c btr/b
 			row/row0sel.c row/row0uins.c row/row0umod.c row/row0undo.c row/row0upd.c row/row0vers.c
 			srv/srv0mon.c srv/srv0srv.c srv/srv0start.c
 			sync/sync0arr.c sync/sync0rw.c sync/sync0sync.c
-			thr/thr0loc.c
 			trx/trx0i_s.c trx/trx0purge.c trx/trx0rec.c trx/trx0roll.c trx/trx0rseg.c
 			trx/trx0sys.c trx/trx0trx.c trx/trx0undo.c
 			usr/usr0sess.c

=== modified file 'storage/innobase/btr/btr0cur.c'
--- a/storage/innobase/btr/btr0cur.c	2011-03-15 10:57:47 +0000
+++ b/storage/innobase/btr/btr0cur.c	2011-03-24 12:24:08 +0000
@@ -402,7 +402,7 @@ btr_cur_search_to_nth_level(
 
 	ut_ad(level == 0 || mode == PAGE_CUR_LE);
 	ut_ad(dict_index_check_search_tuple(index, tuple));
-	ut_ad(!dict_index_is_ibuf(index) || ibuf_inside());
+	ut_ad(!dict_index_is_ibuf(index) || ibuf_inside(mtr));
 	ut_ad(dtuple_check_typed(tuple));
 
 #ifdef UNIV_DEBUG
@@ -4534,8 +4534,8 @@ btr_check_blob_fil_page_type(
 		ulint	flags = fil_space_get_flags(space_id);
 
 #ifndef UNIV_DEBUG /* Improve debug test coverage */
-		if (UNIV_LIKELY
-		    ((flags & DICT_TF_FORMAT_MASK) == DICT_TF_FORMAT_51)) {
+		if (UNIV_LIKELY((flags & DICT_TF_FORMAT_MASK)
+				== (UNIV_FORMAT_A << DICT_TF_FORMAT_SHIFT))) {
 			/* Old versions of InnoDB did not initialize
 			FIL_PAGE_TYPE on BLOB pages.  Do not print
 			anything about the type mismatch when reading
@@ -4935,8 +4935,8 @@ btr_copy_zblob_prefix(
 	page_zip_set_alloc(&d_stream, heap);
 
 	ut_ad(ut_is_2pow(zip_size));
-	ut_ad(zip_size >= PAGE_ZIP_MIN_SIZE);
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
+	ut_ad(zip_size >= UNIV_ZIP_SIZE_MIN);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 	ut_ad(space_id);
 
 	err = inflateInit(&d_stream);

=== modified file 'storage/innobase/buf/buf0buddy.c'
--- a/storage/innobase/buf/buf0buddy.c	2011-02-03 22:18:48 +0000
+++ b/storage/innobase/buf/buf0buddy.c	2011-03-23 13:43:14 +0000
@@ -434,13 +434,13 @@ buf_buddy_relocate(
 
 	/* We look inside the allocated objects returned by
 	buf_buddy_alloc() and assume that anything of
-	PAGE_ZIP_MIN_SIZE or larger is a compressed page that contains
+	UNIV_ZIP_SIZE_MIN or larger is a compressed page that contains
 	a valid space_id and page_no in the page header.  Should the
 	fields be invalid, we will be unable to relocate the block.
 	We also assume that anything that fits sizeof(buf_page_t)
 	actually is a properly initialized buf_page_t object. */
 
-	if (size >= PAGE_ZIP_MIN_SIZE) {
+	if (size >= UNIV_ZIP_SIZE_MIN) {
 		/* This is a compressed page. */
 		mutex_t*	mutex;
 
@@ -659,7 +659,7 @@ buddy_nonfree:
 	/* Free the block to the buddy list. */
 	bpage = buf;
 #ifdef UNIV_DEBUG
-	if (i < buf_buddy_get_slot(PAGE_ZIP_MIN_SIZE)) {
+	if (i < buf_buddy_get_slot(UNIV_ZIP_SIZE_MIN)) {
 		/* This area has most likely been allocated for at
 		least one compressed-only block descriptor.  Check
 		that there are no live objects in the area.  This is

=== modified file 'storage/innobase/buf/buf0buf.c'
--- a/storage/innobase/buf/buf0buf.c	2011-03-15 13:26:34 +0000
+++ b/storage/innobase/buf/buf0buf.c	2011-03-24 12:24:08 +0000
@@ -2461,9 +2461,6 @@ buf_page_get_zip(
 	unsigned	access_time;
 	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
 
-#ifndef UNIV_LOG_DEBUG
-	ut_ad(!ibuf_inside());
-#endif
 	buf_pool->stat.n_page_gets++;
 
 	for (;;) {
@@ -2908,8 +2905,9 @@ buf_page_get_gen(
 	ut_ad(zip_size == fil_space_get_zip_size(space));
 	ut_ad(ut_is_2pow(zip_size));
 #ifndef UNIV_LOG_DEBUG
-	ut_ad(!ibuf_inside() || ibuf_page_low(space, zip_size, offset,
-					      FALSE, file, line, NULL));
+	ut_ad(!ibuf_inside(mtr)
+	      || ibuf_page_low(space, zip_size, offset,
+			       FALSE, file, line, NULL));
 #endif
 	buf_pool->stat.n_page_gets++;
 	fold = buf_page_address_fold(space, offset);
@@ -3347,7 +3345,8 @@ wait_until_unfixed:
 		/* In the case of a first access, try to apply linear
 		read-ahead */
 
-		buf_read_ahead_linear(space, zip_size, offset);
+		buf_read_ahead_linear(space, zip_size, offset,
+				      ibuf_inside(mtr));
 	}
 
 #ifdef UNIV_IBUF_COUNT_DEBUG
@@ -3408,7 +3407,7 @@ buf_page_optimistic_get(
 	access_time = buf_page_is_accessed(&block->page);
 	buf_page_set_accessed_make_young(&block->page, access_time);
 
-	ut_ad(!ibuf_inside()
+	ut_ad(!ibuf_inside(mtr)
 	      || ibuf_page(buf_block_get_space(block),
 			   buf_block_get_zip_size(block),
 			   buf_block_get_page_no(block), NULL));
@@ -3464,7 +3463,8 @@ buf_page_optimistic_get(
 
 		buf_read_ahead_linear(buf_block_get_space(block),
 				      buf_block_get_zip_size(block),
-				      buf_block_get_page_no(block));
+				      buf_block_get_page_no(block),
+				      ibuf_inside(mtr));
 	}
 
 #ifdef UNIV_IBUF_COUNT_DEBUG
@@ -3540,7 +3540,7 @@ buf_page_get_known_nowait(
 		buf_pool_mutex_exit(buf_pool);
 	}
 
-	ut_ad(!ibuf_inside() || (mode == BUF_KEEP_OLD));
+	ut_ad(!ibuf_inside(mtr) || mode == BUF_KEEP_OLD);
 
 	if (rw_latch == RW_S_LATCH) {
 		success = rw_lock_s_lock_nowait(&(block->lock),
@@ -3815,14 +3815,13 @@ buf_page_init_for_read(
 		/* It is a read-ahead within an ibuf routine */
 
 		ut_ad(!ibuf_bitmap_page(zip_size, offset));
-		ut_ad(ibuf_inside());
 
-		mtr_start(&mtr);
+		ibuf_mtr_start(&mtr);
 
 		if (!recv_no_ibuf_operations
 		    && !ibuf_page(space, zip_size, offset, &mtr)) {
 
-			mtr_commit(&mtr);
+			ibuf_mtr_commit(&mtr);
 
 			return(NULL);
 		}
@@ -4019,7 +4018,7 @@ func_exit:
 
 	if (mode == BUF_READ_IBUF_PAGES_ONLY) {
 
-		mtr_commit(&mtr);
+		ibuf_mtr_commit(&mtr);
 	}
 
 
@@ -5619,7 +5618,7 @@ buf_page_init_for_backup_restore(
 
 	/* We assume that block->page.data has been allocated
 	with zip_size == UNIV_PAGE_SIZE. */
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 	ut_ad(ut_is_2pow(zip_size));
 	page_zip_set_size(&block->page.zip, zip_size);
 	if (zip_size) {

=== modified file 'storage/innobase/buf/buf0flu.c'
--- a/storage/innobase/buf/buf0flu.c	2011-03-14 15:03:04 +0000
+++ b/storage/innobase/buf/buf0flu.c	2011-03-23 13:43:14 +0000
@@ -1021,7 +1021,7 @@ buf_flush_init_for_writing(
 		ulint		zip_size = page_zip_get_size(page_zip);
 		ut_ad(zip_size);
 		ut_ad(ut_is_2pow(zip_size));
-		ut_ad(zip_size <= UNIV_PAGE_SIZE);
+		ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 
 		switch (UNIV_EXPECT(fil_page_get_type(page), FIL_PAGE_INDEX)) {
 		case FIL_PAGE_TYPE_ALLOCATED:

=== modified file 'storage/innobase/buf/buf0rea.c'
--- a/storage/innobase/buf/buf0rea.c	2011-02-10 12:17:21 +0000
+++ b/storage/innobase/buf/buf0rea.c	2011-03-24 12:24:08 +0000
@@ -236,10 +236,10 @@ UNIV_INTERN
 ulint
 buf_read_ahead_linear(
 /*==================*/
-	ulint	space,	/*!< in: space id */
-	ulint	zip_size,/*!< in: compressed page size in bytes, or 0 */
-	ulint	offset)	/*!< in: page number of a page; NOTE: the current thread
-			must want access to this page (see NOTE 3 above) */
+	ulint	space,		/*!< in: space id */
+	ulint	zip_size,	/*!< in: compressed page size in bytes, or 0 */
+	ulint	offset,		/*!< in: page number; see NOTE 3 above */
+	ibool	inside_ibuf)	/*!< in: TRUE if we are inside ibuf routine */
 {
 	buf_pool_t*	buf_pool = buf_pool_get(space, offset);
 	ib_int64_t	tablespace_version;
@@ -434,11 +434,9 @@ buf_read_ahead_linear(
 
 	/* If we got this far, read-ahead can be sensible: do it */
 
-	if (ibuf_inside()) {
-		ibuf_mode = BUF_READ_IBUF_PAGES_ONLY;
-	} else {
-		ibuf_mode = BUF_READ_ANY_PAGE;
-	}
+	ibuf_mode = inside_ibuf
+		? BUF_READ_IBUF_PAGES_ONLY | OS_AIO_SIMULATED_WAKE_LATER
+		: BUF_READ_ANY_PAGE | OS_AIO_SIMULATED_WAKE_LATER;
 
 	count = 0;
 
@@ -455,7 +453,7 @@ buf_read_ahead_linear(
 		if (!ibuf_bitmap_page(zip_size, i)) {
 			count += buf_read_page_low(
 				&err, FALSE,
-				ibuf_mode | OS_AIO_SIMULATED_WAKE_LATER,
+				ibuf_mode,
 				space, zip_size, FALSE, tablespace_version, i);
 			if (err == DB_TABLESPACE_DELETED) {
 				ut_print_timestamp(stderr);
@@ -525,7 +523,6 @@ buf_read_ibuf_merge_pages(
 {
 	ulint	i;
 
-	ut_ad(!ibuf_inside());
 #ifdef UNIV_IBUF_DEBUG
 	ut_a(n_stored < UNIV_PAGE_SIZE);
 #endif

=== modified file 'storage/innobase/data/data0data.c'
--- a/storage/innobase/data/data0data.c	2010-07-29 10:44:35 +0000
+++ b/storage/innobase/data/data0data.c	2011-03-23 13:43:14 +0000
@@ -583,7 +583,7 @@ dtuple_convert_big_rec(
 		return(NULL);
 	}
 
-	if (dict_table_get_format(index->table) < DICT_TF_FORMAT_ZIP) {
+	if (dict_table_get_format(index->table) < UNIV_FORMAT_B) {
 		/* up to MySQL 5.1: store a 768-byte prefix locally */
 		local_len = BTR_EXTERN_FIELD_REF_SIZE + DICT_MAX_INDEX_COL_LEN;
 	} else {

=== modified file 'storage/innobase/dict/dict0boot.c'
--- a/storage/innobase/dict/dict0boot.c	2010-10-20 08:54:44 +0000
+++ b/storage/innobase/dict/dict0boot.c	2011-03-18 15:48:14 +0000
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
+Copyright (c) 1996, 2011, Oracle and/or its affiliates. All Rights Reserved.
 
 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
@@ -283,7 +283,7 @@ dict_boot(void)
 	/* Insert into the dictionary cache the descriptions of the basic
 	system tables */
 	/*-------------------------*/
-	table = dict_mem_table_create("SYS_TABLES", DICT_HDR_SPACE, 8, 0);
+	table = dict_mem_table_create("SYS_TABLES", DICT_HDR_SPACE, 8, 0, 0);
 
 	dict_mem_table_add_col(table, heap, "NAME", DATA_BINARY, 0, 0);
 	dict_mem_table_add_col(table, heap, "ID", DATA_BINARY, 0, 0);
@@ -335,7 +335,7 @@ dict_boot(void)
 	ut_a(error == DB_SUCCESS);
 
 	/*-------------------------*/
-	table = dict_mem_table_create("SYS_COLUMNS", DICT_HDR_SPACE, 7, 0);
+	table = dict_mem_table_create("SYS_COLUMNS", DICT_HDR_SPACE, 7, 0, 0);
 
 	dict_mem_table_add_col(table, heap, "TABLE_ID", DATA_BINARY, 0, 0);
 	dict_mem_table_add_col(table, heap, "POS", DATA_INT, 0, 4);
@@ -367,7 +367,7 @@ dict_boot(void)
 	ut_a(error == DB_SUCCESS);
 
 	/*-------------------------*/
-	table = dict_mem_table_create("SYS_INDEXES", DICT_HDR_SPACE, 7, 0);
+	table = dict_mem_table_create("SYS_INDEXES", DICT_HDR_SPACE, 7, 0, 0);
 
 	dict_mem_table_add_col(table, heap, "TABLE_ID", DATA_BINARY, 0, 0);
 	dict_mem_table_add_col(table, heap, "ID", DATA_BINARY, 0, 0);
@@ -413,7 +413,7 @@ dict_boot(void)
 	ut_a(error == DB_SUCCESS);
 
 	/*-------------------------*/
-	table = dict_mem_table_create("SYS_FIELDS", DICT_HDR_SPACE, 3, 0);
+	table = dict_mem_table_create("SYS_FIELDS", DICT_HDR_SPACE, 3, 0, 0);
 
 	dict_mem_table_add_col(table, heap, "INDEX_ID", DATA_BINARY, 0, 0);
 	dict_mem_table_add_col(table, heap, "POS", DATA_INT, 0, 4);

=== modified file 'storage/innobase/dict/dict0crea.c'
--- a/storage/innobase/dict/dict0crea.c	2010-11-03 09:30:27 +0000
+++ b/storage/innobase/dict/dict0crea.c	2011-03-23 13:43:14 +0000
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
+Copyright (c) 1996, 2011, Oracle and/or its affiliates. All Rights Reserved.
 
 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
@@ -97,13 +97,13 @@ dict_create_sys_tables_tuple(
 	dfield = dtuple_get_nth_field(entry, 3/*TYPE*/);
 
 	ptr = mem_heap_alloc(heap, 4);
-	if (table->flags & (~DICT_TF_COMPACT & ~(~0 << DICT_TF_BITS))) {
+	if (table->flags & ~DICT_TF_COMPACT) {
 		ut_a(table->flags & DICT_TF_COMPACT);
-		ut_a(dict_table_get_format(table) >= DICT_TF_FORMAT_ZIP);
+		ut_a(dict_table_get_format(table) >= UNIV_FORMAT_B);
 		ut_a((table->flags & DICT_TF_ZSSIZE_MASK)
 		     <= (DICT_TF_ZSSIZE_MAX << DICT_TF_ZSSIZE_SHIFT));
-		ut_a(!(table->flags & (~0 << DICT_TF2_BITS)));
-		mach_write_to_4(ptr, table->flags & ~(~0 << DICT_TF_BITS));
+		ut_a(!(table->flags & ~DICT_TF_BIT_MASK));
+		mach_write_to_4(ptr, table->flags & DICT_TF_BIT_MASK);
 	} else {
 		mach_write_to_4(ptr, DICT_TABLE_ORDINARY);
 	}
@@ -120,7 +120,9 @@ dict_create_sys_tables_tuple(
 	dfield = dtuple_get_nth_field(entry, 5/*MIX_LEN*/);
 
 	ptr = mem_heap_alloc(heap, 4);
-	mach_write_to_4(ptr, table->flags >> DICT_TF2_SHIFT);
+	/* Be sure all non-used bits are zero. */
+	ut_a(!(table->flags2 & ~DICT_TF2_BIT_MASK));
+	mach_write_to_4(ptr, table->flags2);
 
 	dfield_set_data(dfield, ptr, 4);
 	/* 8: CLUSTER_NAME ---------------------*/
@@ -287,11 +289,12 @@ dict_build_table_def_step(
 			is_path = FALSE;
 		}
 
-		ut_ad(dict_table_get_format(table) <= DICT_TF_FORMAT_MAX);
+		ut_ad(dict_table_get_format(table) <= UNIV_FORMAT_MAX);
 		ut_ad(!dict_table_zip_size(table)
-		      || dict_table_get_format(table) >= DICT_TF_FORMAT_ZIP);
+		      || dict_table_get_format(table) >= UNIV_FORMAT_B);
 
-		flags = table->flags & ~(~0 << DICT_TF_BITS);
+		flags = table->flags;
+		ut_a(!(flags & ~DICT_TF_BIT_MASK));
 		error = fil_create_new_single_table_tablespace(
 			space, path_or_name, is_path,
 			flags == DICT_TF_COMPACT ? 0 : flags,
@@ -309,8 +312,10 @@ dict_build_table_def_step(
 
 		mtr_commit(&mtr);
 	} else {
-		/* Create in the system tablespace: disallow new features */
-		table->flags &= (~0 << DICT_TF_BITS) | DICT_TF_COMPACT;
+		/* Create in the system tablespace: disallow Barracuda
+		features by keeping only the first bit which says whether
+		the row format is redundant or compact */
+		table->flags &= DICT_TF_COMPACT;
 	}
 
 	row = dict_create_sys_tables_tuple(table, node->heap);
@@ -1123,7 +1128,7 @@ dict_create_index_step(
 			node->table, node->index, FIL_NULL,
 			trx_is_strict(trx)
 			|| dict_table_get_format(node->table)
-			>= DICT_TF_FORMAT_ZIP);
+			>= UNIV_FORMAT_B);
 
 		node->index = dict_index_get_if_in_cache_low(index_id);
 		ut_a(!node->index == (err != DB_SUCCESS));

=== modified file 'storage/innobase/dict/dict0dict.c'
--- a/storage/innobase/dict/dict0dict.c	2011-02-18 13:00:57 +0000
+++ b/storage/innobase/dict/dict0dict.c	2011-03-23 13:43:14 +0000
@@ -1988,14 +1988,14 @@ too_big:
 	}
 
 	switch (dict_table_get_format(table)) {
-	case DICT_TF_FORMAT_51:
+	case UNIV_FORMAT_A:
 		/* ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT store
 		prefixes of externally stored columns locally within
 		the record.  There are no special considerations for
 		the undo log record size. */
 		goto undo_size_ok;
 
-	case DICT_TF_FORMAT_ZIP:
+	case UNIV_FORMAT_B:
 		/* In ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPRESSED,
 		column prefix indexes require that prefixes of
 		externally stored columns are written to the undo log.
@@ -2005,8 +2005,8 @@ too_big:
 		checked for below. */
 		break;
 
-#if DICT_TF_FORMAT_ZIP != DICT_TF_FORMAT_MAX
-# error "DICT_TF_FORMAT_ZIP != DICT_TF_FORMAT_MAX"
+#if UNIV_FORMAT_B != UNIV_FORMAT_MAX
+# error "UNIV_FORMAT_B != UNIV_FORMAT_MAX"
 #endif
 	}
 
@@ -5085,7 +5085,7 @@ dict_ind_init(void)
 	dict_table_t*		table;
 
 	/* create dummy table and index for REDUNDANT infimum and supremum */
-	table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0);
+	table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0, 0);
 	dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
 			       DATA_ENGLISH | DATA_NOT_NULL, 8);
 
@@ -5097,7 +5097,8 @@ dict_ind_init(void)
 
 	/* create dummy table and index for COMPACT infimum and supremum */
 	table = dict_mem_table_create("SYS_DUMMY2",
-				      DICT_HDR_SPACE, 1, DICT_TF_COMPACT);
+				      DICT_HDR_SPACE, 1,
+				      DICT_TF_COMPACT, 0);
 	dict_mem_table_add_col(table, NULL, NULL, DATA_CHAR,
 			       DATA_ENGLISH | DATA_NOT_NULL, 8);
 	dict_ind_compact = dict_mem_index_create("SYS_DUMMY2", "SYS_DUMMY2",

=== modified file 'storage/innobase/dict/dict0load.c'
--- a/storage/innobase/dict/dict0load.c	2011-03-07 16:11:09 +0000
+++ b/storage/innobase/dict/dict0load.c	2011-03-23 13:43:14 +0000
@@ -605,22 +605,22 @@ dict_sys_tables_get_flags(
 	field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
 	n_cols = mach_read_from_4(field);
 
-	if (UNIV_UNLIKELY(!(n_cols & 0x80000000UL))) {
+	if (UNIV_UNLIKELY(!(n_cols & DICT_N_COLS_COMPACT))) {
 		/* New file formats require ROW_FORMAT=COMPACT. */
 		return(ULINT_UNDEFINED);
 	}
 
 	switch (flags & (DICT_TF_FORMAT_MASK | DICT_TF_COMPACT)) {
 	default:
-	case DICT_TF_FORMAT_51 << DICT_TF_FORMAT_SHIFT:
-	case DICT_TF_FORMAT_51 << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
+	case UNIV_FORMAT_A << DICT_TF_FORMAT_SHIFT:
+	case UNIV_FORMAT_A << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
 		/* flags should be DICT_TABLE_ORDINARY,
 		or DICT_TF_FORMAT_MASK should be nonzero. */
 		return(ULINT_UNDEFINED);
 
-	case DICT_TF_FORMAT_ZIP << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
-#if DICT_TF_FORMAT_MAX > DICT_TF_FORMAT_ZIP
-# error "missing case labels for DICT_TF_FORMAT_ZIP .. DICT_TF_FORMAT_MAX"
+	case UNIV_FORMAT_B << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
+#if UNIV_FORMAT_MAX > UNIV_FORMAT_B
+# error "missing case labels for UNIV_FORMAT_B .. UNIV_FORMAT_MAX"
 #endif
 		/* We support this format. */
 		break;
@@ -632,7 +632,7 @@ dict_sys_tables_get_flags(
 		return(ULINT_UNDEFINED);
 	}
 
-	if (UNIV_UNLIKELY(flags & (~0 << DICT_TF_BITS))) {
+	if (UNIV_UNLIKELY(flags & ~DICT_TF_BIT_MASK)) {
 		/* Some unused bits are set. */
 		return(ULINT_UNDEFINED);
 	}
@@ -746,7 +746,7 @@ loop:
 			ibool	is_temp;
 
 			field = rec_get_nth_field_old(rec, 4, &len);
-			if (0x80000000UL &  mach_read_from_4(field)) {
+			if (mach_read_from_4(field) & DICT_N_COLS_COMPACT) {
 				/* ROW_FORMAT=COMPACT: read the is_temp
 				flag from SYS_TABLES.MIX_LEN. */
 				field = rec_get_nth_field_old(rec, 7, &len);
@@ -1485,7 +1485,8 @@ dict_load_table_low(
 	ulint		len;
 	ulint		space;
 	ulint		n_cols;
-	ulint		flags;
+	ulint		flags = 0;
+	ulint		flags2 = 0;
 
 	if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
 		return("delete-marked record in SYS_TABLES");
@@ -1567,27 +1568,22 @@ err_len:
 				(ulong) flags);
 			return("incorrect flags in SYS_TABLES");
 		}
-	} else {
-		flags = 0;
 	}
 
 	/* The high-order bit of N_COLS is the "compact format" flag.
 	For tables in that format, MIX_LEN may hold additional flags. */
-	if (n_cols & 0x80000000UL) {
-		ulint	flags2;
-
+	if (n_cols & DICT_N_COLS_COMPACT) {
 		flags |= DICT_TF_COMPACT;
 
-		field = rec_get_nth_field_old(rec, 7, &len);
+		field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
 
 		if (UNIV_UNLIKELY(len != 4)) {
-
 			goto err_len;
 		}
 
 		flags2 = mach_read_from_4(field);
 
-		if (flags2 & (~0 << (DICT_TF2_BITS - DICT_TF2_SHIFT))) {
+		if (flags2 & ~DICT_TF2_BIT_MASK) {
 			ut_print_timestamp(stderr);
 			fputs("  InnoDB: Warning: table ", stderr);
 			ut_print_filename(stderr, name);
@@ -1596,15 +1592,13 @@ err_len:
 				" has unknown flags %lx.\n",
 				(ulong) flags2);
 
-			flags2 &= ~(~0 << (DICT_TF2_BITS - DICT_TF2_SHIFT));
+			flags2 &= DICT_TF2_BIT_MASK;
 		}
-
-		flags |= flags2 << DICT_TF2_SHIFT;
 	}
 
 	/* See if the tablespace is available. */
-	*table = dict_mem_table_create(name, space, n_cols & ~0x80000000UL,
-				       flags);
+	*table = dict_mem_table_create(
+		name, space, n_cols & ~DICT_N_COLS_COMPACT, flags, flags2);
 
 	field = rec_get_nth_field_old(rec, 3/*ID*/, &len);
 	ut_ad(len == 8); /* this was checked earlier */
@@ -1707,11 +1701,10 @@ err_exit:
 		/* The system tablespace is always available. */
 	} else if (!fil_space_for_table_exists_in_mem(
 			   table->space, name,
-			   (table->flags >> DICT_TF2_SHIFT)
-			   & DICT_TF2_TEMPORARY,
+			   table->flags2 & DICT_TF2_TEMPORARY,
 			   FALSE, FALSE)) {
 
-		if (table->flags & (DICT_TF2_TEMPORARY << DICT_TF2_SHIFT)) {
+		if (table->flags2 & DICT_TF2_TEMPORARY) {
 			/* Do not bother to retry opening temporary tables. */
 			table->ibd_file_missing = TRUE;
 		} else {
@@ -1727,7 +1720,7 @@ err_exit:
 			if (!fil_open_single_table_tablespace(
 				TRUE, table->space,
 				table->flags == DICT_TF_COMPACT ? 0 :
-				table->flags & ~(~0 << DICT_TF_BITS), name)) {
+				table->flags, name)) {
 				/* We failed to find a sensible
 				tablespace file */
 

=== modified file 'storage/innobase/dict/dict0mem.c'
--- a/storage/innobase/dict/dict0mem.c	2011-02-08 12:16:14 +0000
+++ b/storage/innobase/dict/dict0mem.c	2011-03-18 15:48:14 +0000
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
+Copyright (c) 1996, 2011, Oracle and/or its affiliates. All Rights Reserved.
 
 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
@@ -63,13 +63,15 @@ dict_mem_table_create(
 				ignored if the table is made a member of
 				a cluster */
 	ulint		n_cols,	/*!< in: number of columns */
-	ulint		flags)	/*!< in: table flags */
+	ulint		flags,	/*!< in: table flags */
+	ulint		flags2)	/*!< in: table flags2 */
 {
 	dict_table_t*	table;
 	mem_heap_t*	heap;
 
 	ut_ad(name);
-	ut_a(!(flags & (~0 << DICT_TF2_BITS)));
+	ut_a(!(flags & ~DICT_TF_BIT_MASK));
+	ut_a(!(flags2 & ~DICT_TF2_BIT_MASK));
 
 	heap = mem_heap_create(DICT_HEAP_SIZE);
 
@@ -78,6 +80,7 @@ dict_mem_table_create(
 	table->heap = heap;
 
 	table->flags = (unsigned int) flags;
+	table->flags2 = (unsigned int) flags2;
 	table->name = ut_malloc(strlen(name) + 1);
 	memcpy(table->name, name, strlen(name) + 1);
 	table->space = (unsigned int) space;

=== modified file 'storage/innobase/dict/dict0stats.c'
--- a/storage/innobase/dict/dict0stats.c	2011-03-15 14:33:31 +0000
+++ b/storage/innobase/dict/dict0stats.c	2011-03-18 12:49:38 +0000
@@ -1798,7 +1798,7 @@ dict_stats_fetch_index_stats_step(
 		switch (i) {
 		case 0: /* innodb.index_stats.index_name */
 
-			ut_a(dtype_get_mtype(type) == DATA_VARCHAR);
+			ut_a(dtype_get_mtype(type) == DATA_VARMYSQL);
 
 			/* search for index in table's indexes whose name
 			matches data; the fetched index name is in data,
@@ -1829,7 +1829,7 @@ dict_stats_fetch_index_stats_step(
 
 		case 1: /* innodb.index_stats.stat_name */
 
-			ut_a(dtype_get_mtype(type) == DATA_VARCHAR);
+			ut_a(dtype_get_mtype(type) == DATA_VARMYSQL);
 
 			ut_a(index != NULL);
 

=== modified file 'storage/innobase/fil/fil0fil.c'
--- a/storage/innobase/fil/fil0fil.c	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/fil/fil0fil.c	2011-03-24 12:24:08 +0000
@@ -1153,13 +1153,11 @@ fil_space_create(
 	fil_space_t*	space;
 
 	/* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
-	ROW_FORMAT=COMPACT
-	((table->flags & ~(~0 << DICT_TF_BITS)) == DICT_TF_COMPACT) and
+	ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 	ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
-	format, the tablespace flags should equal
-	(table->flags & ~(~0 << DICT_TF_BITS)). */
+	format, the tablespace flags should equal table->flags. */
 	ut_a(flags != DICT_TF_COMPACT);
-	ut_a(!(flags & (~0UL << DICT_TF_BITS)));
+	ut_a(!(flags & ~DICT_TF_BIT_MASK));
 
 try_again:
 	/*printf(
@@ -2697,13 +2695,11 @@ fil_create_new_single_table_tablespace(
 	ut_a(space_id < SRV_LOG_SPACE_FIRST_ID);
 	ut_a(size >= FIL_IBD_FILE_INITIAL_SIZE);
 	/* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
-	ROW_FORMAT=COMPACT
-	((table->flags & ~(~0 << DICT_TF_BITS)) == DICT_TF_COMPACT) and
+	ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 	ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
-	format, the tablespace flags should equal
-	(table->flags & ~(~0 << DICT_TF_BITS)). */
+	format, the tablespace flags should equal table->flags. */
 	ut_a(flags != DICT_TF_COMPACT);
-	ut_a(!(flags & (~0UL << DICT_TF_BITS)));
+	ut_a(!(flags & ~DICT_TF_BIT_MASK));
 
 	path = fil_make_ibd_name(tablename, is_temp);
 
@@ -2792,7 +2788,7 @@ error_exit2:
 		page_zip_des_t	page_zip;
 		ulint		zip_size;
 
-		zip_size = ((PAGE_ZIP_MIN_SIZE >> 1)
+		zip_size = ((UNIV_ZIP_SIZE_MIN >> 1)
 			    << ((flags & DICT_TF_ZSSIZE_MASK)
 				>> DICT_TF_ZSSIZE_SHIFT));
 
@@ -2959,7 +2955,7 @@ fil_reset_too_high_lsns(
 	fputs(".\n", stderr);
 
 	ut_a(ut_is_2pow(zip_size));
-	ut_a(zip_size <= UNIV_PAGE_SIZE);
+	ut_a(zip_size <= UNIV_ZIP_SIZE_MAX);
 
 	/* Loop through all the pages in the tablespace and reset the lsn and
 	the page checksum if necessary */
@@ -3072,13 +3068,11 @@ fil_open_single_table_tablespace(
 	filepath = fil_make_ibd_name(name, FALSE);
 
 	/* The tablespace flags (FSP_SPACE_FLAGS) should be 0 for
-	ROW_FORMAT=COMPACT
-	((table->flags & ~(~0 << DICT_TF_BITS)) == DICT_TF_COMPACT) and
+	ROW_FORMAT=COMPACT (table->flags == DICT_TF_COMPACT) and
 	ROW_FORMAT=REDUNDANT (table->flags == 0).  For any other
-	format, the tablespace flags should equal
-	(table->flags & ~(~0 << DICT_TF_BITS)). */
+	format, the tablespace flags should equal table->flags. */
 	ut_a(flags != DICT_TF_COMPACT);
-	ut_a(!(flags & (~0UL << DICT_TF_BITS)));
+	ut_a(!(flags & ~DICT_TF_BIT_MASK));
 
 	file = os_file_create_simple_no_error_handling(
 		innodb_file_data_key, filepath, OS_FILE_OPEN,
@@ -3131,8 +3125,7 @@ fil_open_single_table_tablespace(
 
 	ut_free(buf2);
 
-	if (UNIV_UNLIKELY(space_id != id
-			  || space_flags != (flags & ~(~0 << DICT_TF_BITS)))) {
+	if (UNIV_UNLIKELY(space_id != id || space_flags != flags)) {
 		ut_print_timestamp(stderr);
 
 		fputs("  InnoDB: Error: tablespace id and flags in file ",
@@ -4346,8 +4339,6 @@ fil_io(
 	ut_ad(recv_no_ibuf_operations || (type == OS_FILE_WRITE)
 	      || !ibuf_bitmap_page(zip_size, block_offset)
 	      || sync || is_log);
-	ut_ad(!ibuf_inside() || is_log || (type == OS_FILE_WRITE)
-	      || ibuf_page(space_id, zip_size, block_offset, NULL));
 # endif /* UNIV_LOG_DEBUG */
 	if (sync) {
 		mode = OS_AIO_SYNC;

=== modified file 'storage/innobase/fsp/fsp0fsp.c'
--- a/storage/innobase/fsp/fsp0fsp.c	2011-02-03 22:18:48 +0000
+++ b/storage/innobase/fsp/fsp0fsp.c	2011-03-23 13:43:14 +0000
@@ -365,8 +365,8 @@ fsp_get_space_header(
 	fsp_header_t*	header;
 
 	ut_ad(ut_is_2pow(zip_size));
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
-	ut_ad(!zip_size || zip_size >= PAGE_ZIP_MIN_SIZE);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
+	ut_ad(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
 	ut_ad(id || !zip_size);
 
 	block = buf_page_get(id, zip_size, 0, RW_X_LATCH, mtr);
@@ -656,8 +656,8 @@ xdes_calc_descriptor_page(
 		+ (UNIV_PAGE_SIZE / FSP_EXTENT_SIZE) * XDES_SIZE
 #  error
 # endif
-# if PAGE_ZIP_MIN_SIZE <= XDES_ARR_OFFSET \
-		+ (PAGE_ZIP_MIN_SIZE / FSP_EXTENT_SIZE) * XDES_SIZE
+# if UNIV_ZIP_SIZE_MIN <= XDES_ARR_OFFSET \
+		+ (UNIV_ZIP_SIZE_MIN / FSP_EXTENT_SIZE) * XDES_SIZE
 #  error
 # endif
 #endif /* !DOXYGEN */
@@ -1361,8 +1361,8 @@ fsp_fill_free_list(
 	zip_size = dict_table_flags_to_zip_size(
 		mach_read_from_4(FSP_SPACE_FLAGS + header));
 	ut_a(ut_is_2pow(zip_size));
-	ut_a(zip_size <= UNIV_PAGE_SIZE);
-	ut_a(!zip_size || zip_size >= PAGE_ZIP_MIN_SIZE);
+	ut_a(zip_size <= UNIV_ZIP_SIZE_MAX);
+	ut_a(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
 
 	if (space == 0 && srv_auto_extend_last_data_file
 	    && size < limit + FSP_EXTENT_SIZE * FSP_FREE_ADD) {
@@ -1456,8 +1456,8 @@ fsp_fill_free_list(
 #if UNIV_PAGE_SIZE % FSP_EXTENT_SIZE
 # error "UNIV_PAGE_SIZE % FSP_EXTENT_SIZE != 0"
 #endif
-#if PAGE_ZIP_MIN_SIZE % FSP_EXTENT_SIZE
-# error "PAGE_ZIP_MIN_SIZE % FSP_EXTENT_SIZE != 0"
+#if UNIV_ZIP_SIZE_MIN % FSP_EXTENT_SIZE
+# error "UNIV_ZIP_SIZE_MIN % FSP_EXTENT_SIZE != 0"
 #endif
 
 		if (UNIV_UNLIKELY(init_xdes)) {
@@ -3935,8 +3935,8 @@ fsp_validate(
 	latch = fil_space_get_latch(space, &flags);
 	zip_size = dict_table_flags_to_zip_size(flags);
 	ut_a(ut_is_2pow(zip_size));
-	ut_a(zip_size <= UNIV_PAGE_SIZE);
-	ut_a(!zip_size || zip_size >= PAGE_ZIP_MIN_SIZE);
+	ut_a(zip_size <= UNIV_ZIP_SIZE_MAX);
+	ut_a(!zip_size || zip_size >= UNIV_ZIP_SIZE_MIN);
 
 	/* Start first a mini-transaction mtr2 to lock out all other threads
 	from the fsp system */

=== modified file 'storage/innobase/handler/ha_innodb.cc'
--- a/storage/innobase/handler/ha_innodb.cc	2011-03-22 11:44:40 +0000
+++ b/storage/innobase/handler/ha_innodb.cc	2011-03-30 07:34:34 +0000
@@ -70,7 +70,6 @@ extern "C" {
 #include "fil0fil.h"
 #include "trx0xa.h"
 #include "row0merge.h"
-#include "thr0loc.h"
 #include "dict0boot.h"
 #include "dict0stats.h"
 #include "ha_prototypes.h"
@@ -284,7 +283,6 @@ static PSI_mutex_info all_innodb_mutexes
 	{&sync_thread_mutex_key, "sync_thread_mutex", 0},
 #  endif /* UNIV_SYNC_DEBUG */
 	{&trx_doublewrite_mutex_key, "trx_doublewrite_mutex", 0},
-	{&thr_local_mutex_key, "thr_local_mutex", 0},
 	{&trx_undo_mutex_key, "trx_undo_mutex", 0},
 	{&srv_sys_mutex_key, "srv_sys_mutex", 0},
 	{&lock_sys_mutex_key, "lock_mutex", 0},
@@ -2524,7 +2522,7 @@ mem_free_and_error:
 		format_id = innobase_file_format_name_lookup(
 			innobase_file_format_name);
 
-		if (format_id > DICT_TF_FORMAT_MAX) {
+		if (format_id > UNIV_FORMAT_MAX) {
 
 			sql_print_error("InnoDB: wrong innodb_file_format.");
 
@@ -2550,12 +2548,12 @@ mem_free_and_error:
 	if (!innobase_file_format_check) {
 
 		/* Set the value to disable checking. */
-		srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX + 1;
+		srv_max_file_format_at_startup = UNIV_FORMAT_MAX + 1;
 
 	} else {
 
 		/* Set the value to the lowest supported format. */
-		srv_max_file_format_at_startup = DICT_TF_FORMAT_MIN;
+		srv_max_file_format_at_startup = UNIV_FORMAT_MIN;
 	}
 
 	/* Did the user specify a format name that we support?
@@ -2569,7 +2567,7 @@ mem_free_and_error:
 				"should be any value up to %s or its "
 				"equivalent numeric id",
 				trx_sys_file_format_id_to_name(
-					DICT_TF_FORMAT_MAX));
+					UNIV_FORMAT_MAX));
 
 		goto mem_free_and_error;
 	}
@@ -3264,7 +3262,6 @@ innobase_close_connection(
 
 	innobase_rollback_trx(trx);
 
-	thr_local_free(trx->mysql_thread_id);
 	trx_free_for_mysql(trx);
 
 	DBUG_RETURN(0);
@@ -3294,16 +3291,16 @@ ha_innobase::get_row_type() const
 		ut_ad(flags & DICT_TF_COMPACT);
 
 		switch (flags & DICT_TF_FORMAT_MASK) {
-		case DICT_TF_FORMAT_51 << DICT_TF_FORMAT_SHIFT:
+		case UNIV_FORMAT_A << DICT_TF_FORMAT_SHIFT:
 			return(ROW_TYPE_COMPACT);
-		case DICT_TF_FORMAT_ZIP << DICT_TF_FORMAT_SHIFT:
+		case UNIV_FORMAT_B << DICT_TF_FORMAT_SHIFT:
 			if (flags & DICT_TF_ZSSIZE_MASK) {
 				return(ROW_TYPE_COMPRESSED);
 			} else {
 				return(ROW_TYPE_DYNAMIC);
 			}
-#if DICT_TF_FORMAT_ZIP != DICT_TF_FORMAT_MAX
-# error "DICT_TF_FORMAT_ZIP != DICT_TF_FORMAT_MAX"
+#if UNIV_FORMAT_B != UNIV_FORMAT_MAX
+# error "UNIV_FORMAT_B != UNIV_FORMAT_MAX"
 #endif
 		}
 	}
@@ -6664,7 +6661,8 @@ create_table_def(
 					an .ibd file for it (no .ibd extension
 					in the path, though); otherwise this
 					is NULL */
-	ulint		flags)		/*!< in: table flags */
+	ulint		flags,		/*!< in: table flags */
+	ulint		flags2)		/*!< in: table flags2 */
 {
 	Field*		field;
 	dict_table_t*	table;
@@ -6704,7 +6702,7 @@ create_table_def(
 	/* We pass 0 as the space id, and determine at a lower level the space
 	id where to store the table */
 
-	table = dict_mem_table_create(table_name, 0, n_cols, flags);
+	table = dict_mem_table_create(table_name, 0, n_cols, flags, flags2);
 
 	if (path_of_temp_table) {
 		table->dir_path_of_temp_table =
@@ -7023,7 +7021,7 @@ get_row_format_name(
 
 /** If file-format is Antelope, issue warning and set ret false */
 #define CHECK_ERROR_ROW_TYPE_NEEDS_GT_ANTELOPE			\
-	if (srv_file_format < DICT_TF_FORMAT_ZIP) {		\
+	if (srv_file_format < UNIV_FORMAT_B) {		\
 		push_warning_printf(				\
 			thd, MYSQL_ERROR::WARN_LEVEL_WARN,	\
 			ER_ILLEGAL_HA_CREATE_OPTION,		\
@@ -7081,7 +7079,7 @@ create_options_are_valid(
 					" innodb_file_per_table.");
 				ret = FALSE;
 			}
-			if (srv_file_format < DICT_TF_FORMAT_ZIP) {
+			if (srv_file_format < UNIV_FORMAT_B) {
 				push_warning(
 					thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 					ER_ILLEGAL_HA_CREATE_OPTION,
@@ -7179,7 +7177,8 @@ ha_innobase::create(
 	char		norm_name[FN_REFLEN];
 	THD*		thd = ha_thd();
 	ib_int64_t	auto_inc_value;
-	ulint		flags;
+	ulint		flags = 0;
+	ulint		flags2 = 0;
 	/* Cache the value of innodb_file_format, in case it is
 	modified by another thread while the table is being created. */
 	const ulint	file_format = srv_file_format;
@@ -7249,8 +7248,6 @@ ha_innobase::create(
 
 	/* Create the table definition in InnoDB */
 
-	flags = 0;
-
 	/* Validate create options if innodb_strict_mode is set. */
 	if (!create_options_are_valid(thd, form, create_info)) {
 		error = ER_ILLEGAL_HA_CREATE_OPTION;
@@ -7271,7 +7268,7 @@ ha_innobase::create(
 			if (key_block_size == ksize) {
 				flags = ssize << DICT_TF_ZSSIZE_SHIFT
 					| DICT_TF_COMPACT
-					| DICT_TF_FORMAT_ZIP
+					| UNIV_FORMAT_B
 					  << DICT_TF_FORMAT_SHIFT;
 				break;
 			}
@@ -7286,7 +7283,7 @@ ha_innobase::create(
 			flags = 0;
 		}
 
-		if (file_format < DICT_TF_FORMAT_ZIP) {
+		if (file_format < UNIV_FORMAT_B) {
 			push_warning(
 				thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 				ER_ILLEGAL_HA_CREATE_OPTION,
@@ -7335,7 +7332,7 @@ ha_innobase::create(
 			flags = (DICT_TF_ZSSIZE_MAX - 1)
 				<< DICT_TF_ZSSIZE_SHIFT
 				| DICT_TF_COMPACT
-				| DICT_TF_FORMAT_ZIP
+				| UNIV_FORMAT_B
 				<< DICT_TF_FORMAT_SHIFT;
 #if DICT_TF_ZSSIZE_MAX < 1
 # error "DICT_TF_ZSSIZE_MAX < 1"
@@ -7355,7 +7352,7 @@ ha_innobase::create(
 				"InnoDB: ROW_FORMAT=%s requires"
 				" innodb_file_per_table.",
 				get_row_format_name(row_format));
-		} else if (file_format < DICT_TF_FORMAT_ZIP) {
+		} else if (file_format < UNIV_FORMAT_B) {
 			push_warning_printf(
 				thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 				ER_ILLEGAL_HA_CREATE_OPTION,
@@ -7364,7 +7361,7 @@ ha_innobase::create(
 				get_row_format_name(row_format));
 		} else {
 			flags |= DICT_TF_COMPACT
-			         | (DICT_TF_FORMAT_ZIP
+			         | (UNIV_FORMAT_B
 			            << DICT_TF_FORMAT_SHIFT);
 			break;
 		}
@@ -7403,12 +7400,12 @@ ha_innobase::create(
 	}
 
 	if (create_info->options & HA_LEX_CREATE_TMP_TABLE) {
-		flags |= DICT_TF2_TEMPORARY << DICT_TF2_SHIFT;
+		flags2 |= DICT_TF2_TEMPORARY;
 	}
 
 	error = create_table_def(trx, form, norm_name,
 		create_info->options & HA_LEX_CREATE_TMP_TABLE ? name2 : NULL,
-		flags);
+		flags, flags2);
 
 	if (error) {
 		goto cleanup;
@@ -11072,13 +11069,13 @@ innobase_file_format_name_lookup(
 	/* Check for valid parse. */
 	if (*endp == '\0' && *format_name != '\0') {
 
-		if (format_id <= DICT_TF_FORMAT_MAX) {
+		if (format_id <= UNIV_FORMAT_MAX) {
 
 			return(format_id);
 		}
 	} else {
 
-		for (format_id = 0; format_id <= DICT_TF_FORMAT_MAX;
+		for (format_id = 0; format_id <= UNIV_FORMAT_MAX;
 		     format_id++) {
 			const char*	name;
 
@@ -11091,7 +11088,7 @@ innobase_file_format_name_lookup(
 		}
 	}
 
-	return(DICT_TF_FORMAT_MAX + 1);
+	return(UNIV_FORMAT_MAX + 1);
 }
 
 /************************************************************//**
@@ -11108,7 +11105,7 @@ innobase_file_format_validate_and_set(
 
 	format_id = innobase_file_format_name_lookup(format_max);
 
-	if (format_id < DICT_TF_FORMAT_MAX + 1) {
+	if (format_id < UNIV_FORMAT_MAX + 1) {
 		srv_max_file_format_at_startup = format_id;
 
 		return((int) format_id);
@@ -11147,7 +11144,7 @@ innodb_file_format_name_validate(
 		format_id = innobase_file_format_name_lookup(
 			file_format_input);
 
-		if (format_id <= DICT_TF_FORMAT_MAX) {
+		if (format_id <= UNIV_FORMAT_MAX) {
 
 			/* Save a pointer to the name in the
 			'file_format_name_map' constant array. */
@@ -11189,7 +11186,7 @@ innodb_file_format_name_update(
 
 		format_id = innobase_file_format_name_lookup(format_name);
 
-		if (format_id <= DICT_TF_FORMAT_MAX) {
+		if (format_id <= UNIV_FORMAT_MAX) {
 			srv_file_format = format_id;
 		}
 	}
@@ -11244,8 +11241,8 @@ innodb_file_format_max_validate(
 			  "InnoDB: invalid innodb_file_format_max "
 			  "value; can be any format up to %s "
 			  "or equivalent id of %d",
-			  trx_sys_file_format_id_to_name(DICT_TF_FORMAT_MAX),
-			  DICT_TF_FORMAT_MAX);
+			  trx_sys_file_format_id_to_name(UNIV_FORMAT_MAX),
+			  UNIV_FORMAT_MAX);
 		}
 	}
 
@@ -11284,7 +11281,7 @@ innodb_file_format_max_update(
 
 	format_id = innobase_file_format_name_lookup(format_name_in);
 
-	if (format_id > DICT_TF_FORMAT_MAX) {
+	if (format_id > UNIV_FORMAT_MAX) {
 		/* DEFAULT is "on", which is invalid at runtime. */
 		push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 				    ER_WRONG_ARGUMENTS,

=== modified file 'storage/innobase/handler/i_s.cc'
--- a/storage/innobase/handler/i_s.cc	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/handler/i_s.cc	2011-03-23 13:43:14 +0000
@@ -39,7 +39,7 @@ extern "C" {
 #include "btr0pcur.h"	/* for file sys_tables related info. */
 #include "btr0types.h"
 #include "buf0buddy.h"	/* for i_s_cmpmem */
-#include "buf0buf.h"	/* for buf_pool and PAGE_ZIP_MIN_SIZE */
+#include "buf0buf.h"	/* for buf_pool */
 #include "dict0load.h"	/* for file sys_tables related info. */
 #include "dict0mem.h"
 #include "dict0types.h"
@@ -1371,7 +1371,7 @@ i_s_cmp_fill_low(
 	for (uint i = 0; i < PAGE_ZIP_NUM_SSIZE - 1; i++) {
 		page_zip_stat_t*	zip_stat = &page_zip_stat[i];
 
-		table->field[0]->store(PAGE_ZIP_MIN_SIZE << i);
+		table->field[0]->store(UNIV_ZIP_SIZE_MIN << i);
 
 		/* The cumulated counts are not protected by any
 		mutex.  Thus, some operation in page0zip.c could
@@ -3190,8 +3190,9 @@ i_s_innodb_buffer_page_fill(
 			page_info->data_size));
 
 		OK(fields[IDX_BUFFER_PAGE_ZIP_SIZE]->store(
-			page_info->zip_ssize ?
-				 512 << page_info->zip_ssize : 0));
+			page_info->zip_ssize
+			? (UNIV_ZIP_SIZE_MIN >> 1) << page_info->zip_ssize
+			: 0));
 
 #if BUF_PAGE_STATE_BITS > 3
 # error "BUF_PAGE_STATE_BITS > 3, please ensure that all 1<<BUF_PAGE_STATE_BITS values are checked for"

=== modified file 'storage/innobase/ibuf/ibuf0ibuf.c'
--- a/storage/innobase/ibuf/ibuf0ibuf.c	2011-02-10 15:01:01 +0000
+++ b/storage/innobase/ibuf/ibuf0ibuf.c	2011-03-24 12:24:08 +0000
@@ -44,7 +44,6 @@ Created 7/19/1997 Heikki Tuuri
 #include "fsp0fsp.h"
 #include "trx0sys.h"
 #include "fil0fil.h"
-#include "thr0loc.h"
 #include "rem0rec.h"
 #include "btr0cur.h"
 #include "btr0pcur.h"
@@ -321,52 +320,43 @@ still physically like the index page eve
 dropped! So, there seems to be no problem. */
 
 /******************************************************************//**
-Sets the flag in the current OS thread local storage denoting that it is
+Sets the flag in the current mini-transaction record indicating we're
 inside an insert buffer routine. */
 UNIV_INLINE
 void
-ibuf_enter(void)
-/*============*/
+ibuf_enter(
+/*=======*/
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
 {
-	ibool*	ptr;
-
-	ptr = thr_local_get_in_ibuf_field();
-
-	ut_ad(*ptr == FALSE);
-
-	*ptr = TRUE;
+	ut_ad(!mtr->inside_ibuf);
+	mtr->inside_ibuf = TRUE;
 }
 
 /******************************************************************//**
-Sets the flag in the current OS thread local storage denoting that it is
+Sets the flag in the current mini-transaction record indicating we're
 exiting an insert buffer routine. */
 UNIV_INLINE
 void
-ibuf_exit(void)
-/*===========*/
+ibuf_exit(
+/*======*/
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
 {
-	ibool*	ptr;
-
-	ptr = thr_local_get_in_ibuf_field();
-
-	ut_ad(*ptr == TRUE);
-
-	*ptr = FALSE;
+	ut_ad(mtr->inside_ibuf);
+	mtr->inside_ibuf = FALSE;
 }
 
-/******************************************************************//**
-Returns TRUE if the current OS thread is performing an insert buffer
-routine.
-
-For instance, a read-ahead of non-ibuf pages is forbidden by threads
-that are executing an insert buffer routine.
-@return TRUE if inside an insert buffer routine */
-UNIV_INTERN
-ibool
-ibuf_inside(void)
-/*=============*/
+/**************************************************************//**
+Commits an insert buffer mini-transaction and sets the persistent
+cursor latch mode to BTR_NO_LATCHES, that is, detaches the cursor. */
+UNIV_INLINE
+void
+ibuf_btr_pcur_commit_specify_mtr(
+/*=============================*/
+	btr_pcur_t*	pcur,	/*!< in/out: persistent cursor */
+	mtr_t*		mtr)	/*!< in/out: mini-transaction */
 {
-	return(*thr_local_get_in_ibuf_field());
+	ut_d(ibuf_exit(mtr));
+	btr_pcur_commit_specify_mtr(pcur, mtr);
 }
 
 /******************************************************************//**
@@ -376,11 +366,11 @@ static
 page_t*
 ibuf_header_page_get(
 /*=================*/
-	mtr_t*	mtr)	/*!< in: mtr */
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
 {
 	buf_block_t*	block;
 
-	ut_ad(!ibuf_inside());
+	ut_ad(!ibuf_inside(mtr));
 
 	block = buf_page_get(
 		IBUF_SPACE_ID, 0, FSP_IBUF_HEADER_PAGE_NO, RW_X_LATCH, mtr);
@@ -401,7 +391,7 @@ ibuf_tree_root_get(
 	buf_block_t*	block;
 	page_t*		root;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(mutex_own(&ibuf_mutex));
 
 	mtr_x_lock(dict_index_get_lock(ibuf->index), mtr);
@@ -545,7 +535,7 @@ ibuf_init_at_db_start(void)
 
 	fseg_n_reserved_pages(header_page + IBUF_HEADER + IBUF_TREE_SEG_HEADER,
 			      &n_used, &mtr);
-	ibuf_enter();
+	ibuf_enter(&mtr);
 
 	ut_ad(n_used >= 2);
 
@@ -566,14 +556,12 @@ ibuf_init_at_db_start(void)
 	mutex_exit(&ibuf_mutex);
 
 	ibuf->empty = (page_get_n_recs(root) == 0);
-	mtr_commit(&mtr);
-
-	ibuf_exit();
+	ibuf_mtr_commit(&mtr);
 
 	heap = mem_heap_create(450);
 
 	/* Use old-style record format for the insert buffer. */
-	table = dict_mem_table_create(IBUF_TABLE_NAME, IBUF_SPACE_ID, 1, 0);
+	table = dict_mem_table_create(IBUF_TABLE_NAME, IBUF_SPACE_ID, 1, 0, 0);
 
 	dict_mem_table_add_col(table, heap, "DUMMY_COLUMN", DATA_BINARY, 0, 0);
 
@@ -608,11 +596,9 @@ ibuf_max_size_update(
 {
 	ulint	new_size = ((buf_pool_get_curr_size() / UNIV_PAGE_SIZE)
 			    * new_val) / 100;
-	ibuf_enter();
 	mutex_enter(&ibuf_mutex);
 	ibuf->max_size = new_size;
 	mutex_exit(&ibuf_mutex);
-	ibuf_exit();
 }
 
 
@@ -1248,19 +1234,30 @@ ibuf_page_low(
 	return(ret);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_rec_get_page_no(mtr,rec) ibuf_rec_get_page_no_func(mtr,rec)
+#else /* UNIV_DEBUG */
+# define ibuf_rec_get_page_no(mtr,rec) ibuf_rec_get_page_no_func(rec)
+#endif /* UNIV_DEBUG */
+
 /********************************************************************//**
 Returns the page number field of an ibuf record.
 @return	page number */
 static
 ulint
-ibuf_rec_get_page_no(
-/*=================*/
+ibuf_rec_get_page_no_func(
+/*======================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec)	/*!< in: ibuf record */
 {
 	const byte*	field;
 	ulint		len;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(rec_get_n_fields_old(rec) > 2);
 
 	field = rec_get_nth_field_old(rec, 1, &len);
@@ -1282,20 +1279,31 @@ ibuf_rec_get_page_no(
 	return(mach_read_from_4(field));
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_rec_get_space(mtr,rec) ibuf_rec_get_space_func(mtr,rec)
+#else /* UNIV_DEBUG */
+# define ibuf_rec_get_space(mtr,rec) ibuf_rec_get_space_func(rec)
+#endif /* UNIV_DEBUG */
+
 /********************************************************************//**
 Returns the space id field of an ibuf record. For < 4.1.x format records
 returns 0.
 @return	space id */
 static
 ulint
-ibuf_rec_get_space(
-/*===============*/
+ibuf_rec_get_space_func(
+/*====================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec)	/*!< in: ibuf record */
 {
 	const byte*	field;
 	ulint		len;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(rec_get_n_fields_old(rec) > 2);
 
 	field = rec_get_nth_field_old(rec, 1, &len);
@@ -1316,12 +1324,22 @@ ibuf_rec_get_space(
 	return(0);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_rec_get_info(mtr,rec,op,comp,info_len,counter)	\
+	ibuf_rec_get_info_func(mtr,rec,op,comp,info_len,counter)
+#else /* UNIV_DEBUG */
+# define ibuf_rec_get_info(mtr,rec,op,comp,info_len,counter)	\
+	ibuf_rec_get_info_func(rec,op,comp,info_len,counter)
+#endif
 /****************************************************************//**
 Get various information about an ibuf record in >= 4.1.x format. */
 static
 void
-ibuf_rec_get_info(
-/*==============*/
+ibuf_rec_get_info_func(
+/*===================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec,		/*!< in: ibuf record */
 	ibuf_op_t*	op,		/*!< out: operation type, or NULL */
 	ibool*		comp,		/*!< out: compact flag, or NULL */
@@ -1340,7 +1358,9 @@ ibuf_rec_get_info(
 	ulint		info_len_local;
 	ulint		counter_local;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
 	fields = rec_get_n_fields_old(rec);
 	ut_a(fields > 4);
 
@@ -1389,18 +1409,29 @@ ibuf_rec_get_info(
 	}
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_rec_get_op_type(mtr,rec) ibuf_rec_get_op_type_func(mtr,rec)
+#else /* UNIV_DEBUG */
+# define ibuf_rec_get_op_type(mtr,rec) ibuf_rec_get_op_type_func(rec)
+#endif
+
 /****************************************************************//**
 Returns the operation type field of an ibuf record.
 @return	operation type */
 static
 ibuf_op_t
-ibuf_rec_get_op_type(
-/*=================*/
+ibuf_rec_get_op_type_func(
+/*======================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec)	/*!< in: ibuf record */
 {
 	ulint		len;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(rec_get_n_fields_old(rec) > 2);
 
 	(void) rec_get_nth_field_old(rec, 1, &len);
@@ -1412,7 +1443,7 @@ ibuf_rec_get_op_type(
 	} else {
 		ibuf_op_t	op;
 
-		ibuf_rec_get_info(rec, &op, NULL, NULL, NULL);
+		ibuf_rec_get_info(mtr, rec, &op, NULL, NULL, NULL);
 
 		return(op);
 	}
@@ -1515,7 +1546,7 @@ ibuf_dummy_index_create(
 
 	table = dict_mem_table_create("IBUF_DUMMY",
 				      DICT_HDR_SPACE, n,
-				      comp ? DICT_TF_COMPACT : 0);
+				      comp ? DICT_TF_COMPACT : 0, 0);
 
 	index = dict_mem_index_create("IBUF_DUMMY", "IBUF_DUMMY",
 				      DICT_HDR_SPACE, 0, n);
@@ -1611,6 +1642,14 @@ ibuf_build_entry_pre_4_1_x(
 	return(tuple);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_build_entry_from_ibuf_rec(mtr,ibuf_rec,heap,pindex)	\
+	ibuf_build_entry_from_ibuf_rec_func(mtr,ibuf_rec,heap,pindex)
+#else /* UNIV_DEBUG */
+# define ibuf_build_entry_from_ibuf_rec(mtr,ibuf_rec,heap,pindex)	\
+	ibuf_build_entry_from_ibuf_rec_func(ibuf_rec,heap,pindex)
+#endif
+
 /*********************************************************************//**
 Builds the entry used to
 
@@ -1629,8 +1668,11 @@ hold a latch to the ibuf_rec page as lon
 @return own: entry to insert to a non-clustered index */
 static
 dtuple_t*
-ibuf_build_entry_from_ibuf_rec(
-/*===========================*/
+ibuf_build_entry_from_ibuf_rec_func(
+/*================================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	ibuf_rec,	/*!< in: record in an insert buffer */
 	mem_heap_t*	heap,		/*!< in: heap where built */
 	dict_index_t**	pindex)		/*!< out, own: dummy index that
@@ -1647,6 +1689,10 @@ ibuf_build_entry_from_ibuf_rec(
 	ulint		comp;
 	dict_index_t*	index;
 
+	ut_ad(mtr_memo_contains_page(mtr, ibuf_rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, ibuf_rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
+
 	data = rec_get_nth_field_old(ibuf_rec, 1, &len);
 
 	if (len > 1) {
@@ -1667,7 +1713,7 @@ ibuf_build_entry_from_ibuf_rec(
 
 	types = rec_get_nth_field_old(ibuf_rec, 3, &len);
 
-	ibuf_rec_get_info(ibuf_rec, NULL, &comp, &info_len, NULL);
+	ibuf_rec_get_info(mtr, ibuf_rec, NULL, &comp, &info_len, NULL);
 
 	index = ibuf_dummy_index_create(n_fields, comp);
 
@@ -1754,6 +1800,12 @@ ibuf_rec_get_size(
 	return(size);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_rec_get_volume(mtr,rec) ibuf_rec_get_volume_func(mtr,rec)
+#else /* UNIV_DEBUG */
+# define ibuf_rec_get_volume(mtr,rec) ibuf_rec_get_volume_func(rec)
+#endif
+
 /********************************************************************//**
 Returns the space taken by a stored non-clustered index entry if converted to
 an index record.
@@ -1761,8 +1813,11 @@ an index record.
 taken in the page directory */
 static
 ulint
-ibuf_rec_get_volume(
-/*================*/
+ibuf_rec_get_volume_func(
+/*=====================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	ibuf_rec)/*!< in: ibuf record */
 {
 	ulint		len;
@@ -1773,7 +1828,9 @@ ibuf_rec_get_volume(
 	ibool		pre_4_1;
 	ulint		comp;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, ibuf_rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, ibuf_rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(rec_get_n_fields_old(ibuf_rec) > 2);
 
 	data = rec_get_nth_field_old(ibuf_rec, 1, &len);
@@ -1801,7 +1858,7 @@ ibuf_rec_get_volume(
 
 		types = rec_get_nth_field_old(ibuf_rec, 3, &len);
 
-		ibuf_rec_get_info(ibuf_rec, &op, &comp, &info_len, NULL);
+		ibuf_rec_get_info(mtr, ibuf_rec, &op, &comp, &info_len, NULL);
 
 		if (op == IBUF_OP_DELETE_MARK || op == IBUF_OP_DELETE) {
 			/* Delete-marking a record doesn't take any
@@ -1818,7 +1875,7 @@ ibuf_rec_get_volume(
 			mem_heap_t*	heap = mem_heap_create(500);
 
 			entry = ibuf_build_entry_from_ibuf_rec(
-				ibuf_rec, heap, &dummy_index);
+				mtr, ibuf_rec, heap, &dummy_index);
 
 			volume = rec_get_converted_size(dummy_index, entry, 0);
 
@@ -2176,21 +2233,15 @@ ibuf_add_free_page(void)
 		mtr_commit(&mtr);
 
 		return(FALSE);
-	}
-
-	{
-		buf_block_t*	block;
-
-		block = buf_page_get(
+	} else {
+		buf_block_t*	block = buf_page_get(
 			IBUF_SPACE_ID, 0, page_no, RW_X_LATCH, &mtr);
-
 		buf_block_dbg_add_level(block, SYNC_TREE_NODE_NEW);
 
-
 		page = buf_block_get_frame(block);
 	}
 
-	ibuf_enter();
+	ibuf_enter(&mtr);
 
 	mutex_enter(&ibuf_mutex);
 
@@ -2218,9 +2269,7 @@ ibuf_add_free_page(void)
 	ibuf_bitmap_page_set_bits(
 		bitmap_page, page_no, zip_size, IBUF_BITMAP_IBUF, TRUE, &mtr);
 
-	mtr_commit(&mtr);
-
-	ibuf_exit();
+	ibuf_mtr_commit(&mtr);
 
 	return(TRUE);
 }
@@ -2252,7 +2301,7 @@ ibuf_remove_free_page(void)
 	header_page = ibuf_header_page_get(&mtr);
 
 	/* Prevent pessimistic inserts to insert buffer trees for a while */
-	ibuf_enter();
+	ibuf_enter(&mtr);
 	mutex_enter(&ibuf_pessimistic_insert_mutex);
 	mutex_enter(&ibuf_mutex);
 
@@ -2261,14 +2310,12 @@ ibuf_remove_free_page(void)
 		mutex_exit(&ibuf_mutex);
 		mutex_exit(&ibuf_pessimistic_insert_mutex);
 
-		ibuf_exit();
-
-		mtr_commit(&mtr);
+		ibuf_mtr_commit(&mtr);
 
 		return;
 	}
 
-	mtr_start(&mtr2);
+	ibuf_mtr_start(&mtr2);
 
 	root = ibuf_tree_root_get(&mtr2);
 
@@ -2281,9 +2328,8 @@ ibuf_remove_free_page(void)
 	because in fseg_free_page we access level 1 pages, and the root
 	is a level 2 page. */
 
-	mtr_commit(&mtr2);
-
-	ibuf_exit();
+	ibuf_mtr_commit(&mtr2);
+	ibuf_exit(&mtr);
 
 	/* Since pessimistic inserts were prevented, we know that the
 	page is still in the free list. NOTE that also deletes may take
@@ -2298,7 +2344,7 @@ ibuf_remove_free_page(void)
 	buf_page_reset_file_page_was_freed(IBUF_SPACE_ID, page_no);
 #endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
 
-	ibuf_enter();
+	ibuf_enter(&mtr);
 
 	mutex_enter(&ibuf_mutex);
 
@@ -2343,9 +2389,7 @@ ibuf_remove_free_page(void)
 #if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
 	buf_page_set_file_page_was_freed(IBUF_SPACE_ID, page_no);
 #endif /* UNIV_DEBUG_FILE_ACCESSES || UNIV_DEBUG */
-	mtr_commit(&mtr);
-
-	ibuf_exit();
+	ibuf_mtr_commit(&mtr);
 }
 
 /***********************************************************************//**
@@ -2367,8 +2411,6 @@ ibuf_free_excess_pages(void)
 	ut_ad(rw_lock_get_x_lock_count(
 		fil_space_get_latch(IBUF_SPACE_ID, NULL)) == 1);
 
-	ut_ad(!ibuf_inside());
-
 	/* NOTE: We require that the thread did not own the latch before,
 	because then we know that we can obey the correct latching order
 	for ibuf latches */
@@ -2399,20 +2441,30 @@ ibuf_free_excess_pages(void)
 	}
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_get_merge_page_nos(contract,rec,mtr,ids,vers,pages,n_stored) \
+	ibuf_get_merge_page_nos_func(contract,rec,mtr,ids,vers,pages,n_stored)
+#else /* UNIV_DEBUG */
+# define ibuf_get_merge_page_nos(contract,rec,mtr,ids,vers,pages,n_stored) \
+	ibuf_get_merge_page_nos_func(contract,rec,ids,vers,pages,n_stored)
+#endif /* UNIV_DEBUG */
+
 /*********************************************************************//**
 Reads page numbers from a leaf in an ibuf tree.
 @return a lower limit for the combined volume of records which will be
 merged */
 static
 ulint
-ibuf_get_merge_page_nos(
-/*====================*/
+ibuf_get_merge_page_nos_func(
+/*=========================*/
 	ibool		contract,/*!< in: TRUE if this function is called to
 				contract the tree, FALSE if this is called
 				when a single page becomes full and we look
 				if it pays to read also nearby pages */
-	rec_t*		rec,	/*!< in: record from which we read up and down
-				in the chain of records */
+	const rec_t*	rec,	/*!< in: insert buffer record */
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction holding rec */
+#endif /* UNIV_DEBUG */
 	ulint*		space_ids,/*!< in/out: space id's of the pages */
 	ib_int64_t*	space_versions,/*!< in/out: tablespace version
 				timestamps; used to prevent reading in old
@@ -2435,18 +2487,22 @@ ibuf_get_merge_page_nos(
 	ulint	limit;
 	ulint	n_pages;
 
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
+
 	*n_stored = 0;
 
 	limit = ut_min(IBUF_MAX_N_PAGES_MERGED, buf_pool_get_curr_size() / 4);
 
 	if (page_rec_is_supremum(rec)) {
 
-		rec = page_rec_get_prev(rec);
+		rec = page_rec_get_prev_const(rec);
 	}
 
 	if (page_rec_is_infimum(rec)) {
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
 	if (page_rec_is_supremum(rec)) {
@@ -2454,8 +2510,8 @@ ibuf_get_merge_page_nos(
 		return(0);
 	}
 
-	first_page_no = ibuf_rec_get_page_no(rec);
-	first_space_id = ibuf_rec_get_space(rec);
+	first_page_no = ibuf_rec_get_page_no(mtr, rec);
+	first_space_id = ibuf_rec_get_space(mtr, rec);
 	n_pages = 0;
 	prev_page_no = 0;
 	prev_space_id = 0;
@@ -2466,8 +2522,8 @@ ibuf_get_merge_page_nos(
 
 	while (!page_rec_is_infimum(rec) && UNIV_LIKELY(n_pages < limit)) {
 
-		rec_page_no = ibuf_rec_get_page_no(rec);
-		rec_space_id = ibuf_rec_get_space(rec);
+		rec_page_no = ibuf_rec_get_page_no(mtr, rec);
+		rec_space_id = ibuf_rec_get_space(mtr, rec);
 
 		if (rec_space_id != first_space_id
 		    || (rec_page_no / IBUF_MERGE_AREA)
@@ -2484,10 +2540,10 @@ ibuf_get_merge_page_nos(
 		prev_page_no = rec_page_no;
 		prev_space_id = rec_space_id;
 
-		rec = page_rec_get_prev(rec);
+		rec = page_rec_get_prev_const(rec);
 	}
 
-	rec = page_rec_get_next(rec);
+	rec = page_rec_get_next_const(rec);
 
 	/* At the loop start there is no prev page; we mark this with a pair
 	of space id, page no (0, 0) for which there can never be entries in
@@ -2505,8 +2561,8 @@ ibuf_get_merge_page_nos(
 			rec_page_no = 1;
 			rec_space_id = 0;
 		} else {
-			rec_page_no = ibuf_rec_get_page_no(rec);
-			rec_space_id = ibuf_rec_get_space(rec);
+			rec_page_no = ibuf_rec_get_page_no(mtr, rec);
+			rec_space_id = ibuf_rec_get_space(mtr, rec);
 			ut_ad(rec_page_no > IBUF_TREE_ROOT_PAGE_NO);
 		}
 
@@ -2517,9 +2573,9 @@ ibuf_get_merge_page_nos(
 		     || rec_page_no != prev_page_no)
 		    && (prev_space_id != 0 || prev_page_no != 0)) {
 
-			if ((prev_page_no == first_page_no
-			     && prev_space_id == first_space_id)
-			    || contract
+			if (contract
+			    || (prev_page_no == first_page_no
+				&& prev_space_id == first_space_id)
 			    || (volume_for_page
 				> ((IBUF_MERGE_THRESHOLD - 1)
 				   * 4 * UNIV_PAGE_SIZE
@@ -2552,14 +2608,14 @@ ibuf_get_merge_page_nos(
 			break;
 		}
 
-		rec_volume = ibuf_rec_get_volume(rec);
+		rec_volume = ibuf_rec_get_volume(mtr, rec);
 
 		volume_for_page += rec_volume;
 
 		prev_page_no = rec_page_no;
 		prev_space_id = rec_space_id;
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
 #ifdef UNIV_IBUF_DEBUG
@@ -2594,7 +2650,6 @@ ibuf_contract_ext(
 	mtr_t		mtr;
 
 	*n_pages = 0;
-	ut_ad(!ibuf_inside());
 
 	/* We perform a dirty read of ibuf->empty, without latching
 	the insert buffer root page. We trust this dirty read except
@@ -2606,9 +2661,7 @@ ibuf_contract_ext(
 		return(0);
 	}
 
-	mtr_start(&mtr);
-
-	ibuf_enter();
+	ibuf_mtr_start(&mtr);
 
 	/* Open a cursor to a randomly chosen leaf of the tree, at a random
 	position within the leaf */
@@ -2627,24 +2680,21 @@ ibuf_contract_ext(
 		ut_ad(page_get_page_no(btr_pcur_get_page(&pcur))
 		      == FSP_IBUF_TREE_ROOT_PAGE_NO);
 
-		ibuf_exit();
-
-		mtr_commit(&mtr);
+		ibuf_mtr_commit(&mtr);
 		btr_pcur_close(&pcur);
 
 		return(0);
 	}
 
-	sum_sizes = ibuf_get_merge_page_nos(TRUE, btr_pcur_get_rec(&pcur),
+	sum_sizes = ibuf_get_merge_page_nos(TRUE,
+					    btr_pcur_get_rec(&pcur), &mtr,
 					    space_ids, space_versions,
 					    page_nos, n_pages);
 #if 0 /* defined UNIV_IBUF_DEBUG */
 	fprintf(stderr, "Ibuf contract sync %lu pages %lu volume %lu\n",
 		sync, *n_pages, sum_sizes);
 #endif
-	ibuf_exit();
-
-	mtr_commit(&mtr);
+	ibuf_mtr_commit(&mtr);
 	btr_pcur_close(&pcur);
 
 	buf_read_ibuf_merge_pages(sync, space_ids, space_versions, page_nos,
@@ -2695,13 +2745,11 @@ ibuf_contract_in_background(
 		/* Caller has requested a full batch */
 		n_pages = PCT_IO(100);
 	} else {
-
-		ibuf_enter();
-		mutex_enter(&ibuf_mutex);
-
 		/* By default we do a batch of 5% of the io_capacity */
 		n_pages = PCT_IO(5);
 
+		mutex_enter(&ibuf_mutex);
+
 		/* If the ibuf->size is more than half the max_size
 		then we make more agreesive contraction.
 		+1 is to avoid division by zero. */
@@ -2712,7 +2760,6 @@ ibuf_contract_in_background(
 		}
 
 		mutex_exit(&ibuf_mutex);
-		ibuf_exit();
 	}
 
 	while (sum_pages < n_pages) {
@@ -2807,6 +2854,13 @@ ibuf_get_volume_buffered_hash(
 	return(TRUE);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_get_volume_buffered_count(mtr,rec,hash,size,n_recs)	\
+	ibuf_get_volume_buffered_count_func(mtr,rec,hash,size,n_recs)
+#else /* UNIV_DEBUG */
+# define ibuf_get_volume_buffered_count(mtr,rec,hash,size,n_recs)	\
+	ibuf_get_volume_buffered_count_func(rec,hash,size,n_recs)
+#endif
 /*********************************************************************//**
 Update the estimate of the number of records on a page, and
 get the space taken by merging the buffered record to the index page.
@@ -2814,8 +2868,11 @@ get the space taken by merging the buffe
 taken in the page directory */
 static
 ulint
-ibuf_get_volume_buffered_count(
-/*===========================*/
+ibuf_get_volume_buffered_count_func(
+/*================================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,	/*!< in: mini-transaction owning rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec,	/*!< in: insert buffer record */
 	ulint*		hash,	/*!< in/out: hash array */
 	ulint		size,	/*!< in: number of elements in hash array */
@@ -2825,9 +2882,13 @@ ibuf_get_volume_buffered_count(
 	ulint		len;
 	ibuf_op_t	ibuf_op;
 	const byte*	types;
-	ulint		n_fields	= rec_get_n_fields_old(rec);
+	ulint		n_fields;
 
-	ut_ad(ibuf_inside());
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
+	ut_ad(ibuf_inside(mtr));
+
+	n_fields = rec_get_n_fields_old(rec);
 	ut_ad(n_fields > 4);
 	n_fields -= 4;
 
@@ -2912,7 +2973,7 @@ get_volume_comp:
 		mem_heap_t*	heap = mem_heap_create(500);
 
 		entry = ibuf_build_entry_from_ibuf_rec(
-			rec, heap, &dummy_index);
+			mtr, rec, heap, &dummy_index);
 
 		volume = rec_get_converted_size(dummy_index, entry, 0);
 
@@ -2933,7 +2994,7 @@ static
 ulint
 ibuf_get_volume_buffered(
 /*=====================*/
-	btr_pcur_t*	pcur,	/*!< in: pcur positioned at a place in an
+	const btr_pcur_t*pcur,	/*!< in: pcur positioned at a place in an
 				insert buffer tree where we would insert an
 				entry for the index page whose number is
 				page_no, latch mode has to be BTR_MODIFY_PREV
@@ -2943,16 +3004,17 @@ ibuf_get_volume_buffered(
 	lint*		n_recs,	/*!< in/out: minimum number of records on the
 				page after the buffered changes have been
 				applied, or NULL to disable the counting */
-	mtr_t*		mtr)	/*!< in: mtr */
+	mtr_t*		mtr)	/*!< in: mini-transaction of pcur */
 {
-	ulint	volume;
-	rec_t*	rec;
-	page_t*	page;
-	ulint	prev_page_no;
-	page_t*	prev_page;
-	ulint	next_page_no;
-	page_t*	next_page;
-	ulint	hash_bitmap[128 / sizeof(ulint)]; /* bitmap of buffered recs */
+	ulint		volume;
+	const rec_t*	rec;
+	const page_t*	page;
+	ulint		prev_page_no;
+	const page_t*	prev_page;
+	ulint		next_page_no;
+	const page_t*	next_page;
+	/* bitmap of buffered recs */
+	ulint		hash_bitmap[128 / sizeof(ulint)];
 
 	ut_a(trx_sys_multiple_tablespace_format);
 
@@ -2973,26 +3035,22 @@ ibuf_get_volume_buffered(
 	ut_ad(page_validate(page, ibuf->index));
 
 	if (page_rec_is_supremum(rec)) {
-		rec = page_rec_get_prev(rec);
+		rec = page_rec_get_prev_const(rec);
 	}
 
-	for (;;) {
-		if (page_rec_is_infimum(rec)) {
-
-			break;
-		}
+	for (; !page_rec_is_infimum(rec);
+	     rec = page_rec_get_prev_const(rec)) {
+		ut_ad(page_align(rec) == page);
 
-		if (page_no != ibuf_rec_get_page_no(rec)
-		    || space != ibuf_rec_get_space(rec)) {
+		if (page_no != ibuf_rec_get_page_no(mtr, rec)
+		    || space != ibuf_rec_get_space(mtr, rec)) {
 
 			goto count_later;
 		}
 
 		volume += ibuf_get_volume_buffered_count(
-			rec, hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
-
-		rec = page_rec_get_prev(rec);
-		ut_ad(page_align(rec) == page);
+			mtr, rec,
+			hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
 	}
 
 	/* Look at the previous page */
@@ -3008,7 +3066,8 @@ ibuf_get_volume_buffered(
 		buf_block_t*	block;
 
 		block = buf_page_get(
-			IBUF_SPACE_ID, 0, prev_page_no, RW_X_LATCH, mtr);
+			IBUF_SPACE_ID, 0, prev_page_no, RW_X_LATCH,
+			mtr);
 
 		buf_block_dbg_add_level(block, SYNC_TREE_NODE);
 
@@ -3018,14 +3077,15 @@ ibuf_get_volume_buffered(
 	}
 
 #ifdef UNIV_BTR_DEBUG
-	ut_a(btr_page_get_next(prev_page, mtr)
-	     == page_get_page_no(page));
+	ut_a(btr_page_get_next(prev_page, mtr) == page_get_page_no(page));
 #endif /* UNIV_BTR_DEBUG */
 
 	rec = page_get_supremum_rec(prev_page);
-	rec = page_rec_get_prev(rec);
+	rec = page_rec_get_prev_const(rec);
+
+	for (;; rec = page_rec_get_prev_const(rec)) {
+		ut_ad(page_align(rec) == prev_page);
 
-	for (;;) {
 		if (page_rec_is_infimum(rec)) {
 
 			/* We cannot go to yet a previous page, because we
@@ -3035,42 +3095,35 @@ ibuf_get_volume_buffered(
 			return(UNIV_PAGE_SIZE);
 		}
 
-		if (page_no != ibuf_rec_get_page_no(rec)
-		    || space != ibuf_rec_get_space(rec)) {
+		if (page_no != ibuf_rec_get_page_no(mtr, rec)
+		    || space != ibuf_rec_get_space(mtr, rec)) {
 
 			goto count_later;
 		}
 
 		volume += ibuf_get_volume_buffered_count(
-			rec, hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
-
-		rec = page_rec_get_prev(rec);
-		ut_ad(page_align(rec) == prev_page);
+			mtr, rec,
+			hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
 	}
 
 count_later:
 	rec = btr_pcur_get_rec(pcur);
 
 	if (!page_rec_is_supremum(rec)) {
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
-	for (;;) {
-		if (page_rec_is_supremum(rec)) {
-
-			break;
-		}
-
-		if (page_no != ibuf_rec_get_page_no(rec)
-		    || space != ibuf_rec_get_space(rec)) {
+	for (; !page_rec_is_supremum(rec);
+	     rec = page_rec_get_next_const(rec)) {
+		if (page_no != ibuf_rec_get_page_no(mtr, rec)
+		    || space != ibuf_rec_get_space(mtr, rec)) {
 
 			return(volume);
 		}
 
 		volume += ibuf_get_volume_buffered_count(
-			rec, hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
-
-		rec = page_rec_get_next(rec);
+			mtr, rec,
+			hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
 	}
 
 	/* Look at the next page */
@@ -3086,7 +3139,8 @@ count_later:
 		buf_block_t*	block;
 
 		block = buf_page_get(
-			IBUF_SPACE_ID, 0, next_page_no, RW_X_LATCH, mtr);
+			IBUF_SPACE_ID, 0, next_page_no, RW_X_LATCH,
+			mtr);
 
 		buf_block_dbg_add_level(block, SYNC_TREE_NODE);
 
@@ -3100,9 +3154,11 @@ count_later:
 #endif /* UNIV_BTR_DEBUG */
 
 	rec = page_get_infimum_rec(next_page);
-	rec = page_rec_get_next(rec);
+	rec = page_rec_get_next_const(rec);
+
+	for (;; rec = page_rec_get_next_const(rec)) {
+		ut_ad(page_align(rec) == next_page);
 
-	for (;;) {
 		if (page_rec_is_supremum(rec)) {
 
 			/* We give up */
@@ -3110,17 +3166,15 @@ count_later:
 			return(UNIV_PAGE_SIZE);
 		}
 
-		if (page_no != ibuf_rec_get_page_no(rec)
-		    || space != ibuf_rec_get_space(rec)) {
+		if (page_no != ibuf_rec_get_page_no(mtr, rec)
+		    || space != ibuf_rec_get_space(mtr, rec)) {
 
 			return(volume);
 		}
 
 		volume += ibuf_get_volume_buffered_count(
-			rec, hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
-
-		rec = page_rec_get_next(rec);
-		ut_ad(page_align(rec) == next_page);
+			mtr, rec,
+			hash_bitmap, UT_ARR_SIZE(hash_bitmap), n_recs);
 	}
 }
 
@@ -3141,9 +3195,7 @@ ibuf_update_max_tablespace_id(void)
 
 	ut_a(!dict_table_is_comp(ibuf->index->table));
 
-	ibuf_enter();
-
-	mtr_start(&mtr);
+	ibuf_mtr_start(&mtr);
 
 	btr_pcur_open_at_index_side(
 		FALSE, ibuf->index, BTR_SEARCH_LEAF, &pcur, TRUE, &mtr);
@@ -3166,14 +3218,20 @@ ibuf_update_max_tablespace_id(void)
 		max_space_id = mach_read_from_4(field);
 	}
 
-	mtr_commit(&mtr);
-	ibuf_exit();
+	ibuf_mtr_commit(&mtr);
 
 	/* printf("Maximum space id in insert buffer %lu\n", max_space_id); */
 
 	fil_set_max_space_id_if_bigger(max_space_id);
 }
 
+#ifdef UNIV_DEBUG
+# define ibuf_get_entry_counter_low(mtr,rec,space,page_no)	\
+	ibuf_get_entry_counter_low_func(mtr,rec,space,page_no)
+#else /* UNIV_DEBUG */
+# define ibuf_get_entry_counter_low(mtr,rec,space,page_no)	\
+	ibuf_get_entry_counter_low_func(rec,space,page_no)
+#endif
 /****************************************************************//**
 Helper function for ibuf_set_entry_counter. Checks if rec is for (space,
 page_no), and if so, reads counter value from it and returns that + 1.
@@ -3181,8 +3239,11 @@ Otherwise, returns 0.
 @return	new counter value, or 0 */
 static
 ulint
-ibuf_get_entry_counter_low(
-/*=======================*/
+ibuf_get_entry_counter_low_func(
+/*============================*/
+#ifdef UNIV_DEBUG
+	mtr_t*		mtr,		/*!< in: mini-transaction of rec */
+#endif /* UNIV_DEBUG */
 	const rec_t*	rec,		/*!< in: insert buffer record */
 	ulint		space,		/*!< in: space id */
 	ulint		page_no)	/*!< in: page number */
@@ -3191,7 +3252,9 @@ ibuf_get_entry_counter_low(
 	const byte*	field;
 	ulint		len;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
+	ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX)
+	      || mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
 	ut_ad(rec_get_n_fields_old(rec) > 2);
 
 	field = rec_get_nth_field_old(rec, 1, &len);
@@ -3263,12 +3326,15 @@ ibuf_set_entry_counter(
 	ulint		counter = 0;
 
 	/* pcur points to either a user rec or to a page's infimum record. */
+	ut_ad(ibuf_inside(mtr));
+	ut_ad(mtr_memo_contains(mtr, btr_pcur_get_block(pcur),
+				MTR_MEMO_PAGE_X_FIX));
 	ut_ad(page_validate(btr_pcur_get_page(pcur), ibuf->index));
 
 	if (btr_pcur_is_on_user_rec(pcur)) {
 
 		counter = ibuf_get_entry_counter_low(
-			btr_pcur_get_rec(pcur), space, page_no);
+			mtr, btr_pcur_get_rec(pcur), space, page_no);
 
 		if (UNIV_UNLIKELY(counter == ULINT_UNDEFINED)) {
 			/* The record lacks a counter field.
@@ -3324,7 +3390,7 @@ ibuf_set_entry_counter(
 			ut_ad(page_rec_is_user_rec(rec));
 
 			counter = ibuf_get_entry_counter_low(
-				rec, space, page_no);
+				mtr, rec, space, page_no);
 
 			if (UNIV_UNLIKELY(counter == ULINT_UNDEFINED)) {
 				/* The record lacks a counter field.
@@ -3478,7 +3544,6 @@ ibuf_insert_low(
 
 	if (mode == BTR_MODIFY_TREE) {
 		for (;;) {
-			ibuf_enter();
 			mutex_enter(&ibuf_pessimistic_insert_mutex);
 			mutex_enter(&ibuf_mutex);
 
@@ -3489,7 +3554,6 @@ ibuf_insert_low(
 
 			mutex_exit(&ibuf_mutex);
 			mutex_exit(&ibuf_pessimistic_insert_mutex);
-			ibuf_exit();
 
 			if (UNIV_UNLIKELY(!ibuf_add_free_page())) {
 
@@ -3497,11 +3561,9 @@ ibuf_insert_low(
 				return(DB_STRONG_FAIL);
 			}
 		}
-	} else {
-		ibuf_enter();
 	}
 
-	mtr_start(&mtr);
+	ibuf_mtr_start(&mtr);
 
 	btr_pcur_open(ibuf->index, ibuf_entry, PAGE_CUR_LE, mode, &pcur, &mtr);
 	ut_ad(page_validate(btr_pcur_get_page(&pcur), ibuf->index));
@@ -3556,7 +3618,7 @@ fail_exit:
 #ifdef UNIV_IBUF_COUNT_DEBUG
 	ut_a((buffered == 0) || ibuf_count_get(space, page_no));
 #endif
-	mtr_start(&bitmap_mtr);
+	ibuf_mtr_start(&bitmap_mtr);
 
 	bitmap_page = ibuf_bitmap_get_map_page(space, page_no,
 					       zip_size, &bitmap_mtr);
@@ -3577,15 +3639,15 @@ fail_exit:
 		if (buffered + entry_size + page_dir_calc_reserved_space(1)
 		    > ibuf_index_page_calc_free_from_bits(zip_size, bits)) {
 			/* Release the bitmap page latch early. */
-			mtr_commit(&bitmap_mtr);
+			ibuf_mtr_commit(&bitmap_mtr);
 
 			/* It may not fit */
 			do_merge = TRUE;
 
-			ibuf_get_merge_page_nos(
-				FALSE, btr_pcur_get_rec(&pcur),
-				space_ids, space_versions,
-				page_nos, &n_stored);
+			ibuf_get_merge_page_nos(FALSE,
+						btr_pcur_get_rec(&pcur), &mtr,
+						space_ids, space_versions,
+						page_nos, &n_stored);
 
 			goto fail_exit;
 		}
@@ -3598,7 +3660,7 @@ fail_exit:
 	    && !ibuf_set_entry_counter(ibuf_entry, space, page_no, &pcur,
 				       mode == BTR_MODIFY_PREV, &mtr)) {
 bitmap_fail:
-		mtr_commit(&bitmap_mtr);
+		ibuf_mtr_commit(&bitmap_mtr);
 
 		goto fail_exit;
 	}
@@ -3616,7 +3678,7 @@ bitmap_fail:
 					  &bitmap_mtr);
 	}
 
-	mtr_commit(&bitmap_mtr);
+	ibuf_mtr_commit(&bitmap_mtr);
 
 	cursor = btr_pcur_get_btr_cur(&pcur);
 
@@ -3681,9 +3743,8 @@ func_exit:
 	}
 #endif
 
-	mtr_commit(&mtr);
+	ibuf_mtr_commit(&mtr);
 	btr_pcur_close(&pcur);
-	ibuf_exit();
 
 	mem_heap_free(heap);
 
@@ -3937,7 +3998,7 @@ ibuf_insert_to_index_page(
 	page_t*		page		= buf_block_get_frame(block);
 	rec_t*		rec;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(dtuple_check_typed(entry));
 	ut_ad(!buf_block_align(page)->is_hashed);
 
@@ -4084,7 +4145,7 @@ ibuf_set_del_mark(
 	page_cur_t	page_cur;
 	ulint		low_match;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(dtuple_check_typed(entry));
 
 	low_match = page_cur_search(
@@ -4141,7 +4202,7 @@ ibuf_delete(
 	page_cur_t	page_cur;
 	ulint		low_match;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(dtuple_check_typed(entry));
 
 	low_match = page_cur_search(
@@ -4229,7 +4290,7 @@ ibuf_restore_pos(
 		/* The tablespace has been dropped.  It is possible
 		that another thread has deleted the insert buffer
 		entry.  Do not complain. */
-		btr_pcur_commit_specify_mtr(pcur, mtr);
+		ibuf_btr_pcur_commit_specify_mtr(pcur, mtr);
 	} else {
 		fprintf(stderr,
 			"InnoDB: ERROR: Submit the output to"
@@ -4247,7 +4308,7 @@ ibuf_restore_pos(
 			      page_rec_get_next(btr_pcur_get_rec(pcur)));
 		fflush(stderr);
 
-		btr_pcur_commit_specify_mtr(pcur, mtr);
+		ibuf_btr_pcur_commit_specify_mtr(pcur, mtr);
 
 		fputs("InnoDB: Validating insert buffer tree:\n", stderr);
 		if (!btr_validate_index(ibuf->index, NULL)) {
@@ -4271,8 +4332,8 @@ ibool
 ibuf_delete_rec(
 /*============*/
 	ulint		space,	/*!< in: space id */
-	ulint		page_no,/*!< in: index page number where the record
-				should belong */
+	ulint		page_no,/*!< in: index page number that the record
+				should belong to */
 	btr_pcur_t*	pcur,	/*!< in: pcur positioned on the record to
 				delete, having latch mode BTR_MODIFY_LEAF */
 	const dtuple_t*	search_tuple,
@@ -4283,10 +4344,10 @@ ibuf_delete_rec(
 	page_t*		root;
 	ulint		err;
 
-	ut_ad(ibuf_inside());
+	ut_ad(ibuf_inside(mtr));
 	ut_ad(page_rec_is_user_rec(btr_pcur_get_rec(pcur)));
-	ut_ad(ibuf_rec_get_page_no(btr_pcur_get_rec(pcur)) == page_no);
-	ut_ad(ibuf_rec_get_space(btr_pcur_get_rec(pcur)) == space);
+	ut_ad(ibuf_rec_get_page_no(mtr, btr_pcur_get_rec(pcur)) == page_no);
+	ut_ad(ibuf_rec_get_space(mtr, btr_pcur_get_rec(pcur)) == space);
 
 	success = btr_cur_optimistic_delete(btr_pcur_get_btr_cur(pcur), mtr);
 
@@ -4319,22 +4380,22 @@ ibuf_delete_rec(
 	}
 
 	ut_ad(page_rec_is_user_rec(btr_pcur_get_rec(pcur)));
-	ut_ad(ibuf_rec_get_page_no(btr_pcur_get_rec(pcur)) == page_no);
-	ut_ad(ibuf_rec_get_space(btr_pcur_get_rec(pcur)) == space);
+	ut_ad(ibuf_rec_get_page_no(mtr, btr_pcur_get_rec(pcur)) == page_no);
+	ut_ad(ibuf_rec_get_space(mtr, btr_pcur_get_rec(pcur)) == space);
 
 	/* We have to resort to a pessimistic delete from ibuf */
 	btr_pcur_store_position(pcur, mtr);
+	ibuf_btr_pcur_commit_specify_mtr(pcur, mtr);
 
-	btr_pcur_commit_specify_mtr(pcur, mtr);
-
+	ibuf_mtr_start(mtr);
 	mutex_enter(&ibuf_mutex);
 
-	mtr_start(mtr);
-
 	if (!ibuf_restore_pos(space, page_no, search_tuple,
 			      BTR_MODIFY_TREE, pcur, mtr)) {
 
 		mutex_exit(&ibuf_mutex);
+		ut_ad(!ibuf_inside(mtr));
+		ut_ad(mtr->state == MTR_COMMITTED);
 		goto func_exit;
 	}
 
@@ -4351,9 +4412,11 @@ ibuf_delete_rec(
 	mutex_exit(&ibuf_mutex);
 
 	ibuf->empty = (page_get_n_recs(root) == 0);
-	btr_pcur_commit_specify_mtr(pcur, mtr);
+	ibuf_btr_pcur_commit_specify_mtr(pcur, mtr);
 
 func_exit:
+	ut_ad(!ibuf_inside(mtr));
+	ut_ad(mtr->state == MTR_COMMITTED);
 	btr_pcur_close(pcur);
 
 	return(TRUE);
@@ -4445,18 +4508,20 @@ ibuf_merge_or_delete_for_page(
 			update_ibuf_bitmap = FALSE;
 		} else {
 			page_t*	bitmap_page;
+			ulint	bitmap_bits;
 
-			mtr_start(&mtr);
+			ibuf_mtr_start(&mtr);
 
 			bitmap_page = ibuf_bitmap_get_map_page(
 				space, page_no, zip_size, &mtr);
+			bitmap_bits = ibuf_bitmap_page_get_bits(
+				bitmap_page, page_no, zip_size,
+				IBUF_BITMAP_BUFFERED, &mtr);
 
-			if (!ibuf_bitmap_page_get_bits(bitmap_page, page_no,
-						       zip_size,
-						       IBUF_BITMAP_BUFFERED,
-						       &mtr)) {
+			ibuf_mtr_commit(&mtr);
+
+			if (!bitmap_bits) {
 				/* No inserts buffered for this page */
-				mtr_commit(&mtr);
 
 				if (!tablespace_being_deleted) {
 					fil_decr_pending_ibuf_merges(space);
@@ -4464,7 +4529,6 @@ ibuf_merge_or_delete_for_page(
 
 				return;
 			}
-			mtr_commit(&mtr);
 		}
 	} else if (block
 		   && (ibuf_fixed_addr_page(space, zip_size, page_no)
@@ -4473,11 +4537,9 @@ ibuf_merge_or_delete_for_page(
 		return;
 	}
 
-	ibuf_enter();
-
 	heap = mem_heap_create(512);
 
-	if (!trx_sys_multiple_tablespace_format) {
+	if (UNIV_UNLIKELY(!trx_sys_multiple_tablespace_format)) {
 		ut_a(trx_doublewrite_must_reset_space_ids);
 		search_tuple = ibuf_search_tuple_build(space, page_no, heap);
 	} else {
@@ -4504,7 +4566,7 @@ ibuf_merge_or_delete_for_page(
 
 			ut_print_timestamp(stderr);
 
-			mtr_start(&mtr);
+			ibuf_mtr_start(&mtr);
 
 			fputs("  InnoDB: Dump of the ibuf bitmap page:\n",
 			      stderr);
@@ -4512,8 +4574,7 @@ ibuf_merge_or_delete_for_page(
 			bitmap_page = ibuf_bitmap_get_map_page(space, page_no,
 							       zip_size, &mtr);
 			buf_page_print(bitmap_page, 0);
-
-			mtr_commit(&mtr);
+			ibuf_mtr_commit(&mtr);
 
 			fputs("\nInnoDB: Dump of the page:\n", stderr);
 
@@ -4544,7 +4605,7 @@ ibuf_merge_or_delete_for_page(
 	memset(dops, 0, sizeof(dops));
 
 loop:
-	mtr_start(&mtr);
+	ibuf_mtr_start(&mtr);
 
 	if (block) {
 		ibool success;
@@ -4578,8 +4639,8 @@ loop:
 		rec = btr_pcur_get_rec(&pcur);
 
 		/* Check if the entry is for this index page */
-		if (ibuf_rec_get_page_no(rec) != page_no
-		    || ibuf_rec_get_space(rec) != space) {
+		if (ibuf_rec_get_page_no(&mtr, rec) != page_no
+		    || ibuf_rec_get_space(&mtr, rec) != space) {
 
 			if (block) {
 				page_header_reset_last_insert(
@@ -4602,7 +4663,7 @@ loop:
 			dtuple_t*	entry;
 			trx_id_t	max_trx_id;
 			dict_index_t*	dummy_index;
-			ibuf_op_t	op = ibuf_rec_get_op_type(rec);
+			ibuf_op_t	op = ibuf_rec_get_op_type(&mtr, rec);
 
 			max_trx_id = page_get_max_trx_id(page_align(rec));
 			page_update_max_trx_id(block, page_zip, max_trx_id,
@@ -4611,7 +4672,7 @@ loop:
 			ut_ad(page_validate(page_align(rec), ibuf->index));
 
 			entry = ibuf_build_entry_from_ibuf_rec(
-				rec, heap, &dummy_index);
+				&mtr, rec, heap, &dummy_index);
 
 			ut_ad(page_validate(block->frame, dummy_index));
 
@@ -4644,13 +4705,14 @@ loop:
 				Store and restore the cursor position. */
 				ut_ad(rec == btr_pcur_get_rec(&pcur));
 				ut_ad(page_rec_is_user_rec(rec));
-				ut_ad(ibuf_rec_get_page_no(rec) == page_no);
-				ut_ad(ibuf_rec_get_space(rec) == space);
+				ut_ad(ibuf_rec_get_page_no(&mtr, rec)
+				      == page_no);
+				ut_ad(ibuf_rec_get_space(&mtr, rec) == space);
 
 				btr_pcur_store_position(&pcur, &mtr);
-				btr_pcur_commit_specify_mtr(&pcur, &mtr);
+				ibuf_btr_pcur_commit_specify_mtr(&pcur, &mtr);
 
-				mtr_start(&mtr);
+				ibuf_mtr_start(&mtr);
 
 				success = buf_page_get_known_nowait(
 					RW_X_LATCH, block,
@@ -4665,7 +4727,8 @@ loop:
 						      BTR_MODIFY_LEAF,
 						      &pcur, &mtr)) {
 
-					mtr_commit(&mtr);
+					ut_ad(!ibuf_inside(&mtr));
+					ut_ad(mtr.state == MTR_COMMITTED);
 					mops[op]++;
 					ibuf_dummy_index_free(dummy_index);
 					goto loop;
@@ -4680,7 +4743,7 @@ loop:
 
 			ibuf_dummy_index_free(dummy_index);
 		} else {
-			dops[ibuf_rec_get_op_type(rec)]++;
+			dops[ibuf_rec_get_op_type(&mtr, rec)]++;
 		}
 
 		/* Delete the record from ibuf */
@@ -4691,7 +4754,7 @@ loop:
 
 			goto loop;
 		} else if (btr_pcur_is_after_last_on_page(&pcur)) {
-			mtr_commit(&mtr);
+			ibuf_mtr_commit(&mtr);
 			btr_pcur_close(&pcur);
 
 			goto loop;
@@ -4725,7 +4788,7 @@ reset_bit:
 		}
 	}
 
-	mtr_commit(&mtr);
+	ibuf_mtr_commit(&mtr);
 	btr_pcur_close(&pcur);
 	mem_heap_free(heap);
 
@@ -4749,8 +4812,6 @@ reset_bit:
 		fil_decr_pending_ibuf_merges(space);
 	}
 
-	ibuf_exit();
-
 #ifdef UNIV_IBUF_COUNT_DEBUG
 	ut_a(ibuf_count_get(space, page_no) == 0);
 #endif
@@ -4770,9 +4831,8 @@ ibuf_delete_for_discarded_space(
 	mem_heap_t*	heap;
 	btr_pcur_t	pcur;
 	dtuple_t*	search_tuple;
-	rec_t*		ibuf_rec;
+	const rec_t*	ibuf_rec;
 	ulint		page_no;
-	ibool		closed;
 	mtr_t		mtr;
 
 	/* Counts for discarded operations. */
@@ -4787,9 +4847,7 @@ ibuf_delete_for_discarded_space(
 
 	memset(dops, 0, sizeof(dops));
 loop:
-	ibuf_enter();
-
-	mtr_start(&mtr);
+	ibuf_mtr_start(&mtr);
 
 	/* Position pcur in the insert buffer at the first entry for the
 	space */
@@ -4809,39 +4867,34 @@ loop:
 		ibuf_rec = btr_pcur_get_rec(&pcur);
 
 		/* Check if the entry is for this space */
-		if (ibuf_rec_get_space(ibuf_rec) != space) {
+		if (ibuf_rec_get_space(&mtr, ibuf_rec) != space) {
 
 			goto leave_loop;
 		}
 
-		page_no = ibuf_rec_get_page_no(ibuf_rec);
+		page_no = ibuf_rec_get_page_no(&mtr, ibuf_rec);
 
-		dops[ibuf_rec_get_op_type(ibuf_rec)]++;
+		dops[ibuf_rec_get_op_type(&mtr, ibuf_rec)]++;
 
 		/* Delete the record from ibuf */
-		closed = ibuf_delete_rec(space, page_no, &pcur, search_tuple,
-					 &mtr);
-		if (closed) {
+		if (ibuf_delete_rec(space, page_no, &pcur, search_tuple,
+				    &mtr)) {
 			/* Deletion was pessimistic and mtr was committed:
 			we start from the beginning again */
 
-			ibuf_exit();
-
 			goto loop;
 		}
 
 		if (btr_pcur_is_after_last_on_page(&pcur)) {
-			mtr_commit(&mtr);
+			ibuf_mtr_commit(&mtr);
 			btr_pcur_close(&pcur);
 
-			ibuf_exit();
-
 			goto loop;
 		}
 	}
 
 leave_loop:
-	mtr_commit(&mtr);
+	ibuf_mtr_commit(&mtr);
 	btr_pcur_close(&pcur);
 
 #ifdef HAVE_ATOMIC_BUILTINS
@@ -4853,8 +4906,6 @@ leave_loop:
 	mutex_exit(&ibuf_mutex);
 #endif /* HAVE_ATOMIC_BUILTINS */
 
-	ibuf_exit();
-
 	mem_heap_free(heap);
 }
 
@@ -4870,18 +4921,15 @@ ibuf_is_empty(void)
 	const page_t*	root;
 	mtr_t		mtr;
 
-	ibuf_enter();
-	mtr_start(&mtr);
+	ibuf_mtr_start(&mtr);
 
 	mutex_enter(&ibuf_mutex);
 	root = ibuf_tree_root_get(&mtr);
 	mutex_exit(&ibuf_mutex);
 
 	is_empty = (page_get_n_recs(root) == 0);
-	mtr_commit(&mtr);
-	ibuf_exit();
-
 	ut_a(is_empty == ibuf->empty);
+	ibuf_mtr_commit(&mtr);
 
 	return(is_empty);
 }

=== modified file 'storage/innobase/include/btr0pcur.h'
--- a/storage/innobase/include/btr0pcur.h	2010-09-09 09:07:48 +0000
+++ b/storage/innobase/include/btr0pcur.h	2011-03-22 12:34:16 +0000
@@ -151,7 +151,7 @@ UNIV_INLINE
 ulint
 btr_pcur_get_up_match(
 /*==================*/
-	btr_pcur_t*	cursor); /*!< in: memory buffer for persistent cursor */
+	const btr_pcur_t*	cursor); /*!< in: persistent cursor */
 /**************************************************************//**
 Gets the low_match value for a pcur after a search.
 @return number of matched fields at the cursor or to the right if
@@ -160,7 +160,7 @@ UNIV_INLINE
 ulint
 btr_pcur_get_low_match(
 /*===================*/
-	btr_pcur_t*	cursor); /*!< in: memory buffer for persistent cursor */
+	const btr_pcur_t*	cursor); /*!< in: persistent cursor */
 /**************************************************************//**
 If mode is PAGE_CUR_G or PAGE_CUR_GE, opens a persistent cursor on the first
 user record satisfying the search condition, in the case PAGE_CUR_L or
@@ -265,22 +265,6 @@ ulint
 btr_pcur_get_rel_pos(
 /*=================*/
 	const btr_pcur_t*	cursor);/*!< in: persistent cursor */
-/*********************************************************//**
-Sets the mtr field for a pcur. */
-UNIV_INLINE
-void
-btr_pcur_set_mtr(
-/*=============*/
-	btr_pcur_t*	cursor,	/*!< in: persistent cursor */
-	mtr_t*		mtr);	/*!< in, own: mtr */
-/*********************************************************//**
-Gets the mtr field for a pcur.
-@return	mtr */
-UNIV_INLINE
-mtr_t*
-btr_pcur_get_mtr(
-/*=============*/
-	btr_pcur_t*	cursor);	/*!< in: persistent cursor */
 /**************************************************************//**
 Commits the mtr and sets the pcur latch mode to BTR_NO_LATCHES,
 that is, the cursor becomes detached. If there have been modifications
@@ -388,10 +372,6 @@ page_cur_t*
 btr_pcur_get_page_cur(
 /*==================*/
 	const btr_pcur_t*	cursor);	/*!< in: persistent cursor */
-#else /* UNIV_DEBUG */
-# define btr_pcur_get_btr_cur(cursor) (&(cursor)->btr_cur)
-# define btr_pcur_get_page_cur(cursor) (&(cursor)->btr_cur.page_cur)
-#endif /* UNIV_DEBUG */
 /*********************************************************//**
 Returns the page of a persistent cursor.
 @return	pointer to the page */
@@ -399,7 +379,7 @@ UNIV_INLINE
 page_t*
 btr_pcur_get_page(
 /*==============*/
-	btr_pcur_t*	cursor);/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor);/*!< in: persistent cursor */
 /*********************************************************//**
 Returns the buffer block of a persistent cursor.
 @return	pointer to the block */
@@ -407,7 +387,7 @@ UNIV_INLINE
 buf_block_t*
 btr_pcur_get_block(
 /*===============*/
-	btr_pcur_t*	cursor);/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor);/*!< in: persistent cursor */
 /*********************************************************//**
 Returns the record of a persistent cursor.
 @return	pointer to the record */
@@ -415,7 +395,14 @@ UNIV_INLINE
 rec_t*
 btr_pcur_get_rec(
 /*=============*/
-	btr_pcur_t*	cursor);/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor);/*!< in: persistent cursor */
+#else /* UNIV_DEBUG */
+# define btr_pcur_get_btr_cur(cursor) (&(cursor)->btr_cur)
+# define btr_pcur_get_page_cur(cursor) (&(cursor)->btr_cur.page_cur)
+# define btr_pcur_get_page(cursor) ((cursor)->btr_cur.page_cur.block->frame)
+# define btr_pcur_get_block(cursor) ((cursor)->btr_cur.page_cur.block)
+# define btr_pcur_get_rec(cursor) ((cursor)->btr_cur.page_cur.rec)
+#endif /* UNIV_DEBUG */
 /*********************************************************//**
 Checks if the persistent cursor is on a user record. */
 UNIV_INLINE
@@ -518,9 +505,6 @@ struct btr_pcur_struct{
 	/* NOTE that the following fields may possess dynamically allocated
 	memory which should be freed if not needed anymore! */
 
-	mtr_t*		mtr;		/*!< NULL, or this field may contain
-					a mini-transaction which holds the
-					latch on the cursor page */
 	byte*		old_rec_buf;	/*!< NULL, or a dynamically allocated
 					buffer for old_rec */
 	ulint		buf_size;	/*!< old_rec_buf size if old_rec_buf

=== modified file 'storage/innobase/include/btr0pcur.ic'
--- a/storage/innobase/include/btr0pcur.ic	2010-09-09 09:07:48 +0000
+++ b/storage/innobase/include/btr0pcur.ic	2011-03-22 12:34:16 +0000
@@ -42,34 +42,6 @@ btr_pcur_get_rel_pos(
 	return(cursor->rel_pos);
 }
 
-/*********************************************************//**
-Sets the mtr field for a pcur. */
-UNIV_INLINE
-void
-btr_pcur_set_mtr(
-/*=============*/
-	btr_pcur_t*	cursor,	/*!< in: persistent cursor */
-	mtr_t*		mtr)	/*!< in, own: mtr */
-{
-	ut_ad(cursor);
-
-	cursor->mtr = mtr;
-}
-
-/*********************************************************//**
-Gets the mtr field for a pcur.
-@return	mtr */
-UNIV_INLINE
-mtr_t*
-btr_pcur_get_mtr(
-/*=============*/
-	btr_pcur_t*	cursor)	/*!< in: persistent cursor */
-{
-	ut_ad(cursor);
-
-	return(cursor->mtr);
-}
-
 #ifdef UNIV_DEBUG
 /*********************************************************//**
 Returns the btr cursor component of a persistent cursor.
@@ -95,7 +67,7 @@ btr_pcur_get_page_cur(
 {
 	return(btr_cur_get_page_cur(btr_pcur_get_btr_cur(cursor)));
 }
-#endif /* UNIV_DEBUG */
+
 /*********************************************************//**
 Returns the page of a persistent cursor.
 @return	pointer to the page */
@@ -103,7 +75,7 @@ UNIV_INLINE
 page_t*
 btr_pcur_get_page(
 /*==============*/
-	btr_pcur_t*	cursor)	/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor)	/*!< in: persistent cursor */
 {
 	ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
 
@@ -117,7 +89,7 @@ UNIV_INLINE
 buf_block_t*
 btr_pcur_get_block(
 /*===============*/
-	btr_pcur_t*	cursor)	/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor)	/*!< in: persistent cursor */
 {
 	ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
 
@@ -131,13 +103,14 @@ UNIV_INLINE
 rec_t*
 btr_pcur_get_rec(
 /*=============*/
-	btr_pcur_t*	cursor)	/*!< in: persistent cursor */
+	const btr_pcur_t*	cursor)	/*!< in: persistent cursor */
 {
 	ut_ad(cursor->pos_state == BTR_PCUR_IS_POSITIONED);
 	ut_ad(cursor->latch_mode != BTR_NO_LATCHES);
 
 	return(btr_cur_get_rec(btr_pcur_get_btr_cur(cursor)));
 }
+#endif /* UNIV_DEBUG */
 
 /**************************************************************//**
 Gets the up_match value for a pcur after a search.
@@ -147,9 +120,9 @@ UNIV_INLINE
 ulint
 btr_pcur_get_up_match(
 /*==================*/
-	btr_pcur_t*	cursor) /*!< in: memory buffer for persistent cursor */
+	const btr_pcur_t*	cursor) /*!< in: persistent cursor */
 {
-	btr_cur_t*	btr_cursor;
+	const btr_cur_t*	btr_cursor;
 
 	ut_ad((cursor->pos_state == BTR_PCUR_WAS_POSITIONED)
 	      || (cursor->pos_state == BTR_PCUR_IS_POSITIONED));
@@ -169,9 +142,9 @@ UNIV_INLINE
 ulint
 btr_pcur_get_low_match(
 /*===================*/
-	btr_pcur_t*	cursor) /*!< in: memory buffer for persistent cursor */
+	const btr_pcur_t*	cursor) /*!< in: persistent cursor */
 {
-	btr_cur_t*	btr_cursor;
+	const btr_cur_t*	btr_cursor;
 
 	ut_ad((cursor->pos_state == BTR_PCUR_WAS_POSITIONED)
 	      || (cursor->pos_state == BTR_PCUR_IS_POSITIONED));

=== modified file 'storage/innobase/include/buf0buf.h'
--- a/storage/innobase/include/buf0buf.h	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/include/buf0buf.h	2011-03-23 13:43:14 +0000
@@ -1838,8 +1838,8 @@ struct buf_pool_struct{
 #if BUF_BUDDY_HIGH != UNIV_PAGE_SIZE
 # error "BUF_BUDDY_HIGH != UNIV_PAGE_SIZE"
 #endif
-#if BUF_BUDDY_LOW > PAGE_ZIP_MIN_SIZE
-# error "BUF_BUDDY_LOW > PAGE_ZIP_MIN_SIZE"
+#if BUF_BUDDY_LOW > UNIV_ZIP_SIZE_MIN
+# error "BUF_BUDDY_LOW > UNIV_ZIP_SIZE_MIN"
 #endif
 	/* @} */
 };

=== modified file 'storage/innobase/include/buf0buf.ic'
--- a/storage/innobase/include/buf0buf.ic	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/include/buf0buf.ic	2011-03-23 13:43:14 +0000
@@ -690,7 +690,8 @@ buf_page_get_zip_size(
 /*==================*/
 	const buf_page_t*	bpage)	/*!< in: pointer to the control block */
 {
-	return(bpage->zip.ssize ? 512 << bpage->zip.ssize : 0);
+	return(bpage->zip.ssize
+	       ? (UNIV_ZIP_SIZE_MIN >> 1) << bpage->zip.ssize : 0);
 }
 
 /*********************************************************************//**
@@ -702,7 +703,8 @@ buf_block_get_zip_size(
 /*===================*/
 	const buf_block_t*	block)	/*!< in: pointer to the control block */
 {
-	return(block->page.zip.ssize ? 512 << block->page.zip.ssize : 0);
+	return(block->page.zip.ssize
+	       ? (UNIV_ZIP_SIZE_MIN >> 1) << block->page.zip.ssize : 0);
 }
 
 #ifndef UNIV_HOTBACKUP

=== modified file 'storage/innobase/include/buf0flu.ic'
--- a/storage/innobase/include/buf0flu.ic	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/include/buf0flu.ic	2011-03-28 13:09:20 +0000
@@ -70,7 +70,7 @@ buf_flush_note_modification(
 
 	ut_ad(!buf_pool_mutex_own(buf_pool));
 	ut_ad(!buf_flush_list_mutex_own(buf_pool));
-	ut_ad(log_flush_order_mutex_own());
+	ut_ad(!mtr->made_dirty || log_flush_order_mutex_own());
 
 	ut_ad(mtr->start_lsn != 0);
 	ut_ad(mtr->modifications);
@@ -81,6 +81,8 @@ buf_flush_note_modification(
 	block->page.newest_modification = mtr->end_lsn;
 
 	if (!block->page.oldest_modification) {
+		ut_a(mtr->made_dirty);
+		ut_ad(log_flush_order_mutex_own());
 		buf_flush_insert_into_flush_list(
 			buf_pool, block, mtr->start_lsn);
 	} else {

=== modified file 'storage/innobase/include/buf0rea.h'
--- a/storage/innobase/include/buf0rea.h	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/include/buf0rea.h	2011-03-24 12:24:08 +0000
@@ -70,10 +70,10 @@ UNIV_INTERN
 ulint
 buf_read_ahead_linear(
 /*==================*/
-	ulint	space,	/*!< in: space id */
-	ulint	zip_size,/*!< in: compressed page size in bytes, or 0 */
-	ulint	offset);/*!< in: page number of a page; NOTE: the current thread
-			must want access to this page (see NOTE 3 above) */
+	ulint	space,		/*!< in: space id */
+	ulint	zip_size,	/*!< in: compressed page size in bytes, or 0 */
+	ulint	offset,		/*!< in: page number; see NOTE 3 above */
+	ibool	inside_ibuf);	/*!< in: TRUE if we are inside ibuf routine */
 /********************************************************************//**
 Issues read requests for pages which the ibuf module wants to read in, in
 order to contract the insert buffer tree. Technically, this function is like

=== modified file 'storage/innobase/include/dict0dict.ic'
--- a/storage/innobase/include/dict0dict.ic	2011-02-09 09:27:12 +0000
+++ b/storage/innobase/include/dict0dict.ic	2011-03-23 13:43:14 +0000
@@ -467,10 +467,10 @@ dict_table_flags_to_zip_size(
 	ulint	zip_size = flags & DICT_TF_ZSSIZE_MASK;
 
 	if (UNIV_UNLIKELY(zip_size)) {
-		zip_size = ((PAGE_ZIP_MIN_SIZE >> 1)
+		zip_size = ((UNIV_ZIP_SIZE_MIN >> 1)
 			 << (zip_size >> DICT_TF_ZSSIZE_SHIFT));
 
-		ut_ad(zip_size <= UNIV_PAGE_SIZE);
+		ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 	}
 
 	return(zip_size);

=== modified file 'storage/innobase/include/dict0mem.h'
--- a/storage/innobase/include/dict0mem.h	2011-02-09 09:27:12 +0000
+++ b/storage/innobase/include/dict0mem.h	2011-03-23 13:43:14 +0000
@@ -64,17 +64,17 @@ combination of types */
 
 /** Table flags.  All unused bits must be 0. */
 /* @{ */
-#define DICT_TF_COMPACT			1	/* Compact page format.
-						This must be set for
-						new file formats
-						(later than
-						DICT_TF_FORMAT_51). */
+/** Compact page format.
+This flag is set for all row formats later than Redundant.  It identifies
+the Compact row format.  It is also used for the later formats, Compressed
+and Dynamic, because they add features to the Compact structure. */
+#define DICT_TF_COMPACT			1
 
 /** Compressed page size (0=uncompressed, up to 15 compressed sizes) */
 /* @{ */
 #define DICT_TF_ZSSIZE_SHIFT		1
 #define DICT_TF_ZSSIZE_MASK		(15 << DICT_TF_ZSSIZE_SHIFT)
-#define DICT_TF_ZSSIZE_MAX (UNIV_PAGE_SIZE_SHIFT - PAGE_ZIP_MIN_SIZE_SHIFT + 1)
+#define DICT_TF_ZSSIZE_MAX (UNIV_PAGE_SIZE_SHIFT - UNIV_ZIP_SIZE_SHIFT_MIN + 1)
 /* @} */
 
 /** File format */
@@ -82,38 +82,29 @@ combination of types */
 #define DICT_TF_FORMAT_SHIFT		5	/* file format */
 #define DICT_TF_FORMAT_MASK		\
 ((~(~0 << (DICT_TF_BITS - DICT_TF_FORMAT_SHIFT))) << DICT_TF_FORMAT_SHIFT)
-#define DICT_TF_FORMAT_51		0	/*!< InnoDB/MySQL up to 5.1 */
-#define DICT_TF_FORMAT_ZIP		1	/*!< InnoDB plugin for 5.1:
-						compressed tables,
-						new BLOB treatment */
-/** Maximum supported file format */
-#define DICT_TF_FORMAT_MAX		DICT_TF_FORMAT_ZIP
-
-/** Minimum supported file format */
-#define DICT_TF_FORMAT_MIN		DICT_TF_FORMAT_51
+#define DICT_N_COLS_COMPACT	0x80000000UL	/*!< Set if ROW_FORMAT!=REDUNDANT */
 
 /* @} */
 #define DICT_TF_BITS			6	/*!< number of flag bits */
-#if (1 << (DICT_TF_BITS - DICT_TF_FORMAT_SHIFT)) <= DICT_TF_FORMAT_MAX
-# error "DICT_TF_BITS is insufficient for DICT_TF_FORMAT_MAX"
+#if (1 << (DICT_TF_BITS - DICT_TF_FORMAT_SHIFT)) <= UNIV_FORMAT_MAX
+# error "DICT_TF_BITS is insufficient for UNIV_FORMAT_MAX"
 #endif
+/** Valid table flag bits */
+#define DICT_TF_BIT_MASK		(~(~0 << DICT_TF_BITS))	
 /* @} */
 
-/** @brief Additional table flags.
+/** @brief Table Flags set number 2.
 
 These flags will be stored in SYS_TABLES.MIX_LEN.  All unused flags
 will be written as 0.  The column may contain garbage for tables
 created with old versions of InnoDB that only implemented
 ROW_FORMAT=REDUNDANT. */
 /* @{ */
-#define DICT_TF2_SHIFT			DICT_TF_BITS
-						/*!< Shift value for
-						table->flags. */
-#define DICT_TF2_TEMPORARY		1	/*!< TRUE for tables from
-						CREATE TEMPORARY TABLE. */
-#define DICT_TF2_BITS			(DICT_TF2_SHIFT + 1)
-						/*!< Total number of bits
-						in table->flags. */
+/** Total number of bits in table->flags2. */
+#define DICT_TF2_BITS		1
+#define DICT_TF2_BIT_MASK	~(~0 << DICT_TF2_BITS)
+
+#define DICT_TF2_TEMPORARY	(1<<0)	/*!< 1 if CREATE TEMPORARY TABLE */
 /* @} */
 
 /** Tables could be chained together with Foreign key constraint. When
@@ -145,7 +136,8 @@ dict_mem_table_create(
 					is ignored if the table is made
 					a member of a cluster */
 	ulint		n_cols,		/*!< in: number of columns */
-	ulint		flags);		/*!< in: table flags */
+	ulint		flags,		/*!< in: table flags */
+	ulint		flags2);	/*!< in: table flags2 */
 /****************************************************************//**
 Free a table memory object. */
 UNIV_INTERN
@@ -486,7 +478,8 @@ struct dict_table_struct{
 	unsigned	space:32;
 				/*!< space where the clustered index of the
 				table is placed */
-	unsigned	flags:DICT_TF2_BITS;/*!< DICT_TF_COMPACT, ... */
+	unsigned	flags:DICT_TF_BITS;	/*!< DICT_TF_... */
+	unsigned	flags2:DICT_TF2_BITS;	/*!< DICT_TF2_... */
 	unsigned	ibd_file_missing:1;
 				/*!< TRUE if this is in a single-table
 				tablespace and the .ibd file is missing; then

=== modified file 'storage/innobase/include/ibuf0ibuf.h'
--- a/storage/innobase/include/ibuf0ibuf.h	2011-02-10 15:01:01 +0000
+++ b/storage/innobase/include/ibuf0ibuf.h	2011-03-24 12:24:08 +0000
@@ -116,6 +116,22 @@ UNIV_INTERN
 void
 ibuf_update_max_tablespace_id(void);
 /*===============================*/
+/***************************************************************//**
+Starts an insert buffer mini-transaction. */
+UNIV_INLINE
+void
+ibuf_mtr_start(
+/*===========*/
+	mtr_t*	mtr)	/*!< out: mini-transaction */
+	__attribute__((nonnull));
+/***************************************************************//**
+Commits an insert buffer mini-transaction. */
+UNIV_INLINE
+void
+ibuf_mtr_commit(
+/*============*/
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
+	__attribute__((nonnull));
 /*********************************************************************//**
 Initializes an ibuf bitmap page. */
 UNIV_INTERN
@@ -236,10 +252,12 @@ routine.
 For instance, a read-ahead of non-ibuf pages is forbidden by threads
 that are executing an insert buffer routine.
 @return TRUE if inside an insert buffer routine */
-UNIV_INTERN
+UNIV_INLINE
 ibool
-ibuf_inside(void);
-/*=============*/
+ibuf_inside(
+/*========*/
+	const mtr_t*	mtr)	/*!< in: mini-transaction */
+	__attribute__((nonnull, pure));
 /***********************************************************************//**
 Checks if a page address is an ibuf bitmap page (level 3 page) address.
 @return	TRUE if a bitmap page */

=== modified file 'storage/innobase/include/ibuf0ibuf.ic'
--- a/storage/innobase/include/ibuf0ibuf.ic	2011-02-10 15:01:01 +0000
+++ b/storage/innobase/include/ibuf0ibuf.ic	2011-03-24 12:24:08 +0000
@@ -37,6 +37,30 @@ buffer inserts to this page.  If there i
 corresponding bits are set in the ibuf bitmap. */
 #define IBUF_PAGE_SIZE_PER_FREE_SPACE	32
 
+/***************************************************************//**
+Starts an insert buffer mini-transaction. */
+UNIV_INLINE
+void
+ibuf_mtr_start(
+/*===========*/
+	mtr_t*	mtr)	/*!< out: mini-transaction */
+{
+	mtr_start(mtr);
+	mtr->inside_ibuf = TRUE;
+}
+/***************************************************************//**
+Commits an insert buffer mini-transaction. */
+UNIV_INLINE
+void
+ibuf_mtr_commit(
+/*============*/
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
+{
+	ut_ad(mtr->inside_ibuf);
+	ut_d(mtr->inside_ibuf = FALSE);
+	mtr_commit(mtr);
+}
+
 /** Insert buffer struct */
 struct ibuf_struct{
 	ulint		size;		/*!< current size of the ibuf index
@@ -121,6 +145,22 @@ ibuf_should_try(
 	return(FALSE);
 }
 
+/******************************************************************//**
+Returns TRUE if the current OS thread is performing an insert buffer
+routine.
+
+For instance, a read-ahead of non-ibuf pages is forbidden by threads
+that are executing an insert buffer routine.
+@return TRUE if inside an insert buffer routine */
+UNIV_INLINE
+ibool
+ibuf_inside(
+/*========*/
+	const mtr_t*	mtr)	/*!< in: mini-transaction */
+{
+	return(mtr->inside_ibuf);
+}
+
 /***********************************************************************//**
 Checks if a page address is an ibuf bitmap page address.
 @return	TRUE if a bitmap page */
@@ -158,7 +198,7 @@ ibuf_index_page_calc_free_bits(
 	ulint	n;
 	ut_ad(ut_is_2pow(zip_size));
 	ut_ad(!zip_size || zip_size > IBUF_PAGE_SIZE_PER_FREE_SPACE);
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 
 	if (zip_size) {
 		n = max_ins_size
@@ -193,7 +233,7 @@ ibuf_index_page_calc_free_from_bits(
 	ut_ad(bits < 4);
 	ut_ad(ut_is_2pow(zip_size));
 	ut_ad(!zip_size || zip_size > IBUF_PAGE_SIZE_PER_FREE_SPACE);
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 
 	if (zip_size) {
 		if (bits == 3) {

=== modified file 'storage/innobase/include/mtr0mtr.h'
--- a/storage/innobase/include/mtr0mtr.h	2011-03-14 11:45:13 +0000
+++ b/storage/innobase/include/mtr0mtr.h	2011-03-28 13:09:20 +0000
@@ -193,21 +193,21 @@ functions).  The page number parameter w
 #include "log0log.h"
 
 /***************************************************************//**
-Starts a mini-transaction and creates a mini-transaction handle
-and buffer in the memory buffer given by the caller.
-@return	mtr buffer which also acts as the mtr handle */
+Starts a mini-transaction. */
 UNIV_INLINE
-mtr_t*
+void
 mtr_start(
 /*======*/
-	mtr_t*	mtr);	/*!< in: memory buffer for the mtr buffer */
+	mtr_t*	mtr)	/*!< out: mini-transaction */
+	__attribute__((nonnull));
 /***************************************************************//**
 Commits a mini-transaction. */
 UNIV_INTERN
 void
 mtr_commit(
 /*=======*/
-	mtr_t*	mtr);	/*!< in: mini-transaction */
+	mtr_t*	mtr)	/*!< in/out: mini-transaction */
+	__attribute__((nonnull));
 /**********************************************************//**
 Sets and returns a savepoint in mtr.
 @return	savepoint */
@@ -366,7 +366,6 @@ mtr_memo_push(
 	void*	object,	/*!< in: object */
 	ulint	type);	/*!< in: object type: MTR_MEMO_S_LOCK, ... */
 
-
 /* Type definition of a mini-transaction memo stack slot. */
 typedef	struct mtr_memo_slot_struct	mtr_memo_slot_t;
 struct mtr_memo_slot_struct{
@@ -381,9 +380,13 @@ struct mtr_struct{
 #endif
 	dyn_array_t	memo;	/*!< memo stack for locks etc. */
 	dyn_array_t	log;	/*!< mini-transaction log */
+	ibool		inside_ibuf;
+				/*!< TRUE if inside ibuf changes */
 	ibool		modifications;
 				/* TRUE if the mtr made modifications to
 				buffer pool pages */
+	ibool		made_dirty;/*!< TRUE if mtr has made at least
+				one buffer pool page dirty */
 	ulint		n_log_recs;
 				/* count of how many page initial log records
 				have been written to the mtr log */

=== modified file 'storage/innobase/include/mtr0mtr.ic'
--- a/storage/innobase/include/mtr0mtr.ic	2010-12-01 08:52:55 +0000
+++ b/storage/innobase/include/mtr0mtr.ic	2011-03-28 13:09:20 +0000
@@ -29,27 +29,35 @@ Created 11/26/1995 Heikki Tuuri
 #endif /* !UNIV_HOTBACKUP */
 #include "mach0data.h"
 
+/***************************************************//**
+Checks if a mini-transaction is dirtying a clean page.
+@return TRUE if the mtr is dirtying a clean page. */
+UNIV_INTERN
+ibool
+mtr_block_dirtied(
+/*==============*/
+	const buf_block_t*	block)	/*!< in: block being x-fixed */
+	__attribute__((nonnull,warn_unused_result));
+
 /***************************************************************//**
-Starts a mini-transaction and creates a mini-transaction handle
-and a buffer in the memory buffer given by the caller.
-@return	mtr buffer which also acts as the mtr handle */
+Starts a mini-transaction. */
 UNIV_INLINE
-mtr_t*
+void
 mtr_start(
 /*======*/
-	mtr_t*	mtr)	/*!< in: memory buffer for the mtr buffer */
+	mtr_t*	mtr)	/*!< out: mini-transaction */
 {
 	dyn_array_create(&(mtr->memo));
 	dyn_array_create(&(mtr->log));
 
 	mtr->log_mode = MTR_LOG_ALL;
 	mtr->modifications = FALSE;
+	mtr->inside_ibuf = FALSE;
 	mtr->n_log_recs = 0;
+	mtr->made_dirty = FALSE;
 
 	ut_d(mtr->state = MTR_ACTIVE);
 	ut_d(mtr->magic_n = MTR_MAGIC_N);
-
-	return(mtr);
 }
 
 /***************************************************//**
@@ -72,6 +80,15 @@ mtr_memo_push(
 	ut_ad(mtr->magic_n == MTR_MAGIC_N);
 	ut_ad(mtr->state == MTR_ACTIVE);
 
+	/* If this mtr has x-fixed a clean page then we set
+	the made_dirty flag. This tells us if we need to
+	grab log_flush_order_mutex at mtr_commit so that we
+	can insert the dirtied page to the flush list. */
+	if (type == MTR_MEMO_PAGE_X_FIX && !mtr->made_dirty) {
+		mtr->made_dirty =
+			mtr_block_dirtied((const buf_block_t *)object);
+	}
+
 	memo = &(mtr->memo);
 
 	slot = (mtr_memo_slot_t*) dyn_array_push(memo, sizeof *slot);

=== modified file 'storage/innobase/include/page0page.h'
--- a/storage/innobase/include/page0page.h	2010-07-29 10:44:35 +0000
+++ b/storage/innobase/include/page0page.h	2011-03-22 12:34:16 +0000
@@ -952,7 +952,7 @@ UNIV_INTERN
 ibool
 page_rec_validate(
 /*==============*/
-	rec_t*		rec,	/*!< in: physical record */
+	const rec_t*	rec,	/*!< in: physical record */
 	const ulint*	offsets);/*!< in: array returned by rec_get_offsets() */
 /***************************************************************//**
 Checks that the first directory slot points to the infimum record and
@@ -972,7 +972,7 @@ UNIV_INTERN
 ibool
 page_simple_validate_old(
 /*=====================*/
-	page_t*	page);	/*!< in: old-style index page */
+	const page_t*	page);	/*!< in: index page in ROW_FORMAT=REDUNDANT */
 /***************************************************************//**
 This function checks the consistency of an index page when we do not
 know the index. This is also resilient so that this should never crash
@@ -982,7 +982,7 @@ UNIV_INTERN
 ibool
 page_simple_validate_new(
 /*=====================*/
-	page_t*	block);	/*!< in: new-style index page */
+	const page_t*	page);	/*!< in: index page in ROW_FORMAT!=REDUNDANT */
 /***************************************************************//**
 This function checks the consistency of an index page.
 @return	TRUE if ok */
@@ -990,7 +990,7 @@ UNIV_INTERN
 ibool
 page_validate(
 /*==========*/
-	page_t*		page,	/*!< in: index page */
+	const page_t*	page,	/*!< in: index page */
 	dict_index_t*	index);	/*!< in: data dictionary index containing
 				the page record type definition */
 /***************************************************************//**

=== modified file 'storage/innobase/include/page0types.h'
--- a/storage/innobase/include/page0types.h	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/include/page0types.h	2011-03-23 13:43:14 +0000
@@ -49,13 +49,9 @@ page0*.h includes rem0rec.h and may incl
 /** Number of bits needed for representing different compressed page sizes */
 #define PAGE_ZIP_SSIZE_BITS 3
 
-/** log2 of smallest compressed page size */
-#define PAGE_ZIP_MIN_SIZE_SHIFT	10
-/** Smallest compressed page size */
-#define PAGE_ZIP_MIN_SIZE	(1 << PAGE_ZIP_MIN_SIZE_SHIFT)
-
 /** Number of supported compressed page sizes */
-#define PAGE_ZIP_NUM_SSIZE (UNIV_PAGE_SIZE_SHIFT - PAGE_ZIP_MIN_SIZE_SHIFT + 2)
+#define PAGE_ZIP_NUM_SSIZE	\
+	(UNIV_ZIP_SIZE_SHIFT_MAX - UNIV_ZIP_SIZE_SHIFT_MIN + 2)
 #if PAGE_ZIP_NUM_SSIZE > (1 << PAGE_ZIP_SSIZE_BITS)
 # error "PAGE_ZIP_NUM_SSIZE > (1 << PAGE_ZIP_SSIZE_BITS)"
 #endif
@@ -77,7 +73,7 @@ struct page_zip_des_struct
 	unsigned	ssize:PAGE_ZIP_SSIZE_BITS;
 					/*!< 0 or compressed page size;
 					the size in bytes is
-					PAGE_ZIP_MIN_SIZE << (ssize - 1). */
+					UNIV_ZIP_SIZE_MIN << (ssize - 1). */
 };
 
 /** Compression statistics for a given page size */

=== modified file 'storage/innobase/include/page0zip.ic'
--- a/storage/innobase/include/page0zip.ic	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/include/page0zip.ic	2011-03-23 13:43:14 +0000
@@ -124,9 +124,9 @@ page_zip_get_size(
 		return(0);
 	}
 
-	size = (PAGE_ZIP_MIN_SIZE >> 1) << page_zip->ssize;
+	size = (UNIV_ZIP_SIZE_MIN >> 1) << page_zip->ssize;
 
-	ut_ad(size >= PAGE_ZIP_MIN_SIZE);
+	ut_ad(size >= UNIV_ZIP_SIZE_MIN);
 	ut_ad(size <= UNIV_PAGE_SIZE);
 
 	return(size);

=== modified file 'storage/innobase/include/rem0rec.h'
--- a/storage/innobase/include/rem0rec.h	2010-10-21 22:25:09 +0000
+++ b/storage/innobase/include/rem0rec.h	2011-03-22 12:34:16 +0000
@@ -600,6 +600,7 @@ ulint
 rec_offs_size(
 /*==========*/
 	const ulint*	offsets);/*!< in: array returned by rec_get_offsets() */
+#ifdef UNIV_DEBUG
 /**********************************************************//**
 Returns a pointer to the start of the record.
 @return	pointer to start */
@@ -607,7 +608,7 @@ UNIV_INLINE
 byte*
 rec_get_start(
 /*==========*/
-	rec_t*		rec,	/*!< in: pointer to record */
+	const rec_t*	rec,	/*!< in: pointer to record */
 	const ulint*	offsets);/*!< in: array returned by rec_get_offsets() */
 /**********************************************************//**
 Returns a pointer to the end of the record.
@@ -616,8 +617,12 @@ UNIV_INLINE
 byte*
 rec_get_end(
 /*========*/
-	rec_t*		rec,	/*!< in: pointer to record */
+	const rec_t*	rec,	/*!< in: pointer to record */
 	const ulint*	offsets);/*!< in: array returned by rec_get_offsets() */
+#else /* UNIV_DEBUG */
+# define rec_get_start(rec, offsets) ((rec) - rec_offs_extra_size(offsets))
+# define rec_get_end(rec, offsets) ((rec) + rec_offs_data_size(offsets))
+#endif /* UNIV_DEBUG */
 /***************************************************************//**
 Copies a physical record to a buffer.
 @return	pointer to the origin of the copy */

=== modified file 'storage/innobase/include/rem0rec.ic'
--- a/storage/innobase/include/rem0rec.ic	2010-07-29 10:44:35 +0000
+++ b/storage/innobase/include/rem0rec.ic	2011-03-22 12:34:16 +0000
@@ -1462,6 +1462,7 @@ rec_offs_size(
 	return(rec_offs_data_size(offsets) + rec_offs_extra_size(offsets));
 }
 
+#ifdef UNIV_DEBUG
 /**********************************************************//**
 Returns a pointer to the end of the record.
 @return	pointer to end */
@@ -1469,11 +1470,11 @@ UNIV_INLINE
 byte*
 rec_get_end(
 /*========*/
-	rec_t*		rec,	/*!< in: pointer to record */
+	const rec_t*	rec,	/*!< in: pointer to record */
 	const ulint*	offsets)/*!< in: array returned by rec_get_offsets() */
 {
 	ut_ad(rec_offs_validate(rec, NULL, offsets));
-	return(rec + rec_offs_data_size(offsets));
+	return((rec_t*) rec + rec_offs_data_size(offsets));
 }
 
 /**********************************************************//**
@@ -1483,12 +1484,13 @@ UNIV_INLINE
 byte*
 rec_get_start(
 /*==========*/
-	rec_t*		rec,	/*!< in: pointer to record */
+	const rec_t*	rec,	/*!< in: pointer to record */
 	const ulint*	offsets)/*!< in: array returned by rec_get_offsets() */
 {
 	ut_ad(rec_offs_validate(rec, NULL, offsets));
-	return(rec - rec_offs_extra_size(offsets));
+	return((rec_t*) rec - rec_offs_extra_size(offsets));
 }
+#endif /* UNIV_DEBUG */
 
 /***************************************************************//**
 Copies a physical record to a buffer.

=== modified file 'storage/innobase/include/srv0srv.h'
--- a/storage/innobase/include/srv0srv.h	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/include/srv0srv.h	2011-03-23 13:46:37 +0000
@@ -109,7 +109,7 @@ extern ibool	srv_file_per_table;
 /** The file format to use on new *.ibd files. */
 extern ulint	srv_file_format;
 /** Whether to check file format during startup.  A value of
-DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
+UNIV_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
 set it to the highest format we support. */
 extern ulint	srv_max_file_format_at_startup;
 /** Place locks to records only i.e. do not use next-key locking except
@@ -575,27 +575,6 @@ srv_conc_exit_innodb(
 /*=================*/
 	trx_t*	trx);	/*!< in: transaction object associated with the
 			thread */
-/***************************************************************//**
-Puts a MySQL OS thread to wait for a lock to be released. If an error
-occurs during the wait trx->error_state associated with thr is
-!= DB_SUCCESS when we return. DB_LOCK_WAIT_TIMEOUT and DB_DEADLOCK
-are possible errors. DB_DEADLOCK is returned if selective deadlock
-resolution chose this transaction as a victim. */
-UNIV_INTERN
-void
-srv_suspend_mysql_thread(
-/*=====================*/
-	que_thr_t*	thr);	/*!< in: query thread associated with the MySQL
-				OS thread */
-/********************************************************************//**
-Releases a MySQL OS thread waiting for a lock to be released, if the
-thread is already suspended. */
-UNIV_INTERN
-void
-srv_release_mysql_thread_if_suspended(
-/*==================================*/
-	que_thr_t*	thr);	/*!< in: query thread associated with the
-				MySQL OS thread */
 /*********************************************************************//**
 A thread which prints the info output by various InnoDB monitors.
 @return	a dummy parameter */

=== modified file 'storage/innobase/include/sync0sync.h'
--- a/storage/innobase/include/sync0sync.h	2011-02-22 05:11:15 +0000
+++ b/storage/innobase/include/sync0sync.h	2011-03-24 12:24:08 +0000
@@ -108,7 +108,6 @@ extern mysql_pfs_key_t	syn_arr_mutex_key
 extern mysql_pfs_key_t	sync_thread_mutex_key;
 # endif /* UNIV_SYNC_DEBUG */
 extern mysql_pfs_key_t	trx_doublewrite_mutex_key;
-extern mysql_pfs_key_t	thr_local_mutex_key;
 extern mysql_pfs_key_t	trx_undo_mutex_key;
 extern mysql_pfs_key_t	trx_mutex_key;
 extern mysql_pfs_key_t	lock_sys_mutex_key;

=== removed file 'storage/innobase/include/thr0loc.h'
--- a/storage/innobase/include/thr0loc.h	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/include/thr0loc.h	1970-01-01 00:00:00 +0000
@@ -1,90 +0,0 @@
-/*****************************************************************************
-
-Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
-
-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; version 2 of the License.
-
-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
-
-*****************************************************************************/
-
-/**************************************************//**
-@file include/thr0loc.h
-The thread local storage
-
-Created 10/5/1995 Heikki Tuuri
-*******************************************************/
-
-/* This module implements storage private to each thread,
-a capability useful in some situations like storing the
-OS handle to the current thread, or its priority. */
-
-#ifndef thr0loc_h
-#define thr0loc_h
-
-#include "univ.i"
-#include "os0thread.h"
-
-/****************************************************************//**
-Initializes the thread local storage module. */
-UNIV_INTERN
-void
-thr_local_init(void);
-/*================*/
- /****************************************************************//**
-Close the thread local storage module. */
-UNIV_INTERN
-void
-thr_local_close(void);
-/*=================*/
-/*******************************************************************//**
-Creates a local storage struct for the calling new thread. */
-UNIV_INTERN
-void
-thr_local_create(void);
-/*==================*/
-/*******************************************************************//**
-Frees the local storage struct for the specified thread. */
-UNIV_INTERN
-void
-thr_local_free(
-/*===========*/
-	os_thread_id_t	id);	/*!< in: thread id */
-/*******************************************************************//**
-Gets the slot number in the thread table of a thread.
-@return	slot number */
-UNIV_INTERN
-ulint
-thr_local_get_slot_no(
-/*==================*/
-	os_thread_id_t	id);	/*!< in: thread id of the thread */
-/*******************************************************************//**
-Sets in the local storage the slot number in the thread table of a thread. */
-UNIV_INTERN
-void
-thr_local_set_slot_no(
-/*==================*/
-	os_thread_id_t	id,	/*!< in: thread id of the thread */
-	ulint		slot_no);/*!< in: slot number */
-/*******************************************************************//**
-Returns pointer to the 'in_ibuf' field within the current thread local
-storage.
-@return	pointer to the in_ibuf field */
-UNIV_INTERN
-ibool*
-thr_local_get_in_ibuf_field(void);
-/*=============================*/
-
-#ifndef UNIV_NONINL
-#include "thr0loc.ic"
-#endif
-
-#endif

=== removed file 'storage/innobase/include/thr0loc.ic'
--- a/storage/innobase/include/thr0loc.ic	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/include/thr0loc.ic	1970-01-01 00:00:00 +0000
@@ -1,24 +0,0 @@
-/*****************************************************************************
-
-Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
-
-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; version 2 of the License.
-
-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
-
-*****************************************************************************/
-
-/**************************************************//**
-@file include/thr0loc.ic
-Thread local storage
-
-Created 10/4/1995 Heikki Tuuri
-*******************************************************/

=== modified file 'storage/innobase/include/univ.i'
--- a/storage/innobase/include/univ.i	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/include/univ.i	2011-03-23 13:43:14 +0000
@@ -308,25 +308,58 @@ management to ensure correct alignment f
 			========================
 */
 
-/* The 2-logarithm of UNIV_PAGE_SIZE: */
+/** Antelope File Format: InnoDB/MySQL up to 5.1.
+This format includes REDUNDANT and COMPACT row formats */
+#define UNIV_FORMAT_A		0
+
+/** Barracuda File Format: Introduced in InnoDB plugin for 5.1:
+This format includes COMPRESSED and DYNAMIC row formats,
+including new BLOB treatment */
+#define UNIV_FORMAT_B		1
+
+/** Minimum supported file format */
+#define UNIV_FORMAT_MIN		UNIV_FORMAT_A
+
+/** Maximum supported file format */
+#define UNIV_FORMAT_MAX		UNIV_FORMAT_B
+
+/** The 2-logarithm of UNIV_PAGE_SIZE: */
 #define UNIV_PAGE_SIZE_SHIFT	14
-/* The universal page size of the database */
+
+/** The universal page size of the database */
 #define UNIV_PAGE_SIZE		(1 << UNIV_PAGE_SIZE_SHIFT)
 
-/* Maximum number of parallel threads in a parallelized operation */
+/** log2 of smallest compressed page size (1<<10 == 1024 bytes) */
+#define UNIV_ZIP_SIZE_SHIFT_MIN	10
+
+/** log2 of largest compressed page size (1<<14 == 16384 bytes).
+A compressed page directory entry reserves 14 bits for the start offset
+and 2 bits for flags. This limits the uncompressed page size to 16k.
+So even though a 16k uncompressed page can theoretically be compressed
+into a larger compressed page, it is not a useful feature so we will
+limit both with this same constant. */
+#define UNIV_ZIP_SIZE_SHIFT_MAX	14
+
+/** Smallest compressed page size */
+#define UNIV_ZIP_SIZE_MIN	(1 << UNIV_ZIP_SIZE_SHIFT_MIN)
+
+/** Largest compressed page size */
+#define UNIV_ZIP_SIZE_MAX	(1 << UNIV_ZIP_SIZE_SHIFT_MAX)
+
+/** Maximum number of parallel threads in a parallelized operation */
 #define UNIV_MAX_PARALLELISM	32
 
-/* The maximum length of a table name. This is the MySQL limit and is
+/** The maximum length of a table name. This is the MySQL limit and is
 defined in mysql_com.h like NAME_CHAR_LEN*SYSTEM_CHARSET_MBMAXLEN, the
 number does not include a terminating '\0'. InnoDB probably can handle
 longer names internally */
 #define MAX_TABLE_NAME_LEN	192
 
-/* The maximum length of a database name. Like MAX_TABLE_NAME_LEN this is
+/** The maximum length of a database name. Like MAX_TABLE_NAME_LEN this is
 the MySQL's NAME_LEN, see check_and_convert_db_name(). */
 #define MAX_DATABASE_NAME_LEN	MAX_TABLE_NAME_LEN
 
-/* MAX_FULL_NAME_LEN defines the full name path including the
+/** MAX_FULL_NAME_LEN defines the full name path including the
 database name and table name. In addition, 14 bytes is added for:
 	2 for surrounding quotes around table name
 	1 for the separating dot (.)
@@ -371,7 +404,7 @@ typedef long int		lint;
 typedef __int64			ib_int64_t;
 typedef unsigned __int64	ib_uint64_t;
 #elif !defined(UNIV_HOTBACKUP)
-/* Note: longlong and ulonglong come from MySQL headers. */
+/** Note: longlong and ulonglong come from MySQL headers. */
 typedef longlong		ib_int64_t;
 typedef ulonglong		ib_uint64_t;
 #endif
@@ -386,31 +419,31 @@ typedef unsigned long long int	ullint;
 #endif
 #endif
 
-/* The 'undefined' value for a ulint */
+/** The 'undefined' value for a ulint */
 #define ULINT_UNDEFINED		((ulint)(-1))
 
-/* The 'undefined' value for a ib_uint64_t */
+/** The 'undefined' value for a ib_uint64_t */
 #define UINT64_UNDEFINED	((ib_uint64_t)(-1))
 
 /** The bitmask of 32-bit unsigned integer */
 #define ULINT32_MASK		0xFFFFFFFF
-/* The undefined 32-bit unsigned integer */
+/** The undefined 32-bit unsigned integer */
 #define	ULINT32_UNDEFINED	ULINT32_MASK
 
-/* Maximum value for a ulint */
+/** Maximum value for a ulint */
 #define ULINT_MAX		((ulint)(-2))
 
-/* Maximum value for ib_uint64_t */
+/** Maximum value for ib_uint64_t */
 #define IB_ULONGLONG_MAX	((ib_uint64_t) (~0ULL))
 #define IB_UINT64_MAX		IB_ULONGLONG_MAX
 
 /** The generic InnoDB system object identifier data type */
 typedef ib_uint64_t	ib_id_t;
 
-/* The 'undefined' value for a ullint */
+/** The 'undefined' value for a ullint */
 #define ULLINT_UNDEFINED        ((ullint)(-1))
 
-/* This 'ibool' type is used within Innobase. Remember that different included
+/** This 'ibool' type is used within Innobase. Remember that different included
 headers may define 'bool' differently. Do not assume that 'bool' is a ulint! */
 #define ibool			ulint
 
@@ -421,7 +454,7 @@ headers may define 'bool' differently. D
 
 #endif
 
-/* The following number as the length of a logical field means that the field
+/** The following number as the length of a logical field means that the field
 has the SQL NULL as its value. NOTE that because we assume that the length
 of a field is a 32-bit integer when we store it, for example, to an undo log
 on disk, we must have also this number fit in 32 bits, also in 64-bit
@@ -429,7 +462,7 @@ computers! */
 
 #define UNIV_SQL_NULL ULINT32_UNDEFINED
 
-/* Lengths which are not UNIV_SQL_NULL, but bigger than the following
+/** Lengths which are not UNIV_SQL_NULL, but bigger than the following
 number indicate that a field contains a reference to an externally
 stored part of the field in the tablespace. The length field then
 contains the sum of the following flag and the locally stored len. */

=== modified file 'storage/innobase/lock/lock0lock.c'
--- a/storage/innobase/lock/lock0lock.c	2011-03-04 01:47:48 +0000
+++ b/storage/innobase/lock/lock0lock.c	2011-03-18 13:58:55 +0000
@@ -2071,13 +2071,7 @@ lock_rec_lock_fast(
 
 			status = LOCK_REC_FAIL;
 		} else if (!impl) {
-			/* If the nth bit of the record lock is already
-			set then we do not set a new lock bit, otherwise
-			we do set */
-
-			if (!lock_rec_get_nth_bit(lock, heap_no)) {
-				lock_rec_set_nth_bit(lock, heap_no);
-			}
+			lock_rec_set_nth_bit(lock, heap_no);
 
 			status = LOCK_REC_SUCCESS_CREATED;
 		}
@@ -4246,10 +4240,11 @@ lock_rec_unlock(
 	const rec_t*		rec,	/*!< in: record */
 	enum lock_mode		lock_mode)/*!< in: LOCK_S or LOCK_X */
 {
-	lock_t*	first_lock;
-	lock_t*	lock;
-	ulint	heap_no;
-	size_t	stmt_len;
+	lock_t*		first_lock;
+	lock_t*		lock;
+	ulint		heap_no;
+	const char*	stmt;
+	size_t		stmt_len;
 
 	ut_ad(trx);
 	ut_ad(rec);
@@ -4277,15 +4272,15 @@ lock_rec_unlock(
 	lock_mutex_exit();
 	trx_mutex_exit(trx);
 
-	/* Ignore stmt_len, because it should not hurt to see more
-	context (the tail of a multi-statement) in the error message. */
+	stmt = innobase_get_stmt(trx->mysql_thd, &stmt_len);
 	ut_print_timestamp(stderr);
 	fprintf(stderr,
-		"  InnoDB: Error: unlock row could not"
-		" find a %lu mode lock on the record\n"
-		"InnoDB: current statement: %s\n",
-		(ulong) lock_mode,
-		innobase_get_stmt(trx->mysql_thd, &stmt_len));
+		" InnoDB: Error: unlock row could not"
+		" find a %lu mode lock on the record\n",
+		(ulong) lock_mode);
+	ut_print_timestamp(stderr);
+	fprintf(stderr, " InnoDB: current statement: %.*s\n",
+		(int) stmt_len, stmt);
 
 	return;
 

=== modified file 'storage/innobase/mtr/mtr0log.c'
--- a/storage/innobase/mtr/mtr0log.c	2011-01-25 10:51:35 +0000
+++ b/storage/innobase/mtr/mtr0log.c	2011-03-18 15:48:14 +0000
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
+Copyright (c) 1995, 2011, Oracle and/or its affiliates. All Rights Reserved.
 
 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
@@ -563,7 +563,7 @@ mlog_parse_index(
 		n = n_uniq = 1;
 	}
 	table = dict_mem_table_create("LOG_DUMMY", DICT_HDR_SPACE, n,
-				      comp ? DICT_TF_COMPACT : 0);
+				      comp ? DICT_TF_COMPACT : 0, 0);
 	ind = dict_mem_index_create("LOG_DUMMY", "LOG_DUMMY",
 				    DICT_HDR_SPACE, 0, n);
 	ind->table = table;

=== modified file 'storage/innobase/mtr/mtr0mtr.c'
--- a/storage/innobase/mtr/mtr0mtr.c	2011-03-15 16:01:03 +0000
+++ b/storage/innobase/mtr/mtr0mtr.c	2011-03-28 13:09:20 +0000
@@ -37,6 +37,25 @@ Created 11/26/1995 Heikki Tuuri
 
 #ifndef UNIV_HOTBACKUP
 # include "log0recv.h"
+
+/***************************************************//**
+Checks if a mini-transaction is dirtying a clean page.
+@return TRUE if the mtr is dirtying a clean page. */
+UNIV_INTERN
+ibool
+mtr_block_dirtied(
+/*==============*/
+	const buf_block_t*	block)	/*!< in: block being x-fixed */
+{
+	ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
+	ut_ad(block->page.buf_fix_count > 0);
+
+	/* It is OK to read oldest_modification because no
+	other thread can be performing a write of it and it
+	is only during write that the value is reset to 0. */
+	return(block->page.oldest_modification == 0);
+}
+
 /*****************************************************************//**
 Releases the item in the slot given. */
 static
@@ -128,7 +147,7 @@ mtr_memo_slot_note_modification(
 	if (slot->object != NULL && slot->type == MTR_MEMO_PAGE_X_FIX) {
 		buf_block_t*	block = (buf_block_t*) slot->object;
 
-		ut_ad(log_flush_order_mutex_own());
+		ut_ad(!mtr->made_dirty || log_flush_order_mutex_own());
 		buf_flush_note_modification(block, mtr);
 	}
 }
@@ -227,7 +246,15 @@ mtr_log_reserve_and_write(
 	mtr->end_lsn = log_close();
 
 func_exit:
-	log_flush_order_mutex_enter();
+
+	/* No need to acquire log_flush_order_mutex if this mtr has
+	not dirtied a clean page. log_flush_order_mutex is used to
+	ensure ordered insertions in the flush_list. We need to
+	insert in the flush_list iff the page in question was clean
+	before modifications. */
+	if (mtr->made_dirty) {
+		log_flush_order_mutex_enter();
+	}
 
 	/* It is now safe to release the log mutex because the
 	flush_order mutex will ensure that we are the first one
@@ -238,7 +265,9 @@ func_exit:
 		mtr_memo_note_modifications(mtr);
 	}
 
-	log_flush_order_mutex_exit();
+	if (mtr->made_dirty) {
+		log_flush_order_mutex_exit();
+	}
 }
 #endif /* !UNIV_HOTBACKUP */
 
@@ -253,6 +282,7 @@ mtr_commit(
 	ut_ad(mtr);
 	ut_ad(mtr->magic_n == MTR_MAGIC_N);
 	ut_ad(mtr->state == MTR_ACTIVE);
+	ut_ad(!mtr->inside_ibuf);
 	ut_d(mtr->state = MTR_COMMITTING);
 
 #ifndef UNIV_HOTBACKUP

=== modified file 'storage/innobase/page/page0page.c'
--- a/storage/innobase/page/page0page.c	2011-02-08 12:16:14 +0000
+++ b/storage/innobase/page/page0page.c	2011-03-22 12:34:16 +0000
@@ -166,11 +166,11 @@ static
 ibool
 page_dir_slot_check(
 /*================*/
-	page_dir_slot_t*	slot)	/*!< in: slot */
+	const page_dir_slot_t*	slot)	/*!< in: slot */
 {
-	page_t*	page;
-	ulint	n_slots;
-	ulint	n_owned;
+	const page_t*	page;
+	ulint		n_slots;
+	ulint		n_owned;
 
 	ut_a(slot);
 
@@ -1803,12 +1803,12 @@ UNIV_INTERN
 ibool
 page_rec_validate(
 /*==============*/
-	rec_t*		rec,	/*!< in: physical record */
+	const rec_t*	rec,	/*!< in: physical record */
 	const ulint*	offsets)/*!< in: array returned by rec_get_offsets() */
 {
-	ulint	n_owned;
-	ulint	heap_no;
-	page_t*	page;
+	ulint		n_owned;
+	ulint		heap_no;
+	const page_t*	page;
 
 	page = page_align(rec);
 	ut_a(!page_is_comp(page) == !rec_offs_comp(offsets));
@@ -1889,16 +1889,16 @@ UNIV_INTERN
 ibool
 page_simple_validate_old(
 /*=====================*/
-	page_t*	page)	/*!< in: old-style index page */
+	const page_t*	page)	/*!< in: index page in ROW_FORMAT=REDUNDANT */
 {
-	page_dir_slot_t* slot;
-	ulint		slot_no;
-	ulint		n_slots;
-	rec_t*		rec;
-	byte*		rec_heap_top;
-	ulint		count;
-	ulint		own_count;
-	ibool		ret	= FALSE;
+	const page_dir_slot_t*	slot;
+	ulint			slot_no;
+	ulint			n_slots;
+	const rec_t*		rec;
+	const byte*		rec_heap_top;
+	ulint			count;
+	ulint			own_count;
+	ibool			ret	= FALSE;
 
 	ut_a(!page_is_comp(page));
 
@@ -2011,7 +2011,7 @@ page_simple_validate_old(
 			goto func_exit;
 		}
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 		own_count++;
 	}
 
@@ -2072,7 +2072,7 @@ page_simple_validate_old(
 			goto func_exit;
 		}
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
 	if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {
@@ -2099,16 +2099,16 @@ UNIV_INTERN
 ibool
 page_simple_validate_new(
 /*=====================*/
-	page_t*	page)	/*!< in: new-style index page */
+	const page_t*	page)	/*!< in: index page in ROW_FORMAT!=REDUNDANT */
 {
-	page_dir_slot_t* slot;
-	ulint		slot_no;
-	ulint		n_slots;
-	rec_t*		rec;
-	byte*		rec_heap_top;
-	ulint		count;
-	ulint		own_count;
-	ibool		ret	= FALSE;
+	const page_dir_slot_t*	slot;
+	ulint			slot_no;
+	ulint			n_slots;
+	const rec_t*		rec;
+	const byte*		rec_heap_top;
+	ulint			count;
+	ulint			own_count;
+	ibool			ret	= FALSE;
 
 	ut_a(page_is_comp(page));
 
@@ -2221,7 +2221,7 @@ page_simple_validate_new(
 			goto func_exit;
 		}
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 		own_count++;
 	}
 
@@ -2283,7 +2283,7 @@ page_simple_validate_new(
 			goto func_exit;
 		}
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
 	if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {
@@ -2308,26 +2308,26 @@ UNIV_INTERN
 ibool
 page_validate(
 /*==========*/
-	page_t*		page,	/*!< in: index page */
+	const page_t*	page,	/*!< in: index page */
 	dict_index_t*	index)	/*!< in: data dictionary index containing
 				the page record type definition */
 {
-	page_dir_slot_t*slot;
-	mem_heap_t*	heap;
-	byte*		buf;
-	ulint		count;
-	ulint		own_count;
-	ulint		rec_own_count;
-	ulint		slot_no;
-	ulint		data_size;
-	rec_t*		rec;
-	rec_t*		old_rec		= NULL;
-	ulint		offs;
-	ulint		n_slots;
-	ibool		ret		= FALSE;
-	ulint		i;
-	ulint*		offsets		= NULL;
-	ulint*		old_offsets	= NULL;
+	const page_dir_slot_t*	slot;
+	mem_heap_t*		heap;
+	byte*			buf;
+	ulint			count;
+	ulint			own_count;
+	ulint			rec_own_count;
+	ulint			slot_no;
+	ulint			data_size;
+	const rec_t*		rec;
+	const rec_t*		old_rec		= NULL;
+	ulint			offs;
+	ulint			n_slots;
+	ibool			ret		= FALSE;
+	ulint			i;
+	ulint*			offsets		= NULL;
+	ulint*			old_offsets	= NULL;
 
 	if (UNIV_UNLIKELY((ibool) !!page_is_comp(page)
 			  != dict_table_is_comp(index->table))) {
@@ -2482,7 +2482,7 @@ page_validate(
 		count++;
 		own_count++;
 		old_rec = rec;
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 
 		/* set old_offsets to offsets; recycle offsets */
 		{
@@ -2556,7 +2556,7 @@ n_owned_zero:
 			buf[offs + i] = 1;
 		}
 
-		rec = page_rec_get_next(rec);
+		rec = page_rec_get_next_const(rec);
 	}
 
 	if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {

=== modified file 'storage/innobase/page/page0zip.c'
--- a/storage/innobase/page/page0zip.c	2011-03-15 10:57:47 +0000
+++ b/storage/innobase/page/page0zip.c	2011-03-18 15:48:14 +0000
@@ -1516,7 +1516,7 @@ page_zip_fields_decode(
 	}
 
 	table = dict_mem_table_create("ZIP_DUMMY", DICT_HDR_SPACE, n,
-				      DICT_TF_COMPACT);
+				      DICT_TF_COMPACT, 0);
 	index = dict_mem_index_create("ZIP_DUMMY", "ZIP_DUMMY",
 				      DICT_HDR_SPACE, 0, n);
 	index->table = table;

=== modified file 'storage/innobase/pars/pars0pars.c'
--- a/storage/innobase/pars/pars0pars.c	2010-10-21 03:54:20 +0000
+++ b/storage/innobase/pars/pars0pars.c	2011-03-18 15:48:14 +0000
@@ -1,6 +1,6 @@
 /*****************************************************************************
 
-Copyright (c) 1996, 2010, Innobase Oy. All Rights Reserved.
+Copyright (c) 1996, 2011, Oracle and/or its affiliates. All Rights Reserved.
 
 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
@@ -1629,7 +1629,7 @@ pars_create_table(
 	/* As the InnoDB SQL parser is for internal use only,
 	for creating some system tables, this function will only
 	create tables in the old (not compact) record format. */
-	table = dict_mem_table_create(table_sym->name, 0, n_cols, 0);
+	table = dict_mem_table_create(table_sym->name, 0, n_cols, 0, 0);
 
 #ifdef UNIV_DEBUG
 	if (not_fit_in_memory != NULL) {

=== modified file 'storage/innobase/row/row0ext.c'
--- a/storage/innobase/row/row0ext.c	2010-07-16 21:00:50 +0000
+++ b/storage/innobase/row/row0ext.c	2011-03-23 13:43:14 +0000
@@ -93,7 +93,7 @@ row_ext_create(
 					     + (n_ext - 1) * sizeof ret->len);
 
 	ut_ad(ut_is_2pow(zip_size));
-	ut_ad(zip_size <= UNIV_PAGE_SIZE);
+	ut_ad(zip_size <= UNIV_ZIP_SIZE_MAX);
 
 	ret->n_ext = n_ext;
 	ret->ext = ext;

=== modified file 'storage/innobase/row/row0merge.c'
--- a/storage/innobase/row/row0merge.c	2011-02-17 12:02:22 +0000
+++ b/storage/innobase/row/row0merge.c	2011-03-18 15:48:14 +0000
@@ -2295,7 +2295,8 @@ row_merge_create_temporary_table(
 	ut_ad(table);
 	ut_ad(mutex_own(&dict_sys->mutex));
 
-	new_table = dict_mem_table_create(table_name, 0, n_cols, table->flags);
+	new_table = dict_mem_table_create(
+		table_name, 0, n_cols, table->flags, table->flags2);
 
 	for (i = 0; i < n_cols; i++) {
 		const dict_col_t*	col;

=== modified file 'storage/innobase/row/row0mysql.c'
--- a/storage/innobase/row/row0mysql.c	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/row/row0mysql.c	2011-03-18 15:48:14 +0000
@@ -1610,14 +1610,12 @@ row_unlock_for_mysql(
 			/* We did not update the record: unlock it */
 
 			rec = btr_pcur_get_rec(pcur);
-			index = btr_pcur_get_btr_cur(pcur)->index;
 
 			lock_rec_unlock(trx, btr_pcur_get_block(pcur),
 					rec, prebuilt->select_lock_type);
 
 			if (prebuilt->new_rec_locks >= 2) {
 				rec = btr_pcur_get_rec(clust_pcur);
-				index = btr_pcur_get_btr_cur(clust_pcur)->index;
 
 				lock_rec_unlock(trx,
 						btr_pcur_get_block(clust_pcur),
@@ -3418,8 +3416,7 @@ check_next_foreign:
 			is_temp = TRUE;
 		} else {
 			name_or_path = name;
-			is_temp = (table->flags >> DICT_TF2_SHIFT)
-				& DICT_TF2_TEMPORARY;
+			is_temp = table->flags2 & DICT_TF2_TEMPORARY;
 		}
 
 		dict_table_remove_from_cache(table);
@@ -3554,15 +3551,19 @@ row_mysql_drop_temp_tables(void)
 			break;
 		}
 
+		/* The high order bit of N_COLS is set unless
+		ROW_FORMAT=REDUNDANT. */
 		rec = btr_pcur_get_rec(&pcur);
 		field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
-		if (len != 4 || !(mach_read_from_4(field) & 0x80000000UL)) {
+		if (len != 4
+		    || !(mach_read_from_4(field) & DICT_N_COLS_COMPACT)) {
 			continue;
 		}
 
-		/* Because this is not a ROW_FORMAT=REDUNDANT table,
-		the is_temp flag is valid.  Examine it. */
-
+		/* Older versions of InnoDB, which only supported tables
+		in ROW_FORMAT=REDUNDANT could write garbage to
+		SYS_TABLES.MIX_LEN, where we now store the is_temp flag.
+		Above, we assumed is_temp=0 if ROW_FORMAT=REDUNDANT. */
 		field = rec_get_nth_field_old(rec, 7/*MIX_LEN*/, &len);
 		if (len != 4
 		    || !(mach_read_from_4(field) & DICT_TF2_TEMPORARY)) {

=== modified file 'storage/innobase/row/row0row.c'
--- a/storage/innobase/row/row0row.c	2010-07-29 12:33:56 +0000
+++ b/storage/innobase/row/row0row.c	2011-03-23 13:43:14 +0000
@@ -298,8 +298,7 @@ row_build(
 		/* REDUNDANT and COMPACT formats store a local
 		768-byte prefix of each externally stored
 		column. No cache is needed. */
-		ut_ad(dict_table_get_format(index->table)
-		      < DICT_TF_FORMAT_ZIP);
+		ut_ad(dict_table_get_format(index->table) < UNIV_FORMAT_B);
 	} else if (j) {
 		*ext = row_ext_create(j, ext_cols, row,
 				      dict_table_zip_size(index->table),

=== modified file 'storage/innobase/row/row0sel.c'
--- a/storage/innobase/row/row0sel.c	2011-03-02 23:09:09 +0000
+++ b/storage/innobase/row/row0sel.c	2011-03-29 12:56:34 +0000
@@ -58,7 +58,7 @@ Created 12/19/1997 Heikki Tuuri
 #include "buf0lru.h"
 #include "ha_prototypes.h"
 
-#include "my_handler.h" /* enum icp_result */
+#include "my_compare.h" /* enum icp_result */
 
 /* Maximum number of rows to prefetch; MySQL interface has another parameter */
 #define SEL_MAX_N_PREFETCH	16

=== modified file 'storage/innobase/row/row0umod.c'
--- a/storage/innobase/row/row0umod.c	2011-02-02 14:08:30 +0000
+++ b/storage/innobase/row/row0umod.c	2011-03-23 13:43:14 +0000
@@ -698,7 +698,7 @@ row_undo_mod_upd_exist_sec(
 				store a local 768-byte prefix of each
 				externally stored column. */
 				ut_a(dict_table_get_format(index->table)
-				     >= DICT_TF_FORMAT_ZIP);
+				     >= UNIV_FORMAT_B);
 
 				/* This is only legitimate when
 				rolling back an incomplete transaction

=== modified file 'storage/innobase/row/row0undo.c'
--- a/storage/innobase/row/row0undo.c	2010-08-24 02:07:08 +0000
+++ b/storage/innobase/row/row0undo.c	2011-03-23 13:43:14 +0000
@@ -200,7 +200,7 @@ row_undo_search_clust_to_pcur(
 	} else {
 		row_ext_t**	ext;
 
-		if (dict_table_get_format(node->table) >= DICT_TF_FORMAT_ZIP) {
+		if (dict_table_get_format(node->table) >= UNIV_FORMAT_B) {
 			/* In DYNAMIC or COMPRESSED format, there is
 			no prefix of externally stored columns in the
 			clustered index record. Build a cache of

=== modified file 'storage/innobase/row/row0upd.c'
--- a/storage/innobase/row/row0upd.c	2011-02-08 12:16:14 +0000
+++ b/storage/innobase/row/row0upd.c	2011-03-23 13:43:14 +0000
@@ -1507,7 +1507,7 @@ row_upd_store_row(
 	offsets = rec_get_offsets(rec, clust_index, offsets_,
 				  ULINT_UNDEFINED, &heap);
 
-	if (dict_table_get_format(node->table) >= DICT_TF_FORMAT_ZIP) {
+	if (dict_table_get_format(node->table) >= UNIV_FORMAT_B) {
 		/* In DYNAMIC or COMPRESSED format, there is no prefix
 		of externally stored columns in the clustered index
 		record. Build a cache of column prefixes. */

=== modified file 'storage/innobase/srv/srv0srv.c'
--- a/storage/innobase/srv/srv0srv.c	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/srv/srv0srv.c	2011-03-24 12:24:08 +0000
@@ -47,7 +47,6 @@ Created 10/8/1995 Heikki Tuuri
 #include "mem0mem.h"
 #include "mem0pool.h"
 #include "sync0sync.h"
-#include "thr0loc.h"
 #include "que0que.h"
 #include "log0recv.h"
 #include "pars0pars.h"
@@ -106,12 +105,12 @@ UNIV_INTERN my_bool	srv_file_per_table;
 /** The file format to use on new *.ibd files. */
 UNIV_INTERN ulint	srv_file_format = 0;
 /** Whether to check file format during startup.  A value of
-DICT_TF_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
+UNIV_FORMAT_MAX + 1 means no checking ie. FALSE.  The default is to
 set it to the highest format we support. */
-UNIV_INTERN ulint	srv_max_file_format_at_startup = DICT_TF_FORMAT_MAX;
+UNIV_INTERN ulint	srv_max_file_format_at_startup = UNIV_FORMAT_MAX;
 
-#if DICT_TF_FORMAT_51
-# error "DICT_TF_FORMAT_51 must be 0!"
+#if UNIV_FORMAT_A
+# error "UNIV_FORMAT_A must be 0!"
 #endif
 /** Place locks to records only i.e. do not use next-key locking except
 on duplicate key checking and foreign key checking */
@@ -693,6 +692,7 @@ srv_table_get_nth_slot(
 /*===================*/
 	ulint	index)		/*!< in: index of the slot */
 {
+	ut_ad(srv_sys_mutex_own());
 	ut_a(index < OS_THREAD_MAX_N);
 
 	return(srv_sys->sys_threads + index);
@@ -721,28 +721,61 @@ srv_get_n_threads(void)
 	return(n_threads);
 }
 
+#ifdef UNIV_DEBUG
 /*********************************************************************//**
-Reserves a slot in the thread table for the current thread. Also creates the
-thread local storage struct for the current thread. NOTE! The server mutex
-has to be reserved by the caller!
-@return	reserved slot index */
+Validates the type of a thread table slot.
+@return TRUE if ok */
 static
-ulint
+ibool
+srv_thread_type_validate(
+/*=====================*/
+	enum srv_thread_type	type)	/*!< in: thread type */
+{
+	switch (type) {
+	case SRV_NONE:
+		break;
+	case SRV_WORKER:
+	case SRV_PURGE:
+	case SRV_MASTER:
+		return(TRUE);
+	}
+	ut_error;
+	return(FALSE);
+}
+#endif /* UNIV_DEBUG */
+
+/*********************************************************************//**
+Gets the type of a thread table slot.
+@return thread type */
+static
+enum srv_thread_type
+srv_slot_get_type(
+/*==============*/
+	const srv_slot_t*	slot)	/*!< in: thread slot */
+{
+	enum srv_thread_type	type	= (enum srv_thread_type) slot->type;
+	ut_ad(srv_thread_type_validate(type));
+	return(type);
+}
+
+/*********************************************************************//**
+Reserves a slot in the thread table for the current thread.
+NOTE! The server mutex has to be reserved by the caller!
+@return	reserved slot */
+static
+srv_slot_t*
 srv_table_reserve_slot(
 /*===================*/
 	enum srv_thread_type	type)	/*!< in: type of the thread */
 {
-	ulint		i;
 	srv_slot_t*	slot;
 
 	ut_ad(srv_sys_mutex_own());
 
-	ut_a(type > 0);
-	ut_a(type <= SRV_MASTER);
+	ut_ad(srv_thread_type_validate(type));
 
 	if (type != SRV_MASTER) {
-
-		i = 1;
+		ulint	i = 1;
 
 		/* Find an empty slot. */
 		for (slot = srv_table_get_nth_slot(i);
@@ -753,60 +786,45 @@ srv_table_reserve_slot(
 		}
 
 	} else {
-		ut_a(type != SRV_NONE);
-		i = 0;
 		slot = srv_table_get_nth_slot(0);
 	}
 
-	ut_a(slot->in_use == FALSE);
-
 	slot->in_use = TRUE;
 	slot->suspended = FALSE;
 	slot->type = type;
+	ut_ad(srv_slot_get_type(slot) == type);
 	slot->id = os_thread_get_curr_id();
 	slot->handle = os_thread_get_curr();
 
-	thr_local_create();
-
-	thr_local_set_slot_no(os_thread_get_curr_id(), i);
-
-	return(i);
+	return(slot);
 }
 
 /*********************************************************************//**
-Suspends the calling thread to wait for the event in its thread slot.
-@return	event for the calling thread to wait */
+Suspends the calling thread to wait for the event in its thread slot. */
 static
-os_event_t
-srv_suspend_thread(void)
-/*====================*/
+void
+srv_suspend_thread(
+/*===============*/
+	srv_slot_t*	slot)	/*!< in/out: thread slot */
 {
-	srv_slot_t*		slot;
-	os_event_t		event;
-	ulint			slot_no;
 	enum srv_thread_type	type;
 
 	srv_sys_mutex_enter();
-
-	slot_no = thr_local_get_slot_no(os_thread_get_curr_id());
+	ut_ad(slot->in_use);
+	ut_ad(!slot->suspended);
+	ut_ad(slot->id == os_thread_get_curr_id());
 
 	if (srv_print_thread_releases) {
 		fprintf(stderr,
 			"Suspending thread %lu to slot %lu\n",
-			(ulong) os_thread_get_curr_id(), (ulong) slot_no);
+			(ulong) os_thread_get_curr_id(),
+			(ulong) (slot - srv_sys->sys_threads));
 	}
 
-	slot = srv_table_get_nth_slot(slot_no);
-
-	type = slot->type;
-
-	ut_ad(type >= SRV_WORKER);
-	ut_ad(type <= SRV_MASTER);
-
-	/* The master thread always uses slot number 0. */
-	ut_a(type != SRV_MASTER || slot_no == 0);
+	type = srv_slot_get_type(slot);
 
-	event = slot->event;
+	/* The master thread always uses the first slot. */
+	ut_a(type != SRV_MASTER || slot == srv_sys->sys_threads);
 
 	slot->suspended = TRUE;
 
@@ -814,11 +832,9 @@ srv_suspend_thread(void)
 
 	srv_sys->n_threads_active[type]--;
 
-	os_event_reset(event);
+	os_event_reset(slot->event);
 
 	srv_sys_mutex_exit();
-
-	return(event);
 }
 
 /*********************************************************************//**
@@ -836,8 +852,7 @@ srv_release_threads(
 	ulint		i;
 	ulint		count	= 0;
 
-	ut_ad(type >= SRV_WORKER);
-	ut_ad(type <= SRV_MASTER);
+	ut_ad(srv_thread_type_validate(type));
 	ut_ad(n > 0);
 
 	srv_sys_mutex_enter();
@@ -847,7 +862,8 @@ srv_release_threads(
 
 		slot = srv_table_get_nth_slot(i);
 
-		if (slot->in_use && slot->type == type && slot->suspended) {
+		if (slot->in_use && slot->suspended
+		    && srv_slot_get_type(slot) == type) {
 
 			slot->suspended = FALSE;
 
@@ -894,6 +910,7 @@ srv_thread_has_reserved_slot(
 	ulint			i;
 	ulint			slot_no = ULINT_UNDEFINED;
 
+	ut_ad(srv_thread_type_validate(type));
 	srv_sys_mutex_enter();
 
 	for (i = 0; i < OS_THREAD_MAX_N; i++) {
@@ -901,7 +918,7 @@ srv_thread_has_reserved_slot(
 
 		slot = srv_table_get_nth_slot(i);
 
-		if (slot->in_use && slot->type == type) {
+		if (slot->in_use && srv_slot_get_type(slot) == type) {
 			slot_no = i;
 			break;
 		}
@@ -943,7 +960,7 @@ srv_init(void)
 	for (i = 0; i < OS_THREAD_MAX_N; i++) {
 		srv_slot_t*	slot;
 
-		slot = srv_table_get_nth_slot(i);
+		slot = srv_sys->sys_threads + i;
 
 		slot->event = os_event_create(NULL);
 
@@ -1012,7 +1029,6 @@ srv_general_init(void)
 	os_sync_init();
 	sync_init();
 	mem_init(srv_mem_pool_size);
-	thr_local_init();
 	que_init();
 	row_mysql_init();
 }
@@ -2032,7 +2048,7 @@ srv_active_wake_master_thread(void)
 		/* Only if the master thread has been started. */
 
 		if (slot->in_use) {
-			ut_a(slot->type == SRV_MASTER);
+			ut_a(srv_slot_get_type(slot) == SRV_MASTER);
 
 			if (slot->suspended) {
 
@@ -2552,9 +2568,8 @@ srv_master_thread(
 			/*!< in: a dummy parameter required by
 			os_thread_create */
 {
-	ulint		slot;
+	srv_slot_t*	slot;
 	ulint		old_activity_count = srv_get_activity_count();
-	os_event_t	event;
 	ib_time_t	last_print_time;
 
 #ifdef UNIV_DEBUG_THREAD_CREATION
@@ -2572,7 +2587,7 @@ srv_master_thread(
 	srv_sys_mutex_enter();
 
 	slot = srv_table_reserve_slot(SRV_MASTER);
-	ut_a(slot == 0);
+	ut_a(slot == srv_sys->sys_threads);
 
 	srv_sys->n_threads_active[SRV_MASTER]++;
 
@@ -2607,7 +2622,7 @@ loop:
 suspend_thread:
 	srv_main_thread_op_info = "suspending";
 
-	event = srv_suspend_thread();
+	srv_suspend_thread(slot);
 
 	/* DO NOT CHANGE THIS STRING. innobase_start_or_create_for_mysql()
 	waits for database activity to die down when converting < 4.1.x
@@ -2615,7 +2630,7 @@ suspend_thread:
 	manual also mentions this string in several places. */
 	srv_main_thread_op_info = "waiting for server activity";
 
-	os_event_wait(event);
+	os_event_wait(slot->event);
 
 	if (srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS) {
 		/* This is only extra safety, the thread should exit
@@ -2626,8 +2641,6 @@ suspend_thread:
 	}
 
 	goto loop;
-
-	OS_THREAD_DUMMY_RETURN;	/* Not reached, avoid compiler warning */
 }
 
 /*********************************************************************//**
@@ -2681,7 +2694,6 @@ srv_worker_thread(
 						required by os_thread_create */
 {
 	srv_slot_t*	slot;
-	ulint		slot_no = ULINT_UNDEFINED;
 
 #ifdef UNIV_DEBUG_THREAD_CREATION
 	fprintf(stderr, "Worker thread starts, id %lu\n",
@@ -2690,7 +2702,7 @@ srv_worker_thread(
 
 	srv_sys_mutex_enter();
 
-	slot_no = srv_table_reserve_slot(SRV_WORKER);
+	slot = srv_table_reserve_slot(SRV_WORKER);
 
 	++srv_sys->n_threads_active[SRV_WORKER];
 
@@ -2701,11 +2713,9 @@ srv_worker_thread(
 
 	while (srv_shutdown_state == SRV_SHUTDOWN_NONE && !srv_fast_shutdown) {
 
-		os_event_t	event;
-
-		event = srv_suspend_thread();
+		srv_suspend_thread(slot);
 
-		os_event_wait(event);
+		os_event_wait(slot->event);
 
 		srv_task_execute();
 
@@ -2715,15 +2725,12 @@ srv_worker_thread(
 		srv_wake_purge_thread_if_not_active();
 	}
 
-	srv_suspend_thread();
-
-	/* Free the thread local memory. */
-	thr_local_free(os_thread_get_curr_id());
+	srv_suspend_thread(slot);
 
 	srv_sys_mutex_enter();
 
 	/* Free the slot for reuse. */
-	slot = srv_table_get_nth_slot(slot_no);
+	ut_ad(slot->in_use);
 	slot->in_use = FALSE;
 
 	srv_sys_mutex_exit();
@@ -2751,7 +2758,6 @@ srv_purge_coordinator_thread(
 						required by os_thread_create */
 {
 	srv_slot_t*	slot;
-	ulint		slot_no = ULINT_UNDEFINED;
 
 	ut_a(srv_n_purge_threads >= 1);
 
@@ -2769,7 +2775,7 @@ srv_purge_coordinator_thread(
 
 	srv_sys_mutex_enter();
 
-	slot_no = srv_table_reserve_slot(SRV_PURGE);
+	slot = srv_table_reserve_slot(SRV_PURGE);
 
 	++srv_sys->n_threads_active[SRV_PURGE];
 
@@ -2900,19 +2906,16 @@ srv_purge_coordinator_thread(
 	}
 
 	/* Decrement the active count. */
-	srv_suspend_thread();
+	srv_suspend_thread(slot);
 
 	srv_sys_mutex_enter();
 
 	/* Free the slot for reuse. */
-	slot = srv_table_get_nth_slot(slot_no);
+	ut_ad(slot->in_use);
 	slot->in_use = FALSE;
 
 	srv_sys_mutex_exit();
 
-	/* Free the thread local memory. */
-	thr_local_free(os_thread_get_curr_id());
-
 #ifdef UNIV_DEBUG_THREAD_CREATION
 	fprintf(stderr, "Purge coordinator exiting, id %lu\n",
 		os_thread_pf(os_thread_get_curr_id()));

=== modified file 'storage/innobase/srv/srv0start.c'
--- a/storage/innobase/srv/srv0start.c	2011-03-14 13:00:56 +0000
+++ b/storage/innobase/srv/srv0start.c	2011-03-24 12:24:08 +0000
@@ -85,7 +85,6 @@ Created 2/16/1996 Heikki Tuuri
 # include "row0row.h"
 # include "row0mysql.h"
 # include "btr0pcur.h"
-# include "thr0loc.h"
 # include "os0sync.h" /* for INNODB_RW_LOCKS_USE_ATOMICS */
 # include "zlib.h" /* for ZLIB_VERSION */
 
@@ -2260,7 +2259,6 @@ innobase_shutdown_for_mysql(void)
 	ibuf_close();
 	log_shutdown();
 	lock_sys_close();
-	thr_local_close();
 	trx_sys_file_format_close();
 	trx_sys_close();
 

=== removed directory 'storage/innobase/thr'
=== removed file 'storage/innobase/thr/thr0loc.c'
--- a/storage/innobase/thr/thr0loc.c	2010-11-11 13:16:04 +0000
+++ b/storage/innobase/thr/thr0loc.c	1970-01-01 00:00:00 +0000
@@ -1,307 +0,0 @@
-/*****************************************************************************
-
-Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
-
-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; version 2 of the License.
-
-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
-
-*****************************************************************************/
-
-/**************************************************//**
-@file thr/thr0loc.c
-The thread local storage
-
-Created 10/5/1995 Heikki Tuuri
-*******************************************************/
-
-#include "thr0loc.h"
-#ifdef UNIV_NONINL
-#include "thr0loc.ic"
-#endif
-
-#include "sync0sync.h"
-#include "hash0hash.h"
-#include "mem0mem.h"
-#include "srv0srv.h"
-
-/*
-	IMPLEMENTATION OF THREAD LOCAL STORAGE
-	======================================
-
-The threads sometimes need private data which depends on the thread id.
-This is implemented as a hash table, where the hash value is calculated
-from the thread id, to prepare for a large number of threads. The hash table
-is protected by a mutex. If you need modify the program and put new data to
-the thread local storage, just add it to struct thr_local_struct in the
-header file. */
-
-/** Mutex protecting thr_local_hash */
-static mutex_t		thr_local_mutex;
-
-/** The hash table. The module is not yet initialized when it is NULL. */
-static hash_table_t*	thr_local_hash	= NULL;
-
-/** Thread local data */
-typedef struct thr_local_struct thr_local_t;
-
-#ifdef UNIV_PFS_MUTEX
-/* Key to register the mutex with performance schema */
-UNIV_INTERN mysql_pfs_key_t	thr_local_mutex_key;
-#endif /* UNIV_PFS_MUTEX */
-
-/** @brief Thread local data.
-The private data for each thread should be put to
-the structure below and the accessor functions written
-for the field. */
-struct thr_local_struct{
-	os_thread_id_t	id;	/*!< id of the thread which owns this struct */
-	os_thread_t	handle;	/*!< operating system handle to the thread */
-	ulint		slot_no;/*!< the index of the slot in the thread table
-				for this thread */
-	ibool		in_ibuf;/*!< TRUE if the thread is doing an ibuf
-				operation */
-	hash_node_t	hash;	/*!< hash chain node */
-	ulint		magic_n;/*!< magic number (THR_LOCAL_MAGIC_N) */
-};
-
-/** The value of thr_local_struct::magic_n */
-#define THR_LOCAL_MAGIC_N	1231234
-
-#ifdef UNIV_DEBUG
-/*******************************************************************//**
-Validates thread local data.
-@return	TRUE if valid */
-static
-ibool
-thr_local_validate(
-/*===============*/
-	const thr_local_t*	local)	/*!< in: data to validate */
-{
-	ut_ad(local->magic_n == THR_LOCAL_MAGIC_N);
-	ut_ad(local->slot_no == ULINT_UNDEFINED
-	      || local->slot_no < OS_THREAD_MAX_N);
-	ut_ad(local->in_ibuf == FALSE || local->in_ibuf == TRUE);
-	return(TRUE);
-}
-#endif /* UNIV_DEBUG */
-
-/*******************************************************************//**
-Returns the local storage struct for a thread.
-@return	local storage */
-static
-thr_local_t*
-thr_local_get(
-/*==========*/
-	os_thread_id_t	id)	/*!< in: thread id of the thread */
-{
-	thr_local_t*	local;
-
-try_again:
-	ut_ad(thr_local_hash);
-	ut_ad(mutex_own(&thr_local_mutex));
-
-	/* Look for the local struct in the hash table */
-
-	local = NULL;
-
-	HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
-		    thr_local_t*, local, ut_ad(thr_local_validate(local)),
-		    os_thread_eq(local->id, id));
-	if (local == NULL) {
-		mutex_exit(&thr_local_mutex);
-
-		thr_local_create();
-
-		mutex_enter(&thr_local_mutex);
-
-		goto try_again;
-	}
-
-	ut_ad(thr_local_validate(local));
-
-	return(local);
-}
-
-/*******************************************************************//**
-Gets the slot number in the thread table of a thread.
-@return	slot number */
-UNIV_INTERN
-ulint
-thr_local_get_slot_no(
-/*==================*/
-	os_thread_id_t	id)	/*!< in: thread id of the thread */
-{
-	ulint		slot_no;
-	thr_local_t*	local;
-
-	mutex_enter(&thr_local_mutex);
-
-	local = thr_local_get(id);
-
-	slot_no = local->slot_no;
-
-	mutex_exit(&thr_local_mutex);
-
-	return(slot_no);
-}
-
-/*******************************************************************//**
-Sets the slot number in the thread table of a thread. */
-UNIV_INTERN
-void
-thr_local_set_slot_no(
-/*==================*/
-	os_thread_id_t	id,	/*!< in: thread id of the thread */
-	ulint		slot_no)/*!< in: slot number */
-{
-	thr_local_t*	local;
-
-	mutex_enter(&thr_local_mutex);
-
-	local = thr_local_get(id);
-
-	local->slot_no = slot_no;
-
-	mutex_exit(&thr_local_mutex);
-}
-
-/*******************************************************************//**
-Returns pointer to the 'in_ibuf' field within the current thread local
-storage.
-@return	pointer to the in_ibuf field */
-UNIV_INTERN
-ibool*
-thr_local_get_in_ibuf_field(void)
-/*=============================*/
-{
-	thr_local_t*	local;
-
-	mutex_enter(&thr_local_mutex);
-
-	local = thr_local_get(os_thread_get_curr_id());
-
-	mutex_exit(&thr_local_mutex);
-
-	return(&(local->in_ibuf));
-}
-
-/*******************************************************************//**
-Creates a local storage struct for the calling new thread. */
-UNIV_INTERN
-void
-thr_local_create(void)
-/*==================*/
-{
-	thr_local_t*	local;
-
-	if (thr_local_hash == NULL) {
-		thr_local_init();
-	}
-
-	local = mem_alloc(sizeof(thr_local_t));
-
-	local->id = os_thread_get_curr_id();
-	local->handle = os_thread_get_curr();
-	local->magic_n = THR_LOCAL_MAGIC_N;
-	local->slot_no = ULINT_UNDEFINED;
-	local->in_ibuf = FALSE;
-
-	mutex_enter(&thr_local_mutex);
-
-	HASH_INSERT(thr_local_t, hash, thr_local_hash,
-		    os_thread_pf(os_thread_get_curr_id()),
-		    local);
-
-	mutex_exit(&thr_local_mutex);
-}
-
-/*******************************************************************//**
-Frees the local storage struct for the specified thread. */
-UNIV_INTERN
-void
-thr_local_free(
-/*===========*/
-	os_thread_id_t	id)	/*!< in: thread id */
-{
-	thr_local_t*	local;
-
-	mutex_enter(&thr_local_mutex);
-
-	/* Look for the local struct in the hash table */
-
-	HASH_SEARCH(hash, thr_local_hash, os_thread_pf(id),
-		    thr_local_t*, local, ut_ad(thr_local_validate(local)),
-		    os_thread_eq(local->id, id));
-	if (local == NULL) {
-		mutex_exit(&thr_local_mutex);
-
-		return;
-	}
-
-	HASH_DELETE(thr_local_t, hash, thr_local_hash,
-		    os_thread_pf(id), local);
-
-	mutex_exit(&thr_local_mutex);
-
-	ut_a(local->magic_n == THR_LOCAL_MAGIC_N);
-	ut_ad(thr_local_validate(local));
-
-	mem_free(local);
-}
-
-/****************************************************************//**
-Initializes the thread local storage module. */
-UNIV_INTERN
-void
-thr_local_init(void)
-/*================*/
-{
-
-	ut_a(thr_local_hash == NULL);
-
-	thr_local_hash = hash_create(OS_THREAD_MAX_N + 100);
-
-	mutex_create(thr_local_mutex_key,
-		     &thr_local_mutex, SYNC_THR_LOCAL);
-}
-
-/********************************************************************
-Close the thread local storage module. */
-UNIV_INTERN
-void
-thr_local_close(void)
-/*=================*/
-{
-	ulint		i;
-
-	ut_a(thr_local_hash != NULL);
-
-	/* Free the hash elements. We don't remove them from the table
-	because we are going to destroy the table anyway. */
-	for (i = 0; i < hash_get_n_cells(thr_local_hash); i++) {
-		thr_local_t*	local;
-
-		local = HASH_GET_FIRST(thr_local_hash, i);
-
-		while (local) {
-			thr_local_t*	prev_local = local;
-
-			local = HASH_GET_NEXT(hash, prev_local);
-			ut_a(prev_local->magic_n == THR_LOCAL_MAGIC_N);
-			ut_ad(thr_local_validate(prev_local));
-			mem_free(prev_local);
-		}
-	}
-
-	hash_table_free(thr_local_hash);
-	thr_local_hash = NULL;
-}

=== modified file 'storage/innobase/trx/trx0i_s.c'
--- a/storage/innobase/trx/trx0i_s.c	2011-01-19 08:45:58 +0000
+++ b/storage/innobase/trx/trx0i_s.c	2011-03-28 09:15:58 +0000
@@ -519,7 +519,7 @@ fill_trx_row(
 		query[stmt_len] = '\0';
 
 		row->trx_query = ha_storage_put_memlim(
-			cache->storage, stmt, stmt_len + 1,
+			cache->storage, query, stmt_len + 1,
 			MAX_ALLOWED_FOR_STORAGE(cache));
 
 		row->trx_query_cs = innobase_get_charset(trx->mysql_thd);

=== modified file 'storage/innobase/trx/trx0rec.c'
--- a/storage/innobase/trx/trx0rec.c	2010-08-07 06:52:02 +0000
+++ b/storage/innobase/trx/trx0rec.c	2011-03-23 13:43:14 +0000
@@ -353,7 +353,7 @@ trx_undo_rec_get_col_val(
 		/* @see dtuple_convert_big_rec() */
 		ut_ad(*len >= BTR_EXTERN_FIELD_REF_SIZE * 2);
 		/* we do not have access to index->table here
-		ut_ad(dict_table_get_format(index->table) >= DICT_TF_FORMAT_ZIP
+		ut_ad(dict_table_get_format(index->table) >= UNIV_FORMAT_B
 		      || *len >= REC_MAX_INDEX_COL_LEN
 		      + BTR_EXTERN_FIELD_REF_SIZE);
 		*/
@@ -1084,7 +1084,7 @@ trx_undo_rec_get_partial_row(
 				ut_a(dfield_get_len(dfield)
 				     >= 2 * BTR_EXTERN_FIELD_REF_SIZE);
 				ut_a(dict_table_get_format(index->table)
-				     >= DICT_TF_FORMAT_ZIP
+				     >= UNIV_FORMAT_B
 				     || dfield_get_len(dfield)
 				     >= REC_MAX_INDEX_COL_LEN
 				     + BTR_EXTERN_FIELD_REF_SIZE);

=== modified file 'storage/innobase/trx/trx0sys.c'
--- a/storage/innobase/trx/trx0sys.c	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/trx/trx0sys.c	2011-03-23 13:43:14 +0000
@@ -1189,7 +1189,7 @@ trx_sys_file_format_id_to_name(
 
 /*****************************************************************//**
 Check for the max file format tag stored on disk. Note: If max_format_id
-is == DICT_TF_FORMAT_MAX + 1 then we only print a warning.
+is == UNIV_FORMAT_MAX + 1 then we only print a warning.
 @return	DB_SUCCESS or error code */
 UNIV_INTERN
 ulint
@@ -1206,15 +1206,15 @@ trx_sys_file_format_max_check(
 	if (format_id == ULINT_UNDEFINED) {
 		/* Format ID was not set. Set it to minimum possible
 		value. */
-		format_id = DICT_TF_FORMAT_MIN;
+		format_id = UNIV_FORMAT_MIN;
 	}
 
 	ut_print_timestamp(stderr);
 	fprintf(stderr,
 		" InnoDB: highest supported file format is %s.\n",
-		trx_sys_file_format_id_to_name(DICT_TF_FORMAT_MAX));
+		trx_sys_file_format_id_to_name(UNIV_FORMAT_MAX));
 
-	if (format_id > DICT_TF_FORMAT_MAX) {
+	if (format_id > UNIV_FORMAT_MAX) {
 
 		ut_a(format_id < FILE_FORMAT_NAME_N);
 
@@ -1222,11 +1222,11 @@ trx_sys_file_format_max_check(
 		fprintf(stderr,
 			" InnoDB: %s: the system tablespace is in a file "
 			"format that this version doesn't support - %s\n",
-			((max_format_id <= DICT_TF_FORMAT_MAX)
+			((max_format_id <= UNIV_FORMAT_MAX)
 				? "Error" : "Warning"),
 			trx_sys_file_format_id_to_name(format_id));
 
-		if (max_format_id <= DICT_TF_FORMAT_MAX) {
+		if (max_format_id <= UNIV_FORMAT_MAX) {
 			return(DB_ERROR);
 		}
 	}
@@ -1255,7 +1255,7 @@ trx_sys_file_format_max_set(
 {
 	ibool		ret = FALSE;
 
-	ut_a(format_id <= DICT_TF_FORMAT_MAX);
+	ut_a(format_id <= UNIV_FORMAT_MAX);
 
 	mutex_enter(&file_format_max.mutex);
 
@@ -1286,7 +1286,7 @@ trx_sys_file_format_tag_init(void)
 
 	/* If format_id is not set then set it to the minimum. */
 	if (format_id == ULINT_UNDEFINED) {
-		trx_sys_file_format_max_set(DICT_TF_FORMAT_MIN, NULL);
+		trx_sys_file_format_max_set(UNIV_FORMAT_MIN, NULL);
 	}
 }
 
@@ -1305,7 +1305,7 @@ trx_sys_file_format_max_upgrade(
 
 	ut_a(name);
 	ut_a(file_format_max.name != NULL);
-	ut_a(format_id <= DICT_TF_FORMAT_MAX);
+	ut_a(format_id <= UNIV_FORMAT_MAX);
 
 	mutex_enter(&file_format_max.mutex);
 
@@ -1342,7 +1342,7 @@ trx_sys_file_format_init(void)
 
 	/* We don't need a mutex here, as this function should only
 	be called once at start up. */
-	file_format_max.id = DICT_TF_FORMAT_MIN;
+	file_format_max.id = UNIV_FORMAT_MIN;
 
 	file_format_max.name = trx_sys_file_format_id_to_name(
 		file_format_max.id);

=== modified file 'storage/innobase/trx/trx0trx.c'
--- a/storage/innobase/trx/trx0trx.c	2011-03-09 07:32:36 +0000
+++ b/storage/innobase/trx/trx0trx.c	2011-03-24 12:24:08 +0000
@@ -39,7 +39,6 @@ Created 3/26/1996 Heikki Tuuri
 #include "read0read.h"
 #include "srv0srv.h"
 #include "srv0start.h"
-#include "thr0loc.h"
 #include "btr0sea.h"
 #include "os0proc.h"
 #include "trx0xa.h"

=== modified file 'storage/myisam/ft_stopwords.c'
--- a/storage/myisam/ft_stopwords.c	2011-03-28 08:51:35 +0000
+++ b/storage/myisam/ft_stopwords.c	2011-03-29 12:56:34 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2000-2005 MySQL AB
+/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
    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
@@ -16,7 +16,7 @@
 /* Written by Sergei A. Golubchik, who has a shared copyright to this code */
 
 #include "ftdefs.h"
-#include "my_handler.h"
+#include "my_compare.h"
 
 
 static CHARSET_INFO *ft_stopword_cs= NULL;

=== modified file 'storage/myisam/mi_check.c'
--- a/storage/myisam/mi_check.c	2011-03-28 08:51:35 +0000
+++ b/storage/myisam/mi_check.c	2011-03-29 12:56:34 +0000
@@ -80,6 +80,7 @@ static SORT_KEY_BLOCKS	*alloc_key_blocks
 					  uint buffer_length);
 static ha_checksum mi_byte_checksum(const uchar *buf, uint length);
 static void set_data_file_type(SORT_INFO *sort_info, MYISAM_SHARE *share);
+static HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a);
 
 void myisamchk_init(MI_CHECK *param)
 {
@@ -4729,3 +4730,92 @@ set_data_file_type(SORT_INFO *sort_info,
     share->delete_record=tmp.delete_record;
   }
 }
+
+/*
+  Find the first NULL value in index-suffix values tuple
+
+  SYNOPSIS
+    ha_find_null()
+      keyseg     Array of keyparts for key suffix
+      a          Key suffix value tuple
+
+  DESCRIPTION
+    Find the first NULL value in index-suffix values tuple.
+
+  TODO
+    Consider optimizing this function or its use so we don't search for
+    NULL values in completely NOT NULL index suffixes.
+
+  RETURN
+    First key part that has NULL as value in values tuple, or the last key
+    part (with keyseg->type==HA_TYPE_END) if values tuple doesn't contain
+    NULLs.
+*/
+
+static HA_KEYSEG *ha_find_null(HA_KEYSEG *keyseg, uchar *a)
+{
+  for (; (enum ha_base_keytype) keyseg->type != HA_KEYTYPE_END; keyseg++)
+  {
+    uchar *end;
+    if (keyseg->null_bit)
+    {
+      if (!*a++)
+        return keyseg;
+    }
+    end= a+ keyseg->length;
+
+    switch ((enum ha_base_keytype) keyseg->type) {
+    case HA_KEYTYPE_TEXT:
+    case HA_KEYTYPE_BINARY:
+    case HA_KEYTYPE_BIT:
+      if (keyseg->flag & HA_SPACE_PACK)
+      {
+        int a_length;
+        get_key_length(a_length, a);
+        a += a_length;
+        break;
+      }
+      else
+        a= end;
+      break;
+    case HA_KEYTYPE_VARTEXT1:
+    case HA_KEYTYPE_VARTEXT2:
+    case HA_KEYTYPE_VARBINARY1:
+    case HA_KEYTYPE_VARBINARY2:
+      {
+        int a_length;
+        get_key_length(a_length, a);
+        a+= a_length;
+        break;
+      }
+    case HA_KEYTYPE_NUM:
+      if (keyseg->flag & HA_SPACE_PACK)
+      {
+        int alength= *a++;
+        end= a+alength;
+      }
+      a= end;
+      break;
+    case HA_KEYTYPE_INT8:
+    case HA_KEYTYPE_SHORT_INT:
+    case HA_KEYTYPE_USHORT_INT:
+    case HA_KEYTYPE_LONG_INT:
+    case HA_KEYTYPE_ULONG_INT:
+    case HA_KEYTYPE_INT24:
+    case HA_KEYTYPE_UINT24:
+#ifdef HAVE_LONG_LONG
+    case HA_KEYTYPE_LONGLONG:
+    case HA_KEYTYPE_ULONGLONG:
+#endif
+    case HA_KEYTYPE_FLOAT:
+    case HA_KEYTYPE_DOUBLE:
+      a= end;
+      break;
+    case HA_KEYTYPE_END:                        /* purecov: inspected */
+      /* keep compiler happy */
+      DBUG_ASSERT(0);
+      break;
+    }
+  }
+  return keyseg;
+}

=== modified file 'storage/myisam/mi_range.c'
--- a/storage/myisam/mi_range.c	2011-03-22 12:56:15 +0000
+++ b/storage/myisam/mi_range.c	2011-03-29 12:56:34 +0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 2000-2004, 2006 MySQL AB, 2008-2009 Sun Microsystems, Inc
+/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
+
 
    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
@@ -139,8 +140,8 @@ static ha_rows _mi_record_pos(MI_INFO *i
     key_len=USE_WHOLE_KEY;
 
   /*
-    my_handler.c:ha_compare_text() has a flag 'skip_end_space'.
-    This is set in my_handler.c:ha_key_cmp() in dependence on the
+    my_compare.c:ha_compare_text() has a flag 'skip_end_space'.
+    This is set in my_compare.c:ha_key_cmp() in dependence on the
     compare flags 'nextflag' and the column type.
 
     TEXT columns are of type HA_KEYTYPE_VARTEXT. In this case the

=== modified file 'storage/perfschema/pfs.cc'
--- a/storage/perfschema/pfs.cc	2011-03-29 00:46:15 +0000
+++ b/storage/perfschema/pfs.cc	2011-04-01 22:04:26 +0000
@@ -4651,26 +4651,22 @@ static void end_socket_wait_v1(PSI_socke
   }
   else
   {
-    /* Aggregate to the socket instrument for now (counted) */
-    byte_stat->aggregate_counted();
+    /* Aggregate to the socket instrument (event count and byte count) */
+    byte_stat->aggregate_counted(bytes);
   }
 
   /** Global thread aggregation */
   if (flags & STATE_FLAG_THREAD)
   {
-    PFS_single_stat *event_name_array;
-    event_name_array= thread->m_instr_class_waits_stats;
-    uint index= socket->m_class->m_event_name_index;
-
-    /* Aggregate to EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME */ // move to visitor
-    if (flags & STATE_FLAG_TIMED)
-    {
-      /* Aggregate to EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME (timed) */
-      event_name_array[index].aggregate_value(wait_time);
-    }
-    else
+    /*
+       Aggregate to EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME (counted).
+       Aggregation of socket wait stats per thread are deferred.
+     */
+    if (flags & ~STATE_FLAG_TIMED) // TODO MA: Moved to consumer except count
     {
-      /* Aggregate to EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME (counted) */
+      PFS_single_stat *event_name_array;
+      event_name_array= thread->m_instr_class_waits_stats;
+      uint index= socket->m_class->m_event_name_index;
       event_name_array[index].aggregate_counted();
     }
 
@@ -4715,6 +4711,11 @@ static void set_socket_info_v1(PSI_socke
   DBUG_ASSERT(socket);
   PFS_socket *pfs= reinterpret_cast<PFS_socket*>(socket);
 
+  /** Set socket thread owner */
+  PFS_thread *thread= my_pthread_getspecific_ptr(PFS_thread*, THR_PFS);
+  if (thread)
+    pfs->m_thread_owner= thread;
+  
   /** Set socket descriptor */
   if (fd != NULL)
     pfs->m_fd= *fd;

=== modified file 'storage/perfschema/pfs_instr.cc'
--- a/storage/perfschema/pfs_instr.cc	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/pfs_instr.cc	2011-04-01 22:04:26 +0000
@@ -1434,10 +1434,16 @@ void destroy_socket(PFS_socket *pfs)
   DBUG_ASSERT(pfs != NULL);
   PFS_socket_class *klass= pfs->m_class;
 
+#if 0   // TODO MA: Here or defer to consumer?
+  /* Combine per-operation stats into a single stat */
+  PFS_single_stat stat;
+  pfs->m_socket_stat.m_io_stat.sum_waits(&stat);
+  
   /* Aggregate to EVENTS_WAITS_SUMMARY_BY_EVENT_NAME */
   uint index= klass->m_event_name_index;
-  global_instr_class_waits_array[index].aggregate(&pfs->m_wait_stat);
+  global_instr_class_waits_array[index].aggregate(&stat);
   pfs->m_wait_stat.reset();
+#endif
 
   /* Aggregate to SOCKET_SUMMARY_BY_INSTANCE and BY_EVENT_NAME */
   klass->m_socket_stat.m_io_stat.aggregate(&pfs->m_socket_stat.m_io_stat);

=== modified file 'storage/perfschema/pfs_instr.h'
--- a/storage/perfschema/pfs_instr.h	2011-03-28 23:37:10 +0000
+++ b/storage/perfschema/pfs_instr.h	2011-04-01 22:04:26 +0000
@@ -56,8 +56,6 @@ struct PFS_instr
 {
   /** Internal lock. */
   pfs_lock m_lock;
-  /** Instrument wait statistics. */
-  PFS_single_stat m_wait_stat;
 };
 
 /** Instrumented mutex implementation. @see PSI_mutex. */
@@ -67,6 +65,8 @@ struct PFS_mutex : public PFS_instr
   const void *m_identity;
   /** Mutex class. */
   PFS_mutex_class *m_class;
+  /** Instrument wait statistics. */
+  PFS_single_stat m_wait_stat;
   /**
     Mutex lock usage statistics.
     This statistic is not exposed in user visible tables yet.
@@ -88,6 +88,8 @@ struct PFS_rwlock : public PFS_instr
   const void *m_identity;
   /** RWLock class. */
   PFS_rwlock_class *m_class;
+  /** Instrument wait statistics. */
+  PFS_single_stat m_wait_stat;
   /**
     RWLock read lock usage statistics.
     This statistic is not exposed in user visible tables yet.
@@ -121,6 +123,8 @@ struct PFS_cond : public PFS_instr
   const void *m_identity;
   /** Condition class. */
   PFS_cond_class *m_class;
+  /** Instrument wait statistics. */
+  PFS_single_stat m_wait_stat;
   /** Condition instance usage statistics. */
   PFS_cond_stat m_cond_stat;
 };
@@ -137,6 +141,8 @@ struct PFS_file : public PFS_instr
   uint m_filename_length;
   /** File class. */
   PFS_file_class *m_class;
+  /** Instrument wait statistics. */
+  PFS_single_stat m_wait_stat;
   /** File usage statistics. */
   PFS_file_stat m_file_stat;
 };
@@ -217,6 +223,8 @@ struct PFS_socket : public PFS_instr
   bool m_idle;
   /** Socket class. */
   PFS_socket_class *m_class;
+  /** Instrument wait statistics. */
+  PFS_single_stat m_wait_stat;
   /** Socket usage statistics. */
   PFS_socket_stat m_socket_stat;
 };

=== modified file 'storage/perfschema/pfs_instr_class.cc'
--- a/storage/perfschema/pfs_instr_class.cc	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/pfs_instr_class.cc	2011-04-01 22:04:26 +0000
@@ -567,7 +567,8 @@ void cleanup_socket_class(void)
 static void init_instr_class(PFS_instr_class *klass,
                              const char *name,
                              uint name_length,
-                             int flags)
+                             int flags,
+                             PFS_class_type class_type)
 {
   DBUG_ASSERT(name_length <= PFS_MAX_INFO_NAME_LENGTH);
   memset(klass, 0, sizeof(PFS_instr_class));
@@ -576,6 +577,8 @@ static void init_instr_class(PFS_instr_c
   klass->m_flags= flags;
   klass->m_enabled= true;
   klass->m_timed= true;
+  klass->m_deferred= false;
+  klass->m_type= class_type;
 }
 
 #define REGISTER_CLASS_BODY_PART(INDEX, ARRAY, MAX, NAME, NAME_LENGTH) \
@@ -639,7 +642,7 @@ PFS_sync_key register_mutex_class(const
         in INSTALL PLUGIN.
     */
     entry= &mutex_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_MUTEX);
     entry->m_lock_stat.reset();
     entry->m_index= index;
     entry->m_event_name_index= mutex_class_start + index;
@@ -700,7 +703,7 @@ PFS_sync_key register_rwlock_class(const
   if (index < rwlock_class_max)
   {
     entry= &rwlock_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_RWLOCK);
     entry->m_read_lock_stat.reset();
     entry->m_write_lock_stat.reset();
     entry->m_index= index;
@@ -736,7 +739,7 @@ PFS_sync_key register_cond_class(const c
   if (index < cond_class_max)
   {
     entry= &cond_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_COND);
     entry->m_index= index;
     entry->m_event_name_index= cond_class_start + index;
     entry->m_singleton= NULL;
@@ -875,7 +878,7 @@ PFS_file_key register_file_class(const c
   if (index < file_class_max)
   {
     entry= &file_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_FILE);
     entry->m_index= index;
     entry->m_event_name_index= file_class_start + index;
     entry->m_singleton= NULL;
@@ -909,7 +912,7 @@ PFS_stage_key register_stage_class(const
   if (index < stage_class_max)
   {
     entry= &stage_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_STAGE);
     entry->m_index= index;
     entry->m_event_name_index= index;
     PFS_atomic::add_u32(&stage_class_allocated_count, 1);
@@ -943,7 +946,7 @@ PFS_statement_key register_statement_cla
   if (index < statement_class_max)
   {
     entry= &statement_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_STATEMENT);
     entry->m_index= index;
     entry->m_event_name_index= index;
     PFS_atomic::add_u32(&statement_class_allocated_count, 1);
@@ -1022,10 +1025,11 @@ PFS_socket_key register_socket_class(con
   if (index < socket_class_max)
   {
     entry= &socket_class_array[index];
-    init_instr_class(entry, name, name_length, flags);
+    init_instr_class(entry, name, name_length, flags, PFS_CLASS_SOCKET);
     entry->m_index= index;
     entry->m_event_name_index= socket_class_start + index;
     entry->m_singleton= NULL;
+    entry->m_deferred= true;
     PFS_atomic::add_u32(&socket_class_allocated_count, 1);
     return (index + 1);
   }

=== modified file 'storage/perfschema/pfs_instr_class.h'
--- a/storage/perfschema/pfs_instr_class.h	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/pfs_instr_class.h	2011-04-01 22:04:26 +0000
@@ -66,6 +66,18 @@ typedef unsigned int PFS_statement_key;
 /** Key, naming a socket instrument. */
 typedef unsigned int PFS_socket_key;
 
+enum PFS_class_type
+{
+  PFS_CLASS_MUTEX=  1,
+  PFS_CLASS_RWLOCK= 2,
+  PFS_CLASS_COND=   3,
+  PFS_CLASS_FILE=   4,
+  PFS_CLASS_TABLE=  5,
+  PFS_CLASS_STAGE=  6,
+  PFS_CLASS_STATEMENT= 7,
+  PFS_CLASS_SOCKET= 8
+};
+
 struct PFS_thread;
 
 extern uint mutex_class_start;
@@ -79,6 +91,8 @@ extern uint wait_class_max;
 /** Information for all instrumentation. */
 struct PFS_instr_class
 {
+  /** Class type */
+  PFS_class_type m_type;
   /** Instrument name. */
   char m_name[PFS_MAX_INFO_NAME_LENGTH];
   /** Length in bytes of @c m_name. */
@@ -89,6 +103,8 @@ struct PFS_instr_class
   bool m_enabled;
   /** True if this instrument is timed. */
   bool m_timed;
+  /** True if this instrument requires deferred aggregation */
+  bool m_deferred;
   /**
     Instrument name index.
     Self index in:

=== modified file 'storage/perfschema/pfs_stat.h'
--- a/storage/perfschema/pfs_stat.h	2011-03-28 23:37:10 +0000
+++ b/storage/perfschema/pfs_stat.h	2011-04-01 22:04:26 +0000
@@ -88,18 +88,38 @@ struct PFS_byte_stat : public PFS_single
   /** Byte count statistics */
   ulonglong m_bytes;
 
+  /* Aggregate wait stats, event count and byte count */
   inline void aggregate(const PFS_byte_stat *stat)
   {
     PFS_single_stat::aggregate(stat);
     m_bytes+= stat->m_bytes;
   }
 
+  /* Aggregate individual wait time, event count and byte count */
   inline void aggregate(ulonglong wait, ulonglong bytes)
   {
-    if (wait != 0) aggregate_value(wait);
-    if (bytes != 0) m_bytes+= bytes;
+    if (wait != 0)
+      aggregate_value(wait);
+
+    if (bytes != 0)
+      m_bytes+= bytes;
   }
 
+  /* Aggregate wait stats and event count */
+  inline void aggregate_waits(const PFS_byte_stat *stat)
+  {
+    PFS_single_stat::aggregate(stat);
+  }
+
+  /* Aggregate event count and byte count */
+  inline void aggregate_counted(ulonglong bytes)
+  {
+    PFS_single_stat::aggregate_counted();
+
+    if (bytes != 0)
+      m_bytes+= bytes;
+  }
+    
   PFS_byte_stat()
   {
     reset();
@@ -482,7 +502,16 @@ struct PFS_socket_io_stat
     m_misc.aggregate(&stat->m_misc);
   }
 
-  inline void consolidate(PFS_byte_stat *stat)
+  /* Sum waits and byte counts */
+  inline void sum(PFS_byte_stat *stat)
+  {
+    stat->aggregate(&m_read);
+    stat->aggregate(&m_write);
+    stat->aggregate(&m_misc);
+  }
+
+  /* Sum waits only */
+  inline void sum_waits(PFS_single_stat *stat)
   {
     stat->aggregate(&m_read);
     stat->aggregate(&m_write);

=== modified file 'storage/perfschema/pfs_visitor.cc'
--- a/storage/perfschema/pfs_visitor.cc	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/pfs_visitor.cc	2011-04-01 22:04:26 +0000
@@ -17,6 +17,7 @@
 #include "my_sys.h"
 #include "pfs_visitor.h"
 #include "pfs_instr.h"
+#include "pfs_instr_class.h"
 
 /**
   @file storage/perfschema/pfs_visitor.cc
@@ -179,6 +180,8 @@ void PFS_instance_iterator::visit_file_i
   }
 }
 
+/** Socket instance iterator visting all socket instances */
+
 void PFS_instance_iterator::visit_socket_instances(PFS_socket_class *klass,
                                                    PFS_instance_visitor *visitor)
 {
@@ -186,31 +189,68 @@ void PFS_instance_iterator::visit_socket
 
   visitor->visit_socket_class(klass);
 
-  if (klass->is_singleton())
+  PFS_socket *pfs= socket_array;
+  PFS_socket *pfs_last= pfs + socket_max;
+  for ( ; pfs < pfs_last; pfs++)
   {
-    PFS_socket *pfs= sanitize_socket(klass->m_singleton);
-    if (likely(pfs != NULL))
+    if ((pfs->m_class == klass) && pfs->m_lock.is_populated())
     {
-      if (likely(pfs->m_lock.is_populated()))
-      {
-        visitor->visit_socket(pfs);
-      }
+      visitor->visit_socket(pfs);
     }
   }
-  else
+}
+
+/** Socket instance iterator visting sockets owned by PFS_thread */
+
+void PFS_instance_iterator::visit_socket_instances(PFS_socket_class *klass,
+                                                   PFS_instance_visitor *visitor,
+                                                   PFS_thread *thread)
+{
+  DBUG_ASSERT(visitor != NULL);
+  DBUG_ASSERT(thread != NULL);
+
+  /* Get accumulated socket stats for this thread */
+  visitor->visit_socket_class(klass);
+
+  /* Get current socket stats from each socket instance owned by this thread */
+  PFS_socket *pfs= socket_array;
+  PFS_socket *pfs_last= pfs + socket_max;
+  ulong thread_id= thread->m_thread_internal_id;
+
+  for ( ; pfs < pfs_last; pfs++)
   {
-    PFS_socket *pfs= socket_array;
-    PFS_socket *pfs_last= pfs + socket_max;
-    for ( ; pfs < pfs_last; pfs++)
+    if (pfs->m_class == klass && pfs->m_lock.is_populated()
+         && pfs->m_thread_owner != NULL)
     {
-      if ((pfs->m_class == klass) && pfs->m_lock.is_populated())
-      {
+      if (pfs->m_thread_owner->m_thread_internal_id == thread_id)
         visitor->visit_socket(pfs);
-      }
     }
   }
 }
 
+
+/** Generic instance iterator with PFS_thread as matching criteria */
+
+void PFS_instance_iterator::visit_instances(PFS_instr_class *instr_class,
+                                            PFS_instance_visitor *visitor,
+                                            PFS_thread *thread)
+{
+  DBUG_ASSERT(visitor != NULL);
+  DBUG_ASSERT(instr_class != NULL);
+
+  switch (instr_class->m_type)
+  {
+  case PFS_CLASS_SOCKET:
+    {
+      PFS_socket_class *klass= reinterpret_cast<PFS_socket_class *>(instr_class);
+      PFS_instance_iterator::visit_socket_instances(klass, visitor, thread);
+    }
+    break;
+  default:
+    break;
+  }
+}
+
 /** Object iterator */
 
 void PFS_object_iterator::visit_all_tables(PFS_object_visitor *visitor)
@@ -380,8 +420,13 @@ void PFS_instance_wait_visitor::visit_fi
 
 void PFS_instance_wait_visitor::visit_socket_class(PFS_socket_class *pfs) 
 {
+  /* Collect global wait stats */
   uint index= pfs->m_event_name_index;
-  m_stat.aggregate(& global_instr_class_waits_array[index]);
+  m_stat.aggregate(&global_instr_class_waits_array[index]);
+
+  /* If deferred, then pull wait stats directly from the socket class. */
+  if (pfs->m_deferred)
+    pfs->m_socket_stat.m_io_stat.sum_waits(&m_stat);
 }
 
 void PFS_instance_wait_visitor::visit_mutex(PFS_mutex *pfs) 
@@ -406,7 +451,10 @@ void PFS_instance_wait_visitor::visit_fi
 
 void PFS_instance_wait_visitor::visit_socket(PFS_socket *pfs) 
 {
-  m_stat.aggregate(& pfs->m_wait_stat);
+  /* Combine per-operation socket wait stats before aggregating */
+  PFS_single_stat stat;
+  pfs->m_socket_stat.m_io_stat.sum_waits(&stat);
+  m_stat.aggregate(&stat);
 }
 
 /** Table IO wait visitor */
@@ -565,11 +613,13 @@ PFS_instance_socket_io_stat_visitor::~PF
 
 void PFS_instance_socket_io_stat_visitor::visit_socket_class(PFS_socket_class *pfs) 
 {
+  /* Aggregate wait times, event counts and byte counts */
   m_socket_io_stat.aggregate(&pfs->m_socket_stat.m_io_stat);
 }
 
 void PFS_instance_socket_io_stat_visitor::visit_socket(PFS_socket *pfs) 
 {
+  /* Aggregate wait times, event counts and byte counts */
   m_socket_io_stat.aggregate(&pfs->m_socket_stat.m_io_stat);
 }
 

=== modified file 'storage/perfschema/pfs_visitor.h'
--- a/storage/perfschema/pfs_visitor.h	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/pfs_visitor.h	2011-04-01 22:04:26 +0000
@@ -56,7 +56,7 @@ public:
   virtual ~PFS_connection_visitor() {}
   /** Visit all connections. */
   virtual void visit_global() {}
-  /** Visit all a thread. */
+  /** Visit a thread. */
   virtual void visit_thread(PFS_thread *pfs) {}
 };
 
@@ -157,6 +157,23 @@ public:
   */
   static void visit_socket_instances(PFS_socket_class *klass,
                                      PFS_instance_visitor *visitor);
+  /**
+    Visit a socket class and related instances.
+    @param klass the klass to visit.
+    @param visitor the visitor to call
+  */
+  static void visit_socket_instances(PFS_socket_class *klass,
+                                     PFS_instance_visitor *visitor,
+                                     PFS_thread *thread);
+  /**
+    Visit an instrument class and related instances.
+    @param klass the klass to visit.
+    @param visitor the visitor to call
+    @param thread comparison criteria
+  */
+  static void visit_instances(PFS_instr_class *klass,
+                              PFS_instance_visitor *visitor,
+                              PFS_thread *thread);
 };
 
 /**

=== modified file 'storage/perfschema/table_events_waits_summary.cc'
--- a/storage/perfschema/table_events_waits_summary.cc	2011-03-28 23:37:10 +0000
+++ b/storage/perfschema/table_events_waits_summary.cc	2011-04-01 22:04:26 +0000
@@ -109,8 +109,6 @@ void table_events_waits_summary_by_insta
                  PFS_single_stat *pfs_stat)
 {
   pfs_lock lock;
-  PFS_single_stat *stat= (pfs_stat ? pfs_stat : &pfs->m_wait_stat);
-
   m_row_exists= false;
 
   /*
@@ -124,19 +122,12 @@ void table_events_waits_summary_by_insta
   m_row.m_object_instance_addr= (intptr) object_instance_begin;
 
   time_normalizer *normalizer= time_normalizer::get(wait_timer);
-  m_row.m_stat.set(normalizer, stat);//& pfs->m_wait_stat);
+  m_row.m_stat.set(normalizer, pfs_stat);
 
   if (pfs->m_lock.end_optimistic_lock(&lock))
     m_row_exists= true;
 }
 
-void table_events_waits_summary_by_instance
-::make_instr_row(PFS_instr *pfs, PFS_instr_class *klass,
-                 const void *object_instance_begin)
-{
-  make_instr_row(pfs, klass, object_instance_begin, NULL);
-}
-
 /**
   Build a row, for mutex statistics in a thread.
   @param pfs              the mutex this cursor is reading
@@ -148,7 +139,7 @@ void table_events_waits_summary_by_insta
   if (unlikely(safe_class == NULL))
     return;
 
-  make_instr_row(pfs, safe_class, pfs->m_identity);
+  make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_wait_stat);
 }
 
 /**
@@ -162,7 +153,7 @@ void table_events_waits_summary_by_insta
   if (unlikely(safe_class == NULL))
     return;
 
-  make_instr_row(pfs, safe_class, pfs->m_identity);
+  make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_wait_stat);
 }
 
 /**
@@ -176,7 +167,7 @@ void table_events_waits_summary_by_insta
   if (unlikely(safe_class == NULL))
     return;
 
-  make_instr_row(pfs, safe_class, pfs->m_identity);
+  make_instr_row(pfs, safe_class, pfs->m_identity, &pfs->m_wait_stat);
 }
 
 /**
@@ -194,7 +185,7 @@ void table_events_waits_summary_by_insta
     Files don't have a in memory structure associated to it,
     so we use the address of the PFS_file buffer as object_instance_begin
   */
-  make_instr_row(pfs, safe_class, pfs);
+  make_instr_row(pfs, safe_class, pfs, &pfs->m_wait_stat);
 }
 
 /**
@@ -207,17 +198,17 @@ void table_events_waits_summary_by_insta
   safe_class= sanitize_socket_class(pfs->m_class);
   if (unlikely(safe_class == NULL))
     return;
-  PFS_byte_stat pfs_stat;
 
   /*
      Consolidate wait times and byte counts for individual operations. This is
      done by the consumer in order to reduce overhead on the socket instrument.
   */
-  pfs->m_socket_stat.m_io_stat.consolidate(&pfs_stat);
+  PFS_byte_stat pfs_stat;
+  pfs->m_socket_stat.m_io_stat.sum(&pfs_stat);
 
   /*
-    Sockets don't have a in memory structure associated to it,
-    so we use the address of the PFS_socket buffer as object_instance_begin
+    Sockets don't have an associated in-memory structure, so use the address of
+    the PFS_socket buffer as object_instance_begin.
   */
   make_instr_row(pfs, safe_class, pfs, &pfs_stat);
 }

=== modified file 'storage/perfschema/table_events_waits_summary.h'
--- a/storage/perfschema/table_events_waits_summary.h	2011-03-28 23:37:10 +0000
+++ b/storage/perfschema/table_events_waits_summary.h	2011-04-01 22:04:26 +0000
@@ -59,8 +59,6 @@ protected:
   void make_instr_row(PFS_instr *pfs, PFS_instr_class *klass,
                       const void *object_instance_begin,
                       PFS_single_stat *pfs_stat);
-  void make_instr_row(PFS_instr *pfs, PFS_instr_class *klass,
-                      const void *object_instance_begin);
   virtual void make_mutex_row(PFS_mutex *pfs);
   virtual void make_rwlock_row(PFS_rwlock *pfs);
   virtual void make_cond_row(PFS_cond *pfs);

=== modified file 'storage/perfschema/table_ews_by_thread_by_event_name.cc'
--- a/storage/perfschema/table_ews_by_thread_by_event_name.cc	2011-03-28 20:15:26 +0000
+++ b/storage/perfschema/table_ews_by_thread_by_event_name.cc	2011-04-01 22:04:26 +0000
@@ -235,7 +235,22 @@ void table_ews_by_thread_by_event_name
   m_row.m_event_name.make_row(klass);
 
   PFS_connection_wait_visitor visitor(klass);
-  PFS_connection_iterator::visit_thread(thread, & visitor);
+  PFS_connection_iterator::visit_thread(thread, &visitor);
+
+  /*
+     If the aggregation for this class is deferred, then we must pull the stats
+     from the class itself and from the instances associated with this thread.
+  */  
+  if (klass->m_deferred)
+  {
+    PFS_instance_socket_io_stat_visitor inst_visitor;
+    PFS_instance_iterator::visit_instances(klass, &inst_visitor, thread);
+    /* Sum separate per-operation stats into one */
+    PFS_byte_stat stat;
+    inst_visitor.m_socket_io_stat.sum(&stat);
+    /* Combine the deferred stats and global stats */
+    visitor.m_stat.aggregate(&stat);
+  }
 
   if (! thread->m_lock.end_optimistic_lock(&lock))
     return;

=== modified file 'storage/perfschema/table_socket_instances.cc'
--- a/storage/perfschema/table_socket_instances.cc	2011-03-28 23:37:10 +0000
+++ b/storage/perfschema/table_socket_instances.cc	2011-04-01 22:04:26 +0000
@@ -166,7 +166,7 @@ void table_socket_instances::make_row(PF
 
   if (safe_thread != NULL)
   {
-    m_row.m_thread_id= safe_thread->m_thread_id;
+    m_row.m_thread_id= safe_thread->m_thread_internal_id;
     m_row.m_thread_id_set= true;
   }
 
@@ -175,7 +175,7 @@ void table_socket_instances::make_row(PF
 }
 
 int table_socket_instances::read_row_values(TABLE *table,
-                                          unsigned char *,
+                                          unsigned char *buf,
                                           Field **fields,
                                           bool read_all)
 {
@@ -184,6 +184,10 @@ int table_socket_instances::read_row_val
   if (unlikely(!m_row_exists))
     return HA_ERR_RECORD_DELETED;
 
+  /* Set the null bits */
+  DBUG_ASSERT(table->s->null_bytes == 1);
+  buf[0]= 0;
+
   for (; (f= *fields) ; fields++)
   {
     if (read_all || bitmap_is_set(table->read_set, f->field_index))


Attachment: [text/bzr-bundle] bzr/cpowers@tma-1-20110401230138-mwrloxpyb11257ou.bundle
Thread
bzr push into mysql-trunk branch (chris.powers:3274 to 3276) Christopher Powers2 Apr