2011-06-30 17:37:13 +02:00
|
|
|
/*
|
2011-11-21 18:13:14 +01:00
|
|
|
Copyright (c) 2002, 2010, Oracle and/or its affiliates.
|
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
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
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
|
2011-06-30 17:37:13 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
typedef bool (*dt_processor)(THD *thd, LEX *lex, TABLE_LIST *derived);
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_optimize(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_create(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_fill(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
|
|
|
|
|
|
|
|
dt_processor processors[]=
|
|
|
|
{
|
|
|
|
&mysql_derived_init,
|
|
|
|
&mysql_derived_prepare,
|
|
|
|
&mysql_derived_optimize,
|
|
|
|
&mysql_derived_merge,
|
|
|
|
&mysql_derived_merge_for_insert,
|
|
|
|
&mysql_derived_create,
|
|
|
|
&mysql_derived_fill,
|
|
|
|
&mysql_derived_reinit,
|
|
|
|
};
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
/*
|
2010-05-26 22:18:18 +02:00
|
|
|
@brief
|
|
|
|
Run specified phases on all derived tables/views in given LEX.
|
2004-02-01 14:30:32 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@param lex LEX for this thread
|
|
|
|
@param phases phases to run derived tables/views through
|
2004-02-01 14:30:32 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@return FALSE OK
|
|
|
|
@return TRUE Error
|
2004-02-01 14:30:32 +01:00
|
|
|
*/
|
2005-10-27 23:18:23 +02:00
|
|
|
bool
|
2010-05-26 22:18:18 +02:00
|
|
|
mysql_handle_derived(LEX *lex, uint phases)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2010-05-26 22:18:18 +02:00
|
|
|
THD *thd= lex->thd;
|
|
|
|
if (!lex->derived_tables)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lex->thd->derived_tables_processing= TRUE;
|
|
|
|
|
|
|
|
for (uint phase= 0; phase < DT_PHASES && !res; phase++)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
uint phase_flag= DT_INIT << phase;
|
|
|
|
if (phase_flag > phases)
|
|
|
|
break;
|
|
|
|
if (!(phases & phase_flag))
|
|
|
|
continue;
|
|
|
|
if (phase_flag >= DT_CREATE && !thd->fill_derived_tables())
|
|
|
|
break;
|
|
|
|
|
2004-02-01 14:30:32 +01:00
|
|
|
for (SELECT_LEX *sl= lex->all_selects_list;
|
2010-05-26 22:18:18 +02:00
|
|
|
sl && !res;
|
2004-02-01 14:30:32 +01:00
|
|
|
sl= sl->next_select_in_list())
|
|
|
|
{
|
|
|
|
for (TABLE_LIST *cursor= sl->get_table_list();
|
2010-05-26 22:18:18 +02:00
|
|
|
cursor && !res;
|
2004-07-16 00:15:55 +02:00
|
|
|
cursor= cursor->next_local)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2011-06-14 04:03:03 +02:00
|
|
|
if (!cursor->is_view_or_derived() && phases == DT_MERGE_FOR_INSERT)
|
|
|
|
continue;
|
2010-05-26 22:18:18 +02:00
|
|
|
uint8 allowed_phases= (cursor->is_merged_derived() ? DT_PHASES_MERGE :
|
2011-06-14 04:03:03 +02:00
|
|
|
DT_PHASES_MATERIALIZE | DT_MERGE_FOR_INSERT);
|
2010-05-26 22:18:18 +02:00
|
|
|
/*
|
|
|
|
Skip derived tables to which the phase isn't applicable.
|
|
|
|
TODO: mark derived at the parse time, later set it's type
|
|
|
|
(merged or materialized)
|
|
|
|
*/
|
|
|
|
if ((phase_flag != DT_PREPARE && !(allowed_phases & phase_flag)) ||
|
2011-06-14 04:03:03 +02:00
|
|
|
(cursor->merged_for_insert && phase_flag != DT_REINIT &&
|
|
|
|
phase_flag != DT_PREPARE))
|
2010-05-26 22:18:18 +02:00
|
|
|
continue;
|
|
|
|
res= (*processors[phase])(lex->thd, lex, cursor);
|
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
|
|
|
}
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
lex->thd->derived_tables_processing= FALSE;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Run through phases for the given derived table/view.
|
|
|
|
|
|
|
|
@param lex LEX for this thread
|
|
|
|
@param derived the derived table to handle
|
|
|
|
@param phase_map phases to process tables/views through
|
|
|
|
|
|
|
|
@details
|
|
|
|
|
|
|
|
This function process the derived table (view) 'derived' to performs all
|
|
|
|
actions that are to be done on the table at the phases specified by
|
|
|
|
phase_map. The processing is carried out starting from the actions
|
|
|
|
performed at the earlier phases (those having smaller ordinal numbers).
|
|
|
|
|
|
|
|
@note
|
|
|
|
This function runs specified phases of the derived tables handling on the
|
|
|
|
given derived table/view. This function is used in the chain of calls:
|
|
|
|
SELECT_LEX::handle_derived ->
|
|
|
|
TABLE_LIST::handle_derived ->
|
|
|
|
mysql_handle_single_derived
|
|
|
|
This chain of calls implements the bottom-up handling of the derived tables:
|
|
|
|
i.e. most inner derived tables/views are handled first. This order is
|
|
|
|
required for the all phases except the merge and the create steps.
|
|
|
|
For the sake of code simplicity this order is kept for all phases.
|
|
|
|
|
|
|
|
@return FALSE ok
|
|
|
|
@return TRUE error
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool
|
|
|
|
mysql_handle_single_derived(LEX *lex, TABLE_LIST *derived, uint phases)
|
|
|
|
{
|
|
|
|
bool res= FALSE;
|
|
|
|
THD *thd= lex->thd;
|
|
|
|
uint8 allowed_phases= (derived->is_merged_derived() ? DT_PHASES_MERGE :
|
|
|
|
DT_PHASES_MATERIALIZE);
|
|
|
|
if (!lex->derived_tables)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lex->thd->derived_tables_processing= TRUE;
|
|
|
|
|
|
|
|
for (uint phase= 0; phase < DT_PHASES; phase++)
|
|
|
|
{
|
|
|
|
uint phase_flag= DT_INIT << phase;
|
|
|
|
if (phase_flag > phases)
|
|
|
|
break;
|
|
|
|
if (!(phases & phase_flag))
|
|
|
|
continue;
|
|
|
|
/* Skip derived tables to which the phase isn't applicable. */
|
|
|
|
if (phase_flag != DT_PREPARE &&
|
|
|
|
!(allowed_phases & phase_flag))
|
|
|
|
continue;
|
|
|
|
if (phase_flag >= DT_CREATE && !thd->fill_derived_tables())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((res= (*processors[phase])(lex->thd, lex, derived)))
|
|
|
|
break;
|
|
|
|
}
|
2005-03-28 14:13:31 +02:00
|
|
|
lex->thd->derived_tables_processing= FALSE;
|
|
|
|
return res;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-03 16:45:40 +02:00
|
|
|
/**
|
2010-05-26 22:18:18 +02:00
|
|
|
@brief
|
|
|
|
Run specified phases for derived tables/views in the given list
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@param lex LEX for this thread
|
|
|
|
@param table_list list of derived tables/view to handle
|
|
|
|
@param phase_map phases to process tables/views through
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@details
|
|
|
|
This function runs phases specified by the 'phases_map' on derived
|
|
|
|
tables/views found in the 'dt_list' with help of the
|
|
|
|
TABLE_LIST::handle_derived function.
|
|
|
|
'lex' is passed as an argument to the TABLE_LIST::handle_derived.
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@return FALSE ok
|
|
|
|
@return TRUE error
|
|
|
|
*/
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
bool
|
|
|
|
mysql_handle_list_of_derived(LEX *lex, TABLE_LIST *table_list, uint phases)
|
|
|
|
{
|
|
|
|
for (TABLE_LIST *tl= table_list; tl; tl= tl->next_local)
|
|
|
|
{
|
|
|
|
if (tl->is_view_or_derived() &&
|
|
|
|
tl->handle_derived(lex, phases))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Merge a derived table/view into the embedding select
|
|
|
|
|
|
|
|
@param thd thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
This function merges the given derived table / view into the parent select
|
|
|
|
construction. Any derived table/reference to view occurred in the FROM
|
|
|
|
clause of the embedding select is represented by a TABLE_LIST structure a
|
|
|
|
pointer to which is passed to the function as in the parameter 'derived'.
|
|
|
|
This structure contains the number/map, alias, a link to SELECT_LEX of the
|
|
|
|
derived table and other info. If the 'derived' table is used in a nested join
|
|
|
|
then additionally the structure contains a reference to the ON expression
|
|
|
|
for this join.
|
|
|
|
|
|
|
|
The merge process results in elimination of the derived table (or the
|
|
|
|
reference to a view) such that:
|
|
|
|
- the FROM list of the derived table/view is wrapped into a nested join
|
|
|
|
after which the nest is added to the FROM list of the embedding select
|
|
|
|
- the WHERE condition of the derived table (view) is ANDed with the ON
|
|
|
|
condition attached to the table.
|
|
|
|
|
|
|
|
@note
|
|
|
|
Tables are merged into the leaf_tables list, original derived table is removed
|
|
|
|
from this list also. SELECT_LEX::table_list list is left untouched.
|
|
|
|
Where expression is merged with derived table's on_expr and can be found after
|
|
|
|
the merge through the SELECT_LEX::table_list.
|
|
|
|
|
|
|
|
Examples of the derived table/view merge:
|
|
|
|
|
|
|
|
Schema:
|
|
|
|
Tables: t1(f1), t2(f2), t3(f3)
|
|
|
|
View v1: SELECT f1 FROM t1 WHERE f1 < 1
|
|
|
|
|
|
|
|
Example with a view:
|
|
|
|
Before merge:
|
|
|
|
|
|
|
|
The query (Q1): SELECT f1,f2 FROM t2 LEFT JOIN v1 ON f1 = f2
|
|
|
|
|
|
|
|
(LEX of the main query)
|
|
|
|
|
|
|
|
|
(select_lex)
|
|
|
|
|
|
|
|
|
(FROM table list)
|
|
|
|
|
|
|
|
|
(join list)= t2, v1
|
|
|
|
/ \
|
|
|
|
/ (on_expr)= (f1 = f2)
|
|
|
|
|
|
|
|
|
(LEX of the v1 view)
|
|
|
|
|
|
|
|
|
(select_lex)= SELECT f1 FROM t1 WHERE f1 < 1
|
|
|
|
|
|
|
|
|
|
|
|
After merge:
|
|
|
|
|
|
|
|
The rewritten query Q1 (Q1'):
|
|
|
|
SELECT f1,f2 FROM t2 LEFT JOIN (t1) ON ((f1 = f2) and (f1 < 1))
|
|
|
|
|
|
|
|
(LEX of the main query)
|
|
|
|
|
|
|
|
|
(select_lex)
|
|
|
|
|
|
|
|
|
(FROM table list)
|
|
|
|
|
|
|
|
|
(join list)= t2, (t1)
|
|
|
|
\
|
|
|
|
(on_expr)= (f1 = f2) and (f1 < 1)
|
|
|
|
|
|
|
|
In this example table numbers are assigned as follows:
|
|
|
|
(outer select): t2 - 1, v1 - 2
|
|
|
|
(inner select): t1 - 1
|
|
|
|
After the merge table numbers will be:
|
|
|
|
(outer select): t2 - 1, t1 - 2
|
|
|
|
|
|
|
|
Example with a derived table:
|
|
|
|
The query Q2:
|
|
|
|
SELECT f1,f2
|
|
|
|
FROM (SELECT f1 FROM t1, t3 WHERE f1=f3 and f1 < 1) tt, t2
|
|
|
|
WHERE f1 = f2
|
|
|
|
|
|
|
|
Before merge:
|
|
|
|
(LEX of the main query)
|
|
|
|
|
|
|
|
|
(select_lex)
|
|
|
|
/ \
|
|
|
|
(FROM table list) (WHERE clause)= (f1 = f2)
|
|
|
|
|
|
|
|
|
(join list)= tt, t2
|
|
|
|
/ \
|
|
|
|
/ (on_expr)= (empty)
|
|
|
|
/
|
|
|
|
(select_lex)= SELECT f1 FROM t1, t3 WHERE f1 = f3 and f1 < 1
|
|
|
|
|
|
|
|
After merge:
|
|
|
|
|
|
|
|
The rewritten query Q2 (Q2'):
|
|
|
|
SELECT f1,f2
|
|
|
|
FROM (t1, t3) JOIN t2 ON (f1 = f3 and f1 < 1)
|
|
|
|
WHERE f1 = f2
|
|
|
|
|
|
|
|
(LEX of the main query)
|
|
|
|
|
|
|
|
|
(select_lex)
|
|
|
|
/ \
|
|
|
|
(FROM table list) (WHERE clause)= (f1 = f2)
|
|
|
|
|
|
|
|
|
(join list)= t2, (t1, t3)
|
|
|
|
\
|
|
|
|
(on_expr)= (f1 = f3 and f1 < 1)
|
|
|
|
|
|
|
|
In this example table numbers are assigned as follows:
|
|
|
|
(outer select): tt - 1, t2 - 2
|
|
|
|
(inner select): t1 - 1, t3 - 2
|
|
|
|
After the merge table numbers will be:
|
|
|
|
(outer select): t1 - 1, t2 - 2, t3 - 3
|
|
|
|
|
|
|
|
@return FALSE if derived table/view were successfully merged.
|
|
|
|
@return TRUE if an error occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
bool res= FALSE;
|
|
|
|
SELECT_LEX *dt_select= derived->get_single_select();
|
|
|
|
table_map map;
|
|
|
|
uint tablenr;
|
|
|
|
SELECT_LEX *parent_lex= derived->select_lex;
|
|
|
|
Query_arena *arena, backup;
|
|
|
|
|
|
|
|
if (derived->merged)
|
|
|
|
return FALSE;
|
|
|
|
|
2011-07-17 08:57:43 +02:00
|
|
|
if (thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI)
|
|
|
|
thd->save_prep_leaf_list= TRUE;
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
|
|
|
|
derived->merged= TRUE;
|
|
|
|
|
2011-07-17 08:57:43 +02:00
|
|
|
if (!derived->merged_for_insert ||
|
|
|
|
(derived->is_multitable() &&
|
|
|
|
(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI)))
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
2011-06-14 04:03:03 +02:00
|
|
|
/*
|
|
|
|
Check whether there is enough free bits in table map to merge subquery.
|
|
|
|
If not - materialize it. This check isn't cached so when there is a big
|
|
|
|
and small subqueries, and the bigger one can't be merged it wouldn't
|
|
|
|
block the smaller one.
|
|
|
|
*/
|
|
|
|
if (parent_lex->get_free_table_map(&map, &tablenr))
|
|
|
|
{
|
|
|
|
/* There is no enough table bits, fall back to materialization. */
|
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
|
|
|
goto exit_merge;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dt_select->leaf_tables.elements + tablenr > MAX_TABLES)
|
|
|
|
{
|
|
|
|
/* There is no enough table bits, fall back to materialization. */
|
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
|
|
|
goto exit_merge;
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2011-06-14 04:03:03 +02:00
|
|
|
if (dt_select->options & OPTION_SCHEMA_TABLE)
|
|
|
|
parent_lex->options |= OPTION_SCHEMA_TABLE;
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2011-06-14 04:03:03 +02:00
|
|
|
parent_lex->cond_count+= dt_select->cond_count;
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2011-06-14 04:03:03 +02:00
|
|
|
if (!derived->get_unit()->prepared)
|
|
|
|
{
|
|
|
|
dt_select->leaf_tables.empty();
|
|
|
|
make_leaves_list(dt_select->leaf_tables, derived, TRUE, 0);
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2011-06-14 04:03:03 +02:00
|
|
|
derived->nested_join= (NESTED_JOIN*) thd->calloc(sizeof(NESTED_JOIN));
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!derived->nested_join)
|
|
|
|
{
|
|
|
|
res= TRUE;
|
|
|
|
goto exit_merge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge derived table's subquery in the parent select. */
|
2011-07-17 08:57:43 +02:00
|
|
|
if (parent_lex->merge_subquery(thd, derived, dt_select, tablenr, map))
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
|
|
|
res= TRUE;
|
|
|
|
goto exit_merge;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
exclude select lex so it doesn't show up in explain.
|
|
|
|
do this only for derived table as for views this is already done.
|
|
|
|
|
|
|
|
From sql_view.cc
|
|
|
|
Add subqueries units to SELECT into which we merging current view.
|
|
|
|
unit(->next)* chain starts with subqueries that are used by this
|
|
|
|
view and continues with subqueries that are used by other views.
|
|
|
|
We must not add any subquery twice (otherwise we'll form a loop),
|
|
|
|
to do this we remember in end_unit the first subquery that has
|
|
|
|
been already added.
|
|
|
|
*/
|
|
|
|
derived->get_unit()->exclude_level();
|
|
|
|
if (parent_lex->join)
|
2011-06-05 04:56:06 +02:00
|
|
|
parent_lex->join->table_count+= dt_select->join->table_count - 1;
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
if (derived->get_unit()->prepared)
|
|
|
|
{
|
|
|
|
Item *expr= derived->on_expr;
|
|
|
|
expr= and_conds(expr, dt_select->join ? dt_select->join->conds : 0);
|
|
|
|
if (expr && (derived->prep_on_expr || expr != derived->on_expr))
|
|
|
|
{
|
|
|
|
derived->on_expr= expr;
|
|
|
|
derived->prep_on_expr= expr->copy_andor_structure(thd);
|
|
|
|
}
|
|
|
|
if (derived->on_expr &&
|
|
|
|
((!derived->on_expr->fixed &&
|
|
|
|
derived->on_expr->fix_fields(thd, &derived->on_expr)) ||
|
|
|
|
derived->on_expr->check_cols(1)))
|
|
|
|
{
|
|
|
|
res= TRUE; /* purecov: inspected */
|
|
|
|
goto exit_merge;
|
|
|
|
}
|
|
|
|
// Update used tables cache according to new table map
|
|
|
|
if (derived->on_expr)
|
2011-05-17 07:39:43 +02:00
|
|
|
{
|
|
|
|
derived->on_expr->fix_after_pullout(parent_lex, &derived->on_expr);
|
|
|
|
fix_list_after_tbl_changes(parent_lex, &derived->nested_join->join_list);
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit_merge:
|
|
|
|
if (arena)
|
|
|
|
thd->restore_active_arena(arena, &backup);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Merge a view for the embedding INSERT/UPDATE/DELETE
|
|
|
|
|
|
|
|
@param thd thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
This function substitutes the derived table for the first table from
|
|
|
|
the query of the derived table thus making it a correct target table for the
|
|
|
|
INSERT/UPDATE/DELETE statements. As this operation is correct only for
|
|
|
|
single table views only, for multi table views this function does nothing.
|
|
|
|
The derived parameter isn't checked to be a view as derived tables aren't
|
|
|
|
allowed for INSERT/UPDATE/DELETE statements.
|
|
|
|
|
|
|
|
@return FALSE if derived table/view were successfully merged.
|
|
|
|
@return TRUE if an error occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
if (derived->merged_for_insert)
|
|
|
|
return FALSE;
|
2011-06-14 04:03:03 +02:00
|
|
|
if (derived->is_materialized_derived())
|
2011-07-19 05:05:33 +02:00
|
|
|
return mysql_derived_prepare(thd, lex, derived);
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!derived->is_multitable())
|
|
|
|
{
|
2011-06-14 04:03:03 +02:00
|
|
|
if (!derived->updatable)
|
|
|
|
return derived->create_field_translation(thd);
|
2011-07-17 08:57:43 +02:00
|
|
|
if (derived->merge_underlying_list)
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
2011-07-17 08:57:43 +02:00
|
|
|
derived->table= derived->merge_underlying_list->table;
|
|
|
|
derived->schema_table= derived->merge_underlying_list->schema_table;
|
|
|
|
derived->merged_for_insert= TRUE;
|
|
|
|
}
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Initialize a derived table/view
|
|
|
|
|
|
|
|
@param thd Thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@detail
|
|
|
|
Fill info about derived table/view without preparing an
|
|
|
|
underlying select. Such as: create a field translation for views, mark it as
|
|
|
|
a multitable if it is and so on.
|
|
|
|
|
|
|
|
@return
|
|
|
|
false OK
|
|
|
|
true Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
DBUG_ENTER("mysql_derived_init");
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
// Skip already prepared views/DT
|
|
|
|
if (!unit || unit->prepared)
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
|
|
|
|
DBUG_RETURN(derived->init_derived(thd, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Create temporary table structure (but do not fill it)
|
|
|
|
|
|
|
|
@param thd Thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@detail
|
|
|
|
Prepare underlying select for a derived table/view. To properly resolve
|
|
|
|
names in the embedding query the TABLE structure is created. Actual table
|
|
|
|
is created later by the mysql_derived_create function.
|
|
|
|
|
|
|
|
This function is called before any command containing derived table
|
|
|
|
is executed. All types of derived tables are handled by this function:
|
|
|
|
- Anonymous derived tables, or
|
|
|
|
- Named derived tables (aka views).
|
|
|
|
|
|
|
|
The table reference, contained in @c derived, is updated with the
|
|
|
|
fields of a new temporary table.
|
2008-09-03 16:45:40 +02:00
|
|
|
Derived tables are stored in @c thd->derived_tables and closed by
|
|
|
|
close_thread_tables().
|
|
|
|
|
|
|
|
This function is part of the procedure that starts in
|
|
|
|
open_and_lock_tables(), a procedure that - among other things - introduces
|
|
|
|
new table and table reference objects (to represent derived tables) that
|
|
|
|
don't exist in the privilege database. This means that normal privilege
|
|
|
|
checking cannot handle them. Hence this function does some extra tricks in
|
|
|
|
order to bypass normal privilege checking, by exploiting the fact that the
|
|
|
|
current state of privilege verification is attached as GRANT_INFO structures
|
|
|
|
on the relevant TABLE and TABLE_REF objects.
|
|
|
|
|
|
|
|
For table references, the current state of accrued access is stored inside
|
|
|
|
TABLE_LIST::grant. Hence this function must update the state of fulfilled
|
|
|
|
privileges for the new TABLE_LIST, an operation which is normally performed
|
|
|
|
exclusively by the table and database access checking functions,
|
|
|
|
check_access() and check_grant(), respectively. This modification is done
|
|
|
|
for both views and anonymous derived tables: The @c SELECT privilege is set
|
|
|
|
as fulfilled by the user. However, if a view is referenced and the table
|
|
|
|
reference is queried against directly (see TABLE_LIST::referencing_view),
|
|
|
|
the state of privilege checking (GRANT_INFO struct) is copied as-is to the
|
|
|
|
temporary table.
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
Only the TABLE structure is created here, actual table is created by the
|
|
|
|
mysql_derived_create function.
|
2008-09-03 16:45:40 +02:00
|
|
|
|
|
|
|
@note This function sets @c SELECT_ACL for @c TEMPTABLE views as well as
|
|
|
|
anonymous derived tables, but this is ok since later access checking will
|
|
|
|
distinguish between them.
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
@see mysql_handle_derived(), mysql_derived_fill(), GRANT_INFO
|
2008-09-03 16:45:40 +02:00
|
|
|
|
|
|
|
@return
|
|
|
|
false OK
|
|
|
|
true Error
|
A fix and a test case for Bug#6513 "Test Suite: Values inserted by using
cursor is interpreted latin1 character and Bug#9819 "Cursors: Mysql Server
Crash while fetching from table with 5 million records."
A fix for a possible memory leak when fetching into an SP cursor
in a long loop.
The patch uses a common implementation of cursors in the binary protocol and
in stored procedures and implements materialized cursors.
For implementation details, see comments in sql_cursor.cc
include/my_sys.h:
- declaration for multi_alloc_root
libmysqld/Makefile.am:
- drop protocol_cursor.cc, add sql_cursor.cc (replaces the old
implementation of cursors with a new one)
mysql-test/r/ctype_ujis.result:
- test results fixed (a test case for Bug#6513)
mysql-test/r/sp-big.result:
- test results fixed (a test case for Bug#9819)
mysql-test/t/ctype_ujis.test:
Add a test case for Bug#6513 "Test Suite: Values inserted by using cursor is
interpreted latin1 character"
mysql-test/t/sp-big.test:
Add a restricted test case for Bug#9819 "Cursors: Mysql Server Crash
while fetching from table with 5 million records."
mysys/my_alloc.c:
- an implementation of multi_alloc_root; this is largely a copy-paste
from mulalloc.c, but the function is small and there is no easy way
to reuse the existing C function.
sql/Makefile.am:
- add sql_cursor.h, sql_cursor.cc (a new implementation of stored procedure
cursors) and drop protocol_cursor.cc (the old one)
sql/handler.cc:
- now TABLE object has its mem_root always initialized.
Adjust the implementation handler::ha_open
sql/item_subselect.cc:
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/protocol.h:
- drop Protocol_cursor
sql/sp_head.cc:
- move juggling with Query_arena::free_list and Item::next to
sp_eval_func_item, as this is needed in 3 places already.
sql/sp_head.h:
- declare a no-op implementation for cleanup_stmt in sp_instr_cpush.
This method is needed for non-materializing cursors, which are yet not
used in stored procedures.
- declaration for sp_eval_func_item
sql/sp_rcontext.cc:
- reimplement sp_cursor using the new implementation of server side cursors.
- use sp_eval_func_item to assign values of SP variables from the
row fetched from a cursor. This should fix a possible memory leak in
the old implementation of sp_cursor::fetch
sql/sp_rcontext.h:
- reimplement sp_cursor using the new implementation of server side cursors.
sql/sql_class.cc:
- disable the functionality that closes transient cursors at commit/rollback;
transient cursors are not used in 5.0, instead we use materialized ones.
To be enabled in a later version.
sql/sql_class.h:
- adjust to the rename Cursor -> Server_side_cursor
- additional declarations of select_union used in materialized cursors
sql/sql_derived.cc:
- reuse bits of tmp table code in UNION, derived tables, and materialized
cursors
- cleanup comments
sql/sql_lex.h:
- declarations of auxiliary methods used by materialized cursors
- a cleanup in st_select_lex_unit interface
sql/sql_list.h:
- add an array operator new[] to class Sql_alloc
sql/sql_prepare.cc:
- split the tight coupling of cursors and prepared statements to reuse
the same implementation in stored procedures
- cleanups of error processing in Prepared_statement::{prepare,execute}
sql/sql_select.cc:
- move the implementation of sensitive (non-materializing) cursors to
sql_cursor.cc
- make temporary tables self-contained: the table, its record and fields
are allocated in TABLE::mem_root. This implementation is not clean
and resets thd->mem_root several times because of the way create_tmp_table
works (many additional things are done inside it).
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/sql_select.h:
- move the declaration of sensitive (non-materializing) cursors to
sql_cursor.cc
sql/sql_union.cc:
- move pieces of st_select_unit::prepare to select_union and st_table
methods to be able to reuse code in the implementation of materialized
cursors
sql/sql_view.cc:
- adjust to the changed signature of st_select_lex_unit::prepare
sql/table.cc:
- implement auxiliary st_table methods for use with temporary tables
sql/table.h:
- add declarations for auxiliary methods of st_table used to work with
temporary tables
tests/mysql_client_test.c:
- if cursors are materialized, a parallel update of the table used
in the cursor may go through: update the test.
sql/sql_cursor.cc:
New BitKeeper file ``sql/sql_cursor.cc'' -- implementation of server side
cursors
sql/sql_cursor.h:
New BitKeeper file ``sql/sql_cursor.h'' - declarations for
server side cursors.
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
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
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
// Skip already prepared views/DT
|
2011-06-14 04:03:03 +02:00
|
|
|
if (!unit || unit->prepared ||
|
|
|
|
(derived->merged_for_insert &&
|
|
|
|
!(derived->is_multitable() &&
|
|
|
|
(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI))))
|
2010-05-26 22:18:18 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2005-07-01 06:05:42 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
Query_arena *arena= thd->stmt_arena, backup;
|
|
|
|
if (arena->is_conventional())
|
|
|
|
arena= 0; // For easier test
|
|
|
|
else
|
|
|
|
thd->set_n_backup_active_arena(arena, &backup);
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2010-05-26 22:18:18 +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;
|
|
|
|
// Prepare underlying views/DT first.
|
|
|
|
sl->handle_derived(lex, DT_PREPARE);
|
|
|
|
}
|
A fix and a test case for Bug#6513 "Test Suite: Values inserted by using
cursor is interpreted latin1 character and Bug#9819 "Cursors: Mysql Server
Crash while fetching from table with 5 million records."
A fix for a possible memory leak when fetching into an SP cursor
in a long loop.
The patch uses a common implementation of cursors in the binary protocol and
in stored procedures and implements materialized cursors.
For implementation details, see comments in sql_cursor.cc
include/my_sys.h:
- declaration for multi_alloc_root
libmysqld/Makefile.am:
- drop protocol_cursor.cc, add sql_cursor.cc (replaces the old
implementation of cursors with a new one)
mysql-test/r/ctype_ujis.result:
- test results fixed (a test case for Bug#6513)
mysql-test/r/sp-big.result:
- test results fixed (a test case for Bug#9819)
mysql-test/t/ctype_ujis.test:
Add a test case for Bug#6513 "Test Suite: Values inserted by using cursor is
interpreted latin1 character"
mysql-test/t/sp-big.test:
Add a restricted test case for Bug#9819 "Cursors: Mysql Server Crash
while fetching from table with 5 million records."
mysys/my_alloc.c:
- an implementation of multi_alloc_root; this is largely a copy-paste
from mulalloc.c, but the function is small and there is no easy way
to reuse the existing C function.
sql/Makefile.am:
- add sql_cursor.h, sql_cursor.cc (a new implementation of stored procedure
cursors) and drop protocol_cursor.cc (the old one)
sql/handler.cc:
- now TABLE object has its mem_root always initialized.
Adjust the implementation handler::ha_open
sql/item_subselect.cc:
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/protocol.h:
- drop Protocol_cursor
sql/sp_head.cc:
- move juggling with Query_arena::free_list and Item::next to
sp_eval_func_item, as this is needed in 3 places already.
sql/sp_head.h:
- declare a no-op implementation for cleanup_stmt in sp_instr_cpush.
This method is needed for non-materializing cursors, which are yet not
used in stored procedures.
- declaration for sp_eval_func_item
sql/sp_rcontext.cc:
- reimplement sp_cursor using the new implementation of server side cursors.
- use sp_eval_func_item to assign values of SP variables from the
row fetched from a cursor. This should fix a possible memory leak in
the old implementation of sp_cursor::fetch
sql/sp_rcontext.h:
- reimplement sp_cursor using the new implementation of server side cursors.
sql/sql_class.cc:
- disable the functionality that closes transient cursors at commit/rollback;
transient cursors are not used in 5.0, instead we use materialized ones.
To be enabled in a later version.
sql/sql_class.h:
- adjust to the rename Cursor -> Server_side_cursor
- additional declarations of select_union used in materialized cursors
sql/sql_derived.cc:
- reuse bits of tmp table code in UNION, derived tables, and materialized
cursors
- cleanup comments
sql/sql_lex.h:
- declarations of auxiliary methods used by materialized cursors
- a cleanup in st_select_lex_unit interface
sql/sql_list.h:
- add an array operator new[] to class Sql_alloc
sql/sql_prepare.cc:
- split the tight coupling of cursors and prepared statements to reuse
the same implementation in stored procedures
- cleanups of error processing in Prepared_statement::{prepare,execute}
sql/sql_select.cc:
- move the implementation of sensitive (non-materializing) cursors to
sql_cursor.cc
- make temporary tables self-contained: the table, its record and fields
are allocated in TABLE::mem_root. This implementation is not clean
and resets thd->mem_root several times because of the way create_tmp_table
works (many additional things are done inside it).
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/sql_select.h:
- move the declaration of sensitive (non-materializing) cursors to
sql_cursor.cc
sql/sql_union.cc:
- move pieces of st_select_unit::prepare to select_union and st_table
methods to be able to reuse code in the implementation of materialized
cursors
sql/sql_view.cc:
- adjust to the changed signature of st_select_lex_unit::prepare
sql/table.cc:
- implement auxiliary st_table methods for use with temporary tables
sql/table.h:
- add declarations for auxiliary methods of st_table used to work with
temporary tables
tests/mysql_client_test.c:
- if cursors are materialized, a parallel update of the table used
in the cursor may go through: update the test.
sql/sql_cursor.cc:
New BitKeeper file ``sql/sql_cursor.cc'' -- implementation of server side
cursors
sql/sql_cursor.h:
New BitKeeper file ``sql/sql_cursor.h'' - declarations for
server side cursors.
2005-09-22 00:11:21 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
unit->derived= derived;
|
|
|
|
|
|
|
|
if (!(derived->derived_result= new select_union))
|
|
|
|
DBUG_RETURN(TRUE); // out of memory
|
|
|
|
|
2011-05-17 07:39:43 +02:00
|
|
|
lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_DERIVED;
|
2010-05-26 22:18:18 +02:00
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
|
|
|
if ((res= unit->prepare(thd, derived->derived_result, 0)))
|
|
|
|
goto exit;
|
2011-05-17 07:39:43 +02:00
|
|
|
lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_DERIVED;
|
2010-05-26 22:18:18 +02:00
|
|
|
if ((res= check_duplicate_names(unit->types, 0)))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check whether we can merge this derived table into main select.
|
|
|
|
Depending on the result field translation will or will not
|
|
|
|
be created.
|
|
|
|
*/
|
|
|
|
if (derived->init_derived(thd, FALSE))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Temp table is created so that it hounours if UNION without ALL is to be
|
|
|
|
processed
|
|
|
|
|
|
|
|
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).
|
|
|
|
*/
|
2011-06-24 23:38:53 +02:00
|
|
|
thd->create_tmp_table_for_derived= TRUE;
|
2010-05-26 22:18:18 +02:00
|
|
|
if (derived->derived_result->create_result_table(thd, &unit->types, FALSE,
|
|
|
|
(first_select->options |
|
|
|
|
thd->options |
|
|
|
|
TMP_TABLE_ALL_COLUMNS),
|
|
|
|
derived->alias,
|
|
|
|
FALSE, FALSE))
|
2011-06-24 23:38:53 +02:00
|
|
|
{
|
|
|
|
thd->create_tmp_table_for_derived= FALSE;
|
2010-05-26 22:18:18 +02:00
|
|
|
goto exit;
|
2011-06-24 23:38:53 +02:00
|
|
|
}
|
|
|
|
thd->create_tmp_table_for_derived= FALSE;
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
derived->table= derived->derived_result->table;
|
|
|
|
if (derived->is_derived() && derived->is_merged_derived())
|
|
|
|
first_select->mark_as_belong_to_derived(derived);
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
exit:
|
2010-05-26 22:18:18 +02:00
|
|
|
/* Hide "Unknown column" or "Unknown function" error */
|
|
|
|
if (derived->view)
|
|
|
|
{
|
|
|
|
if (thd->is_error() &&
|
Bug#12713 "Error in a stored function called from a SELECT doesn't
cause ROLLBACK of statement", part 1. Review fixes.
Do not send OK/EOF packets to the client until we reached the end of
the current statement.
This is a consolidation, to keep the functionality that is shared by all
SQL statements in one place in the server.
Currently this functionality includes:
- close_thread_tables()
- log_slow_statement().
After this patch and the subsequent patch for Bug#12713, it shall also include:
- ha_autocommit_or_rollback()
- net_end_statement()
- query_cache_end_of_result().
In future it may also include:
- mysql_reset_thd_for_next_command().
include/mysql_com.h:
Rename now unused members of NET: no_send_ok, no_send_error, report_error.
These were server-specific variables related to the client/server
protocol. They have been made obsolete by this patch.
Previously the same members of NET were used to store the error message
both on the client and on the server.
The error message was stored in net.last_error (client: mysql->net.last_error,
server: thd->net.last_error).
The error code was stored in net.last_errno (client: mysql->net.last_errno,
server: thd->net.last_errno).
The server error code and message are now stored elsewhere
(in the Diagnostics_area), thus NET members are no longer used by the
server.
Rename last_error to client_last_error, last_errno to client_last_errno
to avoid potential bugs introduced by merges.
include/mysql_h.ic:
Update the ABI file to reflect a rename.
Renames do not break the binary compatibility.
libmysql/libmysql.c:
Rename last_error to client_last_error, last_errno to client_last_errno.
This is necessary to ensure no unnoticed bugs introduced by merged
changesets.
Remove net.report_error, net.no_send_ok, net.no_send_error.
libmysql/manager.c:
Rename net.last_errno to net.client_last_errno.
libmysqld/lib_sql.cc:
Rename net.last_errno to net.client_last_errno.
Update the embedded implementation of the client-server protocol to
reflect the refactoring of protocol.cc.
libmysqld/libmysqld.c:
Rename net.last_errno to net.client_last_errno.
mysql-test/r/events.result:
Update to reflect the change in mysql_rm_db(). Now we drop stored
routines and events for a given database name only if there
is a directory for this database name. ha_drop_database() and
query_cache_invalidate() are called likewise.
Previously we would attempt to drop routines/events even if database
directory was not found (it worked, since routines and events are stored
in tables). This fixes Bug 29958 "Weird message on DROP DATABASE if mysql.proc
does not exist".
The change was done because the previous code used to call send_ok()
twice, which led to an assertion failure when asserts against it were
added by this patch.
mysql-test/r/grant.result:
Fix the patch for Bug 16470, now FLUSH PRIVILEGES produces an error
if mysql.procs_priv is missing.
This fixes the assert that send_ok() must not called after send_error()
(the original patch for Bug 16470 was prone to this).
mysql-test/suite/rpl/r/rpl_row_tabledefs_2myisam.result:
Produce a more detailed error message.
mysql-test/suite/rpl/r/rpl_row_tabledefs_3innodb.result:
Produce a more detailed error message.
mysql-test/t/grant.test:
Update the test, now FLUSH PRIVILEGES returns an error if mysql.procs_priv
is missing.
server-tools/instance-manager/mysql_connection.cc:
Rename net.last_errno to net.client_last_errno.
sql/ha_ndbcluster_binlog.cc:
Add asserts.
Use getters to access statement status information.
Add a comment why run_query() is broken. Reset the diagnostics area
in the end of run_query() to fulfill the invariant that the diagnostics_area
is never assigned twice per statement (see the comment in the code
when this can happen). We still do not clear thd->is_fatal_error and
thd->is_slave_error, which may lead to bugs, I consider the whole affair
as something to be dealt with separately.
sql/ha_partition.cc:
fatal_error() doesn't set an error by itself. Perhaps we should
remove this method altogether and instead add a flag to my_error
to set thd->is_fatal_error property.
Meanwhile, this change is a part of inspection made to the entire source
code with the goal to ensure that fatal_error()
is always accompanied by my_error().
sql/item_func.cc:
There is no net.last_error anymore. Remove the obsolete assignment.
sql/log_event.cc:
Use getters to access statement error status information.
sql/log_event_old.cc:
Use getters to access statement error status information.
sql/mysqld.cc:
Previously, if a continue handler for an error was found, my_message_sql()
would not set an error in THD. Since the current statement
must be aborted in any case, find_handler() had a hack to assign
thd->net.report_error to 1.
Remove this hack. Set an error in my_message_sql() even if the continue
handler is found. The error will be cleared anyway when the handler
is executed. This is one action among many in this patch to ensure the
invariant that whenever thd->is_error() is TRUE, we have a message in
thd->main_da.message().
sql/net_serv.cc:
Use a full-blown my_error() in net_serv.cc to report an error,
instead of just setting net->last_errno. This ensures the invariant that
whenever thd->is_error() returns TRUE, we have a message in
thd->main_da.message().
Remove initialization of removed NET members.
sql/opt_range.cc:
Use my_error() instead of just raising thd->net.report_error.
This ensures the invariant that whenever thd->is_error() returns TRUE,
there is a message in thd->main_da.message().
sql/opt_sum.cc:
Move invocation of fatal_error() right next to the place where
we set the error message. That makes it easier to track that whenever
fatal_error() is called, there is a message in THD.
sql/protocol.cc:
Rename send_ok() and send_eof() to net_send_ok() and net_send_eof()
respectively. These functions write directly to the network and are not
for use anywhere outside the client/server protocol code.
Remove the code that was responsible for cases when either there is
no error code, or no error message, or both.
Instead the calling code ensures that they are always present. Asserts
are added to enforce the invariant.
Instead of a direct access to thd->server_status and thd->total_warn_count
use function parameters, since these from now on don't always come directly
from THD.
Introduce net_end_statement(), the single-entry-point replacement API for
send_ok(), send_eof() and net_send_error().
Implement Protocol::end_partial_result_set to use in select_send::abort()
when there is a continue handler.
sql/protocol.h:
Update declarations.
sql/repl_failsafe.cc:
Use getters to access statement status information in THD.
Rename net.last_error to net.client_last_error.
sql/rpl_record.cc:
Set an error message in prepare_record() if there is no default
value for the field -- later we do print this message to the client.
sql/rpl_rli.cc:
Use getters to access statement status information in THD.
sql/slave.cc:
In create_table_from_dump() (a common function that is used in
LOAD MASTER TABLE SQL statement and COM_LOAD_MASTER_DATA), instead of hacks
with no_send_ok, clear the diagnostics area when mysql_rm_table() succeeded.
Update has_temporary_error() to work correctly when no error is set.
This is the case when Incident_log_event is executed: it always returns
an error but does not set an error message.
Use getters to access error status information.
sql/sp_head.cc:
Instead of hacks with no_send_error, work through the diagnostics area
interface to suppress sending of OK/ERROR packets to the client.
Move query_cache_end_of_result before log_slow_statement(), similarly
to how it's done in dispatch_command().
sql/sp_rcontext.cc:
Remove hacks with assignment of thd->net.report_error, they are not
necessary any more (see the changes in mysqld.cc).
sql/sql_acl.cc:
Use getters to access error status information in THD.
sql/sql_base.cc:
Access thd->main_da.sql_errno() only if there is an error. This fixes
a bug when auto-discovery, that was effectively disabled under pre-locking.
sql/sql_binlog.cc:
Remove hacks with no_send_ok/no_send_error, they are not necessary
anymore: the caller is responsible for network communication.
sql/sql_cache.cc:
Disable sending of OK/ERROR/EOF packet in the end of dispatch_command
if the response has been served from the query cache. This raises the
question whether we should store EOF packet in the query cache at all,
or generate it anew for each statement (we should generate it anew), but
this is to be addressed separately.
sql/sql_class.cc:
Implement class Diagnostics_area. Please see comments in sql_class.h
for details.
Fix a subtle coding mistake in select_send::send_data: when on slave,
an error in Item::send() was ignored.
The problem became visible due to asserts that the diagnostics area is
never double assigned.
Remove initialization of removed NET members.
In select_send::abort() do not call select_send::send_eof(). This is
not inheritance-safe. Even if a stored procedure continue handler is
found, the current statement is aborted, not succeeded.
Instead introduce a Protocol API to send the required response,
Protocol::end_partial_result_set().
This simplifies implementation of select_send::send_eof(). No need
to add more asserts that there is no error, there is an assert inside
Diagnostics_area::set_ok_status() already.
Leave no trace of no_send_* in the code.
sql/sql_class.h:
Declare class Diagnostics_area.
Remove the hack with no_send_ok from
Substatement_state.
Provide inline implementations of send_ok/send_eof.
Add commetns.
sql/sql_connect.cc:
Remove hacks with no_send_error.
Since now an error in THD is always set if net->error, it's not necessary
to check both net->error and thd->is_error() in the do_command loop.
Use thd->main_da.message() instead of net->last_errno.
Remove the hack with is_slave_error in sys_init_connect. Since now we do not
reset the diagnostics area in net_send_error (it's reset at the beginning
of the next statement), we can access it safely even after
execute_init_command.
sql/sql_db.cc:
Update the code to satisfy the invariant that the diagnostics area is never
assigned twice.
Incidentally, this fixes Bug 29958 "Weird message on DROP DATABASE if
mysql.proc does not exist".
sql/sql_delete.cc:
Change multi-delete to abort in abort(), as per select_send protocol.
Fixes the merge error with the test for Bug 29136
sql/sql_derived.cc:
Use getters to access error information.
sql/sql_insert.cc:
Use getters to access error information.
sql-common/client.c:
Rename last_error to client_last_error, last_errno to client_last_errno.
sql/sql_parse.cc:
Remove hacks with no_send_error. Deploy net_end_statement().
The story of COM_SHUTDOWN is interesting. Long story short, the server
would become on its death's door, and only no_send_ok/no_send_error assigned
by send_ok()/net_send_error() would hide its babbling from the client.
First of all, COM_QUIT does not require a response. So, the comment saying
"Let's send a response to possible COM_QUIT" is not only groundless
(even mysqladmin shutdown/mysql_shutdown() doesn't send COM_QUIT after
COM_SHUTDOWN), it's plainly incorrect.
Secondly, besides this additional 'OK' packet to respond to a hypothetical
COM_QUIT, there was the following code in dispatch_command():
if (thd->killed)
thd->send_kill_message();
if (thd->is_error()
net_send_error(thd);
This worked out really funny for the thread through which COM_SHUTDOWN
was delivered: we would get COM_SHUTDOWN, say okay, say okay again,
kill everybody, get the kill signal ourselves, and then attempt to say
"Server shutdown in progress" to the client that is very likely long gone.
This all became visible when asserts were added that the Diagnostics_area
is not assigned twice.
Move query_cache_end_of_result() to the end of dispatch_command(), since
net_send_eof() has been moved there. This is safe, query_cache_end_of_result()
is a no-op if there is no started query in the cache.
Consistently use select_send interface to call abort() or send_eof()
depending on the operation result.
Remove thd->fatal_error() from reset_master(), it was a no-op.
in hacks with no_send_error woudl save us
from complete breakage of the client/server protocol.
Consistently use select_send::abort() whenever there is an error,
and select_send::send_eof() in case of success.
The issue became visible due to added asserts.
sql/sql_partition.cc:
Always set an error in THD whenever there is a call to fatal_error().
sql/sql_prepare.cc:
Deploy class Diagnostics_area.
Remove the unnecessary juggling with the protocol in
Select_fetch_protocol_binary::send_eof(). EOF packet format is
protocol-independent.
sql/sql_select.cc:
Call fatal_error() directly in opt_sum_query.
Call my_error() whenever we call thd->fatal_error().
sql/sql_servers.cc:
Use getters to access error information in THD.
sql/sql_show.cc:
Use getters to access error information in THD.
Add comments.
Call my_error() whenever we call fatal_error().
sql/sql_table.cc:
Replace hacks with no_send_ok with the interface of the diagnostics area.
Clear the error if ENOENT error in ha_delete_table().
sql/sql_update.cc:
Introduce multi_update::abort(), which is the proper way to abort a
multi-update. This fixes the merge conflict between this patch and
the patch for Bug 29136.
sql/table.cc:
Use a getter to access error information in THD.
sql/tztime.cc:
Use a getter to access error information in THD.
2007-12-12 16:21:01 +01:00
|
|
|
(thd->main_da.sql_errno() == ER_BAD_FIELD_ERROR ||
|
2010-05-26 22:18:18 +02:00
|
|
|
thd->main_da.sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION ||
|
|
|
|
thd->main_da.sql_errno() == ER_SP_DOES_NOT_EXIST))
|
2004-11-05 16:29:47 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
thd->clear_error();
|
|
|
|
my_error(ER_VIEW_INVALID, MYF(0), derived->db,
|
|
|
|
derived->table_name);
|
2004-11-05 16:29:47 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02: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 (derived->table)
|
|
|
|
free_tmp_table(thd, derived->table);
|
|
|
|
delete derived->derived_result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TABLE *table= derived->table;
|
|
|
|
table->derived_select_number= first_select->select_number;
|
|
|
|
table->s->tmp_table= NON_TRANSACTIONAL_TMP_TABLE;
|
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
|
|
|
if (derived->referencing_view)
|
|
|
|
table->grant= derived->grant;
|
2004-11-05 16:29:47 +01:00
|
|
|
else
|
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
table->grant.privilege= SELECT_ACL;
|
|
|
|
if (derived->is_derived())
|
|
|
|
derived->grant.privilege= SELECT_ACL;
|
2004-11-05 16:29:47 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
#endif
|
|
|
|
/* 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
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
if (arena)
|
|
|
|
thd->restore_active_arena(arena, &backup);
|
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
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Runs optimize phase for a derived table/view.
|
|
|
|
|
|
|
|
@param thd thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
Runs optimize phase for given 'derived' derived table/view.
|
|
|
|
If optimizer finds out that it's of the type "SELECT a_constant" then this
|
|
|
|
functions also materializes it.
|
|
|
|
|
|
|
|
@return FALSE ok.
|
|
|
|
@return TRUE if an error occur.
|
A fix and a test case for Bug#6513 "Test Suite: Values inserted by using
cursor is interpreted latin1 character and Bug#9819 "Cursors: Mysql Server
Crash while fetching from table with 5 million records."
A fix for a possible memory leak when fetching into an SP cursor
in a long loop.
The patch uses a common implementation of cursors in the binary protocol and
in stored procedures and implements materialized cursors.
For implementation details, see comments in sql_cursor.cc
include/my_sys.h:
- declaration for multi_alloc_root
libmysqld/Makefile.am:
- drop protocol_cursor.cc, add sql_cursor.cc (replaces the old
implementation of cursors with a new one)
mysql-test/r/ctype_ujis.result:
- test results fixed (a test case for Bug#6513)
mysql-test/r/sp-big.result:
- test results fixed (a test case for Bug#9819)
mysql-test/t/ctype_ujis.test:
Add a test case for Bug#6513 "Test Suite: Values inserted by using cursor is
interpreted latin1 character"
mysql-test/t/sp-big.test:
Add a restricted test case for Bug#9819 "Cursors: Mysql Server Crash
while fetching from table with 5 million records."
mysys/my_alloc.c:
- an implementation of multi_alloc_root; this is largely a copy-paste
from mulalloc.c, but the function is small and there is no easy way
to reuse the existing C function.
sql/Makefile.am:
- add sql_cursor.h, sql_cursor.cc (a new implementation of stored procedure
cursors) and drop protocol_cursor.cc (the old one)
sql/handler.cc:
- now TABLE object has its mem_root always initialized.
Adjust the implementation handler::ha_open
sql/item_subselect.cc:
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/protocol.h:
- drop Protocol_cursor
sql/sp_head.cc:
- move juggling with Query_arena::free_list and Item::next to
sp_eval_func_item, as this is needed in 3 places already.
sql/sp_head.h:
- declare a no-op implementation for cleanup_stmt in sp_instr_cpush.
This method is needed for non-materializing cursors, which are yet not
used in stored procedures.
- declaration for sp_eval_func_item
sql/sp_rcontext.cc:
- reimplement sp_cursor using the new implementation of server side cursors.
- use sp_eval_func_item to assign values of SP variables from the
row fetched from a cursor. This should fix a possible memory leak in
the old implementation of sp_cursor::fetch
sql/sp_rcontext.h:
- reimplement sp_cursor using the new implementation of server side cursors.
sql/sql_class.cc:
- disable the functionality that closes transient cursors at commit/rollback;
transient cursors are not used in 5.0, instead we use materialized ones.
To be enabled in a later version.
sql/sql_class.h:
- adjust to the rename Cursor -> Server_side_cursor
- additional declarations of select_union used in materialized cursors
sql/sql_derived.cc:
- reuse bits of tmp table code in UNION, derived tables, and materialized
cursors
- cleanup comments
sql/sql_lex.h:
- declarations of auxiliary methods used by materialized cursors
- a cleanup in st_select_lex_unit interface
sql/sql_list.h:
- add an array operator new[] to class Sql_alloc
sql/sql_prepare.cc:
- split the tight coupling of cursors and prepared statements to reuse
the same implementation in stored procedures
- cleanups of error processing in Prepared_statement::{prepare,execute}
sql/sql_select.cc:
- move the implementation of sensitive (non-materializing) cursors to
sql_cursor.cc
- make temporary tables self-contained: the table, its record and fields
are allocated in TABLE::mem_root. This implementation is not clean
and resets thd->mem_root several times because of the way create_tmp_table
works (many additional things are done inside it).
- adjust to the changed declaration of st_select_lex_unit::prepare
sql/sql_select.h:
- move the declaration of sensitive (non-materializing) cursors to
sql_cursor.cc
sql/sql_union.cc:
- move pieces of st_select_unit::prepare to select_union and st_table
methods to be able to reuse code in the implementation of materialized
cursors
sql/sql_view.cc:
- adjust to the changed signature of st_select_lex_unit::prepare
sql/table.cc:
- implement auxiliary st_table methods for use with temporary tables
sql/table.h:
- add declarations for auxiliary methods of st_table used to work with
temporary tables
tests/mysql_client_test.c:
- if cursors are materialized, a parallel update of the table used
in the cursor may go through: update the test.
sql/sql_cursor.cc:
New BitKeeper file ``sql/sql_cursor.cc'' -- implementation of server side
cursors
sql/sql_cursor.h:
New BitKeeper file ``sql/sql_cursor.h'' - declarations for
server side cursors.
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_optimize(THD *thd, LEX *lex, TABLE_LIST *derived)
|
2004-11-05 16:29:47 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2011-08-09 07:02:10 +02:00
|
|
|
if (unit->optimized)
|
2010-05-26 22:18:18 +02:00
|
|
|
return FALSE;
|
|
|
|
lex->current_select= first_select;
|
|
|
|
|
|
|
|
if (unit->is_union())
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
// optimize union without execution
|
|
|
|
res= unit->optimize();
|
|
|
|
}
|
|
|
|
else if (unit->derived)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!derived->is_merged_derived())
|
2004-04-08 22:28:47 +02:00
|
|
|
{
|
2011-06-06 21:19:35 +02:00
|
|
|
JOIN *join= first_select->join;
|
2012-01-18 12:31:20 +01:00
|
|
|
unit->set_limit(first_select);
|
2010-05-26 22:18:18 +02:00
|
|
|
unit->optimized= TRUE;
|
2011-06-06 21:19:35 +02:00
|
|
|
if ((res= join->optimize()))
|
2010-05-26 22:18:18 +02:00
|
|
|
goto err;
|
2011-06-06 21:19:35 +02:00
|
|
|
if (join->table_count == join->const_tables)
|
|
|
|
derived->fill_me= TRUE;
|
2004-04-08 22:28:47 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Materialize derived tables/views of the "SELECT a_constant" type.
|
|
|
|
Such tables should be materialized at the optimization phase for
|
|
|
|
correct constant evaluation.
|
|
|
|
*/
|
|
|
|
if (!res && derived->fill_me && !derived->merged_for_insert)
|
|
|
|
{
|
|
|
|
if (derived->is_merged_derived())
|
2004-04-08 22:28:47 +02:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
2004-04-08 22:28:47 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
if ((res= mysql_derived_create(thd, lex, derived)))
|
|
|
|
goto err;
|
|
|
|
if ((res= mysql_derived_fill(thd, lex, derived)))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
lex->current_select= save_current_select;
|
|
|
|
return res;
|
|
|
|
}
|
2003-06-12 15:52:36 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Actually create result table for a materialized derived table/view.
|
|
|
|
|
|
|
|
@param thd thread handle
|
|
|
|
@param lex LEX of the embedding query.
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
This function actually creates the result table for given 'derived'
|
|
|
|
table/view, but it doesn't fill it.
|
|
|
|
'thd' and 'lex' parameters are not used by this function.
|
|
|
|
|
|
|
|
@return FALSE ok.
|
|
|
|
@return TRUE if an error occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_create(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
TABLE *table= derived->table;
|
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
|
|
|
|
if (table->created)
|
|
|
|
return FALSE;
|
|
|
|
select_union *result= (select_union*)unit->result;
|
|
|
|
if (table->s->db_type() == TMP_ENGINE_HTON)
|
|
|
|
{
|
|
|
|
if (create_internal_tmp_table(table, result->tmp_table_param.keyinfo,
|
|
|
|
result->tmp_table_param.start_recinfo,
|
|
|
|
&result->tmp_table_param.recinfo,
|
|
|
|
(unit->first_select()->options |
|
|
|
|
thd->options | TMP_TABLE_ALL_COLUMNS)))
|
|
|
|
return(TRUE);
|
2004-09-04 22:05:12 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
if (open_tmp_table(table))
|
|
|
|
return TRUE;
|
|
|
|
table->file->extra(HA_EXTRA_WRITE_CACHE);
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
@brief
|
|
|
|
Execute subquery of a materialized derived table/view and fill the result
|
|
|
|
table.
|
|
|
|
|
|
|
|
@param thd Thread handle
|
|
|
|
@param lex LEX for this thread
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
Execute subquery of given 'derived' table/view and fill the result
|
|
|
|
table. After result table is filled, if this is not the EXPLAIN statement,
|
|
|
|
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.
|
|
|
|
'lex' is unused and 'thd' is passed as an argument to an underlying function.
|
|
|
|
|
|
|
|
@note
|
|
|
|
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.
|
|
|
|
|
|
|
|
@return FALSE OK
|
|
|
|
@return TRUE Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_fill(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
bool res= FALSE;
|
|
|
|
|
|
|
|
if (unit->executed && !unit->uncacheable && !unit->describe)
|
|
|
|
return FALSE;
|
|
|
|
/*check that table creation passed without problems. */
|
2011-05-20 01:04:01 +02:00
|
|
|
DBUG_ASSERT(derived->table && derived->table->created);
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
select_union *derived_result= derived->derived_result;
|
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
|
|
|
if (unit->is_union())
|
|
|
|
{
|
|
|
|
// execute union without clean up
|
|
|
|
res= unit->exec();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unit->set_limit(first_select);
|
|
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
|
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
|
|
|
|
lex->current_select= first_select;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
if (derived_result->flush())
|
|
|
|
res= TRUE;
|
|
|
|
unit->executed= TRUE;
|
|
|
|
}
|
|
|
|
if (res || !lex->describe)
|
|
|
|
unit->cleanup();
|
|
|
|
lex->current_select= save_current_select;
|
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
return res;
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Re-initialize given derived table/view for the next execution.
|
|
|
|
|
|
|
|
@param thd thread handle
|
|
|
|
@param lex LEX for this thread
|
|
|
|
@param derived reference to the derived table.
|
|
|
|
|
|
|
|
@details
|
|
|
|
Re-initialize given 'derived' table/view for the next execution.
|
|
|
|
All underlying views/derived tables are recursively reinitialized prior
|
|
|
|
to re-initialization of given derived table.
|
|
|
|
'thd' and 'lex' are passed as arguments to called functions.
|
|
|
|
|
|
|
|
@return FALSE OK
|
|
|
|
@return TRUE Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
st_select_lex_unit *unit= derived->get_unit();
|
|
|
|
|
|
|
|
if (derived->table)
|
|
|
|
derived->merged_for_insert= FALSE;
|
|
|
|
unit->unclean();
|
|
|
|
unit->types.empty();
|
|
|
|
/* for derived tables & PS (which can't be reset by Item_subquery) */
|
|
|
|
unit->reinit_exec_mechanism();
|
|
|
|
unit->set_thd(thd);
|
|
|
|
return FALSE;
|
|
|
|
}
|