2003-01-07 10:45:06 +01:00
|
|
|
/* Copyright (C) 2002-2003 MySQL AB
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Derived tables
|
2003-01-07 10:45:06 +01:00
|
|
|
These were introduced by Sinisa <sinisa@mysql.com>
|
2002-03-26 14:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
|
|
#include "sql_select.h"
|
|
|
|
#include "sql_acl.h"
|
|
|
|
|
2002-12-14 14:13:25 +01:00
|
|
|
/*
|
|
|
|
Resolve derived tables in all queries
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
|
|
|
|
thd Thread handle
|
|
|
|
lex LEX for this thread
|
|
|
|
unit node that contains all SELECT's for derived tables
|
|
|
|
t TABLE_LIST for the upper SELECT
|
|
|
|
|
|
|
|
IMPLEMENTATION
|
2003-01-07 10:45:06 +01:00
|
|
|
Derived table is resolved with temporary table. It is created based on the
|
|
|
|
queries defined. After temporary table is created, if this is not EXPLAIN,
|
|
|
|
then the entire unit / node is deleted. unit is deleted if UNION is used
|
|
|
|
for derived table and node is deleted is it is a simple SELECT.
|
|
|
|
|
|
|
|
After table creation, the above TABLE_LIST is updated with a new table.
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
This function is called before any command containing derived table
|
|
|
|
is executed.
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
Derived tables is stored in thd->derived_tables and freed in
|
|
|
|
close_thread_tables()
|
2002-12-14 14:13:25 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
TODO
|
|
|
|
Move creation of derived tables in open_and_lock_tables()
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
0 ok
|
|
|
|
1 Error
|
|
|
|
-1 Error and error message given
|
2002-12-14 14:13:25 +01:00
|
|
|
*/
|
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2003-02-12 20:55:37 +01:00
|
|
|
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit,
|
|
|
|
TABLE_LIST *org_table_list)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-11-23 01:01:15 +01:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
2002-03-26 14:06:05 +01:00
|
|
|
TABLE *table;
|
2003-01-07 10:45:06 +01:00
|
|
|
int res;
|
2002-03-26 14:06:05 +01:00
|
|
|
select_union *derived_result;
|
2003-11-23 01:01:15 +01:00
|
|
|
TABLE_LIST *tables= (TABLE_LIST *)first_select->table_list.first;
|
|
|
|
bool is_union= first_select->next_select() &&
|
|
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
|
|
|
bool is_subsel= first_select->first_inner_unit() ? 1: 0;
|
2003-07-03 01:30:52 +02:00
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
2003-01-07 10:45:06 +01:00
|
|
|
DBUG_ENTER("mysql_derived");
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2003-01-07 10:45:06 +01:00
|
|
|
/*
|
|
|
|
In create_total_list, derived tables have to be treated in case of
|
|
|
|
EXPLAIN, This is because unit/node is not deleted in that
|
|
|
|
case. Current code in this function has to be improved to
|
|
|
|
recognize better when this function is called from derived tables
|
|
|
|
and when from other functions.
|
|
|
|
*/
|
2003-01-26 20:30:35 +01:00
|
|
|
if ((is_union || is_subsel) && unit->create_total_list(thd, lex, &tables, 1))
|
2002-12-12 15:09:06 +01:00
|
|
|
DBUG_RETURN(-1);
|
|
|
|
|
2003-02-12 20:55:37 +01:00
|
|
|
/*
|
|
|
|
We have to do access checks here as this code is executed before any
|
|
|
|
sql command is started to execute.
|
|
|
|
*/
|
2003-09-26 12:33:13 +02:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2002-03-26 14:06:05 +01:00
|
|
|
if (tables)
|
2003-09-26 12:33:13 +02:00
|
|
|
res= check_table_access(thd,SELECT_ACL, tables,0);
|
2002-03-26 14:06:05 +01:00
|
|
|
else
|
2003-09-26 12:33:13 +02:00
|
|
|
res= check_access(thd, SELECT_ACL, any_db,0,0,0);
|
2002-03-26 14:06:05 +01:00
|
|
|
if (res)
|
2003-09-04 14:12:20 +02:00
|
|
|
DBUG_RETURN(1);
|
2003-09-26 12:33:13 +02:00
|
|
|
#endif
|
|
|
|
|
2002-11-09 14:40:46 +01:00
|
|
|
if (!(res=open_and_lock_tables(thd,tables)))
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-01-26 20:30:35 +01:00
|
|
|
if (is_union || is_subsel)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
The following code is a re-do of fix_tables_pointers() found
|
|
|
|
in sql_select.cc for UNION's within derived tables. The only
|
|
|
|
difference is in navigation, as in derived tables we care for
|
|
|
|
this level only.
|
|
|
|
|
|
|
|
*/
|
|
|
|
fix_tables_pointers(unit);
|
|
|
|
}
|
|
|
|
|
2003-11-28 11:18:13 +01:00
|
|
|
if (!(derived_result= new select_union(0)))
|
2003-11-23 01:01:15 +01:00
|
|
|
DBUG_RETURN(1); // out of memory
|
|
|
|
|
2003-11-23 20:26:43 +01:00
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
2003-12-10 21:46:14 +01:00
|
|
|
if ((res= unit->prepare(thd, derived_result, 0)))
|
2003-01-29 18:42:39 +01:00
|
|
|
goto exit;
|
2003-06-12 15:52:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
This is done in order to redo all field optimisations when any of the
|
|
|
|
involved tables is used in the outer query
|
|
|
|
*/
|
|
|
|
if (tables)
|
|
|
|
{
|
|
|
|
for (TABLE_LIST *cursor= tables; cursor; cursor= cursor->next)
|
|
|
|
cursor->table->clear_query_id= 1;
|
|
|
|
}
|
2003-01-29 18:42:39 +01:00
|
|
|
|
2003-11-28 11:18:13 +01:00
|
|
|
derived_result->tmp_table_param.init();
|
2003-11-26 19:12:26 +01:00
|
|
|
derived_result->tmp_table_param.field_count= unit->types.elements;
|
2003-05-21 20:39:58 +02:00
|
|
|
/*
|
|
|
|
Temp table is created so that it hounours if UNION without ALL is to be
|
|
|
|
processed
|
|
|
|
*/
|
2003-11-26 19:12:26 +01:00
|
|
|
if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param,
|
|
|
|
unit->types, (ORDER*) 0,
|
2003-01-26 20:30:35 +01:00
|
|
|
is_union && !unit->union_option, 1,
|
2003-11-23 01:01:15 +01:00
|
|
|
(first_select->options | thd->options |
|
2003-01-26 20:30:35 +01:00
|
|
|
TMP_TABLE_ALL_COLUMNS),
|
2003-08-12 14:04:49 +02:00
|
|
|
HA_POS_ERROR,
|
|
|
|
org_table_list->alias)))
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-01-07 10:45:06 +01:00
|
|
|
res= -1;
|
2002-03-26 14:06:05 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2003-11-23 01:01:15 +01:00
|
|
|
derived_result->set_table(table);
|
|
|
|
|
|
|
|
if (is_union)
|
|
|
|
res= mysql_union(thd, lex, derived_result, unit);
|
|
|
|
else
|
2004-01-14 14:15:42 +01:00
|
|
|
{
|
|
|
|
unit->offset_limit_cnt= first_select->offset_limit;
|
|
|
|
unit->select_limit_cnt= first_select->select_limit+
|
|
|
|
first_select->offset_limit;
|
|
|
|
if (unit->select_limit_cnt < first_select->select_limit)
|
|
|
|
unit->select_limit_cnt= HA_POS_ERROR;
|
|
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
|
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
|
|
|
|
lex->current_select= first_select;
|
2003-11-23 01:01:15 +01:00
|
|
|
res= mysql_select(thd, &first_select->ref_pointer_array,
|
|
|
|
(TABLE_LIST*) first_select->table_list.first,
|
|
|
|
first_select->with_wild,
|
|
|
|
first_select->item_list, first_select->where,
|
|
|
|
(first_select->order_list.elements+
|
|
|
|
first_select->group_list.elements),
|
|
|
|
(ORDER *) first_select->order_list.first,
|
|
|
|
(ORDER *) first_select->group_list.first,
|
|
|
|
first_select->having, (ORDER*) NULL,
|
|
|
|
(first_select->options | thd->options |
|
|
|
|
SELECT_NO_UNLOCK),
|
|
|
|
derived_result, unit, first_select);
|
2004-01-14 14:15:42 +01:00
|
|
|
}
|
2003-11-23 01:01:15 +01:00
|
|
|
|
|
|
|
if (!res)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-11-23 01:01:15 +01:00
|
|
|
/*
|
|
|
|
Here we entirely fix both TABLE_LIST and list of SELECT's as
|
|
|
|
there were no derived tables
|
|
|
|
*/
|
|
|
|
if (derived_result->flush())
|
|
|
|
res= 1;
|
2002-12-12 15:09:06 +01:00
|
|
|
else
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2003-11-23 01:01:15 +01:00
|
|
|
org_table_list->real_name=table->real_name;
|
|
|
|
org_table_list->table=table;
|
|
|
|
table->derived_select_number= first_select->select_number;
|
|
|
|
table->tmp_table= TMP_TABLE;
|
2003-09-26 12:33:13 +02:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2003-11-23 01:01:15 +01:00
|
|
|
org_table_list->grant.privilege= SELECT_ACL;
|
2003-09-26 12:33:13 +02:00
|
|
|
#endif
|
2003-11-23 01:01:15 +01:00
|
|
|
if (lex->describe)
|
|
|
|
{
|
|
|
|
// to fix a problem in EXPLAIN
|
|
|
|
if (tables)
|
2002-11-30 18:53:31 +01:00
|
|
|
{
|
2003-11-23 01:01:15 +01:00
|
|
|
for (TABLE_LIST *cursor= tables; cursor; cursor= cursor->next)
|
|
|
|
if (cursor->table_list)
|
|
|
|
cursor->table_list->table=cursor->table;
|
2002-11-30 18:53:31 +01:00
|
|
|
}
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
2003-11-23 01:01:15 +01:00
|
|
|
else
|
2004-01-06 12:13:04 +01:00
|
|
|
{
|
2003-11-23 01:01:15 +01:00
|
|
|
unit->exclude_tree();
|
2004-01-06 12:13:04 +01:00
|
|
|
unit->cleanup();
|
|
|
|
}
|
2003-11-23 01:01:15 +01:00
|
|
|
org_table_list->db= (char *)"";
|
|
|
|
// Force read of table stats in the optimizer
|
|
|
|
table->file->info(HA_STATUS_VARIABLE);
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
2003-11-23 01:01:15 +01:00
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
if (res)
|
2003-01-26 20:30:35 +01:00
|
|
|
free_tmp_table(thd, table);
|
|
|
|
else
|
|
|
|
{
|
2003-02-12 20:55:37 +01:00
|
|
|
/* Add new temporary table to list of open derived tables */
|
2003-01-26 20:30:35 +01:00
|
|
|
table->next= thd->derived_tables;
|
|
|
|
thd->derived_tables= table;
|
|
|
|
}
|
2003-01-25 12:19:46 +01:00
|
|
|
|
2002-03-26 14:06:05 +01:00
|
|
|
exit:
|
2003-11-28 11:18:13 +01:00
|
|
|
delete derived_result;
|
2002-12-26 00:28:59 +01:00
|
|
|
lex->current_select= save_current_select;
|
2003-01-07 10:45:06 +01:00
|
|
|
close_thread_tables(thd, 0, 1);
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
}
|