diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result
index f4cf5217fa710f33259648e90d4be559cf128a33..bfde408343ea459a73e04f22a32ad211efa4edc4 100644
--- a/mysql-test/r/group_by.result
+++ b/mysql-test/r/group_by.result
@@ -758,6 +758,15 @@ select date(left(f1+0,8)) from t1 group by 1;
 date(left(f1+0,8))
 2005-06-06
 drop table t1;
+create table t1(f1 varchar(5) key);
+insert into t1 values (1),(2);
+select sql_buffer_result max(f1) is null from t1;
+max(f1) is null
+0
+select sql_buffer_result max(f1)+1 from t1;
+max(f1)+1
+3
+drop table t1;
 CREATE TABLE t1 (n int);
 INSERT INTO t1 VALUES (1);
 SELECT n+1 AS n FROM t1 GROUP BY n;
diff --git a/mysql-test/r/olap.result b/mysql-test/r/olap.result
index df0ee09ea8ef7a2e339072a426c2d51b097c2c45..73dd3fc8aa2aa0e357b80c9fa20ce5d207f17d6e 100644
--- a/mysql-test/r/olap.result
+++ b/mysql-test/r/olap.result
@@ -579,7 +579,6 @@ a	LENGTH(a)	COUNT(*)
 1	1	1
 2	1	1
 NULL	NULL	2
-DROP TABLE t1;
 CREATE TABLE t1(id int, type char(1));
 INSERT INTO t1 VALUES
 (1,"A"),(2,"C"),(3,"A"),(4,"A"),(5,"B"),
diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test
index 8300b502518bc158ad4bec267434709bdbd1a70b..4631fad335225bda785af02591fcbc098cbbcdd7 100644
--- a/mysql-test/t/group_by.test
+++ b/mysql-test/t/group_by.test
@@ -575,6 +575,16 @@ CREATE TABLE t1 (id varchar(20) NOT NULL);
 INSERT INTO t1 VALUES ('trans1'), ('trans2');
 CREATE TABLE t2 (id varchar(20) NOT NULL, err_comment blob NOT NULL);
 INSERT INTO t2 VALUES ('trans1', 'a problem');
+#
+# BUG#12695: Item_func_isnull::update_used_tables
+# did not update const_item_cache
+#
+create table t1(f1 varchar(5) key);
+insert into t1 values (1),(2);
+select sql_buffer_result max(f1) is null from t1;
+select sql_buffer_result max(f1)+1 from t1;
+drop table t1;
+
 SELECT COUNT(DISTINCT(t1.id)), LEFT(err_comment, 256) AS comment
   FROM t1 LEFT JOIN t2 ON t1.id=t2.id GROUP BY comment;
 
diff --git a/mysql-test/t/olap.test b/mysql-test/t/olap.test
index 3a6a5e46f378c7a073dd2e6452d644a6d6fb8206..adaf6883d438e94ea12a46bf88795749df46e517 100644
--- a/mysql-test/t/olap.test
+++ b/mysql-test/t/olap.test
@@ -262,7 +262,6 @@ SELECT a, b, a AS c, COUNT(*) AS count FROM t1 GROUP BY a, b, c WITH ROLLUP;
 
 DROP TABLE t1;
 
-#
 # Bug #12885(1): derived table specified by a subquery with
 #                ROLLUP over expressions on not nullable group by attributes 
 #
diff --git a/sql/item_cmpfunc.h b/sql/item_cmpfunc.h
index 3bbc78f5db8d3ce174c16bddcadabaf321bef35c..09a0fa8c3575e50cee60aefafcd25ed92243ce79 100644
--- a/sql/item_cmpfunc.h
+++ b/sql/item_cmpfunc.h
@@ -910,7 +910,7 @@ public:
     else
     {
       args[0]->update_used_tables();
-      if (!(used_tables_cache=args[0]->used_tables()))
+      if ((const_item_cache= !(used_tables_cache= args[0]->used_tables())))
       {
 	/* Remember if the value is always NULL or never NULL */
 	cached_value= (longlong) args[0]->is_null();
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index 13587d8a4c3f41c672ecb0a003bc60a56bde3b78..4f991615bfa4ec1ff0c71e65f766f7b2bb43efcf 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -2743,7 +2743,7 @@ int dump_leaf_key(byte* key, element_count count __attribute__((unused)),
   if (item->no_appended)
     item->no_appended= FALSE;
   else
-    item->result.append(*item->separator);
+    result->append(*item->separator);
 
   tmp.length(0);
 
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 497d20c2ab82e345b567f36cbd1d179eb96adc28..975014b9780099903e5467bfed45257eebd36314 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -383,8 +383,10 @@ void THD::cleanup(void)
   my_free((char*) variables.time_format, MYF(MY_ALLOW_ZERO_PTR));
   my_free((char*) variables.date_format, MYF(MY_ALLOW_ZERO_PTR));
   my_free((char*) variables.datetime_format, MYF(MY_ALLOW_ZERO_PTR));
+  
   sp_cache_clear(&sp_proc_cache);
   sp_cache_clear(&sp_func_cache);
+
   if (global_read_lock)
     unlock_global_read_lock(this);
   if (ull)
@@ -424,9 +426,6 @@ THD::~THD()
 
   ha_close_connection(this);
 
-  sp_cache_clear(&sp_proc_cache);
-  sp_cache_clear(&sp_func_cache);
-
   DBUG_PRINT("info", ("freeing host"));
   if (host != my_localhost)			// If not pointer to constant
     safeFree(host);