sql_derived.cc 8.33 KB
Newer Older
1
/* Copyright (C) 2002-2003 MySQL AB
unknown's avatar
unknown committed
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>
unknown's avatar
unknown committed
21 22 23 24 25 26
*/


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

unknown's avatar
unknown committed
27

28 29

/*
unknown's avatar
unknown committed
30
  call given derived table processor (preparing or filling tables)
31 32 33 34

  SYNOPSIS
    mysql_handle_derived()
    lex                 LEX for this thread
unknown's avatar
unknown committed
35
    processor           procedure of derived table processing
36 37 38 39 40 41

  RETURN
    0	ok
    -1	Error
    1	Error and error message given
*/
unknown's avatar
unknown committed
42

43
int
unknown's avatar
unknown committed
44
mysql_handle_derived(LEX *lex, int (*processor)(THD*, LEX*, TABLE_LIST*))
45
{
46
  int res= 0;
47 48
  if (lex->derived_tables)
  {
49
    lex->thd->derived_tables_processing= TRUE;
50 51 52 53 54 55
    for (SELECT_LEX *sl= lex->all_selects_list;
	 sl;
	 sl= sl->next_select_in_list())
    {
      for (TABLE_LIST *cursor= sl->get_table_list();
	   cursor;
unknown's avatar
VIEW  
unknown committed
56
	   cursor= cursor->next_local)
57
      {
unknown's avatar
unknown committed
58
	if ((res= (*processor)(lex->thd, lex, cursor)))
59
	  goto out;
60
      }
61 62 63 64 65 66 67 68 69
      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;
      }
70 71
    }
  }
72 73 74
out:
  lex->thd->derived_tables_processing= FALSE;
  return res;
75 76 77
}


78
/*
unknown's avatar
unknown committed
79
  Create temporary table structure (but do not fill it)
80 81

  SYNOPSIS
unknown's avatar
unknown committed
82
    mysql_derived_prepare()
83 84
    thd			Thread handle
    lex                 LEX for this thread
unknown's avatar
unknown committed
85
    orig_table_list     TABLE_LIST for the upper SELECT
86 87

  IMPLEMENTATION
unknown's avatar
unknown committed
88
    Derived table is resolved with temporary table.
89 90

    After table creation, the above TABLE_LIST is updated with a new table.
91

92 93
    This function is called before any command containing derived table
    is executed.
94

95 96
    Derived tables is stored in thd->derived_tables and freed in
    close_thread_tables()
97

98 99 100 101
  RETURN
    0	ok
    1	Error
    -1	Error and error message given
unknown's avatar
unknown committed
102
  */
unknown's avatar
unknown committed
103

unknown's avatar
unknown committed
104
int mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
unknown's avatar
unknown committed
105
{
unknown's avatar
unknown committed
106 107
  SELECT_LEX_UNIT *unit= orig_table_list->derived;
  int res= 0;
unknown's avatar
unknown committed
108
  DBUG_ENTER("mysql_derived_prepare");
unknown's avatar
unknown committed
109
  if (unit)
unknown's avatar
unknown committed
110
  {
unknown's avatar
unknown committed
111 112 113 114 115 116
    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;

117 118 119 120
    /* prevent name resolving out of derived table */
    for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
      sl->context.outer_context= 0;

unknown's avatar
unknown committed
121 122 123 124
    if (!(derived_result= new select_union(0)))
      DBUG_RETURN(1); // out of memory

    // st_select_lex_unit::prepare correctly work for single select
unknown's avatar
unknown committed
125
    if ((res= unit->prepare(thd, derived_result, 0, orig_table_list->alias)))
unknown's avatar
unknown committed
126 127
      goto exit;

128 129 130 131 132
    if (check_duplicate_names(unit->types, 0))
    {
      res= -1;
      goto exit;
    }
unknown's avatar
unknown committed
133 134 135 136 137 138

    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
unknown's avatar
unknown committed
139 140 141 142 143 144

      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).
unknown's avatar
unknown committed
145 146 147
    */
    if (!(table= create_tmp_table(thd, &derived_result->tmp_table_param,
				  unit->types, (ORDER*) 0,
unknown's avatar
unknown committed
148
				  FALSE, 1,
unknown's avatar
unknown committed
149
				  (first_select->options | thd->options |
150
				   TMP_TABLE_ALL_COLUMNS),
unknown's avatar
unknown committed
151 152 153 154 155 156 157 158 159
				  HA_POS_ERROR,
				  orig_table_list->alias)))
    {
      res= -1;
      goto exit;
    }
    derived_result->set_table(table);

exit:
160 161 162 163 164 165 166 167
    /* 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,
168
                 orig_table_list->table_name);
169 170 171
      }
    }

unknown's avatar
unknown committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
    /*
      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())
186
      {
unknown's avatar
unknown committed
187
	delete derived_result;
188 189
	derived_result= NULL;
      }
unknown's avatar
unknown committed
190 191
      orig_table_list->derived_result= derived_result;
      orig_table_list->table= table;
192
      orig_table_list->table_name= (char*) table->s->table_name;
193
      orig_table_list->table_name_length= strlen((char*)table->s->table_name);
unknown's avatar
unknown committed
194
      table->derived_select_number= first_select->select_number;
195
      table->s->tmp_table= TMP_TABLE;
unknown's avatar
unknown committed
196 197 198 199
#ifndef NO_EMBEDDED_ACCESS_CHECKS
      table->grant.privilege= SELECT_ACL;
#endif
      orig_table_list->db= (char *)"";
200
      orig_table_list->db_length= 0;
unknown's avatar
unknown committed
201 202 203 204 205 206
      // 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;
    }
207
  }
unknown's avatar
unknown committed
208 209
  else if (orig_table_list->ancestor)
    orig_table_list->set_ancestor();
unknown's avatar
unknown committed
210
  DBUG_RETURN(res);
unknown's avatar
unknown committed
211 212
}

213

unknown's avatar
unknown committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/*
  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.
229 230
    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.
unknown's avatar
unknown committed
231 232 233 234 235

  RETURN
    0	ok
    1	Error
    -1	Error and error message given
236
  */
unknown's avatar
unknown committed
237 238 239 240 241 242 243 244 245

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)
246
  {
unknown's avatar
unknown committed
247 248 249 250 251
    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;
252 253 254
    if (is_union)
    {
      // execute union without clean up
unknown's avatar
unknown committed
255
      res= unit->exec();
256 257 258
    }
    else
    {
259
      unit->set_limit(first_select);
260 261 262 263
      if (unit->select_limit_cnt == HA_POS_ERROR)
	first_select->options&= ~OPTION_FOUND_ROWS;

      lex->current_select= first_select;
unknown's avatar
unknown committed
264
      res= mysql_select(thd, &first_select->ref_pointer_array,
265 266 267 268 269 270 271 272 273 274 275 276
			(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);
    }
unknown's avatar
unknown committed
277

unknown's avatar
unknown committed
278
    if (!res)
279
    {
unknown's avatar
unknown committed
280 281 282 283 284 285
      /*
        Here we entirely fix both TABLE_LIST and list of SELECT's as
        there were no derived tables
      */
      if (derived_result->flush())
        res= 1;
286

unknown's avatar
unknown committed
287 288 289
      if (!lex->describe)
        unit->cleanup();
    }
290
    else
unknown's avatar
unknown committed
291 292
      unit->cleanup();
    lex->current_select= save_current_select;
unknown's avatar
unknown committed
293
  }
unknown's avatar
unknown committed
294
  return res;
unknown's avatar
unknown committed
295
}