From cea642e32b3b12439df130d2b4c2067931d71b02 Mon Sep 17 00:00:00 2001
From: Kristofer Pettersson <kristofer.pettersson@sun.com>
Date: Tue, 3 Nov 2009 17:18:43 +0100
Subject: [PATCH] Moved test case for bug 31157 from query_cache.test to
 subselect.test

---
 mysql-test/r/query_cache.result |  2 ++
 mysql-test/r/subselect.result   | 14 ++++++++++++++
 mysql-test/t/query_cache.test   |  3 ++-
 mysql-test/t/subselect.test     | 15 +++++++++++++++
 4 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/query_cache.result b/mysql-test/r/query_cache.result
index 6cabc24d0e..2817bec219 100644
--- a/mysql-test/r/query_cache.result
+++ b/mysql-test/r/query_cache.result
@@ -1708,6 +1708,7 @@ Qcache_hits	2
 DROP TABLE t1;
 SET GLOBAL query_cache_size= default;
 End of 5.0 tests
+SET GLOBAL query_cache_size=1024*1024*512;
 CREATE TABLE t1 (a ENUM('rainbow'));
 INSERT INTO t1 VALUES (),(),(),(),();
 SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
@@ -1722,4 +1723,5 @@ SELECT 1 FROM t1 GROUP BY
 1
 1
 DROP TABLE t1;
+SET GLOBAL query_cache_size= default;
 End of 5.1 tests
diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 788abbd257..65ff82ca26 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -4563,4 +4563,18 @@ id	g	v	s
 51	50	NULL	l
 61	60	NULL	l
 drop table t1, t2;
+CREATE TABLE t1 (a ENUM('rainbow'));
+INSERT INTO t1 VALUES (),(),(),(),();
+SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
+1
+1
+DROP TABLE t1;
+CREATE TABLE t1 (a LONGBLOB);
+INSERT INTO t1 SET a = 'aaaa';
+INSERT INTO t1 SET a = 'aaaa';
+SELECT 1 FROM t1 GROUP BY
+(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
+1
+1
+DROP TABLE t1;
 End of 5.1 tests.
diff --git a/mysql-test/t/query_cache.test b/mysql-test/t/query_cache.test
index 7841e7274d..0edc9cca38 100644
--- a/mysql-test/t/query_cache.test
+++ b/mysql-test/t/query_cache.test
@@ -1294,7 +1294,7 @@ SET GLOBAL query_cache_size= default;
 # Bug #31157: Crash when select+order by the avg of some field within the
 # group by
 #
-
+SET GLOBAL query_cache_size=1024*1024*512;
 CREATE TABLE t1 (a ENUM('rainbow'));
 INSERT INTO t1 VALUES (),(),(),(),();
 SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
@@ -1305,6 +1305,7 @@ INSERT INTO t1 SET a = 'aaaa';
 SELECT 1 FROM t1 GROUP BY
   (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
 DROP TABLE t1;
+SET GLOBAL query_cache_size= default;
 
 --echo End of 5.1 tests
 
diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test
index 544017ebb9..17b15bc5a9 100644
--- a/mysql-test/t/subselect.test
+++ b/mysql-test/t/subselect.test
@@ -3544,4 +3544,19 @@ where v in(select v
            where t1.g=t2.g) is unknown;
 drop table t1, t2;
 
+#
+# Bug #31157: Crash when select+order by the avg of some field within the
+# group by
+#
+CREATE TABLE t1 (a ENUM('rainbow'));
+INSERT INTO t1 VALUES (),(),(),(),();
+SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
+DROP TABLE t1;
+CREATE TABLE t1 (a LONGBLOB);
+INSERT INTO t1 SET a = 'aaaa';
+INSERT INTO t1 SET a = 'aaaa';
+SELECT 1 FROM t1 GROUP BY
+  (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
+DROP TABLE t1;
+
 --echo End of 5.1 tests.
-- 
2.30.9