~drizzle-trunk/drizzle/development

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