From 72065f34e674d5dcbb8a6c7d6e96609fbb3ecab7 Mon Sep 17 00:00:00 2001
From: unknown <Sinisa@sinisa.nasamreza.org>
Date: Sat, 22 Feb 2003 17:02:36 +0200
Subject: [PATCH] Allowing NULL values in UNION's with first SELECT having only
 NOT NULL columns.

---
 mysql-test/r/union.result | 27 +++++++++++++++++++++++++++
 mysql-test/t/union.test   | 17 +++++++++++++++++
 sql/sql_union.cc          |  5 ++++-
 3 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/union.result b/mysql-test/r/union.result
index cb3a8029a8..cf166f47f3 100644
--- a/mysql-test/r/union.result
+++ b/mysql-test/r/union.result
@@ -203,3 +203,30 @@ No tables used
 select 1 as a,(select a union select a);
 a	(select a union select a)
 1	1
+drop table if exists t1,t2;
+CREATE TABLE t1 (  id int(3) unsigned default '0') TYPE=MyISAM;
+INSERT INTO t1 (id) VALUES("1");
+CREATE TABLE t2 ( id int(3) unsigned default '0',  id_master int(5) default '0',  text1 varchar(5) default NULL,  text2 varchar(5) default NULL) TYPE=MyISAM;
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("1", "1",
+"foo1", "bar1");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("2", "1",
+"foo2", "bar2");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("3", "1", NULL,
+"bar3");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("4", "1",
+"foo4", "bar4");
+SELECT 1 AS id_master, 1 AS id, NULL AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;
+id_master	id	text1	text2
+1	1	NULL	ABCDE
+1	1		bar1
+1	2		bar2
+1	3	NULL	bar3
+1	4		bar4
+SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;
+id_master	id	text1	text2
+1	1	ABCDE	ABCDE
+1	1	foo1	bar1
+1	2	foo2	bar2
+1	3	NULL	bar3
+1	4	foo4	bar4
+drop table if exists t1,t2;
diff --git a/mysql-test/t/union.test b/mysql-test/t/union.test
index b46f54c5c4..af2dd15497 100644
--- a/mysql-test/t/union.test
+++ b/mysql-test/t/union.test
@@ -107,3 +107,20 @@ drop table t1,t2;
 --error 1096
 select * union select 1;
 select 1 as a,(select a union select a);
+--disable_warnings
+drop table if exists t1,t2;
+--enable_warnings
+CREATE TABLE t1 (  id int(3) unsigned default '0') TYPE=MyISAM;
+INSERT INTO t1 (id) VALUES("1");
+CREATE TABLE t2 ( id int(3) unsigned default '0',  id_master int(5) default '0',  text1 varchar(5) default NULL,  text2 varchar(5) default NULL) TYPE=MyISAM;
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("1", "1",
+"foo1", "bar1");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("2", "1",
+"foo2", "bar2");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("3", "1", NULL,
+"bar3");
+INSERT INTO t2 (id, id_master, text1, text2) VALUES("4", "1",
+"foo4", "bar4");
+SELECT 1 AS id_master, 1 AS id, NULL AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;
+SELECT 1 AS id_master, 1 AS id, 'ABCDE' AS text1, 'ABCDE' AS text2 UNION SELECT id_master, t2.id, text1, text2 FROM t1 LEFT JOIN t2 ON t1.id = t2.id_master;
+drop table if exists t1,t2;
diff --git a/sql/sql_union.cc b/sql/sql_union.cc
index a9ccff0364..feaa8371ac 100644
--- a/sql/sql_union.cc
+++ b/sql/sql_union.cc
@@ -146,7 +146,10 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result,
     if (setup_tables(first_table) ||
 	setup_wild(thd, first_table, sl->item_list, 0, sl->with_wild))
       goto err;
-	
+    List_iterator<Item> it(sl->item_list);	
+    Item *item;
+    while((item=it++))
+      item->maybe_null=1;
     item_list= sl->item_list;
     sl->with_wild= 0;
     if (setup_ref_array(thd, &sl->ref_pointer_array, 
-- 
2.30.9