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"
|
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
/*
|
2005-09-22 00:11:21 +02:00
|
|
|
Call given derived table processor (preparing or filling tables)
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_handle_derived()
|
|
|
|
lex LEX for this thread
|
2004-11-05 16:29:47 +01:00
|
|
|
processor procedure of derived table processing
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
RETURN
|
2005-10-27 23:18:23 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2004-02-01 14:30:32 +01:00
|
|
|
*/
|
2004-02-09 13:44:03 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool
|
|
|
|
mysql_handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*))
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-02-01 14:30:32 +01:00
|
|
|
if (lex->derived_tables)
|
|
|
|
{
|
2005-03-28 14:13:31 +02:00
|
|
|
lex->thd->derived_tables_processing= TRUE;
|
2004-02-01 14:30:32 +01:00
|
|
|
for (SELECT_LEX *sl= lex->all_selects_list;
|
|
|
|
sl;
|
|
|
|
sl= sl->next_select_in_list())
|
|
|
|
{
|
|
|
|
for (TABLE_LIST *cursor= sl->get_table_list();
|
|
|
|
cursor;
|
2004-07-16 00:15:55 +02:00
|
|
|
cursor= cursor->next_local)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
if ((res= (*processor)(lex->thd, lex, cursor)))
|
2005-03-28 14:13:31 +02:00
|
|
|
goto out;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
2004-02-01 19:07:44 +01:00
|
|
|
if (lex->describe)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Force join->join_tmp creation, because we will use this JOIN
|
|
|
|
twice for EXPLAIN and we have to have unchanged join for EXPLAINing
|
|
|
|
*/
|
|
|
|
sl->uncacheable|= UNCACHEABLE_EXPLAIN;
|
|
|
|
sl->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN;
|
|
|
|
}
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
}
|
2005-03-28 14:13:31 +02:00
|
|
|
out:
|
|
|
|
lex->thd->derived_tables_processing= FALSE;
|
|
|
|
return res;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-14 14:13:25 +01:00
|
|
|
/*
|
2004-11-05 16:29:47 +01:00
|
|
|
Create temporary table structure (but do not fill it)
|
2002-12-14 14:13:25 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
2004-12-06 16:15:54 +01:00
|
|
|
mysql_derived_prepare()
|
2002-12-14 14:13:25 +01:00
|
|
|
thd Thread handle
|
|
|
|
lex LEX for this thread
|
2004-11-05 16:29:47 +01:00
|
|
|
orig_table_list TABLE_LIST for the upper SELECT
|
2002-12-14 14:13:25 +01:00
|
|
|
|
|
|
|
IMPLEMENTATION
|
2004-11-05 16:29:47 +01:00
|
|
|
Derived table is resolved with temporary table.
|
2003-01-07 10:45:06 +01:00
|
|
|
|
|
|
|
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
|
|
|
RETURN
|
2005-10-27 23:18:23 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
2005-09-22 00:11:21 +02:00
|
|
|
ulonglong create_options;
|
2004-12-06 16:15:54 +01:00
|
|
|
DBUG_ENTER("mysql_derived_prepare");
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-11-05 16:29:47 +01:00
|
|
|
if (unit)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
TABLE *table= 0;
|
|
|
|
select_union *derived_result;
|
|
|
|
bool is_union= first_select->next_select() &&
|
|
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
/* prevent name resolving out of derived table */
|
|
|
|
for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
|
|
|
|
sl->context.outer_context= 0;
|
|
|
|
|
2005-09-22 00:11:21 +02:00
|
|
|
if (!(derived_result= new select_union))
|
2005-10-27 23:18:23 +02:00
|
|
|
DBUG_RETURN(TRUE); // out of memory
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= unit->prepare(thd, derived_result, 0)))
|
2004-11-05 16:29:47 +01:00
|
|
|
goto exit;
|
|
|
|
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= check_duplicate_names(unit->types, 0)))
|
2005-08-10 15:45:00 +02:00
|
|
|
goto exit;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2005-09-22 00:11:21 +02:00
|
|
|
create_options= (first_select->options | thd->options |
|
|
|
|
TMP_TABLE_ALL_COLUMNS);
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
Temp table is created so that it hounours if UNION without ALL is to be
|
|
|
|
processed
|
2005-02-22 14:47:00 +01:00
|
|
|
|
|
|
|
As 'distinct' parameter we always pass FALSE (0), because underlying
|
|
|
|
query will control distinct condition by itself. Correct test of
|
|
|
|
distinct underlying query will be is_union &&
|
|
|
|
!unit->union_distinct->next_select() (i.e. it is union and last distinct
|
|
|
|
SELECT is last SELECT of UNION).
|
2004-11-05 16:29:47 +01:00
|
|
|
*/
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= derived_result->create_result_table(thd, &unit->types, FALSE,
|
|
|
|
create_options,
|
|
|
|
orig_table_list->alias)))
|
2004-11-05 16:29:47 +01:00
|
|
|
goto exit;
|
2005-09-22 00:11:21 +02:00
|
|
|
|
|
|
|
table= derived_result->table;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
exit:
|
2004-12-16 14:31:36 +01:00
|
|
|
/* Hide "Unknown column" or "Unknown function" error */
|
|
|
|
if (orig_table_list->view)
|
|
|
|
{
|
|
|
|
if (thd->net.last_errno == ER_BAD_FIELD_ERROR ||
|
|
|
|
thd->net.last_errno == ER_SP_DOES_NOT_EXIST)
|
|
|
|
{
|
|
|
|
thd->clear_error();
|
|
|
|
my_error(ER_VIEW_INVALID, MYF(0), orig_table_list->db,
|
2005-01-06 12:00:13 +01:00
|
|
|
orig_table_list->table_name);
|
2004-12-16 14:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
if it is preparation PS only or commands that need only VIEW structure
|
|
|
|
then we do not need real data and we can skip execution (and parameters
|
|
|
|
is not defined, too)
|
|
|
|
*/
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (table)
|
|
|
|
free_tmp_table(thd, table);
|
|
|
|
delete derived_result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!thd->fill_derived_tables())
|
2005-02-08 20:52:50 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
delete derived_result;
|
2005-02-08 20:52:50 +01:00
|
|
|
derived_result= NULL;
|
|
|
|
}
|
2004-11-05 16:29:47 +01:00
|
|
|
orig_table_list->derived_result= derived_result;
|
|
|
|
orig_table_list->table= table;
|
2005-11-23 21:45:02 +01:00
|
|
|
orig_table_list->table_name= table->s->table_name.str;
|
|
|
|
orig_table_list->table_name_length= table->s->table_name.length;
|
2004-11-05 16:29:47 +01:00
|
|
|
table->derived_select_number= first_select->select_number;
|
2005-01-06 12:00:13 +01:00
|
|
|
table->s->tmp_table= TMP_TABLE;
|
2004-11-05 16:29:47 +01:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2005-10-27 23:18:23 +02:00
|
|
|
if (orig_table_list->referencing_view)
|
|
|
|
table->grant= orig_table_list->grant;
|
|
|
|
else
|
|
|
|
table->grant.privilege= SELECT_ACL;
|
2004-11-05 16:29:47 +01:00
|
|
|
#endif
|
|
|
|
orig_table_list->db= (char *)"";
|
2005-02-08 20:52:50 +01:00
|
|
|
orig_table_list->db_length= 0;
|
2004-11-05 16:29:47 +01:00
|
|
|
// Force read of table stats in the optimizer
|
|
|
|
table->file->info(HA_STATUS_VARIABLE);
|
|
|
|
/* Add new temporary table to list of open derived tables */
|
|
|
|
table->next= thd->derived_tables;
|
|
|
|
thd->derived_tables= table;
|
|
|
|
}
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
2005-10-27 23:18:23 +02:00
|
|
|
else if (orig_table_list->merge_underlying_list)
|
|
|
|
orig_table_list->set_underlying_merge();
|
2004-12-06 16:15:54 +01:00
|
|
|
DBUG_RETURN(res);
|
2004-11-05 16:29:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-26 20:30:35 +01:00
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
fill derived table
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_derived_filling()
|
|
|
|
thd Thread handle
|
|
|
|
lex LEX for this thread
|
|
|
|
unit node that contains all SELECT's for derived tables
|
|
|
|
orig_table_list TABLE_LIST for the upper SELECT
|
|
|
|
|
|
|
|
IMPLEMENTATION
|
|
|
|
Derived table is resolved with temporary table. It is created based on the
|
|
|
|
queries defined. After temporary table is filled, 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.
|
2005-06-07 12:11:36 +02:00
|
|
|
If you use this function, make sure it's not called at prepare.
|
|
|
|
Due to evaluation of LIMIT clause it can not be used at prepared stage.
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
RETURN
|
2005-10-27 23:18:23 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
2004-11-05 16:29:47 +01:00
|
|
|
{
|
|
|
|
TABLE *table= orig_table_list->table;
|
|
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
/*check that table creation pass without problem and it is derived table */
|
|
|
|
if (table && unit)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
select_union *derived_result= orig_table_list->derived_result;
|
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
|
|
|
bool is_union= first_select->next_select() &&
|
|
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
2004-04-08 22:28:47 +02:00
|
|
|
if (is_union)
|
|
|
|
{
|
|
|
|
// execute union without clean up
|
2004-11-05 16:29:47 +01:00
|
|
|
res= unit->exec();
|
2004-04-08 22:28:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-07 12:11:36 +02:00
|
|
|
unit->set_limit(first_select);
|
2004-04-08 22:28:47 +02:00
|
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
|
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
|
|
|
|
lex->current_select= first_select;
|
2004-11-05 16:29:47 +01:00
|
|
|
res= mysql_select(thd, &first_select->ref_pointer_array,
|
2004-04-08 22:28:47 +02:00
|
|
|
(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);
|
|
|
|
}
|
2003-06-12 15:52:36 +02:00
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
if (!res)
|
2004-01-14 14:15:42 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
Here we entirely fix both TABLE_LIST and list of SELECT's as
|
|
|
|
there were no derived tables
|
|
|
|
*/
|
|
|
|
if (derived_result->flush())
|
2005-10-27 23:18:23 +02:00
|
|
|
res= TRUE;
|
2003-11-23 01:01:15 +01:00
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
if (!lex->describe)
|
|
|
|
unit->cleanup();
|
|
|
|
}
|
2003-01-26 20:30:35 +01:00
|
|
|
else
|
2004-11-05 16:29:47 +01:00
|
|
|
unit->cleanup();
|
|
|
|
lex->current_select= save_current_select;
|
2004-09-04 22:05:12 +02:00
|
|
|
}
|
2004-11-05 16:29:47 +01:00
|
|
|
return res;
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|