opt_range.cc 371 KB
Newer Older
Mikael Ronstrom's avatar
Mikael Ronstrom committed
1
/* Copyright 2000-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2 3 4

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6 7 8 9 10 11 12 13 14 15

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16 17 18 19 20
/*
  TODO:
  Fix that MAYBE_KEY are stored in the tree so that we can detect use
  of full hash keys for queries like:

21 22
  select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);

23 24
*/

25
/*
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  This file contains:

  RangeAnalysisModule  
    A module that accepts a condition, index (or partitioning) description, 
    and builds lists of intervals (in index/partitioning space), such that 
    all possible records that match the condition are contained within the 
    intervals.
    The entry point for the range analysis module is get_mm_tree() function.
    
    The lists are returned in form of complicated structure of interlinked
    SEL_TREE/SEL_IMERGE/SEL_ARG objects.
    See check_quick_keys, find_used_partitions for examples of how to walk 
    this structure.
    All direct "users" of this module are located within this file, too.


  PartitionPruningModule
    A module that accepts a partitioned table, condition, and finds which
    partitions we will need to use in query execution. Search down for
    "PartitionPruningModule" for description.
    The module has single entry point - prune_partitions() function.


  Range/index_merge/groupby-minmax optimizer module  
    A module that accepts a table, condition, and returns 
     - a QUICK_*_SELECT object that can be used to retrieve rows that match
       the specified condition, or a "no records will match the condition" 
       statement.

    The module entry points are
      test_quick_select()
      get_quick_select_for_ref()


  Record retrieval code for range/index_merge/groupby-min-max.
    Implementations of QUICK_*_SELECT classes.
62 63
*/

64
#ifdef USE_PRAGMA_IMPLEMENTATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
65 66 67 68 69 70 71 72 73 74 75 76
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include <m_ctype.h>
#include "sql_select.h"

#ifndef EXTRA_DEBUG
#define test_rb_tree(A,B) {}
#define test_use_count(A) {}
#endif

77
/*
78
  Convert double value to #rows. Currently this does floor(), and we
79 80
  might consider using round() instead.
*/
81
#define double2rows(x) ((ha_rows)(x))
82

83
static int sel_cmp(Field *f,uchar *a,uchar *b,uint8 a_flag,uint8 b_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
84

85
static uchar is_null_string[2]= {1,0};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
86

87
class RANGE_OPT_PARAM;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*
  A construction block of the SEL_ARG-graph.
  
  The following description only covers graphs of SEL_ARG objects with 
  sel_arg->type==KEY_RANGE:

  One SEL_ARG object represents an "elementary interval" in form
  
      min_value <=?  table.keypartX  <=? max_value
  
  The interval is a non-empty interval of any kind: with[out] minimum/maximum
  bound, [half]open/closed, single-point interval, etc.

  1. SEL_ARG GRAPH STRUCTURE
  
  SEL_ARG objects are linked together in a graph. The meaning of the graph
  is better demostrated by an example:
  
     tree->keys[i]
      | 
      |             $              $
      |    part=1   $     part=2   $    part=3
      |             $              $
      |  +-------+  $   +-------+  $   +--------+
      |  | kp1<1 |--$-->| kp2=5 |--$-->| kp3=10 |
      |  +-------+  $   +-------+  $   +--------+
      |      |      $              $       |
      |      |      $              $   +--------+
      |      |      $              $   | kp3=12 | 
      |      |      $              $   +--------+ 
      |  +-------+  $              $   
      \->| kp1=2 |--$--------------$-+ 
         +-------+  $              $ |   +--------+
             |      $              $  ==>| kp3=11 |
         +-------+  $              $ |   +--------+
         | kp1=3 |--$--------------$-+       |
         +-------+  $              $     +--------+
             |      $              $     | kp3=14 |
            ...     $              $     +--------+
 
  The entire graph is partitioned into "interval lists".

  An interval list is a sequence of ordered disjoint intervals over the same
  key part. SEL_ARG are linked via "next" and "prev" pointers. Additionally,
  all intervals in the list form an RB-tree, linked via left/right/parent 
  pointers. The RB-tree root SEL_ARG object will be further called "root of the
  interval list".
  
    In the example pic, there are 4 interval lists: 
    "kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
    The vertical lines represent SEL_ARG::next/prev pointers.
    
  In an interval list, each member X may have SEL_ARG::next_key_part pointer
  pointing to the root of another interval list Y. The pointed interval list
  must cover a key part with greater number (i.e. Y->part > X->part).
    
    In the example pic, the next_key_part pointers are represented by
    horisontal lines.

  2. SEL_ARG GRAPH SEMANTICS

  It represents a condition in a special form (we don't have a name for it ATM)
  The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
  
  For example, the picture represents the condition in form:
   (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR 
   (kp1=2 AND (kp3=11 OR kp3=14)) OR 
   (kp1=3 AND (kp3=11 OR kp3=14))


  3. SEL_ARG GRAPH USE

  Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
  Then walk the SEL_ARG graph and get a list of dijsoint ordered key
  intervals (i.e. intervals in form
  
   (constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)

  Those intervals can be used to access the index. The uses are in:
   - check_quick_select() - Walk the SEL_ARG graph and find an estimate of
                            how many table records are contained within all
                            intervals.
   - get_quick_select()   - Walk the SEL_ARG, materialize the key intervals,
                            and create QUICK_RANGE_SELECT object that will
                            read records within these intervals.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

  4. SPACE COMPLEXITY NOTES 

    SEL_ARG graph is a representation of an ordered disjoint sequence of
    intervals over the ordered set of index tuple values.

    For multi-part keys, one can construct a WHERE expression such that its
    list of intervals will be of combinatorial size. Here is an example:
     
      (keypart1 IN (1,2, ..., n1)) AND 
      (keypart2 IN (1,2, ..., n2)) AND 
      (keypart3 IN (1,2, ..., n3))
    
    For this WHERE clause the list of intervals will have n1*n2*n3 intervals
    of form
     
      (keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
    
    SEL_ARG graph structure aims to reduce the amount of required space by
    "sharing" the elementary intervals when possible (the pic at the
    beginning of this comment has examples of such sharing). The sharing may 
    prevent combinatorial blowup:

      There are WHERE clauses that have combinatorial-size interval lists but
      will be represented by a compact SEL_ARG graph.
      Example:
        (keypartN IN (1,2, ..., n1)) AND 
        ...
        (keypart2 IN (1,2, ..., n2)) AND 
        (keypart1 IN (1,2, ..., n3))

    but not in all cases:

    - There are WHERE clauses that do have a compact SEL_ARG-graph
      representation but get_mm_tree() and its callees will construct a
      graph of combinatorial size.
      Example:
        (keypart1 IN (1,2, ..., n1)) AND 
        (keypart2 IN (1,2, ..., n2)) AND 
        ...
        (keypartN IN (1,2, ..., n3))

    - There are WHERE clauses for which the minimal possible SEL_ARG graph
      representation will have combinatorial size.
      Example:
        By induction: Let's take any interval on some keypart in the middle:

220
           kp15=c0
221 222 223 224 225 226 227 228
        
        Then let's AND it with this interval 'structure' from preceding and
        following keyparts:

          (kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
        
        We will obtain this SEL_ARG graph:
 
229 230 231 232 233 234 235 236 237 238
             kp14     $      kp15      $      kp16
                      $                $
         +---------+  $   +---------+  $   +---------+
         | kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
         +---------+  $   +---------+  $   +---------+
              |       $                $              
         +---------+  $   +---------+  $             
         | kp14=c2 |--$-->| kp15=c0 |  $             
         +---------+  $   +---------+  $             
                      $                $
239
                      
240
       Note that we had to duplicate "kp15=c0" and there was no way to avoid
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
       that. 
       The induction step: AND the obtained expression with another "wrapping"
       expression like (*).
       When the process ends because of the limit on max. number of keyparts 
       we'll have:

         WHERE clause length  is O(3*#max_keyparts)
         SEL_ARG graph size   is O(2^(#max_keyparts/2))

       (it is also possible to construct a case where instead of 2 in 2^n we
        have a bigger constant, e.g. 4, and get a graph with 4^(31/2)= 2^31
        nodes)

    We avoid consuming too much memory by setting a limit on the number of
    SEL_ARG object we can construct during one range analysis invocation.
256 257
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
258 259 260 261 262 263
class SEL_ARG :public Sql_alloc
{
public:
  uint8 min_flag,max_flag,maybe_flag;
  uint8 part;					// Which key part
  uint8 maybe_null;
264 265 266 267 268 269 270 271 272 273 274 275
  /* 
    Number of children of this element in the RB-tree, plus 1 for this
    element itself.
  */
  uint16 elements;
  /*
    Valid only for elements which are RB-tree roots: Number of times this
    RB-tree is referred to (it is referred by SEL_ARG::next_key_part or by
    SEL_TREE::keys[i] or by a temporary SEL_ARG* variable)
  */
  ulong use_count;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
276
  Field *field;
277
  uchar *min_value,*max_value;			// Pointer to range
bk@work.mysql.com's avatar
bk@work.mysql.com committed
278

279 280 281
  /*
    eq_tree() requires that left == right == 0 if the type is MAYBE_KEY.
   */
282 283 284 285
  SEL_ARG *left,*right;   /* R-B tree children */
  SEL_ARG *next,*prev;    /* Links for bi-directional interval list */
  SEL_ARG *parent;        /* R-B tree parent */
  SEL_ARG *next_key_part; 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
286 287 288
  enum leaf_color { BLACK,RED } color;
  enum Type { IMPOSSIBLE, MAYBE, MAYBE_KEY, KEY_RANGE } type;

289
  enum { MAX_SEL_ARGS = 16000 };
290

bk@work.mysql.com's avatar
bk@work.mysql.com committed
291 292
  SEL_ARG() {}
  SEL_ARG(SEL_ARG &);
293 294
  SEL_ARG(Field *,const uchar *, const uchar *);
  SEL_ARG(Field *field, uint8 part, uchar *min_value, uchar *max_value,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
295 296
	  uint8 min_flag, uint8 max_flag, uint8 maybe_flag);
  SEL_ARG(enum Type type_arg)
297
    :min_flag(0),elements(1),use_count(1),left(0),right(0),next_key_part(0),
298
    color(BLACK), type(type_arg)
299
  {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
300 301
  inline bool is_same(SEL_ARG *arg)
  {
302
    if (type != arg->type || part != arg->part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
303 304 305 306 307 308 309
      return 0;
    if (type != KEY_RANGE)
      return 1;
    return cmp_min_to_min(arg) == 0 && cmp_max_to_max(arg) == 0;
  }
  inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; }
  inline void maybe_smaller() { maybe_flag=1; }
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
310 311
  /* Return true iff it's a single-point null interval */
  inline bool is_null_interval() { return maybe_null && max_value[0] == 1; } 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  inline int cmp_min_to_min(SEL_ARG* arg)
  {
    return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag);
  }
  inline int cmp_min_to_max(SEL_ARG* arg)
  {
    return sel_cmp(field,min_value, arg->max_value, min_flag, arg->max_flag);
  }
  inline int cmp_max_to_max(SEL_ARG* arg)
  {
    return sel_cmp(field,max_value, arg->max_value, max_flag, arg->max_flag);
  }
  inline int cmp_max_to_min(SEL_ARG* arg)
  {
    return sel_cmp(field,max_value, arg->min_value, max_flag, arg->min_flag);
  }
  SEL_ARG *clone_and(SEL_ARG* arg)
  {						// Get overlapping range
330
    uchar *new_min,*new_max;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    uint8 flag_min,flag_max;
    if (cmp_min_to_min(arg) >= 0)
    {
      new_min=min_value; flag_min=min_flag;
    }
    else
    {
      new_min=arg->min_value; flag_min=arg->min_flag; /* purecov: deadcode */
    }
    if (cmp_max_to_max(arg) <= 0)
    {
      new_max=max_value; flag_max=max_flag;
    }
    else
    {
      new_max=arg->max_value; flag_max=arg->max_flag;
    }
    return new SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
		       test(maybe_flag && arg->maybe_flag));
  }
  SEL_ARG *clone_first(SEL_ARG *arg)
  {						// min <= X < arg->min
    return new SEL_ARG(field,part, min_value, arg->min_value,
		       min_flag, arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX,
		       maybe_flag | arg->maybe_flag);
  }
  SEL_ARG *clone_last(SEL_ARG *arg)
  {						// min <= X <= key_max
    return new SEL_ARG(field, part, min_value, arg->max_value,
		       min_flag, arg->max_flag, maybe_flag | arg->maybe_flag);
  }
362
  SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

  bool copy_min(SEL_ARG* arg)
  {						// Get overlapping range
    if (cmp_min_to_min(arg) > 0)
    {
      min_value=arg->min_value; min_flag=arg->min_flag;
      if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
	  (NO_MAX_RANGE | NO_MIN_RANGE))
	return 1;				// Full range
    }
    maybe_flag|=arg->maybe_flag;
    return 0;
  }
  bool copy_max(SEL_ARG* arg)
  {						// Get overlapping range
    if (cmp_max_to_max(arg) <= 0)
    {
      max_value=arg->max_value; max_flag=arg->max_flag;
      if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
	  (NO_MAX_RANGE | NO_MIN_RANGE))
	return 1;				// Full range
    }
    maybe_flag|=arg->maybe_flag;
    return 0;
  }

  void copy_min_to_min(SEL_ARG *arg)
  {
    min_value=arg->min_value; min_flag=arg->min_flag;
  }
  void copy_min_to_max(SEL_ARG *arg)
  {
    max_value=arg->min_value;
    max_flag=arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
  }
  void copy_max_to_min(SEL_ARG *arg)
  {
    min_value=arg->max_value;
    min_flag=arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
  }
403
  /* returns a number of keypart values (0 or 1) appended to the key buffer */
404
  int store_min(uint length, uchar **min_key,uint min_key_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
405
  {
406 407 408
    if ((min_flag & GEOM_FLAG) ||
        (!(min_flag & NO_MIN_RANGE) &&
	!(min_key_flag & (NO_MIN_RANGE | NEAR_MIN))))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
409 410 411 412
    {
      if (maybe_null && *min_value)
      {
	**min_key=1;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
413
	bzero(*min_key+1,length-1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
414 415
      }
      else
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
416 417
	memcpy(*min_key,min_value,length);
      (*min_key)+= length;
418
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
419
    }
420
    return 0;
421
  }
422
  /* returns a number of keypart values (0 or 1) appended to the key buffer */
423
  int store_max(uint length, uchar **max_key, uint max_key_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
424 425 426 427 428 429 430
  {
    if (!(max_flag & NO_MAX_RANGE) &&
	!(max_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
    {
      if (maybe_null && *max_value)
      {
	**max_key=1;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
431
	bzero(*max_key+1,length-1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
432 433
      }
      else
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
434 435
	memcpy(*max_key,max_value,length);
      (*max_key)+= length;
436
      return 1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
437
    }
438
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
439 440
  }

441 442 443 444 445 446 447 448 449 450 451 452 453
  /*
    Returns a number of keypart values appended to the key buffer
    for min key and max key. This function is used by both Range
    Analysis and Partition pruning. For partition pruning we have
    to ensure that we don't store also subpartition fields. Thus
    we have to stop at the last partition part and not step into
    the subpartition fields. For Range Analysis we set last_part
    to MAX_KEY which we should never reach.
  */
  int store_min_key(KEY_PART *key,
                    uchar **range_key,
                    uint *range_key_flag,
                    uint last_part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
454 455
  {
    SEL_ARG *key_tree= first();
456 457
    uint res= key_tree->store_min(key[key_tree->part].store_length,
                                  range_key, *range_key_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
458 459
    *range_key_flag|= key_tree->min_flag;
    if (key_tree->next_key_part &&
460
	key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
461
        key_tree->part != last_part &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
462
	key_tree->next_key_part->part == key_tree->part+1 &&
463
	!(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)))
464 465 466 467
      res+= key_tree->next_key_part->store_min_key(key,
                                                   range_key,
                                                   range_key_flag,
                                                   last_part);
468
    return res;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
469 470
  }

471
  /* returns a number of keypart values appended to the key buffer */
472 473 474 475
  int store_max_key(KEY_PART *key,
                    uchar **range_key,
                    uint *range_key_flag,
                    uint last_part)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
476 477
  {
    SEL_ARG *key_tree= last();
478 479
    uint res=key_tree->store_max(key[key_tree->part].store_length,
                                 range_key, *range_key_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
480 481
    (*range_key_flag)|= key_tree->max_flag;
    if (key_tree->next_key_part &&
482
	key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
483
        key_tree->part != last_part &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
484
	key_tree->next_key_part->part == key_tree->part+1 &&
485
	!(*range_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
486 487 488 489
      res+= key_tree->next_key_part->store_max_key(key,
                                                   range_key,
                                                   range_key_flag,
                                                   last_part);
490
    return res;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  }

  SEL_ARG *insert(SEL_ARG *key);
  SEL_ARG *tree_delete(SEL_ARG *key);
  SEL_ARG *find_range(SEL_ARG *key);
  SEL_ARG *rb_insert(SEL_ARG *leaf);
  friend SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key, SEL_ARG *par);
#ifdef EXTRA_DEBUG
  friend int test_rb_tree(SEL_ARG *element,SEL_ARG *parent);
  void test_use_count(SEL_ARG *root);
#endif
  SEL_ARG *first();
  SEL_ARG *last();
  void make_root();
  inline bool simple_key()
  {
    return !next_key_part && elements == 1;
  }
  void increment_use_count(long count)
  {
    if (next_key_part)
    {
      next_key_part->use_count+=count;
      count*= (next_key_part->use_count-count);
      for (SEL_ARG *pos=next_key_part->first(); pos ; pos=pos->next)
	if (pos->next_key_part)
	  pos->increment_use_count(count);
    }
  }
  void free_tree()
  {
    for (SEL_ARG *pos=first(); pos ; pos=pos->next)
      if (pos->next_key_part)
      {
	pos->next_key_part->use_count--;
	pos->next_key_part->free_tree();
      }
  }

  inline SEL_ARG **parent_ptr()
  {
    return parent->left == this ? &parent->left : &parent->right;
  }
534

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

  /*
    Check if this SEL_ARG object represents a single-point interval

    SYNOPSIS
      is_singlepoint()
    
    DESCRIPTION
      Check if this SEL_ARG object (not tree) represents a single-point
      interval, i.e. if it represents a "keypart = const" or 
      "keypart IS NULL".

    RETURN
      TRUE   This SEL_ARG object represents a singlepoint interval
      FALSE  Otherwise
  */

552 553
  bool is_singlepoint()
  {
554 555 556 557 558 559
    /* 
      Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field) 
      flags, and the same for right edge.
    */
    if (min_flag || max_flag)
      return FALSE;
560 561
    uchar *min_val= min_value;
    uchar *max_val= max_value;
562 563 564 565 566 567 568 569 570 571 572 573 574

    if (maybe_null)
    {
      /* First byte is a NULL value indicator */
      if (*min_val != *max_val)
        return FALSE;

      if (*min_val)
        return TRUE; /* This "x IS NULL" */
      min_val++;
      max_val++;
    }
    return !field->key_cmp(min_val, max_val);
575
  }
576
  SEL_ARG *clone_tree(RANGE_OPT_PARAM *param);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
577 578
};

579
class SEL_IMERGE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
580

581

bk@work.mysql.com's avatar
bk@work.mysql.com committed
582 583 584
class SEL_TREE :public Sql_alloc
{
public:
585 586 587 588 589
  /*
    Starting an effort to document this field:
    (for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) => 
       (type == SEL_TREE::IMPOSSIBLE)
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
590 591
  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
592
  SEL_TREE() :type(KEY)
593
  {
594
    keys_map.clear_all();
595 596
    bzero((char*) keys,sizeof(keys));
  }
597
  SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param);
598 599 600 601 602 603
  /*
    Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
    keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
    merit in range analyzer functions (e.g. get_mm_parts) returning a
    pointer to such SEL_TREE instead of NULL)
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
604
  SEL_ARG *keys[MAX_KEY];
605 606
  key_map keys_map;        /* bitmask of non-NULL elements in keys */

607 608
  /*
    Possible ways to read rows using index_merge. The list is non-empty only
609 610 611
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
  */
  List<SEL_IMERGE> merges;
612

613 614
  /* The members below are filled/used only after get_mm_tree is done */
  key_map ror_scans_map;   /* bitmask of ROR scan-able elements in keys */
615
  uint    n_ror_scans;     /* number of set bits in ror_scans_map */
616 617 618 619

  struct st_ror_scan_info **ror_scans;     /* list of ROR key scans */
  struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
  /* Note that #records for each key scan is stored in table->quick_rows */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
620 621
};

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
class RANGE_OPT_PARAM
{
public:
  THD	*thd;   /* Current thread handle */
  TABLE *table; /* Table being analyzed */
  COND *cond;   /* Used inside get_mm_tree(). */
  table_map prev_tables;
  table_map read_tables;
  table_map current_table; /* Bit of the table being analyzed */

  /* Array of parts of all keys for which range analysis is performed */
  KEY_PART *key_parts;
  KEY_PART *key_parts_end;
  MEM_ROOT *mem_root; /* Memory that will be freed when range analysis completes */
  MEM_ROOT *old_root; /* Memory that will last until the query end */
  /*
    Number of indexes used in range analysis (In SEL_TREE::keys only first
    #keys elements are not empty)
  */
  uint keys;
  
  /* 
    If true, the index descriptions describe real indexes (and it is ok to
    call field->optimize_range(real_keynr[...], ...).
    Otherwise index description describes fake indexes.
  */
  bool using_real_indexes;
  
650 651
  bool remove_jump_scans;
  
652 653 654 655 656
  /*
    used_key_no -> table_key_no translation table. Only makes sense if
    using_real_indexes==TRUE
  */
  uint real_keynr[MAX_KEY];
657

Mikael Ronstrom's avatar
Mikael Ronstrom committed
658 659 660 661
  /*
    Used to store 'current key tuples', in both range analysis and
    partitioning (list) analysis
  */
662 663 664
  uchar min_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH],
    max_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];

665 666
  /* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
  uint alloced_sel_args; 
667
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
668

669 670 671
class PARAM : public RANGE_OPT_PARAM
{
public:
672
  KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
673 674
  longlong baseflag;
  uint max_key_part, range_count;
675

676
  bool quick;				// Don't calulate possible keys
677

678
  uint fields_bitmap_size;
679
  MY_BITMAP needed_fields;    /* bitmask of fields needed by the query */
680
  MY_BITMAP tmp_covered_fields;
681 682 683

  key_map *needed_reg;        /* ptr to SQL_SELECT::needed_reg */

684 685
  uint *imerge_cost_buff;     /* buffer for index_merge cost estimates */
  uint imerge_cost_buff_size; /* size of the buffer */
686

687
  /* TRUE if last checked tree->key can be used for ROR-scan */
688
  bool is_ror_scan;
689 690
  /* Number of ranges in the last checked tree->key */
  uint n_ranges;
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
691
  uint8 first_null_comp; /* first null component if any, 0 - otherwise */
692
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
693

694 695 696 697 698
class TABLE_READ_PLAN;
  class TRP_RANGE;
  class TRP_ROR_INTERSECT;
  class TRP_ROR_UNION;
  class TRP_ROR_INDEX_MERGE;
699
  class TRP_GROUP_MIN_MAX;
700 701 702

struct st_ror_scan_info;

703
static SEL_TREE * get_mm_parts(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
704 705
			       Item_func::Functype type,Item *value,
			       Item_result cmp_type);
706
static SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
707
			    KEY_PART *key_part,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
708
			    Item_func::Functype type,Item *value);
709
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond);
710 711

static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);
712
static ha_rows check_quick_select(PARAM *param,uint index,SEL_ARG *key_tree,
713
                                  bool update_tbl_stats);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
714
static ha_rows check_quick_keys(PARAM *param,uint index,SEL_ARG *key_tree,
715 716
                                uchar *min_key, uint min_key_flag, int,
                                uchar *max_key, uint max_key_flag, int);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
717

718
QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
719
                                     SEL_ARG *key_tree,
720
                                     MEM_ROOT *alloc = NULL);
721
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
722
                                       bool index_read_must_be_used,
723
                                       bool update_tbl_stats,
724 725 726 727 728 729
                                       double read_time);
static
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
                                          double read_time,
                                          bool *are_all_covering);
static
730 731
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
732 733 734 735
                                                   double read_time);
static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
                                         double read_time);
736
static
737 738
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree,
                                          double read_time);
739
static double get_index_only_read_time(const PARAM* param, ha_rows records,
740 741
                                       int keynr);

bk@work.mysql.com's avatar
bk@work.mysql.com committed
742
#ifndef DBUG_OFF
743 744
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
                           const char *msg);
745 746
static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
747 748
                                struct st_ror_scan_info **end);
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
749
#endif
750

751 752
static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
753
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
754 755 756
static SEL_ARG *key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2);
static SEL_ARG *key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
                        uint clone_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
757
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
758
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
759 760
                    SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
                    uchar *max_key,uint max_key_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
761 762 763
static bool eq_tree(SEL_ARG* a,SEL_ARG *b);

static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
764
static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
765
                             uint length);
766
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM* param);
767 768 769


/*
770
  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
771
  a condition in the following form:
772
   (t_1||t_2||...||t_N) && (next)
773

774
  where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
775 776 777 778 779 780 781 782 783 784 785
  (t_i,t_j) contains SEL_ARGS for the same index.

  SEL_TREE contained in SEL_IMERGE always has merges=NULL.

  This class relies on memory manager to do the cleanup.
*/

class SEL_IMERGE : public Sql_alloc
{
  enum { PREALLOCED_TREES= 10};
public:
786
  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
787 788 789 790 791 792 793 794 795 796 797
  SEL_TREE **trees;             /* trees used to do index_merge   */
  SEL_TREE **trees_next;        /* last of these trees            */
  SEL_TREE **trees_end;         /* end of allocated space         */

  SEL_ARG  ***best_keys;        /* best keys to read in SEL_TREEs */

  SEL_IMERGE() :
    trees(&trees_prealloced[0]),
    trees_next(trees),
    trees_end(trees + PREALLOCED_TREES)
  {}
798
  SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param);
799 800 801
  int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
  int or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree);
  int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge);
802 803 804
};


805
/*
806 807
  Add SEL_TREE to this index_merge without any checks,

808 809
  NOTES
    This function implements the following:
810 811 812 813 814 815 816
      (x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs

  RETURN
     0 - OK
    -1 - Out of memory.
*/

817
int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
  if (trees_next == trees_end)
  {
    const int realloc_ratio= 2;		/* Double size for next round */
    uint old_elements= (trees_end - trees);
    uint old_size= sizeof(SEL_TREE**) * old_elements;
    uint new_size= old_size * realloc_ratio;
    SEL_TREE **new_trees;
    if (!(new_trees= (SEL_TREE**)alloc_root(param->mem_root, new_size)))
      return -1;
    memcpy(new_trees, trees, old_size);
    trees=      new_trees;
    trees_next= trees + old_elements;
    trees_end=  trees + old_elements * realloc_ratio;
  }
  *(trees_next++)= tree;
  return 0;
}


/*
  Perform OR operation on this SEL_IMERGE and supplied SEL_TREE new_tree,
  combining new_tree with one of the trees in this SEL_IMERGE if they both
  have SEL_ARGs for the same key.
842

843 844 845 846 847
  SYNOPSIS
    or_sel_tree_with_checks()
      param    PARAM from SQL_SELECT::test_quick_select
      new_tree SEL_TREE with type KEY or KEY_SMALLER.

848
  NOTES
849
    This does the following:
850 851
    (t_1||...||t_k)||new_tree =
     either
852 853 854
       = (t_1||...||t_k||new_tree)
     or
       = (t_1||....||(t_j|| new_tree)||...||t_k),
855

856
     where t_i, y are SEL_TREEs.
857 858
    new_tree is combined with the first t_j it has a SEL_ARG on common
    key with. As a consequence of this, choice of keys to do index_merge
859 860
    read may depend on the order of conditions in WHERE part of the query.

861
  RETURN
862
    0  OK
863
    1  One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
864 865 866 867
       and (*this) should be discarded.
   -1  An error occurred.
*/

868
int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree)
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
{
  for (SEL_TREE** tree = trees;
       tree != trees_next;
       tree++)
  {
    if (sel_trees_can_be_ored(*tree, new_tree, param))
    {
      *tree = tree_or(param, *tree, new_tree);
      if (!*tree)
        return 1;
      if (((*tree)->type == SEL_TREE::MAYBE) ||
          ((*tree)->type == SEL_TREE::ALWAYS))
        return 1;
      /* SEL_TREE::IMPOSSIBLE is impossible here */
      return 0;
    }
  }

887
  /* New tree cannot be combined with any of existing trees. */
888 889 890 891 892 893 894 895 896
  return or_sel_tree(param, new_tree);
}


/*
  Perform OR operation on this index_merge and supplied index_merge list.

  RETURN
    0 - OK
897
    1 - One of conditions in result is always TRUE and this SEL_IMERGE
898 899 900 901
        should be discarded.
   -1 - An error occurred
*/

902
int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge)
903 904 905 906 907 908 909 910 911 912 913 914
{
  for (SEL_TREE** tree= imerge->trees;
       tree != imerge->trees_next;
       tree++)
  {
    if (or_sel_tree_with_checks(param, *tree))
      return 1;
  }
  return 0;
}


915
SEL_TREE::SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param): Sql_alloc()
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
  keys_map= arg->keys_map;
  type= arg->type;
  for (int idx= 0; idx < MAX_KEY; idx++)
  {
    if ((keys[idx]= arg->keys[idx]))
      keys[idx]->increment_use_count(1);
  }

  List_iterator<SEL_IMERGE> it(arg->merges);
  for (SEL_IMERGE *el= it++; el; el= it++)
  {
    SEL_IMERGE *merge= new SEL_IMERGE(el, param);
    if (!merge || merge->trees == merge->trees_next)
    {
      merges.empty();
      return;
    }
    merges.push_back (merge);
  }
}


939
SEL_IMERGE::SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param) : Sql_alloc()
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
{
  uint elements= (arg->trees_end - arg->trees);
  if (elements > PREALLOCED_TREES)
  {
    uint size= elements * sizeof (SEL_TREE **);
    if (!(trees= (SEL_TREE **)alloc_root(param->mem_root, size)))
      goto mem_err;
  }
  else
    trees= &trees_prealloced[0];

  trees_next= trees;
  trees_end= trees + elements;

  for (SEL_TREE **tree = trees, **arg_tree= arg->trees; tree < trees_end; 
       tree++, arg_tree++)
  {
    if (!(*tree= new SEL_TREE(*arg_tree, param)))
      goto mem_err;
  }

  return;

mem_err:
  trees= &trees_prealloced[0];
  trees_next= trees;
  trees_end= trees;
}


970
/*
971
  Perform AND operation on two index_merge lists and store result in *im1.
972 973 974 975 976 977 978 979 980 981 982
*/

inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
{
  im1->concat(im2);
}


/*
  Perform OR operation on 2 index_merge lists, storing result in first list.

983
  NOTES
984 985 986
    The following conversion is implemented:
     (a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
      => (a_1||b_1).
987 988

    i.e. all conjuncts except the first one are currently dropped.
989 990
    This is done to avoid producing N*K ways to do index_merge.

monty@mysql.com's avatar
monty@mysql.com committed
991
    If (a_1||b_1) produce a condition that is always TRUE, NULL is returned
992
    and index_merge is discarded (while it is actually possible to try
993
    harder).
994

995 996
    As a consequence of this, choice of keys to do index_merge read may depend
    on the order of conditions in WHERE part of the query.
997 998

  RETURN
999
    0     OK, result is stored in *im1
1000 1001 1002
    other Error, both passed lists are unusable
*/

1003
int imerge_list_or_list(RANGE_OPT_PARAM *param,
1004 1005 1006 1007 1008 1009
                        List<SEL_IMERGE> *im1,
                        List<SEL_IMERGE> *im2)
{
  SEL_IMERGE *imerge= im1->head();
  im1->empty();
  im1->push_back(imerge);
1010

1011 1012 1013 1014 1015 1016 1017 1018
  return imerge->or_sel_imerge_with_checks(param, im2->head());
}


/*
  Perform OR operation on index_merge list and key tree.

  RETURN
1019
    0     OK, result is stored in *im1.
1020 1021 1022
    other Error
*/

1023
int imerge_list_or_tree(RANGE_OPT_PARAM *param,
1024 1025 1026 1027 1028
                        List<SEL_IMERGE> *im1,
                        SEL_TREE *tree)
{
  SEL_IMERGE *imerge;
  List_iterator<SEL_IMERGE> it(*im1);
1029
  bool tree_used= FALSE;
monty@mishka.local's avatar
monty@mishka.local committed
1030
  while ((imerge= it++))
1031
  {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    SEL_TREE *or_tree;
    if (tree_used)
    {
      or_tree= new SEL_TREE (tree, param);
      if (!or_tree ||
          (or_tree->keys_map.is_clear_all() && or_tree->merges.is_empty()))
        return FALSE;
    }
    else
      or_tree= tree;

    if (imerge->or_sel_tree_with_checks(param, or_tree))
1044
      it.remove();
1045
    tree_used= TRUE;
1046 1047 1048
  }
  return im1->is_empty();
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1049

1050

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1051
/***************************************************************************
1052
** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1053 1054 1055 1056 1057 1058 1059 1060 1061
***************************************************************************/

	/* make a select from mysql info
	   Error is set as following:
	   0 = ok
	   1 = Got some error (out of memory?)
	   */

SQL_SELECT *make_select(TABLE *head, table_map const_tables,
monty@mysql.com's avatar
monty@mysql.com committed
1062 1063 1064
			table_map read_tables, COND *conds,
                        bool allow_null_cond,
                        int *error)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1065 1066 1067 1068 1069
{
  SQL_SELECT *select;
  DBUG_ENTER("make_select");

  *error=0;
1070 1071

  if (!conds && !allow_null_cond)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1072 1073 1074
    DBUG_RETURN(0);
  if (!(select= new SQL_SELECT))
  {
1075 1076
    *error= 1;			// out of memory
    DBUG_RETURN(0);		/* purecov: inspected */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1077 1078 1079 1080 1081 1082
  }
  select->read_tables=read_tables;
  select->const_tables=const_tables;
  select->head=head;
  select->cond=conds;

igor@hundin.mysql.fi's avatar
igor@hundin.mysql.fi committed
1083
  if (head->sort.io_cache)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1084
  {
igor@hundin.mysql.fi's avatar
igor@hundin.mysql.fi committed
1085
    select->file= *head->sort.io_cache;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1086 1087
    select->records=(ha_rows) (select->file.end_of_file/
			       head->file->ref_length);
1088
    my_free(head->sort.io_cache, MYF(0));
igor@hundin.mysql.fi's avatar
igor@hundin.mysql.fi committed
1089
    head->sort.io_cache=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1090 1091 1092 1093 1094 1095 1096
  }
  DBUG_RETURN(select);
}


SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
{
serg@serg.mylan's avatar
serg@serg.mylan committed
1097
  quick_keys.clear_all(); needed_reg.clear_all();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1098 1099 1100 1101
  my_b_clear(&file);
}


1102
void SQL_SELECT::cleanup()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1103 1104
{
  delete quick;
1105
  quick= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1106
  if (free_cond)
1107 1108
  {
    free_cond=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1109
    delete cond;
1110
    cond= 0;
1111
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1112 1113 1114
  close_cached_file(&file);
}

1115 1116 1117 1118 1119 1120

SQL_SELECT::~SQL_SELECT()
{
  cleanup();
}

1121
#undef index					// Fix for Unixware 7
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1122

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1123 1124 1125 1126 1127
QUICK_SELECT_I::QUICK_SELECT_I()
  :max_used_key_length(0),
   used_key_parts(0)
{}

1128
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1129
                                       bool no_alloc, MEM_ROOT *parent_alloc)
1130
  :dont_free(0),error(0),free_file(0),in_range(0),cur_range(NULL),last_range(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1131
{
1132 1133 1134 1135
  my_bitmap_map *bitmap;
  DBUG_ENTER("QUICK_RANGE_SELECT::QUICK_RANGE_SELECT");

  in_ror_merged_scan= 0;
monty@mysql.com's avatar
monty@mysql.com committed
1136
  sorted= 0;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1137 1138
  index= key_nr;
  head=  table;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1139
  key_part_info= head->key_info[index].key_part;
1140
  my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1141

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1142
  /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
ingo@mysql.com's avatar
ingo@mysql.com committed
1143 1144 1145 1146 1147 1148
  multi_range_bufsiz= thd->variables.read_rnd_buff_size;
  multi_range_count= thd->variables.multi_range_count;
  multi_range_length= 0;
  multi_range= NULL;
  multi_range_buff= NULL;

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1149
  if (!no_alloc && !parent_alloc)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1150
  {
1151 1152
    // Allocates everything through the internal memroot
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1153
    thd->mem_root= &alloc;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1154 1155 1156
  }
  else
    bzero((char*) &alloc,sizeof(alloc));
1157 1158
  file= head->file;
  record= head->record[0];
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
  save_read_set= head->read_set;
  save_write_set= head->write_set;

  /* Allocate a bitmap for used columns */
  if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size,
                                           MYF(MY_WME))))
  {
    column_bitmap.bitmap= 0;
    error= 1;
  }
  else
    bitmap_init(&column_bitmap, bitmap, head->s->fields, FALSE);
  DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1172 1173
}

monty@mysql.com's avatar
monty@mysql.com committed
1174

1175 1176
int QUICK_RANGE_SELECT::init()
{
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
1177
  DBUG_ENTER("QUICK_RANGE_SELECT::init");
ingo@mysql.com's avatar
ingo@mysql.com committed
1178

1179 1180
  if (file->inited != handler::NONE)
    file->ha_index_or_rnd_end();
1181
  DBUG_RETURN(FALSE);
monty@mysql.com's avatar
monty@mysql.com committed
1182 1183 1184 1185 1186 1187
}


void QUICK_RANGE_SELECT::range_end()
{
  if (file->inited != handler::NONE)
1188
    file->ha_index_or_rnd_end();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1189 1190
}

monty@mysql.com's avatar
monty@mysql.com committed
1191

1192
QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
1193
{
1194
  DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
1195 1196
  if (!dont_free)
  {
1197 1198
    /* file is NULL for CPK scan on covering ROR-intersection */
    if (file) 
1199
    {
1200
      range_end();
1201 1202 1203 1204 1205
      if (head->key_read)
      {
        head->key_read= 0;
        file->extra(HA_EXTRA_NO_KEYREAD);
      }
1206 1207
      if (free_file)
      {
1208
        DBUG_PRINT("info", ("Freeing separate handler 0x%lx (free: %d)", (long) file,
1209
                            free_file));
1210
        file->ha_external_lock(current_thd, F_UNLCK);
1211
        file->close();
1212
        delete file;
1213
      }
1214
    }
1215
    delete_dynamic(&ranges); /* ranges are allocated in alloc */
1216
    free_root(&alloc,MYF(0));
1217
    my_free((char*) column_bitmap.bitmap, MYF(MY_ALLOW_ZERO_PTR));
1218
  }
1219 1220 1221
  head->column_bitmaps_set(save_read_set, save_write_set);
  x_free(multi_range);
  x_free(multi_range_buff);
1222
  DBUG_VOID_RETURN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1223 1224
}

1225

1226
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
1227
                                                   TABLE *table)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1228
  :pk_quick_select(NULL), thd(thd_param)
1229
{
1230
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT");
1231 1232
  index= MAX_KEY;
  head= table;
1233
  bzero(&read_record, sizeof(read_record));
1234
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1235
  DBUG_VOID_RETURN;
1236 1237 1238 1239
}

int QUICK_INDEX_MERGE_SELECT::init()
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1240 1241
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::init");
  DBUG_RETURN(0);
1242 1243
}

1244
int QUICK_INDEX_MERGE_SELECT::reset()
1245
{
1246
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::reset");
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1247
  DBUG_RETURN(read_keys_and_merge());
1248 1249
}

1250
bool
1251 1252
QUICK_INDEX_MERGE_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
{
1253 1254
  /*
    Save quick_select that does scan on clustered primary key as it will be
1255
    processed separately.
1256
  */
1257
  if (head->file->primary_key_is_clustered() &&
1258
      quick_sel_range->index == head->s->primary_key)
1259 1260 1261 1262
    pk_quick_select= quick_sel_range;
  else
    return quick_selects.push_back(quick_sel_range);
  return 0;
1263 1264 1265 1266
}

QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT()
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1267 1268
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
  QUICK_RANGE_SELECT* quick;
1269
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT");
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1270 1271 1272
  quick_it.rewind();
  while ((quick= quick_it++))
    quick->file= NULL;
1273
  quick_selects.delete_elements();
1274
  delete pk_quick_select;
1275 1276 1277
  /* It's ok to call the next two even if they are already deinitialized */
  end_read_record(&read_record);
  free_io_cache(head);
1278
  free_root(&alloc,MYF(0));
1279
  DBUG_VOID_RETURN;
1280 1281
}

1282 1283 1284 1285 1286

QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
                                                       TABLE *table,
                                                       bool retrieve_full_rows,
                                                       MEM_ROOT *parent_alloc)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1287
  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
1288
    scans_inited(FALSE)
1289 1290
{
  index= MAX_KEY;
1291
  head= table;
1292 1293
  record= head->record[0];
  if (!parent_alloc)
1294
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1295 1296
  else
    bzero(&alloc, sizeof(MEM_ROOT));
1297 1298
  last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc,
                                  head->file->ref_length);
1299 1300
}

1301

1302
/*
1303 1304 1305
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init()
1306

1307 1308 1309 1310 1311
  RETURN
    0      OK
    other  Error code
*/

1312 1313
int QUICK_ROR_INTERSECT_SELECT::init()
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1314 1315 1316
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init");
 /* Check if last_rowid was successfully allocated in ctor */
  DBUG_RETURN(!last_rowid);
1317 1318 1319 1320
}


/*
1321 1322 1323 1324
  Initialize this quick select to be a ROR-merged scan.

  SYNOPSIS
    QUICK_RANGE_SELECT::init_ror_merged_scan()
monty@mysql.com's avatar
monty@mysql.com committed
1325
      reuse_handler If TRUE, use head->file, otherwise create a separate
1326 1327 1328 1329
                    handler object

  NOTES
    This function creates and prepares for subsequent use a separate handler
1330
    object if it can't reuse head->file. The reason for this is that during
1331 1332 1333
    ROR-merge several key scans are performed simultaneously, and a single
    handler is only capable of preserving context of a single key scan.

1334
    In ROR-merge the quick select doing merge does full records retrieval,
1335
    merged quick selects read only keys.
1336 1337

  RETURN
1338 1339 1340 1341
    0  ROR child scan initialized, ok to use.
    1  error
*/

1342
int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
1343
{
1344
  handler *save_file= file, *org_file;
1345
  THD *thd;
1346
  DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");
1347

1348
  in_ror_merged_scan= 1;
1349 1350
  if (reuse_handler)
  {
1351 1352
    DBUG_PRINT("info", ("Reusing handler 0x%lx", (long) file));
    if (init() || reset())
1353 1354 1355
    {
      DBUG_RETURN(1);
    }
1356 1357
    head->column_bitmaps_set(&column_bitmap, &column_bitmap);
    goto end;
1358 1359 1360 1361 1362 1363 1364 1365
  }

  /* Create a separate handler object for this quick select */
  if (free_file)
  {
    /* already have own 'handler' object. */
    DBUG_RETURN(0);
  }
1366

1367
  thd= head->in_use;
1368
  if (!(file= head->file->clone(thd->mem_root)))
1369
  {
1370 1371 1372 1373 1374 1375 1376
    /* 
      Manually set the error flag. Note: there seems to be quite a few
      places where a failure could cause the server to "hang" the client by
      sending no response to a query. ATM those are not real errors because 
      the storage engine calls in question happen to never fail with the 
      existing storage engines. 
    */
1377
    my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
1378
    /* Caller will free the memory */
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1379
    goto failure;  /* purecov: inspected */
1380
  }
1381 1382 1383

  head->column_bitmaps_set(&column_bitmap, &column_bitmap);

1384
  if (file->ha_external_lock(thd, F_RDLCK))
1385
    goto failure;
1386

1387
  if (init() || reset())
1388
  {
1389
    file->ha_external_lock(thd, F_UNLCK);
1390 1391 1392
    file->close();
    goto failure;
  }
monty@mysql.com's avatar
monty@mysql.com committed
1393
  free_file= TRUE;
1394
  last_rowid= file->ref;
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

end:
  /*
    We are only going to read key fields and call position() on 'file'
    The following sets head->tmp_set to only use this key and then updates
    head->read_set and head->write_set to use this bitmap.
    The now bitmap is stored in 'column_bitmap' which is used in ::get_next()
  */
  org_file= head->file;
  head->file= file;
  /* We don't have to set 'head->keyread' here as the 'file' is unique */
1406 1407 1408 1409 1410
  if (!head->no_keyread)
  {
    head->key_read= 1;
    head->mark_columns_used_by_index(index);
  }
1411 1412 1413 1414 1415
  head->prepare_for_position();
  head->file= org_file;
  bitmap_copy(&column_bitmap, head->read_set);
  head->column_bitmaps_set(&column_bitmap, &column_bitmap);

1416 1417 1418
  DBUG_RETURN(0);

failure:
1419 1420
  head->column_bitmaps_set(save_read_set, save_write_set);
  delete file;
1421 1422 1423 1424
  file= save_file;
  DBUG_RETURN(1);
}

1425 1426 1427 1428 1429

/*
  Initialize this quick select to be a part of a ROR-merged scan.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
monty@mysql.com's avatar
monty@mysql.com committed
1430
      reuse_handler If TRUE, use head->file, otherwise create separate
1431
                    handler object.
1432
  RETURN
1433 1434 1435 1436
    0     OK
    other error code
*/
int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler)
1437 1438 1439
{
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
  QUICK_RANGE_SELECT* quick;
1440
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");
1441 1442

  /* Initialize all merged "children" quick selects */
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1443
  DBUG_ASSERT(!need_to_fetch_row || reuse_handler);
1444 1445 1446
  if (!need_to_fetch_row && reuse_handler)
  {
    quick= quick_it++;
1447
    /*
1448
      There is no use of this->file. Use it for the first of merged range
1449 1450
      selects.
    */
monty@mysql.com's avatar
monty@mysql.com committed
1451
    if (quick->init_ror_merged_scan(TRUE))
1452 1453 1454
      DBUG_RETURN(1);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
  }
monty@mishka.local's avatar
monty@mishka.local committed
1455
  while ((quick= quick_it++))
1456
  {
monty@mysql.com's avatar
monty@mysql.com committed
1457
    if (quick->init_ror_merged_scan(FALSE))
1458 1459
      DBUG_RETURN(1);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
1460
    /* All merged scans share the same record buffer in intersection. */
1461 1462 1463
    quick->record= head->record[0];
  }

monty@mysql.com's avatar
monty@mysql.com committed
1464
  if (need_to_fetch_row && head->file->ha_rnd_init(1))
1465 1466 1467 1468 1469 1470 1471
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}

1472

1473
/*
1474 1475 1476 1477 1478 1479 1480 1481
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()
  RETURN
    0      OK
    other  Error code
*/

1482 1483 1484
int QUICK_ROR_INTERSECT_SELECT::reset()
{
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1485 1486
  if (!scans_inited && init_ror_merged_scan(TRUE))
    DBUG_RETURN(1);
1487
  scans_inited= TRUE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1488 1489 1490 1491 1492
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  QUICK_RANGE_SELECT *quick;
  while ((quick= it++))
    quick->reset();
  DBUG_RETURN(0);
1493 1494
}

1495 1496 1497

/*
  Add a merged quick select to this ROR-intersection quick select.
1498

1499 1500 1501 1502 1503 1504
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::push_quick_back()
      quick Quick select to be added. The quick select must return
            rows in rowid order.
  NOTES
    This call can only be made before init() is called.
1505

1506
  RETURN
1507
    FALSE OK
monty@mysql.com's avatar
monty@mysql.com committed
1508
    TRUE  Out of memory.
1509 1510
*/

1511
bool
1512 1513
QUICK_ROR_INTERSECT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick)
{
1514
  return quick_selects.push_back(quick);
1515 1516 1517
}

QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
1518
{
1519
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
1520
  quick_selects.delete_elements();
1521 1522
  delete cpk_quick;
  free_root(&alloc,MYF(0));
monty@mysql.com's avatar
monty@mysql.com committed
1523 1524
  if (need_to_fetch_row && head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
1525 1526 1527
  DBUG_VOID_RETURN;
}

monty@mysql.com's avatar
monty@mysql.com committed
1528

1529 1530
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
                                               TABLE *table)
1531
  : thd(thd_param), scans_inited(FALSE)
1532 1533 1534 1535 1536 1537
{
  index= MAX_KEY;
  head= table;
  rowid_length= table->file->ref_length;
  record= head->record[0];
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
monty@mysql.com's avatar
monty@mysql.com committed
1538
  thd_param->mem_root= &alloc;
1539 1540
}

1541 1542 1543 1544 1545

/*
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::init()
1546

1547 1548 1549 1550 1551
  RETURN
    0      OK
    other  Error code
*/

1552 1553
int QUICK_ROR_UNION_SELECT::init()
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1554
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::init");
1555
  if (init_queue(&queue, quick_selects.elements, 0,
monty@mysql.com's avatar
monty@mysql.com committed
1556
                 FALSE , QUICK_ROR_UNION_SELECT::queue_cmp,
1557 1558 1559
                 (void*) this))
  {
    bzero(&queue, sizeof(QUEUE));
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1560
    DBUG_RETURN(1);
1561
  }
1562

1563
  if (!(cur_rowid= (uchar*) alloc_root(&alloc, 2*head->file->ref_length)))
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1564
    DBUG_RETURN(1);
1565
  prev_rowid= cur_rowid + head->file->ref_length;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1566
  DBUG_RETURN(0);
1567 1568
}

1569

1570
/*
1571
  Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
1572 1573
  queue.

1574 1575 1576 1577 1578 1579
  SYNPOSIS
    QUICK_ROR_UNION_SELECT::queue_cmp()
      arg   Pointer to QUICK_ROR_UNION_SELECT
      val1  First merged select
      val2  Second merged select
*/
1580

1581
int QUICK_ROR_UNION_SELECT::queue_cmp(void *arg, uchar *val1, uchar *val2)
1582
{
1583
  QUICK_ROR_UNION_SELECT *self= (QUICK_ROR_UNION_SELECT*)arg;
1584 1585 1586 1587
  return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
                                   ((QUICK_SELECT_I*)val2)->last_rowid);
}

1588

1589
/*
1590 1591 1592
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()
1593

1594 1595 1596 1597 1598
  RETURN
    0      OK
    other  Error code
*/

1599 1600
int QUICK_ROR_UNION_SELECT::reset()
{
1601
  QUICK_SELECT_I *quick;
1602 1603
  int error;
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
monty@mysql.com's avatar
monty@mysql.com committed
1604
  have_prev_rowid= FALSE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1605 1606 1607 1608 1609 1610 1611 1612
  if (!scans_inited)
  {
    List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
    while ((quick= it++))
    {
      if (quick->init_ror_merged_scan(FALSE))
        DBUG_RETURN(1);
    }
1613
    scans_inited= TRUE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1614 1615
  }
  queue_remove_all(&queue);
1616 1617
  /*
    Initialize scans for merged quick selects and put all merged quick
1618 1619 1620 1621 1622
    selects into the queue.
  */
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
sergefp@mysql.com's avatar
sergefp@mysql.com committed
1623
    if (quick->reset())
1624
      DBUG_RETURN(1);
1625 1626 1627 1628
    if ((error= quick->get_next()))
    {
      if (error == HA_ERR_END_OF_FILE)
        continue;
monty@mysql.com's avatar
monty@mysql.com committed
1629
      DBUG_RETURN(error);
1630 1631
    }
    quick->save_last_pos();
1632
    queue_insert(&queue, (uchar*)quick);
1633 1634
  }

monty@mysql.com's avatar
monty@mysql.com committed
1635
  if (head->file->ha_rnd_init(1))
1636 1637 1638 1639 1640 1641 1642 1643 1644
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(1);
  }

  DBUG_RETURN(0);
}


1645
bool
1646 1647 1648 1649 1650 1651 1652 1653 1654
QUICK_ROR_UNION_SELECT::push_quick_back(QUICK_SELECT_I *quick_sel_range)
{
  return quick_selects.push_back(quick_sel_range);
}

QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT()
{
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT");
  delete_queue(&queue);
1655
  quick_selects.delete_elements();
1656 1657
  if (head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
1658 1659
  free_root(&alloc,MYF(0));
  DBUG_VOID_RETURN;
1660 1661
}

1662

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1663 1664
QUICK_RANGE::QUICK_RANGE()
  :min_key(0),max_key(0),min_length(0),max_length(0),
serg@janus.mylan's avatar
serg@janus.mylan committed
1665 1666
   flag(NO_MIN_RANGE | NO_MAX_RANGE),
  min_keypart_map(0), max_keypart_map(0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
{}

SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
{
  type=arg.type;
  min_flag=arg.min_flag;
  max_flag=arg.max_flag;
  maybe_flag=arg.maybe_flag;
  maybe_null=arg.maybe_null;
  part=arg.part;
  field=arg.field;
  min_value=arg.min_value;
  max_value=arg.max_value;
  next_key_part=arg.next_key_part;
  use_count=1; elements=1;
}


inline void SEL_ARG::make_root()
{
  left=right= &null_element;
  color=BLACK;
  next=prev=0;
  use_count=0; elements=1;
}

1693 1694
SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
                 const uchar *max_value_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1695
  :min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
1696 1697
   elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
   max_value((uchar*) max_value_arg), next(0),prev(0),
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1698 1699 1700 1701 1702
   next_key_part(0),color(BLACK),type(KEY_RANGE)
{
  left=right= &null_element;
}

1703 1704
SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
                 uchar *min_value_, uchar *max_value_,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1705 1706 1707 1708 1709 1710 1711 1712 1713
		 uint8 min_flag_,uint8 max_flag_,uint8 maybe_flag_)
  :min_flag(min_flag_),max_flag(max_flag_),maybe_flag(maybe_flag_),
   part(part_),maybe_null(field_->real_maybe_null()), elements(1),use_count(1),
   field(field_), min_value(min_value_), max_value(max_value_),
   next(0),prev(0),next_key_part(0),color(BLACK),type(KEY_RANGE)
{
  left=right= &null_element;
}

1714 1715
SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, 
                        SEL_ARG **next_arg)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1716 1717
{
  SEL_ARG *tmp;
1718 1719 1720 1721 1722

  /* Bail out if we have already generated too many SEL_ARGs */
  if (++param->alloced_sel_args > MAX_SEL_ARGS)
    return 0;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1723 1724
  if (type != KEY_RANGE)
  {
1725
    if (!(tmp= new (param->mem_root) SEL_ARG(type)))
1726
      return 0;					// out of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1727 1728 1729
    tmp->prev= *next_arg;			// Link into next/prev chain
    (*next_arg)->next=tmp;
    (*next_arg)= tmp;
1730
    tmp->part= this->part;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1731 1732 1733
  }
  else
  {
1734 1735
    if (!(tmp= new (param->mem_root) SEL_ARG(field,part, min_value,max_value,
                                             min_flag, max_flag, maybe_flag)))
1736
      return 0;					// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1737 1738 1739
    tmp->parent=new_parent;
    tmp->next_key_part=next_key_part;
    if (left != &null_element)
1740 1741
      if (!(tmp->left=left->clone(param, tmp, next_arg)))
	return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1742 1743 1744 1745 1746 1747

    tmp->prev= *next_arg;			// Link into next/prev chain
    (*next_arg)->next=tmp;
    (*next_arg)= tmp;

    if (right != &null_element)
1748
      if (!(tmp->right= right->clone(param, tmp, next_arg)))
1749
	return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1750 1751
  }
  increment_use_count(1);
1752
  tmp->color= color;
1753
  tmp->elements= this->elements;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
  return tmp;
}

SEL_ARG *SEL_ARG::first()
{
  SEL_ARG *next_arg=this;
  if (!next_arg->left)
    return 0;					// MAYBE_KEY
  while (next_arg->left != &null_element)
    next_arg=next_arg->left;
  return next_arg;
}

SEL_ARG *SEL_ARG::last()
{
  SEL_ARG *next_arg=this;
  if (!next_arg->right)
    return 0;					// MAYBE_KEY
  while (next_arg->right != &null_element)
    next_arg=next_arg->right;
  return next_arg;
}

1777

bk@work.mysql.com's avatar
bk@work.mysql.com committed
1778 1779 1780
/*
  Check if a compare is ok, when one takes ranges in account
  Returns -2 or 2 if the ranges where 'joined' like  < 2 and >= 2
1781
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1782

1783 1784
static int sel_cmp(Field *field, uchar *a, uchar *b, uint8 a_flag,
                   uint8 b_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
{
  int cmp;
  /* First check if there was a compare to a min or max element */
  if (a_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
  {
    if ((a_flag & (NO_MIN_RANGE | NO_MAX_RANGE)) ==
	(b_flag & (NO_MIN_RANGE | NO_MAX_RANGE)))
      return 0;
    return (a_flag & NO_MIN_RANGE) ? -1 : 1;
  }
  if (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
    return (b_flag & NO_MIN_RANGE) ? 1 : -1;

  if (field->real_maybe_null())			// If null is part of key
  {
    if (*a != *b)
    {
      return *a ? -1 : 1;
    }
    if (*a)
      goto end;					// NULL where equal
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
1806
    a++; b++;					// Skip NULL marker
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1807
  }
1808
  cmp=field->key_cmp(a , b);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
  if (cmp) return cmp < 0 ? -1 : 1;		// The values differed

  // Check if the compared equal arguments was defined with open/closed range
 end:
  if (a_flag & (NEAR_MIN | NEAR_MAX))
  {
    if ((a_flag & (NEAR_MIN | NEAR_MAX)) == (b_flag & (NEAR_MIN | NEAR_MAX)))
      return 0;
    if (!(b_flag & (NEAR_MIN | NEAR_MAX)))
      return (a_flag & NEAR_MIN) ? 2 : -2;
    return (a_flag & NEAR_MIN) ? 1 : -1;
  }
  if (b_flag & (NEAR_MIN | NEAR_MAX))
    return (b_flag & NEAR_MIN) ? -2 : 2;
  return 0;					// The elements where equal
}


1827
SEL_ARG *SEL_ARG::clone_tree(RANGE_OPT_PARAM *param)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1828 1829 1830
{
  SEL_ARG tmp_link,*next_arg,*root;
  next_arg= &tmp_link;
1831 1832
  if (!(root= clone(param, (SEL_ARG *) 0, &next_arg)))
    return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1833 1834
  next_arg->next=0;				// Fix last link
  tmp_link.next->prev=0;			// Fix first link
1835 1836
  if (root)					// If not OOM
    root->use_count= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1837 1838 1839
  return root;
}

1840

1841
/*
1842
  Find the best index to retrieve first N records in given order
1843 1844 1845 1846 1847 1848 1849 1850

  SYNOPSIS
    get_index_for_order()
      table  Table to be accessed
      order  Required ordering
      limit  Number of records that will be retrieved

  DESCRIPTION
1851 1852 1853 1854
    Find the best index that allows to retrieve first #limit records in the 
    given order cheaper then one would retrieve them using full table scan.

  IMPLEMENTATION
1855
    Run through all table indexes and find the shortest index that allows
1856 1857
    records to be retrieved in given order. We look for the shortest index
    as we will have fewer index pages to read with it.
1858 1859 1860 1861 1862

    This function is used only by UPDATE/DELETE, so we take into account how
    the UPDATE/DELETE code will work:
     * index can only be scanned in forward direction
     * HA_EXTRA_KEYREAD will not be used
1863
    Perhaps these assumptions could be relaxed.
1864 1865

  RETURN
1866
    Number of the index that produces the required ordering in the cheapest way
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
    MAX_KEY if no such index was found.
*/

uint get_index_for_order(TABLE *table, ORDER *order, ha_rows limit)
{
  uint idx;
  uint match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
  ORDER *ord;
  
  for (ord= order; ord; ord= ord->next)
    if (!ord->asc)
      return MAX_KEY;

sergefp@mysql.com's avatar
sergefp@mysql.com committed
1880
  for (idx= 0; idx < table->s->keys; idx++)
1881 1882 1883 1884
  {
    if (!(table->keys_in_use_for_query.is_set(idx)))
      continue;
    KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
1885
    uint n_parts=  table->key_info[idx].key_parts;
1886 1887 1888 1889 1890 1891 1892 1893 1894
    uint partno= 0;
    
    /* 
      The below check is sufficient considering we now have either BTREE 
      indexes (records are returned in order for any index prefix) or HASH 
      indexes (records are not returned in order for any index prefix).
    */
    if (!(table->file->index_flags(idx, 0, 1) & HA_READ_ORDER))
      continue;
1895
    for (ord= order; ord && partno < n_parts; ord= ord->next, partno++)
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
    {
      Item *item= order->item[0];
      if (!(item->type() == Item::FIELD_ITEM &&
           ((Item_field*)item)->field->eq(keyinfo[partno].field)))
        break;
    }
    
    if (!ord && table->key_info[idx].key_length < match_key_len)
    {
      /* 
        Ok, the ordering is compatible and this key is shorter then
        previous match (we want shorter keys as we'll have to read fewer
        index pages for the same number of records)
      */
      match_key= idx;
      match_key_len= table->key_info[idx].key_length;
    }
  }

  if (match_key != MAX_KEY)
  {
    /* 
      Found an index that allows records to be retrieved in the requested 
      order. Now we'll check if using the index is cheaper then doing a table
      scan.
    */
    double full_scan_time= table->file->scan_time();
    double index_scan_time= table->file->read_time(match_key, 1, limit);
    if (index_scan_time > full_scan_time)
      match_key= MAX_KEY;
  }
  return match_key;
}


serg@serg.mylan's avatar
serg@serg.mylan committed
1931
/*
1932
  Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
1933 1934 1935 1936 1937
  objects from table read plans.
*/
class TABLE_READ_PLAN
{
public:
1938 1939
  /*
    Plan read cost, with or without cost of full row retrieval, depending
1940 1941
    on plan creation parameters.
  */
1942
  double read_cost;
1943
  ha_rows records; /* estimate of #rows to be examined */
serg@serg.mylan's avatar
serg@serg.mylan committed
1944

1945 1946
  /*
    If TRUE, the scan returns rows in rowid order. This is used only for
1947 1948
    scans that can be both ROR and non-ROR.
  */
1949
  bool is_ror;
1950

1951 1952 1953 1954 1955
  /*
    Create quick select for this plan.
    SYNOPSIS
     make_quick()
       param               Parameter from test_quick_select
monty@mysql.com's avatar
monty@mysql.com committed
1956
       retrieve_full_rows  If TRUE, created quick select will do full record
1957 1958
                           retrieval.
       parent_alloc        Memory pool to use, if any.
1959

1960 1961
    NOTES
      retrieve_full_rows is ignored by some implementations.
1962 1963

    RETURN
1964 1965 1966
      created quick select
      NULL on any error.
  */
1967 1968 1969 1970
  virtual QUICK_SELECT_I *make_quick(PARAM *param,
                                     bool retrieve_full_rows,
                                     MEM_ROOT *parent_alloc=NULL) = 0;

1971
  /* Table read plans are allocated on MEM_ROOT and are never deleted */
1972 1973
  static void *operator new(size_t size, MEM_ROOT *mem_root)
  { return (void*) alloc_root(mem_root, (uint) size); }
1974
  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
1975
  static void operator delete(void *ptr, MEM_ROOT *mem_root) { /* Never called */ }
1976 1977
  virtual ~TABLE_READ_PLAN() {}               /* Remove gcc warning */

1978 1979 1980 1981 1982 1983 1984
};

class TRP_ROR_INTERSECT;
class TRP_ROR_UNION;
class TRP_INDEX_MERGE;


1985
/*
1986
  Plan for a QUICK_RANGE_SELECT scan.
1987 1988 1989
  TRP_RANGE::make_quick ignores retrieve_full_rows parameter because
  QUICK_RANGE_SELECT doesn't distinguish between 'index only' scans and full
  record retrieval scans.
serg@serg.mylan's avatar
serg@serg.mylan committed
1990
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1991

1992
class TRP_RANGE : public TABLE_READ_PLAN
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1993
{
1994
public:
1995 1996
  SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
  uint     key_idx; /* key number in PARAM::key */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1997

1998
  TRP_RANGE(SEL_ARG *key_arg, uint idx_arg)
1999 2000
   : key(key_arg), key_idx(idx_arg)
  {}
2001
  virtual ~TRP_RANGE() {}                     /* Remove gcc warning */
2002

2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc)
  {
    DBUG_ENTER("TRP_RANGE::make_quick");
    QUICK_RANGE_SELECT *quick;
    if ((quick= get_quick_select(param, key_idx, key, parent_alloc)))
    {
      quick->records= records;
      quick->read_time= read_cost;
    }
    DBUG_RETURN(quick);
  }
};
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2016 2017


2018 2019
/* Plan for QUICK_ROR_INTERSECT_SELECT scan. */

2020 2021 2022
class TRP_ROR_INTERSECT : public TABLE_READ_PLAN
{
public:
2023 2024
  TRP_ROR_INTERSECT() {}                      /* Remove gcc warning */
  virtual ~TRP_ROR_INTERSECT() {}             /* Remove gcc warning */
2025
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2026
                             MEM_ROOT *parent_alloc);
2027

2028
  /* Array of pointers to ROR range scans used in this intersection */
2029
  struct st_ror_scan_info **first_scan;
2030 2031
  struct st_ror_scan_info **last_scan; /* End of the above array */
  struct st_ror_scan_info *cpk_scan;  /* Clustered PK scan, if there is one */
monty@mysql.com's avatar
monty@mysql.com committed
2032
  bool is_covering; /* TRUE if no row retrieval phase is necessary */
2033
  double index_scan_costs; /* SUM(cost(index_scan)) */
2034 2035
};

2036

2037
/*
2038 2039
  Plan for QUICK_ROR_UNION_SELECT scan.
  QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
2040
  is ignored by make_quick.
2041
*/
2042

2043 2044 2045
class TRP_ROR_UNION : public TABLE_READ_PLAN
{
public:
2046 2047
  TRP_ROR_UNION() {}                          /* Remove gcc warning */
  virtual ~TRP_ROR_UNION() {}                 /* Remove gcc warning */
2048
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2049
                             MEM_ROOT *parent_alloc);
2050 2051
  TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
  TABLE_READ_PLAN **last_ror;  /* end of the above array */
2052 2053
};

2054 2055 2056 2057

/*
  Plan for QUICK_INDEX_MERGE_SELECT scan.
  QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
2058
  is ignored by make_quick.
2059 2060
*/

2061 2062 2063
class TRP_INDEX_MERGE : public TABLE_READ_PLAN
{
public:
2064 2065
  TRP_INDEX_MERGE() {}                        /* Remove gcc warning */
  virtual ~TRP_INDEX_MERGE() {}               /* Remove gcc warning */
2066
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2067
                             MEM_ROOT *parent_alloc);
2068 2069
  TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
  TRP_RANGE **range_scans_end; /* end of the array */
2070 2071 2072
};


2073 2074 2075 2076 2077 2078 2079
/*
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan. 
*/

class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
{
private:
2080
  bool have_min, have_max, have_agg_distinct;
2081 2082 2083 2084 2085 2086 2087
  KEY_PART_INFO *min_max_arg_part;
  uint group_prefix_len;
  uint used_key_parts;
  uint group_key_parts;
  KEY *index_info;
  uint index;
  uint key_infix_len;
2088
  uchar key_infix[MAX_KEY_LENGTH];
2089 2090 2091
  SEL_TREE *range_tree; /* Represents all range predicates in the query. */
  SEL_ARG  *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
  uint param_idx; /* Index of used key in param->key. */
2092
  bool is_index_scan; /* Use index_next() instead of random read */ 
2093
public:
2094
  /* Number of records selected by the ranges in index_tree. */
2095 2096
  ha_rows quick_prefix_records;
public:
2097 2098
  TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg, 
                    bool have_agg_distinct_arg,
2099 2100 2101
                    KEY_PART_INFO *min_max_arg_part_arg,
                    uint group_prefix_len_arg, uint used_key_parts_arg,
                    uint group_key_parts_arg, KEY *index_info_arg,
2102
                    uint index_arg, uint key_infix_len_arg,
2103
                    uchar *key_infix_arg,
2104 2105 2106
                    SEL_TREE *tree_arg, SEL_ARG *index_tree_arg,
                    uint param_idx_arg, ha_rows quick_prefix_records_arg)
  : have_min(have_min_arg), have_max(have_max_arg),
2107
    have_agg_distinct(have_agg_distinct_arg),
2108 2109 2110 2111
    min_max_arg_part(min_max_arg_part_arg),
    group_prefix_len(group_prefix_len_arg), used_key_parts(used_key_parts_arg),
    group_key_parts(group_key_parts_arg), index_info(index_info_arg),
    index(index_arg), key_infix_len(key_infix_len_arg), range_tree(tree_arg),
2112
    index_tree(index_tree_arg), param_idx(param_idx_arg), is_index_scan(FALSE),
2113
    quick_prefix_records(quick_prefix_records_arg)
2114 2115 2116 2117
    {
      if (key_infix_len)
        memcpy(this->key_infix, key_infix_arg, key_infix_len);
    }
2118
  virtual ~TRP_GROUP_MIN_MAX() {}             /* Remove gcc warning */
2119 2120 2121

  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
2122
  void use_index_scan() { is_index_scan= TRUE; }
2123 2124 2125
};


2126
/*
2127
  Fill param->needed_fields with bitmap of fields used in the query.
2128
  SYNOPSIS
2129 2130
    fill_used_fields_bitmap()
      param Parameter from test_quick_select function.
2131

2132 2133 2134
  NOTES
    Clustered PK members are not put into the bitmap as they are implicitly
    present in all keys (and it is impossible to avoid reading them).
2135 2136 2137
  RETURN
    0  Ok
    1  Out of memory.
2138 2139 2140 2141 2142
*/

static int fill_used_fields_bitmap(PARAM *param)
{
  TABLE *table= param->table;
2143
  my_bitmap_map *tmp;
2144
  uint pk;
2145
  param->tmp_covered_fields.bitmap= 0;
2146 2147 2148 2149
  param->fields_bitmap_size= table->s->column_bitmap_size;
  if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
                                  param->fields_bitmap_size)) ||
      bitmap_init(&param->needed_fields, tmp, table->s->fields, FALSE))
2150
    return 1;
2151

2152 2153
  bitmap_copy(&param->needed_fields, table->read_set);
  bitmap_union(&param->needed_fields, table->write_set);
2154

2155
  pk= param->table->s->primary_key;
2156
  if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
2157
  {
2158
    /* The table uses clustered PK and it is not internally generated */
2159
    KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
2160
    KEY_PART_INFO *key_part_end= key_part +
2161
                                 param->table->key_info[pk].key_parts;
2162
    for (;key_part != key_part_end; ++key_part)
2163
      bitmap_clear_bit(&param->needed_fields, key_part->fieldnr-1);
2164 2165 2166 2167 2168
  }
  return 0;
}


serg@serg.mylan's avatar
serg@serg.mylan committed
2169
/*
2170
  Test if a key can be used in different ranges
serg@serg.mylan's avatar
serg@serg.mylan committed
2171 2172

  SYNOPSIS
2173 2174 2175 2176 2177
    SQL_SELECT::test_quick_select()
      thd               Current thread
      keys_to_use       Keys to use for range retrieval
      prev_tables       Tables assumed to be already read when the scan is
                        performed (but not read at the moment of this call)
2178 2179 2180
      limit             Query limit
      force_quick_range Prefer to use range (instead of full table scan) even
                        if it is more expensive.
2181 2182 2183 2184 2185

  NOTES
    Updates the following in the select parameter:
      needed_reg - Bits for keys with may be used if all prev regs are read
      quick      - Parameter to use when reading records.
2186

2187
    In the table struct the following information is updated:
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
      quick_keys           - Which keys can be used
      quick_rows           - How many rows the key matches
      quick_condition_rows - E(# rows that will satisfy the table condition)

  IMPLEMENTATION
    quick_condition_rows value is obtained as follows:
      
      It is a minimum of E(#output rows) for all considered table access
      methods (range and index_merge accesses over various indexes).
    
    The obtained value is not a true E(#rows that satisfy table condition)
    but rather a pessimistic estimate. To obtain a true E(#...) one would
    need to combine estimates of various access methods, taking into account
    correlations between sets of rows they will return.
    
    For example, if values of tbl.key1 and tbl.key2 are independent (a right
2204
    assumption if we have no information about their correlation) then the
2205 2206 2207
    correct estimate will be:
    
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) = 
2208
      = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
2209

2210 2211 2212 2213 2214
    which is smaller than 
      
       MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))

    which is currently produced.
serg@serg.mylan's avatar
serg@serg.mylan committed
2215

2216
  TODO
2217 2218 2219 2220 2221 2222 2223
   * Change the value returned in quick_condition_rows from a pessimistic
     estimate to true E(#rows that satisfy table condition). 
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection 
      for this)
   
   * Check if this function really needs to modify keys_to_use, and change the
     code to pass it by reference if it doesn't.
2224

2225 2226 2227
   * In addition to force_quick_range other means can be (an usually are) used
     to make this function prefer range over full table scan. Figure out if
     force_quick_range is really needed.
2228

2229 2230 2231 2232
  RETURN
   -1 if impossible select (i.e. certainly no rows will be selected)
    0 if can't use quick_select
    1 if found usable ranges and quick select has been successfully created.
serg@serg.mylan's avatar
serg@serg.mylan committed
2233
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2234

2235 2236
int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
				  table_map prev_tables,
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2237 2238 2239 2240
				  ha_rows limit, bool force_quick_range)
{
  uint idx;
  double scan_time;
2241
  DBUG_ENTER("SQL_SELECT::test_quick_select");
serg@serg.mylan's avatar
serg@serg.mylan committed
2242
  DBUG_PRINT("enter",("keys_to_use: %lu  prev_tables: %lu  const_tables: %lu",
2243
		      (ulong) keys_to_use.to_ulonglong(), (ulong) prev_tables,
serg@serg.mylan's avatar
serg@serg.mylan committed
2244
		      (ulong) const_tables));
2245
  DBUG_PRINT("info", ("records: %lu", (ulong) head->file->stats.records));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2246 2247
  delete quick;
  quick=0;
2248 2249
  needed_reg.clear_all();
  quick_keys.clear_all();
2250 2251
  if (keys_to_use.is_clear_all())
    DBUG_RETURN(0);
2252
  records= head->file->stats.records;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2253 2254
  if (!records)
    records++;					/* purecov: inspected */
2255 2256
  scan_time= (double) records / TIME_FOR_COMPARE + 1;
  read_time= (double) head->file->scan_time() + scan_time + 1.1;
2257 2258
  if (head->force_index)
    scan_time= read_time= DBL_MAX;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2259
  if (limit < records)
2260
    read_time= (double) records + scan_time + 1; // Force to use index
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2261
  else if (read_time <= 2.0 && !force_quick_range)
2262
    DBUG_RETURN(0);				/* No need for quick select */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2263

2264
  DBUG_PRINT("info",("Time to scan table: %g", read_time));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2265

2266 2267
  keys_to_use.intersect(head->keys_in_use_for_query);
  if (!keys_to_use.is_clear_all())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2268
  {
2269
#ifndef EMBEDDED_LIBRARY                      // Avoid compiler warning
2270
    uchar buff[STACK_BUFF_ALLOC];
2271
#endif
2272
    MEM_ROOT alloc;
2273
    SEL_TREE *tree= NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2274
    KEY_PART *key_parts;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2275
    KEY *key_info;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2276
    PARAM param;
serg@serg.mylan's avatar
serg@serg.mylan committed
2277

2278
    if (check_stack_overrun(thd, 2*STACK_MIN_SIZE + sizeof(PARAM), buff))
2279 2280
      DBUG_RETURN(0);                           // Fatal error flag is set

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2281
    /* set up parameter that is passed to all functions */
2282
    param.thd= thd;
2283
    param.baseflag= head->file->ha_table_flags();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2284 2285 2286 2287 2288
    param.prev_tables=prev_tables | const_tables;
    param.read_tables=read_tables;
    param.current_table= head->map;
    param.table=head;
    param.keys=0;
2289
    param.mem_root= &alloc;
2290
    param.old_root= thd->mem_root;
2291
    param.needed_reg= &needed_reg;
2292
    param.imerge_cost_buff_size= 0;
2293
    param.using_real_indexes= TRUE;
2294
    param.remove_jump_scans= TRUE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2295

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2296
    thd->no_errors=1;				// Don't warn about NULL
2297
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
2298 2299 2300 2301
    if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc,
                                                  sizeof(KEY_PART)*
                                                  head->s->key_parts)) ||
        fill_used_fields_bitmap(&param))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2302
    {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2303
      thd->no_errors=0;
2304
      free_root(&alloc,MYF(0));			// Return memory & allocator
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2305 2306 2307
      DBUG_RETURN(0);				// Can't use range
    }
    key_parts= param.key_parts;
2308
    thd->mem_root= &alloc;
2309 2310 2311 2312

    /*
      Make an array with description of all key parts of all table keys.
      This is used in get_mm_parts function.
2313
    */
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2314
    key_info= head->key_info;
2315
    for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2316
    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2317
      KEY_PART_INFO *key_part_info;
2318
      if (!keys_to_use.is_set(idx))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2319 2320 2321 2322 2323
	continue;
      if (key_info->flags & HA_FULLTEXT)
	continue;    // ToDo: ft-keys in non-ft ranges, if possible   SerG

      param.key[param.keys]=key_parts;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2324 2325 2326
      key_part_info= key_info->key_part;
      for (uint part=0 ; part < key_info->key_parts ;
	   part++, key_parts++, key_part_info++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2327
      {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
2328 2329 2330 2331 2332 2333
	key_parts->key=		 param.keys;
	key_parts->part=	 part;
	key_parts->length=       key_part_info->length;
	key_parts->store_length= key_part_info->store_length;
	key_parts->field=	 key_part_info->field;
	key_parts->null_bit=	 key_part_info->null_bit;
2334
        key_parts->image_type =
2335
          (key_info->flags & HA_SPATIAL) ? Field::itMBR : Field::itRAW;
2336 2337
        /* Only HA_PART_KEY_SEG is used */
        key_parts->flag=         (uint8) key_part_info->key_part_flag;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2338 2339 2340 2341
      }
      param.real_keynr[param.keys++]=idx;
    }
    param.key_parts_end=key_parts;
2342
    param.alloced_sel_args= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2343

sergefp@mysql.com's avatar
sergefp@mysql.com committed
2344
    /* Calculate cost of full index read for the shortest covering index */
2345
    if (!head->covering_keys.is_clear_all())
sergefp@mysql.com's avatar
sergefp@mysql.com committed
2346
    {
2347
      int key_for_use= find_shortest_key(head, &head->covering_keys);
2348 2349 2350
      double key_read_time= (get_index_only_read_time(&param, records,
                                                     key_for_use) +
                             (double) records / TIME_FOR_COMPARE);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
2351 2352 2353 2354 2355
      DBUG_PRINT("info",  ("'all'+'using index' scan will be using key %d, "
                           "read time %g", key_for_use, key_read_time));
      if (key_read_time < read_time)
        read_time= key_read_time;
    }
2356

2357 2358 2359 2360 2361
    TABLE_READ_PLAN *best_trp= NULL;
    TRP_GROUP_MIN_MAX *group_trp;
    double best_read_time= read_time;

    if (cond)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2362
    {
2363 2364 2365 2366 2367 2368 2369 2370
      if ((tree= get_mm_tree(&param,cond)))
      {
        if (tree->type == SEL_TREE::IMPOSSIBLE)
        {
          records=0L;                      /* Return -1 from this function. */
          read_time= (double) HA_POS_ERROR;
          goto free_mem;
        }
2371 2372 2373 2374 2375 2376
        /*
          If the tree can't be used for range scans, proceed anyway, as we
          can construct a group-min-max quick select
        */
        if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
          tree= NULL;
2377
      }
2378 2379 2380 2381 2382 2383
    }

    /*
      Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
      Notice that it can be constructed no matter if there is a range tree.
    */
2384
    group_trp= get_best_group_min_max(&param, tree, best_read_time);
2385
    if (group_trp)
2386
    {
2387 2388 2389 2390 2391 2392 2393
      param.table->quick_condition_rows= min(group_trp->records,
                                             head->file->stats.records);
      if (group_trp->read_cost < best_read_time)
      {
        best_trp= group_trp;
        best_read_time= best_trp->read_cost;
      }
2394 2395 2396
    }

    if (tree)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2397
    {
monty@mysql.com's avatar
monty@mysql.com committed
2398 2399 2400
      /*
        It is possible to use a range-based quick select (but it might be
        slower than 'all' table scan).
2401 2402
      */
      if (tree->merges.is_empty())
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2403
      {
2404 2405 2406 2407 2408
        TRP_RANGE         *range_trp;
        TRP_ROR_INTERSECT *rori_trp;
        bool can_build_covering= FALSE;

        /* Get best 'range' plan and prepare data for making other plans */
2409
        if ((range_trp= get_key_scans_params(&param, tree, FALSE, TRUE,
2410 2411 2412 2413 2414 2415
                                             best_read_time)))
        {
          best_trp= range_trp;
          best_read_time= best_trp->read_cost;
        }

2416
        /*
2417 2418 2419
          Simultaneous key scans and row deletes on several handler
          objects are not allowed so don't use ROR-intersection for
          table deletes.
2420
        */
2421
        if ((thd->lex->sql_command != SQLCOM_DELETE) && 
2422
             optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE))
2423
        {
2424
          /*
2425 2426
            Get best non-covering ROR-intersection plan and prepare data for
            building covering ROR-intersection.
2427
          */
2428 2429
          if ((rori_trp= get_best_ror_intersect(&param, tree, best_read_time,
                                                &can_build_covering)))
2430
          {
2431 2432
            best_trp= rori_trp;
            best_read_time= best_trp->read_cost;
2433 2434
            /*
              Try constructing covering ROR-intersect only if it looks possible
2435 2436
              and worth doing.
            */
2437 2438 2439 2440
            if (!rori_trp->is_covering && can_build_covering &&
                (rori_trp= get_best_covering_ror_intersect(&param, tree,
                                                           best_read_time)))
              best_trp= rori_trp;
2441 2442
          }
        }
2443 2444 2445
      }
      else
      {
2446
        if (optimizer_flag(thd, OPTIMIZER_SWITCH_INDEX_MERGE))
2447
        {
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466
          /* Try creating index_merge/ROR-union scan. */
          SEL_IMERGE *imerge;
          TABLE_READ_PLAN *best_conj_trp= NULL, *new_conj_trp;
          LINT_INIT(new_conj_trp); /* no empty index_merge lists possible */
          DBUG_PRINT("info",("No range reads possible,"
                             " trying to construct index_merge"));
          List_iterator_fast<SEL_IMERGE> it(tree->merges);
          while ((imerge= it++))
          {
            new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
            if (new_conj_trp)
              set_if_smaller(param.table->quick_condition_rows, 
                             new_conj_trp->records);
            if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
                                   best_conj_trp->read_cost))
              best_conj_trp= new_conj_trp;
          }
          if (best_conj_trp)
            best_trp= best_conj_trp;
2467
        }
2468 2469
      }
    }
2470

2471
    thd->mem_root= param.old_root;
2472 2473 2474 2475 2476 2477 2478 2479 2480

    /* If we got a read plan, create a quick select from it. */
    if (best_trp)
    {
      records= best_trp->records;
      if (!(quick= best_trp->make_quick(&param, TRUE)) || quick->init())
      {
        delete quick;
        quick= NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2481 2482
      }
    }
2483 2484

  free_mem:
2485
    free_root(&alloc,MYF(0));			// Return memory & allocator
2486
    thd->mem_root= param.old_root;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2487
    thd->no_errors=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
2488
  }
2489

2490
  DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
2491

bk@work.mysql.com's avatar
bk@work.mysql.com committed
2492 2493 2494 2495 2496 2497 2498
  /*
    Assume that if the user is using 'limit' we will only need to scan
    limit rows if we are using a key
  */
  DBUG_RETURN(records ? test(quick) : -1);
}

2499
/****************************************************************************
2500
 * Partition pruning module
2501 2502 2503
 ****************************************************************************/
#ifdef WITH_PARTITION_STORAGE_ENGINE

2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
/*
  PartitionPruningModule

  This part of the code does partition pruning. Partition pruning solves the
  following problem: given a query over partitioned tables, find partitions
  that we will not need to access (i.e. partitions that we can assume to be
  empty) when executing the query.
  The set of partitions to prune doesn't depend on which query execution
  plan will be used to execute the query.
  
  HOW IT WORKS
  
  Partition pruning module makes use of RangeAnalysisModule. The following
  examples show how the problem of partition pruning can be reduced to the 
  range analysis problem:
  
  EXAMPLE 1
    Consider a query:
    
      SELECT * FROM t1 WHERE (t1.a < 5 OR t1.a = 10) AND t1.a > 3 AND t1.b='z'
    
    where table t1 is partitioned using PARTITION BY RANGE(t1.a).  An apparent
    way to find the used (i.e. not pruned away) partitions is as follows:
    
    1. analyze the WHERE clause and extract the list of intervals over t1.a
       for the above query we will get this list: {(3 < t1.a < 5), (t1.a=10)}

    2. for each interval I
       {
         find partitions that have non-empty intersection with I;
         mark them as used;
       }
       
  EXAMPLE 2
    Suppose the table is partitioned by HASH(part_func(t1.a, t1.b)). Then
    we need to:

    1. Analyze the WHERE clause and get a list of intervals over (t1.a, t1.b).
       The list of intervals we'll obtain will look like this:
       ((t1.a, t1.b) = (1,'foo')),
       ((t1.a, t1.b) = (2,'bar')), 
2545
       ((t1,a, t1.b) > (10,'zz'))
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
       
    2. for each interval I 
       {
         if (the interval has form "(t1.a, t1.b) = (const1, const2)" )
         {
           calculate HASH(part_func(t1.a, t1.b));
           find which partition has records with this hash value and mark
             it as used;
         }
         else
         {
           mark all partitions as used; 
           break;
         }
       }

   For both examples the step #1 is exactly what RangeAnalysisModule could
   be used to do, if it was provided with appropriate index description
   (array of KEY_PART structures). 
   In example #1, we need to provide it with description of index(t1.a), 
   in example #2, we need to provide it with description of index(t1.a, t1.b).
   
   These index descriptions are further called "partitioning index
   descriptions". Note that it doesn't matter if such indexes really exist,
   as range analysis module only uses the description.
   
   Putting it all together, partitioning module works as follows:
   
   prune_partitions() {
2575
     call create_partition_index_description();
2576 2577 2578 2579 2580 2581 2582 2583 2584

     call get_mm_tree(); // invoke the RangeAnalysisModule
     
     // analyze the obtained interval list and get used partitions 
     call find_used_partitions();
  }

*/

2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
struct st_part_prune_param;
struct st_part_opt_info;

typedef void (*mark_full_part_func)(partition_info*, uint32);

/*
  Partition pruning operation context
*/
typedef struct st_part_prune_param
{
2595
  RANGE_OPT_PARAM range_param; /* Range analyzer parameters */
2596

2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
  /***************************************************************
   Following fields are filled in based solely on partitioning 
   definition and not modified after that:
   **************************************************************/
  partition_info *part_info; /* Copy of table->part_info */
  /* Function to get partition id from partitioning fields only */
  get_part_id_func get_top_partition_id_func;
  /* Function to mark a partition as used (w/all subpartitions if they exist)*/
  mark_full_part_func mark_full_partition_used;
 
  /* Partitioning 'index' description, array of key parts */
  KEY_PART *key;
  
  /*
    Number of fields in partitioning 'index' definition created for
    partitioning (0 if partitioning 'index' doesn't include partitioning
    fields)
  */
  uint part_fields;
  uint subpart_fields; /* Same as above for subpartitioning */
  
  /* 
    Number of the last partitioning field keypart in the index, or -1 if
    partitioning index definition doesn't include partitioning fields.
  */
  int last_part_partno;
  int last_subpart_partno; /* Same as above for supartitioning */

  /*
    is_part_keypart[i] == test(keypart #i in partitioning index is a member
                               used in partitioning)
    Used to maintain current values of cur_part_fields and cur_subpart_fields
  */
  my_bool *is_part_keypart;
  /* Same as above for subpartitioning */
  my_bool *is_subpart_keypart;

2634 2635
  my_bool ignore_part_fields; /* Ignore rest of partioning fields */

2636 2637 2638 2639 2640 2641 2642 2643 2644
  /***************************************************************
   Following fields form find_used_partitions() recursion context:
   **************************************************************/
  SEL_ARG **arg_stack;     /* "Stack" of SEL_ARGs */
  SEL_ARG **arg_stack_end; /* Top of the stack    */
  /* Number of partitioning fields for which we have a SEL_ARG* in arg_stack */
  uint cur_part_fields;
  /* Same as cur_part_fields, but for subpartitioning */
  uint cur_subpart_fields;
2645

2646 2647 2648
  /* Iterator to be used to obtain the "current" set of used partitions */
  PARTITION_ITERATOR part_iter;

2649
  /* Initialized bitmap of num_subparts size */
2650
  MY_BITMAP subparts_bitmap;
2651 2652 2653 2654 2655

  uchar *cur_min_key;
  uchar *cur_max_key;

  uint cur_min_flag, cur_max_flag;
2656 2657
} PART_PRUNE_PARAM;

2658
static bool create_partition_index_description(PART_PRUNE_PARAM *prune_par);
2659
static int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree);
2660 2661 2662 2663
static int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar,
                                       SEL_IMERGE *imerge);
static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
                                            List<SEL_IMERGE> &merges);
2664 2665 2666 2667 2668 2669
static void mark_all_partitions_as_used(partition_info *part_info);

#ifndef DBUG_OFF
static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end);
static void dbug_print_field(Field *field);
static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part);
2670
static void dbug_print_singlepoint_range(SEL_ARG **start, uint num);
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
#endif


/*
  Perform partition pruning for a given table and condition.

  SYNOPSIS
    prune_partitions()
      thd           Thread handle
      table         Table to perform partition pruning for
      pprune_cond   Condition to use for partition pruning
  
  DESCRIPTION
    This function assumes that all partitions are marked as unused when it
    is invoked. The function analyzes the condition, finds partitions that
    need to be used to retrieve the records that match the condition, and 
    marks them as used by setting appropriate bit in part_info->used_partitions
    In the worst case all partitions are marked as used.

  NOTE
    This function returns promptly if called for non-partitioned table.

  RETURN
    TRUE   We've inferred that no partitions need to be used (i.e. no table
           records will satisfy pprune_cond)
    FALSE  Otherwise
*/

bool prune_partitions(THD *thd, TABLE *table, Item *pprune_cond)
{
  bool retval= FALSE;
  partition_info *part_info = table->part_info;
  DBUG_ENTER("prune_partitions");

  if (!part_info)
    DBUG_RETURN(FALSE); /* not a partitioned table */
  
  if (!pprune_cond)
  {
    mark_all_partitions_as_used(part_info);
    DBUG_RETURN(FALSE);
  }
  
  PART_PRUNE_PARAM prune_param;
  MEM_ROOT alloc;
  RANGE_OPT_PARAM  *range_par= &prune_param.range_param;
2717
  my_bitmap_map *old_sets[2];
2718 2719 2720 2721 2722 2723

  prune_param.part_info= part_info;
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
  range_par->mem_root= &alloc;
  range_par->old_root= thd->mem_root;

2724
  if (create_partition_index_description(&prune_param))
2725 2726 2727 2728 2729 2730
  {
    mark_all_partitions_as_used(part_info);
    free_root(&alloc,MYF(0));		// Return memory & allocator
    DBUG_RETURN(FALSE);
  }
  
2731 2732
  dbug_tmp_use_all_columns(table, old_sets, 
                           table->read_set, table->write_set);
2733 2734 2735 2736 2737 2738 2739 2740
  range_par->thd= thd;
  range_par->table= table;
  /* range_par->cond doesn't need initialization */
  range_par->prev_tables= range_par->read_tables= 0;
  range_par->current_table= table->map;

  range_par->keys= 1; // one index
  range_par->using_real_indexes= FALSE;
2741
  range_par->remove_jump_scans= FALSE;
2742
  range_par->real_keynr[0]= 0;
2743
  range_par->alloced_sel_args= 0;
2744 2745 2746

  thd->no_errors=1;				// Don't warn about NULL
  thd->mem_root=&alloc;
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2747 2748 2749

  bitmap_clear_all(&part_info->used_partitions);

2750 2751 2752 2753 2754
  prune_param.key= prune_param.range_param.key_parts;
  SEL_TREE *tree;
  int res;

  tree= get_mm_tree(range_par, pprune_cond);
2755
  if (!tree)
2756 2757 2758 2759 2760 2761 2762
    goto all_used;

  if (tree->type == SEL_TREE::IMPOSSIBLE)
  {
    retval= TRUE;
    goto end;
  }
2763 2764 2765

  if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
    goto all_used;
2766

2767 2768
  if (tree->merges.is_empty())
  {
2769
    /* Range analysis has produced a single list of intervals. */
2770 2771 2772
    prune_param.arg_stack_end= prune_param.arg_stack;
    prune_param.cur_part_fields= 0;
    prune_param.cur_subpart_fields= 0;
2773 2774 2775 2776 2777
    
    prune_param.cur_min_key= prune_param.range_param.min_key;
    prune_param.cur_max_key= prune_param.range_param.max_key;
    prune_param.cur_min_flag= prune_param.cur_max_flag= 0;

2778
    init_all_partitions_iterator(part_info, &prune_param.part_iter);
2779 2780 2781 2782 2783 2784
    if (!tree->keys[0] || (-1 == (res= find_used_partitions(&prune_param,
                                                            tree->keys[0]))))
      goto all_used;
  }
  else
  {
2785 2786
    if (tree->merges.elements == 1)
    {
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
      /* 
        Range analysis has produced a "merge" of several intervals lists, a 
        SEL_TREE that represents an expression in form         
          sel_imerge = (tree1 OR tree2 OR ... OR treeN)
        that cannot be reduced to one tree. This can only happen when 
        partitioning index has several keyparts and the condition is OR of
        conditions that refer to different key parts. For example, we'll get
        here for "partitioning_field=const1 OR subpartitioning_field=const2"
      */
      if (-1 == (res= find_used_partitions_imerge(&prune_param,
                                                  tree->merges.head())))
2798 2799 2800
        goto all_used;
    }
    else
2801
    {
2802 2803 2804 2805 2806 2807 2808 2809 2810
      /* 
        Range analysis has produced a list of several imerges, i.e. a
        structure that represents a condition in form 
        imerge_list= (sel_imerge1 AND sel_imerge2 AND ... AND sel_imergeN)
        This is produced for complicated WHERE clauses that range analyzer
        can't really analyze properly.
      */
      if (-1 == (res= find_used_partitions_imerge_list(&prune_param,
                                                       tree->merges)))
2811 2812 2813 2814
        goto all_used;
    }
  }
  
2815 2816 2817 2818 2819 2820
  /*
    res == 0 => no used partitions => retval=TRUE
    res == 1 => some used partitions => retval=FALSE
    res == -1 - we jump over this line to all_used:
  */
  retval= test(!res);
2821 2822 2823
  goto end;

all_used:
2824
  retval= FALSE; // some partitions are used
2825 2826
  mark_all_partitions_as_used(prune_param.part_info);
end:
2827
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
2828 2829 2830 2831 2832 2833 2834 2835
  thd->no_errors=0;
  thd->mem_root= range_par->old_root;
  free_root(&alloc,MYF(0));			// Return memory & allocator
  DBUG_RETURN(retval);
}


/*
2836
  Store field key image to table record
2837 2838

  SYNOPSIS
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
    store_key_image_to_rec()
      field  Field which key image should be stored
      ptr    Field value in key format
      len    Length of the value, in bytes

  DESCRIPTION
    Copy the field value from its key image to the table record. The source
    is the value in key image format, occupying len bytes in buffer pointed
    by ptr. The destination is table record, in "field value in table record"
    format.
2849 2850
*/

2851
void store_key_image_to_rec(Field *field, uchar *ptr, uint len)
2852 2853
{
  /* Do the same as print_key() does */ 
2854 2855
  my_bitmap_map *old_map;

2856 2857 2858 2859 2860 2861 2862
  if (field->real_maybe_null())
  {
    if (*ptr)
    {
      field->set_null();
      return;
    }
2863
    field->set_notnull();
2864 2865
    ptr++;
  }    
2866 2867
  old_map= dbug_tmp_use_all_columns(field->table,
                                    field->table->write_set);
2868
  field->set_key_image(ptr, len); 
2869
  dbug_tmp_restore_column_map(field->table->write_set, old_map);
2870 2871 2872 2873 2874 2875 2876 2877 2878
}


/*
  For SEL_ARG* array, store sel_arg->min values into table record buffer

  SYNOPSIS
    store_selargs_to_rec()
      ppar   Partition pruning context
2879
      start  Array of SEL_ARG* for which the minimum values should be stored
2880
      num    Number of elements in the array
2881 2882 2883 2884

  DESCRIPTION
    For each SEL_ARG* interval in the specified array, store the left edge
    field value (sel_arg->min, key image format) into the table record.
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
*/

static void store_selargs_to_rec(PART_PRUNE_PARAM *ppar, SEL_ARG **start,
                                 int num)
{
  KEY_PART *parts= ppar->range_param.key_parts;
  for (SEL_ARG **end= start + num; start != end; start++)
  {
    SEL_ARG *sel_arg= (*start);
    store_key_image_to_rec(sel_arg->field, sel_arg->min_value,
                           parts[sel_arg->part].length);
  }
}


/* Mark a partition as used in the case when there are no subpartitions */
static void mark_full_partition_used_no_parts(partition_info* part_info,
                                              uint32 part_id)
{
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2904 2905
  DBUG_ENTER("mark_full_partition_used_no_parts");
  DBUG_PRINT("enter", ("Mark partition %u as used", part_id));
2906
  bitmap_set_bit(&part_info->used_partitions, part_id);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2907
  DBUG_VOID_RETURN;
2908 2909 2910 2911 2912 2913 2914
}


/* Mark a partition as used in the case when there are subpartitions */
static void mark_full_partition_used_with_parts(partition_info *part_info,
                                                uint32 part_id)
{
2915 2916
  uint32 start= part_id * part_info->num_subparts;
  uint32 end=   start + part_info->num_subparts; 
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2917 2918
  DBUG_ENTER("mark_full_partition_used_with_parts");

2919
  for (; start != end; start++)
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2920 2921
  {
    DBUG_PRINT("info", ("1:Mark subpartition %u as used", start));
2922
    bitmap_set_bit(&part_info->used_partitions, start);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2923 2924
  }
  DBUG_VOID_RETURN;
2925 2926
}

2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
/*
  Find the set of used partitions for List<SEL_IMERGE>
  SYNOPSIS
    find_used_partitions_imerge_list
      ppar      Partition pruning context.
      key_tree  Intervals tree to perform pruning for.
      
  DESCRIPTION
    List<SEL_IMERGE> represents "imerge1 AND imerge2 AND ...". 
    The set of used partitions is an intersection of used partitions sets
    for imerge_{i}.
2938
    We accumulate this intersection in a separate bitmap.
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
 
  RETURN 
    See find_used_partitions()
*/

static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
                                            List<SEL_IMERGE> &merges)
{
  MY_BITMAP all_merges;
  uint bitmap_bytes;
2949
  my_bitmap_map *bitmap_buf;
2950 2951
  uint n_bits= ppar->part_info->used_partitions.n_bits;
  bitmap_bytes= bitmap_buffer_size(n_bits);
2952 2953
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(ppar->range_param.mem_root,
                                                bitmap_bytes)))
2954
  {
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2955
    /*
2956
      Fallback, process just the first SEL_IMERGE. This can leave us with more
2957 2958 2959 2960 2961 2962
      partitions marked as used then actually needed.
    */
    return find_used_partitions_imerge(ppar, merges.head());
  }
  bitmap_init(&all_merges, bitmap_buf, n_bits, FALSE);
  bitmap_set_prefix(&all_merges, n_bits);
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2963

2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
  List_iterator<SEL_IMERGE> it(merges);
  SEL_IMERGE *imerge;
  while ((imerge=it++))
  {
    int res= find_used_partitions_imerge(ppar, imerge);
    if (!res)
    {
      /* no used partitions on one ANDed imerge => no used partitions at all */
      return 0;
    }
patg@govinda.patg.net's avatar
patg@govinda.patg.net committed
2974

2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
    if (res != -1)
      bitmap_intersect(&all_merges, &ppar->part_info->used_partitions);

    if (bitmap_is_clear_all(&all_merges))
      return 0;

    bitmap_clear_all(&ppar->part_info->used_partitions);
  }
  memcpy(ppar->part_info->used_partitions.bitmap, all_merges.bitmap,
         bitmap_bytes);
  return 1;
}


/*
  Find the set of used partitions for SEL_IMERGE structure
  SYNOPSIS
    find_used_partitions_imerge()
      ppar      Partition pruning context.
      key_tree  Intervals tree to perform pruning for.
      
  DESCRIPTION
    SEL_IMERGE represents "tree1 OR tree2 OR ...". The implementation is
    trivial - just use mark used partitions for each tree and bail out early
    if for some tree_{i} all partitions are used.
 
  RETURN 
    See find_used_partitions().
*/

3005
static
3006
int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar, SEL_IMERGE *imerge)
3007
{
3008 3009 3010 3011 3012 3013
  int res= 0;
  for (SEL_TREE **ptree= imerge->trees; ptree < imerge->trees_next; ptree++)
  {
    ppar->arg_stack_end= ppar->arg_stack;
    ppar->cur_part_fields= 0;
    ppar->cur_subpart_fields= 0;
3014 3015 3016 3017 3018
    
    ppar->cur_min_key= ppar->range_param.min_key;
    ppar->cur_max_key= ppar->range_param.max_key;
    ppar->cur_min_flag= ppar->cur_max_flag= 0;

3019
    init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3020 3021
    SEL_ARG *key_tree= (*ptree)->keys[0];
    if (!key_tree || (-1 == (res |= find_used_partitions(ppar, key_tree))))
3022 3023 3024
      return -1;
  }
  return res;
3025 3026 3027 3028
}


/*
3029
  Collect partitioning ranges for the SEL_ARG tree and mark partitions as used
3030 3031 3032 3033

  SYNOPSIS
    find_used_partitions()
      ppar      Partition pruning context.
3034
      key_tree  SEL_ARG range tree to perform pruning for
3035 3036 3037

  DESCRIPTION
    This function 
3038 3039
      * recursively walks the SEL_ARG* tree collecting partitioning "intervals"
      * finds the partitions one needs to use to get rows in these intervals
3040
      * marks these partitions as used.
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057
    The next session desribes the process in greater detail.
 
  IMPLEMENTATION
    TYPES OF RESTRICTIONS THAT WE CAN OBTAIN PARTITIONS FOR    
    We can find out which [sub]partitions to use if we obtain restrictions on 
    [sub]partitioning fields in the following form:
    1.  "partition_field1=const1 AND ... AND partition_fieldN=constN"
    1.1  Same as (1) but for subpartition fields

    If partitioning supports interval analysis (i.e. partitioning is a
    function of a single table field, and partition_info::
    get_part_iter_for_interval != NULL), then we can also use condition in
    this form:
    2.  "const1 <=? partition_field <=? const2"
    2.1  Same as (2) but for subpartition_field

    INFERRING THE RESTRICTIONS FROM SEL_ARG TREE
3058
    
3059
    The below is an example of what SEL_ARG tree may represent:
3060
    
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
    (start)
     |                           $
     |   Partitioning keyparts   $  subpartitioning keyparts
     |                           $
     |     ...          ...      $
     |      |            |       $
     | +---------+  +---------+  $  +-----------+  +-----------+
     \-| par1=c1 |--| par2=c2 |-----| subpar1=c3|--| subpar2=c5|
       +---------+  +---------+  $  +-----------+  +-----------+
            |                    $        |             |
            |                    $        |        +-----------+ 
            |                    $        |        | subpar2=c6|
            |                    $        |        +-----------+ 
            |                    $        |
            |                    $  +-----------+  +-----------+
            |                    $  | subpar1=c4|--| subpar2=c8|
            |                    $  +-----------+  +-----------+
            |                    $         
            |                    $
       +---------+               $  +------------+  +------------+
       | par1=c2 |------------------| subpar1=c10|--| subpar2=c12|
       +---------+               $  +------------+  +------------+
            |                    $
           ...                   $

    The up-down connections are connections via SEL_ARG::left and
    SEL_ARG::right. A horizontal connection to the right is the
    SEL_ARG::next_key_part connection.
3089
    
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
    find_used_partitions() traverses the entire tree via recursion on
     * SEL_ARG::next_key_part (from left to right on the picture)
     * SEL_ARG::left|right (up/down on the pic). Left-right recursion is
       performed for each depth level.
    
    Recursion descent on SEL_ARG::next_key_part is used to accumulate (in
    ppar->arg_stack) constraints on partitioning and subpartitioning fields.
    For the example in the above picture, one of stack states is:
      in find_used_partitions(key_tree = "subpar2=c5") (***)
      in find_used_partitions(key_tree = "subpar1=c3")
      in find_used_partitions(key_tree = "par2=c2")   (**)
      in find_used_partitions(key_tree = "par1=c1")
      in prune_partitions(...)
    We apply partitioning limits as soon as possible, e.g. when we reach the
    depth (**), we find which partition(s) correspond to "par1=c1 AND par2=c2",
    and save them in ppar->part_iter.
    When we reach the depth (***), we find which subpartition(s) correspond to
    "subpar1=c3 AND subpar2=c5", and then mark appropriate subpartitions in
    appropriate subpartitions as used.
    
    It is possible that constraints on some partitioning fields are missing.
    For the above example, consider this stack state:
      in find_used_partitions(key_tree = "subpar2=c12") (***)
      in find_used_partitions(key_tree = "subpar1=c10")
      in find_used_partitions(key_tree = "par1=c2")
      in prune_partitions(...)
    Here we don't have constraints for all partitioning fields. Since we've
    never set the ppar->part_iter to contain used set of partitions, we use
    its default "all partitions" value.  We get  subpartition id for 
    "subpar1=c3 AND subpar2=c5", and mark that subpartition as used in every
    partition.

    The inverse is also possible: we may get constraints on partitioning
    fields, but not constraints on subpartitioning fields. In that case,
    calls to find_used_partitions() with depth below (**) will return -1,
    and we will mark entire partition as used.
3126

3127 3128
  TODO
    Replace recursion on SEL_ARG::left and SEL_ARG::right with a loop
3129 3130 3131 3132 3133

  RETURN
    1   OK, one or more [sub]partitions are marked as used.
    0   The passed condition doesn't match any partitions
   -1   Couldn't infer any partition pruning "intervals" from the passed 
3134 3135
        SEL_ARG* tree (which means that all partitions should be marked as
        used) Marking partitions as used is the responsibility of the caller.
3136 3137 3138 3139 3140 3141
*/

static 
int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree)
{
  int res, left_res=0, right_res=0;
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3142
  int key_tree_part= (int)key_tree->part;
3143
  bool set_full_part_if_bad_ret= FALSE;
3144 3145
  bool ignore_part_fields= ppar->ignore_part_fields;
  bool did_set_ignore_part_fields= FALSE;
3146
  RANGE_OPT_PARAM *range_par= &(ppar->range_param);
3147

3148
  if (check_stack_overrun(range_par->thd, 3*STACK_MIN_SIZE, NULL))
3149
    return -1;
3150 3151 3152 3153 3154 3155 3156

  if (key_tree->left != &null_element)
  {
    if (-1 == (left_res= find_used_partitions(ppar,key_tree->left)))
      return -1;
  }

3157
  /* Push SEL_ARG's to stack to enable looking backwards as well */
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3158 3159
  ppar->cur_part_fields+= ppar->is_part_keypart[key_tree_part];
  ppar->cur_subpart_fields+= ppar->is_subpart_keypart[key_tree_part];
3160 3161
  *(ppar->arg_stack_end++)= key_tree;

3162 3163
  if (key_tree->type == SEL_ARG::KEY_RANGE)
  {
3164 3165
    if (ppar->part_info->get_part_iter_for_interval && 
        key_tree->part <= ppar->last_part_partno)
3166
    {
3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
      if (ignore_part_fields)
      {
        /*
          We come here when a condition on the first partitioning
          fields led to evaluating the partitioning condition
          (due to finding a condition of the type a < const or
          b > const). Thus we must ignore the rest of the
          partitioning fields but we still want to analyse the
          subpartitioning fields.
        */
        if (key_tree->next_key_part)
          res= find_used_partitions(ppar, key_tree->next_key_part);
        else
          res= -1;
        goto pop_and_go_right;
      }
      /* Collect left and right bound, their lengths and flags */
      uchar *min_key= ppar->cur_min_key;
      uchar *max_key= ppar->cur_max_key;
      uchar *tmp_min_key= min_key;
      uchar *tmp_max_key= max_key;
      key_tree->store_min(ppar->key[key_tree->part].store_length,
                          &tmp_min_key, ppar->cur_min_flag);
      key_tree->store_max(ppar->key[key_tree->part].store_length,
                          &tmp_max_key, ppar->cur_max_flag);
      uint flag;
      if (key_tree->next_key_part &&
          key_tree->next_key_part->part == key_tree->part+1 &&
          key_tree->next_key_part->part <= ppar->last_part_partno &&
          key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
      {
        /*
          There are more key parts for partition pruning to handle
          This mainly happens when the condition is an equality
          condition.
        */
        if ((tmp_min_key - min_key) == (tmp_max_key - max_key) && 
            (memcmp(min_key, max_key, (uint)(tmp_max_key - max_key)) == 0) &&
            !key_tree->min_flag && !key_tree->max_flag)
        {
          /* Set 'parameters' */
          ppar->cur_min_key= tmp_min_key;
          ppar->cur_max_key= tmp_max_key;
          uint save_min_flag= ppar->cur_min_flag;
          uint save_max_flag= ppar->cur_max_flag;

          ppar->cur_min_flag|= key_tree->min_flag;
          ppar->cur_max_flag|= key_tree->max_flag;
          
          res= find_used_partitions(ppar, key_tree->next_key_part);
           
          /* Restore 'parameters' back */
          ppar->cur_min_key= min_key;
          ppar->cur_max_key= max_key;

          ppar->cur_min_flag= save_min_flag;
          ppar->cur_max_flag= save_max_flag;
          goto pop_and_go_right;
        }
        /* We have arrived at the last field in the partition pruning */
        uint tmp_min_flag= key_tree->min_flag,
             tmp_max_flag= key_tree->max_flag;
        if (!tmp_min_flag)
          key_tree->next_key_part->store_min_key(ppar->key,
                                                 &tmp_min_key,
                                                 &tmp_min_flag,
                                                 ppar->last_part_partno);
        if (!tmp_max_flag)
          key_tree->next_key_part->store_max_key(ppar->key,
                                                 &tmp_max_key,
                                                 &tmp_max_flag,
                                                 ppar->last_part_partno);
        flag= tmp_min_flag | tmp_max_flag;
      }
      else
        flag= key_tree->min_flag | key_tree->max_flag;
      
3244
      if (tmp_min_key != range_par->min_key)
3245 3246 3247
        flag&= ~NO_MIN_RANGE;
      else
        flag|= NO_MIN_RANGE;
3248
      if (tmp_max_key != range_par->max_key)
3249 3250 3251 3252 3253 3254
        flag&= ~NO_MAX_RANGE;
      else
        flag|= NO_MAX_RANGE;

      /*
        We need to call the interval mapper if we have a condition which
3255
        makes sense to prune on. In the example of COLUMNS on a and
3256 3257 3258 3259 3260
        b it makes sense if we have a condition on a, or conditions on
        both a and b. If we only have conditions on b it might make sense
        but this is a harder case we will solve later. For the harder case
        this clause then turns into use of all partitions and thus we
        simply set res= -1 as if the mapper had returned that.
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3261
        TODO: What to do here is defined in WL#4065.
3262
      */
3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
      if (ppar->arg_stack[0]->part == 0)
      {
        uint32 i;
        uint32 store_length_array[MAX_KEY];
        uint32 num_keys= ppar->part_fields;

        for (i= 0; i < num_keys; i++)
          store_length_array[i]= ppar->key[i].store_length;
        res= ppar->part_info->
             get_part_iter_for_interval(ppar->part_info,
                                        FALSE,
                                        store_length_array,
3275 3276 3277 3278
                                        range_par->min_key,
                                        range_par->max_key,
                                        tmp_min_key - range_par->min_key,
                                        tmp_max_key - range_par->max_key,
3279 3280 3281 3282 3283 3284 3285 3286
                                        flag,
                                        &ppar->part_iter);
        if (!res)
          goto pop_and_go_right; /* res==0 --> no satisfying partitions */
      }
      else
        res= -1;

3287
      if (res == -1)
3288
      {
3289
        /* get a full range iterator */
3290
        init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3291 3292
      }
      /* 
3293
        Save our intent to mark full partition as used if we will not be able
3294 3295
        to obtain further limits on subpartitions
      */
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3296
      if (key_tree_part < ppar->last_part_partno)
3297 3298 3299 3300 3301 3302 3303 3304
      {
        /*
          We need to ignore the rest of the partitioning fields in all
          evaluations after this
        */
        did_set_ignore_part_fields= TRUE;
        ppar->ignore_part_fields= TRUE;
      }
3305 3306 3307 3308
      set_full_part_if_bad_ret= TRUE;
      goto process_next_key_part;
    }

Mikael Ronstrom's avatar
Mikael Ronstrom committed
3309
    if (key_tree_part == ppar->last_subpart_partno && 
3310 3311 3312 3313
        (NULL != ppar->part_info->get_subpart_iter_for_interval))
    {
      PARTITION_ITERATOR subpart_iter;
      DBUG_EXECUTE("info", dbug_print_segment_range(key_tree,
3314
                                                    range_par->key_parts););
3315 3316 3317
      res= ppar->part_info->
           get_subpart_iter_for_interval(ppar->part_info,
                                         TRUE,
3318
                                         NULL, /* Currently not used here */
3319 3320
                                         key_tree->min_value, 
                                         key_tree->max_value,
3321 3322 3323
                                         0, 0, /* Those are ignored here */
                                         key_tree->min_flag |
                                           key_tree->max_flag,
3324 3325 3326
                                         &subpart_iter);
      DBUG_ASSERT(res); /* We can't get "no satisfying subpartitions" */
      if (res == -1)
3327
        goto pop_and_go_right; /* all subpartitions satisfy */
3328 3329 3330
        
      uint32 subpart_id;
      bitmap_clear_all(&ppar->subparts_bitmap);
3331 3332
      while ((subpart_id= subpart_iter.get_next(&subpart_iter)) !=
             NOT_A_PARTITION_ID)
3333 3334 3335 3336 3337 3338 3339
        bitmap_set_bit(&ppar->subparts_bitmap, subpart_id);

      /* Mark each partition as used in each subpartition.  */
      uint32 part_id;
      while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
              NOT_A_PARTITION_ID)
      {
3340
        for (uint i= 0; i < ppar->part_info->num_subparts; i++)
3341 3342
          if (bitmap_is_set(&ppar->subparts_bitmap, i))
            bitmap_set_bit(&ppar->part_info->used_partitions,
3343
                           part_id * ppar->part_info->num_subparts + i);
3344
      }
3345
      goto pop_and_go_right;
3346 3347
    }

3348 3349
    if (key_tree->is_singlepoint())
    {
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3350
      if (key_tree_part == ppar->last_part_partno &&
3351 3352
          ppar->cur_part_fields == ppar->part_fields &&
          ppar->part_info->get_part_iter_for_interval == NULL)
3353 3354 3355 3356 3357 3358
      {
        /* 
          Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all partitioning
          fields. Save all constN constants into table record buffer.
        */
        store_selargs_to_rec(ppar, ppar->arg_stack, ppar->part_fields);
3359
        DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack,
3360 3361
                                                       ppar->part_fields););
        uint32 part_id;
3362
        longlong func_value;
3363
        /* Find in which partition the {const1, ...,constN} tuple goes */
3364 3365
        if (ppar->get_top_partition_id_func(ppar->part_info, &part_id,
                                            &func_value))
3366 3367 3368 3369 3370
        {
          res= 0; /* No satisfying partitions */
          goto pop_and_go_right;
        }
        /* Rembember the limit we got - single partition #part_id */
3371
        init_single_partition_iterator(part_id, &ppar->part_iter);
3372 3373 3374 3375 3376 3377 3378 3379 3380
        
        /*
          If there are no subpartitions/we fail to get any limit for them, 
          then we'll mark full partition as used. 
        */
        set_full_part_if_bad_ret= TRUE;
        goto process_next_key_part;
      }

Mikael Ronstrom's avatar
Mikael Ronstrom committed
3381
      if (key_tree_part == ppar->last_subpart_partno &&
3382
          ppar->cur_subpart_fields == ppar->subpart_fields)
3383 3384 3385 3386 3387 3388 3389
      {
        /* 
          Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all subpartitioning
          fields. Save all constN constants into table record buffer.
        */
        store_selargs_to_rec(ppar, ppar->arg_stack_end - ppar->subpart_fields,
                             ppar->subpart_fields);
3390
        DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack_end- 
3391 3392 3393 3394
                                                       ppar->subpart_fields,
                                                       ppar->subpart_fields););
        /* Find the subpartition (it's HASH/KEY so we always have one) */
        partition_info *part_info= ppar->part_info;
3395 3396 3397 3398 3399
        uint32 part_id, subpart_id;
                 
        if (part_info->get_subpartition_id(part_info, &subpart_id))
          return 0;

3400
        /* Mark this partition as used in each subpartition. */
3401 3402
        while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
                NOT_A_PARTITION_ID)
3403 3404
        {
          bitmap_set_bit(&part_info->used_partitions,
3405
                         part_id * part_info->num_subparts + subpart_id);
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417
        }
        res= 1; /* Some partitions were marked as used */
        goto pop_and_go_right;
      }
    }
    else
    {
      /* 
        Can't handle condition on current key part. If we're that deep that 
        we're processing subpartititoning's key parts, this means we'll not be
        able to infer any suitable condition, so bail out.
      */
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3418
      if (key_tree_part >= ppar->last_part_partno)
3419 3420 3421 3422
      {
        res= -1;
        goto pop_and_go_right;
      }
3423 3424 3425 3426 3427 3428
    }
  }

process_next_key_part:
  if (key_tree->next_key_part)
    res= find_used_partitions(ppar, key_tree->next_key_part);
3429
  else
3430
    res= -1;
3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441

  if (did_set_ignore_part_fields)
  {
    /*
      We have returned from processing all key trees linked to our next
      key part. We are ready to be moving down (using right pointers) and
      this tree is a new evaluation requiring its own decision on whether
      to ignore partitioning fields.
    */
    ppar->ignore_part_fields= FALSE;
  }
3442
  if (set_full_part_if_bad_ret)
3443
  {
3444
    if (res == -1)
3445
    {
3446 3447 3448
      /* Got "full range" for subpartitioning fields */
      uint32 part_id;
      bool found= FALSE;
3449 3450
      while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
             NOT_A_PARTITION_ID)
3451
      {
3452 3453
        ppar->mark_full_partition_used(ppar->part_info, part_id);
        found= TRUE;
3454
      }
3455
      res= test(found);
3456
    }
3457 3458 3459 3460
    /*
      Restore the "used partitions iterator" to the default setting that
      specifies iteration over all partitions.
    */
3461
    init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3462 3463 3464
  }

pop_and_go_right:
3465 3466
  /* Pop this key part info off the "stack" */
  ppar->arg_stack_end--;
Mikael Ronstrom's avatar
Mikael Ronstrom committed
3467 3468
  ppar->cur_part_fields-=    ppar->is_part_keypart[key_tree_part];
  ppar->cur_subpart_fields-= ppar->is_subpart_keypart[key_tree_part];
3469 3470 3471

  if (res == -1)
    return -1;
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
  if (key_tree->right != &null_element)
  {
    if (-1 == (right_res= find_used_partitions(ppar,key_tree->right)))
      return -1;
  }
  return (left_res || right_res || res);
}
 

static void mark_all_partitions_as_used(partition_info *part_info)
{
  bitmap_set_all(&part_info->used_partitions);
}


/*
  Check if field types allow to construct partitioning index description
 
  SYNOPSIS
    fields_ok_for_partition_index()
      pfield  NULL-terminated array of pointers to fields.

  DESCRIPTION
    For an array of fields, check if we can use all of the fields to create
    partitioning index description.
    
    We can't process GEOMETRY fields - for these fields singlepoint intervals
    cant be generated, and non-singlepoint are "special" kinds of intervals
    to which our processing logic can't be applied.

    It is not known if we could process ENUM fields, so they are disabled to be
    on the safe side.

  RETURN 
    TRUE   Yes, fields can be used in partitioning index
    FALSE  Otherwise
*/

static bool fields_ok_for_partition_index(Field **pfield)
{
  if (!pfield)
    return FALSE;
  for (; (*pfield); pfield++)
  {
    enum_field_types ftype= (*pfield)->real_type();
3517
    if (ftype == MYSQL_TYPE_ENUM || ftype == MYSQL_TYPE_GEOMETRY)
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528
      return FALSE;
  }
  return TRUE;
}


/*
  Create partition index description and fill related info in the context
  struct

  SYNOPSIS
3529
    create_partition_index_description()
3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545
      prune_par  INOUT Partition pruning context

  DESCRIPTION
    Create partition index description. Partition index description is:

      part_index(used_fields_list(part_expr), used_fields_list(subpart_expr))

    If partitioning/sub-partitioning uses BLOB or Geometry fields, then
    corresponding fields_list(...) is not included into index description
    and we don't perform partition pruning for partitions/subpartitions.

  RETURN
    TRUE   Out of memory or can't do partition pruning at all
    FALSE  OK
*/

3546
static bool create_partition_index_description(PART_PRUNE_PARAM *ppar)
3547 3548 3549 3550 3551 3552
{
  RANGE_OPT_PARAM *range_par= &(ppar->range_param);
  partition_info *part_info= ppar->part_info;
  uint used_part_fields, used_subpart_fields;

  used_part_fields= fields_ok_for_partition_index(part_info->part_field_array) ?
3553
                      part_info->num_part_fields : 0;
3554 3555
  used_subpart_fields= 
    fields_ok_for_partition_index(part_info->subpart_field_array)? 
3556
      part_info->num_subpart_fields : 0;
3557 3558 3559
  
  uint total_parts= used_part_fields + used_subpart_fields;

3560
  ppar->ignore_part_fields= FALSE;
3561 3562 3563 3564 3565 3566 3567
  ppar->part_fields=      used_part_fields;
  ppar->last_part_partno= (int)used_part_fields - 1;

  ppar->subpart_fields= used_subpart_fields;
  ppar->last_subpart_partno= 
    used_subpart_fields?(int)(used_part_fields + used_subpart_fields - 1): -1;

3568
  if (part_info->is_sub_partitioned())
3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590
  {
    ppar->mark_full_partition_used=  mark_full_partition_used_with_parts;
    ppar->get_top_partition_id_func= part_info->get_part_partition_id;
  }
  else
  {
    ppar->mark_full_partition_used=  mark_full_partition_used_no_parts;
    ppar->get_top_partition_id_func= part_info->get_partition_id;
  }

  KEY_PART *key_part;
  MEM_ROOT *alloc= range_par->mem_root;
  if (!total_parts || 
      !(key_part= (KEY_PART*)alloc_root(alloc, sizeof(KEY_PART)*
                                               total_parts)) ||
      !(ppar->arg_stack= (SEL_ARG**)alloc_root(alloc, sizeof(SEL_ARG*)* 
                                                      total_parts)) ||
      !(ppar->is_part_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
                                                           total_parts)) ||
      !(ppar->is_subpart_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
                                                           total_parts)))
    return TRUE;
3591 3592 3593
 
  if (ppar->subpart_fields)
  {
3594
    my_bitmap_map *buf;
3595
    uint32 bufsize= bitmap_buffer_size(ppar->part_info->num_subparts);
3596
    if (!(buf= (my_bitmap_map*) alloc_root(alloc, bufsize)))
3597
      return TRUE;
3598
    bitmap_init(&ppar->subparts_bitmap, buf, ppar->part_info->num_subparts,
3599
                FALSE);
3600
  }
3601 3602 3603
  range_par->key_parts= key_part;
  Field **field= (ppar->part_fields)? part_info->part_field_array :
                                           part_info->subpart_field_array;
3604
  bool in_subpart_fields= FALSE;
3605 3606 3607 3608
  for (uint part= 0; part < total_parts; part++, key_part++)
  {
    key_part->key=          0;
    key_part->part=	    part;
3609 3610
    key_part->length= (uint16)(*field)->key_length();
    key_part->store_length= (uint16)get_partition_field_store_length(*field);
3611

3612 3613 3614
    DBUG_PRINT("info", ("part %u length %u store_length %u", part,
                         key_part->length, key_part->store_length));

3615 3616
    key_part->field=        (*field);
    key_part->image_type =  Field::itRAW;
3617 3618 3619 3620 3621
    /* 
      We set keypart flag to 0 here as the only HA_PART_KEY_SEG is checked
      in the RangeAnalysisModule.
    */
    key_part->flag=         0;
3622 3623
    /* We don't set key_parts->null_bit as it will not be used */

3624 3625
    ppar->is_part_keypart[part]= !in_subpart_fields;
    ppar->is_subpart_keypart[part]= in_subpart_fields;
3626 3627 3628 3629 3630 3631

    /*
      Check if this was last field in this array, in this case we
      switch to subpartitioning fields. (This will only happens if
      there are subpartitioning fields to cater for).
    */
3632 3633 3634
    if (!*(++field))
    {
      field= part_info->subpart_field_array;
3635
      in_subpart_fields= TRUE;
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
    }
  }
  range_par->key_parts_end= key_part;

  DBUG_EXECUTE("info", print_partitioning_index(range_par->key_parts,
                                                range_par->key_parts_end););
  return FALSE;
}


#ifndef DBUG_OFF

static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end)
{
  DBUG_ENTER("print_partitioning_index");
  DBUG_LOCK_FILE;
  fprintf(DBUG_FILE, "partitioning INDEX(");
  for (KEY_PART *p=parts; p != parts_end; p++)
  {
    fprintf(DBUG_FILE, "%s%s", p==parts?"":" ,", p->field->field_name);
  }
3657
  fputs(");\n", DBUG_FILE);
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}

/* Print field value into debug trace, in NULL-aware way. */
static void dbug_print_field(Field *field)
{
  if (field->is_real_null())
    fprintf(DBUG_FILE, "NULL");
  else
  {
3669 3670 3671
    char buf[256];
    String str(buf, sizeof(buf), &my_charset_bin);
    str.length(0);
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685
    String *pstr;
    pstr= field->val_str(&str);
    fprintf(DBUG_FILE, "'%s'", pstr->c_ptr_safe());
  }
}


/* Print a "c1 < keypartX < c2" - type interval into debug trace. */
static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part)
{
  DBUG_ENTER("dbug_print_segment_range");
  DBUG_LOCK_FILE;
  if (!(arg->min_flag & NO_MIN_RANGE))
  {
3686
    store_key_image_to_rec(part->field, arg->min_value, part->length);
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701
    dbug_print_field(part->field);
    if (arg->min_flag & NEAR_MIN)
      fputs(" < ", DBUG_FILE);
    else
      fputs(" <= ", DBUG_FILE);
  }

  fprintf(DBUG_FILE, "%s", part->field->field_name);

  if (!(arg->max_flag & NO_MAX_RANGE))
  {
    if (arg->max_flag & NEAR_MAX)
      fputs(" < ", DBUG_FILE);
    else
      fputs(" <= ", DBUG_FILE);
3702
    store_key_image_to_rec(part->field, arg->max_value, part->length);
3703 3704
    dbug_print_field(part->field);
  }
3705
  fputs("\n", DBUG_FILE);
3706 3707 3708 3709 3710 3711 3712 3713 3714
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}


/*
  Print a singlepoint multi-keypart range interval to debug trace
 
  SYNOPSIS
3715
    dbug_print_singlepoint_range()
3716 3717 3718 3719 3720 3721 3722 3723
      start  Array of SEL_ARG* ptrs representing conditions on key parts
      num    Number of elements in the array.

  DESCRIPTION
    This function prints a "keypartN=constN AND ... AND keypartK=constK"-type 
    interval to debug trace.
*/

3724
static void dbug_print_singlepoint_range(SEL_ARG **start, uint num)
3725
{
3726
  DBUG_ENTER("dbug_print_singlepoint_range");
3727 3728
  DBUG_LOCK_FILE;
  SEL_ARG **end= start + num;
3729

3730 3731 3732 3733 3734 3735
  for (SEL_ARG **arg= start; arg != end; arg++)
  {
    Field *field= (*arg)->field;
    fprintf(DBUG_FILE, "%s%s=", (arg==start)?"":", ", field->field_name);
    dbug_print_field(field);
  }
3736
  fputs("\n", DBUG_FILE);
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746
  DBUG_UNLOCK_FILE;
  DBUG_VOID_RETURN;
}
#endif

/****************************************************************************
 * Partition pruning code ends
 ****************************************************************************/
#endif

3747

3748
/*
3749 3750 3751 3752
  Get cost of 'sweep' full records retrieval.
  SYNOPSIS
    get_sweep_read_cost()
      param            Parameter from test_quick_select
3753
      records          # of records to be retrieved
3754
  RETURN
3755
    cost of sweep
3756
*/
3757

3758
double get_sweep_read_cost(const PARAM *param, ha_rows records)
3759
{
3760
  double result;
3761
  DBUG_ENTER("get_sweep_read_cost");
3762 3763
  if (param->table->file->primary_key_is_clustered())
  {
3764
    result= param->table->file->read_time(param->table->s->primary_key,
3765
                                          (uint)records, records);
3766 3767
  }
  else
3768
  {
3769
    double n_blocks=
3770 3771
      ceil(ulonglong2double(param->table->file->stats.data_file_length) /
           IO_SIZE);
3772 3773 3774 3775
    double busy_blocks=
      n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(records)));
    if (busy_blocks < 1.0)
      busy_blocks= 1.0;
3776
    DBUG_PRINT("info",("sweep: nblocks: %g, busy_blocks: %g", n_blocks,
3777
                       busy_blocks));
3778
    /*
3779
      Disabled: Bail out if # of blocks to read is bigger than # of blocks in
3780 3781 3782 3783 3784 3785 3786 3787
      table data file.
    if (max_cost != DBL_MAX  && (busy_blocks+index_reads_cost) >= n_blocks)
      return 1;
    */
    JOIN *join= param->thd->lex->select_lex.join;
    if (!join || join->tables == 1)
    {
      /* No join, assume reading is done in one 'sweep' */
3788
      result= busy_blocks*(DISK_SEEK_BASE_COST +
3789 3790 3791 3792
                          DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
    }
    else
    {
3793
      /*
3794 3795 3796
        Possibly this is a join with source table being non-last table, so
        assume that disk seeks are random here.
      */
3797
      result= busy_blocks;
3798 3799
    }
  }
3800
  DBUG_PRINT("return",("cost: %g", result));
3801
  DBUG_RETURN(result);
3802
}
3803 3804


3805 3806 3807 3808
/*
  Get best plan for a SEL_IMERGE disjunctive expression.
  SYNOPSIS
    get_best_disjunct_quick()
3809 3810
      param     Parameter from check_quick_select function
      imerge    Expression to use
3811
      read_time Don't create scans with cost > read_time
3812

3813
  NOTES
3814
    index_merge cost is calculated as follows:
3815
    index_merge_cost =
3816 3817 3818 3819 3820
      cost(index_reads) +         (see #1)
      cost(rowid_to_row_scan) +   (see #2)
      cost(unique_use)            (see #3)

    1. cost(index_reads) =SUM_i(cost(index_read_i))
3821 3822
       For non-CPK scans,
         cost(index_read_i) = {cost of ordinary 'index only' scan}
3823 3824 3825 3826 3827
       For CPK scan,
         cost(index_read_i) = {cost of non-'index only' scan}

    2. cost(rowid_to_row_scan)
      If table PK is clustered then
3828
        cost(rowid_to_row_scan) =
3829
          {cost of ordinary clustered PK scan with n_ranges=n_rows}
3830 3831

      Otherwise, we use the following model to calculate costs:
3832
      We need to retrieve n_rows rows from file that occupies n_blocks blocks.
3833
      We assume that offsets of rows we need are independent variates with
3834
      uniform distribution in [0..max_file_offset] range.
3835

3836 3837
      We'll denote block as "busy" if it contains row(s) we need to retrieve
      and "empty" if doesn't contain rows we need.
3838

3839
      Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
3840
      applies to any block in file). Let x_i be a variate taking value 1 if
3841
      block #i is empty and 0 otherwise.
3842

3843 3844
      Then E(x_i) = (1 - 1/n_blocks)^n_rows;

3845 3846
      E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
        = n_blocks * ((1 - 1/n_blocks)^n_rows) =
3847 3848 3849 3850
       ~= n_blocks * exp(-n_rows/n_blocks).

      E(n_busy_blocks) = n_blocks*(1 - (1 - 1/n_blocks)^n_rows) =
       ~= n_blocks * (1 - exp(-n_rows/n_blocks)).
3851

3852 3853
      Average size of "hole" between neighbor non-empty blocks is
           E(hole_size) = n_blocks/E(n_busy_blocks).
3854

3855 3856 3857 3858 3859 3860
      The total cost of reading all needed blocks in one "sweep" is:

      E(n_busy_blocks)*
       (DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*n_blocks/E(n_busy_blocks)).

    3. Cost of Unique use is calculated in Unique::get_use_cost function.
3861 3862 3863 3864 3865

  ROR-union cost is calculated in the same way index_merge, but instead of
  Unique a priority queue is used.

  RETURN
3866 3867
    Created read plan
    NULL - Out of memory or no read scan could be built.
3868
*/
3869

3870 3871
static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
3872
                                         double read_time)
3873 3874 3875 3876 3877 3878 3879
{
  SEL_TREE **ptree;
  TRP_INDEX_MERGE *imerge_trp= NULL;
  uint n_child_scans= imerge->trees_next - imerge->trees;
  TRP_RANGE **range_scans;
  TRP_RANGE **cur_child;
  TRP_RANGE **cpk_scan= NULL;
monty@mysql.com's avatar
monty@mysql.com committed
3880
  bool imerge_too_expensive= FALSE;
3881 3882 3883 3884
  double imerge_cost= 0.0;
  ha_rows cpk_scan_records= 0;
  ha_rows non_cpk_scan_records= 0;
  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
monty@mysql.com's avatar
monty@mysql.com committed
3885 3886
  bool all_scans_ror_able= TRUE;
  bool all_scans_rors= TRUE;
3887 3888 3889 3890 3891 3892 3893
  uint unique_calc_buff_size;
  TABLE_READ_PLAN **roru_read_plans;
  TABLE_READ_PLAN **cur_roru_plan;
  double roru_index_costs;
  ha_rows roru_total_records;
  double roru_intersect_part= 1.0;
  DBUG_ENTER("get_best_disjunct_quick");
3894
  DBUG_PRINT("info", ("Full table scan cost: %g", read_time));
3895

3896
  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
3897 3898 3899
                                             sizeof(TRP_RANGE*)*
                                             n_child_scans)))
    DBUG_RETURN(NULL);
3900
  /*
3901 3902 3903
    Collect best 'range' scan for each of disjuncts, and, while doing so,
    analyze possibility of ROR scans. Also calculate some values needed by
    other parts of the code.
3904
  */
3905
  for (ptree= imerge->trees, cur_child= range_scans;
3906
       ptree != imerge->trees_next;
3907
       ptree++, cur_child++)
3908
  {
3909 3910
    DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
                                        "tree in SEL_IMERGE"););
3911
    if (!(*cur_child= get_key_scans_params(param, *ptree, TRUE, FALSE, read_time)))
3912 3913
    {
      /*
3914
        One of index scans in this index_merge is more expensive than entire
3915 3916 3917
        table read for another available option. The entire index_merge (and
        any possible ROR-union) will be more expensive then, too. We continue
        here only to update SQL_SELECT members.
3918
      */
monty@mysql.com's avatar
monty@mysql.com committed
3919
      imerge_too_expensive= TRUE;
3920 3921 3922
    }
    if (imerge_too_expensive)
      continue;
3923

3924 3925 3926
    imerge_cost += (*cur_child)->read_cost;
    all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
    all_scans_rors &= (*cur_child)->is_ror;
3927
    if (pk_is_clustered &&
3928 3929
        param->real_keynr[(*cur_child)->key_idx] ==
        param->table->s->primary_key)
3930
    {
3931 3932
      cpk_scan= cur_child;
      cpk_scan_records= (*cur_child)->records;
3933 3934
    }
    else
3935
      non_cpk_scan_records += (*cur_child)->records;
3936
  }
3937

3938
  DBUG_PRINT("info", ("index_merge scans cost %g", imerge_cost));
3939
  if (imerge_too_expensive || (imerge_cost > read_time) ||
3940
      ((non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) &&
3941
      read_time != DBL_MAX))
3942
  {
3943 3944
    /*
      Bail out if it is obvious that both index_merge and ROR-union will be
3945
      more expensive
3946
    */
3947 3948
    DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
                        "full table scan, bailing out"));
3949
    DBUG_RETURN(NULL);
3950
  }
3951 3952 3953 3954 3955 3956 3957

  /* 
    If all scans happen to be ROR, proceed to generate a ROR-union plan (it's 
    guaranteed to be cheaper than non-ROR union), unless ROR-unions are
    disabled in @@optimizer_switch
  */
  if (all_scans_rors && 
3958
      optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
3959
  {
3960 3961
    roru_read_plans= (TABLE_READ_PLAN**)range_scans;
    goto skip_to_ror_scan;
3962
  }
3963

3964 3965
  if (cpk_scan)
  {
3966 3967
    /*
      Add one ROWID comparison for each row retrieved on non-CPK scan.  (it
3968 3969 3970
      is done in QUICK_RANGE_SELECT::row_in_ranges)
     */
    imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
3971 3972 3973
  }

  /* Calculate cost(rowid_to_row_scan) */
3974
  imerge_cost += get_sweep_read_cost(param, non_cpk_scan_records);
3975
  DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
3976
                     imerge_cost));
3977
  if (imerge_cost > read_time || 
3978
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION))
3979
  {
3980
    goto build_ror_index_merge;
3981
  }
3982 3983

  /* Add Unique operations cost */
3984
  unique_calc_buff_size=
3985
    Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
3986 3987 3988 3989 3990 3991
                                    param->table->file->ref_length,
                                    param->thd->variables.sortbuff_size);
  if (param->imerge_cost_buff_size < unique_calc_buff_size)
  {
    if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
                                                     unique_calc_buff_size)))
3992
      DBUG_RETURN(NULL);
3993 3994 3995
    param->imerge_cost_buff_size= unique_calc_buff_size;
  }

3996
  imerge_cost +=
3997
    Unique::get_use_cost(param->imerge_cost_buff, (uint)non_cpk_scan_records,
3998 3999
                         param->table->file->ref_length,
                         param->thd->variables.sortbuff_size);
4000
  DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
4001 4002 4003 4004 4005 4006 4007
                     imerge_cost, read_time));
  if (imerge_cost < read_time)
  {
    if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
    {
      imerge_trp->read_cost= imerge_cost;
      imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
4008
      imerge_trp->records= min(imerge_trp->records,
4009
                               param->table->file->stats.records);
4010 4011 4012 4013 4014
      imerge_trp->range_scans= range_scans;
      imerge_trp->range_scans_end= range_scans + n_child_scans;
      read_time= imerge_cost;
    }
  }
4015

4016
build_ror_index_merge:
4017 4018
  if (!all_scans_ror_able || 
      param->thd->lex->sql_command == SQLCOM_DELETE ||
4019
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
4020
    DBUG_RETURN(imerge_trp);
4021

4022 4023
  /* Ok, it is possible to build a ROR-union, try it. */
  bool dummy;
4024
  if (!(roru_read_plans=
4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037
          (TABLE_READ_PLAN**)alloc_root(param->mem_root,
                                        sizeof(TABLE_READ_PLAN*)*
                                        n_child_scans)))
    DBUG_RETURN(imerge_trp);
skip_to_ror_scan:
  roru_index_costs= 0.0;
  roru_total_records= 0;
  cur_roru_plan= roru_read_plans;

  /* Find 'best' ROR scan for each of trees in disjunction */
  for (ptree= imerge->trees, cur_child= range_scans;
       ptree != imerge->trees_next;
       ptree++, cur_child++, cur_roru_plan++)
4038
  {
4039 4040
    /*
      Assume the best ROR scan is the one that has cheapest full-row-retrieval
4041 4042
      scan cost.
      Also accumulate index_only scan costs as we'll need them to calculate
4043 4044 4045 4046 4047 4048 4049
      overall index_intersection cost.
    */
    double cost;
    if ((*cur_child)->is_ror)
    {
      /* Ok, we have index_only cost, now get full rows scan cost */
      cost= param->table->file->
4050
              read_time(param->real_keynr[(*cur_child)->key_idx], 1,
4051 4052 4053 4054 4055 4056 4057
                        (*cur_child)->records) +
              rows2double((*cur_child)->records) / TIME_FOR_COMPARE;
    }
    else
      cost= read_time;

    TABLE_READ_PLAN *prev_plan= *cur_child;
4058
    if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
4059 4060 4061 4062 4063 4064 4065 4066 4067
                                                 &dummy)))
    {
      if (prev_plan->is_ror)
        *cur_roru_plan= prev_plan;
      else
        DBUG_RETURN(imerge_trp);
      roru_index_costs += (*cur_roru_plan)->read_cost;
    }
    else
4068 4069
      roru_index_costs +=
        ((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
4070
    roru_total_records += (*cur_roru_plan)->records;
4071
    roru_intersect_part *= (*cur_roru_plan)->records /
4072
                           param->table->file->stats.records;
4073
  }
4074

4075 4076
  /*
    rows to retrieve=
4077
      SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
4078
    This is valid because index_merge construction guarantees that conditions
4079 4080 4081
    in disjunction do not share key parts.
  */
  roru_total_records -= (ha_rows)(roru_intersect_part*
4082
                                  param->table->file->stats.records);
4083 4084
  /* ok, got a ROR read plan for each of the disjuncts
    Calculate cost:
4085 4086 4087 4088 4089 4090
    cost(index_union_scan(scan_1, ... scan_n)) =
      SUM_i(cost_of_index_only_scan(scan_i)) +
      queue_use_cost(rowid_len, n) +
      cost_of_row_retrieval
    See get_merge_buffers_cost function for queue_use_cost formula derivation.
  */
4091

4092
  double roru_total_cost;
4093 4094 4095
  roru_total_cost= roru_index_costs +
                   rows2double(roru_total_records)*log((double)n_child_scans) /
                   (TIME_FOR_COMPARE_ROWID * M_LN2) +
4096 4097
                   get_sweep_read_cost(param, roru_total_records);

4098
  DBUG_PRINT("info", ("ROR-union: cost %g, %d members", roru_total_cost,
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112
                      n_child_scans));
  TRP_ROR_UNION* roru;
  if (roru_total_cost < read_time)
  {
    if ((roru= new (param->mem_root) TRP_ROR_UNION))
    {
      roru->first_ror= roru_read_plans;
      roru->last_ror= roru_read_plans + n_child_scans;
      roru->read_cost= roru_total_cost;
      roru->records= roru_total_records;
      DBUG_RETURN(roru);
    }
  }
  DBUG_RETURN(imerge_trp);
4113 4114 4115 4116 4117 4118 4119
}


/*
  Calculate cost of 'index only' scan for given index and number of records.

  SYNOPSIS
4120
    get_index_only_read_time()
4121 4122 4123 4124 4125
      param    parameters structure
      records  #of records to read
      keynr    key to read

  NOTES
4126
    It is assumed that we will read trough the whole key range and that all
4127 4128 4129 4130
    key blocks are half full (normally things are much better). It is also
    assumed that each time we read the next key from the index, the handler
    performs a random seek, thus the cost is proportional to the number of
    blocks read.
4131 4132 4133 4134 4135 4136

  TODO:
    Move this to handler->read_time() by adding a flag 'index-only-read' to
    this call. The reason for doing this is that the current function doesn't
    handle the case when the row is stored in the b-tree (like in innodb
    clustered index)
4137 4138
*/

4139
static double get_index_only_read_time(const PARAM* param, ha_rows records,
4140
                                       int keynr)
4141 4142
{
  double read_time;
4143
  uint keys_per_block= (param->table->file->stats.block_size/2/
4144 4145 4146 4147
			(param->table->key_info[keynr].key_length+
			 param->table->file->ref_length) + 1);
  read_time=((double) (records+keys_per_block-1)/
             (double) keys_per_block);
4148
  return read_time;
4149 4150
}

4151

4152 4153
typedef struct st_ror_scan_info
{
4154 4155 4156 4157 4158
  uint      idx;      /* # of used key in param->keys */
  uint      keynr;    /* # of used key in table */
  ha_rows   records;  /* estimate of # records this scan will return */

  /* Set of intervals over key fields that will be used for row retrieval. */
4159
  SEL_ARG   *sel_arg;
4160 4161

  /* Fields used in the query and covered by this ROR scan. */
4162 4163
  MY_BITMAP covered_fields;
  uint      used_fields_covered; /* # of set bits in covered_fields */
4164
  int       key_rec_length; /* length of key record (including rowid) */
4165 4166

  /*
4167 4168
    Cost of reading all index records with values in sel_arg intervals set
    (assuming there is no need to access full table records)
4169 4170
  */
  double    index_read_cost;
4171 4172 4173
  uint      first_uncovered_field; /* first unused bit in covered_fields */
  uint      key_components; /* # of parts in the key */
} ROR_SCAN_INFO;
4174 4175 4176


/*
4177
  Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
4178
  sel_arg set of intervals.
4179

4180 4181
  SYNOPSIS
    make_ror_scan()
4182 4183 4184
      param    Parameter from test_quick_select function
      idx      Index of key in param->keys
      sel_arg  Set of intervals for a given key
4185

4186
  RETURN
4187
    NULL - out of memory
4188
    ROR scan structure containing a scan for {idx, sel_arg}
4189 4190 4191 4192 4193 4194
*/

static
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
{
  ROR_SCAN_INFO *ror_scan;
4195
  my_bitmap_map *bitmap_buf;
4196 4197
  uint keynr;
  DBUG_ENTER("make_ror_scan");
4198

4199 4200 4201 4202 4203 4204
  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
                                             sizeof(ROR_SCAN_INFO))))
    DBUG_RETURN(NULL);

  ror_scan->idx= idx;
  ror_scan->keynr= keynr= param->real_keynr[idx];
4205 4206
  ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
                             param->table->file->ref_length);
4207 4208
  ror_scan->sel_arg= sel_arg;
  ror_scan->records= param->table->quick_rows[keynr];
4209

4210 4211
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
                                                param->fields_bitmap_size)))
4212
    DBUG_RETURN(NULL);
4213

4214
  if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
4215
                  param->table->s->fields, FALSE))
4216 4217
    DBUG_RETURN(NULL);
  bitmap_clear_all(&ror_scan->covered_fields);
4218

4219
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
4220
  KEY_PART_INFO *key_part_end= key_part +
4221 4222 4223
                               param->table->key_info[keynr].key_parts;
  for (;key_part != key_part_end; ++key_part)
  {
4224 4225
    if (bitmap_is_set(&param->needed_fields, key_part->fieldnr-1))
      bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
4226
  }
4227
  ror_scan->index_read_cost=
4228 4229 4230 4231 4232 4233
    get_index_only_read_time(param, param->table->quick_rows[ror_scan->keynr],
                             ror_scan->keynr);
  DBUG_RETURN(ror_scan);
}


4234
/*
4235 4236 4237 4238 4239 4240 4241
  Compare two ROR_SCAN_INFO** by  E(#records_matched) * key_record_length.
  SYNOPSIS
    cmp_ror_scan_info()
      a ptr to first compared value
      b ptr to second compared value

  RETURN
4242
   -1 a < b
4243 4244
    0 a = b
    1 a > b
4245
*/
4246

4247
static int cmp_ror_scan_info(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
4248 4249 4250 4251 4252 4253 4254
{
  double val1= rows2double((*a)->records) * (*a)->key_rec_length;
  double val2= rows2double((*b)->records) * (*b)->key_rec_length;
  return (val1 < val2)? -1: (val1 == val2)? 0 : 1;
}

/*
4255 4256 4257
  Compare two ROR_SCAN_INFO** by
   (#covered fields in F desc,
    #components asc,
4258
    number of first not covered component asc)
4259 4260 4261 4262 4263 4264 4265

  SYNOPSIS
    cmp_ror_scan_info_covering()
      a ptr to first compared value
      b ptr to second compared value

  RETURN
4266
   -1 a < b
4267 4268
    0 a = b
    1 a > b
4269
*/
4270

4271
static int cmp_ror_scan_info_covering(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
{
  if ((*a)->used_fields_covered > (*b)->used_fields_covered)
    return -1;
  if ((*a)->used_fields_covered < (*b)->used_fields_covered)
    return 1;
  if ((*a)->key_components < (*b)->key_components)
    return -1;
  if ((*a)->key_components > (*b)->key_components)
    return 1;
  if ((*a)->first_uncovered_field < (*b)->first_uncovered_field)
    return -1;
  if ((*a)->first_uncovered_field > (*b)->first_uncovered_field)
    return 1;
  return 0;
}

4288

4289
/* Auxiliary structure for incremental ROR-intersection creation */
4290
typedef struct
4291 4292 4293
{
  const PARAM *param;
  MY_BITMAP covered_fields; /* union of fields covered by all scans */
4294
  /*
4295
    Fraction of table records that satisfies conditions of all scans.
4296
    This is the number of full records that will be retrieved if a
4297 4298
    non-index_only index intersection will be employed.
  */
4299 4300 4301 4302
  double out_rows;
  /* TRUE if covered_fields is a superset of needed_fields */
  bool is_covering;

4303
  ha_rows index_records; /* sum(#records to look in indexes) */
4304 4305
  double index_scan_costs; /* SUM(cost of 'index-only' scans) */
  double total_cost;
4306
} ROR_INTERSECT_INFO;
4307 4308


4309 4310 4311 4312
/*
  Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.

  SYNOPSIS
4313 4314 4315
    ror_intersect_init()
      param         Parameter from test_quick_select

4316 4317 4318 4319 4320 4321
  RETURN
    allocated structure
    NULL on error
*/

static
4322
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
4323 4324
{
  ROR_INTERSECT_INFO *info;
4325
  my_bitmap_map* buf;
4326
  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
4327 4328 4329
                                              sizeof(ROR_INTERSECT_INFO))))
    return NULL;
  info->param= param;
4330 4331
  if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
                                         param->fields_bitmap_size)))
4332
    return NULL;
4333
  if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
monty@mysql.com's avatar
monty@mysql.com committed
4334
                  FALSE))
4335
    return NULL;
4336
  info->is_covering= FALSE;
4337
  info->index_scan_costs= 0.0;
4338
  info->index_records= 0;
4339
  info->out_rows= (double) param->table->file->stats.records;
4340
  bitmap_clear_all(&info->covered_fields);
4341 4342 4343
  return info;
}

4344 4345 4346 4347
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
{
  dst->param= src->param;
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap, 
4348
         no_bytes_in_map(&src->covered_fields));
4349 4350 4351 4352 4353 4354
  dst->out_rows= src->out_rows;
  dst->is_covering= src->is_covering;
  dst->index_records= src->index_records;
  dst->index_scan_costs= src->index_scan_costs;
  dst->total_cost= src->total_cost;
}
4355 4356


4357
/*
4358
  Get selectivity of a ROR scan wrt ROR-intersection.
4359

4360
  SYNOPSIS
4361 4362 4363 4364
    ror_scan_selectivity()
      info  ROR-interection 
      scan  ROR scan
      
4365
  NOTES
4366
    Suppose we have a condition on several keys
4367 4368
    cond=k_11=c_11 AND k_12=c_12 AND ...  // parts of first key
         k_21=c_21 AND k_22=c_22 AND ...  // parts of second key
4369
          ...
4370
         k_n1=c_n1 AND k_n3=c_n3 AND ...  (1) //parts of the key used by *scan
4371

4372 4373
    where k_ij may be the same as any k_pq (i.e. keys may have common parts).

4374
    A full row is retrieved if entire condition holds.
4375 4376

    The recursive procedure for finding P(cond) is as follows:
4377

4378
    First step:
4379
    Pick 1st part of 1st key and break conjunction (1) into two parts:
4380 4381
      cond= (k_11=c_11 AND R)

4382
    Here R may still contain condition(s) equivalent to k_11=c_11.
4383 4384
    Nevertheless, the following holds:

4385
      P(k_11=c_11 AND R) = P(k_11=c_11) * P(R | k_11=c_11).
4386 4387 4388 4389 4390

    Mark k_11 as fixed field (and satisfied condition) F, save P(F),
    save R to be cond and proceed to recursion step.

    Recursion step:
4391
    We have a set of fixed fields/satisfied conditions) F, probability P(F),
4392 4393 4394
    and remaining conjunction R
    Pick next key part on current key and its condition "k_ij=c_ij".
    We will add "k_ij=c_ij" into F and update P(F).
4395
    Lets denote k_ij as t,  R = t AND R1, where R1 may still contain t. Then
4396

4397
     P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)
4398 4399 4400 4401 4402 4403 4404

    (where '|' mean conditional probability, not "or")

    Consider the first multiplier in (2). One of the following holds:
    a) F contains condition on field used in t (i.e. t AND F = F).
      Then P(t|F) = 1

4405 4406
    b) F doesn't contain condition on field used in t. Then F and t are
     considered independent.
4407

4408
     P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
4409 4410
          = P(t|fields_before_t_in_key).

4411 4412
     P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
                                   #records(fields_before_t_in_key, t)
4413 4414

    The second multiplier is calculated by applying this step recursively.
4415

4416 4417 4418 4419 4420
  IMPLEMENTATION
    This function calculates the result of application of the "recursion step"
    described above for all fixed key members of a single key, accumulating set
    of covered fields, selectivity, etc.

4421
    The calculation is conducted as follows:
4422
    Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate
4423

4424
     n_{k1}      n_{k2}
4425
    --------- * ---------  * .... (3)
4426
     n_{k1-1}    n_{k2-1}
4427

4428 4429 4430 4431
    where k1,k2,... are key parts which fields were not yet marked as fixed
    ( this is result of application of option b) of the recursion step for
      parts of a single key).
    Since it is reasonable to expect that most of the fields are not marked
4432
    as fixed, we calculate (3) as
4433

4434
                                  n_{i1}      n_{i2}
4435
    (3) = n_{max_key_part}  / (   --------- * ---------  * ....  )
4436
                                  n_{i1-1}    n_{i2-1}
4437 4438 4439

    where i1,i2, .. are key parts that were already marked as fixed.

4440 4441
    In order to minimize number of expensive records_in_range calls we group
    and reduce adjacent fractions.
4442

4443
  RETURN
4444
    Selectivity of given ROR scan.
4445 4446
*/

4447 4448
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info, 
                                   const ROR_SCAN_INFO *scan)
4449 4450
{
  double selectivity_mult= 1.0;
4451
  KEY_PART_INFO *key_part= info->param->table->key_info[scan->keynr].key_part;
4452 4453
  uchar key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
  uchar *key_ptr= key_val;
4454
  SEL_ARG *sel_arg, *tuple_arg= NULL;
4455
  key_part_map keypart_map= 0;
4456
  bool cur_covered;
4457
  bool prev_covered= test(bitmap_is_set(&info->covered_fields,
4458
                                        key_part->fieldnr-1));
sergefp@mysql.com's avatar
sergefp@mysql.com committed
4459 4460
  key_range min_range;
  key_range max_range;
4461
  min_range.key= key_val;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
4462
  min_range.flag= HA_READ_KEY_EXACT;
4463
  max_range.key= key_val;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
4464
  max_range.flag= HA_READ_AFTER_KEY;
4465
  ha_rows prev_records= info->param->table->file->stats.records;
4466
  DBUG_ENTER("ror_scan_selectivity");
4467 4468 4469

  for (sel_arg= scan->sel_arg; sel_arg;
       sel_arg= sel_arg->next_key_part)
4470
  {
4471
    DBUG_PRINT("info",("sel_arg step"));
4472
    cur_covered= test(bitmap_is_set(&info->covered_fields,
4473
                                    key_part[sel_arg->part].fieldnr-1));
4474
    if (cur_covered != prev_covered)
4475
    {
4476
      /* create (part1val, ..., part{n-1}val) tuple. */
4477 4478
      ha_rows records;
      if (!tuple_arg)
4479
      {
4480 4481
        tuple_arg= scan->sel_arg;
        /* Here we use the length of the first key part */
4482
        tuple_arg->store_min(key_part->store_length, &key_ptr, 0);
4483
        keypart_map= 1;
4484 4485 4486 4487
      }
      while (tuple_arg->next_key_part != sel_arg)
      {
        tuple_arg= tuple_arg->next_key_part;
4488 4489 4490
        tuple_arg->store_min(key_part[tuple_arg->part].store_length,
                             &key_ptr, 0);
        keypart_map= (keypart_map << 1) | 1;
4491
      }
4492
      min_range.length= max_range.length= (size_t) (key_ptr - key_val);
4493
      min_range.keypart_map= max_range.keypart_map= keypart_map;
4494 4495
      records= (info->param->table->file->
                records_in_range(scan->keynr, &min_range, &max_range));
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506
      if (cur_covered)
      {
        /* uncovered -> covered */
        double tmp= rows2double(records)/rows2double(prev_records);
        DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
        selectivity_mult *= tmp;
        prev_records= HA_POS_ERROR;
      }
      else
      {
        /* covered -> uncovered */
4507
        prev_records= records;
4508
      }
4509
    }
4510 4511 4512 4513
    prev_covered= cur_covered;
  }
  if (!prev_covered)
  {
4514
    double tmp= rows2double(info->param->table->quick_rows[scan->keynr]) /
4515 4516
                rows2double(prev_records);
    DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
4517
    selectivity_mult *= tmp;
4518
  }
4519 4520 4521
  DBUG_PRINT("info", ("Returning multiplier: %g", selectivity_mult));
  DBUG_RETURN(selectivity_mult);
}
4522

4523

4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560
/*
  Check if adding a ROR scan to a ROR-intersection reduces its cost of
  ROR-intersection and if yes, update parameters of ROR-intersection,
  including its cost.

  SYNOPSIS
    ror_intersect_add()
      param        Parameter from test_quick_select
      info         ROR-intersection structure to add the scan to.
      ror_scan     ROR scan info to add.
      is_cpk_scan  If TRUE, add the scan as CPK scan (this can be inferred
                   from other parameters and is passed separately only to
                   avoid duplicating the inference code)

  NOTES
    Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
    intersection decreases. The cost of ROR-intersection is calculated as
    follows:

    cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval

    When we add a scan the first increases and the second decreases.

    cost_of_full_rows_retrieval=
      (union of indexes used covers all needed fields) ?
        cost_of_sweep_read(E(rows_to_retrieve), rows_in_table) :
        0

    E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
                           ror_scan_selectivity({scan1}, scan2) * ... *
                           ror_scan_selectivity({scan1,...}, scanN). 
  RETURN
    TRUE   ROR scan added to ROR-intersection, cost updated.
    FALSE  It doesn't make sense to add this ROR scan to this ROR-intersection.
*/

static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
4561
                              ROR_SCAN_INFO* ror_scan, bool is_cpk_scan)
4562 4563 4564 4565 4566 4567 4568
{
  double selectivity_mult= 1.0;

  DBUG_ENTER("ror_intersect_add");
  DBUG_PRINT("info", ("Current out_rows= %g", info->out_rows));
  DBUG_PRINT("info", ("Adding scan on %s",
                      info->param->table->key_info[ror_scan->keynr].name));
4569
  DBUG_PRINT("info", ("is_cpk_scan: %d",is_cpk_scan));
4570 4571

  selectivity_mult = ror_scan_selectivity(info, ror_scan);
4572 4573 4574
  if (selectivity_mult == 1.0)
  {
    /* Don't add this scan if it doesn't improve selectivity. */
4575
    DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
4576
    DBUG_RETURN(FALSE);
4577
  }
4578 4579 4580
  
  info->out_rows *= selectivity_mult;
  
4581
  if (is_cpk_scan)
4582
  {
4583 4584 4585 4586 4587 4588
    /*
      CPK scan is used to filter out rows. We apply filtering for 
      each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
      per check this gives us:
    */
    info->index_scan_costs += rows2double(info->index_records) / 
4589 4590 4591 4592
                              TIME_FOR_COMPARE_ROWID;
  }
  else
  {
4593
    info->index_records += info->param->table->quick_rows[ror_scan->keynr];
4594 4595
    info->index_scan_costs += ror_scan->index_read_cost;
    bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
4596 4597 4598 4599 4600 4601
    if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
                                               &info->covered_fields))
    {
      DBUG_PRINT("info", ("ROR-intersect is covering now"));
      info->is_covering= TRUE;
    }
4602
  }
4603

4604
  info->total_cost= info->index_scan_costs;
4605
  DBUG_PRINT("info", ("info->total_cost: %g", info->total_cost));
4606 4607
  if (!info->is_covering)
  {
4608 4609 4610
    info->total_cost += 
      get_sweep_read_cost(info->param, double2rows(info->out_rows));
    DBUG_PRINT("info", ("info->total_cost= %g", info->total_cost));
4611
  }
4612 4613
  DBUG_PRINT("info", ("New out_rows: %g", info->out_rows));
  DBUG_PRINT("info", ("New cost: %g, %scovering", info->total_cost,
4614
                      info->is_covering?"" : "non-"));
4615
  DBUG_RETURN(TRUE);
4616 4617
}

4618

4619 4620
/*
  Get best ROR-intersection plan using non-covering ROR-intersection search
4621 4622 4623 4624
  algorithm. The returned plan may be covering.

  SYNOPSIS
    get_best_ror_intersect()
4625 4626 4627
      param            Parameter from test_quick_select function.
      tree             Transformed restriction condition to be used to look
                       for ROR scans.
4628
      read_time        Do not return read plans with cost > read_time.
4629
      are_all_covering [out] set to TRUE if union of all scans covers all
4630 4631
                       fields needed by the query (and it is possible to build
                       a covering ROR-intersection)
4632

4633
  NOTES
4634 4635 4636 4637 4638
    get_key_scans_params must be called before this function can be called.
    
    When this function is called by ROR-union construction algorithm it
    assumes it is building an uncovered ROR-intersection (and thus # of full
    records to be retrieved is wrong here). This is a hack.
4639

4640
  IMPLEMENTATION
4641
    The approximate best non-covering plan search algorithm is as follows:
4642

4643 4644 4645 4646
    find_min_ror_intersection_scan()
    {
      R= select all ROR scans;
      order R by (E(#records_matched) * key_record_length).
4647

4648 4649 4650 4651 4652 4653
      S= first(R); -- set of scans that will be used for ROR-intersection
      R= R-first(S);
      min_cost= cost(S);
      min_scan= make_scan(S);
      while (R is not empty)
      {
4654 4655
        firstR= R - first(R);
        if (!selectivity(S + firstR < selectivity(S)))
4656
          continue;
4657
          
4658 4659 4660 4661 4662 4663 4664 4665 4666
        S= S + first(R);
        if (cost(S) < min_cost)
        {
          min_cost= cost(S);
          min_scan= make_scan(S);
        }
      }
      return min_scan;
    }
4667

4668
    See ror_intersect_add function for ROR intersection costs.
4669

4670
    Special handling for Clustered PK scans
4671 4672
    Clustered PK contains all table fields, so using it as a regular scan in
    index intersection doesn't make sense: a range scan on CPK will be less
4673 4674
    expensive in this case.
    Clustered PK scan has special handling in ROR-intersection: it is not used
4675
    to retrieve rows, instead its condition is used to filter row references
4676
    we get from scans on other keys.
4677 4678

  RETURN
4679
    ROR-intersection table read plan
4680
    NULL if out of memory or no suitable plan found.
4681 4682
*/

4683 4684 4685 4686 4687 4688
static
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
                                          double read_time,
                                          bool *are_all_covering)
{
  uint idx;
4689
  double min_cost= DBL_MAX;
4690
  DBUG_ENTER("get_best_ror_intersect");
4691

4692
  if ((tree->n_ror_scans < 2) || !param->table->file->stats.records ||
4693
      !optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
4694
    DBUG_RETURN(NULL);
4695 4696

  /*
4697 4698
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of 
    them. Also find and save clustered PK scan if there is one.
4699
  */
4700
  ROR_SCAN_INFO **cur_ror_scan;
4701
  ROR_SCAN_INFO *cpk_scan= NULL;
4702
  uint cpk_no;
monty@mysql.com's avatar
monty@mysql.com committed
4703
  bool cpk_scan_used= FALSE;
4704

4705 4706 4707 4708
  if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     param->keys)))
    return NULL;
4709 4710
  cpk_no= ((param->table->file->primary_key_is_clustered()) ?
           param->table->s->primary_key : MAX_KEY);
4711

4712
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
4713
  {
4714
    ROR_SCAN_INFO *scan;
4715
    if (!tree->ror_scans_map.is_set(idx))
4716
      continue;
4717
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
4718
      return NULL;
4719
    if (param->real_keynr[idx] == cpk_no)
4720
    {
4721 4722
      cpk_scan= scan;
      tree->n_ror_scans--;
4723 4724
    }
    else
4725
      *(cur_ror_scan++)= scan;
4726
  }
4727

4728
  tree->ror_scans_end= cur_ror_scan;
4729 4730
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
                                          tree->ror_scans,
4731 4732
                                          tree->ror_scans_end););
  /*
4733
    Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
4734 4735
    ROR_SCAN_INFO's.
    Step 2: Get best ROR-intersection using an approximate algorithm.
4736
  */
4737 4738
  my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
           (qsort_cmp)cmp_ror_scan_info);
4739 4740
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
                                          tree->ror_scans,
4741
                                          tree->ror_scans_end););
4742

4743 4744 4745 4746 4747 4748 4749 4750 4751
  ROR_SCAN_INFO **intersect_scans; /* ROR scans used in index intersection */
  ROR_SCAN_INFO **intersect_scans_end;
  if (!(intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     tree->n_ror_scans)))
    return NULL;
  intersect_scans_end= intersect_scans;

  /* Create and incrementally update ROR intersection. */
4752 4753 4754
  ROR_INTERSECT_INFO *intersect, *intersect_best;
  if (!(intersect= ror_intersect_init(param)) || 
      !(intersect_best= ror_intersect_init(param)))
4755
    return NULL;
4756

4757
  /* [intersect_scans,intersect_scans_best) will hold the best intersection */
4758
  ROR_SCAN_INFO **intersect_scans_best;
4759
  cur_ror_scan= tree->ror_scans;
4760
  intersect_scans_best= intersect_scans;
4761
  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
4762
  {
4763
    /* S= S + first(R);  R= R - first(R); */
4764
    if (!ror_intersect_add(intersect, *cur_ror_scan, FALSE))
4765 4766 4767 4768 4769 4770
    {
      cur_ror_scan++;
      continue;
    }
    
    *(intersect_scans_end++)= *(cur_ror_scan++);
4771

4772
    if (intersect->total_cost < min_cost)
4773
    {
4774
      /* Local minimum found, save it */
4775
      ror_intersect_cpy(intersect_best, intersect);
4776
      intersect_scans_best= intersect_scans_end;
4777
      min_cost = intersect->total_cost;
4778 4779
    }
  }
4780

4781 4782 4783 4784 4785 4786
  if (intersect_scans_best == intersect_scans)
  {
    DBUG_PRINT("info", ("None of scans increase selectivity"));
    DBUG_RETURN(NULL);
  }
    
4787 4788 4789 4790
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
                                          "best ROR-intersection",
                                          intersect_scans,
                                          intersect_scans_best););
4791

4792
  *are_all_covering= intersect->is_covering;
4793
  uint best_num= intersect_scans_best - intersect_scans;
4794 4795
  ror_intersect_cpy(intersect, intersect_best);

4796 4797
  /*
    Ok, found the best ROR-intersection of non-CPK key scans.
4798 4799
    Check if we should add a CPK scan. If the obtained ROR-intersection is 
    covering, it doesn't make sense to add CPK scan.
4800 4801
  */
  if (cpk_scan && !intersect->is_covering)
4802
  {
4803
    if (ror_intersect_add(intersect, cpk_scan, TRUE) && 
4804
        (intersect->total_cost < min_cost))
4805
    {
monty@mysql.com's avatar
monty@mysql.com committed
4806
      cpk_scan_used= TRUE;
4807
      intersect_best= intersect; //just set pointer here
4808 4809
    }
  }
4810

4811
  /* Ok, return ROR-intersect plan if we have found one */
4812
  TRP_ROR_INTERSECT *trp= NULL;
4813
  if (min_cost < read_time && (cpk_scan_used || best_num > 1))
4814
  {
4815 4816
    if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
      DBUG_RETURN(trp);
4817 4818
    if (!(trp->first_scan=
           (ROR_SCAN_INFO**)alloc_root(param->mem_root,
4819 4820 4821 4822
                                       sizeof(ROR_SCAN_INFO*)*best_num)))
      DBUG_RETURN(NULL);
    memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
    trp->last_scan=  trp->first_scan + best_num;
4823 4824 4825 4826 4827 4828
    trp->is_covering= intersect_best->is_covering;
    trp->read_cost= intersect_best->total_cost;
    /* Prevent divisons by zero */
    ha_rows best_rows = double2rows(intersect_best->out_rows);
    if (!best_rows)
      best_rows= 1;
4829
    set_if_smaller(param->table->quick_condition_rows, best_rows);
4830
    trp->records= best_rows;
4831 4832 4833 4834 4835
    trp->index_scan_costs= intersect_best->index_scan_costs;
    trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
    DBUG_PRINT("info", ("Returning non-covering ROR-intersect plan:"
                        "cost %g, records %lu",
                        trp->read_cost, (ulong) trp->records));
4836
  }
4837
  DBUG_RETURN(trp);
4838 4839 4840 4841
}


/*
4842
  Get best covering ROR-intersection.
4843
  SYNOPSIS
4844
    get_best_covering_ror_intersect()
4845 4846 4847
      param     Parameter from test_quick_select function.
      tree      SEL_TREE with sets of intervals for different keys.
      read_time Don't return table read plans with cost > read_time.
4848

4849 4850
  RETURN
    Best covering ROR-intersection plan
4851
    NULL if no plan found.
4852 4853

  NOTES
4854
    get_best_ror_intersect must be called for a tree before calling this
4855
    function for it.
4856
    This function invalidates tree->ror_scans member values.
4857

4858 4859 4860 4861 4862
  The following approximate algorithm is used:
    I=set of all covering indexes
    F=set of all fields to cover
    S={}

4863 4864
    do
    {
4865 4866 4867 4868 4869 4870 4871
      Order I by (#covered fields in F desc,
                  #components asc,
                  number of first not covered component asc);
      F=F-covered by first(I);
      S=S+first(I);
      I=I-first(I);
    } while F is not empty.
4872 4873
*/

4874
static
4875 4876
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
4877
                                                   double read_time)
4878
{
4879
  ROR_SCAN_INFO **ror_scan_mark;
4880
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
4881 4882
  DBUG_ENTER("get_best_covering_ror_intersect");

4883
  if (!optimizer_flag(param->thd, OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
4884 4885
    DBUG_RETURN(NULL);

4886
  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
4887
    (*scan)->key_components=
4888
      param->table->key_info[(*scan)->keynr].key_parts;
4889

4890 4891
  /*
    Run covering-ROR-search algorithm.
4892
    Assume set I is [ror_scan .. ror_scans_end)
4893
  */
4894

4895 4896
  /*I=set of all covering indexes */
  ror_scan_mark= tree->ror_scans;
4897

4898 4899
  MY_BITMAP *covered_fields= &param->tmp_covered_fields;
  if (!covered_fields->bitmap) 
4900
    covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
4901 4902
                                               param->fields_bitmap_size);
  if (!covered_fields->bitmap ||
4903 4904
      bitmap_init(covered_fields, covered_fields->bitmap,
                  param->table->s->fields, FALSE))
4905
    DBUG_RETURN(0);
4906
  bitmap_clear_all(covered_fields);
4907 4908 4909

  double total_cost= 0.0f;
  ha_rows records=0;
4910 4911
  bool all_covered;

4912 4913 4914 4915
  DBUG_PRINT("info", ("Building covering ROR-intersection"));
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                           "building covering ROR-I",
                                           ror_scan_mark, ror_scans_end););
4916 4917
  do
  {
4918
    /*
4919
      Update changed sorting info:
4920
        #covered fields,
4921
	number of first not covered component
4922 4923 4924 4925
      Calculate and save these values for each of remaining scans.
    */
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
    {
4926
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
4927
      (*scan)->used_fields_covered=
4928
        bitmap_bits_set(&(*scan)->covered_fields);
4929
      (*scan)->first_uncovered_field=
4930 4931 4932
        bitmap_get_first(&(*scan)->covered_fields);
    }

4933 4934
    my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
             (qsort_cmp)cmp_ror_scan_info_covering);
4935 4936 4937 4938

    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                             "remaining scans",
                                             ror_scan_mark, ror_scans_end););
4939

4940 4941 4942
    /* I=I-first(I) */
    total_cost += (*ror_scan_mark)->index_read_cost;
    records += (*ror_scan_mark)->records;
4943
    DBUG_PRINT("info", ("Adding scan on %s",
4944 4945 4946 4947
                        param->table->key_info[(*ror_scan_mark)->keynr].name));
    if (total_cost > read_time)
      DBUG_RETURN(NULL);
    /* F=F-covered by first(I) */
4948 4949
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
4950 4951 4952 4953
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
  
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
    DBUG_RETURN(NULL);
4954 4955 4956 4957 4958 4959 4960 4961 4962

  /*
    Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
    cost total_cost.
  */
  DBUG_PRINT("info", ("Covering ROR-intersect scans cost: %g", total_cost));
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                           "creating covering ROR-intersect",
                                           tree->ror_scans, ror_scan_mark););
4963

4964
  /* Add priority queue use cost. */
4965 4966
  total_cost += rows2double(records)*
                log((double)(ror_scan_mark - tree->ror_scans)) /
4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
                (TIME_FOR_COMPARE_ROWID * M_LN2);
  DBUG_PRINT("info", ("Covering ROR-intersect full cost: %g", total_cost));

  if (total_cost > read_time)
    DBUG_RETURN(NULL);

  TRP_ROR_INTERSECT *trp;
  if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
    DBUG_RETURN(trp);
  uint best_num= (ror_scan_mark - tree->ror_scans);
  if (!(trp->first_scan= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     best_num)))
    DBUG_RETURN(NULL);
4981
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
4982
  trp->last_scan=  trp->first_scan + best_num;
monty@mysql.com's avatar
monty@mysql.com committed
4983
  trp->is_covering= TRUE;
4984 4985
  trp->read_cost= total_cost;
  trp->records= records;
4986
  trp->cpk_scan= NULL;
4987
  set_if_smaller(param->table->quick_condition_rows, records); 
4988

4989 4990 4991
  DBUG_PRINT("info",
             ("Returning covering ROR-intersect plan: cost %g, records %lu",
              trp->read_cost, (ulong) trp->records));
4992
  DBUG_RETURN(trp);
4993 4994 4995
}


4996
/*
4997
  Get best "range" table read plan for given SEL_TREE.
4998
  Also update PARAM members and store ROR scans info in the SEL_TREE.
4999
  SYNOPSIS
5000
    get_key_scans_params
5001
      param        parameters from test_quick_select
5002
      tree         make range select for this SEL_TREE
monty@mysql.com's avatar
monty@mysql.com committed
5003
      index_read_must_be_used if TRUE, assume 'index only' option will be set
5004
                             (except for clustered PK indexes)
5005 5006
      read_time    don't create read plans with cost > read_time.
  RETURN
5007
    Best range read plan
5008
    NULL if no plan found or error occurred
5009 5010
*/

5011
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
5012 5013
                                       bool index_read_must_be_used, 
                                       bool update_tbl_stats,
5014
                                       double read_time)
5015 5016
{
  int idx;
5017
  SEL_ARG **key,**end, **key_to_read= NULL;
5018
  ha_rows UNINIT_VAR(best_records);              /* protected by key_to_read */
5019
  TRP_RANGE* read_plan= NULL;
5020
  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
5021
  DBUG_ENTER("get_key_scans_params");
5022
  /*
5023 5024
    Note that there may be trees that have type SEL_TREE::KEY but contain no
    key reads at all, e.g. tree for expression "key1 is not null" where key1
5025
    is defined as "not null".
5026 5027
  */
  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
5028 5029 5030 5031
                                      "tree scans"););
  tree->ror_scans_map.clear_all();
  tree->n_ror_scans= 0;
  for (idx= 0,key=tree->keys, end=key+param->keys;
5032 5033 5034 5035 5036 5037 5038
       key != end ;
       key++,idx++)
  {
    ha_rows found_records;
    double found_read_time;
    if (*key)
    {
5039
      uint keynr= param->real_keynr[idx];
5040 5041
      if ((*key)->type == SEL_ARG::MAYBE_KEY ||
          (*key)->maybe_flag)
5042
        param->needed_reg->set_bit(keynr);
5043

monty@mysql.com's avatar
monty@mysql.com committed
5044
      bool read_index_only= index_read_must_be_used ? TRUE :
5045
                            (bool) param->table->covering_keys.is_set(keynr);
5046

5047
      found_records= check_quick_select(param, idx, *key, update_tbl_stats);
5048 5049 5050 5051 5052
      if (param->is_ror_scan)
      {
        tree->n_ror_scans++;
        tree->ror_scans_map.set_bit(idx);
      }
5053
      double cpu_cost= (double) found_records / TIME_FOR_COMPARE;
5054
      if (found_records != HA_POS_ERROR && found_records > 2 &&
sergefp@mysql.com's avatar
sergefp@mysql.com committed
5055
          read_index_only &&
monty@mysql.com's avatar
monty@mysql.com committed
5056
          (param->table->file->index_flags(keynr, param->max_key_part,1) &
monty@mysql.com's avatar
monty@mysql.com committed
5057
           HA_KEYREAD_ONLY) &&
5058
          !(pk_is_clustered && keynr == param->table->s->primary_key))
5059 5060 5061 5062 5063
      {
        /*
          We can resolve this by only reading through this key. 
          0.01 is added to avoid races between range and 'index' scan.
        */
5064
        found_read_time= get_index_only_read_time(param,found_records,keynr) +
5065 5066
                         cpu_cost + 0.01;
      }
5067
      else
5068
      {
5069
        /*
5070 5071 5072
          cost(read_through_index) = cost(disk_io) + cost(row_in_range_checks)
          The row_in_range check is in QUICK_RANGE_SELECT::cmp_next function.
        */
5073 5074 5075
	found_read_time= param->table->file->read_time(keynr,
                                                       param->range_count,
                                                       found_records) +
5076 5077
			 cpu_cost + 0.01;
      }
5078 5079 5080
      DBUG_PRINT("info",("key %s: found_read_time: %g (cur. read_time: %g)",
                         param->table->key_info[keynr].name, found_read_time,
                         read_time));
5081

5082
      if (read_time > found_read_time && found_records != HA_POS_ERROR)
5083
      {
5084
        read_time=    found_read_time;
5085
        best_records= found_records;
5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101
        key_to_read=  key;
      }

    }
  }

  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->ror_scans_map,
                                      "ROR scans"););
  if (key_to_read)
  {
    idx= key_to_read - tree->keys;
    if ((read_plan= new (param->mem_root) TRP_RANGE(*key_to_read, idx)))
    {
      read_plan->records= best_records;
      read_plan->is_ror= tree->ror_scans_map.is_set(idx);
      read_plan->read_cost= read_time;
5102 5103 5104 5105
      DBUG_PRINT("info",
                 ("Returning range plan for key %s, cost %g, records %lu",
                  param->table->key_info[param->real_keynr[idx]].name,
                  read_plan->read_cost, (ulong) read_plan->records));
5106 5107 5108 5109 5110 5111 5112 5113 5114
    }
  }
  else
    DBUG_PRINT("info", ("No 'range' table read plan found"));

  DBUG_RETURN(read_plan);
}


5115
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126
                                            bool retrieve_full_rows,
                                            MEM_ROOT *parent_alloc)
{
  QUICK_INDEX_MERGE_SELECT *quick_imerge;
  QUICK_RANGE_SELECT *quick;
  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
    return NULL;

  quick_imerge->records= records;
  quick_imerge->read_time= read_cost;
5127 5128
  for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
       range_scan++)
5129 5130
  {
    if (!(quick= (QUICK_RANGE_SELECT*)
5131
          ((*range_scan)->make_quick(param, FALSE, &quick_imerge->alloc)))||
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
        quick_imerge->push_quick_back(quick))
    {
      delete quick;
      delete quick_imerge;
      return NULL;
    }
  }
  return quick_imerge;
}

5142
QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
5143 5144 5145 5146 5147 5148 5149
                                              bool retrieve_full_rows,
                                              MEM_ROOT *parent_alloc)
{
  QUICK_ROR_INTERSECT_SELECT *quick_intrsect;
  QUICK_RANGE_SELECT *quick;
  DBUG_ENTER("TRP_ROR_INTERSECT::make_quick");
  MEM_ROOT *alloc;
5150 5151

  if ((quick_intrsect=
5152
         new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
5153 5154
                                        (retrieve_full_rows? (!is_covering) :
                                         FALSE),
5155 5156
                                        parent_alloc)))
  {
5157
    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
5158 5159 5160
                                             "creating ROR-intersect",
                                             first_scan, last_scan););
    alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
5161
    for (; first_scan != last_scan;++first_scan)
5162 5163 5164 5165
    {
      if (!(quick= get_quick_select(param, (*first_scan)->idx,
                                    (*first_scan)->sel_arg, alloc)) ||
          quick_intrsect->push_quick_back(quick))
5166
      {
5167 5168
        delete quick_intrsect;
        DBUG_RETURN(NULL);
5169 5170
      }
    }
5171 5172 5173 5174
    if (cpk_scan)
    {
      if (!(quick= get_quick_select(param, cpk_scan->idx,
                                    cpk_scan->sel_arg, alloc)))
5175
      {
5176 5177
        delete quick_intrsect;
        DBUG_RETURN(NULL);
5178
      }
5179
      quick->file= NULL; 
5180
      quick_intrsect->cpk_quick= quick;
5181
    }
5182
    quick_intrsect->records= records;
5183
    quick_intrsect->read_time= read_cost;
5184
  }
5185 5186 5187
  DBUG_RETURN(quick_intrsect);
}

5188

5189
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
5190 5191 5192 5193 5194 5195 5196
                                          bool retrieve_full_rows,
                                          MEM_ROOT *parent_alloc)
{
  QUICK_ROR_UNION_SELECT *quick_roru;
  TABLE_READ_PLAN **scan;
  QUICK_SELECT_I *quick;
  DBUG_ENTER("TRP_ROR_UNION::make_quick");
5197 5198
  /*
    It is impossible to construct a ROR-union that will not retrieve full
5199
    rows, ignore retrieve_full_rows parameter.
5200 5201 5202
  */
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
  {
5203
    for (scan= first_ror; scan != last_ror; scan++)
5204
    {
5205
      if (!(quick= (*scan)->make_quick(param, FALSE, &quick_roru->alloc)) ||
5206 5207 5208 5209 5210
          quick_roru->push_quick_back(quick))
        DBUG_RETURN(NULL);
    }
    quick_roru->records= records;
    quick_roru->read_time= read_cost;
5211
  }
5212
  DBUG_RETURN(quick_roru);
5213 5214
}

5215

5216
/*
monty@mysql.com's avatar
monty@mysql.com committed
5217
  Build a SEL_TREE for <> or NOT BETWEEN predicate
5218 5219 5220 5221 5222 5223
 
  SYNOPSIS
    get_ne_mm_tree()
      param       PARAM from SQL_SELECT::test_quick_select
      cond_func   item for the predicate
      field       field in the predicate
monty@mysql.com's avatar
monty@mysql.com committed
5224 5225
      lt_value    constant that field should be smaller
      gt_value    constant that field should be greaterr
5226 5227 5228
      cmp_type    compare type for the field

  RETURN 
monty@mysql.com's avatar
monty@mysql.com committed
5229 5230
    #  Pointer to tree built tree
    0  on error
5231 5232
*/

5233
static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
monty@mysql.com's avatar
monty@mysql.com committed
5234 5235
                                Field *field,
                                Item *lt_value, Item *gt_value,
5236 5237
                                Item_result cmp_type)
{
monty@mysql.com's avatar
monty@mysql.com committed
5238
  SEL_TREE *tree;
5239
  tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
monty@mysql.com's avatar
monty@mysql.com committed
5240
                     lt_value, cmp_type);
5241 5242 5243 5244
  if (tree)
  {
    tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
					    Item_func::GT_FUNC,
monty@mysql.com's avatar
monty@mysql.com committed
5245
					    gt_value, cmp_type));
5246 5247 5248 5249 5250
  }
  return tree;
}
   

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
/*
  Build a SEL_TREE for a simple predicate
 
  SYNOPSIS
    get_func_mm_tree()
      param       PARAM from SQL_SELECT::test_quick_select
      cond_func   item for the predicate
      field       field in the predicate
      value       constant in the predicate
      cmp_type    compare type for the field
5261
      inv         TRUE <> NOT cond_func is considered
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5262
                  (makes sense only when cond_func is BETWEEN or IN) 
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5263 5264

  RETURN 
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5265
    Pointer to the tree built tree
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5266 5267
*/

5268
static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
5269
                                  Field *field, Item *value,
5270
                                  Item_result cmp_type, bool inv)
5271 5272 5273 5274
{
  SEL_TREE *tree= 0;
  DBUG_ENTER("get_func_mm_tree");

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5275
  switch (cond_func->functype()) {
5276

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5277
  case Item_func::NE_FUNC:
monty@mysql.com's avatar
monty@mysql.com committed
5278
    tree= get_ne_mm_tree(param, cond_func, field, value, value, cmp_type);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5279
    break;
5280

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5281
  case Item_func::BETWEEN:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5282
  {
evgen@moonbone.local's avatar
evgen@moonbone.local committed
5283
    if (!value)
5284
    {
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5285
      if (inv)
5286
      {
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5287 5288 5289 5290
        tree= get_ne_mm_tree(param, cond_func, field, cond_func->arguments()[1],
                             cond_func->arguments()[2], cmp_type);
      }
      else
5291
      {
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5292 5293 5294 5295 5296 5297 5298 5299 5300
        tree= get_mm_parts(param, cond_func, field, Item_func::GE_FUNC,
		           cond_func->arguments()[1],cmp_type);
        if (tree)
        {
          tree= tree_and(param, tree, get_mm_parts(param, cond_func, field,
					           Item_func::LE_FUNC,
					           cond_func->arguments()[2],
                                                   cmp_type));
        }
5301
      }
5302
    }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5303 5304 5305
    else
      tree= get_mm_parts(param, cond_func, field,
                         (inv ?
evgen@sunlight.local's avatar
evgen@sunlight.local committed
5306 5307 5308 5309
                          (value == (Item*)1 ? Item_func::GT_FUNC :
                                               Item_func::LT_FUNC):
                          (value == (Item*)1 ? Item_func::LE_FUNC :
                                               Item_func::GE_FUNC)),
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5310
                         cond_func->arguments()[0], cmp_type);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5311
    break;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5312
  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5313
  case Item_func::IN_FUNC:
5314 5315
  {
    Item_func_in *func=(Item_func_in*) cond_func;
5316

5317 5318 5319 5320 5321
    /*
      Array for IN() is constructed when all values have the same result
      type. Tree won't be built for values with different result types,
      so we check it here to avoid unnecessary work.
    */
5322 5323
    if (!func->arg_types_compatible)
      break;     
5324

5325
    if (inv)
5326
    {
5327
      if (func->array && func->array->result_type() != ROW_RESULT)
5328
      {
5329
        /*
5330 5331 5332
          We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
          where c{i} are constants. Our goal is to produce a SEL_TREE that 
          represents intervals:
5333 5334 5335 5336 5337
          
          ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ...    (*)
          
          where $MIN is either "-inf" or NULL.
          
5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
          The most straightforward way to produce it is to convert NOT IN
          into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
          analyzer to build SEL_TREE from that. The problem is that the
          range analyzer will use O(N^2) memory (which is probably a bug),
          and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
          will run out of memory.

          Another problem with big lists like (*) is that a big list is
          unlikely to produce a good "range" access, while considering that
          range access will require expensive CPU calculations (and for 
          MyISAM even index accesses). In short, big NOT IN lists are rarely
          worth analyzing.

          Considering the above, we'll handle NOT IN as follows:
          * if the number of entries in the NOT IN list is less than
            NOT_IN_IGNORE_THRESHOLD, construct the SEL_TREE (*) manually.
          * Otherwise, don't produce a SEL_TREE.
5355
        */
5356
#define NOT_IN_IGNORE_THRESHOLD 1000
5357 5358
        MEM_ROOT *tmp_root= param->mem_root;
        param->thd->mem_root= param->old_root;
5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369
        /* 
          Create one Item_type constant object. We'll need it as
          get_mm_parts only accepts constant values wrapped in Item_Type
          objects.
          We create the Item on param->mem_root which points to
          per-statement mem_root (while thd->mem_root is currently pointing
          to mem_root local to range optimizer).
        */
        Item *value_item= func->array->create_item();
        param->thd->mem_root= tmp_root;

5370
        if (func->array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
5371
          break;
5372

5373
        /* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388
        uint i=0;
        do 
        {
          func->array->value_to_item(i, value_item);
          tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
                             value_item, cmp_type);
          if (!tree)
            break;
          i++;
        } while (i < func->array->count && tree->type == SEL_TREE::IMPOSSIBLE);

        if (!tree || tree->type == SEL_TREE::IMPOSSIBLE)
        {
          /* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
          tree= NULL;
5389
          break;
5390
        }
5391
        SEL_TREE *tree2;
5392
        for (; i < func->array->count; i++)
5393
        {
5394
          if (func->array->compare_elems(i, i-1))
5395
          {
5396 5397 5398 5399 5400
            /* Get a SEL_TREE for "-inf < X < c_i" interval */
            func->array->value_to_item(i, value_item);
            tree2= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
                                value_item, cmp_type);
            if (!tree2)
5401
            {
5402 5403 5404
              tree= NULL;
              break;
            }
5405

5406 5407 5408 5409
            /* Change all intervals to be "c_{i-1} < X < c_i" */
            for (uint idx= 0; idx < param->keys; idx++)
            {
              SEL_ARG *new_interval, *last_val;
5410 5411
              if (((new_interval= tree2->keys[idx])) &&
                  (tree->keys[idx]) &&
5412
                  ((last_val= tree->keys[idx]->last())))
5413
              {
5414 5415
                new_interval->min_value= last_val->max_value;
                new_interval->min_flag= NEAR_MIN;
5416 5417
              }
            }
5418 5419 5420 5421 5422
            /* 
              The following doesn't try to allocate memory so no need to
              check for NULL.
            */
            tree= tree_or(param, tree, tree2);
5423 5424
          }
        }
5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435
        
        if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
        {
          /* 
            Get the SEL_TREE for the last "c_last < X < +inf" interval 
            (value_item cotains c_last already)
          */
          tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
                              value_item, cmp_type);
          tree= tree_or(param, tree, tree2);
        }
5436 5437
      }
      else
5438
      {
5439 5440 5441 5442
        tree= get_ne_mm_tree(param, cond_func, field,
                             func->arguments()[1], func->arguments()[1],
                             cmp_type);
        if (tree)
5443
        {
5444 5445 5446 5447 5448 5449 5450
          Item **arg, **end;
          for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
               arg < end ; arg++)
          {
            tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field, 
                                                        *arg, *arg, cmp_type));
          }
5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467
        }
      }
    }
    else
    {    
      tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
                         func->arguments()[1], cmp_type);
      if (tree)
      {
        Item **arg, **end;
        for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
             arg < end ; arg++)
        {
          tree= tree_or(param, tree, get_mm_parts(param, cond_func, field, 
                                                  Item_func::EQ_FUNC,
                                                  *arg, cmp_type));
        }
5468 5469
      }
    }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5470
    break;
5471
  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5472
  default: 
5473
  {
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5474 5475 5476 5477 5478 5479 5480
    /* 
       Here the function for the following predicates are processed:
       <, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL.
       If the predicate is of the form (value op field) it is handled
       as the equivalent predicate (field rev_op value), e.g.
       2 <= a is handled as a >= 2.
    */
5481 5482 5483
    Item_func::Functype func_type=
      (value != cond_func->arguments()[0]) ? cond_func->functype() :
        ((Item_bool_func2*) cond_func)->rev_functype();
5484
    tree= get_mm_parts(param, cond_func, field, func_type, value, cmp_type);
5485
  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5486 5487
  }

5488 5489
  DBUG_RETURN(tree);
}
5490

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561

/*
  Build conjunction of all SEL_TREEs for a simple predicate applying equalities
 
  SYNOPSIS
    get_full_func_mm_tree()
      param       PARAM from SQL_SELECT::test_quick_select
      cond_func   item for the predicate
      field_item  field in the predicate
      value       constant in the predicate
                  (for BETWEEN it contains the number of the field argument,
                   for IN it's always 0) 
      inv         TRUE <> NOT cond_func is considered
                  (makes sense only when cond_func is BETWEEN or IN)

  DESCRIPTION
    For a simple SARGable predicate of the form (f op c), where f is a field and
    c is a constant, the function builds a conjunction of all SEL_TREES that can
    be obtained by the substitution of f for all different fields equal to f.

  NOTES  
    If the WHERE condition contains a predicate (fi op c),
    then not only SELL_TREE for this predicate is built, but
    the trees for the results of substitution of fi for
    each fj belonging to the same multiple equality as fi
    are built as well.
    E.g. for WHERE t1.a=t2.a AND t2.a > 10 
    a SEL_TREE for t2.a > 10 will be built for quick select from t2
    and   
    a SEL_TREE for t1.a > 10 will be built for quick select from t1.

    A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
    in a similar way: we build a conjuction of trees for the results
    of all substitutions of fi for equal fj.
    Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
    differently. It is considered as a conjuction of two SARGable
    predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
    is called for each of them separately producing trees for 
       AND j (f1j <=c ) and AND j (f2j <= c) 
    After this these two trees are united in one conjunctive tree.
    It's easy to see that the same tree is obtained for
       AND j,k (f1j <=c AND f2k<=c)
    which is equivalent to 
       AND j,k (c BETWEEN f1j AND f2k).
    The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
    which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
    function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
    producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
    trees are united in one OR-tree. The expression 
      (AND j (f1j > c) OR AND j (f2j < c)
    is equivalent to the expression
      AND j,k (f1j > c OR f2k < c) 
    which is just a translation of 
      AND j,k (c NOT BETWEEN f1j AND f2k)

    In the cases when one of the items f1, f2 is a constant c1 we do not create
    a tree for it at all. It works for BETWEEN predicates but does not
    work for NOT BETWEEN predicates as we have to evaluate the expression
    with it. If it is TRUE then the other tree can be completely ignored.
    We do not do it now and no trees are built in these cases for
    NOT BETWEEN predicates.

    As to IN predicates only ones of the form (f IN (c1,...,cn)),
    where f1 is a field and c1,...,cn are constant, are considered as
    SARGable. We never try to narrow the index scan using predicates of
    the form (c IN (c1,...,f,...,cn)). 
      
  RETURN 
    Pointer to the tree representing the built conjunction of SEL_TREEs
*/

5562 5563
static SEL_TREE *get_full_func_mm_tree(RANGE_OPT_PARAM *param,
                                       Item_func *cond_func,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601
                                       Item_field *field_item, Item *value, 
                                       bool inv)
{
  SEL_TREE *tree= 0;
  SEL_TREE *ftree= 0;
  table_map ref_tables= 0;
  table_map param_comp= ~(param->prev_tables | param->read_tables |
		          param->current_table);
  DBUG_ENTER("get_full_func_mm_tree");

  for (uint i= 0; i < cond_func->arg_count; i++)
  {
    Item *arg= cond_func->arguments()[i]->real_item();
    if (arg != field_item)
      ref_tables|= arg->used_tables();
  }
  Field *field= field_item->field;
  Item_result cmp_type= field->cmp_type();
  if (!((ref_tables | field->table->map) & param_comp))
    ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
  Item_equal *item_equal= field_item->item_equal;
  if (item_equal)
  {
    Item_equal_iterator it(*item_equal);
    Item_field *item;
    while ((item= it++))
    {
      Field *f= item->field;
      if (field->eq(f))
        continue;
      if (!((ref_tables | f->table->map) & param_comp))
      {
        tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
      }
    }
  }
  DBUG_RETURN(ftree);
5602 5603
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5604 5605
	/* make a select tree of all keys in condition */

5606
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5607 5608
{
  SEL_TREE *tree=0;
5609 5610
  SEL_TREE *ftree= 0;
  Item_field *field_item= 0;
5611
  bool inv= FALSE;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5612
  Item *value= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625
  DBUG_ENTER("get_mm_tree");

  if (cond->type() == Item::COND_ITEM)
  {
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());

    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
    {
      tree=0;
      Item *item;
      while ((item=li++))
      {
	SEL_TREE *new_tree=get_mm_tree(param,item);
5626 5627
	if (param->thd->is_fatal_error || 
            param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
5628
	  DBUG_RETURN(0);	// out of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643
	tree=tree_and(param,tree,new_tree);
	if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
	  break;
      }
    }
    else
    {						// COND OR
      tree=get_mm_tree(param,li++);
      if (tree)
      {
	Item *item;
	while ((item=li++))
	{
	  SEL_TREE *new_tree=get_mm_tree(param,item);
	  if (!new_tree)
5644
	    DBUG_RETURN(0);	// out of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655
	  tree=tree_or(param,tree,new_tree);
	  if (!tree || tree->type == SEL_TREE::ALWAYS)
	    break;
	}
      }
    }
    DBUG_RETURN(tree);
  }
  /* Here when simple cond */
  if (cond->const_item())
  {
5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667
    /*
      During the cond->val_int() evaluation we can come across a subselect 
      item which may allocate memory on the thd->mem_root and assumes 
      all the memory allocated has the same life span as the subselect 
      item itself. So we have to restore the thread's mem_root here.
    */
    MEM_ROOT *tmp_root= param->mem_root;
    param->thd->mem_root= param->old_root;
    tree= cond->val_int() ? new(tmp_root) SEL_TREE(SEL_TREE::ALWAYS) :
                            new(tmp_root) SEL_TREE(SEL_TREE::IMPOSSIBLE);
    param->thd->mem_root= tmp_root;
    DBUG_RETURN(tree);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5668
  }
5669

5670 5671 5672
  table_map ref_tables= 0;
  table_map param_comp= ~(param->prev_tables | param->read_tables |
		          param->current_table);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5673 5674
  if (cond->type() != Item::FUNC_ITEM)
  {						// Should be a field
5675
    ref_tables= cond->used_tables();
5676 5677
    if ((ref_tables & param->current_table) ||
	(ref_tables & ~(param->prev_tables | param->read_tables)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5678 5679 5680
      DBUG_RETURN(0);
    DBUG_RETURN(new SEL_TREE(SEL_TREE::MAYBE));
  }
5681

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5682
  Item_func *cond_func= (Item_func*) cond;
5683 5684 5685
  if (cond_func->functype() == Item_func::BETWEEN ||
      cond_func->functype() == Item_func::IN_FUNC)
    inv= ((Item_func_opt_neg *) cond_func)->negated;
5686
  else if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5687
    DBUG_RETURN(0);			       
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5688

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5689 5690
  param->cond= cond;

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5691 5692
  switch (cond_func->functype()) {
  case Item_func::BETWEEN:
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
    {
      field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
      ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
    }

    /*
      Concerning the code below see the NOTES section in
      the comments for the function get_full_func_mm_tree()
    */
    for (uint i= 1 ; i < cond_func->arg_count ; i++)
    {
      if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
      {
        field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
        SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func, 
5709
                                    field_item, (Item*)(intptr)i, inv);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722
        if (inv)
          tree= !tree ? tmp : tree_or(param, tree, tmp);
        else 
          tree= tree_and(param, tree, tmp);
      }
      else if (inv)
      { 
        tree= 0;
        break;
      }
    }

    ftree = tree_and(param, ftree, tree);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5723 5724
    break;
  case Item_func::IN_FUNC:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5725 5726
  {
    Item_func_in *func=(Item_func_in*) cond_func;
5727
    if (func->key_item()->real_item()->type() != Item::FIELD_ITEM)
5728
      DBUG_RETURN(0);
5729
    field_item= (Item_field*) (func->key_item()->real_item());
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5730
    ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5731
    break;
5732
  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5733
  case Item_func::MULT_EQUAL_FUNC:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5734
  {
5735 5736
    Item_equal *item_equal= (Item_equal *) cond;    
    if (!(value= item_equal->get_const()))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5737 5738 5739 5740
      DBUG_RETURN(0);
    Item_equal_iterator it(*item_equal);
    ref_tables= value->used_tables();
    while ((field_item= it++))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5741
    {
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5742 5743 5744
      Field *field= field_item->field;
      Item_result cmp_type= field->cmp_type();
      if (!((ref_tables | field->table->map) & param_comp))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5745
      {
5746
        tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5747 5748
		           value,cmp_type);
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5749 5750
      }
    }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5751
    
5752
    DBUG_RETURN(ftree);
5753 5754
  }
  default:
5755
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5756
    {
5757
      field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
5758
      value= cond_func->arg_count > 1 ? cond_func->arguments()[1] : 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5759
    }
5760
    else if (cond_func->have_rev_func() &&
5761 5762
             cond_func->arguments()[1]->real_item()->type() ==
                                                            Item::FIELD_ITEM)
5763
    {
5764
      field_item= (Item_field*) (cond_func->arguments()[1]->real_item());
5765 5766 5767 5768
      value= cond_func->arguments()[0];
    }
    else
      DBUG_RETURN(0);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
5769
    ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5770
  }
5771 5772

  DBUG_RETURN(ftree);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5773 5774 5775 5776
}


static SEL_TREE *
5777
get_mm_parts(RANGE_OPT_PARAM *param, COND *cond_func, Field *field,
5778
	     Item_func::Functype type,
5779
	     Item *value, Item_result cmp_type)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5780 5781 5782 5783 5784
{
  DBUG_ENTER("get_mm_parts");
  if (field->table != param->table)
    DBUG_RETURN(0);

5785 5786
  KEY_PART *key_part = param->key_parts;
  KEY_PART *end = param->key_parts_end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5787 5788 5789 5790
  SEL_TREE *tree=0;
  if (value &&
      value->used_tables() & ~(param->prev_tables | param->read_tables))
    DBUG_RETURN(0);
5791
  for (; key_part != end ; key_part++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5792 5793 5794 5795
  {
    if (field->eq(key_part->field))
    {
      SEL_ARG *sel_arg=0;
5796
      if (!tree && !(tree=new SEL_TREE()))
5797
	DBUG_RETURN(0);				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5798 5799
      if (!value || !(value->used_tables() & ~param->read_tables))
      {
5800 5801
	sel_arg=get_mm_leaf(param,cond_func,
			    key_part->field,key_part,type,value);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5802 5803 5804 5805 5806 5807 5808 5809
	if (!sel_arg)
	  continue;
	if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
	{
	  tree->type=SEL_TREE::IMPOSSIBLE;
	  DBUG_RETURN(tree);
	}
      }
5810 5811
      else
      {
5812
	// This key may be used later
5813
	if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
5814
	  DBUG_RETURN(0);			// OOM
5815
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5816 5817
      sel_arg->part=(uchar) key_part->part;
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
5818
      tree->keys_map.set_bit(key_part->key);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5819 5820
    }
  }
5821

5822 5823
  if (tree && tree->merges.is_empty() && tree->keys_map.is_clear_all())
    tree= NULL;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5824 5825 5826 5827 5828
  DBUG_RETURN(tree);
}


static SEL_ARG *
monty@mysql.com's avatar
monty@mysql.com committed
5829 5830
get_mm_leaf(RANGE_OPT_PARAM *param, COND *conf_func, Field *field,
            KEY_PART *key_part, Item_func::Functype type,Item *value)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5831
{
5832
  uint maybe_null=(uint) field->real_maybe_null();
5833
  bool optimize_range;
5834 5835
  SEL_ARG *tree= 0;
  MEM_ROOT *alloc= param->mem_root;
5836
  uchar *str;
5837
  ulonglong orig_sql_mode;
5838
  int err;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5839 5840
  DBUG_ENTER("get_mm_leaf");

5841 5842
  /*
    We need to restore the runtime mem_root of the thread in this
konstantin@mysql.com's avatar
konstantin@mysql.com committed
5843
    function because it evaluates the value of its argument, while
5844 5845 5846 5847 5848 5849
    the argument can be any, e.g. a subselect. The subselect
    items, in turn, assume that all the memory allocated during
    the evaluation has the same life span as the item itself.
    TODO: opt_range.cc should not reset thd->mem_root at all.
  */
  param->thd->mem_root= param->old_root;
5850 5851
  if (!value)					// IS NULL or IS NOT NULL
  {
5852
    if (field->table->maybe_null)		// Can't use a key on this
5853
      goto end;
5854
    if (!maybe_null)				// Not null field
5855 5856 5857 5858 5859 5860 5861
    {
      if (type == Item_func::ISNULL_FUNC)
        tree= &null_element;
      goto end;
    }
    if (!(tree= new (alloc) SEL_ARG(field,is_null_string,is_null_string)))
      goto end;                                 // out of memory
5862 5863 5864 5865 5866
    if (type == Item_func::ISNOTNULL_FUNC)
    {
      tree->min_flag=NEAR_MIN;		    /* IS NOT NULL ->  X > NULL */
      tree->max_flag=NO_MAX_RANGE;
    }
5867
    goto end;
5868 5869 5870
  }

  /*
5871 5872 5873 5874 5875 5876 5877 5878 5879 5880
    1. Usually we can't use an index if the column collation
       differ from the operation collation.

    2. However, we can reuse a case insensitive index for
       the binary searches:

       WHERE latin1_swedish_ci_column = 'a' COLLATE lati1_bin;

       WHERE latin1_swedish_ci_colimn = BINARY 'a '

5881 5882 5883 5884
  */
  if (field->result_type() == STRING_RESULT &&
      value->result_type() == STRING_RESULT &&
      key_part->image_type == Field::itRAW &&
5885
      ((Field_str*)field)->charset() != conf_func->compare_collation() &&
5886 5887
      !(conf_func->compare_collation()->state & MY_CS_BINSORT &&
        (type == Item_func::EQUAL_FUNC || type == Item_func::EQ_FUNC)))
5888
    goto end;
5889

5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910
  if (key_part->image_type == Field::itMBR)
  {
    switch (type) {
    case Item_func::SP_EQUALS_FUNC:
    case Item_func::SP_DISJOINT_FUNC:
    case Item_func::SP_INTERSECTS_FUNC:
    case Item_func::SP_TOUCHES_FUNC:
    case Item_func::SP_CROSSES_FUNC:
    case Item_func::SP_WITHIN_FUNC:
    case Item_func::SP_CONTAINS_FUNC:
    case Item_func::SP_OVERLAPS_FUNC:
      break;
    default:
      /* 
        We cannot involve spatial indexes for queries that
        don't use MBREQUALS(), MBRDISJOINT(), etc. functions.
      */
      goto end;
    }
  }

5911 5912 5913 5914 5915
  if (param->using_real_indexes)
    optimize_range= field->optimize_range(param->real_keynr[key_part->key],
                                          key_part->part);
  else
    optimize_range= TRUE;
5916

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5917 5918 5919
  if (type == Item_func::LIKE_FUNC)
  {
    bool like_error;
5920 5921
    char buff1[MAX_FIELD_WIDTH];
    uchar *min_str,*max_str;
5922
    String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
5923
    size_t length, offset, min_length, max_length;
5924
    uint field_length= field->pack_length()+maybe_null;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5925

5926
    if (!optimize_range)
5927
      goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5928
    if (!(res= value->val_str(&tmp)))
5929 5930 5931 5932
    {
      tree= &null_element;
      goto end;
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5933

5934 5935 5936 5937 5938
    /*
      TODO:
      Check if this was a function. This should have be optimized away
      in the sql_select.cc
    */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5939 5940 5941 5942 5943 5944
    if (res != &tmp)
    {
      tmp.copy(*res);				// Get own copy
      res= &tmp;
    }
    if (field->cmp_type() != STRING_RESULT)
5945
      goto end;                                 // Can only optimize strings
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5946 5947

    offset=maybe_null;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
5948 5949 5950
    length=key_part->store_length;

    if (length != key_part->length  + maybe_null)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5951
    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
5952 5953 5954
      /* key packed with length prefix */
      offset+= HA_KEY_BLOB_LENGTH;
      field_length= length - HA_KEY_BLOB_LENGTH;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5955 5956 5957
    }
    else
    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
5958 5959 5960 5961 5962 5963 5964 5965
      if (unlikely(length < field_length))
      {
	/*
	  This can only happen in a table created with UNIREG where one key
	  overlaps many fields
	*/
	length= field_length;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5966
      else
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
5967
	field_length= length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5968 5969
    }
    length+=offset;
5970
    if (!(min_str= (uchar*) alloc_root(alloc, length*2)))
5971
      goto end;
5972

bk@work.mysql.com's avatar
bk@work.mysql.com committed
5973 5974 5975
    max_str=min_str+length;
    if (maybe_null)
      max_str[0]= min_str[0]=0;
5976

5977
    field_length-= maybe_null;
5978
    like_error= my_like_range(field->charset(),
monty@mysql.com's avatar
monty@mysql.com committed
5979
			      res->ptr(), res->length(),
monty@mysql.com's avatar
monty@mysql.com committed
5980 5981
			      ((Item_func_like*)(param->cond))->escape,
			      wild_one, wild_many,
5982
			      field_length,
5983
			      (char*) min_str+offset, (char*) max_str+offset,
monty@mysql.com's avatar
monty@mysql.com committed
5984
			      &min_length, &max_length);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5985
    if (like_error)				// Can't optimize with LIKE
5986
      goto end;
monty@mysql.com's avatar
monty@mysql.com committed
5987

5988
    if (offset != maybe_null)			// BLOB or VARCHAR
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5989 5990 5991 5992
    {
      int2store(min_str+maybe_null,min_length);
      int2store(max_str+maybe_null,max_length);
    }
5993 5994
    tree= new (alloc) SEL_ARG(field, min_str, max_str);
    goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5995 5996
  }

5997
  if (!optimize_range &&
5998
      type != Item_func::EQ_FUNC &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
5999
      type != Item_func::EQUAL_FUNC)
6000
    goto end;                                   // Can't optimize this
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6001

6002 6003 6004 6005
  /*
    We can't always use indexes when comparing a string index to a number
    cmp_type() is checked to allow compare of dates to numbers
  */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6006 6007 6008
  if (field->result_type() == STRING_RESULT &&
      value->result_type() != STRING_RESULT &&
      field->cmp_type() != value->result_type())
6009
    goto end;
6010
  /* For comparison purposes allow invalid dates like 2000-01-32 */
evgen@moonbone.local's avatar
evgen@moonbone.local committed
6011
  orig_sql_mode= field->table->in_use->variables.sql_mode;
6012
  if (value->real_item()->type() == Item::STRING_ITEM &&
6013 6014
      (field->type() == MYSQL_TYPE_DATE ||
       field->type() == MYSQL_TYPE_DATETIME))
6015
    field->table->in_use->variables.sql_mode|= MODE_INVALID_DATES;
6016
  err= value->save_in_field_no_warnings(field, 1);
6017
  if (err > 0)
6018
  {
6019
    if (field->cmp_type() != value->result_type())
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
6020
    {
6021 6022 6023 6024 6025 6026
      if ((type == Item_func::EQ_FUNC || type == Item_func::EQUAL_FUNC) &&
          value->result_type() == item_cmp_type(field->result_type(),
                                                value->result_type()))
      {
        tree= new (alloc) SEL_ARG(field, 0, 0);
        tree->type= SEL_ARG::IMPOSSIBLE;
6027
        field->table->in_use->variables.sql_mode= orig_sql_mode;
6028 6029 6030
        goto end;
      }
      else
6031 6032
      {
        /*
6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043
          TODO: We should return trees of the type SEL_ARG::IMPOSSIBLE
          for the cases like int_field > 999999999999999999999999 as well.
        */
        tree= 0;
        if (err == 3 && field->type() == FIELD_TYPE_DATE &&
            (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC ||
             type == Item_func::LT_FUNC || type == Item_func::LE_FUNC) )
        {
          /*
            We were saving DATETIME into a DATE column, the conversion went ok
            but a non-zero time part was cut off.
6044

6045 6046 6047
            In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
            values. Index over a DATE column uses DATE comparison. Changing 
            from one comparison to the other is possible:
6048

6049 6050
            datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
            datetime(date_col)<='2007-12-10 12:34:55' -> date_col<='2007-12-10'
6051

6052 6053
            datetime(date_col)> '2007-12-10 12:34:55' -> date_col>='2007-12-10'
            datetime(date_col)>='2007-12-10 12:34:55' -> date_col>='2007-12-10'
6054

6055 6056
            but we'll need to convert '>' to '>=' and '<' to '<='. This will
            be done together with other types at the end of this function
6057
            (grep for stored_field_cmp_to_item)
6058 6059 6060
          */
        }
        else
6061 6062
        {
          field->table->in_use->variables.sql_mode= orig_sql_mode;
6063
          goto end;
6064
        }
6065
      }
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
6066
    }
6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077

    /*
      guaranteed at this point:  err > 0; field and const of same type
      If an integer got bounded (e.g. to within 0..255 / -128..127)
      for < or >, set flags as for <= or >= (no NEAR_MAX / NEAR_MIN)
    */
    else if (err == 1 && field->result_type() == INT_RESULT)
    {
      if (type == Item_func::LT_FUNC && (value->val_int() > 0))
        type = Item_func::LE_FUNC;
      else if (type == Item_func::GT_FUNC &&
6078
               (field->type() != FIELD_TYPE_BIT) &&
6079 6080 6081 6082 6083 6084 6085
               !((Field_num*)field)->unsigned_flag &&
               !((Item_int*)value)->unsigned_flag &&
               (value->val_int() < 0))
        type = Item_func::GE_FUNC;
    }
  }
  else if (err < 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6086
  {
6087
    field->table->in_use->variables.sql_mode= orig_sql_mode;
6088
    /* This happens when we try to insert a NULL field in a not null column */
6089 6090
    tree= &null_element;                        // cmp with NULL is never TRUE
    goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6091
  }
6092
  field->table->in_use->variables.sql_mode= orig_sql_mode;
6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103

  /*
    Any sargable predicate except "<=>" involving NULL as a constant is always
    FALSE
  */
  if (type != Item_func::EQUAL_FUNC && field->is_real_null())
  {
    tree= &null_element;
    goto end;
  }
  
6104
  str= (uchar*) alloc_root(alloc, key_part->store_length+1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6105
  if (!str)
6106
    goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6107
  if (maybe_null)
6108 6109 6110
    *str= (uchar) field->is_real_null();        // Set to 1 if null
  field->get_key_image(str+maybe_null, key_part->length,
                       key_part->image_type);
6111 6112
  if (!(tree= new (alloc) SEL_ARG(field, str, str)))
    goto end;                                   // out of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6113

timour@mysql.com's avatar
timour@mysql.com committed
6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124
  /*
    Check if we are comparing an UNSIGNED integer with a negative constant.
    In this case we know that:
    (a) (unsigned_int [< | <=] negative_constant) == FALSE
    (b) (unsigned_int [> | >=] negative_constant) == TRUE
    In case (a) the condition is false for all values, and in case (b) it
    is true for all values, so we can avoid unnecessary retrieval and condition
    testing, and we also get correct comparison of unsinged integers with
    negative integers (which otherwise fails because at query execution time
    negative integers are cast to unsigned if compared with unsigned).
   */
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
6125 6126
  if (field->result_type() == INT_RESULT &&
      value->result_type() == INT_RESULT &&
6127 6128 6129
      ((field->type() == FIELD_TYPE_BIT || 
       ((Field_num *) field)->unsigned_flag) && 
       !((Item_int*) value)->unsigned_flag))
timour@mysql.com's avatar
timour@mysql.com committed
6130 6131 6132 6133 6134 6135 6136
  {
    longlong item_val= value->val_int();
    if (item_val < 0)
    {
      if (type == Item_func::LT_FUNC || type == Item_func::LE_FUNC)
      {
        tree->type= SEL_ARG::IMPOSSIBLE;
6137
        goto end;
timour@mysql.com's avatar
timour@mysql.com committed
6138 6139
      }
      if (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC)
6140 6141 6142 6143
      {
        tree= 0;
        goto end;
      }
timour@mysql.com's avatar
timour@mysql.com committed
6144 6145
    }
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6146 6147 6148

  switch (type) {
  case Item_func::LT_FUNC:
6149
    if (stored_field_cmp_to_item(param->thd, field, value) == 0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161
      tree->max_flag=NEAR_MAX;
    /* fall through */
  case Item_func::LE_FUNC:
    if (!maybe_null)
      tree->min_flag=NO_MIN_RANGE;		/* From start */
    else
    {						// > NULL
      tree->min_value=is_null_string;
      tree->min_flag=NEAR_MIN;
    }
    break;
  case Item_func::GT_FUNC:
6162
    /* Don't use open ranges for partial key_segments */
6163
    if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
6164
        (stored_field_cmp_to_item(param->thd, field, value) <= 0))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6165
      tree->min_flag=NEAR_MIN;
6166 6167
    tree->max_flag= NO_MAX_RANGE;
    break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6168
  case Item_func::GE_FUNC:
6169 6170
    /* Don't use open ranges for partial key_segments */
    if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
6171
        (stored_field_cmp_to_item(param->thd, field, value) < 0))
6172
      tree->min_flag= NEAR_MIN;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6173 6174
    tree->max_flag=NO_MAX_RANGE;
    break;
6175
  case Item_func::SP_EQUALS_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6176 6177 6178
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_EQUAL;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6179
  case Item_func::SP_DISJOINT_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6180 6181 6182
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_DISJOINT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6183
  case Item_func::SP_INTERSECTS_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6184 6185 6186
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6187
  case Item_func::SP_TOUCHES_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6188 6189 6190
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6191 6192

  case Item_func::SP_CROSSES_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6193 6194 6195
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6196
  case Item_func::SP_WITHIN_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6197 6198 6199
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_WITHIN;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6200 6201

  case Item_func::SP_CONTAINS_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6202 6203 6204
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_CONTAIN;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6205
  case Item_func::SP_OVERLAPS_FUNC:
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6206 6207 6208
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
6209

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6210 6211 6212
  default:
    break;
  }
6213 6214 6215

end:
  param->thd->mem_root= alloc;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6216 6217 6218 6219 6220 6221 6222 6223 6224
  DBUG_RETURN(tree);
}


/******************************************************************************
** Tree manipulation functions
** If tree is 0 it means that the condition can't be tested. It refers
** to a non existent table or to a field in current table with isn't a key.
** The different tree flags:
monty@mysql.com's avatar
monty@mysql.com committed
6225 6226
** IMPOSSIBLE:	 Condition is never TRUE
** ALWAYS:	 Condition is always TRUE
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6227 6228 6229 6230 6231 6232
** MAYBE:	 Condition may exists when tables are read
** MAYBE_KEY:	 Condition refers to a key that may be used in join loop
** KEY_RANGE:	 Condition uses a key
******************************************************************************/

/*
6233 6234
  Add a new key test to a key when scanning through all keys
  This will never be called for same key parts.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272
*/

static SEL_ARG *
sel_add(SEL_ARG *key1,SEL_ARG *key2)
{
  SEL_ARG *root,**key_link;

  if (!key1)
    return key2;
  if (!key2)
    return key1;

  key_link= &root;
  while (key1 && key2)
  {
    if (key1->part < key2->part)
    {
      *key_link= key1;
      key_link= &key1->next_key_part;
      key1=key1->next_key_part;
    }
    else
    {
      *key_link= key2;
      key_link= &key2->next_key_part;
      key2=key2->next_key_part;
    }
  }
  *key_link=key1 ? key1 : key2;
  return root;
}

#define CLONE_KEY1_MAYBE 1
#define CLONE_KEY2_MAYBE 2
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)


static SEL_TREE *
6273
tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294
{
  DBUG_ENTER("tree_and");
  if (!tree1)
    DBUG_RETURN(tree2);
  if (!tree2)
    DBUG_RETURN(tree1);
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree1);
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree2);
  if (tree1->type == SEL_TREE::MAYBE)
  {
    if (tree2->type == SEL_TREE::KEY)
      tree2->type=SEL_TREE::KEY_SMALLER;
    DBUG_RETURN(tree2);
  }
  if (tree2->type == SEL_TREE::MAYBE)
  {
    tree1->type=SEL_TREE::KEY_SMALLER;
    DBUG_RETURN(tree1);
  }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6295 6296
  key_map  result_keys;
  result_keys.clear_all();
6297
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309
  /* Join the trees key per key */
  SEL_ARG **key1,**key2,**end;
  for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
       key1 != end ; key1++,key2++)
  {
    uint flag=0;
    if (*key1 || *key2)
    {
      if (*key1 && !(*key1)->simple_key())
	flag|=CLONE_KEY1_MAYBE;
      if (*key2 && !(*key2)->simple_key())
	flag|=CLONE_KEY2_MAYBE;
6310
      *key1=key_and(param, *key1, *key2, flag);
6311
      if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6312 6313
      {
	tree1->type= SEL_TREE::IMPOSSIBLE;
6314
        DBUG_RETURN(tree1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6315
      }
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6316
      result_keys.set_bit(key1 - tree1->keys);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6317
#ifdef EXTRA_DEBUG
6318 6319
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
          (*key1)->test_use_count(*key1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6320 6321 6322
#endif
    }
  }
6323 6324
  tree1->keys_map= result_keys;
  /* dispose index_merge if there is a "range" option */
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6325
  if (!result_keys.is_clear_all())
6326 6327 6328 6329 6330 6331 6332
  {
    tree1->merges.empty();
    DBUG_RETURN(tree1);
  }

  /* ok, both trees are index_merge trees */
  imerge_list_and_list(&tree1->merges, &tree2->merges);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6333 6334 6335 6336
  DBUG_RETURN(tree1);
}


6337
/*
6338 6339
  Check if two SEL_TREES can be combined into one (i.e. a single key range
  read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
6340
  using index_merge.
6341 6342
*/

6343 6344
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, 
                           RANGE_OPT_PARAM* param)
6345
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6346
  key_map common_keys= tree1->keys_map;
6347
  DBUG_ENTER("sel_trees_can_be_ored");
6348
  common_keys.intersect(tree2->keys_map);
6349

sergefp@mysql.com's avatar
sergefp@mysql.com committed
6350
  if (common_keys.is_clear_all())
monty@mysql.com's avatar
monty@mysql.com committed
6351
    DBUG_RETURN(FALSE);
6352 6353

  /* trees have a common key, check if they refer to same key part */
6354
  SEL_ARG **key1,**key2;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6355
  for (uint key_no=0; key_no < param->keys; key_no++)
6356
  {
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6357
    if (common_keys.is_set(key_no))
6358 6359 6360 6361 6362
    {
      key1= tree1->keys + key_no;
      key2= tree2->keys + key_no;
      if ((*key1)->part == (*key2)->part)
      {
monty@mysql.com's avatar
monty@mysql.com committed
6363
        DBUG_RETURN(TRUE);
6364 6365 6366
      }
    }
  }
monty@mysql.com's avatar
monty@mysql.com committed
6367
  DBUG_RETURN(FALSE);
6368
}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6369

6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433

/*
  Remove the trees that are not suitable for record retrieval.
  SYNOPSIS
    param  Range analysis parameter
    tree   Tree to be processed, tree->type is KEY or KEY_SMALLER
 
  DESCRIPTION
    This function walks through tree->keys[] and removes the SEL_ARG* trees
    that are not "maybe" trees (*) and cannot be used to construct quick range
    selects.
    (*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
          these types here as well.

    A SEL_ARG* tree cannot be used to construct quick select if it has
    tree->part != 0. (e.g. it could represent "keypart2 < const").

    WHY THIS FUNCTION IS NEEDED
    
    Normally we allow construction of SEL_TREE objects that have SEL_ARG
    trees that do not allow quick range select construction. For example for
    " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
    tree1= SEL_TREE { SEL_ARG{keypart1=1} }
    tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
                                               from this
    call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
                                   tree.
    
    There is an exception though: when we construct index_merge SEL_TREE,
    any SEL_ARG* tree that cannot be used to construct quick range select can
    be removed, because current range analysis code doesn't provide any way
    that tree could be later combined with another tree.
    Consider an example: we should not construct
    st1 = SEL_TREE { 
      merges = SEL_IMERGE { 
                            SEL_TREE(t.key1part1 = 1), 
                            SEL_TREE(t.key2part2 = 2)   -- (*)
                          } 
                   };
    because 
     - (*) cannot be used to construct quick range select, 
     - There is no execution path that would cause (*) to be converted to 
       a tree that could be used.

    The latter is easy to verify: first, notice that the only way to convert
    (*) into a usable tree is to call tree_and(something, (*)).

    Second look at what tree_and/tree_or function would do when passed a
    SEL_TREE that has the structure like st1 tree has, and conlcude that 
    tree_and(something, (*)) will not be called.

  RETURN
    0  Ok, some suitable trees left
    1  No tree->keys[] left.
*/

static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
{
  bool res= FALSE;
  for (uint i=0; i < param->keys; i++)
  {
    if (tree->keys[i])
    {
      if (tree->keys[i]->part)
6434
      {
6435
        tree->keys[i]= NULL;
6436 6437
        tree->keys_map.clear_bit(i);
      }
6438 6439 6440 6441
      else
        res= TRUE;
    }
  }
6442
  return !res;
6443 6444 6445
}


bk@work.mysql.com's avatar
bk@work.mysql.com committed
6446
static SEL_TREE *
6447
tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460
{
  DBUG_ENTER("tree_or");
  if (!tree1 || !tree2)
    DBUG_RETURN(0);
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree2);
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
    DBUG_RETURN(tree1);
  if (tree1->type == SEL_TREE::MAYBE)
    DBUG_RETURN(tree1);				// Can't use this
  if (tree2->type == SEL_TREE::MAYBE)
    DBUG_RETURN(tree2);

6461
  SEL_TREE *result= 0;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6462 6463
  key_map  result_keys;
  result_keys.clear_all();
6464
  if (sel_trees_can_be_ored(tree1, tree2, param))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6465
  {
6466 6467 6468 6469
    /* Join the trees key per key */
    SEL_ARG **key1,**key2,**end;
    for (key1= tree1->keys,key2= tree2->keys,end= key1+param->keys ;
         key1 != end ; key1++,key2++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6470
    {
6471
      *key1=key_or(param, *key1, *key2);
6472 6473 6474
      if (*key1)
      {
        result=tree1;				// Added to tree1
sergefp@mysql.com's avatar
sergefp@mysql.com committed
6475
        result_keys.set_bit(key1 - tree1->keys);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6476
#ifdef EXTRA_DEBUG
6477 6478
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
          (*key1)->test_use_count(*key1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6479
#endif
6480 6481 6482 6483 6484 6485 6486 6487 6488 6489
      }
    }
    if (result)
      result->keys_map= result_keys;
  }
  else
  {
    /* ok, two trees have KEY type but cannot be used without index merge */
    if (tree1->merges.is_empty() && tree2->merges.is_empty())
    {
6490 6491 6492 6493 6494 6495 6496
      if (param->remove_jump_scans)
      {
        bool no_trees= remove_nonrange_trees(param, tree1);
        no_trees= no_trees || remove_nonrange_trees(param, tree2);
        if (no_trees)
          DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
      }
6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517
      SEL_IMERGE *merge;
      /* both trees are "range" trees, produce new index merge structure */
      if (!(result= new SEL_TREE()) || !(merge= new SEL_IMERGE()) ||
          (result->merges.push_back(merge)) ||
          (merge->or_sel_tree(param, tree1)) ||
          (merge->or_sel_tree(param, tree2)))
        result= NULL;
      else
        result->type= tree1->type;
    }
    else if (!tree1->merges.is_empty() && !tree2->merges.is_empty())
    {
      if (imerge_list_or_list(param, &tree1->merges, &tree2->merges))
        result= new SEL_TREE(SEL_TREE::ALWAYS);
      else
        result= tree1;
    }
    else
    {
      /* one tree is index merge tree and another is range tree */
      if (tree1->merges.is_empty())
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
6518
        swap_variables(SEL_TREE*, tree1, tree2);
6519 6520 6521
      
      if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
         DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
6522 6523 6524 6525 6526
      /* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
      if (imerge_list_or_tree(param, &tree1->merges, tree2))
        result= new SEL_TREE(SEL_TREE::ALWAYS);
      else
        result= tree1;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6527 6528 6529 6530 6531 6532 6533 6534 6535
    }
  }
  DBUG_RETURN(result);
}


/* And key trees where key1->part < key2 -> part */

static SEL_ARG *
6536 6537
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, 
             uint clone_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6538 6539 6540 6541 6542 6543
{
  SEL_ARG *next;
  ulong use_count=key1->use_count;

  if (key1->elements != 1)
  {
6544
    key2->use_count+=key1->elements-1; //psergey: why we don't count that key1 has n-k-p?
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6545 6546 6547 6548
    key2->increment_use_count((int) key1->elements-1);
  }
  if (key1->type == SEL_ARG::MAYBE_KEY)
  {
6549 6550
    key1->right= key1->left= &null_element;
    key1->next= key1->prev= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6551 6552 6553 6554 6555
  }
  for (next=key1->first(); next ; next=next->next)
  {
    if (next->next_key_part)
    {
6556
      SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6557 6558 6559 6560 6561 6562 6563 6564
      if (tmp && tmp->type == SEL_ARG::IMPOSSIBLE)
      {
	key1=key1->tree_delete(next);
	continue;
      }
      next->next_key_part=tmp;
      if (use_count)
	next->increment_use_count(use_count);
6565 6566
      if (param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
        break;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577
    }
    else
      next->next_key_part=key2;
  }
  if (!key1)
    return &null_element;			// Impossible ranges
  key1->use_count++;
  return key1;
}


6578 6579 6580 6581 6582
/*
  Produce a SEL_ARG graph that represents "key1 AND key2"

  SYNOPSIS
    key_and()
6583 6584 6585 6586
      param   Range analysis context (needed to track if we have allocated
              too many SEL_ARGs)
      key1    First argument, root of its RB-tree
      key2    Second argument, root of its RB-tree
6587 6588 6589 6590 6591 6592

  RETURN
    RB-tree root of the resulting SEL_ARG graph.
    NULL if the result of AND operation is an empty interval {0}.
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6593
static SEL_ARG *
6594
key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6595 6596 6597 6598 6599 6600 6601 6602 6603
{
  if (!key1)
    return key2;
  if (!key2)
    return key1;
  if (key1->part != key2->part)
  {
    if (key1->part > key2->part)
    {
6604
      swap_variables(SEL_ARG *, key1, key2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6605 6606 6607 6608 6609
      clone_flag=swap_clone_flag(clone_flag);
    }
    // key1->part < key2->part
    key1->use_count--;
    if (key1->use_count > 0)
6610
      if (!(key1= key1->clone_tree(param)))
6611
	return 0;				// OOM
6612
    return and_all_keys(param, key1, key2, clone_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6613 6614 6615
  }

  if (((clone_flag & CLONE_KEY2_MAYBE) &&
6616 6617
       !(clone_flag & CLONE_KEY1_MAYBE) &&
       key2->type != SEL_ARG::MAYBE_KEY) ||
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6618 6619
      key1->type == SEL_ARG::MAYBE_KEY)
  {						// Put simple key in key2
6620
    swap_variables(SEL_ARG *, key1, key2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6621 6622 6623
    clone_flag=swap_clone_flag(clone_flag);
  }

6624
  /* If one of the key is MAYBE_KEY then the found region may be smaller */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6625 6626 6627 6628 6629
  if (key2->type == SEL_ARG::MAYBE_KEY)
  {
    if (key1->use_count > 1)
    {
      key1->use_count--;
6630
      if (!(key1=key1->clone_tree(param)))
6631
	return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6632 6633 6634 6635
      key1->use_count++;
    }
    if (key1->type == SEL_ARG::MAYBE_KEY)
    {						// Both are maybe key
6636 6637
      key1->next_key_part=key_and(param, key1->next_key_part, 
                                  key2->next_key_part, clone_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6638 6639 6640 6641 6642 6643 6644 6645
      if (key1->next_key_part &&
	  key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
	return key1;
    }
    else
    {
      key1->maybe_smaller();
      if (key2->next_key_part)
6646 6647
      {
	key1->use_count--;			// Incremented in and_all_keys
6648
	return and_all_keys(param, key1, key2, clone_flag);
6649
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6650 6651 6652 6653 6654
      key2->use_count--;			// Key2 doesn't have a tree
    }
    return key1;
  }

6655 6656
  if ((key1->min_flag | key2->min_flag) & GEOM_FLAG)
  {
6657
    /* TODO: why not leave one of the trees? */
6658
    key1->free_tree();
6659 6660 6661 6662
    key2->free_tree();
    return 0;					// Can't optimize this
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676
  key1->use_count--;
  key2->use_count--;
  SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;

  while (e1 && e2)
  {
    int cmp=e1->cmp_min_to_min(e2);
    if (cmp < 0)
    {
      if (get_range(&e1,&e2,key1))
	continue;
    }
    else if (get_range(&e2,&e1,key2))
      continue;
6677 6678
    SEL_ARG *next=key_and(param, e1->next_key_part, e2->next_key_part,
                          clone_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6679 6680 6681 6682 6683
    e1->increment_use_count(1);
    e2->increment_use_count(1);
    if (!next || next->type != SEL_ARG::IMPOSSIBLE)
    {
      SEL_ARG *new_arg= e1->clone_and(e2);
6684 6685
      if (!new_arg)
	return &null_element;			// End of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724
      new_arg->next_key_part=next;
      if (!new_tree)
      {
	new_tree=new_arg;
      }
      else
	new_tree=new_tree->insert(new_arg);
    }
    if (e1->cmp_max_to_max(e2) < 0)
      e1=e1->next;				// e1 can't overlapp next e2
    else
      e2=e2->next;
  }
  key1->free_tree();
  key2->free_tree();
  if (!new_tree)
    return &null_element;			// Impossible range
  return new_tree;
}


static bool
get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1)
{
  (*e1)=root1->find_range(*e2);			// first e1->min < e2->min
  if ((*e1)->cmp_max_to_min(*e2) < 0)
  {
    if (!((*e1)=(*e1)->next))
      return 1;
    if ((*e1)->cmp_min_to_max(*e2) > 0)
    {
      (*e2)=(*e2)->next;
      return 1;
    }
  }
  return 0;
}


6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781
/**
   Combine two range expression under a common OR. On a logical level, the
   transformation is key_or( expr1, expr2 ) => expr1 OR expr2.

   Both expressions are assumed to be in the SEL_ARG format. In a logic sense,
   theformat is reminiscent of DNF, since an expression such as the following

   ( 1 < kp1 < 10 AND p1 ) OR ( 10 <= kp2 < 20 AND p2 )

   where there is a key consisting of keyparts ( kp1, kp2, ..., kpn ) and p1
   and p2 are valid SEL_ARG expressions over keyparts kp2 ... kpn, is a valid
   SEL_ARG condition. The disjuncts appear ordered by the minimum endpoint of
   the first range and ranges must not overlap. It follows that they are also
   ordered by maximum endpoints. Thus

   ( 1 < kp1 <= 2 AND ( kp2 = 2 OR kp2 = 3 ) ) OR kp1 = 3

   Is a a valid SER_ARG expression for a key of at least 2 keyparts.
   
   For simplicity, we will assume that expr2 is a single range predicate,
   i.e. on the form ( a < x < b AND ... ). It is easy to generalize to a
   disjunction of several predicates by subsequently call key_or for each
   disjunct.

   The algorithm iterates over each disjunct of expr1, and for each disjunct
   where the first keypart's range overlaps with the first keypart's range in
   expr2:
   
   If the predicates are equal for the rest of the keyparts, or if there are
   no more, the range in expr2 has its endpoints copied in, and the SEL_ARG
   node in expr2 is deallocated. If more ranges became connected in expr1, the
   surplus is also dealocated. If they differ, two ranges are created.
   
   - The range leading up to the overlap. Empty if endpoints are equal.

   - The overlapping sub-range. May be the entire range if they are equal.

   Finally, there may be one more range if expr2's first keypart's range has a
   greater maximum endpoint than the last range in expr1.

   For the overlapping sub-range, we recursively call key_or. Thus in order to
   compute key_or of

     (1) ( 1 < kp1 < 10 AND 1 < kp2 < 10 ) 

     (2) ( 2 < kp1 < 20 AND 4 < kp2 < 20 )

   We create the ranges 1 < kp <= 2, 2 < kp1 < 10, 10 <= kp1 < 20. For the
   first one, we simply hook on the condition for the second keypart from (1)
   : 1 < kp2 < 10. For the second range 2 < kp1 < 10, key_or( 1 < kp2 < 10, 4
   < kp2 < 20 ) is called, yielding 1 < kp2 < 20. For the last range, we reuse
   the range 4 < kp2 < 20 from (2) for the second keypart. The result is thus
   
   ( 1  <  kp1 <= 2 AND 1 < kp2 < 10 ) OR
   ( 2  <  kp1 < 10 AND 1 < kp2 < 20 ) OR
   ( 10 <= kp1 < 20 AND 4 < kp2 < 20 )
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6782
static SEL_ARG *
6783
key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6784 6785 6786 6787 6788 6789 6790 6791 6792 6793
{
  if (!key1)
  {
    if (key2)
    {
      key2->use_count--;
      key2->free_tree();
    }
    return 0;
  }
6794
  if (!key2)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6795 6796 6797 6798 6799 6800 6801 6802
  {
    key1->use_count--;
    key1->free_tree();
    return 0;
  }
  key1->use_count--;
  key2->use_count--;

6803 6804
  if (key1->part != key2->part || 
      (key1->min_flag | key2->min_flag) & GEOM_FLAG)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828
  {
    key1->free_tree();
    key2->free_tree();
    return 0;					// Can't optimize this
  }

  // If one of the key is MAYBE_KEY then the found region may be bigger
  if (key1->type == SEL_ARG::MAYBE_KEY)
  {
    key2->free_tree();
    key1->use_count++;
    return key1;
  }
  if (key2->type == SEL_ARG::MAYBE_KEY)
  {
    key1->free_tree();
    key2->use_count++;
    return key2;
  }

  if (key1->use_count > 0)
  {
    if (key2->use_count == 0 || key1->elements > key2->elements)
    {
6829
      swap_variables(SEL_ARG *,key1,key2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6830
    }
6831
    if (key1->use_count > 0 || !(key1=key1->clone_tree(param)))
6832
      return 0;					// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851
  }

  // Add tree at key2 to tree at key1
  bool key2_shared=key2->use_count != 0;
  key1->maybe_flag|=key2->maybe_flag;

  for (key2=key2->first(); key2; )
  {
    SEL_ARG *tmp=key1->find_range(key2);	// Find key1.min <= key2.min
    int cmp;

    if (!tmp)
    {
      tmp=key1->first();			// tmp.min > key2.min
      cmp= -1;
    }
    else if ((cmp=tmp->cmp_max_to_min(key2)) < 0)
    {						// Found tmp.max < key2.min
      SEL_ARG *next=tmp->next;
6852
      /* key1 on the left of key2 non-overlapping */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6853 6854 6855 6856 6857 6858
      if (cmp == -2 && eq_tree(tmp->next_key_part,key2->next_key_part))
      {
	// Join near ranges like tmp.max < 0 and key2.min >= 0
	SEL_ARG *key2_next=key2->next;
	if (key2_shared)
	{
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
6859
	  if (!(key2=new SEL_ARG(*key2)))
6860
	    return 0;		// out of memory
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880
	  key2->increment_use_count(key1->use_count+1);
	  key2->next=key2_next;			// New copy of key2
	}
	key2->copy_min(tmp);
	if (!(key1=key1->tree_delete(tmp)))
	{					// Only one key in tree
	  key1=key2;
	  key1->make_root();
	  key2=key2_next;
	  break;
	}
      }
      if (!(tmp=next))				// tmp.min > key2.min
	break;					// Copy rest of key2
    }
    if (cmp < 0)
    {						// tmp.min > key2.min
      int tmp_cmp;
      if ((tmp_cmp=tmp->cmp_min_to_max(key2)) > 0) // if tmp.min > key2.max
      {
6881
        /* tmp is on the right of key2 non-overlapping */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901
	if (tmp_cmp == 2 && eq_tree(tmp->next_key_part,key2->next_key_part))
	{					// ranges are connected
	  tmp->copy_min_to_min(key2);
	  key1->merge_flags(key2);
	  if (tmp->min_flag & NO_MIN_RANGE &&
	      tmp->max_flag & NO_MAX_RANGE)
	  {
	    if (key1->maybe_flag)
	      return new SEL_ARG(SEL_ARG::MAYBE_KEY);
	    return 0;
	  }
	  key2->increment_use_count(-1);	// Free not used tree
	  key2=key2->next;
	  continue;
	}
	else
	{
	  SEL_ARG *next=key2->next;		// Keys are not overlapping
	  if (key2_shared)
	  {
6902 6903
	    SEL_ARG *cpy= new SEL_ARG(*key2);	// Must make copy
	    if (!cpy)
6904
	      return 0;				// OOM
6905
	    key1=key1->insert(cpy);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6906 6907 6908 6909 6910 6911 6912 6913 6914 6915
	    key2->increment_use_count(key1->use_count+1);
	  }
	  else
	    key1=key1->insert(key2);		// Will destroy key2_root
	  key2=next;
	  continue;
	}
      }
    }

6916 6917 6918 6919
    /* 
      tmp.min >= key2.min && tmp.min <= key.max  (overlapping ranges)
      key2.min <= tmp.min <= key2.max 
    */  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6920 6921 6922 6923
    if (eq_tree(tmp->next_key_part,key2->next_key_part))
    {
      if (tmp->is_same(key2))
      {
6924 6925 6926 6927
        /* 
          Found exact match of key2 inside key1. 
          Use the relevant range in key1.
        */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6928 6929 6930 6931 6932 6933
	tmp->merge_flags(key2);			// Copy maybe flags
	key2->increment_use_count(-1);		// Free not used tree
      }
      else
      {
	SEL_ARG *last=tmp;
6934 6935 6936 6937 6938
        SEL_ARG *first=tmp;
        /* 
          Find the last range in tmp that overlaps key2 and has the same 
          condition on the rest of the keyparts.
        */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6939 6940 6941
	while (last->next && last->next->cmp_min_to_max(key2) <= 0 &&
	       eq_tree(last->next->next_key_part,key2->next_key_part))
	{
6942 6943 6944 6945 6946 6947 6948 6949 6950
          /*
            We've found the last overlapping key1 range in last.
            This means that the ranges between (and including) the 
            first overlapping range (tmp) and the last overlapping range
            (last) are fully nested into the current range of key2 
            and can safely be discarded. We just need the minimum endpoint
            of the first overlapping range (tmp) so we can compare it with
            the minimum endpoint of the enclosing key2 range.
          */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6951 6952 6953 6954
	  SEL_ARG *save=last;
	  last=last->next;
	  key1=key1->tree_delete(save);
	}
6955 6956 6957 6958 6959 6960 6961
        /*
          The tmp range (the first overlapping range) could have been discarded
          by the previous loop. We should re-direct tmp to the new united range 
          that's taking its place.
        */
        tmp= last;
        last->copy_min(first);
6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976
        bool full_range= last->copy_min(key2);
        if (!full_range)
        {
          if (last->next && key2->cmp_max_to_min(last->next) >= 0)
          {
            last->max_value= last->next->min_value;
            if (last->next->min_flag & NEAR_MIN)
              last->max_flag&= ~NEAR_MAX;
            else
              last->max_flag|= NEAR_MAX;
          }
          else
            full_range= last->copy_max(key2);
        }
	if (full_range)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
	{					// Full range
	  key1->free_tree();
	  for (; key2 ; key2=key2->next)
	    key2->increment_use_count(-1);	// Free not used tree
	  if (key1->maybe_flag)
	    return new SEL_ARG(SEL_ARG::MAYBE_KEY);
	  return 0;
	}
      }
    }

    if (cmp >= 0 && tmp->cmp_min_to_min(key2) < 0)
    {						// tmp.min <= x < key2.min
      SEL_ARG *new_arg=tmp->clone_first(key2);
6991 6992
      if (!new_arg)
	return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005
      if ((new_arg->next_key_part= key1->next_key_part))
	new_arg->increment_use_count(key1->use_count+1);
      tmp->copy_min_to_min(key2);
      key1=key1->insert(new_arg);
    }

    // tmp.min >= key2.min && tmp.min <= key2.max
    SEL_ARG key(*key2);				// Get copy we can modify
    for (;;)
    {
      if (tmp->cmp_min_to_min(&key) > 0)
      {						// key.min <= x < tmp.min
	SEL_ARG *new_arg=key.clone_first(tmp);
7006 7007
	if (!new_arg)
	  return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7008 7009 7010 7011 7012 7013 7014 7015
	if ((new_arg->next_key_part=key.next_key_part))
	  new_arg->increment_use_count(key1->use_count+1);
	key1=key1->insert(new_arg);
      }
      if ((cmp=tmp->cmp_max_to_max(&key)) <= 0)
      {						// tmp.min. <= x <= tmp.max
	tmp->maybe_flag|= key.maybe_flag;
	key.increment_use_count(key1->use_count+1);
7016
	tmp->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7017 7018 7019 7020 7021
	if (!cmp)				// Key2 is ready
	  break;
	key.copy_max_to_min(tmp);
	if (!(tmp=tmp->next))
	{
7022 7023 7024 7025
	  SEL_ARG *tmp2= new SEL_ARG(key);
	  if (!tmp2)
	    return 0;				// OOM
	  key1=key1->insert(tmp2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7026 7027 7028 7029 7030
	  key2=key2->next;
	  goto end;
	}
	if (tmp->cmp_min_to_max(&key) > 0)
	{
7031 7032 7033 7034
	  SEL_ARG *tmp2= new SEL_ARG(key);
	  if (!tmp2)
	    return 0;				// OOM
	  key1=key1->insert(tmp2);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7035 7036 7037 7038 7039 7040
	  break;
	}
      }
      else
      {
	SEL_ARG *new_arg=tmp->clone_last(&key); // tmp.min <= x <= key.max
7041 7042
	if (!new_arg)
	  return 0;				// OOM
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7043 7044
	tmp->copy_max_to_min(&key);
	tmp->increment_use_count(key1->use_count+1);
7045 7046
	/* Increment key count as it may be used for next loop */
	key.increment_use_count(1);
7047
	new_arg->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060
	key1=key1->insert(new_arg);
	break;
      }
    }
    key2=key2->next;
  }

end:
  while (key2)
  {
    SEL_ARG *next=key2->next;
    if (key2_shared)
    {
7061 7062 7063
      SEL_ARG *tmp=new SEL_ARG(*key2);		// Must make copy
      if (!tmp)
	return 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7064
      key2->increment_use_count(key1->use_count+1);
7065
      key1=key1->insert(tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110
    }
    else
      key1=key1->insert(key2);			// Will destroy key2_root
    key2=next;
  }
  key1->use_count++;
  return key1;
}


/* Compare if two trees are equal */

static bool eq_tree(SEL_ARG* a,SEL_ARG *b)
{
  if (a == b)
    return 1;
  if (!a || !b || !a->is_same(b))
    return 0;
  if (a->left != &null_element && b->left != &null_element)
  {
    if (!eq_tree(a->left,b->left))
      return 0;
  }
  else if (a->left != &null_element || b->left != &null_element)
    return 0;
  if (a->right != &null_element && b->right != &null_element)
  {
    if (!eq_tree(a->right,b->right))
      return 0;
  }
  else if (a->right != &null_element || b->right != &null_element)
    return 0;
  if (a->next_key_part != b->next_key_part)
  {						// Sub range
    if (!a->next_key_part != !b->next_key_part ||
	!eq_tree(a->next_key_part, b->next_key_part))
      return 0;
  }
  return 1;
}


SEL_ARG *
SEL_ARG::insert(SEL_ARG *key)
{
7111
  SEL_ARG *element,**UNINIT_VAR(par),*UNINIT_VAR(last_element);
7112

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179
  for (element= this; element != &null_element ; )
  {
    last_element=element;
    if (key->cmp_min_to_min(element) > 0)
    {
      par= &element->right; element= element->right;
    }
    else
    {
      par = &element->left; element= element->left;
    }
  }
  *par=key;
  key->parent=last_element;
	/* Link in list */
  if (par == &last_element->left)
  {
    key->next=last_element;
    if ((key->prev=last_element->prev))
      key->prev->next=key;
    last_element->prev=key;
  }
  else
  {
    if ((key->next=last_element->next))
      key->next->prev=key;
    key->prev=last_element;
    last_element->next=key;
  }
  key->left=key->right= &null_element;
  SEL_ARG *root=rb_insert(key);			// rebalance tree
  root->use_count=this->use_count;		// copy root info
  root->elements= this->elements+1;
  root->maybe_flag=this->maybe_flag;
  return root;
}


/*
** Find best key with min <= given key
** Because the call context this should never return 0 to get_range
*/

SEL_ARG *
SEL_ARG::find_range(SEL_ARG *key)
{
  SEL_ARG *element=this,*found=0;

  for (;;)
  {
    if (element == &null_element)
      return found;
    int cmp=element->cmp_min_to_min(key);
    if (cmp == 0)
      return element;
    if (cmp < 0)
    {
      found=element;
      element=element->right;
    }
    else
      element=element->left;
  }
}


/*
7180 7181 7182 7183 7184
  Remove a element from the tree

  SYNOPSIS
    tree_delete()
    key		Key that is to be deleted from tree (this)
7185

7186 7187 7188 7189 7190
  NOTE
    This also frees all sub trees that is used by the element

  RETURN
    root of new tree (with key deleted)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7191 7192 7193 7194 7195 7196 7197
*/

SEL_ARG *
SEL_ARG::tree_delete(SEL_ARG *key)
{
  enum leaf_color remove_color;
  SEL_ARG *root,*nod,**par,*fix_par;
7198 7199 7200 7201
  DBUG_ENTER("tree_delete");

  root=this;
  this->parent= 0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247

  /* Unlink from list */
  if (key->prev)
    key->prev->next=key->next;
  if (key->next)
    key->next->prev=key->prev;
  key->increment_use_count(-1);
  if (!key->parent)
    par= &root;
  else
    par=key->parent_ptr();

  if (key->left == &null_element)
  {
    *par=nod=key->right;
    fix_par=key->parent;
    if (nod != &null_element)
      nod->parent=fix_par;
    remove_color= key->color;
  }
  else if (key->right == &null_element)
  {
    *par= nod=key->left;
    nod->parent=fix_par=key->parent;
    remove_color= key->color;
  }
  else
  {
    SEL_ARG *tmp=key->next;			// next bigger key (exist!)
    nod= *tmp->parent_ptr()= tmp->right;	// unlink tmp from tree
    fix_par=tmp->parent;
    if (nod != &null_element)
      nod->parent=fix_par;
    remove_color= tmp->color;

    tmp->parent=key->parent;			// Move node in place of key
    (tmp->left=key->left)->parent=tmp;
    if ((tmp->right=key->right) != &null_element)
      tmp->right->parent=tmp;
    tmp->color=key->color;
    *par=tmp;
    if (fix_par == key)				// key->right == key->next
      fix_par=tmp;				// new parent of nod
  }

  if (root == &null_element)
7248
    DBUG_RETURN(0);				// Maybe root later
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7249 7250 7251 7252 7253 7254 7255
  if (remove_color == BLACK)
    root=rb_delete_fixup(root,nod,fix_par);
  test_rb_tree(root,root->parent);

  root->use_count=this->use_count;		// Fix root counters
  root->elements=this->elements-1;
  root->maybe_flag=this->maybe_flag;
7256
  DBUG_RETURN(root);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431
}


	/* Functions to fix up the tree after insert and delete */

static void left_rotate(SEL_ARG **root,SEL_ARG *leaf)
{
  SEL_ARG *y=leaf->right;
  leaf->right=y->left;
  if (y->left != &null_element)
    y->left->parent=leaf;
  if (!(y->parent=leaf->parent))
    *root=y;
  else
    *leaf->parent_ptr()=y;
  y->left=leaf;
  leaf->parent=y;
}

static void right_rotate(SEL_ARG **root,SEL_ARG *leaf)
{
  SEL_ARG *y=leaf->left;
  leaf->left=y->right;
  if (y->right != &null_element)
    y->right->parent=leaf;
  if (!(y->parent=leaf->parent))
    *root=y;
  else
    *leaf->parent_ptr()=y;
  y->right=leaf;
  leaf->parent=y;
}


SEL_ARG *
SEL_ARG::rb_insert(SEL_ARG *leaf)
{
  SEL_ARG *y,*par,*par2,*root;
  root= this; root->parent= 0;

  leaf->color=RED;
  while (leaf != root && (par= leaf->parent)->color == RED)
  {					// This can't be root or 1 level under
    if (par == (par2= leaf->parent->parent)->left)
    {
      y= par2->right;
      if (y->color == RED)
      {
	par->color=BLACK;
	y->color=BLACK;
	leaf=par2;
	leaf->color=RED;		/* And the loop continues */
      }
      else
      {
	if (leaf == par->right)
	{
	  left_rotate(&root,leaf->parent);
	  par=leaf;			/* leaf is now parent to old leaf */
	}
	par->color=BLACK;
	par2->color=RED;
	right_rotate(&root,par2);
	break;
      }
    }
    else
    {
      y= par2->left;
      if (y->color == RED)
      {
	par->color=BLACK;
	y->color=BLACK;
	leaf=par2;
	leaf->color=RED;		/* And the loop continues */
      }
      else
      {
	if (leaf == par->left)
	{
	  right_rotate(&root,par);
	  par=leaf;
	}
	par->color=BLACK;
	par2->color=RED;
	left_rotate(&root,par2);
	break;
      }
    }
  }
  root->color=BLACK;
  test_rb_tree(root,root->parent);
  return root;
}


SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key,SEL_ARG *par)
{
  SEL_ARG *x,*w;
  root->parent=0;

  x= key;
  while (x != root && x->color == SEL_ARG::BLACK)
  {
    if (x == par->left)
    {
      w=par->right;
      if (w->color == SEL_ARG::RED)
      {
	w->color=SEL_ARG::BLACK;
	par->color=SEL_ARG::RED;
	left_rotate(&root,par);
	w=par->right;
      }
      if (w->left->color == SEL_ARG::BLACK && w->right->color == SEL_ARG::BLACK)
      {
	w->color=SEL_ARG::RED;
	x=par;
      }
      else
      {
	if (w->right->color == SEL_ARG::BLACK)
	{
	  w->left->color=SEL_ARG::BLACK;
	  w->color=SEL_ARG::RED;
	  right_rotate(&root,w);
	  w=par->right;
	}
	w->color=par->color;
	par->color=SEL_ARG::BLACK;
	w->right->color=SEL_ARG::BLACK;
	left_rotate(&root,par);
	x=root;
	break;
      }
    }
    else
    {
      w=par->left;
      if (w->color == SEL_ARG::RED)
      {
	w->color=SEL_ARG::BLACK;
	par->color=SEL_ARG::RED;
	right_rotate(&root,par);
	w=par->left;
      }
      if (w->right->color == SEL_ARG::BLACK && w->left->color == SEL_ARG::BLACK)
      {
	w->color=SEL_ARG::RED;
	x=par;
      }
      else
      {
	if (w->left->color == SEL_ARG::BLACK)
	{
	  w->right->color=SEL_ARG::BLACK;
	  w->color=SEL_ARG::RED;
	  left_rotate(&root,w);
	  w=par->left;
	}
	w->color=par->color;
	par->color=SEL_ARG::BLACK;
	w->left->color=SEL_ARG::BLACK;
	right_rotate(&root,par);
	x=root;
	break;
      }
    }
    par=x->parent;
  }
  x->color=SEL_ARG::BLACK;
  return root;
}


7432
	/* Test that the properties for a red-black tree hold */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469

#ifdef EXTRA_DEBUG
int test_rb_tree(SEL_ARG *element,SEL_ARG *parent)
{
  int count_l,count_r;

  if (element == &null_element)
    return 0;					// Found end of tree
  if (element->parent != parent)
  {
    sql_print_error("Wrong tree: Parent doesn't point at parent");
    return -1;
  }
  if (element->color == SEL_ARG::RED &&
      (element->left->color == SEL_ARG::RED ||
       element->right->color == SEL_ARG::RED))
  {
    sql_print_error("Wrong tree: Found two red in a row");
    return -1;
  }
  if (element->left == element->right && element->left != &null_element)
  {						// Dummy test
    sql_print_error("Wrong tree: Found right == left");
    return -1;
  }
  count_l=test_rb_tree(element->left,element);
  count_r=test_rb_tree(element->right,element);
  if (count_l >= 0 && count_r >= 0)
  {
    if (count_l == count_r)
      return count_l+(element->color == SEL_ARG::BLACK);
    sql_print_error("Wrong tree: Incorrect black-count: %d - %d",
	    count_l,count_r);
  }
  return -1;					// Error, no more warnings
}

7470

7471 7472 7473
/**
  Count how many times SEL_ARG graph "root" refers to its part "key" via
  transitive closure.
7474
  
7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486
  @param root  An RB-Root node in a SEL_ARG graph.
  @param key   Another RB-Root node in that SEL_ARG graph.

  The passed "root" node may refer to "key" node via root->next_key_part,
  root->next->n

  This function counts how many times the node "key" is referred (via
  SEL_ARG::next_key_part) by 
  - intervals of RB-tree pointed by "root", 
  - intervals of RB-trees that are pointed by SEL_ARG::next_key_part from 
  intervals of RB-tree pointed by "root",
  - and so on.
7487
    
7488 7489
  Here is an example (horizontal links represent next_key_part pointers, 
  vertical links - next/prev prev pointers):  
7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508
    
         +----+               $
         |root|-----------------+
         +----+               $ |
           |                  $ |
           |                  $ |
         +----+       +---+   $ |     +---+    Here the return value
         |    |- ... -|   |---$-+--+->|key|    will be 4.
         +----+       +---+   $ |  |  +---+
           |                  $ |  |
          ...                 $ |  |
           |                  $ |  |
         +----+   +---+       $ |  |
         |    |---|   |---------+  |
         +----+   +---+       $    |
           |        |         $    |
          ...     +---+       $    |
                  |   |------------+
                  +---+       $
7509 7510
  @return 
  Number of links to "key" from nodes reachable from "root".
7511 7512
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529
static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
{
  ulong count= 0;
  for (root=root->first(); root ; root=root->next)
  {
    if (root->next_key_part)
    {
      if (root->next_key_part == key)
	count++;
      if (root->next_key_part->part < key->part)
	count+=count_key_part_usage(root->next_key_part,key);
    }
  }
  return count;
}


7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543
/*
  Check if SEL_ARG::use_count value is correct

  SYNOPSIS
    SEL_ARG::test_use_count()
      root  The root node of the SEL_ARG graph (an RB-tree root node that
            has the least value of sel_arg->part in the entire graph, and
            thus is the "origin" of the graph)

  DESCRIPTION
    Check if SEL_ARG::use_count value is correct. See the definition of
    use_count for what is "correct".
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7544 7545
void SEL_ARG::test_use_count(SEL_ARG *root)
{
7546
  uint e_count=0;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7547 7548
  if (this == root && use_count != 1)
  {
monty@mysql.com's avatar
monty@mysql.com committed
7549
    sql_print_information("Use_count: Wrong count %lu for root",use_count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561
    return;
  }
  if (this->type != SEL_ARG::KEY_RANGE)
    return;
  for (SEL_ARG *pos=first(); pos ; pos=pos->next)
  {
    e_count++;
    if (pos->next_key_part)
    {
      ulong count=count_key_part_usage(root,pos->next_key_part);
      if (count > pos->next_key_part->use_count)
      {
7562 7563 7564
        sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu "
                              "should be %lu", (long unsigned int)pos,
                              pos->next_key_part->use_count, count);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7565 7566 7567 7568 7569 7570
	return;
      }
      pos->next_key_part->test_use_count(root);
    }
  }
  if (e_count != elements)
monty@mysql.com's avatar
monty@mysql.com committed
7571
    sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
7572
                      e_count, elements, (long unsigned int) this);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7573 7574 7575 7576
}

#endif

7577 7578 7579 7580 7581 7582
/*
  Calculate estimate of number records that will be retrieved by a range
  scan on given index using given SEL_ARG intervals tree.
  SYNOPSIS
    check_quick_select
      param  Parameter from test_quick_select
7583 7584 7585 7586 7587 7588
      idx               Number of index to use in tree->keys
      tree              Transformed selection condition, tree->keys[idx]
                        holds the range tree to be used for scanning.
      update_tbl_stats  If true, update table->quick_keys with information
                        about range scan we've evaluated.

7589
  NOTES
7590
    param->is_ror_scan is set to reflect if the key scan is a ROR (see
7591
    is_key_scan_ror function for more info)
7592
    param->table->quick_*, param->range_count (and maybe others) are
7593
    updated with data of given key scan, see check_quick_keys for details.
7594 7595

  RETURN
7596
    Estimate # of records to be retrieved.
7597
    HA_POS_ERROR if estimate calculation failed due to table handler problems.
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7598

7599
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7600 7601

static ha_rows
7602
check_quick_select(PARAM *param,uint idx,SEL_ARG *tree, bool update_tbl_stats)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7603 7604
{
  ha_rows records;
7605 7606
  bool    cpk_scan;
  uint key;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7607
  DBUG_ENTER("check_quick_select");
7608

monty@mysql.com's avatar
monty@mysql.com committed
7609
  param->is_ror_scan= FALSE;
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7610
  param->first_null_comp= 0;
7611

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7612 7613
  if (!tree)
    DBUG_RETURN(HA_POS_ERROR);			// Can't use it
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7614 7615
  param->max_key_part=0;
  param->range_count=0;
7616 7617
  key= param->real_keynr[idx];

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7618 7619 7620 7621
  if (tree->type == SEL_ARG::IMPOSSIBLE)
    DBUG_RETURN(0L);				// Impossible select. return
  if (tree->type != SEL_ARG::KEY_RANGE || tree->part != 0)
    DBUG_RETURN(HA_POS_ERROR);				// Don't use tree
7622 7623 7624 7625 7626

  enum ha_key_alg key_alg= param->table->key_info[key].algorithm;
  if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
  {
    /* Records are not ordered by rowid for other types of indexes. */
monty@mysql.com's avatar
monty@mysql.com committed
7627
    cpk_scan= FALSE;
7628 7629 7630 7631 7632 7633 7634
  }
  else
  {
    /*
      Clustered PK scan is a special case, check_quick_keys doesn't recognize
      CPK scans as ROR scans (while actually any CPK scan is a ROR scan).
    */
7635 7636
    cpk_scan= ((param->table->s->primary_key == param->real_keynr[idx]) &&
               param->table->file->primary_key_is_clustered());
7637
    param->is_ror_scan= !cpk_scan;
7638
  }
7639
  param->n_ranges= 0;
7640

7641 7642 7643
  records= check_quick_keys(param, idx, tree,
                            param->min_key, 0, -1,
                            param->max_key, 0, -1);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7644
  if (records != HA_POS_ERROR)
7645
  {
7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657
    if (update_tbl_stats)
    {
      param->table->quick_keys.set_bit(key);
      param->table->quick_key_parts[key]=param->max_key_part+1;
      param->table->quick_n_ranges[key]= param->n_ranges;
      param->table->quick_condition_rows=
        min(param->table->quick_condition_rows, records);
    }
    /*
      Need to save quick_rows in any case as it is used when calculating
      cost of ROR intersection:
    */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7658
    param->table->quick_rows[key]=records;
7659
    if (cpk_scan)
monty@mysql.com's avatar
monty@mysql.com committed
7660
      param->is_ror_scan= TRUE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7661
  }
7662 7663
  if (param->table->file->index_flags(key, 0, TRUE) & HA_KEY_SCAN_NOT_ROR)
    param->is_ror_scan= FALSE;
7664
  DBUG_PRINT("exit", ("Records: %lu", (ulong) records));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7665 7666 7667 7668
  DBUG_RETURN(records);
}


7669
/*
7670 7671
  Recursively calculate estimate of # rows that will be retrieved by
  key scan on key idx.
7672 7673
  SYNOPSIS
    check_quick_keys()
7674
      param         Parameter from test_quick select function.
7675
      idx           Number of key to use in PARAM::keys in list of used keys
7676 7677 7678
                    (param->real_keynr[idx] holds the key number in table)
      key_tree      SEL_ARG tree being examined.
      min_key       Buffer with partial min key value tuple
7679
      min_key_flag
7680
      max_key       Buffer with partial max key value tuple
7681 7682
      max_key_flag

7683
  NOTES
7684 7685
    The function does the recursive descent on the tree via SEL_ARG::left,
    SEL_ARG::right, and SEL_ARG::next_key_part edges. The #rows estimates
7686 7687
    are calculated using records_in_range calls at the leaf nodes and then
    summed.
7688

7689 7690
    param->min_key and param->max_key are used to hold prefixes of key value
    tuples.
7691 7692

    The side effects are:
7693

7694 7695
    param->max_key_part is updated to hold the maximum number of key parts used
      in scan minus 1.
7696 7697

    param->range_count is incremented if the function finds a range that
7698
      wasn't counted by the caller.
7699

7700 7701 7702
    param->is_ror_scan is cleared if the function detects that the key scan is
      not a Rowid-Ordered Retrieval scan ( see comments for is_key_scan_ror
      function for description of which key scans are ROR scans)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
7703 7704 7705 7706 7707

  RETURN
    #records      E(#records) for given subtree
    HA_POS_ERROR  if subtree cannot be used for record retrieval

7708 7709
*/

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7710
static ha_rows
7711
check_quick_keys(PARAM *param, uint idx, SEL_ARG *key_tree,
7712 7713
		 uchar *min_key, uint min_key_flag, int min_keypart,
                 uchar *max_key, uint max_key_flag, int max_keypart)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7714
{
monty@mysql.com's avatar
monty@mysql.com committed
7715 7716
  ha_rows records=0, tmp;
  uint tmp_min_flag, tmp_max_flag, keynr, min_key_length, max_key_length;
7717
  uint tmp_min_keypart= min_keypart, tmp_max_keypart= max_keypart;
7718
  uchar *tmp_min_key, *tmp_max_key;
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7719
  uint8 save_first_null_comp= param->first_null_comp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7720 7721 7722 7723

  param->max_key_part=max(param->max_key_part,key_tree->part);
  if (key_tree->left != &null_element)
  {
7724 7725 7726 7727 7728 7729
    /*
      There are at least two intervals for current key part, i.e. condition
      was converted to something like
        (keyXpartY less/equals c1) OR (keyXpartY more/equals c2).
      This is not a ROR scan if the key is not Clustered Primary Key.
    */
monty@mysql.com's avatar
monty@mysql.com committed
7730
    param->is_ror_scan= FALSE;
7731 7732 7733
    records=check_quick_keys(param, idx, key_tree->left,
                             min_key, min_key_flag, min_keypart,
			     max_key, max_key_flag, max_keypart);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7734 7735 7736 7737
    if (records == HA_POS_ERROR)			// Impossible
      return records;
  }

monty@mysql.com's avatar
monty@mysql.com committed
7738 7739
  tmp_min_key= min_key;
  tmp_max_key= max_key;
7740 7741 7742 7743 7744 7745
  tmp_min_keypart+= key_tree->store_min(param->key[idx][key_tree->part].store_length,
                                        &tmp_min_key, min_key_flag);
  tmp_max_keypart+= key_tree->store_max(param->key[idx][key_tree->part].store_length,
                                        &tmp_max_key, max_key_flag);
  min_key_length= (uint) (tmp_min_key - param->min_key);
  max_key_length= (uint) (tmp_max_key - param->max_key);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7746

7747 7748
  if (param->is_ror_scan)
  {
7749
    /*
7750
      If the index doesn't cover entire key, mark the scan as non-ROR scan.
7751
      Actually we're cutting off some ROR scans here.
7752 7753 7754
    */
    uint16 fieldnr= param->table->key_info[param->real_keynr[idx]].
                    key_part[key_tree->part].fieldnr - 1;
7755
    if (param->table->field[fieldnr]->key_length() !=
7756
        param->key[idx][key_tree->part].length)
monty@mysql.com's avatar
monty@mysql.com committed
7757
      param->is_ror_scan= FALSE;
7758 7759
  }

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7760 7761 7762
  if (!param->first_null_comp && key_tree->is_null_interval())
    param->first_null_comp= key_tree->part+1;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7763
  if (key_tree->next_key_part &&
7764 7765
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
      key_tree->next_key_part->part == key_tree->part+1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7766 7767
  {						// const key as prefix
    if (min_key_length == max_key_length &&
7768
	!memcmp(min_key, max_key, (uint) (tmp_max_key - max_key)) &&
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7769 7770
	!key_tree->min_flag && !key_tree->max_flag)
    {
7771 7772 7773 7774
      tmp=check_quick_keys(param,idx,key_tree->next_key_part, tmp_min_key,
                           min_key_flag | key_tree->min_flag, tmp_min_keypart,
                           tmp_max_key, max_key_flag | key_tree->max_flag,
                           tmp_max_keypart);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7775 7776
      goto end;					// Ugly, but efficient
    }
7777
    else
7778 7779
    {
      /* The interval for current key part is not c1 <= keyXpartY <= c1 */
monty@mysql.com's avatar
monty@mysql.com committed
7780
      param->is_ror_scan= FALSE;
7781
    }
7782

bk@work.mysql.com's avatar
bk@work.mysql.com committed
7783 7784 7785
    tmp_min_flag=key_tree->min_flag;
    tmp_max_flag=key_tree->max_flag;
    if (!tmp_min_flag)
7786
      tmp_min_keypart+=
7787 7788
      key_tree->next_key_part->store_min_key(param->key[idx],
                                             &tmp_min_key,
7789
                                             &tmp_min_flag,
7790
                                             MAX_KEY);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7791
    if (!tmp_max_flag)
7792
      tmp_max_keypart+=
7793 7794
      key_tree->next_key_part->store_max_key(param->key[idx],
                                             &tmp_max_key,
7795
                                             &tmp_max_flag,
7796
                                             MAX_KEY);
7797 7798
    min_key_length= (uint) (tmp_min_key - param->min_key);
    max_key_length= (uint) (tmp_max_key - param->max_key);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7799 7800 7801
  }
  else
  {
7802 7803
    tmp_min_flag= min_key_flag | key_tree->min_flag;
    tmp_max_flag= max_key_flag | key_tree->max_flag;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7804 7805
  }

7806 7807 7808
  if (unlikely(param->thd->killed != 0))
    return HA_POS_ERROR;
  
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7809
  keynr=param->real_keynr[idx];
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
7810
  param->range_count++;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7811 7812
  if (!tmp_min_flag && ! tmp_max_flag &&
      (uint) key_tree->part+1 == param->table->key_info[keynr].key_parts &&
7813 7814
      param->table->key_info[keynr].flags & HA_NOSAME &&
      min_key_length == max_key_length &&
7815
      !memcmp(param->min_key, param->max_key, min_key_length) &&
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7816
      !param->first_null_comp)
7817
  {
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7818
    tmp=1;					// Max one record
7819 7820
    param->n_ranges++;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7821
  else
7822
  {
7823 7824
    if (param->is_ror_scan)
    {
7825 7826 7827 7828 7829 7830 7831 7832 7833
      /*
        If we get here, the condition on the key was converted to form
        "(keyXpart1 = c1) AND ... AND (keyXpart{key_tree->part - 1} = cN) AND
          somecond(keyXpart{key_tree->part})"
        Check if
          somecond is "keyXpart{key_tree->part} = const" and
          uncovered "tail" of KeyX parts is either empty or is identical to
          first members of clustered primary key.
      */
7834
      if (!(min_key_length == max_key_length &&
7835
            !memcmp(min_key, max_key, (uint) (tmp_max_key - max_key)) &&
7836
            !key_tree->min_flag && !key_tree->max_flag &&
7837
            is_key_scan_ror(param, keynr, key_tree->part + 1)))
monty@mysql.com's avatar
monty@mysql.com committed
7838
        param->is_ror_scan= FALSE;
7839
    }
7840
    param->n_ranges++;
7841

7842
    if (tmp_min_flag & GEOM_FLAG)
7843
    {
7844
      key_range min_range;
7845
      min_range.key=    param->min_key;
7846
      min_range.length= min_key_length;
7847
      min_range.keypart_map= make_keypart_map(tmp_min_keypart);
7848 7849 7850
      /* In this case tmp_min_flag contains the handler-read-function */
      min_range.flag=   (ha_rkey_function) (tmp_min_flag ^ GEOM_FLAG);

7851 7852
      tmp= param->table->file->records_in_range(keynr,
                                                &min_range, (key_range*) 0);
7853 7854 7855
    }
    else
    {
7856 7857
      key_range min_range, max_range;

7858
      min_range.key=    param->min_key;
7859 7860 7861
      min_range.length= min_key_length;
      min_range.flag=   (tmp_min_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
                         HA_READ_KEY_EXACT);
7862
      min_range.keypart_map= make_keypart_map(tmp_min_keypart);
7863
      max_range.key=    param->max_key;
7864 7865 7866
      max_range.length= max_key_length;
      max_range.flag=   (tmp_max_flag & NEAR_MAX ?
                         HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY);
7867
      max_range.keypart_map= make_keypart_map(tmp_max_keypart);
7868 7869 7870 7871 7872
      tmp=param->table->file->records_in_range(keynr,
                                               (min_key_length ? &min_range :
                                                (key_range*) 0),
                                               (max_key_length ? &max_range :
                                                (key_range*) 0));
7873 7874
    }
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7875 7876 7877 7878 7879 7880
 end:
  if (tmp == HA_POS_ERROR)			// Impossible range
    return tmp;
  records+=tmp;
  if (key_tree->right != &null_element)
  {
7881 7882 7883 7884 7885 7886
    /*
      There are at least two intervals for current key part, i.e. condition
      was converted to something like
        (keyXpartY less/equals c1) OR (keyXpartY more/equals c2).
      This is not a ROR scan if the key is not Clustered Primary Key.
    */
monty@mysql.com's avatar
monty@mysql.com committed
7887
    param->is_ror_scan= FALSE;
7888 7889 7890
    tmp=check_quick_keys(param, idx, key_tree->right,
                         min_key, min_key_flag, min_keypart,
                         max_key, max_key_flag, max_keypart);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7891 7892 7893 7894
    if (tmp == HA_POS_ERROR)
      return tmp;
    records+=tmp;
  }
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
7895
  param->first_null_comp= save_first_null_comp;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7896 7897 7898
  return records;
}

7899

7900
/*
7901
  Check if key scan on given index with equality conditions on first n key
7902 7903 7904 7905
  parts is a ROR scan.

  SYNOPSIS
    is_key_scan_ror()
7906
      param  Parameter from test_quick_select
7907 7908 7909 7910
      keynr  Number of key in the table. The key must not be a clustered
             primary key.
      nparts Number of first key parts for which equality conditions
             are present.
7911

7912 7913 7914
  NOTES
    ROR (Rowid Ordered Retrieval) key scan is a key scan that produces
    ordered sequence of rowids (ha_xxx::cmp_ref is the comparison function)
7915

7916 7917
    This function is needed to handle a practically-important special case:
    an index scan is a ROR scan if it is done using a condition in form
7918

7919
        "key1_1=c_1 AND ... AND key1_n=c_n"
7920

7921 7922
    where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])

7923
    and the table has a clustered Primary Key defined as 
7924

7925 7926 7927 7928 7929
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k) 
    
    i.e. the first key parts of it are identical to uncovered parts ot the 
    key being scanned. This function assumes that the index flags do not
    include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
7930

7931
  RETURN
7932 7933
    TRUE   The scan is ROR-scan
    FALSE  Otherwise
7934
*/
7935

7936 7937 7938 7939
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts)
{
  KEY *table_key= param->table->key_info + keynr;
  KEY_PART_INFO *key_part= table_key->key_part + nparts;
7940 7941 7942
  KEY_PART_INFO *key_part_end= (table_key->key_part +
                                table_key->key_parts);
  uint pk_number;
7943

7944
  if (key_part == key_part_end)
monty@mysql.com's avatar
monty@mysql.com committed
7945
    return TRUE;
7946
  pk_number= param->table->s->primary_key;
7947
  if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
monty@mysql.com's avatar
monty@mysql.com committed
7948
    return FALSE;
7949 7950

  KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
7951
  KEY_PART_INFO *pk_part_end= pk_part +
7952
                              param->table->key_info[pk_number].key_parts;
7953 7954
  for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
       ++key_part, ++pk_part)
7955
  {
7956
    if ((key_part->field != pk_part->field) ||
7957
        (key_part->length != pk_part->length))
7958
      return FALSE;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7959
  }
7960
  return (key_part == key_part_end);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7961 7962 7963
}


7964 7965
/*
  Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.
7966

7967 7968
  SYNOPSIS
    get_quick_select()
7969
      param
7970
      idx          Index of used key in param->key.
7971 7972
      key_tree     SEL_ARG tree for the used key
      parent_alloc If not NULL, use it to allocate memory for
7973
                   quick select data. Otherwise use quick->alloc.
7974
  NOTES
7975
    The caller must call QUICK_SELECT::init for returned quick select
7976

7977
    CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
7978
    deallocated when the returned quick select is deleted.
7979 7980 7981 7982

  RETURN
    NULL on error
    otherwise created quick select
7983
*/
7984

7985 7986 7987
QUICK_RANGE_SELECT *
get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree,
                 MEM_ROOT *parent_alloc)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7988
{
7989
  QUICK_RANGE_SELECT *quick;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
7990
  DBUG_ENTER("get_quick_select");
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
7991 7992 7993 7994 7995 7996 7997 7998 7999

  if (param->table->key_info[param->real_keynr[idx]].flags & HA_SPATIAL)
    quick=new QUICK_RANGE_SELECT_GEOM(param->thd, param->table,
                                      param->real_keynr[idx],
                                      test(parent_alloc),
                                      parent_alloc);
  else
    quick=new QUICK_RANGE_SELECT(param->thd, param->table,
                                 param->real_keynr[idx],
monty@mysql.com's avatar
monty@mysql.com committed
8000
                                 test(parent_alloc));
8001

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8002
  if (quick)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013
  {
    if (quick->error ||
	get_quick_keys(param,quick,param->key[idx],key_tree,param->min_key,0,
		       param->max_key,0))
    {
      delete quick;
      quick=0;
    }
    else
    {
      quick->key_parts=(KEY_PART*)
8014 8015 8016 8017
        memdup_root(parent_alloc? parent_alloc : &quick->alloc,
                    (char*) param->key[idx],
                    sizeof(KEY_PART)*
                    param->table->key_info[param->real_keynr[idx]].key_parts);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8018
    }
8019
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8020 8021 8022 8023 8024 8025 8026
  DBUG_RETURN(quick);
}


/*
** Fix this to get all possible sub_ranges
*/
8027 8028
bool
get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
8029 8030
	       SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
	       uchar *max_key, uint max_key_flag)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8031 8032 8033
{
  QUICK_RANGE *range;
  uint flag;
8034 8035
  int min_part= key_tree->part-1, // # of keypart values in min_key buffer
      max_part= key_tree->part-1; // # of keypart values in max_key buffer
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8036 8037 8038 8039 8040 8041 8042

  if (key_tree->left != &null_element)
  {
    if (get_quick_keys(param,quick,key,key_tree->left,
		       min_key,min_key_flag, max_key, max_key_flag))
      return 1;
  }
8043
  uchar *tmp_min_key=min_key,*tmp_max_key=max_key;
8044 8045 8046 8047
  min_part+= key_tree->store_min(key[key_tree->part].store_length,
                                 &tmp_min_key,min_key_flag);
  max_part+= key_tree->store_max(key[key_tree->part].store_length,
                                 &tmp_max_key,max_key_flag);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8048 8049

  if (key_tree->next_key_part &&
8050 8051
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
      key_tree->next_key_part->part == key_tree->part+1)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8052
  {						  // const key as prefix
8053 8054 8055
    if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
         memcmp(min_key, max_key, (uint)(tmp_max_key - max_key))==0 &&
	 key_tree->min_flag==0 && key_tree->max_flag==0)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8056 8057 8058 8059 8060 8061 8062 8063 8064 8065
    {
      if (get_quick_keys(param,quick,key,key_tree->next_key_part,
			 tmp_min_key, min_key_flag | key_tree->min_flag,
			 tmp_max_key, max_key_flag | key_tree->max_flag))
	return 1;
      goto end;					// Ugly, but efficient
    }
    {
      uint tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
      if (!tmp_min_flag)
8066 8067 8068 8069
        min_part+= key_tree->next_key_part->store_min_key(key,
                                                          &tmp_min_key,
                                                          &tmp_min_flag,
                                                          MAX_KEY);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8070
      if (!tmp_max_flag)
8071 8072 8073 8074
        max_part+= key_tree->next_key_part->store_max_key(key,
                                                          &tmp_max_key,
                                                          &tmp_max_flag,
                                                          MAX_KEY);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8075 8076 8077 8078
      flag=tmp_min_flag | tmp_max_flag;
    }
  }
  else
8079 8080 8081 8082
  {
    flag = (key_tree->min_flag & GEOM_FLAG) ?
      key_tree->min_flag : key_tree->min_flag | key_tree->max_flag;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8083

8084 8085 8086 8087 8088
  /*
    Ensure that some part of min_key and max_key are used.  If not,
    regard this as no lower/upper range
  */
  if ((flag & GEOM_FLAG) == 0)
8089 8090 8091 8092 8093 8094 8095 8096 8097 8098
  {
    if (tmp_min_key != param->min_key)
      flag&= ~NO_MIN_RANGE;
    else
      flag|= NO_MIN_RANGE;
    if (tmp_max_key != param->max_key)
      flag&= ~NO_MAX_RANGE;
    else
      flag|= NO_MAX_RANGE;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8099 8100 8101 8102 8103 8104 8105 8106
  if (flag == 0)
  {
    uint length= (uint) (tmp_min_key - param->min_key);
    if (length == (uint) (tmp_max_key - param->max_key) &&
	!memcmp(param->min_key,param->max_key,length))
    {
      KEY *table_key=quick->head->key_info+quick->index;
      flag=EQ_RANGE;
8107
      if ((table_key->flags & HA_NOSAME) && key->part == table_key->key_parts-1)
8108 8109 8110 8111 8112 8113 8114 8115 8116
      {
	if (!(table_key->flags & HA_NULL_PART_KEY) ||
	    !null_part_in_key(key,
			      param->min_key,
			      (uint) (tmp_min_key - param->min_key)))
	  flag|= UNIQUE_RANGE;
	else
	  flag|= NULL_RANGE;
      }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8117 8118 8119 8120
    }
  }

  /* Get range for retrieving rows in QUICK_SELECT::get_next */
8121
  if (!(range= new QUICK_RANGE(param->min_key,
8122
			       (uint) (tmp_min_key - param->min_key),
8123
                               min_part >=0 ? make_keypart_map(min_part) : 0,
8124
			       param->max_key,
8125
			       (uint) (tmp_max_key - param->max_key),
8126
                               max_part >=0 ? make_keypart_map(max_part) : 0,
8127
			       flag)))
8128 8129
    return 1;			// out of memory

8130 8131
  set_if_bigger(quick->max_used_key_length, range->min_length);
  set_if_bigger(quick->max_used_key_length, range->max_length);
8132
  set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
8133
  if (insert_dynamic(&quick->ranges, (uchar*) &range))
8134 8135
    return 1;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147
 end:
  if (key_tree->right != &null_element)
    return get_quick_keys(param,quick,key,key_tree->right,
			  min_key,min_key_flag,
			  max_key,max_key_flag);
  return 0;
}

/*
  Return 1 if there is only one range and this uses the whole primary key
*/

8148
bool QUICK_RANGE_SELECT::unique_key_range()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8149 8150 8151
{
  if (ranges.elements == 1)
  {
8152 8153
    QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
    if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8154 8155
    {
      KEY *key=head->key_info+index;
8156
      return (key->flags & HA_NOSAME) && key->key_length == tmp->min_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8157 8158 8159 8160 8161
    }
  }
  return 0;
}

8162

monty@mysql.com's avatar
monty@mysql.com committed
8163
/* Returns TRUE if any part of the key is NULL */
8164

8165
static bool null_part_in_key(KEY_PART *key_part, const uchar *key, uint length)
8166
{
8167
  for (const uchar *end=key+length ;
8168
       key < end;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8169
       key+= key_part++->store_length)
8170
  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8171 8172
    if (key_part->null_bit && *key)
      return 1;
8173 8174 8175 8176
  }
  return 0;
}

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8177

8178
bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
8179
{
8180
  return is_key_used(head, index, fields);
8181 8182
}

8183
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
8184 8185 8186 8187 8188
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
8189
    if (is_key_used(head, quick->index, fields))
8190 8191 8192 8193 8194
      return 1;
  }
  return 0;
}

8195
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
8196 8197 8198 8199 8200
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
8201
    if (is_key_used(head, quick->index, fields))
8202 8203 8204 8205 8206
      return 1;
  }
  return 0;
}

8207
bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
8208 8209 8210 8211 8212
{
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
8213
    if (quick->is_keys_used(fields))
8214 8215 8216 8217 8218
      return 1;
  }
  return 0;
}

monty@mysql.com's avatar
monty@mysql.com committed
8219

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8220 8221
/*
  Create quick select from ref/ref_or_null scan.
8222

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8223 8224 8225 8226 8227
  SYNOPSIS
    get_quick_select_for_ref()
      thd      Thread handle
      table    Table to access
      ref      ref[_or_null] scan parameters
8228
      records  Estimate of number of records (needed only to construct
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8229 8230 8231 8232
               quick select)
  NOTES
    This allocates things in a new memory root, as this may be called many
    times during a query.
8233 8234

  RETURN
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8235 8236 8237
    Quick select that retrieves the same rows as passed ref scan
    NULL on error.
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8238

8239
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8240
                                             TABLE_REF *ref, ha_rows records)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8241
{
8242 8243
  MEM_ROOT *old_root, *alloc;
  QUICK_RANGE_SELECT *quick;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8244 8245
  KEY *key_info = &table->key_info[ref->key];
  KEY_PART *key_part;
serg@serg.mylan's avatar
serg@serg.mylan committed
8246
  QUICK_RANGE *range;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8247
  uint part;
8248 8249 8250 8251 8252 8253

  old_root= thd->mem_root;
  /* The following call may change thd->mem_root */
  quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0);
  /* save mem_root set by QUICK_RANGE_SELECT constructor */
  alloc= thd->mem_root;
8254 8255 8256 8257 8258
  /*
    return back default mem_root (thd->mem_root) changed by
    QUICK_RANGE_SELECT constructor
  */
  thd->mem_root= old_root;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8259 8260

  if (!quick)
8261
    return 0;			/* no ranges found */
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8262
  if (quick->init())
monty@mysql.com's avatar
monty@mysql.com committed
8263
    goto err;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8264
  quick->records= records;
8265

8266
  if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) ||
8267
      !(range= new(alloc) QUICK_RANGE()))
monty@mysql.com's avatar
monty@mysql.com committed
8268
    goto err;                                   // out of memory
8269

8270
  range->min_key= range->max_key= ref->key_buff;
8271 8272 8273
  range->min_length= range->max_length= ref->key_length;
  range->min_keypart_map= range->max_keypart_map=
    make_prev_keypart_map(ref->key_parts);
8274
  range->flag= (ref->key_length == key_info->key_length ? EQ_RANGE : 0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8275 8276

  if (!(quick->key_parts=key_part=(KEY_PART *)
8277
	alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8278 8279 8280 8281 8282 8283
    goto err;

  for (part=0 ; part < ref->key_parts ;part++,key_part++)
  {
    key_part->part=part;
    key_part->field=        key_info->key_part[part].field;
8284
    key_part->length=       key_info->key_part[part].length;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8285
    key_part->store_length= key_info->key_part[part].store_length;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8286
    key_part->null_bit=     key_info->key_part[part].null_bit;
8287
    key_part->flag=         (uint8) key_info->key_part[part].key_part_flag;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8288
  }
8289
  if (insert_dynamic(&quick->ranges,(uchar*)&range))
8290 8291
    goto err;

8292
  /*
8293 8294 8295 8296 8297
     Add a NULL range if REF_OR_NULL optimization is used.
     For example:
       if we have "WHERE A=2 OR A IS NULL" we created the (A=2) range above
       and have ref->null_ref_key set. Will create a new NULL range here.
  */
8298 8299 8300 8301 8302
  if (ref->null_ref_key)
  {
    QUICK_RANGE *null_range;

    *ref->null_ref_key= 1;		// Set null byte then create a range
8303
    if (!(null_range= new (alloc)
8304
          QUICK_RANGE(ref->key_buff, ref->key_length,
8305
                      make_prev_keypart_map(ref->key_parts),
8306
                      ref->key_buff, ref->key_length,
8307
                      make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
8308 8309
      goto err;
    *ref->null_ref_key= 0;		// Clear null byte
8310
    if (insert_dynamic(&quick->ranges,(uchar*)&null_range))
8311 8312 8313 8314
      goto err;
  }

  return quick;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8315 8316 8317 8318 8319 8320

err:
  delete quick;
  return 0;
}

8321 8322

/*
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8323 8324 8325 8326 8327 8328
  Perform key scans for all used indexes (except CPK), get rowids and merge 
  them into an ordered non-recurrent sequence of rowids.
  
  The merge/duplicate removal is performed using Unique class. We put all
  rowids into Unique, get the sorted sequence and destroy the Unique.
  
8329
  If table has a clustered primary key that covers all rows (TRUE for bdb
8330 8331 8332
  and innodb currently) and one of the index_merge scans is a scan on PK,
  then rows that will be retrieved by PK scan are not put into Unique and 
  primary key scan is not performed here, it is performed later separately.
8333

8334 8335 8336
  RETURN
    0     OK
    other error
8337
*/
8338

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8339
int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
8340
{
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8341 8342
  List_iterator_fast<QUICK_RANGE_SELECT> cur_quick_it(quick_selects);
  QUICK_RANGE_SELECT* cur_quick;
8343
  int result;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8344
  Unique *unique;
8345 8346
  handler *file= head->file;
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
8347

8348
  /* We're going to just read rowids. */
8349 8350
  file->extra(HA_EXTRA_KEYREAD);
  head->prepare_for_position();
8351

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8352 8353
  cur_quick_it.rewind();
  cur_quick= cur_quick_it++;
8354
  DBUG_ASSERT(cur_quick != 0);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8355 8356 8357 8358 8359
  
  /*
    We reuse the same instance of handler so we need to call both init and 
    reset here.
  */
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8360
  if (cur_quick->init() || cur_quick->reset())
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8361
    DBUG_RETURN(1);
8362

8363 8364
  unique= new Unique(refpos_order_cmp, (void *)file,
                     file->ref_length,
8365
                     thd->variables.sortbuff_size);
8366 8367
  if (!unique)
    DBUG_RETURN(1);
monty@mysql.com's avatar
monty@mysql.com committed
8368
  for (;;)
8369
  {
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8370
    while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
8371
    {
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8372 8373 8374
      cur_quick->range_end();
      cur_quick= cur_quick_it++;
      if (!cur_quick)
8375
        break;
8376

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8377 8378
      if (cur_quick->file->inited != handler::NONE) 
        cur_quick->file->ha_index_end();
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8379
      if (cur_quick->init() || cur_quick->reset())
8380 8381
      {
        delete unique;
8382
        DBUG_RETURN(1);
8383
      }
8384 8385 8386
    }

    if (result)
8387
    {
8388
      if (result != HA_ERR_END_OF_FILE)
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8389 8390
      {
        cur_quick->range_end();
8391
        delete unique;
8392
        DBUG_RETURN(result);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8393
      }
8394
      break;
8395
    }
8396

8397
    if (thd->killed)
8398 8399
    {
      delete unique;
8400
      DBUG_RETURN(1);
8401
    }
8402

8403
    /* skip row if it will be retrieved by clustered PK scan */
8404 8405
    if (pk_quick_select && pk_quick_select->row_in_ranges())
      continue;
8406

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8407 8408
    cur_quick->file->position(cur_quick->record);
    result= unique->unique_add((char*)cur_quick->file->ref);
8409
    if (result)
8410 8411
    {
      delete unique;
8412
      DBUG_RETURN(1);
8413
    }
monty@mysql.com's avatar
monty@mysql.com committed
8414
  }
8415

8416 8417 8418 8419 8420
  /*
    Ok all rowids are in the Unique now. The next call will initialize
    head->sort structure so it can be used to iterate through the rowids
    sequence.
  */
8421
  result= unique->get(head);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8422
  delete unique;
monty@mysql.com's avatar
monty@mysql.com committed
8423
  doing_pk_scan= FALSE;
8424 8425
  /* index_merge currently doesn't support "using index" at all */
  file->extra(HA_EXTRA_NO_KEYREAD);
8426
  init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1 , 1, TRUE);
8427 8428 8429
  DBUG_RETURN(result);
}

8430

8431 8432 8433
/*
  Get next row for index_merge.
  NOTES
8434 8435 8436 8437
    The rows are read from
      1. rowids stored in Unique.
      2. QUICK_RANGE_SELECT with clustered primary key (if any).
    The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
8438
*/
8439

8440 8441
int QUICK_INDEX_MERGE_SELECT::get_next()
{
8442
  int result;
8443
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");
8444

8445 8446 8447
  if (doing_pk_scan)
    DBUG_RETURN(pk_quick_select->get_next());

8448
  if ((result= read_record.read_record(&read_record)) == -1)
8449 8450 8451
  {
    result= HA_ERR_END_OF_FILE;
    end_read_record(&read_record);
8452
    free_io_cache(head);
8453
    /* All rows from Unique have been retrieved, do a clustered PK scan */
monty@mysql.com's avatar
monty@mysql.com committed
8454
    if (pk_quick_select)
8455
    {
monty@mysql.com's avatar
monty@mysql.com committed
8456
      doing_pk_scan= TRUE;
8457 8458
      if ((result= pk_quick_select->init()) ||
          (result= pk_quick_select->reset()))
8459 8460 8461 8462 8463 8464
        DBUG_RETURN(result);
      DBUG_RETURN(pk_quick_select->get_next());
    }
  }

  DBUG_RETURN(result);
8465 8466
}

8467 8468

/*
8469
  Retrieve next record.
8470
  SYNOPSIS
8471 8472
     QUICK_ROR_INTERSECT_SELECT::get_next()

8473
  NOTES
8474 8475
    Invariant on enter/exit: all intersected selects have retrieved all index
    records with rowid <= some_rowid_val and no intersected select has
8476 8477 8478 8479
    retrieved any index records with rowid > some_rowid_val.
    We start fresh and loop until we have retrieved the same rowid in each of
    the key scans or we got an error.

8480
    If a Clustered PK scan is present, it is used only to check if row
8481 8482 8483 8484 8485
    satisfies its condition (and never used for row retrieval).

  RETURN
   0     - Ok
   other - Error code if any error occurred.
8486 8487 8488 8489 8490 8491 8492 8493 8494
*/

int QUICK_ROR_INTERSECT_SELECT::get_next()
{
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
  QUICK_RANGE_SELECT* quick;
  int error, cmp;
  uint last_rowid_count=0;
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next");
8495

8496
  do
8497
  {
8498 8499
    /* Get a rowid for first quick and save it as a 'candidate' */
    quick= quick_it++;
8500
    error= quick->get_next();
8501 8502
    if (cpk_quick)
    {
8503
      while (!error && !cpk_quick->row_in_ranges())
8504 8505 8506 8507
        error= quick->get_next();
    }
    if (error)
      DBUG_RETURN(error);
8508

8509 8510 8511
    quick->file->position(quick->record);
    memcpy(last_rowid, quick->file->ref, head->file->ref_length);
    last_rowid_count= 1;
8512

8513
    while (last_rowid_count < quick_selects.elements)
8514
    {
8515 8516 8517 8518 8519
      if (!(quick= quick_it++))
      {
        quick_it.rewind();
        quick= quick_it++;
      }
8520

8521 8522 8523 8524 8525 8526 8527 8528 8529 8530
      do
      {
        if ((error= quick->get_next()))
          DBUG_RETURN(error);
        quick->file->position(quick->record);
        cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
      } while (cmp < 0);

      /* Ok, current select 'caught up' and returned ref >= cur_ref */
      if (cmp > 0)
8531
      {
8532 8533
        /* Found a row with ref > cur_ref. Make it a new 'candidate' */
        if (cpk_quick)
8534
        {
8535 8536 8537 8538 8539
          while (!cpk_quick->row_in_ranges())
          {
            if ((error= quick->get_next()))
              DBUG_RETURN(error);
          }
8540
        }
8541 8542 8543 8544 8545 8546 8547
        memcpy(last_rowid, quick->file->ref, head->file->ref_length);
        last_rowid_count= 1;
      }
      else
      {
        /* current 'candidate' row confirmed by this select */
        last_rowid_count++;
8548 8549 8550
      }
    }

8551
    /* We get here if we got the same row ref in all scans. */
8552 8553 8554
    if (need_to_fetch_row)
      error= head->file->rnd_pos(head->record[0], last_rowid);
  } while (error == HA_ERR_RECORD_DELETED);
8555 8556 8557 8558
  DBUG_RETURN(error);
}


8559 8560
/*
  Retrieve next record.
8561 8562
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::get_next()
8563

8564
  NOTES
8565 8566
    Enter/exit invariant:
    For each quick select in the queue a {key,rowid} tuple has been
8567
    retrieved but the corresponding row hasn't been passed to output.
8568

8569
  RETURN
8570 8571
   0     - Ok
   other - Error code if any error occurred.
8572 8573 8574 8575 8576 8577
*/

int QUICK_ROR_UNION_SELECT::get_next()
{
  int error, dup_row;
  QUICK_SELECT_I *quick;
8578
  uchar *tmp;
8579
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");
8580

8581 8582
  do
  {
8583 8584 8585 8586 8587
    do
    {
      if (!queue.elements)
        DBUG_RETURN(HA_ERR_END_OF_FILE);
      /* Ok, we have a queue with >= 1 scans */
8588

8589 8590
      quick= (QUICK_SELECT_I*)queue_top(&queue);
      memcpy(cur_rowid, quick->last_rowid, rowid_length);
8591

8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603
      /* put into queue rowid from the same stream as top element */
      if ((error= quick->get_next()))
      {
        if (error != HA_ERR_END_OF_FILE)
          DBUG_RETURN(error);
        queue_remove(&queue, 0);
      }
      else
      {
        quick->save_last_pos();
        queue_replaced(&queue);
      }
8604

8605 8606 8607 8608 8609 8610 8611 8612 8613
      if (!have_prev_rowid)
      {
        /* No rows have been returned yet */
        dup_row= FALSE;
        have_prev_rowid= TRUE;
      }
      else
        dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
    } while (dup_row);
8614

8615 8616 8617
    tmp= cur_rowid;
    cur_rowid= prev_rowid;
    prev_rowid= tmp;
8618

8619 8620
    error= head->file->rnd_pos(quick->record, prev_rowid);
  } while (error == HA_ERR_RECORD_DELETED);
8621 8622 8623
  DBUG_RETURN(error);
}

8624

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8625
int QUICK_RANGE_SELECT::reset()
ingo@mysql.com's avatar
ingo@mysql.com committed
8626 8627
{
  uint  mrange_bufsiz;
8628
  uchar *mrange_buff;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8629 8630
  DBUG_ENTER("QUICK_RANGE_SELECT::reset");
  next=0;
8631
  last_range= NULL;
8632
  in_range= FALSE;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
8633
  cur_range= (QUICK_RANGE**) ranges.buffer;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
8634

8635
  if (file->inited == handler::NONE && (error= file->ha_index_init(index,1)))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
8636
    DBUG_RETURN(error);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
8637
 
ingo@mysql.com's avatar
ingo@mysql.com committed
8638 8639 8640 8641 8642 8643 8644
  /* Do not allocate the buffers twice. */
  if (multi_range_length)
  {
    DBUG_ASSERT(multi_range_length == min(multi_range_count, ranges.elements));
    DBUG_RETURN(0);
  }

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8645 8646
  /* Allocate the ranges array. */
  DBUG_ASSERT(ranges.elements);
ingo@mysql.com's avatar
ingo@mysql.com committed
8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662
  multi_range_length= min(multi_range_count, ranges.elements);
  DBUG_ASSERT(multi_range_length > 0);
  while (multi_range_length && ! (multi_range= (KEY_MULTI_RANGE*)
                                  my_malloc(multi_range_length *
                                            sizeof(KEY_MULTI_RANGE),
                                            MYF(MY_WME))))
  {
    /* Try to shrink the buffers until it is 0. */
    multi_range_length/= 2;
  }
  if (! multi_range)
  {
    multi_range_length= 0;
    DBUG_RETURN(HA_ERR_OUT_OF_MEM);
  }

sergefp@mysql.com's avatar
sergefp@mysql.com committed
8663
  /* Allocate the handler buffer if necessary.  */
8664
  if (file->ha_table_flags() & HA_NEED_READ_RANGE_BUFFER)
ingo@mysql.com's avatar
ingo@mysql.com committed
8665 8666
  {
    mrange_bufsiz= min(multi_range_bufsiz,
8667
                       ((uint)QUICK_SELECT_I::records + 1)* head->s->reclength);
ingo@mysql.com's avatar
ingo@mysql.com committed
8668 8669 8670

    while (mrange_bufsiz &&
           ! my_multi_malloc(MYF(MY_WME),
8671 8672 8673
                             &multi_range_buff,
                             (uint) sizeof(*multi_range_buff),
                             &mrange_buff, (uint) mrange_bufsiz,
ingo@mysql.com's avatar
ingo@mysql.com committed
8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690
                             NullS))
    {
      /* Try to shrink the buffers until both are 0. */
      mrange_bufsiz/= 2;
    }
    if (! multi_range_buff)
    {
      my_free((char*) multi_range, MYF(0));
      multi_range= NULL;
      multi_range_length= 0;
      DBUG_RETURN(HA_ERR_OUT_OF_MEM);
    }

    /* Initialize the handler buffer. */
    multi_range_buff->buffer= mrange_buff;
    multi_range_buff->buffer_end= mrange_buff + mrange_bufsiz;
    multi_range_buff->end_of_used_area= mrange_buff;
8691 8692 8693 8694 8695 8696 8697 8698
#ifdef HAVE_purify
    /*
      We need this until ndb will use the buffer efficiently
      (Now ndb stores  complete row in here, instead of only the used fields
      which gives us valgrind warnings in compare_record[])
    */
    bzero((char*) mrange_buff, mrange_bufsiz);
#endif
ingo@mysql.com's avatar
ingo@mysql.com committed
8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717
  }
  DBUG_RETURN(0);
}


/*
  Get next possible record using quick-struct.

  SYNOPSIS
    QUICK_RANGE_SELECT::get_next()

  NOTES
    Record is read into table->record[0]

  RETURN
    0			Found row
    HA_ERR_END_OF_FILE	No (more) rows in range
    #			Error code
*/
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8718

8719
int QUICK_RANGE_SELECT::get_next()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8720
{
ingo@mysql.com's avatar
ingo@mysql.com committed
8721 8722 8723 8724
  int             result;
  KEY_MULTI_RANGE *mrange;
  key_range       *start_key;
  key_range       *end_key;
8725
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next");
ingo@mysql.com's avatar
ingo@mysql.com committed
8726 8727 8728
  DBUG_ASSERT(multi_range_length && multi_range &&
              (cur_range >= (QUICK_RANGE**) ranges.buffer) &&
              (cur_range <= (QUICK_RANGE**) ranges.buffer + ranges.elements));
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8729

8730 8731 8732 8733 8734 8735 8736 8737 8738
  if (in_ror_merged_scan)
  {
    /*
      We don't need to signal the bitmap change as the bitmap is always the
      same for this head->file
    */
    head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap);
  }

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8739 8740
  for (;;)
  {
ingo@mysql.com's avatar
ingo@mysql.com committed
8741
    if (in_range)
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8742
    {
ingo@mysql.com's avatar
ingo@mysql.com committed
8743 8744
      /* We did already start to read this key. */
      result= file->read_multi_range_next(&mrange);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8745
      if (result != HA_ERR_END_OF_FILE)
8746
        goto end;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8747
    }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8748

ingo@mysql.com's avatar
ingo@mysql.com committed
8749 8750 8751 8752 8753 8754
    uint count= min(multi_range_length, ranges.elements -
                    (cur_range - (QUICK_RANGE**) ranges.buffer));
    if (count == 0)
    {
      /* Ranges have already been used up before. None is left for read. */
      in_range= FALSE;
8755 8756
      if (in_ror_merged_scan)
        head->column_bitmaps_set_no_signal(save_read_set, save_write_set);
ingo@mysql.com's avatar
ingo@mysql.com committed
8757 8758 8759 8760 8761 8762 8763 8764 8765
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
    KEY_MULTI_RANGE *mrange_slot, *mrange_end;
    for (mrange_slot= multi_range, mrange_end= mrange_slot+count;
         mrange_slot < mrange_end;
         mrange_slot++)
    {
      start_key= &mrange_slot->start_key;
      end_key= &mrange_slot->end_key;
8766
      last_range= *(cur_range++);
ingo@mysql.com's avatar
ingo@mysql.com committed
8767

8768
      start_key->key=    (const uchar*) last_range->min_key;
8769 8770 8771
      start_key->length= last_range->min_length;
      start_key->flag=   ((last_range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
                          (last_range->flag & EQ_RANGE) ?
ingo@mysql.com's avatar
ingo@mysql.com committed
8772
                          HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
8773
      start_key->keypart_map= last_range->min_keypart_map;
8774
      end_key->key=      (const uchar*) last_range->max_key;
8775
      end_key->length=   last_range->max_length;
ingo@mysql.com's avatar
ingo@mysql.com committed
8776 8777 8778 8779
      /*
        We use HA_READ_AFTER_KEY here because if we are reading on a key
        prefix. We want to find all keys with this prefix.
      */
8780
      end_key->flag=     (last_range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
ingo@mysql.com's avatar
ingo@mysql.com committed
8781
                          HA_READ_AFTER_KEY);
serg@janus.mylan's avatar
serg@janus.mylan committed
8782
      end_key->keypart_map= last_range->max_keypart_map;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8783

8784
      mrange_slot->range_flag= last_range->flag;
ingo@mysql.com's avatar
ingo@mysql.com committed
8785
    }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8786

ingo@mysql.com's avatar
ingo@mysql.com committed
8787 8788
    result= file->read_multi_range_first(&mrange, multi_range, count,
                                         sorted, multi_range_buff);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8789
    if (result != HA_ERR_END_OF_FILE)
8790
      goto end;
ingo@mysql.com's avatar
ingo@mysql.com committed
8791
    in_range= FALSE; /* No matching rows; go to next set of ranges. */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8792
  }
8793 8794 8795 8796 8797 8798 8799 8800 8801

end:
  in_range= ! result;
  if (in_ror_merged_scan)
  {
    /* Restore bitmaps set on entry */
    head->column_bitmaps_set_no_signal(save_read_set, save_write_set);
  }
  DBUG_RETURN(result);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8802 8803
}

8804 8805 8806 8807 8808 8809
/*
  Get the next record with a different prefix.

  SYNOPSIS
    QUICK_RANGE_SELECT::get_next_prefix()
    prefix_length  length of cur_prefix
8810
    cur_prefix     prefix of a key to be searched for
8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831

  DESCRIPTION
    Each subsequent call to the method retrieves the first record that has a
    prefix with length prefix_length different from cur_prefix, such that the
    record with the new prefix is within the ranges described by
    this->ranges. The record found is stored into the buffer pointed by
    this->record.
    The method is useful for GROUP-BY queries with range conditions to
    discover the prefix of the next group that satisfies the range conditions.

  TODO
    This method is a modified copy of QUICK_RANGE_SELECT::get_next(), so both
    methods should be unified into a more general one to reduce code
    duplication.

  RETURN
    0                  on success
    HA_ERR_END_OF_FILE if returned all keys
    other              if some error occurred
*/

8832
int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
8833
                                        key_part_map keypart_map,
8834
                                        uchar *cur_prefix)
8835 8836 8837 8838 8839 8840 8841
{
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");

  for (;;)
  {
    int result;
    key_range start_key, end_key;
8842
    if (last_range)
8843 8844
    {
      /* Read the next record in the same range with prefix after cur_prefix. */
8845
      DBUG_ASSERT(cur_prefix != 0);
8846 8847
      result= file->index_read_map(record, cur_prefix, keypart_map,
                                   HA_READ_AFTER_KEY);
8848 8849 8850 8851
      if (result || (file->compare_key(file->end_range) <= 0))
        DBUG_RETURN(result);
    }

ingo@mysql.com's avatar
ingo@mysql.com committed
8852 8853 8854 8855
    uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
    if (count == 0)
    {
      /* Ranges have already been used up before. None is left for read. */
8856
      last_range= 0;
ingo@mysql.com's avatar
ingo@mysql.com committed
8857 8858
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
8859
    last_range= *(cur_range++);
8860

8861
    start_key.key=    (const uchar*) last_range->min_key;
8862
    start_key.length= min(last_range->min_length, prefix_length);
8863
    start_key.keypart_map= last_range->min_keypart_map & keypart_map;
8864 8865
    start_key.flag=   ((last_range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
		       (last_range->flag & EQ_RANGE) ?
8866
		       HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
8867
    end_key.key=      (const uchar*) last_range->max_key;
8868
    end_key.length=   min(last_range->max_length, prefix_length);
8869
    end_key.keypart_map= last_range->max_keypart_map & keypart_map;
8870 8871 8872 8873
    /*
      We use READ_AFTER_KEY here because if we are reading on a key
      prefix we want to find all keys with this prefix
    */
8874
    end_key.flag=     (last_range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
8875 8876
		       HA_READ_AFTER_KEY);

8877 8878
    result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0,
				   last_range->max_keypart_map ? &end_key : 0,
8879
                                   test(last_range->flag & EQ_RANGE),
8880
				   TRUE);
8881 8882
    if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
      last_range= 0;			// Stop searching
8883 8884 8885

    if (result != HA_ERR_END_OF_FILE)
      DBUG_RETURN(result);
8886
    last_range= 0;			// No matching rows; go to next range
8887 8888 8889 8890
  }
}


pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8891
/* Get next for geometrical indexes */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8892

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8893
int QUICK_RANGE_SELECT_GEOM::get_next()
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8894
{
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8895
  DBUG_ENTER("QUICK_RANGE_SELECT_GEOM::get_next");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8896

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8897
  for (;;)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8898
  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8899
    int result;
8900
    if (last_range)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8901
    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8902
      // Already read through key
8903
      result= file->index_next_same(record, last_range->min_key,
8904
				    last_range->min_length);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8905 8906
      if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8907
    }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8908

ingo@mysql.com's avatar
ingo@mysql.com committed
8909 8910 8911 8912
    uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
    if (count == 0)
    {
      /* Ranges have already been used up before. None is left for read. */
8913
      last_range= 0;
ingo@mysql.com's avatar
ingo@mysql.com committed
8914 8915
      DBUG_RETURN(HA_ERR_END_OF_FILE);
    }
8916
    last_range= *(cur_range++);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8917

8918 8919 8920 8921
    result= file->index_read_map(record, last_range->min_key,
                                 last_range->min_keypart_map,
                                 (ha_rkey_function)(last_range->flag ^
                                                    GEOM_FLAG));
8922
    if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
8923
      DBUG_RETURN(result);
8924
    last_range= 0;				// Not found, to next range
bk@work.mysql.com's avatar
bk@work.mysql.com committed
8925 8926 8927
  }
}

8928

8929 8930 8931 8932
/*
  Check if current row will be retrieved by this QUICK_RANGE_SELECT

  NOTES
8933 8934
    It is assumed that currently a scan is being done on another index
    which reads all necessary parts of the index that is scanned by this
8935
    quick select.
8936
    The implementation does a binary search on sorted array of disjoint
8937 8938
    ranges, without taking size of range into account.

8939
    This function is used to filter out clustered PK scan rows in
8940 8941
    index_merge quick select.

8942
  RETURN
monty@mysql.com's avatar
monty@mysql.com committed
8943 8944
    TRUE  if current row will be retrieved by this quick select
    FALSE if not
8945 8946 8947 8948
*/

bool QUICK_RANGE_SELECT::row_in_ranges()
{
8949
  QUICK_RANGE *res;
8950 8951 8952 8953 8954
  uint min= 0;
  uint max= ranges.elements - 1;
  uint mid= (max + min)/2;

  while (min != max)
8955
  {
8956 8957 8958 8959 8960 8961 8962 8963 8964
    if (cmp_next(*(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid)))
    {
      /* current row value > mid->max */
      min= mid + 1;
    }
    else
      max= mid;
    mid= (min + max) / 2;
  }
8965 8966
  res= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
  return (!cmp_next(res) && !cmp_prev(res));
8967 8968
}

8969
/*
8970 8971 8972 8973 8974 8975 8976
  This is a hack: we inherit from QUICK_SELECT so that we can use the
  get_next() interface, but we have to hold a pointer to the original
  QUICK_SELECT because its data are used all over the place.  What
  should be done is to factor out the data that is needed into a base
  class (QUICK_SELECT), and then have two subclasses (_ASC and _DESC)
  which handle the ranges and implement the get_next() function.  But
  for now, this seems to work right at least.
8977
 */
8978

8979
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
8980
                                     uint used_key_parts_arg)
Georgi Kodinov's avatar
Georgi Kodinov committed
8981
 :QUICK_RANGE_SELECT(*q), rev_it(rev_ranges),
8982
  used_key_parts (used_key_parts_arg)
8983
{
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8984
  QUICK_RANGE *r;
8985 8986 8987 8988 8989 8990 8991
  /* 
    Use default MRR implementation for reverse scans. No table engine
    currently can do an MRR scan with output in reverse index order.
  */
  multi_range_length= 0;
  multi_range= NULL;
  multi_range_buff= NULL;
8992

8993
  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
8994 8995
  QUICK_RANGE **end_range= pr + ranges.elements;
  for (; pr!=end_range; pr++)
monty@mysql.com's avatar
monty@mysql.com committed
8996
    rev_ranges.push_front(*pr);
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8997

8998
  /* Remove EQ_RANGE flag for keys that are not using the full key */
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
8999
  for (r = rev_it++; r; r = rev_it++)
9000 9001 9002 9003 9004 9005 9006 9007
  {
    if ((r->flag & EQ_RANGE) &&
	head->key_info[index].key_length != r->max_length)
      r->flag&= ~EQ_RANGE;
  }
  rev_it.rewind();
  q->dont_free=1;				// Don't free shared mem
  delete q;
9008 9009
}

9010

9011 9012 9013 9014 9015 9016
int QUICK_SELECT_DESC::get_next()
{
  DBUG_ENTER("QUICK_SELECT_DESC::get_next");

  /* The max key is handled as follows:
   *   - if there is NO_MAX_RANGE, start at the end and move backwards
9017 9018
   *   - if it is an EQ_RANGE, which means that max key covers the entire
   *     key, go directly to the key and read through it (sorting backwards is
9019 9020 9021 9022 9023 9024 9025 9026 9027 9028
   *     same as sorting forwards)
   *   - if it is NEAR_MAX, go to the key or next, step back once, and
   *     move backwards
   *   - otherwise (not NEAR_MAX == include the key), go after the key,
   *     step back once, and move backwards
   */

  for (;;)
  {
    int result;
9029
    if (last_range)
9030
    {						// Already read through key
9031 9032
      result = ((last_range->flag & EQ_RANGE && 
                 used_key_parts <= head->key_info[index].key_parts) ? 
Georgi Kodinov's avatar
Georgi Kodinov committed
9033
                file->index_next_same(record, last_range->min_key,
9034 9035
                                      last_range->min_length) :
                file->index_prev(record));
9036 9037 9038 9039 9040 9041 9042 9043 9044
      if (!result)
      {
	if (cmp_prev(*rev_it.ref()) == 0)
	  DBUG_RETURN(0);
      }
      else if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
    }

9045
    if (!(last_range= rev_it++))
9046 9047
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used

9048
    if (last_range->flag & NO_MAX_RANGE)        // Read last record
9049
    {
9050 9051 9052
      int local_error;
      if ((local_error=file->index_last(record)))
	DBUG_RETURN(local_error);		// Empty table
9053
      if (cmp_prev(last_range) == 0)
9054
	DBUG_RETURN(0);
9055
      last_range= 0;                            // No match; go to next range
9056 9057 9058
      continue;
    }

9059 9060 9061
    if (last_range->flag & EQ_RANGE &&
        used_key_parts <= head->key_info[index].key_parts)

9062
    {
9063 9064 9065
      result = file->index_read_map(record, last_range->max_key,
                                    last_range->max_keypart_map,
                                    HA_READ_KEY_EXACT);
9066 9067 9068
    }
    else
    {
9069
      DBUG_ASSERT(last_range->flag & NEAR_MAX ||
9070 9071
                  (last_range->flag & EQ_RANGE && 
                   used_key_parts > head->key_info[index].key_parts) ||
9072
                  range_reads_after_key(last_range));
9073 9074 9075 9076 9077
      result=file->index_read_map(record, last_range->max_key,
                                  last_range->max_keypart_map,
                                  ((last_range->flag & NEAR_MAX) ?
                                   HA_READ_BEFORE_KEY :
                                   HA_READ_PREFIX_LAST_OR_PREV));
9078 9079 9080
    }
    if (result)
    {
9081
      if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
9082
	DBUG_RETURN(result);
9083
      last_range= 0;                            // Not found, to next range
9084 9085
      continue;
    }
9086
    if (cmp_prev(last_range) == 0)
9087
    {
9088 9089
      if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
	last_range= 0;				// Stop searching
9090 9091
      DBUG_RETURN(0);				// Found key is in range
    }
9092
    last_range= 0;                              // To next range
9093 9094 9095
  }
}

9096

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109
/*
  Compare if found key is over max-value
  Returns 0 if key <= range->max_key
*/

int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
{
  if (range_arg->flag & NO_MAX_RANGE)
    return 0;                                   /* key can't be to large */

  KEY_PART *key_part=key_parts;
  uint store_length;

9110
  for (uchar *key=range_arg->max_key, *end=key+range_arg->max_length;
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128
       key < end;
       key+= store_length, key_part++)
  {
    int cmp;
    store_length= key_part->store_length;
    if (key_part->null_bit)
    {
      if (*key)
      {
        if (!key_part->field->is_null())
          return 1;
        continue;
      }
      else if (key_part->field->is_null())
        return 0;
      key++;					// Skip null byte
      store_length--;
    }
9129
    if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
9130 9131 9132 9133 9134 9135 9136 9137
      return 0;
    if (cmp > 0)
      return 1;
  }
  return (range_arg->flag & NEAR_MAX) ? 1 : 0;          // Exact match
}


9138
/*
9139 9140 9141
  Returns 0 if found key is inside range (found key >= range->min_key).
*/

9142
int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
9143
{
9144
  int cmp;
9145
  if (range_arg->flag & NO_MIN_RANGE)
9146
    return 0;					/* key can't be to small */
9147

9148
  cmp= key_cmp(key_part_info, range_arg->min_key,
monty@mysql.com's avatar
monty@mysql.com committed
9149
               range_arg->min_length);
9150
  if (cmp > 0 || (cmp == 0 && !(range_arg->flag & NEAR_MIN)))
9151 9152
    return 0;
  return 1;                                     // outside of range
9153 9154
}

9155

9156
/*
monty@mysql.com's avatar
monty@mysql.com committed
9157
 * TRUE if this range will require using HA_READ_AFTER_KEY
9158
   See comment in get_next() about this
9159
 */
9160

9161
bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
9162
{
9163
  return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
9164
	  !(range_arg->flag & EQ_RANGE) ||
9165
	  head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
9166 9167
}

9168

9169 9170 9171 9172 9173 9174 9175 9176 9177
void QUICK_RANGE_SELECT::add_info_string(String *str)
{
  KEY *key_info= head->key_info + index;
  str->append(key_info->name);
}

void QUICK_INDEX_MERGE_SELECT::add_info_string(String *str)
{
  QUICK_RANGE_SELECT *quick;
monty@mysql.com's avatar
monty@mysql.com committed
9178
  bool first= TRUE;
9179
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
9180
  str->append(STRING_WITH_LEN("sort_union("));
9181 9182 9183 9184 9185
  while ((quick= it++))
  {
    if (!first)
      str->append(',');
    else
monty@mysql.com's avatar
monty@mysql.com committed
9186
      first= FALSE;
9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198
    quick->add_info_string(str);
  }
  if (pk_quick_select)
  {
    str->append(',');
    pk_quick_select->add_info_string(str);
  }
  str->append(')');
}

void QUICK_ROR_INTERSECT_SELECT::add_info_string(String *str)
{
9199
  bool first= TRUE;
9200 9201
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
9202
  str->append(STRING_WITH_LEN("intersect("));
9203 9204 9205 9206 9207
  while ((quick= it++))
  {
    KEY *key_info= head->key_info + quick->index;
    if (!first)
      str->append(',');
9208
    else
monty@mysql.com's avatar
monty@mysql.com committed
9209
      first= FALSE;
9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222
    str->append(key_info->name);
  }
  if (cpk_quick)
  {
    KEY *key_info= head->key_info + cpk_quick->index;
    str->append(',');
    str->append(key_info->name);
  }
  str->append(')');
}

void QUICK_ROR_UNION_SELECT::add_info_string(String *str)
{
9223
  bool first= TRUE;
9224 9225
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
9226
  str->append(STRING_WITH_LEN("union("));
9227 9228 9229 9230 9231
  while ((quick= it++))
  {
    if (!first)
      str->append(',');
    else
monty@mysql.com's avatar
monty@mysql.com committed
9232
      first= FALSE;
9233 9234 9235 9236 9237 9238
    quick->add_info_string(str);
  }
  str->append(')');
}


9239
void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
9240
                                              String *used_lengths)
9241 9242 9243 9244 9245 9246 9247 9248 9249
{
  char buf[64];
  uint length;
  KEY *key_info= head->key_info + index;
  key_names->append(key_info->name);
  length= longlong2str(max_used_key_length, buf, 10) - buf;
  used_lengths->append(buf, length);
}

9250 9251
void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
                                                    String *used_lengths)
9252 9253 9254
{
  char buf[64];
  uint length;
monty@mysql.com's avatar
monty@mysql.com committed
9255
  bool first= TRUE;
9256
  QUICK_RANGE_SELECT *quick;
9257

9258 9259 9260
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
9261
    if (first)
monty@mysql.com's avatar
monty@mysql.com committed
9262
      first= FALSE;
9263 9264
    else
    {
9265 9266
      key_names->append(',');
      used_lengths->append(',');
9267
    }
9268

9269 9270
    KEY *key_info= head->key_info + quick->index;
    key_names->append(key_info->name);
9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284
    length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
    used_lengths->append(buf, length);
  }
  if (pk_quick_select)
  {
    KEY *key_info= head->key_info + pk_quick_select->index;
    key_names->append(',');
    key_names->append(key_info->name);
    length= longlong2str(pk_quick_select->max_used_key_length, buf, 10) - buf;
    used_lengths->append(',');
    used_lengths->append(buf, length);
  }
}

9285 9286
void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
                                                      String *used_lengths)
9287 9288 9289
{
  char buf[64];
  uint length;
9290
  bool first= TRUE;
9291 9292 9293 9294 9295 9296
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    KEY *key_info= head->key_info + quick->index;
    if (first)
monty@mysql.com's avatar
monty@mysql.com committed
9297
      first= FALSE;
9298
    else
9299 9300
    {
      key_names->append(',');
9301
      used_lengths->append(',');
9302 9303
    }
    key_names->append(key_info->name);
9304 9305 9306
    length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
    used_lengths->append(buf, length);
  }
9307

9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318
  if (cpk_quick)
  {
    KEY *key_info= head->key_info + cpk_quick->index;
    key_names->append(',');
    key_names->append(key_info->name);
    length= longlong2str(cpk_quick->max_used_key_length, buf, 10) - buf;
    used_lengths->append(',');
    used_lengths->append(buf, length);
  }
}

9319 9320
void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
                                                  String *used_lengths)
9321
{
9322
  bool first= TRUE;
9323 9324 9325 9326 9327
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if (first)
monty@mysql.com's avatar
monty@mysql.com committed
9328
      first= FALSE;
9329
    else
9330
    {
9331 9332 9333
      used_lengths->append(',');
      key_names->append(',');
    }
9334
    quick->add_keys_and_lengths(key_names, used_lengths);
9335 9336 9337
  }
}

9338 9339 9340 9341 9342 9343 9344 9345

/*******************************************************************************
* Implementation of QUICK_GROUP_MIN_MAX_SELECT
*******************************************************************************/

static inline uint get_field_keypart(KEY *index, Field *field);
static inline SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree,
                                             PARAM *param, uint *param_idx);
9346
static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
9347
                       KEY_PART_INFO *first_non_group_part,
9348 9349
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
9350
                       uchar *key_infix, uint *key_infix_len,
9351
                       KEY_PART_INFO **first_non_infix_part);
9352
static bool
9353 9354
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type);
9355

9356 9357 9358 9359 9360 9361
static void
cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
                   uint group_key_parts, SEL_TREE *range_tree,
                   SEL_ARG *index_tree, ha_rows quick_prefix_records,
                   bool have_min, bool have_max,
                   double *read_cost, ha_rows *records);
9362

timour@mysql.com's avatar
timour@mysql.com committed
9363

9364
/**
9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383
  Test if this access method is applicable to a GROUP query with MIN/MAX
  functions, and if so, construct a new TRP object.

  DESCRIPTION
    Test whether a query can be computed via a QUICK_GROUP_MIN_MAX_SELECT.
    Queries computable via a QUICK_GROUP_MIN_MAX_SELECT must satisfy the
    following conditions:
    A) Table T has at least one compound index I of the form:
       I = <A_1, ...,A_k, [B_1,..., B_m], C, [D_1,...,D_n]>
    B) Query conditions:
    B0. Q is over a single table T.
    B1. The attributes referenced by Q are a subset of the attributes of I.
    B2. All attributes QA in Q can be divided into 3 overlapping groups:
        - SA = {S_1, ..., S_l, [C]} - from the SELECT clause, where C is
          referenced by any number of MIN and/or MAX functions if present.
        - WA = {W_1, ..., W_p} - from the WHERE clause
        - GA = <G_1, ..., G_k> - from the GROUP BY clause (if any)
             = SA              - if Q is a DISTINCT query (based on the
                                 equivalence of DISTINCT and GROUP queries.
monty@mysql.com's avatar
monty@mysql.com committed
9384 9385
        - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
          GROUP BY and not referenced by MIN/MAX functions.
9386
        with the following properties specified below.
9387 9388
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not 
        applicable.
9389 9390 9391 9392 9393 9394 9395 9396 9397 9398

    SA1. There is at most one attribute in SA referenced by any number of
         MIN and/or MAX functions which, which if present, is denoted as C.
    SA2. The position of the C attribute in the index is after the last A_k.
    SA3. The attribute C can be referenced in the WHERE clause only in
         predicates of the forms:
         - (C {< | <= | > | >= | =} const)
         - (const {< | <= | > | >= | =} C)
         - (C between const_i and const_j)
         - C IS NULL
9399 9400
         - C IS NOT NULL
         - C != const
9401 9402 9403
    SA4. If Q has a GROUP BY clause, there are no other aggregate functions
         except MIN and MAX. For queries with DISTINCT, aggregate functions
         are allowed.
9404
    SA5. The select list in DISTINCT queries should not contain expressions.
9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418
    GA1. If Q has a GROUP BY clause, then GA is a prefix of I. That is, if
         G_i = A_j => i = j.
    GA2. If Q has a DISTINCT clause, then there is a permutation of SA that
         forms a prefix of I. This permutation is used as the GROUP clause
         when the DISTINCT query is converted to a GROUP query.
    GA3. The attributes in GA may participate in arbitrary predicates, divided
         into two groups:
         - RNG(G_1,...,G_q ; where q <= k) is a range condition over the
           attributes of a prefix of GA
         - PA(G_i1,...G_iq) is an arbitrary predicate over an arbitrary subset
           of GA. Since P is applied to only GROUP attributes it filters some
           groups, and thus can be applied after the grouping.
    GA4. There are no expressions among G_i, just direct column references.
    NGA1.If in the index I there is a gap between the last GROUP attribute G_k,
9419 9420 9421 9422
         and the MIN/MAX attribute C, then NGA must consist of exactly the
         index attributes that constitute the gap. As a result there is a
         permutation of NGA that coincides with the gap in the index
         <B_1, ..., B_m>.
9423 9424 9425 9426 9427 9428 9429
    NGA2.If BA <> {}, then the WHERE clause must contain a conjunction EQ of
         equality conditions for all NG_i of the form (NG_i = const) or
         (const = NG_i), such that each NG_i is referenced in exactly one
         conjunct. Informally, the predicates provide constants to fill the
         gap in the index.
    WA1. There are no other attributes in the WHERE clause except the ones
         referenced in predicates RNG, PA, PC, EQ defined above. Therefore
9430 9431 9432 9433
         WA is subset of (GA union NGA union C) for GA,NGA,C that pass the
         above tests. By transitivity then it also follows that each WA_i
         participates in the index I (if this was already tested for GA, NGA
         and C).
9434 9435

    C) Overall query form:
9436 9437 9438 9439
       SELECT EXPR([A_1,...,A_k], [B_1,...,B_m], [MIN(C)], [MAX(C)])
         FROM T
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
         [AND EQ(B_1,...,B_m)]
9440 9441
         [AND PC(C)]
         [AND PA(A_i1,...,A_iq)]
9442 9443 9444 9445
       GROUP BY A_1,...,A_k
       [HAVING PH(A_1, ..., B_1,..., C)]
    where EXPR(...) is an arbitrary expression over some or all SELECT fields,
    or:
9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474
       SELECT DISTINCT A_i1,...,A_ik
         FROM T
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
         [AND PA(A_i1,...,A_iq)];

  NOTES
    If the current query satisfies the conditions above, and if
    (mem_root! = NULL), then the function constructs and returns a new TRP
    object, that is later used to construct a new QUICK_GROUP_MIN_MAX_SELECT.
    If (mem_root == NULL), then the function only tests whether the current
    query satisfies the conditions above, and, if so, sets
    is_applicable = TRUE.

    Queries with DISTINCT for which index access can be used are transformed
    into equivalent group-by queries of the form:

    SELECT A_1,...,A_k FROM T
     WHERE [RNG(A_1,...,A_p ; where p <= k)]
      [AND PA(A_i1,...,A_iq)]
    GROUP BY A_1,...,A_k;

    The group-by list is a permutation of the select attributes, according
    to their order in the index.

  TODO
  - What happens if the query groups by the MIN/MAX field, and there is no
    other field as in: "select min(a) from t1 group by a" ?
  - We assume that the general correctness of the GROUP-BY query was checked
    before this point. Is this correct, or do we have to check it completely?
9475 9476
  - Lift the limitation in condition (B3), that is, make this access method 
    applicable to ROLLUP queries.
9477

9478 9479 9480 9481 9482 9483
 @param  param     Parameter from test_quick_select
 @param  sel_tree  Range tree generated by get_mm_tree
 @param  read_time Best read time so far (=table/index scan time)
 @return table read plan
   @retval NULL  Loose index scan not applicable or mem_root == NULL
   @retval !NULL Loose index scan table read plan
9484 9485 9486
*/

static TRP_GROUP_MIN_MAX *
9487
get_best_group_min_max(PARAM *param, SEL_TREE *tree, double read_time)
9488 9489
{
  THD *thd= param->thd;
9490
  JOIN *join= thd->lex->current_select->join;
9491 9492 9493
  TABLE *table= param->table;
  bool have_min= FALSE;              /* TRUE if there is a MIN function. */
  bool have_max= FALSE;              /* TRUE if there is a MAX function. */
9494
  Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
9495 9496 9497 9498
  KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
  uint group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
  KEY *index_info= NULL;    /* The index chosen for data access. */
  uint index= 0;            /* The id of the chosen index. */
9499
  uint group_key_parts= 0;  // Number of index key parts in the group prefix.
9500
  uint used_key_parts= 0;   /* Number of index key parts used for access. */
9501
  uchar key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
9502 9503 9504 9505 9506 9507
  uint key_infix_len= 0;          /* Length of key_infix. */
  TRP_GROUP_MIN_MAX *read_plan= NULL; /* The eventually constructed TRP. */
  uint key_part_nr;
  ORDER *tmp_group;
  Item *item;
  Item_field *item_field;
9508 9509 9510
  bool is_agg_distinct;
  List<Item_field> agg_distinct_flds;

9511 9512 9513
  DBUG_ENTER("get_best_group_min_max");

  /* Perform few 'cheap' tests whether this access method is applicable. */
9514
  if (!join)
9515 9516
    DBUG_RETURN(NULL);        /* This is not a select statement. */
  if ((join->tables != 1) ||  /* The query must reference one table. */
9517
      (join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
9518
    DBUG_RETURN(NULL);
9519
  if (table->s->keys == 0)        /* There are no indexes to use. */
9520 9521
    DBUG_RETURN(NULL);

9522
  /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
monty@mishka.local's avatar
monty@mishka.local committed
9523
  if (join->make_sum_func_list(join->all_fields, join->fields_list, 1))
9524
    DBUG_RETURN(NULL);
9525 9526 9527 9528 9529 9530 9531 9532 9533 9534

  List_iterator<Item> select_items_it(join->fields_list);
  is_agg_distinct = is_indexed_agg_distinct(join, &agg_distinct_flds);

  if ((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
      (!join->select_distinct) &&
      !is_agg_distinct)
    DBUG_RETURN(NULL);
  /* Analyze the query in more detail. */

9535
  if (join->sum_funcs[0])
9536
  {
9537 9538 9539
    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
9540
    {
9541 9542 9543 9544
      if (min_max_item->sum_func() == Item_sum::MIN_FUNC)
        have_min= TRUE;
      else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
        have_max= TRUE;
9545 9546 9547 9548
      else if (min_max_item->sum_func() == Item_sum::COUNT_DISTINCT_FUNC ||
               min_max_item->sum_func() == Item_sum::SUM_DISTINCT_FUNC ||
               min_max_item->sum_func() == Item_sum::AVG_DISTINCT_FUNC)
        continue;
9549
      else
9550 9551
        DBUG_RETURN(NULL);

gkodinov/kgeorge@macbook.local's avatar
gkodinov/kgeorge@macbook.local committed
9552
      /* The argument of MIN/MAX. */
9553
      Item *expr= min_max_item->get_arg(0)->real_item();
9554
      if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
9555
      {
9556 9557 9558 9559
        if (! min_max_arg_item)
          min_max_arg_item= (Item_field*) expr;
        else if (! min_max_arg_item->eq(expr, 1))
          DBUG_RETURN(NULL);
9560
      }
9561 9562
      else
        DBUG_RETURN(NULL);
9563
    }
9564 9565 9566 9567 9568
  }
  /* Check (SA5). */
  if (join->select_distinct)
  {
    while ((item= select_items_it++))
9569
    {
9570
      if (item->real_item()->type() != Item::FIELD_ITEM)
9571
        DBUG_RETURN(NULL);
9572 9573 9574 9575 9576 9577
    }
  }

  /* Check (GA4) - that there are no expressions among the group attributes. */
  for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
  {
9578
    if ((*tmp_group->item)->real_item()->type() != Item::FIELD_ITEM)
9579 9580 9581 9582 9583 9584 9585 9586 9587
      DBUG_RETURN(NULL);
  }

  /*
    Check that table has at least one compound index such that the conditions
    (GA1,GA2) are all TRUE. If there is more than one such index, select the
    first one. Here we set the variables: group_prefix_len and index_info.
  */
  KEY *cur_index_info= table->key_info;
9588
  KEY *cur_index_info_end= cur_index_info + table->s->keys;
9589 9590 9591 9592 9593 9594
  /* Cost-related variables for the best index so far. */
  double best_read_cost= DBL_MAX;
  ha_rows best_records= 0;
  SEL_ARG *best_index_tree= NULL;
  ha_rows best_quick_prefix_records= 0;
  uint best_param_idx= 0;
9595 9596

  const uint pk= param->table->s->primary_key;
9597
  uint max_key_part;  
9598 9599
  SEL_ARG *cur_index_tree= NULL;
  ha_rows cur_quick_prefix_records= 0;
9600
  uint cur_param_idx=MAX_KEY;
9601 9602 9603 9604

  for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
       cur_index_info++, cur_index++)
  {
9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617
    KEY_PART_INFO *cur_part;
    KEY_PART_INFO *end_part; /* Last part for loops. */
    /* Last index part. */
    KEY_PART_INFO *last_part;
    KEY_PART_INFO *first_non_group_part;
    KEY_PART_INFO *first_non_infix_part;
    uint key_infix_parts;
    uint cur_group_key_parts= 0;
    uint cur_group_prefix_len= 0;
    double cur_read_cost;
    ha_rows cur_records;
    key_map used_key_parts_map;
    uint cur_key_infix_len= 0;
9618
    uchar cur_key_infix[MAX_KEY_LENGTH];
9619 9620
    uint cur_used_key_parts;
    
9621
    /* Check (B1) - if current index is covering. */
9622
    if (!table->covering_keys.is_set(cur_index))
9623
      goto next_index;
9624

timour@mysql.com's avatar
timour@mysql.com committed
9625 9626 9627 9628 9629 9630 9631 9632 9633 9634
    /*
      If the current storage manager is such that it appends the primary key to
      each index, then the above condition is insufficient to check if the
      index is covering. In such cases it may happen that some fields are
      covered by the PK index, but not by the current index. Since we can't
      use the concatenation of both indexes for index lookup, such an index
      does not qualify as covering in our case. If this is the case, below
      we check that all query fields are indeed covered by 'cur_index'.
    */
    if (pk < MAX_KEY && cur_index != pk &&
9635
        (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
timour@mysql.com's avatar
timour@mysql.com committed
9636 9637 9638 9639 9640 9641
    {
      /* For each table field */
      for (uint i= 0; i < table->s->fields; i++)
      {
        Field *cur_field= table->field[i];
        /*
9642 9643
          If the field is used in the current query ensure that it's
          part of 'cur_index'
timour@mysql.com's avatar
timour@mysql.com committed
9644
        */
9645 9646 9647
        if (bitmap_is_set(table->read_set, cur_field->field_index) &&
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
          goto next_index;                  // Field was not part of key
timour@mysql.com's avatar
timour@mysql.com committed
9648 9649 9650
      }
    }

9651 9652
    max_key_part= 0;
    used_key_parts_map.clear_all();
9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673
    /*
      Check (GA1) for GROUP BY queries.
    */
    if (join->group_list)
    {
      cur_part= cur_index_info->key_part;
      end_part= cur_part + cur_index_info->key_parts;
      /* Iterate in parallel over the GROUP list and the index parts. */
      for (tmp_group= join->group_list; tmp_group && (cur_part != end_part);
           tmp_group= tmp_group->next, cur_part++)
      {
        /*
          TODO:
          tmp_group::item is an array of Item, is it OK to consider only the
          first Item? If so, then why? What is the array for?
        */
        /* Above we already checked that all group items are fields. */
        DBUG_ASSERT((*tmp_group->item)->type() == Item::FIELD_ITEM);
        Item_field *group_field= (Item_field *) (*tmp_group->item);
        if (group_field->field->eq(cur_part->field))
        {
9674 9675
          cur_group_prefix_len+= cur_part->store_length;
          ++cur_group_key_parts;
9676 9677
          max_key_part= cur_part - cur_index_info->key_part + 1;
          used_key_parts_map.set_bit(max_key_part);
9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690
        }
        else
          goto next_index;
      }
    }
    /*
      Check (GA2) if this is a DISTINCT query.
      If GA2, then Store a new ORDER object in group_fields_array at the
      position of the key part of item_field->field. Thus we get the ORDER
      objects for each field ordered as the corresponding key parts.
      Later group_fields_array of ORDER objects is used to convert the query
      to a GROUP query.
    */
9691 9692
    if ((!join->group_list && join->select_distinct) ||
             is_agg_distinct)
9693
    {
9694
      if (!is_agg_distinct)
9695
      {
9696 9697 9698 9699 9700 9701
        select_items_it.rewind();
      }

      List_iterator<Item_field> agg_distinct_flds_it (agg_distinct_flds);
      while (NULL != (item = (is_agg_distinct ?
             (Item *) agg_distinct_flds_it++ : select_items_it++)))
9702
      {
9703 9704 9705 9706 9707 9708 9709 9710
        /* (SA5) already checked above. */
        item_field= (Item_field*) item->real_item(); 
        DBUG_ASSERT(item->real_item()->type() == Item::FIELD_ITEM);

        /* not doing loose index scan for derived tables */
        if (!item_field->field)
          goto next_index;

9711 9712
        /* Find the order of the key part in the index. */
        key_part_nr= get_field_keypart(cur_index_info, item_field->field);
timour@mysql.com's avatar
timour@mysql.com committed
9713 9714 9715 9716
        /*
          Check if this attribute was already present in the select list.
          If it was present, then its corresponding key part was alredy used.
        */
9717
        if (used_key_parts_map.is_set(key_part_nr))
timour@mysql.com's avatar
timour@mysql.com committed
9718
          continue;
9719 9720
        if (key_part_nr < 1 ||
            (!is_agg_distinct && key_part_nr > join->fields_list.elements))
9721 9722
          goto next_index;
        cur_part= cur_index_info->key_part + key_part_nr - 1;
9723
        cur_group_prefix_len+= cur_part->store_length;
9724
        used_key_parts_map.set_bit(key_part_nr);
timour@mysql.com's avatar
timour@mysql.com committed
9725
        ++cur_group_key_parts;
9726
        max_key_part= max(max_key_part,key_part_nr);
9727
      }
9728 9729 9730 9731 9732 9733 9734 9735
      /*
        Check that used key parts forms a prefix of the index.
        To check this we compare bits in all_parts and cur_parts.
        all_parts have all bits set from 0 to (max_key_part-1).
        cur_parts have bits set for only used keyparts.
      */
      ulonglong all_parts, cur_parts;
      all_parts= (1<<max_key_part) - 1;
9736
      cur_parts= used_key_parts_map.to_ulonglong() >> 1;
9737 9738
      if (all_parts != cur_parts)
        goto next_index;
9739 9740 9741 9742 9743 9744
    }

    /* Check (SA2). */
    if (min_max_arg_item)
    {
      key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
9745
      if (key_part_nr <= cur_group_key_parts)
9746 9747 9748 9749 9750 9751 9752 9753
        goto next_index;
      min_max_arg_part= cur_index_info->key_part + key_part_nr - 1;
    }

    /*
      Check (NGA1, NGA2) and extract a sequence of constants to be used as part
      of all search keys.
    */
9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770

    /*
      If there is MIN/MAX, each keypart between the last group part and the
      MIN/MAX part must participate in one equality with constants, and all
      keyparts after the MIN/MAX part must not be referenced in the query.

      If there is no MIN/MAX, the keyparts after the last group part can be
      referenced only in equalities with constants, and the referenced keyparts
      must form a sequence without any gaps that starts immediately after the
      last group keypart.
    */
    last_part= cur_index_info->key_part + cur_index_info->key_parts;
    first_non_group_part= (cur_group_key_parts < cur_index_info->key_parts) ?
                          cur_index_info->key_part + cur_group_key_parts :
                          NULL;
    first_non_infix_part= min_max_arg_part ?
                          (min_max_arg_part < last_part) ?
9771
                             min_max_arg_part :
9772 9773 9774 9775
                             NULL :
                           NULL;
    if (first_non_group_part &&
        (!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
9776
    {
9777 9778 9779 9780 9781 9782 9783
      if (tree)
      {
        uint dummy;
        SEL_ARG *index_range_tree= get_index_range_tree(cur_index, tree, param,
                                                        &dummy);
        if (!get_constant_key_infix(cur_index_info, index_range_tree,
                                    first_non_group_part, min_max_arg_part,
9784 9785
                                    last_part, thd, cur_key_infix, 
                                    &cur_key_infix_len,
9786 9787 9788 9789 9790
                                    &first_non_infix_part))
          goto next_index;
      }
      else if (min_max_arg_part &&
               (min_max_arg_part - first_non_group_part > 0))
timour@mysql.com's avatar
timour@mysql.com committed
9791
      {
9792 9793 9794 9795
        /*
          There is a gap but no range tree, thus no predicates at all for the
          non-group keyparts.
        */
9796
        goto next_index;
timour@mysql.com's avatar
timour@mysql.com committed
9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816
      }
      else if (first_non_group_part && join->conds)
      {
        /*
          If there is no MIN/MAX function in the query, but some index
          key part is referenced in the WHERE clause, then this index
          cannot be used because the WHERE condition over the keypart's
          field cannot be 'pushed' to the index (because there is no
          range 'tree'), and the WHERE clause must be evaluated before
          GROUP BY/DISTINCT.
        */
        /*
          Store the first and last keyparts that need to be analyzed
          into one array that can be passed as parameter.
        */
        KEY_PART_INFO *key_part_range[2];
        key_part_range[0]= first_non_group_part;
        key_part_range[1]= last_part;

        /* Check if cur_part is referenced in the WHERE clause. */
9817
        if (join->conds->walk(&Item::find_item_in_field_list_processor, 0,
9818
                              (uchar*) key_part_range))
timour@mysql.com's avatar
timour@mysql.com committed
9819 9820
          goto next_index;
      }
9821 9822
    }

9823 9824 9825 9826 9827 9828
    /*
      Test (WA1) partially - that no other keypart after the last infix part is
      referenced in the query.
    */
    if (first_non_infix_part)
    {
9829 9830 9831
      cur_part= first_non_infix_part +
                (min_max_arg_part && (min_max_arg_part < last_part));
      for (; cur_part != last_part; cur_part++)
9832
      {
9833
        if (bitmap_is_set(table->read_set, cur_part->field->field_index))
9834 9835 9836 9837
          goto next_index;
      }
    }

9838
    /* If we got to this point, cur_index_info passes the test. */
Ignacio Galarza's avatar
Ignacio Galarza committed
9839
    key_infix_parts= cur_key_infix_len ? (uint) 
9840
                     (first_non_infix_part - first_non_group_part) : 0;
9841
    cur_used_key_parts= cur_group_key_parts + key_infix_parts;
9842

9843 9844 9845 9846 9847 9848 9849 9850
    /* Compute the cost of using this index. */
    if (tree)
    {
      /* Find the SEL_ARG sub-tree that corresponds to the chosen index. */
      cur_index_tree= get_index_range_tree(cur_index, tree, param,
                                           &cur_param_idx);
      /* Check if this range tree can be used for prefix retrieval. */
      cur_quick_prefix_records= check_quick_select(param, cur_param_idx,
9851
                                                    cur_index_tree, TRUE);
9852
    }
9853
    cost_group_min_max(table, cur_index_info, cur_used_key_parts,
9854 9855 9856
                       cur_group_key_parts, tree, cur_index_tree,
                       cur_quick_prefix_records, have_min, have_max,
                       &cur_read_cost, &cur_records);
timour@mysql.com's avatar
timour@mysql.com committed
9857 9858 9859 9860 9861 9862
    /*
      If cur_read_cost is lower than best_read_cost use cur_index.
      Do not compare doubles directly because they may have different
      representations (64 vs. 80 bits).
    */
    if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
9863 9864 9865 9866 9867 9868 9869 9870 9871 9872
    {
      index_info= cur_index_info;
      index= cur_index;
      best_read_cost= cur_read_cost;
      best_records= cur_records;
      best_index_tree= cur_index_tree;
      best_quick_prefix_records= cur_quick_prefix_records;
      best_param_idx= cur_param_idx;
      group_key_parts= cur_group_key_parts;
      group_prefix_len= cur_group_prefix_len;
9873 9874 9875 9876
      key_infix_len= cur_key_infix_len;
      if (key_infix_len)
        memcpy (key_infix, cur_key_infix, sizeof (key_infix));
      used_key_parts= cur_used_key_parts;
9877
    }
9878

9879
  next_index:;
9880 9881 9882 9883
  }
  if (!index_info) /* No usable index found. */
    DBUG_RETURN(NULL);

9884 9885 9886
  /* Check (SA3) for the where clause. */
  if (join->conds && min_max_arg_item &&
      !check_group_min_max_predicates(join->conds, min_max_arg_item,
9887 9888
                                      (index_info->flags & HA_SPATIAL) ?
                                      Field::itMBR : Field::itRAW))
9889 9890 9891 9892
    DBUG_RETURN(NULL);

  /* The query passes all tests, so construct a new TRP object. */
  read_plan= new (param->mem_root)
9893 9894
                 TRP_GROUP_MIN_MAX(have_min, have_max, is_agg_distinct,
                                   min_max_arg_part,
9895 9896 9897
                                   group_prefix_len, used_key_parts,
                                   group_key_parts, index_info, index,
                                   key_infix_len,
9898
                                   (key_infix_len > 0) ? key_infix : NULL,
9899
                                   tree, best_index_tree, best_param_idx,
9900
                                   best_quick_prefix_records);
9901 9902 9903 9904 9905
  if (read_plan)
  {
    if (tree && read_plan->quick_prefix_records == 0)
      DBUG_RETURN(NULL);

9906 9907
    read_plan->read_cost= best_read_cost;
    read_plan->records=   best_records;
9908 9909 9910 9911 9912
    if (read_time < best_read_cost && is_agg_distinct)
    {
      read_plan->read_cost= 0;
      read_plan->use_index_scan();
    }
9913

9914 9915 9916 9917 9918 9919 9920 9921 9922 9923
    DBUG_PRINT("info",
               ("Returning group min/max plan: cost: %g, records: %lu",
                read_plan->read_cost, (ulong) read_plan->records));
  }

  DBUG_RETURN(read_plan);
}


/*
9924 9925
  Check that the MIN/MAX attribute participates only in range predicates
  with constants.
9926 9927 9928 9929 9930 9931

  SYNOPSIS
    check_group_min_max_predicates()
    cond              tree (or subtree) describing all or part of the WHERE
                      clause being analyzed
    min_max_arg_item  the field referenced by the MIN/MAX function(s)
9932
    min_max_arg_part  the keypart of the MIN/MAX argument if any
9933 9934 9935

  DESCRIPTION
    The function walks recursively over the cond tree representing a WHERE
9936
    clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
9937 9938
    aggregate function, it is referenced only by one of the following
    predicates: {=, !=, <, <=, >, >=, between, is null, is not null}.
9939 9940 9941 9942 9943 9944 9945

  RETURN
    TRUE  if cond passes the test
    FALSE o/w
*/

static bool
9946 9947
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type)
9948 9949
{
  DBUG_ENTER("check_group_min_max_predicates");
9950
  DBUG_ASSERT(cond && min_max_arg_item);
9951

gkodinov/kgeorge@macbook.local's avatar
gkodinov/kgeorge@macbook.local committed
9952
  cond= cond->real_item();
9953 9954 9955 9956 9957 9958 9959 9960
  Item::Type cond_type= cond->type();
  if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
  {
    DBUG_PRINT("info", ("Analyzing: %s", ((Item_func*) cond)->func_name()));
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
    Item *and_or_arg;
    while ((and_or_arg= li++))
    {
monty@mishka.local's avatar
monty@mishka.local committed
9961
      if (!check_group_min_max_predicates(and_or_arg, min_max_arg_item,
9962
                                         image_type))
9963 9964 9965 9966 9967
        DBUG_RETURN(FALSE);
    }
    DBUG_RETURN(TRUE);
  }

9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978
  /*
    TODO:
    This is a very crude fix to handle sub-selects in the WHERE clause
    (Item_subselect objects). With the test below we rule out from the
    optimization all queries with subselects in the WHERE clause. What has to
    be done, is that here we should analyze whether the subselect references
    the MIN/MAX argument field, and disallow the optimization only if this is
    so.
  */
  if (cond_type == Item::SUBSELECT_ITEM)
    DBUG_RETURN(FALSE);
9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989

  /*
    Condition of the form 'field' is equivalent to 'field <> 0' and thus
    satisfies the SA3 condition.
  */
  if (cond_type == Item::FIELD_ITEM)
  {
    DBUG_PRINT("info", ("Analyzing: %s", cond->full_name()));
    DBUG_RETURN(TRUE);
  }

9990
  /* We presume that at this point there are no other Items than functions. */
9991 9992 9993 9994 9995 9996 9997 9998 9999
  DBUG_ASSERT(cond_type == Item::FUNC_ITEM);

  /* Test if cond references only group-by or non-group fields. */
  Item_func *pred= (Item_func*) cond;
  Item **arguments= pred->arguments();
  Item *cur_arg;
  DBUG_PRINT("info", ("Analyzing: %s", pred->func_name()));
  for (uint arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
  {
gkodinov/kgeorge@macbook.local's avatar
gkodinov/kgeorge@macbook.local committed
10000
    cur_arg= arguments[arg_idx]->real_item();
10001 10002 10003
    DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
    if (cur_arg->type() == Item::FIELD_ITEM)
    {
10004
      if (min_max_arg_item->eq(cur_arg, 1)) 
10005 10006 10007
      {
       /*
         If pred references the MIN/MAX argument, check whether pred is a range
10008
         condition that compares the MIN/MAX argument with a constant.
10009 10010
       */
        Item_func::Functype pred_type= pred->functype();
10011 10012 10013 10014 10015 10016 10017 10018 10019 10020
        if (pred_type != Item_func::EQUAL_FUNC     &&
            pred_type != Item_func::LT_FUNC        &&
            pred_type != Item_func::LE_FUNC        &&
            pred_type != Item_func::GT_FUNC        &&
            pred_type != Item_func::GE_FUNC        &&
            pred_type != Item_func::BETWEEN        &&
            pred_type != Item_func::ISNULL_FUNC    &&
            pred_type != Item_func::ISNOTNULL_FUNC &&
            pred_type != Item_func::EQ_FUNC        &&
            pred_type != Item_func::NE_FUNC)
10021 10022 10023 10024
          DBUG_RETURN(FALSE);

        /* Check that pred compares min_max_arg_item with a constant. */
        Item *args[3];
10025
        bzero(args, 3 * sizeof(Item*));
10026 10027 10028 10029
        bool inv;
        /* Test if this is a comparison of a field and a constant. */
        if (!simple_pred(pred, args, &inv))
          DBUG_RETURN(FALSE);
10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048

        /* Check for compatible string comparisons - similar to get_mm_leaf. */
        if (args[0] && args[1] && !args[2] && // this is a binary function
            min_max_arg_item->result_type() == STRING_RESULT &&
            /*
              Don't use an index when comparing strings of different collations.
            */
            ((args[1]->result_type() == STRING_RESULT &&
              image_type == Field::itRAW &&
              ((Field_str*) min_max_arg_item->field)->charset() !=
              pred->compare_collation())
             ||
             /*
               We can't always use indexes when comparing a string index to a
               number.
             */
             (args[1]->result_type() != STRING_RESULT &&
              min_max_arg_item->field->cmp_type() != args[1]->result_type())))
          DBUG_RETURN(FALSE);
10049 10050 10051 10052
      }
    }
    else if (cur_arg->type() == Item::FUNC_ITEM)
    {
monty@mishka.local's avatar
monty@mishka.local committed
10053
      if (!check_group_min_max_predicates(cur_arg, min_max_arg_item,
10054
                                         image_type))
10055 10056 10057 10058
        DBUG_RETURN(FALSE);
    }
    else if (cur_arg->const_item())
    {
10059 10060 10061 10062 10063
      /*
        For predicates of the form "const OP expr" we also have to check 'expr'
        to make a decision.
      */
      continue;
10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077
    }
    else
      DBUG_RETURN(FALSE);
  }

  DBUG_RETURN(TRUE);
}


/*
  Extract a sequence of constants from a conjunction of equality predicates.

  SYNOPSIS
    get_constant_key_infix()
10078 10079 10080 10081 10082 10083 10084 10085 10086
    index_info             [in]  Descriptor of the chosen index.
    index_range_tree       [in]  Range tree for the chosen index
    first_non_group_part   [in]  First index part after group attribute parts
    min_max_arg_part       [in]  The keypart of the MIN/MAX argument if any
    last_part              [in]  Last keypart of the index
    thd                    [in]  Current thread
    key_infix              [out] Infix of constants to be used for index lookup
    key_infix_len          [out] Lenghth of the infix
    first_non_infix_part   [out] The first keypart after the infix (if any)
10087 10088 10089
    
  DESCRIPTION
    Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
10090 10091
    for each keypart field NGF_i not in GROUP-BY, check that there is a
    constant equality predicate among conds with the form (NGF_i = const_ci) or
10092 10093
    (const_ci = NGF_i).
    Thus all the NGF_i attributes must fill the 'gap' between the last group-by
10094 10095 10096 10097 10098 10099
    attribute and the MIN/MAX attribute in the index (if present). If these
    conditions hold, copy each constant from its corresponding predicate into
    key_infix, in the order its NG_i attribute appears in the index, and update
    key_infix_len with the total length of the key parts in key_infix.

  RETURN
10100
    TRUE  if the index passes the test
10101 10102 10103 10104
    FALSE o/w
*/

static bool
10105
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
10106
                       KEY_PART_INFO *first_non_group_part,
10107 10108
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
10109
                       uchar *key_infix, uint *key_infix_len,
10110
                       KEY_PART_INFO **first_non_infix_part)
10111 10112 10113
{
  SEL_ARG       *cur_range;
  KEY_PART_INFO *cur_part;
10114 10115
  /* End part for the first loop below. */
  KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
10116 10117

  *key_infix_len= 0;
10118
  uchar *key_ptr= key_infix;
10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132
  for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
  {
    /*
      Find the range tree for the current keypart. We assume that
      index_range_tree points to the leftmost keypart in the index.
    */
    for (cur_range= index_range_tree; cur_range;
         cur_range= cur_range->next_key_part)
    {
      if (cur_range->field->eq(cur_part->field))
        break;
    }
    if (!cur_range)
    {
10133 10134 10135 10136 10137 10138 10139
      if (min_max_arg_part)
        return FALSE; /* The current keypart has no range predicates at all. */
      else
      {
        *first_non_infix_part= cur_part;
        return TRUE;
      }
10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150
    }

    /* Check that the current range tree is a single point interval. */
    if (cur_range->prev || cur_range->next)
      return FALSE; /* This is not the only range predicate for the field. */
    if ((cur_range->min_flag & NO_MIN_RANGE) ||
        (cur_range->max_flag & NO_MAX_RANGE) ||
        (cur_range->min_flag & NEAR_MIN) || (cur_range->max_flag & NEAR_MAX))
      return FALSE;

    uint field_length= cur_part->store_length;
10151
    if (cur_range->maybe_null &&
10152
         cur_range->min_value[0] && cur_range->max_value[0])
10153 10154
    { 
      /*
10155 10156 10157
        cur_range specifies 'IS NULL'. In this case the argument points
        to a "null value" (is_null_string) that may not always be long
        enough for a direct memcpy to a field.
10158 10159 10160 10161 10162 10163 10164 10165 10166
      */
      DBUG_ASSERT (field_length > 0);
      *key_ptr= 1;
      bzero(key_ptr+1,field_length-1);
      key_ptr+= field_length;
      *key_infix_len+= field_length;
    }
    else if (memcmp(cur_range->min_value, cur_range->max_value, field_length) == 0)
    { /* cur_range specifies an equality condition. */
10167 10168 10169 10170 10171 10172 10173 10174
      memcpy(key_ptr, cur_range->min_value, field_length);
      key_ptr+= field_length;
      *key_infix_len+= field_length;
    }
    else
      return FALSE;
  }

10175 10176 10177
  if (!min_max_arg_part && (cur_part == last_part))
    *first_non_infix_part= last_part;

10178 10179 10180 10181
  return TRUE;
}


10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201
/*
  Find the key part referenced by a field.

  SYNOPSIS
    get_field_keypart()
    index  descriptor of an index
    field  field that possibly references some key part in index

  NOTES
    The return value can be used to get a KEY_PART_INFO pointer by
    part= index->key_part + get_field_keypart(...) - 1;

  RETURN
    Positive number which is the consecutive number of the key part, or
    0 if field does not reference any index field.
*/

static inline uint
get_field_keypart(KEY *index, Field *field)
{
10202
  KEY_PART_INFO *part, *end;
10203

10204
  for (part= index->key_part, end= part + index->key_parts; part < end; part++)
10205 10206
  {
    if (field->eq(part->field))
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
10207
      return part - index->key_part + 1;
10208
  }
10209
  return 0;
10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250
}


/*
  Find the SEL_ARG sub-tree that corresponds to the chosen index.

  SYNOPSIS
    get_index_range_tree()
    index     [in]  The ID of the index being looked for
    range_tree[in]  Tree of ranges being searched
    param     [in]  PARAM from SQL_SELECT::test_quick_select
    param_idx [out] Index in the array PARAM::key that corresponds to 'index'

  DESCRIPTION

    A SEL_TREE contains range trees for all usable indexes. This procedure
    finds the SEL_ARG sub-tree for 'index'. The members of a SEL_TREE are
    ordered in the same way as the members of PARAM::key, thus we first find
    the corresponding index in the array PARAM::key. This index is returned
    through the variable param_idx, to be used later as argument of
    check_quick_select().

  RETURN
    Pointer to the SEL_ARG subtree that corresponds to index.
*/

SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree, PARAM *param,
                               uint *param_idx)
{
  uint idx= 0; /* Index nr in param->key_parts */
  while (idx < param->keys)
  {
    if (index == param->real_keynr[idx])
      break;
    idx++;
  }
  *param_idx= idx;
  return(range_tree->keys[idx]);
}


10251
/*
10252
  Compute the cost of a quick_group_min_max_select for a particular index.
10253 10254

  SYNOPSIS
10255 10256 10257 10258 10259 10260 10261
    cost_group_min_max()
    table                [in] The table being accessed
    index_info           [in] The index used to access the table
    used_key_parts       [in] Number of key parts used to access the index
    group_key_parts      [in] Number of index key parts in the group prefix
    range_tree           [in] Tree of ranges for all indexes
    index_tree           [in] The range tree for the current index
monty@mysql.com's avatar
monty@mysql.com committed
10262 10263
    quick_prefix_records [in] Number of records retrieved by the internally
			      used quick range select if any
10264 10265 10266 10267
    have_min             [in] True if there is a MIN function
    have_max             [in] True if there is a MAX function
    read_cost           [out] The cost to retrieve rows via this quick select
    records             [out] The number of rows retrieved
10268 10269

  DESCRIPTION
monty@mysql.com's avatar
monty@mysql.com committed
10270 10271
    This method computes the access cost of a TRP_GROUP_MIN_MAX instance and
    the number of rows returned. It updates this->read_cost and this->records.
10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310

  NOTES
    The cost computation distinguishes several cases:
    1) No equality predicates over non-group attributes (thus no key_infix).
       If groups are bigger than blocks on the average, then we assume that it
       is very unlikely that block ends are aligned with group ends, thus even
       if we look for both MIN and MAX keys, all pairs of neighbor MIN/MAX
       keys, except for the first MIN and the last MAX keys, will be in the
       same block.  If groups are smaller than blocks, then we are going to
       read all blocks.
    2) There are equality predicates over non-group attributes.
       In this case the group prefix is extended by additional constants, and
       as a result the min/max values are inside sub-groups of the original
       groups. The number of blocks that will be read depends on whether the
       ends of these sub-groups will be contained in the same or in different
       blocks. We compute the probability for the two ends of a subgroup to be
       in two different blocks as the ratio of:
       - the number of positions of the left-end of a subgroup inside a group,
         such that the right end of the subgroup is past the end of the buffer
         containing the left-end, and
       - the total number of possible positions for the left-end of the
         subgroup, which is the number of keys in the containing group.
       We assume it is very unlikely that two ends of subsequent subgroups are
       in the same block.
    3) The are range predicates over the group attributes.
       Then some groups may be filtered by the range predicates. We use the
       selectivity of the range predicates to decide how many groups will be
       filtered.

  TODO
     - Take into account the optional range predicates over the MIN/MAX
       argument.
     - Check if we have a PK index and we use all cols - then each key is a
       group, and it will be better to use an index scan.

  RETURN
    None
*/

10311 10312 10313 10314 10315
void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
                        uint group_key_parts, SEL_TREE *range_tree,
                        SEL_ARG *index_tree, ha_rows quick_prefix_records,
                        bool have_min, bool have_max,
                        double *read_cost, ha_rows *records)
10316
{
10317
  ha_rows table_records;
10318 10319 10320 10321 10322 10323 10324 10325 10326 10327
  uint num_groups;
  uint num_blocks;
  uint keys_per_block;
  uint keys_per_group;
  uint keys_per_subgroup; /* Average number of keys in sub-groups */
                          /* formed by a key infix. */
  double p_overlap; /* Probability that a sub-group overlaps two blocks. */
  double quick_prefix_selectivity;
  double io_cost;
  double cpu_cost= 0; /* TODO: CPU cost of index_read calls? */
timour@mysql.com's avatar
timour@mysql.com committed
10328
  DBUG_ENTER("cost_group_min_max");
monty@mysql.com's avatar
monty@mysql.com committed
10329

10330 10331
  table_records= table->file->stats.records;
  keys_per_block= (table->file->stats.block_size / 2 /
10332 10333
                   (index_info->key_length + table->file->ref_length)
                        + 1);
10334
  num_blocks= (uint)(table_records / keys_per_block) + 1;
10335 10336 10337 10338 10339

  /* Compute the number of keys in a group. */
  keys_per_group= index_info->rec_per_key[group_key_parts - 1];
  if (keys_per_group == 0) /* If there is no statistics try to guess */
    /* each group contains 10% of all records */
10340 10341
    keys_per_group= (uint)(table_records / 10) + 1;
  num_groups= (uint)(table_records / keys_per_group) + 1;
10342 10343 10344 10345 10346 10347

  /* Apply the selectivity of the quick select for group prefixes. */
  if (range_tree && (quick_prefix_records != HA_POS_ERROR))
  {
    quick_prefix_selectivity= (double) quick_prefix_records /
                              (double) table_records;
serg@serg.mylan's avatar
serg@serg.mylan committed
10348
    num_groups= (uint) rint(num_groups * quick_prefix_selectivity);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
10349
    set_if_bigger(num_groups, 1);
10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380
  }

  if (used_key_parts > group_key_parts)
  { /*
      Compute the probability that two ends of a subgroup are inside
      different blocks.
    */
    keys_per_subgroup= index_info->rec_per_key[used_key_parts - 1];
    if (keys_per_subgroup >= keys_per_block) /* If a subgroup is bigger than */
      p_overlap= 1.0;       /* a block, it will overlap at least two blocks. */
    else
    {
      double blocks_per_group= (double) num_blocks / (double) num_groups;
      p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
      p_overlap= min(p_overlap, 1.0);
    }
    io_cost= (double) min(num_groups * (1 + p_overlap), num_blocks);
  }
  else
    io_cost= (keys_per_group > keys_per_block) ?
             (have_min && have_max) ? (double) (num_groups + 1) :
                                      (double) num_groups :
             (double) num_blocks;

  /*
    TODO: If there is no WHERE clause and no other expressions, there should be
    no CPU cost. We leave it here to make this cost comparable to that of index
    scan as computed in SQL_SELECT::test_quick_select().
  */
  cpu_cost= (double) num_groups / TIME_FOR_COMPARE;

10381
  *read_cost= io_cost + cpu_cost;
10382
  *records= num_groups;
10383 10384

  DBUG_PRINT("info",
10385 10386 10387
             ("table rows: %lu  keys/block: %u  keys/group: %u  result rows: %lu  blocks: %u",
              (ulong)table_records, keys_per_block, keys_per_group, 
              (ulong) *records, num_blocks));
10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409
  DBUG_VOID_RETURN;
}


/*
  Construct a new quick select object for queries with group by with min/max.

  SYNOPSIS
    TRP_GROUP_MIN_MAX::make_quick()
    param              Parameter from test_quick_select
    retrieve_full_rows ignored
    parent_alloc       Memory pool to use, if any.

  NOTES
    Make_quick ignores the retrieve_full_rows parameter because
    QUICK_GROUP_MIN_MAX_SELECT always performs 'index only' scans.
    The other parameter are ignored as well because all necessary
    data to create the QUICK object is computed at this TRP creation
    time.

  RETURN
    New QUICK_GROUP_MIN_MAX_SELECT object if successfully created,
10410
    NULL otherwise.
10411 10412 10413 10414 10415 10416 10417 10418 10419
*/

QUICK_SELECT_I *
TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool retrieve_full_rows,
                              MEM_ROOT *parent_alloc)
{
  QUICK_GROUP_MIN_MAX_SELECT *quick;
  DBUG_ENTER("TRP_GROUP_MIN_MAX::make_quick");

10420
  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
10421
                                        param->thd->lex->current_select->join,
10422 10423
                                        have_min, have_max, 
                                        have_agg_distinct, min_max_arg_part,
10424 10425 10426
                                        group_prefix_len, group_key_parts,
                                        used_key_parts, index_info, index,
                                        read_cost, records, key_infix_len,
10427
                                        key_infix, parent_alloc, is_index_scan);
10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443
  if (!quick)
    DBUG_RETURN(NULL);

  if (quick->init())
  {
    delete quick;
    DBUG_RETURN(NULL);
  }

  if (range_tree)
  {
    DBUG_ASSERT(quick_prefix_records > 0);
    if (quick_prefix_records == HA_POS_ERROR)
      quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
    else
      /* Make a QUICK_RANGE_SELECT to be used for group prefix retrieval. */
10444 10445
      quick->quick_prefix_select= get_quick_select(param, param_idx,
                                                   index_tree,
10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467
                                                   &quick->alloc);

    /*
      Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
      attribute, and create an array of QUICK_RANGES to be used by the
      new quick select.
    */
    if (min_max_arg_part)
    {
      SEL_ARG *min_max_range= index_tree;
      while (min_max_range) /* Find the tree for the MIN/MAX key part. */
      {
        if (min_max_range->field->eq(min_max_arg_part->field))
          break;
        min_max_range= min_max_range->next_key_part;
      }
      /* Scroll to the leftmost interval for the MIN/MAX argument. */
      while (min_max_range && min_max_range->prev)
        min_max_range= min_max_range->prev;
      /* Create an array of QUICK_RANGEs for the MIN/MAX argument. */
      while (min_max_range)
      {
10468
        if (quick->add_range(min_max_range))
10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481
        {
          delete quick;
          quick= NULL;
          DBUG_RETURN(NULL);
        }
        min_max_range= min_max_range->next;
      }
    }
  }
  else
    quick->quick_prefix_select= NULL;

  quick->update_key_stat();
10482
  quick->adjust_prefix_ranges();
10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506

  DBUG_RETURN(quick);
}


/*
  Construct new quick select for group queries with min/max.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::QUICK_GROUP_MIN_MAX_SELECT()
    table             The table being accessed
    join              Descriptor of the current query
    have_min          TRUE if the query selects a MIN function
    have_max          TRUE if the query selects a MAX function
    min_max_arg_part  The only argument field of all MIN/MAX functions
    group_prefix_len  Length of all key parts in the group prefix
    prefix_key_parts  All key parts in the group prefix
    index_info        The index chosen for data access
    use_index         The id of index_info
    read_cost         Cost of this access method
    records           Number of records returned
    key_infix_len     Length of the key infix appended to the group prefix
    key_infix         Infix of constants from equality predicates
    parent_alloc      Memory pool for this and quick_prefix_select data
10507 10508 10509
    is_index_scan     get the next different key not by jumping on it via
                      index read, but by scanning until the end of the 
                      rows with equal key value.
10510 10511 10512 10513 10514

  RETURN
    None
*/

monty@mysql.com's avatar
monty@mysql.com committed
10515 10516
QUICK_GROUP_MIN_MAX_SELECT::
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
10517
                           bool have_max_arg, bool have_agg_distinct_arg,
monty@mysql.com's avatar
monty@mysql.com committed
10518
                           KEY_PART_INFO *min_max_arg_part_arg,
10519
                           uint group_prefix_len_arg, uint group_key_parts_arg,
monty@mysql.com's avatar
monty@mysql.com committed
10520 10521 10522
                           uint used_key_parts_arg, KEY *index_info_arg,
                           uint use_index, double read_cost_arg,
                           ha_rows records_arg, uint key_infix_len_arg,
10523 10524
                           uchar *key_infix_arg, MEM_ROOT *parent_alloc,
                           bool is_index_scan_arg)
monty@mysql.com's avatar
monty@mysql.com committed
10525
  :join(join_arg), index_info(index_info_arg),
serg@janus.mylan's avatar
serg@janus.mylan committed
10526 10527
   group_prefix_len(group_prefix_len_arg),
   group_key_parts(group_key_parts_arg), have_min(have_min_arg),
10528 10529 10530 10531 10532
   have_max(have_max_arg), have_agg_distinct(have_agg_distinct_arg),
   seen_first_key(FALSE), min_max_arg_part(min_max_arg_part_arg),
   key_infix(key_infix_arg), key_infix_len(key_infix_len_arg),
   min_functions_it(NULL), max_functions_it(NULL), 
   is_index_scan(is_index_scan_arg)
10533 10534 10535 10536 10537 10538
{
  head=       table;
  file=       head->file;
  index=      use_index;
  record=     head->record[0];
  tmp_record= head->record[1];
10539 10540 10541
  read_time= read_cost_arg;
  records= records_arg;
  used_key_parts= used_key_parts_arg;
10542
  real_key_parts= used_key_parts_arg;
10543 10544 10545
  real_prefix_len= group_prefix_len + key_infix_len;
  group_prefix= NULL;
  min_max_arg_len= min_max_arg_part ? min_max_arg_part->store_length : 0;
monty@mysql.com's avatar
monty@mysql.com committed
10546 10547 10548 10549 10550 10551

  /*
    We can't have parent_alloc set as the init function can't handle this case
    yet.
  */
  DBUG_ASSERT(!parent_alloc);
10552 10553 10554
  if (!parent_alloc)
  {
    init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
monty@mysql.com's avatar
monty@mysql.com committed
10555
    join->thd->mem_root= &alloc;
10556 10557
  }
  else
10558
    bzero(&alloc, sizeof(MEM_ROOT));            // ensure that it's not used
10559 10560 10561 10562 10563 10564 10565 10566 10567
}


/*
  Do post-constructor initialization.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::init()
  
10568 10569 10570 10571 10572 10573
  DESCRIPTION
    The method performs initialization that cannot be done in the constructor
    such as memory allocations that may fail. It allocates memory for the
    group prefix and inifix buffers, and for the lists of MIN/MAX item to be
    updated during execution.

10574 10575 10576 10577 10578 10579 10580 10581 10582 10583
  RETURN
    0      OK
    other  Error code
*/

int QUICK_GROUP_MIN_MAX_SELECT::init()
{
  if (group_prefix) /* Already initialized. */
    return 0;

10584
  if (!(last_prefix= (uchar*) alloc_root(&alloc, group_prefix_len)))
10585 10586 10587 10588 10589
      return 1;
  /*
    We may use group_prefix to store keys with all select fields, so allocate
    enough space for it.
  */
10590
  if (!(group_prefix= (uchar*) alloc_root(&alloc,
10591 10592 10593 10594 10595 10596 10597 10598 10599
                                         real_prefix_len + min_max_arg_len)))
    return 1;

  if (key_infix_len > 0)
  {
    /*
      The memory location pointed to by key_infix will be deleted soon, so
      allocate a new buffer and copy the key_infix into it.
    */
10600
    uchar *tmp_key_infix= (uchar*) alloc_root(&alloc, key_infix_len);
10601 10602 10603 10604 10605 10606 10607 10608
    if (!tmp_key_infix)
      return 1;
    memcpy(tmp_key_infix, this->key_infix, key_infix_len);
    this->key_infix= tmp_key_infix;
  }

  if (min_max_arg_part)
  {
monty@mishka.local's avatar
monty@mishka.local committed
10609
    if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16))
10610 10611
      return 1;

10612 10613
    if (have_min)
    {
monty@mishka.local's avatar
monty@mishka.local committed
10614
      if (!(min_functions= new List<Item_sum>))
10615 10616 10617 10618 10619 10620
        return 1;
    }
    else
      min_functions= NULL;
    if (have_max)
    {
monty@mishka.local's avatar
monty@mishka.local committed
10621
      if (!(max_functions= new List<Item_sum>))
10622 10623 10624 10625
        return 1;
    }
    else
      max_functions= NULL;
10626

10627 10628 10629
    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
10630
    {
10631 10632 10633 10634
      if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
        min_functions->push_back(min_max_item);
      else if (have_max && (min_max_item->sum_func() == Item_sum::MAX_FUNC))
        max_functions->push_back(min_max_item);
10635 10636
    }

10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647
    if (have_min)
    {
      if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
        return 1;
    }

    if (have_max)
    {
      if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
        return 1;
    }
10648
  }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
10649 10650
  else
    min_max_ranges.elements= 0;
10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663

  return 0;
}


QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
{
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT");
  if (file->inited != handler::NONE) 
    file->ha_index_end();
  if (min_max_arg_part)
    delete_dynamic(&min_max_ranges);
  free_root(&alloc,MYF(0));
10664 10665
  delete min_functions_it;
  delete max_functions_it;
10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684
  delete quick_prefix_select;
  DBUG_VOID_RETURN; 
}


/*
  Eventually create and add a new quick range object.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::add_range()
    sel_range  Range object from which a 

  NOTES
    Construct a new QUICK_RANGE object from a SEL_ARG object, and
    add it to the array min_max_ranges. If sel_arg is an infinite
    range, e.g. (x < 5 or x > 4), then skip it and do not construct
    a quick range.

  RETURN
10685 10686
    FALSE on success
    TRUE  otherwise
10687 10688 10689 10690 10691 10692 10693 10694
*/

bool QUICK_GROUP_MIN_MAX_SELECT::add_range(SEL_ARG *sel_range)
{
  QUICK_RANGE *range;
  uint range_flag= sel_range->min_flag | sel_range->max_flag;

  /* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
monty@mishka.local's avatar
monty@mishka.local committed
10695
  if ((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
10696
    return FALSE;
10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708

  if (!(sel_range->min_flag & NO_MIN_RANGE) &&
      !(sel_range->max_flag & NO_MAX_RANGE))
  {
    if (sel_range->maybe_null &&
        sel_range->min_value[0] && sel_range->max_value[0])
      range_flag|= NULL_RANGE; /* IS NULL condition */
    else if (memcmp(sel_range->min_value, sel_range->max_value,
                    min_max_arg_len) == 0)
      range_flag|= EQ_RANGE;  /* equality condition */
  }
  range= new QUICK_RANGE(sel_range->min_value, min_max_arg_len,
10709
                         make_keypart_map(sel_range->part),
10710
                         sel_range->max_value, min_max_arg_len,
10711
                         make_keypart_map(sel_range->part),
10712 10713
                         range_flag);
  if (!range)
10714
    return TRUE;
10715
  if (insert_dynamic(&min_max_ranges, (uchar*)&range))
10716 10717
    return TRUE;
  return FALSE;
10718 10719 10720
}


10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749
/*
  Opens the ranges if there are more conditions in quick_prefix_select than
  the ones used for jumping through the prefixes.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges()

  NOTES
    quick_prefix_select is made over the conditions on the whole key.
    It defines a number of ranges of length x. 
    However when jumping through the prefixes we use only the the first 
    few most significant keyparts in the range key. However if there
    are more keyparts to follow the ones we are using we must make the 
    condition on the key inclusive (because x < "ab" means 
    x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
    To achive the above we must turn off the NEAR_MIN/NEAR_MAX
*/
void QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges ()
{
  if (quick_prefix_select &&
      group_prefix_len < quick_prefix_select->max_used_key_length)
  {
    DYNAMIC_ARRAY *arr;
    uint inx;

    for (inx= 0, arr= &quick_prefix_select->ranges; inx < arr->elements; inx++)
    {
      QUICK_RANGE *range;

10750
      get_dynamic(arr, (uchar*)&range, inx);
10751 10752 10753 10754 10755 10756
      range->flag &= ~(NEAR_MIN | NEAR_MAX);
    }
  }
}


10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782
/*
  Determine the total number and length of the keys that will be used for
  index lookup.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()

  DESCRIPTION
    The total length of the keys used for index lookup depends on whether
    there are any predicates referencing the min/max argument, and/or if
    the min/max argument field can be NULL.
    This function does an optimistic analysis whether the search key might
    be extended by a constant for the min/max keypart. It is 'optimistic'
    because during actual execution it may happen that a particular range
    is skipped, and then a shorter key will be used. However this is data
    dependent and can't be easily estimated here.

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
{
  max_used_key_length= real_prefix_len;
  if (min_max_ranges.elements > 0)
  {
10783
    QUICK_RANGE *cur_range;
10784 10785
    if (have_min)
    { /* Check if the right-most range has a lower boundary. */
10786
      get_dynamic(&min_max_ranges, (uchar*)&cur_range,
10787 10788 10789 10790
                  min_max_ranges.elements - 1);
      if (!(cur_range->flag & NO_MIN_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
10791
        used_key_parts++;
10792 10793 10794 10795 10796
        return;
      }
    }
    if (have_max)
    { /* Check if the left-most range has an upper boundary. */
10797
      get_dynamic(&min_max_ranges, (uchar*)&cur_range, 0);
10798 10799 10800
      if (!(cur_range->flag & NO_MAX_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
10801
        used_key_parts++;
10802 10803 10804 10805
        return;
      }
    }
  }
10806 10807
  else if (have_min && min_max_arg_part &&
           min_max_arg_part->field->real_maybe_null())
10808
  {
10809 10810 10811 10812 10813 10814 10815 10816
    /*
      If a MIN/MAX argument value is NULL, we can quickly determine
      that we're in the beginning of the next group, because NULLs
      are always < any other value. This allows us to quickly
      determine the end of the current group and jump to the next
      group (see next_min()) and thus effectively increases the
      usable key length.
    */
10817
    max_used_key_length+= min_max_arg_len;
10818
    used_key_parts++;
10819 10820 10821 10822 10823 10824 10825 10826 10827 10828
  }
}


/*
  Initialize a quick group min/max select for key retrieval.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::reset()

10829 10830 10831 10832
  DESCRIPTION
    Initialize the index chosen for access and find and store the prefix
    of the last group. The method is expensive since it performs disk access.

10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843
  RETURN
    0      OK
    other  Error code
*/

int QUICK_GROUP_MIN_MAX_SELECT::reset(void)
{
  int result;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::reset");

  file->extra(HA_EXTRA_KEYREAD); /* We need only the key attributes */
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
10844
  if ((result= file->ha_index_init(index,1)))
10845
    DBUG_RETURN(result);
sergefp@mysql.com's avatar
sergefp@mysql.com committed
10846 10847
  if (quick_prefix_select && quick_prefix_select->reset())
    DBUG_RETURN(1);
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
10848 10849 10850
  result= file->index_last(record);
  if (result == HA_ERR_END_OF_FILE)
    DBUG_RETURN(0);
10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889
  /* Save the prefix of the last group. */
  key_copy(last_prefix, record, index_info, group_prefix_len);

  DBUG_RETURN(0);
}



/* 
  Get the next key containing the MIN and/or MAX key for the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::get_next()

  DESCRIPTION
    The method finds the next subsequent group of records that satisfies the
    query conditions and finds the keys that contain the MIN/MAX values for
    the key part referenced by the MIN/MAX function(s). Once a group and its
    MIN/MAX values are found, store these values in the Item_sum objects for
    the MIN/MAX functions. The rest of the values in the result row are stored
    in the Item_field::result_field of each select field. If the query does
    not contain MIN and/or MAX functions, then the function only finds the
    group prefix, which is a query answer itself.

  NOTES
    If both MIN and MAX are computed, then we use the fact that if there is
    no MIN key, there can't be a MAX key as well, so we can skip looking
    for a MAX key in this case.

  RETURN
    0                  on success
    HA_ERR_END_OF_FILE if returned all keys
    other              if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::get_next()
{
  int min_res= 0;
  int max_res= 0;
timour@mysql.com's avatar
timour@mysql.com committed
10890 10891 10892 10893 10894 10895 10896
#ifdef HPUX11
  /*
    volatile is required by a bug in the HP compiler due to which the
    last test of result fails.
  */
  volatile int result;
#else
10897
  int result;
timour@mysql.com's avatar
timour@mysql.com committed
10898
#endif
10899
  int is_last_prefix= 0;
10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913

  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::get_next");

  /*
    Loop until a group is found that satisfies all query conditions or the last
    group is reached.
  */
  do
  {
    result= next_prefix();
    /*
      Check if this is the last group prefix. Notice that at this point
      this->record contains the current prefix in record format.
    */
10914 10915 10916 10917 10918 10919 10920 10921 10922 10923
    if (!result)
    {
      is_last_prefix= key_cmp(index_info->key_part, last_prefix,
                              group_prefix_len);
      DBUG_ASSERT(is_last_prefix <= 0);
    }
    else 
    {
      if (result == HA_ERR_KEY_NOT_FOUND)
        continue;
10924
      break;
10925
    }
10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943

    if (have_min)
    {
      min_res= next_min();
      if (min_res == 0)
        update_min_result();
    }
    /* If there is no MIN in the group, there is no MAX either. */
    if ((have_max && !have_min) ||
        (have_max && have_min && (min_res == 0)))
    {
      max_res= next_max();
      if (max_res == 0)
        update_max_result();
      /* If a MIN was found, a MAX must have been found as well. */
      DBUG_ASSERT((have_max && !have_min) ||
                  (have_max && have_min && (max_res == 0)));
    }
10944
    /*
10945
      If this is just a GROUP BY or DISTINCT without MIN or MAX and there
10946 10947 10948 10949
      are equality predicates for the key parts after the group, find the
      first sub-group with the extended prefix.
    */
    if (!have_min && !have_max && key_infix_len > 0)
10950 10951 10952
      result= file->index_read_map(record, group_prefix,
                                   make_prev_keypart_map(real_key_parts),
                                   HA_READ_KEY_EXACT);
10953

10954
    result= have_min ? min_res : have_max ? max_res : result;
10955 10956
  } while ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
           is_last_prefix != 0);
10957

10958
  if (result == HA_ERR_KEY_NOT_FOUND)
10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971
    result= HA_ERR_END_OF_FILE;

  DBUG_RETURN(result);
}


/*
  Retrieve the minimal key in the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_min()

  DESCRIPTION
10972 10973
    Find the minimal key within this group such that the key satisfies the query
    conditions and NULL semantics. The found key is loaded into this->record.
10974 10975 10976 10977 10978 10979 10980 10981 10982 10983

  IMPLEMENTATION
    Depending on the values of min_max_ranges.elements, key_infix_len, and
    whether there is a  NULL in the MIN field, this function may directly
    return without any data access. In this case we use the key loaded into
    this->record by the call to this->next_prefix() just before this call.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if no MIN key was found that fulfills all conditions.
10984
    HA_ERR_END_OF_FILE   - "" -
10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000
    other                if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_min()
{
  int result= 0;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_min");

  /* Find the MIN key using the eventually extended group prefix. */
  if (min_max_ranges.elements > 0)
  {
    if ((result= next_min_in_range()))
      DBUG_RETURN(result);
  }
  else
  {
11001
    /* Apply the constant equality conditions to the non-group select fields */
11002 11003
    if (key_infix_len > 0)
    {
11004 11005 11006
      if ((result= file->index_read_map(record, group_prefix,
                                        make_prev_keypart_map(real_key_parts),
                                        HA_READ_KEY_EXACT)))
11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020
        DBUG_RETURN(result);
    }

    /*
      If the min/max argument field is NULL, skip subsequent rows in the same
      group with NULL in it. Notice that:
      - if the first row in a group doesn't have a NULL in the field, no row
      in the same group has (because NULL < any other value),
      - min_max_arg_part->field->ptr points to some place in 'record'.
    */
    if (min_max_arg_part && min_max_arg_part->field->is_null())
    {
      /* Find the first subsequent record without NULL in the MIN/MAX field. */
      key_copy(tmp_record, record, index_info, 0);
11021 11022 11023
      result= file->index_read_map(record, tmp_record,
                                   make_keypart_map(real_key_parts),
                                   HA_READ_AFTER_KEY);
11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035
      /*
        Check if the new record belongs to the current group by comparing its
        prefix with the group's prefix. If it is from the next group, then the
        whole group has NULLs in the MIN/MAX field, so use the first record in
        the group as a result.
        TODO:
        It is possible to reuse this new record as the result candidate for the
        next call to next_min(), and to save one lookup in the next call. For
        this add a new member 'this->next_group_prefix'.
      */
      if (!result)
      {
11036
        if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
11037
          key_restore(record, tmp_record, index_info, 0);
11038
      }
11039
      else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE)
11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058
        result= 0; /* There is a result in any case. */
    }
  }

  /*
    If the MIN attribute is non-nullable, this->record already contains the
    MIN key in the group, so just return.
  */
  DBUG_RETURN(result);
}


/* 
  Retrieve the maximal key in the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_max()

  DESCRIPTION
11059
    Lookup the maximal key of the group, and store it into this->record.
11060 11061 11062 11063

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
11064
    HA_ERR_END_OF_FILE	 - "" -
11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077
    other                if some error occurred
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_max()
{
  int result;

  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_max");

  /* Get the last key in the (possibly extended) group. */
  if (min_max_ranges.elements > 0)
    result= next_max_in_range();
  else
11078 11079 11080
    result= file->index_read_map(record, group_prefix,
                                 make_prev_keypart_map(real_key_parts),
                                 HA_READ_PREFIX_LAST);
11081 11082 11083 11084
  DBUG_RETURN(result);
}


11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134
/** 
  Find the next different key value by skiping all the rows with the same key 
  value.

  Implements a specialized loose index access method for queries 
  containing aggregate functions with distinct of the form:
    SELECT [SUM|COUNT|AVG](DISTINCT a,...) FROM t
  This method comes to replace the index scan + Unique class 
  (distinct selection) for loose index scan that visits all the rows of a 
  covering index instead of jumping in the begining of each group.
  TODO: Placeholder function. To be replaced by a handler API call

  @param is_index_scan     hint to use index scan instead of random index read 
                           to find the next different value.
  @param file              table handler
  @param key_part          group key to compare
  @param record            row data
  @param group_prefix      current key prefix data
  @param group_prefix_len  length of the current key prefix data
  @param group_key_parts   number of the current key prefix columns
  @return status
    @retval  0  success
    @retval !0  failure
*/

static int index_next_different (bool is_index_scan, handler *file, 
                                KEY_PART_INFO *key_part, uchar * record, 
                                const uchar * group_prefix,
                                uint group_prefix_len, 
                                uint group_key_parts)
{
  if (is_index_scan)
  {
    int result= 0;

    while (!key_cmp (key_part, group_prefix, group_prefix_len))
    {
      result= file->index_next(record);
      if (result)
        return(result);
    }
    return result;
  }
  else
    return file->index_read_map(record, group_prefix,
                                make_prev_keypart_map(group_key_parts),
                                HA_READ_AFTER_KEY);
}


11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162
/*
  Determine the prefix of the next group.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_prefix()

  DESCRIPTION
    Determine the prefix of the next group that satisfies the query conditions.
    If there is a range condition referencing the group attributes, use a
    QUICK_RANGE_SELECT object to retrieve the *first* key that satisfies the
    condition. If there is a key infix of constants, append this infix
    immediately after the group attributes. The possibly extended prefix is
    stored in this->group_prefix. The first key of the found group is stored in
    this->record, on which relies this->next_min().

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the formed prefix
    HA_ERR_END_OF_FILE   if there are no more keys
    other                if some error occurred
*/
int QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
{
  int result;
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_prefix");

  if (quick_prefix_select)
  {
11163
    uchar *cur_prefix= seen_first_key ? group_prefix : NULL;
11164
    if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
11165
                         make_prev_keypart_map(group_key_parts), cur_prefix)))
11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180
      DBUG_RETURN(result);
    seen_first_key= TRUE;
  }
  else
  {
    if (!seen_first_key)
    {
      result= file->index_first(record);
      if (result)
        DBUG_RETURN(result);
      seen_first_key= TRUE;
    }
    else
    {
      /* Load the first key in this group into record. */
11181 11182 11183
      result= index_next_different (is_index_scan, file, index_info->key_part,
                            record, group_prefix, group_prefix_len, 
                            group_key_parts);
11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216
      if (result)
        DBUG_RETURN(result);
    }
  }

  /* Save the prefix of this group for subsequent calls. */
  key_copy(group_prefix, record, index_info, group_prefix_len);
  /* Append key_infix to group_prefix. */
  if (key_infix_len > 0)
    memcpy(group_prefix + group_prefix_len,
           key_infix, key_infix_len);

  DBUG_RETURN(0);
}


/*
  Find the minimal key in a group that satisfies some range conditions for the
  min/max argument field.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()

  DESCRIPTION
    Given the sequence of ranges min_max_ranges, find the minimal key that is
    in the left-most possible range. If there is no such key, then the current
    group does not have a MIN key that satisfies the WHERE clause. If a key is
    found, its value is stored in this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
                         the ranges
11217
    HA_ERR_END_OF_FILE   - "" -
11218 11219 11220 11221 11222 11223
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
{
  ha_rkey_function find_flag;
11224
  key_part_map keypart_map;
11225 11226 11227 11228 11229 11230 11231 11232
  QUICK_RANGE *cur_range;
  bool found_null= FALSE;
  int result= HA_ERR_KEY_NOT_FOUND;

  DBUG_ASSERT(min_max_ranges.elements > 0);

  for (uint range_idx= 0; range_idx < min_max_ranges.elements; range_idx++)
  { /* Search from the left-most range to the right. */
11233
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx);
11234 11235 11236 11237 11238 11239

    /*
      If the current value for the min/max argument is bigger than the right
      boundary of cur_range, there is no need to check this range.
    */
    if (range_idx != 0 && !(cur_range->flag & NO_MAX_RANGE) &&
11240
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->max_key,
11241
                 min_max_arg_len) == 1))
11242 11243 11244 11245
      continue;

    if (cur_range->flag & NO_MIN_RANGE)
    {
11246
      keypart_map= make_prev_keypart_map(real_key_parts);
11247
      find_flag= HA_READ_KEY_EXACT;
11248 11249 11250 11251 11252 11253
    }
    else
    {
      /* Extend the search key with the lower boundary for this range. */
      memcpy(group_prefix + real_prefix_len, cur_range->min_key,
             cur_range->min_length);
11254
      keypart_map= make_keypart_map(real_key_parts);
11255 11256 11257 11258 11259
      find_flag= (cur_range->flag & (EQ_RANGE | NULL_RANGE)) ?
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MIN) ?
                 HA_READ_AFTER_KEY : HA_READ_KEY_OR_NEXT;
    }

11260
    result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
11261
    if (result)
11262
    {
11263 11264 11265 11266
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
          (cur_range->flag & (EQ_RANGE | NULL_RANGE)))
        continue; /* Check the next range. */

11267 11268 11269 11270 11271
      /*
        In all other cases (HA_ERR_*, HA_READ_KEY_EXACT with NO_MIN_RANGE,
        HA_READ_AFTER_KEY, HA_READ_KEY_OR_NEXT) if the lookup failed for this
        range, it can't succeed for any other subsequent range.
      */
11272
      break;
11273
    }
11274 11275 11276 11277 11278 11279

    /* A key was found. */
    if (cur_range->flag & EQ_RANGE)
      break; /* No need to perform the checks below for equal keys. */

    if (cur_range->flag & NULL_RANGE)
11280 11281 11282 11283 11284 11285
    {
      /*
        Remember this key, and continue looking for a non-NULL key that
        satisfies some other condition.
      */
      memcpy(tmp_record, record, head->s->rec_buff_length);
11286 11287 11288 11289 11290 11291 11292
      found_null= TRUE;
      continue;
    }

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
    {
11293
      result= HA_ERR_KEY_NOT_FOUND;
11294 11295 11296 11297 11298 11299 11300
      continue;
    }

    /* If there is an upper limit, check if the found key is in the range. */
    if ( !(cur_range->flag & NO_MAX_RANGE) )
    {
      /* Compose the MAX key for the range. */
11301
      uchar *max_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
11302 11303 11304 11305 11306 11307
      memcpy(max_key, group_prefix, real_prefix_len);
      memcpy(max_key + real_prefix_len, cur_range->max_key,
             cur_range->max_length);
      /* Compare the found key with max_key. */
      int cmp_res= key_cmp(index_info->key_part, max_key,
                           real_prefix_len + min_max_arg_len);
11308 11309 11310 11311 11312 11313 11314 11315
      /*
        The key is outside of the range if: 
        the interval is open and the key is equal to the maximum boundry
        or
        the key is greater than the maximum
      */
      if (((cur_range->flag & NEAR_MAX) && cmp_res == 0) ||
          cmp_res > 0)
11316
      {
11317
        result= HA_ERR_KEY_NOT_FOUND;
11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331
        continue;
      }
    }
    /* If we got to this point, the current key qualifies as MIN. */
    DBUG_ASSERT(result == 0);
    break;
  }
  /*
    If there was a key with NULL in the MIN/MAX field, and there was no other
    key without NULL from the same group that satisfies some other condition,
    then use the key with the NULL.
  */
  if (found_null && result)
  {
11332
    memcpy(record, tmp_record, head->s->rec_buff_length);
11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355
    result= 0;
  }
  return result;
}


/*
  Find the maximal key in a group that satisfies some range conditions for the
  min/max argument field.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()

  DESCRIPTION
    Given the sequence of ranges min_max_ranges, find the maximal key that is
    in the right-most possible range. If there is no such key, then the current
    group does not have a MAX key that satisfies the WHERE clause. If a key is
    found, its value is stored in this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
                         the ranges
11356
    HA_ERR_END_OF_FILE   - "" -
11357 11358 11359 11360 11361 11362
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
{
  ha_rkey_function find_flag;
11363
  key_part_map keypart_map;
11364 11365 11366 11367 11368 11369 11370
  QUICK_RANGE *cur_range;
  int result;

  DBUG_ASSERT(min_max_ranges.elements > 0);

  for (uint range_idx= min_max_ranges.elements; range_idx > 0; range_idx--)
  { /* Search from the right-most range to the left. */
11371
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx - 1);
11372 11373 11374 11375 11376 11377 11378

    /*
      If the current value for the min/max argument is smaller than the left
      boundary of cur_range, there is no need to check this range.
    */
    if (range_idx != min_max_ranges.elements &&
        !(cur_range->flag & NO_MIN_RANGE) &&
11379
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->min_key,
11380
                 min_max_arg_len) == -1))
11381 11382 11383 11384
      continue;

    if (cur_range->flag & NO_MAX_RANGE)
    {
11385
      keypart_map= make_prev_keypart_map(real_key_parts);
11386
      find_flag= HA_READ_PREFIX_LAST;
11387 11388 11389 11390 11391 11392
    }
    else
    {
      /* Extend the search key with the upper boundary for this range. */
      memcpy(group_prefix + real_prefix_len, cur_range->max_key,
             cur_range->max_length);
11393
      keypart_map= make_keypart_map(real_key_parts);
11394 11395 11396 11397 11398
      find_flag= (cur_range->flag & EQ_RANGE) ?
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MAX) ?
                 HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV;
    }

11399
    result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
11400

monty@mysql.com's avatar
monty@mysql.com committed
11401 11402
    if (result)
    {
11403 11404 11405 11406
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
          (cur_range->flag & EQ_RANGE))
        continue; /* Check the next range. */

11407 11408 11409 11410 11411
      /*
        In no key was found with this upper bound, there certainly are no keys
        in the ranges to the left.
      */
      return result;
monty@mysql.com's avatar
monty@mysql.com committed
11412
    }
11413 11414
    /* A key was found. */
    if (cur_range->flag & EQ_RANGE)
monty@mysql.com's avatar
monty@mysql.com committed
11415
      return 0; /* No need to perform the checks below for equal keys. */
11416 11417 11418

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
monty@mysql.com's avatar
monty@mysql.com committed
11419
      continue;                                 // Row not found
11420 11421 11422 11423 11424

    /* If there is a lower limit, check if the found key is in the range. */
    if ( !(cur_range->flag & NO_MIN_RANGE) )
    {
      /* Compose the MIN key for the range. */
11425
      uchar *min_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
11426 11427 11428 11429 11430 11431
      memcpy(min_key, group_prefix, real_prefix_len);
      memcpy(min_key + real_prefix_len, cur_range->min_key,
             cur_range->min_length);
      /* Compare the found key with min_key. */
      int cmp_res= key_cmp(index_info->key_part, min_key,
                           real_prefix_len + min_max_arg_len);
11432 11433 11434 11435 11436 11437 11438 11439
      /*
        The key is outside of the range if: 
        the interval is open and the key is equal to the minimum boundry
        or
        the key is less than the minimum
      */
      if (((cur_range->flag & NEAR_MIN) && cmp_res == 0) ||
          cmp_res < 0)
11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513
        continue;
    }
    /* If we got to this point, the current key qualifies as MAX. */
    return result;
  }
  return HA_ERR_KEY_NOT_FOUND;
}


/*
  Update all MIN function results with the newly found value.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_min_result()

  DESCRIPTION
    The method iterates through all MIN functions and updates the result value
    of each function by calling Item_sum::reset(), which in turn picks the new
    result value from this->head->record[0], previously updated by
    next_min(). The updated value is stored in a member variable of each of the
    Item_sum objects, depending on the value type.

  IMPLEMENTATION
    The update must be done separately for MIN and MAX, immediately after
    next_min() was called and before next_max() is called, because both MIN and
    MAX take their result value from the same buffer this->head->record[0]
    (i.e.  this->record).

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
{
  Item_sum *min_func;

  min_functions_it->rewind();
  while ((min_func= (*min_functions_it)++))
    min_func->reset();
}


/*
  Update all MAX function results with the newly found value.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::update_max_result()

  DESCRIPTION
    The method iterates through all MAX functions and updates the result value
    of each function by calling Item_sum::reset(), which in turn picks the new
    result value from this->head->record[0], previously updated by
    next_max(). The updated value is stored in a member variable of each of the
    Item_sum objects, depending on the value type.

  IMPLEMENTATION
    The update must be done separately for MIN and MAX, immediately after
    next_max() was called, because both MIN and MAX take their result value
    from the same buffer this->head->record[0] (i.e.  this->record).

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
{
  Item_sum *max_func;

  max_functions_it->rewind();
  while ((max_func= (*max_functions_it)++))
    max_func->reset();
}


11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528
/*
  Append comma-separated list of keys this quick select uses to key_names;
  append comma-separated list of corresponding used lengths to used_lengths.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths()
    key_names    [out] Names of used indexes
    used_lengths [out] Corresponding lengths of the index names

  DESCRIPTION
    This method is used by select_describe to extract the names of the
    indexes used by a quick select.

*/

11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539
void QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths(String *key_names,
                                                      String *used_lengths)
{
  char buf[64];
  uint length;
  key_names->append(index_info->name);
  length= longlong2str(max_used_key_length, buf, 10) - buf;
  used_lengths->append(buf, length);
}


11540
#ifndef DBUG_OFF
11541

11542 11543 11544 11545 11546 11547 11548
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
                           const char *msg)
{
  SEL_ARG **key,**end;
  int idx;
  char buff[1024];
  DBUG_ENTER("print_sel_tree");
11549

11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564
  String tmp(buff,sizeof(buff),&my_charset_bin);
  tmp.length(0);
  for (idx= 0,key=tree->keys, end=key+param->keys ;
       key != end ;
       key++,idx++)
  {
    if (tree_map->is_set(idx))
    {
      uint keynr= param->real_keynr[idx];
      if (tmp.length())
        tmp.append(',');
      tmp.append(param->table->key_info[keynr].name);
    }
  }
  if (!tmp.length())
11565
    tmp.append(STRING_WITH_LEN("(empty)"));
11566

11567
  DBUG_PRINT("info", ("SEL_TREE: 0x%lx (%s)  scans: %s", (long) tree, msg, tmp.ptr()));
11568

11569 11570
  DBUG_VOID_RETURN;
}
11571

11572 11573 11574 11575

static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
                                struct st_ror_scan_info **end)
11576
{
serg@serg.mylan's avatar
serg@serg.mylan committed
11577
  DBUG_ENTER("print_ror_scans_arr");
11578 11579 11580 11581

  char buff[1024];
  String tmp(buff,sizeof(buff),&my_charset_bin);
  tmp.length(0);
11582
  for (;start != end; start++)
11583
  {
11584 11585 11586
    if (tmp.length())
      tmp.append(',');
    tmp.append(table->key_info[(*start)->keynr].name);
11587
  }
11588
  if (!tmp.length())
11589
    tmp.append(STRING_WITH_LEN("(empty)"));
11590 11591
  DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.ptr()));
  DBUG_VOID_RETURN;
11592 11593
}

bk@work.mysql.com's avatar
bk@work.mysql.com committed
11594 11595 11596 11597 11598 11599 11600 11601
/*****************************************************************************
** Print a quick range for debugging
** TODO:
** This should be changed to use a String to store each row instead
** of locking the DEBUG stream !
*****************************************************************************/

static void
11602
print_key(KEY_PART *key_part, const uchar *key, uint used_length)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11603 11604
{
  char buff[1024];
11605
  const uchar *key_end= key+used_length;
11606
  String tmp(buff,sizeof(buff),&my_charset_bin);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11607
  uint store_length;
11608
  TABLE *table= key_part->field->table;
11609 11610 11611
  my_bitmap_map *old_sets[2];

  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11612

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11613
  for (; key < key_end; key+=store_length, key_part++)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11614
  {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11615 11616 11617
    Field *field=      key_part->field;
    store_length= key_part->store_length;

bk@work.mysql.com's avatar
bk@work.mysql.com committed
11618 11619
    if (field->real_maybe_null())
    {
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11620
      if (*key)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11621 11622 11623 11624
      {
	fwrite("NULL",sizeof(char),4,DBUG_FILE);
	continue;
      }
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11625 11626
      key++;					// Skip null byte
      store_length--;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11627
    }
11628
    field->set_key_image(key, key_part->length);
monty@mysql.com's avatar
monty@mysql.com committed
11629 11630 11631 11632
    if (field->type() == MYSQL_TYPE_BIT)
      (void) field->val_int_as_str(&tmp, 1);
    else
      field->val_str(&tmp);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11633
    fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11634 11635
    if (key+store_length < key_end)
      fputc('/',DBUG_FILE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11636
  }
11637
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11638 11639
}

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11640

11641
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
11642
{
11643
  char buf[MAX_KEY/8+1];
11644
  TABLE *table;
11645
  my_bitmap_map *old_sets[2];
11646
  DBUG_ENTER("print_quick");
serg@serg.mylan's avatar
serg@serg.mylan committed
11647
  if (!quick)
11648
    DBUG_VOID_RETURN;
11649
  DBUG_LOCK_FILE;
11650

11651
  table= quick->head;
11652
  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);
monty@mysql.com's avatar
monty@mysql.com committed
11653
  quick->dbug_dump(0, TRUE);
11654
  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
11655

11656
  fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));
11657

11658
  DBUG_UNLOCK_FILE;
11659 11660 11661
  DBUG_VOID_RETURN;
}

pem@mysql.comhem.se's avatar
pem@mysql.comhem.se committed
11662

11663 11664
void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
{
11665
  /* purecov: begin inspected */
11666 11667
  fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
	  indent, "", head->key_info[index].name, max_used_key_length);
11668

11669
  if (verbose)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11670
  {
11671 11672
    QUICK_RANGE *range;
    QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
11673 11674
    QUICK_RANGE **end_range= pr + ranges.elements;
    for (; pr != end_range; ++pr)
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11675
    {
11676 11677 11678 11679
      fprintf(DBUG_FILE, "%*s", indent + 2, "");
      range= *pr;
      if (!(range->flag & NO_MIN_RANGE))
      {
11680
        print_key(key_parts, range->min_key, range->min_length);
11681 11682 11683 11684 11685 11686
        if (range->flag & NEAR_MIN)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
      }
      fputs("X",DBUG_FILE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11687

11688 11689 11690 11691 11692 11693
      if (!(range->flag & NO_MAX_RANGE))
      {
        if (range->flag & NEAR_MAX)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
11694
        print_key(key_parts, range->max_key, range->max_length);
11695 11696
      }
      fputs("\n",DBUG_FILE);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11697 11698
    }
  }
11699
  /* purecov: end */    
11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711
}

void QUICK_INDEX_MERGE_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  QUICK_RANGE_SELECT *quick;
  fprintf(DBUG_FILE, "%*squick index_merge select\n", indent, "");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
    quick->dbug_dump(indent+2, verbose);
  if (pk_quick_select)
  {
11712
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
11713 11714 11715 11716 11717 11718 11719 11720 11721
    pk_quick_select->dbug_dump(indent+2, verbose);
  }
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
}

void QUICK_ROR_INTERSECT_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  QUICK_RANGE_SELECT *quick;
11722
  fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
11723 11724 11725
          indent, "", need_to_fetch_row? "":"non-");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
11726
    quick->dbug_dump(indent+2, verbose);
11727 11728
  if (cpk_quick)
  {
11729
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743
    cpk_quick->dbug_dump(indent+2, verbose);
  }
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
}

void QUICK_ROR_UNION_SELECT::dbug_dump(int indent, bool verbose)
{
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  QUICK_SELECT_I *quick;
  fprintf(DBUG_FILE, "%*squick ROR-union select\n", indent, "");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
    quick->dbug_dump(indent+2, verbose);
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11744 11745
}

11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789

/*
  Print quick select information to DBUG_FILE.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::dbug_dump()
    indent  Indentation offset
    verbose If TRUE show more detailed output.

  DESCRIPTION
    Print the contents of this quick select to DBUG_FILE. The method also
    calls dbug_dump() for the used quick select if any.

  IMPLEMENTATION
    Caller is responsible for locking DBUG_FILE before this call and unlocking
    it afterwards.

  RETURN
    None
*/

void QUICK_GROUP_MIN_MAX_SELECT::dbug_dump(int indent, bool verbose)
{
  fprintf(DBUG_FILE,
          "%*squick_group_min_max_select: index %s (%d), length: %d\n",
	  indent, "", index_info->name, index, max_used_key_length);
  if (key_infix_len > 0)
  {
    fprintf(DBUG_FILE, "%*susing key_infix with length %d:\n",
            indent, "", key_infix_len);
  }
  if (quick_prefix_select)
  {
    fprintf(DBUG_FILE, "%*susing quick_range_select:\n", indent, "");
    quick_prefix_select->dbug_dump(indent + 2, verbose);
  }
  if (min_max_ranges.elements > 0)
  {
    fprintf(DBUG_FILE, "%*susing %d quick_ranges for MIN/MAX:\n",
            indent, "", min_max_ranges.elements);
  }
}


monty@mysql.com's avatar
monty@mysql.com committed
11790
#endif /* NOT_USED */
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11791 11792

/*****************************************************************************
gkodinov@dl145s.mysql.com's avatar
gkodinov@dl145s.mysql.com committed
11793
** Instantiate templates 
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11794 11795
*****************************************************************************/

11796
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
bk@work.mysql.com's avatar
bk@work.mysql.com committed
11797 11798 11799
template class List<QUICK_RANGE>;
template class List_iterator<QUICK_RANGE>;
#endif