Commit 657b3112 authored by unknown's avatar unknown

derived tables with UNION's ...

Scrum task !!!!!


mysql-test/r/analyse.result:
  reverting a fix
mysql-test/r/derived.result:
  derived tables with UNION's ...
mysql-test/t/analyse.test:
  reverting a fix
mysql-test/t/derived.test:
  derived tables with UNION's ...
sql/mysql_priv.h:
  derived tables with UNION's ...
sql/sql_analyse.cc:
  reverting a fix
sql/sql_derived.cc:
  derived tables with UNION's ...
sql/sql_lex.cc:
  derived tables with UNION's ...
sql/sql_select.cc:
  derived tables with UNION's ...
sql/sql_union.cc:
  derived tables with UNION's ...
parent 62ce900c
This diff is collapsed.
......@@ -63,6 +63,31 @@ a
select 1 from (select 1) as a;
1
1
select * from (select * from t1 union select * from t1) a;
a b
1 a
2 b
3 c
select * from (select * from t1 union all select * from t1) a;
a b
1 a
2 b
3 c
3 c
1 a
2 b
3 c
3 c
explain select * from (select * from t1 union select * from t1) a;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3
2 DERIVED t1 ALL NULL NULL NULL NULL 4
3 UNION t1 ALL NULL NULL NULL NULL 4
explain select * from (select * from t1 union all select * from t1) a;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 8
2 DERIVED t1 ALL NULL NULL NULL NULL 4
3 UNION t1 ALL NULL NULL NULL NULL 4
drop table if exists t1;
create table t1(a int not null, t char(8), index(a));
SELECT * FROM (SELECT * FROM t1) as b ORDER BY a ASC LIMIT 0,20;
......
This diff is collapsed.
......@@ -24,6 +24,10 @@ drop table if exists t1.t2,t3;
select * from (select 1) as a;
select a from (select 1 as a) as b;
select 1 from (select 1) as a;
select * from (select * from t1 union select * from t1) a;
select * from (select * from t1 union all select * from t1) a;
explain select * from (select * from t1 union select * from t1) a;
explain select * from (select * from t1 union all select * from t1) a;
drop table if exists t1;
create table t1(a int not null, t char(8), index(a));
disable_query_log;
......
......@@ -419,7 +419,7 @@ int mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit,
select_result *result);
int mysql_explain_select(THD *thd, SELECT_LEX *sl, char const *type,
select_result *result);
int mysql_union(THD *thd, LEX *lex,select_result *result);
int mysql_union(THD *thd, LEX *lex,select_result *result,SELECT_LEX_UNIT *unit);
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *s, TABLE_LIST *t);
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
Item_result_field ***copy_func, Field **from_field,
......
......@@ -324,7 +324,7 @@ void field_str::add()
}
else
{
// bzero((char*) &s, sizeof(s)); // Let tree handle free of this
bzero((char*) &s, sizeof(s)); // Let tree handle free of this
if ((treemem += length) > pc->max_treemem)
{
room_in_tree = 0; // Remove tree, too big tree
......
......@@ -41,8 +41,13 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
select_union *derived_result;
TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
TMP_TABLE_PARAM tmp_table_param;
bool is_union=sl->next_select() && sl->next_select()->linkage == UNION_TYPE;
DBUG_ENTER("mysql_derived");
if (is_union && unit->create_total_list(thd, lex, &tables))
DBUG_RETURN(-1);
if (tables)
res= check_table_access(thd,SELECT_ACL, tables);
else
......@@ -58,6 +63,19 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
if (!(res=open_and_lock_tables(thd,tables)))
{
if (is_union)
{
for (SELECT_LEX *sel= sl;
sel;
sel= sel->next_select())
{
for (TABLE_LIST *cursor= (TABLE_LIST *)sel->table_list.first;
cursor;
cursor=cursor->next)
cursor->table= cursor->table_list->table;
}
}
if (setup_fields(thd,tables,item_list,0,0,1))
{
res=-1;
......@@ -66,7 +84,7 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
bzero((char*) &tmp_table_param,sizeof(tmp_table_param));
tmp_table_param.field_count=item_list.elements;
if (!(table=create_tmp_table(thd, &tmp_table_param, item_list,
(ORDER*) 0, 0, 1,
(ORDER*) 0, is_union && !unit->union_option, 1,
(sl->options | thd->options |
TMP_TABLE_ALL_COLUMNS),
HA_POS_ERROR)))
......@@ -87,6 +105,9 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
SELECT_LEX_NODE *save_current_select= lex->current_select;
lex->current_select= sl;
if (is_union)
res=mysql_union(thd,lex,derived_result,unit);
else
res= mysql_select(thd, tables, sl->item_list,
sl->where, (ORDER *) sl->order_list.first,
(ORDER*) sl->group_list.first,
......@@ -111,8 +132,13 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
if (tables)
tables->table_list->table=tables->table; // to fix a problem in EXPLAIN
}
else
{
if (is_union)
unit->exclude();
else
sl->exclude();
}
t->db=(char *)"";
t->derived=(SELECT_LEX *)0; // just in case ...
table->file->info(HA_STATUS_VARIABLE);
......
......@@ -995,6 +995,7 @@ void st_select_lex::init_select()
use_index.empty();
ftfunc_list_alloc.empty();
ftfunc_list= &ftfunc_list_alloc;
if (linkage != UNION_TYPE)
linkage= UNSPECIFIED_TYPE;
}
......@@ -1206,7 +1207,7 @@ bool st_select_lex_unit::create_total_list_n_last_return(THD *thd, st_lex *lex,
net_printf(thd,ER_WRONG_USAGE,"UNION","ORDER BY");
return 1;
}
if (sl->linkage == DERIVED_TABLE_TYPE)
if (sl->linkage == DERIVED_TABLE_TYPE && !sl->next_select())
continue;
for (SELECT_LEX_UNIT *inner= sl->first_inner_unit();
inner;
......
......@@ -162,7 +162,7 @@ int handle_select(THD *thd, LEX *lex, select_result *result)
register SELECT_LEX *select_lex = &lex->select_lex;
fix_tables_pointers(lex->all_selects_list);
if (select_lex->next_select())
res=mysql_union(thd,lex,result);
res=mysql_union(thd,lex,result,&lex->unit);
else
res=mysql_select(thd,(TABLE_LIST*) select_lex->table_list.first,
select_lex->item_list,
......
......@@ -24,10 +24,9 @@
#include "mysql_priv.h"
#include "sql_select.h"
int mysql_union(THD *thd, LEX *lex, select_result *result)
int mysql_union(THD *thd, LEX *lex, select_result *result,SELECT_LEX_UNIT *unit)
{
DBUG_ENTER("mysql_union");
SELECT_LEX_UNIT *unit= &lex->unit;
int res= 0;
if (!(res= unit->prepare(thd, result)))
res= unit->exec();
......@@ -125,7 +124,6 @@ int st_select_lex_unit::prepare(THD *thd, select_result *result)
SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
SELECT_LEX *sl;
if (lex_select_save->linkage != DERIVED_TABLE_TYPE)
thd->lex.current_select=first_select();
/* Global option */
if (((void*)(global_parameters)) == ((void*)this))
......
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