UNION's

parent 27caeeac
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
#
# 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;
......@@ -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;
......
......@@ -21,75 +21,46 @@
#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++)
{
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));
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++))
fields.push_back(item);*/
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=tables->db ? tables->db : thd->db;
resulting->db=s->db ? s->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
for (sl=&lex->select_lex;sl;sl=sl->next)
{
TABLE_LIST *tables=(TABLE_LIST*) sl->table_list.first;
select_insert *result;
if ((result=new select_insert(table, create_result->fields, DUP_IGNORE, true)))
if ((result=new select_insert(table,&list, DUP_IGNORE, true)))
{
res=mysql_select(thd,tables,sl->item_list,
sl->where,
......@@ -102,23 +73,14 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects)
result);
delete result;
if (res)
{
delete create_result;
return res;
}
}
else
{
delete create_result;
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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment