mirror of
https://github.com/MariaDB/server.git
synced 2025-11-21 06:21:35 +03:00
BitKeeper/etc/ignore: auto-union BitKeeper/etc/logging_ok: auto-union VC++Files/sql/mysqld.dsp: Auto merged client/Makefile.am: Auto merged client/mysql.cc: Auto merged client/mysqltest.c: Auto merged configure.in: Auto merged include/my_global.h: Auto merged include/my_pthread.h: Auto merged include/my_sys.h: Auto merged include/mysql_com.h: Auto merged libmysqld/Makefile.am: Auto merged libmysqld/lib_sql.cc: Auto merged myisam/mi_check.c: Auto merged myisam/myisamchk.c: Auto merged myisam/myisamdef.h: Auto merged mysql-test/install_test_db.sh: Auto merged mysql-test/r/insert_select.result: Auto merged mysql-test/r/join_outer.result: Auto merged mysql-test/r/null.result: Auto merged mysql-test/r/connect.result: Auto merged mysql-test/r/func_sapdb.result: Auto merged mysql-test/r/mix_innodb_myisam_binlog.result: Auto merged mysql-test/r/mysqldump.result: Auto merged mysql-test/r/rpl_change_master.result: Auto merged mysql-test/r/rpl_log.result: Auto merged mysql-test/r/show_check.result: Auto merged mysql-test/r/symlink.result: Auto merged mysql-test/r/rpl_flush_log_loop.result: Auto merged mysql-test/r/rpl_flush_tables.result: Auto merged mysql-test/r/rpl_loaddata.result: Auto merged mysql-test/r/rpl_loaddata_rule_m.result: Auto merged mysql-test/r/rpl_loaddata_rule_s.result: Auto merged mysql-test/r/rpl_max_relay_size.result: Auto merged mysql-test/r/rpl_reset_slave.result: Auto merged mysql-test/r/rpl_temporary.result: Auto merged mysql-test/r/rpl_until.result: Auto merged mysql-test/r/rpl_user_variables.result: Auto merged mysql-test/r/subselect.result: Auto merged mysql-test/r/union.result: Auto merged mysql-test/r/variables.result: Auto merged mysql-test/t/func_sapdb.test: Auto merged mysql-test/t/mix_innodb_myisam_binlog.test: Auto merged mysql-test/t/mysqlbinlog.test: Auto merged mysql-test/t/rpl_change_master.test: Auto merged mysql-test/t/rpl_log.test: Auto merged mysql-test/t/variables.test: Auto merged netware/BUILD/compile-linux-tools: Auto merged netware/BUILD/compile-netware-END: Auto merged netware/BUILD/compile-netware-all: Auto merged netware/BUILD/compile-netware-standard: Auto merged mysql-test/t/rpl_empty_master_crash.test: Auto merged mysql-test/t/rpl_error_ignored_table.test: Auto merged mysql-test/t/rpl_flush_log_loop.test: Auto merged mysql-test/t/rpl_loaddata.test: Auto merged mysql-test/t/rpl_loaddata_rule_m.test: Auto merged mysql-test/t/rpl_loaddata_rule_s.test: Auto merged mysql-test/t/rpl_max_relay_size.test: Auto merged mysql-test/t/rpl_openssl.test: Auto merged mysql-test/t/rpl_relayrotate-slave.opt: Auto merged mysql-test/t/rpl_reset_slave.test: Auto merged mysql-test/t/rpl_trunc_binlog.test: Auto merged mysql-test/t/rpl_until.test: Auto merged mysql-test/t/rpl_user_variables.test: Auto merged mysql-test/t/subselect.test: Auto merged scripts/make_binary_distribution.sh: Auto merged scripts/mysql_create_system_tables.sh: Auto merged scripts/mysql_fix_privilege_tables.sql: Auto merged scripts/mysql_install_db.sh: Auto merged sql/filesort.cc: Auto merged sql/ha_berkeley.cc: Auto merged sql/ha_berkeley.h: Auto merged sql/ha_innodb.h: Auto merged sql/ha_myisam.cc: Auto merged sql/handler.cc: Auto merged sql/handler.h: Auto merged sql/item.cc: Auto merged sql/item.h: Auto merged sql/item_cmpfunc.cc: Auto merged sql/item_cmpfunc.h: Auto merged sql/item_create.cc: Auto merged sql/item_create.h: Auto merged sql/item_subselect.cc: Auto merged sql/item_sum.cc: Auto merged sql/item_sum.h: Auto merged sql/item_timefunc.h: Auto merged sql/lock.cc: Auto merged sql/log_event.h: Auto merged sql/mysqld.cc: Auto merged sql/net_serv.cc: Auto merged sql/protocol.cc: Auto merged sql/protocol.h: Auto merged sql/repl_failsafe.cc: Auto merged sql/sql_acl.cc: Auto merged sql/sql_acl.h: Auto merged sql/sql_base.cc: Auto merged sql/sql_cache.cc: Auto merged sql/sql_class.h: Auto merged sql/sql_db.cc: Auto merged sql/sql_delete.cc: Auto merged sql/sql_derived.cc: Auto merged sql/sql_lex.h: Auto merged sql/sql_list.h: Auto merged sql/sql_load.cc: Auto merged sql/sql_rename.cc: Auto merged sql/sql_repl.cc: Auto merged sql/sql_repl.h: Auto merged sql/sql_select.cc: Auto merged sql/sql_select.h: Auto merged sql/sql_show.cc: Auto merged sql/sql_table.cc: Auto merged sql/sql_test.cc: Auto merged sql/sql_update.cc: Auto merged sql-common/client.c: Auto merged tests/client_test.c: Auto merged
221 lines
5.9 KiB
C++
221 lines
5.9 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"
|
|
#include "sql_acl.h"
|
|
|
|
static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *s,
|
|
TABLE_LIST *t);
|
|
|
|
/*
|
|
Resolve derived tables in all queries
|
|
|
|
SYNOPSIS
|
|
mysql_handle_derived()
|
|
lex LEX for this thread
|
|
|
|
RETURN
|
|
0 ok
|
|
-1 Error
|
|
1 Error and error message given
|
|
*/
|
|
|
|
int
|
|
mysql_handle_derived(LEX *lex)
|
|
{
|
|
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)
|
|
{
|
|
int res;
|
|
if (cursor->derived && (res=mysql_derived(lex->thd, lex,
|
|
cursor->derived,
|
|
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;
|
|
}
|
|
|
|
|
|
/*
|
|
Resolve derived tables in all queries
|
|
|
|
SYNOPSIS
|
|
mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
|
|
thd Thread handle
|
|
lex LEX for this thread
|
|
unit node that contains all SELECT's for derived tables
|
|
t 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 created, 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.
|
|
|
|
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
|
|
*/
|
|
|
|
|
|
static int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit,
|
|
TABLE_LIST *org_table_list)
|
|
{
|
|
SELECT_LEX *first_select= unit->first_select();
|
|
TABLE *table;
|
|
int res;
|
|
select_union *derived_result;
|
|
bool is_union= first_select->next_select() &&
|
|
first_select->next_select()->linkage == UNION_TYPE;
|
|
bool is_subsel= first_select->first_inner_unit() ? 1: 0;
|
|
SELECT_LEX *save_current_select= lex->current_select;
|
|
DBUG_ENTER("mysql_derived");
|
|
|
|
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,
|
|
org_table_list->alias)))
|
|
{
|
|
res= -1;
|
|
goto exit;
|
|
}
|
|
derived_result->set_table(table);
|
|
|
|
|
|
if (is_union)
|
|
{
|
|
// execute union without clean up
|
|
if (!(res= unit->prepare(thd, derived_result, SELECT_NO_UNLOCK)))
|
|
res= unit->exec();
|
|
}
|
|
else
|
|
{
|
|
unit->set_limit(first_select, first_select);
|
|
|
|
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;
|
|
else
|
|
{
|
|
org_table_list->real_name= table->real_name;
|
|
org_table_list->table= table;
|
|
if (org_table_list->table_list)
|
|
{
|
|
org_table_list->table_list->real_name= table->real_name;
|
|
org_table_list->table_list->table= table;
|
|
}
|
|
table->derived_select_number= first_select->select_number;
|
|
table->tmp_table= TMP_TABLE;
|
|
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
|
table->grant.privilege= SELECT_ACL;
|
|
#endif
|
|
org_table_list->db= (char *)"";
|
|
// Force read of table stats in the optimizer
|
|
table->file->info(HA_STATUS_VARIABLE);
|
|
}
|
|
|
|
if (!lex->describe)
|
|
unit->cleanup();
|
|
if (res)
|
|
free_tmp_table(thd, table);
|
|
else
|
|
{
|
|
/* Add new temporary table to list of open derived tables */
|
|
table->next= thd->derived_tables;
|
|
thd->derived_tables= table;
|
|
}
|
|
|
|
exit:
|
|
delete derived_result;
|
|
lex->current_select= save_current_select;
|
|
}
|
|
DBUG_RETURN(res);
|
|
}
|