mirror of
https://github.com/MariaDB/server.git
synced 2025-08-07 00:04:31 +03:00
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 ...
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -63,6 +63,31 @@ a
|
|||||||
select 1 from (select 1) as a;
|
select 1 from (select 1) as a;
|
||||||
1
|
1
|
||||||
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;
|
drop table if exists t1;
|
||||||
create table t1(a int not null, t char(8), index(a));
|
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;
|
SELECT * FROM (SELECT * FROM t1) as b ORDER BY a ASC LIMIT 0,20;
|
||||||
|
File diff suppressed because one or more lines are too long
@@ -24,6 +24,10 @@ drop table if exists t1.t2,t3;
|
|||||||
select * from (select 1) as a;
|
select * from (select 1) as a;
|
||||||
select a from (select 1 as a) as b;
|
select a from (select 1 as a) as b;
|
||||||
select 1 from (select 1) as a;
|
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;
|
drop table if exists t1;
|
||||||
create table t1(a int not null, t char(8), index(a));
|
create table t1(a int not null, t char(8), index(a));
|
||||||
disable_query_log;
|
disable_query_log;
|
||||||
|
@@ -419,7 +419,7 @@ int mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit,
|
|||||||
select_result *result);
|
select_result *result);
|
||||||
int mysql_explain_select(THD *thd, SELECT_LEX *sl, char const *type,
|
int mysql_explain_select(THD *thd, SELECT_LEX *sl, char const *type,
|
||||||
select_result *result);
|
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);
|
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,
|
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
|
||||||
Item_result_field ***copy_func, Field **from_field,
|
Item_result_field ***copy_func, Field **from_field,
|
||||||
|
@@ -324,7 +324,7 @@ void field_str::add()
|
|||||||
}
|
}
|
||||||
else
|
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)
|
if ((treemem += length) > pc->max_treemem)
|
||||||
{
|
{
|
||||||
room_in_tree = 0; // Remove tree, too big tree
|
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;
|
select_union *derived_result;
|
||||||
TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
|
TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
|
||||||
TMP_TABLE_PARAM tmp_table_param;
|
TMP_TABLE_PARAM tmp_table_param;
|
||||||
|
bool is_union=sl->next_select() && sl->next_select()->linkage == UNION_TYPE;
|
||||||
DBUG_ENTER("mysql_derived");
|
DBUG_ENTER("mysql_derived");
|
||||||
|
|
||||||
|
|
||||||
|
if (is_union && unit->create_total_list(thd, lex, &tables))
|
||||||
|
DBUG_RETURN(-1);
|
||||||
|
|
||||||
if (tables)
|
if (tables)
|
||||||
res= check_table_access(thd,SELECT_ACL, tables);
|
res= check_table_access(thd,SELECT_ACL, tables);
|
||||||
else
|
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 (!(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))
|
if (setup_fields(thd,tables,item_list,0,0,1))
|
||||||
{
|
{
|
||||||
res=-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));
|
bzero((char*) &tmp_table_param,sizeof(tmp_table_param));
|
||||||
tmp_table_param.field_count=item_list.elements;
|
tmp_table_param.field_count=item_list.elements;
|
||||||
if (!(table=create_tmp_table(thd, &tmp_table_param, item_list,
|
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 |
|
(sl->options | thd->options |
|
||||||
TMP_TABLE_ALL_COLUMNS),
|
TMP_TABLE_ALL_COLUMNS),
|
||||||
HA_POS_ERROR)))
|
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;
|
SELECT_LEX_NODE *save_current_select= lex->current_select;
|
||||||
lex->current_select= sl;
|
lex->current_select= sl;
|
||||||
|
if (is_union)
|
||||||
|
res=mysql_union(thd,lex,derived_result,unit);
|
||||||
|
else
|
||||||
res= mysql_select(thd, tables, sl->item_list,
|
res= mysql_select(thd, tables, sl->item_list,
|
||||||
sl->where, (ORDER *) sl->order_list.first,
|
sl->where, (ORDER *) sl->order_list.first,
|
||||||
(ORDER*) sl->group_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)
|
if (tables)
|
||||||
tables->table_list->table=tables->table; // to fix a problem in EXPLAIN
|
tables->table_list->table=tables->table; // to fix a problem in EXPLAIN
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (is_union)
|
||||||
|
unit->exclude();
|
||||||
else
|
else
|
||||||
sl->exclude();
|
sl->exclude();
|
||||||
|
}
|
||||||
t->db=(char *)"";
|
t->db=(char *)"";
|
||||||
t->derived=(SELECT_LEX *)0; // just in case ...
|
t->derived=(SELECT_LEX *)0; // just in case ...
|
||||||
table->file->info(HA_STATUS_VARIABLE);
|
table->file->info(HA_STATUS_VARIABLE);
|
||||||
|
@@ -995,6 +995,7 @@ void st_select_lex::init_select()
|
|||||||
use_index.empty();
|
use_index.empty();
|
||||||
ftfunc_list_alloc.empty();
|
ftfunc_list_alloc.empty();
|
||||||
ftfunc_list= &ftfunc_list_alloc;
|
ftfunc_list= &ftfunc_list_alloc;
|
||||||
|
if (linkage != UNION_TYPE)
|
||||||
linkage= UNSPECIFIED_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");
|
net_printf(thd,ER_WRONG_USAGE,"UNION","ORDER BY");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (sl->linkage == DERIVED_TABLE_TYPE)
|
if (sl->linkage == DERIVED_TABLE_TYPE && !sl->next_select())
|
||||||
continue;
|
continue;
|
||||||
for (SELECT_LEX_UNIT *inner= sl->first_inner_unit();
|
for (SELECT_LEX_UNIT *inner= sl->first_inner_unit();
|
||||||
inner;
|
inner;
|
||||||
|
@@ -162,7 +162,7 @@ int handle_select(THD *thd, LEX *lex, select_result *result)
|
|||||||
register SELECT_LEX *select_lex = &lex->select_lex;
|
register SELECT_LEX *select_lex = &lex->select_lex;
|
||||||
fix_tables_pointers(lex->all_selects_list);
|
fix_tables_pointers(lex->all_selects_list);
|
||||||
if (select_lex->next_select())
|
if (select_lex->next_select())
|
||||||
res=mysql_union(thd,lex,result);
|
res=mysql_union(thd,lex,result,&lex->unit);
|
||||||
else
|
else
|
||||||
res=mysql_select(thd,(TABLE_LIST*) select_lex->table_list.first,
|
res=mysql_select(thd,(TABLE_LIST*) select_lex->table_list.first,
|
||||||
select_lex->item_list,
|
select_lex->item_list,
|
||||||
|
@@ -24,10 +24,9 @@
|
|||||||
#include "mysql_priv.h"
|
#include "mysql_priv.h"
|
||||||
#include "sql_select.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");
|
DBUG_ENTER("mysql_union");
|
||||||
SELECT_LEX_UNIT *unit= &lex->unit;
|
|
||||||
int res= 0;
|
int res= 0;
|
||||||
if (!(res= unit->prepare(thd, result)))
|
if (!(res= unit->prepare(thd, result)))
|
||||||
res= unit->exec();
|
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_NODE *lex_select_save= thd->lex.current_select;
|
||||||
SELECT_LEX *sl;
|
SELECT_LEX *sl;
|
||||||
|
|
||||||
if (lex_select_save->linkage != DERIVED_TABLE_TYPE)
|
|
||||||
thd->lex.current_select=first_select();
|
thd->lex.current_select=first_select();
|
||||||
/* Global option */
|
/* Global option */
|
||||||
if (((void*)(global_parameters)) == ((void*)this))
|
if (((void*)(global_parameters)) == ((void*)this))
|
||||||
|
Reference in New Issue
Block a user