2011-06-30 17:37:13 +02:00
|
|
|
/*
|
2012-01-13 15:50:02 +01:00
|
|
|
Copyright (c) 2002, 2011, Oracle and/or its affiliates.
|
2021-04-21 06:58:42 +02:00
|
|
|
Copyright (c) 2010, 2021, MariaDB
|
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
|
2019-05-11 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-06-18 05:42:16 +02:00
|
|
|
#include "mariadb.h" /* NO_EMBEDDED_ACCESS_CHECKS */
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "sql_priv.h"
|
|
|
|
#include "unireg.h"
|
|
|
|
#include "sql_derived.h"
|
2002-03-26 14:06:05 +01:00
|
|
|
#include "sql_select.h"
|
2018-10-09 11:36:09 +02:00
|
|
|
#include "derived_handler.h"
|
2011-10-19 21:45:18 +02:00
|
|
|
#include "sql_base.h"
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "sql_view.h" // check_duplicate_names
|
|
|
|
#include "sql_acl.h" // SELECT_ACL
|
2016-05-09 22:39:10 +02:00
|
|
|
#include "sql_class.h"
|
2015-12-17 21:52:14 +01:00
|
|
|
#include "sql_cte.h"
|
2019-02-13 06:52:16 +01:00
|
|
|
#include "my_json_writer.h"
|
2019-02-18 12:41:20 +01:00
|
|
|
#include "opt_trace.h"
|
2004-11-05 16:29:47 +01:00
|
|
|
|
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
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_optimize(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_create(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_fill(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived);
|
|
|
|
static bool mysql_derived_merge_for_insert(THD *thd, LEX *lex,
|
|
|
|
TABLE_LIST *derived);
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
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
|
|
|
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;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_handle_derived");
|
|
|
|
DBUG_PRINT("enter", ("phases: 0x%x", phases));
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!lex->derived_tables)
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2013-06-06 22:33:40 +02:00
|
|
|
TABLE_LIST *cursor= sl->get_table_list();
|
2019-02-27 15:53:25 +01:00
|
|
|
sl->changed_elements|= TOUCHED_SEL_DERIVED;
|
2013-06-06 22:33:40 +02:00
|
|
|
/*
|
|
|
|
DT_MERGE_FOR_INSERT is not needed for views/derived tables inside
|
|
|
|
subqueries. Views and derived tables of subqueries should be
|
|
|
|
processed normally.
|
|
|
|
*/
|
|
|
|
if (phases == DT_MERGE_FOR_INSERT &&
|
2018-05-22 19:08:39 +02:00
|
|
|
cursor && (cursor->top_table()->select_lex !=
|
|
|
|
lex->first_select_lex()))
|
2013-06-06 22:33:40 +02:00
|
|
|
continue;
|
|
|
|
for (;
|
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;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(res);
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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);
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_handle_single_derived");
|
2015-04-22 10:14:11 +02:00
|
|
|
DBUG_PRINT("enter", ("phases: 0x%x allowed: 0x%x alias: '%s'",
|
|
|
|
phases, allowed_phases,
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>")));
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!lex->derived_tables)
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2019-04-16 23:02:54 +02:00
|
|
|
if (derived->select_lex)
|
|
|
|
derived->select_lex->changed_elements|= TOUCHED_SEL_DERIVED;
|
|
|
|
else
|
|
|
|
DBUG_ASSERT(derived->prelocking_placeholder);
|
2010-05-26 22:18:18 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-02-13 06:52:16 +01:00
|
|
|
|
2005-03-28 14:13:31 +02:00
|
|
|
lex->thd->derived_tables_processing= FALSE;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(res);
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
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;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_derived_merge");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2019-02-13 06:52:16 +01:00
|
|
|
const char *cause= NULL;
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
if (derived->merged)
|
2018-01-15 14:50:35 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
DBUG_PRINT("info", ("Irreversibly merged: exit"));
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2018-01-15 14:50:35 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2013-12-18 14:59:51 +01:00
|
|
|
if (dt_select->uncacheable & UNCACHEABLE_RAND)
|
|
|
|
{
|
|
|
|
/* There is random function => fall back to materialization. */
|
2019-02-13 06:52:16 +01:00
|
|
|
cause= "Random function in the select";
|
2019-02-18 12:41:20 +01:00
|
|
|
if (unlikely(thd->trace_started()))
|
|
|
|
{
|
|
|
|
OPT_TRACE_VIEWS_TRANSFORM(thd, trace_wrapper, trace_derived,
|
|
|
|
derived->is_derived() ? "derived" : "view",
|
|
|
|
derived->alias.str ? derived->alias.str : "<NULL>",
|
|
|
|
derived->get_unit()->first_select()->select_number,
|
|
|
|
"materialized");
|
|
|
|
trace_derived.add("cause", cause);
|
|
|
|
}
|
2013-12-18 14:59:51 +01:00
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2013-12-18 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
2019-02-10 07:54:26 +01:00
|
|
|
if (derived->dt_handler)
|
2018-10-09 11:36:09 +02:00
|
|
|
{
|
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI)
|
|
|
|
thd->save_prep_leaf_list= TRUE;
|
2011-07-17 08:57:43 +02:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-02-13 06:52:16 +01:00
|
|
|
if (parent_lex->get_free_table_map(&map, &tablenr) ||
|
|
|
|
dt_select->leaf_tables.elements + tablenr > MAX_TABLES)
|
2011-06-14 04:03:03 +02:00
|
|
|
{
|
|
|
|
/* There is no enough table bits, fall back to materialization. */
|
2019-02-13 06:52:16 +01:00
|
|
|
cause= "Not enough table bits to merge subquery";
|
2014-06-02 14:36:06 +02:00
|
|
|
goto unconditional_materialization;
|
2011-06-14 04:03:03 +02:00
|
|
|
}
|
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
|
|
|
if (!derived->get_unit()->prepared)
|
|
|
|
{
|
|
|
|
dt_select->leaf_tables.empty();
|
2015-04-17 12:30:15 +02:00
|
|
|
make_leaves_list(thd, dt_select->leaf_tables, derived, TRUE, 0);
|
2011-06-14 04:03:03 +02:00
|
|
|
}
|
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
|
|
|
}
|
2018-02-05 16:00:13 +01:00
|
|
|
derived->merged= TRUE;
|
2010-05-26 22:18:18 +02:00
|
|
|
if (derived->get_unit()->prepared)
|
|
|
|
{
|
|
|
|
Item *expr= derived->on_expr;
|
2015-08-11 09:18:38 +02:00
|
|
|
expr= and_conds(thd, expr, dt_select->join ? dt_select->join->conds : 0);
|
2017-02-27 00:40:18 +01:00
|
|
|
if (expr)
|
|
|
|
expr->top_level_item();
|
2017-03-08 18:44:22 +01:00
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
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 &&
|
2018-06-05 08:25:39 +02:00
|
|
|
derived->on_expr->fix_fields_if_needed_for_bool(thd, &derived->on_expr))
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-11-08 15:47:49 +01:00
|
|
|
derived->on_expr->fix_after_pullout(parent_lex, &derived->on_expr,
|
|
|
|
TRUE);
|
2011-05-17 07:39:43 +02:00
|
|
|
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);
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(res);
|
2014-06-02 14:36:06 +02:00
|
|
|
|
|
|
|
unconditional_materialization:
|
2019-02-13 06:52:16 +01:00
|
|
|
|
|
|
|
if (unlikely(thd->trace_started()))
|
|
|
|
{
|
2019-02-18 12:41:20 +01:00
|
|
|
OPT_TRACE_VIEWS_TRANSFORM(thd,trace_wrapper, trace_derived,
|
|
|
|
derived->is_derived() ? "derived" : "view",
|
|
|
|
derived->alias.str ? derived->alias.str : "<NULL>",
|
|
|
|
derived->get_unit()->first_select()->select_number,
|
|
|
|
"materialized");
|
|
|
|
trace_derived.add("cause", cause);
|
2019-02-13 06:52:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-02 14:36:06 +02:00
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
2016-05-08 22:04:41 +02:00
|
|
|
if (!derived->table || !derived->table->is_created())
|
2014-06-02 14:36:06 +02:00
|
|
|
res= mysql_derived_create(thd, lex, derived);
|
|
|
|
goto exit_merge;
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_derived_merge_for_insert");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2015-01-29 14:12:32 +01:00
|
|
|
DBUG_PRINT("info", ("merged_for_insert: %d is_materialized_derived: %d "
|
|
|
|
"is_multitable: %d single_table_updatable: %d "
|
|
|
|
"merge_underlying_list: %d",
|
|
|
|
derived->merged_for_insert,
|
|
|
|
derived->is_materialized_derived(),
|
|
|
|
derived->is_multitable(),
|
|
|
|
derived->single_table_updatable(),
|
|
|
|
derived->merge_underlying_list != 0));
|
2010-05-26 22:18:18 +02:00
|
|
|
if (derived->merged_for_insert)
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2017-08-16 19:18:39 +02:00
|
|
|
if (derived->init_derived(thd, FALSE))
|
|
|
|
DBUG_RETURN(TRUE);
|
2011-06-14 04:03:03 +02:00
|
|
|
if (derived->is_materialized_derived())
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(mysql_derived_prepare(thd, lex, derived));
|
2015-02-11 01:26:50 +01:00
|
|
|
if ((thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI))
|
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
if (!derived->is_multitable())
|
|
|
|
{
|
2012-02-03 12:01:05 +01:00
|
|
|
if (!derived->single_table_updatable())
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_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;
|
2015-01-29 14:12:32 +01:00
|
|
|
DBUG_ASSERT(derived->table);
|
2011-07-17 08:57:43 +02:00
|
|
|
}
|
2015-01-29 14:12:32 +01:00
|
|
|
}
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2008-09-03 16:45:40 +02:00
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
DBUG_ENTER("mysql_derived_init");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
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);
|
|
|
|
|
2016-02-25 14:55:04 +01:00
|
|
|
bool res= derived->init_derived(thd, TRUE);
|
|
|
|
|
|
|
|
derived->updatable= derived->updatable && derived->is_view();
|
|
|
|
|
|
|
|
DBUG_RETURN(res);
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-21 06:58:42 +02:00
|
|
|
/**
|
2021-04-16 18:53:16 +02:00
|
|
|
@brief
|
|
|
|
Prevent name resolution out of context of ON expressions in derived tables
|
|
|
|
|
|
|
|
@param
|
|
|
|
join_list list of tables used in from list of a derived
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function sets the Name_resolution_context::outer_context to NULL
|
|
|
|
for all ON expressions contexts in the given join list. It does this
|
|
|
|
recursively for all nested joins the list contains.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void nullify_outer_context_for_on_clauses(List<TABLE_LIST>& join_list)
|
|
|
|
{
|
|
|
|
List_iterator<TABLE_LIST> li(join_list);
|
|
|
|
while (TABLE_LIST *table= li++)
|
|
|
|
{
|
|
|
|
if (table->on_context)
|
|
|
|
table->on_context->outer_context= NULL;
|
|
|
|
if (table->nested_join)
|
|
|
|
nullify_outer_context_for_on_clauses(table->nested_join->join_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
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();
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2018-01-07 17:03:44 +01:00
|
|
|
DBUG_ENTER("mysql_derived_prepare");
|
|
|
|
DBUG_PRINT("enter", ("unit: %p table_list: %p alias: '%s'",
|
|
|
|
unit, derived, derived->alias.str));
|
2016-05-10 21:32:02 +02:00
|
|
|
if (!unit)
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
|
2016-05-09 22:39:10 +02:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
|
2017-04-29 06:58:04 +02:00
|
|
|
if (derived->is_recursive_with_table() &&
|
|
|
|
!derived->is_with_table_recursive_reference() &&
|
|
|
|
!derived->with->rec_result && derived->with->get_sq_rec_ref())
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is a non-recursive reference to a recursive CTE whose
|
|
|
|
specification unit has not been prepared at the regular processing of
|
|
|
|
derived table references. This can happen only in the case when
|
|
|
|
the specification unit has no recursive references at the top level.
|
|
|
|
Force the preparation of the specification unit. Use a recursive
|
|
|
|
table reference from a subquery for this.
|
|
|
|
*/
|
|
|
|
DBUG_ASSERT(derived->with->get_sq_rec_ref());
|
2018-04-04 11:16:12 +02:00
|
|
|
if (unlikely(mysql_derived_prepare(lex->thd, lex,
|
|
|
|
derived->with->get_sq_rec_ref())))
|
2017-04-29 06:58:04 +02:00
|
|
|
DBUG_RETURN(TRUE);
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:39:10 +02:00
|
|
|
if (unit->prepared && derived->is_recursive_with_table() &&
|
|
|
|
!derived->table)
|
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
/*
|
|
|
|
Here 'derived' is either a non-recursive table reference to a recursive
|
|
|
|
with table or a recursive table reference to a recursvive table whose
|
|
|
|
specification has been already prepared (a secondary recursive table
|
|
|
|
reference.
|
|
|
|
*/
|
2017-03-14 11:52:00 +01:00
|
|
|
if (!(derived->derived_result= new (thd->mem_root) select_unit(thd)))
|
2016-05-09 22:39:10 +02:00
|
|
|
DBUG_RETURN(TRUE); // out of memory
|
|
|
|
thd->create_tmp_table_for_derived= TRUE;
|
2016-08-30 07:45:17 +02:00
|
|
|
res= derived->derived_result->create_result_table(
|
|
|
|
thd, &unit->types, FALSE,
|
|
|
|
(first_select->options |
|
|
|
|
thd->variables.option_bits |
|
|
|
|
TMP_TABLE_ALL_COLUMNS),
|
2018-01-07 17:03:44 +01:00
|
|
|
&derived->alias, FALSE, FALSE, FALSE, 0);
|
2016-05-09 22:39:10 +02:00
|
|
|
thd->create_tmp_table_for_derived= FALSE;
|
|
|
|
|
2018-04-04 11:16:12 +02:00
|
|
|
if (likely(!res) && !derived->table)
|
2016-05-09 22:39:10 +02:00
|
|
|
{
|
|
|
|
derived->derived_result->set_unit(unit);
|
|
|
|
derived->table= derived->derived_result->table;
|
|
|
|
if (derived->is_with_table_recursive_reference())
|
2016-06-06 19:01:16 +02:00
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
/* Here 'derived" is a secondary recursive table reference */
|
2020-12-16 18:11:11 +01:00
|
|
|
unit->with_element->rec_result->rec_table_refs.push_back(derived);
|
2016-06-06 19:01:16 +02:00
|
|
|
}
|
2016-05-09 22:39:10 +02:00
|
|
|
}
|
|
|
|
DBUG_ASSERT(derived->table || res);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
// Skip already prepared views/DT
|
2016-05-10 21:32:02 +02:00
|
|
|
if (unit->prepared ||
|
2011-06-14 04:03:03 +02:00
|
|
|
(derived->merged_for_insert &&
|
|
|
|
!(derived->is_multitable() &&
|
|
|
|
(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
|
|
|
|
thd->lex->sql_command == SQLCOM_DELETE_MULTI))))
|
2019-11-25 14:01:43 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
System versioned tables may still require to get versioning conditions
|
2019-12-02 09:48:37 +01:00
|
|
|
when modifying view (see vers_setup_conds()). Only UPDATE and DELETE are
|
|
|
|
affected because they use WHERE condition.
|
2019-11-25 14:01:43 +01:00
|
|
|
*/
|
|
|
|
if (!unit->prepared &&
|
|
|
|
derived->table->versioned() &&
|
2019-12-02 09:48:37 +01:00
|
|
|
derived->merge_underlying_list &&
|
|
|
|
/* choose only those merged views that do not select from other views */
|
|
|
|
!derived->merge_underlying_list->merge_underlying_list)
|
|
|
|
{
|
|
|
|
switch (thd->lex->sql_command)
|
|
|
|
{
|
|
|
|
case SQLCOM_DELETE:
|
|
|
|
case SQLCOM_DELETE_MULTI:
|
|
|
|
case SQLCOM_UPDATE:
|
|
|
|
case SQLCOM_UPDATE_MULTI:
|
2019-12-03 13:46:49 +01:00
|
|
|
if ((res= first_select->vers_setup_conds(thd,
|
|
|
|
derived->merge_underlying_list)))
|
2019-12-02 09:48:37 +01:00
|
|
|
goto exit;
|
2019-12-03 13:46:49 +01:00
|
|
|
if (derived->merge_underlying_list->where)
|
|
|
|
{
|
|
|
|
Query_arena_stmt on_stmt_arena(thd);
|
|
|
|
derived->where= and_items(thd, derived->where,
|
|
|
|
derived->merge_underlying_list->where);
|
|
|
|
}
|
2019-12-02 09:48:37 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
DBUG_RETURN(FALSE);
|
2019-11-25 14:01:43 +01:00
|
|
|
}
|
2005-07-01 06:05:42 +02: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())
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2021-04-16 18:53:16 +02:00
|
|
|
// Prevent it for the WHERE clause
|
2010-05-26 22:18:18 +02:00
|
|
|
sl->context.outer_context= 0;
|
2021-04-16 18:53:16 +02:00
|
|
|
|
|
|
|
// And for ON clauses, if there are any
|
|
|
|
nullify_outer_context_for_on_clauses(*sl->join_list);
|
|
|
|
|
2016-07-27 07:58:33 +02:00
|
|
|
if (!derived->is_with_table_recursive_reference() ||
|
|
|
|
(!derived->with->with_anchor &&
|
|
|
|
!derived->with->is_with_prepared_anchor()))
|
2016-05-09 22:39:10 +02:00
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
/*
|
|
|
|
Prepare underlying views/DT first unless 'derived' is a recursive
|
|
|
|
table reference and either the anchors from the specification of
|
|
|
|
'derived' has been already prepared or there no anchor in this
|
|
|
|
specification
|
|
|
|
*/
|
2016-05-09 22:39:10 +02:00
|
|
|
if ((res= sl->handle_derived(lex, DT_PREPARE)))
|
|
|
|
goto exit;
|
|
|
|
}
|
2014-01-28 22:23:14 +01:00
|
|
|
if (derived->outer_join && sl->first_cond_optimization)
|
2013-09-25 14:30:13 +02:00
|
|
|
{
|
|
|
|
/* Mark that table is part of OUTER JOIN and fields may be NULL */
|
|
|
|
for (TABLE_LIST *cursor= (TABLE_LIST*) sl->table_list.first;
|
|
|
|
cursor;
|
|
|
|
cursor= cursor->next_local)
|
|
|
|
cursor->outer_join|= JOIN_TYPE_OUTER;
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
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
|
|
|
|
2019-02-13 06:52:16 +01:00
|
|
|
if (unlikely(thd->trace_started()))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Add to optimizer trace whether a derived table/view
|
|
|
|
is merged into the parent select or not.
|
|
|
|
*/
|
2019-02-18 12:41:20 +01:00
|
|
|
OPT_TRACE_VIEWS_TRANSFORM(thd, trace_wrapper, trace_derived,
|
|
|
|
derived->is_derived() ? "derived" : "view",
|
|
|
|
derived->alias.str ? derived->alias.str : "<NULL>",
|
|
|
|
derived->get_unit()->first_select()->select_number,
|
|
|
|
derived->is_merged_derived() ? "merged" : "materialized");
|
2019-02-13 06:52:16 +01:00
|
|
|
}
|
2018-01-15 14:50:35 +01:00
|
|
|
/*
|
|
|
|
Above cascade call of prepare is important for PS protocol, but after it
|
|
|
|
is called we can check if we really need prepare for this derived
|
|
|
|
*/
|
|
|
|
if (derived->merged)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("info", ("Irreversibly merged: exit"));
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
2016-11-12 05:55:03 +01:00
|
|
|
derived->fill_me= FALSE;
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2020-12-16 18:11:11 +01:00
|
|
|
if ((!derived->is_with_table_recursive_reference() ||
|
|
|
|
!derived->derived_result) &&
|
2020-12-23 19:28:02 +01:00
|
|
|
!(derived->derived_result= new (thd->mem_root) select_unit(thd)))
|
2010-05-26 22:18:18 +02:00
|
|
|
DBUG_RETURN(TRUE); // out of memory
|
|
|
|
|
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
2018-04-18 14:29:48 +02:00
|
|
|
if ((res= unit->prepare(derived, derived->derived_result, 0)))
|
2010-05-26 22:18:18 +02:00
|
|
|
goto exit;
|
2015-12-17 21:52:14 +01:00
|
|
|
if (derived->with &&
|
2020-01-27 21:50:16 +01:00
|
|
|
(res= derived->with->process_columns_of_derived_unit(thd, unit)))
|
|
|
|
goto exit;
|
2015-11-18 19:55:17 +01:00
|
|
|
if ((res= check_duplicate_names(thd, unit->types, 0)))
|
2010-05-26 22:18:18 +02:00
|
|
|
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.
|
|
|
|
*/
|
2020-12-16 18:11:11 +01:00
|
|
|
if (!derived->is_with_table_recursive_reference() &&
|
|
|
|
derived->init_derived(thd, FALSE))
|
2010-05-26 22:18:18 +02:00
|
|
|
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
|
2017-03-14 11:52:00 +01:00
|
|
|
distinct underlying query will be is_unit_op &&
|
2010-05-26 22:18:18 +02:00
|
|
|
!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;
|
2016-05-09 22:39:10 +02:00
|
|
|
if (!(derived->table) &&
|
|
|
|
derived->derived_result->create_result_table(thd, &unit->types, FALSE,
|
|
|
|
(first_select->options |
|
|
|
|
thd->variables.option_bits |
|
|
|
|
TMP_TABLE_ALL_COLUMNS),
|
2018-01-07 17:03:44 +01:00
|
|
|
&derived->alias,
|
2017-03-14 11:52:00 +01:00
|
|
|
FALSE, FALSE, FALSE,
|
|
|
|
0))
|
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
|
|
|
|
2016-05-09 22:39:10 +02:00
|
|
|
if (!derived->table)
|
|
|
|
derived->table= derived->derived_result->table;
|
2015-01-29 14:12:32 +01:00
|
|
|
DBUG_ASSERT(derived->table);
|
2010-05-26 22:18:18 +02:00
|
|
|
if (derived->is_derived() && derived->is_merged_derived())
|
|
|
|
first_select->mark_as_belong_to_derived(derived);
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2019-02-10 07:54:26 +01:00
|
|
|
derived->dt_handler= derived->find_derived_handler(thd);
|
|
|
|
if (derived->dt_handler)
|
|
|
|
{
|
|
|
|
char query_buff[4096];
|
|
|
|
String derived_query(query_buff, sizeof(query_buff), thd->charset());
|
|
|
|
derived_query.length(0);
|
|
|
|
derived->derived->print(&derived_query,
|
|
|
|
enum_query_type(QT_VIEW_INTERNAL |
|
|
|
|
QT_ITEM_ORIGINAL_FUNC_NULLIF |
|
|
|
|
QT_PARSABLE));
|
|
|
|
if (!thd->make_lex_string(&derived->derived_spec,
|
|
|
|
derived_query.ptr(), derived_query.length()))
|
|
|
|
{
|
|
|
|
delete derived->dt_handler;
|
|
|
|
derived->dt_handler= NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() &&
|
2013-06-15 17:32:08 +02:00
|
|
|
(thd->get_stmt_da()->sql_errno() == ER_BAD_FIELD_ERROR ||
|
|
|
|
thd->get_stmt_da()->sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION ||
|
|
|
|
thd->get_stmt_da()->sql_errno() == ER_SP_DOES_NOT_EXIST))
|
2004-12-16 14:31:36 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
thd->clear_error();
|
2018-01-07 17:03:44 +01:00
|
|
|
my_error(ER_VIEW_INVALID, MYF(0), derived->db.str,
|
|
|
|
derived->table_name.str);
|
2004-12-16 14:31:36 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
2004-12-16 14:31:36 +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)
|
|
|
|
{
|
2017-03-27 07:59:33 +02:00
|
|
|
if (!derived->is_with_table_recursive_reference())
|
|
|
|
{
|
2019-12-02 09:48:37 +01:00
|
|
|
if (derived->table && derived->table->s->tmp_table)
|
2017-03-27 07:59:33 +02:00
|
|
|
free_tmp_table(thd, derived->table);
|
|
|
|
delete derived->derived_result;
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TABLE *table= derived->table;
|
|
|
|
table->derived_select_number= first_select->select_number;
|
2014-01-24 13:50:18 +01:00
|
|
|
table->s->tmp_table= INTERNAL_TMP_TABLE;
|
2010-05-26 22:18:18 +02:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2017-08-07 13:42:35 +02:00
|
|
|
if (derived->is_view())
|
2010-05-26 22:18:18 +02:00
|
|
|
table->grant= derived->grant;
|
2004-11-05 16:29:47 +01:00
|
|
|
else
|
|
|
|
{
|
2017-08-07 13:42:35 +02:00
|
|
|
DBUG_ASSERT(derived->is_derived());
|
|
|
|
DBUG_ASSERT(derived->is_anonymous_derived_table());
|
2010-05-26 22:18:18 +02:00
|
|
|
table->grant.privilege= SELECT_ACL;
|
2017-08-07 13:42:35 +02:00
|
|
|
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 */
|
2016-05-09 22:39:10 +02:00
|
|
|
if (!derived->is_with_table_recursive_reference())
|
|
|
|
{
|
|
|
|
table->next= thd->derived_tables;
|
|
|
|
thd->derived_tables= table;
|
|
|
|
}
|
2013-09-25 14:30:13 +02:00
|
|
|
|
|
|
|
/* If table is used by a left join, mark that any column may be null */
|
|
|
|
if (derived->outer_join)
|
|
|
|
table->maybe_null= 1;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
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
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
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;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_derived_optimize");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2018-01-15 14:50:35 +01:00
|
|
|
if (derived->merged)
|
|
|
|
{
|
|
|
|
DBUG_PRINT("info", ("Irreversibly merged: exit"));
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2019-02-10 07:54:26 +01:00
|
|
|
if (derived->is_materialized_derived() && derived->dt_handler)
|
2018-10-09 11:36:09 +02:00
|
|
|
{
|
2019-02-13 07:56:24 +01:00
|
|
|
/* Create an object for execution of the query specifying the table */
|
2018-10-09 11:36:09 +02:00
|
|
|
if (!(derived->pushdown_derived=
|
|
|
|
new (thd->mem_root) Pushdown_derived(derived, derived->dt_handler)))
|
|
|
|
{
|
|
|
|
delete derived->dt_handler;
|
2019-02-10 07:54:26 +01:00
|
|
|
derived->dt_handler= NULL;
|
|
|
|
DBUG_RETURN(TRUE);
|
2018-10-09 11:36:09 +02:00
|
|
|
}
|
2019-02-10 07:54:26 +01:00
|
|
|
}
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
lex->current_select= first_select;
|
|
|
|
|
2017-03-14 11:52:00 +01:00
|
|
|
if (unit->is_unit_op())
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2017-08-04 06:19:19 +02:00
|
|
|
if (unit->optimized)
|
|
|
|
DBUG_RETURN(FALSE);
|
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;
|
2014-10-14 18:36:50 +02:00
|
|
|
unit->set_limit(unit->global_parameters());
|
2017-08-04 06:19:19 +02:00
|
|
|
if (join &&
|
2017-09-08 20:26:35 +02:00
|
|
|
join->optimization_state == JOIN::OPTIMIZATION_PHASE_1_DONE &&
|
2017-08-04 06:19:19 +02:00
|
|
|
join->with_two_phase_optimization)
|
|
|
|
{
|
|
|
|
if (unit->optimized_2)
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
unit->optimized_2= TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unit->optimized)
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
unit->optimized= TRUE;
|
2019-02-23 23:53:27 +01:00
|
|
|
if (!join)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This happens when derived is used in SELECT for which
|
|
|
|
zer_result_cause != 0.
|
|
|
|
In this case join is already destroyed.
|
|
|
|
*/
|
|
|
|
DBUG_RETURN(FALSE);
|
|
|
|
}
|
2017-08-04 06:19:19 +02:00
|
|
|
}
|
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-01-14 14:15:42 +01:00
|
|
|
{
|
2010-05-26 22:18:18 +02:00
|
|
|
derived->change_refs_to_fields();
|
|
|
|
derived->set_materialized_derived();
|
2004-11-05 16:29:47 +01: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;
|
2004-09-04 22:05:12 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
err:
|
|
|
|
lex->current_select= save_current_select;
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(res);
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
Bug #58730 Assertion failed: table->key_read == 0 in close_thread_table,
temptable views
The TABLE::key_read field indicates if the optimizer has found that row
retrieval only should access the index tree. The triggered assert
inside close_thread_table() checks that this field has been reset when
the table is about to be closed.
During normal execution, these fields are reset right before tables are
closed at the end of mysql_execute_command(). But in the case of errors,
tables are closed earlier. The patch for Bug#52044 refactored the open
tables code so that close_thread_tables() is called immediately if
opening of tables fails. At this point in the execution, it could
happend that all TABLE::key_read fields had not been properly reset,
therefore triggering the assert.
The problematic statement in this case was EXPLAIN where the query
accessed two derived tables and where the first derived table was
processed successfully while the second derived table was not.
Since it was an EXPLAIN, TABLE::key_read fields were not reset after
successful derived table processing since the state needs to be
accessible afterwards. When processing of the second derived table
failed, it's corresponding SELECT_LEX_UNIT was cleaned, which caused
it's TABLE::key_read fields to be reset. Since processing failed,
the error path of open_and_lock_tables() was entered and
close_thread_tables() was called. The assert was then triggered due
to the TABLE::key_read fields set during processing of the first
derived table.
This patch fixes the problem by adding a new derived table processor,
mysql_derived_cleanup() that is called after mysql_derived_filling().
It causes cleanup of all SELECT_LEX_UNITs to be called, resetting
all relevant TABLE::key_read fields.
Test case added to derived.test.
2010-12-16 10:55:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-05-26 22:18:18 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_create(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_derived_create");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2010-05-26 22:18:18 +02:00
|
|
|
TABLE *table= derived->table;
|
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
|
|
|
|
2016-05-08 22:04:41 +02:00
|
|
|
if (table->is_created())
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2017-03-14 11:52:00 +01:00
|
|
|
select_unit *result= derived->derived_result;
|
2010-05-26 22:18:18 +02:00
|
|
|
if (table->s->db_type() == TMP_ENGINE_HTON)
|
|
|
|
{
|
2012-03-13 21:34:20 +01:00
|
|
|
result->tmp_table_param.keyinfo= table->s->key_info;
|
2010-05-26 22:18:18 +02:00
|
|
|
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 |
|
2013-06-05 22:53:35 +02:00
|
|
|
thd->variables.option_bits | TMP_TABLE_ALL_COLUMNS)))
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2004-09-04 22:05:12 +02:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
if (open_tmp_table(table))
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(TRUE);
|
2010-05-26 22:18:18 +02:00
|
|
|
table->file->extra(HA_EXTRA_WRITE_CACHE);
|
|
|
|
table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-22 19:08:49 +01:00
|
|
|
void TABLE_LIST::register_as_derived_with_rec_ref(With_element *rec_elem)
|
|
|
|
{
|
|
|
|
rec_elem->derived_with_rec_ref.link_in_list(this, &this->next_with_rec_ref);
|
|
|
|
is_derived_with_recursive_reference= true;
|
|
|
|
get_unit()->uncacheable|= UNCACHEABLE_DEPENDENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TABLE_LIST::is_nonrecursive_derived_with_rec_ref()
|
|
|
|
{
|
|
|
|
return is_derived_with_recursive_reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-30 07:45:17 +02:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Fill the recursive with table
|
|
|
|
|
|
|
|
@param thd The thread handle
|
|
|
|
|
|
|
|
@details
|
|
|
|
The method is called only for recursive with tables.
|
|
|
|
The method executes the recursive part of the specification
|
|
|
|
of this with table until no more rows are added to the table
|
|
|
|
or the number of the performed iteration reaches the allowed
|
|
|
|
maximum.
|
|
|
|
|
|
|
|
@retval
|
|
|
|
false on success
|
|
|
|
true on failure
|
|
|
|
*/
|
|
|
|
|
2016-06-26 06:38:40 +02:00
|
|
|
bool TABLE_LIST::fill_recursive(THD *thd)
|
|
|
|
{
|
|
|
|
bool rc= false;
|
|
|
|
st_select_lex_unit *unit= get_unit();
|
2016-08-30 07:45:17 +02:00
|
|
|
rc= with->instantiate_tmp_tables();
|
|
|
|
while (!rc && !with->all_are_stabilized())
|
2016-08-11 00:51:40 +02:00
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
if (with->level > thd->variables.max_recursive_iterations)
|
|
|
|
break;
|
|
|
|
with->prepare_for_next_iteration();
|
2016-08-11 00:51:40 +02:00
|
|
|
rc= unit->exec_recursive();
|
|
|
|
}
|
2016-08-30 07:45:17 +02:00
|
|
|
if (!rc)
|
2016-06-26 06:38:40 +02:00
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
TABLE *src= with->rec_result->table;
|
2017-01-04 23:33:24 +01:00
|
|
|
rc =src->insert_all_rows_into_tmp_table(thd,
|
|
|
|
table,
|
|
|
|
&with->rec_result->tmp_table_param,
|
|
|
|
true);
|
2016-08-30 07:45:17 +02:00
|
|
|
}
|
2016-06-26 06:38:40 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-26 22:18:18 +02:00
|
|
|
/*
|
|
|
|
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
|
2016-08-30 07:45:17 +02:00
|
|
|
table. After result table is filled, if this is not the EXPLAIN statement
|
|
|
|
and the table is not specified with a recursion 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.
|
2010-05-26 22:18:18 +02:00
|
|
|
'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
|
Bug #58730 Assertion failed: table->key_read == 0 in close_thread_table,
temptable views
The TABLE::key_read field indicates if the optimizer has found that row
retrieval only should access the index tree. The triggered assert
inside close_thread_table() checks that this field has been reset when
the table is about to be closed.
During normal execution, these fields are reset right before tables are
closed at the end of mysql_execute_command(). But in the case of errors,
tables are closed earlier. The patch for Bug#52044 refactored the open
tables code so that close_thread_tables() is called immediately if
opening of tables fails. At this point in the execution, it could
happend that all TABLE::key_read fields had not been properly reset,
therefore triggering the assert.
The problematic statement in this case was EXPLAIN where the query
accessed two derived tables and where the first derived table was
processed successfully while the second derived table was not.
Since it was an EXPLAIN, TABLE::key_read fields were not reset after
successful derived table processing since the state needs to be
accessible afterwards. When processing of the second derived table
failed, it's corresponding SELECT_LEX_UNIT was cleaned, which caused
it's TABLE::key_read fields to be reset. Since processing failed,
the error path of open_and_lock_tables() was entered and
close_thread_tables() was called. The assert was then triggered due
to the TABLE::key_read fields set during processing of the first
derived table.
This patch fixes the problem by adding a new derived table processor,
mysql_derived_cleanup() that is called after mysql_derived_filling().
It causes cleanup of all SELECT_LEX_UNITs to be called, resetting
all relevant TABLE::key_read fields.
Test case added to derived.test.
2010-12-16 10:55:23 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_fill(THD *thd, LEX *lex, TABLE_LIST *derived)
|
Bug #58730 Assertion failed: table->key_read == 0 in close_thread_table,
temptable views
The TABLE::key_read field indicates if the optimizer has found that row
retrieval only should access the index tree. The triggered assert
inside close_thread_table() checks that this field has been reset when
the table is about to be closed.
During normal execution, these fields are reset right before tables are
closed at the end of mysql_execute_command(). But in the case of errors,
tables are closed earlier. The patch for Bug#52044 refactored the open
tables code so that close_thread_tables() is called immediately if
opening of tables fails. At this point in the execution, it could
happend that all TABLE::key_read fields had not been properly reset,
therefore triggering the assert.
The problematic statement in this case was EXPLAIN where the query
accessed two derived tables and where the first derived table was
processed successfully while the second derived table was not.
Since it was an EXPLAIN, TABLE::key_read fields were not reset after
successful derived table processing since the state needs to be
accessible afterwards. When processing of the second derived table
failed, it's corresponding SELECT_LEX_UNIT was cleaned, which caused
it's TABLE::key_read fields to be reset. Since processing failed,
the error path of open_and_lock_tables() was entered and
close_thread_tables() was called. The assert was then triggered due
to the TABLE::key_read fields set during processing of the first
derived table.
This patch fixes the problem by adding a new derived table processor,
mysql_derived_cleanup() that is called after mysql_derived_filling().
It causes cleanup of all SELECT_LEX_UNITs to be called, resetting
all relevant TABLE::key_read fields.
Test case added to derived.test.
2010-12-16 10:55:23 +01:00
|
|
|
{
|
2017-12-20 13:52:27 +01:00
|
|
|
Field_iterator_table field_iterator;
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX_UNIT *unit= derived->get_unit();
|
2016-06-06 19:01:16 +02:00
|
|
|
bool derived_is_recursive= derived->is_recursive_with_table();
|
2010-05-26 22:18:18 +02:00
|
|
|
bool res= FALSE;
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_ENTER("mysql_derived_fill");
|
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2010-05-26 22:18:18 +02:00
|
|
|
|
2016-06-06 19:01:16 +02:00
|
|
|
if (unit->executed && !unit->uncacheable && !unit->describe &&
|
|
|
|
!derived_is_recursive)
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
/*check that table creation passed without problems. */
|
2016-05-08 22:04:41 +02:00
|
|
|
DBUG_ASSERT(derived->table && derived->table->is_created());
|
2017-03-14 11:52:00 +01:00
|
|
|
select_unit *derived_result= derived->derived_result;
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
2017-08-04 06:19:19 +02:00
|
|
|
|
2018-10-09 11:36:09 +02:00
|
|
|
if (derived->pushdown_derived)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (unit->executed)
|
|
|
|
DBUG_RETURN(FALSE);
|
2019-02-13 07:56:24 +01:00
|
|
|
/* Execute the query that specifies the derived table by a foreign engine */
|
2018-10-09 11:36:09 +02:00
|
|
|
res= derived->pushdown_derived->execute();
|
2019-02-10 07:54:26 +01:00
|
|
|
unit->executed= true;
|
2018-10-09 11:36:09 +02:00
|
|
|
delete derived->pushdown_derived;
|
|
|
|
DBUG_RETURN(res);
|
|
|
|
}
|
|
|
|
|
2017-08-10 23:25:19 +02:00
|
|
|
if (unit->executed && !derived_is_recursive &&
|
|
|
|
(unit->uncacheable & UNCACHEABLE_DEPENDENT))
|
2017-08-04 06:19:19 +02:00
|
|
|
{
|
|
|
|
if ((res= derived->table->file->ha_delete_all_rows()))
|
|
|
|
goto err;
|
2017-08-10 23:25:19 +02:00
|
|
|
JOIN *join= unit->first_select()->join;
|
|
|
|
join->first_record= false;
|
|
|
|
for (uint i= join->top_join_tab_count;
|
|
|
|
i < join->top_join_tab_count + join->aggr_tables;
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
if ((res= join->join_tab[i].table->file->ha_delete_all_rows()))
|
|
|
|
goto err;
|
|
|
|
}
|
2017-08-04 06:19:19 +02:00
|
|
|
}
|
2016-06-26 06:38:40 +02:00
|
|
|
|
|
|
|
if (derived_is_recursive)
|
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
if (derived->is_with_table_recursive_reference())
|
|
|
|
{
|
|
|
|
/* Here only one iteration step is performed */
|
|
|
|
res= unit->exec_recursive();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In this case all iteration are performed */
|
|
|
|
res= derived->fill_recursive(thd);
|
|
|
|
}
|
2016-06-26 06:38:40 +02:00
|
|
|
}
|
2017-03-14 11:52:00 +01:00
|
|
|
else if (unit->is_unit_op())
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
|
|
|
// execute union without clean up
|
|
|
|
res= unit->exec();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-30 07:45:17 +02:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
2014-10-14 18:36:50 +02:00
|
|
|
unit->set_limit(unit->global_parameters());
|
2019-09-26 09:49:50 +02:00
|
|
|
if (unit->lim.is_unlimited())
|
2010-05-26 22:18:18 +02:00
|
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
|
|
|
|
lex->current_select= first_select;
|
2016-05-08 22:04:41 +02:00
|
|
|
res= mysql_select(thd,
|
2011-10-19 21:45:18 +02:00
|
|
|
first_select->table_list.first,
|
2010-05-26 22:18:18 +02:00
|
|
|
first_select->item_list, first_select->where,
|
|
|
|
(first_select->order_list.elements+
|
|
|
|
first_select->group_list.elements),
|
2011-10-19 21:45:18 +02:00
|
|
|
first_select->order_list.first,
|
|
|
|
first_select->group_list.first,
|
2010-05-26 22:18:18 +02:00
|
|
|
first_select->having, (ORDER*) NULL,
|
2011-10-19 21:45:18 +02:00
|
|
|
(first_select->options |thd->variables.option_bits |
|
2010-05-26 22:18:18 +02:00
|
|
|
SELECT_NO_UNLOCK),
|
|
|
|
derived_result, unit, first_select);
|
|
|
|
}
|
|
|
|
|
2016-06-26 06:38:40 +02:00
|
|
|
if (!res && !derived_is_recursive)
|
2010-05-26 22:18:18 +02:00
|
|
|
{
|
|
|
|
if (derived_result->flush())
|
|
|
|
res= TRUE;
|
|
|
|
unit->executed= TRUE;
|
2017-12-20 13:52:27 +01:00
|
|
|
|
|
|
|
if (derived->field_translation)
|
|
|
|
{
|
|
|
|
/* reset translation table to materialized table */
|
|
|
|
field_iterator.set_table(derived->table);
|
|
|
|
for (uint i= 0;
|
|
|
|
!field_iterator.end_of_fields();
|
|
|
|
field_iterator.next(), i= i + 1)
|
|
|
|
{
|
|
|
|
Item *item;
|
|
|
|
|
|
|
|
if (!(item= field_iterator.create_item(thd)))
|
|
|
|
{
|
|
|
|
res= TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
thd->change_item_tree(&derived->field_translation[i].item, item);
|
|
|
|
}
|
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
2017-08-04 06:19:19 +02:00
|
|
|
err:
|
2020-06-08 10:09:49 +02:00
|
|
|
if (res || (!derived_is_recursive && !lex->describe && !unit->uncacheable))
|
Bug #58730 Assertion failed: table->key_read == 0 in close_thread_table,
temptable views
The TABLE::key_read field indicates if the optimizer has found that row
retrieval only should access the index tree. The triggered assert
inside close_thread_table() checks that this field has been reset when
the table is about to be closed.
During normal execution, these fields are reset right before tables are
closed at the end of mysql_execute_command(). But in the case of errors,
tables are closed earlier. The patch for Bug#52044 refactored the open
tables code so that close_thread_tables() is called immediately if
opening of tables fails. At this point in the execution, it could
happend that all TABLE::key_read fields had not been properly reset,
therefore triggering the assert.
The problematic statement in this case was EXPLAIN where the query
accessed two derived tables and where the first derived table was
processed successfully while the second derived table was not.
Since it was an EXPLAIN, TABLE::key_read fields were not reset after
successful derived table processing since the state needs to be
accessible afterwards. When processing of the second derived table
failed, it's corresponding SELECT_LEX_UNIT was cleaned, which caused
it's TABLE::key_read fields to be reset. Since processing failed,
the error path of open_and_lock_tables() was entered and
close_thread_tables() was called. The assert was then triggered due
to the TABLE::key_read fields set during processing of the first
derived table.
This patch fixes the problem by adding a new derived table processor,
mysql_derived_cleanup() that is called after mysql_derived_filling().
It causes cleanup of all SELECT_LEX_UNITs to be called, resetting
all relevant TABLE::key_read fields.
Test case added to derived.test.
2010-12-16 10:55:23 +01:00
|
|
|
unit->cleanup();
|
2010-05-26 22:18:18 +02:00
|
|
|
lex->current_select= save_current_select;
|
|
|
|
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(res);
|
Bug #58730 Assertion failed: table->key_read == 0 in close_thread_table,
temptable views
The TABLE::key_read field indicates if the optimizer has found that row
retrieval only should access the index tree. The triggered assert
inside close_thread_table() checks that this field has been reset when
the table is about to be closed.
During normal execution, these fields are reset right before tables are
closed at the end of mysql_execute_command(). But in the case of errors,
tables are closed earlier. The patch for Bug#52044 refactored the open
tables code so that close_thread_tables() is called immediately if
opening of tables fails. At this point in the execution, it could
happend that all TABLE::key_read fields had not been properly reset,
therefore triggering the assert.
The problematic statement in this case was EXPLAIN where the query
accessed two derived tables and where the first derived table was
processed successfully while the second derived table was not.
Since it was an EXPLAIN, TABLE::key_read fields were not reset after
successful derived table processing since the state needs to be
accessible afterwards. When processing of the second derived table
failed, it's corresponding SELECT_LEX_UNIT was cleaned, which caused
it's TABLE::key_read fields to be reset. Since processing failed,
the error path of open_and_lock_tables() was entered and
close_thread_tables() was called. The assert was then triggered due
to the TABLE::key_read fields set during processing of the first
derived table.
This patch fixes the problem by adding a new derived table processor,
mysql_derived_cleanup() that is called after mysql_derived_filling().
It causes cleanup of all SELECT_LEX_UNITs to be called, resetting
all relevant TABLE::key_read fields.
Test case added to derived.test.
2010-12-16 10:55:23 +01:00
|
|
|
}
|
2010-05-26 22:18:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2019-04-16 11:58:56 +02:00
|
|
|
static
|
2010-05-26 22:18:18 +02:00
|
|
|
bool mysql_derived_reinit(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_ENTER("mysql_derived_reinit");
|
2017-12-20 13:52:27 +01:00
|
|
|
DBUG_PRINT("enter", ("Alias: '%s' Unit: %p",
|
2018-01-07 17:03:44 +01:00
|
|
|
(derived->alias.str ? derived->alias.str : "<NULL>"),
|
2017-12-20 13:52:27 +01:00
|
|
|
derived->get_unit()));
|
2010-05-26 22:18:18 +02:00
|
|
|
st_select_lex_unit *unit= derived->get_unit();
|
|
|
|
|
2015-01-29 14:12:32 +01:00
|
|
|
derived->merged_for_insert= FALSE;
|
2010-05-26 22:18:18 +02:00
|
|
|
unit->unclean();
|
|
|
|
unit->types.empty();
|
2014-11-14 13:46:21 +01:00
|
|
|
/* for derived tables & PS (which can't be reset by Item_subselect) */
|
2010-05-26 22:18:18 +02:00
|
|
|
unit->reinit_exec_mechanism();
|
|
|
|
unit->set_thd(thd);
|
2014-03-07 12:57:07 +01:00
|
|
|
DBUG_RETURN(FALSE);
|
2010-05-26 22:18:18 +02:00
|
|
|
}
|
2016-05-01 21:29:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief
|
2018-05-15 23:45:59 +02:00
|
|
|
Extract condition that can be pushed into a derived table/view
|
2016-05-01 21:29:47 +02:00
|
|
|
|
2018-05-15 23:45:59 +02:00
|
|
|
@param thd the thread handle
|
|
|
|
@param cond current condition
|
|
|
|
@param derived the reference to the derived table/view
|
2016-05-01 21:29:47 +02:00
|
|
|
|
|
|
|
@details
|
2018-05-15 23:45:59 +02:00
|
|
|
This function builds the most restrictive condition depending only on
|
|
|
|
the derived table/view (directly or indirectly through equality) that
|
|
|
|
can be extracted from the given condition cond and pushes it into the
|
|
|
|
derived table/view.
|
|
|
|
|
|
|
|
Example of the transformation:
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
FROM t1,
|
|
|
|
(
|
|
|
|
SELECT x,MAX(y) AS max_y
|
|
|
|
FROM t2
|
|
|
|
GROUP BY x
|
|
|
|
) AS d_tab
|
|
|
|
WHERE d_tab.x>1 AND d_tab.max_y<30;
|
|
|
|
|
|
|
|
=>
|
|
|
|
|
|
|
|
SELECT *
|
|
|
|
FROM t1,
|
|
|
|
(
|
|
|
|
SELECT x,z,MAX(y) AS max_y
|
|
|
|
FROM t2
|
|
|
|
WHERE x>1
|
|
|
|
HAVING max_y<30
|
|
|
|
GROUP BY x
|
|
|
|
) AS d_tab
|
|
|
|
WHERE d_tab.x>1 AND d_tab.max_y<30;
|
|
|
|
|
|
|
|
In details:
|
|
|
|
1. Check what pushable formula can be extracted from cond
|
|
|
|
2. Build a clone PC of the formula that can be extracted
|
|
|
|
(the clone is built only if the extracted formula is a AND subformula
|
|
|
|
of cond or conjunction of such subformulas)
|
|
|
|
Do for every select specifying derived table/view:
|
|
|
|
3. If there is no HAVING clause prepare PC to be conjuncted with
|
|
|
|
WHERE clause of the select. Otherwise do 4-7.
|
|
|
|
4. Check what formula PC_where can be extracted from PC to be pushed
|
|
|
|
into the WHERE clause of the select
|
|
|
|
5. Build PC_where and if PC_where is a conjunct(s) of PC remove it from PC
|
|
|
|
getting PC_having
|
|
|
|
6. Prepare PC_where to be conjuncted with the WHERE clause of the select
|
|
|
|
7. Prepare PC_having to be conjuncted with the HAVING clause of the select
|
|
|
|
@note
|
|
|
|
This method is similar to pushdown_cond_for_in_subquery()
|
|
|
|
|
|
|
|
@retval TRUE if an error occurs
|
|
|
|
@retval FALSE otherwise
|
2016-05-01 21:29:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
bool pushdown_cond_for_derived(THD *thd, Item *cond, TABLE_LIST *derived)
|
|
|
|
{
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_ENTER("pushdown_cond_for_derived");
|
2016-05-01 21:29:47 +02:00
|
|
|
if (!cond)
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-09-02 08:44:42 +02:00
|
|
|
|
2016-09-05 05:11:58 +02:00
|
|
|
st_select_lex_unit *unit= derived->get_unit();
|
2020-11-18 22:21:19 +01:00
|
|
|
st_select_lex *first_sl= unit->first_select();
|
|
|
|
st_select_lex *sl= first_sl;
|
2016-09-05 05:11:58 +02:00
|
|
|
|
2017-04-25 08:58:23 +02:00
|
|
|
if (derived->prohibit_cond_pushdown)
|
|
|
|
DBUG_RETURN(false);
|
|
|
|
|
2016-09-26 02:28:49 +02:00
|
|
|
/* Do not push conditions into constant derived */
|
|
|
|
if (unit->executed)
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-09-26 02:28:49 +02:00
|
|
|
|
|
|
|
/* Do not push conditions into recursive with tables */
|
|
|
|
if (derived->is_recursive_with_table())
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-09-26 02:28:49 +02:00
|
|
|
|
|
|
|
/* Do not push conditions into unit with global ORDER BY ... LIMIT */
|
2020-12-19 12:59:37 +01:00
|
|
|
if (unit->fake_select_lex &&
|
|
|
|
unit->fake_select_lex->limit_params.explicit_limit)
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-09-26 02:28:49 +02:00
|
|
|
|
2016-09-05 05:11:58 +02:00
|
|
|
/* Check whether any select of 'unit' allows condition pushdown */
|
2016-09-26 02:28:49 +02:00
|
|
|
bool some_select_allows_cond_pushdown= false;
|
2016-09-05 05:11:58 +02:00
|
|
|
for (; sl; sl= sl->next_select())
|
|
|
|
{
|
|
|
|
if (sl->cond_pushdown_is_allowed())
|
|
|
|
{
|
2016-09-26 02:28:49 +02:00
|
|
|
some_select_allows_cond_pushdown= true;
|
2016-09-05 05:11:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 02:28:49 +02:00
|
|
|
if (!some_select_allows_cond_pushdown)
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-09-05 05:11:58 +02:00
|
|
|
|
2018-05-15 23:45:59 +02:00
|
|
|
/* 1. Check what pushable formula can be extracted from cond */
|
2016-05-01 21:29:47 +02:00
|
|
|
Item *extracted_cond;
|
2018-05-15 23:45:59 +02:00
|
|
|
cond->check_pushable_cond(&Item::pushable_cond_checker_for_derived,
|
|
|
|
(uchar *)(&derived->table->map));
|
|
|
|
/* 2. Build a clone PC of the formula that can be extracted */
|
|
|
|
extracted_cond=
|
|
|
|
cond->build_pushable_cond(thd,
|
|
|
|
&Item::pushable_equality_checker_for_derived,
|
|
|
|
((uchar *)&derived->table->map));
|
2016-05-01 21:29:47 +02:00
|
|
|
if (!extracted_cond)
|
|
|
|
{
|
|
|
|
/* Nothing can be pushed into the derived table */
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-05-01 21:29:47 +02:00
|
|
|
}
|
2018-05-15 23:45:59 +02:00
|
|
|
|
2016-05-01 21:29:47 +02:00
|
|
|
st_select_lex *save_curr_select= thd->lex->current_select;
|
|
|
|
for (; sl; sl= sl->next_select())
|
|
|
|
{
|
2017-08-13 04:58:16 +02:00
|
|
|
Item *extracted_cond_copy;
|
2021-01-12 07:20:31 +01:00
|
|
|
if (!sl->cond_pushdown_is_allowed())
|
|
|
|
continue;
|
2016-05-01 21:29:47 +02:00
|
|
|
/*
|
|
|
|
For each select of the unit except the last one
|
|
|
|
create a clone of extracted_cond
|
|
|
|
*/
|
2017-08-13 04:58:16 +02:00
|
|
|
extracted_cond_copy= !sl->next_select() ?
|
|
|
|
extracted_cond :
|
2017-11-22 07:01:07 +01:00
|
|
|
extracted_cond->build_clone(thd);
|
2016-05-01 21:29:47 +02:00
|
|
|
if (!extracted_cond_copy)
|
|
|
|
continue;
|
2016-09-05 05:11:58 +02:00
|
|
|
|
2020-11-18 22:21:19 +01:00
|
|
|
/*
|
|
|
|
Rename the columns of all non-first selects of a union to be compatible
|
|
|
|
by names with the columns of the first select. It will allow to use copies
|
|
|
|
of the same expression pushed into having clauses of different selects.
|
|
|
|
*/
|
|
|
|
if (sl != first_sl)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(sl->item_list.elements == first_sl->item_list.elements);
|
|
|
|
List_iterator_fast<Item> it(sl->item_list);
|
|
|
|
List_iterator_fast<Item> nm_it(unit->types);
|
2020-12-01 18:51:14 +01:00
|
|
|
while (Item *item= it++)
|
2020-11-18 22:21:19 +01:00
|
|
|
item->share_name_with(nm_it++);
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:45:59 +02:00
|
|
|
/* Collect fields that are used in the GROUP BY of sl */
|
|
|
|
if (sl->have_window_funcs())
|
2016-05-01 21:29:47 +02:00
|
|
|
{
|
2018-05-15 23:45:59 +02:00
|
|
|
if (sl->group_list.first || sl->join->implicit_grouping)
|
|
|
|
continue;
|
|
|
|
ORDER *common_partition_fields=
|
|
|
|
sl->find_common_window_func_partition_fields(thd);
|
|
|
|
if (!common_partition_fields)
|
2016-05-01 21:29:47 +02:00
|
|
|
continue;
|
2018-06-17 19:48:00 +02:00
|
|
|
sl->collect_grouping_fields_for_derived(thd, common_partition_fields);
|
2016-05-01 21:29:47 +02:00
|
|
|
}
|
2018-05-15 23:45:59 +02:00
|
|
|
else
|
2018-06-17 19:48:00 +02:00
|
|
|
sl->collect_grouping_fields_for_derived(thd, sl->group_list.first);
|
2018-05-15 23:45:59 +02:00
|
|
|
|
|
|
|
Item *remaining_cond= NULL;
|
|
|
|
/* Do 4-6 */
|
|
|
|
sl->pushdown_cond_into_where_clause(thd, extracted_cond_copy,
|
|
|
|
&remaining_cond,
|
|
|
|
&Item::derived_field_transformer_for_where,
|
|
|
|
(uchar *) sl);
|
2020-12-01 18:51:14 +01:00
|
|
|
|
2018-05-15 23:45:59 +02:00
|
|
|
if (!remaining_cond)
|
|
|
|
continue;
|
2016-05-01 21:29:47 +02:00
|
|
|
/*
|
2018-05-15 23:45:59 +02:00
|
|
|
7. Prepare PC_having to be conjuncted with the HAVING clause of
|
|
|
|
the select
|
2016-05-01 21:29:47 +02:00
|
|
|
*/
|
2018-05-15 23:45:59 +02:00
|
|
|
remaining_cond=
|
|
|
|
remaining_cond->transform(thd,
|
|
|
|
&Item::derived_field_transformer_for_having,
|
|
|
|
(uchar *) sl);
|
|
|
|
if (!remaining_cond)
|
2016-05-01 21:29:47 +02:00
|
|
|
continue;
|
2016-09-14 20:44:41 +02:00
|
|
|
|
2019-03-23 13:28:22 +01:00
|
|
|
if (remaining_cond->walk(&Item::cleanup_excluding_const_fields_processor,
|
|
|
|
0, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mark_or_conds_to_avoid_pushdown(remaining_cond);
|
|
|
|
|
|
|
|
sl->cond_pushed_into_having= remaining_cond;
|
2016-05-01 21:29:47 +02:00
|
|
|
}
|
|
|
|
thd->lex->current_select= save_curr_select;
|
2017-01-18 09:27:19 +01:00
|
|
|
DBUG_RETURN(false);
|
2016-05-01 21:29:47 +02:00
|
|
|
}
|
2018-10-09 11:36:09 +02:00
|
|
|
|
|
|
|
|
2019-02-13 07:56:24 +01:00
|
|
|
/**
|
|
|
|
@brief
|
|
|
|
Look for provision of the derived_handler interface by a foreign engine
|
|
|
|
|
|
|
|
@param thd The thread handler
|
|
|
|
|
|
|
|
@details
|
|
|
|
The function looks through its tables of the query that specifies this
|
|
|
|
derived table searching for a table whose handlerton owns a
|
|
|
|
create_derived call-back function. If the call of this function returns
|
|
|
|
a derived_handler interface object then the server will push the query
|
|
|
|
specifying the derived table into this engine.
|
|
|
|
This is a responsibility of the create_derived call-back function to
|
|
|
|
check whether the engine can execute the query.
|
|
|
|
|
|
|
|
@retval the found derived_handler if the search is successful
|
|
|
|
0 otherwise
|
|
|
|
*/
|
|
|
|
|
2018-10-09 11:36:09 +02:00
|
|
|
derived_handler *TABLE_LIST::find_derived_handler(THD *thd)
|
|
|
|
{
|
|
|
|
if (!derived || is_recursive_with_table())
|
|
|
|
return 0;
|
|
|
|
for (SELECT_LEX *sl= derived->first_select(); sl; sl= sl->next_select())
|
|
|
|
{
|
|
|
|
if (!(sl->join))
|
|
|
|
continue;
|
|
|
|
for (TABLE_LIST *tbl= sl->join->tables_list; tbl; tbl= tbl->next_local)
|
|
|
|
{
|
|
|
|
if (!tbl->table)
|
|
|
|
continue;
|
|
|
|
handlerton *ht= tbl->table->file->partition_ht();
|
|
|
|
if (!ht->create_derived)
|
|
|
|
continue;
|
|
|
|
derived_handler *dh= ht->create_derived(thd, this);
|
|
|
|
if (dh)
|
|
|
|
{
|
|
|
|
dh->set_derived(this);
|
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TABLE_LIST *TABLE_LIST::get_first_table()
|
|
|
|
{
|
|
|
|
for (SELECT_LEX *sl= derived->first_select(); sl; sl= sl->next_select())
|
|
|
|
{
|
|
|
|
if (!(sl->join))
|
|
|
|
continue;
|
|
|
|
for (TABLE_LIST *tbl= sl->join->tables_list; tbl; tbl= tbl->next_local)
|
|
|
|
{
|
|
|
|
if (!tbl->table)
|
|
|
|
continue;
|
|
|
|
return tbl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|