mirror of
https://github.com/MariaDB/server.git
synced 2025-01-27 01:04:19 +01:00
bb2d3eaa30
BitKeeper/etc/ignore: auto-union BitKeeper/etc/logging_ok: auto-union Build-tools/Do-compile: Auto merged VC++Files/sql/mysqld.dsp: Auto merged client/Makefile.am: Auto merged client/mysql.cc: Auto merged BitKeeper/deleted/.del-acinclude.m4~f4ab416bac5003: Auto merged client/mysqltest.c: Auto merged include/my_base.h: Auto merged innobase/dict/dict0dict.c: Auto merged innobase/dict/dict0load.c: Auto merged innobase/include/dict0dict.h: Auto merged innobase/include/row0mysql.h: Auto merged innobase/os/os0file.c: Auto merged innobase/srv/srv0srv.c: Auto merged libmysql/libmysql.c: Auto merged myisam/mi_check.c: Auto merged myisam/mi_rnext_same.c: Auto merged myisam/mi_write.c: Auto merged myisam/sort.c: Auto merged mysql-test/mysql-test-run.sh: Auto merged mysql-test/r/ctype_ucs.result: Auto merged mysql-test/r/ctype_ujis.result: Auto merged mysql-test/r/gis-rtree.result: Auto merged mysql-test/r/group_by.result: Auto merged mysql-test/r/merge.result: Auto merged mysql-test/r/metadata.result: Auto merged mysql-test/r/ndb_alter_table.result: Auto merged mysql-test/r/ps_1general.result: Auto merged mysql-test/r/insert_update.result: Auto merged mysql-test/r/timezone2.result: Auto merged mysql-test/r/type_enum.result: Auto merged mysql-test/r/variables.result: Auto merged mysql-test/t/ctype_ucs.test: Auto merged mysql-test/t/merge.test: Auto merged mysql-test/t/ps_1general.test: Auto merged mysql-test/t/subselect.test: Auto merged mysql-test/t/system_mysql_db_fix.test: Auto merged mysql-test/t/variables.test: Auto merged ndb/include/ndbapi/NdbConnection.hpp: Auto merged ndb/include/ndbapi/NdbDictionary.hpp: Auto merged ndb/src/common/util/version.c: Auto merged ndb/src/kernel/blocks/dbacc/DbaccInit.cpp: Auto merged ndb/src/kernel/blocks/dbacc/Makefile.am: Auto merged ndb/src/kernel/blocks/dbdict/Dbdict.cpp: Auto merged ndb/src/kernel/blocks/dbdih/DbdihMain.cpp: Auto merged ndb/src/kernel/blocks/dblqh/DblqhInit.cpp: Auto merged ndb/src/kernel/blocks/dbtc/DbtcMain.cpp: Auto merged ndb/src/kernel/blocks/dbtux/Dbtux.hpp: Auto merged ndb/src/ndbapi/NdbBlob.cpp: Auto merged ndb/src/ndbapi/NdbConnection.cpp: Auto merged ndb/src/ndbapi/NdbDictionary.cpp: Auto merged ndb/src/ndbapi/NdbDictionaryImpl.cpp: Auto merged ndb/src/ndbapi/NdbDictionaryImpl.hpp: Auto merged ndb/src/ndbapi/NdbOperationExec.cpp: Auto merged ndb/src/ndbapi/NdbScanOperation.cpp: Auto merged ndb/test/ndbapi/Makefile.am: Auto merged scripts/make_win_src_distribution.sh: Auto merged scripts/mysql_install_db.sh: Auto merged sql/field.cc: Auto merged sql/ha_innodb.cc: Auto merged sql/ha_ndbcluster.cc: Auto merged sql/ha_ndbcluster.h: Auto merged sql/handler.cc: Auto merged sql/item_cmpfunc.cc: Auto merged sql/item_create.h: Auto merged sql/item_func.cc: Auto merged sql/item_geofunc.cc: Auto merged sql/item_row.cc: Auto merged sql/item_strfunc.cc: Auto merged sql/item_strfunc.h: Auto merged sql/item_sum.cc: Auto merged sql/item_sum.h: Auto merged sql/log.cc: Auto merged sql/log_event.cc: Auto merged sql/mysqld.cc: Auto merged sql/net_serv.cc: Auto merged sql/password.c: Auto merged sql/protocol.cc: Auto merged sql/repl_failsafe.cc: Auto merged sql/set_var.cc: Auto merged sql/slave.cc: Auto merged sql/sql_acl.cc: Auto merged sql/sql_cache.cc: Auto merged sql/sql_class.cc: Auto merged sql/sql_derived.cc: Auto merged sql/sql_do.cc: Auto merged sql/sql_handler.cc: Auto merged sql/sql_help.cc: Auto merged sql/sql_lex.cc: Auto merged sql/sql_lex.h: Auto merged sql/sql_repl.cc: Auto merged sql/sql_show.cc: Auto merged sql/sql_union.cc: Auto merged sql/examples/ha_archive.cc: Auto merged sql/strfunc.cc: Auto merged sql/table.cc: Auto merged sql/table.h: Auto merged sql/tztime.h: Auto merged sql/udf_example.cc: Auto merged sql/unireg.cc: Auto merged Makefile.am: Simple merge client/mysqldump.c: Simple merge configure.in: Simple merge libmysqld/lib_sql.cc: Automatic merge mysql-test/r/func_str.result: Automatic merge mysql-test/r/grant.result: simple merge mysql-test/r/multi_update.result: automatc merge mysql-test/r/ps.result: automatic merge mysql-test/r/ps_2myisam.result: Automatic merge mysql-test/r/ps_3innodb.result: Automatic merge mysql-test/r/ps_4heap.result: Automatic merge mysql-test/r/ps_5merge.result: Automatic merge mysql-test/r/ps_6bdb.result: Automatic merge mysql-test/r/ps_7ndb.result: Automatic merge mysql-test/r/show_check.result: Automatic merge mysql-test/r/subselect.result: Automatic merge mysql-test/t/grant.test: Automatic merge mysql-test/t/multi_update.test: Automatic merge mysql-test/t/ps.test: Automatic merge mysql-test/t/show_check.test: Automatic merge ndb/docs/wl2077.txt: merge ndb/src/mgmsrv/main.cpp: merge scripts/mysql_fix_privilege_tables.sh: merge sql/item.cc: Merge (difficult) sql/item.h: simple merge sql/item_cmpfunc.h: Automatic merge sql/item_subselect.cc: Simple merge sql/item_subselect.h: Automatic merge sql/mysql_priv.h: Simple merge sql/slave.h: Automatic merge sql/sql_base.cc: Removed code that was backported to 4.1 sql/sql_class.h: Merge (some code moved to sql_insert.cc) sql/sql_db.cc: simple merge sql/sql_insert.cc: Merge (difficult as logic had changed both in 4.1 and 5.0) Some coded moved here from sql_class.h sql/sql_parse.cc: Merge (difficult) sql/sql_prepare.cc: Simple merge sql/sql_select.cc: Automatic merge sql/sql_table.cc: Simple merge sql/sql_update.cc: Difficult merge because of different logic for multi-updates sql/sql_yacc.yy: Simple merge tests/client_test.c: Simple merge
274 lines
7.6 KiB
C++
274 lines
7.6 KiB
C++
/* Copyright (C) 2002-2003 MySQL AB
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
/*
|
|
Derived tables
|
|
These were introduced by Sinisa <sinisa@mysql.com>
|
|
*/
|
|
|
|
|
|
#include "mysql_priv.h"
|
|
#include "sql_select.h"
|
|
|
|
|
|
|
|
/*
|
|
call given derived table processor (preparing or filling tables)
|
|
|
|
SYNOPSIS
|
|
mysql_handle_derived()
|
|
lex LEX for this thread
|
|
processor procedure of derived table processing
|
|
|
|
RETURN
|
|
0 ok
|
|
-1 Error
|
|
1 Error and error message given
|
|
*/
|
|
|
|
int
|
|
mysql_handle_derived(LEX *lex, int (*processor)(THD*, LEX*, TABLE_LIST*))
|
|
{
|
|
if (lex->derived_tables)
|
|
{
|
|
for (SELECT_LEX *sl= lex->all_selects_list;
|
|
sl;
|
|
sl= sl->next_select_in_list())
|
|
{
|
|
for (TABLE_LIST *cursor= sl->get_table_list();
|
|
cursor;
|
|
cursor= cursor->next_local)
|
|
{
|
|
int res;
|
|
if ((res= (*processor)(lex->thd, lex, cursor)))
|
|
return res;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
Create temporary table structure (but do not fill it)
|
|
|
|
SYNOPSIS
|
|
mysql_derived_prepare()
|
|
thd Thread handle
|
|
lex LEX for this thread
|
|
orig_table_list TABLE_LIST for the upper SELECT
|
|
|
|
IMPLEMENTATION
|
|
Derived table is resolved with temporary table.
|
|
|
|
After table creation, the above TABLE_LIST is updated with a new table.
|
|
|
|
This function is called before any command containing derived table
|
|
is executed.
|
|
|
|
Derived tables is stored in thd->derived_tables and freed in
|
|
close_thread_tables()
|
|
|
|
RETURN
|
|
0 ok
|
|
1 Error
|
|
-1 Error and error message given
|
|
*/
|
|
|
|
int mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
|
{
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
|
int res= 0;
|
|
DBUG_ENTER("mysql_derived_prepare");
|
|
if (unit)
|
|
{
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
TABLE *table= 0;
|
|
select_union *derived_result;
|
|
bool is_union= first_select->next_select() &&
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
|
|
|
if (!(derived_result= new select_union(0)))
|
|
DBUG_RETURN(1); // out of memory
|
|
|
|
// st_select_lex_unit::prepare correctly work for single select
|
|
if ((res= unit->prepare(thd, derived_result, 0)))
|
|
goto exit;
|
|
|
|
|
|
derived_result->tmp_table_param.init();
|
|
derived_result->tmp_table_param.field_count= unit->types.elements;
|
|
/*
|
|
Temp table is created so that it hounours if UNION without ALL is to be
|
|
processed
|
|
*/
|
|
if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param,
|
|
unit->types, (ORDER*) 0,
|
|
is_union && unit->union_distinct, 1,
|
|
(first_select->options | thd->options |
|
|
TMP_TABLE_ALL_COLUMNS),
|
|
HA_POS_ERROR,
|
|
orig_table_list->alias)))
|
|
{
|
|
res= -1;
|
|
goto exit;
|
|
}
|
|
derived_result->set_table(table);
|
|
|
|
exit:
|
|
/* Hide "Unknown column" or "Unknown function" error */
|
|
if (orig_table_list->view)
|
|
{
|
|
if (thd->net.last_errno == ER_BAD_FIELD_ERROR ||
|
|
thd->net.last_errno == ER_SP_DOES_NOT_EXIST)
|
|
{
|
|
thd->clear_error();
|
|
my_error(ER_VIEW_INVALID, MYF(0), orig_table_list->db,
|
|
orig_table_list->real_name);
|
|
}
|
|
}
|
|
|
|
/*
|
|
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())
|
|
delete derived_result;
|
|
orig_table_list->derived_result= derived_result;
|
|
orig_table_list->table= table;
|
|
orig_table_list->real_name= table->real_name;
|
|
table->derived_select_number= first_select->select_number;
|
|
table->tmp_table= TMP_TABLE;
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
|
table->grant.privilege= SELECT_ACL;
|
|
#endif
|
|
orig_table_list->db= (char *)"";
|
|
// 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;
|
|
}
|
|
}
|
|
else if (orig_table_list->ancestor)
|
|
orig_table_list->set_ancestor();
|
|
DBUG_RETURN(res);
|
|
}
|
|
|
|
|
|
/*
|
|
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.
|
|
|
|
RETURN
|
|
0 ok
|
|
1 Error
|
|
-1 Error and error message given
|
|
*/
|
|
|
|
int mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
|
|
{
|
|
TABLE *table= orig_table_list->table;
|
|
SELECT_LEX_UNIT *unit= orig_table_list->derived;
|
|
int res= 0;
|
|
|
|
/*check that table creation pass without problem and it is derived table */
|
|
if (table && unit)
|
|
{
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
select_union *derived_result= orig_table_list->derived_result;
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
|
bool is_union= first_select->next_select() &&
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
|
if (is_union)
|
|
{
|
|
// execute union without clean up
|
|
res= unit->exec();
|
|
}
|
|
else
|
|
{
|
|
unit->offset_limit_cnt= first_select->offset_limit;
|
|
unit->select_limit_cnt= first_select->select_limit+
|
|
first_select->offset_limit;
|
|
if (unit->select_limit_cnt < first_select->select_limit)
|
|
unit->select_limit_cnt= HA_POS_ERROR;
|
|
if (unit->select_limit_cnt == HA_POS_ERROR)
|
|
first_select->options&= ~OPTION_FOUND_ROWS;
|
|
|
|
lex->current_select= first_select;
|
|
res= mysql_select(thd, &first_select->ref_pointer_array,
|
|
(TABLE_LIST*) first_select->table_list.first,
|
|
first_select->with_wild,
|
|
first_select->item_list, first_select->where,
|
|
(first_select->order_list.elements+
|
|
first_select->group_list.elements),
|
|
(ORDER *) first_select->order_list.first,
|
|
(ORDER *) first_select->group_list.first,
|
|
first_select->having, (ORDER*) NULL,
|
|
(first_select->options | thd->options |
|
|
SELECT_NO_UNLOCK),
|
|
derived_result, unit, first_select);
|
|
}
|
|
|
|
if (!res)
|
|
{
|
|
/*
|
|
Here we entirely fix both TABLE_LIST and list of SELECT's as
|
|
there were no derived tables
|
|
*/
|
|
if (derived_result->flush())
|
|
res= 1;
|
|
|
|
if (!lex->describe)
|
|
unit->cleanup();
|
|
}
|
|
else
|
|
unit->cleanup();
|
|
lex->current_select= save_current_select;
|
|
}
|
|
return res;
|
|
}
|