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;
...@@ -1239,12 +1239,12 @@ select_insert::prepare(List<Item> &values) ...@@ -1239,12 +1239,12 @@ select_insert::prepare(List<Item> &values)
table->next_number_field=table->found_next_number_field; table->next_number_field=table->found_next_number_field;
thd->count_cuted_fields=1; /* calc cuted fields */ thd->count_cuted_fields=1; /* calc cuted fields */
thd->cuted_fields=0; thd->cuted_fields=0;
if (info.handle_duplicates != DUP_REPLACE) if (info.handle_duplicates != DUP_REPLACE)
table->file->extra(HA_EXTRA_WRITE_CACHE); table->file->extra(HA_EXTRA_WRITE_CACHE);
if (info.handle_duplicates == DUP_IGNORE || if (info.handle_duplicates == DUP_IGNORE ||
info.handle_duplicates == DUP_REPLACE) info.handle_duplicates == DUP_REPLACE)
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
table->file->deactivate_non_unique_index((ha_rows) 0); table->file->deactivate_non_unique_index((ha_rows) 0);
DBUG_RETURN(0); DBUG_RETURN(0);
} }
......
...@@ -2426,7 +2426,7 @@ mysql_init_query(THD *thd) ...@@ -2426,7 +2426,7 @@ mysql_init_query(THD *thd)
thd->lex.select_lex.item_list.empty(); thd->lex.select_lex.item_list.empty();
thd->lex.value_list.empty(); thd->lex.value_list.empty();
thd->lex.select_lex.table_list.elements=0; 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 = &thd->lex.select_lex;
thd->lex.select_lex.table_list.first=0; thd->lex.select_lex.table_list.first=0;
thd->lex.select_lex.table_list.next= (byte**) &thd->lex.select_lex.table_list.first; thd->lex.select_lex.table_list.next= (byte**) &thd->lex.select_lex.table_list.first;
...@@ -2444,7 +2444,7 @@ mysql_init_select(LEX *lex) ...@@ -2444,7 +2444,7 @@ mysql_init_select(LEX *lex)
select_lex->select_limit=current_thd->default_select_limit; select_lex->select_limit=current_thd->default_select_limit;
select_lex->offset_limit=0; select_lex->offset_limit=0;
select_lex->options=0; select_lex->linkage=UNSPECIFIED_TYPE; 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; lex->proc_list.first=0;
select_lex->order_list.elements=select_lex->group_list.elements=0; select_lex->order_list.elements=select_lex->group_list.elements=0;
select_lex->order_list.first=0; select_lex->order_list.first=0;
......
...@@ -21,104 +21,66 @@ ...@@ -21,104 +21,66 @@
#include "mysql_priv.h" #include "mysql_priv.h"
#include "sql_select.h"
/* Union of selects */ /* Union of selects */
int mysql_union(THD *thd,LEX *lex,uint no_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_LEX *sl, *for_order=&lex->select_lex; int res=0;
select_create *create_result; TABLE *table=(TABLE *)NULL; TABLE_LIST *resulting=(TABLE_LIST *)NULL;
List<Item> fields; TABLE *table=(TABLE *)NULL; TABLE_LIST *resulting=(TABLE_LIST *)NULL;
for (;for_order->next;for_order=for_order->next); for (;for_order->next;for_order=for_order->next);
ORDER *some_order = (ORDER *)for_order->order_list.first; 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; TABLE_LIST *tables=(TABLE_LIST*) sl->table_list.first;
if (!no) // First we do CREATE from SELECT select_insert *result;
{ if ((result=new select_insert(table,&list, DUP_IGNORE, true)))
lex->create_info.options=HA_LEX_CREATE_TMP_TABLE; {
lex->create_info.db_type=DB_TYPE_MYISAM; res=mysql_select(thd,tables,sl->item_list,
lex->create_info.row_type = ROW_TYPE_DEFAULT; sl->where,
lex->create_info.avg_row_length = 0; sl->ftfunc_list,
lex->create_info.max_rows=INT_MAX; lex->create_info.min_rows=0; (ORDER*) some_order,
lex->create_info.comment=lex->create_info.password=NullS; (ORDER*) sl->group_list.first,
lex->create_info.data_file_name=lex->create_info.index_file_name=NullS; sl->having,
lex->create_info.raid_type=lex->create_info.raid_chunks=0; (ORDER*) NULL,
lex->create_info.raid_chunksize=0; sl->options | thd->options,
lex->create_info.if_not_exists=false; result);
lex->create_info.used_fields=0; delete result;
if (res)
if ((create_result=new select_create(tables->db ? tables->db : thd->db, return res;
"ZVEK", &lex->create_info, }
lex->create_list, else
lex->key_list, return -1;
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_result *result; select_result *result;
List<Item> item_list;
List<Item_func_match> ftfunc_list; List<Item_func_match> ftfunc_list;
ftfunc_list.empty(); ftfunc_list.empty();
void(item_list.push_back(new Item_field(NULL,NULL,"*")));
if (lex->exchange) if (lex->exchange)
{ {
if (lex->exchange->dumpfile) if (lex->exchange->dumpfile)
...@@ -129,7 +91,7 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects) ...@@ -129,7 +91,7 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects)
else result=new select_send(); else result=new select_send();
if (result) if (result)
{ {
res=mysql_select(thd,resulting,item_list, res=mysql_select(thd,resulting,list,
NULL, NULL,
ftfunc_list, ftfunc_list,
(ORDER*) NULL, (ORDER*) NULL,
...@@ -144,6 +106,5 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects) ...@@ -144,6 +106,5 @@ int mysql_union(THD *thd,LEX *lex,uint no_of_selects)
} }
else else
res=-1; res=-1;
delete create_result;
return res; 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