Drizzled Public API Documentation

range.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 /*
00021   TODO:
00022   Fix that MAYBE_KEY are stored in the tree so that we can detect use
00023   of full hash keys for queries like:
00024 
00025   select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);
00026 
00027 */
00028 
00029 /*
00030   This cursor contains:
00031 
00032   RangeAnalysisModule
00033     A module that accepts a condition, index (or partitioning) description,
00034     and builds lists of intervals (in index/partitioning space), such that
00035     all possible records that match the condition are contained within the
00036     intervals.
00037     The entry point for the range analysis module is get_mm_tree() function.
00038 
00039     The lists are returned in form of complicated structure of interlinked
00040     optimizer::SEL_TREE/optimizer::SEL_IMERGE/SEL_ARG objects.
00041     See quick_range_seq_next, find_used_partitions for examples of how to walk
00042     this structure.
00043     All direct "users" of this module are located within this cursor, too.
00044 
00045 
00046   Range/index_merge/groupby-minmax optimizer module
00047     A module that accepts a table, condition, and returns
00048      - a QUICK_*_SELECT object that can be used to retrieve rows that match
00049        the specified condition, or a "no records will match the condition"
00050        statement.
00051 
00052     The module entry points are
00053       test_quick_select()
00054       get_quick_select_for_ref()
00055 
00056 
00057   Record retrieval code for range/index_merge/groupby-min-max.
00058     Implementations of QUICK_*_SELECT classes.
00059 
00060   KeyTupleFormat
00061   ~~~~~~~~~~~~~~
00062   The code in this cursor (and elsewhere) makes operations on key value tuples.
00063   Those tuples are stored in the following format:
00064 
00065   The tuple is a sequence of key part values. The length of key part value
00066   depends only on its type (and not depends on the what value is stored)
00067 
00068     KeyTuple: keypart1-data, keypart2-data, ...
00069 
00070   The value of each keypart is stored in the following format:
00071 
00072     keypart_data: [isnull_byte] keypart-value-bytes
00073 
00074   If a keypart may have a NULL value (key_part->field->real_maybe_null() can
00075   be used to check this), then the first byte is a NULL indicator with the
00076   following valid values:
00077     1  - keypart has NULL value.
00078     0  - keypart has non-NULL value.
00079 
00080   <questionable-statement> If isnull_byte==1 (NULL value), then the following
00081   keypart->length bytes must be 0.
00082   </questionable-statement>
00083 
00084   keypart-value-bytes holds the value. Its format depends on the field type.
00085   The length of keypart-value-bytes may or may not depend on the value being
00086   stored. The default is that length is static and equal to
00087   KeyPartInfo::length.
00088 
00089   Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the
00090   value:
00091 
00092      keypart-value-bytes: value_length value_bytes
00093 
00094   The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.
00095 
00096   See key_copy() and key_restore() for code to move data between index tuple
00097   and table record
00098 
00099   CAUTION: the above description is only sergefp's understanding of the
00100            subject and may omit some details.
00101 */
00102 
00103 #include <config.h>
00104 
00105 #include <math.h>
00106 #include <float.h>
00107 
00108 #include <string>
00109 #include <vector>
00110 #include <algorithm>
00111 
00112 #include <boost/dynamic_bitset.hpp>
00113 
00114 #include <drizzled/check_stack_overrun.h>
00115 #include <drizzled/error.h>
00116 #include <drizzled/field/num.h>
00117 #include <drizzled/internal/iocache.h>
00118 #include <drizzled/internal/my_sys.h>
00119 #include <drizzled/item/cmpfunc.h>
00120 #include <drizzled/optimizer/cost_vector.h>
00121 #include <drizzled/optimizer/quick_group_min_max_select.h>
00122 #include <drizzled/optimizer/quick_index_merge_select.h>
00123 #include <drizzled/optimizer/quick_range.h>
00124 #include <drizzled/optimizer/quick_range_select.h>
00125 #include <drizzled/optimizer/quick_ror_intersect_select.h>
00126 #include <drizzled/optimizer/quick_ror_union_select.h>
00127 #include <drizzled/optimizer/range.h>
00128 #include <drizzled/optimizer/range_param.h>
00129 #include <drizzled/optimizer/sel_arg.h>
00130 #include <drizzled/optimizer/sel_imerge.h>
00131 #include <drizzled/optimizer/sel_tree.h>
00132 #include <drizzled/optimizer/sum.h>
00133 #include <drizzled/optimizer/table_read_plan.h>
00134 #include <drizzled/plugin/storage_engine.h>
00135 #include <drizzled/records.h>
00136 #include <drizzled/sql_base.h>
00137 #include <drizzled/sql_select.h>
00138 #include <drizzled/table_reference.h>
00139 #include <drizzled/session.h>
00140 #include <drizzled/key.h>
00141 #include <drizzled/unique.h>
00142 #include <drizzled/temporal.h> /* Needed in get_mm_leaf() for timestamp -> datetime comparisons */
00143 #include <drizzled/sql_lex.h>
00144 #include <drizzled/system_variables.h>
00145 
00146 using namespace std;
00147 
00148 namespace drizzled {
00149 
00150 static const int HA_END_SPACE_KEY= 0;
00151 
00152 /*
00153   Convert double value to #rows. Currently this does floor(), and we
00154   might consider using round() instead.
00155 */
00156 static inline ha_rows double2rows(double x)
00157 {
00158     return static_cast<ha_rows>(x);
00159 }
00160 
00161 static unsigned char is_null_string[2]= {1,0};
00162 
00163 
00207 static void get_sweep_read_cost(Table *table,
00208                                 ha_rows nrows,
00209                                 bool interrupted,
00210                                 optimizer::CostVector *cost)
00211 {
00212   cost->zero();
00213   if (table->cursor->primary_key_is_clustered())
00214   {
00215     cost->setIOCount(table->cursor->read_time(table->getShare()->getPrimaryKey(),
00216                                              static_cast<uint32_t>(nrows),
00217                                              nrows));
00218   }
00219   else
00220   {
00221     double n_blocks=
00222       ceil(static_cast<double>(table->cursor->stats.data_file_length) / IO_SIZE);
00223     double busy_blocks=
00224       n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, static_cast<double>(nrows)));
00225     if (busy_blocks < 1.0)
00226       busy_blocks= 1.0;
00227 
00228     cost->setIOCount(busy_blocks);
00229 
00230     if (! interrupted)
00231     {
00232       /* Assume reading is done in one 'sweep' */
00233       cost->setAvgIOCost((DISK_SEEK_BASE_COST +
00234                           DISK_SEEK_PROP_COST*n_blocks/busy_blocks));
00235     }
00236   }
00237 }
00238 
00239 static optimizer::SEL_TREE * get_mm_parts(optimizer::RangeParameter *param,
00240                                COND *cond_func,
00241                                Field *field,
00242                                Item_func::Functype type,
00243                                Item *value,
00244                                Item_result cmp_type);
00245 
00246 static optimizer::SEL_ARG *get_mm_leaf(optimizer::RangeParameter *param,
00247                                        COND *cond_func,
00248                                        Field *field,
00249                                        KEY_PART *key_part,
00250                                        Item_func::Functype type,
00251                                        Item *value);
00252 
00253 static optimizer::SEL_TREE *get_mm_tree(optimizer::RangeParameter *param, COND *cond);
00254 
00255 static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts);
00256 
00257 static ha_rows check_quick_select(Session *session,
00258                                   optimizer::Parameter *param,
00259                                   uint32_t idx,
00260                                   bool index_only,
00261                                   optimizer::SEL_ARG *tree,
00262                                   bool update_tbl_stats,
00263                                   uint32_t *mrr_flags,
00264                                   uint32_t *bufsize,
00265                                   optimizer::CostVector *cost);
00266 
00267 static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
00268                                                       optimizer::Parameter *param,
00269                                                       optimizer::SEL_TREE *tree,
00270                                                       bool index_read_must_be_used,
00271                                                       bool update_tbl_stats,
00272                                                       double read_time);
00273 
00274 static
00275 optimizer::RorIntersectReadPlan *get_best_ror_intersect(const optimizer::Parameter *param,
00276                                                         optimizer::SEL_TREE *tree,
00277                                                         double read_time,
00278                                                         bool *are_all_covering);
00279 
00280 static
00281 optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
00282                                                                  optimizer::SEL_TREE *tree,
00283                                                                  double read_time);
00284 
00285 static
00286 optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
00287                                                   optimizer::Parameter *param,
00288                                                   optimizer::SEL_IMERGE *imerge,
00289                                                   double read_time);
00290 
00291 static
00292 optimizer::GroupMinMaxReadPlan *get_best_group_min_max(optimizer::Parameter *param, optimizer::SEL_TREE *tree);
00293 
00294 static optimizer::SEL_TREE *tree_and(optimizer::RangeParameter *param,
00295                                      optimizer::SEL_TREE *tree1,
00296                                      optimizer::SEL_TREE *tree2);
00297 
00298 static optimizer::SEL_ARG *sel_add(optimizer::SEL_ARG *key1, optimizer::SEL_ARG *key2);
00299 
00300 static optimizer::SEL_ARG *key_and(optimizer::RangeParameter *param,
00301                                    optimizer::SEL_ARG *key1,
00302                                    optimizer::SEL_ARG *key2,
00303                                    uint32_t clone_flag);
00304 
00305 static bool get_range(optimizer::SEL_ARG **e1, optimizer::SEL_ARG **e2, optimizer::SEL_ARG *root1);
00306 
00307 optimizer::SEL_ARG optimizer::null_element(optimizer::SEL_ARG::IMPOSSIBLE);
00308 
00309 static bool null_part_in_key(KEY_PART *key_part,
00310                              const unsigned char *key,
00311                              uint32_t length);
00312 
00313 bool sel_trees_can_be_ored(optimizer::SEL_TREE *tree1,
00314                            optimizer::SEL_TREE *tree2,
00315                            optimizer::RangeParameter *param);
00316 
00317 
00318 
00319 
00320 
00321 
00322 /*
00323   Perform AND operation on two index_merge lists and store result in *im1.
00324 */
00325 
00326 inline void imerge_list_and_list(List<optimizer::SEL_IMERGE> *im1, List<optimizer::SEL_IMERGE> *im2)
00327 {
00328   im1->concat(im2);
00329 }
00330 
00331 
00332 /***************************************************************************
00333 ** Basic functions for SqlSelect and QuickRangeSelect
00334 ***************************************************************************/
00335 
00336   /* make a select from mysql info
00337      Error is set as following:
00338      0 = ok
00339      1 = Got some error (out of memory?)
00340      */
00341 
00342 optimizer::SqlSelect *optimizer::make_select(Table *head,
00343                                              table_map const_tables,
00344                                              table_map read_tables,
00345                                              COND *conds,
00346                                              bool allow_null_cond,
00347                                              int *error)
00348 {
00349   *error= 0;
00350 
00351   if (! conds && ! allow_null_cond)
00352   {
00353     return 0;
00354   }
00355   optimizer::SqlSelect* select= new optimizer::SqlSelect;
00356   select->read_tables=read_tables;
00357   select->const_tables=const_tables;
00358   select->head=head;
00359   select->cond=conds;
00360 
00361   if (head->sort.io_cache)
00362   {
00363     memcpy(select->file, head->sort.io_cache, sizeof(internal::io_cache_st));
00364     select->records=(ha_rows) (select->file->end_of_file/
00365              head->cursor->ref_length);
00366     delete head->sort.io_cache;
00367     head->sort.io_cache=0;
00368   }
00369   return(select);
00370 }
00371 
00372 
00373 optimizer::SqlSelect::SqlSelect()
00374   :
00375     quick(NULL),
00376     cond(NULL),
00377     file(static_cast<internal::io_cache_st *>(memory::sql_calloc(sizeof(internal::io_cache_st)))),
00378     free_cond(false)
00379 {
00380   quick_keys.reset();
00381   needed_reg.reset();
00382   file->clear();
00383 }
00384 
00385 
00386 void optimizer::SqlSelect::cleanup()
00387 {
00388   delete quick;
00389   quick= NULL;
00390 
00391   if (free_cond)
00392   {
00393     free_cond= 0;
00394     delete cond;
00395     cond= 0;
00396   }
00397   file->close_cached_file();
00398 }
00399 
00400 
00401 optimizer::SqlSelect::~SqlSelect()
00402 {
00403   cleanup();
00404 }
00405 
00406 
00407 bool optimizer::SqlSelect::check_quick(Session *session,
00408                                        bool force_quick_range,
00409                                        ha_rows limit)
00410 {
00411   key_map tmp;
00412   tmp.set();
00413   return (test_quick_select(session,
00414                            tmp,
00415                            0,
00416                            limit,
00417                            force_quick_range,
00418                            false) < 0);
00419 }
00420 
00421 
00422 bool optimizer::SqlSelect::skip_record()
00423 {
00424   return (cond ? cond->val_int() == 0 : 0);
00425 }
00426 
00427 
00428 optimizer::QuickSelectInterface::QuickSelectInterface()
00429   :
00430     max_used_key_length(0),
00431     used_key_parts(0)
00432 {}
00433 
00434 
00435 /*
00436   Find the best index to retrieve first N records in given order
00437 
00438   SYNOPSIS
00439     get_index_for_order()
00440       table  Table to be accessed
00441       order  Required ordering
00442       limit  Number of records that will be retrieved
00443 
00444   DESCRIPTION
00445     Find the best index that allows to retrieve first #limit records in the
00446     given order cheaper then one would retrieve them using full table scan.
00447 
00448   IMPLEMENTATION
00449     Run through all table indexes and find the shortest index that allows
00450     records to be retrieved in given order. We look for the shortest index
00451     as we will have fewer index pages to read with it.
00452 
00453     This function is used only by UPDATE/DELETE, so we take into account how
00454     the UPDATE/DELETE code will work:
00455      * index can only be scanned in forward direction
00456      * HA_EXTRA_KEYREAD will not be used
00457     Perhaps these assumptions could be relaxed.
00458 
00459   RETURN
00460     Number of the index that produces the required ordering in the cheapest way
00461     MAX_KEY if no such index was found.
00462 */
00463 
00464 uint32_t optimizer::get_index_for_order(Table *table, Order *order, ha_rows limit)
00465 {
00466   uint32_t idx;
00467   uint32_t match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
00468   Order *ord;
00469 
00470   for (ord= order; ord; ord= ord->next)
00471     if (!ord->asc)
00472       return MAX_KEY;
00473 
00474   for (idx= 0; idx < table->getShare()->sizeKeys(); idx++)
00475   {
00476     if (!(table->keys_in_use_for_query.test(idx)))
00477       continue;
00478     KeyPartInfo *keyinfo= table->key_info[idx].key_part;
00479     uint32_t n_parts=  table->key_info[idx].key_parts;
00480     uint32_t partno= 0;
00481 
00482     /*
00483       The below check is sufficient considering we now have either BTREE
00484       indexes (records are returned in order for any index prefix) or HASH
00485       indexes (records are not returned in order for any index prefix).
00486     */
00487     if (! (table->index_flags(idx) & HA_READ_ORDER))
00488       continue;
00489     for (ord= order; ord && partno < n_parts; ord= ord->next, partno++)
00490     {
00491       Item *item= order->item[0];
00492       if (! (item->type() == Item::FIELD_ITEM &&
00493            ((Item_field*)item)->field->eq(keyinfo[partno].field)))
00494         break;
00495     }
00496 
00497     if (! ord && table->key_info[idx].key_length < match_key_len)
00498     {
00499       /*
00500         Ok, the ordering is compatible and this key is shorter then
00501         previous match (we want shorter keys as we'll have to read fewer
00502         index pages for the same number of records)
00503       */
00504       match_key= idx;
00505       match_key_len= table->key_info[idx].key_length;
00506     }
00507   }
00508 
00509   if (match_key != MAX_KEY)
00510   {
00511     /*
00512       Found an index that allows records to be retrieved in the requested
00513       order. Now we'll check if using the index is cheaper then doing a table
00514       scan.
00515     */
00516     double full_scan_time= table->cursor->scan_time();
00517     double index_scan_time= table->cursor->read_time(match_key, 1, limit);
00518     if (index_scan_time > full_scan_time)
00519       match_key= MAX_KEY;
00520   }
00521   return match_key;
00522 }
00523 
00524 
00525 
00526 /*
00527   Fill param->needed_fields with bitmap of fields used in the query.
00528   SYNOPSIS
00529     fill_used_fields_bitmap()
00530       param Parameter from test_quick_select function.
00531 
00532   NOTES
00533     Clustered PK members are not put into the bitmap as they are implicitly
00534     present in all keys (and it is impossible to avoid reading them).
00535   RETURN
00536     0  Ok
00537     1  Out of memory.
00538 */
00539 
00540 static int fill_used_fields_bitmap(optimizer::Parameter *param)
00541 {
00542   Table *table= param->table;
00543   uint32_t pk;
00544   param->tmp_covered_fields.clear();
00545   param->needed_fields.resize(table->getShare()->sizeFields());
00546   param->needed_fields.reset();
00547 
00548   param->needed_fields|= *table->read_set;
00549   param->needed_fields|= *table->write_set;
00550 
00551   pk= param->table->getShare()->getPrimaryKey();
00552   if (pk != MAX_KEY && param->table->cursor->primary_key_is_clustered())
00553   {
00554     /* The table uses clustered PK and it is not internally generated */
00555     KeyPartInfo *key_part= param->table->key_info[pk].key_part;
00556     KeyPartInfo *key_part_end= key_part +
00557                                  param->table->key_info[pk].key_parts;
00558     for (;key_part != key_part_end; ++key_part)
00559       param->needed_fields.reset(key_part->fieldnr-1);
00560   }
00561   return 0;
00562 }
00563 
00564 
00565 /*
00566   Test if a key can be used in different ranges
00567 
00568   SYNOPSIS
00569     SqlSelect::test_quick_select()
00570       session               Current thread
00571       keys_to_use       Keys to use for range retrieval
00572       prev_tables       Tables assumed to be already read when the scan is
00573                         performed (but not read at the moment of this call)
00574       limit             Query limit
00575       force_quick_range Prefer to use range (instead of full table scan) even
00576                         if it is more expensive.
00577 
00578   NOTES
00579     Updates the following in the select parameter:
00580       needed_reg - Bits for keys with may be used if all prev regs are read
00581       quick      - Parameter to use when reading records.
00582 
00583     In the table struct the following information is updated:
00584       quick_keys           - Which keys can be used
00585       quick_rows           - How many rows the key matches
00586       quick_condition_rows - E(# rows that will satisfy the table condition)
00587 
00588   IMPLEMENTATION
00589     quick_condition_rows value is obtained as follows:
00590 
00591       It is a minimum of E(#output rows) for all considered table access
00592       methods (range and index_merge accesses over various indexes).
00593 
00594     The obtained value is not a true E(#rows that satisfy table condition)
00595     but rather a pessimistic estimate. To obtain a true E(#...) one would
00596     need to combine estimates of various access methods, taking into account
00597     correlations between sets of rows they will return.
00598 
00599     For example, if values of tbl.key1 and tbl.key2 are independent (a right
00600     assumption if we have no information about their correlation) then the
00601     correct estimate will be:
00602 
00603       E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) =
00604       = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
00605 
00606     which is smaller than
00607 
00608        MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))
00609 
00610     which is currently produced.
00611 
00612   TODO
00613    * Change the value returned in quick_condition_rows from a pessimistic
00614      estimate to true E(#rows that satisfy table condition).
00615      (we can re-use some of E(#rows) calcuation code from index_merge/intersection
00616       for this)
00617 
00618    * Check if this function really needs to modify keys_to_use, and change the
00619      code to pass it by reference if it doesn't.
00620 
00621    * In addition to force_quick_range other means can be (an usually are) used
00622      to make this function prefer range over full table scan. Figure out if
00623      force_quick_range is really needed.
00624 
00625   RETURN
00626    -1 if impossible select (i.e. certainly no rows will be selected)
00627     0 if can't use quick_select
00628     1 if found usable ranges and quick select has been successfully created.
00629 */
00630 
00631 int optimizer::SqlSelect::test_quick_select(Session *session,
00632                                             key_map keys_to_use,
00633                                             table_map prev_tables,
00634                                             ha_rows limit,
00635                                             bool force_quick_range,
00636                                             bool ordered_output)
00637 {
00638   uint32_t idx;
00639   double scan_time;
00640 
00641   delete quick;
00642   quick= NULL;
00643 
00644   needed_reg.reset();
00645   quick_keys.reset();
00646   if (keys_to_use.none())
00647     return 0;
00648   records= head->cursor->stats.records;
00649   if (!records)
00650     records++;
00651   scan_time= (double) records / TIME_FOR_COMPARE + 1;
00652   read_time= (double) head->cursor->scan_time() + scan_time + 1.1;
00653   if (head->force_index)
00654     scan_time= read_time= DBL_MAX;
00655   if (limit < records)
00656     read_time= (double) records + scan_time + 1; // Force to use index
00657   else if (read_time <= 2.0 && !force_quick_range)
00658     return 0;       /* No need for quick select */
00659 
00660   keys_to_use&= head->keys_in_use_for_query;
00661   if (keys_to_use.any())
00662   {
00663     memory::Root alloc;
00664     optimizer::SEL_TREE *tree= NULL;
00665     KEY_PART *key_parts;
00666     KeyInfo *key_info;
00667     optimizer::Parameter param;
00668 
00669     if (check_stack_overrun(session, 2*STACK_MIN_SIZE, NULL))
00670       return 0;                           // Fatal error flag is set
00671 
00672     /* set up parameter that is passed to all functions */
00673     param.session= session;
00674     param.prev_tables= prev_tables | const_tables;
00675     param.read_tables= read_tables;
00676     param.current_table= head->map;
00677     param.table=head;
00678     param.keys=0;
00679     param.mem_root= &alloc;
00680     param.old_root= session->mem_root;
00681     param.needed_reg= &needed_reg;
00682     param.imerge_cost_buff_size= 0;
00683     param.using_real_indexes= true;
00684     param.remove_jump_scans= true;
00685     param.force_default_mrr= ordered_output;
00686 
00687     session->no_errors=1;       // Don't warn about NULL
00688     alloc.init(session->variables.range_alloc_block_size);
00689     param.key_parts= new (alloc) KEY_PART[head->getShare()->key_parts];
00690     if (fill_used_fields_bitmap(&param))
00691     {
00692       session->no_errors=0;
00693       alloc.free_root(MYF(0));      // Return memory & allocator
00694 
00695       return 0;       // Can't use range
00696     }
00697     key_parts= param.key_parts;
00698     session->mem_root= &alloc;
00699 
00700     /*
00701       Make an array with description of all key parts of all table keys.
00702       This is used in get_mm_parts function.
00703     */
00704     key_info= head->key_info;
00705     for (idx=0 ; idx < head->getShare()->sizeKeys() ; idx++, key_info++)
00706     {
00707       KeyPartInfo *key_part_info;
00708       if (! keys_to_use.test(idx))
00709   continue;
00710 
00711       param.key[param.keys]=key_parts;
00712       key_part_info= key_info->key_part;
00713       for (uint32_t part=0;
00714            part < key_info->key_parts;
00715            part++, key_parts++, key_part_info++)
00716       {
00717         key_parts->key= param.keys;
00718         key_parts->part= part;
00719         key_parts->length= key_part_info->length;
00720         key_parts->store_length= key_part_info->store_length;
00721         key_parts->field= key_part_info->field;
00722         key_parts->null_bit= key_part_info->null_bit;
00723         /* Only HA_PART_KEY_SEG is used */
00724         key_parts->flag= (uint8_t) key_part_info->key_part_flag;
00725       }
00726       param.real_keynr[param.keys++]=idx;
00727     }
00728     param.key_parts_end=key_parts;
00729     param.alloced_sel_args= 0;
00730 
00731     /* Calculate cost of full index read for the shortest covering index */
00732     if (!head->covering_keys.none())
00733     {
00734       int key_for_use= head->find_shortest_key(&head->covering_keys);
00735       double key_read_time=
00736         param.table->cursor->index_only_read_time(key_for_use, records) +
00737         (double) records / TIME_FOR_COMPARE;
00738       if (key_read_time < read_time)
00739         read_time= key_read_time;
00740     }
00741 
00742     optimizer::TableReadPlan *best_trp= NULL;
00743     optimizer::GroupMinMaxReadPlan *group_trp= NULL;
00744     double best_read_time= read_time;
00745 
00746     if (cond)
00747     {
00748       if ((tree= get_mm_tree(&param,cond)))
00749       {
00750         if (tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
00751         {
00752           records=0L;                      /* Return -1 from this function. */
00753           read_time= (double) HA_POS_ERROR;
00754           goto free_mem;
00755         }
00756         /*
00757           If the tree can't be used for range scans, proceed anyway, as we
00758           can construct a group-min-max quick select
00759         */
00760         if (tree->type != optimizer::SEL_TREE::KEY && tree->type != optimizer::SEL_TREE::KEY_SMALLER)
00761           tree= NULL;
00762       }
00763     }
00764 
00765     /*
00766       Try to construct a QuickGroupMinMaxSelect.
00767       Notice that it can be constructed no matter if there is a range tree.
00768     */
00769     group_trp= get_best_group_min_max(&param, tree);
00770     if (group_trp)
00771     {
00772       param.table->quick_condition_rows= min(group_trp->records,
00773                                              head->cursor->stats.records);
00774       if (group_trp->read_cost < best_read_time)
00775       {
00776         best_trp= group_trp;
00777         best_read_time= best_trp->read_cost;
00778       }
00779     }
00780 
00781     if (tree)
00782     {
00783       /*
00784         It is possible to use a range-based quick select (but it might be
00785         slower than 'all' table scan).
00786       */
00787       if (tree->merges.is_empty())
00788       {
00789         optimizer::RangeReadPlan *range_trp= NULL;
00790         optimizer::RorIntersectReadPlan *rori_trp= NULL;
00791         bool can_build_covering= false;
00792 
00793         /* Get best 'range' plan and prepare data for making other plans */
00794         if ((range_trp= get_key_scans_params(session, &param, tree, false, true,
00795                                              best_read_time)))
00796         {
00797           best_trp= range_trp;
00798           best_read_time= best_trp->read_cost;
00799         }
00800 
00801         /*
00802           Simultaneous key scans and row deletes on several Cursor
00803           objects are not allowed so don't use ROR-intersection for
00804           table deletes.
00805         */
00806         if ((session->lex().sql_command != SQLCOM_DELETE))
00807         {
00808           /*
00809             Get best non-covering ROR-intersection plan and prepare data for
00810             building covering ROR-intersection.
00811           */
00812           if ((rori_trp= get_best_ror_intersect(&param, tree, best_read_time,
00813                                                 &can_build_covering)))
00814           {
00815             best_trp= rori_trp;
00816             best_read_time= best_trp->read_cost;
00817             /*
00818               Try constructing covering ROR-intersect only if it looks possible
00819               and worth doing.
00820             */
00821             if (!rori_trp->is_covering && can_build_covering &&
00822                 (rori_trp= get_best_covering_ror_intersect(&param, tree,
00823                                                            best_read_time)))
00824               best_trp= rori_trp;
00825           }
00826         }
00827       }
00828       else
00829       {
00830         /* Try creating index_merge/ROR-union scan. */
00831         optimizer::SEL_IMERGE *imerge= NULL;
00832         optimizer::TableReadPlan *best_conj_trp= NULL;
00833         optimizer::TableReadPlan *new_conj_trp= NULL;
00834         List<optimizer::SEL_IMERGE>::iterator it(tree->merges.begin());
00835         while ((imerge= it++))
00836         {
00837           new_conj_trp= get_best_disjunct_quick(session, &param, imerge, best_read_time);
00838           if (new_conj_trp)
00839             set_if_smaller(param.table->quick_condition_rows,
00840                            new_conj_trp->records);
00841           if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
00842                                  best_conj_trp->read_cost))
00843             best_conj_trp= new_conj_trp;
00844         }
00845         if (best_conj_trp)
00846           best_trp= best_conj_trp;
00847       }
00848     }
00849 
00850     session->mem_root= param.old_root;
00851 
00852     /* If we got a read plan, create a quick select from it. */
00853     if (best_trp)
00854     {
00855       records= best_trp->records;
00856       if (! (quick= best_trp->make_quick(&param, true)) || quick->init())
00857       {
00858         delete quick;
00859         quick= NULL;
00860       }
00861     }
00862 
00863   free_mem:
00864     alloc.free_root(MYF(0));      // Return memory & allocator
00865     session->mem_root= param.old_root;
00866     session->no_errors=0;
00867   }
00868 
00869   /*
00870     Assume that if the user is using 'limit' we will only need to scan
00871     limit rows if we are using a key
00872   */
00873   return(records ? test(quick) : -1);
00874 }
00875 
00876 /*
00877   Get best plan for a optimizer::SEL_IMERGE disjunctive expression.
00878   SYNOPSIS
00879     get_best_disjunct_quick()
00880       session
00881       param     Parameter from check_quick_select function
00882       imerge    Expression to use
00883       read_time Don't create scans with cost > read_time
00884 
00885   NOTES
00886     index_merge cost is calculated as follows:
00887     index_merge_cost =
00888       cost(index_reads) +         (see #1)
00889       cost(rowid_to_row_scan) +   (see #2)
00890       cost(unique_use)            (see #3)
00891 
00892     1. cost(index_reads) =SUM_i(cost(index_read_i))
00893        For non-CPK scans,
00894          cost(index_read_i) = {cost of ordinary 'index only' scan}
00895        For CPK scan,
00896          cost(index_read_i) = {cost of non-'index only' scan}
00897 
00898     2. cost(rowid_to_row_scan)
00899       If table PK is clustered then
00900         cost(rowid_to_row_scan) =
00901           {cost of ordinary clustered PK scan with n_ranges=n_rows}
00902 
00903       Otherwise, we use the following model to calculate costs:
00904       We need to retrieve n_rows rows from cursor that occupies n_blocks blocks.
00905       We assume that offsets of rows we need are independent variates with
00906       uniform distribution in [0..max_file_offset] range.
00907 
00908       We'll denote block as "busy" if it contains row(s) we need to retrieve
00909       and "empty" if doesn't contain rows we need.
00910 
00911       Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
00912       applies to any block in cursor). Let x_i be a variate taking value 1 if
00913       block #i is empty and 0 otherwise.
00914 
00915       Then E(x_i) = (1 - 1/n_blocks)^n_rows;
00916 
00917       E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
00918         = n_blocks * ((1 - 1/n_blocks)^n_rows) =
00919        ~= n_blocks * exp(-n_rows/n_blocks).
00920 
00921       E(n_busy_blocks) = n_blocks*(1 - (1 - 1/n_blocks)^n_rows) =
00922        ~= n_blocks * (1 - exp(-n_rows/n_blocks)).
00923 
00924       Average size of "hole" between neighbor non-empty blocks is
00925            E(hole_size) = n_blocks/E(n_busy_blocks).
00926 
00927       The total cost of reading all needed blocks in one "sweep" is:
00928 
00929       E(n_busy_blocks)*
00930        (DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*n_blocks/E(n_busy_blocks)).
00931 
00932     3. Cost of Unique use is calculated in Unique::get_use_cost function.
00933 
00934   ROR-union cost is calculated in the same way index_merge, but instead of
00935   Unique a priority queue is used.
00936 
00937   RETURN
00938     Created read plan
00939     NULL - Out of memory or no read scan could be built.
00940 */
00941 
00942 static
00943 optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
00944                                                   optimizer::Parameter *param,
00945                                                   optimizer::SEL_IMERGE *imerge,
00946                                                   double read_time)
00947 {
00948   optimizer::SEL_TREE **ptree= NULL;
00949   optimizer::IndexMergeReadPlan *imerge_trp= NULL;
00950   uint32_t n_child_scans= imerge->trees_next - imerge->trees;
00951   optimizer::RangeReadPlan **range_scans= NULL;
00952   optimizer::RangeReadPlan **cur_child= NULL;
00953   optimizer::RangeReadPlan **cpk_scan= NULL;
00954   bool imerge_too_expensive= false;
00955   double imerge_cost= 0.0;
00956   ha_rows cpk_scan_records= 0;
00957   ha_rows non_cpk_scan_records= 0;
00958   bool pk_is_clustered= param->table->cursor->primary_key_is_clustered();
00959   bool all_scans_ror_able= true;
00960   bool all_scans_rors= true;
00961   uint32_t unique_calc_buff_size;
00962   optimizer::TableReadPlan **roru_read_plans= NULL;
00963   optimizer::TableReadPlan **cur_roru_plan= NULL;
00964   double roru_index_costs;
00965   ha_rows roru_total_records;
00966   double roru_intersect_part= 1.0;
00967 
00968   range_scans= new (*param->mem_root) optimizer::RangeReadPlan*[n_child_scans];
00969 
00970   /*
00971     Collect best 'range' scan for each of disjuncts, and, while doing so,
00972     analyze possibility of ROR scans. Also calculate some values needed by
00973     other parts of the code.
00974   */
00975   for (ptree= imerge->trees, cur_child= range_scans; ptree != imerge->trees_next; ptree++, cur_child++)
00976   {
00977     if (!(*cur_child= get_key_scans_params(session, param, *ptree, true, false, read_time)))
00978     {
00979       /*
00980         One of index scans in this index_merge is more expensive than entire
00981         table read for another available option. The entire index_merge (and
00982         any possible ROR-union) will be more expensive then, too. We continue
00983         here only to update SqlSelect members.
00984       */
00985       imerge_too_expensive= true;
00986     }
00987     if (imerge_too_expensive)
00988       continue;
00989 
00990     imerge_cost += (*cur_child)->read_cost;
00991     all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
00992     all_scans_rors &= (*cur_child)->is_ror;
00993     if (pk_is_clustered &&
00994         param->real_keynr[(*cur_child)->key_idx] ==
00995         param->table->getShare()->getPrimaryKey())
00996     {
00997       cpk_scan= cur_child;
00998       cpk_scan_records= (*cur_child)->records;
00999     }
01000     else
01001       non_cpk_scan_records += (*cur_child)->records;
01002   }
01003 
01004   if (imerge_too_expensive || (imerge_cost > read_time) ||
01005       ((non_cpk_scan_records+cpk_scan_records >= param->table->cursor->stats.records) && read_time != DBL_MAX))
01006   {
01007     /*
01008       Bail out if it is obvious that both index_merge and ROR-union will be
01009       more expensive
01010     */
01011     return NULL;
01012   }
01013   if (all_scans_rors)
01014   {
01015     roru_read_plans= (optimizer::TableReadPlan **) range_scans;
01016     goto skip_to_ror_scan;
01017   }
01018   if (cpk_scan)
01019   {
01020     /*
01021       Add one ROWID comparison for each row retrieved on non-CPK scan.  (it
01022       is done in QuickRangeSelect::row_in_ranges)
01023      */
01024     imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
01025   }
01026 
01027   /* Calculate cost(rowid_to_row_scan) */
01028   {
01029     optimizer::CostVector sweep_cost;
01030     Join *join= param->session->lex().select_lex.join;
01031     bool is_interrupted= test(join && join->tables == 1);
01032     get_sweep_read_cost(param->table, non_cpk_scan_records, is_interrupted,
01033                         &sweep_cost);
01034     imerge_cost += sweep_cost.total_cost();
01035   }
01036   if (imerge_cost > read_time)
01037     goto build_ror_index_merge;
01038 
01039   /* Add Unique operations cost */
01040   unique_calc_buff_size=
01041     Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
01042                                     param->table->cursor->ref_length,
01043                                     param->session->variables.sortbuff_size);
01044   if (param->imerge_cost_buff_size < unique_calc_buff_size)
01045   {
01046     param->imerge_cost_buff= (uint*)param->mem_root->alloc(unique_calc_buff_size);
01047     param->imerge_cost_buff_size= unique_calc_buff_size;
01048   }
01049 
01050   imerge_cost +=
01051     Unique::get_use_cost(param->imerge_cost_buff, (uint32_t)non_cpk_scan_records,
01052                          param->table->cursor->ref_length,
01053                          param->session->variables.sortbuff_size);
01054   if (imerge_cost < read_time)
01055   {
01056     imerge_trp= new (*param->mem_root) optimizer::IndexMergeReadPlan;
01057     imerge_trp->read_cost= imerge_cost;
01058     imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
01059     imerge_trp->records= min(imerge_trp->records, param->table->cursor->stats.records);
01060     imerge_trp->range_scans= range_scans;
01061     imerge_trp->range_scans_end= range_scans + n_child_scans;
01062     read_time= imerge_cost;
01063   }
01064 
01065 build_ror_index_merge:
01066   if (!all_scans_ror_able || param->session->lex().sql_command == SQLCOM_DELETE)
01067     return(imerge_trp);
01068 
01069   /* Ok, it is possible to build a ROR-union, try it. */
01070   bool dummy;
01071   roru_read_plans= new (*param->mem_root) optimizer::TableReadPlan*[n_child_scans];
01072 skip_to_ror_scan:
01073   roru_index_costs= 0.0;
01074   roru_total_records= 0;
01075   cur_roru_plan= roru_read_plans;
01076 
01077   /* Find 'best' ROR scan for each of trees in disjunction */
01078   for (ptree= imerge->trees, cur_child= range_scans;
01079        ptree != imerge->trees_next;
01080        ptree++, cur_child++, cur_roru_plan++)
01081   {
01082     /*
01083       Assume the best ROR scan is the one that has cheapest full-row-retrieval
01084       scan cost.
01085       Also accumulate index_only scan costs as we'll need them to calculate
01086       overall index_intersection cost.
01087     */
01088     double cost;
01089     if ((*cur_child)->is_ror)
01090     {
01091       /* Ok, we have index_only cost, now get full rows scan cost */
01092       cost= param->table->cursor->
01093               read_time(param->real_keynr[(*cur_child)->key_idx], 1,
01094                         (*cur_child)->records) +
01095               static_cast<double>((*cur_child)->records) / TIME_FOR_COMPARE;
01096     }
01097     else
01098       cost= read_time;
01099 
01100     optimizer::TableReadPlan *prev_plan= *cur_child;
01101     if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
01102                                                  &dummy)))
01103     {
01104       if (prev_plan->is_ror)
01105         *cur_roru_plan= prev_plan;
01106       else
01107         return(imerge_trp);
01108       roru_index_costs += (*cur_roru_plan)->read_cost;
01109     }
01110     else
01111       roru_index_costs +=
01112         ((optimizer::RorIntersectReadPlan*)(*cur_roru_plan))->index_scan_costs;
01113     roru_total_records += (*cur_roru_plan)->records;
01114     roru_intersect_part *= (*cur_roru_plan)->records /
01115                            param->table->cursor->stats.records;
01116   }
01117 
01118   /*
01119     rows to retrieve=
01120       SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
01121     This is valid because index_merge construction guarantees that conditions
01122     in disjunction do not share key parts.
01123   */
01124   roru_total_records -= (ha_rows)(roru_intersect_part*
01125                                   param->table->cursor->stats.records);
01126   /* ok, got a ROR read plan for each of the disjuncts
01127     Calculate cost:
01128     cost(index_union_scan(scan_1, ... scan_n)) =
01129       SUM_i(cost_of_index_only_scan(scan_i)) +
01130       queue_use_cost(rowid_len, n) +
01131       cost_of_row_retrieval
01132     See get_merge_buffers_cost function for queue_use_cost formula derivation.
01133   */
01134   double roru_total_cost;
01135   {
01136     optimizer::CostVector sweep_cost;
01137     Join *join= param->session->lex().select_lex.join;
01138     bool is_interrupted= test(join && join->tables == 1);
01139     get_sweep_read_cost(param->table, roru_total_records, is_interrupted,
01140                         &sweep_cost);
01141     roru_total_cost= roru_index_costs +
01142                      static_cast<double>(roru_total_records)*log((double)n_child_scans) /
01143                      (TIME_FOR_COMPARE_ROWID * M_LN2) +
01144                      sweep_cost.total_cost();
01145   }
01146 
01147   optimizer::RorUnionReadPlan *roru= NULL;
01148   if (roru_total_cost < read_time)
01149   {
01150     if ((roru= new (*param->mem_root) optimizer::RorUnionReadPlan))
01151     {
01152       roru->first_ror= roru_read_plans;
01153       roru->last_ror= roru_read_plans + n_child_scans;
01154       roru->read_cost= roru_total_cost;
01155       roru->records= roru_total_records;
01156       return roru;
01157     }
01158   }
01159   return(imerge_trp);
01160 }
01161 
01162 
01163 
01164 /*
01165   Create optimizer::RorScanInfo* structure with a single ROR scan on index idx using
01166   sel_arg set of intervals.
01167 
01168   SYNOPSIS
01169     make_ror_scan()
01170       param    Parameter from test_quick_select function
01171       idx      Index of key in param->keys
01172       sel_arg  Set of intervals for a given key
01173 
01174   RETURN
01175     NULL - out of memory
01176     ROR scan structure containing a scan for {idx, sel_arg}
01177 */
01178 
01179 static
01180 optimizer::RorScanInfo *make_ror_scan(const optimizer::Parameter *param, int idx, optimizer::SEL_ARG *sel_arg)
01181 {
01182   uint32_t keynr;
01183   optimizer::RorScanInfo* ror_scan= new (*param->mem_root) optimizer::RorScanInfo;
01184 
01185   ror_scan->idx= idx;
01186   ror_scan->keynr= keynr= param->real_keynr[idx];
01187   ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
01188                              param->table->cursor->ref_length);
01189   ror_scan->sel_arg= sel_arg;
01190   ror_scan->records= param->table->quick_rows[keynr];
01191 
01192   ror_scan->covered_fields_size= param->table->getShare()->sizeFields();
01193   boost::dynamic_bitset<> tmp_bitset(param->table->getShare()->sizeFields());
01194   tmp_bitset.reset();
01195 
01196   KeyPartInfo *key_part= param->table->key_info[keynr].key_part;
01197   KeyPartInfo *key_part_end= key_part +
01198                                param->table->key_info[keynr].key_parts;
01199   for (; key_part != key_part_end; ++key_part)
01200   {
01201     if (param->needed_fields.test(key_part->fieldnr-1))
01202       tmp_bitset.set(key_part->fieldnr-1);
01203   }
01204   double rows= param->table->quick_rows[ror_scan->keynr];
01205   ror_scan->index_read_cost=
01206     param->table->cursor->index_only_read_time(ror_scan->keynr, rows);
01207   ror_scan->covered_fields= tmp_bitset.to_ulong();
01208   return ror_scan;
01209 }
01210 
01211 
01212 /*
01213   Compare two optimizer::RorScanInfo** by  E(#records_matched) * key_record_length.
01214   SYNOPSIS
01215     cmp_ror_scan_info()
01216       a ptr to first compared value
01217       b ptr to second compared value
01218 
01219   RETURN
01220    -1 a < b
01221     0 a = b
01222     1 a > b
01223 */
01224 
01225 static int cmp_ror_scan_info(optimizer::RorScanInfo** a, optimizer::RorScanInfo** b)
01226 {
01227   double val1= static_cast<double>((*a)->records) * (*a)->key_rec_length;
01228   double val2= static_cast<double>((*b)->records) * (*b)->key_rec_length;
01229   return (val1 < val2)? -1: (val1 == val2)? 0 : 1;
01230 }
01231 
01232 
01233 /*
01234   Compare two optimizer::RorScanInfo** by
01235    (#covered fields in F desc,
01236     #components asc,
01237     number of first not covered component asc)
01238 
01239   SYNOPSIS
01240     cmp_ror_scan_info_covering()
01241       a ptr to first compared value
01242       b ptr to second compared value
01243 
01244   RETURN
01245    -1 a < b
01246     0 a = b
01247     1 a > b
01248 */
01249 
01250 static int cmp_ror_scan_info_covering(optimizer::RorScanInfo** a, optimizer::RorScanInfo** b)
01251 {
01252   if ((*a)->used_fields_covered > (*b)->used_fields_covered)
01253     return -1;
01254   if ((*a)->used_fields_covered < (*b)->used_fields_covered)
01255     return 1;
01256   if ((*a)->key_components < (*b)->key_components)
01257     return -1;
01258   if ((*a)->key_components > (*b)->key_components)
01259     return 1;
01260   if ((*a)->first_uncovered_field < (*b)->first_uncovered_field)
01261     return -1;
01262   if ((*a)->first_uncovered_field > (*b)->first_uncovered_field)
01263     return 1;
01264   return 0;
01265 }
01266 
01267 /* Auxiliary structure for incremental ROR-intersection creation */
01268 typedef struct st_ror_intersect_info
01269 {
01270   st_ror_intersect_info()
01271     :
01272       param(NULL),
01273       covered_fields(),
01274       out_rows(0.0),
01275       is_covering(false),
01276       index_records(0),
01277       index_scan_costs(0.0),
01278       total_cost(0.0)
01279   {}
01280 
01281   st_ror_intersect_info(const optimizer::Parameter *in_param)
01282     :
01283       param(in_param),
01284       covered_fields(in_param->table->getShare()->sizeFields()),
01285       out_rows(in_param->table->cursor->stats.records),
01286       is_covering(false),
01287       index_records(0),
01288       index_scan_costs(0.0),
01289       total_cost(0.0)
01290   {
01291     covered_fields.reset();
01292   }
01293 
01294   const optimizer::Parameter *param;
01295   boost::dynamic_bitset<> covered_fields; /* union of fields covered by all scans */
01296   /*
01297     Fraction of table records that satisfies conditions of all scans.
01298     This is the number of full records that will be retrieved if a
01299     non-index_only index intersection will be employed.
01300   */
01301   double out_rows;
01302   /* true if covered_fields is a superset of needed_fields */
01303   bool is_covering;
01304 
01305   ha_rows index_records; /* sum(#records to look in indexes) */
01306   double index_scan_costs; /* SUM(cost of 'index-only' scans) */
01307   double total_cost;
01308 } ROR_INTERSECT_INFO;
01309 
01310 
01311 static void ror_intersect_cpy(ROR_INTERSECT_INFO *dst,
01312                               const ROR_INTERSECT_INFO *src)
01313 {
01314   dst->param= src->param;
01315   dst->covered_fields= src->covered_fields;
01316   dst->out_rows= src->out_rows;
01317   dst->is_covering= src->is_covering;
01318   dst->index_records= src->index_records;
01319   dst->index_scan_costs= src->index_scan_costs;
01320   dst->total_cost= src->total_cost;
01321 }
01322 
01323 
01324 /*
01325   Get selectivity of a ROR scan wrt ROR-intersection.
01326 
01327   SYNOPSIS
01328     ror_scan_selectivity()
01329       info  ROR-interection
01330       scan  ROR scan
01331 
01332   NOTES
01333     Suppose we have a condition on several keys
01334     cond=k_11=c_11 AND k_12=c_12 AND ...  // parts of first key
01335          k_21=c_21 AND k_22=c_22 AND ...  // parts of second key
01336           ...
01337          k_n1=c_n1 AND k_n3=c_n3 AND ...  (1) //parts of the key used by *scan
01338 
01339     where k_ij may be the same as any k_pq (i.e. keys may have common parts).
01340 
01341     A full row is retrieved if entire condition holds.
01342 
01343     The recursive procedure for finding P(cond) is as follows:
01344 
01345     First step:
01346     Pick 1st part of 1st key and break conjunction (1) into two parts:
01347       cond= (k_11=c_11 AND R)
01348 
01349     Here R may still contain condition(s) equivalent to k_11=c_11.
01350     Nevertheless, the following holds:
01351 
01352       P(k_11=c_11 AND R) = P(k_11=c_11) * P(R | k_11=c_11).
01353 
01354     Mark k_11 as fixed field (and satisfied condition) F, save P(F),
01355     save R to be cond and proceed to recursion step.
01356 
01357     Recursion step:
01358     We have a set of fixed fields/satisfied conditions) F, probability P(F),
01359     and remaining conjunction R
01360     Pick next key part on current key and its condition "k_ij=c_ij".
01361     We will add "k_ij=c_ij" into F and update P(F).
01362     Lets denote k_ij as t,  R = t AND R1, where R1 may still contain t. Then
01363 
01364      P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)
01365 
01366     (where '|' mean conditional probability, not "or")
01367 
01368     Consider the first multiplier in (2). One of the following holds:
01369     a) F contains condition on field used in t (i.e. t AND F = F).
01370       Then P(t|F) = 1
01371 
01372     b) F doesn't contain condition on field used in t. Then F and t are
01373      considered independent.
01374 
01375      P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
01376           = P(t|fields_before_t_in_key).
01377 
01378      P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
01379                                    #records(fields_before_t_in_key, t)
01380 
01381     The second multiplier is calculated by applying this step recursively.
01382 
01383   IMPLEMENTATION
01384     This function calculates the result of application of the "recursion step"
01385     described above for all fixed key members of a single key, accumulating set
01386     of covered fields, selectivity, etc.
01387 
01388     The calculation is conducted as follows:
01389     Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate
01390 
01391      n_{k1}      n_{k2}
01392     --------- * ---------  * .... (3)
01393      n_{k1-1}    n_{k2-1}
01394 
01395     where k1,k2,... are key parts which fields were not yet marked as fixed
01396     ( this is result of application of option b) of the recursion step for
01397       parts of a single key).
01398     Since it is reasonable to expect that most of the fields are not marked
01399     as fixed, we calculate (3) as
01400 
01401                                   n_{i1}      n_{i2}
01402     (3) = n_{max_key_part}  / (   --------- * ---------  * ....  )
01403                                   n_{i1-1}    n_{i2-1}
01404 
01405     where i1,i2, .. are key parts that were already marked as fixed.
01406 
01407     In order to minimize number of expensive records_in_range calls we group
01408     and reduce adjacent fractions.
01409 
01410   RETURN
01411     Selectivity of given ROR scan.
01412 */
01413 
01414 static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
01415                                    const optimizer::RorScanInfo *scan)
01416 {
01417   double selectivity_mult= 1.0;
01418   KeyPartInfo *key_part= info->param->table->key_info[scan->keynr].key_part;
01419   unsigned char key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
01420   unsigned char *key_ptr= key_val;
01421   optimizer::SEL_ARG *sel_arg= NULL;
01422   optimizer::SEL_ARG *tuple_arg= NULL;
01423   key_part_map keypart_map= 0;
01424   bool cur_covered;
01425   bool prev_covered= test(info->covered_fields.test(key_part->fieldnr-1));
01426   key_range min_range;
01427   key_range max_range;
01428   min_range.key= key_val;
01429   min_range.flag= HA_READ_KEY_EXACT;
01430   max_range.key= key_val;
01431   max_range.flag= HA_READ_AFTER_KEY;
01432   ha_rows prev_records= info->param->table->cursor->stats.records;
01433 
01434   for (sel_arg= scan->sel_arg; sel_arg;
01435        sel_arg= sel_arg->next_key_part)
01436   {
01437     cur_covered=
01438       test(info->covered_fields.test(key_part[sel_arg->part].fieldnr-1));
01439     if (cur_covered != prev_covered)
01440     {
01441       /* create (part1val, ..., part{n-1}val) tuple. */
01442       ha_rows records;
01443       if (!tuple_arg)
01444       {
01445         tuple_arg= scan->sel_arg;
01446         /* Here we use the length of the first key part */
01447         tuple_arg->store_min(key_part->store_length, &key_ptr, 0);
01448         keypart_map= 1;
01449       }
01450       while (tuple_arg->next_key_part != sel_arg)
01451       {
01452         tuple_arg= tuple_arg->next_key_part;
01453         tuple_arg->store_min(key_part[tuple_arg->part].store_length,
01454                              &key_ptr, 0);
01455         keypart_map= (keypart_map << 1) | 1;
01456       }
01457       min_range.length= max_range.length= (size_t) (key_ptr - key_val);
01458       min_range.keypart_map= max_range.keypart_map= keypart_map;
01459       records= (info->param->table->cursor->
01460                 records_in_range(scan->keynr, &min_range, &max_range));
01461       if (cur_covered)
01462       {
01463         /* uncovered -> covered */
01464         selectivity_mult *= static_cast<double>(records) / prev_records;
01465         prev_records= HA_POS_ERROR;
01466       }
01467       else
01468       {
01469         /* covered -> uncovered */
01470         prev_records= records;
01471       }
01472     }
01473     prev_covered= cur_covered;
01474   }
01475   if (!prev_covered)
01476   {
01477     selectivity_mult *= static_cast<double>(info->param->table->quick_rows[scan->keynr]) / prev_records;
01478   }
01479   return selectivity_mult;
01480 }
01481 
01482 
01483 /*
01484   Check if adding a ROR scan to a ROR-intersection reduces its cost of
01485   ROR-intersection and if yes, update parameters of ROR-intersection,
01486   including its cost.
01487 
01488   SYNOPSIS
01489     ror_intersect_add()
01490       param        Parameter from test_quick_select
01491       info         ROR-intersection structure to add the scan to.
01492       ror_scan     ROR scan info to add.
01493       is_cpk_scan  If true, add the scan as CPK scan (this can be inferred
01494                    from other parameters and is passed separately only to
01495                    avoid duplicating the inference code)
01496 
01497   NOTES
01498     Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
01499     intersection decreases. The cost of ROR-intersection is calculated as
01500     follows:
01501 
01502     cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval
01503 
01504     When we add a scan the first increases and the second decreases.
01505 
01506     cost_of_full_rows_retrieval=
01507       (union of indexes used covers all needed fields) ?
01508         cost_of_sweep_read(E(rows_to_retrieve), rows_in_table) :
01509         0
01510 
01511     E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
01512                            ror_scan_selectivity({scan1}, scan2) * ... *
01513                            ror_scan_selectivity({scan1,...}, scanN).
01514   RETURN
01515     true   ROR scan added to ROR-intersection, cost updated.
01516     false  It doesn't make sense to add this ROR scan to this ROR-intersection.
01517 */
01518 
01519 static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
01520                               optimizer::RorScanInfo* ror_scan, bool is_cpk_scan)
01521 {
01522   double selectivity_mult= 1.0;
01523 
01524   selectivity_mult = ror_scan_selectivity(info, ror_scan);
01525   if (selectivity_mult == 1.0)
01526   {
01527     /* Don't add this scan if it doesn't improve selectivity. */
01528     return false;
01529   }
01530 
01531   info->out_rows *= selectivity_mult;
01532 
01533   if (is_cpk_scan)
01534   {
01535     /*
01536       CPK scan is used to filter out rows. We apply filtering for
01537       each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
01538       per check this gives us:
01539     */
01540     info->index_scan_costs += static_cast<double>(info->index_records) /
01541                               TIME_FOR_COMPARE_ROWID;
01542   }
01543   else
01544   {
01545     info->index_records += info->param->table->quick_rows[ror_scan->keynr];
01546     info->index_scan_costs += ror_scan->index_read_cost;
01547     boost::dynamic_bitset<> tmp_bitset= ror_scan->bitsToBitset();
01548     info->covered_fields|= tmp_bitset;
01549     if (! info->is_covering && info->param->needed_fields.is_subset_of(info->covered_fields))
01550     {
01551       info->is_covering= true;
01552     }
01553   }
01554 
01555   info->total_cost= info->index_scan_costs;
01556   if (! info->is_covering)
01557   {
01558     optimizer::CostVector sweep_cost;
01559     Join *join= info->param->session->lex().select_lex.join;
01560     bool is_interrupted= test(join && join->tables == 1);
01561     get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
01562                         is_interrupted, &sweep_cost);
01563     info->total_cost += sweep_cost.total_cost();
01564   }
01565   return true;
01566 }
01567 
01568 
01569 /*
01570   Get best covering ROR-intersection.
01571   SYNOPSIS
01572     get_best_covering_ror_intersect()
01573       param     Parameter from test_quick_select function.
01574       tree      optimizer::SEL_TREE with sets of intervals for different keys.
01575       read_time Don't return table read plans with cost > read_time.
01576 
01577   RETURN
01578     Best covering ROR-intersection plan
01579     NULL if no plan found.
01580 
01581   NOTES
01582     get_best_ror_intersect must be called for a tree before calling this
01583     function for it.
01584     This function invalidates tree->ror_scans member values.
01585 
01586   The following approximate algorithm is used:
01587     I=set of all covering indexes
01588     F=set of all fields to cover
01589     S={}
01590 
01591     do
01592     {
01593       Order I by (#covered fields in F desc,
01594                   #components asc,
01595                   number of first not covered component asc);
01596       F=F-covered by first(I);
01597       S=S+first(I);
01598       I=I-first(I);
01599     } while F is not empty.
01600 */
01601 
01602 static
01603 optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
01604                                                             optimizer::SEL_TREE *tree,
01605                                                             double read_time)
01606 {
01607   optimizer::RorScanInfo **ror_scan_mark;
01608   optimizer::RorScanInfo **ror_scans_end= tree->ror_scans_end;
01609 
01610   for (optimizer::RorScanInfo **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
01611     (*scan)->key_components=
01612       param->table->key_info[(*scan)->keynr].key_parts;
01613 
01614   /*
01615     Run covering-ROR-search algorithm.
01616     Assume set I is [ror_scan .. ror_scans_end)
01617   */
01618 
01619   /*I=set of all covering indexes */
01620   ror_scan_mark= tree->ror_scans;
01621 
01622   boost::dynamic_bitset<> *covered_fields= &param->tmp_covered_fields;
01623   if (covered_fields->empty())
01624   {
01625     covered_fields->resize(param->table->getShare()->sizeFields());
01626   }
01627   covered_fields->reset();
01628 
01629   double total_cost= 0.0f;
01630   ha_rows records=0;
01631   bool all_covered;
01632 
01633   do
01634   {
01635     /*
01636       Update changed sorting info:
01637         #covered fields,
01638   number of first not covered component
01639       Calculate and save these values for each of remaining scans.
01640     */
01641     for (optimizer::RorScanInfo **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
01642     {
01643       /* subtract one bitset from the other */
01644       (*scan)->subtractBitset(*covered_fields);
01645       (*scan)->used_fields_covered=
01646         (*scan)->getBitCount();
01647       (*scan)->first_uncovered_field= (*scan)->findFirstNotSet();
01648     }
01649 
01650     internal::my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark,
01651                        sizeof(optimizer::RorScanInfo*),
01652                        (qsort_cmp)cmp_ror_scan_info_covering);
01653 
01654     /* I=I-first(I) */
01655     total_cost += (*ror_scan_mark)->index_read_cost;
01656     records += (*ror_scan_mark)->records;
01657     if (total_cost > read_time)
01658       return NULL;
01659     /* F=F-covered by first(I) */
01660     boost::dynamic_bitset<> tmp_bitset= (*ror_scan_mark)->bitsToBitset();
01661     *covered_fields|= tmp_bitset;
01662     all_covered= param->needed_fields.is_subset_of(*covered_fields);
01663   } while ((++ror_scan_mark < ror_scans_end) && ! all_covered);
01664 
01665   if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
01666     return NULL;
01667 
01668   /*
01669     Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
01670     cost total_cost.
01671   */
01672   /* Add priority queue use cost. */
01673   total_cost += static_cast<double>(records) *
01674                 log((double)(ror_scan_mark - tree->ror_scans)) /
01675                 (TIME_FOR_COMPARE_ROWID * M_LN2);
01676 
01677   if (total_cost > read_time)
01678     return NULL;
01679 
01680   optimizer::RorIntersectReadPlan* trp= new (*param->mem_root) optimizer::RorIntersectReadPlan;
01681 
01682   uint32_t best_num= (ror_scan_mark - tree->ror_scans);
01683   trp->first_scan= new (*param->mem_root) optimizer::RorScanInfo*[best_num];
01684   memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(optimizer::RorScanInfo*));
01685   trp->last_scan=  trp->first_scan + best_num;
01686   trp->is_covering= true;
01687   trp->read_cost= total_cost;
01688   trp->records= records;
01689   trp->cpk_scan= NULL;
01690   set_if_smaller(param->table->quick_condition_rows, records);
01691 
01692   return(trp);
01693 }
01694 
01695 
01696 /*
01697   Get best ROR-intersection plan using non-covering ROR-intersection search
01698   algorithm. The returned plan may be covering.
01699 
01700   SYNOPSIS
01701     get_best_ror_intersect()
01702       param            Parameter from test_quick_select function.
01703       tree             Transformed restriction condition to be used to look
01704                        for ROR scans.
01705       read_time        Do not return read plans with cost > read_time.
01706       are_all_covering [out] set to true if union of all scans covers all
01707                        fields needed by the query (and it is possible to build
01708                        a covering ROR-intersection)
01709 
01710   NOTES
01711     get_key_scans_params must be called before this function can be called.
01712 
01713     When this function is called by ROR-union construction algorithm it
01714     assumes it is building an uncovered ROR-intersection (and thus # of full
01715     records to be retrieved is wrong here). This is a hack.
01716 
01717   IMPLEMENTATION
01718     The approximate best non-covering plan search algorithm is as follows:
01719 
01720     find_min_ror_intersection_scan()
01721     {
01722       R= select all ROR scans;
01723       order R by (E(#records_matched) * key_record_length).
01724 
01725       S= first(R); -- set of scans that will be used for ROR-intersection
01726       R= R-first(S);
01727       min_cost= cost(S);
01728       min_scan= make_scan(S);
01729       while (R is not empty)
01730       {
01731         firstR= R - first(R);
01732         if (!selectivity(S + firstR < selectivity(S)))
01733           continue;
01734 
01735         S= S + first(R);
01736         if (cost(S) < min_cost)
01737         {
01738           min_cost= cost(S);
01739           min_scan= make_scan(S);
01740         }
01741       }
01742       return min_scan;
01743     }
01744 
01745     See ror_intersect_add function for ROR intersection costs.
01746 
01747     Special handling for Clustered PK scans
01748     Clustered PK contains all table fields, so using it as a regular scan in
01749     index intersection doesn't make sense: a range scan on CPK will be less
01750     expensive in this case.
01751     Clustered PK scan has special handling in ROR-intersection: it is not used
01752     to retrieve rows, instead its condition is used to filter row references
01753     we get from scans on other keys.
01754 
01755   RETURN
01756     ROR-intersection table read plan
01757     NULL if out of memory or no suitable plan found.
01758 */
01759 
01760 static
01761 optimizer::RorIntersectReadPlan *get_best_ror_intersect(const optimizer::Parameter *param,
01762                                                    optimizer::SEL_TREE *tree,
01763                                                    double read_time,
01764                                                    bool *are_all_covering)
01765 {
01766   uint32_t idx= 0;
01767   double min_cost= DBL_MAX;
01768 
01769   if ((tree->n_ror_scans < 2) || ! param->table->cursor->stats.records)
01770     return NULL;
01771 
01772   /*
01773     Step1: Collect ROR-able SEL_ARGs and create optimizer::RorScanInfo for each of
01774     them. Also find and save clustered PK scan if there is one.
01775   */
01776   optimizer::RorScanInfo **cur_ror_scan= NULL;
01777   optimizer::RorScanInfo *cpk_scan= NULL;
01778   uint32_t cpk_no= 0;
01779   bool cpk_scan_used= false;
01780 
01781   tree->ror_scans= new (*param->mem_root) optimizer::RorScanInfo*[param->keys];
01782   cpk_no= ((param->table->cursor->primary_key_is_clustered()) ? param->table->getShare()->getPrimaryKey() : MAX_KEY);
01783 
01784   for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
01785   {
01786     optimizer::RorScanInfo *scan;
01787     if (! tree->ror_scans_map.test(idx))
01788       continue;
01789     if (! (scan= make_ror_scan(param, idx, tree->keys[idx])))
01790       return NULL;
01791     if (param->real_keynr[idx] == cpk_no)
01792     {
01793       cpk_scan= scan;
01794       tree->n_ror_scans--;
01795     }
01796     else
01797       *(cur_ror_scan++)= scan;
01798   }
01799 
01800   tree->ror_scans_end= cur_ror_scan;
01801   /*
01802     Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
01803     optimizer::RorScanInfo's.
01804     Step 2: Get best ROR-intersection using an approximate algorithm.
01805   */
01806   internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(optimizer::RorScanInfo*),
01807                      (qsort_cmp)cmp_ror_scan_info);
01808 
01809   optimizer::RorScanInfo **intersect_scans= NULL; /* ROR scans used in index intersection */
01810   optimizer::RorScanInfo **intersect_scans_end= intersect_scans=  new (*param->mem_root) optimizer::RorScanInfo*[tree->n_ror_scans];
01811   intersect_scans_end= intersect_scans;
01812 
01813   /* Create and incrementally update ROR intersection. */
01814   ROR_INTERSECT_INFO intersect(param);
01815   ROR_INTERSECT_INFO intersect_best(param);
01816 
01817   /* [intersect_scans,intersect_scans_best) will hold the best intersection */
01818   optimizer::RorScanInfo **intersect_scans_best= NULL;
01819   cur_ror_scan= tree->ror_scans;
01820   intersect_scans_best= intersect_scans;
01821   while (cur_ror_scan != tree->ror_scans_end && ! intersect.is_covering)
01822   {
01823     /* S= S + first(R);  R= R - first(R); */
01824     if (! ror_intersect_add(&intersect, *cur_ror_scan, false))
01825     {
01826       cur_ror_scan++;
01827       continue;
01828     }
01829 
01830     *(intersect_scans_end++)= *(cur_ror_scan++);
01831 
01832     if (intersect.total_cost < min_cost)
01833     {
01834       /* Local minimum found, save it */
01835       ror_intersect_cpy(&intersect_best, &intersect);
01836       intersect_scans_best= intersect_scans_end;
01837       min_cost = intersect.total_cost;
01838     }
01839   }
01840 
01841   if (intersect_scans_best == intersect_scans)
01842   {
01843     return NULL;
01844   }
01845 
01846   *are_all_covering= intersect.is_covering;
01847   uint32_t best_num= intersect_scans_best - intersect_scans;
01848   ror_intersect_cpy(&intersect, &intersect_best);
01849 
01850   /*
01851     Ok, found the best ROR-intersection of non-CPK key scans.
01852     Check if we should add a CPK scan. If the obtained ROR-intersection is
01853     covering, it doesn't make sense to add CPK scan.
01854   */
01855   if (cpk_scan && ! intersect.is_covering)
01856   {
01857     if (ror_intersect_add(&intersect, cpk_scan, true) &&
01858         (intersect.total_cost < min_cost))
01859     {
01860       cpk_scan_used= true;
01861       intersect_best= intersect; //just set pointer here
01862     }
01863   }
01864 
01865   /* Ok, return ROR-intersect plan if we have found one */
01866   optimizer::RorIntersectReadPlan *trp= NULL;
01867   if (min_cost < read_time && (cpk_scan_used || best_num > 1))
01868   {
01869     trp= new (*param->mem_root) optimizer::RorIntersectReadPlan;
01870     trp->first_scan= new (*param->mem_root) optimizer::RorScanInfo*[best_num];
01871     memcpy(trp->first_scan, intersect_scans, best_num*sizeof(optimizer::RorScanInfo*));
01872     trp->last_scan=  trp->first_scan + best_num;
01873     trp->is_covering= intersect_best.is_covering;
01874     trp->read_cost= intersect_best.total_cost;
01875     /* Prevent divisons by zero */
01876     ha_rows best_rows = double2rows(intersect_best.out_rows);
01877     if (! best_rows)
01878       best_rows= 1;
01879     set_if_smaller(param->table->quick_condition_rows, best_rows);
01880     trp->records= best_rows;
01881     trp->index_scan_costs= intersect_best.index_scan_costs;
01882     trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
01883   }
01884   return trp;
01885 }
01886 
01887 
01888 /*
01889   Get best "range" table read plan for given optimizer::SEL_TREE, also update some info
01890 
01891   SYNOPSIS
01892     get_key_scans_params()
01893       session
01894       param                    Parameters from test_quick_select
01895       tree                     Make range select for this optimizer::SEL_TREE
01896       index_read_must_be_used  true <=> assume 'index only' option will be set
01897                                (except for clustered PK indexes)
01898       update_tbl_stats         true <=> update table->quick_* with information
01899                                about range scans we've evaluated.
01900       read_time                Maximum cost. i.e. don't create read plans with
01901                                cost > read_time.
01902 
01903   DESCRIPTION
01904     Find the best "range" table read plan for given optimizer::SEL_TREE.
01905     The side effects are
01906      - tree->ror_scans is updated to indicate which scans are ROR scans.
01907      - if update_tbl_stats=true then table->quick_* is updated with info
01908        about every possible range scan.
01909 
01910   RETURN
01911     Best range read plan
01912     NULL if no plan found or error occurred
01913 */
01914 
01915 static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
01916                                                       optimizer::Parameter *param,
01917                                                       optimizer::SEL_TREE *tree,
01918                                                       bool index_read_must_be_used,
01919                                                       bool update_tbl_stats,
01920                                                       double read_time)
01921 {
01922   uint32_t idx;
01923   optimizer::SEL_ARG **key= NULL;
01924   optimizer::SEL_ARG **end= NULL;
01925   optimizer::SEL_ARG **key_to_read= NULL;
01926   ha_rows best_records= 0;
01927   uint32_t best_mrr_flags= 0;
01928   uint32_t best_buf_size= 0;
01929   optimizer::RangeReadPlan *read_plan= NULL;
01930   /*
01931     Note that there may be trees that have type optimizer::SEL_TREE::KEY but contain no
01932     key reads at all, e.g. tree for expression "key1 is not null" where key1
01933     is defined as "not null".
01934   */
01935   tree->ror_scans_map.reset();
01936   tree->n_ror_scans= 0;
01937   for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
01938   {
01939     if (*key)
01940     {
01941       ha_rows found_records;
01942       optimizer::CostVector cost;
01943       double found_read_time= 0.0;
01944       uint32_t mrr_flags, buf_size;
01945       uint32_t keynr= param->real_keynr[idx];
01946       if ((*key)->type == optimizer::SEL_ARG::MAYBE_KEY ||
01947           (*key)->maybe_flag)
01948         param->needed_reg->set(keynr);
01949 
01950       bool read_index_only= index_read_must_be_used ||
01951                             param->table->covering_keys.test(keynr);
01952 
01953       found_records= check_quick_select(session, param, idx, read_index_only, *key,
01954                                         update_tbl_stats, &mrr_flags,
01955                                         &buf_size, &cost);
01956       found_read_time= cost.total_cost();
01957       if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
01958       {
01959         tree->n_ror_scans++;
01960         tree->ror_scans_map.set(idx);
01961       }
01962       if (read_time > found_read_time && found_records != HA_POS_ERROR)
01963       {
01964         read_time=    found_read_time;
01965         best_records= found_records;
01966         key_to_read=  key;
01967         best_mrr_flags= mrr_flags;
01968         best_buf_size=  buf_size;
01969       }
01970     }
01971   }
01972 
01973   if (key_to_read)
01974   {
01975     idx= key_to_read - tree->keys;
01976     read_plan= new (*param->mem_root) optimizer::RangeReadPlan(*key_to_read, idx, best_mrr_flags);
01977     read_plan->records= best_records;
01978     read_plan->is_ror= tree->ror_scans_map.test(idx);
01979     read_plan->read_cost= read_time;
01980     read_plan->mrr_buf_size= best_buf_size;
01981   }
01982   return read_plan;
01983 }
01984 
01985 
01986 optimizer::QuickSelectInterface *optimizer::IndexMergeReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *)
01987 {
01988   /* index_merge always retrieves full rows, ignore retrieve_full_rows */
01989   optimizer::QuickIndexMergeSelect* quick_imerge= new optimizer::QuickIndexMergeSelect(param->session, param->table);
01990   quick_imerge->records= records;
01991   quick_imerge->read_time= read_cost;
01992   for (optimizer::RangeReadPlan **range_scan= range_scans; range_scan != range_scans_end; range_scan++)
01993   {
01994     optimizer::QuickRangeSelect* quick= (optimizer::QuickRangeSelect*)((*range_scan)->make_quick(param, false, &quick_imerge->alloc));
01995     if (not quick)
01996     {
01997       delete quick;
01998       delete quick_imerge;
01999       return NULL;
02000     }
02001     quick_imerge->push_quick_back(quick);
02002   }
02003   return quick_imerge;
02004 }
02005 
02006 optimizer::QuickSelectInterface *optimizer::RorIntersectReadPlan::make_quick(optimizer::Parameter *param,
02007                                                                              bool retrieve_full_rows,
02008                                                                              memory::Root *parent_alloc)
02009 {
02010   optimizer::QuickRorIntersectSelect *quick_intersect= NULL;
02011   optimizer::QuickRangeSelect *quick= NULL;
02012   memory::Root *alloc= NULL;
02013 
02014   if ((quick_intersect=
02015          new optimizer::QuickRorIntersectSelect(param->session,
02016                                                 param->table,
02017                                                 (retrieve_full_rows? (! is_covering) : false),
02018                                                 parent_alloc)))
02019   {
02020     alloc= parent_alloc ? parent_alloc : &quick_intersect->alloc;
02021     for (; first_scan != last_scan; ++first_scan)
02022     {
02023       if (! (quick= optimizer::get_quick_select(param,
02024                                                 (*first_scan)->idx,
02025                                                 (*first_scan)->sel_arg,
02026                                                 HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
02027                                                 0,
02028                                                 alloc)))
02029       {
02030         delete quick_intersect;
02031         return NULL;
02032       }
02033       quick_intersect->push_quick_back(quick);
02034     }
02035     if (cpk_scan)
02036     {
02037       if (! (quick= optimizer::get_quick_select(param,
02038                                                 cpk_scan->idx,
02039                                                 cpk_scan->sel_arg,
02040                                                 HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
02041                                                 0,
02042                                                 alloc)))
02043       {
02044         delete quick_intersect;
02045         return NULL;
02046       }
02047       quick->resetCursor();
02048       quick_intersect->cpk_quick= quick;
02049     }
02050     quick_intersect->records= records;
02051     quick_intersect->read_time= read_cost;
02052   }
02053   return quick_intersect;
02054 }
02055 
02056 
02057 optimizer::QuickSelectInterface *optimizer::RorUnionReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *)
02058 {
02059   /*
02060     It is impossible to construct a ROR-union that will not retrieve full
02061     rows, ignore retrieve_full_rows parameter.
02062   */
02063   optimizer::QuickRorUnionSelect* quick_roru= new optimizer::QuickRorUnionSelect(param->session, param->table);
02064   for (optimizer::TableReadPlan** scan= first_ror; scan != last_ror; scan++)
02065   {
02066     optimizer::QuickSelectInterface* quick= (*scan)->make_quick(param, false, &quick_roru->alloc);
02067     if (not quick)
02068       return NULL;
02069     quick_roru->push_quick_back(quick);
02070   }
02071   quick_roru->records= records;
02072   quick_roru->read_time= read_cost;
02073   return quick_roru;
02074 }
02075 
02076 
02077 /*
02078   Build a optimizer::SEL_TREE for <> or NOT BETWEEN predicate
02079 
02080   SYNOPSIS
02081     get_ne_mm_tree()
02082       param       Parameter from SqlSelect::test_quick_select
02083       cond_func   item for the predicate
02084       field       field in the predicate
02085       lt_value    constant that field should be smaller
02086       gt_value    constant that field should be greaterr
02087       cmp_type    compare type for the field
02088 
02089   RETURN
02090     #  Pointer to tree built tree
02091     0  on error
02092 */
02093 static optimizer::SEL_TREE *get_ne_mm_tree(optimizer::RangeParameter *param,
02094                                 Item_func *cond_func,
02095                                 Field *field,
02096                                 Item *lt_value, Item *gt_value,
02097                                 Item_result cmp_type)
02098 {
02099   optimizer::SEL_TREE *tree= NULL;
02100   tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
02101                      lt_value, cmp_type);
02102   if (tree)
02103   {
02104     tree= tree_or(param,
02105                   tree,
02106                   get_mm_parts(param, cond_func, field,
02107                   Item_func::GT_FUNC,
02108                   gt_value,
02109                   cmp_type));
02110   }
02111   return tree;
02112 }
02113 
02114 
02115 /*
02116   Build a optimizer::SEL_TREE for a simple predicate
02117 
02118   SYNOPSIS
02119     get_func_mm_tree()
02120       param       Parameter from SqlSelect::test_quick_select
02121       cond_func   item for the predicate
02122       field       field in the predicate
02123       value       constant in the predicate
02124       cmp_type    compare type for the field
02125       inv         true <> NOT cond_func is considered
02126                   (makes sense only when cond_func is BETWEEN or IN)
02127 
02128   RETURN
02129     Pointer to the tree built tree
02130 */
02131 static optimizer::SEL_TREE *get_func_mm_tree(optimizer::RangeParameter *param,
02132                                   Item_func *cond_func,
02133                                   Field *field,
02134                                   Item *value,
02135                                   Item_result cmp_type,
02136                                   bool inv)
02137 {
02138   optimizer::SEL_TREE *tree= NULL;
02139 
02140   switch (cond_func->functype())
02141   {
02142 
02143   case Item_func::NE_FUNC:
02144     tree= get_ne_mm_tree(param, cond_func, field, value, value, cmp_type);
02145     break;
02146 
02147   case Item_func::BETWEEN:
02148   {
02149     if (! value)
02150     {
02151       if (inv)
02152       {
02153         tree= get_ne_mm_tree(param,
02154                              cond_func,
02155                              field,
02156                              cond_func->arguments()[1],
02157                              cond_func->arguments()[2],
02158                              cmp_type);
02159       }
02160       else
02161       {
02162         tree= get_mm_parts(param,
02163                            cond_func,
02164                            field,
02165                            Item_func::GE_FUNC,
02166                            cond_func->arguments()[1],
02167                            cmp_type);
02168         if (tree)
02169         {
02170           tree= tree_and(param,
02171                          tree,
02172                          get_mm_parts(param, cond_func, field,
02173                          Item_func::LE_FUNC,
02174                          cond_func->arguments()[2],
02175                          cmp_type));
02176         }
02177       }
02178     }
02179     else
02180       tree= get_mm_parts(param,
02181                          cond_func,
02182                          field,
02183                          (inv ?
02184                           (value == (Item*)1 ? Item_func::GT_FUNC :
02185                                                Item_func::LT_FUNC):
02186                           (value == (Item*)1 ? Item_func::LE_FUNC :
02187                                                Item_func::GE_FUNC)),
02188                          cond_func->arguments()[0],
02189                          cmp_type);
02190     break;
02191   }
02192   case Item_func::IN_FUNC:
02193   {
02194     Item_func_in *func= (Item_func_in*) cond_func;
02195 
02196     /*
02197       Array for IN() is constructed when all values have the same result
02198       type. Tree won't be built for values with different result types,
02199       so we check it here to avoid unnecessary work.
02200     */
02201     if (! func->arg_types_compatible)
02202       break;
02203 
02204     if (inv)
02205     {
02206       if (func->array && func->array->result_type() != ROW_RESULT)
02207       {
02208         /*
02209           We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
02210           where c{i} are constants. Our goal is to produce a optimizer::SEL_TREE that
02211           represents intervals:
02212 
02213           ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ...    (*)
02214 
02215           where $MIN is either "-inf" or NULL.
02216 
02217           The most straightforward way to produce it is to convert NOT IN
02218           into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
02219           analyzer to build optimizer::SEL_TREE from that. The problem is that the
02220           range analyzer will use O(N^2) memory (which is probably a bug),
02221           and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
02222           will run out of memory.
02223 
02224           Another problem with big lists like (*) is that a big list is
02225           unlikely to produce a good "range" access, while considering that
02226           range access will require expensive CPU calculations (and for
02227           MyISAM even index accesses). In short, big NOT IN lists are rarely
02228           worth analyzing.
02229 
02230           Considering the above, we'll handle NOT IN as follows:
02231           * if the number of entries in the NOT IN list is less than
02232             NOT_IN_IGNORE_THRESHOLD, construct the optimizer::SEL_TREE (*) manually.
02233           * Otherwise, don't produce a optimizer::SEL_TREE.
02234         */
02235         const unsigned int NOT_IN_IGNORE_THRESHOLD= 1000;
02236         memory::Root *tmp_root= param->mem_root;
02237         param->session->mem_root= param->old_root;
02238         /*
02239           Create one Item_type constant object. We'll need it as
02240           get_mm_parts only accepts constant values wrapped in Item_Type
02241           objects.
02242           We create the Item on param->mem_root which points to
02243           per-statement mem_root (while session->mem_root is currently pointing
02244           to mem_root local to range optimizer).
02245         */
02246         Item *value_item= func->array->create_item();
02247         param->session->mem_root= tmp_root;
02248 
02249         if (func->array->count > NOT_IN_IGNORE_THRESHOLD || ! value_item)
02250           break;
02251 
02252         /* Get a optimizer::SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
02253         uint32_t i=0;
02254         do
02255         {
02256           func->array->value_to_item(i, value_item);
02257           tree= get_mm_parts(param,
02258                              cond_func,
02259                              field, Item_func::LT_FUNC,
02260                              value_item,
02261                              cmp_type);
02262           if (! tree)
02263             break;
02264           i++;
02265         } while (i < func->array->count && tree->type == optimizer::SEL_TREE::IMPOSSIBLE);
02266 
02267         if (!tree || tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
02268         {
02269           /* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
02270           tree= NULL;
02271           break;
02272         }
02273         optimizer::SEL_TREE *tree2= NULL;
02274         for (; i < func->array->count; i++)
02275         {
02276           if (func->array->compare_elems(i, i-1))
02277           {
02278             /* Get a optimizer::SEL_TREE for "-inf < X < c_i" interval */
02279             func->array->value_to_item(i, value_item);
02280             tree2= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
02281                                 value_item, cmp_type);
02282             if (!tree2)
02283             {
02284               tree= NULL;
02285               break;
02286             }
02287 
02288             /* Change all intervals to be "c_{i-1} < X < c_i" */
02289             for (uint32_t idx= 0; idx < param->keys; idx++)
02290             {
02291               optimizer::SEL_ARG *new_interval, *last_val;
02292               if (((new_interval= tree2->keys[idx])) &&
02293                   (tree->keys[idx]) &&
02294                   ((last_val= tree->keys[idx]->last())))
02295               {
02296                 new_interval->min_value= last_val->max_value;
02297                 new_interval->min_flag= NEAR_MIN;
02298               }
02299             }
02300             /*
02301               The following doesn't try to allocate memory so no need to
02302               check for NULL.
02303             */
02304             tree= tree_or(param, tree, tree2);
02305           }
02306         }
02307 
02308         if (tree && tree->type != optimizer::SEL_TREE::IMPOSSIBLE)
02309         {
02310           /*
02311             Get the optimizer::SEL_TREE for the last "c_last < X < +inf" interval
02312             (value_item cotains c_last already)
02313           */
02314           tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
02315                               value_item, cmp_type);
02316           tree= tree_or(param, tree, tree2);
02317         }
02318       }
02319       else
02320       {
02321         tree= get_ne_mm_tree(param, cond_func, field,
02322                              func->arguments()[1], func->arguments()[1],
02323                              cmp_type);
02324         if (tree)
02325         {
02326           Item **arg, **end;
02327           for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
02328                arg < end ; arg++)
02329           {
02330             tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field,
02331                                                         *arg, *arg, cmp_type));
02332           }
02333         }
02334       }
02335     }
02336     else
02337     {
02338       tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
02339                          func->arguments()[1], cmp_type);
02340       if (tree)
02341       {
02342         Item **arg, **end;
02343         for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
02344              arg < end ; arg++)
02345         {
02346           tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
02347                                                   Item_func::EQ_FUNC,
02348                                                   *arg, cmp_type));
02349         }
02350       }
02351     }
02352     break;
02353   }
02354   default:
02355   {
02356     /*
02357        Here the function for the following predicates are processed:
02358        <, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL.
02359        If the predicate is of the form (value op field) it is handled
02360        as the equivalent predicate (field rev_op value), e.g.
02361        2 <= a is handled as a >= 2.
02362     */
02363     Item_func::Functype func_type=
02364       (value != cond_func->arguments()[0]) ? cond_func->functype() :
02365         ((Item_bool_func2*) cond_func)->rev_functype();
02366     tree= get_mm_parts(param, cond_func, field, func_type, value, cmp_type);
02367   }
02368   }
02369 
02370   return(tree);
02371 }
02372 
02373 
02374 /*
02375   Build conjunction of all optimizer::SEL_TREEs for a simple predicate applying equalities
02376 
02377   SYNOPSIS
02378     get_full_func_mm_tree()
02379       param       Parameter from SqlSelect::test_quick_select
02380       cond_func   item for the predicate
02381       field_item  field in the predicate
02382       value       constant in the predicate
02383                   (for BETWEEN it contains the number of the field argument,
02384                    for IN it's always 0)
02385       inv         true <> NOT cond_func is considered
02386                   (makes sense only when cond_func is BETWEEN or IN)
02387 
02388   DESCRIPTION
02389     For a simple SARGable predicate of the form (f op c), where f is a field and
02390     c is a constant, the function builds a conjunction of all optimizer::SEL_TREES that can
02391     be obtained by the substitution of f for all different fields equal to f.
02392 
02393   NOTES
02394     If the WHERE condition contains a predicate (fi op c),
02395     then not only SELL_TREE for this predicate is built, but
02396     the trees for the results of substitution of fi for
02397     each fj belonging to the same multiple equality as fi
02398     are built as well.
02399     E.g. for WHERE t1.a=t2.a AND t2.a > 10
02400     a optimizer::SEL_TREE for t2.a > 10 will be built for quick select from t2
02401     and
02402     a optimizer::SEL_TREE for t1.a > 10 will be built for quick select from t1.
02403 
02404     A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
02405     in a similar way: we build a conjuction of trees for the results
02406     of all substitutions of fi for equal fj.
02407     Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
02408     differently. It is considered as a conjuction of two SARGable
02409     predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
02410     is called for each of them separately producing trees for
02411        AND j (f1j <=c ) and AND j (f2j <= c)
02412     After this these two trees are united in one conjunctive tree.
02413     It's easy to see that the same tree is obtained for
02414        AND j,k (f1j <=c AND f2k<=c)
02415     which is equivalent to
02416        AND j,k (c BETWEEN f1j AND f2k).
02417     The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
02418     which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
02419     function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
02420     producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
02421     trees are united in one OR-tree. The expression
02422       (AND j (f1j > c) OR AND j (f2j < c)
02423     is equivalent to the expression
02424       AND j,k (f1j > c OR f2k < c)
02425     which is just a translation of
02426       AND j,k (c NOT BETWEEN f1j AND f2k)
02427 
02428     In the cases when one of the items f1, f2 is a constant c1 we do not create
02429     a tree for it at all. It works for BETWEEN predicates but does not
02430     work for NOT BETWEEN predicates as we have to evaluate the expression
02431     with it. If it is true then the other tree can be completely ignored.
02432     We do not do it now and no trees are built in these cases for
02433     NOT BETWEEN predicates.
02434 
02435     As to IN predicates only ones of the form (f IN (c1,...,cn)),
02436     where f1 is a field and c1,...,cn are constant, are considered as
02437     SARGable. We never try to narrow the index scan using predicates of
02438     the form (c IN (c1,...,f,...,cn)).
02439 
02440   RETURN
02441     Pointer to the tree representing the built conjunction of optimizer::SEL_TREEs
02442 */
02443 
02444 static optimizer::SEL_TREE *get_full_func_mm_tree(optimizer::RangeParameter *param,
02445                                        Item_func *cond_func,
02446                                        Item_field *field_item, Item *value,
02447                                        bool inv)
02448 {
02449   optimizer::SEL_TREE *tree= 0;
02450   optimizer::SEL_TREE *ftree= 0;
02451   table_map ref_tables= 0;
02452   table_map param_comp= ~(param->prev_tables | param->read_tables |
02453               param->current_table);
02454 
02455   for (uint32_t i= 0; i < cond_func->arg_count; i++)
02456   {
02457     Item *arg= cond_func->arguments()[i]->real_item();
02458     if (arg != field_item)
02459       ref_tables|= arg->used_tables();
02460   }
02461 
02462   Field *field= field_item->field;
02463   field->setWriteSet();
02464 
02465   Item_result cmp_type= field->cmp_type();
02466   if (!((ref_tables | field->getTable()->map) & param_comp))
02467     ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
02468   Item_equal *item_equal= field_item->item_equal;
02469   if (item_equal)
02470   {
02471     Item_equal_iterator it(item_equal->begin());
02472     Item_field *item;
02473     while ((item= it++))
02474     {
02475       Field *f= item->field;
02476       f->setWriteSet();
02477 
02478       if (field->eq(f))
02479         continue;
02480       if (!((ref_tables | f->getTable()->map) & param_comp))
02481       {
02482         tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
02483         ftree= !ftree ? tree : tree_and(param, ftree, tree);
02484       }
02485     }
02486   }
02487   return(ftree);
02488 }
02489 
02490   /* make a select tree of all keys in condition */
02491 
02492 static optimizer::SEL_TREE *get_mm_tree(optimizer::RangeParameter *param, COND *cond)
02493 {
02494   optimizer::SEL_TREE *tree=0;
02495   optimizer::SEL_TREE *ftree= 0;
02496   Item_field *field_item= 0;
02497   bool inv= false;
02498   Item *value= 0;
02499 
02500   if (cond->type() == Item::COND_ITEM)
02501   {
02502     List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
02503 
02504     if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
02505     {
02506       tree=0;
02507       while (Item* item=li++)
02508       {
02509   optimizer::SEL_TREE *new_tree= get_mm_tree(param,item);
02510   if (param->session->is_fatal_error ||
02511             param->alloced_sel_args > optimizer::SEL_ARG::MAX_SEL_ARGS)
02512     return 0; // out of memory
02513   tree=tree_and(param,tree,new_tree);
02514   if (tree && tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
02515     break;
02516       }
02517     }
02518     else
02519     {           // COND OR
02520       tree= get_mm_tree(param,li++);
02521       if (tree)
02522       {
02523   while (Item* item= li++)
02524   {
02525     optimizer::SEL_TREE *new_tree= get_mm_tree(param,item);
02526     if (!new_tree)
02527       return 0; // out of memory
02528     tree=tree_or(param,tree,new_tree);
02529     if (!tree || tree->type == optimizer::SEL_TREE::ALWAYS)
02530       break;
02531   }
02532       }
02533     }
02534     return(tree);
02535   }
02536   /* Here when simple cond
02537      There are limits on what kinds of const items we can evaluate, grep for
02538      DontEvaluateMaterializedSubqueryTooEarly.
02539   */
02540   if (cond->const_item()  && !cond->is_expensive())
02541   {
02542     /*
02543       During the cond->val_int() evaluation we can come across a subselect
02544       item which may allocate memory on the session->mem_root and assumes
02545       all the memory allocated has the same life span as the subselect
02546       item itself. So we have to restore the thread's mem_root here.
02547     */
02548     memory::Root *tmp_root= param->mem_root;
02549     param->session->mem_root= param->old_root;
02550     tree= cond->val_int() ? new(tmp_root) optimizer::SEL_TREE(optimizer::SEL_TREE::ALWAYS) :
02551                             new(tmp_root) optimizer::SEL_TREE(optimizer::SEL_TREE::IMPOSSIBLE);
02552     param->session->mem_root= tmp_root;
02553     return(tree);
02554   }
02555 
02556   table_map ref_tables= 0;
02557   table_map param_comp= ~(param->prev_tables | param->read_tables |
02558               param->current_table);
02559   if (cond->type() != Item::FUNC_ITEM)
02560   {           // Should be a field
02561     ref_tables= cond->used_tables();
02562     if ((ref_tables & param->current_table) ||
02563   (ref_tables & ~(param->prev_tables | param->read_tables)))
02564       return 0;
02565     return(new optimizer::SEL_TREE(optimizer::SEL_TREE::MAYBE));
02566   }
02567 
02568   Item_func *cond_func= (Item_func*) cond;
02569   if (cond_func->functype() == Item_func::BETWEEN ||
02570       cond_func->functype() == Item_func::IN_FUNC)
02571     inv= ((Item_func_opt_neg *) cond_func)->negated;
02572   else if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
02573     return 0;
02574 
02575   param->cond= cond;
02576 
02577   switch (cond_func->functype()) {
02578   case Item_func::BETWEEN:
02579     if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
02580     {
02581       field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
02582       ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
02583     }
02584 
02585     /*
02586       Concerning the code below see the NOTES section in
02587       the comments for the function get_full_func_mm_tree()
02588     */
02589     for (uint32_t i= 1 ; i < cond_func->arg_count ; i++)
02590     {
02591       if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
02592       {
02593         field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
02594         optimizer::SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func,
02595                                     field_item, (Item*)(intptr_t)i, inv);
02596         if (inv)
02597           tree= !tree ? tmp : tree_or(param, tree, tmp);
02598         else
02599           tree= tree_and(param, tree, tmp);
02600       }
02601       else if (inv)
02602       {
02603         tree= 0;
02604         break;
02605       }
02606     }
02607 
02608     ftree = tree_and(param, ftree, tree);
02609     break;
02610   case Item_func::IN_FUNC:
02611   {
02612     Item_func_in *func=(Item_func_in*) cond_func;
02613     if (func->key_item()->real_item()->type() != Item::FIELD_ITEM)
02614       return 0;
02615     field_item= (Item_field*) (func->key_item()->real_item());
02616     ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
02617     break;
02618   }
02619   case Item_func::MULT_EQUAL_FUNC:
02620   {
02621     Item_equal *item_equal= (Item_equal *) cond;
02622     if (!(value= item_equal->get_const()))
02623       return 0;
02624     Item_equal_iterator it(item_equal->begin());
02625     ref_tables= value->used_tables();
02626     while ((field_item= it++))
02627     {
02628       Field *field= field_item->field;
02629       field->setWriteSet();
02630 
02631       Item_result cmp_type= field->cmp_type();
02632       if (!((ref_tables | field->getTable()->map) & param_comp))
02633       {
02634         tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
02635                value,cmp_type);
02636         ftree= !ftree ? tree : tree_and(param, ftree, tree);
02637       }
02638     }
02639 
02640     return(ftree);
02641   }
02642   default:
02643     if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
02644     {
02645       field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
02646       value= cond_func->arg_count > 1 ? cond_func->arguments()[1] : 0;
02647     }
02648     else if (cond_func->have_rev_func() &&
02649              cond_func->arguments()[1]->real_item()->type() ==
02650                                                             Item::FIELD_ITEM)
02651     {
02652       field_item= (Item_field*) (cond_func->arguments()[1]->real_item());
02653       value= cond_func->arguments()[0];
02654     }
02655     else
02656       return 0;
02657     ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
02658   }
02659 
02660   return(ftree);
02661 }
02662 
02663 
02664 static optimizer::SEL_TREE *
02665 get_mm_parts(optimizer::RangeParameter *param,
02666              COND *cond_func,
02667              Field *field,
02668              Item_func::Functype type,
02669              Item *value, Item_result)
02670 {
02671   if (field->getTable() != param->table)
02672     return 0;
02673 
02674   KEY_PART *key_part = param->key_parts;
02675   KEY_PART *end = param->key_parts_end;
02676   optimizer::SEL_TREE *tree=0;
02677   if (value &&
02678       value->used_tables() & ~(param->prev_tables | param->read_tables))
02679     return 0;
02680   for (; key_part != end; key_part++)
02681   {
02682     if (field->eq(key_part->field))
02683     {
02684       optimizer::SEL_ARG *sel_arg=0;
02685       if (!tree)
02686         tree= new optimizer::SEL_TREE;
02687       if (!value || !(value->used_tables() & ~param->read_tables))
02688       {
02689         sel_arg= get_mm_leaf(param,cond_func, key_part->field,key_part,type,value);
02690         if (! sel_arg)
02691           continue;
02692         if (sel_arg->type == optimizer::SEL_ARG::IMPOSSIBLE)
02693         {
02694           tree->type=optimizer::SEL_TREE::IMPOSSIBLE;
02695           return(tree);
02696         }
02697       }
02698       else
02699       {
02700         // This key may be used later
02701         sel_arg= new optimizer::SEL_ARG(optimizer::SEL_ARG::MAYBE_KEY);
02702       }
02703       sel_arg->part=(unsigned char) key_part->part;
02704       tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
02705       tree->keys_map.set(key_part->key);
02706     }
02707   }
02708 
02709   return tree;
02710 }
02711 
02712 
02713 static optimizer::SEL_ARG *
02714 get_mm_leaf(optimizer::RangeParameter *param,
02715             COND *conf_func,
02716             Field *field,
02717             KEY_PART *key_part,
02718             Item_func::Functype type,
02719             Item *value)
02720 {
02721   uint32_t maybe_null=(uint32_t) field->real_maybe_null();
02722   bool optimize_range;
02723   optimizer::SEL_ARG *tree= NULL;
02724   memory::Root *alloc= param->mem_root;
02725   unsigned char *str;
02726   int err= 0;
02727 
02728   /*
02729     We need to restore the runtime mem_root of the thread in this
02730     function because it evaluates the value of its argument, while
02731     the argument can be any, e.g. a subselect. The subselect
02732     items, in turn, assume that all the memory allocated during
02733     the evaluation has the same life span as the item itself.
02734     TODO: opitimizer/range.cc should not reset session->mem_root at all.
02735   */
02736   param->session->mem_root= param->old_root;
02737   if (!value)         // IS NULL or IS NOT NULL
02738   {
02739     if (field->getTable()->maybe_null)    // Can't use a key on this
02740       goto end;
02741     if (!maybe_null)        // Not null field
02742     {
02743       if (type == Item_func::ISNULL_FUNC)
02744         tree= &optimizer::null_element;
02745       goto end;
02746     }
02747     tree= new (*alloc) optimizer::SEL_ARG(field,is_null_string,is_null_string);
02748     if (type == Item_func::ISNOTNULL_FUNC)
02749     {
02750       tree->min_flag=NEAR_MIN;        /* IS NOT NULL ->  X > NULL */
02751       tree->max_flag=NO_MAX_RANGE;
02752     }
02753     goto end;
02754   }
02755 
02756   /*
02757     1. Usually we can't use an index if the column collation
02758        differ from the operation collation.
02759 
02760     2. However, we can reuse a case insensitive index for
02761        the binary searches:
02762 
02763        WHERE latin1_swedish_ci_column = 'a' COLLATE lati1_bin;
02764 
02765        WHERE latin1_swedish_ci_colimn = BINARY 'a '
02766 
02767   */
02768   if (field->result_type() == STRING_RESULT &&
02769       value->result_type() == STRING_RESULT &&
02770       ((Field_str*)field)->charset() != conf_func->compare_collation() &&
02771       !(conf_func->compare_collation()->state & MY_CS_BINSORT))
02772     goto end;
02773 
02774   if (param->using_real_indexes)
02775     optimize_range= field->optimize_range(param->real_keynr[key_part->key], key_part->part);
02776   else
02777     optimize_range= true;
02778 
02779   if (type == Item_func::LIKE_FUNC)
02780   {
02781     bool like_error;
02782     char buff1[MAX_FIELD_WIDTH];
02783     unsigned char *min_str,*max_str;
02784     String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
02785     size_t length, offset, min_length, max_length;
02786     uint32_t field_length= field->pack_length()+maybe_null;
02787 
02788     if (!optimize_range)
02789       goto end;
02790     if (!(res= value->val_str(&tmp)))
02791     {
02792       tree= &optimizer::null_element;
02793       goto end;
02794     }
02795 
02796     /*
02797       TODO:
02798       Check if this was a function. This should have be optimized away
02799       in the sql_select.cc
02800     */
02801     if (res != &tmp)
02802     {
02803       tmp.copy(*res);       // Get own copy
02804       res= &tmp;
02805     }
02806     if (field->cmp_type() != STRING_RESULT)
02807       goto end;                                 // Can only optimize strings
02808 
02809     offset=maybe_null;
02810     length=key_part->store_length;
02811 
02812     if (length != key_part->length  + maybe_null)
02813     {
02814       /* key packed with length prefix */
02815       offset+= HA_KEY_BLOB_LENGTH;
02816       field_length= length - HA_KEY_BLOB_LENGTH;
02817     }
02818     else
02819     {
02820       if (unlikely(length < field_length))
02821       {
02822         /*
02823           This can only happen in a table created with UNIREG where one key
02824           overlaps many fields
02825         */
02826         length= field_length;
02827       }
02828       else
02829         field_length= length;
02830     }
02831     length+=offset;
02832     min_str= alloc->alloc(length*2);
02833     max_str=min_str+length;
02834     if (maybe_null)
02835       max_str[0]= min_str[0]=0;
02836 
02837     field_length-= maybe_null;
02838     int escape_code= make_escape_code(field->charset(), ((Item_func_like*)(param->cond))->escape);
02839     like_error= my_like_range(field->charset(),
02840                               res->ptr(), res->length(),
02841                               escape_code,
02842                               internal::wild_one, internal::wild_many,
02843                               field_length,
02844                               (char*) min_str+offset, (char*) max_str+offset,
02845                               &min_length, &max_length);
02846     if (like_error)       // Can't optimize with LIKE
02847       goto end;
02848 
02849     if (offset != maybe_null)     // BLOB or VARCHAR
02850     {
02851       int2store(min_str+maybe_null,min_length);
02852       int2store(max_str+maybe_null,max_length);
02853     }
02854     tree= new (alloc) optimizer::SEL_ARG(field, min_str, max_str);
02855     goto end;
02856   }
02857 
02858   if (! optimize_range &&
02859       type != Item_func::EQ_FUNC &&
02860       type != Item_func::EQUAL_FUNC)
02861     goto end;                                   // Can't optimize this
02862 
02863   /*
02864     We can't always use indexes when comparing a string index to a number
02865     cmp_type() is checked to allow compare of dates to numbers
02866   */
02867   if (field->result_type() == STRING_RESULT &&
02868       value->result_type() != STRING_RESULT &&
02869       field->cmp_type() != value->result_type())
02870     goto end;
02871 
02872   /*
02873    * Some notes from Jay...
02874    *
02875    * OK, so previously, and in MySQL, what the optimizer does here is
02876    * override the sql_mode variable to ignore out-of-range or bad date-
02877    * time values.  It does this because the optimizer is populating the
02878    * field variable with the incoming value from the comparison field,
02879    * and the value may exceed the bounds of a proper column type.
02880    *
02881    * For instance, assume the following:
02882    *
02883    * CREATE TABLE t1 (ts TIMESTAMP);
02884    * INSERT INTO t1 ('2009-03-04 00:00:00');
02885    * CREATE TABLE t2 (dt1 DATETIME, dt2 DATETIME);
02886    * INSERT INT t2 ('2003-12-31 00:00:00','2999-12-31 00:00:00');
02887    *
02888    * If we issue this query:
02889    *
02890    * SELECT * FROM t1, t2 WHERE t1.ts BETWEEN t2.dt1 AND t2.dt2;
02891    *
02892    * We will come into bounds issues.  Field_timestamp::store() will be
02893    * called with a datetime value of "2999-12-31 00:00:00" and will throw
02894    * an error for out-of-bounds.  MySQL solves this via a hack with sql_mode
02895    * but Drizzle always throws errors on bad data storage in a Field class.
02896    *
02897    * Therefore, to get around the problem of the Field class being used for
02898    * "storage" here without actually storing anything...we must check to see
02899    * if the value being stored in a Field_timestamp here is out of range.  If
02900    * it is, then we must convert to the highest Timestamp value (or lowest,
02901    * depending on whether the datetime is before or after the epoch.
02902    */
02903   if (field->is_timestamp())
02904   {
02905     /*
02906      * The left-side of the range comparison is a timestamp field.  Therefore,
02907      * we must check to see if the value in the right-hand side is outside the
02908      * range of the UNIX epoch, and cut to the epoch bounds if it is.
02909      */
02910     /* Datetime and date columns are Item::FIELD_ITEM ... and have a result type of STRING_RESULT */
02911     if (value->real_item()->type() == Item::FIELD_ITEM
02912         && value->result_type() == STRING_RESULT)
02913     {
02914       char buff[DateTime::MAX_STRING_LENGTH];
02915       String tmp(buff, sizeof(buff), &my_charset_bin);
02916       String *res= value->val_str(&tmp);
02917 
02918       if (!res)
02919         goto end;
02920       else
02921       {
02922         /*
02923          * Create a datetime from the string and compare to fixed timestamp
02924          * instances representing the epoch boundaries.
02925          */
02926         DateTime value_datetime;
02927 
02928         if (! value_datetime.from_string(res->c_ptr(), (size_t) res->length()))
02929           goto end;
02930 
02931         Timestamp max_timestamp;
02932         Timestamp min_timestamp;
02933 
02934         (void) max_timestamp.from_time_t((time_t) INT32_MAX);
02935         (void) min_timestamp.from_time_t((time_t) 0);
02936 
02937         /* We rely on Temporal class operator overloads to do our comparisons. */
02938         if (value_datetime < min_timestamp)
02939         {
02940           /*
02941            * Datetime in right-hand side column is before UNIX epoch, so adjust to
02942            * lower bound.
02943            */
02944           char new_value_buff[DateTime::MAX_STRING_LENGTH];
02945           int new_value_length;
02946           String new_value_string(new_value_buff, sizeof(new_value_buff), &my_charset_bin);
02947 
02948           new_value_length= min_timestamp.to_string(new_value_string.c_ptr(),
02949             DateTime::MAX_STRING_LENGTH);
02950     assert((new_value_length+1) < DateTime::MAX_STRING_LENGTH);
02951           new_value_string.length(new_value_length);
02952           err= value->save_str_value_in_field(field, &new_value_string);
02953         }
02954         else if (value_datetime > max_timestamp)
02955         {
02956           /*
02957            * Datetime in right hand side column is after UNIX epoch, so adjust
02958            * to the higher bound of the epoch.
02959            */
02960           char new_value_buff[DateTime::MAX_STRING_LENGTH];
02961           int new_value_length;
02962           String new_value_string(new_value_buff, sizeof(new_value_buff), &my_charset_bin);
02963 
02964           new_value_length= max_timestamp.to_string(new_value_string.c_ptr(),
02965           DateTime::MAX_STRING_LENGTH);
02966     assert((new_value_length+1) < DateTime::MAX_STRING_LENGTH);
02967           new_value_string.length(new_value_length);
02968           err= value->save_str_value_in_field(field, &new_value_string);
02969         }
02970         else
02971           err= value->save_in_field(field, 1);
02972       }
02973     }
02974     else /* Not a datetime -> timestamp comparison */
02975       err= value->save_in_field(field, 1);
02976   }
02977   else /* Not a timestamp comparison */
02978     err= value->save_in_field(field, 1);
02979 
02980   if (err > 0)
02981   {
02982     if (field->cmp_type() != value->result_type())
02983     {
02984       if ((type == Item_func::EQ_FUNC || type == Item_func::EQUAL_FUNC) &&
02985           value->result_type() == item_cmp_type(field->result_type(),
02986                                                 value->result_type()))
02987       {
02988         tree= new (alloc) optimizer::SEL_ARG(field, 0, 0);
02989         tree->type= optimizer::SEL_ARG::IMPOSSIBLE;
02990         goto end;
02991       }
02992       else
02993       {
02994         /*
02995           TODO: We should return trees of the type SEL_ARG::IMPOSSIBLE
02996           for the cases like int_field > 999999999999999999999999 as well.
02997         */
02998         tree= 0;
02999         if (err == 3 && field->type() == DRIZZLE_TYPE_DATE &&
03000             (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC ||
03001              type == Item_func::LT_FUNC || type == Item_func::LE_FUNC) )
03002         {
03003           /*
03004             We were saving DATETIME into a DATE column, the conversion went ok
03005             but a non-zero time part was cut off.
03006 
03007             In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
03008             values. Index over a DATE column uses DATE comparison. Changing
03009             from one comparison to the other is possible:
03010 
03011             datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
03012             datetime(date_col)<='2007-12-10 12:34:55' -> date_col<='2007-12-10'
03013 
03014             datetime(date_col)> '2007-12-10 12:34:55' -> date_col>='2007-12-10'
03015             datetime(date_col)>='2007-12-10 12:34:55' -> date_col>='2007-12-10'
03016 
03017             but we'll need to convert '>' to '>=' and '<' to '<='. This will
03018             be done together with other types at the end of this function
03019             (grep for field_is_equal_to_item)
03020           */
03021         }
03022         else
03023           goto end;
03024       }
03025     }
03026 
03027     /*
03028       guaranteed at this point:  err > 0; field and const of same type
03029       If an integer got bounded (e.g. to within 0..255 / -128..127)
03030       for < or >, set flags as for <= or >= (no NEAR_MAX / NEAR_MIN)
03031     */
03032     else if (err == 1 && field->result_type() == INT_RESULT)
03033     {
03034       if (type == Item_func::LT_FUNC && (value->val_int() > 0))
03035         type = Item_func::LE_FUNC;
03036       else if (type == Item_func::GT_FUNC &&
03037                !((Field_num*)field)->unsigned_flag &&
03038                !((Item_int*)value)->unsigned_flag &&
03039                (value->val_int() < 0))
03040         type = Item_func::GE_FUNC;
03041     }
03042     else if (err == 1)
03043     {
03044       tree= new (alloc) optimizer::SEL_ARG(field, 0, 0);
03045       tree->type= optimizer::SEL_ARG::IMPOSSIBLE;
03046       goto end;
03047     }
03048   }
03049   else if (err < 0)
03050   {
03051     /* This happens when we try to insert a NULL field in a not null column */
03052     tree= &optimizer::null_element;                        // cmp with NULL is never true
03053     goto end;
03054   }
03055 
03056   /*
03057     Any predicate except "<=>"(null-safe equality operator) involving NULL as a
03058     constant is always FALSE
03059     Put IMPOSSIBLE Tree(null_element) here.
03060   */
03061   if (type != Item_func::EQUAL_FUNC && field->is_real_null())
03062   {
03063     tree= &optimizer::null_element;
03064     goto end;
03065   }
03066 
03067   str= alloc->alloc(key_part->store_length+1);
03068   if (maybe_null)
03069     *str= field->is_real_null();        // Set to 1 if null
03070   field->get_key_image(str+maybe_null, key_part->length);
03071   tree= new (alloc) optimizer::SEL_ARG(field, str, str);
03072 
03073   /*
03074     Check if we are comparing an UNSIGNED integer with a negative constant.
03075     In this case we know that:
03076     (a) (unsigned_int [< | <=] negative_constant) == false
03077     (b) (unsigned_int [> | >=] negative_constant) == true
03078     In case (a) the condition is false for all values, and in case (b) it
03079     is true for all values, so we can avoid unnecessary retrieval and condition
03080     testing, and we also get correct comparison of unsinged integers with
03081     negative integers (which otherwise fails because at query execution time
03082     negative integers are cast to unsigned if compared with unsigned).
03083    */
03084   if (field->result_type() == INT_RESULT &&
03085       value->result_type() == INT_RESULT &&
03086       ((Field_num*)field)->unsigned_flag && !((Item_int*)value)->unsigned_flag)
03087   {
03088     int64_t item_val= value->val_int();
03089     if (item_val < 0)
03090     {
03091       if (type == Item_func::LT_FUNC || type == Item_func::LE_FUNC)
03092       {
03093         tree->type= optimizer::SEL_ARG::IMPOSSIBLE;
03094         goto end;
03095       }
03096       if (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC)
03097       {
03098         tree= 0;
03099         goto end;
03100       }
03101     }
03102   }
03103 
03104   switch (type) {
03105   case Item_func::LT_FUNC:
03106     if (field_is_equal_to_item(field,value))
03107       tree->max_flag=NEAR_MAX;
03108     /* fall through */
03109   case Item_func::LE_FUNC:
03110     if (!maybe_null)
03111       tree->min_flag=NO_MIN_RANGE;    /* From start */
03112     else
03113     {           // > NULL
03114       tree->min_value=is_null_string;
03115       tree->min_flag=NEAR_MIN;
03116     }
03117     break;
03118   case Item_func::GT_FUNC:
03119     /* Don't use open ranges for partial key_segments */
03120     if (field_is_equal_to_item(field,value) &&
03121         !(key_part->flag & HA_PART_KEY_SEG))
03122       tree->min_flag=NEAR_MIN;
03123     /* fall through */
03124   case Item_func::GE_FUNC:
03125     tree->max_flag=NO_MAX_RANGE;
03126     break;
03127   default:
03128     break;
03129   }
03130 
03131 end:
03132   param->session->mem_root= alloc;
03133   return(tree);
03134 }
03135 
03136 
03137 /******************************************************************************
03138 ** Tree manipulation functions
03139 ** If tree is 0 it means that the condition can't be tested. It refers
03140 ** to a non existent table or to a field in current table with isn't a key.
03141 ** The different tree flags:
03142 ** IMPOSSIBLE:   Condition is never true
03143 ** ALWAYS:   Condition is always true
03144 ** MAYBE:  Condition may exists when tables are read
03145 ** MAYBE_KEY:  Condition refers to a key that may be used in join loop
03146 ** KEY_RANGE:  Condition uses a key
03147 ******************************************************************************/
03148 
03149 /*
03150   Add a new key test to a key when scanning through all keys
03151   This will never be called for same key parts.
03152 */
03153 
03154 static optimizer::SEL_ARG *
03155 sel_add(optimizer::SEL_ARG *key1, optimizer::SEL_ARG *key2)
03156 {
03157   optimizer::SEL_ARG *root= NULL;
03158   optimizer::SEL_ARG **key_link= NULL;
03159 
03160   if (!key1)
03161     return key2;
03162   if (!key2)
03163     return key1;
03164 
03165   key_link= &root;
03166   while (key1 && key2)
03167   {
03168     if (key1->part < key2->part)
03169     {
03170       *key_link= key1;
03171       key_link= &key1->next_key_part;
03172       key1=key1->next_key_part;
03173     }
03174     else
03175     {
03176       *key_link= key2;
03177       key_link= &key2->next_key_part;
03178       key2=key2->next_key_part;
03179     }
03180   }
03181   *key_link=key1 ? key1 : key2;
03182   return root;
03183 }
03184 
03185 static const int CLONE_KEY1_MAYBE= 1;
03186 static const int CLONE_KEY2_MAYBE= 2;
03187 
03188 static uint32_t swap_clone_flag(uint32_t a)
03189 {
03190   return ((a & 1) << 1) | ((a & 2) >> 1);
03191 }
03192 
03193 static optimizer::SEL_TREE *
03194 tree_and(optimizer::RangeParameter *param, optimizer::SEL_TREE *tree1, optimizer::SEL_TREE *tree2)
03195 {
03196   if (!tree1)
03197     return(tree2);
03198   if (!tree2)
03199     return(tree1);
03200   if (tree1->type == optimizer::SEL_TREE::IMPOSSIBLE || tree2->type == optimizer::SEL_TREE::ALWAYS)
03201     return(tree1);
03202   if (tree2->type == optimizer::SEL_TREE::IMPOSSIBLE || tree1->type == optimizer::SEL_TREE::ALWAYS)
03203     return(tree2);
03204   if (tree1->type == optimizer::SEL_TREE::MAYBE)
03205   {
03206     if (tree2->type == optimizer::SEL_TREE::KEY)
03207       tree2->type=optimizer::SEL_TREE::KEY_SMALLER;
03208     return(tree2);
03209   }
03210   if (tree2->type == optimizer::SEL_TREE::MAYBE)
03211   {
03212     tree1->type=optimizer::SEL_TREE::KEY_SMALLER;
03213     return(tree1);
03214   }
03215   key_map  result_keys;
03216   result_keys.reset();
03217 
03218   /* Join the trees key per key */
03219   optimizer::SEL_ARG **key1,**key2,**end;
03220   for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
03221        key1 != end ; key1++,key2++)
03222   {
03223     uint32_t flag=0;
03224     if (*key1 || *key2)
03225     {
03226       if (*key1 && !(*key1)->simple_key())
03227         flag|=CLONE_KEY1_MAYBE;
03228       if (*key2 && !(*key2)->simple_key())
03229         flag|=CLONE_KEY2_MAYBE;
03230       *key1=key_and(param, *key1, *key2, flag);
03231       if (*key1 && (*key1)->type == optimizer::SEL_ARG::IMPOSSIBLE)
03232       {
03233         tree1->type= optimizer::SEL_TREE::IMPOSSIBLE;
03234         return(tree1);
03235       }
03236       result_keys.set(key1 - tree1->keys);
03237     }
03238   }
03239   tree1->keys_map= result_keys;
03240   /* dispose index_merge if there is a "range" option */
03241   if (result_keys.any())
03242   {
03243     tree1->merges.clear();
03244     return(tree1);
03245   }
03246 
03247   /* ok, both trees are index_merge trees */
03248   imerge_list_and_list(&tree1->merges, &tree2->merges);
03249   return(tree1);
03250 }
03251 
03252 
03253 
03254 /* And key trees where key1->part < key2 -> part */
03255 
03256 static optimizer::SEL_ARG *
03257 and_all_keys(optimizer::RangeParameter *param,
03258              optimizer::SEL_ARG *key1,
03259              optimizer::SEL_ARG *key2,
03260              uint32_t clone_flag)
03261 {
03262   optimizer::SEL_ARG *next= NULL;
03263   ulong use_count=key1->use_count;
03264 
03265   if (key1->size() != 1)
03266   {
03267     key2->use_count+=key1->size()-1; //psergey: why we don't count that key1 has n-k-p?
03268     key2->increment_use_count((int) key1->size()-1);
03269   }
03270   if (key1->type == optimizer::SEL_ARG::MAYBE_KEY)
03271   {
03272     key1->right= key1->left= &optimizer::null_element;
03273     key1->next= key1->prev= 0;
03274   }
03275   for (next= key1->first(); next ; next=next->next)
03276   {
03277     if (next->next_key_part)
03278     {
03279       optimizer::SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
03280       if (tmp && tmp->type == optimizer::SEL_ARG::IMPOSSIBLE)
03281       {
03282         key1=key1->tree_delete(next);
03283         continue;
03284       }
03285       next->next_key_part=tmp;
03286       if (use_count)
03287         next->increment_use_count(use_count);
03288       if (param->alloced_sel_args > optimizer::SEL_ARG::MAX_SEL_ARGS)
03289         break;
03290     }
03291     else
03292       next->next_key_part=key2;
03293   }
03294   if (! key1)
03295     return &optimizer::null_element;      // Impossible ranges
03296   key1->use_count++;
03297   return key1;
03298 }
03299 
03300 
03301 /*
03302   Produce a SEL_ARG graph that represents "key1 AND key2"
03303 
03304   SYNOPSIS
03305     key_and()
03306       param   Range analysis context (needed to track if we have allocated
03307               too many SEL_ARGs)
03308       key1    First argument, root of its RB-tree
03309       key2    Second argument, root of its RB-tree
03310 
03311   RETURN
03312     RB-tree root of the resulting SEL_ARG graph.
03313     NULL if the result of AND operation is an empty interval {0}.
03314 */
03315 
03316 static optimizer::SEL_ARG *
03317 key_and(optimizer::RangeParameter *param,
03318         optimizer::SEL_ARG *key1,
03319         optimizer::SEL_ARG *key2,
03320         uint32_t clone_flag)
03321 {
03322   if (! key1)
03323     return key2;
03324   if (! key2)
03325     return key1;
03326   if (key1->part != key2->part)
03327   {
03328     if (key1->part > key2->part)
03329     {
03330       std::swap(key1, key2);
03331       clone_flag=swap_clone_flag(clone_flag);
03332     }
03333     // key1->part < key2->part
03334     key1->use_count--;
03335     if (key1->use_count > 0)
03336       if (! (key1= key1->clone_tree(param)))
03337         return 0;       // OOM
03338     return and_all_keys(param, key1, key2, clone_flag);
03339   }
03340 
03341   if (((clone_flag & CLONE_KEY2_MAYBE) &&
03342        ! (clone_flag & CLONE_KEY1_MAYBE) &&
03343        key2->type != optimizer::SEL_ARG::MAYBE_KEY) ||
03344       key1->type == optimizer::SEL_ARG::MAYBE_KEY)
03345   {           // Put simple key in key2
03346     std::swap(key1, key2);
03347     clone_flag= swap_clone_flag(clone_flag);
03348   }
03349 
03350   /* If one of the key is MAYBE_KEY then the found region may be smaller */
03351   if (key2->type == optimizer::SEL_ARG::MAYBE_KEY)
03352   {
03353     if (key1->use_count > 1)
03354     {
03355       key1->use_count--;
03356       if (! (key1=key1->clone_tree(param)))
03357         return 0;       // OOM
03358       key1->use_count++;
03359     }
03360     if (key1->type == optimizer::SEL_ARG::MAYBE_KEY)
03361     {           // Both are maybe key
03362       key1->next_key_part= key_and(param,
03363                                    key1->next_key_part,
03364                                    key2->next_key_part,
03365                                    clone_flag);
03366       if (key1->next_key_part &&
03367           key1->next_key_part->type == optimizer::SEL_ARG::IMPOSSIBLE)
03368         return key1;
03369     }
03370     else
03371     {
03372       key1->maybe_smaller();
03373       if (key2->next_key_part)
03374       {
03375         key1->use_count--;      // Incremented in and_all_keys
03376         return and_all_keys(param, key1, key2, clone_flag);
03377       }
03378       key2->use_count--;      // Key2 doesn't have a tree
03379     }
03380     return key1;
03381   }
03382 
03383   key1->use_count--;
03384   key2->use_count--;
03385   optimizer::SEL_ARG *e1= key1->first();
03386   optimizer::SEL_ARG *e2= key2->first();
03387   optimizer::SEL_ARG *new_tree= NULL;
03388 
03389   while (e1 && e2)
03390   {
03391     int cmp= e1->cmp_min_to_min(e2);
03392     if (cmp < 0)
03393     {
03394       if (get_range(&e1, &e2, key1))
03395         continue;
03396     }
03397     else if (get_range(&e2, &e1, key2))
03398       continue;
03399     optimizer::SEL_ARG *next= key_and(param,
03400                                       e1->next_key_part,
03401                                       e2->next_key_part,
03402                                       clone_flag);
03403     e1->increment_use_count(1);
03404     e2->increment_use_count(1);
03405     if (! next || next->type != optimizer::SEL_ARG::IMPOSSIBLE)
03406     {
03407       optimizer::SEL_ARG *new_arg= e1->clone_and(e2);
03408       new_arg->next_key_part=next;
03409       if (! new_tree)
03410       {
03411         new_tree=new_arg;
03412       }
03413       else
03414         new_tree=new_tree->insert(new_arg);
03415     }
03416     if (e1->cmp_max_to_max(e2) < 0)
03417       e1=e1->next;        // e1 can't overlapp next e2
03418     else
03419       e2=e2->next;
03420   }
03421   key1->free_tree();
03422   key2->free_tree();
03423   if (! new_tree)
03424     return &optimizer::null_element;      // Impossible range
03425   return new_tree;
03426 }
03427 
03428 
03429 static bool
03430 get_range(optimizer::SEL_ARG **e1, optimizer::SEL_ARG **e2, optimizer::SEL_ARG *root1)
03431 {
03432   (*e1)= root1->find_range(*e2);      // first e1->min < e2->min
03433   if ((*e1)->cmp_max_to_min(*e2) < 0)
03434   {
03435     if (! ((*e1)=(*e1)->next))
03436       return 1;
03437     if ((*e1)->cmp_min_to_max(*e2) > 0)
03438     {
03439       (*e2)=(*e2)->next;
03440       return 1;
03441     }
03442   }
03443   return 0;
03444 }
03445 
03446 
03447 /****************************************************************************
03448   MRR Range Sequence Interface implementation that walks a SEL_ARG* tree.
03449  ****************************************************************************/
03450 
03451 /* MRR range sequence, SEL_ARG* implementation: stack entry */
03452 typedef struct st_range_seq_entry
03453 {
03454   /*
03455     Pointers in min and max keys. They point to right-after-end of key
03456     images. The 0-th entry has these pointing to key tuple start.
03457   */
03458   unsigned char *min_key, *max_key;
03459 
03460   /*
03461     Flags, for {keypart0, keypart1, ... this_keypart} subtuple.
03462     min_key_flag may have NULL_RANGE set.
03463   */
03464   uint32_t min_key_flag, max_key_flag;
03465 
03466   /* Number of key parts */
03467   uint32_t min_key_parts, max_key_parts;
03468   optimizer::SEL_ARG *key_tree;
03469 } RANGE_SEQ_ENTRY;
03470 
03471 
03472 /*
03473   MRR range sequence, SEL_ARG* implementation: SEL_ARG graph traversal context
03474 */
03475 typedef struct st_sel_arg_range_seq
03476 {
03477   uint32_t keyno;      /* index of used tree in optimizer::SEL_TREE structure */
03478   uint32_t real_keyno; /* Number of the index in tables */
03479   optimizer::Parameter *param;
03480   optimizer::SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
03481 
03482   RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];
03483   int i; /* Index of last used element in the above array */
03484 
03485   bool at_start; /* true <=> The traversal has just started */
03486 } SEL_ARG_RANGE_SEQ;
03487 
03488 
03489 /*
03490   Range sequence interface, SEL_ARG* implementation: Initialize the traversal
03491 
03492   SYNOPSIS
03493     init()
03494       init_params  SEL_ARG tree traversal context
03495       n_ranges     [ignored] The number of ranges obtained
03496       flags        [ignored] HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
03497 
03498   RETURN
03499     Value of init_param
03500 */
03501 
03502 static range_seq_t sel_arg_range_seq_init(void *init_param, uint32_t, uint32_t)
03503 {
03504   SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)init_param;
03505   seq->at_start= true;
03506   seq->stack[0].key_tree= NULL;
03507   seq->stack[0].min_key= seq->param->min_key;
03508   seq->stack[0].min_key_flag= 0;
03509   seq->stack[0].min_key_parts= 0;
03510 
03511   seq->stack[0].max_key= seq->param->max_key;
03512   seq->stack[0].max_key_flag= 0;
03513   seq->stack[0].max_key_parts= 0;
03514   seq->i= 0;
03515   return init_param;
03516 }
03517 
03518 
03519 static void step_down_to(SEL_ARG_RANGE_SEQ *arg, optimizer::SEL_ARG *key_tree)
03520 {
03521   RANGE_SEQ_ENTRY *cur= &arg->stack[arg->i+1];
03522   RANGE_SEQ_ENTRY *prev= &arg->stack[arg->i];
03523 
03524   cur->key_tree= key_tree;
03525   cur->min_key= prev->min_key;
03526   cur->max_key= prev->max_key;
03527   cur->min_key_parts= prev->min_key_parts;
03528   cur->max_key_parts= prev->max_key_parts;
03529 
03530   uint16_t stor_length= arg->param->key[arg->keyno][key_tree->part].store_length;
03531   cur->min_key_parts += key_tree->store_min(stor_length, &cur->min_key,
03532                                             prev->min_key_flag);
03533   cur->max_key_parts += key_tree->store_max(stor_length, &cur->max_key,
03534                                             prev->max_key_flag);
03535 
03536   cur->min_key_flag= prev->min_key_flag | key_tree->min_flag;
03537   cur->max_key_flag= prev->max_key_flag | key_tree->max_flag;
03538 
03539   if (key_tree->is_null_interval())
03540     cur->min_key_flag |= NULL_RANGE;
03541   (arg->i)++;
03542 }
03543 
03544 
03545 /*
03546   Range sequence interface, SEL_ARG* implementation: get the next interval
03547 
03548   SYNOPSIS
03549     sel_arg_range_seq_next()
03550       rseq        Value returned from sel_arg_range_seq_init
03551       range  OUT  Store information about the range here
03552 
03553   DESCRIPTION
03554     This is "get_next" function for Range sequence interface implementation
03555     for SEL_ARG* tree.
03556 
03557   IMPLEMENTATION
03558     The traversal also updates those param members:
03559       - is_ror_scan
03560       - range_count
03561       - max_key_part
03562 
03563   RETURN
03564     0  Ok
03565     1  No more ranges in the sequence
03566 */
03567 
03568 //psergey-merge-todo: support check_quick_keys:max_keypart
03569 static uint32_t sel_arg_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
03570 {
03571   optimizer::SEL_ARG *key_tree;
03572   SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)rseq;
03573   if (seq->at_start)
03574   {
03575     key_tree= seq->start;
03576     seq->at_start= false;
03577     goto walk_up_n_right;
03578   }
03579 
03580   key_tree= seq->stack[seq->i].key_tree;
03581   /* Ok, we're at some "full tuple" position in the tree */
03582 
03583   /* Step down if we can */
03584   if (key_tree->next && key_tree->next != &optimizer::null_element)
03585   {
03586     //step down; (update the tuple, we'll step right and stay there)
03587     seq->i--;
03588     step_down_to(seq, key_tree->next);
03589     key_tree= key_tree->next;
03590     seq->param->is_ror_scan= false;
03591     goto walk_right_n_up;
03592   }
03593 
03594   /* Ok, can't step down, walk left until we can step down */
03595   while (1)
03596   {
03597     if (seq->i == 1) // can't step left
03598       return 1;
03599     /* Step left */
03600     seq->i--;
03601     key_tree= seq->stack[seq->i].key_tree;
03602 
03603     /* Step down if we can */
03604     if (key_tree->next && key_tree->next != &optimizer::null_element)
03605     {
03606       // Step down; update the tuple
03607       seq->i--;
03608       step_down_to(seq, key_tree->next);
03609       key_tree= key_tree->next;
03610       break;
03611     }
03612   }
03613 
03614   /*
03615     Ok, we've stepped down from the path to previous tuple.
03616     Walk right-up while we can
03617   */
03618 walk_right_n_up:
03619   while (key_tree->next_key_part && key_tree->next_key_part != &optimizer::null_element &&
03620          key_tree->next_key_part->part == key_tree->part + 1 &&
03621          key_tree->next_key_part->type == optimizer::SEL_ARG::KEY_RANGE)
03622   {
03623     {
03624       RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
03625       uint32_t min_key_length= cur->min_key - seq->param->min_key;
03626       uint32_t max_key_length= cur->max_key - seq->param->max_key;
03627       uint32_t len= cur->min_key - cur[-1].min_key;
03628       if (! (min_key_length == max_key_length &&
03629           ! memcmp(cur[-1].min_key, cur[-1].max_key, len) &&
03630           ! key_tree->min_flag && !key_tree->max_flag))
03631       {
03632         seq->param->is_ror_scan= false;
03633         if (! key_tree->min_flag)
03634           cur->min_key_parts +=
03635             key_tree->next_key_part->store_min_key(seq->param->key[seq->keyno],
03636                                                    &cur->min_key,
03637                                                    &cur->min_key_flag);
03638         if (! key_tree->max_flag)
03639           cur->max_key_parts +=
03640             key_tree->next_key_part->store_max_key(seq->param->key[seq->keyno],
03641                                                    &cur->max_key,
03642                                                    &cur->max_key_flag);
03643         break;
03644       }
03645     }
03646 
03647     /*
03648       Ok, current atomic interval is in form "t.field=const" and there is
03649       next_key_part interval. Step right, and walk up from there.
03650     */
03651     key_tree= key_tree->next_key_part;
03652 
03653 walk_up_n_right:
03654     while (key_tree->prev && key_tree->prev != &optimizer::null_element)
03655     {
03656       /* Step up */
03657       key_tree= key_tree->prev;
03658     }
03659     step_down_to(seq, key_tree);
03660   }
03661 
03662   /* Ok got a tuple */
03663   RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
03664 
03665   range->ptr= (char*)(size_t)(key_tree->part);
03666   {
03667     range->range_flag= cur->min_key_flag | cur->max_key_flag;
03668 
03669     range->start_key.key=    seq->param->min_key;
03670     range->start_key.length= cur->min_key - seq->param->min_key;
03671     range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
03672     range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
03673                                                            HA_READ_KEY_EXACT);
03674 
03675     range->end_key.key=    seq->param->max_key;
03676     range->end_key.length= cur->max_key - seq->param->max_key;
03677     range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
03678                                                          HA_READ_AFTER_KEY);
03679     range->end_key.keypart_map= make_prev_keypart_map(cur->max_key_parts);
03680 
03681     if (!(cur->min_key_flag & ~NULL_RANGE) && !cur->max_key_flag &&
03682         (uint32_t)key_tree->part+1 == seq->param->table->key_info[seq->real_keyno].key_parts &&
03683         (seq->param->table->key_info[seq->real_keyno].flags & (HA_NOSAME)) ==
03684         HA_NOSAME &&
03685         range->start_key.length == range->end_key.length &&
03686         !memcmp(seq->param->min_key,seq->param->max_key,range->start_key.length))
03687       range->range_flag= UNIQUE_RANGE | (cur->min_key_flag & NULL_RANGE);
03688 
03689     if (seq->param->is_ror_scan)
03690     {
03691       /*
03692         If we get here, the condition on the key was converted to form
03693         "(keyXpart1 = c1) AND ... AND (keyXpart{key_tree->part - 1} = cN) AND
03694           somecond(keyXpart{key_tree->part})"
03695         Check if
03696           somecond is "keyXpart{key_tree->part} = const" and
03697           uncovered "tail" of KeyX parts is either empty or is identical to
03698           first members of clustered primary key.
03699       */
03700       if (!(!(cur->min_key_flag & ~NULL_RANGE) && !cur->max_key_flag &&
03701             (range->start_key.length == range->end_key.length) &&
03702             !memcmp(range->start_key.key, range->end_key.key, range->start_key.length) &&
03703             is_key_scan_ror(seq->param, seq->real_keyno, key_tree->part + 1)))
03704         seq->param->is_ror_scan= false;
03705     }
03706   }
03707   seq->param->range_count++;
03708   seq->param->max_key_part= max(seq->param->max_key_part,(uint32_t)key_tree->part);
03709   return 0;
03710 }
03711 
03712 
03713 /*
03714   Calculate cost and E(#rows) for a given index and intervals tree
03715 
03716   SYNOPSIS
03717     check_quick_select()
03718       param             Parameter from test_quick_select
03719       idx               Number of index to use in Parameter::key optimizer::SEL_TREE::key
03720       index_only        true  - assume only index tuples will be accessed
03721                         false - assume full table rows will be read
03722       tree              Transformed selection condition, tree->key[idx] holds
03723                         the intervals for the given index.
03724       update_tbl_stats  true <=> update table->quick_* with information
03725                         about range scan we've evaluated.
03726       mrr_flags   INOUT MRR access flags
03727       cost        OUT   Scan cost
03728 
03729   NOTES
03730     param->is_ror_scan is set to reflect if the key scan is a ROR (see
03731     is_key_scan_ror function for more info)
03732     param->table->quick_*, param->range_count (and maybe others) are
03733     updated with data of given key scan, see quick_range_seq_next for details.
03734 
03735   RETURN
03736     Estimate # of records to be retrieved.
03737     HA_POS_ERROR if estimate calculation failed due to table Cursor problems.
03738 */
03739 
03740 static
03741 ha_rows check_quick_select(Session *session,
03742                            optimizer::Parameter *param,
03743                            uint32_t idx,
03744                            bool index_only,
03745                            optimizer::SEL_ARG *tree,
03746                            bool update_tbl_stats,
03747                            uint32_t *mrr_flags,
03748                            uint32_t *bufsize,
03749                            optimizer::CostVector *cost)
03750 {
03751   SEL_ARG_RANGE_SEQ seq;
03752   RANGE_SEQ_IF seq_if = {sel_arg_range_seq_init, sel_arg_range_seq_next};
03753   Cursor *cursor= param->table->cursor;
03754   ha_rows rows;
03755   uint32_t keynr= param->real_keynr[idx];
03756 
03757   /* Handle cases when we don't have a valid non-empty list of range */
03758   if (! tree)
03759     return(HA_POS_ERROR);
03760   if (tree->type == optimizer::SEL_ARG::IMPOSSIBLE)
03761     return(0L);
03762   if (tree->type != optimizer::SEL_ARG::KEY_RANGE || tree->part != 0)
03763     return(HA_POS_ERROR);
03764 
03765   seq.keyno= idx;
03766   seq.real_keyno= keynr;
03767   seq.param= param;
03768   seq.start= tree;
03769 
03770   param->range_count=0;
03771   param->max_key_part=0;
03772 
03773   param->is_ror_scan= true;
03774   if (param->table->index_flags(keynr) & HA_KEY_SCAN_NOT_ROR)
03775     param->is_ror_scan= false;
03776 
03777   *mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
03778   *mrr_flags|= HA_MRR_NO_ASSOCIATION;
03779 
03780   bool pk_is_clustered= cursor->primary_key_is_clustered();
03781   if (index_only &&
03782       (param->table->index_flags(keynr) & HA_KEYREAD_ONLY) &&
03783       !(pk_is_clustered && keynr == param->table->getShare()->getPrimaryKey()))
03784      *mrr_flags |= HA_MRR_INDEX_ONLY;
03785 
03786   if (session->lex().sql_command != SQLCOM_SELECT)
03787     *mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
03788 
03789   *bufsize= param->session->variables.read_rnd_buff_size;
03790   rows= cursor->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
03791                                           bufsize, mrr_flags, cost);
03792   if (rows != HA_POS_ERROR)
03793   {
03794     param->table->quick_rows[keynr]=rows;
03795     if (update_tbl_stats)
03796     {
03797       param->table->quick_keys.set(keynr);
03798       param->table->quick_key_parts[keynr]=param->max_key_part+1;
03799       param->table->quick_n_ranges[keynr]= param->range_count;
03800       param->table->quick_condition_rows=
03801         min(param->table->quick_condition_rows, rows);
03802     }
03803   }
03804   /* Figure out if the key scan is ROR (returns rows in ROWID order) or not */
03805   enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
03806   if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
03807   {
03808     /*
03809       All scans are non-ROR scans for those index types.
03810       TODO: Don't have this logic here, make table engines return
03811       appropriate flags instead.
03812     */
03813     param->is_ror_scan= false;
03814   }
03815   else
03816   {
03817     /* Clustered PK scan is always a ROR scan (TODO: same as above) */
03818     if (param->table->getShare()->getPrimaryKey() == keynr && pk_is_clustered)
03819       param->is_ror_scan= true;
03820   }
03821 
03822   return(rows); //psergey-merge:todo: maintain first_null_comp.
03823 }
03824 
03825 
03826 /*
03827   Check if key scan on given index with equality conditions on first n key
03828   parts is a ROR scan.
03829 
03830   SYNOPSIS
03831     is_key_scan_ror()
03832       param  Parameter from test_quick_select
03833       keynr  Number of key in the table. The key must not be a clustered
03834              primary key.
03835       nparts Number of first key parts for which equality conditions
03836              are present.
03837 
03838   NOTES
03839     ROR (Rowid Ordered Retrieval) key scan is a key scan that produces
03840     ordered sequence of rowids (ha_xxx::cmp_ref is the comparison function)
03841 
03842     This function is needed to handle a practically-important special case:
03843     an index scan is a ROR scan if it is done using a condition in form
03844 
03845         "key1_1=c_1 AND ... AND key1_n=c_n"
03846 
03847     where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])
03848 
03849     and the table has a clustered Primary Key defined as
03850       PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k)
03851 
03852     i.e. the first key parts of it are identical to uncovered parts ot the
03853     key being scanned. This function assumes that the index flags do not
03854     include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
03855 
03856     Check (1) is made in quick_range_seq_next()
03857 
03858   RETURN
03859     true   The scan is ROR-scan
03860     false  Otherwise
03861 */
03862 
03863 static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts)
03864 {
03865   KeyInfo *table_key= param->table->key_info + keynr;
03866   KeyPartInfo *key_part= table_key->key_part + nparts;
03867   KeyPartInfo *key_part_end= (table_key->key_part +
03868                                 table_key->key_parts);
03869   uint32_t pk_number;
03870 
03871   for (KeyPartInfo *kp= table_key->key_part; kp < key_part; kp++)
03872   {
03873     uint16_t fieldnr= param->table->key_info[keynr].
03874                     key_part[kp - table_key->key_part].fieldnr - 1;
03875     if (param->table->getField(fieldnr)->key_length() != kp->length)
03876       return false;
03877   }
03878 
03879   if (key_part == key_part_end)
03880     return true;
03881 
03882   key_part= table_key->key_part + nparts;
03883   pk_number= param->table->getShare()->getPrimaryKey();
03884   if (!param->table->cursor->primary_key_is_clustered() || pk_number == MAX_KEY)
03885     return false;
03886 
03887   KeyPartInfo *pk_part= param->table->key_info[pk_number].key_part;
03888   KeyPartInfo *pk_part_end= pk_part +
03889                               param->table->key_info[pk_number].key_parts;
03890   for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
03891        ++key_part, ++pk_part)
03892   {
03893     if ((key_part->field != pk_part->field) ||
03894         (key_part->length != pk_part->length))
03895       return false;
03896   }
03897   return (key_part == key_part_end);
03898 }
03899 
03900 
03901 optimizer::QuickRangeSelect *
03902 optimizer::get_quick_select(Parameter *param,
03903                             uint32_t idx,
03904                             optimizer::SEL_ARG *key_tree,
03905                             uint32_t mrr_flags,
03906                             uint32_t mrr_buf_size,
03907                             memory::Root *parent_alloc)
03908 {
03909   optimizer::QuickRangeSelect *quick= new optimizer::QuickRangeSelect(param->session,
03910                                                                       param->table,
03911                                                                       param->real_keynr[idx],
03912                                                                       test(parent_alloc),
03913                                                                       NULL);
03914 
03915   if (quick)
03916   {
03917     if (get_quick_keys(param,
03918                        quick,
03919                        param->key[idx],
03920                        key_tree,
03921                        param->min_key,
03922                        0,
03923                        param->max_key,
03924                        0))
03925     {
03926       delete quick;
03927       quick= NULL;
03928     }
03929     else
03930     {
03931       quick->mrr_flags= mrr_flags;
03932       quick->mrr_buf_size= mrr_buf_size;
03933       quick->key_parts= parent_alloc
03934         ? (KEY_PART*)parent_alloc->memdup(param->key[idx], sizeof(KEY_PART)* param->table->key_info[param->real_keynr[idx]].key_parts)
03935         : (KEY_PART*)quick->alloc.memdup(param->key[idx], sizeof(KEY_PART)* param->table->key_info[param->real_keynr[idx]].key_parts);
03936     }
03937   }
03938   return quick;
03939 }
03940 
03941 
03942 /*
03943 ** Fix this to get all possible sub_ranges
03944 */
03945 bool
03946 optimizer::get_quick_keys(optimizer::Parameter *param,
03947                           optimizer::QuickRangeSelect *quick,
03948                           KEY_PART *key,
03949                           optimizer::SEL_ARG *key_tree,
03950                           unsigned char *min_key,
03951                           uint32_t min_key_flag,
03952                           unsigned char *max_key,
03953                           uint32_t max_key_flag)
03954 {
03955   optimizer::QuickRange *range= NULL;
03956   uint32_t flag;
03957   int min_part= key_tree->part - 1; // # of keypart values in min_key buffer
03958   int max_part= key_tree->part - 1; // # of keypart values in max_key buffer
03959 
03960   if (key_tree->left != &optimizer::null_element)
03961   {
03962     if (get_quick_keys(param,
03963                        quick,
03964                        key,
03965                        key_tree->left,
03966                        min_key,
03967                        min_key_flag,
03968                        max_key,
03969                        max_key_flag))
03970     {
03971       return 1;
03972     }
03973   }
03974   unsigned char *tmp_min_key= min_key,*tmp_max_key= max_key;
03975   min_part+= key_tree->store_min(key[key_tree->part].store_length,
03976                                  &tmp_min_key,min_key_flag);
03977   max_part+= key_tree->store_max(key[key_tree->part].store_length,
03978                                  &tmp_max_key,max_key_flag);
03979 
03980   if (key_tree->next_key_part &&
03981       key_tree->next_key_part->part == key_tree->part+1 &&
03982       key_tree->next_key_part->type == optimizer::SEL_ARG::KEY_RANGE)
03983   {             // const key as prefix
03984     if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
03985         memcmp(min_key, max_key, (uint32_t)(tmp_max_key - max_key))==0 &&
03986         key_tree->min_flag==0 && key_tree->max_flag==0)
03987     {
03988       if (get_quick_keys(param,
03989                          quick,
03990                          key,
03991                          key_tree->next_key_part,
03992                          tmp_min_key,
03993                          min_key_flag | key_tree->min_flag,
03994                          tmp_max_key,
03995                          max_key_flag | key_tree->max_flag))
03996       {
03997         return 1;
03998       }
03999       goto end;         // Ugly, but efficient
04000     }
04001     {
04002       uint32_t tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
04003       if (! tmp_min_flag)
04004       {
04005         min_part+= key_tree->next_key_part->store_min_key(key,
04006                                                           &tmp_min_key,
04007                                                           &tmp_min_flag);
04008       }
04009       if (! tmp_max_flag)
04010       {
04011         max_part+= key_tree->next_key_part->store_max_key(key,
04012                                                           &tmp_max_key,
04013                                                           &tmp_max_flag);
04014       }
04015       flag=tmp_min_flag | tmp_max_flag;
04016     }
04017   }
04018   else
04019   {
04020     flag= key_tree->min_flag | key_tree->max_flag;
04021   }
04022 
04023   /*
04024     Ensure that some part of min_key and max_key are used.  If not,
04025     regard this as no lower/upper range
04026   */
04027   if (tmp_min_key != param->min_key)
04028   {
04029     flag&= ~NO_MIN_RANGE;
04030   }
04031   else
04032   {
04033     flag|= NO_MIN_RANGE;
04034   }
04035   if (tmp_max_key != param->max_key)
04036   {
04037     flag&= ~NO_MAX_RANGE;
04038   }
04039   else
04040   {
04041     flag|= NO_MAX_RANGE;
04042   }
04043   if (flag == 0)
04044   {
04045     uint32_t length= (uint32_t) (tmp_min_key - param->min_key);
04046     if (length == (uint32_t) (tmp_max_key - param->max_key) &&
04047         ! memcmp(param->min_key,param->max_key,length))
04048     {
04049       KeyInfo *table_key= quick->head->key_info+quick->index;
04050       flag= EQ_RANGE;
04051       if ((table_key->flags & (HA_NOSAME)) == HA_NOSAME &&
04052           key->part == table_key->key_parts-1)
04053       {
04054         if (! (table_key->flags & HA_NULL_PART_KEY) ||
04055             ! null_part_in_key(key,
04056                                param->min_key,
04057                                (uint32_t) (tmp_min_key - param->min_key)))
04058         {
04059           flag|= UNIQUE_RANGE;
04060         }
04061         else
04062         {
04063           flag|= NULL_RANGE;
04064         }
04065       }
04066     }
04067   }
04068 
04069   /* Get range for retrieving rows in QUICK_SELECT::get_next */
04070   range= new optimizer::QuickRange(param->min_key,
04071                                            (uint32_t) (tmp_min_key - param->min_key),
04072                                            min_part >=0 ? make_keypart_map(min_part) : 0,
04073                                            param->max_key,
04074                                            (uint32_t) (tmp_max_key - param->max_key),
04075                                            max_part >=0 ? make_keypart_map(max_part) : 0,
04076                                            flag);
04077 
04078   set_if_bigger(quick->max_used_key_length, (uint32_t)range->min_length);
04079   set_if_bigger(quick->max_used_key_length, (uint32_t)range->max_length);
04080   set_if_bigger(quick->used_key_parts, (uint32_t) key_tree->part+1);
04081   quick->ranges.push_back(&range);
04082 
04083  end:
04084   if (key_tree->right != &optimizer::null_element)
04085   {
04086     return get_quick_keys(param,
04087                           quick,
04088                           key,
04089                           key_tree->right,
04090                           min_key,
04091                           min_key_flag,
04092                           max_key,
04093                           max_key_flag);
04094   }
04095   return 0;
04096 }
04097 
04098 /*
04099   Return true if any part of the key is NULL
04100 
04101   SYNOPSIS
04102     null_part_in_key()
04103       key_part  Array of key parts (index description)
04104       key       Key values tuple
04105       length    Length of key values tuple in bytes.
04106 
04107   RETURN
04108     true   The tuple has at least one "keypartX is NULL"
04109     false  Otherwise
04110 */
04111 
04112 static bool null_part_in_key(KEY_PART *key_part, const unsigned char *key, uint32_t length)
04113 {
04114   for (const unsigned char *end=key+length ;
04115        key < end;
04116        key+= key_part++->store_length)
04117   {
04118     if (key_part->null_bit && *key)
04119       return 1;
04120   }
04121   return 0;
04122 }
04123 
04124 
04125 bool optimizer::QuickSelectInterface::is_keys_used(const boost::dynamic_bitset<>& fields)
04126 {
04127   return is_key_used(head, index, fields);
04128 }
04129 
04130 
04131 /*
04132   Create quick select from ref/ref_or_null scan.
04133 
04134   SYNOPSIS
04135     get_quick_select_for_ref()
04136       session      Thread handle
04137       table    Table to access
04138       ref      ref[_or_null] scan parameters
04139       records  Estimate of number of records (needed only to construct
04140                quick select)
04141   NOTES
04142     This allocates things in a new memory root, as this may be called many
04143     times during a query.
04144 
04145   RETURN
04146     Quick select that retrieves the same rows as passed ref scan
04147     NULL on error.
04148 */
04149 
04150 optimizer::QuickRangeSelect *optimizer::get_quick_select_for_ref(Session *session,
04151                                                                  Table *table,
04152                                                                  table_reference_st *ref,
04153                                                                  ha_rows records)
04154 {
04155   memory::Root *old_root= NULL;
04156   memory::Root *alloc= NULL;
04157   KeyInfo *key_info = &table->key_info[ref->key];
04158   KEY_PART *key_part;
04159   optimizer::QuickRange *range= NULL;
04160   uint32_t part;
04161   optimizer::CostVector cost;
04162 
04163   old_root= session->mem_root;
04164   /* The following call may change session->mem_root */
04165   optimizer::QuickRangeSelect *quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0);
04166   /* save mem_root set by QuickRangeSelect constructor */
04167   alloc= session->mem_root;
04168   /*
04169     return back default mem_root (session->mem_root) changed by
04170     QuickRangeSelect constructor
04171   */
04172   session->mem_root= old_root;
04173 
04174   if (! quick)
04175     return 0;     /* no ranges found */
04176   if (quick->init())
04177     goto err;
04178   quick->records= records;
04179 
04180   if (cp_buffer_from_ref(session, ref) && session->is_fatal_error)
04181     goto err;                                   // out of memory
04182   range= new (*alloc) optimizer::QuickRange;
04183 
04184   range->min_key= range->max_key= ref->key_buff;
04185   range->min_length= range->max_length= ref->key_length;
04186   range->min_keypart_map= range->max_keypart_map=
04187     make_prev_keypart_map(ref->key_parts);
04188   range->flag= (ref->key_length == key_info->key_length && (key_info->flags & HA_END_SPACE_KEY) == 0) ? EQ_RANGE : 0;
04189 
04190   quick->key_parts=key_part= new (quick->alloc) KEY_PART[ref->key_parts];
04191 
04192   for (part=0 ; part < ref->key_parts ;part++,key_part++)
04193   {
04194     key_part->part=part;
04195     key_part->field=        key_info->key_part[part].field;
04196     key_part->length=       key_info->key_part[part].length;
04197     key_part->store_length= key_info->key_part[part].store_length;
04198     key_part->null_bit=     key_info->key_part[part].null_bit;
04199     key_part->flag=         (uint8_t) key_info->key_part[part].key_part_flag;
04200   }
04201   quick->ranges.push_back(&range);
04202 
04203   /*
04204      Add a NULL range if REF_OR_NULL optimization is used.
04205      For example:
04206        if we have "WHERE A=2 OR A IS NULL" we created the (A=2) range above
04207        and have ref->null_ref_key set. Will create a new NULL range here.
04208   */
04209   if (ref->null_ref_key)
04210   {
04211     optimizer::QuickRange *null_range= NULL;
04212 
04213     *ref->null_ref_key= 1;    // Set null byte then create a range
04214     null_range= new (alloc)
04215           optimizer::QuickRange(ref->key_buff, ref->key_length,
04216                                  make_prev_keypart_map(ref->key_parts),
04217                                  ref->key_buff, ref->key_length,
04218                                  make_prev_keypart_map(ref->key_parts), EQ_RANGE);
04219     *ref->null_ref_key= 0;    // Clear null byte
04220     quick->ranges.push_back(&null_range);
04221   }
04222 
04223   /* Call multi_range_read_info() to get the MRR flags and buffer size */
04224   quick->mrr_flags= HA_MRR_NO_ASSOCIATION |
04225                     (table->key_read ? HA_MRR_INDEX_ONLY : 0);
04226   if (session->lex().sql_command != SQLCOM_SELECT)
04227     quick->mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
04228 
04229   quick->mrr_buf_size= session->variables.read_rnd_buff_size;
04230   if (table->cursor->multi_range_read_info(quick->index, 1, (uint32_t)records, &quick->mrr_buf_size, &quick->mrr_flags, &cost))
04231     goto err;
04232 
04233   return quick;
04234 err:
04235   delete quick;
04236   return 0;
04237 }
04238 
04239 
04240 /*
04241   Range sequence interface implementation for array<QuickRange>: initialize
04242 
04243   SYNOPSIS
04244     quick_range_seq_init()
04245       init_param  Caller-opaque paramenter: QuickRangeSelect* pointer
04246       n_ranges    Number of ranges in the sequence (ignored)
04247       flags       MRR flags (currently not used)
04248 
04249   RETURN
04250     Opaque value to be passed to quick_range_seq_next
04251 */
04252 
04253 range_seq_t optimizer::quick_range_seq_init(void *init_param, uint32_t, uint32_t)
04254 {
04255   optimizer::QuickRangeSelect *quick= (optimizer::QuickRangeSelect*)init_param;
04256   quick->qr_traversal_ctx.first=  (optimizer::QuickRange**)quick->ranges.buffer;
04257   quick->qr_traversal_ctx.cur=    (optimizer::QuickRange**)quick->ranges.buffer;
04258   quick->qr_traversal_ctx.last=   quick->qr_traversal_ctx.cur +
04259                                   quick->ranges.size();
04260   return &quick->qr_traversal_ctx;
04261 }
04262 
04263 
04264 /*
04265   Range sequence interface implementation for array<QuickRange>: get next
04266 
04267   SYNOPSIS
04268     quick_range_seq_next()
04269       rseq        Value returned from quick_range_seq_init
04270       range  OUT  Store information about the range here
04271 
04272   RETURN
04273     0  Ok
04274     1  No more ranges in the sequence
04275 */
04276 uint32_t optimizer::quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
04277 {
04278   QuickRangeSequenceContext *ctx= (QuickRangeSequenceContext*) rseq;
04279 
04280   if (ctx->cur == ctx->last)
04281     return 1; /* no more ranges */
04282 
04283   optimizer::QuickRange *cur= *(ctx->cur);
04284   key_range *start_key= &range->start_key;
04285   key_range *end_key= &range->end_key;
04286 
04287   start_key->key= cur->min_key;
04288   start_key->length= cur->min_length;
04289   start_key->keypart_map= cur->min_keypart_map;
04290   start_key->flag= ((cur->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
04291                                              (cur->flag & EQ_RANGE) ?
04292                                              HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
04293   end_key->key= cur->max_key;
04294   end_key->length= cur->max_length;
04295   end_key->keypart_map= cur->max_keypart_map;
04296   /*
04297     We use HA_READ_AFTER_KEY here because if we are reading on a key
04298     prefix. We want to find all keys with this prefix.
04299   */
04300   end_key->flag= (cur->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
04301                                          HA_READ_AFTER_KEY);
04302   range->range_flag= cur->flag;
04303   ctx->cur++;
04304   return 0;
04305 }
04306 
04307 
04308 static inline uint32_t get_field_keypart(KeyInfo *index, Field *field);
04309 
04310 static inline optimizer::SEL_ARG * get_index_range_tree(uint32_t index,
04311                                                         optimizer::SEL_TREE *range_tree,
04312                                                         optimizer::Parameter *param,
04313                                                         uint32_t *param_idx);
04314 
04315 static bool get_constant_key_infix(KeyInfo *index_info,
04316                                    optimizer::SEL_ARG *index_range_tree,
04317                                    KeyPartInfo *first_non_group_part,
04318                                    KeyPartInfo *min_max_arg_part,
04319                                    KeyPartInfo *last_part,
04320                                    Session *session,
04321                                    unsigned char *key_infix,
04322                                    uint32_t *key_infix_len,
04323                                    KeyPartInfo **first_non_infix_part);
04324 
04325 static bool check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item);
04326 
04327 static void
04328 cost_group_min_max(Table* table,
04329                    KeyInfo *index_info,
04330                    uint32_t used_key_parts,
04331                    uint32_t group_key_parts,
04332                    optimizer::SEL_TREE *range_tree,
04333                    optimizer::SEL_ARG *index_tree,
04334                    ha_rows quick_prefix_records,
04335                    bool have_min,
04336                    bool have_max,
04337                    double *read_cost,
04338                    ha_rows *records);
04339 
04340 
04341 /*
04342   Test if this access method is applicable to a GROUP query with MIN/MAX
04343   functions, and if so, construct a new TRP object.
04344 
04345   SYNOPSIS
04346     get_best_group_min_max()
04347     param    Parameter from test_quick_select
04348     sel_tree Range tree generated by get_mm_tree
04349 
04350   DESCRIPTION
04351     Test whether a query can be computed via a QuickGroupMinMaxSelect.
04352     Queries computable via a QuickGroupMinMaxSelect must satisfy the
04353     following conditions:
04354     A) Table T has at least one compound index I of the form:
04355        I = <A_1, ...,A_k, [B_1,..., B_m], C, [D_1,...,D_n]>
04356     B) Query conditions:
04357     B0. Q is over a single table T.
04358     B1. The attributes referenced by Q are a subset of the attributes of I.
04359     B2. All attributes QA in Q can be divided into 3 overlapping groups:
04360         - SA = {S_1, ..., S_l, [C]} - from the SELECT clause, where C is
04361           referenced by any number of MIN and/or MAX functions if present.
04362         - WA = {W_1, ..., W_p} - from the WHERE clause
04363         - GA = <G_1, ..., G_k> - from the GROUP BY clause (if any)
04364              = SA              - if Q is a DISTINCT query (based on the
04365                                  equivalence of DISTINCT and GROUP queries.
04366         - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
04367           GROUP BY and not referenced by MIN/MAX functions.
04368         with the following properties specified below.
04369     B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not
04370         applicable.
04371 
04372     SA1. There is at most one attribute in SA referenced by any number of
04373          MIN and/or MAX functions which, which if present, is denoted as C.
04374     SA2. The position of the C attribute in the index is after the last A_k.
04375     SA3. The attribute C can be referenced in the WHERE clause only in
04376          predicates of the forms:
04377          - (C {< | <= | > | >= | =} const)
04378          - (const {< | <= | > | >= | =} C)
04379          - (C between const_i and const_j)
04380          - C IS NULL
04381          - C IS NOT NULL
04382          - C != const
04383     SA4. If Q has a GROUP BY clause, there are no other aggregate functions
04384          except MIN and MAX. For queries with DISTINCT, aggregate functions
04385          are allowed.
04386     SA5. The select list in DISTINCT queries should not contain expressions.
04387     GA1. If Q has a GROUP BY clause, then GA is a prefix of I. That is, if
04388          G_i = A_j => i = j.
04389     GA2. If Q has a DISTINCT clause, then there is a permutation of SA that
04390          forms a prefix of I. This permutation is used as the GROUP clause
04391          when the DISTINCT query is converted to a GROUP query.
04392     GA3. The attributes in GA may participate in arbitrary predicates, divided
04393          into two groups:
04394          - RNG(G_1,...,G_q ; where q <= k) is a range condition over the
04395            attributes of a prefix of GA
04396          - PA(G_i1,...G_iq) is an arbitrary predicate over an arbitrary subset
04397            of GA. Since P is applied to only GROUP attributes it filters some
04398            groups, and thus can be applied after the grouping.
04399     GA4. There are no expressions among G_i, just direct column references.
04400     NGA1.If in the index I there is a gap between the last GROUP attribute G_k,
04401          and the MIN/MAX attribute C, then NGA must consist of exactly the
04402          index attributes that constitute the gap. As a result there is a
04403          permutation of NGA that coincides with the gap in the index
04404          <B_1, ..., B_m>.
04405     NGA2.If BA <> {}, then the WHERE clause must contain a conjunction EQ of
04406          equality conditions for all NG_i of the form (NG_i = const) or
04407          (const = NG_i), such that each NG_i is referenced in exactly one
04408          conjunct. Informally, the predicates provide constants to fill the
04409          gap in the index.
04410     WA1. There are no other attributes in the WHERE clause except the ones
04411          referenced in predicates RNG, PA, PC, EQ defined above. Therefore
04412          WA is subset of (GA union NGA union C) for GA,NGA,C that pass the
04413          above tests. By transitivity then it also follows that each WA_i
04414          participates in the index I (if this was already tested for GA, NGA
04415          and C).
04416 
04417     C) Overall query form:
04418        SELECT EXPR([A_1,...,A_k], [B_1,...,B_m], [MIN(C)], [MAX(C)])
04419          FROM T
04420         WHERE [RNG(A_1,...,A_p ; where p <= k)]
04421          [AND EQ(B_1,...,B_m)]
04422          [AND PC(C)]
04423          [AND PA(A_i1,...,A_iq)]
04424        GROUP BY A_1,...,A_k
04425        [HAVING PH(A_1, ..., B_1,..., C)]
04426     where EXPR(...) is an arbitrary expression over some or all SELECT fields,
04427     or:
04428        SELECT DISTINCT A_i1,...,A_ik
04429          FROM T
04430         WHERE [RNG(A_1,...,A_p ; where p <= k)]
04431          [AND PA(A_i1,...,A_iq)];
04432 
04433   NOTES
04434     If the current query satisfies the conditions above, and if
04435     (mem_root! = NULL), then the function constructs and returns a new TRP
04436     object, that is later used to construct a new QuickGroupMinMaxSelect.
04437     If (mem_root == NULL), then the function only tests whether the current
04438     query satisfies the conditions above, and, if so, sets
04439     is_applicable = true.
04440 
04441     Queries with DISTINCT for which index access can be used are transformed
04442     into equivalent group-by queries of the form:
04443 
04444     SELECT A_1,...,A_k FROM T
04445      WHERE [RNG(A_1,...,A_p ; where p <= k)]
04446       [AND PA(A_i1,...,A_iq)]
04447     GROUP BY A_1,...,A_k;
04448 
04449     The group-by list is a permutation of the select attributes, according
04450     to their order in the index.
04451 
04452   TODO
04453   - What happens if the query groups by the MIN/MAX field, and there is no
04454     other field as in: "select min(a) from t1 group by a" ?
04455   - We assume that the general correctness of the GROUP-BY query was checked
04456     before this point. Is this correct, or do we have to check it completely?
04457   - Lift the limitation in condition (B3), that is, make this access method
04458     applicable to ROLLUP queries.
04459 
04460   RETURN
04461     If mem_root != NULL
04462     - valid GroupMinMaxReadPlan object if this QUICK class can be used for
04463       the query
04464     -  NULL o/w.
04465     If mem_root == NULL
04466     - NULL
04467 */
04468 static optimizer::GroupMinMaxReadPlan *
04469 get_best_group_min_max(optimizer::Parameter *param, optimizer::SEL_TREE *tree)
04470 {
04471   Session *session= param->session;
04472   Join *join= session->lex().current_select->join;
04473   Table *table= param->table;
04474   bool have_min= false;              /* true if there is a MIN function. */
04475   bool have_max= false;              /* true if there is a MAX function. */
04476   Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
04477   KeyPartInfo *min_max_arg_part= NULL; /* The corresponding keypart. */
04478   uint32_t group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
04479   KeyInfo *index_info= NULL;    /* The index chosen for data access. */
04480   uint32_t index= 0;            /* The id of the chosen index. */
04481   uint32_t group_key_parts= 0;  // Number of index key parts in the group prefix.
04482   uint32_t used_key_parts= 0;   /* Number of index key parts used for access. */
04483   unsigned char key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
04484   uint32_t key_infix_len= 0;          /* Length of key_infix. */
04485   optimizer::GroupMinMaxReadPlan *read_plan= NULL; /* The eventually constructed TRP. */
04486   uint32_t key_part_nr;
04487   Order *tmp_group= NULL;
04488   Item *item= NULL;
04489   Item_field *item_field= NULL;
04490 
04491   /* Perform few 'cheap' tests whether this access method is applicable. */
04492   if (! join)
04493     return NULL;        /* This is not a select statement. */
04494 
04495   if ((join->tables != 1) ||  /* The query must reference one table. */
04496       ((! join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
04497        (! join->select_distinct)) ||
04498       (join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
04499     return NULL;
04500   if (table->getShare()->sizeKeys() == 0)        /* There are no indexes to use. */
04501     return NULL;
04502 
04503   /* Analyze the query in more detail. */
04504   List<Item>::iterator select_items_it(join->fields_list.begin());
04505 
04506   /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
04507   if (join->make_sum_func_list(join->all_fields, join->fields_list, 1))
04508     return NULL;
04509 
04510   if (join->sum_funcs[0])
04511   {
04512     Item_sum *min_max_item= NULL;
04513     Item_sum **func_ptr= join->sum_funcs;
04514     while ((min_max_item= *(func_ptr++)))
04515     {
04516       if (min_max_item->sum_func() == Item_sum::MIN_FUNC)
04517         have_min= true;
04518       else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
04519         have_max= true;
04520       else
04521         return NULL;
04522 
04523       /* The argument of MIN/MAX. */
04524       Item *expr= min_max_item->args[0]->real_item();
04525       if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
04526       {
04527         if (! min_max_arg_item)
04528           min_max_arg_item= (Item_field*) expr;
04529         else if (! min_max_arg_item->eq(expr, 1))
04530           return NULL;
04531       }
04532       else
04533         return NULL;
04534     }
04535   }
04536 
04537   /* Check (SA5). */
04538   if (join->select_distinct)
04539   {
04540     while ((item= select_items_it++))
04541     {
04542       if (item->type() != Item::FIELD_ITEM)
04543         return NULL;
04544     }
04545   }
04546 
04547   /* Check (GA4) - that there are no expressions among the group attributes. */
04548   for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
04549   {
04550     if ((*tmp_group->item)->type() != Item::FIELD_ITEM)
04551       return NULL;
04552   }
04553 
04554   /*
04555     Check that table has at least one compound index such that the conditions
04556     (GA1,GA2) are all true. If there is more than one such index, select the
04557     first one. Here we set the variables: group_prefix_len and index_info.
04558   */
04559   KeyInfo *cur_index_info= table->key_info;
04560   KeyInfo *cur_index_info_end= cur_index_info + table->getShare()->sizeKeys();
04561   KeyPartInfo *cur_part= NULL;
04562   KeyPartInfo *end_part= NULL; /* Last part for loops. */
04563   /* Last index part. */
04564   KeyPartInfo *last_part= NULL;
04565   KeyPartInfo *first_non_group_part= NULL;
04566   KeyPartInfo *first_non_infix_part= NULL;
04567   uint32_t key_infix_parts= 0;
04568   uint32_t cur_group_key_parts= 0;
04569   uint32_t cur_group_prefix_len= 0;
04570   /* Cost-related variables for the best index so far. */
04571   double best_read_cost= DBL_MAX;
04572   ha_rows best_records= 0;
04573   optimizer::SEL_ARG *best_index_tree= NULL;
04574   ha_rows best_quick_prefix_records= 0;
04575   uint32_t best_param_idx= 0;
04576   double cur_read_cost= DBL_MAX;
04577   ha_rows cur_records;
04578   optimizer::SEL_ARG *cur_index_tree= NULL;
04579   ha_rows cur_quick_prefix_records= 0;
04580   uint32_t cur_param_idx= MAX_KEY;
04581   key_map used_key_parts_map;
04582   uint32_t cur_key_infix_len= 0;
04583   unsigned char cur_key_infix[MAX_KEY_LENGTH];
04584   uint32_t cur_used_key_parts= 0;
04585   uint32_t pk= param->table->getShare()->getPrimaryKey();
04586 
04587   for (uint32_t cur_index= 0;
04588        cur_index_info != cur_index_info_end;
04589        cur_index_info++, cur_index++)
04590   {
04591     /* Check (B1) - if current index is covering. */
04592     if (! table->covering_keys.test(cur_index))
04593       goto next_index;
04594 
04595     /*
04596       If the current storage manager is such that it appends the primary key to
04597       each index, then the above condition is insufficient to check if the
04598       index is covering. In such cases it may happen that some fields are
04599       covered by the PK index, but not by the current index. Since we can't
04600       use the concatenation of both indexes for index lookup, such an index
04601       does not qualify as covering in our case. If this is the case, below
04602       we check that all query fields are indeed covered by 'cur_index'.
04603     */
04604     if (pk < MAX_KEY && cur_index != pk &&
04605         (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)))
04606     {
04607       /* For each table field */
04608       for (uint32_t i= 0; i < table->getShare()->sizeFields(); i++)
04609       {
04610         Field *cur_field= table->getField(i);
04611         /*
04612           If the field is used in the current query ensure that it's
04613           part of 'cur_index'
04614         */
04615         if ((cur_field->isReadSet()) &&
04616             ! cur_field->part_of_key_not_clustered.test(cur_index))
04617           goto next_index;                  // Field was not part of key
04618       }
04619     }
04620 
04621     /*
04622       Check (GA1) for GROUP BY queries.
04623     */
04624     if (join->group_list)
04625     {
04626       cur_part= cur_index_info->key_part;
04627       end_part= cur_part + cur_index_info->key_parts;
04628       /* Iterate in parallel over the GROUP list and the index parts. */
04629       for (tmp_group= join->group_list;
04630            tmp_group && (cur_part != end_part);
04631            tmp_group= tmp_group->next, cur_part++)
04632       {
04633         /*
04634           TODO:
04635           tmp_group::item is an array of Item, is it OK to consider only the
04636           first Item? If so, then why? What is the array for?
04637         */
04638         /* Above we already checked that all group items are fields. */
04639         assert((*tmp_group->item)->type() == Item::FIELD_ITEM);
04640         Item_field *group_field= (Item_field *) (*tmp_group->item);
04641         if (group_field->field->eq(cur_part->field))
04642         {
04643           cur_group_prefix_len+= cur_part->store_length;
04644           ++cur_group_key_parts;
04645         }
04646         else
04647           goto next_index;
04648       }
04649     }
04650     /*
04651       Check (GA2) if this is a DISTINCT query.
04652       If GA2, then Store a new order_st object in group_fields_array at the
04653       position of the key part of item_field->field. Thus we get the order_st
04654       objects for each field ordered as the corresponding key parts.
04655       Later group_fields_array of order_st objects is used to convert the query
04656       to a GROUP query.
04657     */
04658     else if (join->select_distinct)
04659     {
04660       select_items_it= join->fields_list.begin();
04661       used_key_parts_map.reset();
04662       uint32_t max_key_part= 0;
04663       while ((item= select_items_it++))
04664       {
04665         item_field= (Item_field*) item; /* (SA5) already checked above. */
04666         /* Find the order of the key part in the index. */
04667         key_part_nr= get_field_keypart(cur_index_info, item_field->field);
04668         /*
04669           Check if this attribute was already present in the select list.
04670           If it was present, then its corresponding key part was alredy used.
04671         */
04672         if (used_key_parts_map.test(key_part_nr))
04673           continue;
04674         if (key_part_nr < 1 || key_part_nr > join->fields_list.size())
04675           goto next_index;
04676         cur_part= cur_index_info->key_part + key_part_nr - 1;
04677         cur_group_prefix_len+= cur_part->store_length;
04678         used_key_parts_map.set(key_part_nr);
04679         ++cur_group_key_parts;
04680         max_key_part= max(max_key_part,key_part_nr);
04681       }
04682       /*
04683         Check that used key parts forms a prefix of the index.
04684         To check this we compare bits in all_parts and cur_parts.
04685         all_parts have all bits set from 0 to (max_key_part-1).
04686         cur_parts have bits set for only used keyparts.
04687       */
04688       key_map all_parts;
04689       key_map cur_parts;
04690       for (uint32_t pos= 0; pos < max_key_part; pos++)
04691         all_parts.set(pos);
04692       cur_parts= used_key_parts_map >> 1;
04693       if (all_parts != cur_parts)
04694         goto next_index;
04695     }
04696     else
04697       assert(false);
04698 
04699     /* Check (SA2). */
04700     if (min_max_arg_item)
04701     {
04702       key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
04703       if (key_part_nr <= cur_group_key_parts)
04704         goto next_index;
04705       min_max_arg_part= cur_index_info->key_part + key_part_nr - 1;
04706     }
04707 
04708     /*
04709       Check (NGA1, NGA2) and extract a sequence of constants to be used as part
04710       of all search keys.
04711     */
04712 
04713     /*
04714       If there is MIN/MAX, each keypart between the last group part and the
04715       MIN/MAX part must participate in one equality with constants, and all
04716       keyparts after the MIN/MAX part must not be referenced in the query.
04717 
04718       If there is no MIN/MAX, the keyparts after the last group part can be
04719       referenced only in equalities with constants, and the referenced keyparts
04720       must form a sequence without any gaps that starts immediately after the
04721       last group keypart.
04722     */
04723     last_part= cur_index_info->key_part + cur_index_info->key_parts;
04724     first_non_group_part= (cur_group_key_parts < cur_index_info->key_parts) ?
04725                           cur_index_info->key_part + cur_group_key_parts :
04726                           NULL;
04727     first_non_infix_part= min_max_arg_part ?
04728                           (min_max_arg_part < last_part) ?
04729                              min_max_arg_part :
04730                              NULL :
04731                            NULL;
04732     if (first_non_group_part &&
04733         (! min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
04734     {
04735       if (tree)
04736       {
04737         uint32_t dummy;
04738         optimizer::SEL_ARG *index_range_tree= get_index_range_tree(cur_index,
04739                                                                    tree,
04740                                                                    param,
04741                                                                    &dummy);
04742         if (! get_constant_key_infix(cur_index_info,
04743                                      index_range_tree,
04744                                      first_non_group_part,
04745                                      min_max_arg_part,
04746                                      last_part,
04747                                      session,
04748                                      cur_key_infix,
04749                                      &cur_key_infix_len,
04750                                      &first_non_infix_part))
04751         {
04752           goto next_index;
04753         }
04754       }
04755       else if (min_max_arg_part &&
04756                (min_max_arg_part - first_non_group_part > 0))
04757       {
04758         /*
04759           There is a gap but no range tree, thus no predicates at all for the
04760           non-group keyparts.
04761         */
04762         goto next_index;
04763       }
04764       else if (first_non_group_part && join->conds)
04765       {
04766         /*
04767           If there is no MIN/MAX function in the query, but some index
04768           key part is referenced in the WHERE clause, then this index
04769           cannot be used because the WHERE condition over the keypart's
04770           field cannot be 'pushed' to the index (because there is no
04771           range 'tree'), and the WHERE clause must be evaluated before
04772           GROUP BY/DISTINCT.
04773         */
04774         /*
04775           Store the first and last keyparts that need to be analyzed
04776           into one array that can be passed as parameter.
04777         */
04778         KeyPartInfo *key_part_range[2];
04779         key_part_range[0]= first_non_group_part;
04780         key_part_range[1]= last_part;
04781 
04782         /* Check if cur_part is referenced in the WHERE clause. */
04783         if (join->conds->walk(&Item::find_item_in_field_list_processor,
04784                               0,
04785                               (unsigned char*) key_part_range))
04786           goto next_index;
04787       }
04788     }
04789 
04790     /*
04791       Test (WA1) partially - that no other keypart after the last infix part is
04792       referenced in the query.
04793     */
04794     if (first_non_infix_part)
04795     {
04796       cur_part= first_non_infix_part +
04797                 (min_max_arg_part && (min_max_arg_part < last_part));
04798       for (; cur_part != last_part; cur_part++)
04799       {
04800         if (cur_part->field->isReadSet())
04801           goto next_index;
04802       }
04803     }
04804 
04805     /* If we got to this point, cur_index_info passes the test. */
04806     key_infix_parts= cur_key_infix_len ?
04807                      (first_non_infix_part - first_non_group_part) : 0;
04808     cur_used_key_parts= cur_group_key_parts + key_infix_parts;
04809 
04810     /* Compute the cost of using this index. */
04811     if (tree)
04812     {
04813       /* Find the SEL_ARG sub-tree that corresponds to the chosen index. */
04814       cur_index_tree= get_index_range_tree(cur_index,
04815                                            tree,
04816                                            param,
04817                                            &cur_param_idx);
04818       /* Check if this range tree can be used for prefix retrieval. */
04819       optimizer::CostVector dummy_cost;
04820       uint32_t mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
04821       uint32_t mrr_bufsize= 0;
04822       cur_quick_prefix_records= check_quick_select(session,
04823                                                    param,
04824                                                    cur_param_idx,
04825                                                    false /*don't care*/,
04826                                                    cur_index_tree,
04827                                                    true,
04828                                                    &mrr_flags,
04829                                                    &mrr_bufsize,
04830                                                    &dummy_cost);
04831     }
04832     cost_group_min_max(table,
04833                        cur_index_info,
04834                        cur_used_key_parts,
04835                        cur_group_key_parts,
04836                        tree,
04837                        cur_index_tree,
04838                        cur_quick_prefix_records,
04839                        have_min,
04840                        have_max,
04841                        &cur_read_cost,
04842                        &cur_records);
04843     /*
04844       If cur_read_cost is lower than best_read_cost use cur_index.
04845       Do not compare doubles directly because they may have different
04846       representations (64 vs. 80 bits).
04847     */
04848     if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
04849     {
04850       assert(tree != 0 || cur_param_idx == MAX_KEY);
04851       index_info= cur_index_info;
04852       index= cur_index;
04853       best_read_cost= cur_read_cost;
04854       best_records= cur_records;
04855       best_index_tree= cur_index_tree;
04856       best_quick_prefix_records= cur_quick_prefix_records;
04857       best_param_idx= cur_param_idx;
04858       group_key_parts= cur_group_key_parts;
04859       group_prefix_len= cur_group_prefix_len;
04860       key_infix_len= cur_key_infix_len;
04861 
04862       if (key_infix_len)
04863       {
04864         memcpy(key_infix, cur_key_infix, sizeof (key_infix));
04865       }
04866 
04867       used_key_parts= cur_used_key_parts;
04868     }
04869 
04870   next_index:
04871     cur_group_key_parts= 0;
04872     cur_group_prefix_len= 0;
04873     cur_key_infix_len= 0;
04874   }
04875   if (! index_info) /* No usable index found. */
04876     return NULL;
04877 
04878   /* Check (SA3) for the where clause. */
04879   if (join->conds && min_max_arg_item &&
04880       ! check_group_min_max_predicates(join->conds, min_max_arg_item))
04881     return NULL;
04882 
04883   /* The query passes all tests, so construct a new TRP object. */
04884   read_plan= new (*param->mem_root) optimizer::GroupMinMaxReadPlan(have_min,
04885                                                         have_max,
04886                                                         min_max_arg_part,
04887                                                         group_prefix_len,
04888                                                         used_key_parts,
04889                                                         group_key_parts,
04890                                                         index_info,
04891                                                         index,
04892                                                         key_infix_len,
04893                                                         (key_infix_len > 0) ? key_infix : NULL,
04894                                                         tree,
04895                                                         best_index_tree,
04896                                                         best_param_idx,
04897                                                         best_quick_prefix_records);
04898   if (tree && read_plan->quick_prefix_records == 0)
04899     return NULL;
04900   read_plan->read_cost= best_read_cost;
04901   read_plan->records= best_records;
04902   return read_plan;
04903 }
04904 
04905 
04906 /*
04907   Check that the MIN/MAX attribute participates only in range predicates
04908   with constants.
04909 
04910   SYNOPSIS
04911     check_group_min_max_predicates()
04912     cond              tree (or subtree) describing all or part of the WHERE
04913                       clause being analyzed
04914     min_max_arg_item  the field referenced by the MIN/MAX function(s)
04915     min_max_arg_part  the keypart of the MIN/MAX argument if any
04916 
04917   DESCRIPTION
04918     The function walks recursively over the cond tree representing a WHERE
04919     clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
04920     aggregate function, it is referenced only by one of the following
04921     predicates: {=, !=, <, <=, >, >=, between, is null, is not null}.
04922 
04923   RETURN
04924     true  if cond passes the test
04925     false o/w
04926 */
04927 static bool check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item)
04928 {
04929   assert(cond && min_max_arg_item);
04930 
04931   cond= cond->real_item();
04932   Item::Type cond_type= cond->type();
04933   if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
04934   {
04935     List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
04936     Item *and_or_arg= NULL;
04937     while ((and_or_arg= li++))
04938     {
04939       if (! check_group_min_max_predicates(and_or_arg, min_max_arg_item))
04940         return false;
04941     }
04942     return true;
04943   }
04944 
04945   /*
04946     TODO:
04947     This is a very crude fix to handle sub-selects in the WHERE clause
04948     (Item_subselect objects). With the test below we rule out from the
04949     optimization all queries with subselects in the WHERE clause. What has to
04950     be done, is that here we should analyze whether the subselect references
04951     the MIN/MAX argument field, and disallow the optimization only if this is
04952     so.
04953   */
04954   if (cond_type == Item::SUBSELECT_ITEM)
04955     return false;
04956 
04957   /* We presume that at this point there are no other Items than functions. */
04958   assert(cond_type == Item::FUNC_ITEM);
04959 
04960   /* Test if cond references only group-by or non-group fields. */
04961   Item_func *pred= (Item_func*) cond;
04962   Item **arguments= pred->arguments();
04963   Item *cur_arg= NULL;
04964   for (uint32_t arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
04965   {
04966     cur_arg= arguments[arg_idx]->real_item();
04967     if (cur_arg->type() == Item::FIELD_ITEM)
04968     {
04969       if (min_max_arg_item->eq(cur_arg, 1))
04970       {
04971        /*
04972          If pred references the MIN/MAX argument, check whether pred is a range
04973          condition that compares the MIN/MAX argument with a constant.
04974        */
04975         Item_func::Functype pred_type= pred->functype();
04976         if (pred_type != Item_func::EQUAL_FUNC     &&
04977             pred_type != Item_func::LT_FUNC        &&
04978             pred_type != Item_func::LE_FUNC        &&
04979             pred_type != Item_func::GT_FUNC        &&
04980             pred_type != Item_func::GE_FUNC        &&
04981             pred_type != Item_func::BETWEEN        &&
04982             pred_type != Item_func::ISNULL_FUNC    &&
04983             pred_type != Item_func::ISNOTNULL_FUNC &&
04984             pred_type != Item_func::EQ_FUNC        &&
04985             pred_type != Item_func::NE_FUNC)
04986           return false;
04987 
04988         /* Check that pred compares min_max_arg_item with a constant. */
04989         Item *args[3];
04990         memset(args, 0, 3 * sizeof(Item*));
04991         bool inv= false;
04992         /* Test if this is a comparison of a field and a constant. */
04993         if (! optimizer::simple_pred(pred, args, inv))
04994           return false;
04995 
04996         /* Check for compatible string comparisons - similar to get_mm_leaf. */
04997         if (args[0] && args[1] && !args[2] && // this is a binary function
04998             min_max_arg_item->result_type() == STRING_RESULT &&
04999             /*
05000               Don't use an index when comparing strings of different collations.
05001             */
05002             ((args[1]->result_type() == STRING_RESULT &&
05003               ((Field_str*) min_max_arg_item->field)->charset() !=
05004               pred->compare_collation())
05005              ||
05006              /*
05007                We can't always use indexes when comparing a string index to a
05008                number.
05009              */
05010              (args[1]->result_type() != STRING_RESULT &&
05011               min_max_arg_item->field->cmp_type() != args[1]->result_type())))
05012         {
05013           return false;
05014         }
05015       }
05016     }
05017     else if (cur_arg->type() == Item::FUNC_ITEM)
05018     {
05019       if (! check_group_min_max_predicates(cur_arg, min_max_arg_item))
05020         return false;
05021     }
05022     else if (cur_arg->const_item())
05023     {
05024       return true;
05025     }
05026     else
05027       return false;
05028   }
05029 
05030   return true;
05031 }
05032 
05033 
05034 /*
05035   Extract a sequence of constants from a conjunction of equality predicates.
05036 
05037   SYNOPSIS
05038     get_constant_key_infix()
05039     index_info             [in]  Descriptor of the chosen index.
05040     index_range_tree       [in]  Range tree for the chosen index
05041     first_non_group_part   [in]  First index part after group attribute parts
05042     min_max_arg_part       [in]  The keypart of the MIN/MAX argument if any
05043     last_part              [in]  Last keypart of the index
05044     session                    [in]  Current thread
05045     key_infix              [out] Infix of constants to be used for index lookup
05046     key_infix_len          [out] Lenghth of the infix
05047     first_non_infix_part   [out] The first keypart after the infix (if any)
05048 
05049   DESCRIPTION
05050     Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
05051     for each keypart field NGF_i not in GROUP-BY, check that there is a
05052     constant equality predicate among conds with the form (NGF_i = const_ci) or
05053     (const_ci = NGF_i).
05054     Thus all the NGF_i attributes must fill the 'gap' between the last group-by
05055     attribute and the MIN/MAX attribute in the index (if present). If these
05056     conditions hold, copy each constant from its corresponding predicate into
05057     key_infix, in the order its NG_i attribute appears in the index, and update
05058     key_infix_len with the total length of the key parts in key_infix.
05059 
05060   RETURN
05061     true  if the index passes the test
05062     false o/w
05063 */
05064 static bool
05065 get_constant_key_infix(KeyInfo *,
05066                        optimizer::SEL_ARG *index_range_tree,
05067                        KeyPartInfo *first_non_group_part,
05068                        KeyPartInfo *min_max_arg_part,
05069                        KeyPartInfo *last_part,
05070                        Session *,
05071                        unsigned char *key_infix,
05072                        uint32_t *key_infix_len,
05073                        KeyPartInfo **first_non_infix_part)
05074 {
05075   optimizer::SEL_ARG *cur_range= NULL;
05076   KeyPartInfo *cur_part= NULL;
05077   /* End part for the first loop below. */
05078   KeyPartInfo *end_part= min_max_arg_part ? min_max_arg_part : last_part;
05079 
05080   *key_infix_len= 0;
05081   unsigned char *key_ptr= key_infix;
05082   for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
05083   {
05084     /*
05085       Find the range tree for the current keypart. We assume that
05086       index_range_tree points to the leftmost keypart in the index.
05087     */
05088     for (cur_range= index_range_tree; cur_range;
05089          cur_range= cur_range->next_key_part)
05090     {
05091       if (cur_range->field->eq(cur_part->field))
05092         break;
05093     }
05094     if (! cur_range)
05095     {
05096       if (min_max_arg_part)
05097         return false; /* The current keypart has no range predicates at all. */
05098       else
05099       {
05100         *first_non_infix_part= cur_part;
05101         return true;
05102       }
05103     }
05104 
05105     /* Check that the current range tree is a single point interval. */
05106     if (cur_range->prev || cur_range->next)
05107       return false; /* This is not the only range predicate for the field. */
05108     if ((cur_range->min_flag & NO_MIN_RANGE) ||
05109         (cur_range->max_flag & NO_MAX_RANGE) ||
05110         (cur_range->min_flag & NEAR_MIN) ||
05111         (cur_range->max_flag & NEAR_MAX))
05112       return false;
05113 
05114     uint32_t field_length= cur_part->store_length;
05115     if ((cur_range->maybe_null &&
05116          cur_range->min_value[0] && cur_range->max_value[0]) ||
05117         !memcmp(cur_range->min_value, cur_range->max_value, field_length))
05118     {
05119       /* cur_range specifies 'IS NULL' or an equality condition. */
05120       memcpy(key_ptr, cur_range->min_value, field_length);
05121       key_ptr+= field_length;
05122       *key_infix_len+= field_length;
05123     }
05124     else
05125       return false;
05126   }
05127 
05128   if (!min_max_arg_part && (cur_part == last_part))
05129     *first_non_infix_part= last_part;
05130 
05131   return true;
05132 }
05133 
05134 
05135 /*
05136   Find the key part referenced by a field.
05137 
05138   SYNOPSIS
05139     get_field_keypart()
05140     index  descriptor of an index
05141     field  field that possibly references some key part in index
05142 
05143   NOTES
05144     The return value can be used to get a KeyPartInfo pointer by
05145     part= index->key_part + get_field_keypart(...) - 1;
05146 
05147   RETURN
05148     Positive number which is the consecutive number of the key part, or
05149     0 if field does not reference any index field.
05150 */
05151 static inline uint
05152 get_field_keypart(KeyInfo *index, Field *field)
05153 {
05154   KeyPartInfo *part= NULL;
05155   KeyPartInfo *end= NULL;
05156 
05157   for (part= index->key_part, end= part + index->key_parts; part < end; part++)
05158   {
05159     if (field->eq(part->field))
05160       return part - index->key_part + 1;
05161   }
05162   return 0;
05163 }
05164 
05165 
05166 /*
05167   Find the SEL_ARG sub-tree that corresponds to the chosen index.
05168 
05169   SYNOPSIS
05170     get_index_range_tree()
05171     index     [in]  The ID of the index being looked for
05172     range_tree[in]  Tree of ranges being searched
05173     param     [in]  Parameter from SqlSelect::test_quick_select
05174     param_idx [out] Index in the array Parameter::key that corresponds to 'index'
05175 
05176   DESCRIPTION
05177 
05178     A optimizer::SEL_TREE contains range trees for all usable indexes. This procedure
05179     finds the SEL_ARG sub-tree for 'index'. The members of a optimizer::SEL_TREE are
05180     ordered in the same way as the members of Parameter::key, thus we first find
05181     the corresponding index in the array Parameter::key. This index is returned
05182     through the variable param_idx, to be used later as argument of
05183     check_quick_select().
05184 
05185   RETURN
05186     Pointer to the SEL_ARG subtree that corresponds to index.
05187 */
05188 optimizer::SEL_ARG *get_index_range_tree(uint32_t index,
05189                                          optimizer::SEL_TREE* range_tree,
05190                                          optimizer::Parameter *param,
05191                                          uint32_t *param_idx)
05192 {
05193   uint32_t idx= 0; /* Index nr in param->key_parts */
05194   while (idx < param->keys)
05195   {
05196     if (index == param->real_keynr[idx])
05197       break;
05198     idx++;
05199   }
05200   *param_idx= idx;
05201   return range_tree->keys[idx];
05202 }
05203 
05204 
05205 /*
05206   Compute the cost of a quick_group_min_max_select for a particular index.
05207 
05208   SYNOPSIS
05209     cost_group_min_max()
05210     table                [in] The table being accessed
05211     index_info           [in] The index used to access the table
05212     used_key_parts       [in] Number of key parts used to access the index
05213     group_key_parts      [in] Number of index key parts in the group prefix
05214     range_tree           [in] Tree of ranges for all indexes
05215     index_tree           [in] The range tree for the current index
05216     quick_prefix_records [in] Number of records retrieved by the internally
05217             used quick range select if any
05218     have_min             [in] True if there is a MIN function
05219     have_max             [in] True if there is a MAX function
05220     read_cost           [out] The cost to retrieve rows via this quick select
05221     records             [out] The number of rows retrieved
05222 
05223   DESCRIPTION
05224     This method computes the access cost of a GroupMinMaxReadPlan instance and
05225     the number of rows returned. It updates this->read_cost and this->records.
05226 
05227   NOTES
05228     The cost computation distinguishes several cases:
05229     1) No equality predicates over non-group attributes (thus no key_infix).
05230        If groups are bigger than blocks on the average, then we assume that it
05231        is very unlikely that block ends are aligned with group ends, thus even
05232        if we look for both MIN and MAX keys, all pairs of neighbor MIN/MAX
05233        keys, except for the first MIN and the last MAX keys, will be in the
05234        same block.  If groups are smaller than blocks, then we are going to
05235        read all blocks.
05236     2) There are equality predicates over non-group attributes.
05237        In this case the group prefix is extended by additional constants, and
05238        as a result the min/max values are inside sub-groups of the original
05239        groups. The number of blocks that will be read depends on whether the
05240        ends of these sub-groups will be contained in the same or in different
05241        blocks. We compute the probability for the two ends of a subgroup to be
05242        in two different blocks as the ratio of:
05243        - the number of positions of the left-end of a subgroup inside a group,
05244          such that the right end of the subgroup is past the end of the buffer
05245          containing the left-end, and
05246        - the total number of possible positions for the left-end of the
05247          subgroup, which is the number of keys in the containing group.
05248        We assume it is very unlikely that two ends of subsequent subgroups are
05249        in the same block.
05250     3) The are range predicates over the group attributes.
05251        Then some groups may be filtered by the range predicates. We use the
05252        selectivity of the range predicates to decide how many groups will be
05253        filtered.
05254 
05255   TODO
05256      - Take into account the optional range predicates over the MIN/MAX
05257        argument.
05258      - Check if we have a PK index and we use all cols - then each key is a
05259        group, and it will be better to use an index scan.
05260 
05261   RETURN
05262     None
05263 */
05264 void cost_group_min_max(Table* table,
05265                         KeyInfo *index_info,
05266                         uint32_t used_key_parts,
05267                         uint32_t group_key_parts,
05268                         optimizer::SEL_TREE *range_tree,
05269                         optimizer::SEL_ARG *,
05270                         ha_rows quick_prefix_records,
05271                         bool have_min,
05272                         bool have_max,
05273                         double *read_cost,
05274                         ha_rows *records)
05275 {
05276   ha_rows table_records;
05277   uint32_t num_groups;
05278   uint32_t num_blocks;
05279   uint32_t keys_per_block;
05280   uint32_t keys_per_group;
05281   uint32_t keys_per_subgroup; /* Average number of keys in sub-groups */
05282                           /* formed by a key infix. */
05283   double p_overlap; /* Probability that a sub-group overlaps two blocks. */
05284   double quick_prefix_selectivity;
05285   double io_cost;
05286   double cpu_cost= 0; /* TODO: CPU cost of index_read calls? */
05287 
05288   table_records= table->cursor->stats.records;
05289   keys_per_block= (table->cursor->stats.block_size / 2 /
05290                    (index_info->key_length + table->cursor->ref_length)
05291                         + 1);
05292   num_blocks= (uint32_t) (table_records / keys_per_block) + 1;
05293 
05294   /* Compute the number of keys in a group. */
05295   keys_per_group= index_info->rec_per_key[group_key_parts - 1];
05296   if (keys_per_group == 0) /* If there is no statistics try to guess */
05297     /* each group contains 10% of all records */
05298     keys_per_group= (uint32_t)(table_records / 10) + 1;
05299   num_groups= (uint32_t)(table_records / keys_per_group) + 1;
05300 
05301   /* Apply the selectivity of the quick select for group prefixes. */
05302   if (range_tree && (quick_prefix_records != HA_POS_ERROR))
05303   {
05304     quick_prefix_selectivity= (double) quick_prefix_records /
05305                               (double) table_records;
05306     num_groups= (uint32_t) rint(num_groups * quick_prefix_selectivity);
05307     set_if_bigger(num_groups, 1U);
05308   }
05309 
05310   if (used_key_parts > group_key_parts)
05311   { /*
05312       Compute the probability that two ends of a subgroup are inside
05313       different blocks.
05314     */
05315     keys_per_subgroup= index_info->rec_per_key[used_key_parts - 1];
05316     if (keys_per_subgroup >= keys_per_block) /* If a subgroup is bigger than */
05317       p_overlap= 1.0;       /* a block, it will overlap at least two blocks. */
05318     else
05319     {
05320       double blocks_per_group= (double) num_blocks / (double) num_groups;
05321       p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
05322       p_overlap= min(p_overlap, 1.0);
05323     }
05324     io_cost= (double) min(num_groups * (1 + p_overlap), (double)num_blocks);
05325   }
05326   else
05327     io_cost= (keys_per_group > keys_per_block) ?
05328              (have_min && have_max) ? (double) (num_groups + 1) :
05329                                       (double) num_groups :
05330              (double) num_blocks;
05331 
05332   /*
05333     TODO: If there is no WHERE clause and no other expressions, there should be
05334     no CPU cost. We leave it here to make this cost comparable to that of index
05335     scan as computed in SqlSelect::test_quick_select().
05336   */
05337   cpu_cost= (double) num_groups / TIME_FOR_COMPARE;
05338 
05339   *read_cost= io_cost + cpu_cost;
05340   *records= num_groups;
05341 }
05342 
05343 
05344 /*
05345   Construct a new quick select object for queries with group by with min/max.
05346 
05347   SYNOPSIS
05348     GroupMinMaxReadPlan::make_quick()
05349     param              Parameter from test_quick_select
05350     retrieve_full_rows ignored
05351     parent_alloc       Memory pool to use, if any.
05352 
05353   NOTES
05354     Make_quick ignores the retrieve_full_rows parameter because
05355     QuickGroupMinMaxSelect always performs 'index only' scans.
05356     The other parameter are ignored as well because all necessary
05357     data to create the QUICK object is computed at this TRP creation
05358     time.
05359 
05360   RETURN
05361     New QuickGroupMinMaxSelect object if successfully created,
05362     NULL otherwise.
05363 */
05364 optimizer::QuickSelectInterface *
05365 optimizer::GroupMinMaxReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *parent_alloc)
05366 {
05367   optimizer::QuickGroupMinMaxSelect *quick= new optimizer::QuickGroupMinMaxSelect(param->table,
05368                                                param->session->lex().current_select->join,
05369                                                have_min,
05370                                                have_max,
05371                                                min_max_arg_part,
05372                                                group_prefix_len,
05373                                                group_key_parts,
05374                                                used_key_parts,
05375                                                index_info,
05376                                                index,
05377                                                read_cost,
05378                                                records,
05379                                                key_infix_len,
05380                                                key_infix,
05381                                                parent_alloc);
05382   if (quick->init())
05383   {
05384     delete quick;
05385     return NULL;
05386   }
05387 
05388   if (range_tree)
05389   {
05390     assert(quick_prefix_records > 0);
05391     if (quick_prefix_records == HA_POS_ERROR)
05392     {
05393       quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
05394     }
05395     else
05396     {
05397       /* Make a QuickRangeSelect to be used for group prefix retrieval. */
05398       quick->quick_prefix_select= optimizer::get_quick_select(param,
05399                                                               param_idx,
05400                                                               index_tree,
05401                                                               HA_MRR_USE_DEFAULT_IMPL,
05402                                                               0,
05403                                                               &quick->alloc);
05404     }
05405 
05406     /*
05407       Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
05408       attribute, and create an array of QuickRanges to be used by the
05409       new quick select.
05410     */
05411     if (min_max_arg_part)
05412     {
05413       optimizer::SEL_ARG *min_max_range= index_tree;
05414       while (min_max_range) /* Find the tree for the MIN/MAX key part. */
05415       {
05416         if (min_max_range->field->eq(min_max_arg_part->field))
05417           break;
05418         min_max_range= min_max_range->next_key_part;
05419       }
05420       /* Scroll to the leftmost interval for the MIN/MAX argument. */
05421       while (min_max_range && min_max_range->prev)
05422         min_max_range= min_max_range->prev;
05423       /* Create an array of QuickRanges for the MIN/MAX argument. */
05424       while (min_max_range)
05425       {
05426         if (quick->add_range(min_max_range))
05427         {
05428           delete quick;
05429           quick= NULL;
05430           return NULL;
05431         }
05432         min_max_range= min_max_range->next;
05433       }
05434     }
05435   }
05436   else
05437     quick->quick_prefix_select= NULL;
05438 
05439   quick->update_key_stat();
05440   quick->adjust_prefix_ranges();
05441 
05442   return quick;
05443 }
05444 
05445 
05446 optimizer::QuickSelectInterface *optimizer::RangeReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *parent_alloc)
05447 {
05448   optimizer::QuickRangeSelect *quick= optimizer::get_quick_select(param, key_idx, key, mrr_flags, mrr_buf_size, parent_alloc);
05449   if (quick)
05450   {
05451     quick->records= records;
05452     quick->read_time= read_cost;
05453   }
05454   return quick;
05455 }
05456 
05457 
05458 uint32_t optimizer::RorScanInfo::findFirstNotSet() const
05459 {
05460   boost::dynamic_bitset<> map= bitsToBitset();
05461   for (boost::dynamic_bitset<>::size_type i= 0; i < map.size(); i++)
05462   {
05463     if (not map.test(i))
05464       return i;
05465   }
05466   return map.size();
05467 }
05468 
05469 
05470 size_t optimizer::RorScanInfo::getBitCount() const
05471 {
05472   boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
05473   return tmp_bitset.count();
05474 }
05475 
05476 
05477 void optimizer::RorScanInfo::subtractBitset(const boost::dynamic_bitset<>& in_bitset)
05478 {
05479   boost::dynamic_bitset<> tmp_bitset= bitsToBitset();
05480   tmp_bitset-= in_bitset;
05481   covered_fields= tmp_bitset.to_ulong();
05482 }
05483 
05484 
05485 boost::dynamic_bitset<> optimizer::RorScanInfo::bitsToBitset() const
05486 {
05487   string res;
05488   uint64_t conv= covered_fields;
05489   while (conv)
05490   {
05491     res.push_back((conv & 1) + '0');
05492     conv>>= 1;
05493   }
05494   if (! res.empty())
05495   {
05496     std::reverse(res.begin(), res.end());
05497   }
05498   string final(covered_fields_size - res.length(), '0');
05499   final.append(res);
05500   return boost::dynamic_bitset<>(final);
05501 }
05502 
05503 
05504 } /* namespace drizzled */