sql_union.cc 10.6 KB
Newer Older
1
/* Copyright (C) 2000-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 20 21 22 23 24 25 26

   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 */


/*
  UNION  of select's
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
*/


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

27
int mysql_union(THD *thd, LEX *lex, select_result *result,
unknown's avatar
unknown committed
28
		SELECT_LEX_UNIT *unit, bool tables_and_fields_initied)
unknown's avatar
unknown committed
29 30 31
{
  DBUG_ENTER("mysql_union");
  int res= 0;
unknown's avatar
unknown committed
32
  if (!(res= unit->prepare(thd, result, tables_and_fields_initied)))
unknown's avatar
unknown committed
33 34 35 36 37 38 39 40 41 42 43
    res= unit->exec();
  res|= unit->cleanup();
  DBUG_RETURN(res);
}


/***************************************************************************
** store records in temporary table for UNION
***************************************************************************/

select_union::select_union(TABLE *table_par)
unknown's avatar
unknown committed
44
  :table(table_par), not_describe(0)
unknown's avatar
unknown committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
  bzero((char*) &info,sizeof(info));
  /*
    We can always use DUP_IGNORE because the temporary table will only
    contain a unique key if we are using not using UNION ALL
  */
  info.handle_duplicates= DUP_IGNORE;
}

select_union::~select_union()
{
}


int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
{
  unit= u;
unknown's avatar
unknown committed
62
  if (not_describe && list.elements != table->fields)
unknown's avatar
unknown committed
63 64 65 66 67 68 69 70
  {
    my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
	       ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
    return -1;
  }
  return 0;
}

71

unknown's avatar
unknown committed
72 73 74 75 76 77 78 79
bool select_union::send_data(List<Item> &values)
{
  if (unit->offset_limit_cnt)
  {						// using limit offset,count
    unit->offset_limit_cnt--;
    return 0;
  }
  fill_record(table->field,values);
unknown's avatar
unknown committed
80
  if (thd->net.report_error || write_record(table,&info))
unknown's avatar
unknown committed
81
  {
82 83 84
    if (thd->net.last_errno == ER_RECORD_FILE_FULL)
    {
      thd->clear_error(); // do not report user about table overflow
85
      if (create_myisam_from_heap(thd, table, &tmp_table_param,
unknown's avatar
unknown committed
86
				  info.last_errno, 0))
87 88 89
	return 1;
    }
    else
unknown's avatar
unknown committed
90 91 92 93 94
      return 1;
  }
  return 0;
}

95

unknown's avatar
unknown committed
96 97 98 99
bool select_union::send_eof()
{
  return 0;
}
unknown's avatar
unknown committed
100

101

unknown's avatar
unknown committed
102
bool select_union::flush()
unknown's avatar
unknown committed
103
{
unknown's avatar
unknown committed
104 105 106 107
  int error;
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
  {
    table->file->print_error(error,MYF(0));
unknown's avatar
unknown committed
108
    ::send_error(thd);
unknown's avatar
unknown committed
109 110 111 112 113
    return 1;
  }
  return 0;
}

114 115

int st_select_lex_unit::prepare(THD *thd, select_result *sel_result,
unknown's avatar
unknown committed
116
				bool tables_and_fields_initied)
unknown's avatar
unknown committed
117
{
118 119
  SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
  SELECT_LEX *select_cursor;
unknown's avatar
unknown committed
120 121 122 123 124
  DBUG_ENTER("st_select_lex_unit::prepare");

  if (prepared)
    DBUG_RETURN(0);
  prepared= 1;
unknown's avatar
unknown committed
125
  res= 0;
unknown's avatar
unknown committed
126
  found_rows_for_union= 0;
unknown's avatar
unknown committed
127
  TMP_TABLE_PARAM tmp_table_param;
128
  result= sel_result;
unknown's avatar
unknown committed
129
  t_and_f= tables_and_fields_initied;
unknown's avatar
unknown committed
130 131
  
  bzero((char *)&tmp_table_param,sizeof(TMP_TABLE_PARAM));
132
  thd->lex.current_select= select_cursor= first_select_in_union();
133
  /* Global option */
unknown's avatar
unknown committed
134
  if (((void*)(global_parameters)) == ((void*)this))
135
  {
unknown's avatar
unknown committed
136
    found_rows_for_union= first_select()->options & OPTION_FOUND_ROWS && 
unknown's avatar
unknown committed
137
      global_parameters->select_limit;
138
    if (found_rows_for_union)
unknown's avatar
unknown committed
139
      first_select()->options ^=  OPTION_FOUND_ROWS;
unknown's avatar
unknown committed
140
  }
unknown's avatar
unknown committed
141
  if (t_and_f)
unknown's avatar
unknown committed
142
  {
unknown's avatar
unknown committed
143
    // Item list and tables will be initialized by mysql_derived
144
    item_list= select_cursor->item_list;
145 146 147 148
  }
  else
  {
    item_list.empty();
149
    TABLE_LIST *first_table= (TABLE_LIST*) select_cursor->table_list.first;
unknown's avatar
unknown committed
150

151
    if (setup_tables(first_table) ||
152 153
	setup_wild(thd, first_table, select_cursor->item_list, 0,
		   select_cursor->with_wild))
154
      goto err;
155
    List_iterator<Item> it(select_cursor->item_list);	
156 157 158
    Item *item;
    while((item=it++))
      item->maybe_null=1;
159 160 161 162 163 164
    item_list= select_cursor->item_list;
    select_cursor->with_wild= 0;
    if (setup_ref_array(thd, &select_cursor->ref_pointer_array, 
			(item_list.elements + select_cursor->with_sum_func +
			 select_cursor->order_list.elements + 
			 select_cursor->group_list.elements)) ||
165 166
	setup_fields(thd, select_cursor->ref_pointer_array, first_table,
		     item_list, 0, 0, 1))
unknown's avatar
unknown committed
167
      goto err;
unknown's avatar
unknown committed
168
    t_and_f= 1;
unknown's avatar
unknown committed
169
  }
unknown's avatar
unknown committed
170

unknown's avatar
unknown committed
171 172
  tmp_table_param.field_count=item_list.elements;
  if (!(table= create_tmp_table(thd, &tmp_table_param, item_list,
unknown's avatar
unknown committed
173
				(ORDER*) 0, !union_option,
174
				1, (select_cursor->options | thd->options |
unknown's avatar
unknown committed
175 176
				    TMP_TABLE_ALL_COLUMNS),
				HA_POS_ERROR)))
unknown's avatar
unknown committed
177
    goto err;
unknown's avatar
unknown committed
178 179 180 181
  table->file->extra(HA_EXTRA_WRITE_CACHE);
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
  bzero((char*) &result_table_list,sizeof(result_table_list));
  result_table_list.db= (char*) "";
182
  result_table_list.real_name=result_table_list.alias= (char*) "union";
unknown's avatar
unknown committed
183 184 185
  result_table_list.table=table;

  if (!(union_result=new select_union(table)))
unknown's avatar
unknown committed
186
    goto err;
unknown's avatar
unknown committed
187

unknown's avatar
unknown committed
188
  union_result->not_describe=1;
189
  union_result->tmp_table_param=tmp_table_param;
190

191 192 193 194 195
  /*
    The following piece of code is placed here solely for the purpose of 
    getting correct results with EXPLAIN when UNION is withing a sub-select
    or derived table ...
  */
196

unknown's avatar
unknown committed
197
  if (thd->lex.describe)
unknown's avatar
unknown committed
198
  {
199
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
unknown's avatar
unknown committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    {
      JOIN *join= new JOIN(thd, sl->item_list, 
			   sl->options | thd->options | SELECT_NO_UNLOCK,
			   union_result);
      thd->lex.current_select= sl;
      offset_limit_cnt= sl->offset_limit;
      select_limit_cnt= sl->select_limit+sl->offset_limit;
      if (select_limit_cnt < sl->select_limit)
	select_limit_cnt= HA_POS_ERROR;		// no limit
      if (select_limit_cnt == HA_POS_ERROR)
	sl->options&= ~OPTION_FOUND_ROWS;
      
      res= join->prepare(&sl->ref_pointer_array,
			 (TABLE_LIST*) sl->table_list.first, sl->with_wild,
			 sl->where,
			 ((sl->braces) ? sl->order_list.elements : 0) +
			 sl->group_list.elements,
			 (sl->braces) ? 
			 (ORDER *)sl->order_list.first : (ORDER *) 0,
			 (ORDER*) sl->group_list.first,
			 sl->having,
			 (ORDER*) NULL,
222
			 sl, this, t_and_f);
unknown's avatar
unknown committed
223
      t_and_f= 0;
224
      if (res || thd->is_fatal_error)
unknown's avatar
unknown committed
225 226
	goto err;
    }
unknown's avatar
unknown committed
227
  }
228

229
  item_list.empty();
230
  thd->lex.current_select= lex_select_save;
231
  {
232
    List_iterator<Item> it(select_cursor->item_list);
233 234 235 236 237 238 239 240 241 242
    Field **field;

    for (field= table->field; *field; field++)
    {
      (void) it++;
      if (item_list.push_back(new Item_field(*field)))
	DBUG_RETURN(-1);
    }
  }

243
  DBUG_RETURN(res || thd->is_fatal_error ? 1 : 0);
unknown's avatar
unknown committed
244
err:
245
  thd->lex.current_select= lex_select_save;
unknown's avatar
unknown committed
246
  DBUG_RETURN(-1);
unknown's avatar
unknown committed
247 248
}

249

unknown's avatar
unknown committed
250 251
int st_select_lex_unit::exec()
{
252
  int do_print_slow= 0;
253
  SELECT_LEX_NODE *lex_select_save= thd->lex.current_select;
unknown's avatar
unknown committed
254
  SELECT_LEX *select_cursor=first_select_in_union();
255 256
  DBUG_ENTER("st_select_lex_unit::exec");

unknown's avatar
unknown committed
257
  if (executed && !(dependent || uncacheable))
unknown's avatar
unknown committed
258 259 260
    DBUG_RETURN(0);
  executed= 1;
  
261
  if ((dependent || uncacheable) || !item || !item->assigned())
unknown's avatar
unknown committed
262 263
  {
    if (optimized && item && item->assigned())
unknown's avatar
unknown committed
264
    {
unknown's avatar
unknown committed
265
      item->assigned(0); // We will reinit & rexecute unit
266
      item->reset();
unknown's avatar
unknown committed
267 268
      table->file->delete_all_rows();
    }
269
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
unknown's avatar
unknown committed
270
    {
unknown's avatar
unknown committed
271
      if (optimized)
unknown's avatar
unknown committed
272
	res= sl->join->reinit();
unknown's avatar
unknown committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      else
      {
	JOIN *join= new JOIN(thd, sl->item_list, 
			     sl->options | thd->options | SELECT_NO_UNLOCK,
			     union_result);
	thd->lex.current_select= sl;
	offset_limit_cnt= sl->offset_limit;
	select_limit_cnt= sl->select_limit+sl->offset_limit;
	if (select_limit_cnt < sl->select_limit)
	  select_limit_cnt= HA_POS_ERROR;		// no limit
	if (select_limit_cnt == HA_POS_ERROR)
	  sl->options&= ~OPTION_FOUND_ROWS;
	
	res= join->prepare(&sl->ref_pointer_array,
			   (TABLE_LIST*) sl->table_list.first, sl->with_wild,
			   sl->where,
			   ((sl->braces) ? sl->order_list.elements : 0) +
			   sl->group_list.elements,
			   (sl->braces) ? 
			   (ORDER *)sl->order_list.first : (ORDER *) 0,
			   (ORDER*) sl->group_list.first,
			   sl->having,
			   (ORDER*) NULL,
296
			   sl, this, t_and_f);
unknown's avatar
unknown committed
297 298 299 300 301 302 303 304
	t_and_f=0;
	if (res | thd->is_fatal_error)
	{
	  thd->lex.current_select= lex_select_save;
	  DBUG_RETURN(res);
	}
	res= sl->join->optimize();
      }
unknown's avatar
unknown committed
305 306 307 308
      if (!res)
      {
	sl->join->exec();
	res= sl->join->error;
unknown's avatar
unknown committed
309 310 311 312 313
	if (!res && union_result->flush())
	{
	  thd->lex.current_select= lex_select_save;
	  DBUG_RETURN(1);
	}
unknown's avatar
unknown committed
314
      }
unknown's avatar
unknown committed
315
      if (res)
unknown's avatar
unknown committed
316
      {
317
	thd->lex.current_select= lex_select_save;
unknown's avatar
unknown committed
318
	DBUG_RETURN(res);
unknown's avatar
unknown committed
319
      }
320
      do_print_slow|= select_cursor->options;
unknown's avatar
unknown committed
321
    }
unknown's avatar
unknown committed
322
  }
unknown's avatar
unknown committed
323
  optimized= 1;
324 325

  /* Send result to 'result' */
326 327

  // to correct ORDER BY reference resolving
328 329
  thd->lex.current_select= select_cursor;
  res= -1;
unknown's avatar
unknown committed
330
  {
unknown's avatar
unknown committed
331 332
    List<Item_func_match> empty_list;
    empty_list.empty();
unknown's avatar
unknown committed
333

334
    if (!thd->is_fatal_error)			// Check if EOM
335
    {
unknown's avatar
unknown committed
336
      SELECT_LEX *fake_select  = new SELECT_LEX(&thd->lex);
337 338 339 340 341
      offset_limit_cnt= (select_cursor->braces ?
			 global_parameters->offset_limit : 0);
      select_limit_cnt= (select_cursor->braces ?
			 global_parameters->select_limit+
			 global_parameters->offset_limit : HA_POS_ERROR);
unknown's avatar
unknown committed
342 343 344
      if (select_limit_cnt < global_parameters->select_limit)
	select_limit_cnt= HA_POS_ERROR;		// no limit
      if (select_limit_cnt == HA_POS_ERROR)
345
	thd->options&= ~OPTION_FOUND_ROWS;
unknown's avatar
unknown committed
346 347
      fake_select->ftfunc_list= &empty_list;

348 349 350
      res= mysql_select(thd, &ref_pointer_array, &result_table_list,
			0, item_list, NULL,
			global_parameters->order_list.elements,
unknown's avatar
unknown committed
351
			(ORDER*)global_parameters->order_list.first,
352
			(ORDER*) NULL, NULL, (ORDER*) NULL,
353
			thd->options, result, this, fake_select, 0);
354
      if (found_rows_for_union && !res)
355
	thd->limit_found_rows = (ulonglong)table->file->records;
356 357
      fake_select->exclude();
      delete fake_select;
358 359 360 361 362 363 364 365 366 367
      /*
	Mark for slow query log if any of the union parts didn't use
	indexes efficiently
      */
      select_cursor->options= ((select_cursor->options &
				~(QUERY_NO_INDEX_USED |
				  QUERY_NO_GOOD_INDEX_USED)) |
			       do_print_slow &
			       (QUERY_NO_INDEX_USED |
				QUERY_NO_GOOD_INDEX_USED));
368
    }
unknown's avatar
unknown committed
369
  }
370
  thd->lex.current_select= lex_select_save;
unknown's avatar
unknown committed
371 372 373
  DBUG_RETURN(res);
}

374

unknown's avatar
unknown committed
375
int st_select_lex_unit::cleanup()
unknown's avatar
unknown committed
376
{
377
  int error= 0;
378
  DBUG_ENTER("st_select_lex_unit::cleanup");
379

unknown's avatar
unknown committed
380 381 382
  if (union_result)
  {
    delete union_result;
383 384
    if (table)
      free_tmp_table(thd, table);
unknown's avatar
unknown committed
385 386
    table= 0; // Safety
  }
387
  for (SELECT_LEX *sl= first_select_in_union(); sl; sl= sl->next_select())
unknown's avatar
unknown committed
388
  {
389 390 391 392 393 394
    JOIN *join;
    if ((join= sl->join))
    {
      error|= sl->join->cleanup(thd);
      delete join;
    }
unknown's avatar
unknown committed
395
  }
396
  DBUG_RETURN(error);
unknown's avatar
unknown committed
397
}