~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/**
17
  @file
18
19
  @brief
20
  mysql_select and join optimization
21
22
23
  @defgroup Query_Optimizer  Query Optimizer
24
  @{
25
*/
26
27
#ifdef USE_PRAGMA_IMPLEMENTATION
28
#pragma implementation				// gcc: Class implementation
29
#endif
30
31
#include "mysql_priv.h"
32
#include "sql_select.h"
33
34
#include <m_ctype.h>
35
#include <my_bit.h>
36
#include <hash.h>
37
38
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
39
			      "MAYBE_REF","ALL","range","index",
40
			      "ref_or_null","unique_subquery","index_subquery",
41
                              "index_merge"
42
};
43
44
struct st_sargable_param;
45
46
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
47
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
48
				 DYNAMIC_ARRAY *keyuse);
49
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
50
                                JOIN_TAB *join_tab,
51
                                uint tables, COND *conds,
52
                                COND_EQUAL *cond_equal,
53
                                table_map table_map, SELECT_LEX *select_lex,
54
                                st_sargable_param **sargables);
55
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
56
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
57
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
58
			       table_map used_tables);
59
static bool choose_plan(JOIN *join,table_map join_tables);
60
61
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
62
                             table_map remaining_tables, uint idx,
63
                             double record_count, double read_time);
64
static void optimize_straight_join(JOIN *join, table_map join_tables);
65
static bool greedy_search(JOIN *join, table_map remaining_tables,
66
                             uint depth, uint prune_level);
67
static bool best_extension_by_limited_search(JOIN *join,
68
                                             table_map remaining_tables,
69
                                             uint idx, double record_count,
70
                                             double read_time, uint depth,
71
                                             uint prune_level);
72
static uint determine_search_depth(JOIN* join);
73
static int join_tab_cmp(const void* ptr1, const void* ptr2);
74
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
75
/*
76
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
77
  tested and approved.
78
*/
79
static bool find_best(JOIN *join,table_map rest_tables,uint index,
80
		      double record_count,double read_time);
81
static uint cache_record_length(JOIN *join,uint index);
82
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
83
static bool get_best_combination(JOIN *join);
84
static store_key *get_store_key(THD *thd,
85
				KEYUSE *keyuse, table_map used_tables,
86
				KEY_PART_INFO *key_part, uchar *key_buff,
87
				uint maybe_null);
88
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
89
static void make_outerjoin_info(JOIN *join);
90
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
91
static bool make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after);
92
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
93
static void update_depend_map(JOIN *join);
94
static void update_depend_map(JOIN *join, ORDER *order);
95
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
96
			   bool change_list, bool *simple_order);
97
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
98
                            List<Item> &fields, bool send_row,
99
                            ulonglong select_options, const char *info,
100
                            Item *having);
101
static COND *build_equal_items(THD *thd, COND *cond,
102
                               COND_EQUAL *inherited,
103
                               List<TABLE_LIST> *join_list,
104
                               COND_EQUAL **cond_equal_ref);
105
static COND* substitute_for_best_equal_field(COND *cond,
106
                                             COND_EQUAL *cond_equal,
107
                                             void *table_join_idx);
108
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
109
                            COND *conds, bool top, bool in_sj);
110
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
111
static void restore_prev_nj_state(JOIN_TAB *last);
112
static void reset_nj_counters(List<TABLE_LIST> *join_list);
113
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
114
                                          uint first_unused);
115
116
static 
117
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
118
static void restore_prev_sj_state(const table_map remaining_tables, 
119
                                  const JOIN_TAB *tab);
120
121
static COND *optimize_cond(JOIN *join, COND *conds,
122
                           List<TABLE_LIST> *join_list,
123
			   Item::cond_result *cond_value);
124
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
125
static bool open_tmp_table(TABLE *table);
126
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
127
                                    MI_COLUMNDEF *start_recinfo,
128
                                    MI_COLUMNDEF **recinfo,
129
				    ulonglong options);
130
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
131
132
static enum_nested_loop_state
133
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
134
                     int error);
135
static enum_nested_loop_state
136
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
137
static enum_nested_loop_state
138
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
139
static enum_nested_loop_state
140
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
141
static enum_nested_loop_state
142
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
143
static enum_nested_loop_state
144
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
145
static enum_nested_loop_state
146
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
147
148
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
149
static int join_read_system(JOIN_TAB *tab);
150
static int join_read_const(JOIN_TAB *tab);
151
static int join_read_key(JOIN_TAB *tab);
152
static int join_read_always_key(JOIN_TAB *tab);
153
static int join_read_last_key(JOIN_TAB *tab);
154
static int join_no_more_records(READ_RECORD *info);
155
static int join_read_next(READ_RECORD *info);
156
static int join_read_next_different(READ_RECORD *info);
157
static int join_init_quick_read_record(JOIN_TAB *tab);
158
static int test_if_quick_select(JOIN_TAB *tab);
159
static int join_init_read_record(JOIN_TAB *tab);
160
static int join_read_first(JOIN_TAB *tab);
161
static int join_read_next_same(READ_RECORD *info);
162
static int join_read_next_same_diff(READ_RECORD *info);
163
static int join_read_last(JOIN_TAB *tab);
164
static int join_read_prev_same(READ_RECORD *info);
165
static int join_read_prev(READ_RECORD *info);
166
int join_read_always_key_or_null(JOIN_TAB *tab);
167
int join_read_next_same_or_null(READ_RECORD *info);
168
static COND *make_cond_for_table(COND *cond,table_map table,
169
				 table_map used_table,
170
                                 bool exclude_expensive_cond);
171
static Item* part_of_refkey(TABLE *form,Field *field);
172
uint find_shortest_key(TABLE *table, const key_map *usable_keys);
173
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
174
				    ha_rows select_limit, bool no_changes,
175
                                    const key_map *map);
176
static bool list_contains_unique_index(TABLE *table,
177
                          bool (*find_func) (Field *, void *), void *data);
178
static bool find_field_in_item_list (Field *field, void *data);
179
static bool find_field_in_order_list (Field *field, void *data);
180
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
181
			     ha_rows filesort_limit, ha_rows select_limit,
182
                             bool is_order_by);
183
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
184
			     Item *having);
185
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
186
				   ulong offset,Item *having);
187
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
188
				      uint field_count, Field **first_field,
189
190
				      ulong key_length,Item *having);
191
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
192
static ulong used_blob_length(CACHE_FIELD **ptr);
193
static bool store_record_in_cache(JOIN_CACHE *cache);
194
static void reset_cache_read(JOIN_CACHE *cache);
195
static void reset_cache_write(JOIN_CACHE *cache);
196
static void read_cached_record(JOIN_TAB *tab);
197
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
198
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
199
                                    ORDER *order, List<Item> &fields,
200
                                    List<Item> &all_fields,
201
				    bool *all_order_by_fields_used);
202
static bool test_if_subpart(ORDER *a,ORDER *b);
203
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
204
static void calc_group_buffer(JOIN *join,ORDER *group);
205
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
206
static bool alloc_group_fields(JOIN *join,ORDER *group);
207
// Create list for using with tempory table
208
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
209
				     List<Item> &new_list1,
210
				     List<Item> &new_list2,
211
				     uint elements, List<Item> &items);
212
// Create list for using with tempory table
213
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
214
				      List<Item> &new_list1,
215
				      List<Item> &new_list2,
216
				      uint elements, List<Item> &items);
217
static void init_tmptable_sum_functions(Item_sum **func);
218
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
219
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
220
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
221
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
222
static bool init_sum_functions(Item_sum **func, Item_sum **end);
223
static bool update_sum_func(Item_sum **func);
224
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
225
			    bool distinct, const char *message=NullS);
226
static Item *remove_additional_cond(Item* conds);
227
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
228
static bool test_if_ref(Item_field *left_item,Item *right_item);
229
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
230
                                       Item *new_cond, bool fix_fields);
231
232
/*
233
  This is used to mark equalities that were made from i-th IN-equality.
234
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
235
  The following variable occupies 64 addresses.
236
*/
237
const char *subq_sj_cond_name=
238
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
239
240
static bool bitmap_covers(const table_map x, const table_map y)
241
{
242
  return !test(y & ~x);
243
}
244
245
/**
246
  This handles SELECT with and without UNION.
247
*/
248
249
bool handle_select(THD *thd, LEX *lex, select_result *result,
250
                   ulong setup_tables_done_option)
251
{
252
  bool res;
253
  register SELECT_LEX *select_lex = &lex->select_lex;
254
  DBUG_ENTER("handle_select");
255
  MYSQL_SELECT_START();
256
257
  if (select_lex->master_unit()->is_union() || 
258
      select_lex->master_unit()->fake_select_lex)
259
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
260
  else
261
  {
262
    SELECT_LEX_UNIT *unit= &lex->unit;
263
    unit->set_limit(unit->global_parameters);
264
    thd->thd_marker= 0;
265
    /*
266
      'options' of mysql_select will be set in JOIN, as far as JOIN for
267
      every PS/SP execution new, we will not need reset this flag if 
268
      setup_tables_done_option changed for next rexecution
269
    */
270
    res= mysql_select(thd, &select_lex->ref_pointer_array,
271
		      (TABLE_LIST*) select_lex->table_list.first,
272
		      select_lex->with_wild, select_lex->item_list,
273
		      select_lex->where,
274
		      select_lex->order_list.elements +
275
		      select_lex->group_list.elements,
276
		      (ORDER*) select_lex->order_list.first,
277
		      (ORDER*) select_lex->group_list.first,
278
		      select_lex->having,
279
		      (ORDER*) lex->proc_list.first,
280
		      select_lex->options | thd->options |
281
                      setup_tables_done_option,
282
		      result, unit, select_lex);
283
  }
284
  DBUG_PRINT("info",("res: %d  report_error: %d", res,
285
		     thd->is_error()));
286
  res|= thd->is_error();
287
  if (unlikely(res))
288
    result->abort();
289
290
  MYSQL_SELECT_END();
291
  DBUG_RETURN(res);
292
}
293
294
295
/*
296
  Fix fields referenced from inner selects.
297
298
  SYNOPSIS
299
    fix_inner_refs()
300
    thd               Thread handle
301
    all_fields        List of all fields used in select
302
    select            Current select
303
    ref_pointer_array Array of references to Items used in current select
304
305
  DESCRIPTION
306
    The function serves 3 purposes - adds fields referenced from inner
307
    selects to the current select list, resolves which class to use
308
    to access referenced item (Item_ref of Item_direct_ref) and fixes
309
    references (Item_ref objects) to these fields.
310
311
    If a field isn't already in the select list and the ref_pointer_array
312
    is provided then it is added to the all_fields list and the pointer to
313
    it is saved in the ref_pointer_array.
314
315
    The class to access the outer field is determined by the following rules:
316
    1. If the outer field isn't used under an aggregate function
317
      then the Item_ref class should be used.
318
    2. If the outer field is used under an aggregate function and this
319
      function is aggregated in the select where the outer field was
320
      resolved or in some more inner select then the Item_direct_ref
321
      class should be used.
322
    The resolution is done here and not at the fix_fields() stage as
323
    it can be done only after sum functions are fixed and pulled up to
324
    selects where they are have to be aggregated.
325
    When the class is chosen it substitutes the original field in the
326
    Item_outer_ref object.
327
328
    After this we proceed with fixing references (Item_outer_ref objects) to
329
    this field from inner subqueries.
330
331
  RETURN
332
    TRUE  an error occured
333
    FALSE ok
334
*/
335
336
bool
337
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
338
                 Item **ref_pointer_array)
339
{
340
  Item_outer_ref *ref;
341
  bool res= FALSE;
342
  bool direct_ref= FALSE;
343
344
  List_iterator<Item_outer_ref> ref_it(select->inner_refs_list);
345
  while ((ref= ref_it++))
346
  {
347
    Item *item= ref->outer_ref;
348
    Item **item_ref= ref->ref;
349
    Item_ref *new_ref;
350
    /*
351
      TODO: this field item already might be present in the select list.
352
      In this case instead of adding new field item we could use an
353
      existing one. The change will lead to less operations for copying fields,
354
      smaller temporary tables and less data passed through filesort.
355
    */
356
    if (ref_pointer_array && !ref->found_in_select_list)
357
    {
358
      int el= all_fields.elements;
359
      ref_pointer_array[el]= item;
360
      /* Add the field item to the select list of the current select. */
361
      all_fields.push_front(item);
362
      /*
363
        If it's needed reset each Item_ref item that refers this field with
364
        a new reference taken from ref_pointer_array.
365
      */
366
      item_ref= ref_pointer_array + el;
367
    }
368
369
    if (ref->in_sum_func)
370
    {
371
      Item_sum *sum_func;
372
      if (ref->in_sum_func->nest_level > select->nest_level)
373
        direct_ref= TRUE;
374
      else
375
      {
376
        for (sum_func= ref->in_sum_func; sum_func &&
377
             sum_func->aggr_level >= select->nest_level;
378
             sum_func= sum_func->in_sum_func)
379
        {
380
          if (sum_func->aggr_level == select->nest_level)
381
          {
382
            direct_ref= TRUE;
383
            break;
384
          }
385
        }
386
      }
387
    }
388
    new_ref= direct_ref ?
389
              new Item_direct_ref(ref->context, item_ref, ref->table_name,
390
                          ref->field_name, ref->alias_name_used) :
391
              new Item_ref(ref->context, item_ref, ref->table_name,
392
                          ref->field_name, ref->alias_name_used);
393
    if (!new_ref)
394
      return TRUE;
395
    ref->outer_ref= new_ref;
396
    ref->ref= &ref->outer_ref;
397
398
    if (!ref->fixed && ref->fix_fields(thd, 0))
399
      return TRUE;
400
    thd->used_tables|= item->used_tables();
401
  }
402
  return res;
403
}
404
405
#define MAGIC_IN_WHERE_TOP_LEVEL 10
406
/**
407
  Function to setup clauses without sum functions.
408
*/
409
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
410
			       TABLE_LIST *tables,
411
			       TABLE_LIST *leaves,
412
			       List<Item> &fields,
413
			       List<Item> &all_fields,
414
			       COND **conds,
415
			       ORDER *order,
416
			       ORDER *group, bool *hidden_group_fields)
417
{
418
  int res;
419
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
420
  DBUG_ENTER("setup_without_group");
421
422
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
423
  res= setup_conds(thd, tables, leaves, conds);
424
425
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
426
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
427
                          order);
428
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
429
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
430
                          group, hidden_group_fields);
431
  thd->lex->allow_sum_func= save_allow_sum_func;
432
  DBUG_RETURN(res);
433
}
434
435
/*****************************************************************************
436
  Check fields, find best join, do the select and output fields.
437
  mysql_select assumes that all tables are already opened
438
*****************************************************************************/
439
440
/**
441
  Prepare of whole select (including sub queries in future).
442
443
  @todo
444
    Add check of calculation of GROUP functions and fields:
445
    SELECT COUNT(*)+table.col1 from table1;
446
447
  @retval
448
    -1   on error
449
  @retval
450
    0   on success
451
*/
452
int
453
JOIN::prepare(Item ***rref_pointer_array,
454
	      TABLE_LIST *tables_init,
455
	      uint wild_num, COND *conds_init, uint og_num,
456
	      ORDER *order_init, ORDER *group_init,
457
	      Item *having_init,
458
	      ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
459
	      SELECT_LEX_UNIT *unit_arg)
460
{
461
  DBUG_ENTER("JOIN::prepare");
462
463
  // to prevent double initialization on EXPLAIN
464
  if (optimized)
465
    DBUG_RETURN(0);
466
467
  conds= conds_init;
468
  order= order_init;
469
  group_list= group_init;
470
  having= having_init;
471
  proc_param= proc_param_init;
472
  tables_list= tables_init;
473
  select_lex= select_lex_arg;
474
  select_lex->join= this;
475
  join_list= &select_lex->top_join_list;
476
  union_part= unit_arg->is_union();
477
478
  thd->lex->current_select->is_item_list_lookup= 1;
479
  /*
480
    If we have already executed SELECT, then it have not sense to prevent
481
    its table from update (see unique_table())
482
  */
483
  if (thd->derived_tables_processing)
484
    select_lex->exclude_from_table_unique_test= TRUE;
485
486
  /* Check that all tables, fields, conds and order are ok */
487
488
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
489
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
490
                                    tables_list, &select_lex->leaf_tables,
491
                                    FALSE))
492
      DBUG_RETURN(-1);
493
 
494
  TABLE_LIST *table_ptr;
495
  for (table_ptr= select_lex->leaf_tables;
496
       table_ptr;
497
       table_ptr= table_ptr->next_leaf)
498
    tables++;
499
500
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
501
      select_lex->setup_ref_array(thd, og_num) ||
502
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
503
		   &all_fields, 1) ||
504
      setup_without_group(thd, (*rref_pointer_array), tables_list,
505
			  select_lex->leaf_tables, fields_list,
506
			  all_fields, &conds, order, group_list,
507
			  &hidden_group_fields))
508
    DBUG_RETURN(-1);				/* purecov: inspected */
509
510
  ref_pointer_array= *rref_pointer_array;
511
  
512
  if (having)
513
  {
514
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
515
    thd->where="having clause";
516
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
517
    select_lex->having_fix_field= 1;
518
    bool having_fix_rc= (!having->fixed &&
519
			 (having->fix_fields(thd, &having) ||
520
			  having->check_cols(1)));
521
    select_lex->having_fix_field= 0;
522
    if (having_fix_rc || thd->is_error())
523
      DBUG_RETURN(-1);				/* purecov: inspected */
524
    thd->lex->allow_sum_func= save_allow_sum_func;
525
  }
526
527
  if (!thd->lex->view_prepare_mode)
528
  {
529
    Item_subselect *subselect;
530
    Item_in_subselect *in_subs= NULL;
531
    /*
532
      Are we in a subquery predicate?
533
      TODO: the block below will be executed for every PS execution without need.
534
    */
535
    if ((subselect= select_lex->master_unit()->item))
536
    {
537
      bool do_semijoin= !test(thd->variables.optimizer_switch &
538
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
539
      if (subselect->substype() == Item_subselect::IN_SUBS)
540
        in_subs= (Item_in_subselect*)subselect;
541
542
      DBUG_PRINT("info", ("Checking if subq can be converted to semi-join"));
543
      /*
544
        Check if we're in subquery that is a candidate for flattening into a
545
        semi-join (which is done done in flatten_subqueries()). The
546
        requirements are:
547
          1. Subquery predicate is an IN/=ANY subq predicate
548
          2. Subquery is a single SELECT (not a UNION)
549
          3. Subquery does not have GROUP BY or ORDER BY
550
          4. Subquery does not use aggregate functions or HAVING
551
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
552
          6. No execution method was already chosen (by a prepared statement).
553
554
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
555
               doesn't have a JOIN (TODO: We should handle this at some
556
               point by switching to multi-table UPDATE/DELETE)
557
558
          (**). We're not in a confluent table-less subquery, like
559
                "SELECT 1". 
560
      */
561
      if (in_subs &&                                                    // 1
562
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
563
          !select_lex->group_list.elements && !order &&                 // 3
564
          !having && !select_lex->with_sum_func &&                      // 4
565
          thd->thd_marker &&                                            // 5
566
          select_lex->outer_select()->join &&                           // (*)
567
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
568
          do_semijoin &&
569
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
570
      {
571
        DBUG_PRINT("info", ("Subquery is semi-join conversion candidate"));
572
        {
573
          if (!in_subs->left_expr->fixed &&
574
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
575
          {
576
            DBUG_RETURN(-1);
577
          }
578
          /*
579
            Check that the right part of the subselect contains no more than one
580
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
581
          */
582
          if (subselect->substype() == Item_subselect::IN_SUBS &&
583
             (select_lex->item_list.elements != 
584
              ((Item_in_subselect*)subselect)->left_expr->cols()))
585
          {
586
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
587
            DBUG_RETURN(-1);
588
          }
589
        }
590
591
        /* Register the subquery for further processing */
592
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
593
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
594
      }
595
      else
596
      {
597
        DBUG_PRINT("info", ("Subquery can't be converted to semi-join"));
598
        bool do_materialize= !test(thd->variables.optimizer_switch &
599
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
600
        /*
601
          Check if the subquery predicate can be executed via materialization.
602
          The required conditions are:
603
          1. Subquery predicate is an IN/=ANY subq predicate
604
          2. Subquery is a single SELECT (not a UNION)
605
          3. Subquery is not a table-less query. In this case there is no
606
             point in materializing.
607
          4. Subquery predicate is a top-level predicate
608
             (this implies it is not negated)
609
             TODO: this is a limitation that should be lifeted once we
610
             implement correct NULL semantics (WL#3830)
611
          5. Subquery is non-correlated
612
             TODO:
613
             This is an overly restrictive condition. It can be extended to:
614
             (Subquery is non-correlated ||
615
              Subquery is correlated to any query outer to IN predicate ||
616
              (Subquery is correlated to the immediate outer query &&
617
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
618
               aggregate functions) && subquery predicate is not under "NOT IN"))
619
          6. No execution method was already chosen (by a prepared statement).
620
621
          (*) The subquery must be part of a SELECT statement. The current
622
               condition also excludes multi-table update statements.
623
624
          We have to determine whether we will perform subquery materialization
625
          before calling the IN=>EXISTS transformation, so that we know whether to
626
          perform the whole transformation or only that part of it which wraps
627
          Item_in_subselect in an Item_in_optimizer.
628
        */
629
        if (do_materialize && 
630
            in_subs  &&                                                   // 1
631
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
632
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
633
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
634
        {
635
          if (in_subs->is_top_level_item() &&                             // 4
636
              !in_subs->is_correlated &&                                  // 5
637
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
638
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
639
        }
640
641
        Item_subselect::trans_res trans_res;
642
        if ((trans_res= subselect->select_transformer(this)) !=
643
            Item_subselect::RES_OK)
644
        {
645
          select_lex->fix_prepare_information(thd, &conds, &having);
646
          DBUG_RETURN((trans_res == Item_subselect::RES_ERROR));
647
        }
648
      }
649
    }
650
  }
651
652
  select_lex->fix_prepare_information(thd, &conds, &having);
653
654
  if (order)
655
  {
656
    ORDER *ord;
657
    for (ord= order; ord; ord= ord->next)
658
    {
659
      Item *item= *ord->item;
660
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
661
        item->split_sum_func(thd, ref_pointer_array, all_fields);
662
    }
663
  }
664
665
  if (having && having->with_sum_func)
666
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
667
                            &having, TRUE);
668
  if (select_lex->inner_sum_func_list)
669
  {
670
    Item_sum *end=select_lex->inner_sum_func_list;
671
    Item_sum *item_sum= end;  
672
    do
673
    { 
674
      item_sum= item_sum->next;
675
      item_sum->split_sum_func2(thd, ref_pointer_array,
676
                                all_fields, item_sum->ref_by, FALSE);
677
    } while (item_sum != end);
678
  }
679
680
  if (select_lex->inner_refs_list.elements &&
681
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
682
    DBUG_RETURN(-1);
683
684
  if (group_list)
685
  {
686
    /*
687
      Because HEAP tables can't index BIT fields we need to use an
688
      additional hidden field for grouping because later it will be
689
      converted to a LONG field. Original field will remain of the
690
      BIT type and will be returned to a client.
691
    */
692
    for (ORDER *ord= group_list; ord; ord= ord->next)
693
    {
694
      if ((*ord->item)->type() == Item::FIELD_ITEM &&
695
          (*ord->item)->field_type() == MYSQL_TYPE_BIT)
696
      {
697
        Item_field *field= new Item_field(thd, *(Item_field**)ord->item);
698
        int el= all_fields.elements;
699
        ref_pointer_array[el]= field;
700
        all_fields.push_front(field);
701
        ord->item= ref_pointer_array + el;
702
      }
703
    }
704
  }
705
706
  /*
707
    Check if there are references to un-aggregated columns when computing 
708
    aggregate functions with implicit grouping (there is no GROUP BY).
709
710
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
711
  */
712
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
713
  {
714
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
715
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
716
    DBUG_RETURN(-1);
717
  }
718
  {
719
    /* Caclulate the number of groups */
720
    send_group_parts= 0;
721
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
722
      send_group_parts++;
723
  }
724
  
725
  if (error)
726
    goto err;					/* purecov: inspected */
727
728
  if (result && result->prepare(fields_list, unit_arg))
729
    goto err;					/* purecov: inspected */
730
731
  /* Init join struct */
732
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
733
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
734
  this->group= group_list != 0;
735
  unit= unit_arg;
736
737
#ifdef RESTRICTED_GROUP
738
  if (sum_func_count && !group_list && (func_count || field_count))
739
  {
740
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
741
    goto err;
742
  }
743
#endif
744
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
745
    goto err;
746
  if (alloc_func_list())
747
    goto err;
748
749
  DBUG_RETURN(0); // All OK
750
751
err:
752
  DBUG_RETURN(-1);				/* purecov: inspected */
753
}
754
755
756
/*
757
  Remove the predicates pushed down into the subquery
758
759
  SYNOPSIS
760
    JOIN::remove_subq_pushed_predicates()
761
      where   IN  Must be NULL
762
              OUT The remaining WHERE condition, or NULL
763
764
  DESCRIPTION
765
    Given that this join will be executed using (unique|index)_subquery,
766
    without "checking NULL", remove the predicates that were pushed down
767
    into the subquery.
768
769
    If the subquery compares scalar values, we can remove the condition that
770
    was wrapped into trig_cond (it will be checked when needed by the subquery
771
    engine)
772
773
    If the subquery compares row values, we need to keep the wrapped
774
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
775
    and non-NULL values, we'll do a full table scan and will rely on the
776
    equalities corresponding to non-NULL parts of left tuple to filter out
777
    non-matching records.
778
779
    TODO: We can remove the equalities that will be guaranteed to be true by the
780
    fact that subquery engine will be using index lookup. This must be done only
781
    for cases where there are no conversion errors of significance, e.g. 257
782
    that is searched in a byte. But this requires homogenization of the return 
783
    codes of all Field*::store() methods.
784
*/
785
786
void JOIN::remove_subq_pushed_predicates(Item **where)
787
{
788
  if (conds->type() == Item::FUNC_ITEM &&
789
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
790
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
791
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
792
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
793
                   ((Item_func *)conds)->arguments()[0]))
794
  {
795
    *where= 0;
796
    return;
797
  }
798
}
799
800
801
/*
802
  Index lookup-based subquery: save some flags for EXPLAIN output
803
804
  SYNOPSIS
805
    save_index_subquery_explain_info()
806
      join_tab  Subquery's join tab (there is only one as index lookup is
807
                only used for subqueries that are single-table SELECTs)
808
      where     Subquery's WHERE clause
809
810
  DESCRIPTION
811
    For index lookup-based subquery (i.e. one executed with
812
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
813
    check its EXPLAIN output row should contain 
814
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
815
      "Using Where" (TAB_INFO_USING_WHERE)
816
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
817
    and set appropriate flags in join_tab->packed_info.
818
*/
819
820
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
821
{
822
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
823
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
824
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
825
  if (where)
826
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
827
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
828
  {
829
    if (join_tab->ref.cond_guards[i])
830
    {
831
      join_tab->packed_info |= TAB_INFO_FULL_SCAN_ON_NULL;
832
      break;
833
    }
834
  }
835
}
836
837
838
839
840
/*
841
  Check if the table's rowid is included in the temptable
842
843
  SYNOPSIS
844
    sj_table_is_included()
845
      join      The join
846
      join_tab  The table to be checked
847
848
  DESCRIPTION
849
    SemiJoinDuplicateElimination: check the table's rowid should be included
850
    in the temptable. This is so if
851
852
    1. The table is not embedded within some semi-join nest
853
    2. The has been pulled out of a semi-join nest, or
854
855
    3. The table is functionally dependent on some previous table
856
857
    [4. This is also true for constant tables that can't be
858
        NULL-complemented but this function is not called for such tables]
859
860
  RETURN
861
    TRUE  - Include table's rowid
862
    FALSE - Don't
863
*/
864
865
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
866
{
867
  if (join_tab->emb_sj_nest)
868
    return FALSE;
869
  
870
  /* Check if this table is functionally dependent on the tables that
871
     are within the same outer join nest
872
  */
873
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
874
  if (join_tab->type == JT_EQ_REF)
875
  {
876
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
877
    uint idx;
878
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
879
    {
880
      JOIN_TAB *ref_tab= join->join_tab + idx;
881
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
882
        return TRUE;
883
    }
884
    /* Ok, functionally dependent */
885
    return FALSE;
886
  }
887
  /* Not functionally dependent => need to include*/
888
  return TRUE;
889
}
890
891
892
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
893
                                          SJ_TMP_TABLE *sjtbl);
894
895
896
/*
897
  Setup the strategies to eliminate semi-join duplicates.
898
  
899
  SYNOPSIS
900
    setup_semijoin_dups_elimination()
901
      join           Join to process
902
      options        Join options (needed to see if join buffering will be 
903
                     used or not)
904
      no_jbuf_after  Another bit of information re where join buffering will
905
                     be used.
906
907
  DESCRIPTION
908
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
909
    strategies:
910
911
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
912
                         of row combinations)
913
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
914
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
915
                  together and picking the 1st one)
916
    
917
    The join order has "duplicate-generating ranges", and every range is
918
    served by one strategy or a combination of FirstMatch with with some
919
    other strategy.
920
    
921
    "Duplicate-generating range" is defined as a range within the join order
922
    that contains all of the inner tables of a semi-join. All ranges must be
923
    disjoint, if tables of several semi-joins are interleaved, then the ranges
924
    are joined together, which is equivalent to converting
925
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
926
    to
927
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
928
    .
929
930
    Applicability conditions are as follows:
931
932
    DuplicateWeedout strategy
933
    ~~~~~~~~~~~~~~~~~~~~~~~~~
934
935
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
936
      +------+  +=========================+  +---+
937
        (1)                 (2)               (3)
938
939
       (1) - Prefix of OuterTables (those that participate in 
940
             IN-equality and/or are correlated with subquery) and outer 
941
             Noncorrelated Tables.
942
       (2) - The handled range. The range starts with the first sj-inner
943
             table, and covers all sj-inner and outer tables 
944
             Within the range,  Inner, Outer, outer Noncorrelated tables
945
             may follow in any order.
946
       (3) - The suffix of outer Noncorrelated tables.
947
    
948
    FirstMatch strategy
949
    ~~~~~~~~~~~~~~~~~~~
950
951
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
952
      +------+  +==================+  +---+
953
        (1)             (2)          (3)
954
955
      (1) - Prefix of outer and non-correlated tables
956
      (2) - The handled range, which may contain only inner and
957
            non-correlated tables.
958
      (3) - The suffix of outer Noncorrelated tables.
959
960
    InsideOut strategy 
961
    ~~~~~~~~~~~~~~~~~~
962
963
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
964
     +--------+   +===========+ +=============+   +------+
965
        (1)           (2)          (3)              (4)
966
     
967
      (1) - Prefix that may contain any outer tables. The prefix must contain
968
            all the non-trivially correlated outer tables. (non-trivially means
969
            that the correlation is not just through the IN-equality).
970
      
971
      (2) - Inner table for which the InsideOut scan is performed.
972
973
      (3) - The remainder of the duplicate-generating range. It is served by 
974
            application of FirstMatch strategy, with the exception that
975
            outer IN-correlated tables are considered to be non-correlated.
976
977
      (4) - THe suffix of outer and outer non-correlated tables.
978
979
    If several strategies are applicable, their relative priorities are:
980
      1. InsideOut
981
      2. FirstMatch 
982
      3. DuplicateWeedout
983
984
    This function walks over the join order and sets up the strategies by
985
    setting appropriate members in join_tab structures.
986
987
  RETURN
988
    FALSE  OK 
989
    TRUE   Out of memory error
990
*/
991
992
static
993
int setup_semijoin_dups_elimination(JOIN *join, ulonglong options, uint no_jbuf_after)
994
{
995
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
996
  struct {
997
    /* 
998
      0 - invalid (EOF marker), 
999
      1 - InsideOut, 
1000
      2 - Temptable (maybe confluent),
1001
      3 - Temptable with join buffering
1002
    */
1003
    uint strategy;
1004
    uint start_idx; /* Left range bound */
1005
    uint end_idx;   /* Right range bound */
1006
    /* 
1007
      For Temptable strategy: Bitmap of all outer and correlated tables from 
1008
      all involved join nests.
1009
    */
1010
    table_map outer_tables;
1011
  } dups_ranges [MAX_TABLES];
1012
1013
  TABLE_LIST *emb_insideout_nest= NULL;
1014
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
1015
                               tables) whose ranges we're in */
1016
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
1017
  table_map range_start_map= 0; /* table_map at current range start */
1018
  bool dealing_with_jbuf= FALSE; /* TRUE <=> table within cur range uses join buf */
1019
  int cur_range= 0;
1020
  uint i;
1021
1022
  DBUG_ENTER("setup_semijoin_dups_elimination");
1023
  /*
1024
    First pass: locate the duplicate-generating ranges and pick the strategies.
1025
  */
1026
  for (i=join->const_tables ; i < join->tables ; i++)
1027
  {
1028
    JOIN_TAB *tab=join->join_tab+i;
1029
    TABLE *table=tab->table;
1030
    cur_map |= table->map;
1031
1032
    if (tab->emb_sj_nest) // Encountered an sj-inner table
1033
    {
1034
      if (!emb_sj_map)
1035
      {
1036
        dups_ranges[cur_range].start_idx= i;
1037
        range_start_map= cur_map & ~table->map;
1038
        /*
1039
          Remember if this is a possible start of range that is covered by
1040
          the InsideOut strategy (the reason that it is not covered could
1041
          be that it overlaps with anther semi-join's range. we don't
1042
          support InsideOut for joined ranges)
1043
        */
1044
        if (join->best_positions[i].use_insideout_scan)
1045
          emb_insideout_nest= tab->emb_sj_nest;
1046
      }
1047
1048
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1049
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1050
1051
      if (tab->emb_sj_nest != emb_insideout_nest)
1052
      {
1053
        /*
1054
          Two different semi-joins interleave. This cannot be handled by
1055
          InsideOut strategy.
1056
        */
1057
        emb_insideout_nest= NULL;
1058
      }
1059
    }
1060
1061
    if (emb_sj_map) /* We're in duplicate-generating range */
1062
    {
1063
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1064
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1065
          i <= no_jbuf_after && !dealing_with_jbuf)
1066
      {
1067
        /*
1068
          This table uses join buffering, which makes use of FirstMatch or 
1069
          InsideOut strategies impossible for the current and (we assume) 
1070
          preceding duplicate-producing ranges.
1071
          That is, for the join order:
1072
1073
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
1074
                  |     |     |     |          | \
1075
                  +-----+     +-----+          |  join buffering use
1076
                     r1          r2         we're here
1077
1078
          we'll have to remove r1 and r2 and use duplicate-elimination
1079
          strategy that spans all the tables, starting from the very 1st
1080
          one.
1081
        */
1082
        dealing_with_jbuf= TRUE;
1083
        emb_insideout_nest= FALSE;
1084
1085
        /* 
1086
          Absorb all preceding duplicate-eliminating ranges. Their strategies
1087
          do not matter: 
1088
        */
1089
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
1090
        {
1091
          dups_ranges[cur_range].outer_tables |= 
1092
            dups_ranges[prev_range].outer_tables;
1093
        }
1094
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1095
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1096
        cur_range=  0;
1097
      }
1098
1099
      /*
1100
        Check if we are at the end of duplicate-producing range. We are if
1101
1102
        1. It's an InsideOut range (which presumes all correlated tables are
1103
           in the prefix), and all inner tables are in the join order prefix,
1104
           or
1105
        2. It's a DuplicateElimination range (possibly covering several
1106
           SJ-nests), and all inner, outer, and correlated tables of all 
1107
           sj-nests are in the join order prefix.
1108
      */
1109
      bool end_of_range= FALSE;
1110
      if (emb_insideout_nest && 
1111
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1112
      {
1113
        /* Save that this range is handled with InsideOut: */
1114
        dups_ranges[cur_range].strategy= 1;
1115
        end_of_range= TRUE;
1116
      }
1117
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1118
      {
1119
        /*
1120
          This is a complete range to be handled with either DuplicateWeedout 
1121
          or FirstMatch
1122
        */
1123
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1124
        /* 
1125
          This will hold tables from within the range that need to be put 
1126
          into the join buffer before we can use the FirstMatch on its tail.
1127
        */
1128
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
1129
                                             ~range_start_map;
1130
        end_of_range= TRUE;
1131
      }
1132
1133
      if (end_of_range)
1134
      {
1135
        dups_ranges[cur_range].end_idx= i+1;
1136
        emb_sj_map= emb_outer_tables= 0;
1137
        emb_insideout_nest= NULL;
1138
        dealing_with_jbuf= FALSE;
1139
        dups_ranges[++cur_range].strategy= 0;
1140
      }
1141
    }
1142
  }
1143
1144
  THD *thd= join->thd;
1145
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1146
  /*
1147
    Second pass: setup the chosen strategies    
1148
  */
1149
  for (int j= 0; j < cur_range; j++)
1150
  {
1151
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1152
    JOIN_TAB *jump_to;
1153
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
1154
    {
1155
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1156
      jump_to= tab++;
1157
    }
1158
    else // DuplicateWeedout strategy
1159
    {
1160
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1161
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1162
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1163
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1164
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1165
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1166
      JOIN_TAB *last_outer_tab= tab - 1;
1167
      /*
1168
        Walk through the range and remember
1169
         - tables that need their rowids to be put into temptable
1170
         - the last outer table
1171
      */
1172
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1173
      {
1174
        if (sj_table_is_included(join, tab))
1175
        {
1176
          last_tab->join_tab= tab;
1177
          last_tab->rowid_offset= jt_rowid_offset;
1178
          jt_rowid_offset += tab->table->file->ref_length;
1179
          if (tab->table->maybe_null)
1180
          {
1181
            last_tab->null_byte= jt_null_bits / 8;
1182
            last_tab->null_bit= jt_null_bits++;
1183
          }
1184
          last_tab++;
1185
          tab->table->prepare_for_position();
1186
          tab->rowid_keep_flags= rowid_keep_flags;
1187
        }
1188
        cur_map |= tab->table->map;
1189
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
1190
                                               dups_ranges[j].outer_tables))
1191
          last_outer_tab= tab;
1192
      }
1193
1194
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1195
      {
1196
        SJ_TMP_TABLE *sjtbl;
1197
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1198
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1199
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1200
          DBUG_RETURN(TRUE);
1201
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1202
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1203
        sjtbl->rowid_len= jt_rowid_offset;
1204
        sjtbl->null_bits= jt_null_bits;
1205
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
1206
1207
        *next_sjtbl_ptr= sjtbl;
1208
        next_sjtbl_ptr= &(sjtbl->next);
1209
        sjtbl->next= NULL;
1210
1211
        sjtbl->tmp_table= 
1212
          create_duplicate_weedout_tmp_table(thd, 
1213
                                             sjtbl->rowid_len + 
1214
                                             sjtbl->null_bytes,
1215
                                             sjtbl);
1216
1217
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1218
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1219
      }
1220
      tab= last_outer_tab + 1;
1221
      jump_to= last_outer_tab;
1222
    }
1223
1224
    /* Create the FirstMatch tail */
1225
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1226
    {
1227
      if (tab->emb_sj_nest)
1228
        tab->do_firstmatch= jump_to; 
1229
      else
1230
        jump_to= tab;
1231
    }
1232
  }
1233
  DBUG_RETURN(FALSE);
1234
}
1235
1236
1237
static void cleanup_sj_tmp_tables(JOIN *join)
1238
{
1239
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
1240
       sj_tbl= sj_tbl->next)
1241
  {
1242
    if (sj_tbl->tmp_table)
1243
    {
1244
      free_tmp_table(join->thd, sj_tbl->tmp_table);
1245
    }
1246
  }
1247
  join->sj_tmp_tables= NULL;
1248
}
1249
1250
uint make_join_orderinfo(JOIN *join);
1251
1252
/**
1253
  global select optimisation.
1254
1255
  @note
1256
    error code saved in field 'error'
1257
1258
  @retval
1259
    0   success
1260
  @retval
1261
    1   error
1262
*/
1263
1264
int
1265
JOIN::optimize()
1266
{
1267
  DBUG_ENTER("JOIN::optimize");
1268
  // to prevent double initialization on EXPLAIN
1269
  if (optimized)
1270
    DBUG_RETURN(0);
1271
  optimized= 1;
1272
1273
  thd_proc_info(thd, "optimizing");
1274
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1275
	      unit->select_limit_cnt);
1276
  /* select_limit is used to decide if we are likely to scan the whole table */
1277
  select_limit= unit->select_limit_cnt;
1278
  if (having || (select_options & OPTION_FOUND_ROWS))
1279
    select_limit= HA_POS_ERROR;
1280
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1281
  // Ignore errors of execution if option IGNORE present
1282
  if (thd->lex->ignore)
1283
    thd->lex->current_select->no_error= 1;
1284
1285
#ifdef HAVE_REF_TO_FIELDS			// Not done yet
1286
  /* Add HAVING to WHERE if possible */
1287
  if (having && !group_list && !sum_func_count)
1288
  {
1289
    if (!conds)
1290
    {
1291
      conds= having;
1292
      having= 0;
1293
    }
1294
    else if ((conds=new Item_cond_and(conds,having)))
1295
    {
1296
      /*
1297
        Item_cond_and can't be fixed after creation, so we do not check
1298
        conds->fixed
1299
      */
1300
      conds->fix_fields(thd, &conds);
1301
      conds->change_ref_to_fields(thd, tables_list);
1302
      conds->top_level_item();
1303
      having= 0;
1304
    }
1305
  }
1306
#endif
1307
  SELECT_LEX *sel= thd->lex->current_select;
1308
  if (sel->first_cond_optimization)
1309
  {
1310
    /*
1311
      The following code will allocate the new items in a permanent
1312
      MEMROOT for prepared statements and stored procedures.
1313
    */
1314
    sel->first_cond_optimization= 0;
1315
1316
    /* Convert all outer joins to inner joins if possible */
1317
    conds= simplify_joins(this, join_list, conds, TRUE, FALSE);
1318
    build_bitmap_for_nested_joins(join_list, 0);
1319
1320
    sel->prep_where= conds ? conds->copy_andor_structure(thd) : 0;
1321
  }
1322
1323
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1324
  if (thd->is_error())
1325
  {
1326
    error= 1;
1327
    DBUG_PRINT("error",("Error from optimize_cond"));
1328
    DBUG_RETURN(1);
1329
  }
1330
1331
  {
1332
    having= optimize_cond(this, having, join_list, &having_value);
1333
    if (thd->is_error())
1334
    {
1335
      error= 1;
1336
      DBUG_PRINT("error",("Error from optimize_cond"));
1337
      DBUG_RETURN(1);
1338
    }
1339
    if (select_lex->where)
1340
      select_lex->cond_value= cond_value;
1341
    if (select_lex->having)
1342
      select_lex->having_value= having_value;
1343
1344
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
1345
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1346
    {						/* Impossible cond */
1347
      DBUG_PRINT("info", (having_value == Item::COND_FALSE ? 
1348
                            "Impossible HAVING" : "Impossible WHERE"));
1349
      zero_result_cause=  having_value == Item::COND_FALSE ?
1350
                           "Impossible HAVING" : "Impossible WHERE";
1351
      error= 0;
1352
      DBUG_RETURN(0);
1353
    }
1354
  }
1355
1356
  /* Optimize count(*), min() and max() */
1357
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1358
  {
1359
    int res;
1360
    /*
1361
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1362
      to the WHERE conditions,
1363
      or 1 if all items were resolved,
1364
      or 0, or an error number HA_ERR_...
1365
    */
1366
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1367
    {
1368
      if (res == HA_ERR_KEY_NOT_FOUND)
1369
      {
1370
        DBUG_PRINT("info",("No matching min/max row"));
1371
	zero_result_cause= "No matching min/max row";
1372
	error=0;
1373
	DBUG_RETURN(0);
1374
      }
1375
      if (res > 1)
1376
      {
1377
        error= res;
1378
        DBUG_PRINT("error",("Error from opt_sum_query"));
1379
        DBUG_RETURN(1);
1380
      }
1381
      if (res < 0)
1382
      {
1383
        DBUG_PRINT("info",("No matching min/max row"));
1384
        zero_result_cause= "No matching min/max row";
1385
        error=0;
1386
        DBUG_RETURN(0);
1387
      }
1388
      DBUG_PRINT("info",("Select tables optimized away"));
1389
      zero_result_cause= "Select tables optimized away";
1390
      tables_list= 0;				// All tables resolved
1391
      /*
1392
        Extract all table-independent conditions and replace the WHERE
1393
        clause with them. All other conditions were computed by opt_sum_query
1394
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
1395
        so there is no need to compute the whole WHERE clause again.
1396
        Notice that make_cond_for_table() will always succeed to remove all
1397
        computed conditions, because opt_sum_query() is applicable only to
1398
        conjunctions.
1399
        Preserve conditions for EXPLAIN.
1400
      */
1401
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1402
      {
1403
        COND *table_independent_conds=
1404
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1405
        DBUG_EXECUTE("where",
1406
                     print_where(table_independent_conds,
1407
                                 "where after opt_sum_query()",
1408
                                 QT_ORDINARY););
1409
        conds= table_independent_conds;
1410
      }
1411
    }
1412
  }
1413
  if (!tables_list)
1414
  {
1415
    DBUG_PRINT("info",("No tables"));
1416
    error= 0;
1417
    DBUG_RETURN(0);
1418
  }
1419
  error= -1;					// Error is sent to client
1420
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1421
1422
  /* Calculate how to do the join */
1423
  thd_proc_info(thd, "statistics");
1424
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1425
      thd->is_fatal_error)
1426
  {
1427
    DBUG_PRINT("error",("Error: make_join_statistics() failed"));
1428
    DBUG_RETURN(1);
1429
  }
1430
1431
  /* Remove distinct if only const tables */
1432
  select_distinct= select_distinct && (const_tables != tables);
1433
  thd_proc_info(thd, "preparing");
1434
  if (result->initialize_tables(this))
1435
  {
1436
    DBUG_PRINT("error",("Error: initialize_tables() failed"));
1437
    DBUG_RETURN(1);				// error == -1
1438
  }
1439
  if (const_table_map != found_const_table_map &&
1440
      !(select_options & SELECT_DESCRIBE) &&
1441
      (!conds ||
1442
       !(conds->used_tables() & RAND_TABLE_BIT) ||
1443
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1444
  {
1445
    zero_result_cause= "no matching row in const table";
1446
    DBUG_PRINT("error",("Error: %s", zero_result_cause));
1447
    error= 0;
1448
    DBUG_RETURN(0);
1449
  }
1450
  if (!(thd->options & OPTION_BIG_SELECTS) &&
1451
      best_read > (double) thd->variables.max_join_size &&
1452
      !(select_options & SELECT_DESCRIBE))
1453
  {						/* purecov: inspected */
1454
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1455
    error= -1;
1456
    DBUG_RETURN(1);
1457
  }
1458
  if (const_tables && !thd->locked_tables &&
1459
      !(select_options & SELECT_NO_UNLOCK))
1460
    mysql_unlock_some_tables(thd, table, const_tables);
1461
  if (!conds && outer_join)
1462
  {
1463
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1464
    conds=new Item_int((longlong) 1,1);	// Always true
1465
  }
1466
  select= make_select(*table, const_table_map,
1467
                      const_table_map, conds, 1, &error);
1468
  if (error)
1469
  {						/* purecov: inspected */
1470
    error= -1;					/* purecov: inspected */
1471
    DBUG_PRINT("error",("Error: make_select() failed"));
1472
    DBUG_RETURN(1);
1473
  }
1474
  
1475
  reset_nj_counters(join_list);
1476
  make_outerjoin_info(this);
1477
1478
  /*
1479
    Among the equal fields belonging to the same multiple equality
1480
    choose the one that is to be retrieved first and substitute
1481
    all references to these in where condition for a reference for
1482
    the selected field.
1483
  */
1484
  if (conds)
1485
  {
1486
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1487
    conds->update_used_tables();
1488
    DBUG_EXECUTE("where",
1489
                 print_where(conds,
1490
                             "after substitute_best_equal",
1491
                             QT_ORDINARY););
1492
  }
1493
1494
  /*
1495
    Permorm the the optimization on fields evaluation mentioned above
1496
    for all on expressions.
1497
  */ 
1498
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1499
  {
1500
    if (*tab->on_expr_ref)
1501
    {
1502
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1503
                                                         tab->cond_equal,
1504
                                                         map2table);
1505
      (*tab->on_expr_ref)->update_used_tables();
1506
    }
1507
  }
1508
1509
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
1510
      (select_options & SELECT_DESCRIBE) &&
1511
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1512
  {
1513
    conds=new Item_int((longlong) 0,1);	// Always false
1514
  }
1515
  if (make_join_select(this, select, conds))
1516
  {
1517
    zero_result_cause=
1518
      "Impossible WHERE noticed after reading const tables";
1519
    DBUG_RETURN(0);				// error == 0
1520
  }
1521
1522
  error= -1;					/* if goto err */
1523
1524
  /* Optimize distinct away if possible */
1525
  {
1526
    ORDER *org_order= order;
1527
    order=remove_const(this, order,conds,1, &simple_order);
1528
    if (thd->is_error())
1529
    {
1530
      error= 1;
1531
      DBUG_PRINT("error",("Error from remove_const"));
1532
      DBUG_RETURN(1);
1533
    }
1534
1535
    /*
1536
      If we are using ORDER BY NULL or ORDER BY const_expression,
1537
      return result in any order (even if we are using a GROUP BY)
1538
    */
1539
    if (!order && org_order)
1540
      skip_sort_order= 1;
1541
  }
1542
  /*
1543
     Check if we can optimize away GROUP BY/DISTINCT.
1544
     We can do that if there are no aggregate functions, the
1545
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
1546
     (if present) contain direct references to all key parts of
1547
     an unique index (in whatever order) and if the key parts of the
1548
     unique index cannot contain NULLs.
1549
     Note that the unique keys for DISTINCT and GROUP BY should not
1550
     be the same (as long as they are unique).
1551
1552
     The FROM clause must contain a single non-constant table.
1553
  */
1554
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
1555
      !tmp_table_param.sum_func_count &&
1556
      (!join_tab[const_tables].select ||
1557
       !join_tab[const_tables].select->quick ||
1558
       join_tab[const_tables].select->quick->get_type() != 
1559
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1560
  {
1561
    if (group_list &&
1562
       list_contains_unique_index(join_tab[const_tables].table,
1563
                                 find_field_in_order_list,
1564
                                 (void *) group_list))
1565
    {
1566
      /*
1567
        We have found that grouping can be removed since groups correspond to
1568
        only one row anyway, but we still have to guarantee correct result
1569
        order. The line below effectively rewrites the query from GROUP BY
1570
        <fields> to ORDER BY <fields>. There are two exceptions:
1571
        - if skip_sort_order is set (see above), then we can simply skip
1572
          GROUP BY;
1573
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1574
          with the GROUP BY ones, i.e. either one is a prefix of another.
1575
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
1576
          test_if_subpart() copies the ASC/DESC attributes from the original
1577
          ORDER BY fields.
1578
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1579
          'order' as is.
1580
       */
1581
      if (!order || test_if_subpart(group_list, order))
1582
          order= skip_sort_order ? 0 : group_list;
1583
      /*
1584
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
1585
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
1586
      */
1587
      join_tab->table->keys_in_use_for_order_by=
1588
        join_tab->table->keys_in_use_for_group_by;
1589
      group_list= 0;
1590
      group= 0;
1591
    }
1592
    if (select_distinct &&
1593
       list_contains_unique_index(join_tab[const_tables].table,
1594
                                 find_field_in_item_list,
1595
                                 (void *) &fields_list))
1596
    {
1597
      select_distinct= 0;
1598
    }
1599
  }
1600
  if (group_list || tmp_table_param.sum_func_count)
1601
  {
1602
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1603
      select_distinct=0;
1604
  }
1605
  else if (select_distinct && tables - const_tables == 1)
1606
  {
1607
    /*
1608
      We are only using one table. In this case we change DISTINCT to a
1609
      GROUP BY query if:
1610
      - The GROUP BY can be done through indexes (no sort) and the ORDER
1611
        BY only uses selected fields.
1612
	(In this case we can later optimize away GROUP BY and ORDER BY)
1613
      - We are scanning the whole table without LIMIT
1614
        This can happen if:
1615
        - We are using CALC_FOUND_ROWS
1616
        - We are using an ORDER BY that can't be optimized away.
1617
1618
      We don't want to use this optimization when we are using LIMIT
1619
      because in this case we can just create a temporary table that
1620
      holds LIMIT rows and stop when this table is full.
1621
    */
1622
    JOIN_TAB *tab= &join_tab[const_tables];
1623
    bool all_order_fields_used;
1624
    if (order)
1625
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
1626
        &tab->table->keys_in_use_for_order_by);
1627
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1628
                                          order, fields_list, all_fields,
1629
				          &all_order_fields_used)))
1630
    {
1631
      bool skip_group= (skip_sort_order &&
1632
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
1633
                                &tab->table->keys_in_use_for_group_by) != 0);
1634
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1635
      if ((skip_group && all_order_fields_used) ||
1636
	  select_limit == HA_POS_ERROR ||
1637
	  (order && !skip_sort_order))
1638
      {
1639
	/*  Change DISTINCT to GROUP BY */
1640
	select_distinct= 0;
1641
	no_order= !order;
1642
	if (all_order_fields_used)
1643
	{
1644
	  if (order && skip_sort_order)
1645
	  {
1646
	    /*
1647
	      Force MySQL to read the table in sorted order to get result in
1648
	      ORDER BY order.
1649
	    */
1650
	    tmp_table_param.quick_group=0;
1651
	  }
1652
	  order=0;
1653
        }
1654
	group=1;				// For end_write_group
1655
      }
1656
      else
1657
	group_list= 0;
1658
    }
1659
    else if (thd->is_fatal_error)			// End of memory
1660
      DBUG_RETURN(1);
1661
  }
1662
  simple_group= 0;
1663
  {
1664
    ORDER *old_group_list;
1665
    group_list= remove_const(this, (old_group_list= group_list), conds,
1666
                             rollup.state == ROLLUP::STATE_NONE,
1667
			     &simple_group);
1668
    if (thd->is_error())
1669
    {
1670
      error= 1;
1671
      DBUG_PRINT("error",("Error from remove_const"));
1672
      DBUG_RETURN(1);
1673
    }
1674
    if (old_group_list && !group_list)
1675
      select_distinct= 0;
1676
  }
1677
  if (!group_list && group)
1678
  {
1679
    order=0;					// The output has only one row
1680
    simple_order=1;
1681
    select_distinct= 0;                       // No need in distinct for 1 row
1682
    group_optimized_away= 1;
1683
  }
1684
1685
  calc_group_buffer(this, group_list);
1686
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1687
1688
  if (test_if_subpart(group_list, order) ||
1689
      (!group_list && tmp_table_param.sum_func_count))
1690
    order=0;
1691
1692
  // Can't use sort on head table if using row cache
1693
  if (full_join)
1694
  {
1695
    if (group_list)
1696
      simple_group=0;
1697
    if (order)
1698
      simple_order=0;
1699
  }
1700
1701
  /*
1702
    Check if we need to create a temporary table.
1703
    This has to be done if all tables are not already read (const tables)
1704
    and one of the following conditions holds:
1705
    - We are using DISTINCT (simple distinct's are already optimized away)
1706
    - We are using an ORDER BY or GROUP BY on fields not in the first table
1707
    - We are using different ORDER BY and GROUP BY orders
1708
    - The user wants us to buffer the result.
1709
  */
1710
  need_tmp= (const_tables != tables &&
1711
	     ((select_distinct || !simple_order || !simple_group) ||
1712
	      (group_list && order) ||
1713
	      test(select_options & OPTION_BUFFER_RESULT)));
1714
1715
  uint no_jbuf_after= make_join_orderinfo(this);
1716
  ulonglong select_opts_for_readinfo= 
1717
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1718
1719
  sj_tmp_tables= NULL;
1720
  if (!select_lex->sj_nests.is_empty())
1721
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1722
                                    no_jbuf_after);
1723
1724
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1725
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1726
    DBUG_RETURN(1);
1727
1728
  /* Create all structures needed for materialized subquery execution. */
1729
  if (setup_subquery_materialization())
1730
    DBUG_RETURN(1);
1731
1732
  /*
1733
    is this simple IN subquery?
1734
  */
1735
  if (!group_list && !order &&
1736
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1737
      tables == 1 && conds &&
1738
      !unit->is_union())
1739
  {
1740
    if (!having)
1741
    {
1742
      Item *where= conds;
1743
      if (join_tab[0].type == JT_EQ_REF &&
1744
	  join_tab[0].ref.items[0]->name == in_left_expr_name)
1745
      {
1746
        remove_subq_pushed_predicates(&where);
1747
        save_index_subquery_explain_info(join_tab, where);
1748
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1749
        error= 0;
1750
        DBUG_RETURN(unit->item->
1751
                    change_engine(new
1752
                                  subselect_uniquesubquery_engine(thd,
1753
                                                                  join_tab,
1754
                                                                  unit->item,
1755
                                                                  where)));
1756
      }
1757
      else if (join_tab[0].type == JT_REF &&
1758
	       join_tab[0].ref.items[0]->name == in_left_expr_name)
1759
      {
1760
	remove_subq_pushed_predicates(&where);
1761
        save_index_subquery_explain_info(join_tab, where);
1762
        join_tab[0].type= JT_INDEX_SUBQUERY;
1763
        error= 0;
1764
        DBUG_RETURN(unit->item->
1765
                    change_engine(new
1766
                                  subselect_indexsubquery_engine(thd,
1767
                                                                 join_tab,
1768
                                                                 unit->item,
1769
                                                                 where,
1770
                                                                 NULL,
1771
                                                                 0)));
1772
      }
1773
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
1774
	       join_tab[0].ref.items[0]->name == in_left_expr_name &&
1775
               having->name == in_having_cond)
1776
    {
1777
      join_tab[0].type= JT_INDEX_SUBQUERY;
1778
      error= 0;
1779
      conds= remove_additional_cond(conds);
1780
      save_index_subquery_explain_info(join_tab, conds);
1781
      DBUG_RETURN(unit->item->
1782
		  change_engine(new subselect_indexsubquery_engine(thd,
1783
								   join_tab,
1784
								   unit->item,
1785
								   conds,
1786
                                                                   having,
1787
								   1)));
1788
    }
1789
1790
  }
1791
  /*
1792
    Need to tell handlers that to play it safe, it should fetch all
1793
    columns of the primary key of the tables: this is because MySQL may
1794
    build row pointers for the rows, and for all columns of the primary key
1795
    the read set has not necessarily been set by the server code.
1796
  */
1797
  if (need_tmp || select_distinct || group_list || order)
1798
  {
1799
    for (uint i = const_tables; i < tables; i++)
1800
      join_tab[i].table->prepare_for_position();
1801
  }
1802
1803
  DBUG_EXECUTE("info",TEST_join(this););
1804
1805
  if (const_tables != tables)
1806
  {
1807
    /*
1808
      Because filesort always does a full table scan or a quick range scan
1809
      we must add the removed reference to the select for the table.
1810
      We only need to do this when we have a simple_order or simple_group
1811
      as in other cases the join is done before the sort.
1812
    */
1813
    if ((order || group_list) &&
1814
        (join_tab[const_tables].type != JT_ALL) &&
1815
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
1816
        ((order && simple_order) || (group_list && simple_group)))
1817
    {
1818
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1819
        DBUG_RETURN(1);
1820
      }
1821
    }
1822
    
1823
    if (!(select_options & SELECT_BIG_RESULT) &&
1824
        ((group_list &&
1825
          (!simple_group ||
1826
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
1827
                                    unit->select_limit_cnt, 0, 
1828
                                    &join_tab[const_tables].table->
1829
                                    keys_in_use_for_group_by))) ||
1830
         select_distinct) &&
1831
        tmp_table_param.quick_group)
1832
    {
1833
      need_tmp=1; simple_order=simple_group=0;	// Force tmp table without sort
1834
    }
1835
    if (order)
1836
    {
1837
      /*
1838
        Force using of tmp table if sorting by a SP or UDF function due to
1839
        their expensive and probably non-deterministic nature.
1840
      */
1841
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1842
      {
1843
        Item *item= *tmp_order->item;
1844
        if (item->is_expensive())
1845
        {
1846
          /* Force tmp table without sort */
1847
          need_tmp=1; simple_order=simple_group=0;
1848
          break;
1849
        }
1850
      }
1851
    }
1852
  }
1853
1854
  tmp_having= having;
1855
  if (select_options & SELECT_DESCRIBE)
1856
  {
1857
    error= 0;
1858
    DBUG_RETURN(0);
1859
  }
1860
  having= 0;
1861
1862
  /*
1863
    The loose index scan access method guarantees that all grouping or
1864
    duplicate row elimination (for distinct) is already performed
1865
    during data retrieval, and that all MIN/MAX functions are already
1866
    computed for each group. Thus all MIN/MAX functions should be
1867
    treated as regular functions, and there is no need to perform
1868
    grouping in the main execution loop.
1869
    Notice that currently loose index scan is applicable only for
1870
    single table queries, thus it is sufficient to test only the first
1871
    join_tab element of the plan for its access method.
1872
  */
1873
  if (join_tab->is_using_loose_index_scan())
1874
    tmp_table_param.precomputed_group_by= TRUE;
1875
1876
  /* Create a tmp table if distinct or if the sort is too complicated */
1877
  if (need_tmp)
1878
  {
1879
    DBUG_PRINT("info",("Creating tmp table"));
1880
    thd_proc_info(thd, "Creating tmp table");
1881
1882
    init_items_ref_array();
1883
1884
    tmp_table_param.hidden_field_count= (all_fields.elements -
1885
					 fields_list.elements);
1886
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1887
                                                             (ORDER*) 0);
1888
    /*
1889
      Pushing LIMIT to the temporary table creation is not applicable
1890
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1891
      there are aggregate functions, because in all these cases we need
1892
      all result rows.
1893
    */
1894
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1895
                             !tmp_group &&
1896
                             !thd->lex->current_select->with_sum_func) ?
1897
                            select_limit : HA_POS_ERROR;
1898
1899
    if (!(exec_tmp_table1=
1900
	  create_tmp_table(thd, &tmp_table_param, all_fields,
1901
                           tmp_group,
1902
			   group_list ? 0 : select_distinct,
1903
			   group_list && simple_group,
1904
			   select_options,
1905
                           tmp_rows_limit,
1906
			   (char *) "")))
1907
		{
1908
      DBUG_RETURN(1);
1909
    }
1910
1911
    /*
1912
      We don't have to store rows in temp table that doesn't match HAVING if:
1913
      - we are sorting the table and writing complete group rows to the
1914
        temp table.
1915
      - We are using DISTINCT without resolving the distinct as a GROUP BY
1916
        on all columns.
1917
      
1918
      If having is not handled here, it will be checked before the row
1919
      is sent to the client.
1920
    */    
1921
    if (tmp_having && 
1922
	(sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1923
      having= tmp_having;
1924
1925
    /* if group or order on first table, sort first */
1926
    if (group_list && simple_group)
1927
    {
1928
      DBUG_PRINT("info",("Sorting for group"));
1929
      thd_proc_info(thd, "Sorting for group");
1930
      if (create_sort_index(thd, this, group_list,
1931
			    HA_POS_ERROR, HA_POS_ERROR, FALSE) ||
1932
	  alloc_group_fields(this, group_list) ||
1933
          make_sum_func_list(all_fields, fields_list, 1) ||
1934
          setup_sum_funcs(thd, sum_funcs))
1935
      {
1936
        DBUG_RETURN(1);
1937
      }
1938
      group_list=0;
1939
    }
1940
    else
1941
    {
1942
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1943
          setup_sum_funcs(thd, sum_funcs))
1944
      {
1945
        DBUG_RETURN(1);
1946
      }
1947
1948
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1949
      {
1950
	DBUG_PRINT("info",("Sorting for order"));
1951
        thd_proc_info(thd, "Sorting for order");
1952
        if (create_sort_index(thd, this, order,
1953
                              HA_POS_ERROR, HA_POS_ERROR, TRUE))
1954
        {
1955
          DBUG_RETURN(1);
1956
        }
1957
        order=0;
1958
      }
1959
    }
1960
    
1961
    /*
1962
      Optimize distinct when used on some of the tables
1963
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1964
      In this case we can stop scanning t2 when we have found one t1.a
1965
    */
1966
1967
    if (exec_tmp_table1->distinct)
1968
    {
1969
      table_map used_tables= thd->used_tables;
1970
      JOIN_TAB *last_join_tab= join_tab+tables-1;
1971
      do
1972
      {
1973
	if (used_tables & last_join_tab->table->map)
1974
	  break;
1975
	last_join_tab->not_used_in_distinct=1;
1976
      } while (last_join_tab-- != join_tab);
1977
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1978
      if (order && skip_sort_order)
1979
      {
1980
 	/* Should always succeed */
1981
	if (test_if_skip_sort_order(&join_tab[const_tables],
1982
				    order, unit->select_limit_cnt, 0, 
1983
                                    &join_tab[const_tables].table->
1984
                                      keys_in_use_for_order_by))
1985
	  order=0;
1986
      }
1987
    }
1988
1989
    /* 
1990
      If this join belongs to an uncacheable subquery save 
1991
      the original join 
1992
    */
1993
    if (select_lex->uncacheable && !is_top_level_join() &&
1994
        init_save_join_tab())
1995
      DBUG_RETURN(-1);                         /* purecov: inspected */
1996
  }
1997
1998
  error= 0;
1999
  DBUG_RETURN(0);
2000
}
2001
2002
2003
/**
2004
  Restore values in temporary join.
2005
*/
2006
void JOIN::restore_tmp()
2007
{
2008
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
2009
}
2010
2011
2012
int
2013
JOIN::reinit()
2014
{
2015
  DBUG_ENTER("JOIN::reinit");
2016
2017
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
2018
                                    select_lex->offset_limit->val_uint() :
2019
                                    ULL(0));
2020
2021
  first_record= 0;
2022
2023
  if (exec_tmp_table1)
2024
  {
2025
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
2026
    exec_tmp_table1->file->ha_delete_all_rows();
2027
    free_io_cache(exec_tmp_table1);
2028
    filesort_free_buffers(exec_tmp_table1,0);
2029
  }
2030
  if (exec_tmp_table2)
2031
  {
2032
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
2033
    exec_tmp_table2->file->ha_delete_all_rows();
2034
    free_io_cache(exec_tmp_table2);
2035
    filesort_free_buffers(exec_tmp_table2,0);
2036
  }
2037
  if (items0)
2038
    set_items_ref_array(items0);
2039
2040
  if (join_tab_save)
2041
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
2042
2043
  if (tmp_join)
2044
    restore_tmp();
2045
2046
  /* Reset of sum functions */
2047
  if (sum_funcs)
2048
  {
2049
    Item_sum *func, **func_ptr= sum_funcs;
2050
    while ((func= *(func_ptr++)))
2051
      func->clear();
2052
  }
2053
2054
  DBUG_RETURN(0);
2055
}
2056
2057
/**
2058
   @brief Save the original join layout
2059
      
2060
   @details Saves the original join layout so it can be reused in 
2061
   re-execution and for EXPLAIN.
2062
             
2063
   @return Operation status
2064
   @retval 0      success.
2065
   @retval 1      error occurred.
2066
*/
2067
2068
bool
2069
JOIN::init_save_join_tab()
2070
{
2071
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
2072
    return 1;                                  /* purecov: inspected */
2073
  error= 0;				       // Ensure that tmp_join.error= 0
2074
  restore_tmp();
2075
  return 0;
2076
}
2077
2078
2079
bool
2080
JOIN::save_join_tab()
2081
{
2082
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
2083
  {
2084
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2085
						sizeof(JOIN_TAB) * tables)))
2086
      return 1;
2087
  }
2088
  return 0;
2089
}
2090
2091
2092
/**
2093
  Exec select.
2094
2095
  @todo
2096
    Note, that create_sort_index calls test_if_skip_sort_order and may
2097
    finally replace sorting with index scan if there is a LIMIT clause in
2098
    the query.  It's never shown in EXPLAIN!
2099
2100
  @todo
2101
    When can we have here thd->net.report_error not zero?
2102
*/
2103
void
2104
JOIN::exec()
2105
{
2106
  List<Item> *columns_list= &fields_list;
2107
  int      tmp_error;
2108
  DBUG_ENTER("JOIN::exec");
2109
2110
  thd_proc_info(thd, "executing");
2111
  error= 0;
2112
  (void) result->prepare2(); // Currently, this cannot fail.
2113
2114
  if (!tables_list && (tables || !select_lex->with_sum_func))
2115
  {                                           // Only test of functions
2116
    if (select_options & SELECT_DESCRIBE)
2117
      select_describe(this, FALSE, FALSE, FALSE,
2118
		      (zero_result_cause?zero_result_cause:"No tables used"));
2119
    else
2120
    {
2121
      result->send_fields(*columns_list,
2122
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2123
      /*
2124
        We have to test for 'conds' here as the WHERE may not be constant
2125
        even if we don't have any tables for prepared statements or if
2126
        conds uses something like 'rand()'.
2127
      */
2128
      if (cond_value != Item::COND_FALSE &&
2129
          (!conds || conds->val_int()) &&
2130
          (!having || having->val_int()))
2131
      {
2132
	if (do_send_rows && result->send_data(fields_list))
2133
	  error= 1;
2134
	else
2135
	{
2136
	  error= (int) result->send_eof();
2137
	  send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2138
                         thd->sent_row_count);
2139
	}
2140
      }
2141
      else
2142
      {
2143
	error=(int) result->send_eof();
2144
        send_records= 0;
2145
      }
2146
    }
2147
    /* Single select (without union) always returns 0 or 1 row */
2148
    thd->limit_found_rows= send_records;
2149
    thd->examined_row_count= 0;
2150
    DBUG_VOID_RETURN;
2151
  }
2152
  /*
2153
    Don't reset the found rows count if there're no tables as
2154
    FOUND_ROWS() may be called. Never reset the examined row count here.
2155
    It must be accumulated from all join iterations of all join parts.
2156
  */
2157
  if (tables)
2158
    thd->limit_found_rows= 0;
2159
2160
  if (zero_result_cause)
2161
  {
2162
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
2163
                            *columns_list,
2164
			    send_row_on_empty_set(),
2165
			    select_options,
2166
			    zero_result_cause,
2167
			    having);
2168
    DBUG_VOID_RETURN;
2169
  }
2170
2171
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2172
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2173
    DBUG_VOID_RETURN;
2174
2175
  if (select_options & SELECT_DESCRIBE)
2176
  {
2177
    /*
2178
      Check if we managed to optimize ORDER BY away and don't use temporary
2179
      table to resolve ORDER BY: in that case, we only may need to do
2180
      filesort for GROUP BY.
2181
    */
2182
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2183
    {
2184
      /*
2185
	Reset 'order' to 'group_list' and reinit variables describing
2186
	'order'
2187
      */
2188
      order= group_list;
2189
      simple_order= simple_group;
2190
      skip_sort_order= 0;
2191
    }
2192
    if (order && 
2193
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2194
	(const_tables == tables ||
2195
 	 ((simple_order || skip_sort_order) &&
2196
	  test_if_skip_sort_order(&join_tab[const_tables], order,
2197
				  select_limit, 0, 
2198
                                  &join_tab[const_tables].table->
2199
                                    keys_in_use_for_query))))
2200
      order=0;
2201
    having= tmp_having;
2202
    select_describe(this, need_tmp,
2203
		    order != 0 && !skip_sort_order,
2204
		    select_distinct,
2205
                    !tables ? "No tables used" : NullS);
2206
    DBUG_VOID_RETURN;
2207
  }
2208
2209
  JOIN *curr_join= this;
2210
  List<Item> *curr_all_fields= &all_fields;
2211
  List<Item> *curr_fields_list= &fields_list;
2212
  TABLE *curr_tmp_table= 0;
2213
  /*
2214
    Initialize examined rows here because the values from all join parts
2215
    must be accumulated in examined_row_count. Hence every join
2216
    iteration must count from zero.
2217
  */
2218
  curr_join->examined_rows= 0;
2219
2220
  /* Create a tmp table if distinct or if the sort is too complicated */
2221
  if (need_tmp)
2222
  {
2223
    if (tmp_join)
2224
    {
2225
      /*
2226
        We are in a non cacheable sub query. Get the saved join structure
2227
        after optimization.
2228
        (curr_join may have been modified during last exection and we need
2229
        to reset it)
2230
      */
2231
      curr_join= tmp_join;
2232
    }
2233
    curr_tmp_table= exec_tmp_table1;
2234
2235
    /* Copy data to the temporary table */
2236
    thd_proc_info(thd, "Copying to tmp table");
2237
    DBUG_PRINT("info", ("%s", thd->proc_info));
2238
    if (!curr_join->sort_and_group &&
2239
        curr_join->const_tables != curr_join->tables)
2240
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2241
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2242
    {
2243
      error= tmp_error;
2244
      DBUG_VOID_RETURN;
2245
    }
2246
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2247
    
2248
    if (curr_join->having)
2249
      curr_join->having= curr_join->tmp_having= 0; // Allready done
2250
    
2251
    /* Change sum_fields reference to calculated fields in tmp_table */
2252
    curr_join->all_fields= *curr_all_fields;
2253
    if (!items1)
2254
    {
2255
      items1= items0 + all_fields.elements;
2256
      if (sort_and_group || curr_tmp_table->group)
2257
      {
2258
	if (change_to_use_tmp_fields(thd, items1,
2259
				     tmp_fields_list1, tmp_all_fields1,
2260
				     fields_list.elements, all_fields))
2261
	  DBUG_VOID_RETURN;
2262
      }
2263
      else
2264
      {
2265
	if (change_refs_to_tmp_fields(thd, items1,
2266
				      tmp_fields_list1, tmp_all_fields1,
2267
				      fields_list.elements, all_fields))
2268
	  DBUG_VOID_RETURN;
2269
      }
2270
      curr_join->tmp_all_fields1= tmp_all_fields1;
2271
      curr_join->tmp_fields_list1= tmp_fields_list1;
2272
      curr_join->items1= items1;
2273
    }
2274
    curr_all_fields= &tmp_all_fields1;
2275
    curr_fields_list= &tmp_fields_list1;
2276
    curr_join->set_items_ref_array(items1);
2277
    
2278
    if (sort_and_group || curr_tmp_table->group)
2279
    {
2280
      curr_join->tmp_table_param.field_count+= 
2281
	curr_join->tmp_table_param.sum_func_count+
2282
	curr_join->tmp_table_param.func_count;
2283
      curr_join->tmp_table_param.sum_func_count= 
2284
	curr_join->tmp_table_param.func_count= 0;
2285
    }
2286
    else
2287
    {
2288
      curr_join->tmp_table_param.field_count+= 
2289
	curr_join->tmp_table_param.func_count;
2290
      curr_join->tmp_table_param.func_count= 0;
2291
    }
2292
    
2293
    if (curr_tmp_table->group)
2294
    {						// Already grouped
2295
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2296
	curr_join->order= curr_join->group_list;  /* order by group */
2297
      curr_join->group_list= 0;
2298
    }
2299
    
2300
    /*
2301
      If we have different sort & group then we must sort the data by group
2302
      and copy it to another tmp table
2303
      This code is also used if we are using distinct something
2304
      we haven't been able to store in the temporary table yet
2305
      like SEC_TO_TIME(SUM(...)).
2306
    */
2307
2308
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2309
    {					/* Must copy to another table */
2310
      DBUG_PRINT("info",("Creating group table"));
2311
      
2312
      /* Free first data from old join */
2313
      curr_join->join_free();
2314
      if (make_simple_join(curr_join, curr_tmp_table))
2315
	DBUG_VOID_RETURN;
2316
      calc_group_buffer(curr_join, group_list);
2317
      count_field_types(select_lex, &curr_join->tmp_table_param,
2318
			curr_join->tmp_all_fields1,
2319
			curr_join->select_distinct && !curr_join->group_list);
2320
      curr_join->tmp_table_param.hidden_field_count= 
2321
	(curr_join->tmp_all_fields1.elements-
2322
	 curr_join->tmp_fields_list1.elements);
2323
      
2324
      
2325
      if (exec_tmp_table2)
2326
	curr_tmp_table= exec_tmp_table2;
2327
      else
2328
      {
2329
	/* group data to new table */
2330
2331
        /*
2332
          If the access method is loose index scan then all MIN/MAX
2333
          functions are precomputed, and should be treated as regular
2334
          functions. See extended comment in JOIN::exec.
2335
        */
2336
        if (curr_join->join_tab->is_using_loose_index_scan())
2337
          curr_join->tmp_table_param.precomputed_group_by= TRUE;
2338
2339
	if (!(curr_tmp_table=
2340
	      exec_tmp_table2= create_tmp_table(thd,
2341
						&curr_join->tmp_table_param,
2342
						*curr_all_fields,
2343
						(ORDER*) 0,
2344
						curr_join->select_distinct && 
2345
						!curr_join->group_list,
2346
						1, curr_join->select_options,
2347
						HA_POS_ERROR,
2348
						(char *) "")))
2349
	  DBUG_VOID_RETURN;
2350
	curr_join->exec_tmp_table2= exec_tmp_table2;
2351
      }
2352
      if (curr_join->group_list)
2353
      {
2354
	thd_proc_info(thd, "Creating sort index");
2355
	if (curr_join->join_tab == join_tab && save_join_tab())
2356
	{
2357
	  DBUG_VOID_RETURN;
2358
	}
2359
	if (create_sort_index(thd, curr_join, curr_join->group_list,
2360
			      HA_POS_ERROR, HA_POS_ERROR, FALSE) ||
2361
	    make_group_fields(this, curr_join))
2362
	{
2363
	  DBUG_VOID_RETURN;
2364
	}
2365
        sortorder= curr_join->sortorder;
2366
      }
2367
      
2368
      thd_proc_info(thd, "Copying to group table");
2369
      DBUG_PRINT("info", ("%s", thd->proc_info));
2370
      tmp_error= -1;
2371
      if (curr_join != this)
2372
      {
2373
	if (sum_funcs2)
2374
	{
2375
	  curr_join->sum_funcs= sum_funcs2;
2376
	  curr_join->sum_funcs_end= sum_funcs_end2; 
2377
	}
2378
	else
2379
	{
2380
	  curr_join->alloc_func_list();
2381
	  sum_funcs2= curr_join->sum_funcs;
2382
	  sum_funcs_end2= curr_join->sum_funcs_end;
2383
	}
2384
      }
2385
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2386
					1, TRUE))
2387
        DBUG_VOID_RETURN;
2388
      curr_join->group_list= 0;
2389
      if (!curr_join->sort_and_group &&
2390
          curr_join->const_tables != curr_join->tables)
2391
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2392
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2393
	  (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2394
      {
2395
	error= tmp_error;
2396
	DBUG_VOID_RETURN;
2397
      }
2398
      end_read_record(&curr_join->join_tab->read_record);
2399
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2400
      curr_join->join_tab[0].table= 0;           // Table is freed
2401
      
2402
      // No sum funcs anymore
2403
      if (!items2)
2404
      {
2405
	items2= items1 + all_fields.elements;
2406
	if (change_to_use_tmp_fields(thd, items2,
2407
				     tmp_fields_list2, tmp_all_fields2, 
2408
				     fields_list.elements, tmp_all_fields1))
2409
	  DBUG_VOID_RETURN;
2410
	curr_join->tmp_fields_list2= tmp_fields_list2;
2411
	curr_join->tmp_all_fields2= tmp_all_fields2;
2412
      }
2413
      curr_fields_list= &curr_join->tmp_fields_list2;
2414
      curr_all_fields= &curr_join->tmp_all_fields2;
2415
      curr_join->set_items_ref_array(items2);
2416
      curr_join->tmp_table_param.field_count+= 
2417
	curr_join->tmp_table_param.sum_func_count;
2418
      curr_join->tmp_table_param.sum_func_count= 0;
2419
    }
2420
    if (curr_tmp_table->distinct)
2421
      curr_join->select_distinct=0;		/* Each row is unique */
2422
    
2423
    curr_join->join_free();			/* Free quick selects */
2424
    if (curr_join->select_distinct && ! curr_join->group_list)
2425
    {
2426
      thd_proc_info(thd, "Removing duplicates");
2427
      if (curr_join->tmp_having)
2428
	curr_join->tmp_having->update_used_tables();
2429
      if (remove_duplicates(curr_join, curr_tmp_table,
2430
			    *curr_fields_list, curr_join->tmp_having))
2431
	DBUG_VOID_RETURN;
2432
      curr_join->tmp_having=0;
2433
      curr_join->select_distinct=0;
2434
    }
2435
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2436
    if (make_simple_join(curr_join, curr_tmp_table))
2437
      DBUG_VOID_RETURN;
2438
    calc_group_buffer(curr_join, curr_join->group_list);
2439
    count_field_types(select_lex, &curr_join->tmp_table_param, 
2440
                      *curr_all_fields, 0);
2441
    
2442
  }
2443
  
2444
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2445
  {
2446
    if (make_group_fields(this, curr_join))
2447
    {
2448
      DBUG_VOID_RETURN;
2449
    }
2450
    if (!items3)
2451
    {
2452
      if (!items0)
2453
	init_items_ref_array();
2454
      items3= ref_pointer_array + (all_fields.elements*4);
2455
      setup_copy_fields(thd, &curr_join->tmp_table_param,
2456
			items3, tmp_fields_list3, tmp_all_fields3,
2457
			curr_fields_list->elements, *curr_all_fields);
2458
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2459
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2460
      tmp_table_param.save_copy_field_end=
2461
	curr_join->tmp_table_param.copy_field_end;
2462
      curr_join->tmp_all_fields3= tmp_all_fields3;
2463
      curr_join->tmp_fields_list3= tmp_fields_list3;
2464
    }
2465
    else
2466
    {
2467
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2468
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2469
      curr_join->tmp_table_param.copy_field_end=
2470
	tmp_table_param.save_copy_field_end;
2471
    }
2472
    curr_fields_list= &tmp_fields_list3;
2473
    curr_all_fields= &tmp_all_fields3;
2474
    curr_join->set_items_ref_array(items3);
2475
2476
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2477
				      1, TRUE) || 
2478
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2479
        thd->is_fatal_error)
2480
      DBUG_VOID_RETURN;
2481
  }
2482
  if (curr_join->group_list || curr_join->order)
2483
  {
2484
    DBUG_PRINT("info",("Sorting for send_fields"));
2485
    thd_proc_info(thd, "Sorting result");
2486
    /* If we have already done the group, add HAVING to sorted table */
2487
    if (curr_join->tmp_having && ! curr_join->group_list && 
2488
	! curr_join->sort_and_group)
2489
    {
2490
      // Some tables may have been const
2491
      curr_join->tmp_having->update_used_tables();
2492
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2493
      table_map used_tables= (curr_join->const_table_map |
2494
			      curr_table->table->map);
2495
2496
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2497
						 used_tables,
2498
						 used_tables, 0);
2499
      if (sort_table_cond)
2500
      {
2501
	if (!curr_table->select)
2502
	  if (!(curr_table->select= new SQL_SELECT))
2503
	    DBUG_VOID_RETURN;
2504
	if (!curr_table->select->cond)
2505
	  curr_table->select->cond= sort_table_cond;
2506
	else					// This should never happen
2507
	{
2508
	  if (!(curr_table->select->cond=
2509
		new Item_cond_and(curr_table->select->cond,
2510
				  sort_table_cond)))
2511
	    DBUG_VOID_RETURN;
2512
	  /*
2513
	    Item_cond_and do not need fix_fields for execution, its parameters
2514
	    are fixed or do not need fix_fields, too
2515
	  */
2516
	  curr_table->select->cond->quick_fix_field();
2517
	}
2518
	curr_table->select_cond= curr_table->select->cond;
2519
	curr_table->select_cond->top_level_item();
2520
	DBUG_EXECUTE("where",print_where(curr_table->select->cond,
2521
					 "select and having",
2522
                                         QT_ORDINARY););
2523
	curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2524
						   ~ (table_map) 0,
2525
						   ~used_tables, 0);
2526
	DBUG_EXECUTE("where",print_where(curr_join->tmp_having,
2527
                                         "having after sort",
2528
                                         QT_ORDINARY););
2529
      }
2530
    }
2531
    {
2532
      if (group)
2533
	curr_join->select_limit= HA_POS_ERROR;
2534
      else
2535
      {
2536
	/*
2537
	  We can abort sorting after thd->select_limit rows if we there is no
2538
	  WHERE clause for any tables after the sorted one.
2539
	*/
2540
	JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2541
	JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2542
	for (; curr_table < end_table ; curr_table++)
2543
	{
2544
	  /*
2545
	    table->keyuse is set in the case there was an original WHERE clause
2546
	    on the table that was optimized away.
2547
	  */
2548
	  if (curr_table->select_cond ||
2549
	      (curr_table->keyuse && !curr_table->first_inner))
2550
	  {
2551
	    /* We have to sort all rows */
2552
	    curr_join->select_limit= HA_POS_ERROR;
2553
	    break;
2554
	  }
2555
	}
2556
      }
2557
      if (curr_join->join_tab == join_tab && save_join_tab())
2558
      {
2559
	DBUG_VOID_RETURN;
2560
      }
2561
      /*
2562
	Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2563
	chose FILESORT to be faster than INDEX SCAN or there is no 
2564
	suitable index present.
2565
	Note, that create_sort_index calls test_if_skip_sort_order and may
2566
	finally replace sorting with index scan if there is a LIMIT clause in
2567
	the query. XXX: it's never shown in EXPLAIN!
2568
	OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2569
      */
2570
      if (create_sort_index(thd, curr_join,
2571
			    curr_join->group_list ? 
2572
			    curr_join->group_list : curr_join->order,
2573
			    curr_join->select_limit,
2574
			    (select_options & OPTION_FOUND_ROWS ?
2575
			     HA_POS_ERROR : unit->select_limit_cnt),
2576
                            curr_join->group_list ? TRUE : FALSE))
2577
	DBUG_VOID_RETURN;
2578
      sortorder= curr_join->sortorder;
2579
      if (curr_join->const_tables != curr_join->tables &&
2580
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2581
      {
2582
        /*
2583
          If no IO cache exists for the first table then we are using an
2584
          INDEX SCAN and no filesort. Thus we should not remove the sorted
2585
          attribute on the INDEX SCAN.
2586
        */
2587
        skip_sort_order= 1;
2588
      }
2589
    }
2590
  }
2591
  /* XXX: When can we have here thd->is_error() not zero? */
2592
  if (thd->is_error())
2593
  {
2594
    error= thd->is_error();
2595
    DBUG_VOID_RETURN;
2596
  }
2597
  curr_join->having= curr_join->tmp_having;
2598
  curr_join->fields= curr_fields_list;
2599
2600
  {
2601
    thd_proc_info(thd, "Sending data");
2602
    DBUG_PRINT("info", ("%s", thd->proc_info));
2603
    result->send_fields(*curr_fields_list,
2604
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2605
    error= do_select(curr_join, curr_fields_list, NULL);
2606
    thd->limit_found_rows= curr_join->send_records;
2607
  }
2608
2609
  /* Accumulate the counts from all join iterations of all join parts. */
2610
  thd->examined_row_count+= curr_join->examined_rows;
2611
  DBUG_PRINT("counts", ("thd->examined_row_count: %lu",
2612
                        (ulong) thd->examined_row_count));
2613
2614
  /* 
2615
    With EXPLAIN EXTENDED we have to restore original ref_array
2616
    for a derived table which is always materialized.
2617
    Otherwise we would not be able to print the query  correctly.
2618
  */ 
2619
  if (items0 &&
2620
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
2621
      select_lex->linkage == DERIVED_TABLE_TYPE)      
2622
    set_items_ref_array(items0);
2623
2624
  DBUG_VOID_RETURN;
2625
}
2626
2627
2628
/**
2629
  Clean up join.
2630
2631
  @return
2632
    Return error that hold JOIN.
2633
*/
2634
2635
int
2636
JOIN::destroy()
2637
{
2638
  DBUG_ENTER("JOIN::destroy");
2639
  select_lex->join= 0;
2640
2641
  if (tmp_join)
2642
  {
2643
    if (join_tab != tmp_join->join_tab)
2644
    {
2645
      JOIN_TAB *tab, *end;
2646
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2647
	tab->cleanup();
2648
    }
2649
    tmp_join->tmp_join= 0;
2650
    tmp_table_param.copy_field=0;
2651
    DBUG_RETURN(tmp_join->destroy());
2652
  }
2653
  cond_equal= 0;
2654
2655
  cleanup(1);
2656
  if (exec_tmp_table1)
2657
    free_tmp_table(thd, exec_tmp_table1);
2658
  if (exec_tmp_table2)
2659
    free_tmp_table(thd, exec_tmp_table2);
2660
  delete select;
2661
  delete_dynamic(&keyuse);
2662
  DBUG_RETURN(error);
2663
}
2664
2665
2666
2667
/**
2668
  An entry point to single-unit select (a select without UNION).
2669
2670
  @param thd                  thread handler
2671
  @param rref_pointer_array   a reference to ref_pointer_array of
2672
                              the top-level select_lex for this query
2673
  @param tables               list of all tables used in this query.
2674
                              The tables have been pre-opened.
2675
  @param wild_num             number of wildcards used in the top level 
2676
                              select of this query.
2677
                              For example statement
2678
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2679
                              has 3 wildcards.
2680
  @param fields               list of items in SELECT list of the top-level
2681
                              select
2682
                              e.g. SELECT a, b, c FROM t1 will have Item_field
2683
                              for a, b and c in this list.
2684
  @param conds                top level item of an expression representing
2685
                              WHERE clause of the top level select
2686
  @param og_num               total number of ORDER BY and GROUP BY clauses
2687
                              arguments
2688
  @param order                linked list of ORDER BY agruments
2689
  @param group                linked list of GROUP BY arguments
2690
  @param having               top level item of HAVING expression
2691
  @param proc_param           list of PROCEDUREs
2692
  @param select_options       select options (BIG_RESULT, etc)
2693
  @param result               an instance of result set handling class.
2694
                              This object is responsible for send result
2695
                              set rows to the client or inserting them
2696
                              into a table.
2697
  @param select_lex           the only SELECT_LEX of this query
2698
  @param unit                 top-level UNIT of this query
2699
                              UNIT is an artificial object created by the
2700
                              parser for every SELECT clause.
2701
                              e.g.
2702
                              SELECT * FROM t1 WHERE a1 IN (SELECT * FROM t2)
2703
                              has 2 unions.
2704
2705
  @retval
2706
    FALSE  success
2707
  @retval
2708
    TRUE   an error
2709
*/
2710
2711
bool
2712
mysql_select(THD *thd, Item ***rref_pointer_array,
2713
	     TABLE_LIST *tables, uint wild_num, List<Item> &fields,
2714
	     COND *conds, uint og_num,  ORDER *order, ORDER *group,
2715
	     Item *having, ORDER *proc_param, ulonglong select_options,
2716
	     select_result *result, SELECT_LEX_UNIT *unit,
2717
	     SELECT_LEX *select_lex)
2718
{
2719
  bool err;
2720
  bool free_join= 1;
2721
  DBUG_ENTER("mysql_select");
2722
2723
  select_lex->context.resolve_in_select_list= TRUE;
2724
  JOIN *join;
2725
  if (select_lex->join != 0)
2726
  {
2727
    join= select_lex->join;
2728
    /*
2729
      is it single SELECT in derived table, called in derived table
2730
      creation
2731
    */
2732
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2733
	(select_options & SELECT_DESCRIBE))
2734
    {
2735
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2736
      {
2737
	//here is EXPLAIN of subselect or derived table
2738
	if (join->change_result(result))
2739
	{
2740
	  DBUG_RETURN(TRUE);
2741
	}
2742
      }
2743
      else
2744
      {
2745
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2746
                               conds, og_num, order, group, having, proc_param,
2747
                               select_lex, unit)))
2748
	{
2749
	  goto err;
2750
	}
2751
      }
2752
    }
2753
    free_join= 0;
2754
    join->select_options= select_options;
2755
  }
2756
  else
2757
  {
2758
    if (!(join= new JOIN(thd, fields, select_options, result)))
2759
	DBUG_RETURN(TRUE);
2760
    thd_proc_info(thd, "init");
2761
    thd->used_tables=0;                         // Updated by setup_fields
2762
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2763
                           conds, og_num, order, group, having, proc_param,
2764
                           select_lex, unit)) == true)
2765
    {
2766
      goto err;
2767
    }
2768
  }
2769
2770
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2771
  if (join->flatten_subqueries())
2772
  {
2773
    err= 1;
2774
    goto err;
2775
  }
2776
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2777
2778
  if ((err= join->optimize()))
2779
  {
2780
    goto err;					// 1
2781
  }
2782
2783
  if (thd->lex->describe & DESCRIBE_EXTENDED)
2784
  {
2785
    join->conds_history= join->conds;
2786
    join->having_history= (join->having?join->having:join->tmp_having);
2787
  }
2788
2789
  if (thd->is_error())
2790
    goto err;
2791
2792
  join->exec();
2793
2794
  if (thd->lex->describe & DESCRIBE_EXTENDED)
2795
  {
2796
    select_lex->where= join->conds_history;
2797
    select_lex->having= join->having_history;
2798
  }
2799
2800
err:
2801
  if (free_join)
2802
  {
2803
    thd_proc_info(thd, "end");
2804
    err|= select_lex->cleanup();
2805
    DBUG_RETURN(err || thd->is_error());
2806
  }
2807
  DBUG_RETURN(join->error);
2808
}
2809
2810
2811
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
2812
                          Item_in_subselect* const *el2)
2813
{
2814
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
2815
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2816
}
2817
2818
2819
inline Item * and_items(Item* cond, Item *item)
2820
{
2821
  return (cond? (new Item_cond_and(cond, item)) : item);
2822
}
2823
2824
2825
static TABLE_LIST *alloc_join_nest(THD *thd)
2826
{
2827
  TABLE_LIST *tbl;
2828
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2829
                                       sizeof(NESTED_JOIN))))
2830
    return NULL;
2831
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
2832
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
2833
  return tbl;
2834
}
2835
2836
2837
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2838
{
2839
  List_iterator<TABLE_LIST> it(*tlist);
2840
  TABLE_LIST *table;
2841
  while ((table= it++))
2842
  {
2843
    if (table->on_expr)
2844
      table->on_expr->fix_after_pullout(new_parent, &table->on_expr);
2845
    if (table->nested_join)
2846
      fix_list_after_tbl_changes(new_parent, &table->nested_join->join_list);
2847
  }
2848
}
2849
2850
2851
/*
2852
  Convert a subquery predicate into a TABLE_LIST semi-join nest
2853
2854
  SYNOPSIS
2855
    convert_subq_to_sj()
2856
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
2857
                    clause
2858
       subq_pred    Subquery predicate to be converted
2859
  
2860
  DESCRIPTION
2861
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
2862
    prerequisites are already checked, so the conversion is always successfull.
2863
2864
    Prepared Statements: the transformation is permanent:
2865
     - Changes in TABLE_LIST structures are naturally permanent
2866
     - Item tree changes are performed on statement MEM_ROOT:
2867
        = we activate statement MEM_ROOT 
2868
        = this function is called before the first fix_prepare_information
2869
          call.
2870
2871
    This is intended because the criteria for subquery-to-sj conversion remain
2872
    constant for the lifetime of the Prepared Statement.
2873
2874
  RETURN
2875
    FALSE  OK
2876
    TRUE   Out of memory error
2877
*/
2878
2879
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2880
{
2881
  SELECT_LEX *parent_lex= parent_join->select_lex;
2882
  TABLE_LIST *emb_tbl_nest= NULL;
2883
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2884
  THD *thd= parent_join->thd;
2885
  DBUG_ENTER("convert_subq_to_sj");
2886
2887
  /*
2888
    1. Find out where to put the predicate into.
2889
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2890
  */
2891
  if ((void*)subq_pred->expr_join_nest != (void*)1)
2892
  {
2893
    if (subq_pred->expr_join_nest->nested_join)
2894
    {
2895
      /*
2896
        We're dealing with
2897
2898
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
2899
2900
        The sj-nest will be inserted into the brackets nest.
2901
      */
2902
      emb_tbl_nest=  subq_pred->expr_join_nest;
2903
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
2904
    }
2905
    else if (!subq_pred->expr_join_nest->outer_join)
2906
    {
2907
      /*
2908
        We're dealing with
2909
2910
          ... INNER JOIN tblX ON (subquery AND whatever) ...
2911
2912
        The sj-nest will be tblX's "sibling", i.e. another child of its
2913
        parent. This is ok because tblX is joined as an inner join.
2914
      */
2915
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2916
      if (emb_tbl_nest)
2917
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
2918
    }
2919
    else if (!subq_pred->expr_join_nest->nested_join)
2920
    {
2921
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
2922
      TABLE_LIST *wrap_nest;
2923
      /*
2924
        We're dealing with
2925
2926
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2927
2928
        we'll need to convert it into:
2929
2930
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2931
                        |                      |
2932
                        |<----- wrap_nest ---->|
2933
        
2934
        Q:  other subqueries may be pointing to this element. What to do?
2935
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2936
            But we'll need to fix other pointers.
2937
        A2: Another way: have TABLE_LIST::next_ptr so the following
2938
            subqueries know the table has been nested.
2939
        A3: changes in the TABLE_LIST::outer_join will make everything work
2940
            automatically.
2941
      */
2942
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2943
      {
2944
        DBUG_RETURN(TRUE);
2945
      }
2946
      wrap_nest->embedding= outer_tbl->embedding;
2947
      wrap_nest->join_list= outer_tbl->join_list;
2948
      wrap_nest->alias= (char*) "(sj-wrap)";
2949
2950
      wrap_nest->nested_join->join_list.empty();
2951
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
2952
2953
      outer_tbl->embedding= wrap_nest;
2954
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2955
2956
      /*
2957
        wrap_nest will take place of outer_tbl, so move the outer join flag
2958
        and on_expr
2959
      */
2960
      wrap_nest->outer_join= outer_tbl->outer_join;
2961
      outer_tbl->outer_join= 0;
2962
2963
      wrap_nest->on_expr= outer_tbl->on_expr;
2964
      outer_tbl->on_expr= NULL;
2965
2966
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2967
      TABLE_LIST *tbl;
2968
      while ((tbl= li++))
2969
      {
2970
        if (tbl == outer_tbl)
2971
        {
2972
          li.replace(wrap_nest);
2973
          break;
2974
        }
2975
      }
2976
      /*
2977
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
2978
        semi-join nest into it
2979
      */
2980
      emb_join_list= &wrap_nest->nested_join->join_list;
2981
      emb_tbl_nest=  wrap_nest;
2982
    }
2983
  }
2984
2985
  TABLE_LIST *sj_nest;
2986
  NESTED_JOIN *nested_join;
2987
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2988
  {
2989
    DBUG_RETURN(TRUE);
2990
  }
2991
  nested_join= sj_nest->nested_join;
2992
2993
  sj_nest->join_list= emb_join_list;
2994
  sj_nest->embedding= emb_tbl_nest;
2995
  sj_nest->alias= (char*) "(sj-nest)";
2996
  /* Nests do not participate in those 'chains', so: */
2997
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2998
  emb_join_list->push_back(sj_nest);
2999
3000
  /* 
3001
    nested_join->used_tables and nested_join->not_null_tables are
3002
    initialized in simplify_joins().
3003
  */
3004
  
3005
  /* 
3006
    2. Walk through subquery's top list and set 'embedding' to point to the
3007
       sj-nest.
3008
  */
3009
  st_select_lex *subq_lex= subq_pred->unit->first_select();
3010
  nested_join->join_list.empty();
3011
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
3012
  TABLE_LIST *tl, *last_leaf;
3013
  while ((tl= li++))
3014
  {
3015
    tl->embedding= sj_nest;
3016
    tl->join_list= &nested_join->join_list;
3017
    nested_join->join_list.push_back(tl);
3018
  }
3019
  
3020
  /*
3021
    Reconnect the next_leaf chain.
3022
    TODO: Do we have to put subquery's tables at the end of the chain?
3023
          Inserting them at the beginning would be a bit faster.
3024
    NOTE: We actually insert them at the front! That's because the order is
3025
          reversed in this list.
3026
  */
3027
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
3028
  tl->next_leaf= subq_lex->leaf_tables;
3029
  last_leaf= tl;
3030
3031
  /*
3032
    Same as above for next_local chain
3033
    (a theory: a next_local chain always starts with ::leaf_tables
3034
     because view's tables are inserted after the view)
3035
  */
3036
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
3037
  tl->next_local= subq_lex->leaf_tables;
3038
3039
  /* A theory: no need to re-connect the next_global chain */
3040
3041
  /* 3. Remove the original subquery predicate from the WHERE/ON */
3042
3043
  // The subqueries were replaced for Item_int(1) earlier
3044
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
3045
  /*TODO: also reset the 'with_subselect' there. */
3046
3047
  /* n. Adjust the parent_join->tables counter */
3048
  uint table_no= parent_join->tables;
3049
  /* n. Walk through child's tables and adjust table->map */
3050
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
3051
  {
3052
    tl->table->tablenr= table_no;
3053
    tl->table->map= ((table_map)1) << table_no;
3054
    SELECT_LEX *old_sl= tl->select_lex;
3055
    tl->select_lex= parent_join->select_lex; 
3056
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
3057
      emb->select_lex= parent_join->select_lex;
3058
  }
3059
  parent_join->tables += subq_lex->join->tables;
3060
3061
  /* 
3062
    Put the subquery's WHERE into semi-join's sj_on_expr
3063
    Add the subquery-induced equalities too.
3064
  */
3065
  SELECT_LEX *save_lex= thd->lex->current_select;
3066
  thd->lex->current_select=subq_lex;
3067
  if (!subq_pred->left_expr->fixed &&
3068
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
3069
    DBUG_RETURN(TRUE);
3070
  thd->lex->current_select=save_lex;
3071
3072
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
3073
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
3074
                                        subq_pred->left_expr->used_tables();
3075
  sj_nest->sj_on_expr= subq_lex->where;
3076
3077
  /*
3078
    Create the IN-equalities and inject them into semi-join's ON expression.
3079
    Additionally, for InsideOut strategy
3080
     - Record the number of IN-equalities.
3081
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
3082
       see which of the expressions are bound and which are not (for those
3083
       we'll produce a distinct stream of (ie_i1,...ie_ik).
3084
3085
       (TODO: can we just create a list of pointers and hope the expressions
3086
       will not substitute themselves on fix_fields()? or we need to wrap
3087
       them into Item_direct_view_refs and store pointers to those. The
3088
       pointers to Item_direct_view_refs are guaranteed to be stable as 
3089
       Item_direct_view_refs doesn't substitute itself with anything in 
3090
       Item_direct_view_ref::fix_fields.
3091
  */
3092
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3093
  sj_nest->nested_join->sj_outer_expr_list.empty();
3094
3095
  if (subq_pred->left_expr->cols() == 1)
3096
  {
3097
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3098
3099
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
3100
                                    subq_lex->ref_pointer_array[0]);
3101
    item_eq->name= (char*)subq_sj_cond_name;
3102
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3103
  }
3104
  else
3105
  {
3106
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3107
    {
3108
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3109
                                                element_index(i));
3110
      Item *item_eq= 
3111
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
3112
                         subq_lex->ref_pointer_array[i]);
3113
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3114
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3115
    }
3116
  }
3117
  /* Fix the created equality and AND */
3118
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3119
3120
  /*
3121
    Walk through sj nest's WHERE and ON expressions and call
3122
    item->fix_table_changes() for all items.
3123
  */
3124
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3125
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3126
3127
3128
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3129
  subq_lex->master_unit()->exclude_level();
3130
3131
  DBUG_EXECUTE("where",
3132
               print_where(sj_nest->sj_on_expr,"SJ-EXPR", QT_ORDINARY););
3133
3134
  /* Inject sj_on_expr into the parent's WHERE or ON */
3135
  if (emb_tbl_nest)
3136
  {
3137
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
3138
                                     sj_nest->sj_on_expr);
3139
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3140
  }
3141
  else
3142
  {
3143
    /* Inject into the WHERE */
3144
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3145
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3146
    parent_join->select_lex->where= parent_join->conds;
3147
  }
3148
3149
  DBUG_RETURN(FALSE);
3150
}
3151
3152
3153
/*
3154
  Convert candidate subquery predicates to semi-joins
3155
3156
  SYNOPSIS
3157
    JOIN::flatten_subqueries()
3158
 
3159
  DESCRIPTION
3160
    Convert candidate subquery predicates to semi-joins.
3161
3162
  RETURN 
3163
    FALSE  OK
3164
    TRUE   Error
3165
*/
3166
3167
bool JOIN::flatten_subqueries()
3168
{
3169
  Item_in_subselect **in_subq;
3170
  Item_in_subselect **in_subq_end;
3171
  DBUG_ENTER("JOIN::flatten_subqueries");
3172
3173
  if (sj_subselects.elements() == 0)
3174
    DBUG_RETURN(FALSE);
3175
3176
  /* 1. Fix children subqueries */
3177
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
3178
       in_subq != in_subq_end; in_subq++)
3179
  {
3180
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3181
    child_join->outer_tables = child_join->tables;
3182
    if (child_join->flatten_subqueries())
3183
      DBUG_RETURN(TRUE);
3184
    (*in_subq)->sj_convert_priority= 
3185
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3186
  }
3187
3188
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3189
  /* 
3190
    2. Pick which subqueries to convert:
3191
      sort the subquery array
3192
      - prefer correlated subqueries over uncorrelated;
3193
      - prefer subqueries that have greater number of outer tables;
3194
  */
3195
  sj_subselects.sort(subq_sj_candidate_cmp);
3196
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3197
  /* Replace all subqueries to be flattened with Item_int(1) */
3198
  for (in_subq= sj_subselects.front(); 
3199
       in_subq != in_subq_end && 
3200
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3201
       in_subq++)
3202
  {
3203
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), FALSE))
3204
      DBUG_RETURN(TRUE);
3205
  }
3206
 
3207
  for (in_subq= sj_subselects.front(); 
3208
       in_subq != in_subq_end && 
3209
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3210
       in_subq++)
3211
  {
3212
    if (convert_subq_to_sj(this, *in_subq))
3213
      DBUG_RETURN(TRUE);
3214
  }
3215
3216
  /* 3. Finalize those we didn't convert */
3217
  for (; in_subq!= in_subq_end; in_subq++)
3218
  {
3219
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3220
    Item_subselect::trans_res res;
3221
    (*in_subq)->changed= 0;
3222
    (*in_subq)->fixed= 0;
3223
    res= (*in_subq)->select_transformer(child_join);
3224
    if (res == Item_subselect::RES_ERROR)
3225
      DBUG_RETURN(TRUE);
3226
3227
    (*in_subq)->changed= 1;
3228
    (*in_subq)->fixed= 1;
3229
3230
    Item *substitute= (*in_subq)->substitution;
3231
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3232
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3233
      DBUG_RETURN(TRUE);
3234
3235
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3236
    //  DBUG_RETURN(TRUE);
3237
  }
3238
  sj_subselects.clear();
3239
  DBUG_RETURN(FALSE);
3240
}
3241
3242
3243
/**
3244
  Setup for execution all subqueries of a query, for which the optimizer
3245
  chose hash semi-join.
3246
3247
  @details Iterate over all subqueries of the query, and if they are under an
3248
  IN predicate, and the optimizer chose to compute it via hash semi-join:
3249
  - try to initialize all data structures needed for the materialized execution
3250
    of the IN predicate,
3251
  - if this fails, then perform the IN=>EXISTS transformation which was
3252
    previously blocked during JOIN::prepare.
3253
3254
  This method is part of the "code generation" query processing phase.
3255
3256
  This phase must be called after substitute_for_best_equal_field() because
3257
  that function may replace items with other items from a multiple equality,
3258
  and we need to reference the correct items in the index access method of the
3259
  IN predicate.
3260
3261
  @return Operation status
3262
  @retval FALSE     success.
3263
  @retval TRUE      error occurred.
3264
*/
3265
3266
bool JOIN::setup_subquery_materialization()
3267
{
3268
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3269
       un= un->next_unit())
3270
  {
3271
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3272
    {
3273
      Item_subselect *subquery_predicate= sl->master_unit()->item;
3274
      if (subquery_predicate &&
3275
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
3276
      {
3277
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3278
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3279
            in_subs->setup_engine())
3280
          return TRUE;
3281
      }
3282
    }
3283
  }
3284
  return FALSE;
3285
}
3286
3287
3288
/*
3289
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3290
3291
  SYNOPSIS
3292
    find_eq_ref_candidate()
3293
      table             Table to be checked
3294
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
3295
                        count.
3296
3297
  DESCRIPTION
3298
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3299
3300
  TODO
3301
    Check again if it is feasible to factor common parts with constant table
3302
    search
3303
3304
  RETURN
3305
    TRUE  - There exists an eq_ref(outer-tables) candidate
3306
    FALSE - Otherwise
3307
*/
3308
3309
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3310
{
3311
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3312
  uint key;
3313
3314
  if (keyuse)
3315
  {
3316
    while (1) /* For each key */
3317
    {
3318
      key= keyuse->key;
3319
      KEY *keyinfo= table->key_info + key;
3320
      key_part_map bound_parts= 0;
3321
      if ((keyinfo->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME)
3322
      {
3323
        do  /* For all equalities on all key parts */
3324
        {
3325
          /* Check if this is "t.keypart = expr(outer_tables) */
3326
          if (!(keyuse->used_tables & sj_inner_tables) &&
3327
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3328
          {
3329
            bound_parts |= 1 << keyuse->keypart;
3330
          }
3331
          keyuse++;
3332
        } while (keyuse->key == key && keyuse->table == table);
3333
3334
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3335
          return TRUE;
3336
        if (keyuse->table != table)
3337
          return FALSE;
3338
      }
3339
      else
3340
      {
3341
        do
3342
        {
3343
          keyuse++;
3344
          if (keyuse->table != table)
3345
            return FALSE;
3346
        }
3347
        while (keyuse->key == key);
3348
      }
3349
    }
3350
  }
3351
  return FALSE;
3352
}
3353
3354
3355
/*
3356
  Pull tables out of semi-join nests, if possible
3357
3358
  SYNOPSIS
3359
    pull_out_semijoin_tables()
3360
      join  The join where to do the semi-join flattening
3361
3362
  DESCRIPTION
3363
    Try to pull tables out of semi-join nests.
3364
     
3365
    PRECONDITIONS
3366
    When this function is called, the join may have several semi-join nests
3367
    (possibly within different semi-join nests), but it is guaranteed that
3368
    one semi-join nest does not contain another.
3369
   
3370
    ACTION
3371
    A table can be pulled out of the semi-join nest if
3372
     - It is a constant table
3373
     - It is accessed 
3374
3375
    POSTCONDITIONS
3376
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3377
       tables)
3378
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3379
     * Semi-join nests TABLE_LIST::sj_inner_tables
3380
3381
    This operation is (and should be) performed at each PS execution since
3382
    tables may become/cease to be constant across PS reexecutions.
3383
3384
  RETURN 
3385
    0 - OK
3386
    1 - Out of memory error
3387
*/
3388
3389
int pull_out_semijoin_tables(JOIN *join)
3390
{
3391
  TABLE_LIST *sj_nest;
3392
  DBUG_ENTER("pull_out_semijoin_tables");
3393
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3394
   
3395
  /* Try pulling out of the each of the semi-joins */
3396
  while ((sj_nest= sj_list_it++))
3397
  {
3398
    /* Action #1: Mark the constant tables to be pulled out */
3399
    table_map pulled_tables= 0;
3400
     
3401
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3402
    TABLE_LIST *tbl;
3403
    while ((tbl= child_li++))
3404
    {
3405
      if (tbl->table)
3406
      {
3407
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3408
        if (tbl->table->map & join->const_table_map)
3409
        {
3410
          pulled_tables |= tbl->table->map;
3411
          DBUG_PRINT("info", ("Table %s pulled out (reason: constant)",
3412
                              tbl->table->alias));
3413
        }
3414
      }
3415
    }
3416
    
3417
    /*
3418
      Action #2: Find which tables we can pull out based on
3419
      update_ref_and_keys() data. Note that pulling one table out can allow
3420
      us to pull out some other tables too.
3421
    */
3422
    bool pulled_a_table;
3423
    do 
3424
    {
3425
      pulled_a_table= FALSE;
3426
      child_li.rewind();
3427
      while ((tbl= child_li++))
3428
      {
3429
        if (tbl->table && !(pulled_tables & tbl->table->map))
3430
        {
3431
          if (find_eq_ref_candidate(tbl->table, 
3432
                                    sj_nest->nested_join->used_tables & 
3433
                                    ~pulled_tables))
3434
          {
3435
            pulled_a_table= TRUE;
3436
            pulled_tables |= tbl->table->map;
3437
            DBUG_PRINT("info", ("Table %s pulled out (reason: func dep)",
3438
                                tbl->table->alias));
3439
          }
3440
        }
3441
      }
3442
    } while (pulled_a_table);
3443
 
3444
    child_li.rewind();
3445
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3446
    {
3447
      (sj_nest)->sj_inner_tables= 0;
3448
      DBUG_PRINT("info", ("All semi-join nest tables were pulled out"));
3449
      while ((tbl= child_li++))
3450
      {
3451
        if (tbl->table)
3452
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3453
      }
3454
    }
3455
    else
3456
    {
3457
      /* Record the bitmap of inner tables, mark the inner tables */
3458
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
3459
                             ~pulled_tables;
3460
      (sj_nest)->sj_inner_tables= inner_tables;
3461
      while ((tbl= child_li++))
3462
      {
3463
        if (tbl->table)
3464
        {
3465
          if (inner_tables & tbl->table->map)
3466
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3467
          else
3468
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3469
        }
3470
      }
3471
    }
3472
  }
3473
  DBUG_RETURN(0);
3474
}
3475
3476
/*****************************************************************************
3477
  Create JOIN_TABS, make a guess about the table types,
3478
  Approximate how many records will be used in each table
3479
*****************************************************************************/
3480
3481
3482
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3483
				      TABLE *table,
3484
				      const key_map *keys,ha_rows limit)
3485
{
3486
  int error;
3487
  DBUG_ENTER("get_quick_record_count");
3488
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
3489
    DBUG_RETURN(0);                           // Fatal error flag is set
3490
  if (select)
3491
  {
3492
    select->head=table;
3493
    table->reginfo.impossible_range=0;
3494
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
3495
                                          limit, 0, FALSE)) == 1)
3496
      DBUG_RETURN(select->quick->records);
3497
    if (error == -1)
3498
    {
3499
      table->reginfo.impossible_range=1;
3500
      DBUG_RETURN(0);
3501
    }
3502
    DBUG_PRINT("warning",("Couldn't use record count on const keypart"));
3503
  }
3504
  DBUG_RETURN(HA_POS_ERROR);			/* This shouldn't happend */
3505
}
3506
3507
/*
3508
   This structure is used to collect info on potentially sargable
3509
   predicates in order to check whether they become sargable after
3510
   reading const tables.
3511
   We form a bitmap of indexes that can be used for sargable predicates.
3512
   Only such indexes are involved in range analysis.
3513
*/
3514
typedef struct st_sargable_param
3515
{
3516
  Field *field;              /* field against which to check sargability */
3517
  Item **arg_value;          /* values of potential keys for lookups     */
3518
  uint num_values;           /* number of values in the above array      */
3519
} SARGABLE_PARAM;  
3520
3521
/**
3522
  Calculate the best possible join and initialize the join structure.
3523
3524
  @retval
3525
    0	ok
3526
  @retval
3527
    1	Fatal error
3528
*/
3529
3530
static bool
3531
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3532
		     DYNAMIC_ARRAY *keyuse_array)
3533
{
3534
  int error;
3535
  TABLE *table;
3536
  uint i,table_count,const_count,key;
3537
  table_map found_const_table_map, all_table_map, found_ref, refs;
3538
  key_map const_ref, eq_part;
3539
  TABLE **table_vector;
3540
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3541
  KEYUSE *keyuse,*start_keyuse;
3542
  table_map outer_join=0;
3543
  SARGABLE_PARAM *sargables= 0;
3544
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3545
  DBUG_ENTER("make_join_statistics");
3546
3547
  table_count=join->tables;
3548
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3549
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3550
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3551
  if (!stat || !stat_ref || !table_vector)
3552
    DBUG_RETURN(1);				// Eom /* purecov: inspected */
3553
3554
  join->best_ref=stat_vector;
3555
3556
  stat_end=stat+table_count;
3557
  found_const_table_map= all_table_map=0;
3558
  const_count=0;
3559
3560
  for (s= stat, i= 0;
3561
       tables;
3562
       s++, tables= tables->next_leaf, i++)
3563
  {
3564
    TABLE_LIST *embedding= tables->embedding;
3565
    stat_vector[i]=s;
3566
    s->keys.init();
3567
    s->const_keys.init();
3568
    s->checked_keys.init();
3569
    s->needed_reg.init();
3570
    table_vector[i]=s->table=table=tables->table;
3571
    table->pos_in_table_list= tables;
3572
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3573
    if(error)
3574
    {
3575
        table->file->print_error(error, MYF(0));
3576
        DBUG_RETURN(1);
3577
    }
3578
    table->quick_keys.clear_all();
3579
    table->reginfo.join_tab=s;
3580
    table->reginfo.not_exists_optimize=0;
3581
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3582
    all_table_map|= table->map;
3583
    s->join=join;
3584
    s->info=0;					// For describe
3585
3586
    s->dependent= tables->dep_tables;
3587
    s->key_dependent= 0;
3588
    if (tables->schema_table)
3589
      table->file->stats.records= 2;
3590
    table->quick_condition_rows= table->file->stats.records;
3591
3592
    s->on_expr_ref= &tables->on_expr;
3593
    if (*s->on_expr_ref)
3594
    {
3595
      /* s is the only inner table of an outer join */
3596
      if (!table->file->stats.records && !embedding)
3597
      {						// Empty table
3598
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
3599
	set_position(join,const_count++,s,(KEYUSE*) 0);
3600
	continue;
3601
      }
3602
      outer_join|= table->map;
3603
      s->embedding_map= 0;
3604
      for (;embedding; embedding= embedding->embedding)
3605
        s->embedding_map|= embedding->nested_join->nj_map;
3606
      continue;
3607
    }
3608
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3609
    {
3610
      /* s belongs to a nested join, maybe to several embedded joins */
3611
      s->embedding_map= 0;
3612
      do
3613
      {
3614
        NESTED_JOIN *nested_join= embedding->nested_join;
3615
        s->embedding_map|=nested_join->nj_map;
3616
        s->dependent|= embedding->dep_tables;
3617
        embedding= embedding->embedding;
3618
        outer_join|= nested_join->used_tables;
3619
      }
3620
      while (embedding);
3621
      continue;
3622
    }
3623
    if ((table->s->system || table->file->stats.records <= 1) &&
3624
	!s->dependent &&
3625
	(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3626
        !table->fulltext_searched && !join->no_const_tables)
3627
    {
3628
      set_position(join,const_count++,s,(KEYUSE*) 0);
3629
    }
3630
  }
3631
  stat_vector[i]=0;
3632
  join->outer_join=outer_join;
3633
3634
  if (join->outer_join)
3635
  {
3636
    /* 
3637
       Build transitive closure for relation 'to be dependent on'.
3638
       This will speed up the plan search for many cases with outer joins,
3639
       as well as allow us to catch illegal cross references/
3640
       Warshall's algorithm is used to build the transitive closure.
3641
       As we use bitmaps to represent the relation the complexity
3642
       of the algorithm is O((number of tables)^2). 
3643
    */
3644
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3645
    {
3646
      for (uint j= 0 ; j < table_count ; j++)
3647
      {
3648
        table= stat[j].table;
3649
        if (s->dependent & table->map)
3650
          s->dependent |= table->reginfo.join_tab->dependent;
3651
      }
3652
      if (s->dependent)
3653
        s->table->maybe_null= 1;
3654
    }
3655
    /* Catch illegal cross references for outer joins */
3656
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3657
    {
3658
      if (s->dependent & s->table->map)
3659
      {
3660
        join->tables=0;			// Don't use join->table
3661
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3662
        DBUG_RETURN(1);
3663
      }
3664
      s->key_dependent= s->dependent;
3665
    }
3666
  }
3667
3668
  if (conds || outer_join)
3669
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3670
                            conds, join->cond_equal,
3671
                            ~outer_join, join->select_lex, &sargables))
3672
      DBUG_RETURN(1);
3673
3674
  /* Read tables with 0 or 1 rows (system tables) */
3675
  join->const_table_map= 0;
3676
3677
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3678
       p_pos < p_end ;
3679
       p_pos++)
3680
  {
3681
    int tmp;
3682
    s= p_pos->table;
3683
    s->type=JT_SYSTEM;
3684
    join->const_table_map|=s->table->map;
3685
    if ((tmp=join_read_const_table(s, p_pos)))
3686
    {
3687
      if (tmp > 0)
3688
	DBUG_RETURN(1);			// Fatal error
3689
    }
3690
    else
3691
      found_const_table_map|= s->table->map;
3692
  }
3693
3694
  /* loop until no more const tables are found */
3695
  int ref_changed;
3696
  do
3697
  {
3698
  more_const_tables_found:
3699
    ref_changed = 0;
3700
    found_ref=0;
3701
3702
    /*
3703
      We only have to loop from stat_vector + const_count as
3704
      set_position() will move all const_tables first in stat_vector
3705
    */
3706
3707
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3708
    {
3709
      table=s->table;
3710
3711
      /* 
3712
        If equi-join condition by a key is null rejecting and after a
3713
        substitution of a const table the key value happens to be null
3714
        then we can state that there are no matches for this equi-join.
3715
      */  
3716
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3717
      {
3718
        /* 
3719
          When performing an outer join operation if there are no matching rows
3720
          for the single row of the outer table all the inner tables are to be
3721
          null complemented and thus considered as constant tables.
3722
          Here we apply this consideration to the case of outer join operations 
3723
          with a single inner table only because the case with nested tables
3724
          would require a more thorough analysis.
3725
          TODO. Apply single row substitution to null complemented inner tables
3726
          for nested outer join operations. 
3727
	*/              
3728
        while (keyuse->table == table)
3729
        {
3730
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3731
              keyuse->val->is_null() && keyuse->null_rejecting)
3732
          {
3733
            s->type= JT_CONST;
3734
            mark_as_null_row(table);
3735
            found_const_table_map|= table->map;
3736
	    join->const_table_map|= table->map;
3737
	    set_position(join,const_count++,s,(KEYUSE*) 0);
3738
            goto more_const_tables_found;
3739
           }
3740
	  keyuse++;
3741
        }
3742
      }
3743
3744
      if (s->dependent)				// If dependent on some table
3745
      {
3746
	// All dep. must be constants
3747
	if (s->dependent & ~(found_const_table_map))
3748
	  continue;
3749
	if (table->file->stats.records <= 1L &&
3750
	    (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3751
            !table->pos_in_table_list->embedding)
3752
	{					// system table
3753
	  int tmp= 0;
3754
	  s->type=JT_SYSTEM;
3755
	  join->const_table_map|=table->map;
3756
	  set_position(join,const_count++,s,(KEYUSE*) 0);
3757
	  if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3758
	  {
3759
	    if (tmp > 0)
3760
	      DBUG_RETURN(1);			// Fatal error
3761
	  }
3762
	  else
3763
	    found_const_table_map|= table->map;
3764
	  continue;
3765
	}
3766
      }
3767
      /* check if table can be read by key or table only uses const refs */
3768
      if ((keyuse=s->keyuse))
3769
      {
3770
	s->type= JT_REF;
3771
	while (keyuse->table == table)
3772
	{
3773
	  start_keyuse=keyuse;
3774
	  key=keyuse->key;
3775
	  s->keys.set_bit(key);               // QQ: remove this ?
3776
3777
	  refs=0;
3778
          const_ref.clear_all();
3779
	  eq_part.clear_all();
3780
	  do
3781
	  {
3782
	    if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3783
	    {
3784
	      if (!((~found_const_table_map) & keyuse->used_tables))
3785
		const_ref.set_bit(keyuse->keypart);
3786
	      else
3787
		refs|=keyuse->used_tables;
3788
	      eq_part.set_bit(keyuse->keypart);
3789
	    }
3790
	    keyuse++;
3791
	  } while (keyuse->table == table && keyuse->key == key);
3792
3793
	  if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3794
              !table->fulltext_searched && 
3795
              !table->pos_in_table_list->embedding)
3796
	  {
3797
            if ((table->key_info[key].flags & (HA_NOSAME | HA_END_SPACE_KEY))
3798
                 == HA_NOSAME)
3799
            {
3800
	      if (const_ref == eq_part)
3801
	      {					// Found everything for ref.
3802
	        int tmp;
3803
	        ref_changed = 1;
3804
	        s->type= JT_CONST;
3805
	        join->const_table_map|=table->map;
3806
	        set_position(join,const_count++,s,start_keyuse);
3807
	        if (create_ref_for_key(join, s, start_keyuse,
3808
				       found_const_table_map))
3809
		  DBUG_RETURN(1);
3810
	        if ((tmp=join_read_const_table(s,
3811
                                               join->positions+const_count-1)))
3812
	        {
3813
		  if (tmp > 0)
3814
		    DBUG_RETURN(1);			// Fatal error
3815
	        }
3816
	        else
3817
		  found_const_table_map|= table->map;
3818
	        break;
3819
	      }
3820
	      else
3821
	        found_ref|= refs;      // Table is const if all refs are const
3822
	    }
3823
            else if (const_ref == eq_part)
3824
              s->const_keys.set_bit(key);
3825
          }
3826
	}
3827
      }
3828
    }
3829
  } while (join->const_table_map & found_ref && ref_changed);
3830
 
3831
  /* 
3832
    Update info on indexes that can be used for search lookups as
3833
    reading const tables may has added new sargable predicates. 
3834
  */
3835
  if (const_count && sargables)
3836
  {
3837
    for( ; sargables->field ; sargables++)
3838
    {
3839
      Field *field= sargables->field;
3840
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3841
      key_map possible_keys= field->key_start;
3842
      possible_keys.intersect(field->table->keys_in_use_for_query);
3843
      bool is_const= 1;
3844
      for (uint j=0; j < sargables->num_values; j++)
3845
        is_const&= sargables->arg_value[j]->const_item();
3846
      if (is_const)
3847
        join_tab[0].const_keys.merge(possible_keys);
3848
    }
3849
  }
3850
3851
  if (pull_out_semijoin_tables(join))
3852
    DBUG_RETURN(TRUE);
3853
3854
  /* Calc how many (possible) matched records in each table */
3855
3856
  for (s=stat ; s < stat_end ; s++)
3857
  {
3858
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
3859
    {
3860
      /* Only one matching row */
3861
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3862
      continue;
3863
    }
3864
    /* Approximate found rows and time to read them */
3865
    s->found_records=s->records=s->table->file->stats.records;
3866
    s->read_time=(ha_rows) s->table->file->scan_time();
3867
3868
    /*
3869
      Set a max range of how many seeks we can expect when using keys
3870
      This is can't be to high as otherwise we are likely to use
3871
      table scan.
3872
    */
3873
    s->worst_seeks= min((double) s->found_records / 10,
3874
			(double) s->read_time*3);
3875
    if (s->worst_seeks < 2.0)			// Fix for small tables
3876
      s->worst_seeks=2.0;
3877
3878
    /*
3879
      Add to stat->const_keys those indexes for which all group fields or
3880
      all select distinct fields participate in one index.
3881
    */
3882
    add_group_and_distinct_keys(join, s);
3883
3884
    if (!s->const_keys.is_clear_all() &&
3885
        !s->table->pos_in_table_list->embedding)
3886
    {
3887
      ha_rows records;
3888
      SQL_SELECT *select;
3889
      select= make_select(s->table, found_const_table_map,
3890
			  found_const_table_map,
3891
			  *s->on_expr_ref ? *s->on_expr_ref : conds,
3892
			  1, &error);
3893
      if (!select)
3894
        DBUG_RETURN(1);
3895
      records= get_quick_record_count(join->thd, select, s->table,
3896
				      &s->const_keys, join->row_limit);
3897
      s->quick=select->quick;
3898
      s->needed_reg=select->needed_reg;
3899
      select->quick=0;
3900
      if (records == 0 && s->table->reginfo.impossible_range)
3901
      {
3902
	/*
3903
	  Impossible WHERE or ON expression
3904
	  In case of ON, we mark that the we match one empty NULL row.
3905
	  In case of WHERE, don't set found_const_table_map to get the
3906
	  caller to abort with a zero row result.
3907
	*/
3908
	join->const_table_map|= s->table->map;
3909
	set_position(join,const_count++,s,(KEYUSE*) 0);
3910
	s->type= JT_CONST;
3911
	if (*s->on_expr_ref)
3912
	{
3913
	  /* Generate empty row */
3914
	  s->info= "Impossible ON condition";
3915
	  found_const_table_map|= s->table->map;
3916
	  s->type= JT_CONST;
3917
	  mark_as_null_row(s->table);		// All fields are NULL
3918
	}
3919
      }
3920
      if (records != HA_POS_ERROR)
3921
      {
3922
	s->found_records=records;
3923
	s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3924
      }
3925
      delete select;
3926
    }
3927
  }
3928
3929
  join->join_tab=stat;
3930
  join->map2table=stat_ref;
3931
  join->table= join->all_tables=table_vector;
3932
  join->const_tables=const_count;
3933
  join->found_const_table_map=found_const_table_map;
3934
3935
  /* Find an optimal join order of the non-constant tables. */
3936
  if (join->const_tables != join->tables)
3937
  {
3938
    optimize_keyuse(join, keyuse_array);
3939
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3940
      DBUG_RETURN(TRUE);
3941
  }
3942
  else
3943
  {
3944
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
3945
	   sizeof(POSITION)*join->const_tables);
3946
    join->best_read=1.0;
3947
  }
3948
  /* Generate an execution plan from the found optimal join order. */
3949
  DBUG_RETURN(join->thd->killed || get_best_combination(join));
3950
}
3951
3952
3953
/*****************************************************************************
3954
  Check with keys are used and with tables references with tables
3955
  Updates in stat:
3956
	  keys	     Bitmap of all used keys
3957
	  const_keys Bitmap of all keys with may be used with quick_select
3958
	  keyuse     Pointer to possible keys
3959
*****************************************************************************/
3960
3961
/// Used when finding key fields
3962
typedef struct key_field_t {
3963
  Field		*field;
3964
  Item		*val;			///< May be empty if diff constant
3965
  uint		level;
3966
  uint		optimize; // KEY_OPTIMIZE_*
3967
  bool		eq_func;
3968
  /**
3969
    If true, the condition this struct represents will not be satisfied
3970
    when val IS NULL.
3971
  */
3972
  bool          null_rejecting; 
3973
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3974
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3975
} KEY_FIELD;
3976
3977
/**
3978
  Merge new key definitions to old ones, remove those not used in both.
3979
3980
  This is called for OR between different levels.
3981
3982
  To be able to do 'ref_or_null' we merge a comparison of a column
3983
  and 'column IS NULL' to one test.  This is useful for sub select queries
3984
  that are internally transformed to something like:.
3985
3986
  @code
3987
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
3988
  @endcode
3989
3990
  KEY_FIELD::null_rejecting is processed as follows: @n
3991
  result has null_rejecting=true if it is set for both ORed references.
3992
  for example:
3993
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
3994
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3995
3996
  @todo
3997
    The result of this is that we're missing some 'ref' accesses.
3998
    OptimizerTeam: Fix this
3999
*/
4000
4001
static KEY_FIELD *
4002
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
4003
		 uint and_level)
4004
{
4005
  if (start == new_fields)
4006
    return start;				// Impossible or
4007
  if (new_fields == end)
4008
    return start;				// No new fields, skip all
4009
4010
  KEY_FIELD *first_free=new_fields;
4011
4012
  /* Mark all found fields in old array */
4013
  for (; new_fields != end ; new_fields++)
4014
  {
4015
    for (KEY_FIELD *old=start ; old != first_free ; old++)
4016
    {
4017
      if (old->field == new_fields->field)
4018
      {
4019
        /*
4020
          NOTE: below const_item() call really works as "!used_tables()", i.e.
4021
          it can return FALSE where it is feasible to make it return TRUE.
4022
          
4023
          The cause is as follows: Some of the tables are already known to be
4024
          const tables (the detection code is in make_join_statistics(),
4025
          above the update_ref_and_keys() call), but we didn't propagate 
4026
          information about this: TABLE::const_table is not set to TRUE, and
4027
          Item::update_used_tables() hasn't been called for each item.
4028
          The result of this is that we're missing some 'ref' accesses.
4029
          TODO: OptimizerTeam: Fix this
4030
        */
4031
	if (!new_fields->val->const_item())
4032
	{
4033
	  /*
4034
	    If the value matches, we can use the key reference.
4035
	    If not, we keep it until we have examined all new values
4036
	  */
4037
	  if (old->val->eq(new_fields->val, old->field->binary()))
4038
	  {
4039
	    old->level= and_level;
4040
	    old->optimize= ((old->optimize & new_fields->optimize &
4041
			     KEY_OPTIMIZE_EXISTS) |
4042
			    ((old->optimize | new_fields->optimize) &
4043
			     KEY_OPTIMIZE_REF_OR_NULL));
4044
            old->null_rejecting= (old->null_rejecting &&
4045
                                  new_fields->null_rejecting);
4046
	  }
4047
	}
4048
	else if (old->eq_func && new_fields->eq_func &&
4049
                 old->val->eq_by_collation(new_fields->val, 
4050
                                           old->field->binary(),
4051
                                           old->field->charset()))
4052
4053
	{
4054
	  old->level= and_level;
4055
	  old->optimize= ((old->optimize & new_fields->optimize &
4056
			   KEY_OPTIMIZE_EXISTS) |
4057
			  ((old->optimize | new_fields->optimize) &
4058
			   KEY_OPTIMIZE_REF_OR_NULL));
4059
          old->null_rejecting= (old->null_rejecting &&
4060
                                new_fields->null_rejecting);
4061
	}
4062
	else if (old->eq_func && new_fields->eq_func &&
4063
		 ((old->val->const_item() && old->val->is_null()) || 
4064
                  new_fields->val->is_null()))
4065
	{
4066
	  /* field = expression OR field IS NULL */
4067
	  old->level= and_level;
4068
	  old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
4069
	  /*
4070
            Remember the NOT NULL value unless the value does not depend
4071
            on other tables.
4072
          */
4073
	  if (!old->val->used_tables() && old->val->is_null())
4074
	    old->val= new_fields->val;
4075
          /* The referred expression can be NULL: */ 
4076
          old->null_rejecting= 0;
4077
	}
4078
	else
4079
	{
4080
	  /*
4081
	    We are comparing two different const.  In this case we can't
4082
	    use a key-lookup on this so it's better to remove the value
4083
	    and let the range optimzier handle it
4084
	  */
4085
	  if (old == --first_free)		// If last item
4086
	    break;
4087
	  *old= *first_free;			// Remove old value
4088
	  old--;				// Retry this value
4089
	}
4090
      }
4091
    }
4092
  }
4093
  /* Remove all not used items */
4094
  for (KEY_FIELD *old=start ; old != first_free ;)
4095
  {
4096
    if (old->level != and_level)
4097
    {						// Not used in all levels
4098
      if (old == --first_free)
4099
	break;
4100
      *old= *first_free;			// Remove old value
4101
      continue;
4102
    }
4103
    old++;
4104
  }
4105
  return first_free;
4106
}
4107
4108
4109
/**
4110
  Add a possible key to array of possible keys if it's usable as a key
4111
4112
    @param key_fields      Pointer to add key, if usable
4113
    @param and_level       And level, to be stored in KEY_FIELD
4114
    @param cond            Condition predicate
4115
    @param field           Field used in comparision
4116
    @param eq_func         True if we used =, <=> or IS NULL
4117
    @param value           Value used for comparison with field
4118
    @param usable_tables   Tables which can be used for key optimization
4119
    @param sargables       IN/OUT Array of found sargable candidates
4120
4121
  @note
4122
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4123
    table, we store this to be able to do not exists optimization later.
4124
4125
  @returns
4126
    *key_fields is incremented if we stored a key in the array
4127
*/
4128
4129
static void
4130
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4131
              Field *field, bool eq_func, Item **value, uint num_values,
4132
              table_map usable_tables, SARGABLE_PARAM **sargables)
4133
{
4134
  uint exists_optimize= 0;
4135
  if (!(field->flags & PART_KEY_FLAG))
4136
  {
4137
    // Don't remove column IS NULL on a LEFT JOIN table
4138
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4139
        !field->table->maybe_null || field->null_ptr)
4140
      return;					// Not a key. Skip it
4141
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4142
    DBUG_ASSERT(num_values == 1);
4143
  }
4144
  else
4145
  {
4146
    table_map used_tables=0;
4147
    bool optimizable=0;
4148
    for (uint i=0; i<num_values; i++)
4149
    {
4150
      used_tables|=(value[i])->used_tables();
4151
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4152
        optimizable=1;
4153
    }
4154
    if (!optimizable)
4155
      return;
4156
    if (!(usable_tables & field->table->map))
4157
    {
4158
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4159
          !field->table->maybe_null || field->null_ptr)
4160
	return;					// Can't use left join optimize
4161
      exists_optimize= KEY_OPTIMIZE_EXISTS;
4162
    }
4163
    else
4164
    {
4165
      JOIN_TAB *stat=field->table->reginfo.join_tab;
4166
      key_map possible_keys=field->key_start;
4167
      possible_keys.intersect(field->table->keys_in_use_for_query);
4168
      stat[0].keys.merge(possible_keys);             // Add possible keys
4169
4170
      /*
4171
	Save the following cases:
4172
	Field op constant
4173
	Field LIKE constant where constant doesn't start with a wildcard
4174
	Field = field2 where field2 is in a different table
4175
	Field op formula
4176
	Field IS NULL
4177
	Field IS NOT NULL
4178
         Field BETWEEN ...
4179
         Field IN ...
4180
      */
4181
      stat[0].key_dependent|=used_tables;
4182
4183
      bool is_const=1;
4184
      for (uint i=0; i<num_values; i++)
4185
      {
4186
        if (!(is_const&= value[i]->const_item()))
4187
          break;
4188
      }
4189
      if (is_const)
4190
        stat[0].const_keys.merge(possible_keys);
4191
      else if (!eq_func)
4192
      {
4193
        /* 
4194
          Save info to be able check whether this predicate can be 
4195
          considered as sargable for range analisis after reading const tables.
4196
          We do not save info about equalities as update_const_equal_items
4197
          will take care of updating info on keys from sargable equalities. 
4198
        */
4199
        (*sargables)--;
4200
        (*sargables)->field= field;
4201
        (*sargables)->arg_value= value;
4202
        (*sargables)->num_values= num_values;
4203
      }
4204
      /*
4205
	We can't always use indexes when comparing a string index to a
4206
	number. cmp_type() is checked to allow compare of dates to numbers.
4207
        eq_func is NEVER true when num_values > 1
4208
       */
4209
      if (!eq_func)
4210
      {
4211
        /* 
4212
          Additional optimization: if we're processing
4213
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
4214
          "t.key = c1".
4215
          TODO: This is a very limited fix. A more generic fix is possible. 
4216
          There are 2 options:
4217
          A) Make equality propagation code be able to handle BETWEEN
4218
             (including cases like t1.key BETWEEN t2.key AND t3.key)
4219
          B) Make range optimizer to infer additional "t.key = c" equalities
4220
             and use them in equality propagation process (see details in
4221
             OptimizerKBAndTodo)
4222
        */
4223
        if ((cond->functype() != Item_func::BETWEEN) ||
4224
            ((Item_func_between*) cond)->negated ||
4225
            !value[0]->eq(value[1], field->binary()))
4226
          return;
4227
        eq_func= TRUE;
4228
      }
4229
4230
      if (field->result_type() == STRING_RESULT)
4231
      {
4232
        if ((*value)->result_type() != STRING_RESULT)
4233
        {
4234
          if (field->cmp_type() != (*value)->result_type())
4235
            return;
4236
        }
4237
        else
4238
        {
4239
          /*
4240
            We can't use indexes if the effective collation
4241
            of the operation differ from the field collation.
4242
          */
4243
          if (field->cmp_type() == STRING_RESULT &&
4244
              ((Field_str*)field)->charset() != cond->compare_collation())
4245
            return;
4246
        }
4247
      }
4248
    }
4249
  }
4250
  /*
4251
    For the moment eq_func is always true. This slot is reserved for future
4252
    extensions where we want to remembers other things than just eq comparisons
4253
  */
4254
  DBUG_ASSERT(eq_func);
4255
  /* Store possible eq field */
4256
  (*key_fields)->field=		field;
4257
  (*key_fields)->eq_func=	eq_func;
4258
  (*key_fields)->val=		*value;
4259
  (*key_fields)->level=		and_level;
4260
  (*key_fields)->optimize=	exists_optimize;
4261
  /*
4262
    If the condition has form "tbl.keypart = othertbl.field" and 
4263
    othertbl.field can be NULL, there will be no matches if othertbl.field 
4264
    has NULL value.
4265
    We use null_rejecting in add_not_null_conds() to add
4266
    'othertbl.field IS NOT NULL' to tab->select_cond.
4267
  */
4268
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4269
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4270
                                  ((*value)->type() == Item::FIELD_ITEM) &&
4271
                                  ((Item_field*)*value)->field->maybe_null());
4272
  (*key_fields)->cond_guard= NULL;
4273
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
4274
                              cond->name < subq_sj_cond_name + 64)? 
4275
                              cond->name - subq_sj_cond_name: UINT_MAX;
4276
  (*key_fields)++;
4277
}
4278
4279
/**
4280
  Add possible keys to array of possible keys originated from a simple
4281
  predicate.
4282
4283
    @param  key_fields     Pointer to add key, if usable
4284
    @param  and_level      And level, to be stored in KEY_FIELD
4285
    @param  cond           Condition predicate
4286
    @param  field          Field used in comparision
4287
    @param  eq_func        True if we used =, <=> or IS NULL
4288
    @param  value          Value used for comparison with field
4289
                           Is NULL for BETWEEN and IN    
4290
    @param  usable_tables  Tables which can be used for key optimization
4291
    @param  sargables      IN/OUT Array of found sargable candidates
4292
4293
  @note
4294
    If field items f1 and f2 belong to the same multiple equality and
4295
    a key is added for f1, the the same key is added for f2.
4296
4297
  @returns
4298
    *key_fields is incremented if we stored a key in the array
4299
*/
4300
4301
static void
4302
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4303
                     Item_func *cond, Item_field *field_item,
4304
                     bool eq_func, Item **val,
4305
                     uint num_values, table_map usable_tables,
4306
                     SARGABLE_PARAM **sargables)
4307
{
4308
  Field *field= field_item->field;
4309
  add_key_field(key_fields, and_level, cond, field,
4310
                eq_func, val, num_values, usable_tables, sargables);
4311
  Item_equal *item_equal= field_item->item_equal;
4312
  if (item_equal)
4313
  { 
4314
    /*
4315
      Add to the set of possible key values every substitution of
4316
      the field for an equal field included into item_equal
4317
    */
4318
    Item_equal_iterator it(*item_equal);
4319
    Item_field *item;
4320
    while ((item= it++))
4321
    {
4322
      if (!field->eq(item->field))
4323
      {
4324
        add_key_field(key_fields, and_level, cond, item->field,
4325
                      eq_func, val, num_values, usable_tables,
4326
                      sargables);
4327
      }
4328
    }
4329
  }
4330
}
4331
4332
static void
4333
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4334
               COND *cond, table_map usable_tables,
4335
               SARGABLE_PARAM **sargables)
4336
{
4337
  if (cond->type() == Item_func::COND_ITEM)
4338
  {
4339
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4340
    KEY_FIELD *org_key_fields= *key_fields;
4341
4342
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4343
    {
4344
      Item *item;
4345
      while ((item=li++))
4346
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4347
                       sargables);
4348
      for (; org_key_fields != *key_fields ; org_key_fields++)
4349
	org_key_fields->level= *and_level;
4350
    }
4351
    else
4352
    {
4353
      (*and_level)++;
4354
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
4355
                     sargables);
4356
      Item *item;
4357
      while ((item=li++))
4358
      {
4359
	KEY_FIELD *start_key_fields= *key_fields;
4360
	(*and_level)++;
4361
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4362
                       sargables);
4363
	*key_fields=merge_key_fields(org_key_fields,start_key_fields,
4364
				     *key_fields,++(*and_level));
4365
      }
4366
    }
4367
    return;
4368
  }
4369
4370
  /* 
4371
    Subquery optimization: Conditions that are pushed down into subqueries
4372
    are wrapped into Item_func_trig_cond. We process the wrapped condition
4373
    but need to set cond_guard for KEYUSE elements generated from it.
4374
  */
4375
  {
4376
    if (cond->type() == Item::FUNC_ITEM &&
4377
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4378
    {
4379
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4380
      if (!join->group_list && !join->order &&
4381
          join->unit->item && 
4382
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
4383
          !join->unit->is_union())
4384
      {
4385
        KEY_FIELD *save= *key_fields;
4386
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4387
                       sargables);
4388
        // Indicate that this ref access candidate is for subquery lookup:
4389
        for (; save != *key_fields; save++)
4390
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4391
      }
4392
      return;
4393
    }
4394
  }
4395
4396
  /* If item is of type 'field op field/constant' add it to key_fields */
4397
  if (cond->type() != Item::FUNC_ITEM)
4398
    return;
4399
  Item_func *cond_func= (Item_func*) cond;
4400
  switch (cond_func->select_optimize()) {
4401
  case Item_func::OPTIMIZE_NONE:
4402
    break;
4403
  case Item_func::OPTIMIZE_KEY:
4404
  {
4405
    Item **values;
4406
    // BETWEEN, IN, NE
4407
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4408
	!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4409
    {
4410
      values= cond_func->arguments()+1;
4411
      if (cond_func->functype() == Item_func::NE_FUNC &&
4412
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4413
	     !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4414
        values--;
4415
      DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
4416
                  cond_func->argument_count() != 2);
4417
      add_key_equal_fields(key_fields, *and_level, cond_func,
4418
                           (Item_field*) (cond_func->key_item()->real_item()),
4419
                           0, values, 
4420
                           cond_func->argument_count()-1,
4421
                           usable_tables, sargables);
4422
    }
4423
    if (cond_func->functype() == Item_func::BETWEEN)
4424
    {
4425
      values= cond_func->arguments();
4426
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4427
      {
4428
        Item_field *field_item;
4429
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4430
            &&
4431
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4432
        {
4433
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4434
          add_key_equal_fields(key_fields, *and_level, cond_func,
4435
                               field_item, 0, values, 1, usable_tables, 
4436
                               sargables);
4437
        }
4438
      }  
4439
    }
4440
    break;
4441
  }
4442
  case Item_func::OPTIMIZE_OP:
4443
  {
4444
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4445
		     cond_func->functype() == Item_func::EQUAL_FUNC);
4446
4447
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4448
	!(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4449
    {
4450
      add_key_equal_fields(key_fields, *and_level, cond_func,
4451
	                (Item_field*) (cond_func->arguments()[0])->real_item(),
4452
		           equal_func,
4453
                           cond_func->arguments()+1, 1, usable_tables,
4454
                           sargables);
4455
    }
4456
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4457
	cond_func->functype() != Item_func::LIKE_FUNC &&
4458
	!(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4459
    {
4460
      add_key_equal_fields(key_fields, *and_level, cond_func, 
4461
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
4462
		           equal_func,
4463
                           cond_func->arguments(),1,usable_tables,
4464
                           sargables);
4465
    }
4466
    break;
4467
  }
4468
  case Item_func::OPTIMIZE_NULL:
4469
    /* column_name IS [NOT] NULL */
4470
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4471
	!(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4472
    {
4473
      Item *tmp=new Item_null;
4474
      if (unlikely(!tmp))                       // Should never be true
4475
	return;
4476
      add_key_equal_fields(key_fields, *and_level, cond_func,
4477
		    (Item_field*) (cond_func->arguments()[0])->real_item(),
4478
		    cond_func->functype() == Item_func::ISNULL_FUNC,
4479
			   &tmp, 1, usable_tables, sargables);
4480
    }
4481
    break;
4482
  case Item_func::OPTIMIZE_EQUAL:
4483
    Item_equal *item_equal= (Item_equal *) cond;
4484
    Item *const_item= item_equal->get_const();
4485
    Item_equal_iterator it(*item_equal);
4486
    Item_field *item;
4487
    if (const_item)
4488
    {
4489
      /*
4490
        For each field field1 from item_equal consider the equality 
4491
        field1=const_item as a condition allowing an index access of the table
4492
        with field1 by the keys value of field1.
4493
      */   
4494
      while ((item= it++))
4495
      {
4496
        add_key_field(key_fields, *and_level, cond_func, item->field,
4497
                      TRUE, &const_item, 1, usable_tables, sargables);
4498
      }
4499
    }
4500
    else 
4501
    {
4502
      /*
4503
        Consider all pairs of different fields included into item_equal.
4504
        For each of them (field1, field1) consider the equality 
4505
        field1=field2 as a condition allowing an index access of the table
4506
        with field1 by the keys value of field2.
4507
      */   
4508
      Item_equal_iterator fi(*item_equal);
4509
      while ((item= fi++))
4510
      {
4511
        Field *field= item->field;
4512
        while ((item= it++))
4513
        {
4514
          if (!field->eq(item->field))
4515
          {
4516
            add_key_field(key_fields, *and_level, cond_func, field,
4517
                          TRUE, (Item **) &item, 1, usable_tables,
4518
                          sargables);
4519
          }
4520
        }
4521
        it.rewind();
4522
      }
4523
    }
4524
    break;
4525
  }
4526
}
4527
4528
/**
4529
  Add all keys with uses 'field' for some keypart.
4530
4531
  If field->and_level != and_level then only mark key_part as const_part.
4532
*/
4533
4534
static uint
4535
max_part_bit(key_part_map bits)
4536
{
4537
  uint found;
4538
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4539
  return found;
4540
}
4541
4542
static void
4543
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4544
{
4545
  Field *field=key_field->field;
4546
  TABLE *form= field->table;
4547
  KEYUSE keyuse;
4548
4549
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4550
  {
4551
    for (uint key=0 ; key < form->s->keys ; key++)
4552
    {
4553
      if (!(form->keys_in_use_for_query.is_set(key)))
4554
	continue;
4555
4556
      uint key_parts= (uint) form->key_info[key].key_parts;
4557
      for (uint part=0 ; part <  key_parts ; part++)
4558
      {
4559
	if (field->eq(form->key_info[key].key_part[part].field))
4560
	{
4561
	  keyuse.table= field->table;
4562
	  keyuse.val =  key_field->val;
4563
	  keyuse.key =  key;
4564
	  keyuse.keypart=part;
4565
	  keyuse.keypart_map= (key_part_map) 1 << part;
4566
	  keyuse.used_tables=key_field->val->used_tables();
4567
	  keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4568
          keyuse.null_rejecting= key_field->null_rejecting;
4569
          keyuse.cond_guard= key_field->cond_guard;
4570
          keyuse.sj_pred_no= key_field->sj_pred_no;
4571
	  VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4572
	}
4573
      }
4574
    }
4575
  }
4576
}
4577
4578
static int
4579
sort_keyuse(KEYUSE *a,KEYUSE *b)
4580
{
4581
  int res;
4582
  if (a->table->tablenr != b->table->tablenr)
4583
    return (int) (a->table->tablenr - b->table->tablenr);
4584
  if (a->key != b->key)
4585
    return (int) (a->key - b->key);
4586
  if (a->keypart != b->keypart)
4587
    return (int) (a->keypart - b->keypart);
4588
  // Place const values before other ones
4589
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4590
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
4591
    return res;
4592
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4593
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4594
		(b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4595
}
4596
4597
4598
/*
4599
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
4600
4601
    This function populates KEY_FIELD array with entries generated from the 
4602
    ON condition of the given nested join, and does the same for nested joins 
4603
    contained within this nested join.
4604
4605
  @param[in]      nested_join_table   Nested join pseudo-table to process
4606
  @param[in,out]  end                 End of the key field array
4607
  @param[in,out]  and_level           And-level
4608
  @param[in,out]  sargables           Array of found sargable candidates
4609
4610
4611
  @note
4612
    We can add accesses to the tables that are direct children of this nested 
4613
    join (1), and are not inner tables w.r.t their neighbours (2).
4614
    
4615
    Example for #1 (outer brackets pair denotes nested join this function is 
4616
    invoked for):
4617
    @code
4618
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4619
    @endcode
4620
    Example for #2:
4621
    @code
4622
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4623
    @endcode
4624
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
4625
    t1 only.
4626
    Example #3:
4627
    @code
4628
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4629
    @endcode
4630
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4631
*/
4632
4633
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4634
                                  KEY_FIELD **end, uint *and_level,
4635
                                  SARGABLE_PARAM **sargables)
4636
{
4637
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4638
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4639
  bool have_another = FALSE;
4640
  table_map tables= 0;
4641
  TABLE_LIST *table;
4642
  DBUG_ASSERT(nested_join_table->nested_join);
4643
4644
  while ((table= li++) || (have_another && (li=li2, have_another=FALSE,
4645
                                            (table= li++))))
4646
  {
4647
    if (table->nested_join)
4648
    {
4649
      if (!table->on_expr)
4650
      {
4651
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4652
        have_another= TRUE;
4653
        li2= li;
4654
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
4655
      }
4656
      else
4657
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4658
    }
4659
    else
4660
      if (!table->on_expr)
4661
        tables |= table->table->map;
4662
  }
4663
  if (nested_join_table->on_expr)
4664
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
4665
                   sargables);
4666
}
4667
4668
4669
/**
4670
  Update keyuse array with all possible keys we can use to fetch rows.
4671
  
4672
  @param       thd 
4673
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
4674
  @param       join_tab       Array in tablenr_order
4675
  @param       tables         Number of tables in join
4676
  @param       cond           WHERE condition (note that the function analyzes
4677
                              join_tab[i]->on_expr too)
4678
  @param       normal_tables  Tables not inner w.r.t some outer join (ones
4679
                              for which we can make ref access based the WHERE
4680
                              clause)
4681
  @param       select_lex     current SELECT
4682
  @param[out]  sargables      Array of found sargable candidates
4683
      
4684
   @retval
4685
     0  OK
4686
   @retval
4687
     1  Out of memory.
4688
*/
4689
4690
static bool
4691
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4692
                    uint tables, COND *cond, COND_EQUAL *cond_equal,
4693
                    table_map normal_tables, SELECT_LEX *select_lex,
4694
                    SARGABLE_PARAM **sargables)
4695
{
4696
  uint	and_level,i,found_eq_constant;
4697
  KEY_FIELD *key_fields, *end, *field;
4698
  uint sz;
4699
  uint m= max(select_lex->max_equal_elems,1);
4700
  
4701
  /* 
4702
    We use the same piece of memory to store both  KEY_FIELD 
4703
    and SARGABLE_PARAM structure.
4704
    KEY_FIELD values are placed at the beginning this memory
4705
    while  SARGABLE_PARAM values are put at the end.
4706
    All predicates that are used to fill arrays of KEY_FIELD
4707
    and SARGABLE_PARAM structures have at most 2 arguments
4708
    except BETWEEN predicates that have 3 arguments and 
4709
    IN predicates.
4710
    This any predicate if it's not BETWEEN/IN can be used 
4711
    directly to fill at most 2 array elements, either of KEY_FIELD
4712
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
4713
    can be filled as this predicate is considered as
4714
    saragable with respect to each of its argument.
4715
    An IN predicate can require at most 1 element as currently
4716
    it is considered as sargable only for its first argument.
4717
    Multiple equality can add  elements that are filled after
4718
    substitution of field arguments by equal fields. There
4719
    can be not more than select_lex->max_equal_elems such 
4720
    substitutions.
4721
  */ 
4722
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4723
      (((thd->lex->current_select->cond_count+1)*2 +
4724
	thd->lex->current_select->between_count)*m+1);
4725
  if (!(key_fields=(KEY_FIELD*)	thd->alloc(sz)))
4726
    return TRUE; /* purecov: inspected */
4727
  and_level= 0;
4728
  field= end= key_fields;
4729
  *sargables= (SARGABLE_PARAM *) key_fields + 
4730
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
4731
  /* set a barrier for the array of SARGABLE_PARAM */
4732
  (*sargables)[0].field= 0; 
4733
4734
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
4735
    return TRUE;
4736
  if (cond)
4737
  {
4738
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
4739
                   sargables);
4740
    for (; field != end ; field++)
4741
    {
4742
      add_key_part(keyuse,field);
4743
      /* Mark that we can optimize LEFT JOIN */
4744
      if (field->val->type() == Item::NULL_ITEM &&
4745
	  !field->field->real_maybe_null())
4746
	field->field->table->reginfo.not_exists_optimize=1;
4747
    }
4748
  }
4749
  for (i=0 ; i < tables ; i++)
4750
  {
4751
    /*
4752
      Block the creation of keys for inner tables of outer joins.
4753
      Here only the outer joins that can not be converted to
4754
      inner joins are left and all nests that can be eliminated
4755
      are flattened.
4756
      In the future when we introduce conditional accesses
4757
      for inner tables in outer joins these keys will be taken
4758
      into account as well.
4759
    */ 
4760
    if (*join_tab[i].on_expr_ref)
4761
      add_key_fields(join_tab->join, &end, &and_level, 
4762
                     *join_tab[i].on_expr_ref,
4763
                     join_tab[i].table->map, sargables);
4764
  }
4765
4766
  /* Process ON conditions for the nested joins */
4767
  {
4768
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
4769
    TABLE_LIST *table;
4770
    while ((table= li++))
4771
    {
4772
      if (table->nested_join)
4773
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
4774
                              sargables);
4775
    }
4776
  }
4777
4778
  /* fill keyuse with found key parts */
4779
  for ( ; field != end ; field++)
4780
    add_key_part(keyuse,field);
4781
4782
  /*
4783
    Sort the array of possible keys and remove the following key parts:
4784
    - ref if there is a keypart which is a ref and a const.
4785
      (e.g. if there is a key(a,b) and the clause is a=3 and b=7 and b=t2.d,
4786
      then we skip the key part corresponding to b=t2.d)
4787
    - keyparts without previous keyparts
4788
      (e.g. if there is a key(a,b,c) but only b < 5 (or a=2 and c < 3) is
4789
      used in the query, we drop the partial key parts from consideration).
4790
    Special treatment for ft-keys.
4791
  */
4792
  if (keyuse->elements)
4793
  {
4794
    KEYUSE key_end,*prev,*save_pos,*use;
4795
4796
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
4797
	  (qsort_cmp) sort_keyuse);
4798
4799
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
4800
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
4801
4802
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
4803
    prev= &key_end;
4804
    found_eq_constant=0;
4805
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
4806
    {
4807
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4808
	use->table->const_key_parts[use->key]|= use->keypart_map;
4809
      {
4810
	if (use->key == prev->key && use->table == prev->table)
4811
	{
4812
	  if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4813
	    continue;				/* remove */
4814
	}
4815
	else if (use->keypart != 0)		// First found must be 0
4816
	  continue;
4817
      }
4818
4819
#ifdef HAVE_purify
4820
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
4821
      if (save_pos != use)
4822
#endif
4823
        *save_pos= *use;
4824
      prev=use;
4825
      found_eq_constant= !use->used_tables;
4826
      /* Save ptr to first use */
4827
      if (!use->table->reginfo.join_tab->keyuse)
4828
	use->table->reginfo.join_tab->keyuse=save_pos;
4829
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
4830
      save_pos++;
4831
    }
4832
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4833
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4834
    keyuse->elements=i;
4835
  }
4836
  DBUG_EXECUTE("opt", print_keyuse_array(keyuse););
4837
  return FALSE;
4838
}
4839
4840
/**
4841
  Update some values in keyuse for faster choose_plan() loop.
4842
*/
4843
4844
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
4845
{
4846
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
4847
4848
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
4849
  {
4850
    table_map map;
4851
    /*
4852
      If we find a ref, assume this table matches a proportional
4853
      part of this table.
4854
      For example 100 records matching a table with 5000 records
4855
      gives 5000/100 = 50 records per key
4856
      Constant tables are ignored.
4857
      To avoid bad matches, we don't make ref_table_rows less than 100.
4858
    */
4859
    keyuse->ref_table_rows= ~(ha_rows) 0;	// If no ref
4860
    if (keyuse->used_tables &
4861
	(map= (keyuse->used_tables & ~join->const_table_map &
4862
	       ~OUTER_REF_TABLE_BIT)))
4863
    {
4864
      uint tablenr;
4865
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4866
      if (map == 1)			// Only one table
4867
      {
4868
	TABLE *tmp_table=join->all_tables[tablenr];
4869
	keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
4870
      }
4871
    }
4872
    /*
4873
      Outer reference (external field) is constant for single executing
4874
      of subquery
4875
    */
4876
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
4877
      keyuse->ref_table_rows= 1;
4878
  }
4879
}
4880
4881
4882
/**
4883
  Discover the indexes that can be used for GROUP BY or DISTINCT queries.
4884
4885
  If the query has a GROUP BY clause, find all indexes that contain all
4886
  GROUP BY fields, and add those indexes to join->const_keys.
4887
4888
  If the query has a DISTINCT clause, find all indexes that contain all
4889
  SELECT fields, and add those indexes to join->const_keys.
4890
  This allows later on such queries to be processed by a
4891
  QUICK_GROUP_MIN_MAX_SELECT.
4892
4893
  @param join
4894
  @param join_tab
4895
4896
  @return
4897
    None
4898
*/
4899
4900
static void
4901
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
4902
{
4903
  List<Item_field> indexed_fields;
4904
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4905
  ORDER      *cur_group;
4906
  Item_field *cur_item;
4907
  key_map possible_keys(0);
4908
4909
  if (join->group_list)
4910
  { /* Collect all query fields referenced in the GROUP clause. */
4911
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4912
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4913
                               (uchar*) &indexed_fields);
4914
  }
4915
  else if (join->select_distinct)
4916
  { /* Collect all query fields referenced in the SELECT clause. */
4917
    List<Item> &select_items= join->fields_list;
4918
    List_iterator<Item> select_items_it(select_items);
4919
    Item *item;
4920
    while ((item= select_items_it++))
4921
      item->walk(&Item::collect_item_field_processor, 0,
4922
                 (uchar*) &indexed_fields);
4923
  }
4924
  else
4925
    return;
4926
4927
  if (indexed_fields.elements == 0)
4928
    return;
4929
4930
  /* Intersect the keys of all group fields. */
4931
  cur_item= indexed_fields_it++;
4932
  possible_keys.merge(cur_item->field->part_of_key);
4933
  while ((cur_item= indexed_fields_it++))
4934
  {
4935
    possible_keys.intersect(cur_item->field->part_of_key);
4936
  }
4937
4938
  if (!possible_keys.is_clear_all())
4939
    join_tab->const_keys.merge(possible_keys);
4940
}
4941
4942
4943
/*****************************************************************************
4944
  Go through all combinations of not marked tables and find the one
4945
  which uses least records
4946
*****************************************************************************/
4947
4948
/** Save const tables first as used tables. */
4949
4950
static void
4951
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4952
{
4953
  join->positions[idx].table= table;
4954
  join->positions[idx].key=key;
4955
  join->positions[idx].records_read=1.0;	/* This is a const table */
4956
  join->positions[idx].ref_depend_map= 0;
4957
4958
  /* Move the const table as down as possible in best_ref */
4959
  JOIN_TAB **pos=join->best_ref+idx+1;
4960
  JOIN_TAB *next=join->best_ref[idx];
4961
  for (;next != table ; pos++)
4962
  {
4963
    JOIN_TAB *tmp=pos[0];
4964
    pos[0]=next;
4965
    next=tmp;
4966
  }
4967
  join->best_ref[idx]=table;
4968
}
4969
4970
4971
/*
4972
  Given a semi-join nest, find out which of the IN-equalities are bound
4973
4974
  SYNOPSIS
4975
    get_bound_sj_equalities()
4976
      sj_nest           Semi-join nest
4977
      remaining_tables  Tables that are not yet bound
4978
4979
  DESCRIPTION
4980
    Given a semi-join nest, find out which of the IN-equalities have their
4981
    left part expression bound (i.e. the said expression doesn't refer to
4982
    any of remaining_tables and can be evaluated).
4983
4984
  RETURN
4985
    Bitmap of bound IN-equalities.
4986
*/
4987
4988
ulonglong get_bound_sj_equalities(TABLE_LIST *sj_nest, 
4989
                                  table_map remaining_tables)
4990
{
4991
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4992
  Item *item;
4993
  uint i= 0;
4994
  ulonglong res= 0;
4995
  while ((item= li++))
4996
  {
4997
    /*
4998
      Q: should this take into account equality propagation and how?
4999
      A: If e->outer_side is an Item_field, walk over the equality
5000
         class and see if there is an element that is bound?
5001
      (this is an optional feature)
5002
    */
5003
    if (!(item->used_tables() & remaining_tables))
5004
    {
5005
      res |= 1ULL < i;
5006
    }
5007
  }
5008
  return res;
5009
}
5010
5011
5012
/**
5013
  Find the best access path for an extension of a partial execution
5014
  plan and add this path to the plan.
5015
5016
  The function finds the best access path to table 's' from the passed
5017
  partial plan where an access path is the general term for any means to
5018
  access the data in 's'. An access path may use either an index or a scan,
5019
  whichever is cheaper. The input partial plan is passed via the array
5020
  'join->positions' of length 'idx'. The chosen access method for 's' and its
5021
  cost are stored in 'join->positions[idx]'.
5022
5023
  @param join             pointer to the structure providing all context info
5024
                          for the query
5025
  @param s                the table to be joined by the function
5026
  @param thd              thread for the connection that submitted the query
5027
  @param remaining_tables set of tables not included into the partial plan yet
5028
  @param idx              the length of the partial plan
5029
  @param record_count     estimate for the number of records returned by the
5030
                          partial plan
5031
  @param read_time        the cost of the partial plan
5032
5033
  @return
5034
    None
5035
*/
5036
5037
static void
5038
best_access_path(JOIN      *join,
5039
                 JOIN_TAB  *s,
5040
                 THD       *thd,
5041
                 table_map remaining_tables,
5042
                 uint      idx,
5043
                 double    record_count,
5044
                 double    read_time)
5045
{
5046
  KEYUSE *best_key=         0;
5047
  uint best_max_key_part=   0;
5048
  my_bool found_constraint= 0;
5049
  double best=              DBL_MAX;
5050
  double best_time=         DBL_MAX;
5051
  double records=           DBL_MAX;
5052
  table_map best_ref_depends_map= 0;
5053
  double tmp;
5054
  ha_rows rec;
5055
  uint best_is_sj_inside_out=    0;
5056
  DBUG_ENTER("best_access_path");
5057
5058
  if (s->keyuse)
5059
  {                                            /* Use key if possible */
5060
    TABLE *table= s->table;
5061
    KEYUSE *keyuse,*start_key=0;
5062
    double best_records= DBL_MAX;
5063
    uint max_key_part=0;
5064
    ulonglong bound_sj_equalities= 0;
5065
    bool try_sj_inside_out= FALSE;
5066
    /*
5067
      Discover the bound equalites. We need to do this, if
5068
        1. The next table is an SJ-inner table, and
5069
        2. It is the first table from that semijoin, and
5070
        3. We're not within a semi-join range (i.e. all semi-joins either have
5071
           all or none of their tables in join_table_map), except
5072
           s->emb_sj_nest (which we've just entered).
5073
        3. All correlation references from this sj-nest are bound
5074
    */
5075
    if (s->emb_sj_nest &&                                                 // (1)
5076
        s->emb_sj_nest->sj_in_exprs < 64 && 
5077
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
5078
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
5079
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
5080
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
5081
    {
5082
      /* This table is an InsideOut scan candidate */
5083
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
5084
                                                   remaining_tables);
5085
      try_sj_inside_out= TRUE;
5086
    }
5087
5088
    /* Test how we can use keys */
5089
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
5090
    for (keyuse=s->keyuse ; keyuse->table == table ;)
5091
    {
5092
      key_part_map found_part= 0;
5093
      table_map found_ref= 0;
5094
      uint key= keyuse->key;
5095
      KEY *keyinfo= table->key_info+key;
5096
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
5097
      key_part_map const_part= 0;
5098
      /* The or-null keypart in ref-or-null access: */
5099
      key_part_map ref_or_null_part= 0;
5100
5101
      /* Calculate how many key segments of the current key we can use */
5102
      start_key= keyuse;
5103
      ulonglong handled_sj_equalities=0;
5104
      key_part_map sj_insideout_map= 0;
5105
5106
      do /* For each keypart */
5107
      {
5108
        uint keypart= keyuse->keypart;
5109
        table_map best_part_found_ref= 0;
5110
        double best_prev_record_reads= DBL_MAX;
5111
        
5112
        do /* For each way to access the keypart */
5113
        {
5114
5115
          /*
5116
            if 1. expression doesn't refer to forward tables
5117
               2. we won't get two ref-or-null's
5118
          */
5119
          if (!(remaining_tables & keyuse->used_tables) &&
5120
              !(ref_or_null_part && (keyuse->optimize &
5121
                                     KEY_OPTIMIZE_REF_OR_NULL)))
5122
          {
5123
            found_part|= keyuse->keypart_map;
5124
            if (!(keyuse->used_tables & ~join->const_table_map))
5125
              const_part|= keyuse->keypart_map;
5126
5127
            double tmp2= prev_record_reads(join, idx, (found_ref |
5128
                                                      keyuse->used_tables));
5129
            if (tmp2 < best_prev_record_reads)
5130
            {
5131
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5132
              best_prev_record_reads= tmp2;
5133
            }
5134
            if (rec > keyuse->ref_table_rows)
5135
              rec= keyuse->ref_table_rows;
5136
	    /*
5137
	      If there is one 'key_column IS NULL' expression, we can
5138
	      use this ref_or_null optimisation of this field
5139
	    */
5140
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5141
              ref_or_null_part |= keyuse->keypart_map;
5142
          }
5143
5144
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5145
          {
5146
            if (!(remaining_tables & keyuse->used_tables))
5147
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5148
            else
5149
            {
5150
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5151
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5152
            }
5153
          }
5154
5155
          keyuse++;
5156
        } while (keyuse->table == table && keyuse->key == key &&
5157
                 keyuse->keypart == keypart);
5158
	found_ref|= best_part_found_ref;
5159
      } while (keyuse->table == table && keyuse->key == key);
5160
5161
      /*
5162
        Assume that that each key matches a proportional part of table.
5163
      */
5164
      if (!found_part && !handled_sj_equalities)
5165
        continue;                               // Nothing usable found
5166
5167
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5168
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
5169
5170
      bool sj_inside_out_scan= FALSE;
5171
      {
5172
        found_constraint= 1;
5173
        /*
5174
          Check if InsideOut scan is applicable:
5175
          1. All IN-equalities are either "bound" or "handled"
5176
          2. Index keyparts are 
5177
             ...
5178
        */
5179
        if (try_sj_inside_out && 
5180
            table->covering_keys.is_set(key) &&
5181
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5182
            PREV_BITS(ulonglong, s->emb_sj_nest->sj_in_exprs)) // (1)
5183
        {
5184
          uint n_fixed_parts= max_part_bit(found_part);
5185
          if (n_fixed_parts != keyinfo->key_parts &&
5186
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5187
               PREV_BITS(uint, keyinfo->key_parts))
5188
          {
5189
            /*
5190
              Not all parts are fixed. Produce bitmap of remaining bits and
5191
              check if all of them are covered.
5192
            */
5193
            sj_inside_out_scan= TRUE;
5194
            DBUG_PRINT("info", ("Using sj InsideOut scan"));
5195
            if (!n_fixed_parts)
5196
            {
5197
              /*
5198
                It's a confluent ref scan.
5199
5200
                That is, all found KEYUSE elements refer to IN-equalities,
5201
                and there is really no ref access because there is no
5202
                  t.keypart0 = {bound expression}
5203
5204
                Calculate the cost of complete loose index scan.
5205
              */
5206
              records= (double)s->table->file->stats.records;
5207
5208
              /* The cost is entire index scan cost (divided by 2) */
5209
              best_time= s->table->file->index_only_read_time(key, records);
5210
5211
              /* Now figure how many different keys we will get */
5212
              ulong rpc;
5213
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5214
                records= records / rpc;
5215
              start_key= NULL;
5216
            }
5217
          }
5218
        }
5219
5220
        /*
5221
          Check if we found full key
5222
        */
5223
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5224
            !ref_or_null_part)
5225
        {                                         /* use eq key */
5226
          max_key_part= (uint) ~0;
5227
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5228
          {
5229
            tmp = prev_record_reads(join, idx, found_ref);
5230
            records=1.0;
5231
          }
5232
          else
5233
          {
5234
            if (!found_ref)
5235
            {                                     /* We found a const key */
5236
              /*
5237
                ReuseRangeEstimateForRef-1:
5238
                We get here if we've found a ref(const) (c_i are constants):
5239
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
5240
                
5241
                If range optimizer was able to construct a "range" 
5242
                access on this index, then its condition "quick_cond" was
5243
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5244
                from the range optimizer.
5245
                
5246
                Proof of (*): By properties of range and ref optimizers 
5247
                quick_cond will be equal or tighther than ref_const_cond. 
5248
                ref_const_cond already covers "smallest" possible interval - 
5249
                a singlepoint interval over all keyparts. Therefore, 
5250
                quick_cond is equivalent to ref_const_cond (if it was an 
5251
                empty interval we wouldn't have got here).
5252
              */
5253
              if (table->quick_keys.is_set(key))
5254
                records= (double) table->quick_rows[key];
5255
              else
5256
              {
5257
                /* quick_range couldn't use key! */
5258
                records= (double) s->records/rec;
5259
              }
5260
            }
5261
            else
5262
            {
5263
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5264
              {                                   /* Prefer longer keys */
5265
                records=
5266
                  ((double) s->records / (double) rec *
5267
                   (1.0 +
5268
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
5269
                     (double) table->s->max_key_length)));
5270
                if (records < 2.0)
5271
                  records=2.0;               /* Can't be as good as a unique */
5272
              }
5273
              /*
5274
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
5275
                E(#rows) from range optimizer. Make another try:
5276
                
5277
                If range optimizer produced E(#rows) for a prefix of the ref
5278
                access we're considering, and that E(#rows) is lower then our
5279
                current estimate, make an adjustment. The criteria of when we
5280
                can make an adjustment is a special case of the criteria used
5281
                in ReuseRangeEstimateForRef-3.
5282
              */
5283
              if (table->quick_keys.is_set(key) &&
5284
                  const_part & (1 << table->quick_key_parts[key]) &&
5285
                  table->quick_n_ranges[key] == 1 &&
5286
                  records > (double) table->quick_rows[key])
5287
              {
5288
                records= (double) table->quick_rows[key];
5289
              }
5290
            }
5291
            /* Limit the number of matched rows */
5292
            tmp= records;
5293
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5294
            if (table->covering_keys.is_set(key))
5295
            {
5296
              /* we can use only index tree */
5297
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5298
            }
5299
            else
5300
              tmp= record_count*min(tmp,s->worst_seeks);
5301
          }
5302
        }
5303
        else
5304
        {
5305
          /*
5306
            Use as much key-parts as possible and a uniq key is better
5307
            than a not unique key
5308
            Set tmp to (previous record count) * (records / combination)
5309
          */
5310
          if ((found_part & 1) &&
5311
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5312
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
5313
          {
5314
            max_key_part= max_part_bit(found_part);
5315
            /*
5316
              ReuseRangeEstimateForRef-3:
5317
              We're now considering a ref[or_null] access via
5318
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
5319
              (same-as-above but with one cond replaced 
5320
               with "t.keypart_i IS NULL")]  (**)
5321
              
5322
              Try re-using E(#rows) from "range" optimizer:
5323
              We can do so if "range" optimizer used the same intervals as
5324
              in (**). The intervals used by range optimizer may be not 
5325
              available at this point (as "range" access might have choosen to
5326
              create quick select over another index), so we can't compare
5327
              them to (**). We'll make indirect judgements instead.
5328
              The sufficient conditions for re-use are:
5329
              (C1) All e_i in (**) are constants, i.e. found_ref==FALSE. (if
5330
                   this is not satisfied we have no way to know which ranges
5331
                   will be actually scanned by 'ref' until we execute the 
5332
                   join)
5333
              (C2) max #key parts in 'range' access == K == max_key_part (this
5334
                   is apparently a necessary requirement)
5335
5336
              We also have a property that "range optimizer produces equal or 
5337
              tighter set of scan intervals than ref(const) optimizer". Each
5338
              of the intervals in (**) are "tightest possible" intervals when 
5339
              one limits itself to using keyparts 1..K (which we do in #2).              
5340
              From here it follows that range access used either one, or
5341
              both of the (I1) and (I2) intervals:
5342
              
5343
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
5344
               (same-as-above but with one cond replaced  
5345
                with "t.keypart_i IS NULL")               (I2)
5346
5347
              The remaining part is to exclude the situation where range
5348
              optimizer used one interval while we're considering
5349
              ref-or-null and looking for estimate for two intervals. This
5350
              is done by last limitation:
5351
5352
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5353
            */
5354
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5355
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5356
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5357
            {
5358
              tmp= records= (double) table->quick_rows[key];
5359
            }
5360
            else
5361
            {
5362
              /* Check if we have statistic about the distribution */
5363
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
5364
              {
5365
                /* 
5366
                  Fix for the case where the index statistics is too
5367
                  optimistic: If 
5368
                  (1) We're considering ref(const) and there is quick select
5369
                      on the same index, 
5370
                  (2) and that quick select uses more keyparts (i.e. it will
5371
                      scan equal/smaller interval then this ref(const))
5372
                  (3) and E(#rows) for quick select is higher then our
5373
                      estimate,
5374
                  Then 
5375
                    We'll use E(#rows) from quick select.
5376
5377
                  Q: Why do we choose to use 'ref'? Won't quick select be
5378
                  cheaper in some cases ?
5379
                  TODO: figure this out and adjust the plan choice if needed.
5380
                */
5381
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
5382
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5383
                    records < (double)table->quick_rows[key])         // (3)
5384
                  records= (double)table->quick_rows[key];
5385
5386
                tmp= records;
5387
              }
5388
              else
5389
              {
5390
                /*
5391
                  Assume that the first key part matches 1% of the file
5392
                  and that the whole key matches 10 (duplicates) or 1
5393
                  (unique) records.
5394
                  Assume also that more key matches proportionally more
5395
                  records
5396
                  This gives the formula:
5397
                  records = (x * (b-a) + a*c-b)/(c-1)
5398
5399
                  b = records matched by whole key
5400
                  a = records matched by first key part (1% of all records?)
5401
                  c = number of key parts in key
5402
                  x = used key parts (1 <= x <= c)
5403
                */
5404
                double rec_per_key;
5405
                if (!(rec_per_key=(double)
5406
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
5407
                  rec_per_key=(double) s->records/rec+1;
5408
5409
                if (!s->records)
5410
                  tmp = 0;
5411
                else if (rec_per_key/(double) s->records >= 0.01)
5412
                  tmp = rec_per_key;
5413
                else
5414
                {
5415
                  double a=s->records*0.01;
5416
                  if (keyinfo->key_parts > 1)
5417
                    tmp= (max_key_part * (rec_per_key - a) +
5418
                          a*keyinfo->key_parts - rec_per_key)/
5419
                         (keyinfo->key_parts-1);
5420
                  else
5421
                    tmp= a;
5422
                  set_if_bigger(tmp,1.0);
5423
                }
5424
                records = (ulong) tmp;
5425
              }
5426
5427
              if (ref_or_null_part)
5428
              {
5429
                /* We need to do two key searches to find key */
5430
                tmp *= 2.0;
5431
                records *= 2.0;
5432
              }
5433
5434
              /*
5435
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
5436
                E(#rows) from range optimizer. Make another try:
5437
                
5438
                If range optimizer produced E(#rows) for a prefix of the ref 
5439
                access we're considering, and that E(#rows) is lower then our
5440
                current estimate, make the adjustment.
5441
5442
                The decision whether we can re-use the estimate from the range
5443
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5444
                applied to first table->quick_key_parts[key] key parts.
5445
              */
5446
              if (table->quick_keys.is_set(key) &&
5447
                  table->quick_key_parts[key] <= max_key_part &&
5448
                  const_part & (1 << table->quick_key_parts[key]) &&
5449
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5450
                                                         const_part) &&
5451
                  records > (double) table->quick_rows[key])
5452
              {
5453
                tmp= records= (double) table->quick_rows[key];
5454
              }
5455
            }
5456
5457
            /* Limit the number of matched rows */
5458
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5459
            if (table->covering_keys.is_set(key))
5460
            {
5461
              /* we can use only index tree */
5462
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5463
            }
5464
            else
5465
              tmp= record_count * min(tmp,s->worst_seeks);
5466
          }
5467
          else
5468
            tmp= best_time;                    // Do nothing
5469
        }
5470
5471
        if (sj_inside_out_scan && !start_key)
5472
        {
5473
          tmp= tmp/2;
5474
          if (records)
5475
            records= records/2;
5476
        }
5477
5478
      }
5479
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5480
      {
5481
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
5482
        best= tmp;
5483
        best_records= records;
5484
        best_key= start_key;
5485
        best_max_key_part= max_key_part;
5486
        best_ref_depends_map= found_ref;
5487
        best_is_sj_inside_out= sj_inside_out_scan;
5488
      }
5489
    }
5490
    records= best_records;
5491
  }
5492
5493
  /*
5494
    Don't test table scan if it can't be better.
5495
    Prefer key lookup if we would use the same key for scanning.
5496
5497
    Don't do a table scan on InnoDB tables, if we can read the used
5498
    parts of the row from any of the used index.
5499
    This is because table scans uses index and we would not win
5500
    anything by using a table scan.
5501
5502
    A word for word translation of the below if-statement in sergefp's
5503
    understanding: we check if we should use table scan if:
5504
    (1) The found 'ref' access produces more records than a table scan
5505
        (or index scan, or quick select), or 'ref' is more expensive than
5506
        any of them.
5507
    (2) This doesn't hold: the best way to perform table scan is to to perform
5508
        'range' access using index IDX, and the best way to perform 'ref' 
5509
        access is to use the same index IDX, with the same or more key parts.
5510
        (note: it is not clear how this rule is/should be extended to 
5511
        index_merge quick selects)
5512
    (3) See above note about InnoDB.
5513
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5514
             path, but there is no quick select)
5515
        If the condition in the above brackets holds, then the only possible
5516
        "table scan" access method is ALL/index (there is no quick select).
5517
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
5518
        choose it over ALL/index, there is no need to consider a full table
5519
        scan.
5520
  */
5521
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
5522
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5523
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5524
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5525
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5526
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5527
  {                                             // Check full join
5528
    ha_rows rnd_records= s->found_records;
5529
    /*
5530
      If there is a filtering condition on the table (i.e. ref analyzer found
5531
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5532
      preceding this table in the join order we're now considering), then 
5533
      assume that 25% of the rows will be filtered out by this condition.
5534
5535
      This heuristic is supposed to force tables used in exprZ to be before
5536
      this table in join order.
5537
    */
5538
    if (found_constraint)
5539
      rnd_records-= rnd_records/4;
5540
5541
    /*
5542
      If applicable, get a more accurate estimate. Don't use the two
5543
      heuristics at once.
5544
    */
5545
    if (s->table->quick_condition_rows != s->found_records)
5546
      rnd_records= s->table->quick_condition_rows;
5547
5548
    /*
5549
      Range optimizer never proposes a RANGE if it isn't better
5550
      than FULL: so if RANGE is present, it's always preferred to FULL.
5551
      Here we estimate its cost.
5552
    */
5553
    if (s->quick)
5554
    {
5555
      /*
5556
        For each record we:
5557
        - read record range through 'quick'
5558
        - skip rows which does not satisfy WHERE constraints
5559
        TODO: 
5560
        We take into account possible use of join cache for ALL/index
5561
        access (see first else-branch below), but we don't take it into 
5562
        account here for range/index_merge access. Find out why this is so.
5563
      */
5564
      tmp= record_count *
5565
        (s->quick->read_time +
5566
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5567
    }
5568
    else
5569
    {
5570
      /* Estimate cost of reading table. */
5571
      tmp= s->table->file->scan_time();
5572
      if (s->table->map & join->outer_join)     // Can't use join cache
5573
      {
5574
        /*
5575
          For each record we have to:
5576
          - read the whole table record 
5577
          - skip rows which does not satisfy join condition
5578
        */
5579
        tmp= record_count *
5580
          (tmp +
5581
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5582
      }
5583
      else
5584
      {
5585
        /* We read the table as many times as join buffer becomes full. */
5586
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5587
                           record_count /
5588
                           (double) thd->variables.join_buff_size));
5589
        /* 
5590
            We don't make full cartesian product between rows in the scanned
5591
           table and existing records because we skip all rows from the
5592
           scanned table, which does not satisfy join condition when 
5593
           we read the table (see flush_cached_records for details). Here we
5594
           take into account cost to read and skip these records.
5595
        */
5596
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5597
      }
5598
    }
5599
5600
    /*
5601
      We estimate the cost of evaluating WHERE clause for found records
5602
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5603
      tmp give us total cost of using TABLE SCAN
5604
    */
5605
    if (best == DBL_MAX ||
5606
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5607
         best + record_count/(double) TIME_FOR_COMPARE*records))
5608
    {
5609
      /*
5610
        If the table has a range (s->quick is set) make_join_select()
5611
        will ensure that this will be used
5612
      */
5613
      best= tmp;
5614
      records= rows2double(rnd_records);
5615
      best_key= 0;
5616
      /* range/index_merge/ALL/index access method are "independent", so: */
5617
      best_ref_depends_map= 0;
5618
      best_is_sj_inside_out= FALSE;
5619
    }
5620
  }
5621
5622
  /* Update the cost information for the current partial plan */
5623
  join->positions[idx].records_read= records;
5624
  join->positions[idx].read_time=    best;
5625
  join->positions[idx].key=          best_key;
5626
  join->positions[idx].table=        s;
5627
  join->positions[idx].ref_depend_map= best_ref_depends_map;
5628
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5629
5630
  if (!best_key &&
5631
      idx == join->const_tables &&
5632
      s->table == join->sort_by_table &&
5633
      join->unit->select_limit_cnt >= records)
5634
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5635
5636
  DBUG_VOID_RETURN;
5637
}
5638
5639
5640
/**
5641
  Selects and invokes a search strategy for an optimal query plan.
5642
5643
  The function checks user-configurable parameters that control the search
5644
  strategy for an optimal plan, selects the search method and then invokes
5645
  it. Each specific optimization procedure stores the final optimal plan in
5646
  the array 'join->best_positions', and the cost of the plan in
5647
  'join->best_read'.
5648
5649
  @param join         pointer to the structure providing all context info for
5650
                      the query
5651
  @param join_tables  set of the tables in the query
5652
5653
  @todo
5654
    'MAX_TABLES+2' denotes the old implementation of find_best before
5655
    the greedy version. Will be removed when greedy_search is approved.
5656
5657
  @retval
5658
    FALSE       ok
5659
  @retval
5660
    TRUE        Fatal error
5661
*/
5662
5663
static bool
5664
choose_plan(JOIN *join, table_map join_tables)
5665
{
5666
  uint search_depth= join->thd->variables.optimizer_search_depth;
5667
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5668
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5669
  DBUG_ENTER("choose_plan");
5670
5671
  join->cur_embedding_map= 0;
5672
  reset_nj_counters(join->join_list);
5673
  /*
5674
    if (SELECT_STRAIGHT_JOIN option is set)
5675
      reorder tables so dependent tables come after tables they depend 
5676
      on, otherwise keep tables in the order they were specified in the query 
5677
    else
5678
      Apply heuristic: pre-sort all access plans with respect to the number of
5679
      records accessed.
5680
  */
5681
  my_qsort(join->best_ref + join->const_tables,
5682
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
5683
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
5684
  join->cur_emb_sj_nests= 0;
5685
  if (straight_join)
5686
  {
5687
    optimize_straight_join(join, join_tables);
5688
  }
5689
  else
5690
  {
5691
    if (search_depth == MAX_TABLES+2)
5692
    { /*
5693
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5694
        the greedy version. Will be removed when greedy_search is approved.
5695
      */
5696
      join->best_read= DBL_MAX;
5697
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5698
        DBUG_RETURN(TRUE);
5699
    } 
5700
    else
5701
    {
5702
      if (search_depth == 0)
5703
        /* Automatically determine a reasonable value for 'search_depth' */
5704
        search_depth= determine_search_depth(join);
5705
      if (greedy_search(join, join_tables, search_depth, prune_level))
5706
        DBUG_RETURN(TRUE);
5707
    }
5708
  }
5709
5710
  /* 
5711
    Store the cost of this query into a user variable
5712
    Don't update last_query_cost for statements that are not "flat joins" :
5713
    i.e. they have subqueries, unions or call stored procedures.
5714
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
5715
  */
5716
  if (join->thd->lex->is_single_level_stmt())
5717
    join->thd->status_var.last_query_cost= join->best_read;
5718
  DBUG_RETURN(FALSE);
5719
}
5720
5721
5722
/**
5723
  Compare two JOIN_TAB objects based on the number of accessed records.
5724
5725
  @param ptr1 pointer to first JOIN_TAB object
5726
  @param ptr2 pointer to second JOIN_TAB object
5727
5728
  NOTES
5729
    The order relation implemented by join_tab_cmp() is not transitive,
5730
    i.e. it is possible to choose such a, b and c that (a < b) && (b < c)
5731
    but (c < a). This implies that result of a sort using the relation
5732
    implemented by join_tab_cmp() depends on the order in which
5733
    elements are compared, i.e. the result is implementation-specific.
5734
    Example:
5735
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
5736
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
5737
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
5738
     
5739
  @retval
5740
    1  if first is bigger
5741
  @retval
5742
    -1  if second is bigger
5743
  @retval
5744
    0  if equal
5745
*/
5746
5747
static int
5748
join_tab_cmp(const void* ptr1, const void* ptr2)
5749
{
5750
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5751
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
5752
5753
  if (jt1->dependent & jt2->table->map)
5754
    return 1;
5755
  if (jt2->dependent & jt1->table->map)
5756
    return -1;  
5757
  if (jt1->found_records > jt2->found_records)
5758
    return 1;
5759
  if (jt1->found_records < jt2->found_records)
5760
    return -1; 
5761
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5762
}
5763
5764
5765
/**
5766
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5767
*/
5768
5769
static int
5770
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5771
{
5772
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5773
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
5774
5775
  if (jt1->dependent & jt2->table->map)
5776
    return 1;
5777
  if (jt2->dependent & jt1->table->map)
5778
    return -1;
5779
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5780
}
5781
5782
/**
5783
  Heuristic procedure to automatically guess a reasonable degree of
5784
  exhaustiveness for the greedy search procedure.
5785
5786
  The procedure estimates the optimization time and selects a search depth
5787
  big enough to result in a near-optimal QEP, that doesn't take too long to
5788
  find. If the number of tables in the query exceeds some constant, then
5789
  search_depth is set to this constant.
5790
5791
  @param join   pointer to the structure providing all context info for
5792
                the query
5793
5794
  @note
5795
    This is an extremely simplistic implementation that serves as a stub for a
5796
    more advanced analysis of the join. Ideally the search depth should be
5797
    determined by learning from previous query optimizations, because it will
5798
    depend on the CPU power (and other factors).
5799
5800
  @todo
5801
    this value should be determined dynamically, based on statistics:
5802
    uint max_tables_for_exhaustive_opt= 7;
5803
5804
  @todo
5805
    this value could be determined by some mapping of the form:
5806
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5807
5808
  @return
5809
    A positive integer that specifies the search depth (and thus the
5810
    exhaustiveness) of the depth-first search algorithm used by
5811
    'greedy_search'.
5812
*/
5813
5814
static uint
5815
determine_search_depth(JOIN *join)
5816
{
5817
  uint table_count=  join->tables - join->const_tables;
5818
  uint search_depth;
5819
  /* TODO: this value should be determined dynamically, based on statistics: */
5820
  uint max_tables_for_exhaustive_opt= 7;
5821
5822
  if (table_count <= max_tables_for_exhaustive_opt)
5823
    search_depth= table_count+1; // use exhaustive for small number of tables
5824
  else
5825
    /*
5826
      TODO: this value could be determined by some mapping of the form:
5827
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5828
    */
5829
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
5830
5831
  return search_depth;
5832
}
5833
5834
5835
/**
5836
  Select the best ways to access the tables in a query without reordering them.
5837
5838
    Find the best access paths for each query table and compute their costs
5839
    according to their order in the array 'join->best_ref' (thus without
5840
    reordering the join tables). The function calls sequentially
5841
    'best_access_path' for each table in the query to select the best table
5842
    access method. The final optimal plan is stored in the array
5843
    'join->best_positions', and the corresponding cost in 'join->best_read'.
5844
5845
  @param join          pointer to the structure providing all context info for
5846
                       the query
5847
  @param join_tables   set of the tables in the query
5848
5849
  @note
5850
    This function can be applied to:
5851
    - queries with STRAIGHT_JOIN
5852
    - internally to compute the cost of an arbitrary QEP
5853
  @par
5854
    Thus 'optimize_straight_join' can be used at any stage of the query
5855
    optimization process to finalize a QEP as it is.
5856
*/
5857
5858
static void
5859
optimize_straight_join(JOIN *join, table_map join_tables)
5860
{
5861
  JOIN_TAB *s;
5862
  uint idx= join->const_tables;
5863
  double    record_count= 1.0;
5864
  double    read_time=    0.0;
5865
 
5866
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5867
  {
5868
    /* Find the best access method from 's' to the current partial plan */
5869
    advance_sj_state(join_tables, s);
5870
    best_access_path(join, s, join->thd, join_tables, idx,
5871
                     record_count, read_time);
5872
    /* compute the cost of the new plan extended with 's' */
5873
    record_count*= join->positions[idx].records_read;
5874
    read_time+=    join->positions[idx].read_time;
5875
    join_tables&= ~(s->table->map);
5876
    ++idx;
5877
  }
5878
5879
  read_time+= record_count / (double) TIME_FOR_COMPARE;
5880
  if (join->sort_by_table &&
5881
      join->sort_by_table != join->positions[join->const_tables].table->table)
5882
    read_time+= record_count;  // We have to make a temp table
5883
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
5884
         sizeof(POSITION)*idx);
5885
  join->best_read= read_time;
5886
}
5887
5888
5889
/**
5890
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5891
5892
    The search procedure uses a hybrid greedy/exhaustive search with controlled
5893
    exhaustiveness. The search is performed in N = card(remaining_tables)
5894
    steps. Each step evaluates how promising is each of the unoptimized tables,
5895
    selects the most promising table, and extends the current partial QEP with
5896
    that table.  Currenly the most 'promising' table is the one with least
5897
    expensive extension.\
5898
5899
    There are two extreme cases:
5900
    -# When (card(remaining_tables) < search_depth), the estimate finds the
5901
    best complete continuation of the partial QEP. This continuation can be
5902
    used directly as a result of the search.
5903
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
5904
    consideres the extension of the current QEP with each of the remaining
5905
    unoptimized tables.
5906
5907
    All other cases are in-between these two extremes. Thus the parameter
5908
    'search_depth' controlls the exhaustiveness of the search. The higher the
5909
    value, the longer the optimizaton time and possibly the better the
5910
    resulting plan. The lower the value, the fewer alternative plans are
5911
    estimated, but the more likely to get a bad QEP.
5912
5913
    All intermediate and final results of the procedure are stored in 'join':
5914
    - join->positions     : modified for every partial QEP that is explored
5915
    - join->best_positions: modified for the current best complete QEP
5916
    - join->best_read     : modified for the current best complete QEP
5917
    - join->best_ref      : might be partially reordered
5918
5919
    The final optimal plan is stored in 'join->best_positions', and its
5920
    corresponding cost in 'join->best_read'.
5921
5922
  @note
5923
    The following pseudocode describes the algorithm of 'greedy_search':
5924
5925
    @code
5926
    procedure greedy_search
5927
    input: remaining_tables
5928
    output: pplan;
5929
    {
5930
      pplan = <>;
5931
      do {
5932
        (t, a) = best_extension(pplan, remaining_tables);
5933
        pplan = concat(pplan, (t, a));
5934
        remaining_tables = remaining_tables - t;
5935
      } while (remaining_tables != {})
5936
      return pplan;
5937
    }
5938
5939
  @endcode
5940
    where 'best_extension' is a placeholder for a procedure that selects the
5941
    most "promising" of all tables in 'remaining_tables'.
5942
    Currently this estimate is performed by calling
5943
    'best_extension_by_limited_search' to evaluate all extensions of the
5944
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5945
    mainly depends on that of 'best_extension_by_limited_search'.
5946
5947
  @par
5948
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
5949
    worst-case complexity of this algorithm is <=
5950
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5951
    complexity of greedy_search is O(N!).
5952
5953
  @par
5954
    In the future, 'greedy_search' might be extended to support other
5955
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
5956
5957
  @param join             pointer to the structure providing all context info
5958
                          for the query
5959
  @param remaining_tables set of tables not included into the partial plan yet
5960
  @param search_depth     controlls the exhaustiveness of the search
5961
  @param prune_level      the pruning heuristics that should be applied during
5962
                          search
5963
5964
  @retval
5965
    FALSE       ok
5966
  @retval
5967
    TRUE        Fatal error
5968
*/
5969
5970
static bool
5971
greedy_search(JOIN      *join,
5972
              table_map remaining_tables,
5973
              uint      search_depth,
5974
              uint      prune_level)
5975
{
5976
  double    record_count= 1.0;
5977
  double    read_time=    0.0;
5978
  uint      idx= join->const_tables; // index into 'join->best_ref'
5979
  uint      best_idx;
5980
  uint      size_remain;    // cardinality of remaining_tables
5981
  POSITION  best_pos;
5982
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5983
5984
  DBUG_ENTER("greedy_search");
5985
5986
  /* number of tables that remain to be optimized */
5987
  size_remain= my_count_bits(remaining_tables);
5988
5989
  do {
5990
    /* Find the extension of the current QEP with the lowest cost */
5991
    join->best_read= DBL_MAX;
5992
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5993
                                         read_time, search_depth, prune_level))
5994
      DBUG_RETURN(TRUE);
5995
5996
    if (size_remain <= search_depth)
5997
    {
5998
      /*
5999
        'join->best_positions' contains a complete optimal extension of the
6000
        current partial QEP.
6001
      */
6002
      DBUG_EXECUTE("opt", print_plan(join, join->tables,
6003
                                     record_count, read_time, read_time,
6004
                                     "optimal"););
6005
      DBUG_RETURN(FALSE);
6006
    }
6007
6008
    /* select the first table in the optimal extension as most promising */
6009
    best_pos= join->best_positions[idx];
6010
    best_table= best_pos.table;
6011
    /*
6012
      Each subsequent loop of 'best_extension_by_limited_search' uses
6013
      'join->positions' for cost estimates, therefore we have to update its
6014
      value.
6015
    */
6016
    join->positions[idx]= best_pos;
6017
6018
    /* find the position of 'best_table' in 'join->best_ref' */
6019
    best_idx= idx;
6020
    JOIN_TAB *pos= join->best_ref[best_idx];
6021
    while (pos && best_table != pos)
6022
      pos= join->best_ref[++best_idx];
6023
    DBUG_ASSERT((pos != NULL)); // should always find 'best_table'
6024
    /* move 'best_table' at the first free position in the array of joins */
6025
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
6026
6027
    /* compute the cost of the new plan extended with 'best_table' */
6028
    record_count*= join->positions[idx].records_read;
6029
    read_time+=    join->positions[idx].read_time;
6030
6031
    remaining_tables&= ~(best_table->table->map);
6032
    --size_remain;
6033
    ++idx;
6034
6035
    DBUG_EXECUTE("opt", print_plan(join, join->tables,
6036
                                   record_count, read_time, read_time,
6037
                                   "extended"););
6038
  } while (TRUE);
6039
}
6040
6041
6042
/**
6043
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
6044
  exhaustive search.
6045
6046
    The procedure searches for the optimal ordering of the query tables in set
6047
    'remaining_tables' of size N, and the corresponding optimal access paths to
6048
    each table. The choice of a table order and an access path for each table
6049
    constitutes a query execution plan (QEP) that fully specifies how to
6050
    execute the query.
6051
   
6052
    The maximal size of the found plan is controlled by the parameter
6053
    'search_depth'. When search_depth == N, the resulting plan is complete and
6054
    can be used directly as a QEP. If search_depth < N, the found plan consists
6055
    of only some of the query tables. Such "partial" optimal plans are useful
6056
    only as input to query optimization procedures, and cannot be used directly
6057
    to execute a query.
6058
6059
    The algorithm begins with an empty partial plan stored in 'join->positions'
6060
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
6061
    evaluates the cost of the partial plan extended by all access plans for
6062
    each of the relations in 'remaining_tables', expands the current partial
6063
    plan with the access plan that results in lowest cost of the expanded
6064
    partial plan, and removes the corresponding relation from
6065
    'remaining_tables'. The algorithm continues until it either constructs a
6066
    complete optimal plan, or constructs an optimal plartial plan with size =
6067
    search_depth.
6068
6069
    The final optimal plan is stored in 'join->best_positions'. The
6070
    corresponding cost of the optimal plan is in 'join->best_read'.
6071
6072
  @note
6073
    The procedure uses a recursive depth-first search where the depth of the
6074
    recursion (and thus the exhaustiveness of the search) is controlled by the
6075
    parameter 'search_depth'.
6076
6077
  @note
6078
    The pseudocode below describes the algorithm of
6079
    'best_extension_by_limited_search'. The worst-case complexity of this
6080
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
6081
    the complexity of greedy_search is O(N!).
6082
6083
    @code
6084
    procedure best_extension_by_limited_search(
6085
      pplan in,             // in, partial plan of tables-joined-so-far
6086
      pplan_cost,           // in, cost of pplan
6087
      remaining_tables,     // in, set of tables not referenced in pplan
6088
      best_plan_so_far,     // in/out, best plan found so far
6089
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
6090
      search_depth)         // in, maximum size of the plans being considered
6091
    {
6092
      for each table T from remaining_tables
6093
      {
6094
        // Calculate the cost of using table T as above
6095
        cost = complex-series-of-calculations;
6096
6097
        // Add the cost to the cost so far.
6098
        pplan_cost+= cost;
6099
6100
        if (pplan_cost >= best_plan_so_far_cost)
6101
          // pplan_cost already too great, stop search
6102
          continue;
6103
6104
        pplan= expand pplan by best_access_method;
6105
        remaining_tables= remaining_tables - table T;
6106
        if (remaining_tables is not an empty set
6107
            and
6108
            search_depth > 1)
6109
        {
6110
          best_extension_by_limited_search(pplan, pplan_cost,
6111
                                           remaining_tables,
6112
                                           best_plan_so_far,
6113
                                           best_plan_so_far_cost,
6114
                                           search_depth - 1);
6115
        }
6116
        else
6117
        {
6118
          best_plan_so_far_cost= pplan_cost;
6119
          best_plan_so_far= pplan;
6120
        }
6121
      }
6122
    }
6123
    @endcode
6124
6125
  @note
6126
    When 'best_extension_by_limited_search' is called for the first time,
6127
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6128
    The actual implementation provides a way to optionally use pruning
6129
    heuristic (controlled by the parameter 'prune_level') to reduce the search
6130
    space by skipping some partial plans.
6131
6132
  @note
6133
    The parameter 'search_depth' provides control over the recursion
6134
    depth, and thus the size of the resulting optimal plan.
6135
6136
  @param join             pointer to the structure providing all context info
6137
                          for the query
6138
  @param remaining_tables set of tables not included into the partial plan yet
6139
  @param idx              length of the partial QEP in 'join->positions';
6140
                          since a depth-first search is used, also corresponds
6141
                          to the current depth of the search tree;
6142
                          also an index in the array 'join->best_ref';
6143
  @param record_count     estimate for the number of records returned by the
6144
                          best partial plan
6145
  @param read_time        the cost of the best partial plan
6146
  @param search_depth     maximum depth of the recursion and thus size of the
6147
                          found optimal plan
6148
                          (0 < search_depth <= join->tables+1).
6149
  @param prune_level      pruning heuristics that should be applied during
6150
                          optimization
6151
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6152
6153
  @retval
6154
    FALSE       ok
6155
  @retval
6156
    TRUE        Fatal error
6157
*/
6158
6159
static bool
6160
best_extension_by_limited_search(JOIN      *join,
6161
                                 table_map remaining_tables,
6162
                                 uint      idx,
6163
                                 double    record_count,
6164
                                 double    read_time,
6165
                                 uint      search_depth,
6166
                                 uint      prune_level)
6167
{
6168
  DBUG_ENTER("best_extension_by_limited_search");
6169
6170
  THD *thd= join->thd;
6171
  if (thd->killed)  // Abort
6172
    DBUG_RETURN(TRUE);
6173
6174
  DBUG_EXECUTE("opt", print_plan(join, idx, read_time, record_count, idx,
6175
                                 "SOFAR:"););
6176
6177
  /* 
6178
     'join' is a partial plan with lower cost than the best plan so far,
6179
     so continue expanding it further with the tables in 'remaining_tables'.
6180
  */
6181
  JOIN_TAB *s;
6182
  double best_record_count= DBL_MAX;
6183
  double best_read_time=    DBL_MAX;
6184
6185
  DBUG_EXECUTE("opt", print_plan(join, idx, record_count, read_time, read_time,
6186
                                "part_plan"););
6187
6188
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6189
  {
6190
    table_map real_table_bit= s->table->map;
6191
    if ((remaining_tables & real_table_bit) && 
6192
        !(remaining_tables & s->dependent) && 
6193
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6194
    {
6195
      double current_record_count, current_read_time;
6196
      advance_sj_state(remaining_tables, s);
6197
6198
      /*
6199
        psergey-insideout-todo: 
6200
          when best_access_path() detects it could do an InsideOut scan or 
6201
          some other scan, have it return an insideout scan and a flag that 
6202
          requests to "fork" this loop iteration. (Q: how does that behave 
6203
          when the depth is insufficient??)
6204
      */
6205
      /* Find the best access method from 's' to the current partial plan */
6206
      best_access_path(join, s, thd, remaining_tables, idx,
6207
                       record_count, read_time);
6208
      /* Compute the cost of extending the plan with 's' */
6209
      current_record_count= record_count * join->positions[idx].records_read;
6210
      current_read_time=    read_time + join->positions[idx].read_time;
6211
6212
      /* Expand only partial plans with lower cost than the best QEP so far */
6213
      if ((current_read_time +
6214
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6215
      {
6216
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
6217
                                       current_record_count,
6218
                                       read_time,
6219
                                       (current_read_time +
6220
                                        current_record_count / 
6221
                                        (double) TIME_FOR_COMPARE),
6222
                                       "prune_by_cost"););
6223
        restore_prev_nj_state(s);
6224
        restore_prev_sj_state(remaining_tables, s);
6225
        continue;
6226
      }
6227
6228
      /*
6229
        Prune some less promising partial plans. This heuristic may miss
6230
        the optimal QEPs, thus it results in a non-exhaustive search.
6231
      */
6232
      if (prune_level == 1)
6233
      {
6234
        if (best_record_count > current_record_count ||
6235
            best_read_time > current_read_time ||
6236
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6237
        {
6238
          if (best_record_count >= current_record_count &&
6239
              best_read_time >= current_read_time &&
6240
              /* TODO: What is the reasoning behind this condition? */
6241
              (!(s->key_dependent & remaining_tables) ||
6242
               join->positions[idx].records_read < 2.0))
6243
          {
6244
            best_record_count= current_record_count;
6245
            best_read_time=    current_read_time;
6246
          }
6247
        }
6248
        else
6249
        {
6250
          DBUG_EXECUTE("opt", print_plan(join, idx+1,
6251
                                         current_record_count,
6252
                                         read_time,
6253
                                         current_read_time,
6254
                                         "pruned_by_heuristic"););
6255
          restore_prev_nj_state(s);
6256
          restore_prev_sj_state(remaining_tables, s);
6257
          continue;
6258
        }
6259
      }
6260
6261
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6262
      { /* Recursively expand the current partial plan */
6263
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6264
        if (best_extension_by_limited_search(join,
6265
                                             remaining_tables & ~real_table_bit,
6266
                                             idx + 1,
6267
                                             current_record_count,
6268
                                             current_read_time,
6269
                                             search_depth - 1,
6270
                                             prune_level))
6271
          DBUG_RETURN(TRUE);
6272
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6273
      }
6274
      else
6275
      { /*
6276
          'join' is either the best partial QEP with 'search_depth' relations,
6277
          or the best complete QEP so far, whichever is smaller.
6278
        */
6279
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6280
        if (join->sort_by_table &&
6281
            join->sort_by_table !=
6282
            join->positions[join->const_tables].table->table)
6283
          /* We have to make a temp table */
6284
          current_read_time+= current_record_count;
6285
        if ((search_depth == 1) || (current_read_time < join->best_read))
6286
        {
6287
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6288
                 sizeof(POSITION) * (idx + 1));
6289
          join->best_read= current_read_time - 0.001;
6290
        }
6291
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
6292
                                       current_record_count,
6293
                                       read_time,
6294
                                       current_read_time,
6295
                                       "full_plan"););
6296
      }
6297
      restore_prev_nj_state(s);
6298
      restore_prev_sj_state(remaining_tables, s);
6299
    }
6300
  }
6301
  DBUG_RETURN(FALSE);
6302
}
6303
6304
6305
/**
6306
  @todo
6307
  - TODO: this function is here only temporarily until 'greedy_search' is
6308
  tested and accepted.
6309
6310
  RETURN VALUES
6311
    FALSE       ok
6312
    TRUE        Fatal error
6313
*/
6314
static bool
6315
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6316
	  double read_time)
6317
{
6318
  DBUG_ENTER("find_best");
6319
  THD *thd= join->thd;
6320
  if (thd->killed)
6321
    DBUG_RETURN(TRUE);
6322
  if (!rest_tables)
6323
  {
6324
    DBUG_PRINT("best",("read_time: %g  record_count: %g",read_time,
6325
		       record_count));
6326
6327
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6328
    if (join->sort_by_table &&
6329
	join->sort_by_table !=
6330
	join->positions[join->const_tables].table->table)
6331
      read_time+=record_count;			// We have to make a temp table
6332
    if (read_time < join->best_read)
6333
    {
6334
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
6335
	     sizeof(POSITION)*idx);
6336
      join->best_read= read_time - 0.001;
6337
    }
6338
    DBUG_RETURN(FALSE);
6339
  }
6340
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6341
    DBUG_RETURN(FALSE);					/* Found better before */
6342
6343
  JOIN_TAB *s;
6344
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6345
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6346
  {
6347
    table_map real_table_bit=s->table->map;
6348
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6349
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6350
    {
6351
      double records, best;
6352
      advance_sj_state(rest_tables, s);
6353
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
6354
                       read_time);
6355
      records= join->positions[idx].records_read;
6356
      best= join->positions[idx].read_time;
6357
      /*
6358
	Go to the next level only if there hasn't been a better key on
6359
	this level! This will cut down the search for a lot simple cases!
6360
      */
6361
      double current_record_count=record_count*records;
6362
      double current_read_time=read_time+best;
6363
      if (best_record_count > current_record_count ||
6364
	  best_read_time > current_read_time ||
6365
	  (idx == join->const_tables && s->table == join->sort_by_table))
6366
      {
6367
	if (best_record_count >= current_record_count &&
6368
	    best_read_time >= current_read_time &&
6369
	    (!(s->key_dependent & rest_tables) || records < 2.0))
6370
	{
6371
	  best_record_count=current_record_count;
6372
	  best_read_time=current_read_time;
6373
	}
6374
	swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6375
	if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6376
                      current_record_count,current_read_time))
6377
          DBUG_RETURN(TRUE);
6378
	swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6379
      }
6380
      restore_prev_nj_state(s);
6381
      restore_prev_sj_state(rest_tables, s);
6382
      if (join->select_options & SELECT_STRAIGHT_JOIN)
6383
	break;				// Don't test all combinations
6384
    }
6385
  }
6386
  DBUG_RETURN(FALSE);
6387
}
6388
6389
6390
/**
6391
  Find how much space the prevous read not const tables takes in cache.
6392
*/
6393
6394
static void calc_used_field_length(THD *thd, JOIN_TAB *join_tab)
6395
{
6396
  uint null_fields,blobs,fields,rec_length;
6397
  Field **f_ptr,*field;
6398
  MY_BITMAP *read_set= join_tab->table->read_set;;
6399
6400
  null_fields= blobs= fields= rec_length=0;
6401
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6402
  {
6403
    if (bitmap_is_set(read_set, field->field_index))
6404
    {
6405
      uint flags=field->flags;
6406
      fields++;
6407
      rec_length+=field->pack_length();
6408
      if (flags & BLOB_FLAG)
6409
	blobs++;
6410
      if (!(flags & NOT_NULL_FLAG))
6411
	null_fields++;
6412
    }
6413
  }
6414
  if (null_fields)
6415
    rec_length+=(join_tab->table->s->null_fields+7)/8;
6416
  if (join_tab->table->maybe_null)
6417
    rec_length+=sizeof(my_bool);
6418
  if (blobs)
6419
  {
6420
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6421
			     (join_tab->table->s->reclength- rec_length));
6422
    rec_length+=(uint) max(4,blob_length);
6423
  }
6424
  join_tab->used_fields=fields;
6425
  join_tab->used_fieldlength=rec_length;
6426
  join_tab->used_blobs=blobs;
6427
}
6428
6429
6430
static uint
6431
cache_record_length(JOIN *join,uint idx)
6432
{
6433
  uint length=0;
6434
  JOIN_TAB **pos,**end;
6435
  THD *thd=join->thd;
6436
6437
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6438
       pos != end ;
6439
       pos++)
6440
  {
6441
    JOIN_TAB *join_tab= *pos;
6442
    if (!join_tab->used_fieldlength)		/* Not calced yet */
6443
      calc_used_field_length(thd, join_tab);
6444
    length+=join_tab->used_fieldlength;
6445
  }
6446
  return length;
6447
}
6448
6449
6450
/*
6451
  Get the number of different row combinations for subset of partial join
6452
6453
  SYNOPSIS
6454
    prev_record_reads()
6455
      join       The join structure
6456
      idx        Number of tables in the partial join order (i.e. the
6457
                 partial join order is in join->positions[0..idx-1])
6458
      found_ref  Bitmap of tables for which we need to find # of distinct
6459
                 row combinations.
6460
6461
  DESCRIPTION
6462
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
6463
    tables within that join order (specified in found_ref), find out how many
6464
    distinct row combinations of subset tables will be in the result of the
6465
    partial join order.
6466
     
6467
    This is used as follows: Suppose we have a table accessed with a ref-based
6468
    method. The ref access depends on current rows of tables in found_ref.
6469
    We want to count # of different ref accesses. We assume two ref accesses
6470
    will be different if at least one of access parameters is different.
6471
    Example: consider a query
6472
6473
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6474
6475
    and a join order:
6476
      t1,  ref access on t1.key=c1
6477
      t2,  ref access on t2.key=c2       
6478
      t3,  ref access on t3.key=t1.field 
6479
    
6480
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6481
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6482
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
6483
            n_distinct_ref_scans = #records_read(t1)
6484
    
6485
    The reason for having this function (at least the latest version of it)
6486
    is that we need to account for buffering in join execution. 
6487
    
6488
    An edge-case example: if we have a non-first table in join accessed via
6489
    ref(const) or ref(param) where there is a small number of different
6490
    values of param, then the access will likely hit the disk cache and will
6491
    not require any disk seeks.
6492
    
6493
    The proper solution would be to assume an LRU disk cache of some size,
6494
    calculate probability of cache hits, etc. For now we just count
6495
    identical ref accesses as one.
6496
6497
  RETURN 
6498
    Expected number of row combinations
6499
*/
6500
6501
static double
6502
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6503
{
6504
  double found=1.0;
6505
  POSITION *pos_end= join->positions - 1;
6506
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6507
  {
6508
    if (pos->table->table->map & found_ref)
6509
    {
6510
      found_ref|= pos->ref_depend_map;
6511
      /* 
6512
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
6513
        with no matching row we will get position[t2].records_read==0. 
6514
        Actually the size of output is one null-complemented row, therefore 
6515
        we will use value of 1 whenever we get records_read==0.
6516
6517
        Note
6518
        - the above case can't occur if inner part of outer join has more 
6519
          than one table: table with no matches will not be marked as const.
6520
6521
        - Ideally we should add 1 to records_read for every possible null-
6522
          complemented row. We're not doing it because: 1. it will require
6523
          non-trivial code and add overhead. 2. The value of records_read
6524
          is an inprecise estimate and adding 1 (or, in the worst case,
6525
          #max_nested_outer_joins=64-1) will not make it any more precise.
6526
      */
6527
      if (pos->records_read > DBL_EPSILON)
6528
        found*= pos->records_read;
6529
    }
6530
  }
6531
  return found;
6532
}
6533
6534
6535
/**
6536
  Set up join struct according to best position.
6537
*/
6538
6539
static bool
6540
get_best_combination(JOIN *join)
6541
{
6542
  uint i,tablenr;
6543
  table_map used_tables;
6544
  JOIN_TAB *join_tab,*j;
6545
  KEYUSE *keyuse;
6546
  uint table_count;
6547
  THD *thd=join->thd;
6548
  DBUG_ENTER("get_best_combination");
6549
6550
  table_count=join->tables;
6551
  if (!(join->join_tab=join_tab=
6552
	(JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6553
    DBUG_RETURN(TRUE);
6554
6555
  join->full_join=0;
6556
6557
  used_tables= OUTER_REF_TABLE_BIT;		// Outer row is already read
6558
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
6559
  {
6560
    TABLE *form;
6561
    *j= *join->best_positions[tablenr].table;
6562
    form=join->table[tablenr]=j->table;
6563
    used_tables|= form->map;
6564
    form->reginfo.join_tab=j;
6565
    if (!*j->on_expr_ref)
6566
      form->reginfo.not_exists_optimize=0;	// Only with LEFT JOIN
6567
    DBUG_PRINT("info",("type: %d", j->type));
6568
    if (j->type == JT_CONST)
6569
      continue;					// Handled in make_join_stat..
6570
6571
    j->ref.key = -1;
6572
    j->ref.key_parts=0;
6573
6574
    if (j->type == JT_SYSTEM)
6575
      continue;
6576
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
6577
    {
6578
      j->type=JT_ALL;
6579
      if (tablenr != join->const_tables)
6580
	join->full_join=1;
6581
    }
6582
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6583
      DBUG_RETURN(TRUE);                        // Something went wrong
6584
  }
6585
6586
  for (i=0 ; i < table_count ; i++)
6587
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6588
  update_depend_map(join);
6589
  DBUG_RETURN(0);
6590
}
6591
6592
6593
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6594
			       table_map used_tables)
6595
{
6596
  KEYUSE *keyuse=org_keyuse;
6597
  THD  *thd= join->thd;
6598
  uint keyparts,length,key;
6599
  TABLE *table;
6600
  KEY *keyinfo;
6601
  DBUG_ENTER("create_ref_for_key");
6602
6603
  /*  Use best key from find_best */
6604
  table=j->table;
6605
  key=keyuse->key;
6606
  keyinfo=table->key_info+key;
6607
6608
  {
6609
    keyparts=length=0;
6610
    uint found_part_ref_or_null= 0;
6611
    /*
6612
      Calculate length for the used key
6613
      Stop if there is a missing key part or when we find second key_part
6614
      with KEY_OPTIMIZE_REF_OR_NULL
6615
    */
6616
    do
6617
    {
6618
      if (!(~used_tables & keyuse->used_tables))
6619
      {
6620
	if (keyparts == keyuse->keypart &&
6621
	    !(found_part_ref_or_null & keyuse->optimize))
6622
	{
6623
	  keyparts++;
6624
	  length+= keyinfo->key_part[keyuse->keypart].store_length;
6625
	  found_part_ref_or_null|= keyuse->optimize;
6626
	}
6627
      }
6628
      keyuse++;
6629
    } while (keyuse->table == table && keyuse->key == key);
6630
  }
6631
6632
  /* set up fieldref */
6633
  keyinfo=table->key_info+key;
6634
  j->ref.key_parts=keyparts;
6635
  j->ref.key_length=length;
6636
  j->ref.key=(int) key;
6637
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6638
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6639
						   (keyparts+1)))) ||
6640
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6641
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
6642
  {
6643
    DBUG_RETURN(TRUE);
6644
  }
6645
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
6646
  j->ref.key_err=1;
6647
  j->ref.null_rejecting= 0;
6648
  j->ref.disable_cache= FALSE;
6649
  keyuse=org_keyuse;
6650
6651
  store_key **ref_key= j->ref.key_copy;
6652
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
6653
  bool keyuse_uses_no_tables= TRUE;
6654
  {
6655
    uint i;
6656
    for (i=0 ; i < keyparts ; keyuse++,i++)
6657
    {
6658
      while (keyuse->keypart != i ||
6659
	     ((~used_tables) & keyuse->used_tables))
6660
	keyuse++;				/* Skip other parts */
6661
6662
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6663
      j->ref.items[i]=keyuse->val;		// Save for cond removal
6664
      j->ref.cond_guards[i]= keyuse->cond_guard;
6665
      if (keyuse->null_rejecting) 
6666
        j->ref.null_rejecting |= 1 << i;
6667
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6668
      if (!keyuse->used_tables &&
6669
	  !(join->select_options & SELECT_DESCRIBE))
6670
      {					// Compare against constant
6671
	store_key_item tmp(thd, keyinfo->key_part[i].field,
6672
                           key_buff + maybe_null,
6673
                           maybe_null ?  key_buff : 0,
6674
                           keyinfo->key_part[i].length, keyuse->val);
6675
	if (thd->is_fatal_error)
6676
	  DBUG_RETURN(TRUE);
6677
	tmp.copy();
6678
      }
6679
      else
6680
	*ref_key++= get_store_key(thd,
6681
				  keyuse,join->const_table_map,
6682
				  &keyinfo->key_part[i],
6683
				  key_buff, maybe_null);
6684
      /*
6685
	Remember if we are going to use REF_OR_NULL
6686
	But only if field _really_ can be null i.e. we force JT_REF
6687
	instead of JT_REF_OR_NULL in case if field can't be null
6688
      */
6689
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6690
	null_ref_key= key_buff;
6691
      key_buff+=keyinfo->key_part[i].store_length;
6692
    }
6693
  }
6694
  *ref_key=0;				// end_marker
6695
  if (j->type == JT_CONST)
6696
    j->table->const_table= 1;
6697
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY |
6698
			       HA_END_SPACE_KEY)) != HA_NOSAME) ||
6699
	   keyparts != keyinfo->key_parts || null_ref_key)
6700
  {
6701
    /* Must read with repeat */
6702
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
6703
    j->ref.null_ref_key= null_ref_key;
6704
  }
6705
  else if (keyuse_uses_no_tables)
6706
  {
6707
    /*
6708
      This happen if we are using a constant expression in the ON part
6709
      of an LEFT JOIN.
6710
      SELECT * FROM a LEFT JOIN b ON b.key=30
6711
      Here we should not mark the table as a 'const' as a field may
6712
      have a 'normal' value or a NULL value.
6713
    */
6714
    j->type=JT_CONST;
6715
  }
6716
  else
6717
    j->type=JT_EQ_REF;
6718
  DBUG_RETURN(0);
6719
}
6720
6721
6722
6723
static store_key *
6724
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6725
	      KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6726
{
6727
  if (!((~used_tables) & keyuse->used_tables))		// if const item
6728
  {
6729
    return new store_key_const_item(thd,
6730
				    key_part->field,
6731
				    key_buff + maybe_null,
6732
				    maybe_null ? key_buff : 0,
6733
				    key_part->length,
6734
				    keyuse->val);
6735
  }
6736
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
6737
           (keyuse->val->type() == Item::REF_ITEM &&
6738
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6739
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6740
             Item_ref::DIRECT_REF && 
6741
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6742
    return new store_key_field(thd,
6743
			       key_part->field,
6744
			       key_buff + maybe_null,
6745
			       maybe_null ? key_buff : 0,
6746
			       key_part->length,
6747
			       ((Item_field*) keyuse->val->real_item())->field,
6748
			       keyuse->val->full_name());
6749
  return new store_key_item(thd,
6750
			    key_part->field,
6751
			    key_buff + maybe_null,
6752
			    maybe_null ? key_buff : 0,
6753
			    key_part->length,
6754
			    keyuse->val);
6755
}
6756
6757
/**
6758
  This function is only called for const items on fields which are keys.
6759
6760
  @return
6761
    returns 1 if there was some conversion made when the field was stored.
6762
*/
6763
6764
bool
6765
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6766
{
6767
  bool error;
6768
  TABLE *table= field->table;
6769
  THD *thd= table->in_use;
6770
  ha_rows cuted_fields=thd->cuted_fields;
6771
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
6772
                                                   table->write_set);
6773
6774
  /*
6775
    we should restore old value of count_cuted_fields because
6776
    store_val_in_field can be called from mysql_insert 
6777
    with select_insert, which make count_cuted_fields= 1
6778
   */
6779
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
6780
  thd->count_cuted_fields= check_flag;
6781
  error= item->save_in_field(field, 1);
6782
  thd->count_cuted_fields= old_count_cuted_fields;
6783
  dbug_tmp_restore_column_map(table->write_set, old_map);
6784
  return error || cuted_fields != thd->cuted_fields;
6785
}
6786
6787
6788
static bool
6789
make_simple_join(JOIN *join,TABLE *tmp_table)
6790
{
6791
  TABLE **tableptr;
6792
  JOIN_TAB *join_tab;
6793
  DBUG_ENTER("make_simple_join");
6794
6795
  /*
6796
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6797
    to this function through JOIN::exec (may happen for sub-queries).
6798
  */
6799
  if (!join->table_reexec)
6800
  {
6801
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6802
      DBUG_RETURN(TRUE);                        /* purecov: inspected */
6803
    if (join->tmp_join)
6804
      join->tmp_join->table_reexec= join->table_reexec;
6805
  }
6806
  if (!join->join_tab_reexec)
6807
  {
6808
    if (!(join->join_tab_reexec=
6809
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6810
      DBUG_RETURN(TRUE);                        /* purecov: inspected */
6811
    if (join->tmp_join)
6812
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6813
  }
6814
  tableptr= join->table_reexec;
6815
  join_tab= join->join_tab_reexec;
6816
6817
  join->join_tab=join_tab;
6818
  join->table=tableptr; tableptr[0]=tmp_table;
6819
  join->tables=1;
6820
  join->const_tables=0;
6821
  join->const_table_map=0;
6822
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6823
    join->tmp_table_param.func_count=0;
6824
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6825
  join->first_record=join->sort_and_group=0;
6826
  join->send_records=(ha_rows) 0;
6827
  join->group=0;
6828
  join->row_limit=join->unit->select_limit_cnt;
6829
  join->do_send_rows = (join->row_limit) ? 1 : 0;
6830
6831
  join_tab->cache.buff=0;			/* No caching */
6832
  join_tab->table=tmp_table;
6833
  join_tab->select=0;
6834
  join_tab->select_cond=0;
6835
  join_tab->quick=0;
6836
  join_tab->type= JT_ALL;			/* Map through all records */
6837
  join_tab->keys.init();
6838
  join_tab->keys.set_all();                     /* test everything in quick */
6839
  join_tab->info=0;
6840
  join_tab->on_expr_ref=0;
6841
  join_tab->last_inner= 0;
6842
  join_tab->first_unmatched= 0;
6843
  join_tab->ref.key = -1;
6844
  join_tab->not_used_in_distinct=0;
6845
  join_tab->read_first_record= join_init_read_record;
6846
  join_tab->join=join;
6847
  join_tab->ref.key_parts= 0;
6848
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6849
  join_tab->do_firstmatch= NULL;
6850
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6851
  tmp_table->status=0;
6852
  tmp_table->null_row=0;
6853
  DBUG_RETURN(FALSE);
6854
}
6855
6856
6857
inline void add_cond_and_fix(Item **e1, Item *e2)
6858
{
6859
  if (*e1)
6860
  {
6861
    Item *res;
6862
    if ((res= new Item_cond_and(*e1, e2)))
6863
    {
6864
      *e1= res;
6865
      res->quick_fix_field();
6866
    }
6867
  }
6868
  else
6869
    *e1= e2;
6870
}
6871
6872
6873
/**
6874
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6875
  we've inferred from ref/eq_ref access performed.
6876
6877
    This function is a part of "Early NULL-values filtering for ref access"
6878
    optimization.
6879
6880
    Example of this optimization:
6881
    For query SELECT * FROM t1,t2 WHERE t2.key=t1.field @n
6882
    and plan " any-access(t1), ref(t2.key=t1.field) " @n
6883
    add "t1.field IS NOT NULL" to t1's table condition. @n
6884
6885
    Description of the optimization:
6886
    
6887
      We look through equalities choosen to perform ref/eq_ref access,
6888
      pick equalities that have form "tbl.part_of_key = othertbl.field"
6889
      (where othertbl is a non-const table and othertbl.field may be NULL)
6890
      and add them to conditions on correspoding tables (othertbl in this
6891
      example).
6892
6893
      Exception from that is the case when referred_tab->join != join.
6894
      I.e. don't add NOT NULL constraints from any embedded subquery.
6895
      Consider this query:
6896
      @code
6897
      SELECT A.f2 FROM t1 LEFT JOIN t2 A ON A.f2 = f1
6898
      WHERE A.f3=(SELECT MIN(f3) FROM  t2 C WHERE A.f4 = C.f4) OR A.f3 IS NULL;
6899
      @endocde
6900
      Here condition A.f3 IS NOT NULL is going to be added to the WHERE
6901
      condition of the embedding query.
6902
      Another example:
6903
      SELECT * FROM t10, t11 WHERE (t10.a < 10 OR t10.a IS NULL)
6904
      AND t11.b <=> t10.b AND (t11.a = (SELECT MAX(a) FROM t12
6905
      WHERE t12.b = t10.a ));
6906
      Here condition t10.a IS NOT NULL is going to be added.
6907
      In both cases addition of NOT NULL condition will erroneously reject
6908
      some rows of the result set.
6909
      referred_tab->join != join constraint would disallow such additions.
6910
6911
      This optimization doesn't affect the choices that ref, range, or join
6912
      optimizer make. This was intentional because this was added after 4.1
6913
      was GA.
6914
      
6915
    Implementation overview
6916
      1. update_ref_and_keys() accumulates info about null-rejecting
6917
         predicates in in KEY_FIELD::null_rejecting
6918
      1.1 add_key_part saves these to KEYUSE.
6919
      2. create_ref_for_key copies them to TABLE_REF.
6920
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6921
         appropiate JOIN_TAB members.
6922
*/
6923
6924
static void add_not_null_conds(JOIN *join)
6925
{
6926
  DBUG_ENTER("add_not_null_conds");
6927
  for (uint i=join->const_tables ; i < join->tables ; i++)
6928
  {
6929
    JOIN_TAB *tab=join->join_tab+i;
6930
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6931
         tab->type == JT_REF_OR_NULL) &&
6932
        !tab->table->maybe_null)
6933
    {
6934
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
6935
      {
6936
        if (tab->ref.null_rejecting & (1 << keypart))
6937
        {
6938
          Item *item= tab->ref.items[keypart];
6939
          Item *notnull;
6940
          DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
6941
          Item_field *not_null_item= (Item_field*)item;
6942
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
6943
          /*
6944
            For UPDATE queries such as:
6945
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
6946
            not_null_item is the t1.f1, but it's referred_tab is 0.
6947
          */
6948
          if (!referred_tab || referred_tab->join != join)
6949
            continue;
6950
          if (!(notnull= new Item_func_isnotnull(not_null_item)))
6951
            DBUG_VOID_RETURN;
6952
          /*
6953
            We need to do full fix_fields() call here in order to have correct
6954
            notnull->const_item(). This is needed e.g. by test_quick_select 
6955
            when it is called from make_join_select after this function is 
6956
            called.
6957
          */
6958
          if (notnull->fix_fields(join->thd, &notnull))
6959
            DBUG_VOID_RETURN;
6960
          DBUG_EXECUTE("where",print_where(notnull,
6961
                                           referred_tab->table->alias,
6962
                                           QT_ORDINARY););
6963
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6964
        }
6965
      }
6966
    }
6967
  }
6968
  DBUG_VOID_RETURN;
6969
}
6970
6971
/**
6972
  Build a predicate guarded by match variables for embedding outer joins.
6973
  The function recursively adds guards for predicate cond
6974
  assending from tab to the first inner table  next embedding
6975
  nested outer join and so on until it reaches root_tab
6976
  (root_tab can be 0).
6977
6978
  @param tab       the first inner table for most nested outer join
6979
  @param cond      the predicate to be guarded (must be set)
6980
  @param root_tab  the first inner table to stop
6981
6982
  @return
6983
    -  pointer to the guarded predicate, if success
6984
    -  0, otherwise
6985
*/
6986
6987
static COND*
6988
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
6989
{
6990
  COND *tmp;
6991
  DBUG_ASSERT(cond != 0);
6992
  if (tab == root_tab)
6993
    return cond;
6994
  if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
6995
    tmp= new Item_func_trig_cond(tmp, &tab->found);
6996
  if (tmp)
6997
  {
6998
    tmp->quick_fix_field();
6999
    tmp->update_used_tables();
7000
  }
7001
  return tmp;
7002
}
7003
7004
7005
/**
7006
  Fill in outer join related info for the execution plan structure.
7007
7008
    For each outer join operation left after simplification of the
7009
    original query the function set up the following pointers in the linear
7010
    structure join->join_tab representing the selected execution plan.
7011
    The first inner table t0 for the operation is set to refer to the last
7012
    inner table tk through the field t0->last_inner.
7013
    Any inner table ti for the operation are set to refer to the first
7014
    inner table ti->first_inner.
7015
    The first inner table t0 for the operation is set to refer to the
7016
    first inner table of the embedding outer join operation, if there is any,
7017
    through the field t0->first_upper.
7018
    The on expression for the outer join operation is attached to the
7019
    corresponding first inner table through the field t0->on_expr_ref.
7020
    Here ti are structures of the JOIN_TAB type.
7021
7022
  EXAMPLE. For the query: 
7023
  @code
7024
        SELECT * FROM t1
7025
                      LEFT JOIN
7026
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
7027
                      ON (t1.a=t2.a AND t1.b=t3.b)
7028
          WHERE t1.c > 5,
7029
  @endcode
7030
7031
    given the execution plan with the table order t1,t2,t3,t4
7032
    is selected, the following references will be set;
7033
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
7034
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
7035
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
7036
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
7037
7038
  @param join   reference to the info fully describing the query
7039
7040
  @note
7041
    The function assumes that the simplification procedure has been
7042
    already applied to the join query (see simplify_joins).
7043
    This function can be called only after the execution plan
7044
    has been chosen.
7045
*/
7046
7047
static void
7048
make_outerjoin_info(JOIN *join)
7049
{
7050
  DBUG_ENTER("make_outerjoin_info");
7051
  for (uint i=join->const_tables ; i < join->tables ; i++)
7052
  {
7053
    JOIN_TAB *tab=join->join_tab+i;
7054
    TABLE *table=tab->table;
7055
    TABLE_LIST *tbl= table->pos_in_table_list;
7056
    TABLE_LIST *embedding= tbl->embedding;
7057
7058
    if (tbl->outer_join)
7059
    {
7060
      /* 
7061
        Table tab is the only one inner table for outer join.
7062
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
7063
        is in the query above.)
7064
      */
7065
      tab->last_inner= tab->first_inner= tab;
7066
      tab->on_expr_ref= &tbl->on_expr;
7067
      tab->cond_equal= tbl->cond_equal;
7068
      if (embedding)
7069
        tab->first_upper= embedding->nested_join->first_nested;
7070
    }    
7071
    for ( ; embedding ; embedding= embedding->embedding)
7072
    {
7073
      /* Ignore sj-nests: */
7074
      if (!embedding->on_expr)
7075
        continue;
7076
      NESTED_JOIN *nested_join= embedding->nested_join;
7077
      if (!nested_join->counter_)
7078
      {
7079
        /* 
7080
          Table tab is the first inner table for nested_join.
7081
          Save reference to it in the nested join structure.
7082
        */ 
7083
        nested_join->first_nested= tab;
7084
        tab->on_expr_ref= &embedding->on_expr;
7085
        tab->cond_equal= tbl->cond_equal;
7086
        if (embedding->embedding)
7087
          tab->first_upper= embedding->embedding->nested_join->first_nested;
7088
      }
7089
      if (!tab->first_inner)  
7090
        tab->first_inner= nested_join->first_nested;
7091
      if (++nested_join->counter_ < nested_join->join_list.elements)
7092
        break;
7093
      /* Table tab is the last inner table for nested join. */
7094
      nested_join->first_nested->last_inner= tab;
7095
    }
7096
  }
7097
  DBUG_VOID_RETURN;
7098
}
7099
7100
7101
static bool
7102
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
7103
{
7104
  THD *thd= join->thd;
7105
  DBUG_ENTER("make_join_select");
7106
  if (select)
7107
  {
7108
    add_not_null_conds(join);
7109
    table_map used_tables;
7110
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
7111
    {                        /* there may be a select without a cond. */    
7112
      if (join->tables > 1)
7113
        cond->update_used_tables();		// Tablenr may have changed
7114
      if (join->const_tables == join->tables &&
7115
	  thd->lex->current_select->master_unit() ==
7116
	  &thd->lex->unit)		// not upper level SELECT
7117
        join->const_table_map|=RAND_TABLE_BIT;
7118
      {						// Check const tables
7119
        COND *const_cond=
7120
	  make_cond_for_table(cond,
7121
                              join->const_table_map,
7122
                              (table_map) 0, 1);
7123
        DBUG_EXECUTE("where",print_where(const_cond,"constants", QT_ORDINARY););
7124
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
7125
             tab < join->join_tab+join->tables ; tab++)
7126
        {
7127
          if (*tab->on_expr_ref)
7128
          {
7129
            JOIN_TAB *cond_tab= tab->first_inner;
7130
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
7131
                                           join->const_table_map,
7132
                                           (  table_map) 0, 0);
7133
            if (!tmp)
7134
              continue;
7135
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7136
            if (!tmp)
7137
              DBUG_RETURN(1);
7138
            tmp->quick_fix_field();
7139
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7140
	                            new Item_cond_and(cond_tab->select_cond,
7141
                                                      tmp);
7142
            if (!cond_tab->select_cond)
7143
	      DBUG_RETURN(1);
7144
            cond_tab->select_cond->quick_fix_field();
7145
          }       
7146
        }
7147
        if (const_cond && !const_cond->val_int())
7148
        {
7149
	  DBUG_PRINT("info",("Found impossible WHERE condition"));
7150
	  DBUG_RETURN(1);	 // Impossible const condition
7151
        }
7152
      }
7153
    }
7154
    used_tables=((select->const_tables=join->const_table_map) |
7155
		 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7156
    for (uint i=join->const_tables ; i < join->tables ; i++)
7157
    {
7158
      JOIN_TAB *tab=join->join_tab+i;
7159
      /*
7160
        first_inner is the X in queries like:
7161
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7162
      */
7163
      JOIN_TAB *first_inner_tab= tab->first_inner; 
7164
      table_map current_map= tab->table->map;
7165
      bool use_quick_range=0;
7166
      COND *tmp;
7167
7168
      /*
7169
	Following force including random expression in last table condition.
7170
	It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7171
      */
7172
      if (i == join->tables-1)
7173
	current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7174
      used_tables|=current_map;
7175
7176
      if (tab->type == JT_REF && tab->quick &&
7177
	  (uint) tab->ref.key == tab->quick->index &&
7178
	  tab->ref.key_length < tab->quick->max_used_key_length)
7179
      {
7180
	/* Range uses longer key;  Use this instead of ref on key */
7181
	tab->type=JT_ALL;
7182
	use_quick_range=1;
7183
	tab->use_quick=1;
7184
        tab->ref.key= -1;
7185
	tab->ref.key_parts=0;		// Don't use ref key.
7186
	join->best_positions[i].records_read= rows2double(tab->quick->records);
7187
        /* 
7188
          We will use join cache here : prevent sorting of the first
7189
          table only and sort at the end.
7190
        */
7191
        if (i != join->const_tables && join->tables > join->const_tables + 1)
7192
          join->full_join= 1;
7193
      }
7194
7195
      tmp= NULL;
7196
      if (cond)
7197
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7198
      if (cond && !tmp && tab->quick)
7199
      {						// Outer join
7200
        if (tab->type != JT_ALL)
7201
        {
7202
          /*
7203
            Don't use the quick method
7204
            We come here in the case where we have 'key=constant' and
7205
            the test is removed by make_cond_for_table()
7206
          */
7207
          delete tab->quick;
7208
          tab->quick= 0;
7209
        }
7210
        else
7211
        {
7212
          /*
7213
            Hack to handle the case where we only refer to a table
7214
            in the ON part of an OUTER JOIN. In this case we want the code
7215
            below to check if we should use 'quick' instead.
7216
          */
7217
          DBUG_PRINT("info", ("Item_int"));
7218
          tmp= new Item_int((longlong) 1,1);	// Always true
7219
        }
7220
7221
      }
7222
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7223
          tab->type == JT_EQ_REF)
7224
      {
7225
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7226
	SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7227
                                       thd->memdup((uchar*) select,
7228
                                                   sizeof(*select)));
7229
	if (!sel)
7230
	  DBUG_RETURN(1);			// End of memory
7231
        /*
7232
          If tab is an inner table of an outer join operation,
7233
          add a match guard to the pushed down predicate.
7234
          The guard will turn the predicate on only after
7235
          the first match for outer tables is encountered.
7236
	*/        
7237
        if (cond && tmp)
7238
        {
7239
          /*
7240
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7241
            a cond, so neutralize the hack above.
7242
          */
7243
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7244
            DBUG_RETURN(1);
7245
          tab->select_cond=sel->cond=tmp;
7246
          /* Push condition to storage engine if this is enabled
7247
             and the condition is not guarded */
7248
          tab->table->file->pushed_cond= NULL;
7249
	  if (thd->variables.engine_condition_pushdown)
7250
          {
7251
            COND *push_cond= 
7252
              make_cond_for_table(tmp, current_map, current_map, 0);
7253
            if (push_cond)
7254
            {
7255
              /* Push condition to handler */
7256
              if (!tab->table->file->cond_push(push_cond))
7257
                tab->table->file->pushed_cond= push_cond;
7258
            }
7259
          }
7260
        }
7261
        else
7262
          tab->select_cond= sel->cond= NULL;
7263
7264
	sel->head=tab->table;
7265
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7266
	if (tab->quick)
7267
	{
7268
	  /* Use quick key read if it's a constant and it's not used
7269
	     with key reading */
7270
	  if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7271
	      && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7272
	  {
7273
	    sel->quick=tab->quick;		// Use value from get_quick_...
7274
	    sel->quick_keys.clear_all();
7275
	    sel->needed_reg.clear_all();
7276
	  }
7277
	  else
7278
	  {
7279
	    delete tab->quick;
7280
	  }
7281
	  tab->quick=0;
7282
	}
7283
	uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7284
	if (i == join->const_tables && ref_key)
7285
	{
7286
	  if (!tab->const_keys.is_clear_all() &&
7287
              tab->table->reginfo.impossible_range)
7288
	    DBUG_RETURN(1);
7289
	}
7290
	else if (tab->type == JT_ALL && ! use_quick_range)
7291
	{
7292
	  if (!tab->const_keys.is_clear_all() &&
7293
	      tab->table->reginfo.impossible_range)
7294
	    DBUG_RETURN(1);				// Impossible range
7295
	  /*
7296
	    We plan to scan all rows.
7297
	    Check again if we should use an index.
7298
	    We could have used an column from a previous table in
7299
	    the index if we are using limit and this is the first table
7300
	  */
7301
7302
	  if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7303
	      (!tab->const_keys.is_clear_all() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
7304
	  {
7305
	    /* Join with outer join condition */
7306
	    COND *orig_cond=sel->cond;
7307
	    sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7308
7309
	    /*
7310
              We can't call sel->cond->fix_fields,
7311
              as it will break tab->on_expr if it's AND condition
7312
              (fix_fields currently removes extra AND/OR levels).
7313
              Yet attributes of the just built condition are not needed.
7314
              Thus we call sel->cond->quick_fix_field for safety.
7315
	    */
7316
	    if (sel->cond && !sel->cond->fixed)
7317
	      sel->cond->quick_fix_field();
7318
7319
	    if (sel->test_quick_select(thd, tab->keys,
7320
				       used_tables & ~ current_map,
7321
				       (join->select_options &
7322
					OPTION_FOUND_ROWS ?
7323
					HA_POS_ERROR :
7324
					join->unit->select_limit_cnt), 0,
7325
                                        FALSE) < 0)
7326
            {
7327
	      /*
7328
		Before reporting "Impossible WHERE" for the whole query
7329
		we have to check isn't it only "impossible ON" instead
7330
	      */
7331
              sel->cond=orig_cond;
7332
              if (!*tab->on_expr_ref ||
7333
                  sel->test_quick_select(thd, tab->keys,
7334
                                         used_tables & ~ current_map,
7335
                                         (join->select_options &
7336
                                          OPTION_FOUND_ROWS ?
7337
                                          HA_POS_ERROR :
7338
                                          join->unit->select_limit_cnt),0,
7339
                                          FALSE) < 0)
7340
		DBUG_RETURN(1);			// Impossible WHERE
7341
            }
7342
            else
7343
	      sel->cond=orig_cond;
7344
7345
	    /* Fix for EXPLAIN */
7346
	    if (sel->quick)
7347
	      join->best_positions[i].records_read= (double)sel->quick->records;
7348
	  }
7349
	  else
7350
	  {
7351
	    sel->needed_reg=tab->needed_reg;
7352
	    sel->quick_keys.clear_all();
7353
	  }
7354
	  if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7355
              !sel->needed_reg.is_subset(tab->checked_keys))
7356
	  {
7357
	    tab->keys=sel->quick_keys;
7358
            tab->keys.merge(sel->needed_reg);
7359
	    tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7360
			     (select->quick_keys.is_clear_all() ||
7361
			      (select->quick &&
7362
			       (select->quick->records >= 100L)))) ?
7363
	      2 : 1;
7364
	    sel->read_tables= used_tables & ~current_map;
7365
	  }
7366
	  if (i != join->const_tables && tab->use_quick != 2)
7367
	  {					/* Read with cache */
7368
	    if (cond &&
7369
                (tmp=make_cond_for_table(cond,
7370
					 join->const_table_map |
7371
					 current_map,
7372
					 current_map, 0)))
7373
	    {
7374
              DBUG_EXECUTE("where",print_where(tmp,"cache", QT_ORDINARY););
7375
	      tab->cache.select=(SQL_SELECT*)
7376
		thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7377
	      tab->cache.select->cond=tmp;
7378
	      tab->cache.select->read_tables=join->const_table_map;
7379
	    }
7380
	  }
7381
	}
7382
      }
7383
      
7384
      /* 
7385
        Push down conditions from all on expressions.
7386
        Each of these conditions are guarded by a variable
7387
        that turns if off just before null complemented row for
7388
        outer joins is formed. Thus, the condition from an
7389
        'on expression' are guaranteed not to be checked for
7390
        the null complemented row.
7391
      */ 
7392
7393
      /* First push down constant conditions from on expressions */
7394
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7395
           join_tab < join->join_tab+join->tables ; join_tab++)
7396
      {
7397
        if (*join_tab->on_expr_ref)
7398
        {
7399
          JOIN_TAB *cond_tab= join_tab->first_inner;
7400
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7401
                                         join->const_table_map,
7402
                                         (table_map) 0, 0);
7403
          if (!tmp)
7404
            continue;
7405
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7406
          if (!tmp)
7407
            DBUG_RETURN(1);
7408
          tmp->quick_fix_field();
7409
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7410
	                            new Item_cond_and(cond_tab->select_cond,tmp);
7411
          if (!cond_tab->select_cond)
7412
	    DBUG_RETURN(1);
7413
          cond_tab->select_cond->quick_fix_field();
7414
        }       
7415
      }
7416
7417
      /* Push down non-constant conditions from on expressions */
7418
      JOIN_TAB *last_tab= tab;
7419
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7420
      {  
7421
        /* 
7422
          Table tab is the last inner table of an outer join.
7423
          An on expression is always attached to it.
7424
	*/     
7425
        COND *on_expr= *first_inner_tab->on_expr_ref;
7426
7427
        table_map used_tables2= (join->const_table_map |
7428
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7429
	for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7430
        {
7431
          current_map= tab->table->map;
7432
          used_tables2|= current_map;
7433
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7434
                                              current_map, 0);
7435
          if (tmp_cond)
7436
          {
7437
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7438
            /*
7439
              First add the guards for match variables of
7440
              all embedding outer join operations.
7441
	    */
7442
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7443
                                                     tmp_cond,
7444
                                                     first_inner_tab)))
7445
              DBUG_RETURN(1);
7446
            /* 
7447
              Now add the guard turning the predicate off for 
7448
              the null complemented row.
7449
	    */ 
7450
            DBUG_PRINT("info", ("Item_func_trig_cond"));
7451
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7452
                                              &first_inner_tab->
7453
                                              not_null_compl);
7454
            DBUG_PRINT("info", ("Item_func_trig_cond 0x%lx",
7455
                                (ulong) tmp_cond));
7456
            if (tmp_cond)
7457
              tmp_cond->quick_fix_field();
7458
	    /* Add the predicate to other pushed down predicates */
7459
            DBUG_PRINT("info", ("Item_cond_and"));
7460
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7461
	                          new Item_cond_and(cond_tab->select_cond,
7462
                                                    tmp_cond);
7463
            DBUG_PRINT("info", ("Item_cond_and 0x%lx",
7464
                                (ulong)cond_tab->select_cond));
7465
            if (!cond_tab->select_cond)
7466
	      DBUG_RETURN(1);
7467
            cond_tab->select_cond->quick_fix_field();
7468
          }              
7469
        }
7470
        first_inner_tab= first_inner_tab->first_upper;       
7471
      }
7472
    }
7473
  }
7474
  DBUG_RETURN(0);
7475
}
7476
7477
7478
/* 
7479
  Check if given expression uses only table fields covered by the given index
7480
7481
  SYNOPSIS
7482
    uses_index_fields_only()
7483
      item           Expression to check
7484
      tbl            The table having the index
7485
      keyno          The index number
7486
      other_tbls_ok  TRUE <=> Fields of other non-const tables are allowed
7487
7488
  DESCRIPTION
7489
    Check if given expression only uses fields covered by index #keyno in the
7490
    table tbl. The expression can use any fields in any other tables.
7491
    
7492
    The expression is guaranteed not to be AND or OR - those constructs are 
7493
    handled outside of this function.
7494
7495
  RETURN
7496
    TRUE   Yes
7497
    FALSE  No
7498
*/
7499
7500
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
7501
                            bool other_tbls_ok)
7502
{
7503
  if (item->const_item())
7504
    return TRUE;
7505
7506
  /* 
7507
    Don't push down the triggered conditions. Nested outer joins execution 
7508
    code may need to evaluate a condition several times (both triggered and
7509
    untriggered), and there is no way to put thi
7510
    TODO: Consider cloning the triggered condition and using the copies for:
7511
      1. push the first copy down, to have most restrictive index condition
7512
         possible
7513
      2. Put the second copy into tab->select_cond. 
7514
  */
7515
  if (item->type() == Item::FUNC_ITEM && 
7516
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7517
    return FALSE;
7518
7519
  if (!(item->used_tables() & tbl->map))
7520
    return other_tbls_ok;
7521
7522
  Item::Type item_type= item->type();
7523
  switch (item_type) {
7524
  case Item::FUNC_ITEM:
7525
    {
7526
      /* This is a function, apply condition recursively to arguments */
7527
      Item_func *item_func= (Item_func*)item;
7528
      Item **child;
7529
      Item **item_end= (item_func->arguments()) + item_func->argument_count();
7530
      for (child= item_func->arguments(); child != item_end; child++)
7531
      {
7532
        if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
7533
          return FALSE;
7534
      }
7535
      return TRUE;
7536
    }
7537
  case Item::COND_ITEM:
7538
    {
7539
      /* This is a function, apply condition recursively to arguments */
7540
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7541
      Item *item;
7542
      while ((item=li++))
7543
      {
7544
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7545
          return FALSE;
7546
      }
7547
      return TRUE;
7548
    }
7549
  case Item::FIELD_ITEM:
7550
    {
7551
      Item_field *item_field= (Item_field*)item;
7552
      if (item_field->field->table != tbl) 
7553
        return TRUE;
7554
      return item_field->field->part_of_key.is_set(keyno);
7555
    }
7556
  case Item::REF_ITEM:
7557
    return uses_index_fields_only(item->real_item(), tbl, keyno,
7558
                                  other_tbls_ok);
7559
  default:
7560
    return FALSE; /* Play it safe, don't push unknown non-const items */
7561
  }
7562
}
7563
7564
7565
#define ICP_COND_USES_INDEX_ONLY 10
7566
7567
/*
7568
  Get a part of the condition that can be checked using only index fields
7569
7570
  SYNOPSIS
7571
    make_cond_for_index()
7572
      cond           The source condition
7573
      table          The table that is partially available
7574
      keyno          The index in the above table. Only fields covered by the index
7575
                     are available
7576
      other_tbls_ok  TRUE <=> Fields of other non-const tables are allowed
7577
7578
  DESCRIPTION
7579
    Get a part of the condition that can be checked when for the given table 
7580
    we have values only of fields covered by some index. The condition may
7581
    refer to other tables, it is assumed that we have values of all of their 
7582
    fields.
7583
7584
    Example:
7585
      make_cond_for_index(
7586
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7587
          t2, keyno(t2.key1)) 
7588
      will return
7589
        "cond(t1.field) AND cond(t2.key2)"
7590
7591
  RETURN
7592
    Index condition, or NULL if no condition could be inferred.
7593
*/
7594
7595
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7596
                          bool other_tbls_ok)
7597
{
7598
  if (!cond)
7599
    return NULL;
7600
  if (cond->type() == Item::COND_ITEM)
7601
  {
7602
    uint n_marked= 0;
7603
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7604
    {
7605
      Item_cond_and *new_cond=new Item_cond_and;
7606
      if (!new_cond)
7607
	return (COND*) 0;
7608
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7609
      Item *item;
7610
      while ((item=li++))
7611
      {
7612
	Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7613
	if (fix)
7614
	  new_cond->argument_list()->push_back(fix);
7615
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7616
      }
7617
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7618
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7619
      switch (new_cond->argument_list()->elements) {
7620
      case 0:
7621
	return (COND*) 0;
7622
      case 1:
7623
	return new_cond->argument_list()->head();
7624
      default:
7625
	new_cond->quick_fix_field();
7626
	return new_cond;
7627
      }
7628
    }
7629
    else /* It's OR */
7630
    {
7631
      Item_cond_or *new_cond=new Item_cond_or;
7632
      if (!new_cond)
7633
	return (COND*) 0;
7634
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7635
      Item *item;
7636
      while ((item=li++))
7637
      {
7638
	Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7639
	if (!fix)
7640
	  return (COND*) 0;
7641
	new_cond->argument_list()->push_back(fix);
7642
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7643
      }
7644
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7645
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7646
      new_cond->quick_fix_field();
7647
      new_cond->top_level_item();
7648
      return new_cond;
7649
    }
7650
  }
7651
7652
  if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
7653
    return (COND*) 0;
7654
  cond->marker= ICP_COND_USES_INDEX_ONLY;
7655
  return cond;
7656
}
7657
7658
7659
Item *make_cond_remainder(Item *cond, bool exclude_index)
7660
{
7661
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7662
    return 0; /* Already checked */
7663
7664
  if (cond->type() == Item::COND_ITEM)
7665
  {
7666
    table_map tbl_map= 0;
7667
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7668
    {
7669
      /* Create new top level AND item */
7670
      Item_cond_and *new_cond=new Item_cond_and;
7671
      if (!new_cond)
7672
	return (COND*) 0;
7673
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7674
      Item *item;
7675
      while ((item=li++))
7676
      {
7677
	Item *fix= make_cond_remainder(item, exclude_index);
7678
	if (fix)
7679
        {
7680
	  new_cond->argument_list()->push_back(fix);
7681
          tbl_map |= fix->used_tables();
7682
        }
7683
      }
7684
      switch (new_cond->argument_list()->elements) {
7685
      case 0:
7686
	return (COND*) 0;
7687
      case 1:
7688
	return new_cond->argument_list()->head();
7689
      default:
7690
	new_cond->quick_fix_field();
7691
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7692
	return new_cond;
7693
      }
7694
    }
7695
    else /* It's OR */
7696
    {
7697
      Item_cond_or *new_cond=new Item_cond_or;
7698
      if (!new_cond)
7699
	return (COND*) 0;
7700
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7701
      Item *item;
7702
      while ((item=li++))
7703
      {
7704
	Item *fix= make_cond_remainder(item, FALSE);
7705
	if (!fix)
7706
	  return (COND*) 0;
7707
	new_cond->argument_list()->push_back(fix);
7708
        tbl_map |= fix->used_tables();
7709
      }
7710
      new_cond->quick_fix_field();
7711
      ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7712
      new_cond->top_level_item();
7713
      return new_cond;
7714
    }
7715
  }
7716
  return cond;
7717
}
7718
7719
7720
/*
7721
  Try to extract and push the index condition
7722
7723
  SYNOPSIS
7724
    push_index_cond()
7725
      tab            A join tab that has tab->table->file and its condition
7726
                     in tab->select_cond
7727
      keyno          Index for which extract and push the condition
7728
      other_tbls_ok  TRUE <=> Fields of other non-const tables are allowed
7729
7730
  DESCRIPTION
7731
    Try to extract and push the index condition down to table handler
7732
*/
7733
7734
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7735
{
7736
  DBUG_ENTER("push_index_cond");
7737
  Item *idx_cond;
7738
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7739
      tab->join->thd->variables.engine_condition_pushdown)
7740
  {
7741
    DBUG_EXECUTE("where",
7742
                 print_where(tab->select_cond, "full cond", QT_ORDINARY););
7743
7744
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7745
                                  other_tbls_ok);
7746
7747
    DBUG_EXECUTE("where",
7748
                 print_where(idx_cond, "idx cond", QT_ORDINARY););
7749
7750
    if (idx_cond)
7751
    {
7752
      tab->pre_idx_push_select_cond= tab->select_cond;
7753
      Item *idx_remainder_cond= 
7754
        tab->table->file->idx_cond_push(keyno, idx_cond);
7755
7756
      /*
7757
        Disable eq_ref's "lookup cache" if we've pushed down an index
7758
        condition. 
7759
        TODO: This check happens to work on current ICP implementations, but
7760
        there may exist a compliant implementation that will not work 
7761
        correctly with it. Sort this out when we stabilize the condition
7762
        pushdown APIs.
7763
      */
7764
      if (idx_remainder_cond != idx_cond)
7765
        tab->ref.disable_cache= TRUE;
7766
7767
      Item *row_cond= make_cond_remainder(tab->select_cond, TRUE);
7768
7769
      DBUG_EXECUTE("where",
7770
                   print_where(row_cond, "remainder cond", QT_ORDINARY););
7771
      
7772
      if (row_cond)
7773
      {
7774
        if (!idx_remainder_cond)
7775
          tab->select_cond= row_cond;
7776
        else
7777
        {
7778
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7779
	  tab->select_cond->quick_fix_field();
7780
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
7781
            row_cond->used_tables() | idx_remainder_cond->used_tables();
7782
        }
7783
      }
7784
      else
7785
        tab->select_cond= idx_remainder_cond;
7786
      if (tab->select)
7787
      {
7788
        DBUG_EXECUTE("where",
7789
                     print_where(tab->select->cond,
7790
                                 "select_cond",
7791
                                 QT_ORDINARY););
7792
7793
        tab->select->cond= tab->select_cond;
7794
      }
7795
    }
7796
  }
7797
  DBUG_VOID_RETURN;
7798
}
7799
7800
7801
7802
    /*
7803
      Determine if the set is already ordered for ORDER BY, so it can 
7804
      disable join cache because it will change the ordering of the results.
7805
      Code handles sort table that is at any location (not only first after 
7806
      the const tables) despite the fact that it's currently prohibited.
7807
      We must disable join cache if the first non-const table alone is
7808
      ordered. If there is a temp table the ordering is done as a last
7809
      operation and doesn't prevent join cache usage.
7810
    */
7811
uint make_join_orderinfo(JOIN *join)
7812
{
7813
  uint i;
7814
  if (join->need_tmp)
7815
    return join->tables;
7816
7817
  for (i=join->const_tables ; i < join->tables ; i++)
7818
  {
7819
    JOIN_TAB *tab=join->join_tab+i;
7820
    TABLE *table=tab->table;
7821
    if ((table == join->sort_by_table && 
7822
         (!join->order || join->skip_sort_order)) ||
7823
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7824
    {
7825
      break;
7826
    }
7827
  }
7828
  return i;
7829
}
7830
7831
7832
/*
7833
  Plan refinement stage: do various set ups for the executioner
7834
7835
  SYNOPSIS
7836
    make_join_readinfo()
7837
      join           Join being processed
7838
      options        Join's options (checking for SELECT_DESCRIBE, 
7839
                     SELECT_NO_JOIN_CACHE)
7840
      no_jbuf_after  Don't use join buffering after table with this number.
7841
7842
  DESCRIPTION
7843
    Plan refinement stage: do various set ups for the executioner
7844
      - set up use of join buffering
7845
      - push index conditions
7846
      - increment counters
7847
      - etc
7848
7849
  RETURN 
7850
    FALSE - OK
7851
    TRUE  - Out of memory
7852
*/
7853
7854
static bool
7855
make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
7856
{
7857
  uint i;
7858
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7859
  bool sorted= 1;
7860
  DBUG_ENTER("make_join_readinfo");
7861
7862
  for (i=join->const_tables ; i < join->tables ; i++)
7863
  {
7864
    JOIN_TAB *tab=join->join_tab+i;
7865
    TABLE *table=tab->table;
7866
    bool using_join_cache;
7867
    tab->read_record.table= table;
7868
    tab->read_record.file=table->file;
7869
    tab->next_select=sub_select;		/* normal select */
7870
    /* 
7871
      TODO: don't always instruct first table's ref/range access method to 
7872
      produce sorted output.
7873
    */
7874
    tab->sorted= sorted;
7875
    sorted= 0;                                  // only first must be sorted
7876
    if (tab->insideout_match_tab)
7877
    {
7878
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7879
                                                         [tab->index].
7880
                                                         key_length)))
7881
        return TRUE;
7882
    }
7883
    switch (tab->type) {
7884
    case JT_SYSTEM:				// Only happens with left join
7885
      table->status=STATUS_NO_RECORD;
7886
      tab->read_first_record= join_read_system;
7887
      tab->read_record.read_record= join_no_more_records;
7888
      break;
7889
    case JT_CONST:				// Only happens with left join
7890
      table->status=STATUS_NO_RECORD;
7891
      tab->read_first_record= join_read_const;
7892
      tab->read_record.read_record= join_no_more_records;
7893
      if (table->covering_keys.is_set(tab->ref.key) &&
7894
          !table->no_keyread)
7895
      {
7896
        table->key_read=1;
7897
        table->file->extra(HA_EXTRA_KEYREAD);
7898
      }
7899
      break;
7900
    case JT_EQ_REF:
7901
      table->status=STATUS_NO_RECORD;
7902
      if (tab->select)
7903
      {
7904
	delete tab->select->quick;
7905
	tab->select->quick=0;
7906
      }
7907
      delete tab->quick;
7908
      tab->quick=0;
7909
      tab->read_first_record= join_read_key;
7910
      tab->read_record.read_record= join_no_more_records;
7911
      if (table->covering_keys.is_set(tab->ref.key) &&
7912
	  !table->no_keyread)
7913
      {
7914
	table->key_read=1;
7915
	table->file->extra(HA_EXTRA_KEYREAD);
7916
      }
7917
      else
7918
        push_index_cond(tab, tab->ref.key, TRUE);
7919
      break;
7920
    case JT_REF_OR_NULL:
7921
    case JT_REF:
7922
      table->status=STATUS_NO_RECORD;
7923
      if (tab->select)
7924
      {
7925
	delete tab->select->quick;
7926
	tab->select->quick=0;
7927
      }
7928
      delete tab->quick;
7929
      tab->quick=0;
7930
      if (table->covering_keys.is_set(tab->ref.key) &&
7931
	  !table->no_keyread)
7932
      {
7933
	table->key_read=1;
7934
	table->file->extra(HA_EXTRA_KEYREAD);
7935
      }
7936
      else
7937
        push_index_cond(tab, tab->ref.key, TRUE);
7938
      if (tab->type == JT_REF)
7939
      {
7940
	tab->read_first_record= join_read_always_key;
7941
	tab->read_record.read_record= tab->insideout_match_tab? 
7942
           join_read_next_same_diff : join_read_next_same;
7943
      }
7944
      else
7945
      {
7946
	tab->read_first_record= join_read_always_key_or_null;
7947
	tab->read_record.read_record= join_read_next_same_or_null;
7948
      }
7949
      break;
7950
    case JT_ALL:
7951
      /*
7952
	If previous table use cache
7953
        If the incoming data set is already sorted don't use cache.
7954
      */
7955
      table->status=STATUS_NO_RECORD;
7956
      using_join_cache= FALSE;
7957
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7958
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7959
          !tab->insideout_match_tab)
7960
      {
7961
	if ((options & SELECT_DESCRIBE) ||
7962
	    !join_init_cache(join->thd,join->join_tab+join->const_tables,
7963
			     i-join->const_tables))
7964
	{
7965
          using_join_cache= TRUE;
7966
	  tab[-1].next_select=sub_select_cache; /* Patch previous */
7967
	}
7968
      }
7969
      /* These init changes read_record */
7970
      if (tab->use_quick == 2)
7971
      {
7972
	join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7973
	tab->read_first_record= join_init_quick_read_record;
7974
	if (statistics)
7975
	  status_var_increment(join->thd->status_var.select_range_check_count);
7976
      }
7977
      else
7978
      {
7979
	tab->read_first_record= join_init_read_record;
7980
	if (i == join->const_tables)
7981
	{
7982
	  if (tab->select && tab->select->quick)
7983
	  {
7984
	    if (statistics)
7985
	      status_var_increment(join->thd->status_var.select_range_count);
7986
	  }
7987
	  else
7988
	  {
7989
	    join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7990
	    if (statistics)
7991
	      status_var_increment(join->thd->status_var.select_scan_count);
7992
	  }
7993
	}
7994
	else
7995
	{
7996
	  if (tab->select && tab->select->quick)
7997
	  {
7998
	    if (statistics)
7999
	      status_var_increment(join->thd->status_var.select_full_range_join_count);
8000
	  }
8001
	  else
8002
	  {
8003
	    join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
8004
	    if (statistics)
8005
	      status_var_increment(join->thd->status_var.select_full_join_count);
8006
	  }
8007
	}
8008
	if (!table->no_keyread)
8009
	{
8010
	  if (tab->select && tab->select->quick &&
8011
              tab->select->quick->index != MAX_KEY && //not index_merge
8012
	      table->covering_keys.is_set(tab->select->quick->index))
8013
	  {
8014
	    table->key_read=1;
8015
	    table->file->extra(HA_EXTRA_KEYREAD);
8016
	  }
8017
	  else if (!table->covering_keys.is_clear_all() &&
8018
		   !(tab->select && tab->select->quick))
8019
	  {					// Only read index tree
8020
            if (!tab->insideout_match_tab)
8021
            {
8022
              /*
8023
                See bug #26447: "Using the clustered index for a table scan
8024
                is always faster than using a secondary index".
8025
              */
8026
              if (table->s->primary_key != MAX_KEY &&
8027
                  table->file->primary_key_is_clustered())
8028
                tab->index= table->s->primary_key;
8029
              else
8030
                tab->index=find_shortest_key(table, & table->covering_keys);
8031
            }
8032
	    tab->read_first_record= join_read_first;
8033
	    tab->type=JT_NEXT;		// Read with index_first / index_next
8034
	  }
8035
	}
8036
        if (tab->select && tab->select->quick &&
8037
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
8038
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
8039
      }
8040
      break;
8041
    default:
8042
      DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */
8043
      break;					/* purecov: deadcode */
8044
    case JT_UNKNOWN:
8045
    case JT_MAYBE_REF:
8046
      abort();					/* purecov: deadcode */
8047
    }
8048
  }
8049
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
8050
  DBUG_RETURN(FALSE);
8051
}
8052
8053
8054
/**
8055
  Give error if we some tables are done with a full join.
8056
8057
  This is used by multi_table_update and multi_table_delete when running
8058
  in safe mode.
8059
8060
  @param join		Join condition
8061
8062
  @retval
8063
    0	ok
8064
  @retval
8065
    1	Error (full join used)
8066
*/
8067
8068
bool error_if_full_join(JOIN *join)
8069
{
8070
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
8071
       tab < end;
8072
       tab++)
8073
  {
8074
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
8075
    {
8076
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
8077
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
8078
      return(1);
8079
    }
8080
  }
8081
  return(0);
8082
}
8083
8084
8085
/**
8086
  cleanup JOIN_TAB.
8087
*/
8088
8089
void JOIN_TAB::cleanup()
8090
{
8091
  delete select;
8092
  select= 0;
8093
  delete quick;
8094
  quick= 0;
8095
  x_free(cache.buff);
8096
  cache.buff= 0;
8097
  limit= 0;
8098
  if (table)
8099
  {
8100
    if (table->key_read)
8101
    {
8102
      table->key_read= 0;
8103
      table->file->extra(HA_EXTRA_NO_KEYREAD);
8104
    }
8105
    table->file->ha_index_or_rnd_end();
8106
    /*
8107
      We need to reset this for next select
8108
      (Tested in part_of_refkey)
8109
    */
8110
    table->reginfo.join_tab= 0;
8111
  }
8112
  end_read_record(&read_record);
8113
}
8114
8115
8116
/**
8117
  Partially cleanup JOIN after it has executed: close index or rnd read
8118
  (table cursors), free quick selects.
8119
8120
    This function is called in the end of execution of a JOIN, before the used
8121
    tables are unlocked and closed.
8122
8123
    For a join that is resolved using a temporary table, the first sweep is
8124
    performed against actual tables and an intermediate result is inserted
8125
    into the temprorary table.
8126
    The last sweep is performed against the temporary table. Therefore,
8127
    the base tables and associated buffers used to fill the temporary table
8128
    are no longer needed, and this function is called to free them.
8129
8130
    For a join that is performed without a temporary table, this function
8131
    is called after all rows are sent, but before EOF packet is sent.
8132
8133
    For a simple SELECT with no subqueries this function performs a full
8134
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
8135
    tables.
8136
8137
    If a JOIN is executed for a subquery or if it has a subquery, we can't
8138
    do the full cleanup and need to do a partial cleanup only.
8139
    - If a JOIN is not the top level join, we must not unlock the tables
8140
    because the outer select may not have been evaluated yet, and we
8141
    can't unlock only selected tables of a query.
8142
    - Additionally, if this JOIN corresponds to a correlated subquery, we
8143
    should not free quick selects and join buffers because they will be
8144
    needed for the next execution of the correlated subquery.
8145
    - However, if this is a JOIN for a [sub]select, which is not
8146
    a correlated subquery itself, but has subqueries, we can free it
8147
    fully and also free JOINs of all its subqueries. The exception
8148
    is a subquery in SELECT list, e.g: @n
8149
    SELECT a, (select max(b) from t1) group by c @n
8150
    This subquery will not be evaluated at first sweep and its value will
8151
    not be inserted into the temporary table. Instead, it's evaluated
8152
    when selecting from the temporary table. Therefore, it can't be freed
8153
    here even though it's not correlated.
8154
8155
  @todo
8156
    Unlock tables even if the join isn't top level select in the tree
8157
*/
8158
8159
void JOIN::join_free()
8160
{
8161
  SELECT_LEX_UNIT *tmp_unit;
8162
  SELECT_LEX *sl;
8163
  /*
8164
    Optimization: if not EXPLAIN and we are done with the JOIN,
8165
    free all tables.
8166
  */
8167
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
8168
  bool can_unlock= full;
8169
  DBUG_ENTER("JOIN::join_free");
8170
8171
  cleanup(full);
8172
8173
  for (tmp_unit= select_lex->first_inner_unit();
8174
       tmp_unit;
8175
       tmp_unit= tmp_unit->next_unit())
8176
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
8177
    {
8178
      Item_subselect *subselect= sl->master_unit()->item;
8179
      bool full_local= full && (!subselect || subselect->is_evaluated());
8180
      /*
8181
        If this join is evaluated, we can fully clean it up and clean up all
8182
        its underlying joins even if they are correlated -- they will not be
8183
        used any more anyway.
8184
        If this join is not yet evaluated, we still must clean it up to
8185
        close its table cursors -- it may never get evaluated, as in case of
8186
        ... HAVING FALSE OR a IN (SELECT ...))
8187
        but all table cursors must be closed before the unlock.
8188
      */
8189
      sl->cleanup_all_joins(full_local);
8190
      /* Can't unlock if at least one JOIN is still needed */
8191
      can_unlock= can_unlock && full_local;
8192
    }
8193
8194
  /*
8195
    We are not using tables anymore
8196
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8197
  */
8198
  if (can_unlock && lock && thd->lock &&
8199
      !(select_options & SELECT_NO_UNLOCK) &&
8200
      !select_lex->subquery_in_having &&
8201
      (select_lex == (thd->lex->unit.fake_select_lex ?
8202
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
8203
  {
8204
    /*
8205
      TODO: unlock tables even if the join isn't top level select in the
8206
      tree.
8207
    */
8208
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
8209
    lock= 0;
8210
  }
8211
8212
  DBUG_VOID_RETURN;
8213
}
8214
8215
8216
/**
8217
  Free resources of given join.
8218
8219
  @param fill   true if we should free all resources, call with full==1
8220
                should be last, before it this function can be called with
8221
                full==0
8222
8223
  @note
8224
    With subquery this function definitely will be called several times,
8225
    but even for simple query it can be called several times.
8226
*/
8227
8228
void JOIN::cleanup(bool full)
8229
{
8230
  DBUG_ENTER("JOIN::cleanup");
8231
8232
  if (table)
8233
  {
8234
    JOIN_TAB *tab,*end;
8235
    /*
8236
      Only a sorted table may be cached.  This sorted table is always the
8237
      first non const table in join->table
8238
    */
8239
    if (tables > const_tables) // Test for not-const tables
8240
    {
8241
      free_io_cache(table[const_tables]);
8242
      filesort_free_buffers(table[const_tables],full);
8243
    }
8244
8245
    if (full)
8246
    {
8247
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8248
	tab->cleanup();
8249
      table= 0;
8250
    }
8251
    else
8252
    {
8253
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8254
      {
8255
	if (tab->table)
8256
          tab->table->file->ha_index_or_rnd_end();
8257
      }
8258
    }
8259
    cleanup_sj_tmp_tables(this);//
8260
  }
8261
  /*
8262
    We are not using tables anymore
8263
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8264
  */
8265
  if (full)
8266
  {
8267
    if (tmp_join)
8268
      tmp_table_param.copy_field= 0;
8269
    group_fields.delete_elements();
8270
    /*
8271
      We can't call delete_elements() on copy_funcs as this will cause
8272
      problems in free_elements() as some of the elements are then deleted.
8273
    */
8274
    tmp_table_param.copy_funcs.empty();
8275
    /*
8276
      If we have tmp_join and 'this' JOIN is not tmp_join and
8277
      tmp_table_param.copy_field's  of them are equal then we have to remove
8278
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
8279
      be removed in tmp_table_param.cleanup().
8280
    */
8281
    if (tmp_join &&
8282
        tmp_join != this &&
8283
        tmp_join->tmp_table_param.copy_field ==
8284
        tmp_table_param.copy_field)
8285
    {
8286
      tmp_join->tmp_table_param.copy_field=
8287
        tmp_join->tmp_table_param.save_copy_field= 0;
8288
    }
8289
    tmp_table_param.cleanup();
8290
  }
8291
  DBUG_VOID_RETURN;
8292
}
8293
8294
8295
/**
8296
  Remove the following expressions from ORDER BY and GROUP BY:
8297
  Constant expressions @n
8298
  Expression that only uses tables that are of type EQ_REF and the reference
8299
  is in the ORDER list or if all refereed tables are of the above type.
8300
8301
  In the following, the X field can be removed:
8302
  @code
8303
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
8304
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
8305
  @endcode
8306
8307
  These can't be optimized:
8308
  @code
8309
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
8310
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
8311
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
8312
  @endcode
8313
*/
8314
8315
static bool
8316
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
8317
{
8318
  if (tab->cached_eq_ref_table)			// If cached
8319
    return tab->eq_ref_table;
8320
  tab->cached_eq_ref_table=1;
8321
  /* We can skip const tables only if not an outer table */
8322
  if (tab->type == JT_CONST && !tab->first_inner)
8323
    return (tab->eq_ref_table=1);		/* purecov: inspected */
8324
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
8325
    return (tab->eq_ref_table=0);		// We must use this
8326
  Item **ref_item=tab->ref.items;
8327
  Item **end=ref_item+tab->ref.key_parts;
8328
  uint found=0;
8329
  table_map map=tab->table->map;
8330
8331
  for (; ref_item != end ; ref_item++)
8332
  {
8333
    if (! (*ref_item)->const_item())
8334
    {						// Not a const ref
8335
      ORDER *order;
8336
      for (order=start_order ; order ; order=order->next)
8337
      {
8338
	if ((*ref_item)->eq(order->item[0],0))
8339
	  break;
8340
      }
8341
      if (order)
8342
      {
8343
	found++;
8344
	DBUG_ASSERT(!(order->used & map));
8345
	order->used|=map;
8346
	continue;				// Used in ORDER BY
8347
      }
8348
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8349
	return (tab->eq_ref_table=0);
8350
    }
8351
  }
8352
  /* Check that there was no reference to table before sort order */
8353
  for (; found && start_order ; start_order=start_order->next)
8354
  {
8355
    if (start_order->used & map)
8356
    {
8357
      found--;
8358
      continue;
8359
    }
8360
    if (start_order->depend_map & map)
8361
      return (tab->eq_ref_table=0);
8362
  }
8363
  return tab->eq_ref_table=1;
8364
}
8365
8366
8367
static bool
8368
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8369
{
8370
  if (specialflag &  SPECIAL_SAFE_MODE)
8371
    return 0;			// skip this optimize /* purecov: inspected */
8372
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8373
  {
8374
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8375
      return 0;
8376
  }
8377
  return 1;
8378
}
8379
8380
8381
/** Update the dependency map for the tables. */
8382
8383
static void update_depend_map(JOIN *join)
8384
{
8385
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8386
8387
  for (; join_tab != end ; join_tab++)
8388
  {
8389
    TABLE_REF *ref= &join_tab->ref;
8390
    table_map depend_map=0;
8391
    Item **item=ref->items;
8392
    uint i;
8393
    for (i=0 ; i < ref->key_parts ; i++,item++)
8394
      depend_map|=(*item)->used_tables();
8395
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8396
    depend_map&= ~OUTER_REF_TABLE_BIT;
8397
    for (JOIN_TAB **tab=join->map2table;
8398
	 depend_map ;
8399
	 tab++,depend_map>>=1 )
8400
    {
8401
      if (depend_map & 1)
8402
	ref->depend_map|=(*tab)->ref.depend_map;
8403
    }
8404
  }
8405
}
8406
8407
8408
/** Update the dependency map for the sort order. */
8409
8410
static void update_depend_map(JOIN *join, ORDER *order)
8411
{
8412
  for (; order ; order=order->next)
8413
  {
8414
    table_map depend_map;
8415
    order->item[0]->update_used_tables();
8416
    order->depend_map=depend_map=order->item[0]->used_tables();
8417
    // Not item_sum(), RAND() and no reference to table outside of sub select
8418
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8419
        && !order->item[0]->with_sum_func)
8420
    {
8421
      for (JOIN_TAB **tab=join->map2table;
8422
	   depend_map ;
8423
	   tab++, depend_map>>=1)
8424
      {
8425
	if (depend_map & 1)
8426
	  order->depend_map|=(*tab)->ref.depend_map;
8427
      }
8428
    }
8429
  }
8430
}
8431
8432
8433
/**
8434
  Remove all constants and check if ORDER only contains simple
8435
  expressions.
8436
8437
  simple_order is set to 1 if sort_order only uses fields from head table
8438
  and the head table is not a LEFT JOIN table.
8439
8440
  @param join			Join handler
8441
  @param first_order		List of SORT or GROUP order
8442
  @param cond			WHERE statement
8443
  @param change_list		Set to 1 if we should remove things from list.
8444
                               If this is not set, then only simple_order is
8445
                               calculated.
8446
  @param simple_order		Set to 1 if we are only using simple expressions
8447
8448
  @return
8449
    Returns new sort order
8450
*/
8451
8452
static ORDER *
8453
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8454
             bool change_list, bool *simple_order)
8455
{
8456
  if (join->tables == join->const_tables)
8457
    return change_list ? 0 : first_order;		// No need to sort
8458
8459
  ORDER *order,**prev_ptr;
8460
  table_map first_table= join->join_tab[join->const_tables].table->map;
8461
  table_map not_const_tables= ~join->const_table_map;
8462
  table_map ref;
8463
  DBUG_ENTER("remove_const");
8464
8465
  prev_ptr= &first_order;
8466
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8467
8468
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8469
8470
  update_depend_map(join, first_order);
8471
  for (order=first_order; order ; order=order->next)
8472
  {
8473
    table_map order_tables=order->item[0]->used_tables();
8474
    if (order->item[0]->with_sum_func)
8475
      *simple_order=0;				// Must do a temp table to sort
8476
    else if (!(order_tables & not_const_tables))
8477
    {
8478
      if (order->item[0]->with_subselect)
8479
        order->item[0]->val_str(&order->item[0]->str_value);
8480
      DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8481
      continue;					// skip const item
8482
    }
8483
    else
8484
    {
8485
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8486
	*simple_order=0;
8487
      else
8488
      {
8489
	Item *comp_item=0;
8490
	if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8491
	{
8492
	  DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8493
	  continue;
8494
	}
8495
	if ((ref=order_tables & (not_const_tables ^ first_table)))
8496
	{
8497
	  if (!(order_tables & first_table) &&
8498
              only_eq_ref_tables(join,first_order, ref))
8499
	  {
8500
	    DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8501
	    continue;
8502
	  }
8503
	  *simple_order=0;			// Must do a temp table to sort
8504
	}
8505
      }
8506
    }
8507
    if (change_list)
8508
      *prev_ptr= order;				// use this entry
8509
    prev_ptr= &order->next;
8510
  }
8511
  if (change_list)
8512
    *prev_ptr=0;
8513
  if (prev_ptr == &first_order)			// Nothing to sort/group
8514
    *simple_order=1;
8515
  DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order));
8516
  DBUG_RETURN(first_order);
8517
}
8518
8519
8520
static int
8521
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
8522
		 List<Item> &fields, bool send_row, ulonglong select_options,
8523
		 const char *info, Item *having)
8524
{
8525
  DBUG_ENTER("return_zero_rows");
8526
8527
  if (select_options & SELECT_DESCRIBE)
8528
  {
8529
    select_describe(join, FALSE, FALSE, FALSE, info);
8530
    DBUG_RETURN(0);
8531
  }
8532
8533
  join->join_free();
8534
8535
  if (send_row)
8536
  {
8537
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8538
      mark_as_null_row(table->table);		// All fields are NULL
8539
    if (having && having->val_int() == 0)
8540
      send_row=0;
8541
  }
8542
  if (!(result->send_fields(fields,
8543
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8544
  {
8545
    if (send_row)
8546
    {
8547
      List_iterator_fast<Item> it(fields);
8548
      Item *item;
8549
      while ((item= it++))
8550
	item->no_rows_in_result();
8551
      result->send_data(fields);
8552
    }
8553
    result->send_eof();				// Should be safe
8554
  }
8555
  /* Update results for FOUND_ROWS */
8556
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8557
  DBUG_RETURN(0);
8558
}
8559
8560
/*
8561
  used only in JOIN::clear
8562
*/
8563
static void clear_tables(JOIN *join)
8564
{
8565
  /* 
8566
    must clear only the non-const tables, as const tables
8567
    are not re-calculated.
8568
  */
8569
  for (uint i=join->const_tables ; i < join->tables ; i++)
8570
    mark_as_null_row(join->table[i]);		// All fields are NULL
8571
}
8572
8573
/*****************************************************************************
8574
  Make som simple condition optimization:
8575
  If there is a test 'field = const' change all refs to 'field' to 'const'
8576
  Remove all dummy tests 'item = item', 'const op const'.
8577
  Remove all 'item is NULL', when item can never be null!
8578
  item->marker should be 0 for all items on entry
8579
  Return in cond_value FALSE if condition is impossible (1 = 2)
8580
*****************************************************************************/
8581
8582
class COND_CMP :public ilink {
8583
public:
8584
  static void *operator new(size_t size)
8585
  {
8586
    return (void*) sql_alloc((uint) size);
8587
  }
8588
  static void operator delete(void *ptr __attribute__((unused)),
8589
                              size_t size __attribute__((unused)))
8590
  { TRASH(ptr, size); }
8591
8592
  Item *and_level;
8593
  Item_func *cmp_func;
8594
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8595
};
8596
8597
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8598
template class I_List<COND_CMP>;
8599
template class I_List_iterator<COND_CMP>;
8600
#endif
8601
8602
8603
/**
8604
  Find the multiple equality predicate containing a field.
8605
8606
  The function retrieves the multiple equalities accessed through
8607
  the con_equal structure from current level and up looking for
8608
  an equality containing field. It stops retrieval as soon as the equality
8609
  is found and set up inherited_fl to TRUE if it's found on upper levels.
8610
8611
  @param cond_equal          multiple equalities to search in
8612
  @param field               field to look for
8613
  @param[out] inherited_fl   set up to TRUE if multiple equality is found
8614
                             on upper levels (not on current level of
8615
                             cond_equal)
8616
8617
  @return
8618
    - Item_equal for the found multiple equality predicate if a success;
8619
    - NULL otherwise.
8620
*/
8621
8622
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8623
                            bool *inherited_fl)
8624
{
8625
  Item_equal *item= 0;
8626
  bool in_upper_level= FALSE;
8627
  while (cond_equal)
8628
  {
8629
    List_iterator_fast<Item_equal> li(cond_equal->current_level);
8630
    while ((item= li++))
8631
    {
8632
      if (item->contains(field))
8633
        goto finish;
8634
    }
8635
    in_upper_level= TRUE;
8636
    cond_equal= cond_equal->upper_levels;
8637
  }
8638
  in_upper_level= FALSE;
8639
finish:
8640
  *inherited_fl= in_upper_level;
8641
  return item;
8642
}
8643
8644
  
8645
/**
8646
  Check whether an equality can be used to build multiple equalities.
8647
8648
    This function first checks whether the equality (left_item=right_item)
8649
    is a simple equality i.e. the one that equates a field with another field
8650
    or a constant (field=field_item or field=const_item).
8651
    If this is the case the function looks for a multiple equality
8652
    in the lists referenced directly or indirectly by cond_equal inferring
8653
    the given simple equality. If it doesn't find any, it builds a multiple
8654
    equality that covers the predicate, i.e. the predicate can be inferred
8655
    from this multiple equality.
8656
    The built multiple equality could be obtained in such a way:
8657
    create a binary  multiple equality equivalent to the predicate, then
8658
    merge it, if possible, with one of old multiple equalities.
8659
    This guarantees that the set of multiple equalities covering equality
8660
    predicates will be minimal.
8661
8662
  EXAMPLE:
8663
    For the where condition
8664
    @code
8665
      WHERE a=b AND b=c AND
8666
            (b=2 OR f=e)
8667
    @endcode
8668
    the check_equality will be called for the following equality
8669
    predicates a=b, b=c, b=2 and f=e.
8670
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
8671
      *cond_equal into (0,[Item_equal(a,b)]). 
8672
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
8673
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
8674
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
8675
      and will transform *cond_equal into (ptr(CE),[Item_equal(2,a,b,c)]).
8676
    - For f=e it will be called with *cond_equal=(ptr(CE), [])
8677
      and will transform *cond_equal into (ptr(CE),[Item_equal(f,e)]).
8678
8679
  @note
8680
    Now only fields that have the same type definitions (verified by
8681
    the Field::eq_def method) are placed to the same multiple equalities.
8682
    Because of this some equality predicates are not eliminated and
8683
    can be used in the constant propagation procedure.
8684
    We could weeken the equlity test as soon as at least one of the 
8685
    equal fields is to be equal to a constant. It would require a 
8686
    more complicated implementation: we would have to store, in
8687
    general case, its own constant for each fields from the multiple
8688
    equality. But at the same time it would allow us to get rid
8689
    of constant propagation completely: it would be done by the call
8690
    to build_equal_items_for_cond.
8691
8692
8693
    The implementation does not follow exactly the above rules to
8694
    build a new multiple equality for the equality predicate.
8695
    If it processes the equality of the form field1=field2, it
8696
    looks for multiple equalities me1 containig field1 and me2 containing
8697
    field2. If only one of them is found the fuction expands it with
8698
    the lacking field. If multiple equalities for both fields are
8699
    found they are merged. If both searches fail a new multiple equality
8700
    containing just field1 and field2 is added to the existing
8701
    multiple equalities.
8702
    If the function processes the predicate of the form field1=const,
8703
    it looks for a multiple equality containing field1. If found, the 
8704
    function checks the constant of the multiple equality. If the value
8705
    is unknown, it is setup to const. Otherwise the value is compared with
8706
    const and the evaluation of the equality predicate is performed.
8707
    When expanding/merging equality predicates from the upper levels
8708
    the function first copies them for the current level. It looks
8709
    acceptable, as this happens rarely. The implementation without
8710
    copying would be much more complicated.
8711
8712
  @param left_item   left term of the quality to be checked
8713
  @param right_item  right term of the equality to be checked
8714
  @param item        equality item if the equality originates from a condition
8715
                     predicate, 0 if the equality is the result of row
8716
                     elimination
8717
  @param cond_equal  multiple equalities that must hold together with the
8718
                     equality
8719
8720
  @retval
8721
    TRUE    if the predicate is a simple equality predicate to be used
8722
    for building multiple equalities
8723
  @retval
8724
    FALSE   otherwise
8725
*/
8726
8727
static bool check_simple_equality(Item *left_item, Item *right_item,
8728
                                  Item *item, COND_EQUAL *cond_equal)
8729
{
8730
  if (left_item->type() == Item::REF_ITEM &&
8731
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
8732
  {
8733
    if (((Item_ref*)left_item)->depended_from)
8734
      return FALSE;
8735
    left_item= left_item->real_item();
8736
  }
8737
  if (right_item->type() == Item::REF_ITEM &&
8738
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
8739
  {
8740
    if (((Item_ref*)right_item)->depended_from)
8741
      return FALSE;
8742
    right_item= right_item->real_item();
8743
  }
8744
  if (left_item->type() == Item::FIELD_ITEM &&
8745
      right_item->type() == Item::FIELD_ITEM &&
8746
      !((Item_field*)left_item)->depended_from &&
8747
      !((Item_field*)right_item)->depended_from)
8748
  {
8749
    /* The predicate the form field1=field2 is processed */
8750
8751
    Field *left_field= ((Item_field*) left_item)->field;
8752
    Field *right_field= ((Item_field*) right_item)->field;
8753
8754
    if (!left_field->eq_def(right_field))
8755
      return FALSE;
8756
8757
    /* Search for multiple equalities containing field1 and/or field2 */
8758
    bool left_copyfl, right_copyfl;
8759
    Item_equal *left_item_equal=
8760
               find_item_equal(cond_equal, left_field, &left_copyfl);
8761
    Item_equal *right_item_equal= 
8762
               find_item_equal(cond_equal, right_field, &right_copyfl);
8763
8764
    /* As (NULL=NULL) != TRUE we can't just remove the predicate f=f */
8765
    if (left_field->eq(right_field)) /* f = f */
8766
      return (!(left_field->maybe_null() && !left_item_equal)); 
8767
8768
    if (left_item_equal && left_item_equal == right_item_equal)
8769
    {
8770
      /* 
8771
        The equality predicate is inference of one of the existing
8772
        multiple equalities, i.e the condition is already covered
8773
        by upper level equalities
8774
      */
8775
       return TRUE;
8776
    }
8777
    
8778
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
8779
                              item->name < subq_sj_cond_name + 64);
8780
    /* Copy the found multiple equalities at the current level if needed */
8781
    if (left_copyfl)
8782
    {
8783
      /* left_item_equal of an upper level contains left_item */
8784
      left_item_equal= new Item_equal(left_item_equal);
8785
      cond_equal->current_level.push_back(left_item_equal);
8786
      if (copy_item_name)
8787
        left_item_equal->name = item->name;
8788
    }
8789
    if (right_copyfl)
8790
    {
8791
      /* right_item_equal of an upper level contains right_item */
8792
      right_item_equal= new Item_equal(right_item_equal);
8793
      cond_equal->current_level.push_back(right_item_equal);
8794
      if (copy_item_name)
8795
        right_item_equal->name = item->name;
8796
    }
8797
8798
    if (left_item_equal)
8799
    { 
8800
      /* left item was found in the current or one of the upper levels */
8801
      if (! right_item_equal)
8802
        left_item_equal->add((Item_field *) right_item);
8803
      else
8804
      {
8805
        /* Merge two multiple equalities forming a new one */
8806
        left_item_equal->merge(right_item_equal);
8807
        /* Remove the merged multiple equality from the list */
8808
        List_iterator<Item_equal> li(cond_equal->current_level);
8809
        while ((li++) != right_item_equal) {};
8810
        li.remove();
8811
      }
8812
    }
8813
    else
8814
    { 
8815
      /* left item was not found neither the current nor in upper levels  */
8816
      if (right_item_equal)
8817
      {
8818
        right_item_equal->add((Item_field *) left_item);
8819
        if (copy_item_name)
8820
          right_item_equal->name = item->name;
8821
      }
8822
      else 
8823
      {
8824
        /* None of the fields was found in multiple equalities */
8825
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
8826
                                               (Item_field *) right_item);
8827
        cond_equal->current_level.push_back(item_equal);
8828
        if (copy_item_name)
8829
          item_equal->name = item->name;
8830
      }
8831
    }
8832
    return TRUE;
8833
  }
8834
8835
  {
8836
    /* The predicate of the form field=const/const=field is processed */
8837
    Item *const_item= 0;
8838
    Item_field *field_item= 0;
8839
    if (left_item->type() == Item::FIELD_ITEM &&
8840
        !((Item_field*)left_item)->depended_from &&
8841
        right_item->const_item())
8842
    {
8843
      field_item= (Item_field*) left_item;
8844
      const_item= right_item;
8845
    }
8846
    else if (right_item->type() == Item::FIELD_ITEM &&
8847
             !((Item_field*)right_item)->depended_from &&
8848
             left_item->const_item())
8849
    {
8850
      field_item= (Item_field*) right_item;
8851
      const_item= left_item;
8852
    }
8853
8854
    if (const_item &&
8855
        field_item->result_type() == const_item->result_type())
8856
    {
8857
      bool copyfl;
8858
8859
      if (field_item->result_type() == STRING_RESULT)
8860
      {
8861
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
8862
        if (!item)
8863
        {
8864
          Item_func_eq *eq_item;
8865
          if ((eq_item= new Item_func_eq(left_item, right_item)))
8866
            return FALSE;
8867
          eq_item->set_cmp_func();
8868
          eq_item->quick_fix_field();
8869
          item= eq_item;
8870
        }  
8871
        if ((cs != ((Item_func *) item)->compare_collation()) ||
8872
            !cs->coll->propagate(cs, 0, 0))
8873
          return FALSE;
8874
      }
8875
8876
      Item_equal *item_equal = find_item_equal(cond_equal,
8877
                                               field_item->field, &copyfl);
8878
      if (copyfl)
8879
      {
8880
        item_equal= new Item_equal(item_equal);
8881
        cond_equal->current_level.push_back(item_equal);
8882
      }
8883
      if (item_equal)
8884
      {
8885
        /* 
8886
          The flag cond_false will be set to 1 after this, if item_equal
8887
          already contains a constant and its value is  not equal to
8888
          the value of const_item.
8889
        */
8890
        item_equal->add(const_item);
8891
      }
8892
      else
8893
      {
8894
        item_equal= new Item_equal(const_item, field_item);
8895
        cond_equal->current_level.push_back(item_equal);
8896
      }
8897
      return TRUE;
8898
    }
8899
  }
8900
  return FALSE;
8901
}
8902
8903
8904
/**
8905
  Convert row equalities into a conjunction of regular equalities.
8906
8907
    The function converts a row equality of the form (E1,...,En)=(E'1,...,E'n)
8908
    into a list of equalities E1=E'1,...,En=E'n. For each of these equalities
8909
    Ei=E'i the function checks whether it is a simple equality or a row
8910
    equality. If it is a simple equality it is used to expand multiple
8911
    equalities of cond_equal. If it is a row equality it converted to a
8912
    sequence of equalities between row elements. If Ei=E'i is neither a
8913
    simple equality nor a row equality the item for this predicate is added
8914
    to eq_list.
8915
8916
  @param thd        thread handle
8917
  @param left_row   left term of the row equality to be processed
8918
  @param right_row  right term of the row equality to be processed
8919
  @param cond_equal multiple equalities that must hold together with the
8920
                    predicate
8921
  @param eq_list    results of conversions of row equalities that are not
8922
                    simple enough to form multiple equalities
8923
8924
  @retval
8925
    TRUE    if conversion has succeeded (no fatal error)
8926
  @retval
8927
    FALSE   otherwise
8928
*/
8929
 
8930
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8931
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8932
{ 
8933
  uint n= left_row->cols();
8934
  for (uint i= 0 ; i < n; i++)
8935
  {
8936
    bool is_converted;
8937
    Item *left_item= left_row->element_index(i);
8938
    Item *right_item= right_row->element_index(i);
8939
    if (left_item->type() == Item::ROW_ITEM &&
8940
        right_item->type() == Item::ROW_ITEM)
8941
    {
8942
      is_converted= check_row_equality(thd, 
8943
                                       (Item_row *) left_item,
8944
                                       (Item_row *) right_item,
8945
			               cond_equal, eq_list);
8946
      if (!is_converted)
8947
        thd->lex->current_select->cond_count++;      
8948
    }
8949
    else
8950
    { 
8951
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
8952
      thd->lex->current_select->cond_count++;
8953
    }  
8954
 
8955
    if (!is_converted)
8956
    {
8957
      Item_func_eq *eq_item;
8958
      if (!(eq_item= new Item_func_eq(left_item, right_item)))
8959
        return FALSE;
8960
      eq_item->set_cmp_func();
8961
      eq_item->quick_fix_field();
8962
      eq_list->push_back(eq_item);
8963
    }
8964
  }
8965
  return TRUE;
8966
}
8967
8968
8969
/**
8970
  Eliminate row equalities and form multiple equalities predicates.
8971
8972
    This function checks whether the item is a simple equality
8973
    i.e. the one that equates a field with another field or a constant
8974
    (field=field_item or field=constant_item), or, a row equality.
8975
    For a simple equality the function looks for a multiple equality
8976
    in the lists referenced directly or indirectly by cond_equal inferring
8977
    the given simple equality. If it doesn't find any, it builds/expands
8978
    multiple equality that covers the predicate.
8979
    Row equalities are eliminated substituted for conjunctive regular
8980
    equalities which are treated in the same way as original equality
8981
    predicates.
8982
8983
  @param thd        thread handle
8984
  @param item       predicate to process
8985
  @param cond_equal multiple equalities that must hold together with the
8986
                    predicate
8987
  @param eq_list    results of conversions of row equalities that are not
8988
                    simple enough to form multiple equalities
8989
8990
  @retval
8991
    TRUE   if re-writing rules have been applied
8992
  @retval
8993
    FALSE  otherwise, i.e.
8994
           if the predicate is not an equality,
8995
           or, if the equality is neither a simple one nor a row equality,
8996
           or, if the procedure fails by a fatal error.
8997
*/
8998
8999
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
9000
                           List<Item> *eq_list)
9001
{
9002
  if (item->type() == Item::FUNC_ITEM &&
9003
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
9004
  {
9005
    Item *left_item= ((Item_func*) item)->arguments()[0];
9006
    Item *right_item= ((Item_func*) item)->arguments()[1];
9007
9008
    if (left_item->type() == Item::ROW_ITEM &&
9009
        right_item->type() == Item::ROW_ITEM)
9010
    {
9011
      thd->lex->current_select->cond_count--;
9012
      return check_row_equality(thd,
9013
                                (Item_row *) left_item,
9014
                                (Item_row *) right_item,
9015
                                cond_equal, eq_list);
9016
    }
9017
    else 
9018
      return check_simple_equality(left_item, right_item, item, cond_equal);
9019
  } 
9020
  return FALSE;
9021
}
9022
9023
                          
9024
/**
9025
  Replace all equality predicates in a condition by multiple equality items.
9026
9027
    At each 'and' level the function detects items for equality predicates
9028
    and replaced them by a set of multiple equality items of class Item_equal,
9029
    taking into account inherited equalities from upper levels. 
9030
    If an equality predicate is used not in a conjunction it's just
9031
    replaced by a multiple equality predicate.
9032
    For each 'and' level the function set a pointer to the inherited
9033
    multiple equalities in the cond_equal field of the associated
9034
    object of the type Item_cond_and.   
9035
    The function also traverses the cond tree and and for each field reference
9036
    sets a pointer to the multiple equality item containing the field, if there
9037
    is any. If this multiple equality equates fields to a constant the
9038
    function replaces the field reference by the constant in the cases 
9039
    when the field is not of a string type or when the field reference is
9040
    just an argument of a comparison predicate.
9041
    The function also determines the maximum number of members in 
9042
    equality lists of each Item_cond_and object assigning it to
9043
    thd->lex->current_select->max_equal_elems.
9044
9045
  @note
9046
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
9047
    f1=f2, .., fn-1=fn. It substitutes any inference from these
9048
    equality predicates that is equivalent to the conjunction.
9049
    Thus, =(a1,a2,a3) can substitute for ((a1=a3) AND (a2=a3) AND (a2=a1)) as
9050
    it is equivalent to ((a1=a2) AND (a2=a3)).
9051
    The function always makes a substitution of all equality predicates occured
9052
    in a conjuction for a minimal set of multiple equality predicates.
9053
    This set can be considered as a canonical representation of the
9054
    sub-conjunction of the equality predicates.
9055
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
9056
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
9057
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
9058
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
9059
    (=(t1.a,t2.b) AND =(t3.c=t4.d) AND t2.b>5),
9060
    but if additionally =(t4.d,t2.b) is inherited, it
9061
    will be replaced by (=(t1.a,t2.b,t3.c,t4.d) AND t2.b>5)
9062
9063
    The function performs the substitution in a recursive descent by
9064
    the condtion tree, passing to the next AND level a chain of multiple
9065
    equality predicates which have been built at the upper levels.
9066
    The Item_equal items built at the level are attached to other 
9067
    non-equality conjucts as a sublist. The pointer to the inherited
9068
    multiple equalities is saved in the and condition object (Item_cond_and).
9069
    This chain allows us for any field reference occurence easyly to find a 
9070
    multiple equality that must be held for this occurence.
9071
    For each AND level we do the following:
9072
    - scan it for all equality predicate (=) items
9073
    - join them into disjoint Item_equal() groups
9074
    - process the included OR conditions recursively to do the same for 
9075
      lower AND levels. 
9076
9077
    We need to do things in this order as lower AND levels need to know about
9078
    all possible Item_equal objects in upper levels.
9079
9080
  @param thd        thread handle
9081
  @param cond       condition(expression) where to make replacement
9082
  @param inherited  path to all inherited multiple equality items
9083
9084
  @return
9085
    pointer to the transformed condition
9086
*/
9087
9088
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
9089
                                        COND_EQUAL *inherited)
9090
{
9091
  Item_equal *item_equal;
9092
  COND_EQUAL cond_equal;
9093
  cond_equal.upper_levels= inherited;
9094
9095
  if (cond->type() == Item::COND_ITEM)
9096
  {
9097
    List<Item> eq_list;
9098
    bool and_level= ((Item_cond*) cond)->functype() ==
9099
      Item_func::COND_AND_FUNC;
9100
    List<Item> *args= ((Item_cond*) cond)->argument_list();
9101
    
9102
    List_iterator<Item> li(*args);
9103
    Item *item;
9104
9105
    if (and_level)
9106
    {
9107
      /*
9108
         Retrieve all conjucts of this level detecting the equality
9109
         that are subject to substitution by multiple equality items and
9110
         removing each such predicate from the conjunction after having 
9111
         found/created a multiple equality whose inference the predicate is.
9112
     */      
9113
      while ((item= li++))
9114
      {
9115
        /*
9116
          PS/SP note: we can safely remove a node from AND-OR
9117
          structure here because it's restored before each
9118
          re-execution of any prepared statement/stored procedure.
9119
        */
9120
        if (check_equality(thd, item, &cond_equal, &eq_list))
9121
          li.remove();
9122
      }
9123
9124
      List_iterator_fast<Item_equal> it(cond_equal.current_level);
9125
      while ((item_equal= it++))
9126
      {
9127
        item_equal->fix_length_and_dec();
9128
        item_equal->update_used_tables();
9129
        set_if_bigger(thd->lex->current_select->max_equal_elems,
9130
                      item_equal->members());  
9131
      }
9132
9133
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
9134
      inherited= &(((Item_cond_and*)cond)->cond_equal);
9135
    }
9136
    /*
9137
       Make replacement of equality predicates for lower levels
9138
       of the condition expression.
9139
    */
9140
    li.rewind();
9141
    while ((item= li++))
9142
    { 
9143
      Item *new_item;
9144
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
9145
      {
9146
        /* This replacement happens only for standalone equalities */
9147
        /*
9148
          This is ok with PS/SP as the replacement is done for
9149
          arguments of an AND/OR item, which are restored for each
9150
          execution of PS/SP.
9151
        */
9152
        li.replace(new_item);
9153
      }
9154
    }
9155
    if (and_level)
9156
    {
9157
      args->concat(&eq_list);
9158
      args->concat((List<Item> *)&cond_equal.current_level);
9159
    }
9160
  }
9161
  else if (cond->type() == Item::FUNC_ITEM)
9162
  {
9163
    List<Item> eq_list;
9164
    /*
9165
      If an equality predicate forms the whole and level,
9166
      we call it standalone equality and it's processed here.
9167
      E.g. in the following where condition
9168
      WHERE a=5 AND (b=5 or a=c)
9169
      (b=5) and (a=c) are standalone equalities.
9170
      In general we can't leave alone standalone eqalities:
9171
      for WHERE a=b AND c=d AND (b=c OR d=5)
9172
      b=c is replaced by =(a,b,c,d).  
9173
     */
9174
    if (check_equality(thd, cond, &cond_equal, &eq_list))
9175
    {
9176
      int n= cond_equal.current_level.elements + eq_list.elements;
9177
      if (n == 0)
9178
        return new Item_int((longlong) 1,1);
9179
      else if (n == 1)
9180
      {
9181
        if ((item_equal= cond_equal.current_level.pop()))
9182
        {
9183
          item_equal->fix_length_and_dec();
9184
          item_equal->update_used_tables();
9185
	}
9186
        else
9187
          item_equal= (Item_equal *) eq_list.pop();
9188
        set_if_bigger(thd->lex->current_select->max_equal_elems,
9189
                      item_equal->members());  
9190
        return item_equal;
9191
      }
9192
      else
9193
      {
9194
        /* 
9195
          Here a new AND level must be created. It can happen only
9196
          when a row equality is processed as a standalone predicate.
9197
	*/
9198
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
9199
        and_cond->quick_fix_field();
9200
        List<Item> *args= and_cond->argument_list();
9201
        List_iterator_fast<Item_equal> it(cond_equal.current_level);
9202
        while ((item_equal= it++))
9203
        {
9204
          item_equal->fix_length_and_dec();
9205
          item_equal->update_used_tables();
9206
          set_if_bigger(thd->lex->current_select->max_equal_elems,
9207
                        item_equal->members());  
9208
        }
9209
        and_cond->cond_equal= cond_equal;
9210
        args->concat((List<Item> *)&cond_equal.current_level);
9211
        
9212
        return and_cond;
9213
      }
9214
    }
9215
    /* 
9216
      For each field reference in cond, not from equal item predicates,
9217
      set a pointer to the multiple equality it belongs to (if there is any)
9218
      as soon the field is not of a string type or the field reference is
9219
      an argument of a comparison predicate. 
9220
    */ 
9221
    uchar *is_subst_valid= (uchar *) 1;
9222
    cond= cond->compile(&Item::subst_argument_checker,
9223
                        &is_subst_valid, 
9224
                        &Item::equal_fields_propagator,
9225
                        (uchar *) inherited);
9226
    cond->update_used_tables();
9227
  }
9228
  return cond;
9229
}
9230
9231
9232
/**
9233
  Build multiple equalities for a condition and all on expressions that
9234
  inherit these multiple equalities.
9235
9236
    The function first applies the build_equal_items_for_cond function
9237
    to build all multiple equalities for condition cond utilizing equalities
9238
    referred through the parameter inherited. The extended set of
9239
    equalities is returned in the structure referred by the cond_equal_ref
9240
    parameter. After this the function calls itself recursively for
9241
    all on expressions whose direct references can be found in join_list
9242
    and who inherit directly the multiple equalities just having built.
9243
9244
  @note
9245
    The on expression used in an outer join operation inherits all equalities
9246
    from the on expression of the embedding join, if there is any, or
9247
    otherwise - from the where condition.
9248
    This fact is not obvious, but presumably can be proved.
9249
    Consider the following query:
9250
    @code
9251
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t2.a=t4.a
9252
        WHERE t1.a=t2.a;
9253
    @endcode
9254
    If the on expression in the query inherits =(t1.a,t2.a), then we
9255
    can build the multiple equality =(t1.a,t2.a,t3.a,t4.a) that infers
9256
    the equality t3.a=t4.a. Although the on expression
9257
    t1.a=t3.a AND t2.a=t4.a AND t3.a=t4.a is not equivalent to the one
9258
    in the query the latter can be replaced by the former: the new query
9259
    will return the same result set as the original one.
9260
9261
    Interesting that multiple equality =(t1.a,t2.a,t3.a,t4.a) allows us
9262
    to use t1.a=t3.a AND t3.a=t4.a under the on condition:
9263
    @code
9264
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a
9265
        WHERE t1.a=t2.a
9266
    @endcode
9267
    This query equivalent to:
9268
    @code
9269
      SELECT * FROM (t1 LEFT JOIN (t3,t4) ON t1.a=t3.a AND t3.a=t4.a),t2
9270
        WHERE t1.a=t2.a
9271
    @endcode
9272
    Similarly the original query can be rewritten to the query:
9273
    @code
9274
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
9275
        WHERE t1.a=t2.a
9276
    @endcode
9277
    that is equivalent to:   
9278
    @code
9279
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
9280
        WHERE t1.a=t2.a
9281
    @endcode
9282
    Thus, applying equalities from the where condition we basically
9283
    can get more freedom in performing join operations.
9284
    Althogh we don't use this property now, it probably makes sense to use 
9285
    it in the future.    
9286
  @param thd		      Thread handler
9287
  @param cond                condition to build the multiple equalities for
9288
  @param inherited           path to all inherited multiple equality items
9289
  @param join_list           list of join tables to which the condition
9290
                             refers to
9291
  @param[out] cond_equal_ref pointer to the structure to place built
9292
                             equalities in
9293
9294
  @return
9295
    pointer to the transformed condition containing multiple equalities
9296
*/
9297
   
9298
static COND *build_equal_items(THD *thd, COND *cond,
9299
                               COND_EQUAL *inherited,
9300
                               List<TABLE_LIST> *join_list,
9301
                               COND_EQUAL **cond_equal_ref)
9302
{
9303
  COND_EQUAL *cond_equal= 0;
9304
9305
  if (cond) 
9306
  {
9307
    cond= build_equal_items_for_cond(thd, cond, inherited);
9308
    cond->update_used_tables();
9309
    if (cond->type() == Item::COND_ITEM &&
9310
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
9311
      cond_equal= &((Item_cond_and*) cond)->cond_equal;
9312
    else if (cond->type() == Item::FUNC_ITEM &&
9313
             ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9314
    {
9315
      cond_equal= new COND_EQUAL;
9316
      cond_equal->current_level.push_back((Item_equal *) cond);
9317
    }
9318
  }
9319
  if (cond_equal)
9320
  {
9321
    cond_equal->upper_levels= inherited;
9322
    inherited= cond_equal;
9323
  }
9324
  *cond_equal_ref= cond_equal;
9325
9326
  if (join_list)
9327
  {
9328
    TABLE_LIST *table;
9329
    List_iterator<TABLE_LIST> li(*join_list);
9330
9331
    while ((table= li++))
9332
    {
9333
      if (table->on_expr)
9334
      {
9335
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
9336
          &table->nested_join->join_list : NULL;
9337
        /*
9338
          We can modify table->on_expr because its old value will
9339
          be restored before re-execution of PS/SP.
9340
        */
9341
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
9342
                                          nested_join_list,
9343
                                          &table->cond_equal);
9344
      }
9345
    }
9346
  }
9347
9348
  return cond;
9349
}    
9350
9351
9352
/**
9353
  Compare field items by table order in the execution plan.
9354
9355
    field1 considered as better than field2 if the table containing
9356
    field1 is accessed earlier than the table containing field2.   
9357
    The function finds out what of two fields is better according
9358
    this criteria.
9359
9360
  @param field1          first field item to compare
9361
  @param field2          second field item to compare
9362
  @param table_join_idx  index to tables determining table order
9363
9364
  @retval
9365
    1  if field1 is better than field2
9366
  @retval
9367
    -1  if field2 is better than field1
9368
  @retval
9369
    0  otherwise
9370
*/
9371
9372
static int compare_fields_by_table_order(Item_field *field1,
9373
                                  Item_field *field2,
9374
                                  void *table_join_idx)
9375
{
9376
  int cmp= 0;
9377
  bool outer_ref= 0;
9378
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9379
  {  
9380
    outer_ref= 1;
9381
    cmp= -1;
9382
  }
9383
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9384
  {
9385
    outer_ref= 1;
9386
    cmp++;
9387
  }
9388
  if (outer_ref)
9389
    return cmp;
9390
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
9391
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
9392
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
9393
}
9394
9395
9396
/**
9397
  Generate minimal set of simple equalities equivalent to a multiple equality.
9398
9399
    The function retrieves the fields of the multiple equality item
9400
    item_equal and  for each field f:
9401
    - if item_equal contains const it generates the equality f=const_item;
9402
    - otherwise, if f is not the first field, generates the equality
9403
      f=item_equal->get_first().
9404
    All generated equality are added to the cond conjunction.
9405
9406
  @param cond            condition to add the generated equality to
9407
  @param upper_levels    structure to access multiple equality of upper levels
9408
  @param item_equal      multiple equality to generate simple equality from
9409
9410
  @note
9411
    Before generating an equality function checks that it has not
9412
    been generated for multiple equalities of the upper levels.
9413
    E.g. for the following where condition
9414
    WHERE a=5 AND ((a=b AND b=c) OR  c>4)
9415
    the upper level AND condition will contain =(5,a),
9416
    while the lower level AND condition will contain =(5,a,b,c).
9417
    When splitting =(5,a,b,c) into a separate equality predicates
9418
    we should omit 5=a, as we have it already in the upper level.
9419
    The following where condition gives us a more complicated case:
9420
    WHERE t1.a=t2.b AND t3.c=t4.d AND (t2.b=t3.c OR t4.e>5 ...) AND ...
9421
    Given the tables are accessed in the order t1->t2->t3->t4 for
9422
    the selected query execution plan the lower level multiple
9423
    equality =(t1.a,t2.b,t3.c,t4.d) formally  should be converted to
9424
    t1.a=t2.b AND t1.a=t3.c AND t1.a=t4.d. But t1.a=t2.a will be
9425
    generated for the upper level. Also t3.c=t4.d will be generated there.
9426
    So only t1.a=t3.c should be left in the lower level.
9427
    If cond is equal to 0, then not more then one equality is generated
9428
    and a pointer to it is returned as the result of the function.
9429
9430
  @return
9431
    - The condition with generated simple equalities or
9432
    a pointer to the simple generated equality, if success.
9433
    - 0, otherwise.
9434
*/
9435
9436
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9437
                                  Item_equal *item_equal)
9438
{
9439
  List<Item> eq_list;
9440
  Item_func_eq *eq_item= 0;
9441
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9442
    return new Item_int((longlong) 0,1); 
9443
  Item *item_const= item_equal->get_const();
9444
  Item_equal_iterator it(*item_equal);
9445
  Item *head;
9446
  if (item_const)
9447
    head= item_const;
9448
  else
9449
  {
9450
    head= item_equal->get_first();
9451
    it++;
9452
  }
9453
  Item_field *item_field;
9454
  while ((item_field= it++))
9455
  {
9456
    Item_equal *upper= item_field->find_item_equal(upper_levels);
9457
    Item_field *item= item_field;
9458
    if (upper)
9459
    { 
9460
      if (item_const && upper->get_const())
9461
        item= 0;
9462
      else
9463
      {
9464
        Item_equal_iterator li(*item_equal);
9465
        while ((item= li++) != item_field)
9466
        {
9467
          if (item->find_item_equal(upper_levels) == upper)
9468
            break;
9469
        }
9470
      }
9471
    }
9472
    if (item == item_field)
9473
    {
9474
      if (eq_item)
9475
        eq_list.push_back(eq_item);
9476
      eq_item= new Item_func_eq(item_field, head);
9477
      if (!eq_item)
9478
        return 0;
9479
      eq_item->set_cmp_func();
9480
      eq_item->quick_fix_field();
9481
   }
9482
  }
9483
9484
  if (!cond && !eq_list.head())
9485
  {
9486
    if (!eq_item)
9487
      return new Item_int((longlong) 1,1);
9488
    return eq_item;
9489
  }
9490
9491
  if (eq_item)
9492
    eq_list.push_back(eq_item);
9493
  if (!cond)
9494
    cond= new Item_cond_and(eq_list);
9495
  else
9496
  {
9497
    DBUG_ASSERT(cond->type() == Item::COND_ITEM);
9498
    ((Item_cond *) cond)->add_at_head(&eq_list);
9499
  }
9500
9501
  cond->quick_fix_field();
9502
  cond->update_used_tables();
9503
   
9504
  return cond;
9505
}
9506
9507
9508
/**
9509
  Substitute every field reference in a condition by the best equal field
9510
  and eliminate all multiple equality predicates.
9511
9512
    The function retrieves the cond condition and for each encountered
9513
    multiple equality predicate it sorts the field references in it
9514
    according to the order of tables specified by the table_join_idx
9515
    parameter. Then it eliminates the multiple equality predicate it
9516
    replacing it by the conjunction of simple equality predicates 
9517
    equating every field from the multiple equality to the first
9518
    field in it, or to the constant, if there is any.
9519
    After this the function retrieves all other conjuncted
9520
    predicates substitute every field reference by the field reference
9521
    to the first equal field or equal constant if there are any.
9522
  @param cond            condition to process
9523
  @param cond_equal      multiple equalities to take into consideration
9524
  @param table_join_idx  index to tables determining field preference
9525
9526
  @note
9527
    At the first glance full sort of fields in multiple equality
9528
    seems to be an overkill. Yet it's not the case due to possible
9529
    new fields in multiple equality item of lower levels. We want
9530
    the order in them to comply with the order of upper levels.
9531
9532
  @return
9533
    The transformed condition
9534
*/
9535
9536
static COND* substitute_for_best_equal_field(COND *cond,
9537
                                             COND_EQUAL *cond_equal,
9538
                                             void *table_join_idx)
9539
{
9540
  Item_equal *item_equal;
9541
9542
  if (cond->type() == Item::COND_ITEM)
9543
  {
9544
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9545
9546
    bool and_level= ((Item_cond*) cond)->functype() ==
9547
                      Item_func::COND_AND_FUNC;
9548
    if (and_level)
9549
    {
9550
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
9551
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
9552
9553
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
9554
      while ((item_equal= it++))
9555
      {
9556
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9557
      }
9558
    }
9559
    
9560
    List_iterator<Item> li(*cond_list);
9561
    Item *item;
9562
    while ((item= li++))
9563
    {
9564
      Item *new_item =substitute_for_best_equal_field(item, cond_equal,
9565
                                                      table_join_idx);
9566
      /*
9567
        This works OK with PS/SP re-execution as changes are made to
9568
        the arguments of AND/OR items only
9569
      */
9570
      if (new_item != item)
9571
        li.replace(new_item);
9572
    }
9573
9574
    if (and_level)
9575
    {
9576
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
9577
      while ((item_equal= it++))
9578
      {
9579
        cond= eliminate_item_equal(cond, cond_equal->upper_levels, item_equal);
9580
        // This occurs when eliminate_item_equal() founds that cond is
9581
        // always false and substitutes it with Item_int 0.
9582
        // Due to this, value of item_equal will be 0, so just return it.
9583
        if (cond->type() != Item::COND_ITEM)
9584
          break;
9585
      }
9586
    }
9587
    if (cond->type() == Item::COND_ITEM &&
9588
        !((Item_cond*)cond)->argument_list()->elements)
9589
      cond= new Item_int((int32)cond->val_bool());
9590
9591
  }
9592
  else if (cond->type() == Item::FUNC_ITEM && 
9593
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9594
  {
9595
    item_equal= (Item_equal *) cond;
9596
    item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9597
    if (cond_equal && cond_equal->current_level.head() == item_equal)
9598
      cond_equal= 0;
9599
    return eliminate_item_equal(0, cond_equal, item_equal);
9600
  }
9601
  else
9602
    cond->transform(&Item::replace_equal_field, 0);
9603
  return cond;
9604
}
9605
9606
9607
/**
9608
  Check appearance of new constant items in multiple equalities
9609
  of a condition after reading a constant table.
9610
9611
    The function retrieves the cond condition and for each encountered
9612
    multiple equality checks whether new constants have appeared after
9613
    reading the constant (single row) table tab. If so it adjusts
9614
    the multiple equality appropriately.
9615
9616
  @param cond       condition whose multiple equalities are to be checked
9617
  @param table      constant table that has been read
9618
*/
9619
9620
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
9621
{
9622
  if (!(cond->used_tables() & tab->table->map))
9623
    return;
9624
9625
  if (cond->type() == Item::COND_ITEM)
9626
  {
9627
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
9628
    List_iterator_fast<Item> li(*cond_list);
9629
    Item *item;
9630
    while ((item= li++))
9631
      update_const_equal_items(item, tab);
9632
  }
9633
  else if (cond->type() == Item::FUNC_ITEM && 
9634
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9635
  {
9636
    Item_equal *item_equal= (Item_equal *) cond;
9637
    bool contained_const= item_equal->get_const() != NULL;
9638
    item_equal->update_const();
9639
    if (!contained_const && item_equal->get_const())
9640
    {
9641
      /* Update keys for range analysis */
9642
      Item_equal_iterator it(*item_equal);
9643
      Item_field *item_field;
9644
      while ((item_field= it++))
9645
      {
9646
        Field *field= item_field->field;
9647
        JOIN_TAB *stat= field->table->reginfo.join_tab;
9648
        key_map possible_keys= field->key_start;
9649
        possible_keys.intersect(field->table->keys_in_use_for_query);
9650
        stat[0].const_keys.merge(possible_keys);
9651
9652
        /*
9653
          For each field in the multiple equality (for which we know that it 
9654
          is a constant) we have to find its corresponding key part, and set 
9655
          that key part in const_key_parts.
9656
        */  
9657
        if (!possible_keys.is_clear_all())
9658
        {
9659
          TABLE *tab= field->table;
9660
          KEYUSE *use;
9661
          for (use= stat->keyuse; use && use->table == tab; use++)
9662
            if (possible_keys.is_set(use->key) && 
9663
                tab->key_info[use->key].key_part[use->keypart].field ==
9664
                field)
9665
              tab->const_key_parts[use->key]|= use->keypart_map;
9666
        }
9667
      }
9668
    }
9669
  }
9670
}
9671
9672
9673
/*
9674
  change field = field to field = const for each found field = const in the
9675
  and_level
9676
*/
9677
9678
static void
9679
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
9680
                         Item *and_father, Item *cond,
9681
                         Item *field, Item *value)
9682
{
9683
  if (cond->type() == Item::COND_ITEM)
9684
  {
9685
    bool and_level= ((Item_cond*) cond)->functype() ==
9686
      Item_func::COND_AND_FUNC;
9687
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
9688
    Item *item;
9689
    while ((item=li++))
9690
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
9691
			       field, value);
9692
    return;
9693
  }
9694
  if (cond->eq_cmp_result() == Item::COND_OK)
9695
    return;					// Not a boolean function
9696
9697
  Item_bool_func2 *func=  (Item_bool_func2*) cond;
9698
  Item **args= func->arguments();
9699
  Item *left_item=  args[0];
9700
  Item *right_item= args[1];
9701
  Item_func::Functype functype=  func->functype();
9702
9703
  if (right_item->eq(field,0) && left_item != value &&
9704
      right_item->cmp_context == field->cmp_context &&
9705
      (left_item->result_type() != STRING_RESULT ||
9706
       value->result_type() != STRING_RESULT ||
9707
       left_item->collation.collation == value->collation.collation))
9708
  {
9709
    Item *tmp=value->clone_item();
9710
    tmp->collation.set(right_item->collation);
9711
    
9712
    if (tmp)
9713
    {
9714
      thd->change_item_tree(args + 1, tmp);
9715
      func->update_used_tables();
9716
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9717
	  && and_father != cond && !left_item->const_item())
9718
      {
9719
	cond->marker=1;
9720
	COND_CMP *tmp2;
9721
	if ((tmp2=new COND_CMP(and_father,func)))
9722
	  save_list->push_back(tmp2);
9723
      }
9724
      func->set_cmp_func();
9725
    }
9726
  }
9727
  else if (left_item->eq(field,0) && right_item != value &&
9728
           left_item->cmp_context == field->cmp_context &&
9729
           (right_item->result_type() != STRING_RESULT ||
9730
            value->result_type() != STRING_RESULT ||
9731
            right_item->collation.collation == value->collation.collation))
9732
  {
9733
    Item *tmp= value->clone_item();
9734
    tmp->collation.set(left_item->collation);
9735
    
9736
    if (tmp)
9737
    {
9738
      thd->change_item_tree(args, tmp);
9739
      value= tmp;
9740
      func->update_used_tables();
9741
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9742
	  && and_father != cond && !right_item->const_item())
9743
      {
9744
        args[0]= args[1];                       // For easy check
9745
        thd->change_item_tree(args + 1, value);
9746
	cond->marker=1;
9747
	COND_CMP *tmp2;
9748
	if ((tmp2=new COND_CMP(and_father,func)))
9749
	  save_list->push_back(tmp2);
9750
      }
9751
      func->set_cmp_func();
9752
    }
9753
  }
9754
}
9755
9756
/**
9757
  Remove additional condition inserted by IN/ALL/ANY transformation.
9758
9759
  @param conds   condition for processing
9760
9761
  @return
9762
    new conditions
9763
*/
9764
9765
static Item *remove_additional_cond(Item* conds)
9766
{
9767
  if (conds->name == in_additional_cond)
9768
    return 0;
9769
  if (conds->type() == Item::COND_ITEM)
9770
  {
9771
    Item_cond *cnd= (Item_cond*) conds;
9772
    List_iterator<Item> li(*(cnd->argument_list()));
9773
    Item *item;
9774
    while ((item= li++))
9775
    {
9776
      if (item->name == in_additional_cond)
9777
      {
9778
	li.remove();
9779
	if (cnd->argument_list()->elements == 1)
9780
	  return cnd->argument_list()->head();
9781
	return conds;
9782
      }
9783
    }
9784
  }
9785
  return conds;
9786
}
9787
9788
static void
9789
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
9790
                         COND *and_father, COND *cond)
9791
{
9792
  if (cond->type() == Item::COND_ITEM)
9793
  {
9794
    bool and_level= ((Item_cond*) cond)->functype() ==
9795
      Item_func::COND_AND_FUNC;
9796
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
9797
    Item *item;
9798
    I_List<COND_CMP> save;
9799
    while ((item=li++))
9800
    {
9801
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
9802
    }
9803
    if (and_level)
9804
    {						// Handle other found items
9805
      I_List_iterator<COND_CMP> cond_itr(save);
9806
      COND_CMP *cond_cmp;
9807
      while ((cond_cmp=cond_itr++))
9808
      {
9809
        Item **args= cond_cmp->cmp_func->arguments();
9810
        if (!args[0]->const_item())
9811
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
9812
                                   cond_cmp->and_level, args[0], args[1]);
9813
      }
9814
    }
9815
  }
9816
  else if (and_father != cond && !cond->marker)		// In a AND group
9817
  {
9818
    if (cond->type() == Item::FUNC_ITEM &&
9819
	(((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9820
	 ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
9821
    {
9822
      Item_func_eq *func=(Item_func_eq*) cond;
9823
      Item **args= func->arguments();
9824
      bool left_const= args[0]->const_item();
9825
      bool right_const= args[1]->const_item();
9826
      if (!(left_const && right_const) &&
9827
          args[0]->result_type() == args[1]->result_type())
9828
      {
9829
	if (right_const)
9830
	{
9831
          resolve_const_item(thd, &args[1], args[0]);
9832
	  func->update_used_tables();
9833
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9834
                                   args[0], args[1]);
9835
	}
9836
	else if (left_const)
9837
	{
9838
          resolve_const_item(thd, &args[0], args[1]);
9839
	  func->update_used_tables();
9840
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9841
                                   args[1], args[0]);
9842
	}
9843
      }
9844
    }
9845
  }
9846
}
9847
9848
9849
/**
9850
  Simplify joins replacing outer joins by inner joins whenever it's
9851
  possible.
9852
9853
    The function, during a retrieval of join_list,  eliminates those
9854
    outer joins that can be converted into inner join, possibly nested.
9855
    It also moves the on expressions for the converted outer joins
9856
    and from inner joins to conds.
9857
    The function also calculates some attributes for nested joins:
9858
    - used_tables    
9859
    - not_null_tables
9860
    - dep_tables.
9861
    - on_expr_dep_tables
9862
    The first two attributes are used to test whether an outer join can
9863
    be substituted for an inner join. The third attribute represents the
9864
    relation 'to be dependent on' for tables. If table t2 is dependent
9865
    on table t1, then in any evaluated execution plan table access to
9866
    table t2 must precede access to table t2. This relation is used also
9867
    to check whether the query contains  invalid cross-references.
9868
    The forth attribute is an auxiliary one and is used to calculate
9869
    dep_tables.
9870
    As the attribute dep_tables qualifies possibles orders of tables in the
9871
    execution plan, the dependencies required by the straight join
9872
    modifiers are reflected in this attribute as well.
9873
    The function also removes all braces that can be removed from the join
9874
    expression without changing its meaning.
9875
9876
  @note
9877
    An outer join can be replaced by an inner join if the where condition
9878
    or the on expression for an embedding nested join contains a conjunctive
9879
    predicate rejecting null values for some attribute of the inner tables.
9880
9881
    E.g. in the query:    
9882
    @code
9883
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9884
    @endcode
9885
    the predicate t2.b < 5 rejects nulls.
9886
    The query is converted first to:
9887
    @code
9888
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9889
    @endcode
9890
    then to the equivalent form:
9891
    @code
9892
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9893
    @endcode
9894
9895
9896
    Similarly the following query:
9897
    @code
9898
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9899
        WHERE t2.c < 5  
9900
    @endcode
9901
    is converted to:
9902
    @code
9903
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
9904
9905
    @endcode
9906
9907
    One conversion might trigger another:
9908
    @code
9909
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9910
                       LEFT JOIN t3 ON t3.b=t2.b
9911
        WHERE t3 IS NOT NULL =>
9912
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9913
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
9914
      SELECT * FROM t1, t2, t3
9915
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9916
  @endcode
9917
9918
    The function removes all unnecessary braces from the expression
9919
    produced by the conversions.
9920
    E.g.
9921
    @code
9922
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9923
    @endcode
9924
    finally is converted to: 
9925
    @code
9926
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9927
9928
    @endcode
9929
9930
9931
    It also will remove braces from the following queries:
9932
    @code
9933
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9934
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9935
    @endcode
9936
9937
    The benefit of this simplification procedure is that it might return 
9938
    a query for which the optimizer can evaluate execution plan with more
9939
    join orders. With a left join operation the optimizer does not
9940
    consider any plan where one of the inner tables is before some of outer
9941
    tables.
9942
9943
  IMPLEMENTATION
9944
    The function is implemented by a recursive procedure.  On the recursive
9945
    ascent all attributes are calculated, all outer joins that can be
9946
    converted are replaced and then all unnecessary braces are removed.
9947
    As join list contains join tables in the reverse order sequential
9948
    elimination of outer joins does not require extra recursive calls.
9949
9950
  SEMI-JOIN NOTES
9951
    Remove all semi-joins that have are within another semi-join (i.e. have
9952
    an "ancestor" semi-join nest)
9953
9954
  EXAMPLES
9955
    Here is an example of a join query with invalid cross references:
9956
    @code
9957
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
9958
    @endcode
9959
9960
  @param join        reference to the query info
9961
  @param join_list   list representation of the join to be converted
9962
  @param conds       conditions to add on expressions for converted joins
9963
  @param top         true <=> conds is the where condition
9964
9965
  @return
9966
    - The new condition, if success
9967
    - 0, otherwise
9968
*/
9969
9970
static COND *
9971
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9972
               bool in_sj)
9973
{
9974
  TABLE_LIST *table;
9975
  NESTED_JOIN *nested_join;
9976
  TABLE_LIST *prev_table= 0;
9977
  List_iterator<TABLE_LIST> li(*join_list);
9978
  DBUG_ENTER("simplify_joins");
9979
9980
  /* 
9981
    Try to simplify join operations from join_list.
9982
    The most outer join operation is checked for conversion first. 
9983
  */
9984
  while ((table= li++))
9985
  {
9986
    table_map used_tables;
9987
    table_map not_null_tables= (table_map) 0;
9988
9989
    if ((nested_join= table->nested_join))
9990
    {
9991
      /* 
9992
         If the element of join_list is a nested join apply
9993
         the procedure to its nested join list first.
9994
      */
9995
      if (table->on_expr)
9996
      {
9997
        Item *expr= table->on_expr;
9998
        /* 
9999
           If an on expression E is attached to the table, 
10000
           check all null rejected predicates in this expression.
10001
           If such a predicate over an attribute belonging to
10002
           an inner table of an embedded outer join is found,
10003
           the outer join is converted to an inner join and
10004
           the corresponding on expression is added to E. 
10005
	*/ 
10006
        expr= simplify_joins(join, &nested_join->join_list,
10007
                             expr, FALSE, in_sj || table->sj_on_expr);
10008
10009
        if (!table->prep_on_expr || expr != table->on_expr)
10010
        {
10011
          DBUG_ASSERT(expr);
10012
10013
          table->on_expr= expr;
10014
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
10015
        }
10016
      }
10017
      nested_join->used_tables= (table_map) 0;
10018
      nested_join->not_null_tables=(table_map) 0;
10019
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
10020
                            in_sj || table->sj_on_expr);
10021
      used_tables= nested_join->used_tables;
10022
      not_null_tables= nested_join->not_null_tables;  
10023
    }
10024
    else
10025
    {
10026
      if (!table->prep_on_expr)
10027
        table->prep_on_expr= table->on_expr;
10028
      used_tables= table->table->map;
10029
      if (conds)
10030
        not_null_tables= conds->not_null_tables();
10031
    }
10032
      
10033
    if (table->embedding)
10034
    {
10035
      table->embedding->nested_join->used_tables|= used_tables;
10036
      table->embedding->nested_join->not_null_tables|= not_null_tables;
10037
    }
10038
10039
    if (!table->outer_join || (used_tables & not_null_tables))
10040
    {
10041
      /* 
10042
        For some of the inner tables there are conjunctive predicates
10043
        that reject nulls => the outer join can be replaced by an inner join.
10044
      */
10045
      table->outer_join= 0;
10046
      if (table->on_expr)
10047
      {
10048
        /* Add ON expression to the WHERE or upper-level ON condition. */
10049
        if (conds)
10050
        {
10051
          conds= and_conds(conds, table->on_expr);
10052
          conds->top_level_item();
10053
          /* conds is always a new item as both cond and on_expr existed */
10054
          DBUG_ASSERT(!conds->fixed);
10055
          conds->fix_fields(join->thd, &conds);
10056
        }
10057
        else
10058
          conds= table->on_expr; 
10059
        table->prep_on_expr= table->on_expr= 0;
10060
      }
10061
    }
10062
    
10063
    if (!top)
10064
      continue;
10065
10066
    /* 
10067
      Only inner tables of non-convertible outer joins
10068
      remain with on_expr.
10069
    */ 
10070
    if (table->on_expr)
10071
    {
10072
      table->dep_tables|= table->on_expr->used_tables(); 
10073
      if (table->embedding)
10074
      {
10075
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
10076
        /*
10077
           Embedding table depends on tables used
10078
           in embedded on expressions. 
10079
        */
10080
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
10081
      }
10082
      else
10083
        table->dep_tables&= ~table->table->map;
10084
    }
10085
10086
    if (prev_table)
10087
    {
10088
      /* The order of tables is reverse: prev_table follows table */
10089
      if (prev_table->straight)
10090
        prev_table->dep_tables|= used_tables;
10091
      if (prev_table->on_expr)
10092
      {
10093
        prev_table->dep_tables|= table->on_expr_dep_tables;
10094
        table_map prev_used_tables= prev_table->nested_join ?
10095
	                            prev_table->nested_join->used_tables :
10096
	                            prev_table->table->map;
10097
        /* 
10098
          If on expression contains only references to inner tables
10099
          we still make the inner tables dependent on the outer tables.
10100
          It would be enough to set dependency only on one outer table
10101
          for them. Yet this is really a rare case.
10102
	*/  
10103
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
10104
          prev_table->dep_tables|= used_tables;
10105
      }
10106
    }
10107
    prev_table= table;
10108
  }
10109
    
10110
  /* 
10111
    Flatten nested joins that can be flattened.
10112
    no ON expression and not a semi-join => can be flattened.
10113
  */
10114
  li.rewind();
10115
  while ((table= li++))
10116
  {
10117
    nested_join= table->nested_join;
10118
    if (table->sj_on_expr && !in_sj)
10119
    {
10120
       /*
10121
         If this is a semi-join that is not contained within another semi-join, 
10122
         leave it intact (otherwise it is flattened)
10123
       */
10124
      join->select_lex->sj_nests.push_back(table);
10125
    }
10126
    else if (nested_join && !table->on_expr)
10127
    {
10128
      TABLE_LIST *tbl;
10129
      List_iterator<TABLE_LIST> it(nested_join->join_list);
10130
      while ((tbl= it++))
10131
      {
10132
        tbl->embedding= table->embedding;
10133
        tbl->join_list= table->join_list;
10134
      }      
10135
      li.replace(nested_join->join_list);
10136
    }
10137
  }
10138
  DBUG_RETURN(conds); 
10139
}
10140
10141
10142
/**
10143
  Assign each nested join structure a bit in nested_join_map.
10144
10145
    Assign each nested join structure (except "confluent" ones - those that
10146
    embed only one element) a bit in nested_join_map.
10147
10148
  @param join          Join being processed
10149
  @param join_list     List of tables
10150
  @param first_unused  Number of first unused bit in nested_join_map before the
10151
                       call
10152
10153
  @note
10154
    This function is called after simplify_joins(), when there are no
10155
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
10156
    we will not run out of bits in nested_join_map.
10157
10158
  @return
10159
    First unused bit in nested_join_map after the call.
10160
*/
10161
10162
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
10163
                                          uint first_unused)
10164
{
10165
  List_iterator<TABLE_LIST> li(*join_list);
10166
  TABLE_LIST *table;
10167
  DBUG_ENTER("build_bitmap_for_nested_joins");
10168
  while ((table= li++))
10169
  {
10170
    NESTED_JOIN *nested_join;
10171
    if ((nested_join= table->nested_join))
10172
    {
10173
      /*
10174
        It is guaranteed by simplify_joins() function that a nested join
10175
        that has only one child is either
10176
         - a single-table view (the child is the underlying table), or 
10177
         - a single-table semi-join nest
10178
10179
        We don't assign bits to such sj-nests because 
10180
        1. it is redundant (a "sequence" of one table cannot be interleaved 
10181
            with anything)
10182
        2. we could run out bits in nested_join_map otherwise.
10183
      */
10184
      if (nested_join->join_list.elements != 1)
10185
      {
10186
        /* Don't assign bits to sj-nests */
10187
        if (table->on_expr)
10188
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
10189
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
10190
                                                    first_unused);
10191
      }
10192
    }
10193
  }
10194
  DBUG_RETURN(first_unused);
10195
}
10196
10197
10198
/**
10199
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
10200
10201
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
10202
    the passed join_list.
10203
10204
  @param join_list  List of nested joins to process. It may also contain base
10205
                    tables which will be ignored.
10206
*/
10207
10208
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10209
{
10210
  List_iterator<TABLE_LIST> li(*join_list);
10211
  TABLE_LIST *table;
10212
  DBUG_ENTER("reset_nj_counters");
10213
  while ((table= li++))
10214
  {
10215
    NESTED_JOIN *nested_join;
10216
    if ((nested_join= table->nested_join))
10217
    {
10218
      nested_join->counter_= 0;
10219
      reset_nj_counters(&nested_join->join_list);
10220
    }
10221
  }
10222
  DBUG_VOID_RETURN;
10223
}
10224
10225
10226
/**
10227
  Check interleaving with an inner tables of an outer join for
10228
  extension table.
10229
10230
    Check if table next_tab can be added to current partial join order, and 
10231
    if yes, record that it has been added.
10232
10233
    The function assumes that both current partial join order and its
10234
    extension with next_tab are valid wrt table dependencies.
10235
10236
  @verbatim
10237
     IMPLEMENTATION 
10238
       LIMITATIONS ON JOIN ORDER
10239
         The nested [outer] joins executioner algorithm imposes these limitations
10240
         on join order:
10241
         1. "Outer tables first" -  any "outer" table must be before any 
10242
             corresponding "inner" table.
10243
         2. "No interleaving" - tables inside a nested join must form a continuous
10244
            sequence in join order (i.e. the sequence must not be interrupted by 
10245
            tables that are outside of this nested join).
10246
10247
         #1 is checked elsewhere, this function checks #2 provided that #1 has
10248
         been already checked.
10249
10250
       WHY NEED NON-INTERLEAVING
10251
         Consider an example: 
10252
10253
           select * from t0 join t1 left join (t2 join t3) on cond1
10254
10255
         The join order "t1 t2 t0 t3" is invalid:
10256
10257
         table t0 is outside of the nested join, so WHERE condition for t0 is
10258
         attached directly to t0 (without triggers, and it may be used to access
10259
         t0). Applying WHERE(t0) to (t2,t0,t3) record is invalid as we may miss
10260
         combinations of (t1, t2, t3) that satisfy condition cond1, and produce a
10261
         null-complemented (t1, t2.NULLs, t3.NULLs) row, which should not have
10262
         been produced.
10263
10264
         If table t0 is not between t2 and t3, the problem doesn't exist:
10265
          If t0 is located after (t2,t3), WHERE(t0) is applied after nested join
10266
           processing has finished.
10267
          If t0 is located before (t2,t3), predicates like WHERE_cond(t0, t2) are
10268
           wrapped into condition triggers, which takes care of correct nested
10269
           join processing.
10270
10271
       HOW IT IS IMPLEMENTED
10272
         The limitations on join order can be rephrased as follows: for valid
10273
         join order one must be able to:
10274
           1. write down the used tables in the join order on one line.
10275
           2. for each nested join, put one '(' and one ')' on the said line        
10276
           3. write "LEFT JOIN" and "ON (...)" where appropriate
10277
           4. get a query equivalent to the query we're trying to execute.
10278
10279
         Calls to check_interleaving_with_nj() are equivalent to writing the
10280
         above described line from left to right. 
10281
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
10282
         table B and appropriate brackets on condition that table A and
10283
         appropriate brackets is the last what was written. Graphically the
10284
         transition is as follows:
10285
10286
                              +---- current position
10287
                              |
10288
             ... last_tab ))) | ( next_tab )  )..) | ...
10289
                                X          Y   Z   |
10290
                                                   +- need to move to this
10291
                                                      position.
10292
10293
         Notes about the position:
10294
           The caller guarantees that there is no more then one X-bracket by 
10295
           checking "!(remaining_tables & s->dependent)" before calling this 
10296
           function. X-bracket may have a pair in Y-bracket.
10297
10298
         When "writing" we store/update this auxilary info about the current
10299
         position:
10300
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10301
             joins) we've opened but didn't close.
10302
          2. {each NESTED_JOIN structure not simplified away}->counter - number
10303
             of this nested join's children that have already been added to to
10304
             the partial join order.
10305
  @endverbatim
10306
10307
  @param join       Join being processed
10308
  @param last_tab   Last table in current partial join order (this function is
10309
                    not called for empty partial join orders)
10310
  @param next_tab   Table we're going to extend the current partial join with
10311
10312
  @retval
10313
    FALSE  Join order extended, nested joins info about current join
10314
    order (see NOTE section) updated.
10315
  @retval
10316
    TRUE   Requested join order extension not allowed.
10317
*/
10318
10319
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
10320
{
10321
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
10322
  JOIN *join= last_tab->join;
10323
10324
  if (join->cur_embedding_map & ~next_tab->embedding_map)
10325
  {
10326
    /* 
10327
      next_tab is outside of the "pair of brackets" we're currently in.
10328
      Cannot add it.
10329
    */
10330
    return TRUE;
10331
  }
10332
   
10333
  /*
10334
    Do update counters for "pairs of brackets" that we've left (marked as
10335
    X,Y,Z in the above picture)
10336
  */
10337
  for (;next_emb; next_emb= next_emb->embedding)
10338
  {
10339
    next_emb->nested_join->counter_++;
10340
    if (next_emb->nested_join->counter_ == 1)
10341
    {
10342
      /* 
10343
        next_emb is the first table inside a nested join we've "entered". In
10344
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
10345
        X bracket might have Y pair bracket.
10346
      */
10347
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
10348
    }
10349
    
10350
    if (next_emb->nested_join->join_list.elements !=
10351
        next_emb->nested_join->counter_)
10352
      break;
10353
10354
    /*
10355
      We're currently at Y or Z-bracket as depicted in the above picture.
10356
      Mark that we've left it and continue walking up the brackets hierarchy.
10357
    */
10358
    join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
10359
  }
10360
  return FALSE;
10361
}
10362
10363
10364
/**
10365
  Nested joins perspective: Remove the last table from the join order.
10366
10367
    Remove the last table from the partial join order and update the nested
10368
    joins counters and join->cur_embedding_map. It is ok to call this 
10369
    function for the first table in join order (for which 
10370
    check_interleaving_with_nj has not been called)
10371
10372
  @param last  join table to remove, it is assumed to be the last in current
10373
               partial join order.
10374
*/
10375
10376
static void restore_prev_nj_state(JOIN_TAB *last)
10377
{
10378
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10379
  JOIN *join= last->join;
10380
  while (last_emb)
10381
  {
10382
    if (last_emb->on_expr)
10383
    {
10384
      if (!(--last_emb->nested_join->counter_))
10385
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10386
      else if (last_emb->nested_join->join_list.elements-1 ==
10387
               last_emb->nested_join->counter_) 
10388
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
10389
      else
10390
        break;
10391
    }
10392
    last_emb= last_emb->embedding;
10393
  }
10394
}
10395
10396
10397
10398
static 
10399
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10400
{
10401
  TABLE_LIST *emb_sj_nest;
10402
  if ((emb_sj_nest= tab->emb_sj_nest))
10403
  {
10404
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10405
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
10406
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
10407
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10408
  }
10409
}
10410
10411
10412
/*
10413
  we assume remaining_tables doesnt contain @tab.
10414
*/
10415
10416
static void restore_prev_sj_state(const table_map remaining_tables, 
10417
                                  const JOIN_TAB *tab)
10418
{
10419
  TABLE_LIST *emb_sj_nest;
10420
  if ((emb_sj_nest= tab->emb_sj_nest))
10421
  {
10422
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10423
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
10424
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
10425
    {
10426
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10427
    }
10428
  }
10429
}
10430
10431
10432
static COND *
10433
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10434
              Item::cond_result *cond_value)
10435
{
10436
  THD *thd= join->thd;
10437
  DBUG_ENTER("optimize_cond");
10438
10439
  if (!conds)
10440
    *cond_value= Item::COND_TRUE;
10441
  else
10442
  {
10443
    /* 
10444
      Build all multiple equality predicates and eliminate equality
10445
      predicates that can be inferred from these multiple equalities.
10446
      For each reference of a field included into a multiple equality
10447
      that occurs in a function set a pointer to the multiple equality
10448
      predicate. Substitute a constant instead of this field if the
10449
      multiple equality contains a constant.
10450
    */ 
10451
    DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
10452
    conds= build_equal_items(join->thd, conds, NULL, join_list,
10453
                             &join->cond_equal);
10454
    DBUG_EXECUTE("where",print_where(conds,"after equal_items", QT_ORDINARY););
10455
10456
    /* change field = field to field = const for each found field = const */
10457
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
10458
    /*
10459
      Remove all instances of item == item
10460
      Remove all and-levels where CONST item != CONST item
10461
    */
10462
    DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY););
10463
    conds= remove_eq_conds(thd, conds, cond_value) ;
10464
    DBUG_EXECUTE("info",print_where(conds,"after remove", QT_ORDINARY););
10465
  }
10466
  DBUG_RETURN(conds);
10467
}
10468
10469
10470
/**
10471
  Remove const and eq items.
10472
10473
  @return
10474
    Return new item, or NULL if no condition @n
10475
    cond_value is set to according:
10476
    - COND_OK     : query is possible (field = constant)
10477
    - COND_TRUE   : always true	( 1 = 1 )
10478
    - COND_FALSE  : always false	( 1 = 2 )
10479
*/
10480
10481
COND *
10482
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
10483
{
10484
  if (cond->type() == Item::COND_ITEM)
10485
  {
10486
    bool and_level= ((Item_cond*) cond)->functype()
10487
      == Item_func::COND_AND_FUNC;
10488
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
10489
    Item::cond_result tmp_cond_value;
10490
    bool should_fix_fields=0;
10491
10492
    *cond_value=Item::COND_UNDEF;
10493
    Item *item;
10494
    while ((item=li++))
10495
    {
10496
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
10497
      if (!new_item)
10498
	li.remove();
10499
      else if (item != new_item)
10500
      {
10501
	VOID(li.replace(new_item));
10502
	should_fix_fields=1;
10503
      }
10504
      if (*cond_value == Item::COND_UNDEF)
10505
	*cond_value=tmp_cond_value;
10506
      switch (tmp_cond_value) {
10507
      case Item::COND_OK:			// Not TRUE or FALSE
10508
	if (and_level || *cond_value == Item::COND_FALSE)
10509
	  *cond_value=tmp_cond_value;
10510
	break;
10511
      case Item::COND_FALSE:
10512
	if (and_level)
10513
	{
10514
	  *cond_value=tmp_cond_value;
10515
	  return (COND*) 0;			// Always false
10516
	}
10517
	break;
10518
      case Item::COND_TRUE:
10519
	if (!and_level)
10520
	{
10521
	  *cond_value= tmp_cond_value;
10522
	  return (COND*) 0;			// Always true
10523
	}
10524
	break;
10525
      case Item::COND_UNDEF:			// Impossible
10526
	break; /* purecov: deadcode */
10527
      }
10528
    }
10529
    if (should_fix_fields)
10530
      cond->update_used_tables();
10531
10532
    if (!((Item_cond*) cond)->argument_list()->elements ||
10533
	*cond_value != Item::COND_OK)
10534
      return (COND*) 0;
10535
    if (((Item_cond*) cond)->argument_list()->elements == 1)
10536
    {						// Remove list
10537
      item= ((Item_cond*) cond)->argument_list()->head();
10538
      ((Item_cond*) cond)->argument_list()->empty();
10539
      return item;
10540
    }
10541
  }
10542
  else if (cond->type() == Item::FUNC_ITEM &&
10543
	   ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
10544
  {
10545
    /*
10546
      Handles this special case for some ODBC applications:
10547
      The are requesting the row that was just updated with a auto_increment
10548
      value with this construct:
10549
10550
      SELECT * from table_name where auto_increment_column IS NULL
10551
      This will be changed to:
10552
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
10553
    */
10554
10555
    Item_func_isnull *func=(Item_func_isnull*) cond;
10556
    Item **args= func->arguments();
10557
    if (args[0]->type() == Item::FIELD_ITEM)
10558
    {
10559
      Field *field=((Item_field*) args[0])->field;
10560
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
10561
	  (thd->options & OPTION_AUTO_IS_NULL) &&
10562
	  (thd->first_successful_insert_id_in_prev_stmt > 0 &&
10563
           thd->substitute_null_with_insert_id))
10564
      {
10565
	COND *new_cond;
10566
	if ((new_cond= new Item_func_eq(args[0],
10567
					new Item_int("last_insert_id()",
10568
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
10569
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
10570
	{
10571
	  cond=new_cond;
10572
          /*
10573
            Item_func_eq can't be fixed after creation so we do not check
10574
            cond->fixed, also it do not need tables so we use 0 as second
10575
            argument.
10576
          */
10577
	  cond->fix_fields(thd, &cond);
10578
	}
10579
        /*
10580
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
10581
          clear for next row
10582
        */
10583
        thd->substitute_null_with_insert_id= FALSE;
10584
      }
10585
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10586
      else if (((field->type() == MYSQL_TYPE_DATE) ||
10587
		(field->type() == MYSQL_TYPE_DATETIME)) &&
10588
		(field->flags & NOT_NULL_FLAG) &&
10589
	       !field->table->maybe_null)
10590
      {
10591
	COND *new_cond;
10592
	if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
10593
	{
10594
	  cond=new_cond;
10595
          /*
10596
            Item_func_eq can't be fixed after creation so we do not check
10597
            cond->fixed, also it do not need tables so we use 0 as second
10598
            argument.
10599
          */
10600
	  cond->fix_fields(thd, &cond);
10601
	}
10602
      }
10603
    }
10604
    if (cond->const_item())
10605
    {
10606
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10607
      return (COND*) 0;
10608
    }
10609
  }
10610
  else if (cond->const_item() && !cond->is_expensive())
10611
  /*
10612
    TODO:
10613
    Excluding all expensive functions is too restritive we should exclude only
10614
    materialized IN because it is created later than this phase, and cannot be
10615
    evaluated at this point.
10616
    The condition should be something as (need to fix member access):
10617
      !(cond->type() == Item::FUNC_ITEM &&
10618
        ((Item_func*)cond)->func_name() == "<in_optimizer>" &&
10619
        ((Item_in_optimizer*)cond)->is_expensive()))
10620
  */
10621
  {
10622
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10623
    return (COND*) 0;
10624
  }
10625
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
10626
  {						// boolan compare function
10627
    Item *left_item=	((Item_func*) cond)->arguments()[0];
10628
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10629
    if (left_item->eq(right_item,1))
10630
    {
10631
      if (!left_item->maybe_null ||
10632
	  ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
10633
	return (COND*) 0;			// Compare of identical items
10634
    }
10635
  }
10636
  *cond_value=Item::COND_OK;
10637
  return cond;					// Point at next and level
10638
}
10639
10640
/* 
10641
  Check if equality can be used in removing components of GROUP BY/DISTINCT
10642
  
10643
  SYNOPSIS
10644
    test_if_equality_guarantees_uniqueness()
10645
      l          the left comparison argument (a field if any)
10646
      r          the right comparison argument (a const of any)
10647
  
10648
  DESCRIPTION    
10649
    Checks if an equality predicate can be used to take away 
10650
    DISTINCT/GROUP BY because it is known to be true for exactly one 
10651
    distinct value (e.g. <expr> == <const>).
10652
    Arguments must be of the same type because e.g. 
10653
    <string_field> = <int_const> may match more than 1 distinct value from 
10654
    the column. 
10655
    We must take into consideration and the optimization done for various 
10656
    string constants when compared to dates etc (see Item_int_with_ref) as
10657
    well as the collation of the arguments.
10658
  
10659
  RETURN VALUE  
10660
    TRUE    can be used
10661
    FALSE   cannot be used
10662
*/
10663
static bool
10664
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10665
{
10666
  return r->const_item() &&
10667
    /* elements must be compared as dates */
10668
     (Arg_comparator::can_compare_as_dates(l, r, 0) ||
10669
      /* or of the same result type */
10670
      (r->result_type() == l->result_type() &&
10671
       /* and must have the same collation if compared as strings */
10672
       (l->result_type() != STRING_RESULT ||
10673
        l->collation.collation == r->collation.collation)));
10674
}
10675
10676
/**
10677
  Return TRUE if the item is a const value in all the WHERE clause.
10678
*/
10679
10680
static bool
10681
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
10682
{
10683
  if (cond->type() == Item::COND_ITEM)
10684
  {
10685
    bool and_level= (((Item_cond*) cond)->functype()
10686
		     == Item_func::COND_AND_FUNC);
10687
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
10688
    Item *item;
10689
    while ((item=li++))
10690
    {
10691
      bool res=const_expression_in_where(item, comp_item, const_item);
10692
      if (res)					// Is a const value
10693
      {
10694
	if (and_level)
10695
	  return 1;
10696
      }
10697
      else if (!and_level)
10698
	return 0;
10699
    }
10700
    return and_level ? 0 : 1;
10701
  }
10702
  else if (cond->eq_cmp_result() != Item::COND_OK)
10703
  {						// boolan compare function
10704
    Item_func* func= (Item_func*) cond;
10705
    if (func->functype() != Item_func::EQUAL_FUNC &&
10706
	func->functype() != Item_func::EQ_FUNC)
10707
      return 0;
10708
    Item *left_item=	((Item_func*) cond)->arguments()[0];
10709
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10710
    if (left_item->eq(comp_item,1))
10711
    {
10712
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10713
      {
10714
	if (*const_item)
10715
	  return right_item->eq(*const_item, 1);
10716
	*const_item=right_item;
10717
	return 1;
10718
      }
10719
    }
10720
    else if (right_item->eq(comp_item,1))
10721
    {
10722
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10723
      {
10724
	if (*const_item)
10725
	  return left_item->eq(*const_item, 1);
10726
	*const_item=left_item;
10727
	return 1;
10728
      }
10729
    }
10730
  }
10731
  return 0;
10732
}
10733
10734
/****************************************************************************
10735
  Create internal temporary table
10736
****************************************************************************/
10737
10738
/**
10739
  Create field for temporary table from given field.
10740
10741
  @param thd	       Thread handler
10742
  @param org_field    field from which new field will be created
10743
  @param name         New field name
10744
  @param table	       Temporary table
10745
  @param item	       !=NULL if item->result_field should point to new field.
10746
                      This is relevant for how fill_record() is going to work:
10747
                      If item != NULL then fill_record() will update
10748
                      the record in the original table.
10749
                      If item == NULL then fill_record() will update
10750
                      the temporary table
10751
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10752
                               field instead of blob.
10753
10754
  @retval
10755
    NULL		on error
10756
  @retval
10757
    new_created field
10758
*/
10759
10760
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10761
                                   const char *name, TABLE *table,
10762
                                   Item_field *item, uint convert_blob_length)
10763
{
10764
  Field *new_field;
10765
10766
  /* 
10767
    Make sure that the blob fits into a Field_varstring which has 
10768
    2-byte lenght. 
10769
  */
10770
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10771
      (org_field->flags & BLOB_FLAG))
10772
    new_field= new Field_varstring(convert_blob_length,
10773
                                   org_field->maybe_null(),
10774
                                   org_field->field_name, table->s,
10775
                                   org_field->charset());
10776
  else
10777
    new_field= org_field->new_field(thd->mem_root, table,
10778
                                    table == org_field->table);
10779
  if (new_field)
10780
  {
10781
    new_field->init(table);
10782
    new_field->orig_table= org_field->orig_table;
10783
    if (item)
10784
      item->result_field= new_field;
10785
    else
10786
      new_field->field_name= name;
10787
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10788
    if (org_field->maybe_null() || (item && item->maybe_null))
10789
      new_field->flags&= ~NOT_NULL_FLAG;	// Because of outer join
10790
    if (org_field->type() == MYSQL_TYPE_VAR_STRING ||
10791
        org_field->type() == MYSQL_TYPE_VARCHAR)
10792
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10793
    else if (org_field->type() == FIELD_TYPE_DOUBLE)
10794
      ((Field_double *) new_field)->not_fixed= TRUE;
10795
  }
10796
  return new_field;
10797
}
10798
10799
/**
10800
  Create field for temporary table using type of given item.
10801
10802
  @param thd                   Thread handler
10803
  @param item                  Item to create a field for
10804
  @param table                 Temporary table
10805
  @param copy_func             If set and item is a function, store copy of
10806
                               item in this array
10807
  @param modify_item           1 if item->result_field should point to new
10808
                               item. This is relevent for how fill_record()
10809
                               is going to work:
10810
                               If modify_item is 1 then fill_record() will
10811
                               update the record in the original table.
10812
                               If modify_item is 0 then fill_record() will
10813
                               update the temporary table
10814
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10815
                               field instead of blob.
10816
10817
  @retval
10818
    0  on error
10819
  @retval
10820
    new_created field
10821
*/
10822
10823
static Field *create_tmp_field_from_item(THD *thd, Item *item, TABLE *table,
10824
                                         Item ***copy_func, bool modify_item,
10825
                                         uint convert_blob_length)
10826
{
10827
  bool maybe_null= item->maybe_null;
10828
  Field *new_field;
10829
10830
  switch (item->result_type()) {
10831
  case REAL_RESULT:
10832
    new_field= new Field_double(item->max_length, maybe_null,
10833
                                item->name, item->decimals, TRUE);
10834
    break;
10835
  case INT_RESULT:
10836
    /* 
10837
      Select an integer type with the minimal fit precision.
10838
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10839
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
10840
      Field_long : make them Field_longlong.  
10841
    */
10842
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10843
      new_field=new Field_longlong(item->max_length, maybe_null,
10844
                                   item->name, item->unsigned_flag);
10845
    else
10846
      new_field=new Field_long(item->max_length, maybe_null,
10847
                               item->name, item->unsigned_flag);
10848
    break;
10849
  case STRING_RESULT:
10850
    DBUG_ASSERT(item->collation.collation);
10851
  
10852
    enum enum_field_types type;
10853
    /*
10854
      DATE/TIME fields have STRING_RESULT result type. 
10855
      To preserve type they needed to be handled separately.
10856
    */
10857
    if ((type= item->field_type()) == MYSQL_TYPE_DATETIME ||
10858
        type == MYSQL_TYPE_TIME || type == MYSQL_TYPE_DATE ||
10859
        type == MYSQL_TYPE_TIMESTAMP)
10860
      new_field= item->tmp_table_field_from_field_type(table, 1);
10861
    /* 
10862
      Make sure that the blob fits into a Field_varstring which has 
10863
      2-byte lenght. 
10864
    */
10865
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10866
             convert_blob_length <= Field_varstring::MAX_SIZE && 
10867
             convert_blob_length)
10868
      new_field= new Field_varstring(convert_blob_length, maybe_null,
10869
                                     item->name, table->s,
10870
                                     item->collation.collation);
10871
    else
10872
      new_field= item->make_string_field(table);
10873
    new_field->set_derivation(item->collation.derivation);
10874
    break;
10875
  case DECIMAL_RESULT:
10876
  {
10877
    uint8 dec= item->decimals;
10878
    uint8 intg= ((Item_decimal *) item)->decimal_precision() - dec;
10879
    uint32 len= item->max_length;
10880
10881
    /*
10882
      Trying to put too many digits overall in a DECIMAL(prec,dec)
10883
      will always throw a warning. We must limit dec to
10884
      DECIMAL_MAX_SCALE however to prevent an assert() later.
10885
    */
10886
10887
    if (dec > 0)
10888
    {
10889
      signed int overflow;
10890
10891
      dec= min(dec, DECIMAL_MAX_SCALE);
10892
10893
      /*
10894
        If the value still overflows the field with the corrected dec,
10895
        we'll throw out decimals rather than integers. This is still
10896
        bad and of course throws a truncation warning.
10897
        +1: for decimal point
10898
      */
10899
10900
      overflow= my_decimal_precision_to_length(intg + dec, dec,
10901
                                               item->unsigned_flag) - len;
10902
10903
      if (overflow > 0)
10904
        dec= max(0, dec - overflow);            // too long, discard fract
10905
      else
10906
        len -= item->decimals - dec;            // corrected value fits
10907
    }
10908
10909
    new_field= new Field_new_decimal(len, maybe_null, item->name,
10910
                                     dec, item->unsigned_flag);
10911
    break;
10912
  }
10913
  case ROW_RESULT:
10914
  default:
10915
    // This case should never be choosen
10916
    DBUG_ASSERT(0);
10917
    new_field= 0;
10918
    break;
10919
  }
10920
  if (new_field)
10921
    new_field->init(table);
10922
    
10923
  if (copy_func && item->is_result_field())
10924
    *((*copy_func)++) = item;			// Save for copy_funcs
10925
  if (modify_item)
10926
    item->set_result_field(new_field);
10927
  if (item->type() == Item::NULL_ITEM)
10928
    new_field->is_created_from_null_item= TRUE;
10929
  return new_field;
10930
}
10931
10932
10933
/**
10934
  Create field for information schema table.
10935
10936
  @param thd		Thread handler
10937
  @param table		Temporary table
10938
  @param item		Item to create a field for
10939
10940
  @retval
10941
    0			on error
10942
  @retval
10943
    new_created field
10944
*/
10945
10946
Field *create_tmp_field_for_schema(THD *thd, Item *item, TABLE *table)
10947
{
10948
  if (item->field_type() == MYSQL_TYPE_VARCHAR)
10949
  {
10950
    Field *field;
10951
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10952
      field= new Field_blob(item->max_length, item->maybe_null,
10953
                            item->name, item->collation.collation);
10954
    else
10955
      field= new Field_varstring(item->max_length, item->maybe_null,
10956
                                 item->name,
10957
                                 table->s, item->collation.collation);
10958
    if (field)
10959
      field->init(table);
10960
    return field;
10961
  }
10962
  return item->tmp_table_field_from_field_type(table, 0);
10963
}
10964
10965
10966
/**
10967
  Create field for temporary table.
10968
10969
  @param thd		Thread handler
10970
  @param table		Temporary table
10971
  @param item		Item to create a field for
10972
  @param type		Type of item (normally item->type)
10973
  @param copy_func	If set and item is a function, store copy of item
10974
                       in this array
10975
  @param from_field    if field will be created using other field as example,
10976
                       pointer example field will be written here
10977
  @param default_field	If field has a default value field, store it here
10978
  @param group		1 if we are going to do a relative group by on result
10979
  @param modify_item	1 if item->result_field should point to new item.
10980
                       This is relevent for how fill_record() is going to
10981
                       work:
10982
                       If modify_item is 1 then fill_record() will update
10983
                       the record in the original table.
10984
                       If modify_item is 0 then fill_record() will update
10985
                       the temporary table
10986
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
10987
                             field instead of blob.
10988
10989
  @retval
10990
    0			on error
10991
  @retval
10992
    new_created field
10993
*/
10994
10995
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10996
                        Item ***copy_func, Field **from_field,
10997
                        Field **default_field,
10998
                        bool group, bool modify_item,
10999
                        bool table_cant_handle_bit_fields,
11000
                        bool make_copy_field,
11001
                        uint convert_blob_length)
11002
{
11003
  Field *result;
11004
  Item::Type orig_type= type;
11005
  Item *orig_item= 0;
11006
11007
  if (type != Item::FIELD_ITEM &&
11008
      item->real_item()->type() == Item::FIELD_ITEM)
11009
  {
11010
    orig_item= item;
11011
    item= item->real_item();
11012
    type= Item::FIELD_ITEM;
11013
  }
11014
11015
  switch (type) {
11016
  case Item::SUM_FUNC_ITEM:
11017
  {
11018
    Item_sum *item_sum=(Item_sum*) item;
11019
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
11020
    if (!result)
11021
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
11022
    return result;
11023
  }
11024
  case Item::FIELD_ITEM:
11025
  case Item::DEFAULT_VALUE_ITEM:
11026
  {
11027
    Item_field *field= (Item_field*) item;
11028
    bool orig_modify= modify_item;
11029
    if (orig_type == Item::REF_ITEM)
11030
      modify_item= 0;
11031
    /*
11032
      If item have to be able to store NULLs but underlaid field can't do it,
11033
      create_tmp_field_from_field() can't be used for tmp field creation.
11034
    */
11035
    if (field->maybe_null && !field->field->maybe_null())
11036
    {
11037
      result= create_tmp_field_from_item(thd, item, table, NULL,
11038
                                         modify_item, convert_blob_length);
11039
      *from_field= field->field;
11040
      if (result && modify_item)
11041
        field->result_field= result;
11042
    } 
11043
    else if (table_cant_handle_bit_fields && field->field->type() ==
11044
             MYSQL_TYPE_BIT)
11045
    {
11046
      *from_field= field->field;
11047
      result= create_tmp_field_from_item(thd, item, table, copy_func,
11048
                                        modify_item, convert_blob_length);
11049
      if (result && modify_item)
11050
        field->result_field= result;
11051
    }
11052
    else
11053
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
11054
                                          orig_item ? orig_item->name :
11055
                                          item->name,
11056
                                          table,
11057
                                          modify_item ? field :
11058
                                          NULL,
11059
                                          convert_blob_length);
11060
    if (orig_type == Item::REF_ITEM && orig_modify)
11061
      ((Item_ref*)orig_item)->set_result_field(result);
11062
    if (field->field->eq_def(result))
11063
      *default_field= field->field;
11064
    return result;
11065
  }
11066
  /* Fall through */
11067
  case Item::FUNC_ITEM:
11068
    /* Fall through */
11069
  case Item::COND_ITEM:
11070
  case Item::FIELD_AVG_ITEM:
11071
  case Item::FIELD_STD_ITEM:
11072
  case Item::SUBSELECT_ITEM:
11073
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
11074
  case Item::PROC_ITEM:
11075
  case Item::INT_ITEM:
11076
  case Item::REAL_ITEM:
11077
  case Item::DECIMAL_ITEM:
11078
  case Item::STRING_ITEM:
11079
  case Item::REF_ITEM:
11080
  case Item::NULL_ITEM:
11081
  case Item::VARBIN_ITEM:
11082
    if (make_copy_field)
11083
    {
11084
      DBUG_ASSERT(((Item_result_field*)item)->result_field);
11085
      *from_field= ((Item_result_field*)item)->result_field;
11086
    }
11087
    return create_tmp_field_from_item(thd, item, table,
11088
                                      (make_copy_field ? 0 : copy_func),
11089
                                       modify_item, convert_blob_length);
11090
  case Item::TYPE_HOLDER:  
11091
    result= ((Item_type_holder *)item)->make_field_by_type(table);
11092
    result->set_derivation(item->collation.derivation);
11093
    return result;
11094
  default:					// Dosen't have to be stored
11095
    return 0;
11096
  }
11097
}
11098
11099
/*
11100
  Set up column usage bitmaps for a temporary table
11101
11102
  IMPLEMENTATION
11103
    For temporary tables, we need one bitmap with all columns set and
11104
    a tmp_set bitmap to be used by things like filesort.
11105
*/
11106
11107
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
11108
{
11109
  uint field_count= table->s->fields;
11110
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
11111
              FALSE);
11112
  bitmap_init(&table->tmp_set,
11113
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
11114
              field_count, FALSE);
11115
  /* write_set and all_set are copies of read_set */
11116
  table->def_write_set= table->def_read_set;
11117
  table->s->all_set= table->def_read_set;
11118
  bitmap_set_all(&table->s->all_set);
11119
  table->default_column_bitmaps();
11120
}
11121
11122
11123
/**
11124
  Create a temp table according to a field list.
11125
11126
  Given field pointers are changed to point at tmp_table for
11127
  send_fields. The table object is self contained: it's
11128
  allocated in its own memory root, as well as Field objects
11129
  created for table columns.
11130
  This function will replace Item_sum items in 'fields' list with
11131
  corresponding Item_field items, pointing at the fields in the
11132
  temporary table, unless this was prohibited by TRUE
11133
  value of argument save_sum_fields. The Item_field objects
11134
  are created in THD memory root.
11135
11136
  @param thd                  thread handle
11137
  @param param                a description used as input to create the table
11138
  @param fields               list of items that will be used to define
11139
                              column types of the table (also see NOTES)
11140
  @param group                TODO document
11141
  @param distinct             should table rows be distinct
11142
  @param save_sum_fields      see NOTES
11143
  @param select_options
11144
  @param rows_limit
11145
  @param table_alias          possible name of the temporary table that can
11146
                              be used for name resolving; can be "".
11147
*/
11148
11149
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
11150
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
11151
#define RATIO_TO_PACK_ROWS	       2
11152
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
11153
11154
TABLE *
11155
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
11156
		 ORDER *group, bool distinct, bool save_sum_fields,
11157
		 ulonglong select_options, ha_rows rows_limit,
11158
		 char *table_alias)
11159
{
11160
  MEM_ROOT *mem_root_save, own_root;
11161
  TABLE *table;
11162
  TABLE_SHARE *share;
11163
  uint	i,field_count,null_count,null_pack_length;
11164
  uint  copy_func_count= param->func_count;
11165
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
11166
  uint  blob_count,group_null_items, string_count;
11167
  uint  temp_pool_slot=MY_BIT_NONE;
11168
  uint fieldnr= 0;
11169
  ulong reclength, string_total_length;
11170
  bool  using_unique_constraint= 0;
11171
  bool  use_packed_rows= 0;
11172
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
11173
  char  *tmpname,path[FN_REFLEN];
11174
  uchar	*pos, *group_buff, *bitmaps;
11175
  uchar *null_flags;
11176
  Field **reg_field, **from_field, **default_field;
11177
  uint *blob_field;
11178
  Copy_field *copy=0;
11179
  KEY *keyinfo;
11180
  KEY_PART_INFO *key_part_info;
11181
  Item **copy_func;
11182
  MI_COLUMNDEF *recinfo;
11183
  uint total_uneven_bit_length= 0;
11184
  bool force_copy_fields= param->force_copy_fields;
11185
  DBUG_ENTER("create_tmp_table");
11186
  DBUG_PRINT("enter",
11187
             ("distinct: %d  save_sum_fields: %d  rows_limit: %lu  group: %d",
11188
              (int) distinct, (int) save_sum_fields,
11189
              (ulong) rows_limit,test(group)));
11190
11191
  status_var_increment(thd->status_var.created_tmp_tables);
11192
11193
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11194
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11195
11196
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11197
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11198
            current_pid, temp_pool_slot);
11199
  else
11200
  {
11201
    /* if we run out of slots or we are not using tempool */
11202
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11203
            thd->thread_id, thd->tmp_table++);
11204
  }
11205
11206
  /*
11207
    No need to change table name to lower case as we are only creating
11208
    MyISAM or HEAP tables here
11209
  */
11210
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11211
11212
11213
  if (group)
11214
  {
11215
    if (!param->quick_group)
11216
      group=0;					// Can't use group key
11217
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11218
    {
11219
      /*
11220
        marker == 4 means two things:
11221
        - store NULLs in the key, and
11222
        - convert BIT fields to 64-bit long, needed because MEMORY tables
11223
          can't index BIT fields.
11224
      */
11225
      (*tmp->item)->marker= 4;
11226
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11227
	using_unique_constraint=1;
11228
    }
11229
    if (param->group_length >= MAX_BLOB_WIDTH)
11230
      using_unique_constraint=1;
11231
    if (group)
11232
      distinct=0;				// Can't use distinct
11233
  }
11234
11235
  field_count=param->field_count+param->func_count+param->sum_func_count;
11236
  hidden_field_count=param->hidden_field_count;
11237
11238
  /*
11239
    When loose index scan is employed as access method, it already
11240
    computes all groups and the result of all aggregate functions. We
11241
    make space for the items of the aggregate function in the list of
11242
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11243
    these items are stored in the temporary table.
11244
  */
11245
  if (param->precomputed_group_by)
11246
    copy_func_count+= param->sum_func_count;
11247
  
11248
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11249
11250
  if (!multi_alloc_root(&own_root,
11251
                        &table, sizeof(*table),
11252
                        &share, sizeof(*share),
11253
                        &reg_field, sizeof(Field*) * (field_count+1),
11254
                        &default_field, sizeof(Field*) * (field_count),
11255
                        &blob_field, sizeof(uint)*(field_count+1),
11256
                        &from_field, sizeof(Field*)*field_count,
11257
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
11258
                        &param->keyinfo, sizeof(*param->keyinfo),
11259
                        &key_part_info,
11260
                        sizeof(*key_part_info)*(param->group_parts+1),
11261
                        &param->start_recinfo,
11262
                        sizeof(*param->recinfo)*(field_count*2+4),
11263
                        &tmpname, (uint) strlen(path)+1,
11264
                        &group_buff, (group && ! using_unique_constraint ?
11265
                                      param->group_length : 0),
11266
                        &bitmaps, bitmap_buffer_size(field_count)*2,
11267
                        NullS))
11268
  {
11269
    if (temp_pool_slot != MY_BIT_NONE)
11270
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11271
    DBUG_RETURN(NULL);				/* purecov: inspected */
11272
  }
11273
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11274
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11275
  {
11276
    if (temp_pool_slot != MY_BIT_NONE)
11277
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11278
    free_root(&own_root, MYF(0));               /* purecov: inspected */
11279
    DBUG_RETURN(NULL);				/* purecov: inspected */
11280
  }
11281
  param->items_to_copy= copy_func;
11282
  strmov(tmpname,path);
11283
  /* make table according to fields */
11284
11285
  bzero((char*) table,sizeof(*table));
11286
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
11287
  bzero((char*) default_field, sizeof(Field*) * (field_count));
11288
  bzero((char*) from_field,sizeof(Field*)*field_count);
11289
11290
  table->mem_root= own_root;
11291
  mem_root_save= thd->mem_root;
11292
  thd->mem_root= &table->mem_root;
11293
11294
  table->field=reg_field;
11295
  table->alias= table_alias;
11296
  table->reginfo.lock_type=TL_WRITE;	/* Will be updated */
11297
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11298
  table->map=1;
11299
  table->temp_pool_slot = temp_pool_slot;
11300
  table->copy_blobs= 1;
11301
  table->in_use= thd;
11302
  table->quick_keys.init();
11303
  table->covering_keys.init();
11304
  table->keys_in_use_for_query.init();
11305
11306
  table->s= share;
11307
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11308
  share->blob_field= blob_field;
11309
  share->blob_ptr_size= portable_sizeof_char_ptr;
11310
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11311
  share->table_charset= param->table_charset;
11312
  share->primary_key= MAX_KEY;               // Indicate no primary key
11313
  share->keys_for_keyread.init();
11314
  share->keys_in_use.init();
11315
11316
  /* Calculate which type of fields we will store in the temporary table */
11317
11318
  reclength= string_total_length= 0;
11319
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11320
  param->using_indirect_summary_function=0;
11321
11322
  List_iterator_fast<Item> li(fields);
11323
  Item *item;
11324
  Field **tmp_from_field=from_field;
11325
  while ((item=li++))
11326
  {
11327
    Item::Type type=item->type();
11328
    if (not_all_columns)
11329
    {
11330
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11331
      {
11332
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
11333
          item->update_used_tables();
11334
        if (type == Item::SUBSELECT_ITEM ||
11335
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
11336
        {
11337
	  /*
11338
	    Mark that the we have ignored an item that refers to a summary
11339
	    function. We need to know this if someone is going to use
11340
	    DISTINCT on the result.
11341
	  */
11342
	  param->using_indirect_summary_function=1;
11343
	  continue;
11344
        }
11345
      }
11346
      if (item->const_item() && (int) hidden_field_count <= 0)
11347
        continue; // We don't have to store this
11348
    }
11349
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11350
    {						/* Can't calc group yet */
11351
      ((Item_sum*) item)->result_field=0;
11352
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11353
      {
11354
	Item **argp= ((Item_sum*) item)->args + i;
11355
	Item *arg= *argp;
11356
	if (!arg->const_item())
11357
	{
11358
	  Field *new_field=
11359
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
11360
                             tmp_from_field, &default_field[fieldnr],
11361
                             group != 0,not_all_columns,
11362
                             distinct, 0,
11363
                             param->convert_blob_length);
11364
	  if (!new_field)
11365
	    goto err;					// Should be OOM
11366
	  tmp_from_field++;
11367
	  reclength+=new_field->pack_length();
11368
	  if (new_field->flags & BLOB_FLAG)
11369
	  {
11370
	    *blob_field++= fieldnr;
11371
	    blob_count++;
11372
	  }
11373
          if (new_field->type() == MYSQL_TYPE_BIT)
11374
            total_uneven_bit_length+= new_field->field_length & 7;
11375
	  *(reg_field++)= new_field;
11376
          if (new_field->real_type() == MYSQL_TYPE_STRING ||
11377
              new_field->real_type() == MYSQL_TYPE_VARCHAR)
11378
          {
11379
            string_count++;
11380
            string_total_length+= new_field->pack_length();
11381
          }
11382
          thd->mem_root= mem_root_save;
11383
          thd->change_item_tree(argp, new Item_field(new_field));
11384
          thd->mem_root= &table->mem_root;
11385
	  if (!(new_field->flags & NOT_NULL_FLAG))
11386
          {
11387
	    null_count++;
11388
            /*
11389
              new_field->maybe_null() is still false, it will be
11390
              changed below. But we have to setup Item_field correctly
11391
            */
11392
            (*argp)->maybe_null=1;
11393
          }
11394
          new_field->field_index= fieldnr++;
11395
	}
11396
      }
11397
    }
11398
    else
11399
    {
11400
      /*
11401
	The last parameter to create_tmp_field() is a bit tricky:
11402
11403
	We need to set it to 0 in union, to get fill_record() to modify the
11404
	temporary table.
11405
	We need to set it to 1 on multi-table-update and in select to
11406
	write rows to the temporary table.
11407
	We here distinguish between UNION and multi-table-updates by the fact
11408
	that in the later case group is set to the row pointer.
11409
      */
11410
      Field *new_field= (param->schema_table) ?
11411
        create_tmp_field_for_schema(thd, item, table) :
11412
        create_tmp_field(thd, table, item, type, &copy_func,
11413
                         tmp_from_field, &default_field[fieldnr],
11414
                         group != 0,
11415
                         !force_copy_fields &&
11416
                           (not_all_columns || group !=0),
11417
                         /*
11418
                           If item->marker == 4 then we force create_tmp_field
11419
                           to create a 64-bit longs for BIT fields because HEAP
11420
                           tables can't index BIT fields directly. We do the same
11421
                           for distinct, as we want the distinct index to be
11422
                           usable in this case too.
11423
                         */
11424
                         item->marker == 4 || param->bit_fields_as_long,
11425
                         force_copy_fields,
11426
                         param->convert_blob_length);
11427
11428
      if (!new_field)
11429
      {
11430
	if (thd->is_fatal_error)
11431
	  goto err;				// Got OOM
11432
	continue;				// Some kindf of const item
11433
      }
11434
      if (type == Item::SUM_FUNC_ITEM)
11435
	((Item_sum *) item)->result_field= new_field;
11436
      tmp_from_field++;
11437
      reclength+=new_field->pack_length();
11438
      if (!(new_field->flags & NOT_NULL_FLAG))
11439
	null_count++;
11440
      if (new_field->type() == MYSQL_TYPE_BIT)
11441
        total_uneven_bit_length+= new_field->field_length & 7;
11442
      if (new_field->flags & BLOB_FLAG)
11443
      {
11444
        *blob_field++= fieldnr;
11445
	blob_count++;
11446
      }
11447
      if (item->marker == 4 && item->maybe_null)
11448
      {
11449
	group_null_items++;
11450
	new_field->flags|= GROUP_FLAG;
11451
      }
11452
      new_field->field_index= fieldnr++;
11453
      *(reg_field++)= new_field;
11454
    }
11455
    if (!--hidden_field_count)
11456
    {
11457
      /*
11458
        This was the last hidden field; Remember how many hidden fields could
11459
        have null
11460
      */
11461
      hidden_null_count=null_count;
11462
      /*
11463
	We need to update hidden_field_count as we may have stored group
11464
	functions with constant arguments
11465
      */
11466
      param->hidden_field_count= fieldnr;
11467
      null_count= 0;
11468
    }
11469
  }
11470
  DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field));
11471
  DBUG_ASSERT(field_count >= (uint) (reg_field - table->field));
11472
  field_count= fieldnr;
11473
  *reg_field= 0;
11474
  *blob_field= 0;				// End marker
11475
  share->fields= field_count;
11476
11477
  /* If result table is small; use a heap */
11478
  /* future: storage engine selection can be made dynamic? */
11479
  if (blob_count || using_unique_constraint ||
11480
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11481
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11482
  {
11483
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11484
    table->file= get_new_handler(share, &table->mem_root,
11485
                                 share->db_type());
11486
    if (group &&
11487
	(param->group_parts > table->file->max_key_parts() ||
11488
	 param->group_length > table->file->max_key_length()))
11489
      using_unique_constraint=1;
11490
  }
11491
  else
11492
  {
11493
    share->db_plugin= ha_lock_engine(0, heap_hton);
11494
    table->file= get_new_handler(share, &table->mem_root,
11495
                                 share->db_type());
11496
  }
11497
  if (!table->file)
11498
    goto err;
11499
11500
11501
  if (!using_unique_constraint)
11502
    reclength+= group_null_items;	// null flag is stored separately
11503
11504
  share->blob_fields= blob_count;
11505
  if (blob_count == 0)
11506
  {
11507
    /* We need to ensure that first byte is not 0 for the delete link */
11508
    if (param->hidden_field_count)
11509
      hidden_null_count++;
11510
    else
11511
      null_count++;
11512
  }
11513
  hidden_null_pack_length=(hidden_null_count+7)/8;
11514
  null_pack_length= (hidden_null_pack_length +
11515
                     (null_count + total_uneven_bit_length + 7) / 8);
11516
  reclength+=null_pack_length;
11517
  if (!reclength)
11518
    reclength=1;				// Dummy select
11519
  /* Use packed rows if there is blobs or a lot of space to gain */
11520
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
11521
    use_packed_rows= 1;
11522
11523
  share->reclength= reclength;
11524
  {
11525
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11526
    share->rec_buff_length= alloc_length;
11527
    if (!(table->record[0]= (uchar*)
11528
                            alloc_root(&table->mem_root, alloc_length*3)))
11529
      goto err;
11530
    table->record[1]= table->record[0]+alloc_length;
11531
    share->default_values= table->record[1]+alloc_length;
11532
  }
11533
  copy_func[0]=0;				// End marker
11534
  param->func_count= copy_func - param->items_to_copy; 
11535
11536
  setup_tmp_table_column_bitmaps(table, bitmaps);
11537
11538
  recinfo=param->start_recinfo;
11539
  null_flags=(uchar*) table->record[0];
11540
  pos=table->record[0]+ null_pack_length;
11541
  if (null_pack_length)
11542
  {
11543
    bzero((uchar*) recinfo,sizeof(*recinfo));
11544
    recinfo->type=FIELD_NORMAL;
11545
    recinfo->length=null_pack_length;
11546
    recinfo++;
11547
    bfill(null_flags,null_pack_length,255);	// Set null fields
11548
11549
    table->null_flags= (uchar*) table->record[0];
11550
    share->null_fields= null_count+ hidden_null_count;
11551
    share->null_bytes= null_pack_length;
11552
  }
11553
  null_count= (blob_count == 0) ? 1 : 0;
11554
  hidden_field_count=param->hidden_field_count;
11555
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11556
  {
11557
    Field *field= *reg_field;
11558
    uint length;
11559
    bzero((uchar*) recinfo,sizeof(*recinfo));
11560
11561
    if (!(field->flags & NOT_NULL_FLAG))
11562
    {
11563
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
11564
      {
11565
	/*
11566
	  We have to reserve one byte here for NULL bits,
11567
	  as this is updated by 'end_update()'
11568
	*/
11569
	*pos++=0;				// Null is stored here
11570
	recinfo->length=1;
11571
	recinfo->type=FIELD_NORMAL;
11572
	recinfo++;
11573
	bzero((uchar*) recinfo,sizeof(*recinfo));
11574
      }
11575
      else
11576
      {
11577
	recinfo->null_bit= 1 << (null_count & 7);
11578
	recinfo->null_pos= null_count/8;
11579
      }
11580
      field->move_field(pos,null_flags+null_count/8,
11581
			1 << (null_count & 7));
11582
      null_count++;
11583
    }
11584
    else
11585
      field->move_field(pos,(uchar*) 0,0);
11586
    if (field->type() == MYSQL_TYPE_BIT)
11587
    {
11588
      /* We have to reserve place for extra bits among null bits */
11589
      ((Field_bit*) field)->set_bit_ptr(null_flags + null_count / 8,
11590
                                        null_count & 7);
11591
      null_count+= (field->field_length & 7);
11592
    }
11593
    field->reset();
11594
11595
    /*
11596
      Test if there is a default field value. The test for ->ptr is to skip
11597
      'offset' fields generated by initalize_tables
11598
    */
11599
    if (default_field[i] && default_field[i]->ptr)
11600
    {
11601
      /* 
11602
         default_field[i] is set only in the cases  when 'field' can
11603
         inherit the default value that is defined for the field referred
11604
         by the Item_field object from which 'field' has been created.
11605
      */
11606
      my_ptrdiff_t diff;
11607
      Field *orig_field= default_field[i];
11608
      /* Get the value from default_values */
11609
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11610
                            orig_field->table->record[0]);
11611
      orig_field->move_field_offset(diff);      // Points now at default_values
11612
      if (orig_field->is_real_null())
11613
        field->set_null();
11614
      else
11615
      {
11616
        field->set_notnull();
11617
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
11618
      }
11619
      orig_field->move_field_offset(-diff);     // Back to record[0]
11620
    } 
11621
11622
    if (from_field[i])
11623
    {						/* Not a table Item */
11624
      copy->set(field,from_field[i],save_sum_fields);
11625
      copy++;
11626
    }
11627
    length=field->pack_length();
11628
    pos+= length;
11629
11630
    /* Make entry for create table */
11631
    recinfo->length=length;
11632
    if (field->flags & BLOB_FLAG)
11633
      recinfo->type= (int) FIELD_BLOB;
11634
    else if (use_packed_rows &&
11635
             field->real_type() == MYSQL_TYPE_STRING &&
11636
	     length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11637
      recinfo->type=FIELD_SKIP_ENDSPACE;
11638
    else
11639
      recinfo->type=FIELD_NORMAL;
11640
    if (!--hidden_field_count)
11641
      null_count=(null_count+7) & ~7;		// move to next byte
11642
11643
    // fix table name in field entry
11644
    field->table_name= &table->alias;
11645
  }
11646
11647
  param->copy_field_end=copy;
11648
  param->recinfo=recinfo;
11649
  store_record(table,s->default_values);        // Make empty default record
11650
11651
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)		// No limit
11652
    share->max_rows= ~(ha_rows) 0;
11653
  else
11654
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11655
                                 min(thd->variables.tmp_table_size,
11656
                                     thd->variables.max_heap_table_size) :
11657
                                 thd->variables.tmp_table_size) /
11658
			         share->reclength);
11659
  set_if_bigger(share->max_rows,1);		// For dummy start options
11660
  /*
11661
    Push the LIMIT clause to the temporary table creation, so that we
11662
    materialize only up to 'rows_limit' records instead of all result records.
11663
  */
11664
  set_if_smaller(share->max_rows, rows_limit);
11665
  param->end_write_records= rows_limit;
11666
11667
  keyinfo= param->keyinfo;
11668
11669
  if (group)
11670
  {
11671
    DBUG_PRINT("info",("Creating group key in temporary table"));
11672
    table->group=group;				/* Table is grouped by key */
11673
    param->group_buff=group_buff;
11674
    share->keys=1;
11675
    share->uniques= test(using_unique_constraint);
11676
    table->key_info=keyinfo;
11677
    keyinfo->key_part=key_part_info;
11678
    keyinfo->flags=HA_NOSAME;
11679
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11680
    keyinfo->key_length=0;
11681
    keyinfo->rec_per_key=0;
11682
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11683
    keyinfo->name= (char*) "group_key";
11684
    ORDER *cur_group= group;
11685
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11686
    {
11687
      Field *field=(*cur_group->item)->get_tmp_table_field();
11688
      bool maybe_null=(*cur_group->item)->maybe_null;
11689
      key_part_info->null_bit=0;
11690
      key_part_info->field=  field;
11691
      key_part_info->offset= field->offset(table->record[0]);
11692
      key_part_info->length= (uint16) field->key_length();
11693
      key_part_info->type=   (uint8) field->key_type();
11694
      key_part_info->key_type =
11695
	((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11696
	 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11697
	 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11698
	0 : FIELDFLAG_BINARY;
11699
      if (!using_unique_constraint)
11700
      {
11701
	cur_group->buff=(char*) group_buff;
11702
	if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11703
                                                     group_buff +
11704
                                                     test(maybe_null),
11705
                                                     field->null_ptr,
11706
                                                     field->null_bit)))
11707
	  goto err; /* purecov: inspected */
11708
	if (maybe_null)
11709
	{
11710
	  /*
11711
	    To be able to group on NULL, we reserved place in group_buff
11712
	    for the NULL flag just before the column. (see above).
11713
	    The field data is after this flag.
11714
	    The NULL flag is updated in 'end_update()' and 'end_write()'
11715
	  */
11716
	  keyinfo->flags|= HA_NULL_ARE_EQUAL;	// def. that NULL == NULL
11717
	  key_part_info->null_bit=field->null_bit;
11718
	  key_part_info->null_offset= (uint) (field->null_ptr -
11719
					      (uchar*) table->record[0]);
11720
          cur_group->buff++;                        // Pointer to field data
11721
	  group_buff++;                         // Skipp null flag
11722
	}
11723
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11724
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11725
	group_buff+= cur_group->field->pack_length();
11726
      }
11727
      keyinfo->key_length+=  key_part_info->length;
11728
    }
11729
  }
11730
11731
  if (distinct && field_count != param->hidden_field_count)
11732
  {
11733
    /*
11734
      Create an unique key or an unique constraint over all columns
11735
      that should be in the result.  In the temporary table, there are
11736
      'param->hidden_field_count' extra columns, whose null bits are stored
11737
      in the first 'hidden_null_pack_length' bytes of the row.
11738
    */
11739
    DBUG_PRINT("info",("hidden_field_count: %d", param->hidden_field_count));
11740
11741
    if (blob_count)
11742
    {
11743
      /*
11744
        Special mode for index creation in MyISAM used to support unique
11745
        indexes on blobs with arbitrary length. Such indexes cannot be
11746
        used for lookups.
11747
      */
11748
      share->uniques= 1;
11749
    }
11750
    null_pack_length-=hidden_null_pack_length;
11751
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11752
			 (share->uniques ? test(null_pack_length) : 0));
11753
    table->distinct= 1;
11754
    share->keys= 1;
11755
    if (!(key_part_info= (KEY_PART_INFO*)
11756
          alloc_root(&table->mem_root,
11757
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11758
      goto err;
11759
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11760
    table->key_info=keyinfo;
11761
    keyinfo->key_part=key_part_info;
11762
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11763
    keyinfo->key_length=(uint16) reclength;
11764
    keyinfo->name= (char*) "distinct_key";
11765
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11766
    keyinfo->rec_per_key=0;
11767
11768
    /*
11769
      Create an extra field to hold NULL bits so that unique indexes on
11770
      blobs can distinguish NULL from 0. This extra field is not needed
11771
      when we do not use UNIQUE indexes for blobs.
11772
    */
11773
    if (null_pack_length && share->uniques)
11774
    {
11775
      key_part_info->null_bit=0;
11776
      key_part_info->offset=hidden_null_pack_length;
11777
      key_part_info->length=null_pack_length;
11778
      key_part_info->field= new Field_string(table->record[0],
11779
                                             (uint32) key_part_info->length,
11780
                                             (uchar*) 0,
11781
                                             (uint) 0,
11782
                                             Field::NONE,
11783
                                             NullS, &my_charset_bin);
11784
      if (!key_part_info->field)
11785
        goto err;
11786
      key_part_info->field->init(table);
11787
      key_part_info->key_type=FIELDFLAG_BINARY;
11788
      key_part_info->type=    HA_KEYTYPE_BINARY;
11789
      key_part_info++;
11790
    }
11791
    /* Create a distinct key over the columns we are going to return */
11792
    for (i=param->hidden_field_count, reg_field=table->field + i ;
11793
	 i < field_count;
11794
	 i++, reg_field++, key_part_info++)
11795
    {
11796
      key_part_info->null_bit=0;
11797
      key_part_info->field=    *reg_field;
11798
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
11799
      key_part_info->length=   (uint16) (*reg_field)->pack_length();
11800
      /* TODO:
11801
        The below method of computing the key format length of the
11802
        key part is a copy/paste from opt_range.cc, and table.cc.
11803
        This should be factored out, e.g. as a method of Field.
11804
        In addition it is not clear if any of the Field::*_length
11805
        methods is supposed to compute the same length. If so, it
11806
        might be reused.
11807
      */
11808
      key_part_info->store_length= key_part_info->length;
11809
11810
      if ((*reg_field)->real_maybe_null())
11811
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11812
      if ((*reg_field)->type() == MYSQL_TYPE_BLOB || 
11813
          (*reg_field)->real_type() == MYSQL_TYPE_VARCHAR)
11814
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11815
11816
      key_part_info->type=     (uint8) (*reg_field)->key_type();
11817
      key_part_info->key_type =
11818
	((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11819
	 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11820
	 (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11821
	0 : FIELDFLAG_BINARY;
11822
    }
11823
  }
11824
11825
  if (thd->is_fatal_error)				// If end of memory
11826
    goto err;					 /* purecov: inspected */
11827
  share->db_record_offset= 1;
11828
  if (share->db_type() == myisam_hton)
11829
  {
11830
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11831
                                &param->recinfo, select_options))
11832
      goto err;
11833
  }
11834
  if (open_tmp_table(table))
11835
    goto err;
11836
11837
  thd->mem_root= mem_root_save;
11838
11839
  DBUG_RETURN(table);
11840
11841
err:
11842
  thd->mem_root= mem_root_save;
11843
  free_tmp_table(thd,table);                    /* purecov: inspected */
11844
  if (temp_pool_slot != MY_BIT_NONE)
11845
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11846
  DBUG_RETURN(NULL);				/* purecov: inspected */
11847
}
11848
11849
11850
11851
11852
/*
11853
  Create a temporary table to weed out duplicate rowid combinations
11854
11855
  SYNOPSIS
11856
11857
    create_duplicate_weedout_tmp_table()
11858
      thd
11859
      uniq_tuple_length_arg
11860
      SJ_TMP_TABLE 
11861
11862
  DESCRIPTION
11863
    Create a temporary table to weed out duplicate rowid combinations. The
11864
    table has a single column that is a concatenation of all rowids in the
11865
    combination. 
11866
11867
    Depending on the needed length, there are two cases:
11868
11869
    1. When the length of the column < max_key_length:
11870
11871
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11872
11873
    2. Otherwise (not a valid SQL syntax but internally supported):
11874
11875
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11876
11877
    The code in this function was produced by extraction of relevant parts
11878
    from create_tmp_table().
11879
11880
  RETURN
11881
    created table
11882
    NULL on error
11883
*/
11884
11885
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
11886
                                          uint uniq_tuple_length_arg,
11887
                                          SJ_TMP_TABLE *sjtbl)
11888
{
11889
  MEM_ROOT *mem_root_save, own_root;
11890
  TABLE *table;
11891
  TABLE_SHARE *share;
11892
  uint  temp_pool_slot=MY_BIT_NONE;
11893
  char	*tmpname,path[FN_REFLEN];
11894
  Field **reg_field;
11895
  KEY_PART_INFO *key_part_info;
11896
  KEY *keyinfo;
11897
  uchar *group_buff;
11898
  uchar *bitmaps;
11899
  uint *blob_field;
11900
  MI_COLUMNDEF *recinfo, *start_recinfo;
11901
  bool using_unique_constraint=FALSE;
11902
  bool use_packed_rows= FALSE;
11903
  Field *field, *key_field;
11904
  uint blob_count, null_pack_length, null_count;
11905
  uchar *null_flags;
11906
  uchar *pos;
11907
  DBUG_ENTER("create_duplicate_weedout_tmp_table");
11908
  
11909
  /*
11910
    STEP 1: Get temporary table name
11911
  */
11912
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11913
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11914
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11915
11916
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11917
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11918
	    current_pid, temp_pool_slot);
11919
  else
11920
  {
11921
    /* if we run out of slots or we are not using tempool */
11922
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11923
            thd->thread_id, thd->tmp_table++);
11924
  }
11925
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11926
11927
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
11928
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11929
    using_unique_constraint= TRUE;
11930
11931
  /* STEP 3: Allocate memory for temptable description */
11932
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11933
  if (!multi_alloc_root(&own_root,
11934
                        &table, sizeof(*table),
11935
                        &share, sizeof(*share),
11936
                        &reg_field, sizeof(Field*) * (1+1),
11937
                        &blob_field, sizeof(uint)*2,
11938
                        &keyinfo, sizeof(*keyinfo),
11939
                        &key_part_info, sizeof(*key_part_info) * 2,
11940
                        &start_recinfo,
11941
                        sizeof(*recinfo)*(1*2+4),
11942
                        &tmpname, (uint) strlen(path)+1,
11943
                        &group_buff, (!using_unique_constraint ?
11944
                                      uniq_tuple_length_arg : 0),
11945
                        &bitmaps, bitmap_buffer_size(1)*2,
11946
                        NullS))
11947
  {
11948
    if (temp_pool_slot != MY_BIT_NONE)
11949
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11950
    DBUG_RETURN(NULL);
11951
  }
11952
  strmov(tmpname,path);
11953
  
11954
11955
  /* STEP 4: Create TABLE description */
11956
  bzero((char*) table,sizeof(*table));
11957
  bzero((char*) reg_field,sizeof(Field*)*2);
11958
11959
  table->mem_root= own_root;
11960
  mem_root_save= thd->mem_root;
11961
  thd->mem_root= &table->mem_root;
11962
11963
  table->field=reg_field;
11964
  table->alias= "weedout-tmp";
11965
  table->reginfo.lock_type=TL_WRITE;	/* Will be updated */
11966
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11967
  table->map=1;
11968
  table->temp_pool_slot = temp_pool_slot;
11969
  table->copy_blobs= 1;
11970
  table->in_use= thd;
11971
  table->quick_keys.init();
11972
  table->covering_keys.init();
11973
  table->keys_in_use_for_query.init();
11974
11975
  table->s= share;
11976
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11977
  share->blob_field= blob_field;
11978
  share->blob_ptr_size= portable_sizeof_char_ptr;
11979
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11980
  share->table_charset= NULL;
11981
  share->primary_key= MAX_KEY;               // Indicate no primary key
11982
  share->keys_for_keyread.init();
11983
  share->keys_in_use.init();
11984
11985
  blob_count= 0;
11986
11987
  /* Create the field */
11988
  {
11989
    /*
11990
      For the sake of uniformity, always use Field_varstring (altough we could
11991
      use Field_string for shorter keys)
11992
    */
11993
    field= new Field_varstring(uniq_tuple_length_arg, FALSE, "rowids", share,
11994
                               &my_charset_bin);
11995
    if (!field)
11996
      DBUG_RETURN(0);
11997
    field->table= table;
11998
    field->key_start.init(0);
11999
    field->part_of_key.init(0);
12000
    field->part_of_sortkey.init(0);
12001
    field->unireg_check= Field::NONE;
12002
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
12003
    field->reset_fields();
12004
    field->init(table);
12005
    field->orig_table= NULL;
12006
     
12007
    field->field_index= 0;
12008
    
12009
    *(reg_field++)= field;
12010
    *blob_field= 0;
12011
    *reg_field= 0;
12012
12013
    share->fields= 1;
12014
    share->blob_fields= 0;
12015
  }
12016
12017
  uint reclength= field->pack_length();
12018
  if (using_unique_constraint)
12019
  { 
12020
    share->db_plugin= ha_lock_engine(0, myisam_hton);
12021
    table->file= get_new_handler(share, &table->mem_root,
12022
                                 share->db_type());
12023
    DBUG_ASSERT(uniq_tuple_length_arg <= table->file->max_key_length());
12024
  }
12025
  else
12026
  {
12027
    share->db_plugin= ha_lock_engine(0, heap_hton);
12028
    table->file= get_new_handler(share, &table->mem_root,
12029
                                 share->db_type());
12030
  }
12031
  if (!table->file)
12032
    goto err;
12033
12034
  null_count=1;
12035
  
12036
  null_pack_length= 1;
12037
  reclength += null_pack_length;
12038
12039
  share->reclength= reclength;
12040
  {
12041
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
12042
    share->rec_buff_length= alloc_length;
12043
    if (!(table->record[0]= (uchar*)
12044
                            alloc_root(&table->mem_root, alloc_length*3)))
12045
      goto err;
12046
    table->record[1]= table->record[0]+alloc_length;
12047
    share->default_values= table->record[1]+alloc_length;
12048
  }
12049
  setup_tmp_table_column_bitmaps(table, bitmaps);
12050
12051
  recinfo= start_recinfo;
12052
  null_flags=(uchar*) table->record[0];
12053
  pos=table->record[0]+ null_pack_length;
12054
  if (null_pack_length)
12055
  {
12056
    bzero((uchar*) recinfo,sizeof(*recinfo));
12057
    recinfo->type=FIELD_NORMAL;
12058
    recinfo->length=null_pack_length;
12059
    recinfo++;
12060
    bfill(null_flags,null_pack_length,255);	// Set null fields
12061
12062
    table->null_flags= (uchar*) table->record[0];
12063
    share->null_fields= null_count;
12064
    share->null_bytes= null_pack_length;
12065
  }
12066
  null_count=1;
12067
12068
  {
12069
    //Field *field= *reg_field;
12070
    uint length;
12071
    bzero((uchar*) recinfo,sizeof(*recinfo));
12072
    field->move_field(pos,(uchar*) 0,0);
12073
12074
    field->reset();
12075
    /*
12076
      Test if there is a default field value. The test for ->ptr is to skip
12077
      'offset' fields generated by initalize_tables
12078
    */
12079
    // Initialize the table field:
12080
    bzero(field->ptr, field->pack_length());
12081
12082
    length=field->pack_length();
12083
    pos+= length;
12084
12085
    /* Make entry for create table */
12086
    recinfo->length=length;
12087
    if (field->flags & BLOB_FLAG)
12088
      recinfo->type= (int) FIELD_BLOB;
12089
    else if (use_packed_rows &&
12090
             field->real_type() == MYSQL_TYPE_STRING &&
12091
	     length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
12092
      recinfo->type=FIELD_SKIP_ENDSPACE;
12093
    else
12094
      recinfo->type=FIELD_NORMAL;
12095
12096
    field->table_name= &table->alias;
12097
  }
12098
12099
  //param->recinfo=recinfo;
12100
  //store_record(table,s->default_values);        // Make empty default record
12101
12102
  if (thd->variables.tmp_table_size == ~ (ulonglong) 0)		// No limit
12103
    share->max_rows= ~(ha_rows) 0;
12104
  else
12105
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
12106
                                 min(thd->variables.tmp_table_size,
12107
                                     thd->variables.max_heap_table_size) :
12108
                                 thd->variables.tmp_table_size) /
12109
			         share->reclength);
12110
  set_if_bigger(share->max_rows,1);		// For dummy start options
12111
12112
12113
  //// keyinfo= param->keyinfo;
12114
  if (TRUE)
12115
  {
12116
    DBUG_PRINT("info",("Creating group key in temporary table"));
12117
    share->keys=1;
12118
    share->uniques= test(using_unique_constraint);
12119
    table->key_info=keyinfo;
12120
    keyinfo->key_part=key_part_info;
12121
    keyinfo->flags=HA_NOSAME;
12122
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
12123
    keyinfo->key_length=0;
12124
    keyinfo->rec_per_key=0;
12125
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
12126
    keyinfo->name= (char*) "weedout_key";
12127
    {
12128
      key_part_info->null_bit=0;
12129
      key_part_info->field=  field;
12130
      key_part_info->offset= field->offset(table->record[0]);
12131
      key_part_info->length= (uint16) field->key_length();
12132
      key_part_info->type=   (uint8) field->key_type();
12133
      key_part_info->key_type = FIELDFLAG_BINARY;
12134
      if (!using_unique_constraint)
12135
      {
12136
	if (!(key_field= field->new_key_field(thd->mem_root, table,
12137
                                              group_buff,
12138
                                              field->null_ptr,
12139
                                              field->null_bit)))
12140
	  goto err;
12141
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
12142
      }
12143
      keyinfo->key_length+=  key_part_info->length;
12144
    }
12145
  }
12146
12147
  if (thd->is_fatal_error)				// If end of memory
12148
    goto err;
12149
  share->db_record_offset= 1;
12150
  if (share->db_type() == myisam_hton)
12151
  {
12152
    recinfo++;
12153
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
12154
      goto err;
12155
  }
12156
  sjtbl->start_recinfo= start_recinfo;
12157
  sjtbl->recinfo=       recinfo;
12158
  if (open_tmp_table(table))
12159
    goto err;
12160
12161
  thd->mem_root= mem_root_save;
12162
  DBUG_RETURN(table);
12163
12164
err:
12165
  thd->mem_root= mem_root_save;
12166
  free_tmp_table(thd,table);                    /* purecov: inspected */
12167
  if (temp_pool_slot != MY_BIT_NONE)
12168
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
12169
  DBUG_RETURN(NULL);				/* purecov: inspected */
12170
}
12171
12172
/****************************************************************************/
12173
12174
/**
12175
  Create a reduced TABLE object with properly set up Field list from a
12176
  list of field definitions.
12177
12178
    The created table doesn't have a table handler associated with
12179
    it, has no keys, no group/distinct, no copy_funcs array.
12180
    The sole purpose of this TABLE object is to use the power of Field
12181
    class to read/write data to/from table->record[0]. Then one can store
12182
    the record in any container (RB tree, hash, etc).
12183
    The table is created in THD mem_root, so are the table's fields.
12184
    Consequently, if you don't BLOB fields, you don't need to free it.
12185
12186
  @param thd         connection handle
12187
  @param field_list  list of column definitions
12188
12189
  @return
12190
    0 if out of memory, TABLE object in case of success
12191
*/
12192
12193
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
12194
{
12195
  uint field_count= field_list.elements;
12196
  uint blob_count= 0;
12197
  Field **field;
12198
  Create_field *cdef;                           /* column definition */
12199
  uint record_length= 0;
12200
  uint null_count= 0;                 /* number of columns which may be null */
12201
  uint null_pack_length;              /* NULL representation array length */
12202
  uint *blob_field;
12203
  uchar *bitmaps;
12204
  TABLE *table;
12205
  TABLE_SHARE *share;
12206
12207
  if (!multi_alloc_root(thd->mem_root,
12208
                        &table, sizeof(*table),
12209
                        &share, sizeof(*share),
12210
                        &field, (field_count + 1) * sizeof(Field*),
12211
                        &blob_field, (field_count+1) *sizeof(uint),
12212
                        &bitmaps, bitmap_buffer_size(field_count)*2,
12213
                        NullS))
12214
    return 0;
12215
12216
  bzero(table, sizeof(*table));
12217
  bzero(share, sizeof(*share));
12218
  table->field= field;
12219
  table->s= share;
12220
  share->blob_field= blob_field;
12221
  share->fields= field_count;
12222
  share->blob_ptr_size= portable_sizeof_char_ptr;
12223
  setup_tmp_table_column_bitmaps(table, bitmaps);
12224
12225
  /* Create all fields and calculate the total length of record */
12226
  List_iterator_fast<Create_field> it(field_list);
12227
  while ((cdef= it++))
12228
  {
12229
    *field= make_field(share, 0, cdef->length,
12230
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
12231
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
12232
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
12233
                       cdef->unireg_check,
12234
                       cdef->interval, cdef->field_name);
12235
    if (!*field)
12236
      goto error;
12237
    (*field)->init(table);
12238
    record_length+= (*field)->pack_length();
12239
    if (! ((*field)->flags & NOT_NULL_FLAG))
12240
      null_count++;
12241
12242
    if ((*field)->flags & BLOB_FLAG)
12243
      share->blob_field[blob_count++]= (uint) (field - table->field);
12244
12245
    field++;
12246
  }
12247
  *field= NULL;                             /* mark the end of the list */
12248
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
12249
  share->blob_fields= blob_count;
12250
12251
  null_pack_length= (null_count + 7)/8;
12252
  share->reclength= record_length + null_pack_length;
12253
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12254
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12255
  if (!table->record[0])
12256
    goto error;
12257
12258
  if (null_pack_length)
12259
  {
12260
    table->null_flags= (uchar*) table->record[0];
12261
    share->null_fields= null_count;
12262
    share->null_bytes= null_pack_length;
12263
  }
12264
12265
  table->in_use= thd;           /* field->reset() may access table->in_use */
12266
  {
12267
    /* Set up field pointers */
12268
    uchar *null_pos= table->record[0];
12269
    uchar *field_pos= null_pos + share->null_bytes;
12270
    uint null_bit= 1;
12271
12272
    for (field= table->field; *field; ++field)
12273
    {
12274
      Field *cur_field= *field;
12275
      if ((cur_field->flags & NOT_NULL_FLAG))
12276
        cur_field->move_field(field_pos);
12277
      else
12278
      {
12279
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12280
        null_bit<<= 1;
12281
        if (null_bit == (1 << 8))
12282
        {
12283
          ++null_pos;
12284
          null_bit= 1;
12285
        }
12286
      }
12287
      cur_field->reset();
12288
12289
      field_pos+= cur_field->pack_length();
12290
    }
12291
  }
12292
  return table;
12293
error:
12294
  for (field= table->field; *field; ++field)
12295
    delete *field;                         /* just invokes field destructor */
12296
  return 0;
12297
}
12298
12299
12300
static bool open_tmp_table(TABLE *table)
12301
{
12302
  int error;
12303
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12304
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12305
  {
12306
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
12307
    table->db_stat=0;
12308
    return(1);
12309
  }
12310
  (void) table->file->extra(HA_EXTRA_QUICK);		/* Faster */
12311
  return(0);
12312
}
12313
12314
12315
/*
12316
  Create MyISAM temporary table
12317
12318
  SYNOPSIS
12319
    create_myisam_tmp_table()
12320
      table           Table object that descrimes the table to be created
12321
      keyinfo         Description of the index (there is always one index)
12322
      start_recinfo   MyISAM's column descriptions
12323
      recinfo INOUT   End of MyISAM's column descriptions
12324
      options         Option bits
12325
   
12326
  DESCRIPTION
12327
    Create a MyISAM temporary table according to passed description. The is
12328
    assumed to have one unique index or constraint.
12329
12330
    The passed array or MI_COLUMNDEF structures must have this form:
12331
12332
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12333
         when there are many nullable columns)
12334
      2. Table columns
12335
      3. One free MI_COLUMNDEF element (*recinfo points here)
12336
   
12337
    This function may use the free element to create hash column for unique
12338
    constraint.
12339
12340
   RETURN
12341
     FALSE - OK
12342
     TRUE  - Error
12343
*/
12344
12345
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
12346
                                    MI_COLUMNDEF *start_recinfo,
12347
                                    MI_COLUMNDEF **recinfo, 
12348
				    ulonglong options)
12349
{
12350
  int error;
12351
  MI_KEYDEF keydef;
12352
  MI_UNIQUEDEF uniquedef;
12353
  TABLE_SHARE *share= table->s;
12354
  DBUG_ENTER("create_myisam_tmp_table");
12355
12356
  if (share->keys)
12357
  {						// Get keys for ni_create
12358
    bool using_unique_constraint=0;
12359
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12360
                                            sizeof(*seg) * keyinfo->key_parts);
12361
    if (!seg)
12362
      goto err;
12363
12364
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
12365
    if (keyinfo->key_length >= table->file->max_key_length() ||
12366
	keyinfo->key_parts > table->file->max_key_parts() ||
12367
	share->uniques)
12368
    {
12369
      /* Can't create a key; Make a unique constraint instead of a key */
12370
      share->keys=    0;
12371
      share->uniques= 1;
12372
      using_unique_constraint=1;
12373
      bzero((char*) &uniquedef,sizeof(uniquedef));
12374
      uniquedef.keysegs=keyinfo->key_parts;
12375
      uniquedef.seg=seg;
12376
      uniquedef.null_are_equal=1;
12377
12378
      /* Create extra column for hash value */
12379
      bzero((uchar*) *recinfo,sizeof(**recinfo));
12380
      (*recinfo)->type= FIELD_CHECK;
12381
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12382
      (*recinfo)++;
12383
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
12384
    }
12385
    else
12386
    {
12387
      /* Create an unique key */
12388
      bzero((char*) &keydef,sizeof(keydef));
12389
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12390
      keydef.keysegs=  keyinfo->key_parts;
12391
      keydef.seg= seg;
12392
    }
12393
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12394
    {
12395
      Field *field=keyinfo->key_part[i].field;
12396
      seg->flag=     0;
12397
      seg->language= field->charset()->number;
12398
      seg->length=   keyinfo->key_part[i].length;
12399
      seg->start=    keyinfo->key_part[i].offset;
12400
      if (field->flags & BLOB_FLAG)
12401
      {
12402
	seg->type=
12403
	((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12404
	 HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12405
	seg->bit_start= (uint8)(field->pack_length() - share->blob_ptr_size);
12406
	seg->flag= HA_BLOB_PART;
12407
	seg->length=0;			// Whole blob in unique constraint
12408
      }
12409
      else
12410
      {
12411
	seg->type= keyinfo->key_part[i].type;
12412
        /* Tell handler if it can do suffic space compression */
12413
	if (field->real_type() == MYSQL_TYPE_STRING &&
12414
	    keyinfo->key_part[i].length > 4)
12415
	  seg->flag|= HA_SPACE_PACK;
12416
      }
12417
      if (!(field->flags & NOT_NULL_FLAG))
12418
      {
12419
	seg->null_bit= field->null_bit;
12420
	seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12421
	/*
12422
	  We are using a GROUP BY on something that contains NULL
12423
	  In this case we have to tell MyISAM that two NULL should
12424
	  on INSERT be regarded at the same value
12425
	*/
12426
	if (!using_unique_constraint)
12427
	  keydef.flag|= HA_NULL_ARE_EQUAL;
12428
      }
12429
    }
12430
  }
12431
  MI_CREATE_INFO create_info;
12432
  bzero((char*) &create_info,sizeof(create_info));
12433
12434
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12435
      OPTION_BIG_TABLES)
12436
    create_info.data_file_length= ~(ulonglong) 0;
12437
12438
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12439
		       (uint) (*recinfo-start_recinfo),
12440
		       start_recinfo,
12441
		       share->uniques, &uniquedef,
12442
		       &create_info,
12443
		       HA_CREATE_TMP_TABLE)))
12444
  {
12445
    table->file->print_error(error,MYF(0));	/* purecov: inspected */
12446
    table->db_stat=0;
12447
    goto err;
12448
  }
12449
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12450
  share->db_record_offset= 1;
12451
  DBUG_RETURN(0);
12452
 err:
12453
  DBUG_RETURN(1);
12454
}
12455
12456
12457
void
12458
free_tmp_table(THD *thd, TABLE *entry)
12459
{
12460
  MEM_ROOT own_root= entry->mem_root;
12461
  const char *save_proc_info;
12462
  DBUG_ENTER("free_tmp_table");
12463
  DBUG_PRINT("enter",("table: %s",entry->alias));
12464
12465
  save_proc_info=thd->proc_info;
12466
  thd_proc_info(thd, "removing tmp table");
12467
12468
  if (entry->file)
12469
  {
12470
    if (entry->db_stat)
12471
      entry->file->ha_drop_table(entry->s->table_name.str);
12472
    else
12473
      entry->file->ha_delete_table(entry->s->table_name.str);
12474
    delete entry->file;
12475
  }
12476
12477
  /* free blobs */
12478
  for (Field **ptr=entry->field ; *ptr ; ptr++)
12479
    (*ptr)->free();
12480
  free_io_cache(entry);
12481
12482
  if (entry->temp_pool_slot != MY_BIT_NONE)
12483
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12484
12485
  plugin_unlock(0, entry->s->db_plugin);
12486
12487
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12488
  thd_proc_info(thd, save_proc_info);
12489
12490
  DBUG_VOID_RETURN;
12491
}
12492
12493
/**
12494
  If a HEAP table gets full, create a MyISAM table and copy all rows
12495
  to this.
12496
*/
12497
12498
bool create_myisam_from_heap(THD *thd, TABLE *table,
12499
                             MI_COLUMNDEF *start_recinfo,
12500
                             MI_COLUMNDEF **recinfo, 
12501
			     int error, bool ignore_last_dupp_key_error)
12502
{
12503
  TABLE new_table;
12504
  TABLE_SHARE share;
12505
  const char *save_proc_info;
12506
  int write_err;
12507
  DBUG_ENTER("create_myisam_from_heap");
12508
12509
  if (table->s->db_type() != heap_hton || 
12510
      error != HA_ERR_RECORD_FILE_FULL)
12511
  {
12512
    table->file->print_error(error,MYF(0));
12513
    DBUG_RETURN(1);
12514
  }
12515
  new_table= *table;
12516
  share= *table->s;
12517
  new_table.s= &share;
12518
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12519
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12520
                                        new_table.s->db_type())))
12521
    DBUG_RETURN(1);				// End of memory
12522
12523
  save_proc_info=thd->proc_info;
12524
  thd_proc_info(thd, "converting HEAP to MyISAM");
12525
12526
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12527
                              recinfo, thd->lex->select_lex.options | 
12528
                                               thd->options))
12529
    goto err2;
12530
  if (open_tmp_table(&new_table))
12531
    goto err1;
12532
  if (table->file->indexes_are_disabled())
12533
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12534
  table->file->ha_index_or_rnd_end();
12535
  table->file->ha_rnd_init(1);
12536
  if (table->no_rows)
12537
  {
12538
    new_table.file->extra(HA_EXTRA_NO_ROWS);
12539
    new_table.no_rows=1;
12540
  }
12541
12542
#ifdef TO_BE_DONE_LATER_IN_4_1
12543
  /*
12544
    To use start_bulk_insert() (which is new in 4.1) we need to find
12545
    all places where a corresponding end_bulk_insert() should be put.
12546
  */
12547
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12548
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
12549
#else
12550
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12551
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12552
#endif
12553
12554
  /*
12555
    copy all old rows from heap table to MyISAM table
12556
    This is the only code that uses record[1] to read/write but this
12557
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12558
  */
12559
  while (!table->file->rnd_next(new_table.record[1]))
12560
  {
12561
    write_err= new_table.file->ha_write_row(new_table.record[1]);
12562
    DBUG_EXECUTE_IF("raise_error", write_err= HA_ERR_FOUND_DUPP_KEY ;);
12563
    if (write_err)
12564
      goto err;
12565
  }
12566
  /* copy row that filled HEAP table */
12567
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
12568
  {
12569
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12570
	!ignore_last_dupp_key_error)
12571
      goto err;
12572
  }
12573
12574
  /* remove heap table and change to use myisam table */
12575
  (void) table->file->ha_rnd_end();
12576
  (void) table->file->close();                  // This deletes the table !
12577
  delete table->file;
12578
  table->file=0;
12579
  plugin_unlock(0, table->s->db_plugin);
12580
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12581
  new_table.s= table->s;                       // Keep old share
12582
  *table= new_table;
12583
  *table->s= share;
12584
  
12585
  table->file->change_table_ptr(table, table->s);
12586
  table->use_all_columns();
12587
  if (save_proc_info)
12588
  {
12589
    const char *new_proc_info=
12590
      (!strcmp(save_proc_info,"Copying to tmp table") ?
12591
      "Copying to tmp table on disk" : save_proc_info);
12592
    thd_proc_info(thd, new_proc_info);
12593
  }
12594
  DBUG_RETURN(0);
12595
12596
 err:
12597
  DBUG_PRINT("error",("Got error: %d",write_err));
12598
  table->file->print_error(write_err, MYF(0));
12599
  (void) table->file->ha_rnd_end();
12600
  (void) new_table.file->close();
12601
 err1:
12602
  new_table.file->ha_delete_table(new_table.s->table_name.str);
12603
 err2:
12604
  delete new_table.file;
12605
  thd_proc_info(thd, save_proc_info);
12606
  table->mem_root= new_table.mem_root;
12607
  DBUG_RETURN(1);
12608
}
12609
12610
12611
/**
12612
  @details
12613
  Rows produced by a join sweep may end up in a temporary table or be sent
12614
  to a client. Setup the function of the nested loop join algorithm which
12615
  handles final fully constructed and matched records.
12616
12617
  @param join   join to setup the function for.
12618
12619
  @return
12620
    end_select function to use. This function can't fail.
12621
*/
12622
12623
Next_select_func setup_end_select_func(JOIN *join)
12624
{
12625
  TABLE *table= join->tmp_table;
12626
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
12627
  Next_select_func end_select;
12628
12629
  /* Set up select_end */
12630
  if (table)
12631
  {
12632
    if (table->group && tmp_tbl->sum_func_count && 
12633
        !tmp_tbl->precomputed_group_by)
12634
    {
12635
      if (table->s->keys)
12636
      {
12637
	DBUG_PRINT("info",("Using end_update"));
12638
	end_select=end_update;
12639
      }
12640
      else
12641
      {
12642
	DBUG_PRINT("info",("Using end_unique_update"));
12643
	end_select=end_unique_update;
12644
      }
12645
    }
12646
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
12647
    {
12648
      DBUG_PRINT("info",("Using end_write_group"));
12649
      end_select=end_write_group;
12650
    }
12651
    else
12652
    {
12653
      DBUG_PRINT("info",("Using end_write"));
12654
      end_select=end_write;
12655
      if (tmp_tbl->precomputed_group_by)
12656
      {
12657
        /*
12658
          A preceding call to create_tmp_table in the case when loose
12659
          index scan is used guarantees that
12660
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
12661
          by functions. It is OK here to use memcpy since we copy
12662
          Item_sum pointers into an array of Item pointers.
12663
        */
12664
        memcpy(tmp_tbl->items_to_copy + tmp_tbl->func_count,
12665
               join->sum_funcs,
12666
               sizeof(Item*)*tmp_tbl->sum_func_count);
12667
        tmp_tbl->items_to_copy[tmp_tbl->func_count+tmp_tbl->sum_func_count]= 0;
12668
      }
12669
    }
12670
  }
12671
  else
12672
  {
12673
    if ((join->sort_and_group) &&
12674
        !tmp_tbl->precomputed_group_by)
12675
      end_select= end_send_group;
12676
    else
12677
      end_select= end_send;
12678
  }
12679
  return end_select;
12680
}
12681
12682
12683
/**
12684
  Make a join of all tables and write it on socket or to table.
12685
12686
  @retval
12687
    0  if ok
12688
  @retval
12689
    1  if error is sent
12690
  @retval
12691
    -1  if error should be sent
12692
*/
12693
12694
static int
12695
do_select(JOIN *join,List<Item> *fields,TABLE *table)
12696
{
12697
  int rc= 0;
12698
  enum_nested_loop_state error= NESTED_LOOP_OK;
12699
  JOIN_TAB *join_tab= NULL;
12700
  DBUG_ENTER("do_select");
12701
  
12702
  join->tmp_table= table;			/* Save for easy recursion */
12703
  join->fields= fields;
12704
12705
  if (table)
12706
  {
12707
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
12708
    empty_record(table);
12709
    if (table->group && join->tmp_table_param.sum_func_count &&
12710
        table->s->keys && !table->file->inited)
12711
      table->file->ha_index_init(0, 0);
12712
  }
12713
  /* Set up select_end */
12714
  Next_select_func end_select= setup_end_select_func(join);
12715
  if (join->tables)
12716
  {
12717
    join->join_tab[join->tables-1].next_select= end_select;
12718
12719
    join_tab=join->join_tab+join->const_tables;
12720
  }
12721
  join->send_records=0;
12722
  if (join->tables == join->const_tables)
12723
  {
12724
    /*
12725
      HAVING will be checked after processing aggregate functions,
12726
      But WHERE should checkd here (we alredy have read tables)
12727
    */
12728
    if (!join->conds || join->conds->val_int())
12729
    {
12730
      error= (*end_select)(join, 0, 0);
12731
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
12732
	error= (*end_select)(join, 0, 1);
12733
12734
      /*
12735
        If we don't go through evaluate_join_record(), do the counting
12736
        here.  join->send_records is increased on success in end_send(),
12737
        so we don't touch it here.
12738
      */
12739
      join->examined_rows++;
12740
      join->thd->row_count++;
12741
      DBUG_ASSERT(join->examined_rows <= 1);
12742
    }
12743
    else if (join->send_row_on_empty_set())
12744
    {
12745
      List<Item> *columns_list= fields;
12746
      rc= join->result->send_data(*columns_list);
12747
    }
12748
  }
12749
  else
12750
  {
12751
    DBUG_ASSERT(join->tables);
12752
    error= sub_select(join,join_tab,0);
12753
    if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
12754
      error= sub_select(join,join_tab,1);
12755
    if (error == NESTED_LOOP_QUERY_LIMIT)
12756
      error= NESTED_LOOP_OK;                    /* select_limit used */
12757
  }
12758
  if (error == NESTED_LOOP_NO_MORE_ROWS)
12759
    error= NESTED_LOOP_OK;
12760
12761
  if (error == NESTED_LOOP_OK)
12762
  {
12763
    /*
12764
      Sic: this branch works even if rc != 0, e.g. when
12765
      send_data above returns an error.
12766
    */
12767
    if (!table)					// If sending data to client
12768
    {
12769
      /*
12770
	The following will unlock all cursors if the command wasn't an
12771
	update command
12772
      */
12773
      join->join_free();			// Unlock all cursors
12774
      if (join->result->send_eof())
12775
	rc= 1;                                  // Don't send error
12776
    }
12777
    DBUG_PRINT("info",("%ld records output", (long) join->send_records));
12778
  }
12779
  else
12780
    rc= -1;
12781
  if (table)
12782
  {
12783
    int tmp, new_errno= 0;
12784
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
12785
    {
12786
      DBUG_PRINT("error",("extra(HA_EXTRA_NO_CACHE) failed"));
12787
      new_errno= tmp;
12788
    }
12789
    if ((tmp=table->file->ha_index_or_rnd_end()))
12790
    {
12791
      DBUG_PRINT("error",("ha_index_or_rnd_end() failed"));
12792
      new_errno= tmp;
12793
    }
12794
    if (new_errno)
12795
      table->file->print_error(new_errno,MYF(0));
12796
  }
12797
#ifndef DBUG_OFF
12798
  if (rc)
12799
  {
12800
    DBUG_PRINT("error",("Error: do_select() failed"));
12801
  }
12802
#endif
12803
  DBUG_RETURN(join->thd->is_error() ? -1 : rc);
12804
}
12805
12806
12807
enum_nested_loop_state
12808
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
12809
{
12810
  enum_nested_loop_state rc;
12811
12812
  if (end_of_records)
12813
  {
12814
    rc= flush_cached_records(join,join_tab,FALSE);
12815
    if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
12816
      rc= sub_select(join,join_tab,end_of_records);
12817
    return rc;
12818
  }
12819
  if (join->thd->killed)		// If aborted by user
12820
  {
12821
    join->thd->send_kill_message();
12822
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
12823
  }
12824
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
12825
  {
12826
    if (!store_record_in_cache(&join_tab->cache))
12827
      return NESTED_LOOP_OK;                     // There is more room in cache
12828
    return flush_cached_records(join,join_tab,FALSE);
12829
  }
12830
  rc= flush_cached_records(join, join_tab, TRUE);
12831
  if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
12832
    rc= sub_select(join, join_tab, end_of_records);
12833
  return rc;
12834
}
12835
12836
/**
12837
  Retrieve records ends with a given beginning from the result of a join.
12838
12839
    For a given partial join record consisting of records from the tables 
12840
    preceding the table join_tab in the execution plan, the function
12841
    retrieves all matching full records from the result set and
12842
    send them to the result set stream. 
12843
12844
  @note
12845
    The function effectively implements the  final (n-k) nested loops
12846
    of nested loops join algorithm, where k is the ordinal number of
12847
    the join_tab table and n is the total number of tables in the join query.
12848
    It performs nested loops joins with all conjunctive predicates from
12849
    the where condition pushed as low to the tables as possible.
12850
    E.g. for the query
12851
    @code
12852
      SELECT * FROM t1,t2,t3
12853
      WHERE t1.a=t2.a AND t2.b=t3.b AND t1.a BETWEEN 5 AND 9
12854
    @endcode
12855
    the predicate (t1.a BETWEEN 5 AND 9) will be pushed to table t1,
12856
    given the selected plan prescribes to nest retrievals of the
12857
    joined tables in the following order: t1,t2,t3.
12858
    A pushed down predicate are attached to the table which it pushed to,
12859
    at the field join_tab->select_cond.
12860
    When executing a nested loop of level k the function runs through
12861
    the rows of 'join_tab' and for each row checks the pushed condition
12862
    attached to the table.
12863
    If it is false the function moves to the next row of the
12864
    table. If the condition is true the function recursively executes (n-k-1)
12865
    remaining embedded nested loops.
12866
    The situation becomes more complicated if outer joins are involved in
12867
    the execution plan. In this case the pushed down predicates can be
12868
    checked only at certain conditions.
12869
    Suppose for the query
12870
    @code
12871
      SELECT * FROM t1 LEFT JOIN (t2,t3) ON t3.a=t1.a
12872
      WHERE t1>2 AND (t2.b>5 OR t2.b IS NULL)
12873
    @endcode
12874
    the optimizer has chosen a plan with the table order t1,t2,t3.
12875
    The predicate P1=t1>2 will be pushed down to the table t1, while the
12876
    predicate P2=(t2.b>5 OR t2.b IS NULL) will be attached to the table
12877
    t2. But the second predicate can not be unconditionally tested right
12878
    after a row from t2 has been read. This can be done only after the
12879
    first row with t3.a=t1.a has been encountered.
12880
    Thus, the second predicate P2 is supplied with a guarded value that are
12881
    stored in the field 'found' of the first inner table for the outer join
12882
    (table t2). When the first row with t3.a=t1.a for the  current row 
12883
    of table t1  appears, the value becomes true. For now on the predicate
12884
    is evaluated immediately after the row of table t2 has been read.
12885
    When the first row with t3.a=t1.a has been encountered all
12886
    conditions attached to the inner tables t2,t3 must be evaluated.
12887
    Only when all of them are true the row is sent to the output stream.
12888
    If not, the function returns to the lowest nest level that has a false
12889
    attached condition.
12890
    The predicates from on expressions are also pushed down. If in the 
12891
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
12892
    then t1.a=t2.a would be pushed down to table t2, and without any
12893
    guard.
12894
    If after the run through all rows of table t2, the first inner table
12895
    for the outer join operation, it turns out that no matches are
12896
    found for the current row of t1, then current row from table t1
12897
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
12898
    are checked for the composed row almost in the same way as it had
12899
    been done for the first row with a match. The only difference is
12900
    the predicates from on expressions are not checked. 
12901
12902
  @par
12903
  @b IMPLEMENTATION
12904
  @par
12905
    The function forms output rows for a current partial join of k
12906
    tables tables recursively.
12907
    For each partial join record ending with a certain row from
12908
    join_tab it calls sub_select that builds all possible matching
12909
    tails from the result set.
12910
    To be able  check predicates conditionally items of the class
12911
    Item_func_trig_cond are employed.
12912
    An object of  this class is constructed from an item of class COND
12913
    and a pointer to a guarding boolean variable.
12914
    When the value of the guard variable is true the value of the object
12915
    is the same as the value of the predicate, otherwise it's just returns
12916
    true. 
12917
    To carry out a return to a nested loop level of join table t the pointer 
12918
    to t is remembered in the field 'return_tab' of the join structure.
12919
    Consider the following query:
12920
    @code
12921
        SELECT * FROM t1,
12922
                      LEFT JOIN
12923
                      (t2, t3 LEFT JOIN (t4,t5) ON t5.a=t3.a)
12924
                      ON t4.a=t2.a
12925
           WHERE (t2.b=5 OR t2.b IS NULL) AND (t4.b=2 OR t4.b IS NULL)
12926
    @endcode
12927
    Suppose the chosen execution plan dictates the order t1,t2,t3,t4,t5
12928
    and suppose for a given joined rows from tables t1,t2,t3 there are
12929
    no rows in the result set yet.
12930
    When first row from t5 that satisfies the on condition
12931
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
12932
    becomes 'activated', as well the predicate t4.a=t2.a. But
12933
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
12934
    t4.a=t2.a becomes true. 
12935
    In order not to re-evaluate the predicates that were already evaluated
12936
    as attached pushed down predicates, a pointer to the the first
12937
    most inner unmatched table is maintained in join_tab->first_unmatched.
12938
    Thus, when the first row from t5 with t5.a=t3.a is found
12939
    this pointer for t5 is changed from t4 to t2.             
12940
12941
    @par
12942
    @b STRUCTURE @b NOTES
12943
    @par
12944
    join_tab->first_unmatched points always backwards to the first inner
12945
    table of the embedding nested join, if any.
12946
12947
  @param join      pointer to the structure providing all context info for
12948
                   the query
12949
  @param join_tab  the first next table of the execution plan to be retrieved
12950
  @param end_records  true when we need to perform final steps of retrival   
12951
12952
  @return
12953
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
12954
*/
12955
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
12956
12957
enum_nested_loop_state
12958
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
12959
{
12960
  join_tab->table->null_row=0;
12961
  if (end_of_records)
12962
    return (*join_tab->next_select)(join,join_tab+1,end_of_records);
12963
12964
  int error;
12965
  enum_nested_loop_state rc;
12966
  READ_RECORD *info= &join_tab->read_record;
12967
12968
  if (join_tab->flush_weedout_table)
12969
  {
12970
    do_sj_reset(join_tab->flush_weedout_table);
12971
  }
12972
12973
  if (join->resume_nested_loop)
12974
  {
12975
    /* If not the last table, plunge down the nested loop */
12976
    if (join_tab < join->join_tab + join->tables - 1)
12977
      rc= (*join_tab->next_select)(join, join_tab + 1, 0);
12978
    else
12979
    {
12980
      join->resume_nested_loop= FALSE;
12981
      rc= NESTED_LOOP_OK;
12982
    }
12983
  }
12984
  else
12985
  {
12986
    join->return_tab= join_tab;
12987
12988
    if (join_tab->last_inner)
12989
    {
12990
      /* join_tab is the first inner table for an outer join operation. */
12991
12992
      /* Set initial state of guard variables for this table.*/
12993
      join_tab->found=0;
12994
      join_tab->not_null_compl= 1;
12995
12996
      /* Set first_unmatched for the last inner table of this group */
12997
      join_tab->last_inner->first_unmatched= join_tab;
12998
    }
12999
    join->thd->row_count= 0;
13000
13001
    error= (*join_tab->read_first_record)(join_tab);
13002
    rc= evaluate_join_record(join, join_tab, error);
13003
  }
13004
  
13005
  /* 
13006
    Note: psergey has added the 2nd part of the following condition; the 
13007
    change should probably be made in 5.1, too.
13008
  */
13009
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
13010
  {
13011
    error= info->read_record(info);
13012
    rc= evaluate_join_record(join, join_tab, error);
13013
  }
13014
13015
  if (rc == NESTED_LOOP_NO_MORE_ROWS &&
13016
      join_tab->last_inner && !join_tab->found)
13017
    rc= evaluate_null_complemented_join_record(join, join_tab);
13018
13019
  if (rc == NESTED_LOOP_NO_MORE_ROWS)
13020
    rc= NESTED_LOOP_OK;
13021
  return rc;
13022
}
13023
13024
13025
13026
13027
/*
13028
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
13029
13030
  SYNPOSIS
13031
    do_sj_dups_weedout()
13032
      
13033
  RETURN
13034
    -1  Error
13035
    1   The row combination is a duplicate (discard it)
13036
    0   The row combination is not a duplicate (continue)
13037
*/
13038
13039
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
13040
{
13041
  int error;
13042
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
13043
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
13044
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
13045
  uchar *nulls_ptr= ptr;
13046
  
13047
  /* Put the the rowids tuple into table->record[0]: */
13048
13049
  // 1. Store the length 
13050
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
13051
  {
13052
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
13053
    ptr++;
13054
  }
13055
  else
13056
  {
13057
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
13058
    ptr += 2;
13059
  }
13060
13061
  // 2. Zero the null bytes 
13062
  if (sjtbl->null_bytes)
13063
  {
13064
    bzero(ptr, sjtbl->null_bytes);
13065
    ptr += sjtbl->null_bytes; 
13066
  }
13067
13068
  // 3. Put the rowids
13069
  for (uint i=0; tab != tab_end; tab++, i++)
13070
  {
13071
    handler *h= tab->join_tab->table->file;
13072
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
13073
    {
13074
      /* It's a NULL-complemented row */
13075
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
13076
      bzero(ptr + tab->rowid_offset, h->ref_length);
13077
    }
13078
    else
13079
    {
13080
      /* Copy the rowid value */
13081
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
13082
        h->position(tab->join_tab->table->record[0]);
13083
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
13084
    }
13085
  }
13086
13087
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
13088
  if (error)
13089
  {
13090
    /* create_myisam_from_heap will generate error if needed */
13091
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
13092
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
13093
                                &sjtbl->recinfo, error, 1))
13094
      return -1;
13095
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
13096
    return 1;
13097
  }
13098
  return 0;
13099
}
13100
13101
13102
/*
13103
  SemiJoinDuplicateElimination: Reset the temporary table
13104
*/
13105
13106
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
13107
{
13108
  if (sj_tbl->tmp_table)
13109
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
13110
  return 0;
13111
}
13112
13113
/*
13114
  Process one record of the nested loop join.
13115
13116
    This function will evaluate parts of WHERE/ON clauses that are
13117
    applicable to the partial record on hand and in case of success
13118
    submit this record to the next level of the nested loop.
13119
*/
13120
13121
static enum_nested_loop_state
13122
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
13123
                     int error)
13124
{
13125
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
13126
  ha_rows found_records=join->found_records;
13127
  COND *select_cond= join_tab->select_cond;
13128
13129
  if (error > 0 || (join->thd->is_error()))     // Fatal error
13130
    return NESTED_LOOP_ERROR;
13131
  if (error < 0)
13132
    return NESTED_LOOP_NO_MORE_ROWS;
13133
  if (join->thd->killed)			// Aborted by user
13134
  {
13135
    join->thd->send_kill_message();
13136
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
13137
  }
13138
  DBUG_PRINT("info", ("select cond 0x%lx", (ulong)select_cond));
13139
  if (!select_cond || select_cond->val_int())
13140
  {
13141
    /*
13142
      There is no select condition or the attached pushed down
13143
      condition is true => a match is found.
13144
    */
13145
    bool found= 1;
13146
    while (join_tab->first_unmatched && found)
13147
    {
13148
      /*
13149
        The while condition is always false if join_tab is not
13150
        the last inner join table of an outer join operation.
13151
      */
13152
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13153
      /*
13154
        Mark that a match for current outer table is found.
13155
        This activates push down conditional predicates attached
13156
        to the all inner tables of the outer join.
13157
      */
13158
      first_unmatched->found= 1;
13159
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13160
      {
13161
        if (tab->table->reginfo.not_exists_optimize)
13162
          return NESTED_LOOP_NO_MORE_ROWS;
13163
        /* Check all predicates that has just been activated. */
13164
        /*
13165
          Actually all predicates non-guarded by first_unmatched->found
13166
          will be re-evaluated again. It could be fixed, but, probably,
13167
          it's not worth doing now.
13168
        */
13169
        if (tab->select_cond && !tab->select_cond->val_int())
13170
        {
13171
          /* The condition attached to table tab is false */
13172
          if (tab == join_tab)
13173
            found= 0;
13174
          else
13175
          {
13176
            /*
13177
              Set a return point if rejected predicate is attached
13178
              not to the last table of the current nest level.
13179
            */
13180
            join->return_tab= tab;
13181
            return NESTED_LOOP_OK;
13182
          }
13183
        }
13184
      }
13185
      /*
13186
        Check whether join_tab is not the last inner table
13187
        for another embedding outer join.
13188
      */
13189
      if ((first_unmatched= first_unmatched->first_upper) &&
13190
          first_unmatched->last_inner != join_tab)
13191
        first_unmatched= 0;
13192
      join_tab->first_unmatched= first_unmatched;
13193
    }
13194
13195
    JOIN_TAB *return_tab= join->return_tab;
13196
    join_tab->found_match= TRUE;
13197
    if (join_tab->check_weed_out_table)
13198
    {
13199
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
13200
      if (res == -1)
13201
        return NESTED_LOOP_ERROR;
13202
      if (res == 1)
13203
        return NESTED_LOOP_OK;
13204
    }
13205
    else if (join_tab->do_firstmatch)
13206
    {
13207
      /* 
13208
        We should return to the join_tab->do_firstmatch after we have 
13209
        enumerated all the suffixes for current prefix row combination
13210
      */
13211
      return_tab= join_tab->do_firstmatch;
13212
    }
13213
13214
    /*
13215
      It was not just a return to lower loop level when one
13216
      of the newly activated predicates is evaluated as false
13217
      (See above join->return_tab= tab).
13218
    */
13219
    join->examined_rows++;
13220
    join->thd->row_count++;
13221
    DBUG_PRINT("counts", ("join->examined_rows++: %lu",
13222
                          (ulong) join->examined_rows));
13223
13224
    if (found)
13225
    {
13226
      enum enum_nested_loop_state rc;
13227
      /* A match from join_tab is found for the current partial join. */
13228
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
13229
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13230
        return rc;
13231
      if (return_tab < join->return_tab)
13232
        join->return_tab= return_tab;
13233
13234
      if (join->return_tab < join_tab)
13235
        return NESTED_LOOP_OK;
13236
      /*
13237
        Test if this was a SELECT DISTINCT query on a table that
13238
        was not in the field list;  In this case we can abort if
13239
        we found a row, as no new rows can be added to the result.
13240
      */
13241
      if (not_used_in_distinct && found_records != join->found_records)
13242
        return NESTED_LOOP_NO_MORE_ROWS;
13243
    }
13244
    else
13245
      join_tab->read_record.file->unlock_row();
13246
  }
13247
  else
13248
  {
13249
    /*
13250
      The condition pushed down to the table join_tab rejects all rows
13251
      with the beginning coinciding with the current partial join.
13252
    */
13253
    join->examined_rows++;
13254
    join->thd->row_count++;
13255
    join_tab->read_record.file->unlock_row();
13256
  }
13257
  return NESTED_LOOP_OK;
13258
}
13259
13260
13261
/**
13262
13263
  @details
13264
    Construct a NULL complimented partial join record and feed it to the next
13265
    level of the nested loop. This function is used in case we have
13266
    an OUTER join and no matching record was found.
13267
*/
13268
13269
static enum_nested_loop_state
13270
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13271
{
13272
  /*
13273
    The table join_tab is the first inner table of a outer join operation
13274
    and no matches has been found for the current outer row.
13275
  */
13276
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
13277
  /* Cache variables for faster loop */
13278
  COND *select_cond;
13279
  for ( ; join_tab <= last_inner_tab ; join_tab++)
13280
  {
13281
    /* Change the the values of guard predicate variables. */
13282
    join_tab->found= 1;
13283
    join_tab->not_null_compl= 0;
13284
    /* The outer row is complemented by nulls for each inner tables */
13285
    restore_record(join_tab->table,s->default_values);  // Make empty record
13286
    mark_as_null_row(join_tab->table);       // For group by without error
13287
    select_cond= join_tab->select_cond;
13288
    /* Check all attached conditions for inner table rows. */
13289
    if (select_cond && !select_cond->val_int())
13290
      return NESTED_LOOP_OK;
13291
  }
13292
  join_tab--;
13293
  /*
13294
    The row complemented by nulls might be the first row
13295
    of embedding outer joins.
13296
    If so, perform the same actions as in the code
13297
    for the first regular outer join row above.
13298
  */
13299
  for ( ; ; )
13300
  {
13301
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13302
    if ((first_unmatched= first_unmatched->first_upper) &&
13303
        first_unmatched->last_inner != join_tab)
13304
      first_unmatched= 0;
13305
    join_tab->first_unmatched= first_unmatched;
13306
    if (!first_unmatched)
13307
      break;
13308
    first_unmatched->found= 1;
13309
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13310
    {
13311
      if (tab->select_cond && !tab->select_cond->val_int())
13312
      {
13313
        join->return_tab= tab;
13314
        return NESTED_LOOP_OK;
13315
      }
13316
    }
13317
  }
13318
  /*
13319
    The row complemented by nulls satisfies all conditions
13320
    attached to inner tables.
13321
    Send the row complemented by nulls to be joined with the
13322
    remaining tables.
13323
  */
13324
  return (*join_tab->next_select)(join, join_tab+1, 0);
13325
}
13326
13327
13328
static enum_nested_loop_state
13329
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13330
{
13331
  enum_nested_loop_state rc= NESTED_LOOP_OK;
13332
  int error;
13333
  READ_RECORD *info;
13334
13335
  join_tab->table->null_row= 0;
13336
  if (!join_tab->cache.records)
13337
    return NESTED_LOOP_OK;                      /* Nothing to do */
13338
  if (skip_last)
13339
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
13340
  if (join_tab->use_quick == 2)
13341
  {
13342
    if (join_tab->select->quick)
13343
    {					/* Used quick select last. reset it */
13344
      delete join_tab->select->quick;
13345
      join_tab->select->quick=0;
13346
    }
13347
  }
13348
 /* read through all records */
13349
  if ((error=join_init_read_record(join_tab)))
13350
  {
13351
    reset_cache_write(&join_tab->cache);
13352
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13353
  }
13354
13355
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13356
  {
13357
    tmp->status=tmp->table->status;
13358
    tmp->table->status=0;
13359
  }
13360
13361
  info= &join_tab->read_record;
13362
  do
13363
  {
13364
    if (join->thd->killed)
13365
    {
13366
      join->thd->send_kill_message();
13367
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13368
    }
13369
    SQL_SELECT *select=join_tab->select;
13370
    if (rc == NESTED_LOOP_OK &&
13371
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13372
    {
13373
      uint i;
13374
      reset_cache_read(&join_tab->cache);
13375
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13376
      {
13377
	read_cached_record(join_tab);
13378
	if (!select || !select->skip_record())
13379
        {
13380
          int res= 0;
13381
          if (!join_tab->check_weed_out_table || 
13382
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13383
          {
13384
            rc= (join_tab->next_select)(join,join_tab+1,0);
13385
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13386
            {
13387
              reset_cache_write(&join_tab->cache);
13388
              return rc;
13389
            }
13390
          }
13391
          if (res == -1)
13392
            return NESTED_LOOP_ERROR;
13393
        }
13394
      }
13395
    }
13396
  } while (!(error=info->read_record(info)));
13397
13398
  if (skip_last)
13399
    read_cached_record(join_tab);		// Restore current record
13400
  reset_cache_write(&join_tab->cache);
13401
  if (error > 0)				// Fatal error
13402
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
13403
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13404
    tmp2->table->status=tmp2->status;
13405
  return NESTED_LOOP_OK;
13406
}
13407
13408
13409
/*****************************************************************************
13410
  The different ways to read a record
13411
  Returns -1 if row was not found, 0 if row was found and 1 on errors
13412
*****************************************************************************/
13413
13414
/** Help function when we get some an error from the table handler. */
13415
13416
int report_error(TABLE *table, int error)
13417
{
13418
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13419
  {
13420
    table->status= STATUS_GARBAGE;
13421
    return -1;					// key not found; ok
13422
  }
13423
  /*
13424
    Locking reads can legally return also these errors, do not
13425
    print them to the .err log
13426
  */
13427
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13428
    sql_print_error("Got error %d when reading table '%s'",
13429
		    error, table->s->path.str);
13430
  table->file->print_error(error,MYF(0));
13431
  return 1;
13432
}
13433
13434
13435
int safe_index_read(JOIN_TAB *tab)
13436
{
13437
  int error;
13438
  TABLE *table= tab->table;
13439
  if ((error=table->file->index_read_map(table->record[0],
13440
                                         tab->ref.key_buff,
13441
                                         make_prev_keypart_map(tab->ref.key_parts),
13442
                                         HA_READ_KEY_EXACT)))
13443
    return report_error(table, error);
13444
  return 0;
13445
}
13446
13447
13448
static int
13449
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
13450
{
13451
  int error;
13452
  DBUG_ENTER("join_read_const_table");
13453
  TABLE *table=tab->table;
13454
  table->const_table=1;
13455
  table->null_row=0;
13456
  table->status=STATUS_NO_RECORD;
13457
  
13458
  if (tab->type == JT_SYSTEM)
13459
  {
13460
    if ((error=join_read_system(tab)))
13461
    {						// Info for DESCRIBE
13462
      tab->info="const row not found";
13463
      /* Mark for EXPLAIN that the row was not found */
13464
      pos->records_read=0.0;
13465
      pos->ref_depend_map= 0;
13466
      if (!table->maybe_null || error > 0)
13467
	DBUG_RETURN(error);
13468
    }
13469
  }
13470
  else
13471
  {
13472
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13473
	!table->no_keyread &&
13474
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
13475
    {
13476
      table->key_read=1;
13477
      table->file->extra(HA_EXTRA_KEYREAD);
13478
      tab->index= tab->ref.key;
13479
    }
13480
    error=join_read_const(tab);
13481
    if (table->key_read)
13482
    {
13483
      table->key_read=0;
13484
      table->file->extra(HA_EXTRA_NO_KEYREAD);
13485
    }
13486
    if (error)
13487
    {
13488
      tab->info="unique row not found";
13489
      /* Mark for EXPLAIN that the row was not found */
13490
      pos->records_read=0.0;
13491
      pos->ref_depend_map= 0;
13492
      if (!table->maybe_null || error > 0)
13493
	DBUG_RETURN(error);
13494
    }
13495
  }
13496
  if (*tab->on_expr_ref && !table->null_row)
13497
  {
13498
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13499
      mark_as_null_row(table);  
13500
  }
13501
  if (!table->null_row)
13502
    table->maybe_null=0;
13503
13504
  /* Check appearance of new constant items in Item_equal objects */
13505
  JOIN *join= tab->join;
13506
  if (join->conds)
13507
    update_const_equal_items(join->conds, tab);
13508
  TABLE_LIST *tbl;
13509
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
13510
  {
13511
    TABLE_LIST *embedded;
13512
    TABLE_LIST *embedding= tbl;
13513
    do
13514
    {
13515
      embedded= embedding;
13516
      if (embedded->on_expr)
13517
         update_const_equal_items(embedded->on_expr, tab);
13518
      embedding= embedded->embedding;
13519
    }
13520
    while (embedding &&
13521
           embedding->nested_join->join_list.head() == embedded);
13522
  }
13523
13524
  DBUG_RETURN(0);
13525
}
13526
13527
13528
static int
13529
join_read_system(JOIN_TAB *tab)
13530
{
13531
  TABLE *table= tab->table;
13532
  int error;
13533
  if (table->status & STATUS_GARBAGE)		// If first read
13534
  {
13535
    if ((error=table->file->read_first_row(table->record[0],
13536
					   table->s->primary_key)))
13537
    {
13538
      if (error != HA_ERR_END_OF_FILE)
13539
	return report_error(table, error);
13540
      mark_as_null_row(tab->table);
13541
      empty_record(table);			// Make empty record
13542
      return -1;
13543
    }
13544
    store_record(table,record[1]);
13545
  }
13546
  else if (!table->status)			// Only happens with left join
13547
    restore_record(table,record[1]);			// restore old record
13548
  table->null_row=0;
13549
  return table->status ? -1 : 0;
13550
}
13551
13552
13553
/**
13554
  Read a (constant) table when there is at most one matching row.
13555
13556
  @param tab			Table to read
13557
13558
  @retval
13559
    0	Row was found
13560
  @retval
13561
    -1   Row was not found
13562
  @retval
13563
    1   Got an error (other than row not found) during read
13564
*/
13565
13566
static int
13567
join_read_const(JOIN_TAB *tab)
13568
{
13569
  int error;
13570
  TABLE *table= tab->table;
13571
  if (table->status & STATUS_GARBAGE)		// If first read
13572
  {
13573
    table->status= 0;
13574
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13575
      error=HA_ERR_KEY_NOT_FOUND;
13576
    else
13577
    {
13578
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
13579
                                            (uchar*) tab->ref.key_buff,
13580
                                            make_prev_keypart_map(tab->ref.key_parts),
13581
                                            HA_READ_KEY_EXACT);
13582
    }
13583
    if (error)
13584
    {
13585
      table->status= STATUS_NOT_FOUND;
13586
      mark_as_null_row(tab->table);
13587
      empty_record(table);
13588
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13589
	return report_error(table, error);
13590
      return -1;
13591
    }
13592
    store_record(table,record[1]);
13593
  }
13594
  else if (!(table->status & ~STATUS_NULL_ROW))	// Only happens with left join
13595
  {
13596
    table->status=0;
13597
    restore_record(table,record[1]);			// restore old record
13598
  }
13599
  table->null_row=0;
13600
  return table->status ? -1 : 0;
13601
}
13602
13603
13604
/*
13605
  eq_ref access method implementation: "read_first" function
13606
13607
  SYNOPSIS
13608
    join_read_key()
13609
      tab  JOIN_TAB of the accessed table
13610
13611
  DESCRIPTION
13612
    This is "read_fist" function for the "ref" access method. The difference
13613
    from "ref" is that it has a one-element "cache" (see cmp_buffer_with_ref)
13614
13615
  RETURN
13616
    0  - Ok
13617
   -1  - Row not found 
13618
    1  - Error
13619
*/
13620
13621
static int
13622
join_read_key(JOIN_TAB *tab)
13623
{
13624
  int error;
13625
  TABLE *table= tab->table;
13626
13627
  if (!table->file->inited)
13628
  {
13629
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13630
  }
13631
13632
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
13633
  if (cmp_buffer_with_ref(tab) ||
13634
      (table->status & (STATUS_GARBAGE | STATUS_NO_PARENT | STATUS_NULL_ROW)))
13635
  {
13636
    if (tab->ref.key_err)
13637
    {
13638
      table->status=STATUS_NOT_FOUND;
13639
      return -1;
13640
    }
13641
    error=table->file->index_read_map(table->record[0],
13642
                                      tab->ref.key_buff,
13643
                                      make_prev_keypart_map(tab->ref.key_parts),
13644
                                      HA_READ_KEY_EXACT);
13645
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13646
      return report_error(table, error);
13647
  }
13648
  table->null_row=0;
13649
  return table->status ? -1 : 0;
13650
}
13651
13652
13653
/*
13654
  ref access method implementation: "read_first" function
13655
13656
  SYNOPSIS
13657
    join_read_always_key()
13658
      tab  JOIN_TAB of the accessed table
13659
13660
  DESCRIPTION
13661
    This is "read_fist" function for the "ref" access method.
13662
   
13663
    The functon must leave the index initialized when it returns.
13664
    ref_or_null access implementation depends on that.
13665
13666
  RETURN
13667
    0  - Ok
13668
   -1  - Row not found 
13669
    1  - Error
13670
*/
13671
13672
static int
13673
join_read_always_key(JOIN_TAB *tab)
13674
{
13675
  int error;
13676
  TABLE *table= tab->table;
13677
13678
  /* Initialize the index first */
13679
  if (!table->file->inited)
13680
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13681
 
13682
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
13683
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
13684
  {
13685
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
13686
        return -1;
13687
  }
13688
13689
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13690
    return -1;
13691
  if ((error=table->file->index_read_map(table->record[0],
13692
                                         tab->ref.key_buff,
13693
                                         make_prev_keypart_map(tab->ref.key_parts),
13694
                                         HA_READ_KEY_EXACT)))
13695
  {
13696
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13697
      return report_error(table, error);
13698
    return -1; /* purecov: inspected */
13699
  }
13700
  return 0;
13701
}
13702
13703
13704
/**
13705
  This function is used when optimizing away ORDER BY in 
13706
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
13707
*/
13708
  
13709
static int
13710
join_read_last_key(JOIN_TAB *tab)
13711
{
13712
  int error;
13713
  TABLE *table= tab->table;
13714
13715
  if (!table->file->inited)
13716
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13717
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13718
    return -1;
13719
  if ((error=table->file->index_read_last_map(table->record[0],
13720
                                              tab->ref.key_buff,
13721
                                              make_prev_keypart_map(tab->ref.key_parts))))
13722
  {
13723
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13724
      return report_error(table, error);
13725
    return -1; /* purecov: inspected */
13726
  }
13727
  return 0;
13728
}
13729
13730
13731
	/* ARGSUSED */
13732
static int
13733
join_no_more_records(READ_RECORD *info __attribute__((unused)))
13734
{
13735
  return -1;
13736
}
13737
13738
static int
13739
join_read_next_same_diff(READ_RECORD *info)
13740
{
13741
  TABLE *table= info->table;
13742
  JOIN_TAB *tab=table->reginfo.join_tab;
13743
  if (tab->insideout_match_tab->found_match)
13744
  {
13745
    KEY *key= tab->table->key_info + tab->index;
13746
    do 
13747
    {
13748
      int error;
13749
      /* Save index tuple from record to the buffer */
13750
      key_copy(tab->insideout_buf, info->record, key, 0);
13751
13752
      if ((error=table->file->index_next_same(table->record[0],
13753
                                              tab->ref.key_buff,
13754
                                              tab->ref.key_length)))
13755
      {
13756
        if (error != HA_ERR_END_OF_FILE)
13757
          return report_error(table, error);
13758
        table->status= STATUS_GARBAGE;
13759
        return -1;
13760
      }
13761
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
13762
                      tab->insideout_buf, key->key_length));
13763
    tab->insideout_match_tab->found_match= 0;
13764
    return 0;
13765
  }
13766
  else
13767
    return join_read_next_same(info);
13768
}
13769
13770
static int
13771
join_read_next_same(READ_RECORD *info)
13772
{
13773
  int error;
13774
  TABLE *table= info->table;
13775
  JOIN_TAB *tab=table->reginfo.join_tab;
13776
13777
  if ((error=table->file->index_next_same(table->record[0],
13778
					  tab->ref.key_buff,
13779
					  tab->ref.key_length)))
13780
  {
13781
    if (error != HA_ERR_END_OF_FILE)
13782
      return report_error(table, error);
13783
    table->status= STATUS_GARBAGE;
13784
    return -1;
13785
  }
13786
  return 0;
13787
}
13788
13789
13790
static int
13791
join_read_prev_same(READ_RECORD *info)
13792
{
13793
  int error;
13794
  TABLE *table= info->table;
13795
  JOIN_TAB *tab=table->reginfo.join_tab;
13796
13797
  if ((error=table->file->index_prev(table->record[0])))
13798
    return report_error(table, error);
13799
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
13800
                      tab->ref.key_length))
13801
  {
13802
    table->status=STATUS_NOT_FOUND;
13803
    error= -1;
13804
  }
13805
  return error;
13806
}
13807
13808
13809
static int
13810
join_init_quick_read_record(JOIN_TAB *tab)
13811
{
13812
  if (test_if_quick_select(tab) == -1)
13813
    return -1;					/* No possible records */
13814
  return join_init_read_record(tab);
13815
}
13816
13817
13818
int rr_sequential(READ_RECORD *info);
13819
int init_read_record_seq(JOIN_TAB *tab)
13820
{
13821
  tab->read_record.read_record= rr_sequential;
13822
  if (tab->read_record.file->ha_rnd_init(1))
13823
    return 1;
13824
  return (*tab->read_record.read_record)(&tab->read_record);
13825
}
13826
13827
static int
13828
test_if_quick_select(JOIN_TAB *tab)
13829
{
13830
  delete tab->select->quick;
13831
  tab->select->quick=0;
13832
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
13833
					(table_map) 0, HA_POS_ERROR, 0,
13834
                                        FALSE);
13835
}
13836
13837
13838
static int
13839
join_init_read_record(JOIN_TAB *tab)
13840
{
13841
  if (tab->select && tab->select->quick && tab->select->quick->reset())
13842
    return 1;
13843
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
13844
		   tab->select,1,1);
13845
  return (*tab->read_record.read_record)(&tab->read_record);
13846
}
13847
13848
13849
static int
13850
join_read_first(JOIN_TAB *tab)
13851
{
13852
  int error;
13853
  TABLE *table=tab->table;
13854
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
13855
      !table->no_keyread)
13856
  {
13857
    table->key_read=1;
13858
    table->file->extra(HA_EXTRA_KEYREAD);
13859
  }
13860
  tab->table->status=0;
13861
  tab->read_record.table=table;
13862
  tab->read_record.file=table->file;
13863
  tab->read_record.index=tab->index;
13864
  tab->read_record.record=table->record[0];
13865
  if (tab->insideout_match_tab)
13866
  {
13867
    tab->read_record.do_insideout_scan= tab;
13868
    tab->read_record.read_record=join_read_next_different;
13869
    tab->insideout_match_tab->found_match= 0;
13870
  }
13871
  else
13872
  {
13873
    tab->read_record.read_record=join_read_next;
13874
    tab->read_record.do_insideout_scan= 0;
13875
  }
13876
13877
  if (!table->file->inited)
13878
    table->file->ha_index_init(tab->index, tab->sorted);
13879
  if ((error=tab->table->file->index_first(tab->table->record[0])))
13880
  {
13881
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13882
      report_error(table, error);
13883
    return -1;
13884
  }
13885
  return 0;
13886
}
13887
13888
13889
static int
13890
join_read_next_different(READ_RECORD *info)
13891
{
13892
  JOIN_TAB *tab= info->do_insideout_scan;
13893
  if (tab->insideout_match_tab->found_match)
13894
  {
13895
    KEY *key= tab->table->key_info + tab->index;
13896
    do 
13897
    {
13898
      int error;
13899
      /* Save index tuple from record to the buffer */
13900
      key_copy(tab->insideout_buf, info->record, key, 0);
13901
13902
      if ((error=info->file->index_next(info->record)))
13903
        return report_error(info->table, error);
13904
      
13905
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
13906
                      tab->insideout_buf, key->key_length));
13907
    tab->insideout_match_tab->found_match= 0;
13908
    return 0;
13909
  }
13910
  else
13911
    return join_read_next(info);
13912
}
13913
13914
13915
static int
13916
join_read_next(READ_RECORD *info)
13917
{
13918
  int error;
13919
  if ((error=info->file->index_next(info->record)))
13920
    return report_error(info->table, error);
13921
  return 0;
13922
}
13923
13924
13925
static int
13926
join_read_last(JOIN_TAB *tab)
13927
{
13928
  TABLE *table=tab->table;
13929
  int error;
13930
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
13931
      !table->no_keyread)
13932
  {
13933
    table->key_read=1;
13934
    table->file->extra(HA_EXTRA_KEYREAD);
13935
  }
13936
  tab->table->status=0;
13937
  tab->read_record.read_record=join_read_prev;
13938
  tab->read_record.table=table;
13939
  tab->read_record.file=table->file;
13940
  tab->read_record.index=tab->index;
13941
  tab->read_record.record=table->record[0];
13942
  if (!table->file->inited)
13943
    table->file->ha_index_init(tab->index, 1);
13944
  if ((error= tab->table->file->index_last(tab->table->record[0])))
13945
    return report_error(table, error);
13946
  return 0;
13947
}
13948
13949
13950
static int
13951
join_read_prev(READ_RECORD *info)
13952
{
13953
  int error;
13954
  if ((error= info->file->index_prev(info->record)))
13955
    return report_error(info->table, error);
13956
  return 0;
13957
}
13958
13959
/**
13960
  Reading of key with key reference and one part that may be NULL.
13961
*/
13962
13963
int
13964
join_read_always_key_or_null(JOIN_TAB *tab)
13965
{
13966
  int res;
13967
13968
  /* First read according to key which is NOT NULL */
13969
  *tab->ref.null_ref_key= 0;			// Clear null byte
13970
  if ((res= join_read_always_key(tab)) >= 0)
13971
    return res;
13972
13973
  /* Then read key with null value */
13974
  *tab->ref.null_ref_key= 1;			// Set null byte
13975
  return safe_index_read(tab);
13976
}
13977
13978
13979
int
13980
join_read_next_same_or_null(READ_RECORD *info)
13981
{
13982
  int error;
13983
  if ((error= join_read_next_same(info)) >= 0)
13984
    return error;
13985
  JOIN_TAB *tab= info->table->reginfo.join_tab;
13986
13987
  /* Test if we have already done a read after null key */
13988
  if (*tab->ref.null_ref_key)
13989
    return -1;					// All keys read
13990
  *tab->ref.null_ref_key= 1;			// Set null byte
13991
  return safe_index_read(tab);			// then read null keys
13992
}
13993
13994
13995
/*****************************************************************************
13996
  DESCRIPTION
13997
    Functions that end one nested loop iteration. Different functions
13998
    are used to support GROUP BY clause and to redirect records
13999
    to a table (e.g. in case of SELECT into a temporary table) or to the
14000
    network client.
14001
14002
  RETURN VALUES
14003
    NESTED_LOOP_OK           - the record has been successfully handled
14004
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
14005
                               was detected
14006
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
14007
                               the record
14008
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
14009
                               additionally, the nested loop produced the
14010
                               number of rows specified in the LIMIT clause
14011
                               for the query
14012
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
14013
                               additionally, there is a cursor and the nested
14014
                               loop algorithm produced the number of rows
14015
                               that is specified for current cursor fetch
14016
                               operation.
14017
   All return values except NESTED_LOOP_OK abort the nested loop.
14018
*****************************************************************************/
14019
14020
/* ARGSUSED */
14021
static enum_nested_loop_state
14022
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14023
	 bool end_of_records)
14024
{
14025
  DBUG_ENTER("end_send");
14026
  if (!end_of_records)
14027
  {
14028
    int error;
14029
    if (join->having && join->having->val_int() == 0)
14030
      DBUG_RETURN(NESTED_LOOP_OK);               // Didn't match having
14031
    error=0;
14032
    if (join->do_send_rows)
14033
      error=join->result->send_data(*join->fields);
14034
    if (error)
14035
      DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
14036
    if (++join->send_records >= join->unit->select_limit_cnt &&
14037
	join->do_send_rows)
14038
    {
14039
      if (join->select_options & OPTION_FOUND_ROWS)
14040
      {
14041
	JOIN_TAB *jt=join->join_tab;
14042
	if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
14043
	    && !join->send_group_parts && !join->having && !jt->select_cond &&
14044
	    !(jt->select && jt->select->quick) &&
14045
	    (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
14046
            (jt->ref.key < 0))
14047
	{
14048
	  /* Join over all rows in table;  Return number of found rows */
14049
	  TABLE *table=jt->table;
14050
14051
	  join->select_options ^= OPTION_FOUND_ROWS;
14052
	  if (table->sort.record_pointers ||
14053
	      (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
14054
	  {
14055
	    /* Using filesort */
14056
	    join->send_records= table->sort.found_records;
14057
	  }
14058
	  else
14059
	  {
14060
	    table->file->info(HA_STATUS_VARIABLE);
14061
	    join->send_records= table->file->stats.records;
14062
	  }
14063
	}
14064
	else 
14065
	{
14066
	  join->do_send_rows= 0;
14067
	  if (join->unit->fake_select_lex)
14068
	    join->unit->fake_select_lex->select_limit= 0;
14069
	  DBUG_RETURN(NESTED_LOOP_OK);
14070
	}
14071
      }
14072
      DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
14073
    }
14074
    else if (join->send_records >= join->fetch_limit)
14075
    {
14076
      /*
14077
        There is a server side cursor and all rows for
14078
        this fetch request are sent.
14079
      */
14080
      DBUG_RETURN(NESTED_LOOP_CURSOR_LIMIT);
14081
    }
14082
  }
14083
14084
  DBUG_RETURN(NESTED_LOOP_OK);
14085
}
14086
14087
14088
	/* ARGSUSED */
14089
enum_nested_loop_state
14090
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14091
	       bool end_of_records)
14092
{
14093
  int idx= -1;
14094
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
14095
  DBUG_ENTER("end_send_group");
14096
14097
  if (!join->first_record || end_of_records ||
14098
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14099
  {
14100
    if (join->first_record || 
14101
        (end_of_records && !join->group && !join->group_optimized_away))
14102
    {
14103
      if (idx < (int) join->send_group_parts)
14104
      {
14105
	int error=0;
14106
	{
14107
	  if (!join->first_record)
14108
	  {
14109
            List_iterator_fast<Item> it(*join->fields);
14110
            Item *item;
14111
	    /* No matching rows for group function */
14112
	    join->clear();
14113
14114
            while ((item= it++))
14115
              item->no_rows_in_result();
14116
	  }
14117
	  if (join->having && join->having->val_int() == 0)
14118
	    error= -1;				// Didn't satisfy having
14119
	  else
14120
	  {
14121
	    if (join->do_send_rows)
14122
	      error=join->result->send_data(*join->fields) ? 1 : 0;
14123
	    join->send_records++;
14124
	  }
14125
	  if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
14126
	  {
14127
	    if (join->rollup_send_data((uint) (idx+1)))
14128
	      error= 1;
14129
	  }
14130
	}
14131
	if (error > 0)
14132
          DBUG_RETURN(NESTED_LOOP_ERROR);        /* purecov: inspected */
14133
	if (end_of_records)
14134
	  DBUG_RETURN(NESTED_LOOP_OK);
14135
	if (join->send_records >= join->unit->select_limit_cnt &&
14136
	    join->do_send_rows)
14137
	{
14138
	  if (!(join->select_options & OPTION_FOUND_ROWS))
14139
	    DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
14140
	  join->do_send_rows=0;
14141
	  join->unit->select_limit_cnt = HA_POS_ERROR;
14142
        }
14143
        else if (join->send_records >= join->fetch_limit)
14144
        {
14145
          /*
14146
            There is a server side cursor and all rows
14147
            for this fetch request are sent.
14148
          */
14149
          /*
14150
            Preventing code duplication. When finished with the group reset
14151
            the group functions and copy_fields. We fall through. bug #11904
14152
          */
14153
          ok_code= NESTED_LOOP_CURSOR_LIMIT;
14154
        }
14155
      }
14156
    }
14157
    else
14158
    {
14159
      if (end_of_records)
14160
	DBUG_RETURN(NESTED_LOOP_OK);
14161
      join->first_record=1;
14162
      VOID(test_if_item_cache_changed(join->group_fields));
14163
    }
14164
    if (idx < (int) join->send_group_parts)
14165
    {
14166
      /*
14167
        This branch is executed also for cursors which have finished their
14168
        fetch limit - the reason for ok_code.
14169
      */
14170
      copy_fields(&join->tmp_table_param);
14171
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14172
	DBUG_RETURN(NESTED_LOOP_ERROR);
14173
      DBUG_RETURN(ok_code);
14174
    }
14175
  }
14176
  if (update_sum_func(join->sum_funcs))
14177
    DBUG_RETURN(NESTED_LOOP_ERROR);
14178
  DBUG_RETURN(NESTED_LOOP_OK);
14179
}
14180
14181
14182
	/* ARGSUSED */
14183
enum_nested_loop_state
14184
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14185
	  bool end_of_records)
14186
{
14187
  TABLE *table=join->tmp_table;
14188
  DBUG_ENTER("end_write");
14189
14190
  if (join->thd->killed)			// Aborted by user
14191
  {
14192
    join->thd->send_kill_message();
14193
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
14194
  }
14195
  if (!end_of_records)
14196
  {
14197
    copy_fields(&join->tmp_table_param);
14198
    copy_funcs(join->tmp_table_param.items_to_copy);
14199
#ifdef TO_BE_DELETED
14200
    if (!table->uniques)			// If not unique handling
14201
    {
14202
      /* Copy null values from group to row */
14203
      ORDER   *group;
14204
      for (group=table->group ; group ; group=group->next)
14205
      {
14206
	Item *item= *group->item;
14207
	if (item->maybe_null)
14208
	{
14209
	  Field *field=item->get_tmp_table_field();
14210
	  field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
14211
	}
14212
      }
14213
    }
14214
#endif
14215
    if (!join->having || join->having->val_int())
14216
    {
14217
      int error;
14218
      join->found_records++;
14219
      if ((error=table->file->ha_write_row(table->record[0])))
14220
      {
14221
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
14222
	  goto end;
14223
	if (create_myisam_from_heap(join->thd, table,
14224
                                    join->tmp_table_param.start_recinfo,
14225
                                    &join->tmp_table_param.recinfo,
14226
				    error, 1))
14227
	  DBUG_RETURN(NESTED_LOOP_ERROR);        // Not a table_is_full error
14228
	table->s->uniques=0;			// To ensure rows are the same
14229
      }
14230
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
14231
	  join->do_send_rows)
14232
      {
14233
	if (!(join->select_options & OPTION_FOUND_ROWS))
14234
	  DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
14235
	join->do_send_rows=0;
14236
	join->unit->select_limit_cnt = HA_POS_ERROR;
14237
	DBUG_RETURN(NESTED_LOOP_OK);
14238
      }
14239
    }
14240
  }
14241
end:
14242
  DBUG_RETURN(NESTED_LOOP_OK);
14243
}
14244
14245
/* ARGSUSED */
14246
/** Group by searching after group record and updating it if possible. */
14247
14248
static enum_nested_loop_state
14249
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14250
	   bool end_of_records)
14251
{
14252
  TABLE *table=join->tmp_table;
14253
  ORDER   *group;
14254
  int	  error;
14255
  DBUG_ENTER("end_update");
14256
14257
  if (end_of_records)
14258
    DBUG_RETURN(NESTED_LOOP_OK);
14259
  if (join->thd->killed)			// Aborted by user
14260
  {
14261
    join->thd->send_kill_message();
14262
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
14263
  }
14264
14265
  join->found_records++;
14266
  copy_fields(&join->tmp_table_param);		// Groups are copied twice.
14267
  /* Make a key of group index */
14268
  for (group=table->group ; group ; group=group->next)
14269
  {
14270
    Item *item= *group->item;
14271
    item->save_org_in_field(group->field);
14272
    /* Store in the used key if the field was 0 */
14273
    if (item->maybe_null)
14274
      group->buff[-1]= (char) group->field->is_null();
14275
  }
14276
  if (!table->file->index_read_map(table->record[1],
14277
                                   join->tmp_table_param.group_buff,
14278
                                   HA_WHOLE_KEY,
14279
                                   HA_READ_KEY_EXACT))
14280
  {						/* Update old record */
14281
    restore_record(table,record[1]);
14282
    update_tmptable_sum_func(join->sum_funcs,table);
14283
    if ((error=table->file->ha_update_row(table->record[1],
14284
                                          table->record[0])))
14285
    {
14286
      table->file->print_error(error,MYF(0));	/* purecov: inspected */
14287
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
14288
    }
14289
    DBUG_RETURN(NESTED_LOOP_OK);
14290
  }
14291
14292
  /*
14293
    Copy null bits from group key to table
14294
    We can't copy all data as the key may have different format
14295
    as the row data (for example as with VARCHAR keys)
14296
  */
14297
  KEY_PART_INFO *key_part;
14298
  for (group=table->group,key_part=table->key_info[0].key_part;
14299
       group ;
14300
       group=group->next,key_part++)
14301
  {
14302
    if (key_part->null_bit)
14303
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
14304
  }
14305
  init_tmptable_sum_functions(join->sum_funcs);
14306
  copy_funcs(join->tmp_table_param.items_to_copy);
14307
  if ((error=table->file->ha_write_row(table->record[0])))
14308
  {
14309
    if (create_myisam_from_heap(join->thd, table,
14310
                                join->tmp_table_param.start_recinfo,
14311
                                &join->tmp_table_param.recinfo,
14312
				error, 0))
14313
      DBUG_RETURN(NESTED_LOOP_ERROR);            // Not a table_is_full error
14314
    /* Change method to update rows */
14315
    table->file->ha_index_init(0, 0);
14316
    join->join_tab[join->tables-1].next_select=end_unique_update;
14317
  }
14318
  join->send_records++;
14319
  DBUG_RETURN(NESTED_LOOP_OK);
14320
}
14321
14322
14323
/** Like end_update, but this is done with unique constraints instead of keys.  */
14324
14325
static enum_nested_loop_state
14326
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14327
		  bool end_of_records)
14328
{
14329
  TABLE *table=join->tmp_table;
14330
  int	  error;
14331
  DBUG_ENTER("end_unique_update");
14332
14333
  if (end_of_records)
14334
    DBUG_RETURN(NESTED_LOOP_OK);
14335
  if (join->thd->killed)			// Aborted by user
14336
  {
14337
    join->thd->send_kill_message();
14338
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
14339
  }
14340
14341
  init_tmptable_sum_functions(join->sum_funcs);
14342
  copy_fields(&join->tmp_table_param);		// Groups are copied twice.
14343
  copy_funcs(join->tmp_table_param.items_to_copy);
14344
14345
  if (!(error=table->file->ha_write_row(table->record[0])))
14346
    join->send_records++;			// New group
14347
  else
14348
  {
14349
    if ((int) table->file->get_dup_key(error) < 0)
14350
    {
14351
      table->file->print_error(error,MYF(0));	/* purecov: inspected */
14352
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
14353
    }
14354
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
14355
    {
14356
      table->file->print_error(error,MYF(0));	/* purecov: inspected */
14357
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
14358
    }
14359
    restore_record(table,record[1]);
14360
    update_tmptable_sum_func(join->sum_funcs,table);
14361
    if ((error=table->file->ha_update_row(table->record[1],
14362
                                          table->record[0])))
14363
    {
14364
      table->file->print_error(error,MYF(0));	/* purecov: inspected */
14365
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
14366
    }
14367
  }
14368
  DBUG_RETURN(NESTED_LOOP_OK);
14369
}
14370
14371
14372
	/* ARGSUSED */
14373
enum_nested_loop_state
14374
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14375
		bool end_of_records)
14376
{
14377
  TABLE *table=join->tmp_table;
14378
  int	  idx= -1;
14379
  DBUG_ENTER("end_write_group");
14380
14381
  if (join->thd->killed)
14382
  {						// Aborted by user
14383
    join->thd->send_kill_message();
14384
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
14385
  }
14386
  if (!join->first_record || end_of_records ||
14387
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14388
  {
14389
    if (join->first_record || (end_of_records && !join->group))
14390
    {
14391
      int send_group_parts= join->send_group_parts;
14392
      if (idx < send_group_parts)
14393
      {
14394
	if (!join->first_record)
14395
	{
14396
	  /* No matching rows for group function */
14397
	  join->clear();
14398
	}
14399
        copy_sum_funcs(join->sum_funcs,
14400
                       join->sum_funcs_end[send_group_parts]);
14401
	if (!join->having || join->having->val_int())
14402
	{
14403
          int error= table->file->ha_write_row(table->record[0]);
14404
          if (error && create_myisam_from_heap(join->thd, table,
14405
                                               join->tmp_table_param.start_recinfo,
14406
                                                &join->tmp_table_param.recinfo,
14407
                                               error, 0))
14408
	    DBUG_RETURN(NESTED_LOOP_ERROR);
14409
        }
14410
        if (join->rollup.state != ROLLUP::STATE_NONE)
14411
	{
14412
	  if (join->rollup_write_data((uint) (idx+1), table))
14413
	    DBUG_RETURN(NESTED_LOOP_ERROR);
14414
	}
14415
	if (end_of_records)
14416
	  DBUG_RETURN(NESTED_LOOP_OK);
14417
      }
14418
    }
14419
    else
14420
    {
14421
      if (end_of_records)
14422
	DBUG_RETURN(NESTED_LOOP_OK);
14423
      join->first_record=1;
14424
      VOID(test_if_item_cache_changed(join->group_fields));
14425
    }
14426
    if (idx < (int) join->send_group_parts)
14427
    {
14428
      copy_fields(&join->tmp_table_param);
14429
      copy_funcs(join->tmp_table_param.items_to_copy);
14430
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14431
	DBUG_RETURN(NESTED_LOOP_ERROR);
14432
      DBUG_RETURN(NESTED_LOOP_OK);
14433
    }
14434
  }
14435
  if (update_sum_func(join->sum_funcs))
14436
    DBUG_RETURN(NESTED_LOOP_ERROR);
14437
  DBUG_RETURN(NESTED_LOOP_OK);
14438
}
14439
14440
14441
/*****************************************************************************
14442
  Remove calculation with tables that aren't yet read. Remove also tests
14443
  against fields that are read through key where the table is not a
14444
  outer join table.
14445
  We can't remove tests that are made against columns which are stored
14446
  in sorted order.
14447
*****************************************************************************/
14448
14449
/**
14450
  @return
14451
    1 if right_item is used removable reference key on left_item
14452
*/
14453
14454
static bool test_if_ref(Item_field *left_item,Item *right_item)
14455
{
14456
  Field *field=left_item->field;
14457
  // No need to change const test. We also have to keep tests on LEFT JOIN
14458
  if (!field->table->const_table && !field->table->maybe_null)
14459
  {
14460
    Item *ref_item=part_of_refkey(field->table,field);
14461
    if (ref_item && ref_item->eq(right_item,1))
14462
    {
14463
      right_item= right_item->real_item();
14464
      if (right_item->type() == Item::FIELD_ITEM)
14465
	return (field->eq_def(((Item_field *) right_item)->field));
14466
      /* remove equalities injected by IN->EXISTS transformation */
14467
      else if (right_item->type() == Item::CACHE_ITEM)
14468
        return ((Item_cache *)right_item)->eq_def (field);
14469
      if (right_item->const_item() && !(right_item->is_null()))
14470
      {
14471
	/*
14472
	  We can remove binary fields and numerical fields except float,
14473
	  as float comparison isn't 100 % secure
14474
	  We have to keep normal strings to be able to check for end spaces
14475
14476
          sergefp: the above seems to be too restrictive. Counterexample:
14477
            create table t100 (v varchar(10), key(v)) default charset=latin1;
14478
            insert into t100 values ('a'),('a ');
14479
            explain select * from t100 where v='a';
14480
          The EXPLAIN shows 'using Where'. Running the query returns both
14481
          rows, so it seems there are no problems with endspace in the most
14482
          frequent case?
14483
	*/
14484
	if (field->binary() &&
14485
	    field->real_type() != MYSQL_TYPE_STRING &&
14486
	    field->real_type() != MYSQL_TYPE_VARCHAR &&
14487
	    (field->type() != MYSQL_TYPE_FLOAT || field->decimals() == 0))
14488
	{
14489
	  return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
14490
	}
14491
      }
14492
    }
14493
  }
14494
  return 0;					// keep test
14495
}
14496
14497
/**
14498
   @brief Replaces an expression destructively inside the expression tree of
14499
   the WHERE clase.
14500
14501
   @note Because of current requirements for semijoin flattening, we do not
14502
   need to recurse here, hence this function will only examine the top-level
14503
   AND conditions. (see JOIN::prepare, comment above the line 
14504
   'if (do_materialize)'
14505
   
14506
   @param join The top-level query.
14507
   @param old_cond The expression to be replaced.
14508
   @param new_cond The expression to be substituted.
14509
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
14510
   the new expression.
14511
   @return <code>true</code> if there was an error, <code>false</code> if
14512
   successful.
14513
*/
14514
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
14515
                                       Item *new_cond, bool do_fix_fields)
14516
{
14517
  if (join->conds == old_cond) {
14518
    join->conds= new_cond;
14519
    if (do_fix_fields)
14520
      new_cond->fix_fields(join->thd, &join->conds);
14521
    return FALSE;
14522
  }
14523
  
14524
  if (join->conds->type() == Item::COND_ITEM) {
14525
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
14526
    Item *item;
14527
    while ((item= li++))
14528
      if (item == old_cond) 
14529
      {
14530
        li.replace(new_cond);
14531
        if (do_fix_fields)
14532
          new_cond->fix_fields(join->thd, li.ref());
14533
        return FALSE;
14534
      }
14535
  }
14536
14537
  return TRUE;
14538
}
14539
14540
/*
14541
  Extract a condition that can be checked after reading given table
14542
  
14543
  SYNOPSIS
14544
    make_cond_for_table()
14545
      cond         Condition to analyze
14546
      tables       Tables for which "current field values" are available
14547
      used_table   Table that we're extracting the condition for (may 
14548
                   also include PSEUDO_TABLE_BITS
14549
14550
  DESCRIPTION
14551
    Extract the condition that can be checked after reading the table
14552
    specified in 'used_table', given that current-field values for tables
14553
    specified in 'tables' bitmap are available.
14554
14555
    The function assumes that
14556
      - Constant parts of the condition has already been checked.
14557
      - Condition that could be checked for tables in 'tables' has already 
14558
        been checked.
14559
        
14560
    The function takes into account that some parts of the condition are
14561
    guaranteed to be true by employed 'ref' access methods (the code that
14562
    does this is located at the end, search down for "EQ_FUNC").
14563
14564
14565
  SEE ALSO 
14566
    make_cond_for_info_schema uses similar algorithm
14567
14568
  RETURN
14569
    Extracted condition
14570
*/
14571
14572
static COND *
14573
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
14574
                    bool exclude_expensive_cond)
14575
{
14576
  if (used_table && !(cond->used_tables() & used_table) &&
14577
      /*
14578
        Exclude constant conditions not checked at optimization time if
14579
        the table we are pushing conditions to is the first one.
14580
        As a result, such conditions are not considered as already checked
14581
        and will be checked at execution time, attached to the first table.
14582
      */
14583
      !((used_table & 1) && cond->is_expensive()))
14584
    return (COND*) 0;				// Already checked
14585
  if (cond->type() == Item::COND_ITEM)
14586
  {
14587
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
14588
    {
14589
      /* Create new top level AND item */
14590
      Item_cond_and *new_cond=new Item_cond_and;
14591
      if (!new_cond)
14592
	return (COND*) 0;			// OOM /* purecov: inspected */
14593
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14594
      Item *item;
14595
      while ((item=li++))
14596
      {
14597
	Item *fix=make_cond_for_table(item,tables,used_table,
14598
                                      exclude_expensive_cond);
14599
	if (fix)
14600
	  new_cond->argument_list()->push_back(fix);
14601
      }
14602
      switch (new_cond->argument_list()->elements) {
14603
      case 0:
14604
	return (COND*) 0;			// Always true
14605
      case 1:
14606
	return new_cond->argument_list()->head();
14607
      default:
14608
	/*
14609
	  Item_cond_and do not need fix_fields for execution, its parameters
14610
	  are fixed or do not need fix_fields, too
14611
	*/
14612
	new_cond->quick_fix_field();
14613
	new_cond->used_tables_cache=
14614
	  ((Item_cond_and*) cond)->used_tables_cache &
14615
	  tables;
14616
	return new_cond;
14617
      }
14618
    }
14619
    else
14620
    {						// Or list
14621
      Item_cond_or *new_cond=new Item_cond_or;
14622
      if (!new_cond)
14623
	return (COND*) 0;			// OOM /* purecov: inspected */
14624
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14625
      Item *item;
14626
      while ((item=li++))
14627
      {
14628
	Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
14629
	if (!fix)
14630
	  return (COND*) 0;			// Always true
14631
	new_cond->argument_list()->push_back(fix);
14632
      }
14633
      /*
14634
	Item_cond_and do not need fix_fields for execution, its parameters
14635
	are fixed or do not need fix_fields, too
14636
      */
14637
      new_cond->quick_fix_field();
14638
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
14639
      new_cond->top_level_item();
14640
      return new_cond;
14641
    }
14642
  }
14643
14644
  /*
14645
    Because the following test takes a while and it can be done
14646
    table_count times, we mark each item that we have examined with the result
14647
    of the test
14648
  */
14649
14650
  if (cond->marker == 3 || (cond->used_tables() & ~tables) ||
14651
      /*
14652
        When extracting constant conditions, treat expensive conditions as
14653
        non-constant, so that they are not evaluated at optimization time.
14654
      */
14655
      (!used_table && exclude_expensive_cond && cond->is_expensive()))
14656
    return (COND*) 0;				// Can't check this yet
14657
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
14658
    return cond;				// Not boolean op
14659
14660
  /* 
14661
    Remove equalities that are guaranteed to be true by use of 'ref' access
14662
    method
14663
  */
14664
  if (((Item_func*) cond)->functype() == Item_func::EQ_FUNC)
14665
  {
14666
    Item *left_item=	((Item_func*) cond)->arguments()[0];
14667
    Item *right_item= ((Item_func*) cond)->arguments()[1];
14668
    if (left_item->type() == Item::FIELD_ITEM &&
14669
	test_if_ref((Item_field*) left_item,right_item))
14670
    {
14671
      cond->marker=3;			// Checked when read
14672
      return (COND*) 0;
14673
    }
14674
    if (right_item->type() == Item::FIELD_ITEM &&
14675
	test_if_ref((Item_field*) right_item,left_item))
14676
    {
14677
      cond->marker=3;			// Checked when read
14678
      return (COND*) 0;
14679
    }
14680
  }
14681
  cond->marker=2;
14682
  return cond;
14683
}
14684
14685
14686
static Item *
14687
part_of_refkey(TABLE *table,Field *field)
14688
{
14689
  if (!table->reginfo.join_tab)
14690
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
14691
14692
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
14693
  if (ref_parts)
14694
  {
14695
    KEY_PART_INFO *key_part=
14696
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
14697
    uint part;
14698
14699
    for (part=0 ; part < ref_parts ; part++)
14700
    {
14701
      if (table->reginfo.join_tab->ref.cond_guards[part])
14702
        return 0;
14703
    }
14704
14705
    for (part=0 ; part < ref_parts ; part++,key_part++)
14706
      if (field->eq(key_part->field) &&
14707
	  !(key_part->key_part_flag & HA_PART_KEY_SEG))
14708
	return table->reginfo.join_tab->ref.items[part];
14709
  }
14710
  return (Item*) 0;
14711
}
14712
14713
14714
/**
14715
  Test if one can use the key to resolve ORDER BY.
14716
14717
  @param order                 Sort order
14718
  @param table                 Table to sort
14719
  @param idx                   Index to check
14720
  @param used_key_parts        Return value for used key parts.
14721
14722
14723
  @note
14724
    used_key_parts is set to correct key parts used if return value != 0
14725
    (On other cases, used_key_part may be changed)
14726
14727
  @retval
14728
    1   key is ok.
14729
  @retval
14730
    0   Key can't be used
14731
  @retval
14732
    -1   Reverse key can be used
14733
*/
14734
14735
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
14736
				uint *used_key_parts)
14737
{
14738
  KEY_PART_INFO *key_part,*key_part_end;
14739
  key_part=table->key_info[idx].key_part;
14740
  key_part_end=key_part+table->key_info[idx].key_parts;
14741
  key_part_map const_key_parts=table->const_key_parts[idx];
14742
  int reverse=0;
14743
  my_bool on_primary_key= FALSE;
14744
  DBUG_ENTER("test_if_order_by_key");
14745
14746
  for (; order ; order=order->next, const_key_parts>>=1)
14747
  {
14748
    Field *field=((Item_field*) (*order->item)->real_item())->field;
14749
    int flag;
14750
14751
    /*
14752
      Skip key parts that are constants in the WHERE clause.
14753
      These are already skipped in the ORDER BY by const_expression_in_where()
14754
    */
14755
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
14756
      key_part++; 
14757
14758
    if (key_part == key_part_end)
14759
    {
14760
      /* 
14761
        We are at the end of the key. Check if the engine has the primary
14762
        key as a suffix to the secondary keys. If it has continue to check
14763
        the primary key as a suffix.
14764
      */
14765
      if (!on_primary_key &&
14766
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
14767
          table->s->primary_key != MAX_KEY)
14768
      {
14769
        on_primary_key= TRUE;
14770
        key_part= table->key_info[table->s->primary_key].key_part;
14771
        key_part_end=key_part+table->key_info[table->s->primary_key].key_parts;
14772
        const_key_parts=table->const_key_parts[table->s->primary_key];
14773
14774
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
14775
          key_part++; 
14776
        /*
14777
         The primary and secondary key parts were all const (i.e. there's
14778
         one row).  The sorting doesn't matter.
14779
        */
14780
        if (key_part == key_part_end && reverse == 0)
14781
          DBUG_RETURN(1);
14782
      }
14783
      else
14784
        DBUG_RETURN(0);
14785
    }
14786
14787
    if (key_part->field != field)
14788
      DBUG_RETURN(0);
14789
14790
    /* set flag to 1 if we can use read-next on key, else to -1 */
14791
    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
14792
           1 : -1);
14793
    if (reverse && flag != reverse)
14794
      DBUG_RETURN(0);
14795
    reverse=flag;				// Remember if reverse
14796
    key_part++;
14797
  }
14798
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
14799
    (uint) (key_part - table->key_info[idx].key_part);
14800
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
14801
                         HA_READ_PREV))
14802
    reverse= 0;                                 // Index can't be used
14803
  DBUG_RETURN(reverse);
14804
}
14805
14806
14807
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
14808
{
14809
  uint min_length= (uint) ~0;
14810
  uint best= MAX_KEY;
14811
  if (!usable_keys->is_clear_all())
14812
  {
14813
    for (uint nr=0; nr < table->s->keys ; nr++)
14814
    {
14815
      if (usable_keys->is_set(nr))
14816
      {
14817
        if (table->key_info[nr].key_length < min_length)
14818
        {
14819
          min_length=table->key_info[nr].key_length;
14820
          best=nr;
14821
        }
14822
      }
14823
    }
14824
  }
14825
  return best;
14826
}
14827
14828
/**
14829
  Test if a second key is the subkey of the first one.
14830
14831
  @param key_part              First key parts
14832
  @param ref_key_part          Second key parts
14833
  @param ref_key_part_end      Last+1 part of the second key
14834
14835
  @note
14836
    Second key MUST be shorter than the first one.
14837
14838
  @retval
14839
    1	is a subkey
14840
  @retval
14841
    0	no sub key
14842
*/
14843
14844
inline bool 
14845
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
14846
	  KEY_PART_INFO *ref_key_part_end)
14847
{
14848
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
14849
    if (!key_part->field->eq(ref_key_part->field))
14850
      return 0;
14851
  return 1;
14852
}
14853
14854
/**
14855
  Test if we can use one of the 'usable_keys' instead of 'ref' key
14856
  for sorting.
14857
14858
  @param ref			Number of key, used for WHERE clause
14859
  @param usable_keys		Keys for testing
14860
14861
  @return
14862
    - MAX_KEY			If we can't use other key
14863
    - the number of found key	Otherwise
14864
*/
14865
14866
static uint
14867
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
14868
	       const key_map *usable_keys)
14869
{
14870
  uint nr;
14871
  uint min_length= (uint) ~0;
14872
  uint best= MAX_KEY;
14873
  uint not_used;
14874
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
14875
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
14876
14877
  for (nr= 0 ; nr < table->s->keys ; nr++)
14878
  {
14879
    if (usable_keys->is_set(nr) &&
14880
	table->key_info[nr].key_length < min_length &&
14881
	table->key_info[nr].key_parts >= ref_key_parts &&
14882
	is_subkey(table->key_info[nr].key_part, ref_key_part,
14883
		  ref_key_part_end) &&
14884
	test_if_order_by_key(order, table, nr, &not_used))
14885
    {
14886
      min_length= table->key_info[nr].key_length;
14887
      best= nr;
14888
    }
14889
  }
14890
  return best;
14891
}
14892
14893
14894
/**
14895
  Check if GROUP BY/DISTINCT can be optimized away because the set is
14896
  already known to be distinct.
14897
14898
  Used in removing the GROUP BY/DISTINCT of the following types of
14899
  statements:
14900
  @code
14901
    SELECT [DISTINCT] <unique_key_cols>... FROM <single_table_ref>
14902
      [GROUP BY <unique_key_cols>,...]
14903
  @endcode
14904
14905
    If (a,b,c is distinct)
14906
    then <any combination of a,b,c>,{whatever} is also distinct
14907
14908
    This function checks if all the key parts of any of the unique keys
14909
    of the table are referenced by a list : either the select list
14910
    through find_field_in_item_list or GROUP BY list through
14911
    find_field_in_order_list.
14912
    If the above holds and the key parts cannot contain NULLs then we 
14913
    can safely remove the GROUP BY/DISTINCT,
14914
    as no result set can be more distinct than an unique key.
14915
14916
  @param table                The table to operate on.
14917
  @param find_func            function to iterate over the list and search
14918
                              for a field
14919
14920
  @retval
14921
    1                    found
14922
  @retval
14923
    0                    not found.
14924
*/
14925
14926
static bool
14927
list_contains_unique_index(TABLE *table,
14928
                          bool (*find_func) (Field *, void *), void *data)
14929
{
14930
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
14931
  {
14932
    if (keynr == table->s->primary_key ||
14933
         (table->key_info[keynr].flags & HA_NOSAME))
14934
    {
14935
      KEY *keyinfo= table->key_info + keynr;
14936
      KEY_PART_INFO *key_part, *key_part_end;
14937
14938
      for (key_part=keyinfo->key_part,
14939
           key_part_end=key_part+ keyinfo->key_parts;
14940
           key_part < key_part_end;
14941
           key_part++)
14942
      {
14943
        if (key_part->field->maybe_null() || 
14944
            !find_func(key_part->field, data))
14945
          break;
14946
      }
14947
      if (key_part == key_part_end)
14948
        return 1;
14949
    }
14950
  }
14951
  return 0;
14952
}
14953
14954
14955
/**
14956
  Helper function for list_contains_unique_index.
14957
  Find a field reference in a list of ORDER structures.
14958
  Finds a direct reference of the Field in the list.
14959
14960
  @param field                The field to search for.
14961
  @param data                 ORDER *.The list to search in
14962
14963
  @retval
14964
    1                    found
14965
  @retval
14966
    0                    not found.
14967
*/
14968
14969
static bool
14970
find_field_in_order_list (Field *field, void *data)
14971
{
14972
  ORDER *group= (ORDER *) data;
14973
  bool part_found= 0;
14974
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
14975
  {
14976
    Item *item= (*tmp_group->item)->real_item();
14977
    if (item->type() == Item::FIELD_ITEM &&
14978
        ((Item_field*) item)->field->eq(field))
14979
    {
14980
      part_found= 1;
14981
      break;
14982
    }
14983
  }
14984
  return part_found;
14985
}
14986
14987
14988
/**
14989
  Helper function for list_contains_unique_index.
14990
  Find a field reference in a dynamic list of Items.
14991
  Finds a direct reference of the Field in the list.
14992
14993
  @param[in] field             The field to search for.
14994
  @param[in] data              List<Item> *.The list to search in
14995
14996
  @retval
14997
    1                    found
14998
  @retval
14999
    0                    not found.
15000
*/
15001
15002
static bool
15003
find_field_in_item_list (Field *field, void *data)
15004
{
15005
  List<Item> *fields= (List<Item> *) data;
15006
  bool part_found= 0;
15007
  List_iterator<Item> li(*fields);
15008
  Item *item;
15009
15010
  while ((item= li++))
15011
  {
15012
    if (item->type() == Item::FIELD_ITEM &&
15013
        ((Item_field*) item)->field->eq(field))
15014
    {
15015
      part_found= 1;
15016
      break;
15017
    }
15018
  }
15019
  return part_found;
15020
}
15021
15022
15023
/**
15024
  Test if we can skip the ORDER BY by using an index.
15025
15026
  SYNOPSIS
15027
    test_if_skip_sort_order()
15028
      tab
15029
      order
15030
      select_limit
15031
      no_changes
15032
      map
15033
15034
  If we can use an index, the JOIN_TAB / tab->select struct
15035
  is changed to use the index.
15036
15037
  The index must cover all fields in <order>, or it will not be considered.
15038
15039
  @todo
15040
    - sergeyp: Results of all index merge selects actually are ordered 
15041
    by clustered PK values.
15042
15043
  @retval
15044
    0    We have to use filesort to do the sorting
15045
  @retval
15046
    1    We can use an index.
15047
*/
15048
15049
static bool
15050
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
15051
			bool no_changes, const key_map *map)
15052
{
15053
  int ref_key;
15054
  uint ref_key_parts;
15055
  int order_direction;
15056
  uint used_key_parts;
15057
  TABLE *table=tab->table;
15058
  SQL_SELECT *select=tab->select;
15059
  key_map usable_keys;
15060
  QUICK_SELECT_I *save_quick= 0;
15061
  DBUG_ENTER("test_if_skip_sort_order");
15062
15063
  /*
15064
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
15065
    been taken into account.
15066
  */
15067
  usable_keys= *map;
15068
15069
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
15070
  {
15071
    Item *item= (*tmp_order->item)->real_item();
15072
    if (item->type() != Item::FIELD_ITEM)
15073
    {
15074
      usable_keys.clear_all();
15075
      DBUG_RETURN(0);
15076
    }
15077
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
15078
    if (usable_keys.is_clear_all())
15079
      DBUG_RETURN(0);					// No usable keys
15080
  }
15081
15082
  ref_key= -1;
15083
  /* Test if constant range in WHERE */
15084
  if (tab->ref.key >= 0 && tab->ref.key_parts)
15085
  {
15086
    ref_key=	   tab->ref.key;
15087
    ref_key_parts= tab->ref.key_parts;
15088
    if (tab->type == JT_REF_OR_NULL)
15089
      DBUG_RETURN(0);
15090
  }
15091
  else if (select && select->quick)		// Range found by opt_range
15092
  {
15093
    int quick_type= select->quick->get_type();
15094
    save_quick= select->quick;
15095
    /* 
15096
      assume results are not ordered when index merge is used 
15097
      TODO: sergeyp: Results of all index merge selects actually are ordered 
15098
      by clustered PK values.
15099
    */
15100
  
15101
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
15102
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
15103
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
15104
      DBUG_RETURN(0);
15105
    ref_key=	   select->quick->index;
15106
    ref_key_parts= select->quick->used_key_parts;
15107
  }
15108
15109
  if (ref_key >= 0)
15110
  {
15111
    /*
15112
      We come here when there is a REF key.
15113
    */
15114
    if (!usable_keys.is_set(ref_key))
15115
    {
15116
      /*
15117
	We come here when ref_key is not among usable_keys
15118
      */
15119
      uint new_ref_key;
15120
      /*
15121
	If using index only read, only consider other possible index only
15122
	keys
15123
      */
15124
      if (table->covering_keys.is_set(ref_key))
15125
	usable_keys.intersect(table->covering_keys);
15126
      if (tab->pre_idx_push_select_cond)
15127
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
15128
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
15129
				       &usable_keys)) < MAX_KEY)
15130
      {
15131
	/* Found key that can be used to retrieve data in sorted order */
15132
	if (tab->ref.key >= 0)
15133
	{
15134
          /*
15135
            We'll use ref access method on key new_ref_key. In general case 
15136
            the index search tuple for new_ref_key will be different (e.g.
15137
            when one index is defined as (part1, part2, ...) and another as
15138
            (part1, part2(N), ...) and the WHERE clause contains 
15139
            "part1 = const1 AND part2=const2". 
15140
            So we build tab->ref from scratch here.
15141
          */
15142
          KEYUSE *keyuse= tab->keyuse;
15143
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
15144
            keyuse++;
15145
15146
          if (create_ref_for_key(tab->join, tab, keyuse, 
15147
                                 tab->join->const_table_map))
15148
            DBUG_RETURN(0);
15149
	}
15150
	else
15151
	{
15152
          /*
15153
            The range optimizer constructed QUICK_RANGE for ref_key, and
15154
            we want to use instead new_ref_key as the index. We can't
15155
            just change the index of the quick select, because this may
15156
            result in an incosistent QUICK_SELECT object. Below we
15157
            create a new QUICK_SELECT from scratch so that all its
15158
            parameres are set correctly by the range optimizer.
15159
           */
15160
          key_map new_ref_key_map;
15161
          new_ref_key_map.clear_all();  // Force the creation of quick select
15162
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
15163
15164
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
15165
                                        (tab->join->select_options &
15166
                                         OPTION_FOUND_ROWS) ?
15167
                                        HA_POS_ERROR :
15168
                                        tab->join->unit->select_limit_cnt,0,
15169
                                        TRUE) <=
15170
              0)
15171
            DBUG_RETURN(0);
15172
	}
15173
        ref_key= new_ref_key;
15174
      }
15175
    }
15176
    /* Check if we get the rows in requested sorted order by using the key */
15177
    if (usable_keys.is_set(ref_key) &&
15178
        (order_direction= test_if_order_by_key(order,table,ref_key,
15179
					       &used_key_parts)))
15180
      goto check_reverse_order;
15181
  }
15182
  {
15183
    /*
15184
      Check whether there is an index compatible with the given order
15185
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
15186
      or a table scan.
15187
      It may be the case if ORDER/GROUP BY is used with LIMIT.
15188
    */
15189
    uint nr;
15190
    key_map keys;
15191
    uint best_key_parts= 0;
15192
    int best_key_direction= 0;
15193
    ha_rows best_records= 0;
15194
    double read_time;
15195
    int best_key= -1;
15196
    bool is_best_covering= FALSE;
15197
    double fanout= 1;
15198
    JOIN *join= tab->join;
15199
    uint tablenr= tab - join->join_tab;
15200
    ha_rows table_records= table->file->stats.records;
15201
    bool group= join->group && order == join->group_list;
15202
15203
    /*
15204
      If not used with LIMIT, only use keys if the whole query can be
15205
      resolved with a key;  This is because filesort() is usually faster than
15206
      retrieving all rows through an index.
15207
    */
15208
    if (select_limit >= table_records)
15209
    {
15210
      /* 
15211
        filesort() and join cache are usually faster than reading in 
15212
        index order and not using join cache
15213
        */
15214
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
15215
        DBUG_RETURN(0);
15216
      keys= *table->file->keys_to_use_for_scanning();
15217
      keys.merge(table->covering_keys);
15218
15219
      /*
15220
	We are adding here also the index specified in FORCE INDEX clause, 
15221
	if any.
15222
        This is to allow users to use index in ORDER BY.
15223
      */
15224
      if (table->force_index) 
15225
	keys.merge(group ? table->keys_in_use_for_group_by :
15226
                           table->keys_in_use_for_order_by);
15227
      keys.intersect(usable_keys);
15228
    }
15229
    else
15230
      keys= usable_keys;
15231
15232
    read_time= join->best_positions[tablenr].read_time;
15233
    for (uint i= tablenr+1; i < join->tables; i++)
15234
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
15235
15236
    for (nr=0; nr < table->s->keys ; nr++)
15237
    {
15238
      int direction;
15239
      if (keys.is_set(nr) &&
15240
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
15241
      {
15242
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
15243
	
15244
        /* 
15245
          Don't use an index scan with ORDER BY without limit.
15246
          For GROUP BY without limit always use index scan
15247
          if there is a suitable index. 
15248
          Why we hold to this asymmetry hardly can be explained
15249
          rationally. It's easy to demonstrate that using
15250
          temporary table + filesort could be cheaper for grouping
15251
          queries too.
15252
	*/ 
15253
        if (is_covering ||
15254
            select_limit != HA_POS_ERROR || 
15255
            (ref_key < 0 && (group || table->force_index)))
15256
        { 
15257
          double rec_per_key;
15258
          double index_scan_time;
15259
          KEY *keyinfo= tab->table->key_info+nr;
15260
          if (select_limit == HA_POS_ERROR)
15261
            select_limit= table_records;
15262
          if (group)
15263
          {
15264
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
15265
            set_if_bigger(rec_per_key, 1);
15266
            /*
15267
              With a grouping query each group containing on average
15268
              rec_per_key records produces only one row that will
15269
              be included into the result set.
15270
	    */  
15271
            if (select_limit > table_records/rec_per_key)
15272
                select_limit= table_records;
15273
            else
15274
              select_limit= (ha_rows) (select_limit*rec_per_key);
15275
          }
15276
          /* 
15277
            If tab=tk is not the last joined table tn then to get first
15278
            L records from the result set we can expect to retrieve
15279
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
15280
            rows in the record set on average will match each row tk.
15281
            Usually our estimates for fanouts are too pessimistic.
15282
            So the estimate for L/fanout(tk,tn) will be too optimistic
15283
            and as result we'll choose an index scan when using ref/range
15284
            access + filesort will be cheaper.
15285
	  */
15286
          select_limit= (ha_rows) (select_limit < fanout ?
15287
                                   1 : select_limit/fanout);
15288
          /*
15289
            We assume that each of the tested indexes is not correlated
15290
            with ref_key. Thus, to select first N records we have to scan
15291
            N/selectivity(ref_key) index entries. 
15292
            selectivity(ref_key) = #scanned_records/#table_records =
15293
            table->quick_condition_rows/table_records.
15294
            In any case we can't select more than #table_records.
15295
            N/(table->quick_condition_rows/table_records) > table_records 
15296
            <=> N > table->quick_condition_rows.
15297
          */ 
15298
          if (select_limit > table->quick_condition_rows)
15299
            select_limit= table_records;
15300
          else
15301
            select_limit= (ha_rows) (select_limit *
15302
                                     (double) table_records /
15303
                                      table->quick_condition_rows);
15304
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
15305
          set_if_bigger(rec_per_key, 1);
15306
          /*
15307
            Here we take into account the fact that rows are
15308
            accessed in sequences rec_per_key records in each.
15309
            Rows in such a sequence are supposed to be ordered
15310
            by rowid/primary key. When reading the data
15311
            in a sequence we'll touch not more pages than the
15312
            table file contains.
15313
            TODO. Use the formula for a disk sweep sequential access
15314
            to calculate the cost of accessing data rows for one 
15315
            index entry.
15316
	  */
15317
          index_scan_time= select_limit/rec_per_key *
15318
	                   min(rec_per_key, table->file->scan_time());
15319
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
15320
              index_scan_time < read_time)
15321
          {
15322
            ha_rows quick_records= table_records;
15323
            if (is_best_covering && !is_covering)
15324
              continue;
15325
            if (table->quick_keys.is_set(nr))
15326
              quick_records= table->quick_rows[nr];
15327
            if (best_key < 0 ||
15328
                (select_limit <= min(quick_records,best_records) ?
15329
                 keyinfo->key_parts < best_key_parts :
15330
                 quick_records < best_records))
15331
            {
15332
              best_key= nr;
15333
              best_key_parts= keyinfo->key_parts;
15334
              best_records= quick_records;
15335
              is_best_covering= is_covering;
15336
              best_key_direction= direction; 
15337
            }
15338
          }   
15339
	}      
15340
      }
15341
    }
15342
    if (best_key >= 0)
15343
    {
15344
      bool quick_created= FALSE;
15345
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
15346
      {
15347
        key_map map;
15348
        map.clear_all();       // Force the creation of quick select
15349
        map.set_bit(best_key); // only best_key.
15350
        quick_created=         
15351
          select->test_quick_select(join->thd, map, 0,
15352
                                    join->select_options & OPTION_FOUND_ROWS ?
15353
                                    HA_POS_ERROR :
15354
                                    join->unit->select_limit_cnt,
15355
                                    TRUE, FALSE) > 0;
15356
      }
15357
      if (!no_changes)
15358
      {
15359
        if (!quick_created)
15360
	{
15361
          tab->index= best_key;
15362
          tab->read_first_record= best_key_direction > 0 ?
15363
                                  join_read_first:join_read_last;
15364
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
15365
          if (select && select->quick)
15366
          {
15367
            delete select->quick;
15368
            select->quick= 0;
15369
          }
15370
          if (table->covering_keys.is_set(best_key))
15371
          {
15372
            table->key_read=1;
15373
            table->file->extra(HA_EXTRA_KEYREAD);
15374
          }
15375
          table->file->ha_index_or_rnd_end();
15376
          if (join->select_options & SELECT_DESCRIBE)
15377
          {
15378
            tab->ref.key= -1;
15379
            tab->ref.key_parts= 0;
15380
            if (select_limit < table_records) 
15381
              tab->limit= select_limit;
15382
          }
15383
        }
15384
        else if (tab->type != JT_ALL)
15385
        {
15386
          /*
15387
            We're about to use a quick access to the table.
15388
            We need to change the access method so as the quick access
15389
            method is actually used.
15390
          */
15391
          DBUG_ASSERT(tab->select->quick);
15392
          tab->type=JT_ALL;
15393
          tab->use_quick=1;
15394
          tab->ref.key= -1;
15395
          tab->ref.key_parts=0;		// Don't use ref key.
15396
          tab->read_first_record= join_init_read_record;
15397
          /*
15398
            TODO: update the number of records in join->best_positions[tablenr]
15399
          */
15400
        }
15401
      }
15402
      used_key_parts= best_key_parts;
15403
      order_direction= best_key_direction;
15404
    }
15405
    else
15406
      DBUG_RETURN(0); 
15407
  } 
15408
15409
check_reverse_order:                  
15410
  if (order_direction == -1)		// If ORDER BY ... DESC
15411
  {
15412
    if (select && select->quick)
15413
    {
15414
      /*
15415
	Don't reverse the sort order, if it's already done.
15416
        (In some cases test_if_order_by_key() can be called multiple times
15417
      */
15418
      if (!select->quick->reverse_sorted())
15419
      {
15420
        QUICK_SELECT_DESC *tmp;
15421
        bool error= FALSE;
15422
        int quick_type= select->quick->get_type();
15423
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
15424
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
15425
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
15426
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
15427
        {
15428
          tab->limit= 0;
15429
          select->quick= save_quick;
15430
          DBUG_RETURN(0);                   // Use filesort
15431
        }
15432
            
15433
        /* ORDER BY range_key DESC */
15434
	tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
15435
                                    used_key_parts, &error);
15436
	if (!tmp || error)
15437
	{
15438
	  delete tmp;
15439
          select->quick= save_quick;
15440
          tab->limit= 0;
15441
	  DBUG_RETURN(0);		// Reverse sort not supported
15442
	}
15443
	select->quick=tmp;
15444
      }
15445
    }
15446
    else if (tab->type != JT_NEXT && 
15447
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
15448
    {
15449
      /*
15450
	SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
15451
15452
	Use a traversal function that starts by reading the last row
15453
	with key part (A) and then traverse the index backwards.
15454
      */
15455
      tab->read_first_record= join_read_last_key;
15456
      tab->read_record.read_record= join_read_prev_same;
15457
    }
15458
  }
15459
  else if (select && select->quick)
15460
    select->quick->sorted= 1;
15461
  DBUG_RETURN(1);
15462
}
15463
15464
15465
/*
15466
  If not selecting by given key, create an index how records should be read
15467
15468
  SYNOPSIS
15469
   create_sort_index()
15470
     thd		Thread handler
15471
     tab		Table to sort (in join structure)
15472
     order		How table should be sorted
15473
     filesort_limit	Max number of rows that needs to be sorted
15474
     select_limit	Max number of rows in final output
15475
		        Used to decide if we should use index or not
15476
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
15477
                        Used to decide if we should use index or not     
15478
15479
15480
  IMPLEMENTATION
15481
   - If there is an index that can be used, 'tab' is modified to use
15482
     this index.
15483
   - If no index, create with filesort() an index file that can be used to
15484
     retrieve rows in order (should be done with 'read_record').
15485
     The sorted data is stored in tab->table and will be freed when calling
15486
     free_io_cache(tab->table).
15487
15488
  RETURN VALUES
15489
    0		ok
15490
    -1		Some fatal error
15491
    1		No records
15492
*/
15493
15494
static int
15495
create_sort_index(THD *thd, JOIN *join, ORDER *order,
15496
		  ha_rows filesort_limit, ha_rows select_limit,
15497
                  bool is_order_by)
15498
{
15499
  uint length= 0;
15500
  ha_rows examined_rows;
15501
  TABLE *table;
15502
  SQL_SELECT *select;
15503
  JOIN_TAB *tab;
15504
  DBUG_ENTER("create_sort_index");
15505
15506
  if (join->tables == join->const_tables)
15507
    DBUG_RETURN(0);				// One row, no need to sort
15508
  tab=    join->join_tab + join->const_tables;
15509
  table=  tab->table;
15510
  select= tab->select;
15511
15512
  /*
15513
    When there is SQL_BIG_RESULT do not sort using index for GROUP BY,
15514
    and thus force sorting on disk unless a group min-max optimization
15515
    is going to be used as it is applied now only for one table queries
15516
    with covering indexes.
15517
  */
15518
  if ((order != join->group_list || 
15519
       !(join->select_options & SELECT_BIG_RESULT) ||
15520
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
15521
      test_if_skip_sort_order(tab,order,select_limit,0, 
15522
                              is_order_by ?  &table->keys_in_use_for_order_by :
15523
                              &table->keys_in_use_for_group_by))
15524
    DBUG_RETURN(0);
15525
  for (ORDER *ord= join->order; ord; ord= ord->next)
15526
    length++;
15527
  if (!(join->sortorder= 
15528
        make_unireg_sortorder(order, &length, join->sortorder)))
15529
    goto err;				/* purecov: inspected */
15530
15531
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
15532
                                             MYF(MY_WME | MY_ZEROFILL));
15533
  table->status=0;				// May be wrong if quick_select
15534
15535
  // If table has a range, move it to select
15536
  if (select && !select->quick && tab->ref.key >= 0)
15537
  {
15538
    if (tab->quick)
15539
    {
15540
      select->quick=tab->quick;
15541
      tab->quick=0;
15542
      /* 
15543
        We can only use 'Only index' if quick key is same as ref_key
15544
        and in index_merge 'Only index' cannot be used
15545
      */
15546
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
15547
      {
15548
	table->key_read=0;
15549
	table->file->extra(HA_EXTRA_NO_KEYREAD);
15550
      }
15551
    }
15552
    else
15553
    {
15554
      /*
15555
	We have a ref on a const;  Change this to a range that filesort
15556
	can use.
15557
	For impossible ranges (like when doing a lookup on NULL on a NOT NULL
15558
	field, quick will contain an empty record set.
15559
      */
15560
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
15561
                                                     tab->found_records))))
15562
	goto err;
15563
    }
15564
  }
15565
15566
  /* Fill schema tables with data before filesort if it's necessary */
15567
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
15568
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
15569
    goto err;
15570
15571
  if (table->s->tmp_table)
15572
    table->file->info(HA_STATUS_VARIABLE);	// Get record count
15573
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
15574
                                     select, filesort_limit, 0,
15575
                                     &examined_rows);
15576
  tab->records= table->sort.found_records;	// For SQL_CALC_ROWS
15577
  if (select)
15578
  {
15579
    select->cleanup();				// filesort did select
15580
    tab->select= 0;
15581
  }
15582
  tab->select_cond=0;
15583
  tab->last_inner= 0;
15584
  tab->first_unmatched= 0;
15585
  tab->type=JT_ALL;				// Read with normal read_record
15586
  tab->read_first_record= join_init_read_record;
15587
  tab->join->examined_rows+=examined_rows;
15588
  if (table->key_read)				// Restore if we used indexes
15589
  {
15590
    table->key_read=0;
15591
    table->file->extra(HA_EXTRA_NO_KEYREAD);
15592
  }
15593
  DBUG_RETURN(table->sort.found_records == HA_POS_ERROR);
15594
err:
15595
  DBUG_RETURN(-1);
15596
}
15597
15598
/*****************************************************************************
15599
  Remove duplicates from tmp table
15600
  This should be recoded to add a unique index to the table and remove
15601
  duplicates
15602
  Table is a locked single thread table
15603
  fields is the number of fields to check (from the end)
15604
*****************************************************************************/
15605
15606
static bool compare_record(TABLE *table, Field **ptr)
15607
{
15608
  for (; *ptr ; ptr++)
15609
  {
15610
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
15611
      return 1;
15612
  }
15613
  return 0;
15614
}
15615
15616
static bool copy_blobs(Field **ptr)
15617
{
15618
  for (; *ptr ; ptr++)
15619
  {
15620
    if ((*ptr)->flags & BLOB_FLAG)
15621
      if (((Field_blob *) (*ptr))->copy())
15622
	return 1;				// Error
15623
  }
15624
  return 0;
15625
}
15626
15627
static void free_blobs(Field **ptr)
15628
{
15629
  for (; *ptr ; ptr++)
15630
  {
15631
    if ((*ptr)->flags & BLOB_FLAG)
15632
      ((Field_blob *) (*ptr))->free();
15633
  }
15634
}
15635
15636
15637
static int
15638
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
15639
{
15640
  int error;
15641
  ulong reclength,offset;
15642
  uint field_count;
15643
  THD *thd= join->thd;
15644
  DBUG_ENTER("remove_duplicates");
15645
15646
  entry->reginfo.lock_type=TL_WRITE;
15647
15648
  /* Calculate how many saved fields there is in list */
15649
  field_count=0;
15650
  List_iterator<Item> it(fields);
15651
  Item *item;
15652
  while ((item=it++))
15653
  {
15654
    if (item->get_tmp_table_field() && ! item->const_item())
15655
      field_count++;
15656
  }
15657
15658
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
15659
  {                    // only const items with no OPTION_FOUND_ROWS
15660
    join->unit->select_limit_cnt= 1;		// Only send first row
15661
    DBUG_RETURN(0);
15662
  }
15663
  Field **first_field=entry->field+entry->s->fields - field_count;
15664
  offset= (field_count ? 
15665
           entry->field[entry->s->fields - field_count]->
15666
           offset(entry->record[0]) : 0);
15667
  reclength=entry->s->reclength-offset;
15668
15669
  free_io_cache(entry);				// Safety
15670
  entry->file->info(HA_STATUS_VARIABLE);
15671
  if (entry->s->db_type() == heap_hton ||
15672
      (!entry->s->blob_fields &&
15673
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
15674
	thd->variables.sortbuff_size)))
15675
    error=remove_dup_with_hash_index(join->thd, entry,
15676
				     field_count, first_field,
15677
				     reclength, having);
15678
  else
15679
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
15680
				  having);
15681
15682
  free_blobs(first_field);
15683
  DBUG_RETURN(error);
15684
}
15685
15686
15687
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
15688
				   ulong offset, Item *having)
15689
{
15690
  handler *file=table->file;
15691
  char *org_record,*new_record;
15692
  uchar *record;
15693
  int error;
15694
  ulong reclength= table->s->reclength-offset;
15695
  DBUG_ENTER("remove_dup_with_compare");
15696
15697
  org_record=(char*) (record=table->record[0])+offset;
15698
  new_record=(char*) table->record[1]+offset;
15699
15700
  file->ha_rnd_init(1);
15701
  error=file->rnd_next(record);
15702
  for (;;)
15703
  {
15704
    if (thd->killed)
15705
    {
15706
      thd->send_kill_message();
15707
      error=0;
15708
      goto err;
15709
    }
15710
    if (error)
15711
    {
15712
      if (error == HA_ERR_RECORD_DELETED)
15713
	continue;
15714
      if (error == HA_ERR_END_OF_FILE)
15715
	break;
15716
      goto err;
15717
    }
15718
    if (having && !having->val_int())
15719
    {
15720
      if ((error=file->ha_delete_row(record)))
15721
	goto err;
15722
      error=file->rnd_next(record);
15723
      continue;
15724
    }
15725
    if (copy_blobs(first_field))
15726
    {
15727
      my_message(ER_OUTOFMEMORY, ER(ER_OUTOFMEMORY), MYF(0));
15728
      error=0;
15729
      goto err;
15730
    }
15731
    memcpy(new_record,org_record,reclength);
15732
15733
    /* Read through rest of file and mark duplicated rows deleted */
15734
    bool found=0;
15735
    for (;;)
15736
    {
15737
      if ((error=file->rnd_next(record)))
15738
      {
15739
	if (error == HA_ERR_RECORD_DELETED)
15740
	  continue;
15741
	if (error == HA_ERR_END_OF_FILE)
15742
	  break;
15743
	goto err;
15744
      }
15745
      if (compare_record(table, first_field) == 0)
15746
      {
15747
	if ((error=file->ha_delete_row(record)))
15748
	  goto err;
15749
      }
15750
      else if (!found)
15751
      {
15752
	found=1;
15753
	file->position(record);	// Remember position
15754
      }
15755
    }
15756
    if (!found)
15757
      break;					// End of file
15758
    /* Restart search on next row */
15759
    error=file->restart_rnd_next(record,file->ref);
15760
  }
15761
15762
  file->extra(HA_EXTRA_NO_CACHE);
15763
  DBUG_RETURN(0);
15764
err:
15765
  file->extra(HA_EXTRA_NO_CACHE);
15766
  if (error)
15767
    file->print_error(error,MYF(0));
15768
  DBUG_RETURN(1);
15769
}
15770
15771
15772
/**
15773
  Generate a hash index for each row to quickly find duplicate rows.
15774
15775
  @note
15776
    Note that this will not work on tables with blobs!
15777
*/
15778
15779
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
15780
				      uint field_count,
15781
				      Field **first_field,
15782
				      ulong key_length,
15783
				      Item *having)
15784
{
15785
  uchar *key_buffer, *key_pos, *record=table->record[0];
15786
  int error;
15787
  handler *file= table->file;
15788
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
15789
  uint *field_lengths,*field_length;
15790
  HASH hash;
15791
  DBUG_ENTER("remove_dup_with_hash_index");
15792
15793
  if (!my_multi_malloc(MYF(MY_WME),
15794
		       &key_buffer,
15795
		       (uint) ((key_length + extra_length) *
15796
			       (long) file->stats.records),
15797
		       &field_lengths,
15798
		       (uint) (field_count*sizeof(*field_lengths)),
15799
		       NullS))
15800
    DBUG_RETURN(1);
15801
15802
  {
15803
    Field **ptr;
15804
    ulong total_length= 0;
15805
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
15806
    {
15807
      uint length= (*ptr)->sort_length();
15808
      (*field_length++)= length;
15809
      total_length+= length;
15810
    }
15811
    DBUG_PRINT("info",("field_count: %u  key_length: %lu  total_length: %lu",
15812
                       field_count, key_length, total_length));
15813
    DBUG_ASSERT(total_length <= key_length);
15814
    key_length= total_length;
15815
    extra_length= ALIGN_SIZE(key_length)-key_length;
15816
  }
15817
15818
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
15819
		key_length, (hash_get_key) 0, 0, 0))
15820
  {
15821
    my_free((char*) key_buffer,MYF(0));
15822
    DBUG_RETURN(1);
15823
  }
15824
15825
  file->ha_rnd_init(1);
15826
  key_pos=key_buffer;
15827
  for (;;)
15828
  {
15829
    uchar *org_key_pos;
15830
    if (thd->killed)
15831
    {
15832
      thd->send_kill_message();
15833
      error=0;
15834
      goto err;
15835
    }
15836
    if ((error=file->rnd_next(record)))
15837
    {
15838
      if (error == HA_ERR_RECORD_DELETED)
15839
	continue;
15840
      if (error == HA_ERR_END_OF_FILE)
15841
	break;
15842
      goto err;
15843
    }
15844
    if (having && !having->val_int())
15845
    {
15846
      if ((error=file->ha_delete_row(record)))
15847
	goto err;
15848
      continue;
15849
    }
15850
15851
    /* copy fields to key buffer */
15852
    org_key_pos= key_pos;
15853
    field_length=field_lengths;
15854
    for (Field **ptr= first_field ; *ptr ; ptr++)
15855
    {
15856
      (*ptr)->sort_string(key_pos,*field_length);
15857
      key_pos+= *field_length++;
15858
    }
15859
    /* Check if it exists before */
15860
    if (hash_search(&hash, org_key_pos, key_length))
15861
    {
15862
      /* Duplicated found ; Remove the row */
15863
      if ((error=file->ha_delete_row(record)))
15864
	goto err;
15865
    }
15866
    else
15867
      (void) my_hash_insert(&hash, org_key_pos);
15868
    key_pos+=extra_length;
15869
  }
15870
  my_free((char*) key_buffer,MYF(0));
15871
  hash_free(&hash);
15872
  file->extra(HA_EXTRA_NO_CACHE);
15873
  (void) file->ha_rnd_end();
15874
  DBUG_RETURN(0);
15875
15876
err:
15877
  my_free((char*) key_buffer,MYF(0));
15878
  hash_free(&hash);
15879
  file->extra(HA_EXTRA_NO_CACHE);
15880
  (void) file->ha_rnd_end();
15881
  if (error)
15882
    file->print_error(error,MYF(0));
15883
  DBUG_RETURN(1);
15884
}
15885
15886
15887
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
15888
                                  SORT_FIELD *sortorder)
15889
{
15890
  uint count;
15891
  SORT_FIELD *sort,*pos;
15892
  DBUG_ENTER("make_unireg_sortorder");
15893
15894
  count=0;
15895
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
15896
    count++;
15897
  if (!sortorder)
15898
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
15899
                                       (max(count, *length) + 1));
15900
  pos= sort= sortorder;
15901
15902
  if (!pos)
15903
    return 0;
15904
15905
  for (;order;order=order->next,pos++)
15906
  {
15907
    Item *item= order->item[0]->real_item();
15908
    pos->field= 0; pos->item= 0;
15909
    if (item->type() == Item::FIELD_ITEM)
15910
      pos->field= ((Item_field*) item)->field;
15911
    else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
15912
      pos->field= ((Item_sum*) item)->get_tmp_table_field();
15913
    else if (item->type() == Item::COPY_STR_ITEM)
15914
    {						// Blob patch
15915
      pos->item= ((Item_copy_string*) item)->item;
15916
    }
15917
    else
15918
      pos->item= *order->item;
15919
    pos->reverse=! order->asc;
15920
  }
15921
  *length=count;
15922
  DBUG_RETURN(sort);
15923
}
15924
15925
15926
/*****************************************************************************
15927
  Fill join cache with packed records
15928
  Records are stored in tab->cache.buffer and last record in
15929
  last record is stored with pointers to blobs to support very big
15930
  records
15931
******************************************************************************/
15932
15933
static int
15934
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
15935
{
15936
  register unsigned int i;
15937
  unsigned int length, blobs;
15938
  size_t size;
15939
  CACHE_FIELD *copy,**blob_ptr;
15940
  JOIN_CACHE  *cache;
15941
  JOIN_TAB *join_tab;
15942
  DBUG_ENTER("join_init_cache");
15943
15944
  cache= &tables[table_count].cache;
15945
  cache->fields=blobs=0;
15946
15947
  join_tab=tables;
15948
  for (i=0 ; i < table_count ; i++,join_tab++)
15949
  {
15950
    if (!join_tab->used_fieldlength)		/* Not calced yet */
15951
      calc_used_field_length(thd, join_tab);
15952
    cache->fields+=join_tab->used_fields;
15953
    blobs+=join_tab->used_blobs;
15954
15955
    /* SemiJoinDuplicateElimination: reserve space for rowid */
15956
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15957
    {
15958
      cache->fields++;
15959
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
15960
    }
15961
  }
15962
  if (!(cache->field=(CACHE_FIELD*)
15963
	sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
15964
15965
		  sizeof(CACHE_FIELD*))))
15966
  {
15967
    my_free((uchar*) cache->buff,MYF(0));		/* purecov: inspected */
15968
    cache->buff=0;				/* purecov: inspected */
15969
    DBUG_RETURN(1);				/* purecov: inspected */
15970
  }
15971
  copy=cache->field;
15972
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
15973
    (cache->field+cache->fields+table_count*2);
15974
15975
  length=0;
15976
  for (i=0 ; i < table_count ; i++)
15977
  {
15978
    uint null_fields=0,used_fields;
15979
    Field **f_ptr,*field;
15980
    MY_BITMAP *read_set= tables[i].table->read_set;
15981
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
15982
	 used_fields ;
15983
	 f_ptr++)
15984
    {
15985
      field= *f_ptr;
15986
      if (bitmap_is_set(read_set, field->field_index))
15987
      {
15988
	used_fields--;
15989
	length+=field->fill_cache_field(copy);
15990
	if (copy->blob_field)
15991
	  (*blob_ptr++)=copy;
15992
	if (field->maybe_null())
15993
	  null_fields++;
15994
        copy->get_rowid= NULL;
15995
	copy++;
15996
      }
15997
    }
15998
    /* Copy null bits from table */
15999
    if (null_fields && tables[i].table->s->null_fields)
16000
    {						/* must copy null bits */
16001
      copy->str= tables[i].table->null_flags;
16002
      copy->length= tables[i].table->s->null_bytes;
16003
      copy->strip=0;
16004
      copy->blob_field=0;
16005
      copy->get_rowid= NULL;
16006
      length+=copy->length;
16007
      copy++;
16008
      cache->fields++;
16009
    }
16010
    /* If outer join table, copy null_row flag */
16011
    if (tables[i].table->maybe_null)
16012
    {
16013
      copy->str= (uchar*) &tables[i].table->null_row;
16014
      copy->length=sizeof(tables[i].table->null_row);
16015
      copy->strip=0;
16016
      copy->blob_field=0;
16017
      copy->get_rowid= NULL;
16018
      length+=copy->length;
16019
      copy++;
16020
      cache->fields++;
16021
    }
16022
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
16023
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
16024
    {
16025
      copy->str= tables[i].table->file->ref;
16026
      copy->length= tables[i].table->file->ref_length;
16027
      copy->strip=0;
16028
      copy->blob_field=0;
16029
      copy->get_rowid= NULL;
16030
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
16031
      {
16032
        /* We will need to call h->position(): */
16033
        copy->get_rowid= tables[i].table;
16034
        /* And those after us won't have to: */
16035
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
16036
      }
16037
      copy++;
16038
    }
16039
  }
16040
16041
  cache->length=length+blobs*sizeof(char*);
16042
  cache->blobs=blobs;
16043
  *blob_ptr=0;					/* End sequentel */
16044
  size=max(thd->variables.join_buff_size, cache->length);
16045
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
16046
    DBUG_RETURN(1);				/* Don't use cache */ /* purecov: inspected */
16047
  cache->end=cache->buff+size;
16048
  reset_cache_write(cache);
16049
  DBUG_RETURN(0);
16050
}
16051
16052
16053
static ulong
16054
used_blob_length(CACHE_FIELD **ptr)
16055
{
16056
  uint length,blob_length;
16057
  for (length=0 ; *ptr ; ptr++)
16058
  {
16059
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
16060
    length+=blob_length;
16061
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
16062
  }
16063
  return length;
16064
}
16065
16066
16067
static bool
16068
store_record_in_cache(JOIN_CACHE *cache)
16069
{
16070
  uint length;
16071
  uchar *pos;
16072
  CACHE_FIELD *copy,*end_field;
16073
  bool last_record;
16074
16075
  pos=cache->pos;
16076
  end_field=cache->field+cache->fields;
16077
16078
  length=cache->length;
16079
  if (cache->blobs)
16080
    length+=used_blob_length(cache->blob_ptr);
16081
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
16082
    cache->ptr_record=cache->records;
16083
  /*
16084
    There is room in cache. Put record there
16085
  */
16086
  cache->records++;
16087
  for (copy=cache->field ; copy < end_field; copy++)
16088
  {
16089
    if (copy->blob_field)
16090
    {
16091
      if (last_record)
16092
      {
16093
	copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
16094
				    copy->blob_field->charset());
16095
	pos+=copy->length+sizeof(char*);
16096
      }
16097
      else
16098
      {
16099
	copy->blob_field->get_image(pos, copy->length, // blob length
16100
				    copy->blob_field->charset());
16101
	memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
16102
	pos+=copy->length+copy->blob_length;
16103
      }
16104
    }
16105
    else
16106
    {
16107
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
16108
      if (copy->get_rowid)
16109
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
16110
16111
      if (copy->strip)
16112
      {
16113
	uchar *str,*end;
16114
	for (str=copy->str,end= str+copy->length;
16115
	     end > str && end[-1] == ' ' ;
16116
	     end--) ;
16117
	length=(uint) (end-str);
16118
	memcpy(pos+2, str, length);
16119
        int2store(pos, length);
16120
	pos+= length+2;
16121
      }
16122
      else
16123
      {
16124
	memcpy(pos,copy->str,copy->length);
16125
	pos+=copy->length;
16126
      }
16127
    }
16128
  }
16129
  cache->pos=pos;
16130
  return last_record || (size_t) (cache->end - pos) < cache->length;
16131
}
16132
16133
16134
static void
16135
reset_cache_read(JOIN_CACHE *cache)
16136
{
16137
  cache->record_nr=0;
16138
  cache->pos=cache->buff;
16139
}
16140
16141
16142
static void reset_cache_write(JOIN_CACHE *cache)
16143
{
16144
  reset_cache_read(cache);
16145
  cache->records= 0;
16146
  cache->ptr_record= (uint) ~0;
16147
}
16148
16149
16150
static void
16151
read_cached_record(JOIN_TAB *tab)
16152
{
16153
  uchar *pos;
16154
  uint length;
16155
  bool last_record;
16156
  CACHE_FIELD *copy,*end_field;
16157
16158
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
16159
  pos=tab->cache.pos;
16160
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
16161
       copy < end_field;
16162
       copy++)
16163
  {
16164
    if (copy->blob_field)
16165
    {
16166
      if (last_record)
16167
      {
16168
	copy->blob_field->set_image(pos, copy->length+sizeof(char*),
16169
				    copy->blob_field->charset());
16170
	pos+=copy->length+sizeof(char*);
16171
      }
16172
      else
16173
      {
16174
	copy->blob_field->set_ptr(pos, pos+copy->length);
16175
	pos+=copy->length+copy->blob_field->get_length();
16176
      }
16177
    }
16178
    else
16179
    {
16180
      if (copy->strip)
16181
      {
16182
        length= uint2korr(pos);
16183
	memcpy(copy->str, pos+2, length);
16184
	memset(copy->str+length, ' ', copy->length-length);
16185
	pos+= 2 + length;
16186
      }
16187
      else
16188
      {
16189
	memcpy(copy->str,pos,copy->length);
16190
	pos+=copy->length;
16191
      }
16192
    }
16193
  }
16194
  tab->cache.pos=pos;
16195
  return;
16196
}
16197
16198
16199
/*
16200
  eq_ref: Create the lookup key and check if it is the same as saved key
16201
16202
  SYNOPSIS
16203
    cmp_buffer_with_ref()
16204
      tab  Join tab of the accessed table
16205
 
16206
  DESCRIPTION 
16207
    Used by eq_ref access method: create the index lookup key and check if 
16208
    we've used this key at previous lookup (If yes, we don't need to repeat
16209
    the lookup - the record has been already fetched)
16210
16211
  RETURN 
16212
    TRUE   No cached record for the key, or failed to create the key (due to
16213
           out-of-domain error)
16214
    FALSE  The created key is the same as the previous one (and the record 
16215
           is already in table->record)
16216
*/
16217
16218
static bool
16219
cmp_buffer_with_ref(JOIN_TAB *tab)
16220
{
16221
  bool no_prev_key;
16222
  if (!tab->ref.disable_cache)
16223
  {
16224
    if (!(no_prev_key= tab->ref.key_err))
16225
    {
16226
      /* Previous access found a row. Copy its key */
16227
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
16228
    }
16229
  }
16230
  else 
16231
    no_prev_key= TRUE;
16232
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
16233
                                            &tab->ref)) ||
16234
      no_prev_key)
16235
    return 1;
16236
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
16237
    != 0;
16238
}
16239
16240
16241
bool
16242
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
16243
{
16244
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
16245
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
16246
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
16247
  bool result= 0;
16248
16249
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
16250
  {
16251
    if ((*copy)->copy() & 1)
16252
    {
16253
      result= 1;
16254
      break;
16255
    }
16256
  }
16257
  thd->count_cuted_fields= save_count_cuted_fields;
16258
  dbug_tmp_restore_column_map(table->write_set, old_map);
16259
  return result;
16260
}
16261
16262
16263
/*****************************************************************************
16264
  Group and order functions
16265
*****************************************************************************/
16266
16267
/**
16268
  Resolve an ORDER BY or GROUP BY column reference.
16269
16270
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
16271
  BY clause, find the actual column it represents. If the column being
16272
  resolved is from the GROUP BY clause, the procedure searches the SELECT
16273
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
16274
  the ORDER BY clause, only the SELECT list is being searched.
16275
16276
  If 'order' is resolved to an Item, then order->item is set to the found
16277
  Item. If there is no item for the found column (that is, it was resolved
16278
  into a table field), order->item is 'fixed' and is added to all_fields and
16279
  ref_pointer_array.
16280
16281
  ref_pointer_array and all_fields are updated.
16282
16283
  @param[in] thd		     Pointer to current thread structure
16284
  @param[in,out] ref_pointer_array  All select, group and order by fields
16285
  @param[in] tables                 List of tables to search in (usually
16286
    FROM clause)
16287
  @param[in] order                  Column reference to be resolved
16288
  @param[in] fields                 List of fields to search in (usually
16289
    SELECT list)
16290
  @param[in,out] all_fields         All select, group and order by fields
16291
  @param[in] is_group_field         True if order is a GROUP field, false if
16292
    ORDER by field
16293
16294
  @retval
16295
    FALSE if OK
16296
  @retval
16297
    TRUE  if error occurred
16298
*/
16299
16300
static bool
16301
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16302
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
16303
                   bool is_group_field)
16304
{
16305
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
16306
  Item::Type order_item_type;
16307
  Item **select_item; /* The corresponding item from the SELECT clause. */
16308
  Field *from_field;  /* The corresponding field from the FROM clause. */
16309
  uint counter;
16310
  enum_resolution_type resolution;
16311
16312
  /*
16313
    Local SP variables may be int but are expressions, not positions.
16314
    (And they can't be used before fix_fields is called for them).
16315
  */
16316
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
16317
  {						/* Order by position */
16318
    uint count= (uint) order_item->val_int();
16319
    if (!count || count > fields.elements)
16320
    {
16321
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
16322
               order_item->full_name(), thd->where);
16323
      return TRUE;
16324
    }
16325
    order->item= ref_pointer_array + count - 1;
16326
    order->in_field_list= 1;
16327
    order->counter= count;
16328
    order->counter_used= 1;
16329
    return FALSE;
16330
  }
16331
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
16332
  select_item= find_item_in_list(order_item, fields, &counter,
16333
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
16334
  if (!select_item)
16335
    return TRUE; /* The item is not unique, or some other error occured. */
16336
16337
16338
  /* Check whether the resolved field is not ambiguos. */
16339
  if (select_item != not_found_item)
16340
  {
16341
    Item *view_ref= NULL;
16342
    /*
16343
      If we have found field not by its alias in select list but by its
16344
      original field name, we should additionaly check if we have conflict
16345
      for this name (in case if we would perform lookup in all tables).
16346
    */
16347
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
16348
        order_item->fix_fields(thd, order->item))
16349
      return TRUE;
16350
16351
    /* Lookup the current GROUP field in the FROM clause. */
16352
    order_item_type= order_item->type();
16353
    from_field= (Field*) not_found_field;
16354
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
16355
        order_item_type == Item::REF_ITEM)
16356
    {
16357
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
16358
                                       NULL, &view_ref, IGNORE_ERRORS, TRUE,
16359
                                       FALSE);
16360
      if (!from_field)
16361
        from_field= (Field*) not_found_field;
16362
    }
16363
16364
    if (from_field == not_found_field ||
16365
        (from_field != view_ref_found ?
16366
         /* it is field of base table => check that fields are same */
16367
         ((*select_item)->type() == Item::FIELD_ITEM &&
16368
          ((Item_field*) (*select_item))->field->eq(from_field)) :
16369
         /*
16370
           in is field of view table => check that references on translation
16371
           table are same
16372
         */
16373
         ((*select_item)->type() == Item::REF_ITEM &&
16374
          view_ref->type() == Item::REF_ITEM &&
16375
          ((Item_ref *) (*select_item))->ref ==
16376
          ((Item_ref *) view_ref)->ref)))
16377
    {
16378
      /*
16379
        If there is no such field in the FROM clause, or it is the same field
16380
        as the one found in the SELECT clause, then use the Item created for
16381
        the SELECT field. As a result if there was a derived field that
16382
        'shadowed' a table field with the same name, the table field will be
16383
        chosen over the derived field.
16384
      */
16385
      order->item= ref_pointer_array + counter;
16386
      order->in_field_list=1;
16387
      return FALSE;
16388
    }
16389
    else
16390
    {
16391
      /*
16392
        There is a field with the same name in the FROM clause. This
16393
        is the field that will be chosen. In this case we issue a
16394
        warning so the user knows that the field from the FROM clause
16395
        overshadows the column reference from the SELECT list.
16396
      */
16397
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
16398
                          ER(ER_NON_UNIQ_ERROR),
16399
                          ((Item_ident*) order_item)->field_name,
16400
                          current_thd->where);
16401
    }
16402
  }
16403
16404
  order->in_field_list=0;
16405
  /*
16406
    The call to order_item->fix_fields() means that here we resolve
16407
    'order_item' to a column from a table in the list 'tables', or to
16408
    a column in some outer query. Exactly because of the second case
16409
    we come to this point even if (select_item == not_found_item),
16410
    inspite of that fix_fields() calls find_item_in_list() one more
16411
    time.
16412
16413
    We check order_item->fixed because Item_func_group_concat can put
16414
    arguments for which fix_fields already was called.
16415
  */
16416
  if (!order_item->fixed &&
16417
      (order_item->fix_fields(thd, order->item) ||
16418
       (order_item= *order->item)->check_cols(1) ||
16419
       thd->is_fatal_error))
16420
    return TRUE; /* Wrong field. */
16421
16422
  uint el= all_fields.elements;
16423
  all_fields.push_front(order_item); /* Add new field to field list. */
16424
  ref_pointer_array[el]= order_item;
16425
  order->item= ref_pointer_array + el;
16426
  return FALSE;
16427
}
16428
16429
16430
/**
16431
  Change order to point at item in select list.
16432
16433
  If item isn't a number and doesn't exits in the select list, add it the
16434
  the field list.
16435
*/
16436
16437
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16438
		List<Item> &fields, List<Item> &all_fields, ORDER *order)
16439
{
16440
  thd->where="order clause";
16441
  for (; order; order=order->next)
16442
  {
16443
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
16444
			   all_fields, FALSE))
16445
      return 1;
16446
  }
16447
  return 0;
16448
}
16449
16450
16451
/**
16452
  Intitialize the GROUP BY list.
16453
16454
  @param thd			Thread handler
16455
  @param ref_pointer_array	We store references to all fields that was
16456
                               not in 'fields' here.
16457
  @param fields		All fields in the select part. Any item in
16458
                               'order' that is part of these list is replaced
16459
                               by a pointer to this fields.
16460
  @param all_fields		Total list of all unique fields used by the
16461
                               select. All items in 'order' that was not part
16462
                               of fields will be added first to this list.
16463
  @param order			The fields we should do GROUP BY on.
16464
  @param hidden_group_fields	Pointer to flag that is set to 1 if we added
16465
                               any fields to all_fields.
16466
16467
  @todo
16468
    change ER_WRONG_FIELD_WITH_GROUP to more detailed
16469
    ER_NON_GROUPING_FIELD_USED
16470
16471
  @retval
16472
    0  ok
16473
  @retval
16474
    1  error (probably out of memory)
16475
*/
16476
16477
int
16478
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16479
	    List<Item> &fields, List<Item> &all_fields, ORDER *order,
16480
	    bool *hidden_group_fields)
16481
{
16482
  *hidden_group_fields=0;
16483
  ORDER *ord;
16484
16485
  if (!order)
16486
    return 0;				/* Everything is ok */
16487
16488
  uint org_fields=all_fields.elements;
16489
16490
  thd->where="group statement";
16491
  for (ord= order; ord; ord= ord->next)
16492
  {
16493
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
16494
			   all_fields, TRUE))
16495
      return 1;
16496
    (*ord->item)->marker= UNDEF_POS;		/* Mark found */
16497
    if ((*ord->item)->with_sum_func)
16498
    {
16499
      my_error(ER_WRONG_GROUP_FIELD, MYF(0), (*ord->item)->full_name());
16500
      return 1;
16501
    }
16502
  }
16503
  /* MODE_ONLY_FULL_GROUP_BY */
16504
  {
16505
    /*
16506
      Don't allow one to use fields that is not used in GROUP BY
16507
      For each select a list of field references that aren't under an
16508
      aggregate function is created. Each field in this list keeps the
16509
      position of the select list expression which it belongs to.
16510
16511
      First we check an expression from the select list against the GROUP BY
16512
      list. If it's found there then it's ok. It's also ok if this expression
16513
      is a constant or an aggregate function. Otherwise we scan the list
16514
      of non-aggregated fields and if we'll find at least one field reference
16515
      that belongs to this expression and doesn't occur in the GROUP BY list
16516
      we throw an error. If there are no fields in the created list for a
16517
      select list expression this means that all fields in it are used under
16518
      aggregate functions.
16519
    */
16520
    Item *item;
16521
    Item_field *field;
16522
    int cur_pos_in_select_list= 0;
16523
    List_iterator<Item> li(fields);
16524
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
16525
16526
    field= naf_it++;
16527
    while (field && (item=li++))
16528
    {
16529
      if (item->type() != Item::SUM_FUNC_ITEM && item->marker >= 0 &&
16530
          !item->const_item() &&
16531
          !(item->real_item()->type() == Item::FIELD_ITEM &&
16532
            item->used_tables() & OUTER_REF_TABLE_BIT))
16533
      {
16534
        while (field)
16535
        {
16536
          /* Skip fields from previous expressions. */
16537
          if (field->marker < cur_pos_in_select_list)
16538
            goto next_field;
16539
          /* Found a field from the next expression. */
16540
          if (field->marker > cur_pos_in_select_list)
16541
            break;
16542
          /*
16543
            Check whether the field occur in the GROUP BY list.
16544
            Throw the error later if the field isn't found.
16545
          */
16546
          for (ord= order; ord; ord= ord->next)
16547
            if ((*ord->item)->eq((Item*)field, 0))
16548
              goto next_field;
16549
          /*
16550
            TODO: change ER_WRONG_FIELD_WITH_GROUP to more detailed
16551
            ER_NON_GROUPING_FIELD_USED
16552
          */
16553
          my_error(ER_WRONG_FIELD_WITH_GROUP, MYF(0), field->full_name());
16554
          return 1;
16555
next_field:
16556
          field= naf_it++;
16557
        }
16558
      }
16559
      cur_pos_in_select_list++;
16560
    }
16561
  }
16562
  if (org_fields != all_fields.elements)
16563
    *hidden_group_fields=1;			// group fields is not used
16564
  return 0;
16565
}
16566
16567
/**
16568
  Create a group by that consist of all non const fields.
16569
16570
  Try to use the fields in the order given by 'order' to allow one to
16571
  optimize away 'order by'.
16572
*/
16573
16574
static ORDER *
16575
create_distinct_group(THD *thd, Item **ref_pointer_array,
16576
                      ORDER *order_list, List<Item> &fields,
16577
                      List<Item> &all_fields,
16578
		      bool *all_order_by_fields_used)
16579
{
16580
  List_iterator<Item> li(fields);
16581
  Item *item, **orig_ref_pointer_array= ref_pointer_array;
16582
  ORDER *order,*group,**prev;
16583
16584
  *all_order_by_fields_used= 1;
16585
  while ((item=li++))
16586
    item->marker=0;			/* Marker that field is not used */
16587
16588
  prev= &group;  group=0;
16589
  for (order=order_list ; order; order=order->next)
16590
  {
16591
    if (order->in_field_list)
16592
    {
16593
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
16594
      if (!ord)
16595
	return 0;
16596
      *prev=ord;
16597
      prev= &ord->next;
16598
      (*ord->item)->marker=1;
16599
    }
16600
    else
16601
      *all_order_by_fields_used= 0;
16602
  }
16603
16604
  li.rewind();
16605
  while ((item=li++))
16606
  {
16607
    if (!item->const_item() && !item->with_sum_func && !item->marker)
16608
    {
16609
      /* 
16610
        Don't put duplicate columns from the SELECT list into the 
16611
        GROUP BY list.
16612
      */
16613
      ORDER *ord_iter;
16614
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
16615
        if ((*ord_iter->item)->eq(item, 1))
16616
          goto next_item;
16617
      
16618
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
16619
      if (!ord)
16620
	return 0;
16621
16622
      if (item->type() == Item::FIELD_ITEM &&
16623
          item->field_type() == MYSQL_TYPE_BIT)
16624
      {
16625
        /*
16626
          Because HEAP tables can't index BIT fields we need to use an
16627
          additional hidden field for grouping because later it will be
16628
          converted to a LONG field. Original field will remain of the
16629
          BIT type and will be returned to a client.
16630
        */
16631
        Item_field *new_item= new Item_field(thd, (Item_field*)item);
16632
        int el= all_fields.elements;
16633
        orig_ref_pointer_array[el]= new_item;
16634
        all_fields.push_front(new_item);
16635
        ord->item= orig_ref_pointer_array + el;
16636
      }
16637
      else
16638
      {
16639
        /*
16640
          We have here only field_list (not all_field_list), so we can use
16641
          simple indexing of ref_pointer_array (order in the array and in the
16642
          list are same)
16643
        */
16644
        ord->item= ref_pointer_array;
16645
      }
16646
      ord->asc=1;
16647
      *prev=ord;
16648
      prev= &ord->next;
16649
    }
16650
next_item:
16651
    ref_pointer_array++;
16652
  }
16653
  *prev=0;
16654
  return group;
16655
}
16656
16657
16658
/**
16659
  Update join with count of the different type of fields.
16660
*/
16661
16662
void
16663
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
16664
                  List<Item> &fields, bool reset_with_sum_func)
16665
{
16666
  List_iterator<Item> li(fields);
16667
  Item *field;
16668
16669
  param->field_count=param->sum_func_count=param->func_count=
16670
    param->hidden_field_count=0;
16671
  param->quick_group=1;
16672
  while ((field=li++))
16673
  {
16674
    Item::Type real_type= field->real_item()->type();
16675
    if (real_type == Item::FIELD_ITEM)
16676
      param->field_count++;
16677
    else if (real_type == Item::SUM_FUNC_ITEM)
16678
    {
16679
      if (! field->const_item())
16680
      {
16681
	Item_sum *sum_item=(Item_sum*) field->real_item();
16682
        if (!sum_item->depended_from() ||
16683
            sum_item->depended_from() == select_lex)
16684
        {
16685
          if (!sum_item->quick_group)
16686
            param->quick_group=0;			// UDF SUM function
16687
          param->sum_func_count++;
16688
16689
          for (uint i=0 ; i < sum_item->arg_count ; i++)
16690
          {
16691
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
16692
              param->field_count++;
16693
            else
16694
              param->func_count++;
16695
          }
16696
        }
16697
        param->func_count++;
16698
      }
16699
    }
16700
    else
16701
    {
16702
      param->func_count++;
16703
      if (reset_with_sum_func)
16704
	field->with_sum_func=0;
16705
    }
16706
  }
16707
}
16708
16709
16710
/**
16711
  Return 1 if second is a subpart of first argument.
16712
16713
  If first parts has different direction, change it to second part
16714
  (group is sorted like order)
16715
*/
16716
16717
static bool
16718
test_if_subpart(ORDER *a,ORDER *b)
16719
{
16720
  for (; a && b; a=a->next,b=b->next)
16721
  {
16722
    if ((*a->item)->eq(*b->item,1))
16723
      a->asc=b->asc;
16724
    else
16725
      return 0;
16726
  }
16727
  return test(!b);
16728
}
16729
16730
/**
16731
  Return table number if there is only one table in sort order
16732
  and group and order is compatible, else return 0.
16733
*/
16734
16735
static TABLE *
16736
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
16737
{
16738
  table_map map= (table_map) 0;
16739
  DBUG_ENTER("get_sort_by_table");
16740
16741
  if (!a)
16742
    a=b;					// Only one need to be given
16743
  else if (!b)
16744
    b=a;
16745
16746
  for (; a && b; a=a->next,b=b->next)
16747
  {
16748
    if (!(*a->item)->eq(*b->item,1))
16749
      DBUG_RETURN(0);
16750
    map|=a->item[0]->used_tables();
16751
  }
16752
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
16753
    DBUG_RETURN(0);
16754
16755
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
16756
  if (map != tables->table->map)
16757
    DBUG_RETURN(0);				// More than one table
16758
  DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr));
16759
  DBUG_RETURN(tables->table);
16760
}
16761
16762
16763
/**
16764
  calc how big buffer we need for comparing group entries.
16765
*/
16766
16767
static void
16768
calc_group_buffer(JOIN *join,ORDER *group)
16769
{
16770
  uint key_length=0, parts=0, null_parts=0;
16771
16772
  if (group)
16773
    join->group= 1;
16774
  for (; group ; group=group->next)
16775
  {
16776
    Item *group_item= *group->item;
16777
    Field *field= group_item->get_tmp_table_field();
16778
    if (field)
16779
    {
16780
      enum_field_types type;
16781
      if ((type= field->type()) == MYSQL_TYPE_BLOB)
16782
	key_length+=MAX_BLOB_WIDTH;		// Can't be used as a key
16783
      else if (type == MYSQL_TYPE_VARCHAR || type == MYSQL_TYPE_VAR_STRING)
16784
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
16785
      else if (type == MYSQL_TYPE_BIT)
16786
      {
16787
        /* Bit is usually stored as a longlong key for group fields */
16788
        key_length+= 8;                         // Big enough
16789
      }
16790
      else
16791
	key_length+= field->pack_length();
16792
    }
16793
    else
16794
    { 
16795
      switch (group_item->result_type()) {
16796
      case REAL_RESULT:
16797
        key_length+= sizeof(double);
16798
        break;
16799
      case INT_RESULT:
16800
        key_length+= sizeof(longlong);
16801
        break;
16802
      case DECIMAL_RESULT:
16803
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
16804
                                                (group_item->decimals ? 1 : 0),
16805
                                                group_item->decimals);
16806
        break;
16807
      case STRING_RESULT:
16808
      {
16809
        enum enum_field_types type= group_item->field_type();
16810
        /*
16811
          As items represented as DATE/TIME fields in the group buffer
16812
          have STRING_RESULT result type, we increase the length 
16813
          by 8 as maximum pack length of such fields.
16814
        */
16815
        if (type == MYSQL_TYPE_TIME ||
16816
            type == MYSQL_TYPE_DATE ||
16817
            type == MYSQL_TYPE_DATETIME ||
16818
            type == MYSQL_TYPE_TIMESTAMP)
16819
        {
16820
          key_length+= 8;
16821
        }
16822
        else
16823
        {
16824
          /*
16825
            Group strings are taken as varstrings and require an length field.
16826
            A field is not yet created by create_tmp_field()
16827
            and the sizes should match up.
16828
          */
16829
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
16830
        }
16831
        break;
16832
      }
16833
      default:
16834
        /* This case should never be choosen */
16835
        DBUG_ASSERT(0);
16836
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
16837
      }
16838
    }
16839
    parts++;
16840
    if (group_item->maybe_null)
16841
      null_parts++;
16842
  }
16843
  join->tmp_table_param.group_length=key_length+null_parts;
16844
  join->tmp_table_param.group_parts=parts;
16845
  join->tmp_table_param.group_null_parts=null_parts;
16846
}
16847
16848
16849
/**
16850
  allocate group fields or take prepared (cached).
16851
16852
  @param main_join   join of current select
16853
  @param curr_join   current join (join of current select or temporary copy
16854
                     of it)
16855
16856
  @retval
16857
    0   ok
16858
  @retval
16859
    1   failed
16860
*/
16861
16862
static bool
16863
make_group_fields(JOIN *main_join, JOIN *curr_join)
16864
{
16865
  if (main_join->group_fields_cache.elements)
16866
  {
16867
    curr_join->group_fields= main_join->group_fields_cache;
16868
    curr_join->sort_and_group= 1;
16869
  }
16870
  else
16871
  {
16872
    if (alloc_group_fields(curr_join, curr_join->group_list))
16873
      return (1);
16874
    main_join->group_fields_cache= curr_join->group_fields;
16875
  }
16876
  return (0);
16877
}
16878
16879
16880
/**
16881
  Get a list of buffers for saveing last group.
16882
16883
  Groups are saved in reverse order for easyer check loop.
16884
*/
16885
16886
static bool
16887
alloc_group_fields(JOIN *join,ORDER *group)
16888
{
16889
  if (group)
16890
  {
16891
    for (; group ; group=group->next)
16892
    {
16893
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, FALSE);
16894
      if (!tmp || join->group_fields.push_front(tmp))
16895
	return TRUE;
16896
    }
16897
  }
16898
  join->sort_and_group=1;			/* Mark for do_select */
16899
  return FALSE;
16900
}
16901
16902
16903
/*
16904
  Test if a single-row cache of items changed, and update the cache.
16905
16906
  @details Test if a list of items that typically represents a result
16907
  row has changed. If the value of some item changed, update the cached
16908
  value for this item.
16909
  
16910
  @param list list of <item, cached_value> pairs stored as Cached_item.
16911
16912
  @return -1 if no item changed
16913
  @return index of the first item that changed
16914
*/
16915
16916
int test_if_item_cache_changed(List<Cached_item> &list)
16917
{
16918
  DBUG_ENTER("test_if_item_cache_changed");
16919
  List_iterator<Cached_item> li(list);
16920
  int idx= -1,i;
16921
  Cached_item *buff;
16922
16923
  for (i=(int) list.elements-1 ; (buff=li++) ; i--)
16924
  {
16925
    if (buff->cmp())
16926
      idx=i;
16927
  }
16928
  DBUG_PRINT("info", ("idx: %d", idx));
16929
  DBUG_RETURN(idx);
16930
}
16931
16932
16933
/**
16934
  Setup copy_fields to save fields at start of new group.
16935
16936
  Setup copy_fields to save fields at start of new group
16937
16938
  Only FIELD_ITEM:s and FUNC_ITEM:s needs to be saved between groups.
16939
  Change old item_field to use a new field with points at saved fieldvalue
16940
  This function is only called before use of send_fields.
16941
16942
  @param thd                   THD pointer
16943
  @param param                 temporary table parameters
16944
  @param ref_pointer_array     array of pointers to top elements of filed list
16945
  @param res_selected_fields   new list of items of select item list
16946
  @param res_all_fields        new list of all items
16947
  @param elements              number of elements in select item list
16948
  @param all_fields            all fields list
16949
16950
  @todo
16951
    In most cases this result will be sent to the user.
16952
    This should be changed to use copy_int or copy_real depending
16953
    on how the value is to be used: In some cases this may be an
16954
    argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
16955
16956
  @retval
16957
    0     ok
16958
  @retval
16959
    !=0   error
16960
*/
16961
16962
bool
16963
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
16964
		  Item **ref_pointer_array,
16965
		  List<Item> &res_selected_fields, List<Item> &res_all_fields,
16966
		  uint elements, List<Item> &all_fields)
16967
{
16968
  Item *pos;
16969
  List_iterator_fast<Item> li(all_fields);
16970
  Copy_field *copy= NULL;
16971
  res_selected_fields.empty();
16972
  res_all_fields.empty();
16973
  List_iterator_fast<Item> itr(res_all_fields);
16974
  List<Item> extra_funcs;
16975
  uint i, border= all_fields.elements - elements;
16976
  DBUG_ENTER("setup_copy_fields");
16977
16978
  if (param->field_count && 
16979
      !(copy=param->copy_field= new Copy_field[param->field_count]))
16980
    goto err2;
16981
16982
  param->copy_funcs.empty();
16983
  for (i= 0; (pos= li++); i++)
16984
  {
16985
    Field *field;
16986
    uchar *tmp;
16987
    Item *real_pos= pos->real_item();
16988
    if (real_pos->type() == Item::FIELD_ITEM)
16989
    {
16990
      Item_field *item;
16991
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
16992
	goto err;
16993
      if (pos->type() == Item::REF_ITEM)
16994
      {
16995
        /* preserve the names of the ref when dereferncing */
16996
        Item_ref *ref= (Item_ref *) pos;
16997
        item->db_name= ref->db_name;
16998
        item->table_name= ref->table_name;
16999
        item->name= ref->name;
17000
      }
17001
      pos= item;
17002
      if (item->field->flags & BLOB_FLAG)
17003
      {
17004
	if (!(pos= new Item_copy_string(pos)))
17005
	  goto err;
17006
       /*
17007
         Item_copy_string::copy for function can call 
17008
         Item_copy_string::val_int for blob via Item_ref.
17009
         But if Item_copy_string::copy for blob isn't called before,
17010
         it's value will be wrong
17011
         so let's insert Item_copy_string for blobs in the beginning of 
17012
         copy_funcs
17013
         (to see full test case look at having.test, BUG #4358) 
17014
       */
17015
	if (param->copy_funcs.push_front(pos))
17016
	  goto err;
17017
      }
17018
      else
17019
      {
17020
	/* 
17021
	   set up save buffer and change result_field to point at 
17022
	   saved value
17023
	*/
17024
	field= item->field;
17025
	item->result_field=field->new_field(thd->mem_root,field->table, 1);
17026
        /*
17027
          We need to allocate one extra byte for null handling and
17028
          another extra byte to not get warnings from purify in
17029
          Field_string::val_int
17030
        */
17031
	if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
17032
	  goto err;
17033
        if (copy)
17034
        {
17035
          copy->set(tmp, item->result_field);
17036
          item->result_field->move_field(copy->to_ptr,copy->to_null_ptr,1);
17037
#ifdef HAVE_purify
17038
          copy->to_ptr[copy->from_length]= 0;
17039
#endif
17040
          copy++;
17041
        }
17042
      }
17043
    }
17044
    else if ((real_pos->type() == Item::FUNC_ITEM ||
17045
	      real_pos->type() == Item::SUBSELECT_ITEM ||
17046
	      real_pos->type() == Item::CACHE_ITEM ||
17047
	      real_pos->type() == Item::COND_ITEM) &&
17048
	     !real_pos->with_sum_func)
17049
    {						// Save for send fields
17050
      pos= real_pos;
17051
      /* TODO:
17052
	 In most cases this result will be sent to the user.
17053
	 This should be changed to use copy_int or copy_real depending
17054
	 on how the value is to be used: In some cases this may be an
17055
	 argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
17056
      */
17057
      if (!(pos=new Item_copy_string(pos)))
17058
	goto err;
17059
      if (i < border)                           // HAVING, ORDER and GROUP BY
17060
      {
17061
        if (extra_funcs.push_back(pos))
17062
          goto err;
17063
      }
17064
      else if (param->copy_funcs.push_back(pos))
17065
	goto err;
17066
    }
17067
    res_all_fields.push_back(pos);
17068
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17069
      pos;
17070
  }
17071
  param->copy_field_end= copy;
17072
17073
  for (i= 0; i < border; i++)
17074
    itr++;
17075
  itr.sublist(res_selected_fields, elements);
17076
  /*
17077
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
17078
    reference used in these will resolve to a item that is already calculated
17079
  */
17080
  param->copy_funcs.concat(&extra_funcs);
17081
17082
  DBUG_RETURN(0);
17083
17084
 err:
17085
  if (copy)
17086
    delete [] param->copy_field;			// This is never 0
17087
  param->copy_field=0;
17088
err2:
17089
  DBUG_RETURN(TRUE);
17090
}
17091
17092
17093
/**
17094
  Make a copy of all simple SELECT'ed items.
17095
17096
  This is done at the start of a new group so that we can retrieve
17097
  these later when the group changes.
17098
*/
17099
17100
void
17101
copy_fields(TMP_TABLE_PARAM *param)
17102
{
17103
  Copy_field *ptr=param->copy_field;
17104
  Copy_field *end=param->copy_field_end;
17105
17106
  for (; ptr != end; ptr++)
17107
    (*ptr->do_copy)(ptr);
17108
17109
  List_iterator_fast<Item> it(param->copy_funcs);
17110
  Item_copy_string *item;
17111
  while ((item = (Item_copy_string*) it++))
17112
    item->copy();
17113
}
17114
17115
17116
/**
17117
  Make an array of pointers to sum_functions to speed up
17118
  sum_func calculation.
17119
17120
  @retval
17121
    0	ok
17122
  @retval
17123
    1	Error
17124
*/
17125
17126
bool JOIN::alloc_func_list()
17127
{
17128
  uint func_count, group_parts;
17129
  DBUG_ENTER("alloc_func_list");
17130
17131
  func_count= tmp_table_param.sum_func_count;
17132
  /*
17133
    If we are using rollup, we need a copy of the summary functions for
17134
    each level
17135
  */
17136
  if (rollup.state != ROLLUP::STATE_NONE)
17137
    func_count*= (send_group_parts+1);
17138
17139
  group_parts= send_group_parts;
17140
  /*
17141
    If distinct, reserve memory for possible
17142
    disctinct->group_by optimization
17143
  */
17144
  if (select_distinct)
17145
  {
17146
    group_parts+= fields_list.elements;
17147
    /*
17148
      If the ORDER clause is specified then it's possible that
17149
      it also will be optimized, so reserve space for it too
17150
    */
17151
    if (order)
17152
    {
17153
      ORDER *ord;
17154
      for (ord= order; ord; ord= ord->next)
17155
        group_parts++;
17156
    }
17157
  }
17158
17159
  /* This must use calloc() as rollup_make_fields depends on this */
17160
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
17161
				      sizeof(Item_sum***) * (group_parts+1));
17162
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
17163
  DBUG_RETURN(sum_funcs == 0);
17164
}
17165
17166
17167
/**
17168
  Initialize 'sum_funcs' array with all Item_sum objects.
17169
17170
  @param field_list        All items
17171
  @param send_fields       Items in select list
17172
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
17173
  @param recompute         Set to TRUE if sum_funcs must be recomputed
17174
17175
  @retval
17176
    0  ok
17177
  @retval
17178
    1  error
17179
*/
17180
17181
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
17182
			      bool before_group_by, bool recompute)
17183
{
17184
  List_iterator_fast<Item> it(field_list);
17185
  Item_sum **func;
17186
  Item *item;
17187
  DBUG_ENTER("make_sum_func_list");
17188
17189
  if (*sum_funcs && !recompute)
17190
    DBUG_RETURN(FALSE); /* We have already initialized sum_funcs. */
17191
17192
  func= sum_funcs;
17193
  while ((item=it++))
17194
  {
17195
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17196
        (!((Item_sum*) item)->depended_from() ||
17197
         ((Item_sum *)item)->depended_from() == select_lex))
17198
      *func++= (Item_sum*) item;
17199
  }
17200
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
17201
  {
17202
    rollup.state= ROLLUP::STATE_READY;
17203
    if (rollup_make_fields(field_list, send_fields, &func))
17204
      DBUG_RETURN(TRUE);			// Should never happen
17205
  }
17206
  else if (rollup.state == ROLLUP::STATE_NONE)
17207
  {
17208
    for (uint i=0 ; i <= send_group_parts ;i++)
17209
      sum_funcs_end[i]= func;
17210
  }
17211
  else if (rollup.state == ROLLUP::STATE_READY)
17212
    DBUG_RETURN(FALSE);                         // Don't put end marker
17213
  *func=0;					// End marker
17214
  DBUG_RETURN(FALSE);
17215
}
17216
17217
17218
/**
17219
  Change all funcs and sum_funcs to fields in tmp table, and create
17220
  new list of all items.
17221
17222
  @param thd                   THD pointer
17223
  @param ref_pointer_array     array of pointers to top elements of filed list
17224
  @param res_selected_fields   new list of items of select item list
17225
  @param res_all_fields        new list of all items
17226
  @param elements              number of elements in select item list
17227
  @param all_fields            all fields list
17228
17229
  @retval
17230
    0     ok
17231
  @retval
17232
    !=0   error
17233
*/
17234
17235
static bool
17236
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
17237
			 List<Item> &res_selected_fields,
17238
			 List<Item> &res_all_fields,
17239
			 uint elements, List<Item> &all_fields)
17240
{
17241
  List_iterator_fast<Item> it(all_fields);
17242
  Item *item_field,*item;
17243
  DBUG_ENTER("change_to_use_tmp_fields");
17244
17245
  res_selected_fields.empty();
17246
  res_all_fields.empty();
17247
17248
  uint i, border= all_fields.elements - elements;
17249
  for (i= 0; (item= it++); i++)
17250
  {
17251
    Field *field;
17252
17253
    if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) ||
17254
        (item->type() == Item::FUNC_ITEM &&
17255
         ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
17256
      item_field= item;
17257
    else
17258
    {
17259
      if (item->type() == Item::FIELD_ITEM)
17260
      {
17261
	item_field= item->get_tmp_table_item(thd);
17262
      }
17263
      else if ((field= item->get_tmp_table_field()))
17264
      {
17265
	if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
17266
	  item_field= ((Item_sum*) item)->result_item(field);
17267
	else
17268
	  item_field= (Item*) new Item_field(field);
17269
	if (!item_field)
17270
	  DBUG_RETURN(TRUE);                    // Fatal error
17271
17272
        if (item->real_item()->type() != Item::FIELD_ITEM)
17273
          field->orig_table= 0;
17274
	item_field->name= item->name;
17275
        if (item->type() == Item::REF_ITEM)
17276
        {
17277
          Item_field *ifield= (Item_field *) item_field;
17278
          Item_ref *iref= (Item_ref *) item;
17279
          ifield->table_name= iref->table_name;
17280
          ifield->db_name= iref->db_name;
17281
        }
17282
#ifndef DBUG_OFF
17283
	if (!item_field->name)
17284
	{
17285
	  char buff[256];
17286
	  String str(buff,sizeof(buff),&my_charset_bin);
17287
	  str.length(0);
17288
	  item->print(&str, QT_ORDINARY);
17289
	  item_field->name= sql_strmake(str.ptr(),str.length());
17290
	}
17291
#endif
17292
      }
17293
      else
17294
	item_field= item;
17295
    }
17296
    res_all_fields.push_back(item_field);
17297
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17298
      item_field;
17299
  }
17300
17301
  List_iterator_fast<Item> itr(res_all_fields);
17302
  for (i= 0; i < border; i++)
17303
    itr++;
17304
  itr.sublist(res_selected_fields, elements);
17305
  DBUG_RETURN(FALSE);
17306
}
17307
17308
17309
/**
17310
  Change all sum_func refs to fields to point at fields in tmp table.
17311
  Change all funcs to be fields in tmp table.
17312
17313
  @param thd                   THD pointer
17314
  @param ref_pointer_array     array of pointers to top elements of filed list
17315
  @param res_selected_fields   new list of items of select item list
17316
  @param res_all_fields        new list of all items
17317
  @param elements              number of elements in select item list
17318
  @param all_fields            all fields list
17319
17320
  @retval
17321
    0	ok
17322
  @retval
17323
    1	error
17324
*/
17325
17326
static bool
17327
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
17328
			  List<Item> &res_selected_fields,
17329
			  List<Item> &res_all_fields, uint elements,
17330
			  List<Item> &all_fields)
17331
{
17332
  List_iterator_fast<Item> it(all_fields);
17333
  Item *item, *new_item;
17334
  res_selected_fields.empty();
17335
  res_all_fields.empty();
17336
17337
  uint i, border= all_fields.elements - elements;
17338
  for (i= 0; (item= it++); i++)
17339
  {
17340
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
17341
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17342
      new_item;
17343
  }
17344
17345
  List_iterator_fast<Item> itr(res_all_fields);
17346
  for (i= 0; i < border; i++)
17347
    itr++;
17348
  itr.sublist(res_selected_fields, elements);
17349
17350
  return thd->is_fatal_error;
17351
}
17352
17353
17354
17355
/******************************************************************************
17356
  Code for calculating functions
17357
******************************************************************************/
17358
17359
17360
/**
17361
  Call ::setup for all sum functions.
17362
17363
  @param thd           thread handler
17364
  @param func_ptr      sum function list
17365
17366
  @retval
17367
    FALSE  ok
17368
  @retval
17369
    TRUE   error
17370
*/
17371
17372
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
17373
{
17374
  Item_sum *func;
17375
  DBUG_ENTER("setup_sum_funcs");
17376
  while ((func= *(func_ptr++)))
17377
  {
17378
    if (func->setup(thd))
17379
      DBUG_RETURN(TRUE);
17380
  }
17381
  DBUG_RETURN(FALSE);
17382
}
17383
17384
17385
static void
17386
init_tmptable_sum_functions(Item_sum **func_ptr)
17387
{
17388
  Item_sum *func;
17389
  while ((func= *(func_ptr++)))
17390
    func->reset_field();
17391
}
17392
17393
17394
/** Update record 0 in tmp_table from record 1. */
17395
17396
static void
17397
update_tmptable_sum_func(Item_sum **func_ptr,
17398
			 TABLE *tmp_table __attribute__((unused)))
17399
{
17400
  Item_sum *func;
17401
  while ((func= *(func_ptr++)))
17402
    func->update_field();
17403
}
17404
17405
17406
/** Copy result of sum functions to record in tmp_table. */
17407
17408
static void
17409
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
17410
{
17411
  for (; func_ptr != end_ptr ; func_ptr++)
17412
    (void) (*func_ptr)->save_in_result_field(1);
17413
  return;
17414
}
17415
17416
17417
static bool
17418
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
17419
{
17420
  for (; func_ptr != end_ptr ;func_ptr++)
17421
  {
17422
    if ((*func_ptr)->reset())
17423
      return 1;
17424
  }
17425
  /* If rollup, calculate the upper sum levels */
17426
  for ( ; *func_ptr ; func_ptr++)
17427
  {
17428
    if ((*func_ptr)->add())
17429
      return 1;
17430
  }
17431
  return 0;
17432
}
17433
17434
17435
static bool
17436
update_sum_func(Item_sum **func_ptr)
17437
{
17438
  Item_sum *func;
17439
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
17440
    if (func->add())
17441
      return 1;
17442
  return 0;
17443
}
17444
17445
/** Copy result of functions to record in tmp_table. */
17446
17447
void
17448
copy_funcs(Item **func_ptr)
17449
{
17450
  Item *func;
17451
  for (; (func = *func_ptr) ; func_ptr++)
17452
    func->save_in_result_field(1);
17453
}
17454
17455
17456
/**
17457
  Create a condition for a const reference and add this to the
17458
  currenct select for the table.
17459
*/
17460
17461
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
17462
{
17463
  DBUG_ENTER("add_ref_to_table_cond");
17464
  if (!join_tab->ref.key_parts)
17465
    DBUG_RETURN(FALSE);
17466
17467
  Item_cond_and *cond=new Item_cond_and();
17468
  TABLE *table=join_tab->table;
17469
  int error;
17470
  if (!cond)
17471
    DBUG_RETURN(TRUE);
17472
17473
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
17474
  {
17475
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
17476
			      fieldnr-1];
17477
    Item *value=join_tab->ref.items[i];
17478
    cond->add(new Item_func_equal(new Item_field(field), value));
17479
  }
17480
  if (thd->is_fatal_error)
17481
    DBUG_RETURN(TRUE);
17482
17483
  if (!cond->fixed)
17484
    cond->fix_fields(thd, (Item**)&cond);
17485
  if (join_tab->select)
17486
  {
17487
    error=(int) cond->add(join_tab->select->cond);
17488
    join_tab->select_cond=join_tab->select->cond=cond;
17489
  }
17490
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
17491
                                          &error)))
17492
    join_tab->select_cond=cond;
17493
17494
  DBUG_RETURN(error ? TRUE : FALSE);
17495
}
17496
17497
17498
/**
17499
  Free joins of subselect of this select.
17500
17501
  @param thd      THD pointer
17502
  @param select   pointer to st_select_lex which subselects joins we will free
17503
*/
17504
17505
void free_underlaid_joins(THD *thd, SELECT_LEX *select)
17506
{
17507
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
17508
       unit;
17509
       unit= unit->next_unit())
17510
    unit->cleanup();
17511
}
17512
17513
/****************************************************************************
17514
  ROLLUP handling
17515
****************************************************************************/
17516
17517
/**
17518
  Replace occurences of group by fields in an expression by ref items.
17519
17520
  The function replaces occurrences of group by fields in expr
17521
  by ref objects for these fields unless they are under aggregate
17522
  functions.
17523
  The function also corrects value of the the maybe_null attribute
17524
  for the items of all subexpressions containing group by fields.
17525
17526
  @b EXAMPLES
17527
    @code
17528
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
17529
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
17530
  @endcode
17531
17532
  @b IMPLEMENTATION
17533
17534
    The function recursively traverses the tree of the expr expression,
17535
    looks for occurrences of the group by fields that are not under
17536
    aggregate functions and replaces them for the corresponding ref items.
17537
17538
  @note
17539
    This substitution is needed GROUP BY queries with ROLLUP if
17540
    SELECT list contains expressions over group by attributes.
17541
17542
  @param thd                  reference to the context
17543
  @param expr                 expression to make replacement
17544
  @param group_list           list of references to group by items
17545
  @param changed        out:  returns 1 if item contains a replaced field item
17546
17547
  @todo
17548
    - TODO: Some functions are not null-preserving. For those functions
17549
    updating of the maybe_null attribute is an overkill. 
17550
17551
  @retval
17552
    0	if ok
17553
  @retval
17554
    1   on error
17555
*/
17556
17557
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
17558
                             bool *changed)
17559
{
17560
  if (expr->arg_count)
17561
  {
17562
    Name_resolution_context *context= &thd->lex->current_select->context;
17563
    Item **arg,**arg_end;
17564
    bool arg_changed= FALSE;
17565
    for (arg= expr->arguments(),
17566
         arg_end= expr->arguments()+expr->arg_count;
17567
         arg != arg_end; arg++)
17568
    {
17569
      Item *item= *arg;
17570
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
17571
      {
17572
        ORDER *group_tmp;
17573
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17574
        {
17575
          if (item->eq(*group_tmp->item,0))
17576
          {
17577
            Item *new_item;
17578
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
17579
                                        item->name)))
17580
              return 1;                                 // fatal_error is set
17581
            thd->change_item_tree(arg, new_item);
17582
            arg_changed= TRUE;
17583
          }
17584
        }
17585
      }
17586
      else if (item->type() == Item::FUNC_ITEM)
17587
      {
17588
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
17589
          return 1;
17590
      }
17591
    }
17592
    if (arg_changed)
17593
    {
17594
      expr->maybe_null= 1;
17595
      *changed= TRUE;
17596
    }
17597
  }
17598
  return 0;
17599
}
17600
17601
17602
/** Allocate memory needed for other rollup functions. */
17603
17604
bool JOIN::rollup_init()
17605
{
17606
  uint i,j;
17607
  Item **ref_array;
17608
17609
  tmp_table_param.quick_group= 0;	// Can't create groups in tmp table
17610
  rollup.state= ROLLUP::STATE_INITED;
17611
17612
  /*
17613
    Create pointers to the different sum function groups
17614
    These are updated by rollup_make_fields()
17615
  */
17616
  tmp_table_param.group_parts= send_group_parts;
17617
17618
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17619
                                                sizeof(Item**) +
17620
                                                sizeof(List<Item>) +
17621
				                ref_pointer_array_size)
17622
				                * send_group_parts )))
17623
    return 1;
17624
  
17625
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17626
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17627
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17628
17629
  /*
17630
    Prepare space for field list for the different levels
17631
    These will be filled up in rollup_make_fields()
17632
  */
17633
  for (i= 0 ; i < send_group_parts ; i++)
17634
  {
17635
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17636
    List<Item> *rollup_fields= &rollup.fields[i];
17637
    rollup_fields->empty();
17638
    rollup.ref_pointer_arrays[i]= ref_array;
17639
    ref_array+= all_fields.elements;
17640
  }
17641
  for (i= 0 ; i < send_group_parts; i++)
17642
  {
17643
    for (j=0 ; j < fields_list.elements ; j++)
17644
      rollup.fields[i].push_back(rollup.null_items[i]);
17645
  }
17646
  List_iterator<Item> it(all_fields);
17647
  Item *item;
17648
  while ((item= it++))
17649
  {
17650
    ORDER *group_tmp;
17651
    bool found_in_group= 0;
17652
17653
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17654
    {
17655
      if (*group_tmp->item == item)
17656
      {
17657
        item->maybe_null= 1;
17658
        found_in_group= 1;
17659
        if (item->const_item())
17660
        {
17661
          /*
17662
            For ROLLUP queries each constant item referenced in GROUP BY list
17663
            is wrapped up into an Item_func object yielding the same value
17664
            as the constant item. The objects of the wrapper class are never
17665
            considered as constant items and besides they inherit all
17666
            properties of the Item_result_field class.
17667
            This wrapping allows us to ensure writing constant items
17668
            into temporary tables whenever the result of the ROLLUP
17669
            operation has to be written into a temporary table, e.g. when
17670
            ROLLUP is used together with DISTINCT in the SELECT list.
17671
            Usually when creating temporary tables for a intermidiate
17672
            result we do not include fields for constant expressions.
17673
	  */           
17674
          Item* new_item= new Item_func_rollup_const(item);
17675
          if (!new_item)
17676
            return 1;
17677
          new_item->fix_fields(thd, (Item **) 0);
17678
          thd->change_item_tree(it.ref(), new_item);
17679
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17680
          { 
17681
            if (*tmp->item == item)
17682
              thd->change_item_tree(tmp->item, new_item);
17683
          }
17684
        }
17685
      }
17686
    }
17687
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
17688
    {
17689
      bool changed= FALSE;
17690
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17691
        return 1;
17692
      /*
17693
        We have to prevent creation of a field in a temporary table for
17694
        an expression that contains GROUP BY attributes.
17695
        Marking the expression item as 'with_sum_func' will ensure this.
17696
      */ 
17697
      if (changed)
17698
        item->with_sum_func= 1;
17699
    }
17700
  }
17701
  return 0;
17702
}
17703
  
17704
17705
/**
17706
  Fill up rollup structures with pointers to fields to use.
17707
17708
  Creates copies of item_sum items for each sum level.
17709
17710
  @param fields_arg		List of all fields (hidden and real ones)
17711
  @param sel_fields		Pointer to selected fields
17712
  @param func			Store here a pointer to all fields
17713
17714
  @retval
17715
    0	if ok;
17716
    In this case func is pointing to next not used element.
17717
  @retval
17718
    1    on error
17719
*/
17720
17721
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17722
			      Item_sum ***func)
17723
{
17724
  List_iterator_fast<Item> it(fields_arg);
17725
  Item *first_field= sel_fields.head();
17726
  uint level;
17727
17728
  /*
17729
    Create field lists for the different levels
17730
17731
    The idea here is to have a separate field list for each rollup level to
17732
    avoid all runtime checks of which columns should be NULL.
17733
17734
    The list is stored in reverse order to get sum function in such an order
17735
    in func that it makes it easy to reset them with init_sum_functions()
17736
17737
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17738
17739
    rollup.fields[0] will contain list where a,b,c is NULL
17740
    rollup.fields[1] will contain list where b,c is NULL
17741
    ...
17742
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17743
    ...
17744
    sum_funcs_end[0] points to all sum functions
17745
    sum_funcs_end[1] points to all sum functions, except grand totals
17746
    ...
17747
  */
17748
17749
  for (level=0 ; level < send_group_parts ; level++)
17750
  {
17751
    uint i;
17752
    uint pos= send_group_parts - level -1;
17753
    bool real_fields= 0;
17754
    Item *item;
17755
    List_iterator<Item> new_it(rollup.fields[pos]);
17756
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17757
    ORDER *start_group;
17758
17759
    /* Point to first hidden field */
17760
    Item **ref_array= ref_array_start + fields_arg.elements-1;
17761
17762
    /* Remember where the sum functions ends for the previous level */
17763
    sum_funcs_end[pos+1]= *func;
17764
17765
    /* Find the start of the group for this level */
17766
    for (i= 0, start_group= group_list ;
17767
	 i++ < pos ;
17768
	 start_group= start_group->next)
17769
      ;
17770
17771
    it.rewind();
17772
    while ((item= it++))
17773
    {
17774
      if (item == first_field)
17775
      {
17776
	real_fields= 1;				// End of hidden fields
17777
	ref_array= ref_array_start;
17778
      }
17779
17780
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17781
          (!((Item_sum*) item)->depended_from() ||
17782
           ((Item_sum *)item)->depended_from() == select_lex))
17783
          
17784
      {
17785
	/*
17786
	  This is a top level summary function that must be replaced with
17787
	  a sum function that is reset for this level.
17788
17789
	  NOTE: This code creates an object which is not that nice in a
17790
	  sub select.  Fortunately it's not common to have rollup in
17791
	  sub selects.
17792
	*/
17793
	item= item->copy_or_same(thd);
17794
	((Item_sum*) item)->make_unique();
17795
	*(*func)= (Item_sum*) item;
17796
	(*func)++;
17797
      }
17798
      else 
17799
      {
17800
	/* Check if this is something that is part of this group by */
17801
	ORDER *group_tmp;
17802
	for (group_tmp= start_group, i= pos ;
17803
             group_tmp ; group_tmp= group_tmp->next, i++)
17804
	{
17805
          if (*group_tmp->item == item)
17806
	  {
17807
	    /*
17808
	      This is an element that is used by the GROUP BY and should be
17809
	      set to NULL in this level
17810
	    */
17811
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17812
            if (!null_item)
17813
              return 1;
17814
	    item->maybe_null= 1;		// Value will be null sometimes
17815
            null_item->result_field= item->get_tmp_table_field();
17816
            item= null_item;
17817
	    break;
17818
	  }
17819
	}
17820
      }
17821
      *ref_array= item;
17822
      if (real_fields)
17823
      {
17824
	(void) new_it++;			// Point to next item
17825
	new_it.replace(item);			// Replace previous
17826
	ref_array++;
17827
      }
17828
      else
17829
	ref_array--;
17830
    }
17831
  }
17832
  sum_funcs_end[0]= *func;			// Point to last function
17833
  return 0;
17834
}
17835
17836
/**
17837
  Send all rollup levels higher than the current one to the client.
17838
17839
  @b SAMPLE
17840
    @code
17841
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17842
  @endcode
17843
17844
  @param idx		Level we are on:
17845
                        - 0 = Total sum level
17846
                        - 1 = First group changed  (a)
17847
                        - 2 = Second group changed (a,b)
17848
17849
  @retval
17850
    0   ok
17851
  @retval
17852
    1   If send_data_failed()
17853
*/
17854
17855
int JOIN::rollup_send_data(uint idx)
17856
{
17857
  uint i;
17858
  for (i= send_group_parts ; i-- > idx ; )
17859
  {
17860
    /* Get reference pointers to sum functions in place */
17861
    memcpy((char*) ref_pointer_array,
17862
	   (char*) rollup.ref_pointer_arrays[i],
17863
	   ref_pointer_array_size);
17864
    if ((!having || having->val_int()))
17865
    {
17866
      if (send_records < unit->select_limit_cnt && do_send_rows &&
17867
	  result->send_data(rollup.fields[i]))
17868
	return 1;
17869
      send_records++;
17870
    }
17871
  }
17872
  /* Restore ref_pointer_array */
17873
  set_items_ref_array(current_ref_pointer_array);
17874
  return 0;
17875
}
17876
17877
/**
17878
  Write all rollup levels higher than the current one to a temp table.
17879
17880
  @b SAMPLE
17881
    @code
17882
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17883
  @endcode
17884
17885
  @param idx                 Level we are on:
17886
                               - 0 = Total sum level
17887
                               - 1 = First group changed  (a)
17888
                               - 2 = Second group changed (a,b)
17889
  @param table               reference to temp table
17890
17891
  @retval
17892
    0   ok
17893
  @retval
17894
    1   if write_data_failed()
17895
*/
17896
17897
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17898
{
17899
  uint i;
17900
  for (i= send_group_parts ; i-- > idx ; )
17901
  {
17902
    /* Get reference pointers to sum functions in place */
17903
    memcpy((char*) ref_pointer_array,
17904
	   (char*) rollup.ref_pointer_arrays[i],
17905
	   ref_pointer_array_size);
17906
    if ((!having || having->val_int()))
17907
    {
17908
      int write_error;
17909
      Item *item;
17910
      List_iterator_fast<Item> it(rollup.fields[i]);
17911
      while ((item= it++))
17912
      {
17913
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
17914
          item->save_in_result_field(1);
17915
      }
17916
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17917
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17918
      {
17919
	if (create_myisam_from_heap(thd, table_arg, 
17920
                                    tmp_table_param.start_recinfo,
17921
                                    &tmp_table_param.recinfo,
17922
                                    write_error, 0))
17923
	  return 1;		     
17924
      }
17925
    }
17926
  }
17927
  /* Restore ref_pointer_array */
17928
  set_items_ref_array(current_ref_pointer_array);
17929
  return 0;
17930
}
17931
17932
/**
17933
  clear results if there are not rows found for group
17934
  (end_send_group/end_write_group)
17935
*/
17936
17937
void JOIN::clear()
17938
{
17939
  clear_tables(this);
17940
  copy_fields(&tmp_table_param);
17941
17942
  if (sum_funcs)
17943
  {
17944
    Item_sum *func, **func_ptr= sum_funcs;
17945
    while ((func= *(func_ptr++)))
17946
      func->clear();
17947
  }
17948
}
17949
17950
/**
17951
  EXPLAIN handling.
17952
17953
  Send a description about what how the select will be done to stdout.
17954
*/
17955
17956
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17957
                     bool distinct,const char *message)
17958
{
17959
  List<Item> field_list;
17960
  List<Item> item_list;
17961
  THD *thd=join->thd;
17962
  select_result *result=join->result;
17963
  Item *item_null= new Item_null();
17964
  CHARSET_INFO *cs= system_charset_info;
17965
  int quick_type;
17966
  DBUG_ENTER("select_describe");
17967
  DBUG_PRINT("info", ("Select 0x%lx, type %s, message %s",
17968
		      (ulong)join->select_lex, join->select_lex->type,
17969
		      message ? message : "NULL"));
17970
  /* Don't log this into the slow query log */
17971
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17972
  join->unit->offset_limit_cnt= 0;
17973
17974
  /* 
17975
    NOTE: the number/types of items pushed into item_list must be in sync with
17976
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17977
  */
17978
  if (message)
17979
  {
17980
    item_list.push_back(new Item_int((int32)
17981
				     join->select_lex->select_number));
17982
    item_list.push_back(new Item_string(join->select_lex->type,
17983
					strlen(join->select_lex->type), cs));
17984
    for (uint i=0 ; i < 7; i++)
17985
      item_list.push_back(item_null);
17986
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17987
      item_list.push_back(item_null);
17988
  
17989
    item_list.push_back(new Item_string(message,strlen(message),cs));
17990
    if (result->send_data(item_list))
17991
      join->error= 1;
17992
  }
17993
  else if (join->select_lex == join->unit->fake_select_lex)
17994
  {
17995
    /* 
17996
      here we assume that the query will return at least two rows, so we
17997
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17998
      and no filesort will be actually done, but executing all selects in
17999
      the UNION to provide precise EXPLAIN information will hardly be
18000
      appreciated :)
18001
    */
18002
    char table_name_buffer[NAME_LEN];
18003
    item_list.empty();
18004
    /* id */
18005
    item_list.push_back(new Item_null);
18006
    /* select_type */
18007
    item_list.push_back(new Item_string(join->select_lex->type,
18008
					strlen(join->select_lex->type),
18009
					cs));
18010
    /* table */
18011
    {
18012
      SELECT_LEX *sl= join->unit->first_select();
18013
      uint len= 6, lastop= 0;
18014
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
18015
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
18016
      {
18017
        len+= lastop;
18018
        lastop= my_snprintf(table_name_buffer + len, NAME_LEN - len,
18019
                            "%u,", sl->select_number);
18020
      }
18021
      if (sl || len + lastop >= NAME_LEN)
18022
      {
18023
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
18024
        len+= 4;
18025
      }
18026
      else
18027
      {
18028
        len+= lastop;
18029
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
18030
      }
18031
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
18032
    }
18033
    /* type */
18034
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
18035
					  strlen(join_type_str[JT_ALL]),
18036
					  cs));
18037
    /* possible_keys */
18038
    item_list.push_back(item_null);
18039
    /* key*/
18040
    item_list.push_back(item_null);
18041
    /* key_len */
18042
    item_list.push_back(item_null);
18043
    /* ref */
18044
    item_list.push_back(item_null);
18045
    /* in_rows */
18046
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18047
      item_list.push_back(item_null);
18048
    /* rows */
18049
    item_list.push_back(item_null);
18050
    /* extra */
18051
    if (join->unit->global_parameters->order_list.first)
18052
      item_list.push_back(new Item_string("Using filesort",
18053
					  14, cs));
18054
    else
18055
      item_list.push_back(new Item_string("", 0, cs));
18056
18057
    if (result->send_data(item_list))
18058
      join->error= 1;
18059
  }
18060
  else
18061
  {
18062
    table_map used_tables=0;
18063
    for (uint i=0 ; i < join->tables ; i++)
18064
    {
18065
      JOIN_TAB *tab=join->join_tab+i;
18066
      TABLE *table=tab->table;
18067
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
18068
      char buff[512]; 
18069
      char buff1[512], buff2[512], buff3[512];
18070
      char keylen_str_buf[64];
18071
      String extra(buff, sizeof(buff),cs);
18072
      char table_name_buffer[NAME_LEN];
18073
      String tmp1(buff1,sizeof(buff1),cs);
18074
      String tmp2(buff2,sizeof(buff2),cs);
18075
      String tmp3(buff3,sizeof(buff3),cs);
18076
      extra.length(0);
18077
      tmp1.length(0);
18078
      tmp2.length(0);
18079
      tmp3.length(0);
18080
18081
      quick_type= -1;
18082
      item_list.empty();
18083
      /* id */
18084
      item_list.push_back(new Item_uint((uint32)
18085
				       join->select_lex->select_number));
18086
      /* select_type */
18087
      item_list.push_back(new Item_string(join->select_lex->type,
18088
					  strlen(join->select_lex->type),
18089
					  cs));
18090
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
18091
      {
18092
        quick_type= tab->select->quick->get_type();
18093
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
18094
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
18095
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
18096
          tab->type = JT_INDEX_MERGE;
18097
        else
18098
	  tab->type = JT_RANGE;
18099
      }
18100
      /* table */
18101
      if (table->derived_select_number)
18102
      {
18103
	/* Derived table name generation */
18104
	int len= my_snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
18105
			     "<derived%u>",
18106
			     table->derived_select_number);
18107
	item_list.push_back(new Item_string(table_name_buffer, len, cs));
18108
      }
18109
      else
18110
      {
18111
        TABLE_LIST *real_table= table->pos_in_table_list; 
18112
	item_list.push_back(new Item_string(real_table->alias,
18113
					    strlen(real_table->alias),
18114
					    cs));
18115
      }
18116
      /* "type" column */
18117
      item_list.push_back(new Item_string(join_type_str[tab->type],
18118
					  strlen(join_type_str[tab->type]),
18119
					  cs));
18120
      /* Build "possible_keys" value and add it to item_list */
18121
      if (!tab->keys.is_clear_all())
18122
      {
18123
        uint j;
18124
        for (j=0 ; j < table->s->keys ; j++)
18125
        {
18126
          if (tab->keys.is_set(j))
18127
          {
18128
            if (tmp1.length())
18129
              tmp1.append(',');
18130
            tmp1.append(table->key_info[j].name, 
18131
			strlen(table->key_info[j].name),
18132
			system_charset_info);
18133
          }
18134
        }
18135
      }
18136
      if (tmp1.length())
18137
	item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
18138
      else
18139
	item_list.push_back(item_null);
18140
18141
      /* Build "key", "key_len", and "ref" values and add them to item_list */
18142
      if (tab->ref.key_parts)
18143
      {
18144
	KEY *key_info=table->key_info+ tab->ref.key;
18145
        register uint length;
18146
	item_list.push_back(new Item_string(key_info->name,
18147
					    strlen(key_info->name),
18148
					    system_charset_info));
18149
        length= longlong2str(tab->ref.key_length, keylen_str_buf, 10) - 
18150
                keylen_str_buf;
18151
        item_list.push_back(new Item_string(keylen_str_buf, length,
18152
                                            system_charset_info));
18153
	for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
18154
	{
18155
	  if (tmp2.length())
18156
	    tmp2.append(',');
18157
	  tmp2.append((*ref)->name(), strlen((*ref)->name()),
18158
		      system_charset_info);
18159
	}
18160
	item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18161
      }
18162
      else if (tab->type == JT_NEXT)
18163
      {
18164
	KEY *key_info=table->key_info+ tab->index;
18165
        register uint length;
18166
	item_list.push_back(new Item_string(key_info->name,
18167
					    strlen(key_info->name),cs));
18168
        length= longlong2str(key_info->key_length, keylen_str_buf, 10) - 
18169
                keylen_str_buf;
18170
        item_list.push_back(new Item_string(keylen_str_buf, 
18171
                                            length,
18172
                                            system_charset_info));
18173
	item_list.push_back(item_null);
18174
      }
18175
      else if (tab->select && tab->select->quick)
18176
      {
18177
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
18178
	item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18179
	item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
18180
	item_list.push_back(item_null);
18181
      }
18182
      else
18183
      {
18184
        if (table_list->schema_table &&
18185
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18186
        {
18187
          const char *tmp_buff;
18188
          int f_idx;
18189
          if (table_list->has_db_lookup_value)
18190
          {
18191
            f_idx= table_list->schema_table->idx_field1;
18192
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
18193
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
18194
          }          
18195
          if (table_list->has_table_lookup_value)
18196
          {
18197
            if (table_list->has_db_lookup_value)
18198
              tmp2.append(',');
18199
            f_idx= table_list->schema_table->idx_field2;
18200
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
18201
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
18202
          }
18203
          if (tmp2.length())
18204
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
18205
          else
18206
            item_list.push_back(item_null);
18207
        }
18208
        else
18209
          item_list.push_back(item_null);
18210
	item_list.push_back(item_null);
18211
	item_list.push_back(item_null);
18212
      }
18213
      
18214
      /* Add "rows" field to item_list. */
18215
      if (table_list->schema_table)
18216
      {
18217
        /* in_rows */
18218
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18219
          item_list.push_back(item_null);
18220
        /* rows */
18221
        item_list.push_back(item_null);
18222
      }
18223
      else
18224
      {
18225
        double examined_rows;
18226
        if (tab->select && tab->select->quick)
18227
          examined_rows= rows2double(tab->select->quick->records);
18228
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
18229
          examined_rows= rows2double(tab->limit ? tab->limit : 
18230
                                     tab->table->file->records());
18231
        else
18232
          examined_rows= join->best_positions[i].records_read; 
18233
 
18234
        item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows, 
18235
                                         MY_INT64_NUM_DECIMAL_DIGITS));
18236
18237
        /* Add "filtered" field to item_list. */
18238
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
18239
        {
18240
          float f= 0.0; 
18241
          if (examined_rows)
18242
            f= (float) (100.0 * join->best_positions[i].records_read /
18243
                        examined_rows);
18244
          item_list.push_back(new Item_float(f, 2));
18245
        }
18246
      }
18247
18248
      /* Build "Extra" field and add it to item_list. */
18249
      my_bool key_read=table->key_read;
18250
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
18251
          table->covering_keys.is_set(tab->index))
18252
	key_read=1;
18253
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
18254
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
18255
        key_read=1;
18256
        
18257
      if (tab->info)
18258
	item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
18259
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
18260
      {
18261
        if (tab->packed_info & TAB_INFO_USING_INDEX)
18262
          extra.append(STRING_WITH_LEN("; Using index"));
18263
        if (tab->packed_info & TAB_INFO_USING_WHERE)
18264
          extra.append(STRING_WITH_LEN("; Using where"));
18265
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
18266
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18267
        /* Skip initial "; "*/
18268
        const char *str= extra.ptr();
18269
        uint32 len= extra.length();
18270
        if (len)
18271
        {
18272
          str += 2;
18273
          len -= 2;
18274
        }
18275
	item_list.push_back(new Item_string(str, len, cs));
18276
      }
18277
      else
18278
      {
18279
        uint keyno= MAX_KEY;
18280
        if (tab->ref.key_parts)
18281
          keyno= tab->ref.key;
18282
        else if (tab->select && tab->select->quick)
18283
          keyno = tab->select->quick->index;
18284
18285
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
18286
            table->file->pushed_idx_cond)
18287
          extra.append(STRING_WITH_LEN("; Using index condition"));
18288
18289
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
18290
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
18291
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
18292
        {
18293
          extra.append(STRING_WITH_LEN("; Using "));
18294
          tab->select->quick->add_info_string(&extra);
18295
        }
18296
          if (tab->select)
18297
	{
18298
	  if (tab->use_quick == 2)
18299
	  {
18300
            /* 4 bits per 1 hex digit + terminating '\0' */
18301
            char buf[MAX_KEY / 4 + 1];
18302
            extra.append(STRING_WITH_LEN("; Range checked for each "
18303
                                         "record (index map: 0x"));
18304
            extra.append(tab->keys.print(buf));
18305
            extra.append(')');
18306
	  }
18307
	  else if (tab->select->cond)
18308
          {
18309
            const COND *pushed_cond= tab->table->file->pushed_cond;
18310
18311
            if (thd->variables.engine_condition_pushdown && pushed_cond)
18312
            {
18313
              extra.append(STRING_WITH_LEN("; Using where with pushed "
18314
                                           "condition"));
18315
              if (thd->lex->describe & DESCRIBE_EXTENDED)
18316
              {
18317
                extra.append(STRING_WITH_LEN(": "));
18318
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
18319
              }
18320
            }
18321
            else
18322
              extra.append(STRING_WITH_LEN("; Using where"));
18323
          }
18324
        }
18325
        if (key_read)
18326
        {
18327
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
18328
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
18329
          else
18330
            extra.append(STRING_WITH_LEN("; Using index"));
18331
        }
18332
        if (table->reginfo.not_exists_optimize)
18333
          extra.append(STRING_WITH_LEN("; Not exists"));
18334
          
18335
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
18336
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
18337
             HA_MRR_USE_DEFAULT_IMPL))
18338
        {
18339
	  extra.append(STRING_WITH_LEN("; Using MRR"));
18340
        }
18341
18342
        if (table_list->schema_table &&
18343
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18344
        {
18345
          if (!table_list->table_open_method)
18346
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
18347
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
18348
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
18349
          else
18350
            extra.append(STRING_WITH_LEN("; Open_full_table"));
18351
          if (table_list->has_db_lookup_value &&
18352
              table_list->has_table_lookup_value)
18353
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18354
          else if (table_list->has_db_lookup_value ||
18355
                   table_list->has_table_lookup_value)
18356
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18357
          else
18358
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
18359
        }
18360
        if (need_tmp_table)
18361
        {
18362
          need_tmp_table=0;
18363
          extra.append(STRING_WITH_LEN("; Using temporary"));
18364
        }
18365
        if (need_order)
18366
        {
18367
          need_order=0;
18368
          extra.append(STRING_WITH_LEN("; Using filesort"));
18369
        }
18370
        if (distinct & test_all_bits(used_tables,thd->used_tables))
18371
          extra.append(STRING_WITH_LEN("; Distinct"));
18372
18373
        if (tab->insideout_match_tab)
18374
        {
18375
          extra.append(STRING_WITH_LEN("; LooseScan"));
18376
        }
18377
18378
        if (tab->flush_weedout_table)
18379
          extra.append(STRING_WITH_LEN("; Start temporary"));
18380
        else if (tab->check_weed_out_table)
18381
          extra.append(STRING_WITH_LEN("; End temporary"));
18382
        else if (tab->do_firstmatch)
18383
        {
18384
          extra.append(STRING_WITH_LEN("; FirstMatch("));
18385
          TABLE *prev_table=tab->do_firstmatch->table;
18386
          if (prev_table->derived_select_number)
18387
          {
18388
            char namebuf[NAME_LEN];
18389
            /* Derived table name generation */
18390
            int len= my_snprintf(namebuf, sizeof(namebuf)-1,
18391
                                 "<derived%u>",
18392
                                 prev_table->derived_select_number);
18393
            extra.append(namebuf, len);
18394
          }
18395
          else
18396
            extra.append(prev_table->pos_in_table_list->alias);
18397
          extra.append(STRING_WITH_LEN(")"));
18398
        }
18399
18400
        for (uint part= 0; part < tab->ref.key_parts; part++)
18401
        {
18402
          if (tab->ref.cond_guards[part])
18403
          {
18404
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18405
            break;
18406
          }
18407
        }
18408
18409
        if (i > 0 && tab[-1].next_select == sub_select_cache)
18410
          extra.append(STRING_WITH_LEN("; Using join buffer"));
18411
18412
        /* Skip initial "; "*/
18413
        const char *str= extra.ptr();
18414
        uint32 len= extra.length();
18415
        if (len)
18416
        {
18417
          str += 2;
18418
          len -= 2;
18419
        }
18420
        item_list.push_back(new Item_string(str, len, cs));
18421
      }
18422
      // For next iteration
18423
      used_tables|=table->map;
18424
      if (result->send_data(item_list))
18425
	join->error= 1;
18426
    }
18427
  }
18428
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18429
       unit;
18430
       unit= unit->next_unit())
18431
  {
18432
    if (mysql_explain_union(thd, unit, result))
18433
      DBUG_VOID_RETURN;
18434
  }
18435
  DBUG_VOID_RETURN;
18436
}
18437
18438
18439
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18440
{
18441
  DBUG_ENTER("mysql_explain_union");
18442
  bool res= 0;
18443
  SELECT_LEX *first= unit->first_select();
18444
18445
  for (SELECT_LEX *sl= first;
18446
       sl;
18447
       sl= sl->next_select())
18448
  {
18449
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18450
    uint8 uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18451
    sl->type= (((&thd->lex->select_lex)==sl)?
18452
	       (sl->first_inner_unit() || sl->next_select() ? 
18453
		"PRIMARY" : "SIMPLE"):
18454
	       ((sl == first)?
18455
		((sl->linkage == DERIVED_TABLE_TYPE) ?
18456
		 "DERIVED":
18457
		 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
18458
		  "DEPENDENT SUBQUERY":
18459
		  (uncacheable?"UNCACHEABLE SUBQUERY":
18460
		   "SUBQUERY"))):
18461
		((uncacheable & UNCACHEABLE_DEPENDENT) ?
18462
		 "DEPENDENT UNION":
18463
		 uncacheable?"UNCACHEABLE UNION":
18464
		 "UNION")));
18465
    sl->options|= SELECT_DESCRIBE;
18466
  }
18467
  if (unit->is_union())
18468
  {
18469
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18470
    unit->fake_select_lex->type= "UNION RESULT";
18471
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
18472
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18473
      res= unit->exec();
18474
    res|= unit->cleanup();
18475
  }
18476
  else
18477
  {
18478
    thd->lex->current_select= first;
18479
    unit->set_limit(unit->global_parameters);
18480
    res= mysql_select(thd, &first->ref_pointer_array,
18481
			(TABLE_LIST*) first->table_list.first,
18482
			first->with_wild, first->item_list,
18483
			first->where,
18484
			first->order_list.elements +
18485
			first->group_list.elements,
18486
			(ORDER*) first->order_list.first,
18487
			(ORDER*) first->group_list.first,
18488
			first->having,
18489
			(ORDER*) thd->lex->proc_list.first,
18490
			first->options | thd->options | SELECT_DESCRIBE,
18491
			result, unit, first);
18492
  }
18493
  DBUG_RETURN(res || thd->is_error());
18494
}
18495
18496
18497
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
18498
                              TABLE_LIST **end)
18499
{
18500
  (*table)->print(thd, str, QT_ORDINARY);
18501
18502
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18503
  {
18504
    TABLE_LIST *curr= *tbl;
18505
    if (curr->outer_join)
18506
    {
18507
      /* MySQL converts right to left joins */
18508
      str->append(STRING_WITH_LEN(" left join "));
18509
    }
18510
    else if (curr->straight)
18511
      str->append(STRING_WITH_LEN(" straight_join "));
18512
    else if (curr->sj_inner_tables)
18513
      str->append(STRING_WITH_LEN(" semi join "));
18514
    else
18515
      str->append(STRING_WITH_LEN(" join "));
18516
    curr->print(thd, str, QT_ORDINARY);
18517
    if (curr->on_expr)
18518
    {
18519
      str->append(STRING_WITH_LEN(" on("));
18520
      curr->on_expr->print(str, QT_ORDINARY);
18521
      str->append(')');
18522
    }
18523
  }
18524
}
18525
18526
18527
/**
18528
  Print joins from the FROM clause.
18529
  @param thd     thread handler
18530
  @param str     string where table should be printed
18531
  @param tables  list of tables in join
18532
  @query_type    type of the query is being generated
18533
*/
18534
18535
static void print_join(THD *thd,
18536
                       String *str,
18537
                       List<TABLE_LIST> *tables,
18538
                       enum_query_type query_type)
18539
{
18540
  /* List is reversed => we should reverse it before using */
18541
  List_iterator_fast<TABLE_LIST> ti(*tables);
18542
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
18543
                                                tables->elements);
18544
  if (table == 0)
18545
    return;  // out of memory
18546
18547
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
18548
    *t= ti++;
18549
  
18550
  /* 
18551
    If the first table is a semi-join nest, swap it with something that is
18552
    not a semi-join nest.
18553
  */
18554
  if ((*table)->sj_inner_tables)
18555
  {
18556
    TABLE_LIST **end= table + tables->elements;
18557
    for (TABLE_LIST **t2= table; t2!=end; t2++)
18558
    {
18559
      if (!(*t2)->sj_inner_tables)
18560
      {
18561
        TABLE_LIST *tmp= *t2;
18562
        *t2= *table;
18563
        *table= tmp;
18564
        break;
18565
      }
18566
    }
18567
  }
18568
  DBUG_ASSERT(tables->elements >= 1);
18569
  print_table_array(thd, str, table, table + tables->elements);
18570
}
18571
18572
18573
/**
18574
  @brief Print an index hint
18575
18576
  @details Prints out the USE|FORCE|IGNORE index hint.
18577
18578
  @param      thd         the current thread
18579
  @param[out] str         appends the index hint here
18580
  @param      hint        what the hint is (as string : "USE INDEX"|
18581
                          "FORCE INDEX"|"IGNORE INDEX")
18582
  @param      hint_length the length of the string in 'hint'
18583
  @param      indexes     a list of index names for the hint
18584
*/
18585
18586
void 
18587
Index_hint::print(THD *thd, String *str)
18588
{
18589
  switch (type)
18590
  {
18591
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
18592
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
18593
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
18594
  }
18595
  str->append (STRING_WITH_LEN(" ("));
18596
  if (key_name.length)
18597
  {
18598
    if (thd && !my_strnncoll(system_charset_info,
18599
                             (const uchar *)key_name.str, key_name.length, 
18600
                             (const uchar *)primary_key_name, 
18601
                             strlen(primary_key_name)))
18602
      str->append(primary_key_name);
18603
    else
18604
      append_identifier(thd, str, key_name.str, key_name.length);
18605
  }
18606
  str->append(')');
18607
}
18608
18609
18610
/**
18611
  Print table as it should be in join list.
18612
18613
  @param str   string where table should be printed
18614
*/
18615
18616
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
18617
{
18618
  if (nested_join)
18619
  {
18620
    str->append('(');
18621
    print_join(thd, str, &nested_join->join_list, query_type);
18622
    str->append(')');
18623
  }
18624
  else
18625
  {
18626
    const char *cmp_name;                         // Name to compare with alias
18627
    if (derived)
18628
    {
18629
      // A derived table
18630
      str->append('(');
18631
      derived->print(str, query_type);
18632
      str->append(')');
18633
      cmp_name= "";                               // Force printing of alias
18634
    }
18635
    else
18636
    {
18637
      // A normal table
18638
      {
18639
        append_identifier(thd, str, db, db_length);
18640
        str->append('.');
18641
      }
18642
      if (schema_table)
18643
      {
18644
        append_identifier(thd, str, schema_table_name,
18645
                          strlen(schema_table_name));
18646
        cmp_name= schema_table_name;
18647
      }
18648
      else
18649
      {
18650
        append_identifier(thd, str, table_name, table_name_length);
18651
        cmp_name= table_name;
18652
      }
18653
    }
18654
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
18655
    {
18656
      char t_alias_buff[MAX_ALIAS_NAME];
18657
      const char *t_alias= alias;
18658
18659
      str->append(' ');
18660
      if (lower_case_table_names== 1)
18661
      {
18662
        if (alias && alias[0])
18663
        {
18664
          strmov(t_alias_buff, alias);
18665
          my_casedn_str(files_charset_info, t_alias_buff);
18666
          t_alias= t_alias_buff;
18667
        }
18668
      }
18669
18670
      append_identifier(thd, str, t_alias, strlen(t_alias));
18671
    }
18672
18673
    if (index_hints)
18674
    {
18675
      List_iterator<Index_hint> it(*index_hints);
18676
      Index_hint *hint;
18677
18678
      while ((hint= it++))
18679
      {
18680
        str->append (STRING_WITH_LEN(" "));
18681
        hint->print (thd, str);
18682
      }
18683
    }
18684
  }
18685
}
18686
18687
18688
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
18689
{
18690
  /* QQ: thd may not be set for sub queries, but this should be fixed */
18691
  if (!thd)
18692
    thd= current_thd;
18693
18694
  str->append(STRING_WITH_LEN("select "));
18695
18696
  /* First add options */
18697
  if (options & SELECT_STRAIGHT_JOIN)
18698
    str->append(STRING_WITH_LEN("straight_join "));
18699
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
18700
      (this == &thd->lex->select_lex))
18701
    str->append(STRING_WITH_LEN("high_priority "));
18702
  if (options & SELECT_DISTINCT)
18703
    str->append(STRING_WITH_LEN("distinct "));
18704
  if (options & SELECT_SMALL_RESULT)
18705
    str->append(STRING_WITH_LEN("sql_small_result "));
18706
  if (options & SELECT_BIG_RESULT)
18707
    str->append(STRING_WITH_LEN("sql_big_result "));
18708
  if (options & OPTION_BUFFER_RESULT)
18709
    str->append(STRING_WITH_LEN("sql_buffer_result "));
18710
  if (options & OPTION_FOUND_ROWS)
18711
    str->append(STRING_WITH_LEN("sql_calc_found_rows "));
18712
18713
  //Item List
18714
  bool first= 1;
18715
  List_iterator_fast<Item> it(item_list);
18716
  Item *item;
18717
  while ((item= it++))
18718
  {
18719
    if (first)
18720
      first= 0;
18721
    else
18722
      str->append(',');
18723
    item->print_item_w_name(str, query_type);
18724
  }
18725
18726
  /*
18727
    from clause
18728
    TODO: support USING/FORCE/IGNORE index
18729
  */
18730
  if (table_list.elements)
18731
  {
18732
    str->append(STRING_WITH_LEN(" from "));
18733
    /* go through join tree */
18734
    print_join(thd, str, &top_join_list, query_type);
18735
  }
18736
  else if (where)
18737
  {
18738
    /*
18739
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
18740
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
18741
    */
18742
    str->append(STRING_WITH_LEN(" from DUAL "));
18743
  }
18744
18745
  // Where
18746
  Item *cur_where= where;
18747
  if (join)
18748
    cur_where= join->conds;
18749
  if (cur_where || cond_value != Item::COND_UNDEF)
18750
  {
18751
    str->append(STRING_WITH_LEN(" where "));
18752
    if (cur_where)
18753
      cur_where->print(str, query_type);
18754
    else
18755
      str->append(cond_value != Item::COND_FALSE ? "1" : "0");
18756
  }
18757
18758
  // group by & olap
18759
  if (group_list.elements)
18760
  {
18761
    str->append(STRING_WITH_LEN(" group by "));
18762
    print_order(str, (ORDER *) group_list.first, query_type);
18763
    switch (olap)
18764
    {
18765
      case CUBE_TYPE:
18766
	str->append(STRING_WITH_LEN(" with cube"));
18767
	break;
18768
      case ROLLUP_TYPE:
18769
	str->append(STRING_WITH_LEN(" with rollup"));
18770
	break;
18771
      default:
18772
	;  //satisfy compiler
18773
    }
18774
  }
18775
18776
  // having
18777
  Item *cur_having= having;
18778
  if (join)
18779
    cur_having= join->having;
18780
18781
  if (cur_having || having_value != Item::COND_UNDEF)
18782
  {
18783
    str->append(STRING_WITH_LEN(" having "));
18784
    if (cur_having)
18785
      cur_having->print(str, query_type);
18786
    else
18787
      str->append(having_value != Item::COND_FALSE ? "1" : "0");
18788
  }
18789
18790
  if (order_list.elements)
18791
  {
18792
    str->append(STRING_WITH_LEN(" order by "));
18793
    print_order(str, (ORDER *) order_list.first, query_type);
18794
  }
18795
18796
  // limit
18797
  print_limit(thd, str, query_type);
18798
18799
  // PROCEDURE unsupported here
18800
}
18801
18802
18803
/**
18804
  change select_result object of JOIN.
18805
18806
  @param res		new select_result object
18807
18808
  @retval
18809
    FALSE   OK
18810
  @retval
18811
    TRUE    error
18812
*/
18813
18814
bool JOIN::change_result(select_result *res)
18815
{
18816
  DBUG_ENTER("JOIN::change_result");
18817
  result= res;
18818
  if (result->prepare(fields_list, select_lex->master_unit()) ||
18819
                     result->prepare2())
18820
  {
18821
    DBUG_RETURN(TRUE);
18822
  }
18823
  DBUG_RETURN(FALSE);
18824
}
18825
18826
/**
18827
  @} (end of group Query_Optimizer)
18828
*/