From 9245ed4a12ce25214d60f93cbf20882cb2b78741 Mon Sep 17 00:00:00 2001
From: Sergey Glukhov <Sergey.Glukhov@sun.com>
Date: Fri, 26 Feb 2010 15:39:25 +0400
Subject: Bug#50995 Having clause on subquery result produces incorrect
 results. The problem is that cond->fix_fields(thd, 0) breaks condition(cuts
 off 'having'). The reason of that is that NULL valued Item pointer is present
 in the middle of Item list and it breaks the Item processing loop.

mysql-test/r/having.result:
  test case
mysql-test/t/having.test:
  test case
sql/item_cmpfunc.h:
  added ASSERT to make sure that we do not add NULL valued Item pointer
sql/sql_select.cc:
  skip adding an item to condition if Item pointer is NULL.
  skip adding a list to condition if this list is empty.
---
 mysql-test/t/having.test | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

(limited to 'mysql-test/t/having.test')

diff --git a/mysql-test/t/having.test b/mysql-test/t/having.test
index af9af4fe1fc..185ca4bdddb 100644
--- a/mysql-test/t/having.test
+++ b/mysql-test/t/having.test
@@ -442,4 +442,30 @@ INSERT INTO t1 VALUES (1, 1), (2,2), (3, NULL);
 SELECT b, COUNT(DISTINCT a) FROM t1 GROUP BY b HAVING b is NULL;
 DROP TABLE t1;
 
+
+--echo #
+--echo # Bug#50995 Having clause on subquery result produces incorrect results.
+--echo #
+
+CREATE TABLE t1
+(
+ id1 INT,
+ id2 INT NOT NULL,
+ INDEX id1(id2)
+);
+
+INSERT INTO t1 SET id1=1, id2=1;
+INSERT INTO t1 SET id1=2, id2=1;
+INSERT INTO t1 SET id1=3, id2=1;
+
+SELECT t1.id1,
+(SELECT 0 FROM DUAL
+ WHERE t1.id1=t1.id1) AS amount FROM t1
+WHERE t1.id2 = 1
+HAVING amount > 0
+ORDER BY t1.id1;
+
+DROP TABLE t1;
+
+
 --echo End of 5.0 tests
-- 
cgit v1.2.1