From c39344528e4cb6122f10cc36a8cdfe54610b3ff8 Mon Sep 17 00:00:00 2001
From: unknown <Sinisa@sinisa.nasamreza.org>
Date: Wed, 25 Jul 2001 15:28:37 +0300
Subject: [PATCH] UNION's

sql/sql_insert.cc:
  Some tweaking for unions
sql/sql_parse.cc:
  Init of options for unions
sql/sql_unions.cc:
  T
---
 mysql-test/r/unions_one.result |  15 ++++
 mysql-test/t/unions_one.test   |  16 ++++
 sql/sql_insert.cc              |  12 +--
 sql/sql_parse.cc               |   4 +-
 sql/sql_unions.cc              | 135 ++++++++++++---------------------
 5 files changed, 87 insertions(+), 95 deletions(-)
 create mode 100644 mysql-test/r/unions_one.result
 create mode 100644 mysql-test/t/unions_one.test

diff --git a/mysql-test/r/unions_one.result b/mysql-test/r/unions_one.result
new file mode 100644
index 00000000000..66cffd9aec1
--- /dev/null
+++ b/mysql-test/r/unions_one.result
@@ -0,0 +1,15 @@
+a	b
+1 a
+2 b
+3 c
+4 d
+5 e
+6 f
+a	b
+1 a
+2 b
+3 c
+3 c
+4 d
+5 e
+6 f
diff --git a/mysql-test/t/unions_one.test b/mysql-test/t/unions_one.test
new file mode 100644
index 00000000000..54ade3aa453
--- /dev/null
+++ b/mysql-test/t/unions_one.test
@@ -0,0 +1,16 @@
+#
+# Test of unions
+#
+
+drop table if exists t1,t2;
+CREATE TABLE t1 (a int not null, b char (10) not null);
+insert into t1 values(1,"a"),(2,"b"),(3,"c");
+CREATE TABLE t2 (a int not null, b char (10) not null);
+insert into t2 values (3,"c"),(4,"d"),(5,"e"),(6,"f");
+
+
+select a,b from t1 union select a,b from t2;
+
+select a,b from t1 union all select a,b from t2;
+
+drop table t1,t2;
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 195bd559145..5ac55ca6eac 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -1239,12 +1239,12 @@ select_insert::prepare(List<Item> &values)
   table->next_number_field=table->found_next_number_field;
   thd->count_cuted_fields=1;			/* calc cuted fields */
   thd->cuted_fields=0;
-  if (info.handle_duplicates != DUP_REPLACE)
-    table->file->extra(HA_EXTRA_WRITE_CACHE);
-  if (info.handle_duplicates == DUP_IGNORE ||
-      info.handle_duplicates == DUP_REPLACE)
-    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
-  table->file->deactivate_non_unique_index((ha_rows) 0);
+	if (info.handle_duplicates != DUP_REPLACE)
+		table->file->extra(HA_EXTRA_WRITE_CACHE);
+	if (info.handle_duplicates == DUP_IGNORE ||
+			info.handle_duplicates == DUP_REPLACE)
+		table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
+	table->file->deactivate_non_unique_index((ha_rows) 0);
   DBUG_RETURN(0);
 }
 
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 0fe8ccb881f..59e64888f02 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -2426,7 +2426,7 @@ mysql_init_query(THD *thd)
   thd->lex.select_lex.item_list.empty();
   thd->lex.value_list.empty();
   thd->lex.select_lex.table_list.elements=0;
-  thd->free_list=0;
+  thd->free_list=0;  thd->lex.union_option=0;
   thd->lex.select = &thd->lex.select_lex;
   thd->lex.select_lex.table_list.first=0;
   thd->lex.select_lex.table_list.next= (byte**) &thd->lex.select_lex.table_list.first;
@@ -2444,7 +2444,7 @@ mysql_init_select(LEX *lex)
   select_lex->select_limit=current_thd->default_select_limit;
   select_lex->offset_limit=0;
   select_lex->options=0; select_lex->linkage=UNSPECIFIED_TYPE;
-  select_lex->select_number = 0;  lex->exchange = 0; lex->union_option=0;
+  select_lex->select_number = 0;  lex->exchange = 0;
   lex->proc_list.first=0;
   select_lex->order_list.elements=select_lex->group_list.elements=0;
   select_lex->order_list.first=0;
diff --git a/sql/sql_unions.cc b/sql/sql_unions.cc
index 71b74a505fc..b480415619b 100644
--- a/sql/sql_unions.cc
+++ b/sql/sql_unions.cc
@@ -21,104 +21,66 @@
 
 
 #include "mysql_priv.h"
-
+#include "sql_select.h"
 
 /* Union  of selects */
 
 
 int mysql_union(THD *thd,LEX *lex,uint no_of_selects) 
 {
-  SELECT_LEX *sl, *for_order=&lex->select_lex; uint no=0; int res=0;
-	select_create *create_result;
-  List<Item> fields; TABLE *table=(TABLE *)NULL; TABLE_LIST *resulting=(TABLE_LIST *)NULL;
+  SELECT_LEX *sl, *for_order=&lex->select_lex;  int res=0;
+  TABLE *table=(TABLE *)NULL; TABLE_LIST *resulting=(TABLE_LIST *)NULL;
   for (;for_order->next;for_order=for_order->next);
   ORDER *some_order = (ORDER *)for_order->order_list.first;
-  for (sl=&lex->select_lex;sl;sl=sl->next, no++)
+	List<Item> list;
+	List_iterator<Item> it(lex->select_lex.item_list);
+	Item *item;
+	TABLE_LIST *s=(TABLE_LIST*) lex->select_lex.table_list.first;
+	while ((item= it++))
+		if (list.push_back(item))
+			return -1;
+	if (setup_fields(thd,s,list,0,0))
+		return -1;
+	TMP_TABLE_PARAM *tmp_table_param= new TMP_TABLE_PARAM;
+  count_field_types(tmp_table_param,list,0);
+	tmp_table_param->end_write_records= HA_POS_ERROR; tmp_table_param->copy_field=0;
+  tmp_table_param->copy_field_count=tmp_table_param->field_count=
+    tmp_table_param->sum_func_count= tmp_table_param->func_count=0;
+  if (!(table=create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, !lex->union_option,
+			       0, 0, lex->select_lex.options | thd->options)))
+    return 1;
+	if (!(resulting = (TABLE_LIST *) thd->calloc(sizeof(TABLE_LIST))))
+		return 1;
+	resulting->db=s->db ? s->db : thd->db;
+	resulting->real_name=table->real_name;
+	resulting->name=table->table_name;
+	resulting->table=table;
+
+  for (sl=&lex->select_lex;sl;sl=sl->next)
   {
     TABLE_LIST *tables=(TABLE_LIST*) sl->table_list.first;
-    if (!no) // First we do CREATE from SELECT
-    {
-      lex->create_info.options=HA_LEX_CREATE_TMP_TABLE;
-			lex->create_info.db_type=DB_TYPE_MYISAM;
-      lex->create_info.row_type = ROW_TYPE_DEFAULT;
-			lex->create_info.avg_row_length = 0;
-			lex->create_info.max_rows=INT_MAX; lex->create_info.min_rows=0;
-			lex->create_info.comment=lex->create_info.password=NullS;
-			lex->create_info.data_file_name=lex->create_info.index_file_name=NullS;
-			lex->create_info.raid_type=lex->create_info.raid_chunks=0;
-			lex->create_info.raid_chunksize=0;
-			lex->create_info.if_not_exists=false;
-			lex->create_info.used_fields=0;
-
-      if ((create_result=new select_create(tables->db ? tables->db : thd->db,
-				    "ZVEK", &lex->create_info,
-				    lex->create_list,
-				    lex->key_list,
-				    sl->item_list,DUP_IGNORE,true)))
-      {
-				res=mysql_select(thd,tables,sl->item_list,
-												 sl->where,
-												 sl->ftfunc_list,
-												 (ORDER*) NULL,
-												 (ORDER*) sl->group_list.first,
-												 sl->having,
-												 (ORDER*) some_order,
-												 sl->options | thd->options,
-												 create_result);
-				if (res) 
-				{
-					create_result->abort();
-					delete create_result;
-					return res;
-				}
-				table=create_result->table;
-/*				List_iterator<Item> it(*(create_result->fields));
-				Item *item;
-				while ((item= it++))
-				fields.push_back(item);*/
-				if (!(resulting = (TABLE_LIST *) thd->calloc(sizeof(TABLE_LIST))))
-					return 1;
-				resulting->db=tables->db ? tables->db : thd->db;
-				resulting->real_name=table->real_name;
-				resulting->name=table->table_name;
-				resulting->table=table;
-      }
-      else
-				return -1;
-    }
-    else // Then we do INSERT from SELECT
-    {
-			select_insert *result;
-      if ((result=new select_insert(table, create_result->fields, DUP_IGNORE, true)))
-      {
-				res=mysql_select(thd,tables,sl->item_list,
-												 sl->where,
-                         sl->ftfunc_list,
-												 (ORDER*) some_order,
-												 (ORDER*) sl->group_list.first,
-												 sl->having,
-												 (ORDER*) NULL,
-												 sl->options | thd->options,
-												 result);
-				delete result;
-				if (res) 
-				{
-					delete create_result;
-					return res;
-				}
-      }
-      else
-			{
-				delete create_result;
-				return -1;
-			}
-    }
-  }
+		select_insert *result;
+		if ((result=new select_insert(table,&list, DUP_IGNORE, true)))
+		{
+			res=mysql_select(thd,tables,sl->item_list,
+											 sl->where,
+											 sl->ftfunc_list,
+											 (ORDER*) some_order,
+											 (ORDER*) sl->group_list.first,
+											 sl->having,
+											 (ORDER*) NULL,
+											 sl->options | thd->options,
+											 result);
+			delete result;
+			if (res) 
+				return res;
+		}
+		else
+			return -1;
+	}
   select_result *result;
-  List<Item> item_list;
   List<Item_func_match> ftfunc_list;
   ftfunc_list.empty();
-  void(item_list.push_back(new Item_field(NULL,NULL,"*")));
   if (lex->exchange)
   {
     if (lex->exchange->dumpfile)
@@ -129,7 +91,7 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects)
   else result=new select_send();
   if (result)
   {
-    res=mysql_select(thd,resulting,item_list,
+    res=mysql_select(thd,resulting,list,
 										 NULL,
 										 ftfunc_list,
 										 (ORDER*) NULL,
@@ -144,6 +106,5 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects)
   }
 	else
 		res=-1;
-  delete create_result;
   return res;
 }
-- 
2.30.9