sql_derived.cc 6.68 KB
Newer Older
1
/* Copyright (C) 2002-2003 MySQL AB
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

   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
20
  These were introduced by Sinisa <sinisa@mysql.com>
21 22 23 24 25 26 27
*/


#include "mysql_priv.h"
#include "sql_select.h"
#include "sql_acl.h"

28 29 30 31 32 33 34 35 36 37 38
/*
  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
39 40 41 42 43 44
    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.
45

46 47
    This function is called before any command containing derived table
    is executed.
48

49 50
    Derived tables is stored in thd->derived_tables and freed in
    close_thread_tables()
51

52 53 54 55 56 57 58
  TODO
    Move creation of derived tables in open_and_lock_tables()

  RETURN
    0	ok
    1	Error
    -1	Error and error message given
59 60
*/  

61

62 63
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit,
		  TABLE_LIST *org_table_list)
64
{
65
  SELECT_LEX *first_select= unit->first_select();
66
  TABLE *table;
67
  int res;
68
  select_union *derived_result;
69 70 71 72
  TABLE_LIST *tables= (TABLE_LIST *)first_select->table_list.first;
  bool is_union= first_select->next_select() && 
    first_select->next_select()->linkage == UNION_TYPE;
  bool is_subsel= first_select->first_inner_unit() ? 1: 0;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
73
  SELECT_LEX *save_current_select= lex->current_select;
74
  DBUG_ENTER("mysql_derived");
75
  
76 77 78 79 80 81 82
  /*
    In create_total_list, derived tables have to be treated in case of
    EXPLAIN, This is because unit/node is not deleted in that
    case. Current code in this function has to be improved to
    recognize better when this function is called from derived tables
    and when from other functions.
  */
83
  if ((is_union || is_subsel) && unit->create_total_list(thd, lex, &tables, 1))
84 85
    DBUG_RETURN(-1);

86 87 88 89
  /*
    We have to do access checks here as this code is executed before any
    sql command is started to execute.
  */
hf@deer.(none)'s avatar
hf@deer.(none) committed
90
#ifndef NO_EMBEDDED_ACCESS_CHECKS
91
  if (tables)
hf@deer.(none)'s avatar
hf@deer.(none) committed
92
    res= check_table_access(thd,SELECT_ACL, tables,0);
93
  else
hf@deer.(none)'s avatar
hf@deer.(none) committed
94
    res= check_access(thd, SELECT_ACL, any_db,0,0,0);
95
  if (res)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
96
    DBUG_RETURN(1);
hf@deer.(none)'s avatar
hf@deer.(none) committed
97 98
#endif

99
  if (!(res=open_and_lock_tables(thd,tables)))
100
  {
101 102 103 104 105 106 107 108 109 110 111 112
    if (is_union || is_subsel)
    {
      /* 
	 The following code is a re-do of fix_tables_pointers() found
	 in sql_select.cc for UNION's within derived tables. The only
	 difference is in navigation, as in derived tables we care for
	 this level only.

      */
      fix_tables_pointers(unit);
    }

113
    if (!(derived_result= new select_union(0)))
114 115
      DBUG_RETURN(1); // out of memory

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
116
    // st_select_lex_unit::prepare correctly work for single select
117
    if ((res= unit->prepare(thd, derived_result)))
118
      goto exit;
119 120 121 122 123 124 125 126 127 128

    /* 
       This is done in order to redo all field optimisations when any of the 
       involved tables is used in the outer query 
    */
    if (tables)
    {
      for (TABLE_LIST *cursor= tables;  cursor;  cursor= cursor->next)
	cursor->table->clear_query_id= 1;
    }
129
	
130
    derived_result->tmp_table_param.init();
131
    derived_result->tmp_table_param.field_count= unit->types.elements;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
132 133 134 135
    /*
      Temp table is created so that it hounours if UNION without ALL is to be 
      processed
    */
136 137
    if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param,
				  unit->types, (ORDER*) 0, 
138
				  is_union && !unit->union_option, 1,
139
				  (first_select->options | thd->options |
140
				   TMP_TABLE_ALL_COLUMNS),
141 142
				  HA_POS_ERROR,
				  org_table_list->alias)))
143
    {
144
      res= -1;
145 146
      goto exit;
    }
147 148 149 150 151
    derived_result->set_table(table);

    if (is_union)
      res= mysql_union(thd, lex, derived_result, unit);
    else
152 153 154 155 156 157 158 159 160 161
    {
      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;
162 163 164 165 166 167 168 169 170 171 172 173
      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);
174
    }
175 176

    if (!res)
177
    {
178 179 180 181 182 183
      /*
	Here we entirely fix both TABLE_LIST and list of SELECT's as
	there were no derived tables
      */
      if (derived_result->flush())
	res= 1;
184
      else
185
      {
186 187 188 189
	org_table_list->real_name=table->real_name;
	org_table_list->table=table;
	table->derived_select_number= first_select->select_number;
	table->tmp_table= TMP_TABLE;
hf@deer.(none)'s avatar
hf@deer.(none) committed
190
#ifndef NO_EMBEDDED_ACCESS_CHECKS
191
	org_table_list->grant.privilege= SELECT_ACL;
hf@deer.(none)'s avatar
hf@deer.(none) committed
192
#endif
193 194 195 196
	if (lex->describe)
	{
	  // to fix a problem in EXPLAIN
	  if (tables)
197
	  {
198 199 200
	    for (TABLE_LIST *cursor= tables;  cursor;  cursor= cursor->next)
	      if (cursor->table_list)
		cursor->table_list->table=cursor->table;
201
	  }
202
	}
203 204 205 206 207
	else
	  unit->exclude_tree();
	org_table_list->db= (char *)"";
	  // Force read of table stats in the optimizer
	table->file->info(HA_STATUS_VARIABLE);
208 209
      }
    }
210

211
    if (res)
212 213 214
      free_tmp_table(thd, table);
    else
    {
215
      /* Add new temporary table to list of open derived tables */
216 217 218
      table->next= thd->derived_tables;
      thd->derived_tables= table;
    }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
219

220
exit:
221
    delete derived_result;
222
    lex->current_select= save_current_select;
223
    close_thread_tables(thd, 0, 1);
224 225 226
  }
  DBUG_RETURN(res);
}