From e603c1efde1bb20d983840a282dc4c5b7beaf8e6 Mon Sep 17 00:00:00 2001
From: unknown <bell@laptop.sanja.is.com.ua>
Date: Sat, 20 Sep 2003 18:30:36 +0300
Subject: [PATCH] fixed processing aggregate functions with static tables
 (BUG#1318)

mysql-test/r/func_group.result:
  test of BUG#1318
mysql-test/r/subselect.result:
  correct results
mysql-test/t/func_group.test:
  test of BUG#1318
sql/sql_select.cc:
  hixed processing of static tables
---
 mysql-test/r/func_group.result | 9 +++++++++
 mysql-test/r/subselect.result  | 1 +
 mysql-test/t/func_group.test   | 9 +++++++++
 sql/sql_select.cc              | 8 +++++---
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result
index 3af75b3c5b..8b7581c3cc 100644
--- a/mysql-test/r/func_group.result
+++ b/mysql-test/r/func_group.result
@@ -618,3 +618,12 @@ select coercibility(max(a)) from t1;
 coercibility(max(a))
 3
 drop table t1;
+create table t1 (a int);
+insert into t1 values (1);
+select max(a) as b from t1 having b=1;
+b
+1
+select a from t1 having a=1;
+a
+1
+drop table t1;
diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result
index 703360dc7b..6131dbdc62 100644
--- a/mysql-test/r/subselect.result
+++ b/mysql-test/r/subselect.result
@@ -1338,6 +1338,7 @@ insert into t1 values (1);
 insert into t2 values (1);
 select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
 s1
+1
 drop table t1,t2;
 create table t1 (s1 int);
 create table t2 (s1 int);
diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test
index 8a43716d85..ac3a1986d9 100644
--- a/mysql-test/t/func_group.test
+++ b/mysql-test/t/func_group.test
@@ -360,3 +360,12 @@ create table t1 (a char(10));
 insert into t1 values ('a'),('b'),('c');
 select coercibility(max(a)) from t1;
 drop table t1;
+
+#
+# aggregate functions on static tables
+#
+create table t1 (a int);
+insert into t1 values (1);
+select max(a) as b from t1 having b=1;
+select a from t1 having a=1;
+drop table t1;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index ab985ffbc6..91f54c95c4 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -5295,9 +5295,11 @@ do_select(JOIN *join,List<Item> *fields,TABLE *table,Procedure *procedure)
   join->send_records=0;
   if (join->tables == join->const_tables)
   {
-    if (!join->select_lex->dependent ||
-	((!join->conds || join->conds->val_int()) &&
-	 (!join->having || join->having->val_int())))
+    /*
+      HAVING will be chcked after processing aggregate functions,
+      But WHERE should checkd here (we alredy have read tables)
+    */
+    if(!join->conds || join->conds->val_int())
     {
       if (!(error=(*end_select)(join,join_tab,0)) || error == -3)
 	error=(*end_select)(join,join_tab,1);
-- 
2.30.9