sql_derived.cc 5.58 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 28 29
*/


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

static const char *any_db="*any*";	// Special symbol for check_access

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

48 49
    This function is called before any command containing derived table
    is executed.
50

51 52
    Derived tables is stored in thd->derived_tables and freed in
    close_thread_tables()
53

54 55 56 57 58 59 60
  TODO
    Move creation of derived tables in open_and_lock_tables()

  RETURN
    0	ok
    1	Error
    -1	Error and error message given
61 62
*/  

63

64
int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t)
65
{
66
  SELECT_LEX *sl= unit->first_select();
67 68
  List<Item> item_list;
  TABLE *table;
69
  int res;
70
  select_union *derived_result;
71
  TABLE_LIST *tables= (TABLE_LIST *)sl->table_list.first;
72
  TMP_TABLE_PARAM tmp_table_param;
73
  bool is_union=sl->next_select() && sl->next_select()->linkage == UNION_TYPE;
74
  SELECT_LEX_NODE *save_current_select= lex->current_select;
75
  DBUG_ENTER("mysql_derived");
76
  
77 78 79 80 81 82 83
  /*
    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.
  */
84 85 86
  if (is_union && unit->create_total_list(thd, lex, &tables))
    DBUG_RETURN(-1);

87
  if (tables)
88
    res= check_table_access(thd,SELECT_ACL, tables);
89
  else
90
    res= check_access(thd, SELECT_ACL, any_db);
91 92 93 94 95 96 97 98 99
  if (res)
    DBUG_RETURN(-1);

  Item *item;
  List_iterator<Item> it(sl->item_list);

  while ((item= it++))
    item_list.push_back(item);
    
100
  if (!(res=open_and_lock_tables(thd,tables)))
101
  {
102 103
    if (is_union)
    {
104 105 106 107 108 109 110 111 112
      /* 
	 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.

      */
      for (SELECT_LEX *sel= sl; sel; sel= sel->next_select())
	relink_tables(sel);
113 114
    }

115
    lex->current_select= sl;
116
    if (setup_fields(thd,tables,item_list,0,0,1))
117
    {
118
      res= -1;
119 120 121 122
      goto exit;
    }
    bzero((char*) &tmp_table_param,sizeof(tmp_table_param));
    tmp_table_param.field_count=item_list.elements;
123
    if (!(table=create_tmp_table(thd, &tmp_table_param, item_list,
124 125
			         (ORDER*) 0, 
				 is_union && !unit->union_option, 1,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
126 127
			         (sl->options | thd->options |
				  TMP_TABLE_ALL_COLUMNS),
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
128
                                 HA_POS_ERROR)))
129
    {
130
      res= -1;
131 132 133 134 135
      goto exit;
    }
  
    if ((derived_result=new select_union(table)))
    {
136
      derived_result->tmp_table_param=&tmp_table_param;
137 138 139 140 141
      unit->offset_limit_cnt= sl->offset_limit;
      unit->select_limit_cnt= sl->select_limit+sl->offset_limit;
      if (unit->select_limit_cnt < sl->select_limit)
	unit->select_limit_cnt= HA_POS_ERROR;
      if (unit->select_limit_cnt == HA_POS_ERROR)
142
	sl->options&= ~OPTION_FOUND_ROWS;
143

144
      if (is_union)
145
	res= mysql_union(thd,lex,derived_result,unit);
146 147
      else
	res= mysql_select(thd, tables,  sl->item_list,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
148 149 150
			sl->where, (ORDER *) sl->order_list.first,
			(ORDER*) sl->group_list.first,
			sl->having, (ORDER*) NULL,
151
			sl->options | thd->options  | SELECT_NO_UNLOCK,
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
152
			derived_result, unit, sl, 0);
153

154 155
      if (!res)
      {
156 157 158 159
	/*
	  Here we entirely fix both TABLE_LIST and list of SELECT's as
	  there were no derived tables
	*/
160
	if (derived_result->flush())
161
	  res= 1;
162 163 164 165
	else
	{
	  t->real_name=table->real_name;
	  t->table=table;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
166
	  table->derived_select_number= sl->select_number;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
167
	  table->tmp_table=TMP_TABLE;
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
168
	  if (lex->describe)
169
	  {
170
	    // to fix a problem in EXPLAIN
171
	    if (tables)
172
	      tables->table_list->table=tables->table;
173
	  }
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
174
	  else
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
175
	    unit->exclude();
176 177
	  t->db= (char *)"";
	  t->derived=(SELECT_LEX *) 1; // just in case ...
Sinisa@sinisa.nasamreza.org's avatar
Sinisa@sinisa.nasamreza.org committed
178
	  table->file->info(HA_STATUS_VARIABLE);
179 180 181 182 183
	}
      }
      delete derived_result;
    }
    if (res)
184 185 186 187 188 189
      free_tmp_table(thd, table);
    else
    {
      table->next= thd->derived_tables;
      thd->derived_tables= table;
    }
190
exit:
191
    lex->current_select= save_current_select;
192
    close_thread_tables(thd, 0, 1);
193 194 195
  }
  DBUG_RETURN(res);
}