diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index f8df72c6709..5453345d50c 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -4875,4 +4875,31 @@ f1 f1 f1 f1 f2 f1 f1 18 9 NULL NULL NULL 5 7 SET SESSION join_buffer_size = DEFAULT; DROP TABLE t1,t2,t3,t4,t5,t6; +CREATE TABLE t1(f1 int UNSIGNED) engine=myisam; +INSERT INTO t1 VALUES (3),(2),(1); +set sql_buffer_result=0; +SELECT f1 FROM t1 GROUP BY 1; +f1 +1 +2 +3 +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +f1 +3 +SELECT 1 FROM t1 GROUP BY 1; +1 +1 +set sql_buffer_result=1; +SELECT f1 FROM t1 GROUP BY 1; +f1 +1 +2 +3 +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +f1 +3 +SELECT 1 FROM t1 GROUP BY 1; +1 +1 +drop table t1; End of 5.1 tests diff --git a/mysql-test/r/select_pkeycache.result b/mysql-test/r/select_pkeycache.result index f8df72c6709..5453345d50c 100644 --- a/mysql-test/r/select_pkeycache.result +++ b/mysql-test/r/select_pkeycache.result @@ -4875,4 +4875,31 @@ f1 f1 f1 f1 f2 f1 f1 18 9 NULL NULL NULL 5 7 SET SESSION join_buffer_size = DEFAULT; DROP TABLE t1,t2,t3,t4,t5,t6; +CREATE TABLE t1(f1 int UNSIGNED) engine=myisam; +INSERT INTO t1 VALUES (3),(2),(1); +set sql_buffer_result=0; +SELECT f1 FROM t1 GROUP BY 1; +f1 +1 +2 +3 +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +f1 +3 +SELECT 1 FROM t1 GROUP BY 1; +1 +1 +set sql_buffer_result=1; +SELECT f1 FROM t1 GROUP BY 1; +f1 +1 +2 +3 +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +f1 +3 +SELECT 1 FROM t1 GROUP BY 1; +1 +1 +drop table t1; End of 5.1 tests diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 1139e7acc60..ef1894a5405 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -4121,4 +4121,21 @@ SET SESSION join_buffer_size = DEFAULT; DROP TABLE t1,t2,t3,t4,t5,t6; +# +# Bug #780425: "sql_buffer_result=1 gives wrong result for GROUP BY with a +# constant expression" +# + +CREATE TABLE t1(f1 int UNSIGNED) engine=myisam; +INSERT INTO t1 VALUES (3),(2),(1); +set sql_buffer_result=0; +SELECT f1 FROM t1 GROUP BY 1; +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +SELECT 1 FROM t1 GROUP BY 1; +set sql_buffer_result=1; +SELECT f1 FROM t1 GROUP BY 1; +SELECT f1 FROM t1 GROUP BY '123' = 'abc'; +SELECT 1 FROM t1 GROUP BY 1; +drop table t1; + --echo End of 5.1 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index defd7ed7c32..d1fcb5633bb 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1295,7 +1295,10 @@ JOIN::optimize() DBUG_RETURN(1); } if (old_group_list && !group_list) + { + DBUG_ASSERT(group); select_distinct= 0; + } } if (!group_list && group) { @@ -1303,6 +1306,7 @@ JOIN::optimize() simple_order=1; select_distinct= 0; // No need in distinct for 1 row group_optimized_away= 1; + implicit_grouping= TRUE; } calc_group_buffer(this, group_list);