diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result
index 53c2058f3ec85e2330b7e9d61be2af1c1f7a4e44..2d668499df3e230b8f30d0203f8ae86d545eb145 100644
--- a/mysql-test/r/create.result
+++ b/mysql-test/r/create.result
@@ -1546,4 +1546,15 @@ SHOW INDEX FROM t1;
 Table	Non_unique	Key_name	Seq_in_index	Column_name	Collation	Cardinality	Sub_part	Packed	Null	Index_type	Comment
 t1	1	c1	1	c1	A	NULL	NULL	NULL	YES	BTREE	
 DROP TABLE t1;
+CREATE TABLE t1 (a INTEGER AUTO_INCREMENT PRIMARY KEY, b INTEGER NOT NULL);
+INSERT IGNORE INTO t1 (b) VALUES (5);
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+SELECT a FROM t1;
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+SELECT a FROM t1;
+ERROR 23000: Duplicate entry '1' for key 1
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+SELECT a FROM t1;
+ERROR 23000: Duplicate entry '1' for key 1
+DROP TABLE t1, t2;
 End of 5.0 tests
diff --git a/mysql-test/r/default.result b/mysql-test/r/default.result
index e2eda69c2e7b218ea6b6f9689250a0bd338f8d3a..5f27af882f28d7ac7b1268920a8156a9b5a26743 100644
--- a/mysql-test/r/default.result
+++ b/mysql-test/r/default.result
@@ -205,4 +205,19 @@ Warnings:
 Warning	1364	Field 'id' doesn't have a default value
 drop view v1;
 drop table t1;
+create table t1 (a int unique);
+create table t2 (b int default 10);
+insert into t1 (a) values (1);
+insert into t2 (b) values (1);
+insert into t1 (a) select b from t2 on duplicate key update a=default;
+select * from t1;
+a
+NULL
+insert into t1 (a) values (1);
+insert into t1 (a) select b from t2 on duplicate key update a=default(b);
+select * from t1;
+a
+NULL
+10
+drop table t1, t2;
 End of 5.0 tests.
diff --git a/mysql-test/r/func_regexp.result b/mysql-test/r/func_regexp.result
index c2dbfc3c3317825815073ba1070072ae1bc4a588..f3a1c625a092610faf479ce66c71ca3194c13d0e 100644
--- a/mysql-test/r/func_regexp.result
+++ b/mysql-test/r/func_regexp.result
@@ -114,4 +114,16 @@ End of 4.1 tests
 SELECT 1 REGEXP NULL;
 1 REGEXP NULL
 NULL
+SELECT '' REGEXP BINARY NULL;
+'' REGEXP BINARY NULL
+NULL
+SELECT NULL REGEXP BINARY NULL;
+NULL REGEXP BINARY NULL
+NULL
+SELECT 'A' REGEXP BINARY NULL;
+'A' REGEXP BINARY NULL
+NULL
+SELECT "ABC" REGEXP BINARY NULL;
+"ABC" REGEXP BINARY NULL
+NULL
 End of 5.0 tests
diff --git a/mysql-test/r/type_datetime.result b/mysql-test/r/type_datetime.result
index 3814e2f8348caccd80732fe0a77976a6da8a48bf..9c358c41215f7aa542fe32e9cf7f9a4c16ba6763 100644
--- a/mysql-test/r/type_datetime.result
+++ b/mysql-test/r/type_datetime.result
@@ -560,4 +560,27 @@ select * from t2
 where id in (select id from t2 as x1 where (t2.cur_date is null));
 id	cur_date
 drop table t1,t2;
+SELECT 
+CAST('NULL' AS DATE) <=> CAST('2008-01-01' AS DATE) n1, 
+CAST('2008-01-01' AS DATE) <=> CAST('NULL' AS DATE) n2,
+CAST('NULL' AS DATE) <=> CAST('NULL' AS DATE) n3,
+CAST('NULL' AS DATE) <> CAST('2008-01-01' AS DATE) n4, 
+CAST('2008-01-01' AS DATE) <> CAST('NULL' AS DATE) n5,
+CAST('NULL' AS DATE) <> CAST('NULL' AS DATE) n6,
+CAST('NULL' AS DATE) < CAST('2008-01-01' AS DATE) n7, 
+CAST('2008-01-01' AS DATE) < CAST('NULL' AS DATE) n8,
+CAST('NULL' AS DATE) < CAST('NULL' AS DATE) n9;
+n1	n2	n3	n4	n5	n6	n7	n8	n9
+0	0	1	NULL	NULL	NULL	NULL	NULL	NULL
+Warnings:
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
+Warning	1292	Truncated incorrect datetime value: 'NULL'
 End of 5.0 tests
diff --git a/mysql-test/t/create.test b/mysql-test/t/create.test
index 97a7ea71b295f3fa3ed264d8200972798c8903f7..61ee40477eeb79ed36778db52cca2fb5e2173844 100644
--- a/mysql-test/t/create.test
+++ b/mysql-test/t/create.test
@@ -1172,4 +1172,22 @@ SHOW INDEX FROM t1;
 DROP TABLE t1;
 
 
+#
+# Bug#38821: Assert table->auto_increment_field_not_null failed in open_table()
+#
+CREATE TABLE t1 (a INTEGER AUTO_INCREMENT PRIMARY KEY, b INTEGER NOT NULL);
+INSERT IGNORE INTO t1 (b) VALUES (5);
+
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+  SELECT a FROM t1;
+--error 1062
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+  SELECT a FROM t1;
+--error 1062
+CREATE TABLE IF NOT EXISTS t2 (a INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
+  SELECT a FROM t1;
+
+DROP TABLE t1, t2;
+
+
 --echo End of 5.0 tests
diff --git a/mysql-test/t/default.test b/mysql-test/t/default.test
index 14aa4b02cfe76ff633d43a068c1ca844cfab9264..b719cb834481cf5bccb58836790fb45be414b849 100644
--- a/mysql-test/t/default.test
+++ b/mysql-test/t/default.test
@@ -145,5 +145,24 @@ insert into t1 values(default);
 drop view v1;
 drop table t1;
 
+#
+# Bug #39002: crash with
+#             INSERT ... SELECT ... ON DUPLICATE KEY UPDATE col=DEFAULT
+#
+
+create table t1 (a int unique);
+create table t2 (b int default 10);
+insert into t1 (a) values (1);
+insert into t2 (b) values (1);
+
+insert into t1 (a) select b from t2 on duplicate key update a=default;
+select * from t1;
+
+insert into t1 (a) values (1);
+insert into t1 (a) select b from t2 on duplicate key update a=default(b);
+select * from t1;
+
+drop table t1, t2;
+
 --echo End of 5.0 tests.
 
diff --git a/mysql-test/t/func_regexp.test b/mysql-test/t/func_regexp.test
index 1b35fab9d54534decde6ef22d7e59b2f3f31d690..43fda0d11a8578cba7bb443f5199b34a1ac1ddf9 100644
--- a/mysql-test/t/func_regexp.test
+++ b/mysql-test/t/func_regexp.test
@@ -64,4 +64,14 @@ drop table t1;
 
 SELECT 1 REGEXP NULL;
 
+
+#
+# Bug #39021: SELECT REGEXP BINARY NULL never returns
+#
+
+SELECT '' REGEXP BINARY NULL;
+SELECT NULL REGEXP BINARY NULL;
+SELECT 'A' REGEXP BINARY NULL;
+SELECT "ABC" REGEXP BINARY NULL;
+
 --echo End of 5.0 tests
diff --git a/mysql-test/t/type_datetime.test b/mysql-test/t/type_datetime.test
index 722e4c082a7daa08a38c06390f66d9fd9d30cc8d..16a7bb66755ac4419128281d7e8010053448bd83 100644
--- a/mysql-test/t/type_datetime.test
+++ b/mysql-test/t/type_datetime.test
@@ -388,4 +388,20 @@ where id in (select id from t2 as x1 where (t2.cur_date is null));
 
 drop table t1,t2;
 
+
+#
+# Bug #37526: asymertic operator <=> in trigger
+#
+SELECT 
+  CAST('NULL' AS DATE) <=> CAST('2008-01-01' AS DATE) n1, 
+  CAST('2008-01-01' AS DATE) <=> CAST('NULL' AS DATE) n2,
+  CAST('NULL' AS DATE) <=> CAST('NULL' AS DATE) n3,
+  CAST('NULL' AS DATE) <> CAST('2008-01-01' AS DATE) n4, 
+  CAST('2008-01-01' AS DATE) <> CAST('NULL' AS DATE) n5,
+  CAST('NULL' AS DATE) <> CAST('NULL' AS DATE) n6,
+  CAST('NULL' AS DATE) < CAST('2008-01-01' AS DATE) n7, 
+  CAST('2008-01-01' AS DATE) < CAST('NULL' AS DATE) n8,
+  CAST('NULL' AS DATE) < CAST('NULL' AS DATE) n9;
+
+
 --echo End of 5.0 tests
diff --git a/sql/item.cc b/sql/item.cc
index 100ec8b5cfb206c1520582d7a7d4bc328dfb6e6a..a392931f2cd12a270b8b83c2096859e0aa9d0966 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -6046,6 +6046,13 @@ Item *Item_default_value::transform(Item_transformer transformer, byte *args)
 {
   DBUG_ASSERT(!current_thd->is_stmt_prepare());
 
+  /*
+    If the value of arg is NULL, then this object represents a constant,
+    so further transformation is unnecessary (and impossible).
+  */
+  if (!arg)
+    return 0;
+
   Item *new_item= arg->transform(transformer, args);
   if (!new_item)
     return 0;
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index 1994f6bf1a51679cc66ee0eacad9b8e096a7d86c..0410c7815909eb8e99e95429b5dc06c8f88bffc0 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -966,19 +966,24 @@ get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
        1    if items are equal or both are null
        0    otherwise
     If is_nulls_eq is FALSE:
-      -1   a < b or one of items is null
+      -1   a < b or at least one item is null
        0   a == b
        1   a > b
+    See the table:
+    is_nulls_eq | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
+    a_is_null   | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
+    b_is_null   | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 |
+    result      | 1 | 0 | 0 |0/1|-1 |-1 |-1 |-1/0/1|
 */
 
 int Arg_comparator::compare_datetime()
 {
-  bool is_null= FALSE;
+  bool a_is_null, b_is_null;
   ulonglong a_value, b_value;
 
   /* Get DATE/DATETIME/TIME value of the 'a' item. */
-  a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
-  if (!is_nulls_eq && is_null)
+  a_value= (*get_value_func)(thd, &a, &a_cache, *b, &a_is_null);
+  if (!is_nulls_eq && a_is_null)
   {
     if (owner)
       owner->null_value= 1;
@@ -986,14 +991,15 @@ int Arg_comparator::compare_datetime()
   }
 
   /* Get DATE/DATETIME/TIME value of the 'b' item. */
-  b_value= (*get_value_func)(thd, &b, &b_cache, *a, &is_null);
-  if (is_null)
+  b_value= (*get_value_func)(thd, &b, &b_cache, *a, &b_is_null);
+  if (a_is_null || b_is_null)
   {
     if (owner)
       owner->null_value= is_nulls_eq ? 0 : 1;
-    return is_nulls_eq ? 1 : -1;
+    return is_nulls_eq ? (a_is_null == b_is_null) : -1;
   }
 
+  /* Here we have two not-NULL values. */
   if (owner)
     owner->null_value= 0;
 
@@ -4341,8 +4347,20 @@ void Item_func_like::cleanup()
 
 #ifdef USE_REGEX
 
-bool
-Item_func_regex::regcomp(bool send_error)
+/**
+  @brief Compile regular expression.
+
+  @param[in]    send_error     send error message if any.
+
+  @details Make necessary character set conversion then 
+  compile regular expression passed in the args[1].
+
+  @retval    0     success.
+  @retval    1     error occurred.
+  @retval   -1     given null regular expression.
+ */
+
+int Item_func_regex::regcomp(bool send_error)
 {
   char buff[MAX_FIELD_WIDTH];
   String tmp(buff,sizeof(buff),&my_charset_bin);
@@ -4350,12 +4368,12 @@ Item_func_regex::regcomp(bool send_error)
   int error;
 
   if (args[1]->null_value)
-    return TRUE;
+    return -1;
 
   if (regex_compiled)
   {
     if (!stringcmp(res, &prev_regexp))
-      return FALSE;
+      return 0;
     prev_regexp.copy(*res);
     my_regfree(&preg);
     regex_compiled= 0;
@@ -4367,7 +4385,7 @@ Item_func_regex::regcomp(bool send_error)
     uint dummy_errors;
     if (conv.copy(res->ptr(), res->length(), res->charset(),
                   regex_lib_charset, &dummy_errors))
-      return TRUE;
+      return 1;
     res= &conv;
   }
 
@@ -4379,10 +4397,10 @@ Item_func_regex::regcomp(bool send_error)
       (void) my_regerror(error, &preg, buff, sizeof(buff));
       my_error(ER_REGEXP_ERROR, MYF(0), buff);
     }
-    return TRUE;
+    return 1;
   }
   regex_compiled= 1;
-  return FALSE;
+  return 0;
 }
 
 
@@ -4420,13 +4438,14 @@ Item_func_regex::fix_fields(THD *thd, Item **ref)
   const_item_cache=args[0]->const_item() && args[1]->const_item();
   if (!regex_compiled && args[1]->const_item())
   {
-    if (args[1]->null_value)
+    int comp_res= regcomp(TRUE);
+    if (comp_res == -1)
     {						// Will always return NULL
       maybe_null=1;
       fixed= 1;
       return FALSE;
     }
-    if (regcomp(TRUE))
+    else if (comp_res)
       return TRUE;
     regex_is_const= 1;
     maybe_null= args[0]->maybe_null;
diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h
index d250e1b366ade834911e9b76bdbe9961832c7994..1bd60ff37d976827849c5fe3f558547afab11552 100644
--- a/sql/item_cmpfunc.h
+++ b/sql/item_cmpfunc.h
@@ -1323,7 +1323,7 @@ class Item_func_regex :public Item_bool_func
   CHARSET_INFO *regex_lib_charset;
   int regex_lib_flags;
   String conv;
-  bool regcomp(bool send_error);
+  int regcomp(bool send_error);
 public:
   Item_func_regex(Item *a,Item *b) :Item_bool_func(a,b),
     regex_compiled(0),regex_is_const(0) {}
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 746a04e03f31644cdf0f84316fdca8d74e6e783c..3f43c902faa39da2b996ea87dedaf63b66ce22be 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -2897,7 +2897,11 @@ bool select_insert::send_data(List<Item> &values)
       DBUG_RETURN(1);
     }
   }
-  if (!(error= write_record(thd, table, &info)))
+  
+  error= write_record(thd, table, &info);
+  table->auto_increment_field_not_null= FALSE;
+  
+  if (!error)
   {
     if (table->triggers || info.handle_duplicates == DUP_UPDATE)
     {