~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

MergedĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file
 
23
 *
 
24
 * Implementation of the JOIN class
 
25
 * 
 
26
 * @defgroup Query_Optimizer  Query Optimizer
 
27
 * @{
 
28
 */
 
29
 
 
30
#include "drizzled/server_includes.h"
 
31
#include "drizzled/table_map_iterator.h"
 
32
#include "drizzled/item/cache.h"
 
33
#include "drizzled/item/cmpfunc.h"
 
34
#include "drizzled/item/copy_string.h"
 
35
#include "drizzled/item/uint.h"
 
36
#include "drizzled/cached_item.h"
 
37
#include "drizzled/sql_base.h"
 
38
#include "drizzled/sql_select.h" /* include join.h */
 
39
#include "drizzled/lock.h"
 
40
#include "drizzled/nested_join.h"
 
41
#include "drizzled/join.h"
 
42
#include "drizzled/join_cache.h"
 
43
#include "drizzled/show.h"
 
44
#include "drizzled/field/blob.h"
 
45
#include "drizzled/optimizer/position.h"
 
46
#include "drizzled/optimizer/sargable_param.h"
 
47
#include "drizzled/optimizer/key_use.h"
 
48
#include "mysys/my_bit.h"
 
49
 
 
50
#include <algorithm>
 
51
 
 
52
using namespace std;
 
53
using namespace drizzled;
 
54
 
 
55
/** Declarations of static functions used in this source file. */
 
56
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
57
static void calc_group_buffer(JOIN *join,order_st *group);
 
58
static bool alloc_group_fields(JOIN *join,order_st *group);
 
59
static uint32_t cache_record_length(JOIN *join, uint32_t index);
 
60
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
61
static bool get_best_combination(JOIN *join);
 
62
static void set_position(JOIN *join,
 
63
                         uint32_t index,
 
64
                         JoinTable *table,
 
65
                         optimizer::KeyUse *key);
 
66
static bool choose_plan(JOIN *join,table_map join_tables);
 
67
static void best_access_path(JOIN *join, JoinTable *s,
 
68
                             Session *session,
 
69
                             table_map remaining_tables,
 
70
                             uint32_t idx,
 
71
                             double record_count,
 
72
                             double read_time);
 
73
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
74
static bool greedy_search(JOIN *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
 
75
static bool best_extension_by_limited_search(JOIN *join,
 
76
                                             table_map remaining_tables,
 
77
                                             uint32_t idx,
 
78
                                             double record_count,
 
79
                                             double read_time,
 
80
                                             uint32_t depth,
 
81
                                             uint32_t prune_level);
 
82
static uint32_t determine_search_depth(JOIN* join);
 
83
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
84
static void make_outerjoin_info(JOIN *join);
 
85
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
86
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
87
static void update_depend_map(JOIN *join);
 
88
static void update_depend_map(JOIN *join, order_st *order);
 
89
static order_st *remove_constants(JOIN *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
 
90
static int return_zero_rows(JOIN *join,
 
91
                            select_result *res,
 
92
                            TableList *tables,
 
93
                            List<Item> &fields,
 
94
                            bool send_row,
 
95
                            uint64_t select_options,
 
96
                            const char *info,
 
97
                            Item *having);
 
98
static COND *simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top);
 
99
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields, Item *having);
 
100
static int setup_without_group(Session *session, 
 
101
                               Item **ref_pointer_array,
 
102
                               TableList *tables,
 
103
                               TableList *,
 
104
                               List<Item> &fields,
 
105
                               List<Item> &all_fields,
 
106
                               COND **conds,
 
107
                               order_st *order,
 
108
                               order_st *group,
 
109
                               bool *hidden_group_fields);
 
110
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
 
111
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
 
112
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
113
static void reset_nj_counters(List<TableList> *join_list);
 
114
static bool test_if_subpart(order_st *a,order_st *b);
 
115
static void restore_prev_nj_state(JoinTable *last);
 
116
static uint32_t make_join_orderinfo(JOIN *join);
 
117
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
 
118
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
 
119
 
 
120
/**
 
121
  Prepare of whole select (including sub queries in future).
 
122
 
 
123
  @todo
 
124
    Add check of calculation of GROUP functions and fields:
 
125
    SELECT COUNT(*)+table.col1 from table1;
 
126
 
 
127
  @retval
 
128
    -1   on error
 
129
  @retval
 
130
    0   on success
 
131
*/
 
132
int JOIN::prepare(Item ***rref_pointer_array,
 
133
                  TableList *tables_init,
 
134
                  uint32_t wild_num,
 
135
                  COND *conds_init,
 
136
                  uint32_t og_num,
 
137
                  order_st *order_init,
 
138
                  order_st *group_init,
 
139
                  Item *having_init,
 
140
                  Select_Lex *select_lex_arg,
 
141
                  Select_Lex_Unit *unit_arg)
 
142
{
 
143
  // to prevent double initialization on EXPLAIN
 
144
  if (optimized)
 
145
    return 0;
 
146
 
 
147
  conds= conds_init;
 
148
  order= order_init;
 
149
  group_list= group_init;
 
150
  having= having_init;
 
151
  tables_list= tables_init;
 
152
  select_lex= select_lex_arg;
 
153
  select_lex->join= this;
 
154
  join_list= &select_lex->top_join_list;
 
155
  union_part= unit_arg->is_union();
 
156
 
 
157
  session->lex->current_select->is_item_list_lookup= 1;
 
158
  /*
 
159
    If we have already executed SELECT, then it have not sense to prevent
 
160
    its table from update (see unique_table())
 
161
  */
 
162
  if (session->derived_tables_processing)
 
163
    select_lex->exclude_from_table_unique_test= true;
 
164
 
 
165
  /* Check that all tables, fields, conds and order are ok */
 
166
 
 
167
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
168
      setup_tables_and_check_access(session, &select_lex->context, join_list,
 
169
                                    tables_list, &select_lex->leaf_tables,
 
170
                                    false))
 
171
      return(-1);
 
172
 
 
173
  TableList *table_ptr;
 
174
  for (table_ptr= select_lex->leaf_tables;
 
175
       table_ptr;
 
176
       table_ptr= table_ptr->next_leaf)
 
177
    tables++;
 
178
 
 
179
  if (setup_wild(session, fields_list, &all_fields, wild_num) ||
 
180
      select_lex->setup_ref_array(session, og_num) ||
 
181
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
182
       &all_fields, 1) ||
 
183
      setup_without_group(session, (*rref_pointer_array), tables_list,
 
184
        select_lex->leaf_tables, fields_list,
 
185
        all_fields, &conds, order, group_list,
 
186
        &hidden_group_fields))
 
187
    return(-1);
 
188
 
 
189
  ref_pointer_array= *rref_pointer_array;
 
190
 
 
191
  if (having)
 
192
  {
 
193
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
194
    session->where="having clause";
 
195
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
196
    select_lex->having_fix_field= 1;
 
197
    bool having_fix_rc= (!having->fixed &&
 
198
       (having->fix_fields(session, &having) ||
 
199
        having->check_cols(1)));
 
200
    select_lex->having_fix_field= 0;
 
201
    if (having_fix_rc || session->is_error())
 
202
      return(-1);
 
203
    session->lex->allow_sum_func= save_allow_sum_func;
 
204
  }
 
205
 
 
206
  {
 
207
    Item_subselect *subselect;
 
208
    Item_in_subselect *in_subs= NULL;
 
209
    /*
 
210
      Are we in a subquery predicate?
 
211
      TODO: the block below will be executed for every PS execution without need.
 
212
    */
 
213
    if ((subselect= select_lex->master_unit()->item))
 
214
    {
 
215
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
216
        in_subs= (Item_in_subselect*)subselect;
 
217
 
 
218
      {
 
219
        bool do_materialize= !test(session->variables.optimizer_switch &
 
220
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
221
        /*
 
222
          Check if the subquery predicate can be executed via materialization.
 
223
          The required conditions are:
 
224
          1. Subquery predicate is an IN/=ANY subq predicate
 
225
          2. Subquery is a single SELECT (not a UNION)
 
226
          3. Subquery is not a table-less query. In this case there is no
 
227
             point in materializing.
 
228
          4. Subquery predicate is a top-level predicate
 
229
             (this implies it is not negated)
 
230
             TODO: this is a limitation that should be lifeted once we
 
231
             implement correct NULL semantics (WL#3830)
 
232
          5. Subquery is non-correlated
 
233
             TODO:
 
234
             This is an overly restrictive condition. It can be extended to:
 
235
             (Subquery is non-correlated ||
 
236
              Subquery is correlated to any query outer to IN predicate ||
 
237
              (Subquery is correlated to the immediate outer query &&
 
238
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
239
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
240
          6. No execution method was already chosen (by a prepared statement).
 
241
 
 
242
          (*) The subquery must be part of a SELECT statement. The current
 
243
               condition also excludes multi-table update statements.
 
244
 
 
245
          We have to determine whether we will perform subquery materialization
 
246
          before calling the IN=>EXISTS transformation, so that we know whether to
 
247
          perform the whole transformation or only that part of it which wraps
 
248
          Item_in_subselect in an Item_in_optimizer.
 
249
        */
 
250
        if (do_materialize &&
 
251
            in_subs  &&                                                   // 1
 
252
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
253
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
254
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
255
        {
 
256
          if (in_subs->is_top_level_item() &&                             // 4
 
257
              !in_subs->is_correlated &&                                  // 5
 
258
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
259
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
260
        }
 
261
 
 
262
        Item_subselect::trans_res trans_res;
 
263
        if ((trans_res= subselect->select_transformer(this)) !=
 
264
            Item_subselect::RES_OK)
 
265
        {
 
266
          return((trans_res == Item_subselect::RES_ERROR));
 
267
        }
 
268
      }
 
269
    }
 
270
  }
 
271
 
 
272
  if (order)
 
273
  {
 
274
    order_st *ord;
 
275
    for (ord= order; ord; ord= ord->next)
 
276
    {
 
277
      Item *item= *ord->item;
 
278
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
279
        item->split_sum_func(session, ref_pointer_array, all_fields);
 
280
    }
 
281
  }
 
282
 
 
283
  if (having && having->with_sum_func)
 
284
    having->split_sum_func(session, ref_pointer_array, all_fields,
 
285
                           &having, true);
 
286
  if (select_lex->inner_sum_func_list)
 
287
  {
 
288
    Item_sum *end=select_lex->inner_sum_func_list;
 
289
    Item_sum *item_sum= end;
 
290
    do
 
291
    {
 
292
      item_sum= item_sum->next;
 
293
      item_sum->split_sum_func(session, ref_pointer_array,
 
294
                               all_fields, item_sum->ref_by, false);
 
295
    } while (item_sum != end);
 
296
  }
 
297
 
 
298
  if (select_lex->inner_refs_list.elements &&
 
299
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
 
300
    return(-1);
 
301
 
 
302
  /*
 
303
    Check if there are references to un-aggregated columns when computing
 
304
    aggregate functions with implicit grouping (there is no GROUP BY).
 
305
 
 
306
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
307
  */
 
308
  if (! group_list && 
 
309
      select_lex->full_group_by_flag.test(NON_AGG_FIELD_USED) &&
 
310
      select_lex->full_group_by_flag.test(SUM_FUNC_USED))
 
311
  {
 
312
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
313
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
314
    return(-1);
 
315
  }
 
316
  {
 
317
    /* Caclulate the number of groups */
 
318
    send_group_parts= 0;
 
319
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
320
      send_group_parts++;
 
321
  }
 
322
 
 
323
  if (error)
 
324
    goto err;
 
325
 
 
326
  if (result && result->prepare(fields_list, unit_arg))
 
327
    goto err;
 
328
 
 
329
  /* Init join struct */
 
330
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
331
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
332
  this->group= group_list != 0;
 
333
  unit= unit_arg;
 
334
 
 
335
#ifdef RESTRICTED_GROUP
 
336
  if (sum_func_count && !group_list && (func_count || field_count))
 
337
  {
 
338
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
339
    goto err;
 
340
  }
 
341
#endif
 
342
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
343
    goto err;
 
344
  if (alloc_func_list())
 
345
    goto err;
 
346
 
 
347
  return(0); // All OK
 
348
 
 
349
err:
 
350
  return(-1);
 
351
}
 
352
 
 
353
/*
 
354
  Remove the predicates pushed down into the subquery
 
355
 
 
356
  SYNOPSIS
 
357
    JOIN::remove_subq_pushed_predicates()
 
358
      where   IN  Must be NULL
 
359
              OUT The remaining WHERE condition, or NULL
 
360
 
 
361
  DESCRIPTION
 
362
    Given that this join will be executed using (unique|index)_subquery,
 
363
    without "checking NULL", remove the predicates that were pushed down
 
364
    into the subquery.
 
365
 
 
366
    If the subquery compares scalar values, we can remove the condition that
 
367
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
368
    engine)
 
369
 
 
370
    If the subquery compares row values, we need to keep the wrapped
 
371
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
372
    and non-NULL values, we'll do a full table scan and will rely on the
 
373
    equalities corresponding to non-NULL parts of left tuple to filter out
 
374
    non-matching records.
 
375
 
 
376
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
377
    fact that subquery engine will be using index lookup. This must be done only
 
378
    for cases where there are no conversion errors of significance, e.g. 257
 
379
    that is searched in a byte. But this requires homogenization of the return
 
380
    codes of all Field*::store() methods.
 
381
*/
 
382
void JOIN::remove_subq_pushed_predicates(Item **where)
 
383
{
 
384
  if (conds->type() == Item::FUNC_ITEM &&
 
385
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
386
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
387
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
388
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
389
                   ((Item_func *)conds)->arguments()[0]))
 
390
  {
 
391
    *where= 0;
 
392
    return;
 
393
  }
 
394
}
 
395
 
 
396
/**
 
397
  global select optimisation.
 
398
 
 
399
  @note
 
400
    error code saved in field 'error'
 
401
 
 
402
  @retval
 
403
    0   success
 
404
  @retval
 
405
    1   error
 
406
*/
 
407
int JOIN::optimize()
 
408
{
 
409
  // to prevent double initialization on EXPLAIN
 
410
  if (optimized)
 
411
    return 0;
 
412
  optimized= 1;
 
413
 
 
414
  session->set_proc_info("optimizing");
 
415
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
416
        unit->select_limit_cnt);
 
417
  /* select_limit is used to decide if we are likely to scan the whole table */
 
418
  select_limit= unit->select_limit_cnt;
 
419
  if (having || (select_options & OPTION_FOUND_ROWS))
 
420
    select_limit= HA_POS_ERROR;
 
421
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
422
  // Ignore errors of execution if option IGNORE present
 
423
  if (session->lex->ignore)
 
424
    session->lex->current_select->no_error= 1;
 
425
 
 
426
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
 
427
  /* Add HAVING to WHERE if possible */
 
428
  if (having && !group_list && !sum_func_count)
 
429
  {
 
430
    if (!conds)
 
431
    {
 
432
      conds= having;
 
433
      having= 0;
 
434
    }
 
435
    else if ((conds=new Item_cond_and(conds,having)))
 
436
    {
 
437
      /*
 
438
        Item_cond_and can't be fixed after creation, so we do not check
 
439
        conds->fixed
 
440
      */
 
441
      conds->fix_fields(session, &conds);
 
442
      conds->change_ref_to_fields(session, tables_list);
 
443
      conds->top_level_item();
 
444
      having= 0;
 
445
    }
 
446
  }
 
447
#endif
 
448
 
 
449
  /* Convert all outer joins to inner joins if possible */
 
450
  conds= simplify_joins(this, join_list, conds, true);
 
451
  build_bitmap_for_nested_joins(join_list, 0);
 
452
 
 
453
  conds= optimize_cond(this, conds, join_list, &cond_value);
 
454
  if (session->is_error())
 
455
  {
 
456
    error= 1;
 
457
    return 1;
 
458
  }
 
459
 
 
460
  {
 
461
    having= optimize_cond(this, having, join_list, &having_value);
 
462
    if (session->is_error())
 
463
    {
 
464
      error= 1;
 
465
      return 1;
 
466
    }
 
467
    if (select_lex->where)
 
468
      select_lex->cond_value= cond_value;
 
469
    if (select_lex->having)
 
470
      select_lex->having_value= having_value;
 
471
 
 
472
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
 
473
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
474
    {           /* Impossible cond */
 
475
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
476
                           "Impossible HAVING" : "Impossible WHERE";
 
477
      error= 0;
 
478
      return(0);
 
479
    }
 
480
  }
 
481
 
 
482
  /* Optimize count(*), cmin() and cmax() */
 
483
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
484
  {
 
485
    int res;
 
486
    /*
 
487
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
488
      to the WHERE conditions,
 
489
      or 1 if all items were resolved,
 
490
      or 0, or an error number HA_ERR_...
 
491
    */
 
492
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
493
    {
 
494
      if (res == HA_ERR_KEY_NOT_FOUND)
 
495
      {
 
496
        zero_result_cause= "No matching min/max row";
 
497
        error=0;
 
498
        return(0);
 
499
      }
 
500
      if (res > 1)
 
501
      {
 
502
        error= res;
 
503
        return 1;
 
504
      }
 
505
      if (res < 0)
 
506
      {
 
507
        zero_result_cause= "No matching min/max row";
 
508
        error=0;
 
509
        return(0);
 
510
      }
 
511
      zero_result_cause= "Select tables optimized away";
 
512
      tables_list= 0;       // All tables resolved
 
513
      /*
 
514
        Extract all table-independent conditions and replace the WHERE
 
515
        clause with them. All other conditions were computed by opt_sum_query
 
516
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
517
        so there is no need to compute the whole WHERE clause again.
 
518
        Notice that make_cond_for_table() will always succeed to remove all
 
519
        computed conditions, because opt_sum_query() is applicable only to
 
520
        conjunctions.
 
521
        Preserve conditions for EXPLAIN.
 
522
      */
 
523
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
524
      {
 
525
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
526
        conds= table_independent_conds;
 
527
      }
 
528
    }
 
529
  }
 
530
  if (!tables_list)
 
531
  {
 
532
    error= 0;
 
533
    return(0);
 
534
  }
 
535
  error= -1;          // Error is sent to client
 
536
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
537
 
 
538
  /* Calculate how to do the join */
 
539
  session->set_proc_info("statistics");
 
540
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
541
      session->is_fatal_error)
 
542
  {
 
543
    return 1;
 
544
  }
 
545
 
 
546
  /* Remove distinct if only const tables */
 
547
  select_distinct= select_distinct && (const_tables != tables);
 
548
  session->set_proc_info("preparing");
 
549
  if (result->initialize_tables(this))
 
550
  {
 
551
    return 1;        // error == -1
 
552
  }
 
553
  if (const_table_map != found_const_table_map &&
 
554
      !(select_options & SELECT_DESCRIBE) &&
 
555
      (!conds ||
 
556
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
557
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
558
  {
 
559
    zero_result_cause= "no matching row in const table";
 
560
    error= 0;
 
561
    return(0);
 
562
  }
 
563
  if (!(session->options & OPTION_BIG_SELECTS) &&
 
564
      best_read > (double) session->variables.max_join_size &&
 
565
      !(select_options & SELECT_DESCRIBE))
 
566
  {
 
567
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
568
    error= -1;
 
569
    return 1;
 
570
  }
 
571
  if (const_tables && !(select_options & SELECT_NO_UNLOCK))
 
572
    mysql_unlock_some_tables(session, table, const_tables);
 
573
  if (!conds && outer_join)
 
574
  {
 
575
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
576
    conds=new Item_int((int64_t) 1,1);  // Always true
 
577
  }
 
578
  select= make_select(*table, const_table_map,
 
579
                      const_table_map, conds, 1, &error);
 
580
  if (error)
 
581
  {
 
582
    error= -1;
 
583
    return 1;
 
584
  }
 
585
 
 
586
  reset_nj_counters(join_list);
 
587
  make_outerjoin_info(this);
 
588
 
 
589
  /*
 
590
    Among the equal fields belonging to the same multiple equality
 
591
    choose the one that is to be retrieved first and substitute
 
592
    all references to these in where condition for a reference for
 
593
    the selected field.
 
594
  */
 
595
  if (conds)
 
596
  {
 
597
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
598
    conds->update_used_tables();
 
599
  }
 
600
 
 
601
  /*
 
602
    Permorm the the optimization on fields evaluation mentioned above
 
603
    for all on expressions.
 
604
  */
 
605
  for (JoinTable *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
606
  {
 
607
    if (*tab->on_expr_ref)
 
608
    {
 
609
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
610
                                                         tab->cond_equal,
 
611
                                                         map2table);
 
612
      (*tab->on_expr_ref)->update_used_tables();
 
613
    }
 
614
  }
 
615
 
 
616
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
 
617
      (select_options & SELECT_DESCRIBE) &&
 
618
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
619
  {
 
620
    conds=new Item_int((int64_t) 0,1);  // Always false
 
621
  }
 
622
  if (make_join_select(this, select, conds))
 
623
  {
 
624
    zero_result_cause=
 
625
      "Impossible WHERE noticed after reading const tables";
 
626
    return(0);        // error == 0
 
627
  }
 
628
 
 
629
  error= -1;          /* if goto err */
 
630
 
 
631
  /* Optimize distinct away if possible */
 
632
  {
 
633
    order_st *org_order= order;
 
634
    order= remove_constants(this, order,conds,1, &simple_order);
 
635
    if (session->is_error())
 
636
    {
 
637
      error= 1;
 
638
      return 1;
 
639
    }
 
640
 
 
641
    /*
 
642
      If we are using order_st BY NULL or order_st BY const_expression,
 
643
      return result in any order (even if we are using a GROUP BY)
 
644
    */
 
645
    if (!order && org_order)
 
646
      skip_sort_order= 1;
 
647
  }
 
648
  /*
 
649
     Check if we can optimize away GROUP BY/DISTINCT.
 
650
     We can do that if there are no aggregate functions, the
 
651
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
652
     (if present) contain direct references to all key parts of
 
653
     an unique index (in whatever order) and if the key parts of the
 
654
     unique index cannot contain NULLs.
 
655
     Note that the unique keys for DISTINCT and GROUP BY should not
 
656
     be the same (as long as they are unique).
 
657
 
 
658
     The FROM clause must contain a single non-constant table.
 
659
  */
 
660
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
661
      !tmp_table_param.sum_func_count &&
 
662
      (!join_tab[const_tables].select ||
 
663
       !join_tab[const_tables].select->quick ||
 
664
       join_tab[const_tables].select->quick->get_type() !=
 
665
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
666
  {
 
667
    if (group_list && list_contains_unique_index(join_tab[const_tables].table, find_field_in_order_list, (void *) group_list))
 
668
    {
 
669
      /*
 
670
        We have found that grouping can be removed since groups correspond to
 
671
        only one row anyway, but we still have to guarantee correct result
 
672
        order. The line below effectively rewrites the query from GROUP BY
 
673
        <fields> to order_st BY <fields>. There are two exceptions:
 
674
        - if skip_sort_order is set (see above), then we can simply skip
 
675
          GROUP BY;
 
676
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
677
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
678
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
679
          test_if_subpart() copies the ASC/DESC attributes from the original
 
680
          order_st BY fields.
 
681
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
682
          'order' as is.
 
683
       */
 
684
      if (!order || test_if_subpart(group_list, order))
 
685
          order= skip_sort_order ? 0 : group_list;
 
686
      /*
 
687
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
 
688
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
689
      */
 
690
      join_tab->table->keys_in_use_for_order_by=
 
691
        join_tab->table->keys_in_use_for_group_by;
 
692
      group_list= 0;
 
693
      group= 0;
 
694
    }
 
695
    if (select_distinct &&
 
696
       list_contains_unique_index(join_tab[const_tables].table,
 
697
                                 find_field_in_item_list,
 
698
                                 (void *) &fields_list))
 
699
    {
 
700
      select_distinct= 0;
 
701
    }
 
702
  }
 
703
  if (group_list || tmp_table_param.sum_func_count)
 
704
  {
 
705
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
706
      select_distinct=0;
 
707
  }
 
708
  else if (select_distinct && tables - const_tables == 1)
 
709
  {
 
710
    /*
 
711
      We are only using one table. In this case we change DISTINCT to a
 
712
      GROUP BY query if:
 
713
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
714
        BY only uses selected fields.
 
715
        (In this case we can later optimize away GROUP BY and order_st BY)
 
716
      - We are scanning the whole table without LIMIT
 
717
        This can happen if:
 
718
        - We are using CALC_FOUND_ROWS
 
719
        - We are using an order_st BY that can't be optimized away.
 
720
 
 
721
      We don't want to use this optimization when we are using LIMIT
 
722
      because in this case we can just create a temporary table that
 
723
      holds LIMIT rows and stop when this table is full.
 
724
    */
 
725
    JoinTable *tab= &join_tab[const_tables];
 
726
    bool all_order_fields_used;
 
727
    if (order)
 
728
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
 
729
        &tab->table->keys_in_use_for_order_by);
 
730
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
 
731
                                          order, fields_list, all_fields,
 
732
                  &all_order_fields_used)))
 
733
    {
 
734
      bool skip_group= (skip_sort_order &&
 
735
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
 
736
                                &tab->table->keys_in_use_for_group_by) != 0);
 
737
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
738
      if ((skip_group && all_order_fields_used) ||
 
739
          select_limit == HA_POS_ERROR ||
 
740
          (order && !skip_sort_order))
 
741
      {
 
742
        /*  Change DISTINCT to GROUP BY */
 
743
        select_distinct= 0;
 
744
        no_order= !order;
 
745
        if (all_order_fields_used)
 
746
        {
 
747
          if (order && skip_sort_order)
 
748
          {
 
749
            /*
 
750
              Force MySQL to read the table in sorted order to get result in
 
751
              order_st BY order.
 
752
            */
 
753
            tmp_table_param.quick_group=0;
 
754
          }
 
755
          order=0;
 
756
        }
 
757
        group=1;        // For end_write_group
 
758
      }
 
759
      else
 
760
        group_list= 0;
 
761
    }
 
762
    else if (session->is_fatal_error)     // End of memory
 
763
      return 1;
 
764
  }
 
765
  simple_group= 0;
 
766
  {
 
767
    order_st *old_group_list;
 
768
    group_list= remove_constants(this, (old_group_list= group_list), conds,
 
769
                                 rollup.state == ROLLUP::STATE_NONE,
 
770
                                 &simple_group);
 
771
    if (session->is_error())
 
772
    {
 
773
      error= 1;
 
774
      return 1;
 
775
    }
 
776
    if (old_group_list && !group_list)
 
777
      select_distinct= 0;
 
778
  }
 
779
  if (!group_list && group)
 
780
  {
 
781
    order=0;          // The output has only one row
 
782
    simple_order=1;
 
783
    select_distinct= 0;                       // No need in distinct for 1 row
 
784
    group_optimized_away= 1;
 
785
  }
 
786
 
 
787
  calc_group_buffer(this, group_list);
 
788
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
789
 
 
790
  if (test_if_subpart(group_list, order) ||
 
791
      (!group_list && tmp_table_param.sum_func_count))
 
792
    order=0;
 
793
 
 
794
  // Can't use sort on head table if using row cache
 
795
  if (full_join)
 
796
  {
 
797
    if (group_list)
 
798
      simple_group=0;
 
799
    if (order)
 
800
      simple_order=0;
 
801
  }
 
802
 
 
803
  /*
 
804
    Check if we need to create a temporary table.
 
805
    This has to be done if all tables are not already read (const tables)
 
806
    and one of the following conditions holds:
 
807
    - We are using DISTINCT (simple distinct's are already optimized away)
 
808
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
809
    - We are using different order_st BY and GROUP BY orders
 
810
    - The user wants us to buffer the result.
 
811
  */
 
812
  need_tmp= (const_tables != tables &&
 
813
       ((select_distinct || !simple_order || !simple_group) ||
 
814
        (group_list && order) ||
 
815
        test(select_options & OPTION_BUFFER_RESULT)));
 
816
 
 
817
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
818
  uint64_t select_opts_for_readinfo=
 
819
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
820
 
 
821
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
822
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
823
    return 1;
 
824
 
 
825
  /* Create all structures needed for materialized subquery execution. */
 
826
  if (setup_subquery_materialization())
 
827
    return 1;
 
828
 
 
829
  /*
 
830
    is this simple IN subquery?
 
831
  */
 
832
  if (!group_list && !order &&
 
833
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
834
      tables == 1 && conds &&
 
835
      !unit->is_union())
 
836
  {
 
837
    if (!having)
 
838
    {
 
839
      Item *where= conds;
 
840
      if (join_tab[0].type == AM_EQ_REF && join_tab[0].ref.items[0]->name == in_left_expr_name)
 
841
      {
 
842
        remove_subq_pushed_predicates(&where);
 
843
        save_index_subquery_explain_info(join_tab, where);
 
844
        join_tab[0].type= AM_UNIQUE_SUBQUERY;
 
845
        error= 0;
 
846
        return(unit->item->
 
847
                    change_engine(new
 
848
                                  subselect_uniquesubquery_engine(session,
 
849
                                                                  join_tab,
 
850
                                                                  unit->item,
 
851
                                                                  where)));
 
852
      }
 
853
      else if (join_tab[0].type == AM_REF &&
 
854
         join_tab[0].ref.items[0]->name == in_left_expr_name)
 
855
      {
 
856
        remove_subq_pushed_predicates(&where);
 
857
        save_index_subquery_explain_info(join_tab, where);
 
858
        join_tab[0].type= AM_INDEX_SUBQUERY;
 
859
        error= 0;
 
860
        return(unit->item->
 
861
                    change_engine(new
 
862
                                  subselect_indexsubquery_engine(session,
 
863
                                                                 join_tab,
 
864
                                                                 unit->item,
 
865
                                                                 where,
 
866
                                                                 NULL,
 
867
                                                                 0)));
 
868
      }
 
869
    } 
 
870
    else if (join_tab[0].type == AM_REF_OR_NULL &&
 
871
         join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
872
               having->name == in_having_cond)
 
873
    {
 
874
      join_tab[0].type= AM_INDEX_SUBQUERY;
 
875
      error= 0;
 
876
      conds= remove_additional_cond(conds);
 
877
      save_index_subquery_explain_info(join_tab, conds);
 
878
      return(unit->item->
 
879
      change_engine(new subselect_indexsubquery_engine(session,
 
880
                   join_tab,
 
881
                   unit->item,
 
882
                   conds,
 
883
                                                                   having,
 
884
                   1)));
 
885
    }
 
886
 
 
887
  }
 
888
  /*
 
889
    Need to tell handlers that to play it safe, it should fetch all
 
890
    columns of the primary key of the tables: this is because MySQL may
 
891
    build row pointers for the rows, and for all columns of the primary key
 
892
    the read set has not necessarily been set by the server code.
 
893
  */
 
894
  if (need_tmp || select_distinct || group_list || order)
 
895
  {
 
896
    for (uint32_t i = const_tables; i < tables; i++)
 
897
      join_tab[i].table->prepare_for_position();
 
898
  }
 
899
 
 
900
  if (const_tables != tables)
 
901
  {
 
902
    /*
 
903
      Because filesort always does a full table scan or a quick range scan
 
904
      we must add the removed reference to the select for the table.
 
905
      We only need to do this when we have a simple_order or simple_group
 
906
      as in other cases the join is done before the sort.
 
907
    */
 
908
    if ((order || group_list) &&
 
909
        (join_tab[const_tables].type != AM_ALL) &&
 
910
        (join_tab[const_tables].type != AM_REF_OR_NULL) &&
 
911
        ((order && simple_order) || (group_list && simple_group)))
 
912
    {
 
913
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
 
914
        return 1;
 
915
      }
 
916
    }
 
917
 
 
918
    if (!(select_options & SELECT_BIG_RESULT) &&
 
919
        ((group_list &&
 
920
          (!simple_group ||
 
921
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
922
                                    unit->select_limit_cnt, 0,
 
923
                                    &join_tab[const_tables].table->
 
924
                                    keys_in_use_for_group_by))) ||
 
925
         select_distinct) &&
 
926
        tmp_table_param.quick_group)
 
927
    {
 
928
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
929
    }
 
930
    if (order)
 
931
    {
 
932
      /*
 
933
        Force using of tmp table if sorting by a SP or UDF function due to
 
934
        their expensive and probably non-deterministic nature.
 
935
      */
 
936
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
937
      {
 
938
        Item *item= *tmp_order->item;
 
939
        if (item->is_expensive())
 
940
        {
 
941
          /* Force tmp table without sort */
 
942
          need_tmp=1; simple_order=simple_group=0;
 
943
          break;
 
944
        }
 
945
      }
 
946
    }
 
947
  }
 
948
 
 
949
  tmp_having= having;
 
950
  if (select_options & SELECT_DESCRIBE)
 
951
  {
 
952
    error= 0;
 
953
    return(0);
 
954
  }
 
955
  having= 0;
 
956
 
 
957
  /*
 
958
    The loose index scan access method guarantees that all grouping or
 
959
    duplicate row elimination (for distinct) is already performed
 
960
    during data retrieval, and that all MIN/MAX functions are already
 
961
    computed for each group. Thus all MIN/MAX functions should be
 
962
    treated as regular functions, and there is no need to perform
 
963
    grouping in the main execution loop.
 
964
    Notice that currently loose index scan is applicable only for
 
965
    single table queries, thus it is sufficient to test only the first
 
966
    join_tab element of the plan for its access method.
 
967
  */
 
968
  if (join_tab->is_using_loose_index_scan())
 
969
    tmp_table_param.precomputed_group_by= true;
 
970
 
 
971
  /* Create a tmp table if distinct or if the sort is too complicated */
 
972
  if (need_tmp)
 
973
  {
 
974
    session->set_proc_info("Creating tmp table");
 
975
 
 
976
    init_items_ref_array();
 
977
 
 
978
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
979
           fields_list.elements);
 
980
    order_st *tmp_group= ((!simple_group && 
 
981
                           ! (test_flags.test(TEST_NO_KEY_GROUP))) ? group_list :
 
982
                                                                     (order_st*) 0);
 
983
    /*
 
984
      Pushing LIMIT to the temporary table creation is not applicable
 
985
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
986
      there are aggregate functions, because in all these cases we need
 
987
      all result rows.
 
988
    */
 
989
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
990
                             !tmp_group &&
 
991
                             !session->lex->current_select->with_sum_func) ?
 
992
                            select_limit : HA_POS_ERROR;
 
993
 
 
994
    if (!(exec_tmp_table1=
 
995
          create_tmp_table(session, &tmp_table_param, all_fields,
 
996
                           tmp_group,
 
997
                           group_list ? 0 : select_distinct,
 
998
                           group_list && simple_group,
 
999
                           select_options,
 
1000
                           tmp_rows_limit,
 
1001
                           (char *) "")))
 
1002
    {
 
1003
      return 1;
 
1004
    }
 
1005
 
 
1006
    /*
 
1007
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1008
      - we are sorting the table and writing complete group rows to the
 
1009
        temp table.
 
1010
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1011
        on all columns.
 
1012
 
 
1013
      If having is not handled here, it will be checked before the row
 
1014
      is sent to the client.
 
1015
    */
 
1016
    if (tmp_having && (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1017
      having= tmp_having;
 
1018
 
 
1019
    /* if group or order on first table, sort first */
 
1020
    if (group_list && simple_group)
 
1021
    {
 
1022
      session->set_proc_info("Sorting for group");
 
1023
      if (create_sort_index(session, this, group_list,
 
1024
          HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1025
          alloc_group_fields(this, group_list) ||
 
1026
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1027
          setup_sum_funcs(session, sum_funcs))
 
1028
      {
 
1029
        return 1;
 
1030
      }
 
1031
      group_list=0;
 
1032
    }
 
1033
    else
 
1034
    {
 
1035
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1036
          setup_sum_funcs(session, sum_funcs))
 
1037
      {
 
1038
        return 1;
 
1039
      }
 
1040
 
 
1041
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1042
      {
 
1043
        session->set_proc_info("Sorting for order");
 
1044
        if (create_sort_index(session, this, order,
 
1045
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1046
        {
 
1047
          return 1;
 
1048
        }
 
1049
        order=0;
 
1050
      }
 
1051
    }
 
1052
 
 
1053
    /*
 
1054
      Optimize distinct when used on some of the tables
 
1055
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1056
      In this case we can stop scanning t2 when we have found one t1.a
 
1057
    */
 
1058
 
 
1059
    if (exec_tmp_table1->distinct)
 
1060
    {
 
1061
      table_map used_tables= session->used_tables;
 
1062
      JoinTable *last_join_tab= join_tab+tables-1;
 
1063
      do
 
1064
      {
 
1065
        if (used_tables & last_join_tab->table->map)
 
1066
          break;
 
1067
        last_join_tab->not_used_in_distinct=1;
 
1068
      } while (last_join_tab-- != join_tab);
 
1069
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1070
      if (order && skip_sort_order)
 
1071
      {
 
1072
        /* Should always succeed */
 
1073
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1074
                  order, unit->select_limit_cnt, 0,
 
1075
                                          &join_tab[const_tables].table->
 
1076
                                            keys_in_use_for_order_by))
 
1077
          order= 0;
 
1078
      }
 
1079
    }
 
1080
 
 
1081
    /*
 
1082
      If this join belongs to an uncacheable subquery save
 
1083
      the original join
 
1084
    */
 
1085
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1086
        init_save_join_tab())
 
1087
      return(-1);
 
1088
  }
 
1089
 
 
1090
  error= 0;
 
1091
  return(0);
 
1092
}
 
1093
 
 
1094
/**
 
1095
  Restore values in temporary join.
 
1096
*/
 
1097
void JOIN::restore_tmp()
 
1098
{
 
1099
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1100
}
 
1101
 
 
1102
int JOIN::reinit()
 
1103
{
 
1104
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1105
                                    select_lex->offset_limit->val_uint() :
 
1106
                                    0UL);
 
1107
 
 
1108
  first_record= 0;
 
1109
 
 
1110
  if (exec_tmp_table1)
 
1111
  {
 
1112
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1113
    exec_tmp_table1->file->ha_delete_all_rows();
 
1114
    exec_tmp_table1->free_io_cache();
 
1115
    exec_tmp_table1->filesort_free_buffers();
 
1116
  }
 
1117
  if (exec_tmp_table2)
 
1118
  {
 
1119
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1120
    exec_tmp_table2->file->ha_delete_all_rows();
 
1121
    exec_tmp_table2->free_io_cache();
 
1122
    exec_tmp_table2->filesort_free_buffers();
 
1123
  }
 
1124
  if (items0)
 
1125
    set_items_ref_array(items0);
 
1126
 
 
1127
  if (join_tab_save)
 
1128
    memcpy(join_tab, join_tab_save, sizeof(JoinTable) * tables);
 
1129
 
 
1130
  if (tmp_join)
 
1131
    restore_tmp();
 
1132
 
 
1133
  /* Reset of sum functions */
 
1134
  if (sum_funcs)
 
1135
  {
 
1136
    Item_sum *func, **func_ptr= sum_funcs;
 
1137
    while ((func= *(func_ptr++)))
 
1138
      func->clear();
 
1139
  }
 
1140
 
 
1141
  return(0);
 
1142
}
 
1143
 
 
1144
/**
 
1145
   @brief Save the original join layout
 
1146
 
 
1147
   @details Saves the original join layout so it can be reused in
 
1148
   re-execution and for EXPLAIN.
 
1149
 
 
1150
   @return Operation status
 
1151
   @retval 0      success.
 
1152
   @retval 1      error occurred.
 
1153
*/
 
1154
bool JOIN::init_save_join_tab()
 
1155
{
 
1156
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
 
1157
    return 1;
 
1158
  error= 0;              // Ensure that tmp_join.error= 0
 
1159
  restore_tmp();
 
1160
  return 0;
 
1161
}
 
1162
 
 
1163
bool JOIN::save_join_tab()
 
1164
{
 
1165
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
1166
  {
 
1167
    if (!(join_tab_save= (JoinTable*)session->memdup((unsigned char*) join_tab,
 
1168
            sizeof(JoinTable) * tables)))
 
1169
      return 1;
 
1170
  }
 
1171
  return 0;
 
1172
}
 
1173
 
 
1174
/**
 
1175
  Exec select.
 
1176
 
 
1177
  @todo
 
1178
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
1179
    finally replace sorting with index scan if there is a LIMIT clause in
 
1180
    the query.  It's never shown in EXPLAIN!
 
1181
 
 
1182
  @todo
 
1183
    When can we have here session->net.report_error not zero?
 
1184
*/
 
1185
void JOIN::exec()
 
1186
{
 
1187
  List<Item> *columns_list= &fields_list;
 
1188
  int      tmp_error;
 
1189
 
 
1190
  session->set_proc_info("executing");
 
1191
  error= 0;
 
1192
 
 
1193
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
1194
  {                                           
 
1195
    /* Only test of functions */
 
1196
    if (select_options & SELECT_DESCRIBE)
 
1197
      select_describe(this, false, false, false, (zero_result_cause?zero_result_cause:"No tables used"));
 
1198
    else
 
1199
    {
 
1200
      result->send_fields(*columns_list);
 
1201
      /*
 
1202
        We have to test for 'conds' here as the WHERE may not be constant
 
1203
        even if we don't have any tables for prepared statements or if
 
1204
        conds uses something like 'rand()'.
 
1205
      */
 
1206
      if (cond_value != Item::COND_FALSE &&
 
1207
          (!conds || conds->val_int()) &&
 
1208
          (!having || having->val_int()))
 
1209
      {
 
1210
        if (do_send_rows && result->send_data(fields_list))
 
1211
          error= 1;
 
1212
        else
 
1213
        {
 
1214
          error= (int) result->send_eof();
 
1215
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
 
1216
        }
 
1217
      }
 
1218
      else
 
1219
      {
 
1220
        error= (int) result->send_eof();
 
1221
        send_records= 0;
 
1222
      }
 
1223
    }
 
1224
    /* Single select (without union) always returns 0 or 1 row */
 
1225
    session->limit_found_rows= send_records;
 
1226
    session->examined_row_count= 0;
 
1227
    return;
 
1228
  }
 
1229
  /*
 
1230
    Don't reset the found rows count if there're no tables as
 
1231
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
1232
    It must be accumulated from all join iterations of all join parts.
 
1233
  */
 
1234
  if (tables)
 
1235
    session->limit_found_rows= 0;
 
1236
 
 
1237
  if (zero_result_cause)
 
1238
  {
 
1239
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
1240
                            *columns_list,
 
1241
          send_row_on_empty_set(),
 
1242
          select_options,
 
1243
          zero_result_cause,
 
1244
          having);
 
1245
    return;
 
1246
  }
 
1247
 
 
1248
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
1249
    return;
 
1250
 
 
1251
  if (select_options & SELECT_DESCRIBE)
 
1252
  {
 
1253
    /*
 
1254
      Check if we managed to optimize order_st BY away and don't use temporary
 
1255
      table to resolve order_st BY: in that case, we only may need to do
 
1256
      filesort for GROUP BY.
 
1257
    */
 
1258
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
1259
    {
 
1260
      /* Reset 'order' to 'group_list' and reinit variables describing 'order' */
 
1261
      order= group_list;
 
1262
      simple_order= simple_group;
 
1263
      skip_sort_order= 0;
 
1264
    }
 
1265
    if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
 
1266
    {
 
1267
      if (const_tables == tables 
 
1268
        || ((simple_order || skip_sort_order) 
 
1269
          && test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
 
1270
      order= 0;
 
1271
    }
 
1272
    having= tmp_having;
 
1273
    select_describe(this, need_tmp, order != 0 && !skip_sort_order,  select_distinct, !tables ? "No tables used" : NULL);
 
1274
    return;
 
1275
  }
 
1276
 
 
1277
  JOIN *curr_join= this;
 
1278
  List<Item> *curr_all_fields= &all_fields;
 
1279
  List<Item> *curr_fields_list= &fields_list;
 
1280
  Table *curr_tmp_table= 0;
 
1281
  /*
 
1282
    Initialize examined rows here because the values from all join parts
 
1283
    must be accumulated in examined_row_count. Hence every join
 
1284
    iteration must count from zero.
 
1285
  */
 
1286
  curr_join->examined_rows= 0;
 
1287
 
 
1288
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1289
  if (need_tmp)
 
1290
  {
 
1291
    if (tmp_join)
 
1292
    {
 
1293
      /*
 
1294
        We are in a non cacheable sub query. Get the saved join structure
 
1295
        after optimization.
 
1296
        (curr_join may have been modified during last exection and we need
 
1297
        to reset it)
 
1298
      */
 
1299
      curr_join= tmp_join;
 
1300
    }
 
1301
    curr_tmp_table= exec_tmp_table1;
 
1302
 
 
1303
    /* Copy data to the temporary table */
 
1304
    session->set_proc_info("Copying to tmp table");
 
1305
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
 
1306
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
1307
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1308
    {
 
1309
      error= tmp_error;
 
1310
      return;
 
1311
    }
 
1312
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
1313
 
 
1314
    if (curr_join->having)
 
1315
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
1316
 
 
1317
    /* Change sum_fields reference to calculated fields in tmp_table */
 
1318
    curr_join->all_fields= *curr_all_fields;
 
1319
    if (!items1)
 
1320
    {
 
1321
      items1= items0 + all_fields.elements;
 
1322
      if (sort_and_group || curr_tmp_table->group)
 
1323
      {
 
1324
        if (change_to_use_tmp_fields(session, items1,
 
1325
                  tmp_fields_list1, tmp_all_fields1,
 
1326
                  fields_list.elements, all_fields))
 
1327
          return;
 
1328
      }
 
1329
      else
 
1330
      {
 
1331
        if (change_refs_to_tmp_fields(session, items1,
 
1332
                    tmp_fields_list1, tmp_all_fields1,
 
1333
                    fields_list.elements, all_fields))
 
1334
          return;
 
1335
      }
 
1336
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
1337
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
1338
      curr_join->items1= items1;
 
1339
    }
 
1340
    curr_all_fields= &tmp_all_fields1;
 
1341
    curr_fields_list= &tmp_fields_list1;
 
1342
    curr_join->set_items_ref_array(items1);
 
1343
 
 
1344
    if (sort_and_group || curr_tmp_table->group)
 
1345
    {
 
1346
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
 
1347
                                             + curr_join->tmp_table_param.func_count;
 
1348
      curr_join->tmp_table_param.sum_func_count= 0;
 
1349
      curr_join->tmp_table_param.func_count= 0;
 
1350
    }
 
1351
    else
 
1352
    {
 
1353
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
 
1354
      curr_join->tmp_table_param.func_count= 0;
 
1355
    }
 
1356
 
 
1357
    if (curr_tmp_table->group)
 
1358
    {           // Already grouped
 
1359
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
1360
        curr_join->order= curr_join->group_list;  /* order by group */
 
1361
      curr_join->group_list= 0;
 
1362
    }
 
1363
 
 
1364
    /*
 
1365
      If we have different sort & group then we must sort the data by group
 
1366
      and copy it to another tmp table
 
1367
      This code is also used if we are using distinct something
 
1368
      we haven't been able to store in the temporary table yet
 
1369
      like SEC_TO_TIME(SUM(...)).
 
1370
    */
 
1371
 
 
1372
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) 
 
1373
        || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
1374
    {         /* Must copy to another table */
 
1375
      /* Free first data from old join */
 
1376
      curr_join->join_free();
 
1377
      if (make_simple_join(curr_join, curr_tmp_table))
 
1378
        return;
 
1379
      calc_group_buffer(curr_join, group_list);
 
1380
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
1381
      curr_join->tmp_all_fields1,
 
1382
      curr_join->select_distinct && !curr_join->group_list);
 
1383
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
 
1384
                                                   - curr_join->tmp_fields_list1.elements;
 
1385
 
 
1386
      if (exec_tmp_table2)
 
1387
        curr_tmp_table= exec_tmp_table2;
 
1388
      else
 
1389
      {
 
1390
        /* group data to new table */
 
1391
 
 
1392
        /*
 
1393
          If the access method is loose index scan then all MIN/MAX
 
1394
          functions are precomputed, and should be treated as regular
 
1395
          functions. See extended comment in JOIN::exec.
 
1396
        */
 
1397
        if (curr_join->join_tab->is_using_loose_index_scan())
 
1398
          curr_join->tmp_table_param.precomputed_group_by= true;
 
1399
 
 
1400
        if (!(curr_tmp_table=
 
1401
              exec_tmp_table2= create_tmp_table(session,
 
1402
                                                &curr_join->tmp_table_param,
 
1403
                                                *curr_all_fields,
 
1404
                                                (order_st*) 0,
 
1405
                                                curr_join->select_distinct &&
 
1406
                                                !curr_join->group_list,
 
1407
                                                1, curr_join->select_options,
 
1408
                                                HA_POS_ERROR,
 
1409
                                                (char *) "")))
 
1410
          return;
 
1411
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
1412
      }
 
1413
      if (curr_join->group_list)
 
1414
      {
 
1415
        session->set_proc_info("Creating sort index");
 
1416
        if (curr_join->join_tab == join_tab && save_join_tab())
 
1417
        {
 
1418
          return;
 
1419
        }
 
1420
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
1421
                  HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1422
            make_group_fields(this, curr_join))
 
1423
        {
 
1424
          return;
 
1425
        }
 
1426
        sortorder= curr_join->sortorder;
 
1427
      }
 
1428
 
 
1429
      session->set_proc_info("Copying to group table");
 
1430
      tmp_error= -1;
 
1431
      if (curr_join != this)
 
1432
      {
 
1433
        if (sum_funcs2)
 
1434
        {
 
1435
          curr_join->sum_funcs= sum_funcs2;
 
1436
          curr_join->sum_funcs_end= sum_funcs_end2;
 
1437
        }
 
1438
        else
 
1439
        {
 
1440
          curr_join->alloc_func_list();
 
1441
          sum_funcs2= curr_join->sum_funcs;
 
1442
          sum_funcs_end2= curr_join->sum_funcs_end;
 
1443
        }
 
1444
      }
 
1445
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
 
1446
        return;
 
1447
      curr_join->group_list= 0;
 
1448
 
 
1449
      if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
 
1450
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
1451
      
 
1452
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
 
1453
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1454
      {
 
1455
        error= tmp_error;
 
1456
        return;
 
1457
      }
 
1458
      end_read_record(&curr_join->join_tab->read_record);
 
1459
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
1460
      curr_join->join_tab[0].table= 0;           // Table is freed
 
1461
 
 
1462
      // No sum funcs anymore
 
1463
      if (!items2)
 
1464
      {
 
1465
        items2= items1 + all_fields.elements;
 
1466
        if (change_to_use_tmp_fields(session, items2,
 
1467
                  tmp_fields_list2, tmp_all_fields2,
 
1468
                  fields_list.elements, tmp_all_fields1))
 
1469
          return;
 
1470
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
1471
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
1472
      }
 
1473
      curr_fields_list= &curr_join->tmp_fields_list2;
 
1474
      curr_all_fields= &curr_join->tmp_all_fields2;
 
1475
      curr_join->set_items_ref_array(items2);
 
1476
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
 
1477
      curr_join->tmp_table_param.sum_func_count= 0;
 
1478
    }
 
1479
    if (curr_tmp_table->distinct)
 
1480
      curr_join->select_distinct=0;   /* Each row is unique */
 
1481
 
 
1482
    curr_join->join_free();     /* Free quick selects */
 
1483
    if (curr_join->select_distinct && ! curr_join->group_list)
 
1484
    {
 
1485
      session->set_proc_info("Removing duplicates");
 
1486
      if (curr_join->tmp_having)
 
1487
        curr_join->tmp_having->update_used_tables();
 
1488
 
 
1489
      if (remove_duplicates(curr_join, curr_tmp_table,
 
1490
          *curr_fields_list, curr_join->tmp_having))
 
1491
        return;
 
1492
      
 
1493
      curr_join->tmp_having=0;
 
1494
      curr_join->select_distinct=0;
 
1495
    }
 
1496
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
1497
    if (make_simple_join(curr_join, curr_tmp_table))
 
1498
      return;
 
1499
    calc_group_buffer(curr_join, curr_join->group_list);
 
1500
    count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
 
1501
 
 
1502
  }
 
1503
 
 
1504
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
1505
  {
 
1506
    if (make_group_fields(this, curr_join))
 
1507
      return;
 
1508
 
 
1509
    if (! items3)
 
1510
    {
 
1511
      if (! items0)
 
1512
        init_items_ref_array();
 
1513
      items3= ref_pointer_array + (all_fields.elements*4);
 
1514
      setup_copy_fields(session, &curr_join->tmp_table_param,
 
1515
      items3, tmp_fields_list3, tmp_all_fields3,
 
1516
      curr_fields_list->elements, *curr_all_fields);
 
1517
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
1518
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
1519
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
 
1520
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
1521
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
1522
    }
 
1523
    else
 
1524
    {
 
1525
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
1526
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
1527
      curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
 
1528
    }
 
1529
    curr_fields_list= &tmp_fields_list3;
 
1530
    curr_all_fields= &tmp_all_fields3;
 
1531
    curr_join->set_items_ref_array(items3);
 
1532
 
 
1533
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
1534
              1, true) ||
 
1535
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
1536
        session->is_fatal_error)
 
1537
      return;
 
1538
  }
 
1539
  if (curr_join->group_list || curr_join->order)
 
1540
  {
 
1541
    session->set_proc_info("Sorting result");
 
1542
    /* If we have already done the group, add HAVING to sorted table */
 
1543
    if (curr_join->tmp_having && ! curr_join->group_list && ! curr_join->sort_and_group)
 
1544
    {
 
1545
      // Some tables may have been const
 
1546
      curr_join->tmp_having->update_used_tables();
 
1547
      JoinTable *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
1548
      table_map used_tables= (curr_join->const_table_map |
 
1549
            curr_table->table->map);
 
1550
 
 
1551
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
 
1552
      if (sort_table_cond)
 
1553
      {
 
1554
        if (!curr_table->select)
 
1555
          if (!(curr_table->select= new SQL_SELECT))
 
1556
            return;
 
1557
        if (!curr_table->select->cond)
 
1558
          curr_table->select->cond= sort_table_cond;
 
1559
        else          // This should never happen
 
1560
        {
 
1561
          if (!(curr_table->select->cond=
 
1562
          new Item_cond_and(curr_table->select->cond,
 
1563
                sort_table_cond)))
 
1564
            return;
 
1565
          /*
 
1566
            Item_cond_and do not need fix_fields for execution, its parameters
 
1567
            are fixed or do not need fix_fields, too
 
1568
          */
 
1569
          curr_table->select->cond->quick_fix_field();
 
1570
        }
 
1571
        curr_table->select_cond= curr_table->select->cond;
 
1572
        curr_table->select_cond->top_level_item();
 
1573
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
1574
                    ~ (table_map) 0,
 
1575
                    ~used_tables, 0);
 
1576
      }
 
1577
    }
 
1578
    {
 
1579
      if (group)
 
1580
        curr_join->select_limit= HA_POS_ERROR;
 
1581
      else
 
1582
      {
 
1583
        /*
 
1584
          We can abort sorting after session->select_limit rows if we there is no
 
1585
          WHERE clause for any tables after the sorted one.
 
1586
        */
 
1587
        JoinTable *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
1588
        JoinTable *end_table= &curr_join->join_tab[curr_join->tables];
 
1589
        for (; curr_table < end_table ; curr_table++)
 
1590
        {
 
1591
          /*
 
1592
            table->keyuse is set in the case there was an original WHERE clause
 
1593
            on the table that was optimized away.
 
1594
          */
 
1595
          if (curr_table->select_cond ||
 
1596
              (curr_table->keyuse && !curr_table->first_inner))
 
1597
          {
 
1598
            /* We have to sort all rows */
 
1599
            curr_join->select_limit= HA_POS_ERROR;
 
1600
            break;
 
1601
          }
 
1602
        }
 
1603
      }
 
1604
      if (curr_join->join_tab == join_tab && save_join_tab())
 
1605
        return;
 
1606
      /*
 
1607
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
1608
        chose FILESORT to be faster than INDEX SCAN or there is no
 
1609
        suitable index present.
 
1610
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
1611
        finally replace sorting with index scan if there is a LIMIT clause in
 
1612
        the query. XXX: it's never shown in EXPLAIN!
 
1613
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
1614
      */
 
1615
      if (create_sort_index(session, curr_join,
 
1616
          curr_join->group_list ?
 
1617
          curr_join->group_list : curr_join->order,
 
1618
          curr_join->select_limit,
 
1619
          (select_options & OPTION_FOUND_ROWS ?
 
1620
           HA_POS_ERROR : unit->select_limit_cnt),
 
1621
                            curr_join->group_list ? true : false))
 
1622
        return;
 
1623
 
 
1624
      sortorder= curr_join->sortorder;
 
1625
      if (curr_join->const_tables != curr_join->tables &&
 
1626
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
1627
      {
 
1628
        /*
 
1629
          If no IO cache exists for the first table then we are using an
 
1630
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
1631
          attribute on the INDEX SCAN.
 
1632
        */
 
1633
        skip_sort_order= 1;
 
1634
      }
 
1635
    }
 
1636
  }
 
1637
  /* XXX: When can we have here session->is_error() not zero? */
 
1638
  if (session->is_error())
 
1639
  {
 
1640
    error= session->is_error();
 
1641
    return;
 
1642
  }
 
1643
  curr_join->having= curr_join->tmp_having;
 
1644
  curr_join->fields= curr_fields_list;
 
1645
 
 
1646
  session->set_proc_info("Sending data");
 
1647
  result->send_fields(*curr_fields_list);
 
1648
  error= do_select(curr_join, curr_fields_list, NULL);
 
1649
  session->limit_found_rows= curr_join->send_records;
 
1650
 
 
1651
  /* Accumulate the counts from all join iterations of all join parts. */
 
1652
  session->examined_row_count+= curr_join->examined_rows;
 
1653
 
 
1654
  /*
 
1655
    With EXPLAIN EXTENDED we have to restore original ref_array
 
1656
    for a derived table which is always materialized.
 
1657
    Otherwise we would not be able to print the query  correctly.
 
1658
  */
 
1659
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1660
    set_items_ref_array(items0);
 
1661
 
 
1662
  return;
 
1663
}
 
1664
 
 
1665
/**
 
1666
  Clean up join.
 
1667
 
 
1668
  @return
 
1669
    Return error that hold JOIN.
 
1670
*/
 
1671
int JOIN::destroy()
 
1672
{
 
1673
  select_lex->join= 0;
 
1674
 
 
1675
  if (tmp_join)
 
1676
  {
 
1677
    if (join_tab != tmp_join->join_tab)
 
1678
    {
 
1679
      JoinTable *tab, *end;
 
1680
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
1681
        tab->cleanup();
 
1682
    }
 
1683
    tmp_join->tmp_join= 0;
 
1684
    tmp_table_param.copy_field=0;
 
1685
    return(tmp_join->destroy());
 
1686
  }
 
1687
  cond_equal= 0;
 
1688
 
 
1689
  cleanup(1);
 
1690
  if (exec_tmp_table1)
 
1691
    exec_tmp_table1->free_tmp_table(session);
 
1692
  if (exec_tmp_table2)
 
1693
    exec_tmp_table2->free_tmp_table(session);
 
1694
  delete select;
 
1695
  delete_dynamic(&keyuse);
 
1696
  return(error);
 
1697
}
 
1698
 
 
1699
/**
 
1700
  Setup for execution all subqueries of a query, for which the optimizer
 
1701
  chose hash semi-join.
 
1702
 
 
1703
  @details Iterate over all subqueries of the query, and if they are under an
 
1704
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
1705
  - try to initialize all data structures needed for the materialized execution
 
1706
    of the IN predicate,
 
1707
  - if this fails, then perform the IN=>EXISTS transformation which was
 
1708
    previously blocked during JOIN::prepare.
 
1709
 
 
1710
  This method is part of the "code generation" query processing phase.
 
1711
 
 
1712
  This phase must be called after substitute_for_best_equal_field() because
 
1713
  that function may replace items with other items from a multiple equality,
 
1714
  and we need to reference the correct items in the index access method of the
 
1715
  IN predicate.
 
1716
 
 
1717
  @return Operation status
 
1718
  @retval false     success.
 
1719
  @retval true      error occurred.
 
1720
*/
 
1721
bool JOIN::setup_subquery_materialization()
 
1722
{
 
1723
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
 
1724
       un= un->next_unit())
 
1725
  {
 
1726
    for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
 
1727
    {
 
1728
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
1729
      if (subquery_predicate &&
 
1730
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
1731
      {
 
1732
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
1733
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
1734
            in_subs->setup_engine())
 
1735
          return true;
 
1736
      }
 
1737
    }
 
1738
  }
 
1739
  return false;
 
1740
}
 
1741
 
 
1742
/**
 
1743
  Partially cleanup JOIN after it has executed: close index or rnd read
 
1744
  (table cursors), free quick selects.
 
1745
 
 
1746
    This function is called in the end of execution of a JOIN, before the used
 
1747
    tables are unlocked and closed.
 
1748
 
 
1749
    For a join that is resolved using a temporary table, the first sweep is
 
1750
    performed against actual tables and an intermediate result is inserted
 
1751
    into the temprorary table.
 
1752
    The last sweep is performed against the temporary table. Therefore,
 
1753
    the base tables and associated buffers used to fill the temporary table
 
1754
    are no longer needed, and this function is called to free them.
 
1755
 
 
1756
    For a join that is performed without a temporary table, this function
 
1757
    is called after all rows are sent, but before EOF packet is sent.
 
1758
 
 
1759
    For a simple SELECT with no subqueries this function performs a full
 
1760
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
1761
    tables.
 
1762
 
 
1763
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
1764
    do the full cleanup and need to do a partial cleanup only.
 
1765
    - If a JOIN is not the top level join, we must not unlock the tables
 
1766
    because the outer select may not have been evaluated yet, and we
 
1767
    can't unlock only selected tables of a query.
 
1768
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
1769
    should not free quick selects and join buffers because they will be
 
1770
    needed for the next execution of the correlated subquery.
 
1771
    - However, if this is a JOIN for a [sub]select, which is not
 
1772
    a correlated subquery itself, but has subqueries, we can free it
 
1773
    fully and also free JOINs of all its subqueries. The exception
 
1774
    is a subquery in SELECT list, e.g: @n
 
1775
    SELECT a, (select cmax(b) from t1) group by c @n
 
1776
    This subquery will not be evaluated at first sweep and its value will
 
1777
    not be inserted into the temporary table. Instead, it's evaluated
 
1778
    when selecting from the temporary table. Therefore, it can't be freed
 
1779
    here even though it's not correlated.
 
1780
 
 
1781
  @todo
 
1782
    Unlock tables even if the join isn't top level select in the tree
 
1783
*/
 
1784
void JOIN::join_free()
 
1785
{
 
1786
  Select_Lex_Unit *tmp_unit;
 
1787
  Select_Lex *sl;
 
1788
  /*
 
1789
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
1790
    free all tables.
 
1791
  */
 
1792
  bool full= (!select_lex->uncacheable && !session->lex->describe);
 
1793
  bool can_unlock= full;
 
1794
 
 
1795
  cleanup(full);
 
1796
 
 
1797
  for (tmp_unit= select_lex->first_inner_unit();
 
1798
       tmp_unit;
 
1799
       tmp_unit= tmp_unit->next_unit())
 
1800
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
1801
    {
 
1802
      Item_subselect *subselect= sl->master_unit()->item;
 
1803
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
1804
      /*
 
1805
        If this join is evaluated, we can fully clean it up and clean up all
 
1806
        its underlying joins even if they are correlated -- they will not be
 
1807
        used any more anyway.
 
1808
        If this join is not yet evaluated, we still must clean it up to
 
1809
        close its table cursors -- it may never get evaluated, as in case of
 
1810
        ... HAVING false OR a IN (SELECT ...))
 
1811
        but all table cursors must be closed before the unlock.
 
1812
      */
 
1813
      sl->cleanup_all_joins(full_local);
 
1814
      /* Can't unlock if at least one JOIN is still needed */
 
1815
      can_unlock= can_unlock && full_local;
 
1816
    }
 
1817
 
 
1818
  /*
 
1819
    We are not using tables anymore
 
1820
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
1821
  */
 
1822
  if (can_unlock && lock && session->lock &&
 
1823
      !(select_options & SELECT_NO_UNLOCK) &&
 
1824
      !select_lex->subquery_in_having &&
 
1825
      (select_lex == (session->lex->unit.fake_select_lex ?
 
1826
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
1827
  {
 
1828
    /*
 
1829
      TODO: unlock tables even if the join isn't top level select in the
 
1830
      tree.
 
1831
    */
 
1832
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
 
1833
    lock= 0;
 
1834
  }
 
1835
 
 
1836
  return;
 
1837
}
 
1838
 
 
1839
 
 
1840
/**
 
1841
  Free resources of given join.
 
1842
 
 
1843
  @param fill   true if we should free all resources, call with full==1
 
1844
                should be last, before it this function can be called with
 
1845
                full==0
 
1846
 
 
1847
  @note
 
1848
    With subquery this function definitely will be called several times,
 
1849
    but even for simple query it can be called several times.
 
1850
*/
 
1851
void JOIN::cleanup(bool full)
 
1852
{
 
1853
  if (table)
 
1854
  {
 
1855
    JoinTable *tab,*end;
 
1856
    /*
 
1857
      Only a sorted table may be cached.  This sorted table is always the
 
1858
      first non const table in join->table
 
1859
    */
 
1860
    if (tables > const_tables) // Test for not-const tables
 
1861
    {
 
1862
      table[const_tables]->free_io_cache();
 
1863
      table[const_tables]->filesort_free_buffers(full);
 
1864
    }
 
1865
 
 
1866
    if (full)
 
1867
    {
 
1868
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
1869
        tab->cleanup();
 
1870
      table= 0;
 
1871
    }
 
1872
    else
 
1873
    {
 
1874
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
1875
      {
 
1876
        if (tab->table)
 
1877
          tab->table->file->ha_index_or_rnd_end();
 
1878
      }
 
1879
    }
 
1880
  }
 
1881
  /*
 
1882
    We are not using tables anymore
 
1883
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
1884
  */
 
1885
  if (full)
 
1886
  {
 
1887
    if (tmp_join)
 
1888
      tmp_table_param.copy_field= 0;
 
1889
    group_fields.delete_elements();
 
1890
    /*
 
1891
      We can't call delete_elements() on copy_funcs as this will cause
 
1892
      problems in free_elements() as some of the elements are then deleted.
 
1893
    */
 
1894
    tmp_table_param.copy_funcs.empty();
 
1895
    /*
 
1896
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
1897
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
1898
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
1899
      be removed in tmp_table_param.cleanup().
 
1900
    */
 
1901
    if (tmp_join &&
 
1902
        tmp_join != this &&
 
1903
        tmp_join->tmp_table_param.copy_field ==
 
1904
        tmp_table_param.copy_field)
 
1905
    {
 
1906
      tmp_join->tmp_table_param.copy_field=
 
1907
        tmp_join->tmp_table_param.save_copy_field= 0;
 
1908
    }
 
1909
    tmp_table_param.cleanup();
 
1910
  }
 
1911
  return;
 
1912
}
 
1913
 
 
1914
/*
 
1915
  used only in JOIN::clear
 
1916
*/
 
1917
static void clear_tables(JOIN *join)
 
1918
{
 
1919
  /*
 
1920
    must clear only the non-const tables, as const tables
 
1921
    are not re-calculated.
 
1922
  */
 
1923
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
1924
    join->table[i]->mark_as_null_row();   // All fields are NULL
 
1925
}
 
1926
 
 
1927
/**
 
1928
  Make an array of pointers to sum_functions to speed up
 
1929
  sum_func calculation.
 
1930
 
 
1931
  @retval
 
1932
    0 ok
 
1933
  @retval
 
1934
    1 Error
 
1935
*/
 
1936
bool JOIN::alloc_func_list()
 
1937
{
 
1938
  uint32_t func_count, group_parts;
 
1939
 
 
1940
  func_count= tmp_table_param.sum_func_count;
 
1941
  /*
 
1942
    If we are using rollup, we need a copy of the summary functions for
 
1943
    each level
 
1944
  */
 
1945
  if (rollup.state != ROLLUP::STATE_NONE)
 
1946
    func_count*= (send_group_parts+1);
 
1947
 
 
1948
  group_parts= send_group_parts;
 
1949
  /*
 
1950
    If distinct, reserve memory for possible
 
1951
    disctinct->group_by optimization
 
1952
  */
 
1953
  if (select_distinct)
 
1954
  {
 
1955
    group_parts+= fields_list.elements;
 
1956
    /*
 
1957
      If the order_st clause is specified then it's possible that
 
1958
      it also will be optimized, so reserve space for it too
 
1959
    */
 
1960
    if (order)
 
1961
    {
 
1962
      order_st *ord;
 
1963
      for (ord= order; ord; ord= ord->next)
 
1964
        group_parts++;
 
1965
    }
 
1966
  }
 
1967
 
 
1968
  /* This must use calloc() as rollup_make_fields depends on this */
 
1969
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
 
1970
              sizeof(Item_sum***) * (group_parts+1));
 
1971
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
1972
  return(sum_funcs == 0);
 
1973
}
 
1974
 
 
1975
/**
 
1976
  Initialize 'sum_funcs' array with all Item_sum objects.
 
1977
 
 
1978
  @param field_list        All items
 
1979
  @param send_fields       Items in select list
 
1980
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
1981
  @param recompute         Set to true if sum_funcs must be recomputed
 
1982
 
 
1983
  @retval
 
1984
    0  ok
 
1985
  @retval
 
1986
    1  error
 
1987
*/
 
1988
bool JOIN::make_sum_func_list(List<Item> &field_list, 
 
1989
                              List<Item> &send_fields,
 
1990
                              bool before_group_by, 
 
1991
                              bool recompute)
 
1992
{
 
1993
  List_iterator_fast<Item> it(field_list);
 
1994
  Item_sum **func;
 
1995
  Item *item;
 
1996
 
 
1997
  if (*sum_funcs && !recompute)
 
1998
    return(false); /* We have already initialized sum_funcs. */
 
1999
 
 
2000
  func= sum_funcs;
 
2001
  while ((item=it++))
 
2002
  {
 
2003
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
2004
        (!((Item_sum*) item)->depended_from() ||
 
2005
         ((Item_sum *)item)->depended_from() == select_lex))
 
2006
      *func++= (Item_sum*) item;
 
2007
  }
 
2008
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
2009
  {
 
2010
    rollup.state= ROLLUP::STATE_READY;
 
2011
    if (rollup_make_fields(field_list, send_fields, &func))
 
2012
      return(true);     // Should never happen
 
2013
  }
 
2014
  else if (rollup.state == ROLLUP::STATE_NONE)
 
2015
  {
 
2016
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
2017
      sum_funcs_end[i]= func;
 
2018
  }
 
2019
  else if (rollup.state == ROLLUP::STATE_READY)
 
2020
    return(false);                         // Don't put end marker
 
2021
  *func=0;          // End marker
 
2022
  return(false);
 
2023
}
 
2024
 
 
2025
/** Allocate memory needed for other rollup functions. */
 
2026
bool JOIN::rollup_init()
 
2027
{
 
2028
  uint32_t i,j;
 
2029
  Item **ref_array;
 
2030
 
 
2031
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
 
2032
  rollup.state= ROLLUP::STATE_INITED;
 
2033
 
 
2034
  /*
 
2035
    Create pointers to the different sum function groups
 
2036
    These are updated by rollup_make_fields()
 
2037
  */
 
2038
  tmp_table_param.group_parts= send_group_parts;
 
2039
 
 
2040
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
2041
                                                sizeof(Item**) +
 
2042
                                                sizeof(List<Item>) +
 
2043
                        ref_pointer_array_size)
 
2044
                        * send_group_parts )))
 
2045
    return 1;
 
2046
 
 
2047
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
2048
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
2049
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
2050
 
 
2051
  /*
 
2052
    Prepare space for field list for the different levels
 
2053
    These will be filled up in rollup_make_fields()
 
2054
  */
 
2055
  for (i= 0 ; i < send_group_parts ; i++)
 
2056
  {
 
2057
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
2058
    List<Item> *rollup_fields= &rollup.fields[i];
 
2059
    rollup_fields->empty();
 
2060
    rollup.ref_pointer_arrays[i]= ref_array;
 
2061
    ref_array+= all_fields.elements;
 
2062
  }
 
2063
  for (i= 0 ; i < send_group_parts; i++)
 
2064
  {
 
2065
    for (j=0 ; j < fields_list.elements ; j++)
 
2066
      rollup.fields[i].push_back(rollup.null_items[i]);
 
2067
  }
 
2068
  List_iterator<Item> it(all_fields);
 
2069
  Item *item;
 
2070
  while ((item= it++))
 
2071
  {
 
2072
    order_st *group_tmp;
 
2073
    bool found_in_group= 0;
 
2074
 
 
2075
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
2076
    {
 
2077
      if (*group_tmp->item == item)
 
2078
      {
 
2079
        item->maybe_null= 1;
 
2080
        found_in_group= 1;
 
2081
        if (item->const_item())
 
2082
        {
 
2083
          /*
 
2084
            For ROLLUP queries each constant item referenced in GROUP BY list
 
2085
            is wrapped up into an Item_func object yielding the same value
 
2086
            as the constant item. The objects of the wrapper class are never
 
2087
            considered as constant items and besides they inherit all
 
2088
            properties of the Item_result_field class.
 
2089
            This wrapping allows us to ensure writing constant items
 
2090
            into temporary tables whenever the result of the ROLLUP
 
2091
            operation has to be written into a temporary table, e.g. when
 
2092
            ROLLUP is used together with DISTINCT in the SELECT list.
 
2093
            Usually when creating temporary tables for a intermidiate
 
2094
            result we do not include fields for constant expressions.
 
2095
          */
 
2096
          Item* new_item= new Item_func_rollup_const(item);
 
2097
          if (!new_item)
 
2098
            return 1;
 
2099
          new_item->fix_fields(session, (Item **) 0);
 
2100
          session->change_item_tree(it.ref(), new_item);
 
2101
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
2102
          {
 
2103
            if (*tmp->item == item)
 
2104
              session->change_item_tree(tmp->item, new_item);
 
2105
          }
 
2106
        }
 
2107
      }
 
2108
    }
 
2109
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
2110
    {
 
2111
      bool changed= false;
 
2112
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
 
2113
        return 1;
 
2114
      /*
 
2115
        We have to prevent creation of a field in a temporary table for
 
2116
        an expression that contains GROUP BY attributes.
 
2117
        Marking the expression item as 'with_sum_func' will ensure this.
 
2118
      */
 
2119
      if (changed)
 
2120
        item->with_sum_func= 1;
 
2121
    }
 
2122
  }
 
2123
  return 0;
 
2124
}
 
2125
 
 
2126
/**
 
2127
  Fill up rollup structures with pointers to fields to use.
 
2128
 
 
2129
  Creates copies of item_sum items for each sum level.
 
2130
 
 
2131
  @param fields_arg   List of all fields (hidden and real ones)
 
2132
  @param sel_fields   Pointer to selected fields
 
2133
  @param func     Store here a pointer to all fields
 
2134
 
 
2135
  @retval
 
2136
    0 if ok;
 
2137
    In this case func is pointing to next not used element.
 
2138
  @retval
 
2139
    1    on error
 
2140
*/
 
2141
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
 
2142
{
 
2143
  List_iterator_fast<Item> it(fields_arg);
 
2144
  Item *first_field= sel_fields.head();
 
2145
  uint32_t level;
 
2146
 
 
2147
  /*
 
2148
    Create field lists for the different levels
 
2149
 
 
2150
    The idea here is to have a separate field list for each rollup level to
 
2151
    avoid all runtime checks of which columns should be NULL.
 
2152
 
 
2153
    The list is stored in reverse order to get sum function in such an order
 
2154
    in func that it makes it easy to reset them with init_sum_functions()
 
2155
 
 
2156
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
2157
 
 
2158
    rollup.fields[0] will contain list where a,b,c is NULL
 
2159
    rollup.fields[1] will contain list where b,c is NULL
 
2160
    ...
 
2161
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
2162
    ...
 
2163
    sum_funcs_end[0] points to all sum functions
 
2164
    sum_funcs_end[1] points to all sum functions, except grand totals
 
2165
    ...
 
2166
  */
 
2167
 
 
2168
  for (level=0 ; level < send_group_parts ; level++)
 
2169
  {
 
2170
    uint32_t i;
 
2171
    uint32_t pos= send_group_parts - level -1;
 
2172
    bool real_fields= 0;
 
2173
    Item *item;
 
2174
    List_iterator<Item> new_it(rollup.fields[pos]);
 
2175
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
2176
    order_st *start_group;
 
2177
 
 
2178
    /* Point to first hidden field */
 
2179
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
2180
 
 
2181
    /* Remember where the sum functions ends for the previous level */
 
2182
    sum_funcs_end[pos+1]= *func;
 
2183
 
 
2184
    /* Find the start of the group for this level */
 
2185
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
 
2186
    {}
 
2187
 
 
2188
    it.rewind();
 
2189
    while ((item= it++))
 
2190
    {
 
2191
      if (item == first_field)
 
2192
      {
 
2193
        real_fields= 1;       // End of hidden fields
 
2194
        ref_array= ref_array_start;
 
2195
      }
 
2196
 
 
2197
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
2198
          (!((Item_sum*) item)->depended_from() ||
 
2199
           ((Item_sum *)item)->depended_from() == select_lex))
 
2200
 
 
2201
      {
 
2202
        /*
 
2203
          This is a top level summary function that must be replaced with
 
2204
          a sum function that is reset for this level.
 
2205
 
 
2206
          NOTE: This code creates an object which is not that nice in a
 
2207
          sub select.  Fortunately it's not common to have rollup in
 
2208
          sub selects.
 
2209
        */
 
2210
        item= item->copy_or_same(session);
 
2211
        ((Item_sum*) item)->make_unique();
 
2212
        *(*func)= (Item_sum*) item;
 
2213
        (*func)++;
 
2214
      }
 
2215
      else
 
2216
      {
 
2217
        /* Check if this is something that is part of this group by */
 
2218
        order_st *group_tmp;
 
2219
        for (group_tmp= start_group, i= pos ;
 
2220
                  group_tmp ; group_tmp= group_tmp->next, i++)
 
2221
        {
 
2222
                if (*group_tmp->item == item)
 
2223
          {
 
2224
            /*
 
2225
              This is an element that is used by the GROUP BY and should be
 
2226
              set to NULL in this level
 
2227
            */
 
2228
                  Item_null_result *null_item= new (session->mem_root) Item_null_result();
 
2229
                  if (!null_item)
 
2230
                    return 1;
 
2231
            item->maybe_null= 1;    // Value will be null sometimes
 
2232
                  null_item->result_field= item->get_tmp_table_field();
 
2233
                  item= null_item;
 
2234
            break;
 
2235
          }
 
2236
        }
 
2237
      }
 
2238
      *ref_array= item;
 
2239
      if (real_fields)
 
2240
      {
 
2241
  (void) new_it++;      // Point to next item
 
2242
  new_it.replace(item);     // Replace previous
 
2243
  ref_array++;
 
2244
      }
 
2245
      else
 
2246
  ref_array--;
 
2247
    }
 
2248
  }
 
2249
  sum_funcs_end[0]= *func;      // Point to last function
 
2250
  return 0;
 
2251
}
 
2252
 
 
2253
/**
 
2254
  Send all rollup levels higher than the current one to the client.
 
2255
 
 
2256
  @b SAMPLE
 
2257
    @code
 
2258
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
2259
  @endcode
 
2260
 
 
2261
  @param idx    Level we are on:
 
2262
                        - 0 = Total sum level
 
2263
                        - 1 = First group changed  (a)
 
2264
                        - 2 = Second group changed (a,b)
 
2265
 
 
2266
  @retval
 
2267
    0   ok
 
2268
  @retval
 
2269
    1   If send_data_failed()
 
2270
*/
 
2271
int JOIN::rollup_send_data(uint32_t idx)
 
2272
{
 
2273
  uint32_t i;
 
2274
  for (i= send_group_parts ; i-- > idx ; )
 
2275
  {
 
2276
    /* Get reference pointers to sum functions in place */
 
2277
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2278
     ref_pointer_array_size);
 
2279
    if ((!having || having->val_int()))
 
2280
    {
 
2281
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
2282
    result->send_data(rollup.fields[i]))
 
2283
  return 1;
 
2284
      send_records++;
 
2285
    }
 
2286
  }
 
2287
  /* Restore ref_pointer_array */
 
2288
  set_items_ref_array(current_ref_pointer_array);
 
2289
  return 0;
 
2290
}
 
2291
 
 
2292
/**
 
2293
  Write all rollup levels higher than the current one to a temp table.
 
2294
 
 
2295
  @b SAMPLE
 
2296
    @code
 
2297
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
2298
  @endcode
 
2299
 
 
2300
  @param idx                 Level we are on:
 
2301
                               - 0 = Total sum level
 
2302
                               - 1 = First group changed  (a)
 
2303
                               - 2 = Second group changed (a,b)
 
2304
  @param table               reference to temp table
 
2305
 
 
2306
  @retval
 
2307
    0   ok
 
2308
  @retval
 
2309
    1   if write_data_failed()
 
2310
*/
 
2311
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
2312
{
 
2313
  uint32_t i;
 
2314
  for (i= send_group_parts ; i-- > idx ; )
 
2315
  {
 
2316
    /* Get reference pointers to sum functions in place */
 
2317
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2318
     ref_pointer_array_size);
 
2319
    if ((!having || having->val_int()))
 
2320
    {
 
2321
      int write_error;
 
2322
      Item *item;
 
2323
      List_iterator_fast<Item> it(rollup.fields[i]);
 
2324
      while ((item= it++))
 
2325
      {
 
2326
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
2327
          item->save_in_result_field(1);
 
2328
      }
 
2329
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
2330
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
2331
      {
 
2332
  if (create_myisam_from_heap(session, table_arg,
 
2333
                                    tmp_table_param.start_recinfo,
 
2334
                                    &tmp_table_param.recinfo,
 
2335
                                    write_error, 0))
 
2336
    return 1;
 
2337
      }
 
2338
    }
 
2339
  }
 
2340
  /* Restore ref_pointer_array */
 
2341
  set_items_ref_array(current_ref_pointer_array);
 
2342
  return 0;
 
2343
}
 
2344
 
 
2345
/**
 
2346
  clear results if there are not rows found for group
 
2347
  (end_send_group/end_write_group)
 
2348
*/
 
2349
void JOIN::clear()
 
2350
{
 
2351
  clear_tables(this);
 
2352
  copy_fields(&tmp_table_param);
 
2353
 
 
2354
  if (sum_funcs)
 
2355
  {
 
2356
    Item_sum *func, **func_ptr= sum_funcs;
 
2357
    while ((func= *(func_ptr++)))
 
2358
      func->clear();
 
2359
  }
 
2360
}
 
2361
 
 
2362
/**
 
2363
  change select_result object of JOIN.
 
2364
 
 
2365
  @param res    new select_result object
 
2366
 
 
2367
  @retval
 
2368
    false   OK
 
2369
  @retval
 
2370
    true    error
 
2371
*/
 
2372
bool JOIN::change_result(select_result *res)
 
2373
{
 
2374
  result= res;
 
2375
  if (result->prepare(fields_list, select_lex->master_unit()))
 
2376
  {
 
2377
    return(true);
 
2378
  }
 
2379
  return(false);
 
2380
}
 
2381
 
 
2382
/**
 
2383
  @brief
 
2384
  
 
2385
  Process one record of the nested loop join.
 
2386
 
 
2387
  @details 
 
2388
 
 
2389
  This function will evaluate parts of WHERE/ON clauses that are
 
2390
  applicable to the partial record on hand and in case of success
 
2391
  submit this record to the next level of the nested loop.
 
2392
*/
 
2393
enum_nested_loop_state evaluate_join_record(JOIN *join, JoinTable *join_tab, int error)
 
2394
{
 
2395
  bool not_used_in_distinct= join_tab->not_used_in_distinct;
 
2396
  ha_rows found_records= join->found_records;
 
2397
  COND *select_cond= join_tab->select_cond;
 
2398
 
 
2399
  if (error > 0 || (join->session->is_error()))     // Fatal error
 
2400
    return NESTED_LOOP_ERROR;
 
2401
  if (error < 0)
 
2402
    return NESTED_LOOP_NO_MORE_ROWS;
 
2403
  if (join->session->killed)                    // Aborted by user
 
2404
  {
 
2405
    join->session->send_kill_message();
 
2406
    return NESTED_LOOP_KILLED;
 
2407
  }
 
2408
  if (!select_cond || select_cond->val_int())
 
2409
  {
 
2410
    /*
 
2411
      There is no select condition or the attached pushed down
 
2412
      condition is true => a match is found.
 
2413
    */
 
2414
    bool found= 1;
 
2415
    while (join_tab->first_unmatched && found)
 
2416
    {
 
2417
      /*
 
2418
        The while condition is always false if join_tab is not
 
2419
        the last inner join table of an outer join operation.
 
2420
      */
 
2421
      JoinTable *first_unmatched= join_tab->first_unmatched;
 
2422
      /*
 
2423
        Mark that a match for current outer table is found.
 
2424
        This activates push down conditional predicates attached
 
2425
        to the all inner tables of the outer join.
 
2426
      */
 
2427
      first_unmatched->found= 1;
 
2428
      for (JoinTable *tab= first_unmatched; tab <= join_tab; tab++)
 
2429
      {
 
2430
        if (tab->table->reginfo.not_exists_optimize)
 
2431
          return NESTED_LOOP_NO_MORE_ROWS;
 
2432
        /* Check all predicates that has just been activated. */
 
2433
        /*
 
2434
          Actually all predicates non-guarded by first_unmatched->found
 
2435
          will be re-evaluated again. It could be fixed, but, probably,
 
2436
          it's not worth doing now.
 
2437
        */
 
2438
        if (tab->select_cond && !tab->select_cond->val_int())
 
2439
        {
 
2440
          /* The condition attached to table tab is false */
 
2441
          if (tab == join_tab)
 
2442
            found= 0;
 
2443
          else
 
2444
          {
 
2445
            /*
 
2446
              Set a return point if rejected predicate is attached
 
2447
              not to the last table of the current nest level.
 
2448
            */
 
2449
            join->return_tab= tab;
 
2450
            return NESTED_LOOP_OK;
 
2451
          }
 
2452
        }
 
2453
      }
 
2454
      /*
 
2455
        Check whether join_tab is not the last inner table
 
2456
        for another embedding outer join.
 
2457
      */
 
2458
      if ((first_unmatched= first_unmatched->first_upper) &&
 
2459
          first_unmatched->last_inner != join_tab)
 
2460
        first_unmatched= 0;
 
2461
      join_tab->first_unmatched= first_unmatched;
 
2462
    }
 
2463
 
 
2464
    JoinTable *return_tab= join->return_tab;
 
2465
    join_tab->found_match= true;
 
2466
 
 
2467
    /*
 
2468
      It was not just a return to lower loop level when one
 
2469
      of the newly activated predicates is evaluated as false
 
2470
      (See above join->return_tab= tab).
 
2471
    */
 
2472
    join->examined_rows++;
 
2473
    join->session->row_count++;
 
2474
 
 
2475
    if (found)
 
2476
    {
 
2477
      enum enum_nested_loop_state rc;
 
2478
      /* A match from join_tab is found for the current partial join. */
 
2479
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
2480
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
2481
        return rc;
 
2482
      if (return_tab < join->return_tab)
 
2483
        join->return_tab= return_tab;
 
2484
 
 
2485
      if (join->return_tab < join_tab)
 
2486
        return NESTED_LOOP_OK;
 
2487
      /*
 
2488
        Test if this was a SELECT DISTINCT query on a table that
 
2489
        was not in the field list;  In this case we can abort if
 
2490
        we found a row, as no new rows can be added to the result.
 
2491
      */
 
2492
      if (not_used_in_distinct && found_records != join->found_records)
 
2493
        return NESTED_LOOP_NO_MORE_ROWS;
 
2494
    }
 
2495
    else
 
2496
      join_tab->read_record.file->unlock_row();
 
2497
  }
 
2498
  else
 
2499
  {
 
2500
    /*
 
2501
      The condition pushed down to the table join_tab rejects all rows
 
2502
      with the beginning coinciding with the current partial join.
 
2503
    */
 
2504
    join->examined_rows++;
 
2505
    join->session->row_count++;
 
2506
    join_tab->read_record.file->unlock_row();
 
2507
  }
 
2508
  return NESTED_LOOP_OK;
 
2509
}
 
2510
 
 
2511
/**
 
2512
  @details
 
2513
    Construct a NULL complimented partial join record and feed it to the next
 
2514
    level of the nested loop. This function is used in case we have
 
2515
    an OUTER join and no matching record was found.
 
2516
*/
 
2517
enum_nested_loop_state evaluate_null_complemented_join_record(JOIN *join, JoinTable *join_tab)
 
2518
{
 
2519
  /*
 
2520
    The table join_tab is the first inner table of a outer join operation
 
2521
    and no matches has been found for the current outer row.
 
2522
  */
 
2523
  JoinTable *last_inner_tab= join_tab->last_inner;
 
2524
  /* Cache variables for faster loop */
 
2525
  COND *select_cond;
 
2526
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
2527
  {
 
2528
    /* Change the the values of guard predicate variables. */
 
2529
    join_tab->found= 1;
 
2530
    join_tab->not_null_compl= 0;
 
2531
    /* The outer row is complemented by nulls for each inner tables */
 
2532
    join_tab->table->restoreRecordAsDefault();  // Make empty record
 
2533
    join_tab->table->mark_as_null_row();       // For group by without error
 
2534
    select_cond= join_tab->select_cond;
 
2535
    /* Check all attached conditions for inner table rows. */
 
2536
    if (select_cond && !select_cond->val_int())
 
2537
      return NESTED_LOOP_OK;
 
2538
  }
 
2539
  join_tab--;
 
2540
  /*
 
2541
    The row complemented by nulls might be the first row
 
2542
    of embedding outer joins.
 
2543
    If so, perform the same actions as in the code
 
2544
    for the first regular outer join row above.
 
2545
  */
 
2546
  for ( ; ; )
 
2547
  {
 
2548
    JoinTable *first_unmatched= join_tab->first_unmatched;
 
2549
    if ((first_unmatched= first_unmatched->first_upper) && first_unmatched->last_inner != join_tab)
 
2550
      first_unmatched= 0;
 
2551
    join_tab->first_unmatched= first_unmatched;
 
2552
    if (! first_unmatched)
 
2553
      break;
 
2554
    first_unmatched->found= 1;
 
2555
    for (JoinTable *tab= first_unmatched; tab <= join_tab; tab++)
 
2556
    {
 
2557
      if (tab->select_cond && !tab->select_cond->val_int())
 
2558
      {
 
2559
        join->return_tab= tab;
 
2560
        return NESTED_LOOP_OK;
 
2561
      }
 
2562
    }
 
2563
  }
 
2564
  /*
 
2565
    The row complemented by nulls satisfies all conditions
 
2566
    attached to inner tables.
 
2567
    Send the row complemented by nulls to be joined with the
 
2568
    remaining tables.
 
2569
  */
 
2570
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
2571
}
 
2572
 
 
2573
enum_nested_loop_state flush_cached_records(JOIN *join, JoinTable *join_tab, bool skip_last)
 
2574
{
 
2575
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
2576
  int error;
 
2577
  READ_RECORD *info;
 
2578
 
 
2579
  join_tab->table->null_row= 0;
 
2580
  if (!join_tab->cache.records)
 
2581
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
2582
  if (skip_last)
 
2583
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
2584
  if (join_tab->use_quick == 2)
 
2585
  {
 
2586
    if (join_tab->select->quick)
 
2587
    {                                   /* Used quick select last. reset it */
 
2588
      delete join_tab->select->quick;
 
2589
      join_tab->select->quick=0;
 
2590
    }
 
2591
  }
 
2592
  /* read through all records */
 
2593
  if ((error=join_init_read_record(join_tab)))
 
2594
  {
 
2595
    reset_cache_write(&join_tab->cache);
 
2596
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
2597
  }
 
2598
 
 
2599
  for (JoinTable *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
2600
  {
 
2601
    tmp->status=tmp->table->status;
 
2602
    tmp->table->status=0;
 
2603
  }
 
2604
 
 
2605
  info= &join_tab->read_record;
 
2606
  do
 
2607
  {
 
2608
    if (join->session->killed)
 
2609
    {
 
2610
      join->session->send_kill_message();
 
2611
      return NESTED_LOOP_KILLED;
 
2612
    }
 
2613
    SQL_SELECT *select=join_tab->select;
 
2614
    if (rc == NESTED_LOOP_OK &&
 
2615
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
2616
    {
 
2617
      uint32_t i;
 
2618
      reset_cache_read(&join_tab->cache);
 
2619
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
2620
      {
 
2621
              join_tab->readCachedRecord();
 
2622
              if (!select || !select->skip_record())
 
2623
        {
 
2624
          int res= 0;
 
2625
 
 
2626
          rc= (join_tab->next_select)(join,join_tab+1,0);
 
2627
          if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
2628
          {
 
2629
            reset_cache_write(&join_tab->cache);
 
2630
            return rc;
 
2631
          }
 
2632
 
 
2633
          if (res == -1)
 
2634
            return NESTED_LOOP_ERROR;
 
2635
        }
 
2636
      }
 
2637
    }
 
2638
  } while (!(error=info->read_record(info)));
 
2639
 
 
2640
  if (skip_last)
 
2641
    join_tab->readCachedRecord();               // Restore current record
 
2642
  reset_cache_write(&join_tab->cache);
 
2643
  if (error > 0)                                // Fatal error
 
2644
    return NESTED_LOOP_ERROR;
 
2645
  for (JoinTable *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
2646
    tmp2->table->status=tmp2->status;
 
2647
  return NESTED_LOOP_OK;
 
2648
}
 
2649
 
 
2650
/*****************************************************************************
 
2651
  DESCRIPTION
 
2652
    Functions that end one nested loop iteration. Different functions
 
2653
    are used to support GROUP BY clause and to redirect records
 
2654
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
2655
    network client.
 
2656
 
 
2657
  RETURN VALUES
 
2658
    NESTED_LOOP_OK           - the record has been successfully handled
 
2659
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
2660
                               was detected
 
2661
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
2662
                               the record
 
2663
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
2664
                               additionally, the nested loop produced the
 
2665
                               number of rows specified in the LIMIT clause
 
2666
                               for the query
 
2667
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
2668
                               additionally, there is a cursor and the nested
 
2669
                               loop algorithm produced the number of rows
 
2670
                               that is specified for current cursor fetch
 
2671
                               operation.
 
2672
   All return values except NESTED_LOOP_OK abort the nested loop.
 
2673
*****************************************************************************/
 
2674
enum_nested_loop_state end_send(JOIN *join, JoinTable *, bool end_of_records)
 
2675
{
 
2676
  if (! end_of_records)
 
2677
  {
 
2678
    int error;
 
2679
    if (join->having && join->having->val_int() == 0)
 
2680
      return NESTED_LOOP_OK;               // Didn't match having
 
2681
    error= 0;
 
2682
    if (join->do_send_rows)
 
2683
      error=join->result->send_data(*join->fields);
 
2684
    if (error)
 
2685
      return NESTED_LOOP_ERROR;
 
2686
    if (++join->send_records >= join->unit->select_limit_cnt && join->do_send_rows)
 
2687
    {
 
2688
      if (join->select_options & OPTION_FOUND_ROWS)
 
2689
      {
 
2690
        JoinTable *jt=join->join_tab;
 
2691
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
2692
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
2693
            !(jt->select && jt->select->quick) &&
 
2694
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
2695
                  (jt->ref.key < 0))
 
2696
        {
 
2697
          /* Join over all rows in table;  Return number of found rows */
 
2698
          Table *table= jt->table;
 
2699
 
 
2700
          join->select_options^= OPTION_FOUND_ROWS;
 
2701
          if (table->sort.record_pointers ||
 
2702
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
2703
          {
 
2704
            /* Using filesort */
 
2705
            join->send_records= table->sort.found_records;
 
2706
          }
 
2707
          else
 
2708
          {
 
2709
            table->file->info(HA_STATUS_VARIABLE);
 
2710
            join->send_records= table->file->stats.records;
 
2711
          }
 
2712
        }
 
2713
        else
 
2714
        {
 
2715
          join->do_send_rows= 0;
 
2716
          if (join->unit->fake_select_lex)
 
2717
            join->unit->fake_select_lex->select_limit= 0;
 
2718
          return NESTED_LOOP_OK;
 
2719
        }
 
2720
      }
 
2721
      return NESTED_LOOP_QUERY_LIMIT;      // Abort nicely
 
2722
    }
 
2723
    else if (join->send_records >= join->fetch_limit)
 
2724
    {
 
2725
      /*
 
2726
        There is a server side cursor and all rows for
 
2727
        this fetch request are sent.
 
2728
      */
 
2729
      return NESTED_LOOP_CURSOR_LIMIT;
 
2730
    }
 
2731
  }
 
2732
 
 
2733
  return NESTED_LOOP_OK;
 
2734
}
 
2735
 
 
2736
enum_nested_loop_state end_write(JOIN *join, JoinTable *, bool end_of_records)
 
2737
{
 
2738
  Table *table= join->tmp_table;
 
2739
 
 
2740
  if (join->session->killed)                    // Aborted by user
 
2741
  {
 
2742
    join->session->send_kill_message();
 
2743
    return NESTED_LOOP_KILLED;
 
2744
  }
 
2745
  if (!end_of_records)
 
2746
  {
 
2747
    copy_fields(&join->tmp_table_param);
 
2748
    copy_funcs(join->tmp_table_param.items_to_copy);
 
2749
    if (!join->having || join->having->val_int())
 
2750
    {
 
2751
      int error;
 
2752
      join->found_records++;
 
2753
      if ((error=table->file->ha_write_row(table->record[0])))
 
2754
      {
 
2755
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
2756
          goto end;
 
2757
        if (create_myisam_from_heap(join->session, table,
 
2758
                                          join->tmp_table_param.start_recinfo,
 
2759
                                          &join->tmp_table_param.recinfo,
 
2760
                  error, 1))
 
2761
          return NESTED_LOOP_ERROR;        // Not a table_is_full error
 
2762
        table->s->uniques= 0;                   // To ensure rows are the same
 
2763
      }
 
2764
      if (++join->send_records >= join->tmp_table_param.end_write_records && join->do_send_rows)
 
2765
      {
 
2766
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
2767
          return NESTED_LOOP_QUERY_LIMIT;
 
2768
        join->do_send_rows= 0;
 
2769
        join->unit->select_limit_cnt= HA_POS_ERROR;
 
2770
        return NESTED_LOOP_OK;
 
2771
      }
 
2772
    }
 
2773
  }
 
2774
end:
 
2775
  return NESTED_LOOP_OK;
 
2776
}
 
2777
 
 
2778
/** Group by searching after group record and updating it if possible. */
 
2779
enum_nested_loop_state end_update(JOIN *join, JoinTable *, bool end_of_records)
 
2780
{
 
2781
  Table *table= join->tmp_table;
 
2782
  order_st *group;
 
2783
  int   error;
 
2784
 
 
2785
  if (end_of_records)
 
2786
    return NESTED_LOOP_OK;
 
2787
  if (join->session->killed)                    // Aborted by user
 
2788
  {
 
2789
    join->session->send_kill_message();
 
2790
    return NESTED_LOOP_KILLED;
 
2791
  }
 
2792
 
 
2793
  join->found_records++;
 
2794
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
2795
  /* Make a key of group index */
 
2796
  for (group=table->group ; group ; group=group->next)
 
2797
  {
 
2798
    Item *item= *group->item;
 
2799
    item->save_org_in_field(group->field);
 
2800
    /* Store in the used key if the field was 0 */
 
2801
    if (item->maybe_null)
 
2802
      group->buff[-1]= (char) group->field->is_null();
 
2803
  }
 
2804
  if (!table->file->index_read_map(table->record[1],
 
2805
                                   join->tmp_table_param.group_buff,
 
2806
                                   HA_WHOLE_KEY,
 
2807
                                   HA_READ_KEY_EXACT))
 
2808
  {                                             /* Update old record */
 
2809
    table->restoreRecord();
 
2810
    update_tmptable_sum_func(join->sum_funcs,table);
 
2811
    if ((error= table->file->ha_update_row(table->record[1],
 
2812
                                          table->record[0])))
 
2813
    {
 
2814
      table->file->print_error(error,MYF(0));
 
2815
      return NESTED_LOOP_ERROR;
 
2816
    }
 
2817
    return NESTED_LOOP_OK;
 
2818
  }
 
2819
 
 
2820
  /*
 
2821
    Copy null bits from group key to table
 
2822
    We can't copy all data as the key may have different format
 
2823
    as the row data (for example as with VARCHAR keys)
 
2824
  */
 
2825
  KEY_PART_INFO *key_part;
 
2826
  for (group=table->group,key_part=table->key_info[0].key_part;
 
2827
       group ;
 
2828
       group=group->next,key_part++)
 
2829
  {
 
2830
    if (key_part->null_bit)
 
2831
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
2832
  }
 
2833
  init_tmptable_sum_functions(join->sum_funcs);
 
2834
  copy_funcs(join->tmp_table_param.items_to_copy);
 
2835
  if ((error=table->file->ha_write_row(table->record[0])))
 
2836
  {
 
2837
    if (create_myisam_from_heap(join->session, table,
 
2838
                                join->tmp_table_param.start_recinfo,
 
2839
                                &join->tmp_table_param.recinfo,
 
2840
                                error, 0))
 
2841
      return NESTED_LOOP_ERROR;            // Not a table_is_full error
 
2842
    /* Change method to update rows */
 
2843
    table->file->ha_index_init(0, 0);
 
2844
    join->join_tab[join->tables-1].next_select= end_unique_update;
 
2845
  }
 
2846
  join->send_records++;
 
2847
  return NESTED_LOOP_OK;
 
2848
}
 
2849
 
 
2850
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
2851
enum_nested_loop_state end_unique_update(JOIN *join, JoinTable *, bool end_of_records)
 
2852
{
 
2853
  Table *table= join->tmp_table;
 
2854
  int   error;
 
2855
 
 
2856
  if (end_of_records)
 
2857
    return NESTED_LOOP_OK;
 
2858
  if (join->session->killed)                    // Aborted by user
 
2859
  {
 
2860
    join->session->send_kill_message();
 
2861
    return NESTED_LOOP_KILLED;
 
2862
  }
 
2863
 
 
2864
  init_tmptable_sum_functions(join->sum_funcs);
 
2865
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
2866
  copy_funcs(join->tmp_table_param.items_to_copy);
 
2867
 
 
2868
  if (!(error= table->file->ha_write_row(table->record[0])))
 
2869
    join->send_records++;                       // New group
 
2870
  else
 
2871
  {
 
2872
    if ((int) table->file->get_dup_key(error) < 0)
 
2873
    {
 
2874
      table->file->print_error(error,MYF(0));
 
2875
      return NESTED_LOOP_ERROR;
 
2876
    }
 
2877
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
2878
    {
 
2879
      table->file->print_error(error,MYF(0));
 
2880
      return NESTED_LOOP_ERROR;
 
2881
    }
 
2882
    table->restoreRecord();
 
2883
    update_tmptable_sum_func(join->sum_funcs,table);
 
2884
    if ((error= table->file->ha_update_row(table->record[1],
 
2885
                                          table->record[0])))
 
2886
    {
 
2887
      table->file->print_error(error,MYF(0));
 
2888
      return NESTED_LOOP_ERROR;
 
2889
    }
 
2890
  }
 
2891
  return NESTED_LOOP_OK;
 
2892
}
 
2893
 
 
2894
/**
 
2895
  allocate group fields or take prepared (cached).
 
2896
 
 
2897
  @param main_join   join of current select
 
2898
  @param curr_join   current join (join of current select or temporary copy
 
2899
                     of it)
 
2900
 
 
2901
  @retval
 
2902
    0   ok
 
2903
  @retval
 
2904
    1   failed
 
2905
*/
 
2906
static bool make_group_fields(JOIN *main_join, JOIN *curr_join)
 
2907
{
 
2908
  if (main_join->group_fields_cache.elements)
 
2909
  {
 
2910
    curr_join->group_fields= main_join->group_fields_cache;
 
2911
    curr_join->sort_and_group= 1;
 
2912
  }
 
2913
  else
 
2914
  {
 
2915
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
2916
      return 1;
 
2917
    main_join->group_fields_cache= curr_join->group_fields;
 
2918
  }
 
2919
  return (0);
 
2920
}
 
2921
 
 
2922
/**
 
2923
  calc how big buffer we need for comparing group entries.
 
2924
*/
 
2925
static void calc_group_buffer(JOIN *join,order_st *group)
 
2926
{
 
2927
  uint32_t key_length=0, parts=0, null_parts=0;
 
2928
 
 
2929
  if (group)
 
2930
    join->group= 1;
 
2931
  for (; group ; group=group->next)
 
2932
  {
 
2933
    Item *group_item= *group->item;
 
2934
    Field *field= group_item->get_tmp_table_field();
 
2935
    if (field)
 
2936
    {
 
2937
      enum_field_types type;
 
2938
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
2939
        key_length+=MAX_BLOB_WIDTH;   // Can't be used as a key
 
2940
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
2941
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
2942
      else
 
2943
        key_length+= field->pack_length();
 
2944
    }
 
2945
    else
 
2946
    {
 
2947
      switch (group_item->result_type()) {
 
2948
      case REAL_RESULT:
 
2949
        key_length+= sizeof(double);
 
2950
        break;
 
2951
      case INT_RESULT:
 
2952
        key_length+= sizeof(int64_t);
 
2953
        break;
 
2954
      case DECIMAL_RESULT:
 
2955
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
2956
                                                (group_item->decimals ? 1 : 0),
 
2957
                                                group_item->decimals);
 
2958
        break;
 
2959
      case STRING_RESULT:
 
2960
      {
 
2961
        enum enum_field_types type= group_item->field_type();
 
2962
        /*
 
2963
          As items represented as DATE/TIME fields in the group buffer
 
2964
          have STRING_RESULT result type, we increase the length
 
2965
          by 8 as maximum pack length of such fields.
 
2966
        */
 
2967
        if (type == DRIZZLE_TYPE_DATE ||
 
2968
            type == DRIZZLE_TYPE_DATETIME ||
 
2969
            type == DRIZZLE_TYPE_TIMESTAMP)
 
2970
        {
 
2971
          key_length+= 8;
 
2972
        }
 
2973
        else
 
2974
        {
 
2975
          /*
 
2976
            Group strings are taken as varstrings and require an length field.
 
2977
            A field is not yet created by create_tmp_field()
 
2978
            and the sizes should match up.
 
2979
          */
 
2980
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
2981
        }
 
2982
        break;
 
2983
      }
 
2984
      default:
 
2985
        /* This case should never be choosen */
 
2986
        assert(0);
 
2987
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
2988
      }
 
2989
    }
 
2990
    parts++;
 
2991
    if (group_item->maybe_null)
 
2992
      null_parts++;
 
2993
  }
 
2994
  join->tmp_table_param.group_length=key_length+null_parts;
 
2995
  join->tmp_table_param.group_parts=parts;
 
2996
  join->tmp_table_param.group_null_parts=null_parts;
 
2997
}
 
2998
 
 
2999
/**
 
3000
  Get a list of buffers for saveing last group.
 
3001
 
 
3002
  Groups are saved in reverse order for easyer check loop.
 
3003
*/
 
3004
static bool alloc_group_fields(JOIN *join,order_st *group)
 
3005
{
 
3006
  if (group)
 
3007
  {
 
3008
    for (; group ; group=group->next)
 
3009
    {
 
3010
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
 
3011
      if (!tmp || join->group_fields.push_front(tmp))
 
3012
        return true;
 
3013
    }
 
3014
  }
 
3015
  join->sort_and_group=1;     /* Mark for do_select */
 
3016
  return false;
 
3017
}
 
3018
 
 
3019
static uint32_t cache_record_length(JOIN *join,uint32_t idx)
 
3020
{
 
3021
  uint32_t length=0;
 
3022
  JoinTable **pos,**end;
 
3023
  Session *session=join->session;
 
3024
 
 
3025
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
3026
       pos != end ;
 
3027
       pos++)
 
3028
  {
 
3029
    JoinTable *join_tab= *pos;
 
3030
    if (!join_tab->used_fieldlength)    /* Not calced yet */
 
3031
      calc_used_field_length(session, join_tab);
 
3032
    length+=join_tab->used_fieldlength;
 
3033
  }
 
3034
  return length;
 
3035
}
 
3036
 
 
3037
/*
 
3038
  Get the number of different row combinations for subset of partial join
 
3039
 
 
3040
  SYNOPSIS
 
3041
    prev_record_reads()
 
3042
      join       The join structure
 
3043
      idx        Number of tables in the partial join order (i.e. the
 
3044
                 partial join order is in join->positions[0..idx-1])
 
3045
      found_ref  Bitmap of tables for which we need to find # of distinct
 
3046
                 row combinations.
 
3047
 
 
3048
  DESCRIPTION
 
3049
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
3050
    tables within that join order (specified in found_ref), find out how many
 
3051
    distinct row combinations of subset tables will be in the result of the
 
3052
    partial join order.
 
3053
 
 
3054
    This is used as follows: Suppose we have a table accessed with a ref-based
 
3055
    method. The ref access depends on current rows of tables in found_ref.
 
3056
    We want to count # of different ref accesses. We assume two ref accesses
 
3057
    will be different if at least one of access parameters is different.
 
3058
    Example: consider a query
 
3059
 
 
3060
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
3061
 
 
3062
    and a join order:
 
3063
      t1,  ref access on t1.key=c1
 
3064
      t2,  ref access on t2.key=c2
 
3065
      t3,  ref access on t3.key=t1.field
 
3066
 
 
3067
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
3068
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
3069
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
3070
            n_distinct_ref_scans = #records_read(t1)
 
3071
 
 
3072
    The reason for having this function (at least the latest version of it)
 
3073
    is that we need to account for buffering in join execution.
 
3074
 
 
3075
    An edge-case example: if we have a non-first table in join accessed via
 
3076
    ref(const) or ref(param) where there is a small number of different
 
3077
    values of param, then the access will likely hit the disk cache and will
 
3078
    not require any disk seeks.
 
3079
 
 
3080
    The proper solution would be to assume an LRU disk cache of some size,
 
3081
    calculate probability of cache hits, etc. For now we just count
 
3082
    identical ref accesses as one.
 
3083
 
 
3084
  RETURN
 
3085
    Expected number of row combinations
 
3086
*/
 
3087
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
3088
{
 
3089
  double found=1.0;
 
3090
  optimizer::Position *pos_end= join->getSpecificPosInPartialPlan(-1);
 
3091
  for (optimizer::Position *pos= join->getSpecificPosInPartialPlan(idx - 1); 
 
3092
       pos != pos_end; 
 
3093
       pos--)
 
3094
  {
 
3095
    if (pos->examinePosition(found_ref))
 
3096
    {
 
3097
      found_ref|= pos->getRefDependMap();
 
3098
      /*
 
3099
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
 
3100
        with no matching row we will get position[t2].records_read==0.
 
3101
        Actually the size of output is one null-complemented row, therefore
 
3102
        we will use value of 1 whenever we get records_read==0.
 
3103
 
 
3104
        Note
 
3105
        - the above case can't occur if inner part of outer join has more
 
3106
          than one table: table with no matches will not be marked as const.
 
3107
 
 
3108
        - Ideally we should add 1 to records_read for every possible null-
 
3109
          complemented row. We're not doing it because: 1. it will require
 
3110
          non-trivial code and add overhead. 2. The value of records_read
 
3111
          is an inprecise estimate and adding 1 (or, in the worst case,
 
3112
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
3113
      */
 
3114
      if (pos->getFanout() > DBL_EPSILON)
 
3115
        found*= pos->getFanout();
 
3116
    }
 
3117
  }
 
3118
  return found;
 
3119
}
 
3120
 
 
3121
/**
 
3122
  Set up join struct according to best position.
 
3123
*/
 
3124
static bool get_best_combination(JOIN *join)
 
3125
{
 
3126
  uint32_t i,tablenr;
 
3127
  table_map used_tables;
 
3128
  JoinTable *join_tab,*j;
 
3129
  optimizer::KeyUse *keyuse;
 
3130
  uint32_t table_count;
 
3131
  Session *session=join->session;
 
3132
  optimizer::Position cur_pos;
 
3133
 
 
3134
  table_count=join->tables;
 
3135
  if (!(join->join_tab=join_tab=
 
3136
  (JoinTable*) session->alloc(sizeof(JoinTable)*table_count)))
 
3137
    return(true);
 
3138
 
 
3139
  join->full_join=0;
 
3140
 
 
3141
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
 
3142
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
3143
  {
 
3144
    Table *form;
 
3145
    cur_pos= join->getPosFromOptimalPlan(tablenr);
 
3146
    *j= *cur_pos.getJoinTable();
 
3147
    form=join->table[tablenr]=j->table;
 
3148
    used_tables|= form->map;
 
3149
    form->reginfo.join_tab=j;
 
3150
    if (!*j->on_expr_ref)
 
3151
      form->reginfo.not_exists_optimize=0;  // Only with LEFT JOIN
 
3152
    if (j->type == AM_CONST)
 
3153
      continue;         // Handled in make_join_stat..
 
3154
 
 
3155
    j->ref.key = -1;
 
3156
    j->ref.key_parts=0;
 
3157
 
 
3158
    if (j->type == AM_SYSTEM)
 
3159
      continue;
 
3160
    if (j->keys.none() || ! (keyuse= cur_pos.getKeyUse()))
 
3161
    {
 
3162
      j->type= AM_ALL;
 
3163
      if (tablenr != join->const_tables)
 
3164
        join->full_join=1;
 
3165
    }
 
3166
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
3167
      return(true);                        // Something went wrong
 
3168
  }
 
3169
 
 
3170
  for (i=0 ; i < table_count ; i++)
 
3171
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
3172
  update_depend_map(join);
 
3173
  return(0);
 
3174
}
 
3175
 
 
3176
/** Save const tables first as used tables. */
 
3177
static void set_position(JOIN *join,
 
3178
                         uint32_t idx,
 
3179
                         JoinTable *table,
 
3180
                         optimizer::KeyUse *key)
 
3181
{
 
3182
  optimizer::Position tmp_pos(1.0, /* This is a const table */
 
3183
                              0.0,
 
3184
                              table,
 
3185
                              key,
 
3186
                              0);
 
3187
  join->setPosInPartialPlan(idx, tmp_pos);
 
3188
 
 
3189
  /* Move the const table as down as possible in best_ref */
 
3190
  JoinTable **pos=join->best_ref+idx+1;
 
3191
  JoinTable *next=join->best_ref[idx];
 
3192
  for (;next != table ; pos++)
 
3193
  {
 
3194
    JoinTable *tmp=pos[0];
 
3195
    pos[0]=next;
 
3196
    next=tmp;
 
3197
  }
 
3198
  join->best_ref[idx]=table;
 
3199
}
 
3200
 
 
3201
/**
 
3202
  Selects and invokes a search strategy for an optimal query plan.
 
3203
 
 
3204
  The function checks user-configurable parameters that control the search
 
3205
  strategy for an optimal plan, selects the search method and then invokes
 
3206
  it. Each specific optimization procedure stores the final optimal plan in
 
3207
  the array 'join->best_positions', and the cost of the plan in
 
3208
  'join->best_read'.
 
3209
 
 
3210
  @param join         pointer to the structure providing all context info for
 
3211
                      the query
 
3212
  @param join_tables  set of the tables in the query
 
3213
 
 
3214
  @retval
 
3215
    false       ok
 
3216
  @retval
 
3217
    true        Fatal error
 
3218
*/
 
3219
static bool choose_plan(JOIN *join, table_map join_tables)
 
3220
{
 
3221
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
 
3222
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
 
3223
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
3224
 
 
3225
  join->cur_embedding_map.reset();
 
3226
  reset_nj_counters(join->join_list);
 
3227
  /*
 
3228
    if (SELECT_STRAIGHT_JOIN option is set)
 
3229
      reorder tables so dependent tables come after tables they depend
 
3230
      on, otherwise keep tables in the order they were specified in the query
 
3231
    else
 
3232
      Apply heuristic: pre-sort all access plans with respect to the number of
 
3233
      records accessed.
 
3234
  */
 
3235
  my_qsort(join->best_ref + join->const_tables,
 
3236
           join->tables - join->const_tables, sizeof(JoinTable*),
 
3237
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
3238
  if (straight_join)
 
3239
  {
 
3240
    optimize_straight_join(join, join_tables);
 
3241
  }
 
3242
  else
 
3243
  {
 
3244
    if (search_depth == 0)
 
3245
      /* Automatically determine a reasonable value for 'search_depth' */
 
3246
      search_depth= determine_search_depth(join);
 
3247
    if (greedy_search(join, join_tables, search_depth, prune_level))
 
3248
      return true;
 
3249
  }
 
3250
 
 
3251
  /*
 
3252
    Store the cost of this query into a user variable
 
3253
    Don't update last_query_cost for statements that are not "flat joins" :
 
3254
    i.e. they have subqueries, unions or call stored procedures.
 
3255
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
3256
  */
 
3257
  if (join->session->lex->is_single_level_stmt())
 
3258
    join->session->status_var.last_query_cost= join->best_read;
 
3259
  return(false);
 
3260
}
 
3261
 
 
3262
/**
 
3263
  Find the best access path for an extension of a partial execution
 
3264
  plan and add this path to the plan.
 
3265
 
 
3266
  The function finds the best access path to table 's' from the passed
 
3267
  partial plan where an access path is the general term for any means to
 
3268
  access the data in 's'. An access path may use either an index or a scan,
 
3269
  whichever is cheaper. The input partial plan is passed via the array
 
3270
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
3271
  cost are stored in 'join->positions[idx]'.
 
3272
 
 
3273
  @param join             pointer to the structure providing all context info
 
3274
                          for the query
 
3275
  @param s                the table to be joined by the function
 
3276
  @param session              thread for the connection that submitted the query
 
3277
  @param remaining_tables set of tables not included into the partial plan yet
 
3278
  @param idx              the length of the partial plan
 
3279
  @param record_count     estimate for the number of records returned by the
 
3280
                          partial plan
 
3281
  @param read_time        the cost of the partial plan
 
3282
 
 
3283
  @return
 
3284
    None
 
3285
*/
 
3286
static void best_access_path(JOIN *join,
 
3287
                             JoinTable *s,
 
3288
                             Session *session,
 
3289
                             table_map remaining_tables,
 
3290
                             uint32_t idx,
 
3291
                             double record_count,
 
3292
                             double)
 
3293
{
 
3294
  optimizer::KeyUse *best_key= NULL;
 
3295
  uint32_t best_max_key_part= NULL;
 
3296
  bool found_constraint= 0;
 
3297
  double best= DBL_MAX;
 
3298
  double best_time= DBL_MAX;
 
3299
  double records= DBL_MAX;
 
3300
  table_map best_ref_depends_map= 0;
 
3301
  double tmp;
 
3302
  ha_rows rec;
 
3303
 
 
3304
  if (s->keyuse)
 
3305
  {                                            /* Use key if possible */
 
3306
    Table *table= s->table;
 
3307
    optimizer::KeyUse *keyuse= NULL;
 
3308
    optimizer::KeyUse *start_key= NULL;
 
3309
    double best_records= DBL_MAX;
 
3310
    uint32_t max_key_part=0;
 
3311
 
 
3312
    /* Test how we can use keys */
 
3313
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
3314
    for (keyuse= s->keyuse; keyuse->getTable() == table; )
 
3315
    {
 
3316
      key_part_map found_part= 0;
 
3317
      table_map found_ref= 0;
 
3318
      uint32_t key= keyuse->getKey();
 
3319
      KEY *keyinfo= table->key_info + key;
 
3320
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
3321
      key_part_map const_part= 0;
 
3322
      /* The or-null keypart in ref-or-null access: */
 
3323
      key_part_map ref_or_null_part= 0;
 
3324
 
 
3325
      /* Calculate how many key segments of the current key we can use */
 
3326
      start_key= keyuse;
 
3327
 
 
3328
      do /* For each keypart */
 
3329
      {
 
3330
        uint32_t keypart= keyuse->getKeypart();
 
3331
        table_map best_part_found_ref= 0;
 
3332
        double best_prev_record_reads= DBL_MAX;
 
3333
 
 
3334
        do /* For each way to access the keypart */
 
3335
        {
 
3336
 
 
3337
          /*
 
3338
            if 1. expression doesn't refer to forward tables
 
3339
               2. we won't get two ref-or-null's
 
3340
          */
 
3341
          if (! (remaining_tables & keyuse->getUsedTables()) &&
 
3342
              ! (ref_or_null_part && (keyuse->getOptimizeFlags() &
 
3343
                                      KEY_OPTIMIZE_REF_OR_NULL)))
 
3344
          {
 
3345
            found_part|= keyuse->getKeypartMap();
 
3346
            if (! (keyuse->getUsedTables() & ~join->const_table_map))
 
3347
              const_part|= keyuse->getKeypartMap();
 
3348
 
 
3349
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
3350
                                                       keyuse->getUsedTables()));
 
3351
            if (tmp2 < best_prev_record_reads)
 
3352
            {
 
3353
              best_part_found_ref= keyuse->getUsedTables() & ~join->const_table_map;
 
3354
              best_prev_record_reads= tmp2;
 
3355
            }
 
3356
            if (rec > keyuse->getTableRows())
 
3357
              rec= keyuse->getTableRows();
 
3358
      /*
 
3359
        If there is one 'key_column IS NULL' expression, we can
 
3360
        use this ref_or_null optimisation of this field
 
3361
      */
 
3362
            if (keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)
 
3363
              ref_or_null_part|= keyuse->getKeypartMap();
 
3364
          }
 
3365
 
 
3366
          keyuse++;
 
3367
        } while (keyuse->getTable() == table && keyuse->getKey() == key &&
 
3368
                 keyuse->getKeypart() == keypart);
 
3369
        found_ref|= best_part_found_ref;
 
3370
      } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
3371
 
 
3372
      /*
 
3373
        Assume that that each key matches a proportional part of table.
 
3374
      */
 
3375
      if (!found_part)
 
3376
        continue;                               // Nothing usable found
 
3377
 
 
3378
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
3379
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
3380
 
 
3381
      {
 
3382
        found_constraint= 1;
 
3383
 
 
3384
        /*
 
3385
          Check if we found full key
 
3386
        */
 
3387
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
3388
            !ref_or_null_part)
 
3389
        {                                         /* use eq key */
 
3390
          max_key_part= UINT32_MAX;
 
3391
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
3392
          {
 
3393
            tmp = prev_record_reads(join, idx, found_ref);
 
3394
            records=1.0;
 
3395
          }
 
3396
          else
 
3397
          {
 
3398
            if (!found_ref)
 
3399
            {                                     /* We found a const key */
 
3400
              /*
 
3401
                ReuseRangeEstimateForRef-1:
 
3402
                We get here if we've found a ref(const) (c_i are constants):
 
3403
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
3404
 
 
3405
                If range optimizer was able to construct a "range"
 
3406
                access on this index, then its condition "quick_cond" was
 
3407
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
3408
                from the range optimizer.
 
3409
 
 
3410
                Proof of (*): By properties of range and ref optimizers
 
3411
                quick_cond will be equal or tighther than ref_const_cond.
 
3412
                ref_const_cond already covers "smallest" possible interval -
 
3413
                a singlepoint interval over all keyparts. Therefore,
 
3414
                quick_cond is equivalent to ref_const_cond (if it was an
 
3415
                empty interval we wouldn't have got here).
 
3416
              */
 
3417
              if (table->quick_keys.test(key))
 
3418
                records= (double) table->quick_rows[key];
 
3419
              else
 
3420
              {
 
3421
                /* quick_range couldn't use key! */
 
3422
                records= (double) s->records/rec;
 
3423
              }
 
3424
            }
 
3425
            else
 
3426
            {
 
3427
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
3428
              {                                   /* Prefer longer keys */
 
3429
                records=
 
3430
                  ((double) s->records / (double) rec *
 
3431
                   (1.0 +
 
3432
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
3433
                     (double) table->s->max_key_length)));
 
3434
                if (records < 2.0)
 
3435
                  records=2.0;               /* Can't be as good as a unique */
 
3436
              }
 
3437
              /*
 
3438
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
3439
                E(#rows) from range optimizer. Make another try:
 
3440
 
 
3441
                If range optimizer produced E(#rows) for a prefix of the ref
 
3442
                access we're considering, and that E(#rows) is lower then our
 
3443
                current estimate, make an adjustment. The criteria of when we
 
3444
                can make an adjustment is a special case of the criteria used
 
3445
                in ReuseRangeEstimateForRef-3.
 
3446
              */
 
3447
              if (table->quick_keys.test(key) &&
 
3448
                  const_part & (1 << table->quick_key_parts[key]) &&
 
3449
                  table->quick_n_ranges[key] == 1 &&
 
3450
                  records > (double) table->quick_rows[key])
 
3451
              {
 
3452
                records= (double) table->quick_rows[key];
 
3453
              }
 
3454
            }
 
3455
            /* Limit the number of matched rows */
 
3456
            tmp= records;
 
3457
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
3458
            if (table->covering_keys.test(key))
 
3459
            {
 
3460
              /* we can use only index tree */
 
3461
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
3462
            }
 
3463
            else
 
3464
              tmp= record_count * min(tmp,s->worst_seeks);
 
3465
          }
 
3466
        }
 
3467
        else
 
3468
        {
 
3469
          /*
 
3470
            Use as much key-parts as possible and a uniq key is better
 
3471
            than a not unique key
 
3472
            Set tmp to (previous record count) * (records / combination)
 
3473
          */
 
3474
          if ((found_part & 1) &&
 
3475
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
3476
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
3477
          {
 
3478
            max_key_part= max_part_bit(found_part);
 
3479
            /*
 
3480
              ReuseRangeEstimateForRef-3:
 
3481
              We're now considering a ref[or_null] access via
 
3482
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
 
3483
              (same-as-above but with one cond replaced
 
3484
               with "t.keypart_i IS NULL")]  (**)
 
3485
 
 
3486
              Try re-using E(#rows) from "range" optimizer:
 
3487
              We can do so if "range" optimizer used the same intervals as
 
3488
              in (**). The intervals used by range optimizer may be not
 
3489
              available at this point (as "range" access might have choosen to
 
3490
              create quick select over another index), so we can't compare
 
3491
              them to (**). We'll make indirect judgements instead.
 
3492
              The sufficient conditions for re-use are:
 
3493
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
3494
                   this is not satisfied we have no way to know which ranges
 
3495
                   will be actually scanned by 'ref' until we execute the
 
3496
                   join)
 
3497
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
3498
                   is apparently a necessary requirement)
 
3499
 
 
3500
              We also have a property that "range optimizer produces equal or
 
3501
              tighter set of scan intervals than ref(const) optimizer". Each
 
3502
              of the intervals in (**) are "tightest possible" intervals when
 
3503
              one limits itself to using keyparts 1..K (which we do in #2).
 
3504
              From here it follows that range access used either one, or
 
3505
              both of the (I1) and (I2) intervals:
 
3506
 
 
3507
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
 
3508
               (same-as-above but with one cond replaced
 
3509
                with "t.keypart_i IS NULL")               (I2)
 
3510
 
 
3511
              The remaining part is to exclude the situation where range
 
3512
              optimizer used one interval while we're considering
 
3513
              ref-or-null and looking for estimate for two intervals. This
 
3514
              is done by last limitation:
 
3515
 
 
3516
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
3517
            */
 
3518
            if (table->quick_keys.test(key) && !found_ref &&          //(C1)
 
3519
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
3520
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
3521
            {
 
3522
              tmp= records= (double) table->quick_rows[key];
 
3523
            }
 
3524
            else
 
3525
            {
 
3526
              /* Check if we have statistic about the distribution */
 
3527
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
3528
              {
 
3529
                /*
 
3530
                  Fix for the case where the index statistics is too
 
3531
                  optimistic: If
 
3532
                  (1) We're considering ref(const) and there is quick select
 
3533
                      on the same index,
 
3534
                  (2) and that quick select uses more keyparts (i.e. it will
 
3535
                      scan equal/smaller interval then this ref(const))
 
3536
                  (3) and E(#rows) for quick select is higher then our
 
3537
                      estimate,
 
3538
                  Then
 
3539
                    We'll use E(#rows) from quick select.
 
3540
 
 
3541
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
3542
                  cheaper in some cases ?
 
3543
                  TODO: figure this out and adjust the plan choice if needed.
 
3544
                */
 
3545
                if (!found_ref && table->quick_keys.test(key) &&    // (1)
 
3546
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
3547
                    records < (double)table->quick_rows[key])         // (3)
 
3548
                  records= (double)table->quick_rows[key];
 
3549
 
 
3550
                tmp= records;
 
3551
              }
 
3552
              else
 
3553
              {
 
3554
                /*
 
3555
                  Assume that the first key part matches 1% of the file
 
3556
                  and that the whole key matches 10 (duplicates) or 1
 
3557
                  (unique) records.
 
3558
                  Assume also that more key matches proportionally more
 
3559
                  records
 
3560
                  This gives the formula:
 
3561
                  records = (x * (b-a) + a*c-b)/(c-1)
 
3562
 
 
3563
                  b = records matched by whole key
 
3564
                  a = records matched by first key part (1% of all records?)
 
3565
                  c = number of key parts in key
 
3566
                  x = used key parts (1 <= x <= c)
 
3567
                */
 
3568
                double rec_per_key;
 
3569
                if (!(rec_per_key=(double)
 
3570
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
3571
                  rec_per_key=(double) s->records/rec+1;
 
3572
 
 
3573
                if (!s->records)
 
3574
                  tmp = 0;
 
3575
                else if (rec_per_key/(double) s->records >= 0.01)
 
3576
                  tmp = rec_per_key;
 
3577
                else
 
3578
                {
 
3579
                  double a=s->records*0.01;
 
3580
                  if (keyinfo->key_parts > 1)
 
3581
                    tmp= (max_key_part * (rec_per_key - a) +
 
3582
                          a*keyinfo->key_parts - rec_per_key)/
 
3583
                         (keyinfo->key_parts-1);
 
3584
                  else
 
3585
                    tmp= a;
 
3586
                  set_if_bigger(tmp,1.0);
 
3587
                }
 
3588
                records = (uint32_t) tmp;
 
3589
              }
 
3590
 
 
3591
              if (ref_or_null_part)
 
3592
              {
 
3593
                /* We need to do two key searches to find key */
 
3594
                tmp *= 2.0;
 
3595
                records *= 2.0;
 
3596
              }
 
3597
 
 
3598
              /*
 
3599
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
3600
                E(#rows) from range optimizer. Make another try:
 
3601
 
 
3602
                If range optimizer produced E(#rows) for a prefix of the ref
 
3603
                access we're considering, and that E(#rows) is lower then our
 
3604
                current estimate, make the adjustment.
 
3605
 
 
3606
                The decision whether we can re-use the estimate from the range
 
3607
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
3608
                applied to first table->quick_key_parts[key] key parts.
 
3609
              */
 
3610
              if (table->quick_keys.test(key) &&
 
3611
                  table->quick_key_parts[key] <= max_key_part &&
 
3612
                  const_part & (1 << table->quick_key_parts[key]) &&
 
3613
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
3614
                                                     const_part) ? 1 : 0) &&
 
3615
                  records > (double) table->quick_rows[key])
 
3616
              {
 
3617
                tmp= records= (double) table->quick_rows[key];
 
3618
              }
 
3619
            }
 
3620
 
 
3621
            /* Limit the number of matched rows */
 
3622
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
3623
            if (table->covering_keys.test(key))
 
3624
            {
 
3625
              /* we can use only index tree */
 
3626
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
3627
            }
 
3628
            else
 
3629
              tmp= record_count * min(tmp,s->worst_seeks);
 
3630
          }
 
3631
          else
 
3632
            tmp= best_time;                    // Do nothing
 
3633
        }
 
3634
 
 
3635
      }
 
3636
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
3637
      {
 
3638
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
3639
        best= tmp;
 
3640
        best_records= records;
 
3641
        best_key= start_key;
 
3642
        best_max_key_part= max_key_part;
 
3643
        best_ref_depends_map= found_ref;
 
3644
      }
 
3645
    }
 
3646
    records= best_records;
 
3647
  }
 
3648
 
 
3649
  /*
 
3650
    Don't test table scan if it can't be better.
 
3651
    Prefer key lookup if we would use the same key for scanning.
 
3652
 
 
3653
    Don't do a table scan on InnoDB tables, if we can read the used
 
3654
    parts of the row from any of the used index.
 
3655
    This is because table scans uses index and we would not win
 
3656
    anything by using a table scan.
 
3657
 
 
3658
    A word for word translation of the below if-statement in sergefp's
 
3659
    understanding: we check if we should use table scan if:
 
3660
    (1) The found 'ref' access produces more records than a table scan
 
3661
        (or index scan, or quick select), or 'ref' is more expensive than
 
3662
        any of them.
 
3663
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
3664
        'range' access using index IDX, and the best way to perform 'ref'
 
3665
        access is to use the same index IDX, with the same or more key parts.
 
3666
        (note: it is not clear how this rule is/should be extended to
 
3667
        index_merge quick selects)
 
3668
    (3) See above note about InnoDB.
 
3669
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
3670
             path, but there is no quick select)
 
3671
        If the condition in the above brackets holds, then the only possible
 
3672
        "table scan" access method is ALL/index (there is no quick select).
 
3673
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
3674
        choose it over ALL/index, there is no need to consider a full table
 
3675
        scan.
 
3676
  */
 
3677
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
3678
      ! (s->quick && best_key && s->quick->index == best_key->getKey() &&      // (2)
 
3679
        best_max_key_part >= s->table->quick_key_parts[best_key->getKey()]) &&// (2)
 
3680
      ! ((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
3681
        ! s->table->covering_keys.none() && best_key && !s->quick) && // (3)
 
3682
      ! (s->table->force_index && best_key && !s->quick))                 // (4)
 
3683
  {                                             // Check full join
 
3684
    ha_rows rnd_records= s->found_records;
 
3685
    /*
 
3686
      If there is a filtering condition on the table (i.e. ref analyzer found
 
3687
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
3688
      preceding this table in the join order we're now considering), then
 
3689
      assume that 25% of the rows will be filtered out by this condition.
 
3690
 
 
3691
      This heuristic is supposed to force tables used in exprZ to be before
 
3692
      this table in join order.
 
3693
    */
 
3694
    if (found_constraint)
 
3695
      rnd_records-= rnd_records/4;
 
3696
 
 
3697
    /*
 
3698
      If applicable, get a more accurate estimate. Don't use the two
 
3699
      heuristics at once.
 
3700
    */
 
3701
    if (s->table->quick_condition_rows != s->found_records)
 
3702
      rnd_records= s->table->quick_condition_rows;
 
3703
 
 
3704
    /*
 
3705
      Range optimizer never proposes a RANGE if it isn't better
 
3706
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
3707
      Here we estimate its cost.
 
3708
    */
 
3709
    if (s->quick)
 
3710
    {
 
3711
      /*
 
3712
        For each record we:
 
3713
        - read record range through 'quick'
 
3714
        - skip rows which does not satisfy WHERE constraints
 
3715
        TODO:
 
3716
        We take into account possible use of join cache for ALL/index
 
3717
        access (see first else-branch below), but we don't take it into
 
3718
        account here for range/index_merge access. Find out why this is so.
 
3719
      */
 
3720
      tmp= record_count *
 
3721
        (s->quick->read_time +
 
3722
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
3723
    }
 
3724
    else
 
3725
    {
 
3726
      /* Estimate cost of reading table. */
 
3727
      tmp= s->table->file->scan_time();
 
3728
      if (s->table->map & join->outer_join)     // Can't use join cache
 
3729
      {
 
3730
        /*
 
3731
          For each record we have to:
 
3732
          - read the whole table record
 
3733
          - skip rows which does not satisfy join condition
 
3734
        */
 
3735
        tmp= record_count *
 
3736
          (tmp +
 
3737
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
3738
      }
 
3739
      else
 
3740
      {
 
3741
        /* We read the table as many times as join buffer becomes full. */
 
3742
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
3743
                           record_count /
 
3744
                           (double) session->variables.join_buff_size));
 
3745
        /*
 
3746
            We don't make full cartesian product between rows in the scanned
 
3747
           table and existing records because we skip all rows from the
 
3748
           scanned table, which does not satisfy join condition when
 
3749
           we read the table (see flush_cached_records for details). Here we
 
3750
           take into account cost to read and skip these records.
 
3751
        */
 
3752
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
3753
      }
 
3754
    }
 
3755
 
 
3756
    /*
 
3757
      We estimate the cost of evaluating WHERE clause for found records
 
3758
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
3759
      tmp give us total cost of using Table SCAN
 
3760
    */
 
3761
    if (best == DBL_MAX ||
 
3762
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
3763
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
3764
    {
 
3765
      /*
 
3766
        If the table has a range (s->quick is set) make_join_select()
 
3767
        will ensure that this will be used
 
3768
      */
 
3769
      best= tmp;
 
3770
      records= rows2double(rnd_records);
 
3771
      best_key= 0;
 
3772
      /* range/index_merge/ALL/index access method are "independent", so: */
 
3773
      best_ref_depends_map= 0;
 
3774
    }
 
3775
  }
 
3776
 
 
3777
  /* Update the cost information for the current partial plan */
 
3778
  optimizer::Position tmp_pos(records,
 
3779
                              best,
 
3780
                              s,
 
3781
                              best_key,
 
3782
                              best_ref_depends_map);
 
3783
  join->setPosInPartialPlan(idx, tmp_pos);
 
3784
 
 
3785
  if (!best_key &&
 
3786
      idx == join->const_tables &&
 
3787
      s->table == join->sort_by_table &&
 
3788
      join->unit->select_limit_cnt >= records)
 
3789
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
3790
 
 
3791
  return;
 
3792
}
 
3793
 
 
3794
/**
 
3795
  Select the best ways to access the tables in a query without reordering them.
 
3796
 
 
3797
    Find the best access paths for each query table and compute their costs
 
3798
    according to their order in the array 'join->best_ref' (thus without
 
3799
    reordering the join tables). The function calls sequentially
 
3800
    'best_access_path' for each table in the query to select the best table
 
3801
    access method. The final optimal plan is stored in the array
 
3802
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
3803
 
 
3804
  @param join          pointer to the structure providing all context info for
 
3805
                       the query
 
3806
  @param join_tables   set of the tables in the query
 
3807
 
 
3808
  @note
 
3809
    This function can be applied to:
 
3810
    - queries with STRAIGHT_JOIN
 
3811
    - internally to compute the cost of an arbitrary QEP
 
3812
  @par
 
3813
    Thus 'optimize_straight_join' can be used at any stage of the query
 
3814
    optimization process to finalize a QEP as it is.
 
3815
*/
 
3816
static void optimize_straight_join(JOIN *join, table_map join_tables)
 
3817
{
 
3818
  JoinTable *s;
 
3819
  optimizer::Position partial_pos;
 
3820
  uint32_t idx= join->const_tables;
 
3821
  double    record_count= 1.0;
 
3822
  double    read_time=    0.0;
 
3823
 
 
3824
  for (JoinTable **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
3825
  {
 
3826
    /* Find the best access method from 's' to the current partial plan */
 
3827
    best_access_path(join, s, join->session, join_tables, idx,
 
3828
                     record_count, read_time);
 
3829
    /* compute the cost of the new plan extended with 's' */
 
3830
    partial_pos= join->getPosFromPartialPlan(idx);
 
3831
    record_count*= partial_pos.getFanout();
 
3832
    read_time+=    partial_pos.getCost();
 
3833
    join_tables&= ~(s->table->map);
 
3834
    ++idx;
 
3835
  }
 
3836
 
 
3837
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
3838
  partial_pos= join->getPosFromPartialPlan(join->const_tables);
 
3839
  if (join->sort_by_table &&
 
3840
      partial_pos.hasTableForSorting(join->sort_by_table))
 
3841
    read_time+= record_count;  // We have to make a temp table
 
3842
  join->copyPartialPlanIntoOptimalPlan(idx);
 
3843
  join->best_read= read_time;
 
3844
}
 
3845
 
 
3846
/**
 
3847
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
3848
 
 
3849
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
3850
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
3851
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
3852
    selects the most promising table, and extends the current partial QEP with
 
3853
    that table.  Currenly the most 'promising' table is the one with least
 
3854
    expensive extension.\
 
3855
 
 
3856
    There are two extreme cases:
 
3857
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
3858
    best complete continuation of the partial QEP. This continuation can be
 
3859
    used directly as a result of the search.
 
3860
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
3861
    consideres the extension of the current QEP with each of the remaining
 
3862
    unoptimized tables.
 
3863
 
 
3864
    All other cases are in-between these two extremes. Thus the parameter
 
3865
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
3866
    value, the longer the optimizaton time and possibly the better the
 
3867
    resulting plan. The lower the value, the fewer alternative plans are
 
3868
    estimated, but the more likely to get a bad QEP.
 
3869
 
 
3870
    All intermediate and final results of the procedure are stored in 'join':
 
3871
    - join->positions     : modified for every partial QEP that is explored
 
3872
    - join->best_positions: modified for the current best complete QEP
 
3873
    - join->best_read     : modified for the current best complete QEP
 
3874
    - join->best_ref      : might be partially reordered
 
3875
 
 
3876
    The final optimal plan is stored in 'join->best_positions', and its
 
3877
    corresponding cost in 'join->best_read'.
 
3878
 
 
3879
  @note
 
3880
    The following pseudocode describes the algorithm of 'greedy_search':
 
3881
 
 
3882
    @code
 
3883
    procedure greedy_search
 
3884
    input: remaining_tables
 
3885
    output: pplan;
 
3886
    {
 
3887
      pplan = <>;
 
3888
      do {
 
3889
        (t, a) = best_extension(pplan, remaining_tables);
 
3890
        pplan = concat(pplan, (t, a));
 
3891
        remaining_tables = remaining_tables - t;
 
3892
      } while (remaining_tables != {})
 
3893
      return pplan;
 
3894
    }
 
3895
 
 
3896
  @endcode
 
3897
    where 'best_extension' is a placeholder for a procedure that selects the
 
3898
    most "promising" of all tables in 'remaining_tables'.
 
3899
    Currently this estimate is performed by calling
 
3900
    'best_extension_by_limited_search' to evaluate all extensions of the
 
3901
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
3902
    mainly depends on that of 'best_extension_by_limited_search'.
 
3903
 
 
3904
  @par
 
3905
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
3906
    worst-case complexity of this algorithm is <=
 
3907
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
3908
    complexity of greedy_search is O(N!).
 
3909
 
 
3910
  @par
 
3911
    In the future, 'greedy_search' might be extended to support other
 
3912
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
3913
 
 
3914
  @param join             pointer to the structure providing all context info
 
3915
                          for the query
 
3916
  @param remaining_tables set of tables not included into the partial plan yet
 
3917
  @param search_depth     controlls the exhaustiveness of the search
 
3918
  @param prune_level      the pruning heuristics that should be applied during
 
3919
                          search
 
3920
 
 
3921
  @retval
 
3922
    false       ok
 
3923
  @retval
 
3924
    true        Fatal error
 
3925
*/
 
3926
static bool greedy_search(JOIN      *join,
 
3927
              table_map remaining_tables,
 
3928
              uint32_t      search_depth,
 
3929
              uint32_t      prune_level)
 
3930
{
 
3931
  double    record_count= 1.0;
 
3932
  double    read_time=    0.0;
 
3933
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
3934
  uint32_t      best_idx;
 
3935
  uint32_t      size_remain;    // cardinality of remaining_tables
 
3936
  optimizer::Position best_pos;
 
3937
  JoinTable  *best_table; // the next plan node to be added to the curr QEP
 
3938
 
 
3939
  /* number of tables that remain to be optimized */
 
3940
  size_remain= my_count_bits(remaining_tables);
 
3941
 
 
3942
  do {
 
3943
    /* Find the extension of the current QEP with the lowest cost */
 
3944
    join->best_read= DBL_MAX;
 
3945
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
3946
                                         read_time, search_depth, prune_level))
 
3947
      return(true);
 
3948
 
 
3949
    if (size_remain <= search_depth)
 
3950
    {
 
3951
      /*
 
3952
        'join->best_positions' contains a complete optimal extension of the
 
3953
        current partial QEP.
 
3954
      */
 
3955
      return(false);
 
3956
    }
 
3957
 
 
3958
    /* select the first table in the optimal extension as most promising */
 
3959
    best_pos= join->getPosFromOptimalPlan(idx);
 
3960
    best_table= best_pos.getJoinTable();
 
3961
    /*
 
3962
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
3963
      'join->positions' for cost estimates, therefore we have to update its
 
3964
      value.
 
3965
    */
 
3966
    join->setPosInPartialPlan(idx, best_pos);
 
3967
 
 
3968
    /* find the position of 'best_table' in 'join->best_ref' */
 
3969
    best_idx= idx;
 
3970
    JoinTable *pos= join->best_ref[best_idx];
 
3971
    while (pos && best_table != pos)
 
3972
      pos= join->best_ref[++best_idx];
 
3973
    assert((pos != NULL)); // should always find 'best_table'
 
3974
    /* move 'best_table' at the first free position in the array of joins */
 
3975
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
3976
 
 
3977
    /* compute the cost of the new plan extended with 'best_table' */
 
3978
    optimizer::Position partial_pos= join->getPosFromPartialPlan(idx);
 
3979
    record_count*= partial_pos.getFanout();
 
3980
    read_time+=    partial_pos.getCost();
 
3981
 
 
3982
    remaining_tables&= ~(best_table->table->map);
 
3983
    --size_remain;
 
3984
    ++idx;
 
3985
  } while (true);
 
3986
}
 
3987
 
 
3988
 
 
3989
/**
 
3990
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
3991
  exhaustive search.
 
3992
 
 
3993
    The procedure searches for the optimal ordering of the query tables in set
 
3994
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
3995
    each table. The choice of a table order and an access path for each table
 
3996
    constitutes a query execution plan (QEP) that fully specifies how to
 
3997
    execute the query.
 
3998
 
 
3999
    The maximal size of the found plan is controlled by the parameter
 
4000
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
4001
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
4002
    of only some of the query tables. Such "partial" optimal plans are useful
 
4003
    only as input to query optimization procedures, and cannot be used directly
 
4004
    to execute a query.
 
4005
 
 
4006
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
4007
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
4008
    evaluates the cost of the partial plan extended by all access plans for
 
4009
    each of the relations in 'remaining_tables', expands the current partial
 
4010
    plan with the access plan that results in lowest cost of the expanded
 
4011
    partial plan, and removes the corresponding relation from
 
4012
    'remaining_tables'. The algorithm continues until it either constructs a
 
4013
    complete optimal plan, or constructs an optimal plartial plan with size =
 
4014
    search_depth.
 
4015
 
 
4016
    The final optimal plan is stored in 'join->best_positions'. The
 
4017
    corresponding cost of the optimal plan is in 'join->best_read'.
 
4018
 
 
4019
  @note
 
4020
    The procedure uses a recursive depth-first search where the depth of the
 
4021
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
4022
    parameter 'search_depth'.
 
4023
 
 
4024
  @note
 
4025
    The pseudocode below describes the algorithm of
 
4026
    'best_extension_by_limited_search'. The worst-case complexity of this
 
4027
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
4028
    the complexity of greedy_search is O(N!).
 
4029
 
 
4030
    @code
 
4031
    procedure best_extension_by_limited_search(
 
4032
      pplan in,             // in, partial plan of tables-joined-so-far
 
4033
      pplan_cost,           // in, cost of pplan
 
4034
      remaining_tables,     // in, set of tables not referenced in pplan
 
4035
      best_plan_so_far,     // in/out, best plan found so far
 
4036
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
4037
      search_depth)         // in, maximum size of the plans being considered
 
4038
    {
 
4039
      for each table T from remaining_tables
 
4040
      {
 
4041
        // Calculate the cost of using table T as above
 
4042
        cost = complex-series-of-calculations;
 
4043
 
 
4044
        // Add the cost to the cost so far.
 
4045
        pplan_cost+= cost;
 
4046
 
 
4047
        if (pplan_cost >= best_plan_so_far_cost)
 
4048
          // pplan_cost already too great, stop search
 
4049
          continue;
 
4050
 
 
4051
        pplan= expand pplan by best_access_method;
 
4052
        remaining_tables= remaining_tables - table T;
 
4053
        if (remaining_tables is not an empty set
 
4054
            and
 
4055
            search_depth > 1)
 
4056
        {
 
4057
          best_extension_by_limited_search(pplan, pplan_cost,
 
4058
                                           remaining_tables,
 
4059
                                           best_plan_so_far,
 
4060
                                           best_plan_so_far_cost,
 
4061
                                           search_depth - 1);
 
4062
        }
 
4063
        else
 
4064
        {
 
4065
          best_plan_so_far_cost= pplan_cost;
 
4066
          best_plan_so_far= pplan;
 
4067
        }
 
4068
      }
 
4069
    }
 
4070
    @endcode
 
4071
 
 
4072
  @note
 
4073
    When 'best_extension_by_limited_search' is called for the first time,
 
4074
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
4075
    The actual implementation provides a way to optionally use pruning
 
4076
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
4077
    space by skipping some partial plans.
 
4078
 
 
4079
  @note
 
4080
    The parameter 'search_depth' provides control over the recursion
 
4081
    depth, and thus the size of the resulting optimal plan.
 
4082
 
 
4083
  @param join             pointer to the structure providing all context info
 
4084
                          for the query
 
4085
  @param remaining_tables set of tables not included into the partial plan yet
 
4086
  @param idx              length of the partial QEP in 'join->positions';
 
4087
                          since a depth-first search is used, also corresponds
 
4088
                          to the current depth of the search tree;
 
4089
                          also an index in the array 'join->best_ref';
 
4090
  @param record_count     estimate for the number of records returned by the
 
4091
                          best partial plan
 
4092
  @param read_time        the cost of the best partial plan
 
4093
  @param search_depth     maximum depth of the recursion and thus size of the
 
4094
                          found optimal plan
 
4095
                          (0 < search_depth <= join->tables+1).
 
4096
  @param prune_level      pruning heuristics that should be applied during
 
4097
                          optimization
 
4098
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
4099
 
 
4100
  @retval
 
4101
    false       ok
 
4102
  @retval
 
4103
    true        Fatal error
 
4104
*/
 
4105
static bool best_extension_by_limited_search(JOIN *join,
 
4106
                                             table_map remaining_tables,
 
4107
                                             uint32_t idx,
 
4108
                                             double record_count,
 
4109
                                             double read_time,
 
4110
                                             uint32_t search_depth,
 
4111
                                             uint32_t prune_level)
 
4112
{
 
4113
  Session *session= join->session;
 
4114
  if (session->killed)  // Abort
 
4115
    return(true);
 
4116
 
 
4117
  /*
 
4118
     'join' is a partial plan with lower cost than the best plan so far,
 
4119
     so continue expanding it further with the tables in 'remaining_tables'.
 
4120
  */
 
4121
  JoinTable *s;
 
4122
  double best_record_count= DBL_MAX;
 
4123
  double best_read_time=    DBL_MAX;
 
4124
  optimizer::Position partial_pos;
 
4125
 
 
4126
  for (JoinTable **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
4127
  {
 
4128
    table_map real_table_bit= s->table->map;
 
4129
    if (idx)
 
4130
    {
 
4131
      partial_pos= join->getPosFromPartialPlan(idx - 1);
 
4132
    }
 
4133
    if ((remaining_tables & real_table_bit) &&
 
4134
        ! (remaining_tables & s->dependent) &&
 
4135
        (! idx || ! check_interleaving_with_nj(partial_pos.getJoinTable(), s)))
 
4136
    {
 
4137
      double current_record_count, current_read_time;
 
4138
 
 
4139
      /*
 
4140
        psergey-insideout-todo:
 
4141
          when best_access_path() detects it could do an InsideOut scan or
 
4142
          some other scan, have it return an insideout scan and a flag that
 
4143
          requests to "fork" this loop iteration. (Q: how does that behave
 
4144
          when the depth is insufficient??)
 
4145
      */
 
4146
      /* Find the best access method from 's' to the current partial plan */
 
4147
      best_access_path(join, s, session, remaining_tables, idx,
 
4148
                       record_count, read_time);
 
4149
      /* Compute the cost of extending the plan with 's' */
 
4150
      partial_pos= join->getPosFromPartialPlan(idx);
 
4151
      current_record_count= record_count * partial_pos.getFanout();
 
4152
      current_read_time=    read_time + partial_pos.getCost();
 
4153
 
 
4154
      /* Expand only partial plans with lower cost than the best QEP so far */
 
4155
      if ((current_read_time +
 
4156
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
4157
      {
 
4158
        restore_prev_nj_state(s);
 
4159
        continue;
 
4160
      }
 
4161
 
 
4162
      /*
 
4163
        Prune some less promising partial plans. This heuristic may miss
 
4164
        the optimal QEPs, thus it results in a non-exhaustive search.
 
4165
      */
 
4166
      if (prune_level == 1)
 
4167
      {
 
4168
        if (best_record_count > current_record_count ||
 
4169
            best_read_time > current_read_time ||
 
4170
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
4171
        {
 
4172
          if (best_record_count >= current_record_count &&
 
4173
              best_read_time >= current_read_time &&
 
4174
              /* TODO: What is the reasoning behind this condition? */
 
4175
              (! (s->key_dependent & remaining_tables) ||
 
4176
               partial_pos.isConstTable()))
 
4177
          {
 
4178
            best_record_count= current_record_count;
 
4179
            best_read_time=    current_read_time;
 
4180
          }
 
4181
        }
 
4182
        else
 
4183
        {
 
4184
          restore_prev_nj_state(s);
 
4185
          continue;
 
4186
        }
 
4187
      }
 
4188
 
 
4189
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
4190
      { /* Recursively expand the current partial plan */
 
4191
        std::swap(join->best_ref[idx], *pos);
 
4192
        if (best_extension_by_limited_search(join,
 
4193
                                             remaining_tables & ~real_table_bit,
 
4194
                                             idx + 1,
 
4195
                                             current_record_count,
 
4196
                                             current_read_time,
 
4197
                                             search_depth - 1,
 
4198
                                             prune_level))
 
4199
          return(true);
 
4200
        std::swap(join->best_ref[idx], *pos);
 
4201
      }
 
4202
      else
 
4203
      { /*
 
4204
          'join' is either the best partial QEP with 'search_depth' relations,
 
4205
          or the best complete QEP so far, whichever is smaller.
 
4206
        */
 
4207
        partial_pos= join->getPosFromPartialPlan(join->const_tables);
 
4208
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
4209
        if (join->sort_by_table &&
 
4210
            partial_pos.hasTableForSorting(join->sort_by_table))
 
4211
          /* We have to make a temp table */
 
4212
          current_read_time+= current_record_count;
 
4213
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
4214
        {
 
4215
          join->copyPartialPlanIntoOptimalPlan(idx + 1);
 
4216
          join->best_read= current_read_time - 0.001;
 
4217
        }
 
4218
      }
 
4219
      restore_prev_nj_state(s);
 
4220
    }
 
4221
  }
 
4222
  return(false);
 
4223
}
 
4224
 
 
4225
/**
 
4226
  Heuristic procedure to automatically guess a reasonable degree of
 
4227
  exhaustiveness for the greedy search procedure.
 
4228
 
 
4229
  The procedure estimates the optimization time and selects a search depth
 
4230
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
4231
  find. If the number of tables in the query exceeds some constant, then
 
4232
  search_depth is set to this constant.
 
4233
 
 
4234
  @param join   pointer to the structure providing all context info for
 
4235
                the query
 
4236
 
 
4237
  @note
 
4238
    This is an extremely simplistic implementation that serves as a stub for a
 
4239
    more advanced analysis of the join. Ideally the search depth should be
 
4240
    determined by learning from previous query optimizations, because it will
 
4241
    depend on the CPU power (and other factors).
 
4242
 
 
4243
  @todo
 
4244
    this value should be determined dynamically, based on statistics:
 
4245
    uint32_t max_tables_for_exhaustive_opt= 7;
 
4246
 
 
4247
  @todo
 
4248
    this value could be determined by some mapping of the form:
 
4249
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
4250
 
 
4251
  @return
 
4252
    A positive integer that specifies the search depth (and thus the
 
4253
    exhaustiveness) of the depth-first search algorithm used by
 
4254
    'greedy_search'.
 
4255
*/
 
4256
static uint32_t determine_search_depth(JOIN *join)
 
4257
{
 
4258
  uint32_t table_count=  join->tables - join->const_tables;
 
4259
  uint32_t search_depth;
 
4260
  /* TODO: this value should be determined dynamically, based on statistics: */
 
4261
  uint32_t max_tables_for_exhaustive_opt= 7;
 
4262
 
 
4263
  if (table_count <= max_tables_for_exhaustive_opt)
 
4264
    search_depth= table_count+1; // use exhaustive for small number of tables
 
4265
  else
 
4266
    /*
 
4267
      TODO: this value could be determined by some mapping of the form:
 
4268
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
4269
    */
 
4270
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
4271
 
 
4272
  return search_depth;
 
4273
}
 
4274
 
 
4275
static bool make_simple_join(JOIN *join,Table *tmp_table)
 
4276
{
 
4277
  Table **tableptr;
 
4278
  JoinTable *join_tab;
 
4279
 
 
4280
  /*
 
4281
    Reuse Table * and JoinTable if already allocated by a previous call
 
4282
    to this function through JOIN::exec (may happen for sub-queries).
 
4283
  */
 
4284
  if (!join->table_reexec)
 
4285
  {
 
4286
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
4287
      return(true);
 
4288
    if (join->tmp_join)
 
4289
      join->tmp_join->table_reexec= join->table_reexec;
 
4290
  }
 
4291
  if (!join->join_tab_reexec)
 
4292
  {
 
4293
    if (!(join->join_tab_reexec=
 
4294
          (JoinTable*) join->session->alloc(sizeof(JoinTable))))
 
4295
      return(true);
 
4296
    if (join->tmp_join)
 
4297
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
4298
  }
 
4299
  tableptr= join->table_reexec;
 
4300
  join_tab= join->join_tab_reexec;
 
4301
 
 
4302
  join->join_tab=join_tab;
 
4303
  join->table=tableptr; tableptr[0]=tmp_table;
 
4304
  join->tables=1;
 
4305
  join->const_tables=0;
 
4306
  join->const_table_map=0;
 
4307
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
4308
    join->tmp_table_param.func_count=0;
 
4309
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
4310
  join->first_record=join->sort_and_group=0;
 
4311
  join->send_records=(ha_rows) 0;
 
4312
  join->group=0;
 
4313
  join->row_limit=join->unit->select_limit_cnt;
 
4314
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
4315
 
 
4316
  join_tab->cache.buff=0;                       /* No caching */
 
4317
  join_tab->table=tmp_table;
 
4318
  join_tab->select=0;
 
4319
  join_tab->select_cond=0;
 
4320
  join_tab->quick=0;
 
4321
  join_tab->type= AM_ALL;                       /* Map through all records */
 
4322
  join_tab->keys.set();                     /* test everything in quick */
 
4323
  join_tab->info=0;
 
4324
  join_tab->on_expr_ref=0;
 
4325
  join_tab->last_inner= 0;
 
4326
  join_tab->first_unmatched= 0;
 
4327
  join_tab->ref.key = -1;
 
4328
  join_tab->not_used_in_distinct=0;
 
4329
  join_tab->read_first_record= join_init_read_record;
 
4330
  join_tab->join=join;
 
4331
  join_tab->ref.key_parts= 0;
 
4332
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
4333
  tmp_table->status=0;
 
4334
  tmp_table->null_row=0;
 
4335
  return(false);
 
4336
}
 
4337
 
 
4338
/**
 
4339
  Fill in outer join related info for the execution plan structure.
 
4340
 
 
4341
    For each outer join operation left after simplification of the
 
4342
    original query the function set up the following pointers in the linear
 
4343
    structure join->join_tab representing the selected execution plan.
 
4344
    The first inner table t0 for the operation is set to refer to the last
 
4345
    inner table tk through the field t0->last_inner.
 
4346
    Any inner table ti for the operation are set to refer to the first
 
4347
    inner table ti->first_inner.
 
4348
    The first inner table t0 for the operation is set to refer to the
 
4349
    first inner table of the embedding outer join operation, if there is any,
 
4350
    through the field t0->first_upper.
 
4351
    The on expression for the outer join operation is attached to the
 
4352
    corresponding first inner table through the field t0->on_expr_ref.
 
4353
    Here ti are structures of the JoinTable type.
 
4354
 
 
4355
  EXAMPLE. For the query:
 
4356
  @code
 
4357
        SELECT * FROM t1
 
4358
                      LEFT JOIN
 
4359
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
4360
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
4361
          WHERE t1.c > 5,
 
4362
  @endcode
 
4363
 
 
4364
    given the execution plan with the table order t1,t2,t3,t4
 
4365
    is selected, the following references will be set;
 
4366
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
4367
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
4368
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
 
4369
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
4370
 
 
4371
  @param join   reference to the info fully describing the query
 
4372
 
 
4373
  @note
 
4374
    The function assumes that the simplification procedure has been
 
4375
    already applied to the join query (see simplify_joins).
 
4376
    This function can be called only after the execution plan
 
4377
    has been chosen.
 
4378
*/
 
4379
static void make_outerjoin_info(JOIN *join)
 
4380
{
 
4381
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
4382
  {
 
4383
    JoinTable *tab=join->join_tab+i;
 
4384
    Table *table=tab->table;
 
4385
    TableList *tbl= table->pos_in_table_list;
 
4386
    TableList *embedding= tbl->embedding;
 
4387
 
 
4388
    if (tbl->outer_join)
 
4389
    {
 
4390
      /*
 
4391
        Table tab is the only one inner table for outer join.
 
4392
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
4393
        is in the query above.)
 
4394
      */
 
4395
      tab->last_inner= tab->first_inner= tab;
 
4396
      tab->on_expr_ref= &tbl->on_expr;
 
4397
      tab->cond_equal= tbl->cond_equal;
 
4398
      if (embedding)
 
4399
        tab->first_upper= embedding->nested_join->first_nested;
 
4400
    }
 
4401
    for ( ; embedding ; embedding= embedding->embedding)
 
4402
    {
 
4403
      /* Ignore sj-nests: */
 
4404
      if (!embedding->on_expr)
 
4405
        continue;
 
4406
      nested_join_st *nested_join= embedding->nested_join;
 
4407
      if (!nested_join->counter_)
 
4408
      {
 
4409
        /*
 
4410
          Table tab is the first inner table for nested_join.
 
4411
          Save reference to it in the nested join structure.
 
4412
        */
 
4413
        nested_join->first_nested= tab;
 
4414
        tab->on_expr_ref= &embedding->on_expr;
 
4415
        tab->cond_equal= tbl->cond_equal;
 
4416
        if (embedding->embedding)
 
4417
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
4418
      }
 
4419
      if (!tab->first_inner)
 
4420
        tab->first_inner= nested_join->first_nested;
 
4421
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
4422
        break;
 
4423
      /* Table tab is the last inner table for nested join. */
 
4424
      nested_join->first_nested->last_inner= tab;
 
4425
    }
 
4426
  }
 
4427
  return;
 
4428
}
 
4429
 
 
4430
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
4431
{
 
4432
  Session *session= join->session;
 
4433
  optimizer::Position cur_pos;
 
4434
  if (select)
 
4435
  {
 
4436
    add_not_null_conds(join);
 
4437
    table_map used_tables;
 
4438
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
4439
    {                        /* there may be a select without a cond. */
 
4440
      if (join->tables > 1)
 
4441
        cond->update_used_tables();             // Tablenr may have changed
 
4442
      if (join->const_tables == join->tables &&
 
4443
          session->lex->current_select->master_unit() ==
 
4444
          &session->lex->unit)          // not upper level SELECT
 
4445
        join->const_table_map|=RAND_TABLE_BIT;
 
4446
      {                                         // Check const tables
 
4447
        COND *const_cond=
 
4448
          make_cond_for_table(cond,
 
4449
              join->const_table_map,
 
4450
              (table_map) 0, 1);
 
4451
        for (JoinTable *tab= join->join_tab+join->const_tables;
 
4452
            tab < join->join_tab+join->tables ; tab++)
 
4453
        {
 
4454
          if (*tab->on_expr_ref)
 
4455
          {
 
4456
            JoinTable *cond_tab= tab->first_inner;
 
4457
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
4458
                join->const_table_map,
 
4459
                (  table_map) 0, 0);
 
4460
            if (!tmp)
 
4461
              continue;
 
4462
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
4463
            if (! tmp)
 
4464
              return 1;
 
4465
            tmp->quick_fix_field();
 
4466
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
4467
              new Item_cond_and(cond_tab->select_cond,
 
4468
                  tmp);
 
4469
            if (! cond_tab->select_cond)
 
4470
              return 1;
 
4471
            cond_tab->select_cond->quick_fix_field();
 
4472
          }
 
4473
        }
 
4474
        if (const_cond && ! const_cond->val_int())
 
4475
        {
 
4476
          return 1;      // Impossible const condition
 
4477
        }
 
4478
      }
 
4479
    }
 
4480
    used_tables=((select->const_tables=join->const_table_map) |
 
4481
        OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
4482
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
4483
    {
 
4484
      JoinTable *tab=join->join_tab+i;
 
4485
      /*
 
4486
         first_inner is the X in queries like:
 
4487
         SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
4488
       */
 
4489
      JoinTable *first_inner_tab= tab->first_inner;
 
4490
      table_map current_map= tab->table->map;
 
4491
      bool use_quick_range=0;
 
4492
      COND *tmp;
 
4493
 
 
4494
      /*
 
4495
         Following force including random expression in last table condition.
 
4496
         It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
4497
       */
 
4498
      if (i == join->tables-1)
 
4499
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
4500
      used_tables|=current_map;
 
4501
 
 
4502
      if (tab->type == AM_REF && tab->quick &&
 
4503
          (uint32_t) tab->ref.key == tab->quick->index &&
 
4504
          tab->ref.key_length < tab->quick->max_used_key_length)
 
4505
      {
 
4506
        /* Range uses longer key;  Use this instead of ref on key */
 
4507
        tab->type= AM_ALL;
 
4508
        use_quick_range= 1;
 
4509
        tab->use_quick= 1;
 
4510
        tab->ref.key= -1;
 
4511
        tab->ref.key_parts= 0;          // Don't use ref key.
 
4512
        cur_pos= join->getPosFromOptimalPlan(i);
 
4513
        cur_pos.setFanout(rows2double(tab->quick->records));
 
4514
        /*
 
4515
           We will use join cache here : prevent sorting of the first
 
4516
           table only and sort at the end.
 
4517
         */
 
4518
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
4519
          join->full_join= 1;
 
4520
      }
 
4521
 
 
4522
      tmp= NULL;
 
4523
      if (cond)
 
4524
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
4525
      if (cond && !tmp && tab->quick)
 
4526
      {                                         // Outer join
 
4527
        if (tab->type != AM_ALL)
 
4528
        {
 
4529
          /*
 
4530
             Don't use the quick method
 
4531
             We come here in the case where we have 'key=constant' and
 
4532
             the test is removed by make_cond_for_table()
 
4533
           */
 
4534
          delete tab->quick;
 
4535
          tab->quick= 0;
 
4536
        }
 
4537
        else
 
4538
        {
 
4539
          /*
 
4540
             Hack to handle the case where we only refer to a table
 
4541
             in the ON part of an OUTER JOIN. In this case we want the code
 
4542
             below to check if we should use 'quick' instead.
 
4543
           */
 
4544
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
4545
        }
 
4546
 
 
4547
      }
 
4548
      if (tmp || !cond || tab->type == AM_REF || tab->type == AM_REF_OR_NULL ||
 
4549
          tab->type == AM_EQ_REF)
 
4550
      {
 
4551
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
4552
            session->memdup((unsigned char*) select,
 
4553
              sizeof(*select)));
 
4554
        if (! sel)
 
4555
          return 1;                     // End of memory
 
4556
        /*
 
4557
           If tab is an inner table of an outer join operation,
 
4558
           add a match guard to the pushed down predicate.
 
4559
           The guard will turn the predicate on only after
 
4560
           the first match for outer tables is encountered.
 
4561
         */
 
4562
        if (cond && tmp)
 
4563
        {
 
4564
          /*
 
4565
             Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
4566
             a cond, so neutralize the hack above.
 
4567
           */
 
4568
          if (! (tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
4569
            return 1;
 
4570
          tab->select_cond=sel->cond=tmp;
 
4571
        }
 
4572
        else
 
4573
          tab->select_cond= sel->cond= NULL;
 
4574
 
 
4575
        sel->head=tab->table;
 
4576
        if (tab->quick)
 
4577
        {
 
4578
          /* Use quick key read if it's a constant and it's not used
 
4579
             with key reading */
 
4580
          if (tab->needed_reg.none() && tab->type != AM_EQ_REF
 
4581
              && (tab->type != AM_REF || (uint32_t) tab->ref.key == tab->quick->index))
 
4582
          {
 
4583
            sel->quick=tab->quick;              // Use value from get_quick_...
 
4584
            sel->quick_keys.reset();
 
4585
            sel->needed_reg.reset();
 
4586
          }
 
4587
          else
 
4588
          {
 
4589
            delete tab->quick;
 
4590
          }
 
4591
          tab->quick= 0;
 
4592
        }
 
4593
        uint32_t ref_key= static_cast<uint32_t>(sel->head->reginfo.join_tab->ref.key + 1);
 
4594
        if (i == join->const_tables && ref_key)
 
4595
        {
 
4596
          if (tab->const_keys.any() &&
 
4597
              tab->table->reginfo.impossible_range)
 
4598
            return 1;
 
4599
        }
 
4600
        else if (tab->type == AM_ALL && ! use_quick_range)
 
4601
        {
 
4602
          if (tab->const_keys.any() &&
 
4603
              tab->table->reginfo.impossible_range)
 
4604
            return 1;                           // Impossible range
 
4605
          /*
 
4606
             We plan to scan all rows.
 
4607
             Check again if we should use an index.
 
4608
             We could have used an column from a previous table in
 
4609
             the index if we are using limit and this is the first table
 
4610
           */
 
4611
 
 
4612
          cur_pos= join->getPosFromOptimalPlan(i);
 
4613
          if ((cond && (! ((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
 
4614
              (! tab->const_keys.none() && (i == join->const_tables) &&
 
4615
              (join->unit->select_limit_cnt < cur_pos.getFanout()) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
 
4616
          {
 
4617
            /* Join with outer join condition */
 
4618
            COND *orig_cond= sel->cond;
 
4619
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
4620
 
 
4621
            /*
 
4622
               We can't call sel->cond->fix_fields,
 
4623
               as it will break tab->on_expr if it's AND condition
 
4624
               (fix_fields currently removes extra AND/OR levels).
 
4625
               Yet attributes of the just built condition are not needed.
 
4626
               Thus we call sel->cond->quick_fix_field for safety.
 
4627
             */
 
4628
            if (sel->cond && ! sel->cond->fixed)
 
4629
              sel->cond->quick_fix_field();
 
4630
 
 
4631
            if (sel->test_quick_select(session, tab->keys,
 
4632
                  used_tables & ~ current_map,
 
4633
                  (join->select_options &
 
4634
                   OPTION_FOUND_ROWS ?
 
4635
                   HA_POS_ERROR :
 
4636
                   join->unit->select_limit_cnt), 0,
 
4637
                  false) < 0)
 
4638
            {
 
4639
              /*
 
4640
                 Before reporting "Impossible WHERE" for the whole query
 
4641
                 we have to check isn't it only "impossible ON" instead
 
4642
               */
 
4643
              sel->cond=orig_cond;
 
4644
              if (! *tab->on_expr_ref ||
 
4645
                  sel->test_quick_select(session, tab->keys,
 
4646
                    used_tables & ~ current_map,
 
4647
                    (join->select_options &
 
4648
                     OPTION_FOUND_ROWS ?
 
4649
                     HA_POS_ERROR :
 
4650
                     join->unit->select_limit_cnt),0,
 
4651
                    false) < 0)
 
4652
                return 1;                       // Impossible WHERE
 
4653
            }
 
4654
            else
 
4655
              sel->cond=orig_cond;
 
4656
 
 
4657
            /* Fix for EXPLAIN */
 
4658
            if (sel->quick)
 
4659
            {
 
4660
              cur_pos= join->getPosFromOptimalPlan(i);
 
4661
              cur_pos.setFanout(static_cast<double>(sel->quick->records));
 
4662
            }
 
4663
          }
 
4664
          else
 
4665
          {
 
4666
            sel->needed_reg= tab->needed_reg;
 
4667
            sel->quick_keys.reset();
 
4668
          }
 
4669
          if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
 
4670
              !((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
 
4671
          {
 
4672
            tab->keys= sel->quick_keys;
 
4673
            tab->keys|= sel->needed_reg;
 
4674
            tab->use_quick= (!sel->needed_reg.none() &&
 
4675
                (select->quick_keys.none() ||
 
4676
                 (select->quick &&
 
4677
                  (select->quick->records >= 100L)))) ?
 
4678
              2 : 1;
 
4679
            sel->read_tables= used_tables & ~current_map;
 
4680
          }
 
4681
          if (i != join->const_tables && tab->use_quick != 2)
 
4682
          {                                     /* Read with cache */
 
4683
            if (cond &&
 
4684
                (tmp=make_cond_for_table(cond,
 
4685
                                         join->const_table_map |
 
4686
                                         current_map,
 
4687
                                         current_map, 0)))
 
4688
            {
 
4689
              tab->cache.select= (SQL_SELECT*)
 
4690
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
4691
              tab->cache.select->cond= tmp;
 
4692
              tab->cache.select->read_tables= join->const_table_map;
 
4693
            }
 
4694
          }
 
4695
        }
 
4696
      }
 
4697
 
 
4698
      /*
 
4699
         Push down conditions from all on expressions.
 
4700
         Each of these conditions are guarded by a variable
 
4701
         that turns if off just before null complemented row for
 
4702
         outer joins is formed. Thus, the condition from an
 
4703
         'on expression' are guaranteed not to be checked for
 
4704
         the null complemented row.
 
4705
       */
 
4706
 
 
4707
      /* First push down constant conditions from on expressions */
 
4708
      for (JoinTable *join_tab= join->join_tab+join->const_tables;
 
4709
          join_tab < join->join_tab+join->tables ; join_tab++)
 
4710
      {
 
4711
        if (*join_tab->on_expr_ref)
 
4712
        {
 
4713
          JoinTable *cond_tab= join_tab->first_inner;
 
4714
          tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
4715
              join->const_table_map,
 
4716
              (table_map) 0, 0);
 
4717
          if (!tmp)
 
4718
            continue;
 
4719
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
4720
          if (! tmp)
 
4721
            return 1;
 
4722
          tmp->quick_fix_field();
 
4723
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
4724
            new Item_cond_and(cond_tab->select_cond,tmp);
 
4725
          if (! cond_tab->select_cond)
 
4726
            return 1;
 
4727
          cond_tab->select_cond->quick_fix_field();
 
4728
        }
 
4729
      }
 
4730
 
 
4731
      /* Push down non-constant conditions from on expressions */
 
4732
      JoinTable *last_tab= tab;
 
4733
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
4734
      {
 
4735
        /*
 
4736
           Table tab is the last inner table of an outer join.
 
4737
           An on expression is always attached to it.
 
4738
         */
 
4739
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
4740
 
 
4741
        table_map used_tables2= (join->const_table_map |
 
4742
            OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
4743
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
4744
        {
 
4745
          current_map= tab->table->map;
 
4746
          used_tables2|= current_map;
 
4747
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
4748
              current_map, 0);
 
4749
          if (tmp_cond)
 
4750
          {
 
4751
            JoinTable *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
4752
            /*
 
4753
               First add the guards for match variables of
 
4754
               all embedding outer join operations.
 
4755
             */
 
4756
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
4757
                                                      tmp_cond,
 
4758
                                                      first_inner_tab)))
 
4759
              return 1;
 
4760
            /*
 
4761
               Now add the guard turning the predicate off for
 
4762
               the null complemented row.
 
4763
             */
 
4764
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
4765
                &first_inner_tab->
 
4766
                not_null_compl);
 
4767
            if (tmp_cond)
 
4768
              tmp_cond->quick_fix_field();
 
4769
            /* Add the predicate to other pushed down predicates */
 
4770
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
4771
              new Item_cond_and(cond_tab->select_cond,
 
4772
                                tmp_cond);
 
4773
            if (! cond_tab->select_cond)
 
4774
              return 1;
 
4775
            cond_tab->select_cond->quick_fix_field();
 
4776
          }
 
4777
        }
 
4778
        first_inner_tab= first_inner_tab->first_upper;
 
4779
      }
 
4780
    }
 
4781
  }
 
4782
  return(0);
 
4783
}
 
4784
 
 
4785
/*
 
4786
  Plan refinement stage: do various set ups for the executioner
 
4787
 
 
4788
  SYNOPSIS
 
4789
    make_join_readinfo()
 
4790
      join           Join being processed
 
4791
      options        Join's options (checking for SELECT_DESCRIBE,
 
4792
                     SELECT_NO_JOIN_CACHE)
 
4793
      no_jbuf_after  Don't use join buffering after table with this number.
 
4794
 
 
4795
  DESCRIPTION
 
4796
    Plan refinement stage: do various set ups for the executioner
 
4797
      - set up use of join buffering
 
4798
      - push index conditions
 
4799
      - increment counters
 
4800
      - etc
 
4801
 
 
4802
  RETURN
 
4803
    false - OK
 
4804
    true  - Out of memory
 
4805
*/
 
4806
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
4807
{
 
4808
  uint32_t i;
 
4809
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
4810
  bool sorted= 1;
 
4811
 
 
4812
  for (i=join->const_tables ; i < join->tables ; i++)
 
4813
  {
 
4814
    JoinTable *tab=join->join_tab+i;
 
4815
    Table *table=tab->table;
 
4816
    bool using_join_cache;
 
4817
    tab->read_record.table= table;
 
4818
    tab->read_record.file=table->file;
 
4819
    tab->next_select=sub_select;                /* normal select */
 
4820
    /*
 
4821
      TODO: don't always instruct first table's ref/range access method to
 
4822
      produce sorted output.
 
4823
    */
 
4824
    tab->sorted= sorted;
 
4825
    sorted= 0;                                  // only first must be sorted
 
4826
    if (tab->insideout_match_tab)
 
4827
    {
 
4828
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
 
4829
                                                         [tab->index].
 
4830
                                                         key_length)))
 
4831
        return true;
 
4832
    }
 
4833
    switch (tab->type) {
 
4834
    case AM_SYSTEM:                             // Only happens with left join
 
4835
      table->status=STATUS_NO_RECORD;
 
4836
      tab->read_first_record= join_read_system;
 
4837
      tab->read_record.read_record= join_no_more_records;
 
4838
      break;
 
4839
    case AM_CONST:                              // Only happens with left join
 
4840
      table->status=STATUS_NO_RECORD;
 
4841
      tab->read_first_record= join_read_const;
 
4842
      tab->read_record.read_record= join_no_more_records;
 
4843
      if (table->covering_keys.test(tab->ref.key) &&
 
4844
          !table->no_keyread)
 
4845
      {
 
4846
        table->key_read=1;
 
4847
        table->file->extra(HA_EXTRA_KEYREAD);
 
4848
      }
 
4849
      break;
 
4850
    case AM_EQ_REF:
 
4851
      table->status=STATUS_NO_RECORD;
 
4852
      if (tab->select)
 
4853
      {
 
4854
        delete tab->select->quick;
 
4855
        tab->select->quick=0;
 
4856
      }
 
4857
      delete tab->quick;
 
4858
      tab->quick=0;
 
4859
      tab->read_first_record= join_read_key;
 
4860
      tab->read_record.read_record= join_no_more_records;
 
4861
      if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
 
4862
      {
 
4863
        table->key_read=1;
 
4864
        table->file->extra(HA_EXTRA_KEYREAD);
 
4865
      }
 
4866
      break;
 
4867
    case AM_REF_OR_NULL:
 
4868
    case AM_REF:
 
4869
      table->status=STATUS_NO_RECORD;
 
4870
      if (tab->select)
 
4871
      {
 
4872
        delete tab->select->quick;
 
4873
        tab->select->quick=0;
 
4874
      }
 
4875
      delete tab->quick;
 
4876
      tab->quick=0;
 
4877
      if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
 
4878
      {
 
4879
        table->key_read=1;
 
4880
        table->file->extra(HA_EXTRA_KEYREAD);
 
4881
      }
 
4882
      if (tab->type == AM_REF)
 
4883
      {
 
4884
        tab->read_first_record= join_read_always_key;
 
4885
        tab->read_record.read_record= tab->insideout_match_tab?
 
4886
           join_read_next_same_diff : join_read_next_same;
 
4887
      }
 
4888
      else
 
4889
      {
 
4890
        tab->read_first_record= join_read_always_key_or_null;
 
4891
        tab->read_record.read_record= join_read_next_same_or_null;
 
4892
      }
 
4893
      break;
 
4894
    case AM_ALL:
 
4895
      /*
 
4896
        If previous table use cache
 
4897
        If the incoming data set is already sorted don't use cache.
 
4898
      */
 
4899
      table->status=STATUS_NO_RECORD;
 
4900
      using_join_cache= false;
 
4901
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
4902
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
4903
          !tab->insideout_match_tab)
 
4904
      {
 
4905
        if ((options & SELECT_DESCRIBE) ||
 
4906
            !join_init_cache(join->session,join->join_tab+join->const_tables,
 
4907
                i-join->const_tables))
 
4908
        {
 
4909
                using_join_cache= true;
 
4910
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
4911
        }
 
4912
      }
 
4913
      /* These init changes read_record */
 
4914
      if (tab->use_quick == 2)
 
4915
      {
 
4916
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
4917
        tab->read_first_record= join_init_quick_read_record;
 
4918
        if (statistics)
 
4919
          status_var_increment(join->session->status_var.select_range_check_count);
 
4920
      }
 
4921
      else
 
4922
      {
 
4923
        tab->read_first_record= join_init_read_record;
 
4924
        if (i == join->const_tables)
 
4925
        {
 
4926
          if (tab->select && tab->select->quick)
 
4927
          {
 
4928
            if (statistics)
 
4929
              status_var_increment(join->session->status_var.select_range_count);
 
4930
          }
 
4931
          else
 
4932
          {
 
4933
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
4934
            if (statistics)
 
4935
              status_var_increment(join->session->status_var.select_scan_count);
 
4936
          }
 
4937
        }
 
4938
        else
 
4939
        {
 
4940
          if (tab->select && tab->select->quick)
 
4941
          {
 
4942
            if (statistics)
 
4943
              status_var_increment(join->session->status_var.select_full_range_join_count);
 
4944
          }
 
4945
          else
 
4946
          {
 
4947
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
4948
            if (statistics)
 
4949
              status_var_increment(join->session->status_var.select_full_join_count);
 
4950
          }
 
4951
        }
 
4952
        if (!table->no_keyread)
 
4953
        {
 
4954
          if (tab->select && tab->select->quick &&
 
4955
                    tab->select->quick->index != MAX_KEY && //not index_merge
 
4956
              table->covering_keys.test(tab->select->quick->index))
 
4957
          {
 
4958
            table->key_read=1;
 
4959
            table->file->extra(HA_EXTRA_KEYREAD);
 
4960
          }
 
4961
          else if (!table->covering_keys.none() &&
 
4962
            !(tab->select && tab->select->quick))
 
4963
          {                                     // Only read index tree
 
4964
                  if (!tab->insideout_match_tab)
 
4965
                  {
 
4966
                    /*
 
4967
                      See bug #26447: "Using the clustered index for a table scan
 
4968
                      is always faster than using a secondary index".
 
4969
                    */
 
4970
                    if (table->s->primary_key != MAX_KEY &&
 
4971
                        table->file->primary_key_is_clustered())
 
4972
                      tab->index= table->s->primary_key;
 
4973
                    else
 
4974
                      tab->index= table->find_shortest_key(&table->covering_keys);
 
4975
                  }
 
4976
            tab->read_first_record= join_read_first;
 
4977
            tab->type= AM_NEXT;         // Read with index_first / index_next
 
4978
          }
 
4979
        }
 
4980
      }
 
4981
      break;
 
4982
    default:
 
4983
      break;
 
4984
    case AM_UNKNOWN:
 
4985
    case AM_MAYBE_REF:
 
4986
      abort();
 
4987
    }
 
4988
  }
 
4989
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
4990
  return(false);
 
4991
}
 
4992
 
 
4993
/** Update the dependency map for the tables. */
 
4994
static void update_depend_map(JOIN *join)
 
4995
{
 
4996
  JoinTable *join_tab=join->join_tab, *end=join_tab+join->tables;
 
4997
 
 
4998
  for (; join_tab != end ; join_tab++)
 
4999
  {
 
5000
    table_reference_st *ref= &join_tab->ref;
 
5001
    table_map depend_map= 0;
 
5002
    Item **item=ref->items;
 
5003
    uint32_t i;
 
5004
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
5005
      depend_map|=(*item)->used_tables();
 
5006
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
5007
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
5008
    for (JoinTable **tab=join->map2table; depend_map; tab++,depend_map>>=1 )
 
5009
    {
 
5010
      if (depend_map & 1)
 
5011
        ref->depend_map|=(*tab)->ref.depend_map;
 
5012
    }
 
5013
  }
 
5014
}
 
5015
 
 
5016
/** Update the dependency map for the sort order. */
 
5017
static void update_depend_map(JOIN *join, order_st *order)
 
5018
{
 
5019
  for (; order ; order=order->next)
 
5020
  {
 
5021
    table_map depend_map;
 
5022
    order->item[0]->update_used_tables();
 
5023
    order->depend_map=depend_map=order->item[0]->used_tables();
 
5024
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
5025
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
5026
        && !order->item[0]->with_sum_func)
 
5027
    {
 
5028
      for (JoinTable **tab=join->map2table; depend_map; tab++, depend_map>>=1)
 
5029
      {
 
5030
        if (depend_map & 1)
 
5031
          order->depend_map|=(*tab)->ref.depend_map;
 
5032
      }
 
5033
    }
 
5034
  }
 
5035
}
 
5036
 
 
5037
/**
 
5038
  Remove all constants and check if order_st only contains simple
 
5039
  expressions.
 
5040
 
 
5041
  simple_order is set to 1 if sort_order only uses fields from head table
 
5042
  and the head table is not a LEFT JOIN table.
 
5043
 
 
5044
  @param join                   Join handler
 
5045
  @param first_order            List of SORT or GROUP order
 
5046
  @param cond                   WHERE statement
 
5047
  @param change_list            Set to 1 if we should remove things from list.
 
5048
                               If this is not set, then only simple_order is
 
5049
                               calculated.
 
5050
  @param simple_order           Set to 1 if we are only using simple expressions
 
5051
 
 
5052
  @return
 
5053
    Returns new sort order
 
5054
*/
 
5055
static order_st *remove_constants(JOIN *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
 
5056
{
 
5057
  if (join->tables == join->const_tables)
 
5058
    return change_list ? 0 : first_order;               // No need to sort
 
5059
 
 
5060
  order_st *order,**prev_ptr;
 
5061
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
5062
  table_map not_const_tables= ~join->const_table_map;
 
5063
  table_map ref;
 
5064
 
 
5065
  prev_ptr= &first_order;
 
5066
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
5067
 
 
5068
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
5069
 
 
5070
  update_depend_map(join, first_order);
 
5071
  for (order=first_order; order ; order=order->next)
 
5072
  {
 
5073
    table_map order_tables=order->item[0]->used_tables();
 
5074
    if (order->item[0]->with_sum_func)
 
5075
      *simple_order=0;                          // Must do a temp table to sort
 
5076
    else if (!(order_tables & not_const_tables))
 
5077
    {
 
5078
      if (order->item[0]->with_subselect)
 
5079
        order->item[0]->val_str(&order->item[0]->str_value);
 
5080
      continue;                                 // skip const item
 
5081
    }
 
5082
    else
 
5083
    {
 
5084
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
5085
        *simple_order=0;
 
5086
      else
 
5087
      {
 
5088
        Item *comp_item=0;
 
5089
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
5090
        {
 
5091
          continue;
 
5092
        }
 
5093
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
5094
        {
 
5095
          if (!(order_tables & first_table) &&
 
5096
                    only_eq_ref_tables(join,first_order, ref))
 
5097
          {
 
5098
            continue;
 
5099
          }
 
5100
          *simple_order=0;                      // Must do a temp table to sort
 
5101
        }
 
5102
      }
 
5103
    }
 
5104
    if (change_list)
 
5105
      *prev_ptr= order;                         // use this entry
 
5106
    prev_ptr= &order->next;
 
5107
  }
 
5108
  if (change_list)
 
5109
    *prev_ptr=0;
 
5110
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
5111
    *simple_order=1;
 
5112
  return(first_order);
 
5113
}
 
5114
 
 
5115
static int return_zero_rows(JOIN *join,
 
5116
                            select_result *result,
 
5117
                            TableList *tables,
 
5118
                                        List<Item> &fields,
 
5119
                            bool send_row,
 
5120
                            uint64_t select_options,
 
5121
                            const char *info,
 
5122
                            Item *having)
 
5123
{
 
5124
  if (select_options & SELECT_DESCRIBE)
 
5125
  {
 
5126
    select_describe(join, false, false, false, info);
 
5127
    return(0);
 
5128
  }
 
5129
 
 
5130
  join->join_free();
 
5131
 
 
5132
  if (send_row)
 
5133
  {
 
5134
    for (TableList *table= tables; table; table= table->next_leaf)
 
5135
      table->table->mark_as_null_row();         // All fields are NULL
 
5136
    if (having && having->val_int() == 0)
 
5137
      send_row=0;
 
5138
  }
 
5139
  if (! (result->send_fields(fields)))
 
5140
  {
 
5141
    if (send_row)
 
5142
    {
 
5143
      List_iterator_fast<Item> it(fields);
 
5144
      Item *item;
 
5145
      while ((item= it++))
 
5146
        item->no_rows_in_result();
 
5147
      result->send_data(fields);
 
5148
    }
 
5149
    result->send_eof();                         // Should be safe
 
5150
  }
 
5151
  /* Update results for FOUND_ROWS */
 
5152
  join->session->limit_found_rows= join->session->examined_row_count= 0;
 
5153
  return(0);
 
5154
}
 
5155
 
 
5156
/**
 
5157
  Simplify joins replacing outer joins by inner joins whenever it's
 
5158
  possible.
 
5159
 
 
5160
    The function, during a retrieval of join_list,  eliminates those
 
5161
    outer joins that can be converted into inner join, possibly nested.
 
5162
    It also moves the on expressions for the converted outer joins
 
5163
    and from inner joins to conds.
 
5164
    The function also calculates some attributes for nested joins:
 
5165
    - used_tables
 
5166
    - not_null_tables
 
5167
    - dep_tables.
 
5168
    - on_expr_dep_tables
 
5169
    The first two attributes are used to test whether an outer join can
 
5170
    be substituted for an inner join. The third attribute represents the
 
5171
    relation 'to be dependent on' for tables. If table t2 is dependent
 
5172
    on table t1, then in any evaluated execution plan table access to
 
5173
    table t2 must precede access to table t2. This relation is used also
 
5174
    to check whether the query contains  invalid cross-references.
 
5175
    The forth attribute is an auxiliary one and is used to calculate
 
5176
    dep_tables.
 
5177
    As the attribute dep_tables qualifies possibles orders of tables in the
 
5178
    execution plan, the dependencies required by the straight join
 
5179
    modifiers are reflected in this attribute as well.
 
5180
    The function also removes all braces that can be removed from the join
 
5181
    expression without changing its meaning.
 
5182
 
 
5183
  @note
 
5184
    An outer join can be replaced by an inner join if the where condition
 
5185
    or the on expression for an embedding nested join contains a conjunctive
 
5186
    predicate rejecting null values for some attribute of the inner tables.
 
5187
 
 
5188
    E.g. in the query:
 
5189
    @code
 
5190
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
5191
    @endcode
 
5192
    the predicate t2.b < 5 rejects nulls.
 
5193
    The query is converted first to:
 
5194
    @code
 
5195
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
5196
    @endcode
 
5197
    then to the equivalent form:
 
5198
    @code
 
5199
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
5200
    @endcode
 
5201
 
 
5202
 
 
5203
    Similarly the following query:
 
5204
    @code
 
5205
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
5206
        WHERE t2.c < 5
 
5207
    @endcode
 
5208
    is converted to:
 
5209
    @code
 
5210
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
 
5211
 
 
5212
    @endcode
 
5213
 
 
5214
    One conversion might trigger another:
 
5215
    @code
 
5216
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
5217
                       LEFT JOIN t3 ON t3.b=t2.b
 
5218
        WHERE t3 IS NOT NULL =>
 
5219
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
5220
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
 
5221
      SELECT * FROM t1, t2, t3
 
5222
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
5223
  @endcode
 
5224
 
 
5225
    The function removes all unnecessary braces from the expression
 
5226
    produced by the conversions.
 
5227
    E.g.
 
5228
    @code
 
5229
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
5230
    @endcode
 
5231
    finally is converted to:
 
5232
    @code
 
5233
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
5234
 
 
5235
    @endcode
 
5236
 
 
5237
 
 
5238
    It also will remove braces from the following queries:
 
5239
    @code
 
5240
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
5241
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
5242
    @endcode
 
5243
 
 
5244
    The benefit of this simplification procedure is that it might return
 
5245
    a query for which the optimizer can evaluate execution plan with more
 
5246
    join orders. With a left join operation the optimizer does not
 
5247
    consider any plan where one of the inner tables is before some of outer
 
5248
    tables.
 
5249
 
 
5250
  IMPLEMENTATION
 
5251
    The function is implemented by a recursive procedure.  On the recursive
 
5252
    ascent all attributes are calculated, all outer joins that can be
 
5253
    converted are replaced and then all unnecessary braces are removed.
 
5254
    As join list contains join tables in the reverse order sequential
 
5255
    elimination of outer joins does not require extra recursive calls.
 
5256
 
 
5257
  SEMI-JOIN NOTES
 
5258
    Remove all semi-joins that have are within another semi-join (i.e. have
 
5259
    an "ancestor" semi-join nest)
 
5260
 
 
5261
  EXAMPLES
 
5262
    Here is an example of a join query with invalid cross references:
 
5263
    @code
 
5264
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
 
5265
    @endcode
 
5266
 
 
5267
  @param join        reference to the query info
 
5268
  @param join_list   list representation of the join to be converted
 
5269
  @param conds       conditions to add on expressions for converted joins
 
5270
  @param top         true <=> conds is the where condition
 
5271
 
 
5272
  @return
 
5273
    - The new condition, if success
 
5274
    - 0, otherwise
 
5275
*/
 
5276
static COND *simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top)
 
5277
{
 
5278
  TableList *table;
 
5279
  nested_join_st *nested_join;
 
5280
  TableList *prev_table= 0;
 
5281
  List_iterator<TableList> li(*join_list);
 
5282
 
 
5283
  /*
 
5284
    Try to simplify join operations from join_list.
 
5285
    The most outer join operation is checked for conversion first.
 
5286
  */
 
5287
  while ((table= li++))
 
5288
  {
 
5289
    table_map used_tables;
 
5290
    table_map not_null_tables= (table_map) 0;
 
5291
 
 
5292
    if ((nested_join= table->nested_join))
 
5293
    {
 
5294
      /*
 
5295
         If the element of join_list is a nested join apply
 
5296
         the procedure to its nested join list first.
 
5297
      */
 
5298
      if (table->on_expr)
 
5299
      {
 
5300
        Item *expr= table->on_expr;
 
5301
        /*
 
5302
           If an on expression E is attached to the table,
 
5303
           check all null rejected predicates in this expression.
 
5304
           If such a predicate over an attribute belonging to
 
5305
           an inner table of an embedded outer join is found,
 
5306
           the outer join is converted to an inner join and
 
5307
           the corresponding on expression is added to E.
 
5308
              */
 
5309
        expr= simplify_joins(join, &nested_join->join_list, expr, false);
 
5310
 
 
5311
        if (!table->prep_on_expr || expr != table->on_expr)
 
5312
        {
 
5313
          assert(expr);
 
5314
 
 
5315
          table->on_expr= expr;
 
5316
          table->prep_on_expr= expr->copy_andor_structure(join->session);
 
5317
        }
 
5318
      }
 
5319
      nested_join->used_tables= (table_map) 0;
 
5320
      nested_join->not_null_tables=(table_map) 0;
 
5321
      conds= simplify_joins(join, &nested_join->join_list, conds, top);
 
5322
      used_tables= nested_join->used_tables;
 
5323
      not_null_tables= nested_join->not_null_tables;
 
5324
    }
 
5325
    else
 
5326
    {
 
5327
      if (!table->prep_on_expr)
 
5328
        table->prep_on_expr= table->on_expr;
 
5329
      used_tables= table->table->map;
 
5330
      if (conds)
 
5331
        not_null_tables= conds->not_null_tables();
 
5332
    }
 
5333
 
 
5334
    if (table->embedding)
 
5335
    {
 
5336
      table->embedding->nested_join->used_tables|= used_tables;
 
5337
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
5338
    }
 
5339
 
 
5340
    if (!table->outer_join || (used_tables & not_null_tables))
 
5341
    {
 
5342
      /*
 
5343
        For some of the inner tables there are conjunctive predicates
 
5344
        that reject nulls => the outer join can be replaced by an inner join.
 
5345
      */
 
5346
      table->outer_join= 0;
 
5347
      if (table->on_expr)
 
5348
      {
 
5349
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
5350
        if (conds)
 
5351
        {
 
5352
          conds= and_conds(conds, table->on_expr);
 
5353
          conds->top_level_item();
 
5354
          /* conds is always a new item as both cond and on_expr existed */
 
5355
          assert(!conds->fixed);
 
5356
          conds->fix_fields(join->session, &conds);
 
5357
        }
 
5358
        else
 
5359
          conds= table->on_expr;
 
5360
        table->prep_on_expr= table->on_expr= 0;
 
5361
      }
 
5362
    }
 
5363
 
 
5364
    if (!top)
 
5365
      continue;
 
5366
 
 
5367
    /*
 
5368
      Only inner tables of non-convertible outer joins
 
5369
      remain with on_expr.
 
5370
    */
 
5371
    if (table->on_expr)
 
5372
    {
 
5373
      table->dep_tables|= table->on_expr->used_tables();
 
5374
      if (table->embedding)
 
5375
      {
 
5376
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
 
5377
        /*
 
5378
           Embedding table depends on tables used
 
5379
           in embedded on expressions.
 
5380
        */
 
5381
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
5382
      }
 
5383
      else
 
5384
        table->dep_tables&= ~table->table->map;
 
5385
    }
 
5386
 
 
5387
    if (prev_table)
 
5388
    {
 
5389
      /* The order of tables is reverse: prev_table follows table */
 
5390
      if (prev_table->straight)
 
5391
        prev_table->dep_tables|= used_tables;
 
5392
      if (prev_table->on_expr)
 
5393
      {
 
5394
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
5395
        table_map prev_used_tables= prev_table->nested_join ?
 
5396
                                    prev_table->nested_join->used_tables :
 
5397
                                    prev_table->table->map;
 
5398
        /*
 
5399
          If on expression contains only references to inner tables
 
5400
          we still make the inner tables dependent on the outer tables.
 
5401
          It would be enough to set dependency only on one outer table
 
5402
          for them. Yet this is really a rare case.
 
5403
              */
 
5404
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
5405
          prev_table->dep_tables|= used_tables;
 
5406
      }
 
5407
    }
 
5408
    prev_table= table;
 
5409
  }
 
5410
 
 
5411
  /*
 
5412
    Flatten nested joins that can be flattened.
 
5413
    no ON expression and not a semi-join => can be flattened.
 
5414
  */
 
5415
  li.rewind();
 
5416
  while ((table= li++))
 
5417
  {
 
5418
    nested_join= table->nested_join;
 
5419
    if (nested_join && !table->on_expr)
 
5420
    {
 
5421
      TableList *tbl;
 
5422
      List_iterator<TableList> it(nested_join->join_list);
 
5423
      while ((tbl= it++))
 
5424
      {
 
5425
        tbl->embedding= table->embedding;
 
5426
        tbl->join_list= table->join_list;
 
5427
      }
 
5428
      li.replace(nested_join->join_list);
 
5429
    }
 
5430
  }
 
5431
  return(conds);
 
5432
}
 
5433
 
 
5434
static int remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
5435
{
 
5436
  int error;
 
5437
  uint32_t reclength,offset;
 
5438
  uint32_t field_count;
 
5439
  Session *session= join->session;
 
5440
 
 
5441
  entry->reginfo.lock_type=TL_WRITE;
 
5442
 
 
5443
  /* Calculate how many saved fields there is in list */
 
5444
  field_count=0;
 
5445
  List_iterator<Item> it(fields);
 
5446
  Item *item;
 
5447
  while ((item=it++))
 
5448
  {
 
5449
    if (item->get_tmp_table_field() && ! item->const_item())
 
5450
      field_count++;
 
5451
  }
 
5452
 
 
5453
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
 
5454
  {                    // only const items with no OPTION_FOUND_ROWS
 
5455
    join->unit->select_limit_cnt= 1;            // Only send first row
 
5456
    return(0);
 
5457
  }
 
5458
  Field **first_field=entry->field+entry->s->fields - field_count;
 
5459
  offset= (field_count ?
 
5460
           entry->field[entry->s->fields - field_count]->
 
5461
           offset(entry->record[0]) : 0);
 
5462
  reclength= entry->s->reclength-offset;
 
5463
 
 
5464
  entry->free_io_cache();                               // Safety
 
5465
  entry->file->info(HA_STATUS_VARIABLE);
 
5466
  if (entry->s->db_type() == heap_engine ||
 
5467
      (!entry->s->blob_fields &&
 
5468
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
5469
        session->variables.sortbuff_size)))
 
5470
    error= remove_dup_with_hash_index(join->session, entry,
 
5471
                                     field_count, first_field,
 
5472
                                     reclength, having);
 
5473
  else
 
5474
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
 
5475
                                  having);
 
5476
 
 
5477
  free_blobs(first_field);
 
5478
  return(error);
 
5479
}
 
5480
 
 
5481
/**
 
5482
  Function to setup clauses without sum functions.
 
5483
*/
 
5484
static int setup_without_group(Session *session, 
 
5485
                               Item **ref_pointer_array,
 
5486
                               TableList *tables,
 
5487
                               TableList *,
 
5488
                               List<Item> &fields,
 
5489
                               List<Item> &all_fields,
 
5490
                               COND **conds,
 
5491
                               order_st *order,
 
5492
                               order_st *group,
 
5493
                               bool *hidden_group_fields)
 
5494
{
 
5495
  int res;
 
5496
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
5497
 
 
5498
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5499
  res= session->setup_conds(tables, conds);
 
5500
 
 
5501
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
5502
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
 
5503
                          order);
 
5504
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5505
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
 
5506
                          group, hidden_group_fields);
 
5507
  session->lex->allow_sum_func= save_allow_sum_func;
 
5508
  return(res);
 
5509
}
 
5510
 
 
5511
/**
 
5512
  Calculate the best possible join and initialize the join structure.
 
5513
 
 
5514
  @retval
 
5515
    0   ok
 
5516
  @retval
 
5517
    1   Fatal error
 
5518
*/
 
5519
static bool make_join_statistics(JOIN *join, TableList *tables, COND *conds, DYNAMIC_ARRAY *keyuse_array)
 
5520
{
 
5521
  int error;
 
5522
  Table *table;
 
5523
  uint32_t i;
 
5524
  uint32_t table_count;
 
5525
  uint32_t const_count;
 
5526
  uint32_t key;
 
5527
  table_map found_const_table_map;
 
5528
  table_map all_table_map;
 
5529
  table_map found_ref;
 
5530
  table_map refs;
 
5531
  key_map const_ref;
 
5532
  key_map eq_part;
 
5533
  Table **table_vector= NULL;
 
5534
  JoinTable *stat= NULL;
 
5535
  JoinTable *stat_end= NULL;
 
5536
  JoinTable *s= NULL;
 
5537
  JoinTable **stat_ref= NULL;
 
5538
  optimizer::KeyUse *keyuse= NULL;
 
5539
  optimizer::KeyUse *start_keyuse= NULL;
 
5540
  table_map outer_join= 0;
 
5541
  vector<optimizer::SargableParam> sargables;
 
5542
  JoinTable *stat_vector[MAX_TABLES+1];
 
5543
  optimizer::Position *partial_pos;
 
5544
 
 
5545
  table_count= join->tables;
 
5546
  stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
 
5547
  stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
 
5548
  table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
5549
  if (! stat || ! stat_ref || ! table_vector)
 
5550
    return 1;
 
5551
 
 
5552
  join->best_ref=stat_vector;
 
5553
 
 
5554
  stat_end=stat+table_count;
 
5555
  found_const_table_map= all_table_map=0;
 
5556
  const_count=0;
 
5557
 
 
5558
  for (s= stat, i= 0;
 
5559
       tables;
 
5560
       s++, tables= tables->next_leaf, i++)
 
5561
  {
 
5562
    TableList *embedding= tables->embedding;
 
5563
    stat_vector[i]=s;
 
5564
    s->keys.reset();
 
5565
    s->const_keys.reset();
 
5566
    s->checked_keys.reset();
 
5567
    s->needed_reg.reset();
 
5568
    table_vector[i]=s->table=table=tables->table;
 
5569
    table->pos_in_table_list= tables;
 
5570
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
5571
    if (error)
 
5572
    {
 
5573
        table->file->print_error(error, MYF(0));
 
5574
        return 1;
 
5575
    }
 
5576
    table->quick_keys.reset();
 
5577
    table->reginfo.join_tab=s;
 
5578
    table->reginfo.not_exists_optimize=0;
 
5579
    memset(table->const_key_parts, 0,
 
5580
           sizeof(key_part_map)*table->s->keys);
 
5581
    all_table_map|= table->map;
 
5582
    s->join=join;
 
5583
    s->info=0;                                  // For describe
 
5584
 
 
5585
    s->dependent= tables->dep_tables;
 
5586
    s->key_dependent= 0;
 
5587
    if (tables->schema_table)
 
5588
      table->file->stats.records= 2;
 
5589
    table->quick_condition_rows= table->file->stats.records;
 
5590
 
 
5591
    s->on_expr_ref= &tables->on_expr;
 
5592
    if (*s->on_expr_ref)
 
5593
    {
 
5594
      /* s is the only inner table of an outer join */
 
5595
      if (!table->file->stats.records && !embedding)
 
5596
      {                                         // Empty table
 
5597
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
5598
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5599
        continue;
 
5600
      }
 
5601
      outer_join|= table->map;
 
5602
      s->embedding_map.reset();
 
5603
      for (;embedding; embedding= embedding->embedding)
 
5604
        s->embedding_map|= embedding->nested_join->nj_map;
 
5605
      continue;
 
5606
    }
 
5607
    if (embedding && !(false && ! embedding->embedding))
 
5608
    {
 
5609
      /* s belongs to a nested join, maybe to several embedded joins */
 
5610
      s->embedding_map.reset();
 
5611
      do
 
5612
      {
 
5613
        nested_join_st *nested_join= embedding->nested_join;
 
5614
        s->embedding_map|= nested_join->nj_map;
 
5615
        s->dependent|= embedding->dep_tables;
 
5616
        embedding= embedding->embedding;
 
5617
        outer_join|= nested_join->used_tables;
 
5618
      }
 
5619
      while (embedding);
 
5620
      continue;
 
5621
    }
 
5622
    if ((table->file->stats.records <= 1) && !s->dependent &&
 
5623
              (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && 
 
5624
        !join->no_const_tables)
 
5625
    {
 
5626
      set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5627
    }
 
5628
  }
 
5629
  stat_vector[i]=0;
 
5630
  join->outer_join=outer_join;
 
5631
 
 
5632
  if (join->outer_join)
 
5633
  {
 
5634
    /*
 
5635
       Build transitive closure for relation 'to be dependent on'.
 
5636
       This will speed up the plan search for many cases with outer joins,
 
5637
       as well as allow us to catch illegal cross references/
 
5638
       Warshall's algorithm is used to build the transitive closure.
 
5639
       As we use bitmaps to represent the relation the complexity
 
5640
       of the algorithm is O((number of tables)^2).
 
5641
    */
 
5642
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
5643
    {
 
5644
      for (uint32_t j= 0 ; j < table_count ; j++)
 
5645
      {
 
5646
        table= stat[j].table;
 
5647
        if (s->dependent & table->map)
 
5648
          s->dependent |= table->reginfo.join_tab->dependent;
 
5649
      }
 
5650
      if (s->dependent)
 
5651
        s->table->maybe_null= 1;
 
5652
    }
 
5653
    /* Catch illegal cross references for outer joins */
 
5654
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
5655
    {
 
5656
      if (s->dependent & s->table->map)
 
5657
      {
 
5658
        join->tables=0;                 // Don't use join->table
 
5659
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
5660
        return 1;
 
5661
      }
 
5662
      s->key_dependent= s->dependent;
 
5663
    }
 
5664
  }
 
5665
 
 
5666
  if (conds || outer_join)
 
5667
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
 
5668
                            conds, join->cond_equal,
 
5669
                            ~outer_join, join->select_lex, sargables))
 
5670
      return 1;
 
5671
 
 
5672
  /* Read tables with 0 or 1 rows (system tables) */
 
5673
  join->const_table_map= 0;
 
5674
 
 
5675
  optimizer::Position *p_pos= join->getFirstPosInPartialPlan();
 
5676
  optimizer::Position *p_end= join->getSpecificPosInPartialPlan(const_count);
 
5677
  while (p_pos < p_end)
 
5678
  {
 
5679
    int tmp;
 
5680
    s= p_pos->getJoinTable();
 
5681
    s->type= AM_SYSTEM;
 
5682
    join->const_table_map|=s->table->map;
 
5683
    if ((tmp= join_read_const_table(s, p_pos)))
 
5684
    {
 
5685
      if (tmp > 0)
 
5686
        return 1;                       // Fatal error
 
5687
    }
 
5688
    else
 
5689
      found_const_table_map|= s->table->map;
 
5690
    p_pos++;
 
5691
  }
 
5692
 
 
5693
  /* loop until no more const tables are found */
 
5694
  int ref_changed;
 
5695
  do
 
5696
  {
 
5697
  more_const_tables_found:
 
5698
    ref_changed = 0;
 
5699
    found_ref=0;
 
5700
 
 
5701
    /*
 
5702
      We only have to loop from stat_vector + const_count as
 
5703
      set_position() will move all const_tables first in stat_vector
 
5704
    */
 
5705
 
 
5706
    for (JoinTable **pos= stat_vector+const_count; (s= *pos); pos++)
 
5707
    {
 
5708
      table= s->table;
 
5709
 
 
5710
      /*
 
5711
        If equi-join condition by a key is null rejecting and after a
 
5712
        substitution of a const table the key value happens to be null
 
5713
        then we can state that there are no matches for this equi-join.
 
5714
      */
 
5715
      if ((keyuse= s->keyuse) && *s->on_expr_ref && s->embedding_map.none())
 
5716
      {
 
5717
        /*
 
5718
          When performing an outer join operation if there are no matching rows
 
5719
          for the single row of the outer table all the inner tables are to be
 
5720
          null complemented and thus considered as constant tables.
 
5721
          Here we apply this consideration to the case of outer join operations
 
5722
          with a single inner table only because the case with nested tables
 
5723
          would require a more thorough analysis.
 
5724
          TODO. Apply single row substitution to null complemented inner tables
 
5725
          for nested outer join operations.
 
5726
        */
 
5727
        while (keyuse->getTable() == table)
 
5728
        {
 
5729
          if (! (keyuse->getVal()->used_tables() & ~join->const_table_map) &&
 
5730
              keyuse->getVal()->is_null() && keyuse->isNullRejected())
 
5731
          {
 
5732
            s->type= AM_CONST;
 
5733
            table->mark_as_null_row();
 
5734
            found_const_table_map|= table->map;
 
5735
            join->const_table_map|= table->map;
 
5736
            set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5737
            goto more_const_tables_found;
 
5738
           }
 
5739
          keyuse++;
 
5740
        }
 
5741
      }
 
5742
 
 
5743
      if (s->dependent)                         // If dependent on some table
 
5744
      {
 
5745
        // All dep. must be constants
 
5746
        if (s->dependent & ~(found_const_table_map))
 
5747
          continue;
 
5748
        if (table->file->stats.records <= 1L &&
 
5749
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
5750
                  !table->pos_in_table_list->embedding)
 
5751
        {                                       // system table
 
5752
          int tmp= 0;
 
5753
          s->type= AM_SYSTEM;
 
5754
          join->const_table_map|=table->map;
 
5755
          set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5756
          partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
 
5757
          if ((tmp= join_read_const_table(s, partial_pos)))
 
5758
          {
 
5759
            if (tmp > 0)
 
5760
              return 1;                 // Fatal error
 
5761
          }
 
5762
          else
 
5763
            found_const_table_map|= table->map;
 
5764
          continue;
 
5765
        }
 
5766
      }
 
5767
      /* check if table can be read by key or table only uses const refs */
 
5768
      if ((keyuse=s->keyuse))
 
5769
      {
 
5770
        s->type= AM_REF;
 
5771
        while (keyuse->getTable() == table)
 
5772
        {
 
5773
          start_keyuse= keyuse;
 
5774
          key= keyuse->getKey();
 
5775
          s->keys.set(key);               // QQ: remove this ?
 
5776
 
 
5777
          refs= 0;
 
5778
          const_ref.reset();
 
5779
          eq_part.reset();
 
5780
          do
 
5781
          {
 
5782
            if (keyuse->getVal()->type() != Item::NULL_ITEM && 
 
5783
                ! keyuse->getOptimizeFlags())
 
5784
            {
 
5785
              if (! ((~found_const_table_map) & keyuse->getUsedTables()))
 
5786
                const_ref.set(keyuse->getKeypart());
 
5787
              else
 
5788
                refs|= keyuse->getUsedTables();
 
5789
              eq_part.set(keyuse->getKeypart());
 
5790
            }
 
5791
            keyuse++;
 
5792
          } while (keyuse->getTable() == table && keyuse->getKey() == key);
 
5793
 
 
5794
          if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
 
5795
              ! table->pos_in_table_list->embedding)
 
5796
          {
 
5797
            if ((table->key_info[key].flags & (HA_NOSAME)) == HA_NOSAME)
 
5798
            {
 
5799
              if (const_ref == eq_part)
 
5800
              {                                 // Found everything for ref.
 
5801
                int tmp;
 
5802
                ref_changed = 1;
 
5803
                s->type= AM_CONST;
 
5804
                join->const_table_map|= table->map;
 
5805
                set_position(join, const_count++, s, start_keyuse);
 
5806
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
 
5807
                  return 1;
 
5808
                partial_pos= join->getSpecificPosInPartialPlan(const_count - 1);
 
5809
                if ((tmp=join_read_const_table(s, partial_pos)))
 
5810
                {
 
5811
                  if (tmp > 0)
 
5812
                    return 1;                   // Fatal error
 
5813
                }
 
5814
                else
 
5815
                  found_const_table_map|= table->map;
 
5816
                break;
 
5817
              }
 
5818
              else
 
5819
                found_ref|= refs;      // Table is const if all refs are const
 
5820
            }
 
5821
            else if (const_ref == eq_part)
 
5822
              s->const_keys.set(key);
 
5823
          }
 
5824
        }
 
5825
      }
 
5826
    }
 
5827
  } while (join->const_table_map & found_ref && ref_changed);
 
5828
 
 
5829
  /*
 
5830
    Update info on indexes that can be used for search lookups as
 
5831
    reading const tables may has added new sargable predicates.
 
5832
  */
 
5833
  if (const_count && ! sargables.empty())
 
5834
  {
 
5835
    vector<optimizer::SargableParam>::iterator iter= sargables.begin();
 
5836
    while (iter != sargables.end())
 
5837
    {
 
5838
      Field *field= (*iter).getField();
 
5839
      JoinTable *join_tab= field->table->reginfo.join_tab;
 
5840
      key_map possible_keys= field->key_start;
 
5841
      possible_keys&= field->table->keys_in_use_for_query;
 
5842
      bool is_const= true;
 
5843
      for (uint32_t j= 0; j < (*iter).getNumValues(); j++)
 
5844
        is_const&= (*iter).isConstItem(j);
 
5845
      if (is_const)
 
5846
        join_tab[0].const_keys|= possible_keys;
 
5847
      ++iter;
 
5848
    }
 
5849
  }
 
5850
 
 
5851
  /* Calc how many (possible) matched records in each table */
 
5852
 
 
5853
  for (s=stat ; s < stat_end ; s++)
 
5854
  {
 
5855
    if (s->type == AM_SYSTEM || s->type == AM_CONST)
 
5856
    {
 
5857
      /* Only one matching row */
 
5858
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
5859
      continue;
 
5860
    }
 
5861
    /* Approximate found rows and time to read them */
 
5862
    s->found_records=s->records=s->table->file->stats.records;
 
5863
    s->read_time=(ha_rows) s->table->file->scan_time();
 
5864
 
 
5865
    /*
 
5866
      Set a max range of how many seeks we can expect when using keys
 
5867
      This is can't be to high as otherwise we are likely to use
 
5868
      table scan.
 
5869
    */
 
5870
    s->worst_seeks= min((double) s->found_records / 10,
 
5871
                        (double) s->read_time*3);
 
5872
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
5873
      s->worst_seeks=2.0;
 
5874
 
 
5875
    /*
 
5876
      Add to stat->const_keys those indexes for which all group fields or
 
5877
      all select distinct fields participate in one index.
 
5878
    */
 
5879
    add_group_and_distinct_keys(join, s);
 
5880
 
 
5881
    if (s->const_keys.any() &&
 
5882
        !s->table->pos_in_table_list->embedding)
 
5883
    {
 
5884
      ha_rows records;
 
5885
      SQL_SELECT *select;
 
5886
      select= make_select(s->table, found_const_table_map, found_const_table_map, *s->on_expr_ref ? *s->on_expr_ref : conds, 1, &error);
 
5887
      if (! select)
 
5888
        return 1;
 
5889
      records= get_quick_record_count(join->session, select, s->table, &s->const_keys, join->row_limit);
 
5890
      s->quick=select->quick;
 
5891
      s->needed_reg=select->needed_reg;
 
5892
      select->quick=0;
 
5893
      if (records == 0 && s->table->reginfo.impossible_range)
 
5894
      {
 
5895
        /*
 
5896
          Impossible WHERE or ON expression
 
5897
          In case of ON, we mark that the we match one empty NULL row.
 
5898
          In case of WHERE, don't set found_const_table_map to get the
 
5899
          caller to abort with a zero row result.
 
5900
        */
 
5901
        join->const_table_map|= s->table->map;
 
5902
        set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
 
5903
        s->type= AM_CONST;
 
5904
        if (*s->on_expr_ref)
 
5905
        {
 
5906
          /* Generate empty row */
 
5907
          s->info= "Impossible ON condition";
 
5908
          found_const_table_map|= s->table->map;
 
5909
          s->type= AM_CONST;
 
5910
          s->table->mark_as_null_row();         // All fields are NULL
 
5911
        }
 
5912
      }
 
5913
      if (records != HA_POS_ERROR)
 
5914
      {
 
5915
        s->found_records=records;
 
5916
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
5917
      }
 
5918
      delete select;
 
5919
    }
 
5920
  }
 
5921
 
 
5922
  join->join_tab=stat;
 
5923
  join->map2table=stat_ref;
 
5924
  join->table= join->all_tables=table_vector;
 
5925
  join->const_tables=const_count;
 
5926
  join->found_const_table_map=found_const_table_map;
 
5927
 
 
5928
  /* Find an optimal join order of the non-constant tables. */
 
5929
  if (join->const_tables != join->tables)
 
5930
  {
 
5931
    optimize_keyuse(join, keyuse_array);
 
5932
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
5933
      return(true);
 
5934
  }
 
5935
  else
 
5936
  {
 
5937
    join->copyPartialPlanIntoOptimalPlan(join->const_tables);
 
5938
    join->best_read= 1.0;
 
5939
  }
 
5940
  /* Generate an execution plan from the found optimal join order. */
 
5941
  return (join->session->killed || get_best_combination(join));
 
5942
}
 
5943
 
 
5944
/**
 
5945
  Assign each nested join structure a bit in the nested join bitset.
 
5946
 
 
5947
    Assign each nested join structure (except "confluent" ones - those that
 
5948
    embed only one element) a bit in the nested join bitset.
 
5949
 
 
5950
  @param join          Join being processed
 
5951
  @param join_list     List of tables
 
5952
  @param first_unused  Number of first unused bit in the nest joing bitset before the
 
5953
                       call
 
5954
 
 
5955
  @note
 
5956
    This function is called after simplify_joins(), when there are no
 
5957
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
5958
    we will not run out of bits in the nested join bitset.
 
5959
 
 
5960
  @return
 
5961
    First unused bit in the nest join bitset after the call.
 
5962
*/
 
5963
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
 
5964
{
 
5965
  List_iterator<TableList> li(*join_list);
 
5966
  TableList *table;
 
5967
  while ((table= li++))
 
5968
  {
 
5969
    nested_join_st *nested_join;
 
5970
    if ((nested_join= table->nested_join))
 
5971
    {
 
5972
      /*
 
5973
        It is guaranteed by simplify_joins() function that a nested join
 
5974
        that has only one child is either
 
5975
         - a single-table view (the child is the underlying table), or
 
5976
         - a single-table semi-join nest
 
5977
 
 
5978
        We don't assign bits to such sj-nests because
 
5979
        1. it is redundant (a "sequence" of one table cannot be interleaved
 
5980
            with anything)
 
5981
        2. we could run out of bits in the nested join bitset otherwise.
 
5982
      */
 
5983
      if (nested_join->join_list.elements != 1)
 
5984
      {
 
5985
        /* Don't assign bits to sj-nests */
 
5986
        if (table->on_expr)
 
5987
          nested_join->nj_map.set(first_unused++);
 
5988
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
5989
                                                    first_unused);
 
5990
      }
 
5991
    }
 
5992
  }
 
5993
  return(first_unused);
 
5994
}
 
5995
 
 
5996
 
 
5997
/**
 
5998
  Return table number if there is only one table in sort order
 
5999
  and group and order is compatible, else return 0.
 
6000
*/
 
6001
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
6002
{
 
6003
  table_map map= (table_map) 0;
 
6004
 
 
6005
  if (!a)
 
6006
    a= b;                                       // Only one need to be given
 
6007
  else if (!b)
 
6008
    b= a;
 
6009
 
 
6010
  for (; a && b; a=a->next,b=b->next)
 
6011
  {
 
6012
    if (!(*a->item)->eq(*b->item,1))
 
6013
      return (Table *) NULL;
 
6014
    map|= a->item[0]->used_tables();
 
6015
  }
 
6016
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
6017
    return (Table *) NULL;
 
6018
 
 
6019
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
6020
  if (map != tables->table->map)
 
6021
    return (Table *) NULL;                              // More than one table
 
6022
  return tables->table;
 
6023
}
 
6024
 
 
6025
/**
 
6026
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
6027
 
 
6028
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
6029
    the passed join_list.
 
6030
 
 
6031
  @param join_list  List of nested joins to process. It may also contain base
 
6032
                    tables which will be ignored.
 
6033
*/
 
6034
static void reset_nj_counters(List<TableList> *join_list)
 
6035
{
 
6036
  List_iterator<TableList> li(*join_list);
 
6037
  TableList *table;
 
6038
  while ((table= li++))
 
6039
  {
 
6040
    nested_join_st *nested_join;
 
6041
    if ((nested_join= table->nested_join))
 
6042
    {
 
6043
      nested_join->counter_= 0;
 
6044
      reset_nj_counters(&nested_join->join_list);
 
6045
    }
 
6046
  }
 
6047
  return;
 
6048
}
 
6049
 
 
6050
/**
 
6051
  Return 1 if second is a subpart of first argument.
 
6052
 
 
6053
  If first parts has different direction, change it to second part
 
6054
  (group is sorted like order)
 
6055
*/
 
6056
static bool test_if_subpart(order_st *a,order_st *b)
 
6057
{
 
6058
  for (; a && b; a=a->next,b=b->next)
 
6059
  {
 
6060
    if ((*a->item)->eq(*b->item,1))
 
6061
      a->asc=b->asc;
 
6062
    else
 
6063
      return 0;
 
6064
  }
 
6065
  return test(!b);
 
6066
}
 
6067
 
 
6068
/**
 
6069
  Nested joins perspective: Remove the last table from the join order.
 
6070
 
 
6071
    Remove the last table from the partial join order and update the nested
 
6072
    joins counters and join->cur_embedding_map. It is ok to call this
 
6073
    function for the first table in join order (for which
 
6074
    check_interleaving_with_nj has not been called)
 
6075
 
 
6076
  @param last  join table to remove, it is assumed to be the last in current
 
6077
               partial join order.
 
6078
*/
 
6079
static void restore_prev_nj_state(JoinTable *last)
 
6080
{
 
6081
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
6082
  JOIN *join= last->join;
 
6083
  while (last_emb)
 
6084
  {
 
6085
    if (last_emb->on_expr)
 
6086
    {
 
6087
      if (!(--last_emb->nested_join->counter_))
 
6088
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
6089
      else if (last_emb->nested_join->join_list.elements-1 ==
 
6090
               last_emb->nested_join->counter_)
 
6091
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
6092
      else
 
6093
        break;
 
6094
    }
 
6095
    last_emb= last_emb->embedding;
 
6096
  }
 
6097
}
 
6098
 
 
6099
/**
 
6100
  Determine if the set is already ordered for order_st BY, so it can
 
6101
  disable join cache because it will change the ordering of the results.
 
6102
  Code handles sort table that is at any location (not only first after
 
6103
  the const tables) despite the fact that it's currently prohibited.
 
6104
  We must disable join cache if the first non-const table alone is
 
6105
  ordered. If there is a temp table the ordering is done as a last
 
6106
  operation and doesn't prevent join cache usage.
 
6107
*/
 
6108
static uint32_t make_join_orderinfo(JOIN *join)
 
6109
{
 
6110
  uint32_t i;
 
6111
  if (join->need_tmp)
 
6112
    return join->tables;
 
6113
 
 
6114
  for (i=join->const_tables ; i < join->tables ; i++)
 
6115
  {
 
6116
    JoinTable *tab= join->join_tab+i;
 
6117
    Table *table= tab->table;
 
6118
    if ((table == join->sort_by_table &&
 
6119
        (!join->order || join->skip_sort_order)) ||
 
6120
        (join->sort_by_table == (Table *) 1 &&  i != join->const_tables))
 
6121
    {
 
6122
      break;
 
6123
    }
 
6124
  }
 
6125
  return i;
 
6126
}
 
6127
 
 
6128
/**
 
6129
  Create a condition for a const reference and add this to the
 
6130
  currenct select for the table.
 
6131
*/
 
6132
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab)
 
6133
{
 
6134
  if (!join_tab->ref.key_parts)
 
6135
    return(false);
 
6136
 
 
6137
  Item_cond_and *cond=new Item_cond_and();
 
6138
  Table *table=join_tab->table;
 
6139
  int error;
 
6140
  if (!cond)
 
6141
    return(true);
 
6142
 
 
6143
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
6144
  {
 
6145
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
6146
                              fieldnr-1];
 
6147
    Item *value=join_tab->ref.items[i];
 
6148
    cond->add(new Item_func_equal(new Item_field(field), value));
 
6149
  }
 
6150
  if (session->is_fatal_error)
 
6151
    return(true);
 
6152
 
 
6153
  if (!cond->fixed)
 
6154
    cond->fix_fields(session, (Item**)&cond);
 
6155
  if (join_tab->select)
 
6156
  {
 
6157
    error=(int) cond->add(join_tab->select->cond);
 
6158
    join_tab->select_cond=join_tab->select->cond=cond;
 
6159
  }
 
6160
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
6161
                                          &error)))
 
6162
    join_tab->select_cond=cond;
 
6163
 
 
6164
  return(error ? true : false);
 
6165
}
 
6166
 
 
6167
static void free_blobs(Field **ptr)
 
6168
{
 
6169
  for (; *ptr ; ptr++)
 
6170
  {
 
6171
    if ((*ptr)->flags & BLOB_FLAG)
 
6172
      ((Field_blob *) (*ptr))->free();
 
6173
  }
 
6174
}
 
6175
 
 
6176
/**
 
6177
  @} (end of group Query_Optimizer)
 
6178
*/