From a36145a8f2ba164bffe513c887697548b38955df Mon Sep 17 00:00:00 2001
From: unknown <serg@serg.mylan>
Date: Tue, 18 Nov 2003 22:06:47 +0100
Subject: [PATCH] Bug#1826, HANDLER+ALTER TABLE=crash (unfortunately, it cannot
 be tested in mysql-test suite) more user variable tests

mysql-test/r/user_var.result:
  more user variable tests (just to have this behaviour written down somewhere)
mysql-test/t/user_var.test:
  more user variable tests (just to have this behaviour written down somewhere)
sql/sql_handler.cc:
  Bug#1826, HANDLER+ALTER TABLE=crash
  (unfortunately, it cannot be tested in mysql-test suite)
---
 mysql-test/r/user_var.result | 19 +++++++++++++++++++
 mysql-test/t/user_var.test   | 11 +++++++++++
 sql/sql_handler.cc           | 13 +++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/mysql-test/r/user_var.result b/mysql-test/r/user_var.result
index d7c41d5dbc..3e83438b27 100644
--- a/mysql-test/r/user_var.result
+++ b/mysql-test/r/user_var.result
@@ -97,3 +97,22 @@ drop table t1;
 select @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b, @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b;
 @a:=10	@b:=2	@a>@b	@a:="10"	@b:="2"	@a>@b	@a:=10	@b:=2	@a>@b	@a:="10"	@b:="2"	@a>@b
 10	2	1	10	2	1	10	2	1	10	2	1
+create table t1 (i int not null);
+insert t1 values (1),(2),(2),(3),(3),(3);
+select @a:=0;
+@a:=0
+0
+select @a, @a:=@a+count(*), count(*), @a from t1 group by i;
+@a	@a:=@a+count(*)	count(*)	@a
+0	1	1	0
+0	2	2	0
+0	3	3	0
+select @a:=0;
+@a:=0
+0
+select @a+0, @a:=@a+0+count(*), count(*), @a+0 from t1 group by i;
+@a+0	@a:=@a+0+count(*)	count(*)	@a+0
+0	1	1	0
+1	3	2	0
+3	6	3	0
+drop table t1;
diff --git a/mysql-test/t/user_var.test b/mysql-test/t/user_var.test
index 56528405a2..9cb5d5e1eb 100644
--- a/mysql-test/t/user_var.test
+++ b/mysql-test/t/user_var.test
@@ -53,3 +53,14 @@ drop table t1;
 
 # just for fun :)
 select @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b, @a:=10, @b:=2, @a>@b, @a:="10", @b:="2", @a>@b;
+
+#
+# bug#1739
+# Item_func_set_user_var sets update_query_id, Item_func_get_user_var checks it
+#
+create table t1 (i int not null);
+insert t1 values (1),(2),(2),(3),(3),(3);
+select @a:=0; select @a, @a:=@a+count(*), count(*), @a from t1 group by i;
+select @a:=0; select @a+0, @a:=@a+0+count(*), count(*), @a+0 from t1 group by i;
+drop table t1;
+
diff --git a/sql/sql_handler.cc b/sql/sql_handler.cc
index dd8dfb3116..208545a435 100644
--- a/sql/sql_handler.cc
+++ b/sql/sql_handler.cc
@@ -285,7 +285,20 @@ static TABLE **find_table_ptr_by_name(THD *thd, const char *db,
   {
     if (!memcmp(table->table_cache_key, db, dblen) &&
         !my_strcasecmp((is_alias ? table->table_name : table->real_name),table_name))
+    {
+      if (table->version != refresh_version)
+      {
+        VOID(pthread_mutex_lock(&LOCK_open));
+        if (close_thread_table(thd, ptr))
+        {
+          /* Tell threads waiting for refresh that something has happened */
+          VOID(pthread_cond_broadcast(&COND_refresh));
+        }
+        VOID(pthread_mutex_unlock(&LOCK_open));
+        continue;
+      }
       break;
+    }
     ptr=&(table->next);
   }
   return ptr;
-- 
2.30.9