2011-06-30 17:46:53 +02:00
|
|
|
/* Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-30 17:46:53 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2002-03-26 14:06:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Derived tables
|
2003-01-07 10:45:06 +01:00
|
|
|
These were introduced by Sinisa <sinisa@mysql.com>
|
2002-03-26 14:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-04-07 13:58:40 +02:00
|
|
|
#include "my_global.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"
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "sql_view.h" // check_duplicate_names
|
|
|
|
#include "sql_acl.h" // SELECT_ACL
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
/*
|
2005-09-22 00:11:21 +02:00
|
|
|
Call given derived table processor (preparing or filling tables)
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_handle_derived()
|
|
|
|
lex LEX for this thread
|
2004-11-05 16:29:47 +01:00
|
|
|
processor procedure of derived table processing
|
2004-02-01 14:30:32 +01:00
|
|
|
|
|
|
|
RETURN
|
2005-10-27 23:18:23 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2004-02-01 14:30:32 +01:00
|
|
|
*/
|
2004-02-09 13:44:03 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool
|
|
|
|
mysql_handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*))
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-02-01 14:30:32 +01:00
|
|
|
if (lex->derived_tables)
|
|
|
|
{
|
2005-03-28 14:13:31 +02:00
|
|
|
lex->thd->derived_tables_processing= TRUE;
|
2004-02-01 14:30:32 +01:00
|
|
|
for (SELECT_LEX *sl= lex->all_selects_list;
|
|
|
|
sl;
|
|
|
|
sl= sl->next_select_in_list())
|
|
|
|
{
|
|
|
|
for (TABLE_LIST *cursor= sl->get_table_list();
|
|
|
|
cursor;
|
2004-07-16 00:15:55 +02:00
|
|
|
cursor= cursor->next_local)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
if ((res= (*processor)(lex->thd, lex, cursor)))
|
2005-03-28 14:13:31 +02:00
|
|
|
goto out;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
2004-02-01 19:07:44 +01:00
|
|
|
if (lex->describe)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Force join->join_tmp creation, because we will use this JOIN
|
|
|
|
twice for EXPLAIN and we have to have unchanged join for EXPLAINing
|
|
|
|
*/
|
|
|
|
sl->uncacheable|= UNCACHEABLE_EXPLAIN;
|
|
|
|
sl->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN;
|
|
|
|
}
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
}
|
2005-03-28 14:13:31 +02:00
|
|
|
out:
|
|
|
|
lex->thd->derived_tables_processing= FALSE;
|
|
|
|
return res;
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-03 16:45:40 +02:00
|
|
|
/**
|
|
|
|
@brief Create temporary table structure (but do not fill it).
|
|
|
|
|
|
|
|
@param thd Thread handle
|
|
|
|
@param lex LEX for this thread
|
|
|
|
@param orig_table_list TABLE_LIST for the upper SELECT
|
|
|
|
|
|
|
|
@details
|
|
|
|
|
|
|
|
This function is called before any command containing derived tables is
|
|
|
|
executed. Currently the function is used for derived tables, i.e.
|
|
|
|
|
|
|
|
- Anonymous derived tables, or
|
|
|
|
- Named derived tables (aka views) with the @c TEMPTABLE algorithm.
|
|
|
|
|
|
|
|
The table reference, contained in @c orig_table_list, is updated with the
|
|
|
|
fields of a new temporary table.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
This function implements a signature called "derived table processor", and
|
|
|
|
is passed as a function pointer to mysql_handle_derived().
|
|
|
|
|
|
|
|
@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.
|
|
|
|
|
|
|
|
@see mysql_handle_derived(), mysql_derived_filling(), GRANT_INFO
|
|
|
|
|
|
|
|
@return
|
|
|
|
false OK
|
|
|
|
true Error
|
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2002-03-26 14:06:05 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
2005-09-22 00:11:21 +02:00
|
|
|
ulonglong create_options;
|
2004-12-06 16:15:54 +01:00
|
|
|
DBUG_ENTER("mysql_derived_prepare");
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-11-05 16:29:47 +01:00
|
|
|
if (unit)
|
2002-03-26 14:06:05 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
TABLE *table= 0;
|
|
|
|
select_union *derived_result;
|
|
|
|
|
2005-07-01 06:05:42 +02:00
|
|
|
/* prevent name resolving out of derived table */
|
|
|
|
for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
|
|
|
|
sl->context.outer_context= 0;
|
|
|
|
|
2005-09-22 00:11:21 +02:00
|
|
|
if (!(derived_result= new select_union))
|
2005-10-27 23:18:23 +02:00
|
|
|
DBUG_RETURN(TRUE); // out of memory
|
2004-11-05 16:29:47 +01:00
|
|
|
|
Fixed following problems:
--Bug#52157 various crashes and assertions with multi-table update, stored function
--Bug#54475 improper error handling causes cascading crashing failures in innodb/ndb
--Bug#57703 create view cause Assertion failed: 0, file .\item_subselect.cc, line 846
--Bug#57352 valgrind warnings when creating view
--Recently discovered problem when a nested materialized derived table is used
before being populated and it leads to incorrect result
We have several modes when we should disable subquery evaluation.
The reasons for disabling are different. It could be
uselessness of the evaluation as in case of 'CREATE VIEW'
or 'PREPARE stmt', or we should disable subquery evaluation
if tables are not locked yet as it happens in bug#54475, or
too early evaluation of subqueries can lead to wrong result
as it happened in Bug#19077.
Main problem is that if subquery items are treated as const
they are evaluated in ::fix_fields(), ::fix_length_and_dec()
of the parental items as a lot of these methods have
Item::val_...() calls inside.
We have to make subqueries non-const to prevent unnecessary
subquery evaluation. At the moment we have different methods
for this. Here is a list of these modes:
1. PREPARE stmt;
We use UNCACHEABLE_PREPARE flag.
It is set during parsing in sql_parse.cc, mysql_new_select() for
each SELECT_LEX object and cleared at the end of PREPARE in
sql_prepare.cc, init_stmt_after_parse(). If this flag is set
subquery becomes non-const and evaluation does not happen.
2. CREATE|ALTER VIEW, SHOW CREATE VIEW, I_S tables which
process FRM files
We use LEX::view_prepare_mode field. We set it before
view preparation and check this flag in
::fix_fields(), ::fix_length_and_dec().
Some bugs are fixed using this approach,
some are not(Bug#57352, Bug#57703). The problem here is
that we have a lot of ::fix_fields(), ::fix_length_and_dec()
where we use Item::val_...() calls for const items.
3. Derived tables with subquery = wrong result(Bug19077)
The reason of this bug is too early subquery evaluation.
It was fixed by adding Item::with_subselect field
The check of this field in appropriate places prevents
const item evaluation if the item have subquery.
The fix for Bug19077 fixes only the problem with
convert_constant_item() function and does not cover
other places(::fix_fields(), ::fix_length_and_dec() again)
where subqueries could be evaluated.
Example:
CREATE TABLE t1 (i INT, j BIGINT);
INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2);
SELECT * FROM (SELECT MIN(i) FROM t1
WHERE j = SUBSTRING('12', (SELECT * FROM (SELECT MIN(j) FROM t1) t2))) t3;
DROP TABLE t1;
4. Derived tables with subquery where subquery
is evaluated before table locking(Bug#54475, Bug#52157)
Suggested solution is following:
-Introduce new field LEX::context_analysis_only with the following
possible flags:
#define CONTEXT_ANALYSIS_ONLY_PREPARE 1
#define CONTEXT_ANALYSIS_ONLY_VIEW 2
#define CONTEXT_ANALYSIS_ONLY_DERIVED 4
-Set/clean these flags when we perform
context analysis operation
-Item_subselect::const_item() returns
result depending on LEX::context_analysis_only.
If context_analysis_only is set then we return
FALSE that means that subquery is non-const.
As all subquery types are wrapped by Item_subselect
it allow as to make subquery non-const when
it's necessary.
2010-12-14 10:33:03 +01:00
|
|
|
lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_DERIVED;
|
2004-11-05 16:29:47 +01:00
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= unit->prepare(thd, derived_result, 0)))
|
2004-11-05 16:29:47 +01:00
|
|
|
goto exit;
|
Fixed following problems:
--Bug#52157 various crashes and assertions with multi-table update, stored function
--Bug#54475 improper error handling causes cascading crashing failures in innodb/ndb
--Bug#57703 create view cause Assertion failed: 0, file .\item_subselect.cc, line 846
--Bug#57352 valgrind warnings when creating view
--Recently discovered problem when a nested materialized derived table is used
before being populated and it leads to incorrect result
We have several modes when we should disable subquery evaluation.
The reasons for disabling are different. It could be
uselessness of the evaluation as in case of 'CREATE VIEW'
or 'PREPARE stmt', or we should disable subquery evaluation
if tables are not locked yet as it happens in bug#54475, or
too early evaluation of subqueries can lead to wrong result
as it happened in Bug#19077.
Main problem is that if subquery items are treated as const
they are evaluated in ::fix_fields(), ::fix_length_and_dec()
of the parental items as a lot of these methods have
Item::val_...() calls inside.
We have to make subqueries non-const to prevent unnecessary
subquery evaluation. At the moment we have different methods
for this. Here is a list of these modes:
1. PREPARE stmt;
We use UNCACHEABLE_PREPARE flag.
It is set during parsing in sql_parse.cc, mysql_new_select() for
each SELECT_LEX object and cleared at the end of PREPARE in
sql_prepare.cc, init_stmt_after_parse(). If this flag is set
subquery becomes non-const and evaluation does not happen.
2. CREATE|ALTER VIEW, SHOW CREATE VIEW, I_S tables which
process FRM files
We use LEX::view_prepare_mode field. We set it before
view preparation and check this flag in
::fix_fields(), ::fix_length_and_dec().
Some bugs are fixed using this approach,
some are not(Bug#57352, Bug#57703). The problem here is
that we have a lot of ::fix_fields(), ::fix_length_and_dec()
where we use Item::val_...() calls for const items.
3. Derived tables with subquery = wrong result(Bug19077)
The reason of this bug is too early subquery evaluation.
It was fixed by adding Item::with_subselect field
The check of this field in appropriate places prevents
const item evaluation if the item have subquery.
The fix for Bug19077 fixes only the problem with
convert_constant_item() function and does not cover
other places(::fix_fields(), ::fix_length_and_dec() again)
where subqueries could be evaluated.
Example:
CREATE TABLE t1 (i INT, j BIGINT);
INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2);
SELECT * FROM (SELECT MIN(i) FROM t1
WHERE j = SUBSTRING('12', (SELECT * FROM (SELECT MIN(j) FROM t1) t2))) t3;
DROP TABLE t1;
4. Derived tables with subquery where subquery
is evaluated before table locking(Bug#54475, Bug#52157)
Suggested solution is following:
-Introduce new field LEX::context_analysis_only with the following
possible flags:
#define CONTEXT_ANALYSIS_ONLY_PREPARE 1
#define CONTEXT_ANALYSIS_ONLY_VIEW 2
#define CONTEXT_ANALYSIS_ONLY_DERIVED 4
-Set/clean these flags when we perform
context analysis operation
-Item_subselect::const_item() returns
result depending on LEX::context_analysis_only.
If context_analysis_only is set then we return
FALSE that means that subquery is non-const.
As all subquery types are wrapped by Item_subselect
it allow as to make subquery non-const when
it's necessary.
2010-12-14 10:33:03 +01:00
|
|
|
lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_DERIVED;
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= check_duplicate_names(unit->types, 0)))
|
2005-08-10 15:45:00 +02:00
|
|
|
goto exit;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2009-12-22 10:35:56 +01:00
|
|
|
create_options= (first_select->options | thd->variables.option_bits |
|
2005-09-22 00:11:21 +02:00
|
|
|
TMP_TABLE_ALL_COLUMNS);
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
Temp table is created so that it hounours if UNION without ALL is to be
|
|
|
|
processed
|
2005-02-22 14:47:00 +01:00
|
|
|
|
|
|
|
As 'distinct' parameter we always pass FALSE (0), because underlying
|
|
|
|
query will control distinct condition by itself. Correct test of
|
|
|
|
distinct underlying query will be is_union &&
|
|
|
|
!unit->union_distinct->next_select() (i.e. it is union and last distinct
|
|
|
|
SELECT is last SELECT of UNION).
|
2004-11-05 16:29:47 +01:00
|
|
|
*/
|
2005-09-22 00:11:21 +02:00
|
|
|
if ((res= derived_result->create_result_table(thd, &unit->types, FALSE,
|
|
|
|
create_options,
|
|
|
|
orig_table_list->alias)))
|
2004-11-05 16:29:47 +01:00
|
|
|
goto exit;
|
2005-09-22 00:11:21 +02:00
|
|
|
|
|
|
|
table= derived_result->table;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
exit:
|
2004-12-16 14:31:36 +01:00
|
|
|
/* Hide "Unknown column" or "Unknown function" error */
|
|
|
|
if (orig_table_list->view)
|
|
|
|
{
|
2007-12-12 16:21:01 +01:00
|
|
|
if (thd->is_error() &&
|
2009-09-10 11:18:29 +02:00
|
|
|
(thd->stmt_da->sql_errno() == ER_BAD_FIELD_ERROR ||
|
|
|
|
thd->stmt_da->sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION ||
|
|
|
|
thd->stmt_da->sql_errno() == ER_SP_DOES_NOT_EXIST))
|
2004-12-16 14:31:36 +01:00
|
|
|
{
|
|
|
|
thd->clear_error();
|
|
|
|
my_error(ER_VIEW_INVALID, MYF(0), orig_table_list->db,
|
2005-01-06 12:00:13 +01:00
|
|
|
orig_table_list->table_name);
|
2004-12-16 14:31:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
if it is preparation PS only or commands that need only VIEW structure
|
|
|
|
then we do not need real data and we can skip execution (and parameters
|
|
|
|
is not defined, too)
|
|
|
|
*/
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (table)
|
|
|
|
free_tmp_table(thd, table);
|
|
|
|
delete derived_result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!thd->fill_derived_tables())
|
2005-02-08 20:52:50 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
delete derived_result;
|
2005-02-08 20:52:50 +01:00
|
|
|
derived_result= NULL;
|
|
|
|
}
|
2004-11-05 16:29:47 +01:00
|
|
|
orig_table_list->derived_result= derived_result;
|
|
|
|
orig_table_list->table= table;
|
2005-11-23 21:45:02 +01:00
|
|
|
orig_table_list->table_name= table->s->table_name.str;
|
|
|
|
orig_table_list->table_name_length= table->s->table_name.length;
|
2004-11-05 16:29:47 +01:00
|
|
|
table->derived_select_number= first_select->select_number;
|
2007-03-22 15:07:32 +01:00
|
|
|
table->s->tmp_table= NON_TRANSACTIONAL_TMP_TABLE;
|
2004-11-05 16:29:47 +01:00
|
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
2005-10-27 23:18:23 +02:00
|
|
|
if (orig_table_list->referencing_view)
|
|
|
|
table->grant= orig_table_list->grant;
|
|
|
|
else
|
|
|
|
table->grant.privilege= SELECT_ACL;
|
2004-11-05 16:29:47 +01:00
|
|
|
#endif
|
|
|
|
orig_table_list->db= (char *)"";
|
2005-02-08 20:52:50 +01:00
|
|
|
orig_table_list->db_length= 0;
|
2004-11-05 16:29:47 +01:00
|
|
|
// Force read of table stats in the optimizer
|
|
|
|
table->file->info(HA_STATUS_VARIABLE);
|
|
|
|
/* Add new temporary table to list of open derived tables */
|
|
|
|
table->next= thd->derived_tables;
|
|
|
|
thd->derived_tables= table;
|
|
|
|
}
|
2004-02-01 14:30:32 +01:00
|
|
|
}
|
2005-10-27 23:18:23 +02:00
|
|
|
else if (orig_table_list->merge_underlying_list)
|
|
|
|
orig_table_list->set_underlying_merge();
|
2004-12-06 16:15:54 +01:00
|
|
|
DBUG_RETURN(res);
|
2004-11-05 16:29:47 +01:00
|
|
|
}
|
|
|
|
|
2003-01-26 20:30:35 +01:00
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
fill derived table
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
mysql_derived_filling()
|
|
|
|
thd Thread handle
|
|
|
|
lex LEX for this thread
|
|
|
|
unit node that contains all SELECT's for derived tables
|
|
|
|
orig_table_list TABLE_LIST for the upper SELECT
|
|
|
|
|
|
|
|
IMPLEMENTATION
|
|
|
|
Derived table is resolved with temporary table. It is created based on the
|
|
|
|
queries defined. After temporary table is filled, if this is not EXPLAIN,
|
|
|
|
then the entire unit / node is deleted. unit is deleted if UNION is used
|
|
|
|
for derived table and node is deleted is it is a simple SELECT.
|
2005-06-07 12:11:36 +02:00
|
|
|
If you use this function, make sure it's not called at prepare.
|
|
|
|
Due to evaluation of LIMIT clause it can not be used at prepared stage.
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
RETURN
|
2005-10-27 23:18:23 +02:00
|
|
|
FALSE OK
|
|
|
|
TRUE Error
|
2005-09-22 00:11:21 +02:00
|
|
|
*/
|
2004-11-05 16:29:47 +01:00
|
|
|
|
2005-10-27 23:18:23 +02:00
|
|
|
bool mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
2004-11-05 16:29:47 +01:00
|
|
|
{
|
|
|
|
TABLE *table= orig_table_list->table;
|
|
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
2005-10-27 23:18:23 +02:00
|
|
|
bool res= FALSE;
|
2004-11-05 16:29:47 +01:00
|
|
|
|
|
|
|
/*check that table creation pass without problem and it is derived table */
|
|
|
|
if (table && unit)
|
2004-02-01 14:30:32 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
|
|
select_union *derived_result= orig_table_list->derived_result;
|
|
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
2007-04-23 13:16:49 +02:00
|
|
|
if (unit->is_union())
|
2004-04-08 22:28:47 +02:00
|
|
|
{
|
|
|
|
// execute union without clean up
|
2004-11-05 16:29:47 +01:00
|
|
|
res= unit->exec();
|
2004-04-08 22:28:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-06-07 12:11:36 +02:00
|
|
|
unit->set_limit(first_select);
|
2004-04-08 22:28:47 +02:00
|
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
|
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
|
|
|
|
lex->current_select= first_select;
|
2004-11-05 16:29:47 +01:00
|
|
|
res= mysql_select(thd, &first_select->ref_pointer_array,
|
2010-06-10 22:45:22 +02:00
|
|
|
first_select->table_list.first,
|
2004-04-08 22:28:47 +02:00
|
|
|
first_select->with_wild,
|
|
|
|
first_select->item_list, first_select->where,
|
|
|
|
(first_select->order_list.elements+
|
|
|
|
first_select->group_list.elements),
|
2010-06-10 22:45:22 +02:00
|
|
|
first_select->order_list.first,
|
|
|
|
first_select->group_list.first,
|
2004-04-08 22:28:47 +02:00
|
|
|
first_select->having, (ORDER*) NULL,
|
2009-12-22 10:35:56 +01:00
|
|
|
(first_select->options | thd->variables.option_bits |
|
2004-04-08 22:28:47 +02:00
|
|
|
SELECT_NO_UNLOCK),
|
|
|
|
derived_result, unit, first_select);
|
|
|
|
}
|
2003-06-12 15:52:36 +02:00
|
|
|
|
2004-11-05 16:29:47 +01:00
|
|
|
if (!res)
|
2004-01-14 14:15:42 +01:00
|
|
|
{
|
2004-11-05 16:29:47 +01:00
|
|
|
/*
|
|
|
|
Here we entirely fix both TABLE_LIST and list of SELECT's as
|
|
|
|
there were no derived tables
|
|
|
|
*/
|
|
|
|
if (derived_result->flush())
|
2005-10-27 23:18:23 +02:00
|
|
|
res= TRUE;
|
2004-11-05 16:29:47 +01:00
|
|
|
}
|
|
|
|
lex->current_select= save_current_select;
|
2004-09-04 22:05:12 +02:00
|
|
|
}
|
2004-11-05 16:29:47 +01:00
|
|
|
return res;
|
2002-03-26 14:06:05 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Cleans up the SELECT_LEX_UNIT for the derived table (if any).
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool mysql_derived_cleanup(THD *thd, LEX *lex, TABLE_LIST *derived)
|
|
|
|
{
|
|
|
|
SELECT_LEX_UNIT *unit= derived->derived;
|
|
|
|
if (unit)
|
|
|
|
unit->cleanup();
|
|
|
|
return false;
|
|
|
|
}
|