diff --git a/client/mysql_upgrade.c b/client/mysql_upgrade.c
index e1fc4caf720f3ea3e8c94f83c9f8f7f0b351247d..190bb2383e9525f6c71ba9de87a0d223bf525b54 100644
--- a/client/mysql_upgrade.c
+++ b/client/mysql_upgrade.c
@@ -616,6 +616,18 @@ static int run_mysqlcheck_upgrade(void)
                   "--check-upgrade",
                   "--all-databases",
                   "--auto-repair",
+                  NULL);
+}
+
+
+static int run_mysqlcheck_fixnames(void)
+{
+  verbose("Running 'mysqlcheck'...");
+  return run_tool(mysqlcheck_path,
+                  NULL, /* Send output from mysqlcheck directly to screen */
+                  "--no-defaults",
+                  ds_args.str,
+                  "--all-databases",
                   "--fix-db-names",
                   "--fix-table-names",
                   NULL);
@@ -784,7 +796,8 @@ int main(int argc, char **argv)
   /*
     Run "mysqlcheck" and "mysql_fix_privilege_tables.sql"
   */
-  if (run_mysqlcheck_upgrade() ||
+  if (run_mysqlcheck_fixnames() ||
+      run_mysqlcheck_upgrade() ||
       run_sql_fix_privilege_tables())
   {
     /*
diff --git a/mysql-test/r/fulltext.result b/mysql-test/r/fulltext.result
index 0124ced40ce3e1c9159a2acbcf992de9e93059f0..8633bd6168131670c03fedd1bd7df1b300356345 100644
--- a/mysql-test/r/fulltext.result
+++ b/mysql-test/r/fulltext.result
@@ -522,3 +522,12 @@ WHERE MATCH(a) AGAINST('test' IN BOOLEAN MODE) AND b=1;
 id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
 1	SIMPLE	t1	ref	b	b	5	const	4	Using where
 DROP TABLE t1;
+CREATE TABLE t1(a CHAR(10));
+INSERT INTO t1 VALUES('aaa15');
+SELECT MATCH(a) AGAINST('aaa1* aaa14 aaa16' IN BOOLEAN MODE) FROM t1;
+MATCH(a) AGAINST('aaa1* aaa14 aaa16' IN BOOLEAN MODE)
+1
+SELECT MATCH(a) AGAINST('aaa1* aaa14 aaa15 aaa16' IN BOOLEAN MODE) FROM t1;
+MATCH(a) AGAINST('aaa1* aaa14 aaa15 aaa16' IN BOOLEAN MODE)
+2
+DROP TABLE t1;
diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result
index 78748a4622fa1d6dcfc1c775427ffec9a146eb70..0824f13cafcf77bc52f56ef08c12016780569b1f 100644
--- a/mysql-test/r/func_str.result
+++ b/mysql-test/r/func_str.result
@@ -717,8 +717,6 @@ insert(_latin2'abcd',2,3,_latin2'ef'),
 replace(_latin2'abcd',_latin2'b',_latin2'B'),
 encode('abcd','ab')
 ;
-Warnings:
-Warning	1265	Data truncated for column 'format(130,10)' at row 1
 show create table t1;
 Table	Create Table
 t1	CREATE TABLE `t1` (
@@ -727,7 +725,7 @@ t1	CREATE TABLE `t1` (
   `conv(130,16,10)` varchar(64) DEFAULT NULL,
   `hex(130)` varchar(6) NOT NULL DEFAULT '',
   `char(130)` varbinary(4) NOT NULL DEFAULT '',
-  `format(130,10)` varchar(4) NOT NULL DEFAULT '',
+  `format(130,10)` varchar(37) NOT NULL DEFAULT '',
   `left(_latin2'a',1)` varchar(1) CHARACTER SET latin2 NOT NULL DEFAULT '',
   `right(_latin2'a',1)` varchar(1) CHARACTER SET latin2 NOT NULL DEFAULT '',
   `lcase(_latin2'a')` varchar(1) CHARACTER SET latin2 NOT NULL DEFAULT '',
@@ -2513,4 +2511,12 @@ SELECT HEX(c1) from v1;
 HEX(c1)
 414243
 DROP VIEW v1;
+create table t1(a float);
+insert into t1 values (1.33);
+select format(a, 2) from t1;
+Catalog	Database	Table	Table_alias	Column	Column_alias	Type	Length	Max length	Is_null	Flags	Decimals	Charsetnr
+def					format(a, 2)	253	49	4	Y	0	31	8
+format(a, 2)
+1.33
+drop table t1;
 End of 5.0 tests
diff --git a/mysql-test/r/myisam_data_pointer_size_func.result b/mysql-test/r/myisam_data_pointer_size_func.result
index f32e25c7427f77e47d141e3b55f7a60b90e2a102..90d224d65841f184852c99cf4c80426aca2bd6f5 100644
--- a/mysql-test/r/myisam_data_pointer_size_func.result
+++ b/mysql-test/r/myisam_data_pointer_size_func.result
@@ -40,3 +40,4 @@ count(*)
 65536
 DROP PROCEDURE  sp_addRec;
 DROP TABLE t1;
+SET @@global.myisam_data_pointer_size = default;
diff --git a/mysql-test/r/myisampack.result b/mysql-test/r/myisampack.result
index 5f39d318234a3efc0ee03392948191762dfa8eb2..14b6283bf8f36650973b9d2c5ed312e731378ed7 100644
--- a/mysql-test/r/myisampack.result
+++ b/mysql-test/r/myisampack.result
@@ -27,3 +27,25 @@ CHECK TABLE t1 EXTENDED;
 Table	Op	Msg_type	Msg_text
 test.t1	check	status	OK
 DROP TABLE t1;
+drop table if exists t1;
+create table t1(f1 int, f2 varchar(255));
+insert into t1 values(1, 'foo'), (2, 'bar');
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+flush tables;
+optimize table t1;
+Table	Op	Msg_type	Msg_text
+test.t1	optimize	error	Table 'test.t1' is read only
+Warnings:
+Error	1036	Table 't1' is read only
+drop table t1;
diff --git a/mysql-test/r/partition_datatype.result b/mysql-test/r/partition_datatype.result
index 3e4acfdee12b138bb673abeb3b881208a9df57c7..607afb71da5e13f6d9f40e3e39e67be2bffdbe44 100644
--- a/mysql-test/r/partition_datatype.result
+++ b/mysql-test/r/partition_datatype.result
@@ -324,7 +324,7 @@ partition by hash (a)
 show create table t1;
 Table	Create Table
 t1	CREATE TABLE `t1` (
-  `a` bit(27) NOT NULL DEFAULT '\0\0\0\0',
+  `a` bit(27) NOT NULL DEFAULT b'0',
   PRIMARY KEY (`a`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1
 /*!50100 PARTITION BY HASH (a)
diff --git a/mysql-test/r/type_bit.result b/mysql-test/r/type_bit.result
index 782cddaeff7220e09eefecf0a3888d78f1204d9d..03f3098ea33e8ab5d08980ba9f6308e7d134a024 100644
--- a/mysql-test/r/type_bit.result
+++ b/mysql-test/r/type_bit.result
@@ -708,6 +708,21 @@ HEX(b1)	HEX(b2)	i2
 1	0	100
 1	0	200
 DROP TABLE t1, t2;
+CREATE TABLE IF NOT EXISTS t1 (
+f1 bit(2) NOT NULL default b'10',
+f2 bit(14) NOT NULL default b'11110000111100'
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
+SHOW CREATE TABLE t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `f1` bit(2) NOT NULL DEFAULT b'10',
+  `f2` bit(14) NOT NULL DEFAULT b'11110000111100'
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
+DROP TABLE t1;
+CREATE TABLE IF NOT EXISTS t1 (
+f1 bit(2) NOT NULL default b''
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
+ERROR 42000: Invalid default value for 'f1'
 End of 5.0 tests
 create table t1(a bit(7));
 insert into t1 values(0x40);
diff --git a/mysql-test/r/type_float.result b/mysql-test/r/type_float.result
index 49edaf06847de3ba6ff2a0c12a0246cd834af178..3a8b6db09741b0e3044a413033f1d9b731e0e613 100644
--- a/mysql-test/r/type_float.result
+++ b/mysql-test/r/type_float.result
@@ -398,11 +398,11 @@ insert into t1(d) values (9.2233720368547777e+18),
 (9.22337203685479e18),
 (1.84e19);
 update t1 set u = d;
-select * from t1;
-d	u
-9.22337203685478e+18	9223372036854775808
-9.22337203685478e+18	9223372036854779904
-9.22337203685479e+18	9223372036854790144
-1.84e+19	18400000000000000000
+select u from t1;
+u
+9223372036854775808
+9223372036854779904
+9223372036854790144
+18400000000000000000
 drop table t1;
 End of 5.0 tests
diff --git a/mysql-test/suite/binlog/r/binlog_innodb_row.result b/mysql-test/suite/binlog/r/binlog_innodb_row.result
new file mode 100644
index 0000000000000000000000000000000000000000..fab79c4bc2fee227767e5e077411cf4d931ab30c
--- /dev/null
+++ b/mysql-test/suite/binlog/r/binlog_innodb_row.result
@@ -0,0 +1,31 @@
+CREATE TABLE t1 (i int unique) ENGINE=innodb;
+reset master;
+begin;
+insert into t1 values (1),(2);
+*** the following UPDATE query wont generate any updates for the binlog ***
+update t1 set i = 3 where i < 3;
+ERROR 23000: Duplicate entry '3' for key 'i'
+commit;
+*** Results of the test: the binlog must have only Write_rows events not any Update_rows ***
+show binlog events from <binlog_start>;
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Table_map	#	#	table_id: # (test.t1)
+master-bin.000001	#	Write_rows	#	#	table_id: # flags: STMT_END_F
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+delete from t1;
+reset master;
+begin;
+insert into t1 values (1),(2);
+*** the following UPDATE query wont generate any updates for the binlog ***
+insert into t1 values (3),(4),(1),(2);
+ERROR 23000: Duplicate entry '1' for key 'i'
+commit;
+*** Results of the test: the binlog must have only one Write_rows event not two ***
+show binlog events from <binlog_start>;
+Log_name	Pos	Event_type	Server_id	End_log_pos	Info
+master-bin.000001	#	Query	#	#	use `test`; BEGIN
+master-bin.000001	#	Table_map	#	#	table_id: # (test.t1)
+master-bin.000001	#	Write_rows	#	#	table_id: # flags: STMT_END_F
+master-bin.000001	#	Xid	#	#	COMMIT /* XID */
+drop table t1;
diff --git a/mysql-test/suite/binlog/r/binlog_row_mix_innodb_myisam.result b/mysql-test/suite/binlog/r/binlog_row_mix_innodb_myisam.result
index 0b33d71b6f19c004e28f10aa0ee6c1634bddb011..dcad7e022b78fc0b7bdff4720527811a7845ff39 100644
--- a/mysql-test/suite/binlog/r/binlog_row_mix_innodb_myisam.result
+++ b/mysql-test/suite/binlog/r/binlog_row_mix_innodb_myisam.result
@@ -385,8 +385,6 @@ master-bin.000001	#	Xid	#	#	COMMIT /* XID */
 master-bin.000001	#	Query	#	#	use `test`; BEGIN
 master-bin.000001	#	Table_map	#	#	table_id: # (test.t1)
 master-bin.000001	#	Write_rows	#	#	table_id: # flags: STMT_END_F
-master-bin.000001	#	Table_map	#	#	table_id: # (test.t2)
-master-bin.000001	#	Write_rows	#	#	table_id: # flags: STMT_END_F
 master-bin.000001	#	Query	#	#	use `test`; COMMIT
 master-bin.000001	#	Query	#	#	use `test`; DROP TABLE t2
 master-bin.000001	#	Query	#	#	use `test`; BEGIN
diff --git a/mysql-test/suite/binlog/t/binlog_innodb_row.test b/mysql-test/suite/binlog/t/binlog_innodb_row.test
new file mode 100644
index 0000000000000000000000000000000000000000..aaba98e3284ff1c931b2b0c4948f8898e0267b1b
--- /dev/null
+++ b/mysql-test/suite/binlog/t/binlog_innodb_row.test
@@ -0,0 +1,42 @@
+#
+# Tests of innodb/binlog with the row binlog format
+# 
+source include/have_innodb.inc;
+source include/have_log_bin.inc;
+source include/have_binlog_format_row.inc;
+
+#
+# Bug #40221 Replication failure on RBR + UPDATE the primary key
+#
+
+CREATE TABLE t1 (i int unique) ENGINE=innodb;
+reset master;
+
+# part 1: update can cause the dup key
+
+begin;
+insert into t1 values (1),(2);
+--echo *** the following UPDATE query wont generate any updates for the binlog ***
+--error ER_DUP_ENTRY
+update t1 set i = 3 where i < 3;
+commit;
+
+--echo *** Results of the test: the binlog must have only Write_rows events not any Update_rows ***
+source include/show_binlog_events.inc;
+
+# part 2: insert can cause the dup key
+
+delete from t1;
+reset master;
+
+begin;
+insert into t1 values (1),(2);
+--echo *** the following UPDATE query wont generate any updates for the binlog ***
+--error ER_DUP_ENTRY
+insert into t1 values (3),(4),(1),(2);
+commit;
+
+--echo *** Results of the test: the binlog must have only one Write_rows event not two ***
+source include/show_binlog_events.inc;
+
+drop table t1;
diff --git a/mysql-test/t/fulltext.test b/mysql-test/t/fulltext.test
index 21b06ca0882bbafec5194457d836cfabd72966a9..852369fd56804d692d5647cbba89708cdcdc4f66 100644
--- a/mysql-test/t/fulltext.test
+++ b/mysql-test/t/fulltext.test
@@ -445,3 +445,12 @@ EXPLAIN SELECT * FROM t1 FORCE INDEX(b)
 WHERE MATCH(a) AGAINST('test' IN BOOLEAN MODE) AND b=1;
 
 DROP TABLE t1;
+
+#
+# BUG#37245 - Full text search problem
+#
+CREATE TABLE t1(a CHAR(10));
+INSERT INTO t1 VALUES('aaa15');
+SELECT MATCH(a) AGAINST('aaa1* aaa14 aaa16' IN BOOLEAN MODE) FROM t1;
+SELECT MATCH(a) AGAINST('aaa1* aaa14 aaa15 aaa16' IN BOOLEAN MODE) FROM t1;
+DROP TABLE t1;
diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test
index 5a5f4024bc477fc3c7d46853842f9772eab0408e..5d77c678d52d61187b4136b8db1527648c30e9e8 100644
--- a/mysql-test/t/func_str.test
+++ b/mysql-test/t/func_str.test
@@ -1263,4 +1263,14 @@ CREATE VIEW v1 AS SELECT CHAR(0x414243) as c1;
 SELECT HEX(c1) from v1;
 DROP VIEW v1;
 
+#
+# Bug #35558 Wrong server metadata blows up the client
+#
+create table t1(a float);
+insert into t1 values (1.33);
+--enable_metadata
+select format(a, 2) from t1;
+--disable_metadata
+drop table t1;
+
 --echo End of 5.0 tests
diff --git a/mysql-test/t/myisam_data_pointer_size_func.test b/mysql-test/t/myisam_data_pointer_size_func.test
index 36ad31acf6c76d9051a0d093c7a2f569e57f05b3..985546cbde4cea8364f6b8b82b70f837f6c70d44 100644
--- a/mysql-test/t/myisam_data_pointer_size_func.test
+++ b/mysql-test/t/myisam_data_pointer_size_func.test
@@ -105,6 +105,7 @@ SELECT count(*) from t1;
 DROP PROCEDURE  sp_addRec;
 DROP TABLE t1;
 
+SET @@global.myisam_data_pointer_size = default;
 ################################################################
 # End of functionality Testing for myisam_data_pointer_size    #
 ################################################################
diff --git a/mysql-test/t/myisampack.test b/mysql-test/t/myisampack.test
index 6598af6318a6f74132ccedfcb67e97ccab9537c8..99767f81033cfc56e9acb4cf55b56cc4bca08a9a 100644
--- a/mysql-test/t/myisampack.test
+++ b/mysql-test/t/myisampack.test
@@ -31,3 +31,28 @@ FLUSH TABLES;
 --exec $MYISAMCHK -s --unpack $MYSQLTEST_VARDIR/master-data/test/t1
 CHECK TABLE t1 EXTENDED;
 DROP TABLE t1;
+
+#
+# Bug#40949 Debug version of MySQL server crashes when run OPTIMIZE on compressed table.
+#
+--disable_warnings
+drop table if exists t1;
+--enable_warnings
+create table t1(f1 int, f2 varchar(255));
+insert into t1 values(1, 'foo'), (2, 'bar');
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+insert into t1 select * from t1;
+flush tables;
+--exec $MYISAMPACK $MYSQLTEST_VARDIR/master-data/test/t1
+optimize table t1;
+drop table t1;
diff --git a/mysql-test/t/type_bit.test b/mysql-test/t/type_bit.test
index 5793846a0b2f4ba418a700b3d2a149658db46bdf..6fb65262e14ff0a424c563853aa89d8ca6652643 100644
--- a/mysql-test/t/type_bit.test
+++ b/mysql-test/t/type_bit.test
@@ -352,6 +352,21 @@ SELECT HEX(b1), HEX(b2), i2 FROM t2
 
 DROP TABLE t1, t2;
 
+#
+# Bug #35796 SHOW CREATE TABLE and default value for BIT field
+#
+CREATE TABLE IF NOT EXISTS t1 (
+f1 bit(2) NOT NULL default b'10',
+f2 bit(14) NOT NULL default b'11110000111100'
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
+SHOW CREATE TABLE t1;
+DROP TABLE t1;
+
+--error ER_INVALID_DEFAULT
+CREATE TABLE IF NOT EXISTS t1 (
+f1 bit(2) NOT NULL default b''
+) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
+
 --echo End of 5.0 tests
 
 #
diff --git a/mysql-test/t/type_float.test b/mysql-test/t/type_float.test
index b23755b44fbd9750619da5f8d55f6264ce77e466..3ceef129912b2aa86382dc9f70e495d2eecb986f 100644
--- a/mysql-test/t/type_float.test
+++ b/mysql-test/t/type_float.test
@@ -265,7 +265,7 @@ insert into t1(d) values (9.2233720368547777e+18),
                          (1.84e19);
 
 update t1 set u = d;
-select * from t1;
+select u from t1;
 
 drop table t1;
 
diff --git a/sql/field.cc b/sql/field.cc
index e815320bc9eb9f6b90b2851484a934b6680d1973..f4651764b945b24a99d9ac2a100d47b31781281b 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -3835,7 +3835,7 @@ int Field_longlong::store(double nr)
       error= 1;
     }
     else
-      res=(longlong) (ulonglong) nr;
+      res=(longlong) double2ulonglong(nr);
   }
   else
   {
diff --git a/sql/item.cc b/sql/item.cc
index 7b7c44b4719bc330b5ae017e39b48490dba52052..7f85713915d37918e64f93dde7981ad428936869 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -5114,6 +5114,9 @@ int Item_hex_string::save_in_field(Field *field, bool no_conversions)
 
   ulonglong nr;
   uint32 length= str_value.length();
+  if (!length)
+    return 1;
+
   if (length > 8)
   {
     nr= field->flags & UNSIGNED_FLAG ? ULONGLONG_MAX : LONGLONG_MAX;
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 4e01728f71c7f3e61bfff7c19955952f886f2aa2..bb3f4821c4fe56947c33528639499fcb18d7153d 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -2033,10 +2033,11 @@ Item_func_format::Item_func_format(Item *org, Item *dec)
 
 void Item_func_format::fix_length_and_dec()
 {
-  collation.set(default_charset());
   uint char_length= args[0]->max_length/args[0]->collation.collation->mbmaxlen;
-  max_length= ((char_length + (char_length-args[0]->decimals)/3) *
-               collation.collation->mbmaxlen);
+  uint max_sep_count= char_length/3 + (decimals ? 1 : 0) + /*sign*/1;
+  collation.set(default_charset());
+  max_length= (char_length + max_sep_count + decimals) *
+    collation.collation->mbmaxlen;
 }
 
 
diff --git a/sql/log.cc b/sql/log.cc
index 3ebf2a7c03efad94b2bb9918726ab32c0b6f2933..b0b4444c9121592b73aed96b3eb531f0d16e4f76 100644
--- a/sql/log.cc
+++ b/sql/log.cc
@@ -207,6 +207,7 @@ public:
       truncate(0);
     before_stmt_pos= MY_OFF_T_UNDEF;
     trans_log.end_of_file= max_binlog_cache_size;
+    DBUG_ASSERT(empty());
   }
 
   Rows_log_event *pending() const
@@ -1377,8 +1378,6 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
                       FLAGSTR(thd->options, OPTION_NOT_AUTOCOMMIT),
                       FLAGSTR(thd->options, OPTION_BEGIN)));
 
-  thd->binlog_flush_pending_rows_event(TRUE);
-
   /*
     NULL denotes ROLLBACK with nothing to replicate: i.e., rollback of
     only transactional tables.  If the transaction contain changes to
@@ -1387,6 +1386,7 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
   */
   if (end_ev != NULL)
   {
+    thd->binlog_flush_pending_rows_event(TRUE);
     /*
       Doing a commit or a rollback including non-transactional tables,
       i.e., ending a transaction where we might write the transaction
@@ -1435,6 +1435,7 @@ binlog_end_trans(THD *thd, binlog_trx_data *trx_data,
     mysql_bin_log.update_table_map_version();
   }
 
+  DBUG_ASSERT(thd->binlog_get_pending_rows_event() == NULL);
   DBUG_RETURN(error);
 }
 
@@ -1466,6 +1467,7 @@ static int binlog_prepare(handlerton *hton, THD *thd, bool all)
 */
 static int binlog_commit(handlerton *hton, THD *thd, bool all)
 {
+  int error= 0;
   DBUG_ENTER("binlog_commit");
   binlog_trx_data *const trx_data=
     (binlog_trx_data*) thd_get_ha_data(thd, binlog_hton);
@@ -1499,10 +1501,14 @@ static int binlog_commit(handlerton *hton, THD *thd, bool all)
   {
     Query_log_event qev(thd, STRING_WITH_LEN("COMMIT"), TRUE, FALSE);
     qev.error_code= 0; // see comment in MYSQL_LOG::write(THD, IO_CACHE)
-    int error= binlog_end_trans(thd, trx_data, &qev, all);
-    DBUG_RETURN(error);
+    error= binlog_end_trans(thd, trx_data, &qev, all);
+    goto end;
   }
-  DBUG_RETURN(0);
+
+end:
+  if (!all)
+    trx_data->before_stmt_pos = MY_OFF_T_UNDEF; // part of the stmt commit
+  DBUG_RETURN(error);
 }
 
 /**
@@ -1562,6 +1568,8 @@ static int binlog_rollback(handlerton *hton, THD *thd, bool all)
      */
     error= binlog_end_trans(thd, trx_data, 0, all);
   }
+  if (!all)
+    trx_data->before_stmt_pos = MY_OFF_T_UNDEF; // part of the stmt rollback
   DBUG_RETURN(error);
 }
 
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index 28e8c028362726f4f9ad3e739eb6796cc8c6c8cf..a506429fb972211f47c0ec1e22c0be58904a35cb 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -993,7 +993,7 @@ static bool get_field_default_value(THD *thd, TABLE *table,
 {
   bool has_default;
   bool has_now_default;
-
+  enum enum_field_types field_type= field->type();
   /* 
      We are using CURRENT_TIMESTAMP instead of NOW because it is
      more standard
@@ -1001,7 +1001,7 @@ static bool get_field_default_value(THD *thd, TABLE *table,
   has_now_default= table->timestamp_field == field && 
     field->unireg_check != Field::TIMESTAMP_UN_FIELD;
     
-  has_default= (field->type() != FIELD_TYPE_BLOB &&
+  has_default= (field_type != FIELD_TYPE_BLOB &&
                 !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
                 field->unireg_check != Field::NEXT_NUMBER &&
                 !((thd->variables.sql_mode & (MODE_MYSQL323 | MODE_MYSQL40))
@@ -1016,7 +1016,19 @@ static bool get_field_default_value(THD *thd, TABLE *table,
     {                                             // Not null by default
       char tmp[MAX_FIELD_WIDTH];
       String type(tmp, sizeof(tmp), field->charset());
-      field->val_str(&type);
+      if (field_type == MYSQL_TYPE_BIT)
+      {
+        longlong dec= field->val_int();
+        char *ptr= longlong2str(dec, tmp + 2, 2);
+        uint32 length= (uint32) (ptr - tmp);
+        tmp[0]= 'b';
+        tmp[1]= '\'';        
+        tmp[length]= '\'';
+        type.length(length + 1);
+        quoted= 0;
+      }
+      else
+        field->val_str(&type);
       if (type.length())
       {
         String def_val;
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index ec35616bc89028a6ed205593c07497e469342d45..16d420441d1f443f296a86e06d29137cbb3ff345 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -4319,6 +4319,7 @@ static bool mysql_admin_table(THD* thd, TABLE_LIST* tables,
       table->table=0;				// For query cache
       if (protocol->write())
 	goto err;
+      thd->main_da.reset_diagnostics_area();
       continue;
       /* purecov: end */
     }
diff --git a/storage/myisam/ft_boolean_search.c b/storage/myisam/ft_boolean_search.c
index 051a21b330773e697224374649a119257d67d1c8..660978646225d1de582cea4f008984f3ff8266e9 100644
--- a/storage/myisam/ft_boolean_search.c
+++ b/storage/myisam/ft_boolean_search.c
@@ -161,11 +161,11 @@ static int FTB_WORD_cmp(my_off_t *v, FTB_WORD *a, FTB_WORD *b)
 
 static int FTB_WORD_cmp_list(CHARSET_INFO *cs, FTB_WORD **a, FTB_WORD **b)
 {
-  /* ORDER BY word DESC, ndepth DESC */
-  int i= ha_compare_text(cs, (uchar*) (*b)->word+1,(*b)->len-1,
-                             (uchar*) (*a)->word+1,(*a)->len-1,0,0);
+  /* ORDER BY word, ndepth */
+  int i= ha_compare_text(cs, (uchar*) (*a)->word + 1, (*a)->len - 1,
+                             (uchar*) (*b)->word + 1, (*b)->len - 1, 0, 0);
   if (!i)
-    i=CMP_NUM((*b)->ndepth,(*a)->ndepth);
+    i= CMP_NUM((*a)->ndepth, (*b)->ndepth);
   return i;
 }
 
@@ -865,23 +865,49 @@ static int ftb_find_relevance_add_word(MYSQL_FTPARSER_PARAM *param,
   FT_INFO *ftb= ftb_param->ftb;
   FTB_WORD *ftbw;
   int a, b, c;
+  /*
+    Find right-most element in the array of query words matching this
+    word from a document.
+  */
   for (a= 0, b= ftb->queue.elements, c= (a+b)/2; b-a>1; c= (a+b)/2)
   {
     ftbw= ftb->list[c];
     if (ha_compare_text(ftb->charset, (uchar*)word, len,
                         (uchar*)ftbw->word+1, ftbw->len-1,
-                        (my_bool)(ftbw->flags&FTB_FLAG_TRUNC), 0) > 0)
+                        (my_bool) (ftbw->flags & FTB_FLAG_TRUNC), 0) < 0)
       b= c;
     else
       a= c;
   }
+  /*
+    If there were no words with truncation operator, we iterate to the
+    beginning of an array until array element is equal to the word from
+    a document. This is done mainly because the same word may be
+    mentioned twice (or more) in the query.
+
+    In case query has words with truncation operator we must iterate
+    to the beginning of the array. There may be non-matching query words
+    between matching word with truncation operator and the right-most
+    matching element. E.g., if we're looking for 'aaa15' in an array of
+    'aaa1* aaa14 aaa15 aaa16'.
+
+    Worse of that there still may be match even if the binary search
+    above didn't find matching element. E.g., if we're looking for
+    'aaa15' in an array of 'aaa1* aaa14 aaa16'. The binary search will
+    stop at 'aaa16'.
+  */
   for (; c >= 0; c--)
   {
     ftbw= ftb->list[c];
     if (ha_compare_text(ftb->charset, (uchar*)word, len,
                         (uchar*)ftbw->word + 1,ftbw->len - 1,
                         (my_bool)(ftbw->flags & FTB_FLAG_TRUNC), 0))
-      break;
+    {
+      if (ftb->with_scan & FTB_FLAG_TRUNC)
+        continue;
+      else
+        break;
+    }
     if (ftbw->docid[1] == ftb->info->lastpos)
       continue;
     ftbw->docid[1]= ftb->info->lastpos;