opt_range.cc 256 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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 */

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

unknown's avatar
unknown committed
22 23
  select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);

unknown's avatar
unknown committed
24 25
*/

26 27
/*
  Classes in this file are used in the following way:
unknown's avatar
unknown committed
28 29
  1. For a selection condition a tree of SEL_IMERGE/SEL_TREE/SEL_ARG objects
     is created. #of rows in table and index statistics are ignored at this
30
     step.
unknown's avatar
unknown committed
31 32 33 34
  2. Created SEL_TREE and index stats data are used to construct a
     TABLE_READ_PLAN-derived object (TRP_*). Several 'candidate' table read
     plans may be created.
  3. The least expensive table read plan is used to create a tree of
35 36 37 38
     QUICK_SELECT_I-derived objects which are later used for row retrieval.
     QUICK_RANGEs are also created in this step.
*/

unknown's avatar
unknown committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

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

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


static int sel_cmp(Field *f,char *a,char *b,uint8 a_flag,uint8 b_flag);

static char is_null_string[2]= {1,0};

class SEL_ARG :public Sql_alloc
{
public:
  uint8 min_flag,max_flag,maybe_flag;
  uint8 part;					// Which key part
  uint8 maybe_null;
  uint16 elements;				// Elements in tree
  ulong use_count;				// use of this sub_tree
  Field *field;
  char *min_value,*max_value;			// Pointer to range

  SEL_ARG *left,*right,*next,*prev,*parent,*next_key_part;
  enum leaf_color { BLACK,RED } color;
  enum Type { IMPOSSIBLE, MAYBE, MAYBE_KEY, KEY_RANGE } type;

  SEL_ARG() {}
  SEL_ARG(SEL_ARG &);
  SEL_ARG(Field *,const char *,const char *);
  SEL_ARG(Field *field, uint8 part, char *min_value, char *max_value,
	  uint8 min_flag, uint8 max_flag, uint8 maybe_flag);
  SEL_ARG(enum Type type_arg)
unknown's avatar
unknown committed
79 80 81
    :elements(1),use_count(1),left(0),next_key_part(0),color(BLACK),
     type(type_arg)
  {}
unknown's avatar
unknown committed
82 83
  inline bool is_same(SEL_ARG *arg)
  {
84
    if (type != arg->type || part != arg->part)
unknown's avatar
unknown committed
85 86 87 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 173 174 175 176 177 178 179 180 181 182
      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; }
  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
    char *new_min,*new_max;
    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);
  }
  SEL_ARG *clone(SEL_ARG *new_parent,SEL_ARG **next);

  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;
  }
183
  void store_min(uint length,char **min_key,uint min_key_flag)
unknown's avatar
unknown committed
184
  {
unknown's avatar
unknown committed
185 186 187
    if ((min_flag & GEOM_FLAG) ||
        (!(min_flag & NO_MIN_RANGE) &&
	!(min_key_flag & (NO_MIN_RANGE | NEAR_MIN))))
unknown's avatar
unknown committed
188 189 190 191
    {
      if (maybe_null && *min_value)
      {
	**min_key=1;
unknown's avatar
unknown committed
192
	bzero(*min_key+1,length-1);
unknown's avatar
unknown committed
193 194
      }
      else
unknown's avatar
unknown committed
195 196
	memcpy(*min_key,min_value,length);
      (*min_key)+= length;
unknown's avatar
unknown committed
197
    }
198
  }
unknown's avatar
unknown committed
199 200 201
  void store(uint length,char **min_key,uint min_key_flag,
	     char **max_key, uint max_key_flag)
  {
202
    store_min(length, min_key, min_key_flag);
unknown's avatar
unknown committed
203 204 205 206 207 208
    if (!(max_flag & NO_MAX_RANGE) &&
	!(max_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
    {
      if (maybe_null && *max_value)
      {
	**max_key=1;
unknown's avatar
unknown committed
209
	bzero(*max_key+1,length-1);
unknown's avatar
unknown committed
210 211
      }
      else
unknown's avatar
unknown committed
212 213
	memcpy(*max_key,max_value,length);
      (*max_key)+= length;
unknown's avatar
unknown committed
214 215 216 217 218 219
    }
  }

  void store_min_key(KEY_PART *key,char **range_key, uint *range_key_flag)
  {
    SEL_ARG *key_tree= first();
unknown's avatar
unknown committed
220
    key_tree->store(key[key_tree->part].store_length,
unknown's avatar
unknown committed
221 222 223 224 225 226 227 228 229 230 231 232
		    range_key,*range_key_flag,range_key,NO_MAX_RANGE);
    *range_key_flag|= key_tree->min_flag;
    if (key_tree->next_key_part &&
	key_tree->next_key_part->part == key_tree->part+1 &&
	!(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)) &&
	key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
      key_tree->next_key_part->store_min_key(key,range_key, range_key_flag);
  }

  void store_max_key(KEY_PART *key,char **range_key, uint *range_key_flag)
  {
    SEL_ARG *key_tree= last();
unknown's avatar
unknown committed
233
    key_tree->store(key[key_tree->part].store_length,
unknown's avatar
unknown committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
		    range_key, NO_MIN_RANGE, range_key,*range_key_flag);
    (*range_key_flag)|= key_tree->max_flag;
    if (key_tree->next_key_part &&
	key_tree->next_key_part->part == key_tree->part+1 &&
	!(*range_key_flag & (NO_MAX_RANGE | NEAR_MAX)) &&
	key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
      key_tree->next_key_part->store_max_key(key,range_key, range_key_flag);
  }

  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;
  }
  SEL_ARG *clone_tree();
};

unknown's avatar
unknown committed
287
class SEL_IMERGE;
unknown's avatar
unknown committed
288

289

unknown's avatar
unknown committed
290 291 292 293 294
class SEL_TREE :public Sql_alloc
{
public:
  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
unknown's avatar
unknown committed
295
  SEL_TREE() :type(KEY)
unknown's avatar
unknown committed
296
  {
unknown's avatar
unknown committed
297
    keys_map.clear_all();
unknown's avatar
unknown committed
298 299
    bzero((char*) keys,sizeof(keys));
  }
unknown's avatar
unknown committed
300
  SEL_ARG *keys[MAX_KEY];
301 302
  key_map keys_map;        /* bitmask of non-NULL elements in keys */

unknown's avatar
unknown committed
303 304
  /*
    Possible ways to read rows using index_merge. The list is non-empty only
305 306 307
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
  */
  List<SEL_IMERGE> merges;
unknown's avatar
unknown committed
308

309 310
  /* 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 */
311
  uint    n_ror_scans;     /* number of set bits in ror_scans_map */
312 313 314 315

  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 */
unknown's avatar
unknown committed
316 317 318 319
};


typedef struct st_qsel_param {
320
  THD	*thd;
unknown's avatar
unknown committed
321
  TABLE *table;
322 323
  KEY_PART *key_parts,*key_parts_end;
  KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
324 325
  MEM_ROOT *mem_root;
  table_map prev_tables,read_tables,current_table;
326
  uint baseflag, max_key_part, range_count;
unknown's avatar
unknown committed
327

328 329 330
  uint keys; /* number of keys used in the query */

  /* used_key_no -> table_key_no translation table */
unknown's avatar
unknown committed
331
  uint real_keynr[MAX_KEY];
332

unknown's avatar
unknown committed
333 334
  char min_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH],
    max_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];
335
  bool quick;				// Don't calulate possible keys
336
  COND *cond;
337

unknown's avatar
unknown committed
338
  uint fields_bitmap_size;
339 340 341 342
  MY_BITMAP needed_fields;    /* bitmask of fields needed by the query */

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

343 344
  uint *imerge_cost_buff;     /* buffer for index_merge cost estimates */
  uint imerge_cost_buff_size; /* size of the buffer */
unknown's avatar
unknown committed
345 346 347

 /* TRUE if last checked tree->key can be used for ROR-scan */
  bool is_ror_scan;
unknown's avatar
unknown committed
348 349
} PARAM;

350 351 352 353 354
class TABLE_READ_PLAN;
  class TRP_RANGE;
  class TRP_ROR_INTERSECT;
  class TRP_ROR_UNION;
  class TRP_ROR_INDEX_MERGE;
355
  class TRP_GROUP_MIN_MAX;
356 357 358

struct st_ror_scan_info;

359
static SEL_TREE * get_mm_parts(PARAM *param,COND *cond_func,Field *field,
unknown's avatar
unknown committed
360 361
			       Item_func::Functype type,Item *value,
			       Item_result cmp_type);
362 363
static SEL_ARG *get_mm_leaf(PARAM *param,COND *cond_func,Field *field,
			    KEY_PART *key_part,
unknown's avatar
unknown committed
364 365
			    Item_func::Functype type,Item *value);
static SEL_TREE *get_mm_tree(PARAM *param,COND *cond);
366 367

static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);
unknown's avatar
unknown committed
368 369 370 371 372
static ha_rows check_quick_select(PARAM *param,uint index,SEL_ARG *key_tree);
static ha_rows check_quick_keys(PARAM *param,uint index,SEL_ARG *key_tree,
				char *min_key,uint min_key_flag,
				char *max_key, uint max_key_flag);

unknown's avatar
unknown committed
373
QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
unknown's avatar
unknown committed
374
                                     SEL_ARG *key_tree,
unknown's avatar
unknown committed
375
                                     MEM_ROOT *alloc = NULL);
376
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
unknown's avatar
unknown committed
377
                                       bool index_read_must_be_used,
378 379 380 381 382 383
                                       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
unknown's avatar
unknown committed
384 385
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
386 387 388 389
                                                   double read_time);
static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
                                         double read_time);
390 391
static
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree);
392
static int get_index_merge_params(PARAM *param, key_map& needed_reg,
unknown's avatar
unknown committed
393
                           SEL_IMERGE *imerge, double *read_time,
394
                           ha_rows* imerge_rows);
unknown's avatar
unknown committed
395
inline double get_index_only_read_time(const PARAM* param, ha_rows records,
396 397
                                       int keynr);

unknown's avatar
unknown committed
398
#ifndef DBUG_OFF
399 400
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
                           const char *msg);
unknown's avatar
unknown committed
401 402
static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
403 404 405
                                struct st_ror_scan_info **end);
static void print_rowid(byte* val, int len);
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
unknown's avatar
unknown committed
406
#endif
407

unknown's avatar
unknown committed
408 409 410 411 412 413
static SEL_TREE *tree_and(PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_TREE *tree_or(PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
static SEL_ARG *key_or(SEL_ARG *key1,SEL_ARG *key2);
static SEL_ARG *key_and(SEL_ARG *key1,SEL_ARG *key2,uint clone_flag);
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
unknown's avatar
unknown committed
414
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
unknown's avatar
unknown committed
415 416 417 418 419
			   SEL_ARG *key_tree,char *min_key,uint min_key_flag,
			   char *max_key,uint max_key_flag);
static bool eq_tree(SEL_ARG* a,SEL_ARG *b);

static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
unknown's avatar
unknown committed
420
static bool null_part_in_key(KEY_PART *key_part, const char *key,
unknown's avatar
unknown committed
421
                             uint length);
unknown's avatar
unknown committed
422 423 424 425
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, PARAM* param);


/*
unknown's avatar
unknown committed
426
  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
unknown's avatar
unknown committed
427
  a condition in the following form:
unknown's avatar
unknown committed
428
   (t_1||t_2||...||t_N) && (next)
unknown's avatar
unknown committed
429

unknown's avatar
unknown committed
430
  where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
unknown's avatar
unknown committed
431 432 433 434 435 436 437 438 439 440 441
  (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:
unknown's avatar
unknown committed
442
  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
unknown's avatar
unknown committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  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)
  {}
  int or_sel_tree(PARAM *param, SEL_TREE *tree);
  int or_sel_tree_with_checks(PARAM *param, SEL_TREE *new_tree);
  int or_sel_imerge_with_checks(PARAM *param, SEL_IMERGE* imerge);
};


unknown's avatar
unknown committed
460
/*
unknown's avatar
unknown committed
461 462
  Add SEL_TREE to this index_merge without any checks,

unknown's avatar
unknown committed
463 464
  NOTES
    This function implements the following:
unknown's avatar
unknown committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
      (x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs

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

int SEL_IMERGE::or_sel_tree(PARAM *param, SEL_TREE *tree)
{
  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.
unknown's avatar
unknown committed
497

unknown's avatar
unknown committed
498 499 500 501 502
  SYNOPSIS
    or_sel_tree_with_checks()
      param    PARAM from SQL_SELECT::test_quick_select
      new_tree SEL_TREE with type KEY or KEY_SMALLER.

unknown's avatar
unknown committed
503
  NOTES
unknown's avatar
unknown committed
504
    This does the following:
unknown's avatar
unknown committed
505 506
    (t_1||...||t_k)||new_tree =
     either
unknown's avatar
unknown committed
507 508 509
       = (t_1||...||t_k||new_tree)
     or
       = (t_1||....||(t_j|| new_tree)||...||t_k),
unknown's avatar
unknown committed
510

unknown's avatar
unknown committed
511
     where t_i, y are SEL_TREEs.
unknown's avatar
unknown committed
512 513
    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
unknown's avatar
unknown committed
514 515
    read may depend on the order of conditions in WHERE part of the query.

unknown's avatar
unknown committed
516
  RETURN
unknown's avatar
unknown committed
517
    0  OK
unknown's avatar
unknown committed
518
    1  One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
unknown's avatar
unknown committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
       and (*this) should be discarded.
   -1  An error occurred.
*/

int SEL_IMERGE::or_sel_tree_with_checks(PARAM *param, SEL_TREE *new_tree)
{
  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;
    }
  }

542
  /* New tree cannot be combined with any of existing trees. */
unknown's avatar
unknown committed
543 544 545 546 547 548 549 550 551
  return or_sel_tree(param, new_tree);
}


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

  RETURN
    0 - OK
unknown's avatar
unknown committed
552
    1 - One of conditions in result is always TRUE and this SEL_IMERGE
unknown's avatar
unknown committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        should be discarded.
   -1 - An error occurred
*/

int SEL_IMERGE::or_sel_imerge_with_checks(PARAM *param, SEL_IMERGE* imerge)
{
  for (SEL_TREE** tree= imerge->trees;
       tree != imerge->trees_next;
       tree++)
  {
    if (or_sel_tree_with_checks(param, *tree))
      return 1;
  }
  return 0;
}


unknown's avatar
unknown committed
570
/*
571
  Perform AND operation on two index_merge lists and store result in *im1.
unknown's avatar
unknown committed
572 573 574 575 576 577 578 579 580 581 582
*/

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.

unknown's avatar
unknown committed
583
  NOTES
unknown's avatar
unknown committed
584 585 586
    The following conversion is implemented:
     (a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
      => (a_1||b_1).
unknown's avatar
unknown committed
587 588

    i.e. all conjuncts except the first one are currently dropped.
unknown's avatar
unknown committed
589 590
    This is done to avoid producing N*K ways to do index_merge.

unknown's avatar
unknown committed
591
    If (a_1||b_1) produce a condition that is always TRUE, NULL is returned
unknown's avatar
unknown committed
592
    and index_merge is discarded (while it is actually possible to try
593
    harder).
unknown's avatar
unknown committed
594

595 596
    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.
unknown's avatar
unknown committed
597 598

  RETURN
599
    0     OK, result is stored in *im1
unknown's avatar
unknown committed
600 601 602
    other Error, both passed lists are unusable
*/

unknown's avatar
unknown committed
603
int imerge_list_or_list(PARAM *param,
unknown's avatar
unknown committed
604 605 606 607 608 609
                        List<SEL_IMERGE> *im1,
                        List<SEL_IMERGE> *im2)
{
  SEL_IMERGE *imerge= im1->head();
  im1->empty();
  im1->push_back(imerge);
unknown's avatar
unknown committed
610

unknown's avatar
unknown committed
611 612 613 614 615 616 617 618
  return imerge->or_sel_imerge_with_checks(param, im2->head());
}


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

  RETURN
619
    0     OK, result is stored in *im1.
unknown's avatar
unknown committed
620 621 622
    other Error
*/

unknown's avatar
unknown committed
623
int imerge_list_or_tree(PARAM *param,
unknown's avatar
unknown committed
624 625 626 627 628 629 630 631 632 633 634 635
                        List<SEL_IMERGE> *im1,
                        SEL_TREE *tree)
{
  SEL_IMERGE *imerge;
  List_iterator<SEL_IMERGE> it(*im1);
  while((imerge= it++))
  {
    if (imerge->or_sel_tree_with_checks(param, tree))
      it.remove();
  }
  return im1->is_empty();
}
unknown's avatar
unknown committed
636 637

/***************************************************************************
unknown's avatar
unknown committed
638
** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
unknown's avatar
unknown committed
639 640 641 642 643 644 645 646 647
***************************************************************************/

	/* 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,
648 649
			table_map read_tables, COND *conds, int *error,
                        bool allow_null_cond)
unknown's avatar
unknown committed
650 651 652 653 654
{
  SQL_SELECT *select;
  DBUG_ENTER("make_select");

  *error=0;
655 656

  if (!conds && !allow_null_cond)
unknown's avatar
unknown committed
657 658 659
    DBUG_RETURN(0);
  if (!(select= new SQL_SELECT))
  {
660 661
    *error= 1;			// out of memory
    DBUG_RETURN(0);		/* purecov: inspected */
unknown's avatar
unknown committed
662 663 664 665 666 667
  }
  select->read_tables=read_tables;
  select->const_tables=const_tables;
  select->head=head;
  select->cond=conds;

unknown's avatar
unknown committed
668
  if (head->sort.io_cache)
unknown's avatar
unknown committed
669
  {
unknown's avatar
unknown committed
670
    select->file= *head->sort.io_cache;
unknown's avatar
unknown committed
671 672
    select->records=(ha_rows) (select->file.end_of_file/
			       head->file->ref_length);
unknown's avatar
unknown committed
673 674
    my_free((gptr) (head->sort.io_cache),MYF(0));
    head->sort.io_cache=0;
unknown's avatar
unknown committed
675 676 677 678 679 680 681
  }
  DBUG_RETURN(select);
}


SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
{
unknown's avatar
unknown committed
682
  quick_keys.clear_all(); needed_reg.clear_all();
unknown's avatar
unknown committed
683 684 685 686
  my_b_clear(&file);
}


687
void SQL_SELECT::cleanup()
unknown's avatar
unknown committed
688 689
{
  delete quick;
690
  quick= 0;
unknown's avatar
unknown committed
691
  if (free_cond)
692 693
  {
    free_cond=0;
unknown's avatar
unknown committed
694
    delete cond;
695
    cond= 0;
unknown's avatar
unknown committed
696
  }
unknown's avatar
unknown committed
697 698 699
  close_cached_file(&file);
}

700 701 702 703 704 705

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

unknown's avatar
unknown committed
706
#undef index					// Fix for Unixware 7
unknown's avatar
unknown committed
707

unknown's avatar
unknown committed
708 709 710 711 712
QUICK_SELECT_I::QUICK_SELECT_I()
  :max_used_key_length(0),
   used_key_parts(0)
{}

unknown's avatar
unknown committed
713
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
unknown's avatar
unknown committed
714
                                       bool no_alloc, MEM_ROOT *parent_alloc)
715
  :dont_free(0),error(0),free_file(0),cur_range(NULL),range(0)
unknown's avatar
unknown committed
716
{
unknown's avatar
unknown committed
717
  sorted= 0;
unknown's avatar
unknown committed
718 719
  index= key_nr;
  head=  table;
unknown's avatar
unknown committed
720
  key_part_info= head->key_info[index].key_part;
721
  my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
unknown's avatar
unknown committed
722 723

  if (!no_alloc && !parent_alloc)
unknown's avatar
unknown committed
724
  {
725 726
    // Allocates everything through the internal memroot
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
unknown's avatar
unknown committed
727
    thd->mem_root= &alloc;
unknown's avatar
unknown committed
728 729 730
  }
  else
    bzero((char*) &alloc,sizeof(alloc));
unknown's avatar
unknown committed
731 732
  file= head->file;
  record= head->record[0];
unknown's avatar
unknown committed
733 734
}

unknown's avatar
unknown committed
735

unknown's avatar
unknown committed
736 737
int QUICK_RANGE_SELECT::init()
{
unknown's avatar
unknown committed
738
  DBUG_ENTER("QUICK_RANGE_SELECT::init");
unknown's avatar
unknown committed
739 740 741 742 743 744 745 746 747 748 749
  if (file->inited == handler::NONE)
    DBUG_RETURN(error= file->ha_index_init(index));
  error= 0;
  DBUG_RETURN(0);
}


void QUICK_RANGE_SELECT::range_end()
{
  if (file->inited != handler::NONE)
    file->ha_index_end();
unknown's avatar
unknown committed
750 751
}

unknown's avatar
unknown committed
752

unknown's avatar
unknown committed
753
QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
unknown's avatar
unknown committed
754
{
755
  DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
unknown's avatar
unknown committed
756 757
  if (!dont_free)
  {
unknown's avatar
unknown committed
758 759
    /* file is NULL for CPK scan on covering ROR-intersection */
    if (file) 
760
    {
unknown's avatar
unknown committed
761 762 763 764 765 766 767 768 769
      range_end();
      file->extra(HA_EXTRA_NO_KEYREAD);
      if (free_file)
      {
        DBUG_PRINT("info", ("Freeing separate handler %p (free=%d)", file,
                            free_file));
        file->reset();
        file->close();
      }
unknown's avatar
unknown committed
770
    }
unknown's avatar
unknown committed
771
    delete_dynamic(&ranges); /* ranges are allocated in alloc */
unknown's avatar
unknown committed
772 773 774
    free_root(&alloc,MYF(0));
  }
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
775 776
}

unknown's avatar
unknown committed
777

unknown's avatar
unknown committed
778
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
unknown's avatar
unknown committed
779 780 781
                                                   TABLE *table)
  :cur_quick_it(quick_selects),pk_quick_select(NULL),unique(NULL),
   thd(thd_param)
unknown's avatar
unknown committed
782
{
783
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT");
unknown's avatar
unknown committed
784 785
  index= MAX_KEY;
  head= table;
786
  bzero(&read_record, sizeof(read_record));
787
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
788
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
789 790 791 792 793 794
}

int QUICK_INDEX_MERGE_SELECT::init()
{
  cur_quick_it.rewind();
  cur_quick_select= cur_quick_it++;
795
  return 0;
unknown's avatar
unknown committed
796 797
}

798
int QUICK_INDEX_MERGE_SELECT::reset()
unknown's avatar
unknown committed
799
{
800 801
  int result;
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::reset");
802
  result= cur_quick_select->reset() || prepare_unique();
803
  DBUG_RETURN(result);
unknown's avatar
unknown committed
804 805
}

unknown's avatar
unknown committed
806
bool
unknown's avatar
unknown committed
807 808
QUICK_INDEX_MERGE_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
{
unknown's avatar
unknown committed
809 810
  /*
    Save quick_select that does scan on clustered primary key as it will be
811
    processed separately.
812
  */
unknown's avatar
unknown committed
813
  if (head->file->primary_key_is_clustered() &&
814 815 816 817 818
      quick_sel_range->index == head->primary_key)
    pk_quick_select= quick_sel_range;
  else
    return quick_selects.push_back(quick_sel_range);
  return 0;
unknown's avatar
unknown committed
819 820 821 822
}

QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT()
{
823
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT");
824
  delete unique;
unknown's avatar
unknown committed
825
  quick_selects.delete_elements();
826
  delete pk_quick_select;
unknown's avatar
unknown committed
827
  free_root(&alloc,MYF(0));
828
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
829 830
}

831 832 833 834 835

QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
                                                       TABLE *table,
                                                       bool retrieve_full_rows,
                                                       MEM_ROOT *parent_alloc)
836
  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows)
837 838
{
  index= MAX_KEY;
unknown's avatar
unknown committed
839
  head= table;
840 841
  record= head->record[0];
  if (!parent_alloc)
842
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
843 844
  else
    bzero(&alloc, sizeof(MEM_ROOT));
unknown's avatar
unknown committed
845
  last_rowid= (byte*)alloc_root(parent_alloc? parent_alloc : &alloc,
846 847 848
                                head->file->ref_length);
}

849

unknown's avatar
unknown committed
850
/*
851 852 853
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init()
unknown's avatar
unknown committed
854

855 856 857 858 859
  RETURN
    0      OK
    other  Error code
*/

860 861
int QUICK_ROR_INTERSECT_SELECT::init()
{
862
  /* Check if last_rowid was successfully allocated in ctor */
863 864 865 866 867
  return !last_rowid;
}


/*
868 869 870 871
  Initialize this quick select to be a ROR-merged scan.

  SYNOPSIS
    QUICK_RANGE_SELECT::init_ror_merged_scan()
unknown's avatar
unknown committed
872
      reuse_handler If TRUE, use head->file, otherwise create a separate
873 874 875 876
                    handler object

  NOTES
    This function creates and prepares for subsequent use a separate handler
unknown's avatar
unknown committed
877
    object if it can't reuse head->file. The reason for this is that during
878 879 880
    ROR-merge several key scans are performed simultaneously, and a single
    handler is only capable of preserving context of a single key scan.

unknown's avatar
unknown committed
881
    In ROR-merge the quick select doing merge does full records retrieval,
882
    merged quick selects read only keys.
unknown's avatar
unknown committed
883 884

  RETURN
885 886 887 888
    0  ROR child scan initialized, ok to use.
    1  error
*/

889
int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
890 891
{
  handler *save_file= file;
892
  DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");
unknown's avatar
unknown committed
893

894 895 896 897 898 899 900 901 902
  if (reuse_handler)
  {
    DBUG_PRINT("info", ("Reusing handler %p", file));
    if (file->extra(HA_EXTRA_KEYREAD) ||
        file->extra(HA_EXTRA_RETRIEVE_ALL_COLS) |
        init() || reset())
    {
      DBUG_RETURN(1);
    }
unknown's avatar
unknown committed
903
    DBUG_RETURN(0);
904 905 906 907 908 909 910 911
  }

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

913 914 915 916 917
  if (!(file= get_new_handler(head, head->db_type)))
    goto failure;
  DBUG_PRINT("info", ("Allocated new handler %p", file));
  if (file->ha_open(head->path, head->db_stat, HA_OPEN_IGNORE_IF_LOCKED))
  {
unknown's avatar
unknown committed
918
    /* Caller will free the memory */
919 920
    goto failure;
  }
unknown's avatar
unknown committed
921 922

  if (file->extra(HA_EXTRA_KEYREAD) ||
923 924 925 926 927 928
      file->extra(HA_EXTRA_RETRIEVE_ALL_COLS) ||
      init() || reset())
  {
    file->close();
    goto failure;
  }
unknown's avatar
unknown committed
929
  free_file= TRUE;
930 931 932 933 934 935 936 937
  last_rowid= file->ref;
  DBUG_RETURN(0);

failure:
  file= save_file;
  DBUG_RETURN(1);
}

938 939 940 941 942

/*
  Initialize this quick select to be a part of a ROR-merged scan.
  SYNOPSIS
    QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
unknown's avatar
unknown committed
943
      reuse_handler If TRUE, use head->file, otherwise create separate
944
                    handler object.
unknown's avatar
unknown committed
945
  RETURN
946 947 948 949
    0     OK
    other error code
*/
int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler)
950 951 952
{
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
  QUICK_RANGE_SELECT* quick;
953
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");
954 955 956 957 958 959

  /* Initialize all merged "children" quick selects */
  DBUG_ASSERT(!(need_to_fetch_row && !reuse_handler));
  if (!need_to_fetch_row && reuse_handler)
  {
    quick= quick_it++;
unknown's avatar
unknown committed
960
    /*
961
      There is no use of this->file. Use it for the first of merged range
962 963
      selects.
    */
unknown's avatar
unknown committed
964
    if (quick->init_ror_merged_scan(TRUE))
965 966 967 968 969
      DBUG_RETURN(1);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
  }
  while((quick= quick_it++))
  {
unknown's avatar
unknown committed
970
    if (quick->init_ror_merged_scan(FALSE))
971 972
      DBUG_RETURN(1);
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
973
    /* All merged scans share the same record buffer in intersection. */
974 975 976
    quick->record= head->record[0];
  }

unknown's avatar
unknown committed
977
  if (need_to_fetch_row && head->file->ha_rnd_init(1))
978 979 980 981 982 983 984
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}

985

unknown's avatar
unknown committed
986
/*
987 988 989 990 991 992 993 994
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()
  RETURN
    0      OK
    other  Error code
*/

995 996 997
int QUICK_ROR_INTERSECT_SELECT::reset()
{
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
unknown's avatar
unknown committed
998
  DBUG_RETURN(init_ror_merged_scan(TRUE));
999 1000
}

1001 1002 1003

/*
  Add a merged quick select to this ROR-intersection quick select.
unknown's avatar
unknown committed
1004

1005 1006 1007 1008 1009 1010
  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.
unknown's avatar
unknown committed
1011

1012
  RETURN
unknown's avatar
unknown committed
1013
    FALSE OK
unknown's avatar
unknown committed
1014
    TRUE  Out of memory.
1015 1016
*/

unknown's avatar
unknown committed
1017
bool
1018 1019
QUICK_ROR_INTERSECT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick)
{
1020
  return quick_selects.push_back(quick);
1021 1022 1023
}

QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
unknown's avatar
unknown committed
1024
{
1025
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
unknown's avatar
unknown committed
1026
  quick_selects.delete_elements();
1027 1028
  delete cpk_quick;
  free_root(&alloc,MYF(0));
unknown's avatar
unknown committed
1029 1030
  if (need_to_fetch_row && head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
1031 1032 1033
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
1034

1035 1036
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
                                               TABLE *table)
unknown's avatar
unknown committed
1037
  :thd(thd_param)
1038 1039 1040 1041 1042 1043
{
  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);
unknown's avatar
unknown committed
1044
  thd_param->mem_root= &alloc;
1045 1046
}

1047 1048 1049 1050 1051

/*
  Do post-constructor initialization.
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::init()
unknown's avatar
unknown committed
1052

1053 1054 1055 1056 1057
  RETURN
    0      OK
    other  Error code
*/

1058 1059 1060
int QUICK_ROR_UNION_SELECT::init()
{
  if (init_queue(&queue, quick_selects.elements, 0,
unknown's avatar
unknown committed
1061
                 FALSE , QUICK_ROR_UNION_SELECT::queue_cmp,
1062 1063 1064 1065 1066
                 (void*) this))
  {
    bzero(&queue, sizeof(QUEUE));
    return 1;
  }
unknown's avatar
unknown committed
1067

1068 1069 1070 1071 1072 1073
  if (!(cur_rowid= (byte*)alloc_root(&alloc, 2*head->file->ref_length)))
    return 1;
  prev_rowid= cur_rowid + head->file->ref_length;
  return 0;
}

1074

1075
/*
unknown's avatar
unknown committed
1076
  Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
1077 1078
  queue.

1079 1080 1081 1082 1083 1084 1085 1086
  SYNPOSIS
    QUICK_ROR_UNION_SELECT::queue_cmp()
      arg   Pointer to QUICK_ROR_UNION_SELECT
      val1  First merged select
      val2  Second merged select
*/
int QUICK_ROR_UNION_SELECT::queue_cmp(void *arg, byte *val1, byte *val2)
{
1087
  QUICK_ROR_UNION_SELECT *self= (QUICK_ROR_UNION_SELECT*)arg;
1088 1089 1090 1091
  return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
                                   ((QUICK_SELECT_I*)val2)->last_rowid);
}

1092

unknown's avatar
unknown committed
1093
/*
1094 1095 1096
  Initialize quick select for row retrieval.
  SYNOPSIS
    reset()
unknown's avatar
unknown committed
1097

1098 1099 1100 1101 1102
  RETURN
    0      OK
    other  Error code
*/

1103 1104 1105 1106 1107
int QUICK_ROR_UNION_SELECT::reset()
{
  QUICK_SELECT_I* quick;
  int error;
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
unknown's avatar
unknown committed
1108
  have_prev_rowid= FALSE;
unknown's avatar
unknown committed
1109 1110
  /*
    Initialize scans for merged quick selects and put all merged quick
1111 1112 1113 1114 1115
    selects into the queue.
  */
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
unknown's avatar
unknown committed
1116
    if (quick->init_ror_merged_scan(FALSE))
unknown's avatar
unknown committed
1117
      DBUG_RETURN(1);
1118 1119 1120 1121
    if ((error= quick->get_next()))
    {
      if (error == HA_ERR_END_OF_FILE)
        continue;
unknown's avatar
unknown committed
1122
      DBUG_RETURN(error);
1123 1124 1125 1126 1127
    }
    quick->save_last_pos();
    queue_insert(&queue, (byte*)quick);
  }

unknown's avatar
unknown committed
1128
  if (head->file->ha_rnd_init(1))
1129 1130 1131 1132 1133 1134 1135 1136 1137
  {
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
    DBUG_RETURN(1);
  }

  DBUG_RETURN(0);
}


unknown's avatar
unknown committed
1138
bool
1139 1140 1141 1142 1143 1144 1145 1146 1147
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);
unknown's avatar
unknown committed
1148
  quick_selects.delete_elements();
1149 1150
  if (head->file->inited != handler::NONE)
    head->file->ha_rnd_end();
1151 1152
  free_root(&alloc,MYF(0));
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
1153 1154
}

1155

unknown's avatar
unknown committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
QUICK_RANGE::QUICK_RANGE()
  :min_key(0),max_key(0),min_length(0),max_length(0),
   flag(NO_MIN_RANGE | NO_MAX_RANGE)
{}

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;
}

SEL_ARG::SEL_ARG(Field *f,const char *min_value_arg,const char *max_value_arg)
  :min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
   elements(1), use_count(1), field(f), min_value((char*) min_value_arg),
   max_value((char*) max_value_arg), next(0),prev(0),
   next_key_part(0),color(BLACK),type(KEY_RANGE)
{
  left=right= &null_element;
}

SEL_ARG::SEL_ARG(Field *field_,uint8 part_,char *min_value_,char *max_value_,
		 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;
}

SEL_ARG *SEL_ARG::clone(SEL_ARG *new_parent,SEL_ARG **next_arg)
{
  SEL_ARG *tmp;
  if (type != KEY_RANGE)
  {
1209 1210
    if (!(tmp= new SEL_ARG(type)))
      return 0;					// out of memory
unknown's avatar
unknown committed
1211 1212 1213 1214 1215 1216
    tmp->prev= *next_arg;			// Link into next/prev chain
    (*next_arg)->next=tmp;
    (*next_arg)= tmp;
  }
  else
  {
1217 1218 1219
    if (!(tmp= new SEL_ARG(field,part, min_value,max_value,
			   min_flag, max_flag, maybe_flag)))
      return 0;					// OOM
unknown's avatar
unknown committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    tmp->parent=new_parent;
    tmp->next_key_part=next_key_part;
    if (left != &null_element)
      tmp->left=left->clone(tmp,next_arg);

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

    if (right != &null_element)
1230 1231
      if (!(tmp->right= right->clone(tmp,next_arg)))
	return 0;				// OOM
unknown's avatar
unknown committed
1232 1233
  }
  increment_use_count(1);
1234
  tmp->color= color;
unknown's avatar
unknown committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  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;
}

1258

unknown's avatar
unknown committed
1259 1260 1261
/*
  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
1262
*/
unknown's avatar
unknown committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

static int sel_cmp(Field *field, char *a,char *b,uint8 a_flag,uint8 b_flag)
{
  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
unknown's avatar
unknown committed
1286
    a++; b++;					// Skip NULL marker
unknown's avatar
unknown committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
  }
  cmp=field->key_cmp((byte*) a,(byte*) b);
  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
}


SEL_ARG *SEL_ARG::clone_tree()
{
  SEL_ARG tmp_link,*next_arg,*root;
  next_arg= &tmp_link;
1311
  root= clone((SEL_ARG *) 0, &next_arg);
unknown's avatar
unknown committed
1312 1313
  next_arg->next=0;				// Fix last link
  tmp_link.next->prev=0;			// Fix first link
1314 1315
  if (root)					// If not OOM
    root->use_count= 0;
unknown's avatar
unknown committed
1316 1317 1318
  return root;
}

1319

unknown's avatar
unknown committed
1320
/*
unknown's avatar
unknown committed
1321
  Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
1322 1323 1324 1325 1326
  objects from table read plans.
*/
class TABLE_READ_PLAN
{
public:
unknown's avatar
unknown committed
1327 1328
  /*
    Plan read cost, with or without cost of full row retrieval, depending
1329 1330
    on plan creation parameters.
  */
unknown's avatar
unknown committed
1331
  double read_cost;
1332
  ha_rows records; /* estimate of #rows to be examined */
unknown's avatar
unknown committed
1333

unknown's avatar
unknown committed
1334 1335
  /*
    If TRUE, the scan returns rows in rowid order. This is used only for
1336 1337
    scans that can be both ROR and non-ROR.
  */
1338
  bool is_ror;
unknown's avatar
unknown committed
1339

1340 1341 1342 1343 1344
  /*
    Create quick select for this plan.
    SYNOPSIS
     make_quick()
       param               Parameter from test_quick_select
unknown's avatar
unknown committed
1345
       retrieve_full_rows  If TRUE, created quick select will do full record
1346 1347
                           retrieval.
       parent_alloc        Memory pool to use, if any.
unknown's avatar
unknown committed
1348

1349 1350
    NOTES
      retrieve_full_rows is ignored by some implementations.
unknown's avatar
unknown committed
1351 1352

    RETURN
1353 1354 1355
      created quick select
      NULL on any error.
  */
1356 1357 1358 1359
  virtual QUICK_SELECT_I *make_quick(PARAM *param,
                                     bool retrieve_full_rows,
                                     MEM_ROOT *parent_alloc=NULL) = 0;

1360
  /* Table read plans are allocated on MEM_ROOT and are never deleted */
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
  static void *operator new(size_t size, MEM_ROOT *mem_root)
  { return (void*) alloc_root(mem_root, (uint) size); }
  static void operator delete(void *ptr,size_t size) {}
};

class TRP_ROR_INTERSECT;
class TRP_ROR_UNION;
class TRP_INDEX_MERGE;


1371
/*
unknown's avatar
unknown committed
1372
  Plan for a QUICK_RANGE_SELECT scan.
1373 1374 1375
  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.
unknown's avatar
unknown committed
1376
*/
unknown's avatar
unknown committed
1377

1378
class TRP_RANGE : public TABLE_READ_PLAN
unknown's avatar
unknown committed
1379
{
1380
public:
1381 1382
  SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
  uint     key_idx; /* key number in PARAM::key */
unknown's avatar
unknown committed
1383

unknown's avatar
unknown committed
1384
  TRP_RANGE(SEL_ARG *key_arg, uint idx_arg)
1385 1386
   : key(key_arg), key_idx(idx_arg)
  {}
unknown's avatar
unknown committed
1387

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  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);
  }
};
unknown's avatar
unknown committed
1401 1402


1403 1404
/* Plan for QUICK_ROR_INTERSECT_SELECT scan. */

1405 1406 1407
class TRP_ROR_INTERSECT : public TABLE_READ_PLAN
{
public:
unknown's avatar
unknown committed
1408
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1409
                             MEM_ROOT *parent_alloc);
unknown's avatar
unknown committed
1410

1411
  /* Array of pointers to ROR range scans used in this intersection */
1412
  struct st_ror_scan_info **first_scan;
1413 1414
  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 */
unknown's avatar
unknown committed
1415
  bool is_covering; /* TRUE if no row retrieval phase is necessary */
1416
  double index_scan_costs; /* SUM(cost(index_scan)) */
1417 1418
};

1419

unknown's avatar
unknown committed
1420
/*
1421 1422
  Plan for QUICK_ROR_UNION_SELECT scan.
  QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
unknown's avatar
unknown committed
1423
  is ignored by make_quick.
1424
*/
1425

1426 1427 1428
class TRP_ROR_UNION : public TABLE_READ_PLAN
{
public:
unknown's avatar
unknown committed
1429
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1430
                             MEM_ROOT *parent_alloc);
1431 1432
  TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
  TABLE_READ_PLAN **last_ror;  /* end of the above array */
1433 1434
};

1435 1436 1437 1438

/*
  Plan for QUICK_INDEX_MERGE_SELECT scan.
  QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
unknown's avatar
unknown committed
1439
  is ignored by make_quick.
1440 1441
*/

1442 1443 1444
class TRP_INDEX_MERGE : public TABLE_READ_PLAN
{
public:
unknown's avatar
unknown committed
1445
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1446
                             MEM_ROOT *parent_alloc);
1447 1448
  TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
  TRP_RANGE **range_scans_end; /* end of the array */
1449 1450 1451
};


1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
/*
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan. 
*/

class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
{
private:
  bool have_min, have_max;
  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;
  byte key_infix[MAX_KEY_LENGTH];
  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. */
  /* Number of records selected by the ranges in index_tree. */
public:
  ha_rows quick_prefix_records;
public:
1475 1476 1477 1478
  TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg,
                    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,
1479 1480
                    uint index_arg, uint key_infix_len_arg,
                    byte *key_infix_arg,
1481 1482 1483 1484 1485 1486 1487 1488 1489
                    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),
    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),
    index_tree(index_tree_arg), param_idx(param_idx_arg),
    quick_prefix_records(quick_prefix_records_arg)
1490 1491 1492 1493
    {
      if (key_infix_len)
        memcpy(this->key_infix, key_infix_arg, key_infix_len);
    }
1494 1495 1496 1497 1498 1499

  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
                             MEM_ROOT *parent_alloc);
};


unknown's avatar
unknown committed
1500
/*
1501
  Fill param->needed_fields with bitmap of fields used in the query.
unknown's avatar
unknown committed
1502
  SYNOPSIS
1503 1504
    fill_used_fields_bitmap()
      param Parameter from test_quick_select function.
unknown's avatar
unknown committed
1505

1506 1507 1508
  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).
unknown's avatar
unknown committed
1509 1510 1511
  RETURN
    0  Ok
    1  Out of memory.
1512 1513 1514 1515 1516 1517 1518 1519 1520
*/

static int fill_used_fields_bitmap(PARAM *param)
{
  TABLE *table= param->table;
  param->fields_bitmap_size= (table->fields/8 + 1);
  uchar *tmp;
  uint pk;
  if (!(tmp= (uchar*)alloc_root(param->mem_root,param->fields_bitmap_size)) ||
unknown's avatar
unknown committed
1521
      bitmap_init(&param->needed_fields, tmp, param->fields_bitmap_size*8,
unknown's avatar
unknown committed
1522
                  FALSE))
1523
    return 1;
unknown's avatar
unknown committed
1524

1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
  bitmap_clear_all(&param->needed_fields);
  for (uint i= 0; i < table->fields; i++)
  {
    if (param->thd->query_id == table->field[i]->query_id)
      bitmap_set_bit(&param->needed_fields, i+1);
  }

  pk= param->table->primary_key;
  if (param->table->file->primary_key_is_clustered() && pk != MAX_KEY)
  {
1535
    /* The table uses clustered PK and it is not internally generated */
1536
    KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
unknown's avatar
unknown committed
1537
    KEY_PART_INFO *key_part_end= key_part +
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
                                 param->table->key_info[pk].key_parts;
    for(;key_part != key_part_end; ++key_part)
    {
      bitmap_clear_bit(&param->needed_fields, key_part->fieldnr);
    }
  }
  return 0;
}


unknown's avatar
unknown committed
1548
/*
unknown's avatar
unknown committed
1549
  Test if a key can be used in different ranges
unknown's avatar
unknown committed
1550 1551

  SYNOPSIS
1552 1553 1554 1555 1556
    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)
unknown's avatar
unknown committed
1557 1558 1559
      limit             Query limit
      force_quick_range Prefer to use range (instead of full table scan) even
                        if it is more expensive.
1560 1561 1562 1563 1564

  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.
unknown's avatar
unknown committed
1565

1566 1567 1568
    In the table struct the following information is updated:
      quick_keys - Which keys can be used
      quick_rows - How many rows the key matches
unknown's avatar
unknown committed
1569

1570 1571 1572 1573
  TODO
   Check if this function really needs to modify keys_to_use, and change the
   code to pass it by reference if it doesn't.

unknown's avatar
unknown committed
1574
   In addition to force_quick_range other means can be (an usually are) used
1575 1576
   to make this function prefer range over full table scan. Figure out if
   force_quick_range is really needed.
unknown's avatar
unknown committed
1577

1578 1579 1580 1581
  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.
unknown's avatar
unknown committed
1582
*/
unknown's avatar
unknown committed
1583

1584 1585
int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
				  table_map prev_tables,
unknown's avatar
unknown committed
1586 1587 1588 1589
				  ha_rows limit, bool force_quick_range)
{
  uint idx;
  double scan_time;
1590
  DBUG_ENTER("SQL_SELECT::test_quick_select");
unknown's avatar
unknown committed
1591 1592 1593
  DBUG_PRINT("enter",("keys_to_use: %lu  prev_tables: %lu  const_tables: %lu",
		      keys_to_use.to_ulonglong(), (ulong) prev_tables,
		      (ulong) const_tables));
unknown's avatar
unknown committed
1594 1595 1596

  delete quick;
  quick=0;
1597 1598 1599
  needed_reg.clear_all();
  quick_keys.clear_all();
  if ((specialflag & SPECIAL_SAFE_MODE) && ! force_quick_range ||
unknown's avatar
unknown committed
1600 1601
      !limit)
    DBUG_RETURN(0); /* purecov: inspected */
unknown's avatar
unknown committed
1602 1603
  if (keys_to_use.is_clear_all())
    DBUG_RETURN(0);
1604
  records= head->file->records;
unknown's avatar
unknown committed
1605 1606
  if (!records)
    records++;					/* purecov: inspected */
1607 1608
  scan_time= (double) records / TIME_FOR_COMPARE + 1;
  read_time= (double) head->file->scan_time() + scan_time + 1.1;
1609 1610
  if (head->force_index)
    scan_time= read_time= DBL_MAX;
unknown's avatar
unknown committed
1611
  if (limit < records)
1612
    read_time= (double) records + scan_time + 1; // Force to use index
unknown's avatar
unknown committed
1613
  else if (read_time <= 2.0 && !force_quick_range)
1614
    DBUG_RETURN(0);				/* No need for quick select */
unknown's avatar
unknown committed
1615

1616
  DBUG_PRINT("info",("Time to scan table: %g", read_time));
unknown's avatar
unknown committed
1617

1618 1619
  keys_to_use.intersect(head->keys_in_use_for_query);
  if (!keys_to_use.is_clear_all())
unknown's avatar
unknown committed
1620 1621
  {
    MEM_ROOT *old_root,alloc;
1622
    SEL_TREE *tree= NULL;
unknown's avatar
unknown committed
1623
    KEY_PART *key_parts;
unknown's avatar
unknown committed
1624
    KEY *key_info;
unknown's avatar
unknown committed
1625
    PARAM param;
unknown's avatar
unknown committed
1626

unknown's avatar
unknown committed
1627
    /* set up parameter that is passed to all functions */
1628
    param.thd= thd;
unknown's avatar
unknown committed
1629
    param.baseflag=head->file->table_flags();
unknown's avatar
unknown committed
1630 1631 1632 1633 1634
    param.prev_tables=prev_tables | const_tables;
    param.read_tables=read_tables;
    param.current_table= head->map;
    param.table=head;
    param.keys=0;
1635
    param.mem_root= &alloc;
1636
    param.needed_reg= &needed_reg;
1637
    param.imerge_cost_buff_size= 0;
unknown's avatar
unknown committed
1638

unknown's avatar
unknown committed
1639
    thd->no_errors=1;				// Don't warn about NULL
1640
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
unknown's avatar
unknown committed
1641 1642
    if (!(param.key_parts = (KEY_PART*) alloc_root(&alloc,
						   sizeof(KEY_PART)*
1643 1644
						   head->key_parts))
                              || fill_used_fields_bitmap(&param))
unknown's avatar
unknown committed
1645
    {
unknown's avatar
unknown committed
1646
      thd->no_errors=0;
1647
      free_root(&alloc,MYF(0));			// Return memory & allocator
unknown's avatar
unknown committed
1648 1649 1650
      DBUG_RETURN(0);				// Can't use range
    }
    key_parts= param.key_parts;
1651 1652
    old_root= thd->mem_root;
    thd->mem_root= &alloc;
unknown's avatar
unknown committed
1653 1654 1655 1656

    /*
      Make an array with description of all key parts of all table keys.
      This is used in get_mm_parts function.
1657
    */
unknown's avatar
unknown committed
1658 1659
    key_info= head->key_info;
    for (idx=0 ; idx < head->keys ; idx++, key_info++)
unknown's avatar
unknown committed
1660
    {
unknown's avatar
unknown committed
1661
      KEY_PART_INFO *key_part_info;
1662
      if (!keys_to_use.is_set(idx))
unknown's avatar
unknown committed
1663 1664 1665 1666 1667
	continue;
      if (key_info->flags & HA_FULLTEXT)
	continue;    // ToDo: ft-keys in non-ft ranges, if possible   SerG

      param.key[param.keys]=key_parts;
unknown's avatar
unknown committed
1668 1669 1670
      key_part_info= key_info->key_part;
      for (uint part=0 ; part < key_info->key_parts ;
	   part++, key_parts++, key_part_info++)
unknown's avatar
unknown committed
1671
      {
unknown's avatar
unknown committed
1672 1673 1674 1675 1676 1677
	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;
1678
        key_parts->image_type =
unknown's avatar
unknown committed
1679
          (key_info->flags & HA_SPATIAL) ? Field::itMBR : Field::itRAW;
unknown's avatar
unknown committed
1680 1681 1682 1683 1684
      }
      param.real_keynr[param.keys++]=idx;
    }
    param.key_parts_end=key_parts;

unknown's avatar
unknown committed
1685 1686 1687 1688
    /* Calculate cost of full index read for the shortest covering index */
    if (!head->used_keys.is_clear_all())
    {
      int key_for_use= find_shortest_key(head, &head->used_keys);
1689 1690 1691
      double key_read_time= (get_index_only_read_time(&param, records,
                                                     key_for_use) +
                             (double) records / TIME_FOR_COMPARE);
unknown's avatar
unknown committed
1692 1693 1694 1695 1696
      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;
    }
unknown's avatar
unknown committed
1697

1698 1699 1700 1701 1702 1703 1704 1705
    TABLE_READ_PLAN *best_trp= NULL;
    TRP_GROUP_MIN_MAX *group_trp;
    double best_read_time= read_time;

    if (cond)
      tree= get_mm_tree(&param,cond);

    if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
unknown's avatar
unknown committed
1706
    {
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
      records=0L;                      /* Return -1 from this function. */
      read_time= (double) HA_POS_ERROR;
      goto free_mem;
    }
    else if (tree && tree->type != SEL_TREE::KEY &&
                     tree->type != SEL_TREE::KEY_SMALLER)
      goto free_mem;


    /*
      Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
      Notice that it can be constructed no matter if there is a range tree.
    */
    group_trp= get_best_group_min_max(&param, tree);
    if (group_trp && group_trp->read_cost < best_read_time)
    {
      best_trp= group_trp;
      best_read_time= best_trp->read_cost;
    }

    if (tree)
unknown's avatar
unknown committed
1728
    {
unknown's avatar
unknown committed
1729 1730 1731
      /*
        It is possible to use a range-based quick select (but it might be
        slower than 'all' table scan).
1732 1733
      */
      if (tree->merges.is_empty())
unknown's avatar
unknown committed
1734
      {
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
        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 */
        if ((range_trp= get_key_scans_params(&param, tree, FALSE,
                                             best_read_time)))
        {
          best_trp= range_trp;
          best_read_time= best_trp->read_cost;
        }

unknown's avatar
unknown committed
1747
        /*
1748 1749 1750
          Simultaneous key scans and row deletes on several handler
          objects are not allowed so don't use ROR-intersection for
          table deletes.
unknown's avatar
unknown committed
1751
        */
1752 1753
        if ((thd->lex->sql_command != SQLCOM_DELETE) )//&& 
//          (thd->lex->sql_command != SQLCOM_UPDATE))
unknown's avatar
unknown committed
1754
        {
unknown's avatar
unknown committed
1755
          /*
1756 1757
            Get best non-covering ROR-intersection plan and prepare data for
            building covering ROR-intersection.
unknown's avatar
unknown committed
1758
          */
1759 1760
          if ((rori_trp= get_best_ror_intersect(&param, tree, best_read_time,
                                                &can_build_covering)))
unknown's avatar
unknown committed
1761
          {
1762 1763
            best_trp= rori_trp;
            best_read_time= best_trp->read_cost;
unknown's avatar
unknown committed
1764 1765
            /*
              Try constructing covering ROR-intersect only if it looks possible
1766 1767
              and worth doing.
            */
1768 1769 1770 1771
            if (!rori_trp->is_covering && can_build_covering &&
                (rori_trp= get_best_covering_ror_intersect(&param, tree,
                                                           best_read_time)))
              best_trp= rori_trp;
unknown's avatar
unknown committed
1772 1773
          }
        }
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
      }
      else
      {
        /* 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++))
unknown's avatar
unknown committed
1786
        {
1787 1788 1789 1790
          new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
          if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
                                 best_conj_trp->read_cost))
            best_conj_trp= new_conj_trp;
1791
        }
1792 1793 1794 1795
        if (best_conj_trp)
          best_trp= best_conj_trp;
      }
    }
unknown's avatar
unknown committed
1796

1797
    thd->mem_root= old_root;
1798 1799 1800 1801 1802 1803 1804 1805 1806

    /* 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;
unknown's avatar
unknown committed
1807 1808
      }
    }
1809 1810

  free_mem:
1811
    free_root(&alloc,MYF(0));			// Return memory & allocator
1812
    thd->mem_root= old_root;
unknown's avatar
unknown committed
1813
    thd->no_errors=0;
unknown's avatar
unknown committed
1814
  }
unknown's avatar
unknown committed
1815

1816
  DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
unknown's avatar
unknown committed
1817

unknown's avatar
unknown committed
1818 1819 1820 1821 1822 1823 1824
  /*
    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);
}

unknown's avatar
unknown committed
1825

unknown's avatar
unknown committed
1826
/*
1827 1828 1829 1830
  Get cost of 'sweep' full records retrieval.
  SYNOPSIS
    get_sweep_read_cost()
      param            Parameter from test_quick_select
unknown's avatar
unknown committed
1831
      records          # of records to be retrieved
1832
  RETURN
unknown's avatar
unknown committed
1833
    cost of sweep
1834
*/
1835

1836
double get_sweep_read_cost(const PARAM *param, ha_rows records)
1837
{
1838
  double result;
1839 1840
  if (param->table->file->primary_key_is_clustered())
  {
1841 1842
    result= param->table->file->read_time(param->table->primary_key,
                                          records, records);
1843 1844
  }
  else
unknown's avatar
unknown committed
1845
  {
1846
    double n_blocks=
unknown's avatar
unknown committed
1847
      ceil((double)param->table->file->data_file_length / IO_SIZE);
1848 1849 1850 1851
    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;
unknown's avatar
unknown committed
1852
    DBUG_PRINT("info",("sweep: nblocks=%g, busy_blocks=%g", n_blocks,
1853
                       busy_blocks));
1854
    /*
unknown's avatar
unknown committed
1855
      Disabled: Bail out if # of blocks to read is bigger than # of blocks in
1856 1857 1858 1859 1860 1861 1862 1863
      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' */
unknown's avatar
unknown committed
1864
      result= busy_blocks*(DISK_SEEK_BASE_COST +
1865 1866 1867 1868
                          DISK_SEEK_PROP_COST*n_blocks/busy_blocks);
    }
    else
    {
unknown's avatar
unknown committed
1869
      /*
1870 1871 1872
        Possibly this is a join with source table being non-last table, so
        assume that disk seeks are random here.
      */
1873
      result= busy_blocks;
1874 1875
    }
  }
1876 1877
  DBUG_PRINT("info",("returning cost=%g", result));
  return result;
1878
}
1879 1880


1881 1882 1883 1884
/*
  Get best plan for a SEL_IMERGE disjunctive expression.
  SYNOPSIS
    get_best_disjunct_quick()
1885 1886
      param     Parameter from check_quick_select function
      imerge    Expression to use
1887
      read_time Don't create scans with cost > read_time
unknown's avatar
unknown committed
1888

1889
  NOTES
1890
    index_merge cost is calculated as follows:
unknown's avatar
unknown committed
1891
    index_merge_cost =
1892 1893 1894 1895 1896
      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))
unknown's avatar
unknown committed
1897 1898
       For non-CPK scans,
         cost(index_read_i) = {cost of ordinary 'index only' scan}
1899 1900 1901 1902 1903
       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
unknown's avatar
unknown committed
1904
        cost(rowid_to_row_scan) =
1905
          {cost of ordinary clustered PK scan with n_ranges=n_rows}
unknown's avatar
unknown committed
1906 1907

      Otherwise, we use the following model to calculate costs:
1908
      We need to retrieve n_rows rows from file that occupies n_blocks blocks.
unknown's avatar
unknown committed
1909
      We assume that offsets of rows we need are independent variates with
1910
      uniform distribution in [0..max_file_offset] range.
unknown's avatar
unknown committed
1911

1912 1913
      We'll denote block as "busy" if it contains row(s) we need to retrieve
      and "empty" if doesn't contain rows we need.
unknown's avatar
unknown committed
1914

1915
      Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
unknown's avatar
unknown committed
1916
      applies to any block in file). Let x_i be a variate taking value 1 if
1917
      block #i is empty and 0 otherwise.
unknown's avatar
unknown committed
1918

1919 1920
      Then E(x_i) = (1 - 1/n_blocks)^n_rows;

unknown's avatar
unknown committed
1921 1922
      E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
        = n_blocks * ((1 - 1/n_blocks)^n_rows) =
1923 1924 1925 1926
       ~= 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)).
unknown's avatar
unknown committed
1927

1928 1929
      Average size of "hole" between neighbor non-empty blocks is
           E(hole_size) = n_blocks/E(n_busy_blocks).
unknown's avatar
unknown committed
1930

1931 1932 1933 1934 1935 1936
      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.
unknown's avatar
unknown committed
1937 1938 1939 1940 1941

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

  RETURN
1942 1943
    Created read plan
    NULL - Out of memory or no read scan could be built.
1944
*/
1945

1946 1947
static
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
1948
                                         double read_time)
1949 1950 1951 1952 1953 1954 1955
{
  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;
unknown's avatar
unknown committed
1956
  bool imerge_too_expensive= FALSE;
1957 1958 1959 1960
  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();
unknown's avatar
unknown committed
1961 1962
  bool all_scans_ror_able= TRUE;
  bool all_scans_rors= TRUE;
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
  uint unique_calc_buff_size;
  TABLE_READ_PLAN **roru_read_plans;
  TABLE_READ_PLAN **cur_roru_plan;
  double roru_index_costs;
  double blocks_in_index_read;
  ha_rows roru_total_records;
  double roru_intersect_part= 1.0;
  DBUG_ENTER("get_best_disjunct_quick");
  DBUG_PRINT("info", ("Full table scan cost =%g", read_time));

unknown's avatar
unknown committed
1973
  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
1974 1975 1976
                                             sizeof(TRP_RANGE*)*
                                             n_child_scans)))
    DBUG_RETURN(NULL);
1977
  /*
1978 1979 1980
    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.
1981
  */
1982
  for (ptree= imerge->trees, cur_child= range_scans;
1983
       ptree != imerge->trees_next;
1984
       ptree++, cur_child++)
1985
  {
1986 1987
    DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
                                        "tree in SEL_IMERGE"););
unknown's avatar
unknown committed
1988
    if (!(*cur_child= get_key_scans_params(param, *ptree, TRUE, read_time)))
1989 1990
    {
      /*
1991
        One of index scans in this index_merge is more expensive than entire
1992 1993 1994
        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.
1995
      */
unknown's avatar
unknown committed
1996
      imerge_too_expensive= TRUE;
1997 1998 1999
    }
    if (imerge_too_expensive)
      continue;
unknown's avatar
unknown committed
2000

2001 2002 2003
    imerge_cost += (*cur_child)->read_cost;
    all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
    all_scans_rors &= (*cur_child)->is_ror;
unknown's avatar
unknown committed
2004
    if (pk_is_clustered &&
2005
        param->real_keynr[(*cur_child)->key_idx] == param->table->primary_key)
2006
    {
2007 2008
      cpk_scan= cur_child;
      cpk_scan_records= (*cur_child)->records;
2009 2010
    }
    else
2011
      non_cpk_scan_records += (*cur_child)->records;
2012
  }
unknown's avatar
unknown committed
2013

2014
  DBUG_PRINT("info", ("index_merge scans cost=%g", imerge_cost));
unknown's avatar
unknown committed
2015
  if (imerge_too_expensive || (imerge_cost > read_time) ||
2016 2017
      (non_cpk_scan_records+cpk_scan_records >= param->table->file->records) &&
      read_time != DBL_MAX)
2018
  {
unknown's avatar
unknown committed
2019 2020
    /*
      Bail out if it is obvious that both index_merge and ROR-union will be
2021
      more expensive
2022
    */
2023 2024
    DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
                        "full table scan, bailing out"));
unknown's avatar
unknown committed
2025
    DBUG_RETURN(NULL);
2026
  }
2027
  if (all_scans_rors)
2028
  {
2029 2030
    roru_read_plans= (TABLE_READ_PLAN**)range_scans;
    goto skip_to_ror_scan;
2031
  }
unknown's avatar
unknown committed
2032 2033 2034
  blocks_in_index_read= imerge_cost;
  if (cpk_scan)
  {
2035 2036
    /*
      Add one ROWID comparison for each row retrieved on non-CPK scan.  (it
unknown's avatar
unknown committed
2037 2038 2039
      is done in QUICK_RANGE_SELECT::row_in_ranges)
     */
    imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
2040 2041 2042
  }

  /* Calculate cost(rowid_to_row_scan) */
2043
  imerge_cost += get_sweep_read_cost(param, non_cpk_scan_records);
unknown's avatar
unknown committed
2044
  DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
2045
                     imerge_cost));
2046 2047
  if (imerge_cost > read_time)
    goto build_ror_index_merge;
2048 2049

  /* Add Unique operations cost */
unknown's avatar
unknown committed
2050 2051
  unique_calc_buff_size=
    Unique::get_cost_calc_buff_size(non_cpk_scan_records,
2052 2053 2054 2055 2056 2057
                                    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)))
2058
      DBUG_RETURN(NULL);
2059 2060 2061
    param->imerge_cost_buff_size= unique_calc_buff_size;
  }

unknown's avatar
unknown committed
2062
  imerge_cost +=
2063
    Unique::get_use_cost(param->imerge_cost_buff, non_cpk_scan_records,
unknown's avatar
unknown committed
2064 2065
                         param->table->file->ref_length,
                         param->thd->variables.sortbuff_size);
unknown's avatar
unknown committed
2066
  DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
2067 2068 2069 2070 2071 2072 2073
                     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;
unknown's avatar
unknown committed
2074
      imerge_trp->records= min(imerge_trp->records,
2075 2076 2077 2078 2079 2080
                               param->table->file->records);
      imerge_trp->range_scans= range_scans;
      imerge_trp->range_scans_end= range_scans + n_child_scans;
      read_time= imerge_cost;
    }
  }
unknown's avatar
unknown committed
2081

unknown's avatar
unknown committed
2082
build_ror_index_merge:
2083 2084
  if (!all_scans_ror_able || param->thd->lex->sql_command == SQLCOM_DELETE)
    DBUG_RETURN(imerge_trp);
unknown's avatar
unknown committed
2085

2086 2087
  /* Ok, it is possible to build a ROR-union, try it. */
  bool dummy;
unknown's avatar
unknown committed
2088
  if (!(roru_read_plans=
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
          (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++)
2102
  {
2103 2104
    /*
      Assume the best ROR scan is the one that has cheapest full-row-retrieval
unknown's avatar
unknown committed
2105 2106
      scan cost.
      Also accumulate index_only scan costs as we'll need them to calculate
2107 2108 2109 2110 2111 2112 2113
      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->
unknown's avatar
unknown committed
2114
              read_time(param->real_keynr[(*cur_child)->key_idx], 1,
2115 2116 2117 2118 2119 2120 2121
                        (*cur_child)->records) +
              rows2double((*cur_child)->records) / TIME_FOR_COMPARE;
    }
    else
      cost= read_time;

    TABLE_READ_PLAN *prev_plan= *cur_child;
unknown's avatar
unknown committed
2122
    if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
2123 2124 2125 2126 2127 2128 2129 2130 2131
                                                 &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
unknown's avatar
unknown committed
2132 2133
      roru_index_costs +=
        ((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
2134
    roru_total_records += (*cur_roru_plan)->records;
unknown's avatar
unknown committed
2135
    roru_intersect_part *= (*cur_roru_plan)->records /
2136
                           param->table->file->records;
2137
  }
2138

unknown's avatar
unknown committed
2139 2140
  /*
    rows to retrieve=
2141
      SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
2142
    This is valid because index_merge construction guarantees that conditions
2143 2144 2145
    in disjunction do not share key parts.
  */
  roru_total_records -= (ha_rows)(roru_intersect_part*
unknown's avatar
unknown committed
2146 2147 2148
                                  param->table->file->records);
  /* ok, got a ROR read plan for each of the disjuncts
    Calculate cost:
2149 2150 2151 2152 2153 2154
    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.
  */
unknown's avatar
unknown committed
2155

2156
  double roru_total_cost;
unknown's avatar
unknown committed
2157 2158 2159
  roru_total_cost= roru_index_costs +
                   rows2double(roru_total_records)*log((double)n_child_scans) /
                   (TIME_FOR_COMPARE_ROWID * M_LN2) +
2160 2161
                   get_sweep_read_cost(param, roru_total_records);

unknown's avatar
unknown committed
2162
  DBUG_PRINT("info", ("ROR-union: cost %g, %d members", roru_total_cost,
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176
                      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);
2177 2178 2179 2180 2181 2182 2183
}


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

  SYNOPSIS
2184
    get_index_only_read_time()
2185 2186 2187 2188 2189
      param    parameters structure
      records  #of records to read
      keynr    key to read

  NOTES
unknown's avatar
unknown committed
2190
    It is assumed that we will read trough the whole key range and that all
2191 2192 2193 2194
    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.
2195 2196 2197 2198 2199 2200

  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)
2201 2202
*/

unknown's avatar
unknown committed
2203
inline double get_index_only_read_time(const PARAM* param, ha_rows records,
unknown's avatar
unknown committed
2204
                                       int keynr)
2205 2206 2207 2208 2209 2210 2211
{
  double read_time;
  uint keys_per_block= (param->table->file->block_size/2/
			(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);
unknown's avatar
unknown committed
2212
  return read_time;
2213 2214
}

2215

2216 2217
typedef struct st_ror_scan_info
{
2218 2219 2220 2221 2222
  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. */
unknown's avatar
unknown committed
2223
  SEL_ARG   *sel_arg;
2224 2225

  /* Fields used in the query and covered by this ROR scan. */
unknown's avatar
unknown committed
2226 2227
  MY_BITMAP covered_fields;
  uint      used_fields_covered; /* # of set bits in covered_fields */
2228
  int       key_rec_length; /* length of key record (including rowid) */
2229 2230

  /*
2231 2232
    Cost of reading all index records with values in sel_arg intervals set
    (assuming there is no need to access full table records)
unknown's avatar
unknown committed
2233 2234
  */
  double    index_read_cost;
2235 2236 2237
  uint      first_uncovered_field; /* first unused bit in covered_fields */
  uint      key_components; /* # of parts in the key */
} ROR_SCAN_INFO;
2238 2239 2240


/*
unknown's avatar
unknown committed
2241
  Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
2242
  sel_arg set of intervals.
unknown's avatar
unknown committed
2243

2244 2245
  SYNOPSIS
    make_ror_scan()
2246 2247 2248
      param    Parameter from test_quick_select function
      idx      Index of key in param->keys
      sel_arg  Set of intervals for a given key
2249
  RETURN
unknown's avatar
unknown committed
2250
    NULL - out of memory
2251
    ROR scan structure containing a scan for {idx, sel_arg}
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
*/

static
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
{
  ROR_SCAN_INFO *ror_scan;
  uchar *bitmap_buf;
  uint keynr;
  DBUG_ENTER("make_ror_scan");
  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];
unknown's avatar
unknown committed
2267
  ror_scan->key_rec_length= param->table->key_info[keynr].key_length +
2268 2269 2270
                            param->table->file->ref_length;
  ror_scan->sel_arg= sel_arg;
  ror_scan->records= param->table->quick_rows[keynr];
unknown's avatar
unknown committed
2271 2272

  if (!(bitmap_buf= (uchar*)alloc_root(param->mem_root,
2273 2274
                                      param->fields_bitmap_size)))
    DBUG_RETURN(NULL);
unknown's avatar
unknown committed
2275

2276
  if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
unknown's avatar
unknown committed
2277
                  param->fields_bitmap_size*8, FALSE))
2278 2279
    DBUG_RETURN(NULL);
  bitmap_clear_all(&ror_scan->covered_fields);
unknown's avatar
unknown committed
2280

2281
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
unknown's avatar
unknown committed
2282
  KEY_PART_INFO *key_part_end= key_part +
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
                               param->table->key_info[keynr].key_parts;
  uint n_used_covered= 0;
  for (;key_part != key_part_end; ++key_part)
  {
    if (bitmap_is_set(&param->needed_fields, key_part->fieldnr))
    {
      n_used_covered++;
      bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr);
    }
  }
unknown's avatar
unknown committed
2293
  ror_scan->index_read_cost=
2294 2295 2296 2297 2298 2299
    get_index_only_read_time(param, param->table->quick_rows[ror_scan->keynr],
                             ror_scan->keynr);
  DBUG_RETURN(ror_scan);
}


unknown's avatar
unknown committed
2300
/*
2301 2302 2303 2304 2305 2306 2307
  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
unknown's avatar
unknown committed
2308
   -1 a < b
2309 2310
    0 a = b
    1 a > b
2311
*/
2312
static int cmp_ror_scan_info(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
2313 2314 2315 2316 2317 2318 2319
{
  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;
}

/*
unknown's avatar
unknown committed
2320 2321 2322
  Compare two ROR_SCAN_INFO** by
   (#covered fields in F desc,
    #components asc,
2323
    number of first not covered component asc)
2324 2325 2326 2327 2328 2329 2330

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

  RETURN
unknown's avatar
unknown committed
2331
   -1 a < b
2332 2333
    0 a = b
    1 a > b
2334
*/
2335
static int cmp_ror_scan_info_covering(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
{
  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;
}

/* Auxiliary structure for incremental ROR-intersection creation */
unknown's avatar
unknown committed
2353
typedef struct
2354 2355 2356
{
  const PARAM *param;
  MY_BITMAP covered_fields; /* union of fields covered by all scans */
unknown's avatar
unknown committed
2357

unknown's avatar
unknown committed
2358
  /* TRUE if covered_fields is a superset of needed_fields */
unknown's avatar
unknown committed
2359 2360 2361
  bool is_covering;

  double index_scan_costs; /* SUM(cost of 'index-only' scans) */
2362
  double total_cost;
unknown's avatar
unknown committed
2363
  /*
2364
    Fraction of table records that satisfies conditions of all scans.
unknown's avatar
unknown committed
2365
    This is the number of full records that will be retrieved if a
2366 2367 2368 2369
    non-index_only index intersection will be employed.
  */
  double records_fract;
  ha_rows index_records; /* sum(#records to look in indexes) */
2370
} ROR_INTERSECT_INFO;
2371 2372


2373 2374 2375 2376 2377 2378 2379 2380
/*
  Re-initialize an allocated intersect info to contain zero scans.
  SYNOPSIS
    info Intersection info structure to re-initialize.
*/

static void ror_intersect_reinit(ROR_INTERSECT_INFO *info)
{
unknown's avatar
unknown committed
2381
  info->is_covering= FALSE;
2382 2383 2384 2385 2386 2387 2388 2389 2390
  info->index_scan_costs= 0.0f;
  info->records_fract= 1.0f;
  bitmap_clear_all(&info->covered_fields);
}

/*
  Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.

  SYNOPSIS
unknown's avatar
unknown committed
2391 2392
    ror_intersect_init()
      param         Parameter from test_quick_select
unknown's avatar
unknown committed
2393
      is_index_only If TRUE, set ROR_INTERSECT_INFO to be covering
unknown's avatar
unknown committed
2394

2395 2396 2397 2398 2399 2400
  RETURN
    allocated structure
    NULL on error
*/

static
2401 2402 2403 2404
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param, bool is_index_only)
{
  ROR_INTERSECT_INFO *info;
  uchar* buf;
unknown's avatar
unknown committed
2405
  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
2406 2407 2408 2409 2410 2411
                                              sizeof(ROR_INTERSECT_INFO))))
    return NULL;
  info->param= param;
  if (!(buf= (uchar*)alloc_root(param->mem_root, param->fields_bitmap_size)))
    return NULL;
  if (bitmap_init(&info->covered_fields, buf, param->fields_bitmap_size*8,
unknown's avatar
unknown committed
2412
                  FALSE))
2413
    return NULL;
2414
  ror_intersect_reinit(info);
2415 2416 2417
  return info;
}

2418

2419
/*
unknown's avatar
unknown committed
2420
  Check if adding a ROR scan to a ROR-intersection reduces its cost of
2421 2422
  ROR-intersection and if yes, update parameters of ROR-intersection,
  including its cost.
2423

2424 2425
  SYNOPSIS
    ror_intersect_add()
unknown's avatar
unknown committed
2426
      param        Parameter from test_quick_select
2427 2428
      info         ROR-intersection structure to add the scan to.
      ror_scan     ROR scan info to add.
unknown's avatar
unknown committed
2429
      is_cpk_scan  If TRUE, add the scan as CPK scan (this can be inferred
2430 2431
                   from other parameters and is passed separately only to
                   avoid duplicating the inference code)
unknown's avatar
unknown committed
2432

2433 2434 2435 2436
  NOTES
    Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
    intersection decreases. The cost of ROR-intersection is caclulated as
    follows:
2437

2438
    cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval
unknown's avatar
unknown committed
2439

2440 2441 2442 2443
    if (union of indexes used covers all needed fields)
      cost_of_full_rows_retrieval= 0;
    else
    {
unknown's avatar
unknown committed
2444
      cost_of_full_rows_retrieval=
2445
        cost_of_sweep_read(E(rows_to_retrieve), rows_in_table);
2446 2447
    }

unknown's avatar
unknown committed
2448
    E(rows_to_retrieve) is caclulated as follows:
2449
    Suppose we have a condition on several keys
unknown's avatar
unknown committed
2450 2451
    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
2452 2453
          ...
         k_n1=c_n1 AND k_n3=c_n3 AND ...  (1)
unknown's avatar
unknown committed
2454

2455 2456 2457 2458 2459
    where k_ij may be the same as any k_pq (i.e. keys may have common parts).

    A full row is retrieved iff entire cond holds.

    The recursive procedure for finding P(cond) is as follows:
unknown's avatar
unknown committed
2460

2461
    First step:
unknown's avatar
unknown committed
2462
    Pick 1st part of 1st key and break conjunction (1) into two parts:
2463 2464
      cond= (k_11=c_11 AND R)

unknown's avatar
unknown committed
2465
    Here R may still contain condition(s) equivalent to k_11=c_11.
2466 2467
    Nevertheless, the following holds:

unknown's avatar
unknown committed
2468
      P(k_11=c_11 AND R) = P(k_11=c_11) * P(R|k_11=c_11).
2469 2470 2471 2472 2473

    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:
2474
    We have a set of fixed fields/satisfied conditions) F, probability P(F),
2475 2476 2477
    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).
2478
    Lets denote k_ij as t,  R = t AND R1, where R1 may still contain t. Then
2479

2480
     P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)
2481 2482 2483 2484 2485 2486 2487

    (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

unknown's avatar
unknown committed
2488 2489
    b) F doesn't contain condition on field used in t. Then F and t are
     considered independent.
2490

unknown's avatar
unknown committed
2491
     P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
2492 2493
          = P(t|fields_before_t_in_key).

2494 2495
     P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
                                   #records(fields_before_t_in_key, t)
unknown's avatar
unknown committed
2496 2497

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

2499 2500 2501 2502 2503
  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.

unknown's avatar
unknown committed
2504
    The calculation is conducted as follows:
2505
    Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate
unknown's avatar
unknown committed
2506

2507 2508
     n_{k1}      n_{k_2}
    --------- * ---------  * .... (3)
unknown's avatar
unknown committed
2509
     n_{k1-1}    n_{k2_1}
2510

unknown's avatar
unknown committed
2511 2512 2513 2514 2515
    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
    as fixed, we calcualate (3) as
2516 2517 2518

                                  n_{i1}      n_{i_2}
    (3) = n_{max_key_part}  / (   --------- * ---------  * ....  )
unknown's avatar
unknown committed
2519 2520 2521 2522
                                  n_{i1-1}    n_{i2_1}

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

2523 2524
    In order to minimize number of expensive records_in_range calls we group
    and reduce adjacent fractions.
unknown's avatar
unknown committed
2525

2526
  RETURN
unknown's avatar
unknown committed
2527 2528
    TRUE   ROR scan added to ROR-intersection, cost updated.
    FALSE  It doesn't make sense to add this ROR scan to this ROR-intersection.
2529 2530
*/

2531
bool ror_intersect_add(const PARAM *param, ROR_INTERSECT_INFO *info,
unknown's avatar
unknown committed
2532
                       ROR_SCAN_INFO* ror_scan, bool is_cpk_scan=FALSE)
2533 2534 2535
{
  int i;
  SEL_ARG *sel_arg;
unknown's avatar
unknown committed
2536
  KEY_PART_INFO *key_part=
2537 2538
    info->param->table->key_info[ror_scan->keynr].key_part;
  double selectivity_mult= 1.0;
unknown's avatar
unknown committed
2539
  byte key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
2540

unknown's avatar
unknown committed
2541 2542 2543
  DBUG_ENTER("ror_intersect_add");
  DBUG_PRINT("info", ("Current selectivity= %g", info->records_fract));
  DBUG_PRINT("info", ("Adding scan on %s",
2544
                      info->param->table->key_info[ror_scan->keynr].name));
2545
  SEL_ARG *tuple_arg= NULL;
2546
  char *key_ptr= (char*) key_val;
2547 2548 2549 2550
  bool cur_covered, prev_covered=
     bitmap_is_set(&info->covered_fields, key_part->fieldnr);

  ha_rows prev_records= param->table->file->records;
unknown's avatar
unknown committed
2551 2552 2553 2554 2555 2556
  key_range min_range;
  key_range max_range;
  min_range.key= (byte*) key_val;
  min_range.flag= HA_READ_KEY_EXACT;
  max_range.key= (byte*) key_val;
  max_range.flag= HA_READ_AFTER_KEY;
2557

unknown's avatar
unknown committed
2558
  for(i= 0, sel_arg= ror_scan->sel_arg; sel_arg;
2559 2560
      i++, sel_arg= sel_arg->next_key_part)
  {
2561 2562
    cur_covered= bitmap_is_set(&info->covered_fields, (key_part + i)->fieldnr);
    if (cur_covered != prev_covered)
2563
    {
2564 2565 2566 2567 2568
      /* create (part1val, ..., part{n-1}val) tuple. */
      {
        if (!tuple_arg)
        {
          tuple_arg= ror_scan->sel_arg;
unknown's avatar
unknown committed
2569
          tuple_arg->store_min(key_part->length, &key_ptr, 0);
2570 2571 2572
        }
        while (tuple_arg->next_key_part != sel_arg)
        {
unknown's avatar
unknown committed
2573 2574
          tuple_arg= tuple_arg->next_key_part;
          tuple_arg->store_min(key_part->length, &key_ptr, 0);
2575
        }
unknown's avatar
unknown committed
2576
      }
2577
      ha_rows records;
2578
      min_range.length= max_range.length= ((char*) key_ptr - (char*) key_val);
2579 2580
      records= param->table->file->
                 records_in_range(ror_scan->keynr,
unknown's avatar
unknown committed
2581 2582
                                  &min_range,
                                  &max_range);
2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
      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 */
unknown's avatar
unknown committed
2594
        prev_records= records;
2595
      }
2596
    }
2597 2598 2599 2600
    prev_covered= cur_covered;
  }
  if (!prev_covered)
  {
unknown's avatar
unknown committed
2601
    double tmp= rows2double(param->table->quick_rows[ror_scan->keynr]) /
2602 2603
                rows2double(prev_records);
    DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
unknown's avatar
unknown committed
2604
    selectivity_mult *= tmp;
2605
  }
2606

2607 2608 2609
  if (selectivity_mult == 1.0)
  {
    /* Don't add this scan if it doesn't improve selectivity. */
2610
    DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
unknown's avatar
unknown committed
2611
    DBUG_RETURN(FALSE);
2612 2613
  }

2614
  info->records_fract *= selectivity_mult;
unknown's avatar
unknown committed
2615
  ha_rows cur_scan_records= info->param->table->quick_rows[ror_scan->keynr];
2616
  if (is_cpk_scan)
unknown's avatar
unknown committed
2617
  {
2618 2619 2620 2621 2622 2623 2624 2625 2626
    info->index_scan_costs += rows2double(cur_scan_records)*
                              TIME_FOR_COMPARE_ROWID;
  }
  else
  {
    info->index_records += cur_scan_records;
    info->index_scan_costs += ror_scan->index_read_cost;
    bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
  }
unknown's avatar
unknown committed
2627

2628 2629 2630 2631
  if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
                                             &info->covered_fields))
  {
    DBUG_PRINT("info", ("ROR-intersect is covering now"));
unknown's avatar
unknown committed
2632
    info->is_covering= TRUE;
2633
  }
unknown's avatar
unknown committed
2634

2635 2636 2637 2638
  info->total_cost= info->index_scan_costs;
  if (!info->is_covering)
  {
    ha_rows table_recs= info->param->table->file->records;
unknown's avatar
unknown committed
2639 2640
    info->total_cost +=
      get_sweep_read_cost(info->param,
2641
                          (ha_rows)(info->records_fract*table_recs));
2642
  }
unknown's avatar
unknown committed
2643 2644
  DBUG_PRINT("info", ("New selectivity= %g", info->records_fract));
  DBUG_PRINT("info", ("New cost= %g, %scovering", info->total_cost,
2645
                      info->is_covering?"" : "non-"));
unknown's avatar
unknown committed
2646
  DBUG_RETURN(TRUE);
2647 2648
}

2649

unknown's avatar
unknown committed
2650 2651
/*
  Get best ROR-intersection plan using non-covering ROR-intersection search
2652 2653 2654 2655
  algorithm. The returned plan may be covering.

  SYNOPSIS
    get_best_ror_intersect()
2656 2657 2658
      param            Parameter from test_quick_select function.
      tree             Transformed restriction condition to be used to look
                       for ROR scans.
2659
      read_time        Do not return read plans with cost > read_time.
unknown's avatar
unknown committed
2660
      are_all_covering [out] set to TRUE if union of all scans covers all
2661 2662
                       fields needed by the query (and it is possible to build
                       a covering ROR-intersection)
2663

2664
  NOTES
unknown's avatar
unknown committed
2665
    get_key_scans_params must be called before for the same SEL_TREE before
2666
    this function can be called.
unknown's avatar
unknown committed
2667

2668
    The approximate best non-covering plan search algorithm is as follows:
2669

2670 2671 2672 2673
    find_min_ror_intersection_scan()
    {
      R= select all ROR scans;
      order R by (E(#records_matched) * key_record_length).
unknown's avatar
unknown committed
2674

2675 2676 2677 2678 2679 2680 2681 2682
      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)
      {
        if (!selectivity(S + first(R) < selectivity(S)))
          continue;
2683

2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
        S= S + first(R);
        R= R - first(R);
        if (cost(S) < min_cost)
        {
          min_cost= cost(S);
          min_scan= make_scan(S);
        }
      }
      return min_scan;
    }
2694

2695
    See ror_intersect_add function for ROR intersection costs.
2696

2697
    Special handling for Clustered PK scans
unknown's avatar
unknown committed
2698 2699
    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
2700 2701
    expensive in this case.
    Clustered PK scan has special handling in ROR-intersection: it is not used
unknown's avatar
unknown committed
2702
    to retrieve rows, instead its condition is used to filter row references
2703
    we get from scans on other keys.
2704 2705

  RETURN
unknown's avatar
unknown committed
2706
    ROR-intersection table read plan
2707
    NULL if out of memory or no suitable plan found.
2708 2709
*/

2710 2711 2712 2713 2714 2715 2716 2717
static
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
                                          double read_time,
                                          bool *are_all_covering)
{
  uint idx;
  double min_cost= read_time;
  DBUG_ENTER("get_best_ror_intersect");
2718

2719 2720
  if (tree->n_ror_scans < 2)
    DBUG_RETURN(NULL);
2721 2722

  /*
2723 2724
    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.
2725
  */
2726
  ROR_SCAN_INFO **cur_ror_scan;
2727
  ROR_SCAN_INFO *cpk_scan= NULL;
unknown's avatar
unknown committed
2728
  bool cpk_scan_used= FALSE;
2729 2730 2731 2732
  if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
                                                     sizeof(ROR_SCAN_INFO*)*
                                                     param->keys)))
    return NULL;
2733 2734
  uint cpk_no= (param->table->file->primary_key_is_clustered())?
               param->table->primary_key : MAX_KEY;
unknown's avatar
unknown committed
2735

2736
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
2737
  {
2738
    ROR_SCAN_INFO *scan;
2739
    if (!tree->ror_scans_map.is_set(idx))
2740
      continue;
2741
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
2742
      return NULL;
2743
    if (param->real_keynr[idx] == cpk_no)
2744
    {
2745 2746
      cpk_scan= scan;
      tree->n_ror_scans--;
2747 2748
    }
    else
2749
      *(cur_ror_scan++)= scan;
2750
  }
unknown's avatar
unknown committed
2751

2752
  tree->ror_scans_end= cur_ror_scan;
unknown's avatar
unknown committed
2753 2754
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
                                          tree->ror_scans,
2755 2756
                                          tree->ror_scans_end););
  /*
unknown's avatar
unknown committed
2757
    Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
2758
    ROR_SCAN_INFOs.
2759
    Now, get a minimal key scan using an approximate algorithm.
2760 2761 2762
  */
  qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
        (qsort_cmp)cmp_ror_scan_info);
unknown's avatar
unknown committed
2763 2764
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
                                          tree->ror_scans,
2765
                                          tree->ror_scans_end););
unknown's avatar
unknown committed
2766

2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
  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. */
  ROR_INTERSECT_INFO *intersect;
unknown's avatar
unknown committed
2777
  if (!(intersect= ror_intersect_init(param, FALSE)))
2778
    return NULL;
unknown's avatar
unknown committed
2779

2780
  /* [intersect_scans, intersect_scans_best) will hold the best combination */
unknown's avatar
unknown committed
2781
  ROR_SCAN_INFO **intersect_scans_best;
2782 2783 2784 2785 2786 2787 2788 2789
  ha_rows       best_rows;
  bool          is_best_covering;
  double        best_index_scan_costs;
  LINT_INIT(best_rows);       /* protected by intersect_scans_best */
  LINT_INIT(is_best_covering);
  LINT_INIT(best_index_scan_costs);

  cur_ror_scan= tree->ror_scans;
2790 2791
  /* Start with one scan */
  intersect_scans_best= intersect_scans;
2792
  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
2793
  {
2794
    /* S= S + first(R); */
2795
    if (ror_intersect_add(param, intersect, *cur_ror_scan))
unknown's avatar
unknown committed
2796
      *(intersect_scans_end++)= *cur_ror_scan;
2797
    /* R= R - first(R); */
2798
    cur_ror_scan++;
unknown's avatar
unknown committed
2799

2800
    if (intersect->total_cost < min_cost)
2801
    {
2802 2803
      /* Local minimum found, save it */
      min_cost= intersect->total_cost;
unknown's avatar
unknown committed
2804
      best_rows= (ha_rows)(intersect->records_fract*
2805 2806 2807 2808
                           rows2double(param->table->file->records));
      is_best_covering= intersect->is_covering;
      intersect_scans_best= intersect_scans_end;
      best_index_scan_costs= intersect->index_scan_costs;
2809 2810
    }
  }
unknown's avatar
unknown committed
2811

2812 2813 2814 2815
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
                                          "best ROR-intersection",
                                          intersect_scans,
                                          intersect_scans_best););
unknown's avatar
unknown committed
2816

2817
  *are_all_covering= intersect->is_covering;
unknown's avatar
unknown committed
2818
  uint best_num= intersect_scans_best - intersect_scans;
2819 2820 2821
  /*
    Ok, found the best ROR-intersection of non-CPK key scans.
    Check if we should add a CPK scan.
unknown's avatar
unknown committed
2822 2823

    If the obtained ROR-intersection is covering, it doesn't make sense
2824 2825 2826 2827
    to add CPK scan - Clustered PK contains all fields and if we're doing
    CPK scan doing other CPK scans will only add more overhead.
  */
  if (cpk_scan && !intersect->is_covering)
2828
  {
2829
    /*
unknown's avatar
unknown committed
2830
      Handle the special case: ROR-intersect(PRIMARY, key1) is
2831
      the best, but cost(range(key1)) > cost(best_non_ror_range_scan)
2832
    */
2833
    if (best_num == 0)
unknown's avatar
unknown committed
2834
    {
2835 2836
      cur_ror_scan= tree->ror_scans;
      intersect_scans_end= intersect_scans;
2837
      ror_intersect_reinit(intersect);
2838
      if (!ror_intersect_add(param, intersect, *cur_ror_scan))
2839
        DBUG_RETURN(NULL); /* shouldn't happen actually */
2840 2841 2842
      *(intersect_scans_end++)= *cur_ror_scan;
      best_num++;
    }
2843

2844
    if (ror_intersect_add(param, intersect, cpk_scan))
2845
    {
unknown's avatar
unknown committed
2846
      cpk_scan_used= TRUE;
2847
      min_cost= intersect->total_cost;
unknown's avatar
unknown committed
2848
      best_rows= (ha_rows)(intersect->records_fract*
2849 2850 2851
                           rows2double(param->table->file->records));
      is_best_covering= intersect->is_covering;
      best_index_scan_costs= intersect->index_scan_costs;
2852 2853
    }
  }
unknown's avatar
unknown committed
2854

2855
  /* Ok, return ROR-intersect plan if we have found one */
2856
  TRP_ROR_INTERSECT *trp= NULL;
2857
  if (best_num > 1 || cpk_scan_used)
2858
  {
2859 2860
    if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
      DBUG_RETURN(trp);
unknown's avatar
unknown committed
2861 2862
    if (!(trp->first_scan=
           (ROR_SCAN_INFO**)alloc_root(param->mem_root,
2863 2864 2865 2866 2867 2868 2869 2870
                                       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;
    trp->is_covering= is_best_covering;
    trp->read_cost= min_cost;
    trp->records= best_rows? best_rows : 1;
    trp->index_scan_costs= best_index_scan_costs;
2871
    trp->cpk_scan= cpk_scan;
unknown's avatar
unknown committed
2872
  }
2873 2874 2875
  DBUG_PRINT("info",
             ("Returning non-covering ROR-intersect plan: cost %g, records %lu",
              trp->read_cost, (ulong) trp->records));
2876
  DBUG_RETURN(trp);
2877 2878 2879 2880
}


/*
2881
  Get best covering ROR-intersection.
2882
  SYNOPSIS
2883
    get_best_covering_ror_intersect()
2884 2885 2886
      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.
2887

unknown's avatar
unknown committed
2888 2889
  RETURN
    Best covering ROR-intersection plan
2890
    NULL if no plan found.
2891 2892

  NOTES
2893
    get_best_ror_intersect must be called for a tree before calling this
unknown's avatar
unknown committed
2894
    function for it.
2895
    This function invalidates tree->ror_scans member values.
unknown's avatar
unknown committed
2896

2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
  The following approximate algorithm is used:
    I=set of all covering indexes
    F=set of all fields to cover
    S={}

    do {
      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.
2910 2911
*/

2912
static
unknown's avatar
unknown committed
2913 2914
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
                                                   SEL_TREE *tree,
2915
                                                   double read_time)
2916
{
2917
  ROR_SCAN_INFO **ror_scan_mark;
unknown's avatar
unknown committed
2918
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
2919 2920 2921 2922
  DBUG_ENTER("get_best_covering_ror_intersect");
  uint nbits= param->fields_bitmap_size*8;

  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
unknown's avatar
unknown committed
2923
    (*scan)->key_components=
2924
      param->table->key_info[(*scan)->keynr].key_parts;
unknown's avatar
unknown committed
2925

2926 2927
  /*
    Run covering-ROR-search algorithm.
unknown's avatar
unknown committed
2928
    Assume set I is [ror_scan .. ror_scans_end)
2929
  */
unknown's avatar
unknown committed
2930

2931 2932
  /*I=set of all covering indexes */
  ror_scan_mark= tree->ror_scans;
unknown's avatar
unknown committed
2933

2934 2935
  uchar buf[MAX_KEY/8+1];
  MY_BITMAP covered_fields;
unknown's avatar
unknown committed
2936
  if (bitmap_init(&covered_fields, buf, nbits, FALSE))
2937 2938 2939 2940 2941
    DBUG_RETURN(0);
  bitmap_clear_all(&covered_fields);

  double total_cost= 0.0f;
  ha_rows records=0;
unknown's avatar
unknown committed
2942 2943
  bool all_covered;

2944 2945 2946 2947 2948 2949
  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););
  do {
    /*
unknown's avatar
unknown committed
2950
      Update changed sorting info:
2951
        #covered fields,
unknown's avatar
unknown committed
2952
	number of first not covered component
2953 2954 2955 2956 2957
      Calculate and save these values for each of remaining scans.
    */
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
    {
      bitmap_subtract(&(*scan)->covered_fields, &covered_fields);
unknown's avatar
unknown committed
2958
      (*scan)->used_fields_covered=
2959
        bitmap_bits_set(&(*scan)->covered_fields);
unknown's avatar
unknown committed
2960
      (*scan)->first_uncovered_field=
2961 2962 2963 2964 2965 2966 2967 2968 2969
        bitmap_get_first(&(*scan)->covered_fields);
    }

    qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
          (qsort_cmp)cmp_ror_scan_info_covering);

    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
                                             "remaining scans",
                                             ror_scan_mark, ror_scans_end););
unknown's avatar
unknown committed
2970

2971 2972 2973
    /* I=I-first(I) */
    total_cost += (*ror_scan_mark)->index_read_cost;
    records += (*ror_scan_mark)->records;
unknown's avatar
unknown committed
2974
    DBUG_PRINT("info", ("Adding scan on %s",
2975 2976 2977 2978 2979 2980 2981
                        param->table->key_info[(*ror_scan_mark)->keynr].name));
    if (total_cost > read_time)
      DBUG_RETURN(NULL);
    /* F=F-covered by first(I) */
    bitmap_union(&covered_fields, &(*ror_scan_mark)->covered_fields);
    all_covered= bitmap_is_subset(&param->needed_fields, &covered_fields);
  } while (!all_covered && (++ror_scan_mark < ror_scans_end));
unknown's avatar
unknown committed
2982

2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
  if (!all_covered)
    DBUG_RETURN(NULL); /* should not happen actually */

  /*
    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););
unknown's avatar
unknown committed
2994

2995
  /* Add priority queue use cost. */
unknown's avatar
unknown committed
2996 2997
  total_cost += rows2double(records)*
                log((double)(ror_scan_mark - tree->ror_scans)) /
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
                (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);
  memcpy(trp->first_scan, ror_scan_mark, best_num*sizeof(ROR_SCAN_INFO*));
  trp->last_scan=  trp->first_scan + best_num;
unknown's avatar
unknown committed
3014
  trp->is_covering= TRUE;
3015 3016 3017
  trp->read_cost= total_cost;
  trp->records= records;

3018 3019 3020
  DBUG_PRINT("info",
             ("Returning covering ROR-intersect plan: cost %g, records %lu",
              trp->read_cost, (ulong) trp->records));
3021
  DBUG_RETURN(trp);
3022 3023 3024
}


unknown's avatar
unknown committed
3025
/*
unknown's avatar
unknown committed
3026
  Get best "range" table read plan for given SEL_TREE.
3027
  Also update PARAM members and store ROR scans info in the SEL_TREE.
3028
  SYNOPSIS
3029
    get_key_scans_params
3030
      param        parameters from test_quick_select
unknown's avatar
unknown committed
3031
      tree         make range select for this SEL_TREE
unknown's avatar
unknown committed
3032
      index_read_must_be_used if TRUE, assume 'index only' option will be set
3033
                             (except for clustered PK indexes)
3034 3035
      read_time    don't create read plans with cost > read_time.
  RETURN
unknown's avatar
unknown committed
3036
    Best range read plan
3037
    NULL if no plan found or error occurred
unknown's avatar
unknown committed
3038 3039
*/

3040
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
unknown's avatar
unknown committed
3041
                                       bool index_read_must_be_used,
3042
                                       double read_time)
unknown's avatar
unknown committed
3043 3044
{
  int idx;
3045 3046 3047
  SEL_ARG **key,**end, **key_to_read= NULL;
  ha_rows best_records;
  TRP_RANGE* read_plan= NULL;
3048
  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
3049 3050
  DBUG_ENTER("get_key_scans_params");
  LINT_INIT(best_records); /* protected by key_to_read */
unknown's avatar
unknown committed
3051
  /*
unknown's avatar
unknown committed
3052 3053
    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
3054
    is defined as "not null".
unknown's avatar
unknown committed
3055 3056
  */
  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
3057 3058 3059 3060
                                      "tree scans"););
  tree->ror_scans_map.clear_all();
  tree->n_ror_scans= 0;
  for (idx= 0,key=tree->keys, end=key+param->keys;
unknown's avatar
unknown committed
3061 3062 3063 3064 3065 3066 3067
       key != end ;
       key++,idx++)
  {
    ha_rows found_records;
    double found_read_time;
    if (*key)
    {
3068
      uint keynr= param->real_keynr[idx];
unknown's avatar
unknown committed
3069 3070
      if ((*key)->type == SEL_ARG::MAYBE_KEY ||
          (*key)->maybe_flag)
3071
        param->needed_reg->set_bit(keynr);
unknown's avatar
unknown committed
3072

unknown's avatar
unknown committed
3073 3074
      bool read_index_only= index_read_must_be_used ? TRUE :
                            (bool) param->table->used_keys.is_set(keynr);
3075

3076 3077 3078 3079 3080 3081
      found_records= check_quick_select(param, idx, *key);
      if (param->is_ror_scan)
      {
        tree->n_ror_scans++;
        tree->ror_scans_map.set_bit(idx);
      }
3082
      double cpu_cost= (double) found_records / TIME_FOR_COMPARE;
unknown's avatar
unknown committed
3083
      if (found_records != HA_POS_ERROR && found_records > 2 &&
unknown's avatar
unknown committed
3084
          read_index_only &&
unknown's avatar
unknown committed
3085
          (param->table->file->index_flags(keynr, param->max_key_part,1) &
unknown's avatar
unknown committed
3086
           HA_KEYREAD_ONLY) &&
3087
          !(pk_is_clustered && keynr == param->table->primary_key))
unknown's avatar
unknown committed
3088
        /* We can resolve this by only reading through this key. */
3089 3090
        found_read_time= get_index_only_read_time(param,found_records,keynr) +
                         cpu_cost;
unknown's avatar
unknown committed
3091
      else
unknown's avatar
unknown committed
3092
        /*
3093 3094 3095
          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.
        */
3096 3097 3098 3099
	found_read_time= param->table->file->read_time(keynr,
                                                       param->range_count,
                                                       found_records) +
			 cpu_cost;
3100

unknown's avatar
unknown committed
3101 3102
      DBUG_PRINT("info",("read_time: %g  found_read_time: %g",
                         read_time, found_read_time));
3103

3104 3105
      if (read_time > found_read_time && found_records != HA_POS_ERROR
          /*|| read_time == DBL_MAX*/ )
unknown's avatar
unknown committed
3106
      {
3107
        read_time=    found_read_time;
unknown's avatar
unknown committed
3108
        best_records= found_records;
3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
        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;
3125 3126 3127 3128
      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));
3129 3130 3131 3132 3133 3134 3135 3136 3137
    }
  }
  else
    DBUG_PRINT("info", ("No 'range' table read plan found"));

  DBUG_RETURN(read_plan);
}


unknown's avatar
unknown committed
3138
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153
                                            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;
  for(TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
      range_scan++)
  {
    if (!(quick= (QUICK_RANGE_SELECT*)
unknown's avatar
unknown committed
3154
           ((*range_scan)->make_quick(param, FALSE, &quick_imerge->alloc)))||
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
        quick_imerge->push_quick_back(quick))
    {
      delete quick;
      delete quick_imerge;
      return NULL;
    }
  }
  return quick_imerge;
}

unknown's avatar
unknown committed
3165
QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
3166 3167 3168 3169 3170 3171 3172
                                              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;
unknown's avatar
unknown committed
3173 3174

  if ((quick_intrsect=
3175
         new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
unknown's avatar
unknown committed
3176
                                        retrieve_full_rows? (!is_covering):FALSE,
3177 3178
                                        parent_alloc)))
  {
unknown's avatar
unknown committed
3179
    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
3180 3181 3182 3183 3184 3185 3186 3187
                                             "creating ROR-intersect",
                                             first_scan, last_scan););
    alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
    for(; first_scan != last_scan;++first_scan)
    {
      if (!(quick= get_quick_select(param, (*first_scan)->idx,
                                    (*first_scan)->sel_arg, alloc)) ||
          quick_intrsect->push_quick_back(quick))
unknown's avatar
unknown committed
3188
      {
3189 3190
        delete quick_intrsect;
        DBUG_RETURN(NULL);
unknown's avatar
unknown committed
3191 3192
      }
    }
3193 3194 3195 3196
    if (cpk_scan)
    {
      if (!(quick= get_quick_select(param, cpk_scan->idx,
                                    cpk_scan->sel_arg, alloc)))
unknown's avatar
unknown committed
3197
      {
3198 3199
        delete quick_intrsect;
        DBUG_RETURN(NULL);
unknown's avatar
unknown committed
3200
      }
unknown's avatar
unknown committed
3201
      quick->file= NULL; 
3202
      quick_intrsect->cpk_quick= quick;
unknown's avatar
unknown committed
3203
    }
unknown's avatar
unknown committed
3204
    quick_intrsect->records= records;
3205
    quick_intrsect->read_time= read_cost;
unknown's avatar
unknown committed
3206
  }
3207 3208 3209
  DBUG_RETURN(quick_intrsect);
}

3210

unknown's avatar
unknown committed
3211
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
3212 3213 3214 3215 3216 3217 3218
                                          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");
unknown's avatar
unknown committed
3219 3220
  /*
    It is impossible to construct a ROR-union that will not retrieve full
3221
    rows, ignore retrieve_full_rows parameter.
3222 3223 3224 3225 3226
  */
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
  {
    for(scan= first_ror; scan != last_ror; scan++)
    {
unknown's avatar
unknown committed
3227
      if (!(quick= (*scan)->make_quick(param, FALSE, &quick_roru->alloc)) ||
3228 3229 3230 3231 3232
          quick_roru->push_quick_back(quick))
        DBUG_RETURN(NULL);
    }
    quick_roru->records= records;
    quick_roru->read_time= read_cost;
unknown's avatar
unknown committed
3233
  }
3234
  DBUG_RETURN(quick_roru);
unknown's avatar
unknown committed
3235 3236
}

3237

unknown's avatar
unknown committed
3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
/*
  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

  RETURN 
    Pointer to thre built tree
*/

3253 3254 3255 3256 3257 3258 3259
static SEL_TREE *get_func_mm_tree(PARAM *param, Item_func *cond_func, 
                                  Field *field, Item *value,
                                  Item_result cmp_type)
{
  SEL_TREE *tree= 0;
  DBUG_ENTER("get_func_mm_tree");

unknown's avatar
unknown committed
3260 3261
  switch (cond_func->functype()) {
  case Item_func::NE_FUNC:
3262
    tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
3263 3264 3265
		       value, cmp_type);
    if (tree)
    {
3266
      tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
unknown's avatar
unknown committed
3267 3268
					      Item_func::GT_FUNC,
					      value, cmp_type));
3269
    }
unknown's avatar
unknown committed
3270 3271
    break;
  case Item_func::BETWEEN:
3272
    tree= get_mm_parts(param, cond_func, field, Item_func::GE_FUNC,
3273 3274 3275
		       cond_func->arguments()[1],cmp_type);
    if (tree)
    {
3276
      tree= tree_and(param, tree, get_mm_parts(param, cond_func, field,
3277 3278 3279 3280
					       Item_func::LE_FUNC,
					       cond_func->arguments()[2],
                                               cmp_type));
    }
unknown's avatar
unknown committed
3281 3282
    break;
  case Item_func::IN_FUNC:
3283 3284
  {
    Item_func_in *func=(Item_func_in*) cond_func;
3285
    tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
3286 3287 3288
                       func->arguments()[1], cmp_type);
    if (tree)
    {
unknown's avatar
unknown committed
3289 3290 3291
      Item **arg, **end;
      for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
           arg < end ; arg++)
3292
      {
3293
        tree=  tree_or(param, tree, get_mm_parts(param, cond_func, field, 
3294
                                                 Item_func::EQ_FUNC,
unknown's avatar
unknown committed
3295
                                                 *arg,
3296 3297 3298
                                                 cmp_type));
      }
    }
unknown's avatar
unknown committed
3299
    break;
3300
  }
unknown's avatar
unknown committed
3301
  default: 
3302
  {
unknown's avatar
unknown committed
3303 3304 3305 3306 3307 3308 3309
    /* 
       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.
    */
3310 3311 3312
    Item_func::Functype func_type=
      (value != cond_func->arguments()[0]) ? cond_func->functype() :
        ((Item_bool_func2*) cond_func)->rev_functype();
3313
    tree= get_mm_parts(param, cond_func, field, func_type, value, cmp_type);
3314
  }
unknown's avatar
unknown committed
3315 3316
  }

3317
  DBUG_RETURN(tree);
3318

3319 3320
}

unknown's avatar
unknown committed
3321 3322 3323 3324 3325
	/* make a select tree of all keys in condition */

static SEL_TREE *get_mm_tree(PARAM *param,COND *cond)
{
  SEL_TREE *tree=0;
3326 3327 3328
  SEL_TREE *ftree= 0;
  Item_field *field_item= 0;
  Item *value;
unknown's avatar
unknown committed
3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341
  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);
3342
	if (param->thd->is_fatal_error)
3343
	  DBUG_RETURN(0);	// out of memory
unknown's avatar
unknown committed
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358
	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)
3359
	    DBUG_RETURN(0);	// out of memory
unknown's avatar
unknown committed
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
	  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())
  {
    if (cond->val_int())
      DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
    DBUG_RETURN(new SEL_TREE(SEL_TREE::IMPOSSIBLE));
  }
3375

3376 3377 3378
  table_map ref_tables= 0;
  table_map param_comp= ~(param->prev_tables | param->read_tables |
		          param->current_table);
unknown's avatar
unknown committed
3379 3380
  if (cond->type() != Item::FUNC_ITEM)
  {						// Should be a field
3381
    ref_tables= cond->used_tables();
unknown's avatar
unknown committed
3382 3383
    if ((ref_tables & param->current_table) ||
	(ref_tables & ~(param->prev_tables | param->read_tables)))
unknown's avatar
unknown committed
3384 3385 3386
      DBUG_RETURN(0);
    DBUG_RETURN(new SEL_TREE(SEL_TREE::MAYBE));
  }
3387

unknown's avatar
unknown committed
3388 3389 3390 3391
  Item_func *cond_func= (Item_func*) cond;
  if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
    DBUG_RETURN(0);				// Can't be calculated

unknown's avatar
unknown committed
3392 3393
  param->cond= cond;

unknown's avatar
unknown committed
3394 3395 3396
  switch (cond_func->functype()) {
  case Item_func::BETWEEN:
    if (cond_func->arguments()[0]->type() != Item::FIELD_ITEM)
3397
      DBUG_RETURN(0);
unknown's avatar
unknown committed
3398 3399 3400 3401
    field_item= (Item_field*) (cond_func->arguments()[0]);
    value= NULL;
    break;
  case Item_func::IN_FUNC:
unknown's avatar
unknown committed
3402 3403
  {
    Item_func_in *func=(Item_func_in*) cond_func;
unknown's avatar
unknown committed
3404
    if (func->key_item()->type() != Item::FIELD_ITEM)
3405
      DBUG_RETURN(0);
unknown's avatar
unknown committed
3406 3407 3408
    field_item= (Item_field*) (func->key_item());
    value= NULL;
    break;
3409
  }
unknown's avatar
unknown committed
3410
  case Item_func::MULT_EQUAL_FUNC:
unknown's avatar
unknown committed
3411
  {
3412 3413
    Item_equal *item_equal= (Item_equal *) cond;    
    if (!(value= item_equal->get_const()))
unknown's avatar
unknown committed
3414 3415 3416 3417
      DBUG_RETURN(0);
    Item_equal_iterator it(*item_equal);
    ref_tables= value->used_tables();
    while ((field_item= it++))
unknown's avatar
unknown committed
3418
    {
unknown's avatar
unknown committed
3419 3420 3421
      Field *field= field_item->field;
      Item_result cmp_type= field->cmp_type();
      if (!((ref_tables | field->table->map) & param_comp))
unknown's avatar
unknown committed
3422
      {
3423
        tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
unknown's avatar
unknown committed
3424 3425
		           value,cmp_type);
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
unknown's avatar
unknown committed
3426 3427
      }
    }
unknown's avatar
unknown committed
3428
    
3429
    DBUG_RETURN(ftree);
unknown's avatar
unknown committed
3430 3431
  }
  default:
unknown's avatar
unknown committed
3432 3433
    if (cond_func->arguments()[0]->type() == Item::FIELD_ITEM)
    {
unknown's avatar
unknown committed
3434 3435
      field_item= (Item_field*) (cond_func->arguments()[0]);
      value= cond_func->arg_count > 1 ? cond_func->arguments()[1] : 0;
unknown's avatar
unknown committed
3436
    }
unknown's avatar
unknown committed
3437 3438 3439 3440 3441 3442 3443 3444
    else if (cond_func->have_rev_func() &&
             cond_func->arguments()[1]->type() == Item::FIELD_ITEM)
    {
      field_item= (Item_field*) (cond_func->arguments()[1]);
      value= cond_func->arguments()[0];
    }
    else
      DBUG_RETURN(0);
unknown's avatar
unknown committed
3445
  }
unknown's avatar
unknown committed
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474

  /* 
     If the where condition contains a predicate (ti.field op const),
     then not only SELL_TREE for this predicate is built, but
     the trees for the results of substitution of ti.field for
     each tj.field belonging to the same multiple equality as ti.field
     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.
  */
     
  for (uint i= 0; i < cond_func->arg_count; i++)
  {
    Item *arg= cond_func->arguments()[i];
    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);
  Item_equal *item_equal= field_item->item_equal;
  if (item_equal)
  {
    Item_equal_iterator it(*item_equal);
    Item_field *item;
    while ((item= it++))
unknown's avatar
unknown committed
3475
    {
unknown's avatar
unknown committed
3476 3477 3478 3479
      Field *f= item->field;
      if (field->eq(f))
        continue;
      if (!((ref_tables | f->table->map) & param_comp))
unknown's avatar
unknown committed
3480
      {
unknown's avatar
unknown committed
3481 3482
        tree= get_func_mm_tree(param, cond_func, f, value, cmp_type);
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
unknown's avatar
unknown committed
3483 3484 3485
      }
    }
  }
unknown's avatar
unknown committed
3486
  DBUG_RETURN(ftree);
unknown's avatar
unknown committed
3487 3488 3489 3490
}


static SEL_TREE *
3491
get_mm_parts(PARAM *param, COND *cond_func, Field *field,
unknown's avatar
unknown committed
3492
	     Item_func::Functype type,
3493
	     Item *value, Item_result cmp_type)
unknown's avatar
unknown committed
3494 3495 3496 3497 3498
{
  DBUG_ENTER("get_mm_parts");
  if (field->table != param->table)
    DBUG_RETURN(0);

3499 3500
  KEY_PART *key_part = param->key_parts;
  KEY_PART *end = param->key_parts_end;
unknown's avatar
unknown committed
3501 3502 3503 3504
  SEL_TREE *tree=0;
  if (value &&
      value->used_tables() & ~(param->prev_tables | param->read_tables))
    DBUG_RETURN(0);
3505
  for (; key_part != end ; key_part++)
unknown's avatar
unknown committed
3506 3507 3508 3509
  {
    if (field->eq(key_part->field))
    {
      SEL_ARG *sel_arg=0;
3510
      if (!tree && !(tree=new SEL_TREE()))
3511
	DBUG_RETURN(0);				// OOM
unknown's avatar
unknown committed
3512 3513
      if (!value || !(value->used_tables() & ~param->read_tables))
      {
3514 3515
	sel_arg=get_mm_leaf(param,cond_func,
			    key_part->field,key_part,type,value);
unknown's avatar
unknown committed
3516 3517 3518 3519 3520 3521 3522 3523
	if (!sel_arg)
	  continue;
	if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
	{
	  tree->type=SEL_TREE::IMPOSSIBLE;
	  DBUG_RETURN(tree);
	}
      }
3524 3525
      else
      {
3526
	// This key may be used later
unknown's avatar
unknown committed
3527
	if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
3528
	  DBUG_RETURN(0);			// OOM
3529
      }
unknown's avatar
unknown committed
3530 3531
      sel_arg->part=(uchar) key_part->part;
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
unknown's avatar
unknown committed
3532
      tree->keys_map.set_bit(key_part->key);
unknown's avatar
unknown committed
3533 3534
    }
  }
3535

unknown's avatar
unknown committed
3536 3537 3538 3539 3540
  DBUG_RETURN(tree);
}


static SEL_ARG *
3541
get_mm_leaf(PARAM *param, COND *conf_func, Field *field, KEY_PART *key_part,
unknown's avatar
unknown committed
3542 3543
	    Item_func::Functype type,Item *value)
{
3544
  uint maybe_null=(uint) field->real_maybe_null(), copies;
unknown's avatar
unknown committed
3545
  uint field_length=field->pack_length()+maybe_null;
unknown's avatar
unknown committed
3546
  bool optimize_range;
unknown's avatar
unknown committed
3547
  SEL_ARG *tree;
3548
  char *str, *str2;
unknown's avatar
unknown committed
3549 3550
  DBUG_ENTER("get_mm_leaf");

3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
  if (!value)					// IS NULL or IS NOT NULL
  {
    if (field->table->outer_join)		// Can't use a key on this
      DBUG_RETURN(0);
    if (!maybe_null)				// Not null field
      DBUG_RETURN(type == Item_func::ISNULL_FUNC ? &null_element : 0);
    if (!(tree=new SEL_ARG(field,is_null_string,is_null_string)))
      DBUG_RETURN(0);		// out of memory
    if (type == Item_func::ISNOTNULL_FUNC)
    {
      tree->min_flag=NEAR_MIN;		    /* IS NOT NULL ->  X > NULL */
      tree->max_flag=NO_MAX_RANGE;
    }
    DBUG_RETURN(tree);
  }

  /*
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577
    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 '

3578 3579 3580 3581
  */
  if (field->result_type() == STRING_RESULT &&
      value->result_type() == STRING_RESULT &&
      key_part->image_type == Field::itRAW &&
3582 3583
      ((Field_str*)field)->charset() != conf_func->compare_collation() &&
      !(conf_func->compare_collation()->state & MY_CS_BINSORT))
3584 3585
    DBUG_RETURN(0);

unknown's avatar
unknown committed
3586 3587 3588
  optimize_range= field->optimize_range(param->real_keynr[key_part->key],
                                        key_part->part);

unknown's avatar
unknown committed
3589 3590 3591 3592
  if (type == Item_func::LIKE_FUNC)
  {
    bool like_error;
    char buff1[MAX_FIELD_WIDTH],*min_str,*max_str;
3593
    String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
unknown's avatar
unknown committed
3594 3595
    uint length,offset,min_length,max_length;

unknown's avatar
unknown committed
3596
    if (!optimize_range)
unknown's avatar
unknown committed
3597
      DBUG_RETURN(0);				// Can't optimize this
unknown's avatar
unknown committed
3598 3599 3600
    if (!(res= value->val_str(&tmp)))
      DBUG_RETURN(&null_element);

3601 3602 3603 3604 3605
    /*
      TODO:
      Check if this was a function. This should have be optimized away
      in the sql_select.cc
    */
unknown's avatar
unknown committed
3606 3607 3608 3609 3610 3611 3612 3613 3614
    if (res != &tmp)
    {
      tmp.copy(*res);				// Get own copy
      res= &tmp;
    }
    if (field->cmp_type() != STRING_RESULT)
      DBUG_RETURN(0);				// Can only optimize strings

    offset=maybe_null;
unknown's avatar
unknown committed
3615 3616 3617
    length=key_part->store_length;

    if (length != key_part->length  + maybe_null)
unknown's avatar
unknown committed
3618
    {
unknown's avatar
unknown committed
3619 3620 3621
      /* key packed with length prefix */
      offset+= HA_KEY_BLOB_LENGTH;
      field_length= length - HA_KEY_BLOB_LENGTH;
unknown's avatar
unknown committed
3622 3623 3624
    }
    else
    {
unknown's avatar
unknown committed
3625 3626 3627 3628 3629 3630 3631 3632
      if (unlikely(length < field_length))
      {
	/*
	  This can only happen in a table created with UNIREG where one key
	  overlaps many fields
	*/
	length= field_length;
      }
unknown's avatar
unknown committed
3633
      else
unknown's avatar
unknown committed
3634
	field_length= length;
unknown's avatar
unknown committed
3635 3636
    }
    length+=offset;
3637
    if (!(min_str= (char*) alloc_root(param->mem_root, length*2)))
unknown's avatar
unknown committed
3638 3639 3640 3641
      DBUG_RETURN(0);
    max_str=min_str+length;
    if (maybe_null)
      max_str[0]= min_str[0]=0;
3642 3643

    like_error= my_like_range(field->charset(),
unknown's avatar
unknown committed
3644
			      res->ptr(), res->length(),
unknown's avatar
unknown committed
3645 3646
			      ((Item_func_like*)(param->cond))->escape,
			      wild_one, wild_many,
unknown's avatar
unknown committed
3647
			      field_length-maybe_null,
unknown's avatar
unknown committed
3648 3649
			      min_str+offset, max_str+offset,
			      &min_length, &max_length);
unknown's avatar
unknown committed
3650 3651
    if (like_error)				// Can't optimize with LIKE
      DBUG_RETURN(0);
unknown's avatar
unknown committed
3652

unknown's avatar
unknown committed
3653 3654 3655 3656 3657 3658 3659 3660
    if (offset != maybe_null)			// Blob
    {
      int2store(min_str+maybe_null,min_length);
      int2store(max_str+maybe_null,max_length);
    }
    DBUG_RETURN(new SEL_ARG(field,min_str,max_str));
  }

unknown's avatar
unknown committed
3661
  if (!optimize_range &&
3662
      type != Item_func::EQ_FUNC &&
unknown's avatar
unknown committed
3663 3664 3665
      type != Item_func::EQUAL_FUNC)
    DBUG_RETURN(0);				// Can't optimize this

3666 3667 3668 3669
  /*
    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
  */
unknown's avatar
unknown committed
3670 3671 3672 3673
  if (field->result_type() == STRING_RESULT &&
      value->result_type() != STRING_RESULT &&
      field->cmp_type() != value->result_type())
    DBUG_RETURN(0);
unknown's avatar
unknown committed
3674

3675
  if (value->save_in_field_no_warnings(field, 1) < 0)
unknown's avatar
unknown committed
3676
  {
3677
    /* This happens when we try to insert a NULL field in a not null column */
unknown's avatar
unknown committed
3678
    DBUG_RETURN(&null_element);			// cmp with NULL is never TRUE
unknown's avatar
unknown committed
3679
  }
3680 3681 3682 3683 3684
  /* Get local copy of key */
  copies= 1;
  if (field->key_type() == HA_KEYTYPE_VARTEXT)
    copies= 2;
  str= str2= (char*) alloc_root(param->mem_root,
unknown's avatar
unknown committed
3685
				(key_part->store_length)*copies+1);
unknown's avatar
unknown committed
3686 3687 3688
  if (!str)
    DBUG_RETURN(0);
  if (maybe_null)
3689
    *str= (char) field->is_real_null();		// Set to 1 if null
unknown's avatar
unknown committed
3690 3691
  field->get_key_image(str+maybe_null, key_part->length,
		       field->charset(), key_part->image_type);
3692 3693 3694 3695 3696 3697 3698 3699
  if (copies == 2)
  {
    /*
      The key is stored as 2 byte length + key
      key doesn't match end space. In other words, a key 'X ' should match
      all rows between 'X' and 'X           ...'
    */
    uint length= uint2korr(str+maybe_null);
unknown's avatar
unknown committed
3700
    str2= str+ key_part->store_length;
3701 3702
    /* remove end space */
    while (length > 0 && str[length+HA_KEY_BLOB_LENGTH+maybe_null-1] == ' ')
3703 3704 3705
      length--;
    int2store(str+maybe_null, length);
    /* Create key that is space filled */
3706
    memcpy(str2, str, length + HA_KEY_BLOB_LENGTH + maybe_null);
unknown's avatar
unknown committed
3707 3708 3709 3710
    my_fill_8bit(field->charset(),
		 str2+ length+ HA_KEY_BLOB_LENGTH +maybe_null,
		 key_part->length-length, ' ');
    int2store(str2+maybe_null, key_part->length);
3711 3712
  }
  if (!(tree=new SEL_ARG(field,str,str2)))
3713
    DBUG_RETURN(0);		// out of memory
unknown's avatar
unknown committed
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735

  switch (type) {
  case Item_func::LT_FUNC:
    if (field_is_equal_to_item(field,value))
      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:
    if (field_is_equal_to_item(field,value))
      tree->min_flag=NEAR_MIN;
    /* fall through */
  case Item_func::GE_FUNC:
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3736
  case Item_func::SP_EQUALS_FUNC:
unknown's avatar
unknown committed
3737 3738 3739
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_EQUAL;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3740
  case Item_func::SP_DISJOINT_FUNC:
unknown's avatar
unknown committed
3741 3742 3743
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_DISJOINT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3744
  case Item_func::SP_INTERSECTS_FUNC:
unknown's avatar
unknown committed
3745 3746 3747
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3748
  case Item_func::SP_TOUCHES_FUNC:
unknown's avatar
unknown committed
3749 3750 3751
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3752 3753

  case Item_func::SP_CROSSES_FUNC:
unknown's avatar
unknown committed
3754 3755 3756
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3757
  case Item_func::SP_WITHIN_FUNC:
unknown's avatar
unknown committed
3758 3759 3760
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_WITHIN;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3761 3762

  case Item_func::SP_CONTAINS_FUNC:
unknown's avatar
unknown committed
3763 3764 3765
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_CONTAIN;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3766
  case Item_func::SP_OVERLAPS_FUNC:
unknown's avatar
unknown committed
3767 3768 3769
    tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
    tree->max_flag=NO_MAX_RANGE;
    break;
unknown's avatar
unknown committed
3770

unknown's avatar
unknown committed
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
  default:
    break;
  }
  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:
unknown's avatar
unknown committed
3783 3784
** IMPOSSIBLE:	 Condition is never TRUE
** ALWAYS:	 Condition is always TRUE
unknown's avatar
unknown committed
3785 3786 3787 3788 3789 3790
** 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
******************************************************************************/

/*
3791 3792
  Add a new key test to a key when scanning through all keys
  This will never be called for same key parts.
unknown's avatar
unknown committed
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
*/

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 *
tree_and(PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
  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);
  }

unknown's avatar
unknown committed
3854 3855
  key_map  result_keys;
  result_keys.clear_all();
unknown's avatar
unknown committed
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868
  /* 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;
      *key1=key_and(*key1,*key2,flag);
3869
      if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
unknown's avatar
unknown committed
3870 3871
      {
	tree1->type= SEL_TREE::IMPOSSIBLE;
unknown's avatar
unknown committed
3872
        DBUG_RETURN(tree1);
unknown's avatar
unknown committed
3873
      }
unknown's avatar
unknown committed
3874
      result_keys.set_bit(key1 - tree1->keys);
unknown's avatar
unknown committed
3875
#ifdef EXTRA_DEBUG
3876 3877
      if (*key1)
        (*key1)->test_use_count(*key1);
unknown's avatar
unknown committed
3878 3879 3880
#endif
    }
  }
unknown's avatar
unknown committed
3881 3882
  tree1->keys_map= result_keys;
  /* dispose index_merge if there is a "range" option */
unknown's avatar
unknown committed
3883
  if (!result_keys.is_clear_all())
unknown's avatar
unknown committed
3884 3885 3886 3887 3888 3889 3890
  {
    tree1->merges.empty();
    DBUG_RETURN(tree1);
  }

  /* ok, both trees are index_merge trees */
  imerge_list_and_list(&tree1->merges, &tree2->merges);
unknown's avatar
unknown committed
3891 3892 3893 3894
  DBUG_RETURN(tree1);
}


unknown's avatar
unknown committed
3895
/*
unknown's avatar
unknown committed
3896 3897
  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
3898
  using index_merge.
unknown's avatar
unknown committed
3899 3900 3901 3902
*/

bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, PARAM* param)
{
unknown's avatar
unknown committed
3903
  key_map common_keys= tree1->keys_map;
unknown's avatar
unknown committed
3904
  DBUG_ENTER("sel_trees_can_be_ored");
3905
  common_keys.intersect(tree2->keys_map);
unknown's avatar
unknown committed
3906

unknown's avatar
unknown committed
3907
  if (common_keys.is_clear_all())
unknown's avatar
unknown committed
3908
    DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
3909 3910

  /* trees have a common key, check if they refer to same key part */
unknown's avatar
unknown committed
3911
  SEL_ARG **key1,**key2;
unknown's avatar
unknown committed
3912
  for (uint key_no=0; key_no < param->keys; key_no++)
unknown's avatar
unknown committed
3913
  {
unknown's avatar
unknown committed
3914
    if (common_keys.is_set(key_no))
unknown's avatar
unknown committed
3915 3916 3917 3918 3919
    {
      key1= tree1->keys + key_no;
      key2= tree2->keys + key_no;
      if ((*key1)->part == (*key2)->part)
      {
unknown's avatar
unknown committed
3920
        DBUG_RETURN(TRUE);
unknown's avatar
unknown committed
3921 3922 3923
      }
    }
  }
unknown's avatar
unknown committed
3924
  DBUG_RETURN(FALSE);
unknown's avatar
unknown committed
3925
}
unknown's avatar
unknown committed
3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941

static SEL_TREE *
tree_or(PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
  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);

unknown's avatar
unknown committed
3942
  SEL_TREE *result= 0;
unknown's avatar
unknown committed
3943 3944
  key_map  result_keys;
  result_keys.clear_all();
unknown's avatar
unknown committed
3945
  if (sel_trees_can_be_ored(tree1, tree2, param))
unknown's avatar
unknown committed
3946
  {
unknown's avatar
unknown committed
3947 3948 3949 3950
    /* 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++)
unknown's avatar
unknown committed
3951
    {
unknown's avatar
unknown committed
3952 3953 3954 3955
      *key1=key_or(*key1,*key2);
      if (*key1)
      {
        result=tree1;				// Added to tree1
unknown's avatar
unknown committed
3956
        result_keys.set_bit(key1 - tree1->keys);
unknown's avatar
unknown committed
3957
#ifdef EXTRA_DEBUG
unknown's avatar
unknown committed
3958
        (*key1)->test_use_count(*key1);
unknown's avatar
unknown committed
3959
#endif
unknown's avatar
unknown committed
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
      }
    }
    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())
    {
      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())
unknown's avatar
unknown committed
3991
        swap_variables(SEL_TREE*, tree1, tree2);
unknown's avatar
unknown committed
3992 3993 3994 3995 3996 3997

      /* 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;
unknown's avatar
unknown committed
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018
    }
  }
  DBUG_RETURN(result);
}


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

static SEL_ARG *
and_all_keys(SEL_ARG *key1,SEL_ARG *key2,uint clone_flag)
{
  SEL_ARG *next;
  ulong use_count=key1->use_count;

  if (key1->elements != 1)
  {
    key2->use_count+=key1->elements-1;
    key2->increment_use_count((int) key1->elements-1);
  }
  if (key1->type == SEL_ARG::MAYBE_KEY)
  {
4019 4020
    key1->right= key1->left= &null_element;
    key1->next= key1->prev= 0;
unknown's avatar
unknown committed
4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
  }
  for (next=key1->first(); next ; next=next->next)
  {
    if (next->next_key_part)
    {
      SEL_ARG *tmp=key_and(next->next_key_part,key2,clone_flag);
      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);
    }
    else
      next->next_key_part=key2;
  }
  if (!key1)
    return &null_element;			// Impossible ranges
  key1->use_count++;
  return key1;
}


static SEL_ARG *
key_and(SEL_ARG *key1,SEL_ARG *key2,uint clone_flag)
{
  if (!key1)
    return key2;
  if (!key2)
    return key1;
  if (key1->part != key2->part)
  {
    if (key1->part > key2->part)
    {
4057
      swap_variables(SEL_ARG *, key1, key2);
unknown's avatar
unknown committed
4058 4059 4060 4061 4062
      clone_flag=swap_clone_flag(clone_flag);
    }
    // key1->part < key2->part
    key1->use_count--;
    if (key1->use_count > 0)
4063 4064
      if (!(key1= key1->clone_tree()))
	return 0;				// OOM
unknown's avatar
unknown committed
4065 4066 4067 4068
    return and_all_keys(key1,key2,clone_flag);
  }

  if (((clone_flag & CLONE_KEY2_MAYBE) &&
4069 4070
       !(clone_flag & CLONE_KEY1_MAYBE) &&
       key2->type != SEL_ARG::MAYBE_KEY) ||
unknown's avatar
unknown committed
4071 4072
      key1->type == SEL_ARG::MAYBE_KEY)
  {						// Put simple key in key2
4073
    swap_variables(SEL_ARG *, key1, key2);
unknown's avatar
unknown committed
4074 4075 4076 4077 4078 4079 4080 4081 4082
    clone_flag=swap_clone_flag(clone_flag);
  }

  // If one of the key is MAYBE_KEY then the found region may be smaller
  if (key2->type == SEL_ARG::MAYBE_KEY)
  {
    if (key1->use_count > 1)
    {
      key1->use_count--;
4083 4084
      if (!(key1=key1->clone_tree()))
	return 0;				// OOM
unknown's avatar
unknown committed
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098
      key1->use_count++;
    }
    if (key1->type == SEL_ARG::MAYBE_KEY)
    {						// Both are maybe key
      key1->next_key_part=key_and(key1->next_key_part,key2->next_key_part,
				 clone_flag);
      if (key1->next_key_part &&
	  key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
	return key1;
    }
    else
    {
      key1->maybe_smaller();
      if (key2->next_key_part)
4099 4100
      {
	key1->use_count--;			// Incremented in and_all_keys
unknown's avatar
unknown committed
4101
	return and_all_keys(key1,key2,clone_flag);
4102
      }
unknown's avatar
unknown committed
4103 4104 4105 4106 4107
      key2->use_count--;			// Key2 doesn't have a tree
    }
    return key1;
  }

4108 4109 4110 4111 4112 4113 4114
  if ((key1->min_flag | key2->min_flag) & GEOM_FLAG)
  {
    key1->free_tree();
    key2->free_tree();
    return 0;					// Can't optimize this
  }

4115 4116 4117
  if ((key1->min_flag | key2->min_flag) & GEOM_FLAG)
  {
    key1->free_tree();
4118 4119 4120 4121
    key2->free_tree();
    return 0;					// Can't optimize this
  }

unknown's avatar
unknown committed
4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141
  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;
    SEL_ARG *next=key_and(e1->next_key_part,e2->next_key_part,clone_flag);
    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);
4142 4143
      if (!new_arg)
	return &null_element;			// End of memory
unknown's avatar
unknown committed
4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194
      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;
}


static SEL_ARG *
key_or(SEL_ARG *key1,SEL_ARG *key2)
{
  if (!key1)
  {
    if (key2)
    {
      key2->use_count--;
      key2->free_tree();
    }
    return 0;
  }
4195
  if (!key2)
unknown's avatar
unknown committed
4196 4197 4198 4199 4200 4201 4202 4203
  {
    key1->use_count--;
    key1->free_tree();
    return 0;
  }
  key1->use_count--;
  key2->use_count--;

4204 4205
  if (key1->part != key2->part || 
      (key1->min_flag | key2->min_flag) & GEOM_FLAG)
unknown's avatar
unknown committed
4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
  {
    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)
    {
4230
      swap_variables(SEL_ARG *,key1,key2);
unknown's avatar
unknown committed
4231
    }
4232
    if (key1->use_count > 0 || !(key1=key1->clone_tree()))
4233
      return 0;					// OOM
unknown's avatar
unknown committed
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258
  }

  // 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;
      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)
	{
unknown's avatar
unknown committed
4259
	  if (!(key2=new SEL_ARG(*key2)))
4260
	    return 0;		// out of memory
unknown's avatar
unknown committed
4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300
	  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
      {
	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)
	  {
4301 4302
	    SEL_ARG *cpy= new SEL_ARG(*key2);	// Must make copy
	    if (!cpy)
4303
	      return 0;				// OOM
4304
	    key1=key1->insert(cpy);
unknown's avatar
unknown committed
4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
	    key2->increment_use_count(key1->use_count+1);
	  }
	  else
	    key1=key1->insert(key2);		// Will destroy key2_root
	  key2=next;
	  continue;
	}
      }
    }

    // tmp.max >= key2.min && tmp.min <= key.max  (overlapping ranges)
    if (eq_tree(tmp->next_key_part,key2->next_key_part))
    {
      if (tmp->is_same(key2))
      {
	tmp->merge_flags(key2);			// Copy maybe flags
	key2->increment_use_count(-1);		// Free not used tree
      }
      else
      {
	SEL_ARG *last=tmp;
	while (last->next && last->next->cmp_min_to_max(key2) <= 0 &&
	       eq_tree(last->next->next_key_part,key2->next_key_part))
	{
	  SEL_ARG *save=last;
	  last=last->next;
	  key1=key1->tree_delete(save);
	}
	if (last->copy_min(key2) || last->copy_max(key2))
	{					// 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;
	}
      }
      key2=key2->next;
      continue;
    }

    if (cmp >= 0 && tmp->cmp_min_to_min(key2) < 0)
    {						// tmp.min <= x < key2.min
      SEL_ARG *new_arg=tmp->clone_first(key2);
4350 4351
      if (!new_arg)
	return 0;				// OOM
unknown's avatar
unknown committed
4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364
      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);
4365 4366
	if (!new_arg)
	  return 0;				// OOM
unknown's avatar
unknown committed
4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380
	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);
	tmp->next_key_part=key_or(tmp->next_key_part,key.next_key_part);
	if (!cmp)				// Key2 is ready
	  break;
	key.copy_max_to_min(tmp);
	if (!(tmp=tmp->next))
	{
4381 4382 4383 4384
	  SEL_ARG *tmp2= new SEL_ARG(key);
	  if (!tmp2)
	    return 0;				// OOM
	  key1=key1->insert(tmp2);
unknown's avatar
unknown committed
4385 4386 4387 4388 4389
	  key2=key2->next;
	  goto end;
	}
	if (tmp->cmp_min_to_max(&key) > 0)
	{
4390 4391 4392 4393
	  SEL_ARG *tmp2= new SEL_ARG(key);
	  if (!tmp2)
	    return 0;				// OOM
	  key1=key1->insert(tmp2);
unknown's avatar
unknown committed
4394 4395 4396 4397 4398 4399
	  break;
	}
      }
      else
      {
	SEL_ARG *new_arg=tmp->clone_last(&key); // tmp.min <= x <= key.max
4400 4401
	if (!new_arg)
	  return 0;				// OOM
unknown's avatar
unknown committed
4402 4403
	tmp->copy_max_to_min(&key);
	tmp->increment_use_count(key1->use_count+1);
4404 4405
	/* Increment key count as it may be used for next loop */
	key.increment_use_count(1);
unknown's avatar
unknown committed
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419
	new_arg->next_key_part=key_or(tmp->next_key_part,key.next_key_part);
	key1=key1->insert(new_arg);
	break;
      }
    }
    key2=key2->next;
  }

end:
  while (key2)
  {
    SEL_ARG *next=key2->next;
    if (key2_shared)
    {
4420 4421 4422
      SEL_ARG *tmp=new SEL_ARG(*key2);		// Must make copy
      if (!tmp)
	return 0;
unknown's avatar
unknown committed
4423
      key2->increment_use_count(key1->use_count+1);
4424
      key1=key1->insert(tmp);
unknown's avatar
unknown committed
4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471
    }
    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)
{
  SEL_ARG *element,**par,*last_element;
  LINT_INIT(par); LINT_INIT(last_element);
unknown's avatar
unknown committed
4472

unknown's avatar
unknown committed
4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539
  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;
  }
}


/*
4540 4541 4542 4543 4544
  Remove a element from the tree

  SYNOPSIS
    tree_delete()
    key		Key that is to be deleted from tree (this)
unknown's avatar
unknown committed
4545

4546 4547 4548 4549 4550
  NOTE
    This also frees all sub trees that is used by the element

  RETURN
    root of new tree (with key deleted)
unknown's avatar
unknown committed
4551 4552 4553 4554 4555 4556 4557
*/

SEL_ARG *
SEL_ARG::tree_delete(SEL_ARG *key)
{
  enum leaf_color remove_color;
  SEL_ARG *root,*nod,**par,*fix_par;
4558 4559 4560 4561
  DBUG_ENTER("tree_delete");

  root=this;
  this->parent= 0;
unknown's avatar
unknown committed
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607

  /* 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)
4608
    DBUG_RETURN(0);				// Maybe root later
unknown's avatar
unknown committed
4609 4610 4611 4612 4613 4614 4615
  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;
4616
  DBUG_RETURN(root);
unknown's avatar
unknown committed
4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
}


	/* 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;
}


4792
	/* Test that the properties for a red-black tree hold */
unknown's avatar
unknown committed
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848

#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
}

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;
}


void SEL_ARG::test_use_count(SEL_ARG *root)
{
4849
  uint e_count=0;
unknown's avatar
unknown committed
4850 4851
  if (this == root && use_count != 1)
  {
unknown's avatar
unknown committed
4852
    sql_print_information("Use_count: Wrong count %lu for root",use_count);
unknown's avatar
unknown committed
4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864
    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)
      {
unknown's avatar
unknown committed
4865
	sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu should be %lu",
unknown's avatar
unknown committed
4866 4867 4868 4869 4870 4871 4872
			pos,pos->next_key_part->use_count,count);
	return;
      }
      pos->next_key_part->test_use_count(root);
    }
  }
  if (e_count != elements)
unknown's avatar
unknown committed
4873
    sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
4874
		    e_count, elements, (gptr) this);
unknown's avatar
unknown committed
4875 4876 4877 4878 4879
}

#endif


4880 4881 4882 4883 4884 4885 4886 4887 4888 4889
/*
  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
      idx    Number of index to use in PARAM::key SEL_TREE::key
      tree   Transformed selection condition, tree->key[idx] holds intervals
             tree to be used for scanning.
  NOTES
unknown's avatar
unknown committed
4890
    param->is_ror_scan is set to reflect if the key scan is a ROR (see
4891
    is_key_scan_ror function for more info)
unknown's avatar
unknown committed
4892
    param->table->quick_*, param->range_count (and maybe others) are
4893
    updated with data of given key scan, see check_quick_keys for details.
unknown's avatar
unknown committed
4894 4895

  RETURN
4896
    Estimate # of records to be retrieved.
unknown's avatar
unknown committed
4897
    HA_POS_ERROR if estimate calculation failed due to table handler problems.
unknown's avatar
unknown committed
4898

4899
*/
unknown's avatar
unknown committed
4900 4901 4902 4903 4904

static ha_rows
check_quick_select(PARAM *param,uint idx,SEL_ARG *tree)
{
  ha_rows records;
4905 4906
  bool    cpk_scan;
  uint key;
unknown's avatar
unknown committed
4907
  DBUG_ENTER("check_quick_select");
unknown's avatar
unknown committed
4908

unknown's avatar
unknown committed
4909
  param->is_ror_scan= FALSE;
unknown's avatar
unknown committed
4910

unknown's avatar
unknown committed
4911 4912
  if (!tree)
    DBUG_RETURN(HA_POS_ERROR);			// Can't use it
unknown's avatar
unknown committed
4913 4914
  param->max_key_part=0;
  param->range_count=0;
4915 4916
  key= param->real_keynr[idx];

unknown's avatar
unknown committed
4917 4918 4919 4920
  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
4921 4922 4923 4924 4925

  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. */
unknown's avatar
unknown committed
4926
    cpk_scan= FALSE;
4927 4928 4929 4930 4931 4932 4933 4934 4935
  }
  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).
    */
    cpk_scan= (param->table->primary_key == param->real_keynr[idx]) &&
              param->table->file->primary_key_is_clustered();
unknown's avatar
unknown committed
4936
    param->is_ror_scan= !cpk_scan;
4937 4938
  }

unknown's avatar
unknown committed
4939 4940
  records=check_quick_keys(param,idx,tree,param->min_key,0,param->max_key,0);
  if (records != HA_POS_ERROR)
unknown's avatar
unknown committed
4941
  {
4942
    param->table->quick_keys.set_bit(key);
unknown's avatar
unknown committed
4943 4944
    param->table->quick_rows[key]=records;
    param->table->quick_key_parts[key]=param->max_key_part+1;
unknown's avatar
unknown committed
4945

4946
    if (cpk_scan)
unknown's avatar
unknown committed
4947
      param->is_ror_scan= TRUE;
unknown's avatar
unknown committed
4948
  }
4949
  DBUG_PRINT("exit", ("Records: %lu", (ulong) records));
unknown's avatar
unknown committed
4950 4951 4952 4953
  DBUG_RETURN(records);
}


4954
/*
unknown's avatar
unknown committed
4955 4956
  Recursively calculate estimate of # rows that will be retrieved by
  key scan on key idx.
4957 4958
  SYNOPSIS
    check_quick_keys()
4959
      param         Parameter from test_quick select function.
unknown's avatar
unknown committed
4960
      idx           Number of key to use in PARAM::keys in list of used keys
4961 4962 4963
                    (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
unknown's avatar
unknown committed
4964
      min_key_flag
4965
      max_key       Buffer with partial max key value tuple
4966 4967
      max_key_flag

4968
  NOTES
unknown's avatar
unknown committed
4969 4970
    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
4971 4972
    are calculated using records_in_range calls at the leaf nodes and then
    summed.
4973

4974 4975
    param->min_key and param->max_key are used to hold prefixes of key value
    tuples.
4976 4977

    The side effects are:
unknown's avatar
unknown committed
4978

4979 4980
    param->max_key_part is updated to hold the maximum number of key parts used
      in scan minus 1.
unknown's avatar
unknown committed
4981 4982

    param->range_count is incremented if the function finds a range that
4983
      wasn't counted by the caller.
unknown's avatar
unknown committed
4984

4985 4986 4987
    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)
4988 4989
*/

unknown's avatar
unknown committed
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999
static ha_rows
check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree,
		 char *min_key,uint min_key_flag, char *max_key,
		 uint max_key_flag)
{
  ha_rows records=0,tmp;

  param->max_key_part=max(param->max_key_part,key_tree->part);
  if (key_tree->left != &null_element)
  {
5000 5001 5002 5003 5004 5005
    /*
      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.
    */
unknown's avatar
unknown committed
5006
    param->is_ror_scan= FALSE;
unknown's avatar
unknown committed
5007 5008 5009 5010 5011 5012 5013 5014
    records=check_quick_keys(param,idx,key_tree->left,min_key,min_key_flag,
			     max_key,max_key_flag);
    if (records == HA_POS_ERROR)			// Impossible
      return records;
  }

  uint tmp_min_flag,tmp_max_flag,keynr;
  char *tmp_min_key=min_key,*tmp_max_key=max_key;
unknown's avatar
unknown committed
5015

unknown's avatar
unknown committed
5016
  key_tree->store(param->key[idx][key_tree->part].store_length,
unknown's avatar
unknown committed
5017 5018 5019 5020
		  &tmp_min_key,min_key_flag,&tmp_max_key,max_key_flag);
  uint min_key_length= (uint) (tmp_min_key- param->min_key);
  uint max_key_length= (uint) (tmp_max_key- param->max_key);

5021 5022
  if (param->is_ror_scan)
  {
unknown's avatar
unknown committed
5023
    /*
5024
      If the index doesn't cover entire key, mark the scan as non-ROR scan.
5025
      Actually we're cutting off some ROR scans here.
5026 5027 5028
    */
    uint16 fieldnr= param->table->key_info[param->real_keynr[idx]].
                    key_part[key_tree->part].fieldnr - 1;
unknown's avatar
unknown committed
5029
    if (param->table->field[fieldnr]->key_length() !=
5030
        param->key[idx][key_tree->part].length)
unknown's avatar
unknown committed
5031
      param->is_ror_scan= FALSE;
5032 5033
  }

unknown's avatar
unknown committed
5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
  if (key_tree->next_key_part &&
      key_tree->next_key_part->part == key_tree->part+1 &&
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
  {						// const key as prefix
    if (min_key_length == max_key_length &&
	!memcmp(min_key,max_key, (uint) (tmp_max_key - max_key)) &&
	!key_tree->min_flag && !key_tree->max_flag)
    {
      tmp=check_quick_keys(param,idx,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);
      goto end;					// Ugly, but efficient
    }
5047
    else
5048 5049
    {
      /* The interval for current key part is not c1 <= keyXpartY <= c1 */
unknown's avatar
unknown committed
5050
      param->is_ror_scan= FALSE;
5051
    }
5052

unknown's avatar
unknown committed
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070
    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(param->key[idx], &tmp_min_key,
					     &tmp_min_flag);
    if (!tmp_max_flag)
      key_tree->next_key_part->store_max_key(param->key[idx], &tmp_max_key,
					     &tmp_max_flag);
    min_key_length= (uint) (tmp_min_key- param->min_key);
    max_key_length= (uint) (tmp_max_key- param->max_key);
  }
  else
  {
    tmp_min_flag=min_key_flag | key_tree->min_flag;
    tmp_max_flag=max_key_flag | key_tree->max_flag;
  }

  keynr=param->real_keynr[idx];
unknown's avatar
unknown committed
5071
  param->range_count++;
unknown's avatar
unknown committed
5072 5073
  if (!tmp_min_flag && ! tmp_max_flag &&
      (uint) key_tree->part+1 == param->table->key_info[keynr].key_parts &&
5074 5075
      (param->table->key_info[keynr].flags & (HA_NOSAME | HA_END_SPACE_KEY)) ==
      HA_NOSAME &&
unknown's avatar
unknown committed
5076 5077 5078 5079
      min_key_length == max_key_length &&
      !memcmp(param->min_key,param->max_key,min_key_length))
    tmp=1;					// Max one record
  else
unknown's avatar
unknown committed
5080
  {
5081 5082
    if (param->is_ror_scan)
    {
5083 5084 5085 5086 5087 5088 5089 5090 5091
      /*
        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.
      */
5092 5093
      if (!(min_key_length == max_key_length &&
            !memcmp(min_key,max_key, (uint) (tmp_max_key - max_key)) &&
unknown's avatar
unknown committed
5094
            !key_tree->min_flag && !key_tree->max_flag &&
5095
            is_key_scan_ror(param, keynr, key_tree->part + 1)))
unknown's avatar
unknown committed
5096
        param->is_ror_scan= FALSE;
5097 5098
    }

unknown's avatar
unknown committed
5099
    if (tmp_min_flag & GEOM_FLAG)
unknown's avatar
unknown committed
5100
    {
unknown's avatar
unknown committed
5101 5102 5103 5104 5105 5106 5107 5108
      key_range min_range;
      min_range.key=    (byte*) param->min_key;
      min_range.length= min_key_length;
      /* In this case tmp_min_flag contains the handler-read-function */
      min_range.flag=   (ha_rkey_function) (tmp_min_flag ^ GEOM_FLAG);

      tmp= param->table->file->records_in_range(keynr, &min_range,
                                                (key_range*) 0);
unknown's avatar
unknown committed
5109 5110 5111
    }
    else
    {
unknown's avatar
unknown committed
5112 5113 5114 5115 5116 5117
      key_range min_range, max_range;

      min_range.key=    (byte*) param->min_key;
      min_range.length= min_key_length;
      min_range.flag=   (tmp_min_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
                         HA_READ_KEY_EXACT);
unknown's avatar
unknown committed
5118
      max_range.key=    (byte*) param->max_key;
unknown's avatar
unknown committed
5119 5120 5121 5122 5123 5124 5125 5126
      max_range.length= max_key_length;
      max_range.flag=   (tmp_max_flag & NEAR_MAX ?
                         HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY);
      tmp=param->table->file->records_in_range(keynr,
                                               (min_key_length ? &min_range :
                                                (key_range*) 0),
                                               (max_key_length ? &max_range :
                                                (key_range*) 0));
unknown's avatar
unknown committed
5127 5128
    }
  }
unknown's avatar
unknown committed
5129 5130 5131 5132 5133 5134
 end:
  if (tmp == HA_POS_ERROR)			// Impossible range
    return tmp;
  records+=tmp;
  if (key_tree->right != &null_element)
  {
5135 5136 5137 5138 5139 5140
    /*
      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.
    */
unknown's avatar
unknown committed
5141
    param->is_ror_scan= FALSE;
unknown's avatar
unknown committed
5142 5143 5144 5145 5146 5147 5148 5149 5150
    tmp=check_quick_keys(param,idx,key_tree->right,min_key,min_key_flag,
			 max_key,max_key_flag);
    if (tmp == HA_POS_ERROR)
      return tmp;
    records+=tmp;
  }
  return records;
}

5151

5152
/*
unknown's avatar
unknown committed
5153
  Check if key scan on given index with equality conditions on first n key
5154 5155 5156 5157
  parts is a ROR scan.

  SYNOPSIS
    is_key_scan_ror()
unknown's avatar
unknown committed
5158
      param  Parameter from test_quick_select
5159 5160 5161 5162
      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.
unknown's avatar
unknown committed
5163

5164 5165 5166
  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)
unknown's avatar
unknown committed
5167

5168 5169 5170
    An index scan is a ROR scan if it is done using a condition in form

        "key1_1=c_1 AND ... AND key1_n=c_n"  (1)
unknown's avatar
unknown committed
5171

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

unknown's avatar
unknown committed
5174
    and the table has a clustered Primary Key
5175

unknown's avatar
unknown committed
5176
    PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k) with first key parts being
5177
    identical to uncovered parts ot the key being scanned (2)
unknown's avatar
unknown committed
5178 5179

    Scans on HASH indexes are not ROR scans,
5180 5181 5182 5183 5184 5185
    any range scan on clustered primary key is ROR scan  (3)

    Check (1) is made in check_quick_keys()
    Check (3) is made check_quick_select()
    Check (2) is made by this function.

unknown's avatar
unknown committed
5186
  RETURN
unknown's avatar
unknown committed
5187 5188
    TRUE  If the scan is ROR-scan
    FALSE otherwise
5189
*/
5190

5191 5192 5193 5194
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;
unknown's avatar
unknown committed
5195
  KEY_PART_INFO *key_part_end= table_key->key_part +
5196
                               table_key->key_parts;
unknown's avatar
unknown committed
5197

5198
  if (key_part == key_part_end)
unknown's avatar
unknown committed
5199
    return TRUE;
5200 5201
  uint pk_number= param->table->primary_key;
  if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
unknown's avatar
unknown committed
5202
    return FALSE;
5203 5204

  KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
unknown's avatar
unknown committed
5205
  KEY_PART_INFO *pk_part_end= pk_part +
5206
                              param->table->key_info[pk_number].key_parts;
unknown's avatar
unknown committed
5207
  for(;(key_part!=key_part_end) && (pk_part != pk_part_end);
5208 5209
      ++key_part, ++pk_part)
  {
unknown's avatar
unknown committed
5210
    if ((key_part->field != pk_part->field) ||
5211
        (key_part->length != pk_part->length))
unknown's avatar
unknown committed
5212
      return FALSE;
unknown's avatar
unknown committed
5213
  }
5214
  return (key_part == key_part_end);
unknown's avatar
unknown committed
5215 5216 5217
}


5218 5219
/*
  Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.
unknown's avatar
unknown committed
5220

5221 5222
  SYNOPSIS
    get_quick_select()
unknown's avatar
unknown committed
5223
      param
5224
      idx          Index of used key in param->key.
unknown's avatar
unknown committed
5225 5226
      key_tree     SEL_ARG tree for the used key
      parent_alloc If not NULL, use it to allocate memory for
5227
                   quick select data. Otherwise use quick->alloc.
5228
  NOTES
5229
    The caller must call QUICK_SELECT::init for returned quick select
5230

5231
    CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
5232
    deallocated when the returned quick select is deleted.
5233 5234 5235 5236

  RETURN
    NULL on error
    otherwise created quick select
5237
*/
5238

unknown's avatar
unknown committed
5239 5240 5241
QUICK_RANGE_SELECT *
get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree,
                 MEM_ROOT *parent_alloc)
unknown's avatar
unknown committed
5242
{
unknown's avatar
unknown committed
5243
  QUICK_RANGE_SELECT *quick;
unknown's avatar
unknown committed
5244
  DBUG_ENTER("get_quick_select");
unknown's avatar
unknown committed
5245 5246 5247 5248 5249 5250 5251 5252 5253

  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],
unknown's avatar
unknown committed
5254
                                 test(parent_alloc));
unknown's avatar
unknown committed
5255

unknown's avatar
unknown committed
5256
  if (quick)
unknown's avatar
unknown committed
5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267
  {
    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*)
unknown's avatar
unknown committed
5268 5269 5270 5271
        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);
unknown's avatar
unknown committed
5272
    }
unknown's avatar
unknown committed
5273
  }
unknown's avatar
unknown committed
5274 5275 5276 5277 5278 5279 5280
  DBUG_RETURN(quick);
}


/*
** Fix this to get all possible sub_ranges
*/
unknown's avatar
unknown committed
5281 5282
bool
get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
unknown's avatar
unknown committed
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295
	       SEL_ARG *key_tree,char *min_key,uint min_key_flag,
	       char *max_key, uint max_key_flag)
{
  QUICK_RANGE *range;
  uint flag;

  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;
  }
  char *tmp_min_key=min_key,*tmp_max_key=max_key;
unknown's avatar
unknown committed
5296
  key_tree->store(key[key_tree->part].store_length,
unknown's avatar
unknown committed
5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324
		  &tmp_min_key,min_key_flag,&tmp_max_key,max_key_flag);

  if (key_tree->next_key_part &&
      key_tree->next_key_part->part == key_tree->part+1 &&
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
  {						  // const key as prefix
    if (!((tmp_min_key - min_key) != (tmp_max_key - max_key) ||
	  memcmp(min_key,max_key, (uint) (tmp_max_key - max_key)) ||
	  key_tree->min_flag || key_tree->max_flag))
    {
      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)
	key_tree->next_key_part->store_min_key(key, &tmp_min_key,
					       &tmp_min_flag);
      if (!tmp_max_flag)
	key_tree->next_key_part->store_max_key(key, &tmp_max_key,
					       &tmp_max_flag);
      flag=tmp_min_flag | tmp_max_flag;
    }
  }
  else
unknown's avatar
unknown committed
5325 5326 5327 5328
  {
    flag = (key_tree->min_flag & GEOM_FLAG) ?
      key_tree->min_flag : key_tree->min_flag | key_tree->max_flag;
  }
unknown's avatar
unknown committed
5329

5330 5331 5332 5333 5334
  /*
    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)
unknown's avatar
unknown committed
5335 5336 5337 5338 5339 5340 5341 5342 5343 5344
  {
    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;
  }
unknown's avatar
unknown committed
5345 5346 5347 5348 5349 5350 5351 5352
  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;
5353 5354
      if ((table_key->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME &&
	  key->part == table_key->key_parts-1)
5355 5356 5357 5358 5359 5360 5361 5362 5363
      {
	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;
      }
unknown's avatar
unknown committed
5364 5365 5366 5367
    }
  }

  /* Get range for retrieving rows in QUICK_SELECT::get_next */
5368
  if (!(range= new QUICK_RANGE((const char *) param->min_key,
5369
			       (uint) (tmp_min_key - param->min_key),
5370
			       (const char *) param->max_key,
5371 5372
			       (uint) (tmp_max_key - param->max_key),
			       flag)))
5373 5374
    return 1;			// out of memory

unknown's avatar
unknown committed
5375 5376
  set_if_bigger(quick->max_used_key_length,range->min_length);
  set_if_bigger(quick->max_used_key_length,range->max_length);
unknown's avatar
unknown committed
5377
  set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
5378 5379 5380
  if (insert_dynamic(&quick->ranges, (gptr)&range))
    return 1;

unknown's avatar
unknown committed
5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392
 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
*/

unknown's avatar
unknown committed
5393
bool QUICK_RANGE_SELECT::unique_key_range()
unknown's avatar
unknown committed
5394 5395 5396
{
  if (ranges.elements == 1)
  {
5397 5398
    QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
    if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
unknown's avatar
unknown committed
5399 5400
    {
      KEY *key=head->key_info+index;
5401
      return ((key->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME &&
unknown's avatar
unknown committed
5402 5403 5404 5405 5406 5407
	      key->key_length == tmp->min_length);
    }
  }
  return 0;
}

5408

unknown's avatar
unknown committed
5409
/* Returns TRUE if any part of the key is NULL */
5410 5411 5412

static bool null_part_in_key(KEY_PART *key_part, const char *key, uint length)
{
unknown's avatar
unknown committed
5413
  for (const char *end=key+length ;
5414
       key < end;
unknown's avatar
unknown committed
5415
       key+= key_part++->store_length)
5416
  {
unknown's avatar
unknown committed
5417 5418
    if (key_part->null_bit && *key)
      return 1;
5419 5420 5421 5422
  }
  return 0;
}

unknown's avatar
unknown committed
5423

5424 5425
bool QUICK_SELECT_I::check_if_keys_used(List<Item> *fields)
{
unknown's avatar
unknown committed
5426
  return check_if_key_used(head, index, *fields);
5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464
}

bool QUICK_INDEX_MERGE_SELECT::check_if_keys_used(List<Item> *fields)
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    if (check_if_key_used(head, quick->index, *fields))
      return 1;
  }
  return 0;
}

bool QUICK_ROR_INTERSECT_SELECT::check_if_keys_used(List<Item> *fields)
{
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
    if (check_if_key_used(head, quick->index, *fields))
      return 1;
  }
  return 0;
}

bool QUICK_ROR_UNION_SELECT::check_if_keys_used(List<Item> *fields)
{
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if (quick->check_if_keys_used(fields))
      return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
5465

unknown's avatar
unknown committed
5466
/****************************************************************************
unknown's avatar
unknown committed
5467
  Create a QUICK RANGE based on a key
unknown's avatar
unknown committed
5468 5469
  This allocates things in a new memory root, as this may be called many times
  during a query.
unknown's avatar
unknown committed
5470 5471
****************************************************************************/

unknown's avatar
unknown committed
5472
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
unknown's avatar
unknown committed
5473
                                             TABLE_REF *ref)
unknown's avatar
unknown committed
5474
{
5475 5476
  MEM_ROOT *old_root= thd->mem_root;
  /* The following call may change thd->mem_root */
unknown's avatar
unknown committed
5477
  QUICK_RANGE_SELECT *quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0);
unknown's avatar
unknown committed
5478 5479
  KEY *key_info = &table->key_info[ref->key];
  KEY_PART *key_part;
unknown's avatar
unknown committed
5480
  QUICK_RANGE *range;
unknown's avatar
unknown committed
5481 5482 5483
  uint part;

  if (!quick)
5484
    return 0;			/* no ranges found */
unknown's avatar
unknown committed
5485
  if (quick->init())
unknown's avatar
unknown committed
5486 5487
  {
    delete quick;
unknown's avatar
unknown committed
5488
    goto err;
unknown's avatar
unknown committed
5489
  }
5490

unknown's avatar
unknown committed
5491 5492 5493
  if (cp_buffer_from_ref(ref) && thd->is_fatal_error ||
      !(range= new QUICK_RANGE()))
    goto err;                                   // out of memory
5494

unknown's avatar
unknown committed
5495 5496 5497
  range->min_key=range->max_key=(char*) ref->key_buff;
  range->min_length=range->max_length=ref->key_length;
  range->flag= ((ref->key_length == key_info->key_length &&
5498 5499
		 (key_info->flags & (HA_NOSAME | HA_END_SPACE_KEY)) ==
		 HA_NOSAME) ? EQ_RANGE : 0);
unknown's avatar
unknown committed
5500 5501

  if (!(quick->key_parts=key_part=(KEY_PART *)
5502
	alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
unknown's avatar
unknown committed
5503 5504 5505 5506 5507 5508
    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;
unknown's avatar
unknown committed
5509 5510
    key_part->length=  	    key_info->key_part[part].length;
    key_part->store_length= key_info->key_part[part].store_length;
unknown's avatar
unknown committed
5511 5512
    key_part->null_bit=     key_info->key_part[part].null_bit;
  }
unknown's avatar
unknown committed
5513
  if (insert_dynamic(&quick->ranges,(gptr)&range))
5514 5515
    goto err;

unknown's avatar
unknown committed
5516
  /*
5517 5518 5519 5520 5521
     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.
  */
5522 5523 5524 5525 5526
  if (ref->null_ref_key)
  {
    QUICK_RANGE *null_range;

    *ref->null_ref_key= 1;		// Set null byte then create a range
unknown's avatar
unknown committed
5527 5528
    if (!(null_range= new QUICK_RANGE((char*)ref->key_buff, ref->key_length,
				      (char*)ref->key_buff, ref->key_length,
5529 5530 5531
				      EQ_RANGE)))
      goto err;
    *ref->null_ref_key= 0;		// Clear null byte
unknown's avatar
unknown committed
5532
    if (insert_dynamic(&quick->ranges,(gptr)&null_range))
5533 5534 5535
      goto err;
  }

5536
  thd->mem_root= old_root;
5537
  return quick;
unknown's avatar
unknown committed
5538 5539

err:
5540
  thd->mem_root= old_root;
unknown's avatar
unknown committed
5541 5542 5543 5544
  delete quick;
  return 0;
}

unknown's avatar
unknown committed
5545 5546

/*
5547
  Fetch all row ids into unique.
5548

unknown's avatar
unknown committed
5549
  If table has a clustered primary key that covers all rows (TRUE for bdb
5550
     and innodb currently) and one of the index_merge scans is a scan on PK,
unknown's avatar
unknown committed
5551 5552
  then
    primary key scan rowids are not put into Unique and also
5553
    rows that will be retrieved by PK scan are not put into Unique
unknown's avatar
unknown committed
5554

5555 5556 5557
  RETURN
    0     OK
    other error
unknown's avatar
unknown committed
5558
*/
5559

5560
int QUICK_INDEX_MERGE_SELECT::prepare_unique()
unknown's avatar
unknown committed
5561
{
5562 5563
  int result;
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::prepare_unique");
unknown's avatar
unknown committed
5564

5565
  /* We're going to just read rowids. */
5566 5567
  head->file->extra(HA_EXTRA_KEYREAD);

unknown's avatar
unknown committed
5568 5569
  /*
    Make innodb retrieve all PK member fields, so
5570
     * ha_innobase::position (which uses them) call works.
5571
     * We can filter out rows that will be retrieved by clustered PK.
5572
    (This also creates a deficiency - it is possible that we will retrieve
5573
     parts of key that are not used by current query at all.)
5574 5575 5576 5577 5578
  */
  head->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS);

  cur_quick_select->init();

5579
  unique= new Unique(refpos_order_cmp, (void *)head->file,
5580
                     head->file->ref_length,
5581
                     thd->variables.sortbuff_size);
5582 5583
  if (!unique)
    DBUG_RETURN(1);
unknown's avatar
unknown committed
5584
  for (;;)
5585
  {
unknown's avatar
unknown committed
5586
    while ((result= cur_quick_select->get_next()) == HA_ERR_END_OF_FILE)
5587
    {
unknown's avatar
unknown committed
5588
      cur_quick_select->range_end();
unknown's avatar
unknown committed
5589 5590 5591
      cur_quick_select= cur_quick_it++;
      if (!cur_quick_select)
        break;
5592 5593

      if (cur_quick_select->init())
5594
        DBUG_RETURN(1);
5595 5596 5597

      /* QUICK_RANGE_SELECT::reset never fails */
      cur_quick_select->reset();
unknown's avatar
unknown committed
5598 5599 5600
    }

    if (result)
unknown's avatar
unknown committed
5601
    {
5602 5603
      if (result != HA_ERR_END_OF_FILE)
        DBUG_RETURN(result);
5604
      break;
unknown's avatar
unknown committed
5605
    }
unknown's avatar
unknown committed
5606

5607 5608
    if (thd->killed)
      DBUG_RETURN(1);
unknown's avatar
unknown committed
5609

5610
    /* skip row if it will be retrieved by clustered PK scan */
5611 5612
    if (pk_quick_select && pk_quick_select->row_in_ranges())
      continue;
5613

unknown's avatar
unknown committed
5614
    cur_quick_select->file->position(cur_quick_select->record);
unknown's avatar
unknown committed
5615
    result= unique->unique_add((char*)cur_quick_select->file->ref);
5616
    if (result)
5617 5618
      DBUG_RETURN(1);

unknown's avatar
unknown committed
5619
  }
unknown's avatar
unknown committed
5620

5621 5622
  /* ok, all row ids are in Unique */
  result= unique->get(head);
unknown's avatar
unknown committed
5623
  doing_pk_scan= FALSE;
unknown's avatar
unknown committed
5624 5625
  /* start table scan */
  init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1);
5626 5627
  /* index_merge currently doesn't support "using index" at all */
  head->file->extra(HA_EXTRA_NO_KEYREAD);
5628

5629 5630 5631
  DBUG_RETURN(result);
}

5632

5633 5634 5635
/*
  Get next row for index_merge.
  NOTES
5636 5637 5638 5639
    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.
5640
*/
5641

5642 5643
int QUICK_INDEX_MERGE_SELECT::get_next()
{
5644
  int result;
5645
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");
unknown's avatar
unknown committed
5646

5647 5648 5649 5650 5651 5652 5653 5654 5655
  if (doing_pk_scan)
    DBUG_RETURN(pk_quick_select->get_next());

  result= read_record.read_record(&read_record);

  if (result == -1)
  {
    result= HA_ERR_END_OF_FILE;
    end_read_record(&read_record);
5656
    /* All rows from Unique have been retrieved, do a clustered PK scan */
unknown's avatar
unknown committed
5657
    if (pk_quick_select)
5658
    {
unknown's avatar
unknown committed
5659
      doing_pk_scan= TRUE;
5660 5661 5662 5663 5664 5665 5666
      if ((result= pk_quick_select->init()))
        DBUG_RETURN(result);
      DBUG_RETURN(pk_quick_select->get_next());
    }
  }

  DBUG_RETURN(result);
unknown's avatar
unknown committed
5667 5668
}

5669 5670

/*
unknown's avatar
unknown committed
5671
  Retrieve next record.
5672
  SYNOPSIS
unknown's avatar
unknown committed
5673 5674
     QUICK_ROR_INTERSECT_SELECT::get_next()

5675
  NOTES
5676 5677
    Invariant on enter/exit: all intersected selects have retrieved all index
    records with rowid <= some_rowid_val and no intersected select has
5678 5679 5680 5681
    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.

unknown's avatar
unknown committed
5682
    If a Clustered PK scan is present, it is used only to check if row
5683 5684 5685 5686 5687
    satisfies its condition (and never used for row retrieval).

  RETURN
   0     - Ok
   other - Error code if any error occurred.
5688 5689 5690 5691 5692 5693 5694 5695 5696
*/

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");
unknown's avatar
unknown committed
5697

5698 5699 5700 5701 5702 5703 5704 5705 5706 5707
  /* Get a rowid for first quick and save it as a 'candidate' */
  quick= quick_it++;
  if (cpk_quick)
  {
    do {
      error= quick->get_next();
    }while (!error && !cpk_quick->row_in_ranges());
  }
  else
    error= quick->get_next();
unknown's avatar
unknown committed
5708

5709 5710 5711 5712 5713 5714
  if (error)
    DBUG_RETURN(error);

  quick->file->position(quick->record);
  memcpy(last_rowid, quick->file->ref, head->file->ref_length);
  last_rowid_count= 1;
unknown's avatar
unknown committed
5715

5716 5717 5718 5719 5720 5721 5722
  while (last_rowid_count < quick_selects.elements)
  {
    if (!(quick= quick_it++))
    {
      quick_it.rewind();
      quick= quick_it++;
    }
unknown's avatar
unknown committed
5723

5724 5725 5726 5727
    do {
      if ((error= quick->get_next()))
        DBUG_RETURN(error);
      quick->file->position(quick->record);
unknown's avatar
unknown committed
5728
      cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743
    } while (cmp < 0);

    /* Ok, current select 'caught up' and returned ref >= cur_ref */
    if (cmp > 0)
    {
      /* Found a row with ref > cur_ref. Make it a new 'candidate' */
      if (cpk_quick)
      {
        while (!cpk_quick->row_in_ranges())
        {
          if ((error= quick->get_next()))
            DBUG_RETURN(error);
        }
      }
      memcpy(last_rowid, quick->file->ref, head->file->ref_length);
unknown's avatar
unknown committed
5744
      last_rowid_count= 1;
5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759
    }
    else
    {
      /* current 'candidate' row confirmed by this select */
      last_rowid_count++;
    }
  }

  /* We get here iff we got the same row ref in all scans. */
  if (need_to_fetch_row)
    error= head->file->rnd_pos(head->record[0], last_rowid);
  DBUG_RETURN(error);
}


unknown's avatar
unknown committed
5760 5761
/*
  Retrieve next record.
5762 5763
  SYNOPSIS
    QUICK_ROR_UNION_SELECT::get_next()
unknown's avatar
unknown committed
5764

5765
  NOTES
unknown's avatar
unknown committed
5766 5767
    Enter/exit invariant:
    For each quick select in the queue a {key,rowid} tuple has been
5768
    retrieved but the corresponding row hasn't been passed to output.
5769

unknown's avatar
unknown committed
5770
  RETURN
5771 5772
   0     - Ok
   other - Error code if any error occurred.
5773 5774 5775 5776 5777 5778 5779 5780
*/

int QUICK_ROR_UNION_SELECT::get_next()
{
  int error, dup_row;
  QUICK_SELECT_I *quick;
  byte *tmp;
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");
unknown's avatar
unknown committed
5781

5782 5783 5784 5785
  do
  {
    if (!queue.elements)
      DBUG_RETURN(HA_ERR_END_OF_FILE);
5786
    /* Ok, we have a queue with >= 1 scans */
5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802

    quick= (QUICK_SELECT_I*)queue_top(&queue);
    memcpy(cur_rowid, quick->last_rowid, rowid_length);

    /* 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);
    }
unknown's avatar
unknown committed
5803

5804 5805 5806
    if (!have_prev_rowid)
    {
      /* No rows have been returned yet */
unknown's avatar
unknown committed
5807 5808
      dup_row= FALSE;
      have_prev_rowid= TRUE;
5809 5810 5811 5812
    }
    else
      dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
  }while (dup_row);
unknown's avatar
unknown committed
5813

5814 5815 5816 5817 5818 5819 5820 5821
  tmp= cur_rowid;
  cur_rowid= prev_rowid;
  prev_rowid= tmp;

  error= head->file->rnd_pos(quick->record, prev_rowid);
  DBUG_RETURN(error);
}

unknown's avatar
unknown committed
5822 5823
	/* get next possible record using quick-struct */

unknown's avatar
unknown committed
5824
int QUICK_RANGE_SELECT::get_next()
unknown's avatar
unknown committed
5825
{
unknown's avatar
unknown committed
5826
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next");
unknown's avatar
unknown committed
5827 5828 5829

  for (;;)
  {
5830
    int result;
unknown's avatar
unknown committed
5831
    key_range start_key, end_key;
unknown's avatar
unknown committed
5832
    if (range)
unknown's avatar
unknown committed
5833 5834
    {
      // Already read through key
unknown's avatar
unknown committed
5835
      result= file->read_range_next();
unknown's avatar
unknown committed
5836
      if (result != HA_ERR_END_OF_FILE)
5837
	DBUG_RETURN(result);
unknown's avatar
unknown committed
5838
    }
unknown's avatar
unknown committed
5839

5840
    if (!cur_range)
unknown's avatar
unknown committed
5841
      range= *(cur_range= (QUICK_RANGE**) ranges.buffer);
unknown's avatar
unknown committed
5842
    else
5843
      range=
unknown's avatar
unknown committed
5844 5845
        (cur_range == ((QUICK_RANGE**) ranges.buffer + ranges.elements - 1)) ?
        (QUICK_RANGE*) 0 : *(++cur_range);
5846

5847
    if (!range)
unknown's avatar
unknown committed
5848
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used
unknown's avatar
unknown committed
5849

unknown's avatar
unknown committed
5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862
    start_key.key=    (const byte*) range->min_key;
    start_key.length= range->min_length;
    start_key.flag=   ((range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
		       (range->flag & EQ_RANGE) ?
		       HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
    end_key.key=      (const byte*) range->max_key;
    end_key.length=   range->max_length;
    /*
      We use READ_AFTER_KEY here because if we are reading on a key
      prefix we want to find all keys with this prefix
    */
    end_key.flag=     (range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
		       HA_READ_AFTER_KEY);
unknown's avatar
unknown committed
5863

unknown's avatar
unknown committed
5864 5865
    result= file->read_range_first(range->min_length ? &start_key : 0,
				   range->max_length ? &end_key : 0,
unknown's avatar
unknown committed
5866
                                   test(range->flag & EQ_RANGE),
unknown's avatar
unknown committed
5867 5868 5869 5870 5871 5872 5873
				   sorted);
    if (range->flag == (UNIQUE_RANGE | EQ_RANGE))
      range=0;				// Stop searching

    if (result != HA_ERR_END_OF_FILE)
      DBUG_RETURN(result);
    range=0;				// No matching rows; go to next range
unknown's avatar
unknown committed
5874 5875 5876
  }
}

5877

5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961
/*
  Get the next record with a different prefix.

  SYNOPSIS
    QUICK_RANGE_SELECT::get_next_prefix()
    prefix_length  length of cur_prefix
    cur_prefix     prefix of a key to be searached for

  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
*/

int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length, byte *cur_prefix)
{
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");

  for (;;)
  {
    int result;
    key_range start_key, end_key;
    if (range)
    {
      /* Read the next record in the same range with prefix after cur_prefix. */
      DBUG_ASSERT(cur_prefix);
      result= file->index_read(record, cur_prefix, prefix_length,
                               HA_READ_AFTER_KEY);
      if (result || (file->compare_key(file->end_range) <= 0))
        DBUG_RETURN(result);
    }

    if (!cur_range)
      range= *(cur_range= (QUICK_RANGE**) ranges.buffer); /* First range. */
    else
      range=
        (cur_range == ((QUICK_RANGE**) ranges.buffer + ranges.elements - 1)) ?
        (QUICK_RANGE*) 0 : *(++cur_range);                /* Next range. */

    if (!range)
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used

    start_key.key=    (const byte*) range->min_key;
    start_key.length= min(range->min_length, prefix_length);
    start_key.flag=   ((range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
		       (range->flag & EQ_RANGE) ?
		       HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
    end_key.key=      (const byte*) range->max_key;
    end_key.length=   min(range->max_length, prefix_length);
    /*
      We use READ_AFTER_KEY here because if we are reading on a key
      prefix we want to find all keys with this prefix
    */
    end_key.flag=     (range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
		       HA_READ_AFTER_KEY);

    result= file->read_range_first(range->min_length ? &start_key : 0,
				   range->max_length ? &end_key : 0,
                                   test(range->flag & EQ_RANGE),
				   sorted);
    if (range->flag == (UNIQUE_RANGE | EQ_RANGE))
      range=0;				// Stop searching

    if (result != HA_ERR_END_OF_FILE)
      DBUG_RETURN(result);
    range=0;				// No matching rows; go to next range
  }
}


unknown's avatar
unknown committed
5962
/* Get next for geometrical indexes */
unknown's avatar
unknown committed
5963

unknown's avatar
unknown committed
5964
int QUICK_RANGE_SELECT_GEOM::get_next()
unknown's avatar
unknown committed
5965
{
unknown's avatar
unknown committed
5966
  DBUG_ENTER("QUICK_RANGE_SELECT_GEOM::get_next");
unknown's avatar
unknown committed
5967

unknown's avatar
unknown committed
5968
  for (;;)
unknown's avatar
unknown committed
5969
  {
unknown's avatar
unknown committed
5970 5971
    int result;
    if (range)
unknown's avatar
unknown committed
5972
    {
unknown's avatar
unknown committed
5973 5974 5975 5976 5977
      // Already read through key
      result= file->index_next_same(record, (byte*) range->min_key,
				    range->min_length);
      if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
unknown's avatar
unknown committed
5978
    }
unknown's avatar
unknown committed
5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996

   if (!cur_range)
      range= *(cur_range= (QUICK_RANGE**) ranges.buffer);
    else
      range=
        (cur_range == ((QUICK_RANGE**) ranges.buffer + ranges.elements - 1)) ?
        (QUICK_RANGE*) 0 : *(++cur_range);

    if (!range)
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used

    result= file->index_read(record,
			     (byte*) range->min_key,
			     range->min_length,
			     (ha_rkey_function)(range->flag ^ GEOM_FLAG));
    if (result != HA_ERR_KEY_NOT_FOUND)
      DBUG_RETURN(result);
    range=0;				// Not found, to next range
unknown's avatar
unknown committed
5997 5998 5999
  }
}

unknown's avatar
unknown committed
6000

6001 6002 6003 6004
/*
  Check if current row will be retrieved by this QUICK_RANGE_SELECT

  NOTES
unknown's avatar
unknown committed
6005 6006
    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
6007
    quick select.
unknown's avatar
unknown committed
6008
    The implementation does a binary search on sorted array of disjoint
6009 6010
    ranges, without taking size of range into account.

unknown's avatar
unknown committed
6011
    This function is used to filter out clustered PK scan rows in
6012 6013
    index_merge quick select.

6014
  RETURN
unknown's avatar
unknown committed
6015 6016
    TRUE  if current row will be retrieved by this quick select
    FALSE if not
6017 6018 6019 6020 6021 6022 6023 6024 6025 6026
*/

bool QUICK_RANGE_SELECT::row_in_ranges()
{
  QUICK_RANGE *range;
  uint min= 0;
  uint max= ranges.elements - 1;
  uint mid= (max + min)/2;

  while (min != max)
unknown's avatar
unknown committed
6027
  {
6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040
    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;
  }
  range= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
  return (!cmp_next(range) && !cmp_prev(range));
}

6041
/*
6042 6043 6044 6045 6046 6047 6048
  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.
6049
 */
unknown's avatar
unknown committed
6050

unknown's avatar
unknown committed
6051
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
unknown's avatar
unknown committed
6052 6053
                                     uint used_key_parts)
 : QUICK_RANGE_SELECT(*q), rev_it(rev_ranges)
6054
{
unknown's avatar
unknown committed
6055
  QUICK_RANGE *r;
unknown's avatar
unknown committed
6056

6057 6058
  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
  QUICK_RANGE **last_range= pr + ranges.elements;
unknown's avatar
unknown committed
6059 6060
  for (; pr!=last_range; pr++)
    rev_ranges.push_front(*pr);
unknown's avatar
unknown committed
6061

unknown's avatar
unknown committed
6062
  /* Remove EQ_RANGE flag for keys that are not using the full key */
unknown's avatar
unknown committed
6063
  for (r = rev_it++; r; r = rev_it++)
unknown's avatar
unknown committed
6064 6065 6066 6067 6068 6069 6070 6071
  {
    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;
6072 6073
}

unknown's avatar
unknown committed
6074

6075 6076 6077 6078 6079 6080
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
unknown's avatar
unknown committed
6081 6082
   *   - 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
6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094
   *     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;
    if (range)
    {						// Already read through key
unknown's avatar
unknown committed
6095 6096 6097
      result = ((range->flag & EQ_RANGE)
		? file->index_next_same(record, (byte*) range->min_key,
					range->min_length) :
6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112
		file->index_prev(record));
      if (!result)
      {
	if (cmp_prev(*rev_it.ref()) == 0)
	  DBUG_RETURN(0);
      }
      else if (result != HA_ERR_END_OF_FILE)
	DBUG_RETURN(result);
    }

    if (!(range=rev_it++))
      DBUG_RETURN(HA_ERR_END_OF_FILE);		// All ranges used

    if (range->flag & NO_MAX_RANGE)		// Read last record
    {
6113 6114 6115
      int local_error;
      if ((local_error=file->index_last(record)))
	DBUG_RETURN(local_error);		// Empty table
6116 6117 6118 6119 6120 6121
      if (cmp_prev(range) == 0)
	DBUG_RETURN(0);
      range=0;			// No matching records; go to next range
      continue;
    }

unknown's avatar
unknown committed
6122
    if (range->flag & EQ_RANGE)
6123 6124 6125 6126 6127 6128
    {
      result = file->index_read(record, (byte*) range->max_key,
				range->max_length, HA_READ_KEY_EXACT);
    }
    else
    {
6129 6130 6131 6132 6133
      DBUG_ASSERT(range->flag & NEAR_MAX || range_reads_after_key(range));
      result=file->index_read(record, (byte*) range->max_key,
			      range->max_length,
			      ((range->flag & NEAR_MAX) ?
			       HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV));
6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151
    }
    if (result)
    {
      if (result != HA_ERR_KEY_NOT_FOUND)
	DBUG_RETURN(result);
      range=0;					// Not found, to next range
      continue;
    }
    if (cmp_prev(range) == 0)
    {
      if (range->flag == (UNIQUE_RANGE | EQ_RANGE))
	range = 0;				// Stop searching
      DBUG_RETURN(0);				// Found key is in range
    }
    range = 0;					// To next range
  }
}

6152

unknown's avatar
unknown committed
6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193
/*
  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;

  for (char *key=range_arg->max_key, *end=key+range_arg->max_length;
       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--;
    }
    if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0)
      return 0;
    if (cmp > 0)
      return 1;
  }
  return (range_arg->flag & NEAR_MAX) ? 1 : 0;          // Exact match
}


6194
/*
6195 6196 6197
  Returns 0 if found key is inside range (found key >= range->min_key).
*/

6198
int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
6199
{
unknown's avatar
unknown committed
6200
  int cmp;
6201
  if (range_arg->flag & NO_MIN_RANGE)
unknown's avatar
unknown committed
6202
    return 0;					/* key can't be to small */
6203

unknown's avatar
unknown committed
6204 6205
  cmp= key_cmp(key_part_info, (byte*) range_arg->min_key,
               range_arg->min_length);
unknown's avatar
unknown committed
6206 6207 6208
  if (cmp > 0 || cmp == 0 && !(range_arg->flag & NEAR_MIN))
    return 0;
  return 1;                                     // outside of range
6209 6210
}

6211

6212
/*
unknown's avatar
unknown committed
6213
 * TRUE if this range will require using HA_READ_AFTER_KEY
unknown's avatar
unknown committed
6214
   See comment in get_next() about this
6215
 */
unknown's avatar
unknown committed
6216

6217
bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
6218
{
unknown's avatar
unknown committed
6219
  return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
6220
	  !(range_arg->flag & EQ_RANGE) ||
unknown's avatar
unknown committed
6221
	  head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
6222 6223
}

6224

unknown's avatar
unknown committed
6225
/* TRUE if we are reading over a key that may have a NULL value */
unknown's avatar
unknown committed
6226

unknown's avatar
unknown committed
6227
#ifdef NOT_USED
6228
bool QUICK_SELECT_DESC::test_if_null_range(QUICK_RANGE *range_arg,
unknown's avatar
unknown committed
6229 6230
					   uint used_key_parts)
{
unknown's avatar
unknown committed
6231
  uint offset, end;
unknown's avatar
unknown committed
6232 6233 6234
  KEY_PART *key_part = key_parts,
           *key_part_end= key_part+used_key_parts;

6235
  for (offset= 0,  end = min(range_arg->min_length, range_arg->max_length) ;
unknown's avatar
unknown committed
6236
       offset < end && key_part != key_part_end ;
unknown's avatar
unknown committed
6237
       offset+= key_part++->store_length)
unknown's avatar
unknown committed
6238
  {
6239 6240
    if (!memcmp((char*) range_arg->min_key+offset,
		(char*) range_arg->max_key+offset,
unknown's avatar
unknown committed
6241
		key_part->store_length))
unknown's avatar
unknown committed
6242
      continue;
unknown's avatar
unknown committed
6243 6244

    if (key_part->null_bit && range_arg->min_key[offset])
unknown's avatar
unknown committed
6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256
      return 1;				// min_key is null and max_key isn't
    // Range doesn't cover NULL. This is ok if there is no more null parts
    break;
  }
  /*
    If the next min_range is > NULL, then we can use this, even if
    it's a NULL key
    Example:  SELECT * FROM t1 WHERE a = 2 AND b >0 ORDER BY a DESC,b DESC;

  */
  if (key_part != key_part_end && key_part->null_bit)
  {
6257
    if (offset >= range_arg->min_length || range_arg->min_key[offset])
unknown's avatar
unknown committed
6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269
      return 1;					// Could be null
    key_part++;
  }
  /*
    If any of the key parts used in the ORDER BY could be NULL, we can't
    use the key to sort the data.
  */
  for (; key_part != key_part_end ; key_part++)
    if (key_part->null_bit)
      return 1;					// Covers null part
  return 0;
}
unknown's avatar
unknown committed
6270
#endif
unknown's avatar
unknown committed
6271 6272


6273 6274 6275 6276 6277 6278 6279 6280 6281
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;
unknown's avatar
unknown committed
6282
  bool first= TRUE;
6283 6284 6285 6286 6287 6288 6289
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  str->append("sort_union(");
  while ((quick= it++))
  {
    if (!first)
      str->append(',');
    else
unknown's avatar
unknown committed
6290
      first= FALSE;
6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302
    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)
{
unknown's avatar
unknown committed
6303
  bool first= TRUE;
6304 6305 6306 6307 6308 6309 6310 6311
  QUICK_RANGE_SELECT *quick;
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  str->append("intersect(");
  while ((quick= it++))
  {
    KEY *key_info= head->key_info + quick->index;
    if (!first)
      str->append(',');
unknown's avatar
unknown committed
6312
    else
unknown's avatar
unknown committed
6313
      first= FALSE;
6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326
    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)
{
unknown's avatar
unknown committed
6327
  bool first= TRUE;
6328 6329 6330 6331 6332 6333 6334 6335
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  str->append("union(");
  while ((quick= it++))
  {
    if (!first)
      str->append(',');
    else
unknown's avatar
unknown committed
6336
      first= FALSE;
6337 6338 6339 6340 6341 6342
    quick->add_info_string(str);
  }
  str->append(')');
}


unknown's avatar
unknown committed
6343
void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
6344
                                              String *used_lengths)
6345 6346 6347 6348 6349 6350 6351 6352 6353
{
  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);
}

6354 6355
void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
                                                    String *used_lengths)
6356 6357 6358
{
  char buf[64];
  uint length;
unknown's avatar
unknown committed
6359
  bool first= TRUE;
6360
  QUICK_RANGE_SELECT *quick;
unknown's avatar
unknown committed
6361

6362 6363 6364
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
  while ((quick= it++))
  {
6365
    if (first)
unknown's avatar
unknown committed
6366
      first= FALSE;
6367 6368
    else
    {
6369 6370
      key_names->append(',');
      used_lengths->append(',');
6371
    }
unknown's avatar
unknown committed
6372

6373 6374
    KEY *key_info= head->key_info + quick->index;
    key_names->append(key_info->name);
6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388
    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);
  }
}

6389 6390
void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
                                                      String *used_lengths)
6391 6392 6393
{
  char buf[64];
  uint length;
unknown's avatar
unknown committed
6394
  bool first= TRUE;
6395 6396 6397 6398 6399 6400
  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)
unknown's avatar
unknown committed
6401
      first= FALSE;
6402
    else
6403 6404
    {
      key_names->append(',');
6405
      used_lengths->append(',');
6406 6407
    }
    key_names->append(key_info->name);
6408 6409 6410
    length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
    used_lengths->append(buf, length);
  }
unknown's avatar
unknown committed
6411

6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422
  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);
  }
}

6423 6424
void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
                                                  String *used_lengths)
6425
{
unknown's avatar
unknown committed
6426
  bool first= TRUE;
6427 6428 6429 6430 6431
  QUICK_SELECT_I *quick;
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
  while ((quick= it++))
  {
    if (first)
unknown's avatar
unknown committed
6432
      first= FALSE;
6433
    else
unknown's avatar
unknown committed
6434
    {
6435 6436 6437
      used_lengths->append(',');
      key_names->append(',');
    }
6438
    quick->add_keys_and_lengths(key_names, used_lengths);
6439 6440 6441
  }
}

6442 6443 6444 6445 6446 6447 6448 6449 6450

/*******************************************************************************
* 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);
static bool
6451
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
6452
                       KEY_PART_INFO *first_non_group_part,
6453 6454 6455 6456
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
                       byte *key_infix, uint *key_infix_len,
                       KEY_PART_INFO **first_non_infix_part);
6457
static bool
6458 6459
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type);
6460

6461 6462 6463 6464 6465 6466
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);
6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492

/*
  Test if this access method is applicable to a GROUP query with MIN/MAX
  functions, and if so, construct a new TRP object.

  SYNOPSIS
    get_best_group_min_max()
    param    Parameter from test_quick_select
    sel_tree Range tree generated by get_mm_tree

  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.
unknown's avatar
unknown committed
6493 6494
        - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
          GROUP BY and not referenced by MIN/MAX functions.
6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505
        with the following properties specified below.

    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
6506 6507
         - C IS NOT NULL
         - C != const
6508 6509 6510
    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.
6511
    SA5. The select list in DISTINCT queries should not contain expressions.
6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540
    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,
         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>.
    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
         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).

    C) Overall query form:
6541 6542 6543 6544
       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)]
6545 6546
         [AND PC(C)]
         [AND PA(A_i1,...,A_iq)]
6547 6548 6549 6550
       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:
6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625
       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?

  RETURN
    If mem_root != NULL
    - valid TRP_GROUP_MIN_MAX object if this QUICK class can be used for
      the query
    -  NULL o/w.
    If mem_root == NULL
    - NULL
*/

static TRP_GROUP_MIN_MAX *
get_best_group_min_max(PARAM *param, SEL_TREE *tree)
{
  THD *thd= param->thd;
  JOIN *join= thd->lex->select_lex.join;
  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. */
  Item_field *min_max_arg_item= NULL;/* The argument of all MIN/MAX functions.*/
  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. */
  uint group_key_parts= 0;  /* Number of index key parts in the group prefix. */
  uint used_key_parts= 0;   /* Number of index key parts used for access. */
  byte key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
  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;

  DBUG_ENTER("get_best_group_min_max");

  /* Perform few 'cheap' tests whether this access method is applicable. */
  if (!join || (thd->lex->sql_command != SQLCOM_SELECT))
    DBUG_RETURN(NULL);        /* This is not a select statement. */
  if ((join->tables != 1) ||  /* The query must reference one table. */
      ((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
       (!join->select_distinct)))
    DBUG_RETURN(NULL);
  if(table->keys == 0)        /* There are no indexes to use. */
    DBUG_RETURN(NULL);

  /* Analyze the query in more detail. */
6626
  List_iterator<Item> select_items_it(join->fields_list);
6627

6628 6629 6630 6631
  /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
  if(join->make_sum_func_list(join->all_fields, join->fields_list, 1))
    DBUG_RETURN(NULL);
  if (join->sum_funcs[0])
6632
  {
6633 6634 6635
    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
6636
    {
6637 6638 6639 6640 6641
      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;
      else
6642 6643
        DBUG_RETURN(NULL);

6644 6645
      Item *expr= min_max_item->args[0];    /* The argument of MIN/MAX. */
      if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
6646
      {
6647 6648 6649 6650
        if (! min_max_arg_item)
          min_max_arg_item= (Item_field*) expr;
        else if (! min_max_arg_item->eq(expr, 1))
          DBUG_RETURN(NULL);
6651
      }
6652 6653
      else
        DBUG_RETURN(NULL);
6654
    }
6655
  }
6656

6657 6658 6659 6660
  /* Check (SA5). */
  if (join->select_distinct)
  {
    while ((item= select_items_it++))
6661
    {
6662 6663
      if (item->type() != Item::FIELD_ITEM)
        DBUG_RETURN(NULL);
6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681
    }
  }

  /* Check (GA4) - that there are no expressions among the group attributes. */
  for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
  {
    if ((*tmp_group->item)->type() != Item::FIELD_ITEM)
      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;
  KEY *cur_index_info_end= cur_index_info + table->keys;
  KEY_PART_INFO *cur_part= NULL;
6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707
  KEY_PART_INFO *end_part; /* Last part for loops. */
  /* Last index part. */
  KEY_PART_INFO *last_part= NULL;
  KEY_PART_INFO *first_non_group_part= NULL;
  KEY_PART_INFO *first_non_infix_part= NULL;
  uint key_infix_parts= 0;
  uint cur_group_key_parts= 0;
  uint cur_group_prefix_len= 0;
  /* 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;
  double cur_read_cost= DBL_MAX;
  ha_rows cur_records;
  SEL_ARG *cur_index_tree= NULL;
  ha_rows cur_quick_prefix_records= 0;
  uint cur_param_idx;

  for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
       cur_index_info++, cur_index++)
  {
    /* Check (B1) - if current index is covering. */
    if (!table->used_keys.is_set(cur_index))
      goto next_index;
6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729

    /*
      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))
        {
6730 6731
          cur_group_prefix_len+= cur_part->store_length;
          ++cur_group_key_parts;
6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746
        }
        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.
    */
    else if (join->select_distinct)
    {
6747 6748
      select_items_it.rewind();
      while ((item= select_items_it++))
6749
      {
6750
        item_field= (Item_field*) item; /* (SA5) already checked above. */
6751 6752
        /* Find the order of the key part in the index. */
        key_part_nr= get_field_keypart(cur_index_info, item_field->field);
6753
        if (key_part_nr < 1 || key_part_nr > join->fields_list.elements)
6754 6755
          goto next_index;
        cur_part= cur_index_info->key_part + key_part_nr - 1;
6756
        cur_group_prefix_len+= cur_part->store_length;
6757
      }
6758
      cur_group_key_parts= join->fields_list.elements;
6759 6760 6761 6762 6763 6764 6765 6766
    }
    else
      DBUG_ASSERT(FALSE);

    /* Check (SA2). */
    if (min_max_arg_item)
    {
      key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
6767
      if (key_part_nr <= cur_group_key_parts)
6768 6769 6770 6771 6772 6773 6774 6775
        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.
    */
6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797

    /*
      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) ?
                             min_max_arg_part + 1 :
                             NULL :
                           NULL;
    if (first_non_group_part &&
        (!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
6798
    {
6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815
      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,
                                    last_part, thd, key_infix, &key_infix_len,
                                    &first_non_infix_part))
          goto next_index;
      }
      else if (min_max_arg_part &&
               (min_max_arg_part - first_non_group_part > 0))
        /*
          There is a gap but no range tree, thus no predicates at all for the
          non-group keyparts.
        */
6816 6817 6818
        goto next_index;
    }

6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
    /*
      Test (WA1) partially - that no other keypart after the last infix part is
      referenced in the query.
    */
    if (first_non_infix_part)
    {
      for (cur_part= first_non_infix_part; cur_part != last_part; cur_part++)
      {
        if (cur_part->field->query_id == thd->query_id)
          goto next_index;
      }
    }

6832
    /* If we got to this point, cur_index_info passes the test. */
6833 6834 6835
    key_infix_parts= key_infix_len ?
                     (first_non_infix_part - first_non_group_part) : 0;
    used_key_parts= cur_group_key_parts + key_infix_parts;
6836

6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863
    /* 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,
                                                    cur_index_tree);
    }
    cost_group_min_max(table, cur_index_info, used_key_parts,
                       cur_group_key_parts, tree, cur_index_tree,
                       cur_quick_prefix_records, have_min, have_max,
                       &cur_read_cost, &cur_records);

    if (cur_read_cost < best_read_cost)
    {
      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;
    }
6864 6865

  next_index:
6866 6867
    cur_group_key_parts= 0;
    cur_group_prefix_len= 0;
6868 6869 6870 6871
  }
  if (!index_info) /* No usable index found. */
    DBUG_RETURN(NULL);

6872 6873 6874
  /* Check (SA3) for the where clause. */
  if (join->conds && min_max_arg_item &&
      !check_group_min_max_predicates(join->conds, min_max_arg_item,
6875 6876
                                      (index_info->flags & HA_SPATIAL) ?
                                      Field::itMBR : Field::itRAW))
6877 6878 6879 6880
    DBUG_RETURN(NULL);

  /* The query passes all tests, so construct a new TRP object. */
  read_plan= new (param->mem_root)
6881 6882 6883 6884
                 TRP_GROUP_MIN_MAX(have_min, have_max, min_max_arg_part,
                                   group_prefix_len, used_key_parts,
                                   group_key_parts, index_info, index,
                                   key_infix_len,
6885
                                   (key_infix_len > 0) ? key_infix : NULL,
6886
                                   tree, best_index_tree, best_param_idx,
6887
                                   best_quick_prefix_records);
6888 6889 6890 6891 6892
  if (read_plan)
  {
    if (tree && read_plan->quick_prefix_records == 0)
      DBUG_RETURN(NULL);

6893 6894 6895
    read_plan->read_cost= best_read_cost;
    read_plan->records=   best_records;

6896 6897 6898 6899 6900 6901 6902 6903 6904 6905
    DBUG_PRINT("info",
               ("Returning group min/max plan: cost: %g, records: %lu",
                read_plan->read_cost, (ulong) read_plan->records));
  }

  DBUG_RETURN(read_plan);
}


/*
6906 6907
  Check that the MIN/MAX attribute participates only in range predicates
  with constants.
6908 6909 6910 6911 6912 6913

  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)
6914
    min_max_arg_part  the keypart of the MIN/MAX argument if any
6915 6916 6917

  DESCRIPTION
    The function walks recursively over the cond tree representing a WHERE
6918
    clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
6919 6920
    aggregate function, it is referenced only by one of the following
    predicates: {=, !=, <, <=, >, >=, between, is null, is not null}.
6921 6922 6923 6924 6925 6926 6927

  RETURN
    TRUE  if cond passes the test
    FALSE o/w
*/

static bool
6928 6929
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
                               Field::imagetype image_type)
6930 6931
{
  DBUG_ENTER("check_group_min_max_predicates");
6932
  DBUG_ASSERT(cond && min_max_arg_item);
6933 6934 6935 6936 6937 6938 6939 6940 6941

  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++))
    {
6942 6943
      if(!check_group_min_max_predicates(and_or_arg, min_max_arg_item,
                                         image_type))
6944 6945 6946 6947 6948
        DBUG_RETURN(FALSE);
    }
    DBUG_RETURN(TRUE);
  }

6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961
  /*
    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);
  
  /* We presume that at this point there are no other Items than functions. */
6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974
  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++)
  {
    cur_arg= arguments[arg_idx];
    DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
    if (cur_arg->type() == Item::FIELD_ITEM)
    {
6975
      if (min_max_arg_item->eq(cur_arg, 1)) 
6976 6977 6978
      {
       /*
         If pred references the MIN/MAX argument, check whether pred is a range
6979
         condition that compares the MIN/MAX argument with a constant.
6980 6981
       */
        Item_func::Functype pred_type= pred->functype();
6982 6983 6984 6985 6986 6987 6988 6989 6990 6991
        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)
6992 6993 6994 6995
          DBUG_RETURN(FALSE);

        /* Check that pred compares min_max_arg_item with a constant. */
        Item *args[3];
6996
        bzero(args, 3 * sizeof(Item*));
6997 6998 6999 7000
        bool inv;
        /* Test if this is a comparison of a field and a constant. */
        if (!simple_pred(pred, args, &inv))
          DBUG_RETURN(FALSE);
7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019

        /* 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);
7020 7021 7022 7023
      }
    }
    else if (cur_arg->type() == Item::FUNC_ITEM)
    {
7024 7025
      if(!check_group_min_max_predicates(cur_arg, min_max_arg_item,
                                         image_type))
7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044
        DBUG_RETURN(FALSE);
    }
    else if (cur_arg->const_item())
    {
      DBUG_RETURN(TRUE);
    }
    else
      DBUG_RETURN(FALSE);
  }

  DBUG_RETURN(TRUE);
}


/*
  Extract a sequence of constants from a conjunction of equality predicates.

  SYNOPSIS
    get_constant_key_infix()
7045 7046 7047 7048 7049 7050 7051 7052 7053
    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)
7054 7055 7056
    
  DESCRIPTION
    Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
7057 7058 7059 7060
    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
    (const_ci = NGF_i).
    Thus all the NGF_i attributes must fill the 'gap' between the last group-by
7061 7062 7063 7064 7065 7066
    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
7067
    TRUE  if the index passes the test
7068 7069 7070 7071
    FALSE o/w
*/

static bool
7072
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
7073
                       KEY_PART_INFO *first_non_group_part,
7074 7075 7076 7077
                       KEY_PART_INFO *min_max_arg_part,
                       KEY_PART_INFO *last_part, THD *thd,
                       byte *key_infix, uint *key_infix_len,
                       KEY_PART_INFO **first_non_infix_part)
7078 7079 7080
{
  SEL_ARG       *cur_range;
  KEY_PART_INFO *cur_part;
7081 7082
  /* End part for the first loop below. */
  KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099

  *key_infix_len= 0;
  byte *key_ptr= key_infix;
  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)
    {
7100 7101 7102 7103 7104 7105 7106
      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;
      }
7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130
    }

    /* 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;
    if ((cur_range->maybe_null &&
         cur_range->min_value[0] && cur_range->max_value[0])
        ||
        (memcmp(cur_range->min_value, cur_range->max_value, field_length) == 0))
    { /* cur_range specifies 'IS NULL' or an equality condition. */
      memcpy(key_ptr, cur_range->min_value, field_length);
      key_ptr+= field_length;
      *key_infix_len+= field_length;
    }
    else
      return FALSE;
  }

7131 7132 7133
  if (!min_max_arg_part && (cur_part == last_part))
    *first_non_infix_part= last_part;

7134 7135 7136 7137
  return TRUE;
}


7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157
/*
  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)
{
7158
  KEY_PART_INFO *part, *end;
7159

7160
  for (part= index->key_part, end= part + index->key_parts; part < end; part++)
7161 7162
  {
    if (field->eq(part->field))
unknown's avatar
unknown committed
7163
      return part - index->key_part + 1;
7164
  }
7165
  return 0;
7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206
}


/*
  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]);
}


7207
/*
7208
  Compute the cost of a quick_group_min_max_select for a particular index.
7209 7210

  SYNOPSIS
7211 7212 7213 7214 7215 7216 7217
    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
unknown's avatar
unknown committed
7218 7219
    quick_prefix_records [in] Number of records retrieved by the internally
			      used quick range select if any
7220 7221 7222 7223
    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
7224 7225

  DESCRIPTION
unknown's avatar
unknown committed
7226 7227
    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.
7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266

  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
*/

7267 7268 7269 7270 7271
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)
7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283
{
  uint table_records;
  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? */
7284
  DBUG_ENTER("TRP_GROUP_MIN_MAX::cost");
unknown's avatar
unknown committed
7285

7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303
  table_records= table->file->records;
  keys_per_block= (table->file->block_size / 2 /
                   (index_info->key_length + table->file->ref_length)
                        + 1);
  num_blocks= (table_records / keys_per_block) + 1;

  /* 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 */
    keys_per_group= (table_records / 10) + 1;
  num_groups= (table_records / keys_per_group) + 1;

  /* 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;
unknown's avatar
unknown committed
7304
    num_groups= (uint) rint(num_groups * quick_prefix_selectivity);
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
  }

  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;

7336
  *read_cost= io_cost + cpu_cost;
7337
  *records= num_groups;
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

  DBUG_PRINT("info",
             ("records=%u, keys/block=%u, keys/group=%u, records=%u, blocks=%u",
              table_records, keys_per_block, keys_per_group, records,
              num_blocks));
  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,
    NULL o/w.
*/

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");

7375 7376 7377 7378 7379
  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
                                        param->thd->lex->select_lex.join,
                                        have_min, have_max, min_max_arg_part,
                                        group_prefix_len, used_key_parts,
                                        index_info, index, read_cost, records,
unknown's avatar
unknown committed
7380 7381
                                        key_infix_len, key_infix,
                                        parent_alloc);
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
  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. */
      quick->quick_prefix_select= get_quick_select(param, param_idx, index_tree,
                                                   &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)
      {
7421
        if (quick->add_range(min_max_range))
7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 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
        {
          delete quick;
          quick= NULL;
          DBUG_RETURN(NULL);
        }
        min_max_range= min_max_range->next;
      }
    }
  }
  else
    quick->quick_prefix_select= NULL;

  quick->update_key_stat();

  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

  RETURN
    None
*/

unknown's avatar
unknown committed
7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477
QUICK_GROUP_MIN_MAX_SELECT::
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
                           bool have_max_arg,
                           KEY_PART_INFO *min_max_arg_part_arg,
                           uint group_prefix_len_arg,
                           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,
                           byte *key_infix_arg, MEM_ROOT *parent_alloc)
  :join(join_arg), index_info(index_info_arg),
   group_prefix_len(group_prefix_len_arg), have_min(have_min_arg),
   have_max(have_max_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)
7478 7479 7480 7481 7482 7483
{
  head=       table;
  file=       head->file;
  index=      use_index;
  record=     head->record[0];
  tmp_record= head->record[1];
7484 7485 7486
  read_time= read_cost_arg;
  records= records_arg;
  used_key_parts= used_key_parts_arg;
7487 7488 7489
  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;
unknown's avatar
unknown committed
7490 7491 7492 7493 7494 7495

  /*
    We can't have parent_alloc set as the init function can't handle this case
    yet.
  */
  DBUG_ASSERT(!parent_alloc);
7496 7497 7498
  if (!parent_alloc)
  {
    init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
unknown's avatar
unknown committed
7499
    join->thd->mem_root= &alloc;
7500 7501
  }
  else
7502
    bzero(&alloc, sizeof(MEM_ROOT));            // ensure that it's not used
7503 7504 7505 7506 7507 7508 7509 7510 7511
}


/*
  Do post-constructor initialization.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::init()
  
7512 7513 7514 7515 7516 7517
  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.

7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555
  RETURN
    0      OK
    other  Error code
*/

int QUICK_GROUP_MIN_MAX_SELECT::init()
{
  if (group_prefix) /* Already initialized. */
    return 0;

  if (!(last_prefix= (byte*) alloc_root(&alloc, group_prefix_len)))
      return 1;
  /*
    We may use group_prefix to store keys with all select fields, so allocate
    enough space for it.
  */
  if (!(group_prefix= (byte*) alloc_root(&alloc,
                                         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.
    */
    byte *tmp_key_infix= (byte*) alloc_root(&alloc, key_infix_len);
    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)
  {
    if(my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16))
      return 1;

7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569
    if (have_min)
    {
      if(!(min_functions= new List<Item_sum>))
        return 1;
    }
    else
      min_functions= NULL;
    if (have_max)
    {
      if(!(max_functions= new List<Item_sum>))
        return 1;
    }
    else
      max_functions= NULL;
7570

7571 7572 7573
    Item_sum *min_max_item;
    Item_sum **func_ptr= join->sum_funcs;
    while ((min_max_item= *(func_ptr++)))
7574
    {
7575 7576 7577 7578
      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);
7579 7580
    }

7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595
    if (have_min)
    {
      if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
        return 1;
    }
    else
      min_functions_it= NULL;

    if (have_max)
    {
      if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
        return 1;
    }
    else
      max_functions_it= NULL;
7596
  }
unknown's avatar
unknown committed
7597 7598
  else
    min_max_ranges.elements= 0;
7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630

  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));
  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
7631 7632
    FALSE on success
    TRUE  otherwise
7633 7634 7635 7636 7637 7638 7639 7640 7641
*/

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). */
  if((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
7642
    return FALSE;
7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657

  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,
                         sel_range->max_value, min_max_arg_len,
                         range_flag);
  if (!range)
7658
    return TRUE;
7659
  if (insert_dynamic(&min_max_ranges, (gptr)&range))
7660 7661
    return TRUE;
  return FALSE;
7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690
}


/*
  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)
  {
unknown's avatar
unknown committed
7691
    QUICK_RANGE *cur_range= 0;
7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727
    if (have_min)
    { /* Check if the right-most range has a lower boundary. */
      get_dynamic(&min_max_ranges, (gptr)&cur_range,
                  min_max_ranges.elements - 1);
      if (!(cur_range->flag & NO_MIN_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
        ++used_key_parts;
        return;
      }
    }
    if (have_max)
    { /* Check if the left-most range has an upper boundary. */
      get_dynamic(&min_max_ranges, (gptr)&cur_range, 0);
      if (!(cur_range->flag & NO_MAX_RANGE))
      {
        max_used_key_length+= min_max_arg_len;
        ++used_key_parts;
        return;
      }
    }
  }
  else if (have_min && min_max_arg_part && min_max_arg_part->field->is_null())
  {
    max_used_key_length+= min_max_arg_len;
    ++used_key_parts;
  }
}


/*
  Initialize a quick group min/max select for key retrieval.

  SYNOPSIS
    QUICK_GROUP_MIN_MAX_SELECT::reset()

7728 7729 7730 7731
  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.

7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068
  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 */
  result= file->ha_index_init(index);
  result= file->index_last(record);
  if (quick_prefix_select)
    quick_prefix_select->reset();
  if (result)
    DBUG_RETURN(result);
  /* 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;
  int result;
  int is_last_prefix;

  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.
    */
    is_last_prefix= key_cmp(index_info->key_part, last_prefix,
                            group_prefix_len);
    DBUG_ASSERT(is_last_prefix <= 0);
    if (result == HA_ERR_KEY_NOT_FOUND)
      continue;
    else if (result)
      break;

    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)));
    }
    result= have_min ? min_res : have_max ? max_res : result;
  }
  while (result == HA_ERR_KEY_NOT_FOUND && is_last_prefix != 0);

  if (result == 0)
    /*
      Partially mimic the behavior of end_select_send. Copy the
      field data from Item_field::field into Item_field::result_field
      of each non-aggregated field (the group fields, and optionally
      other fields in non-ANSI SQL mode).
    */
    copy_fields(&join->tmp_table_param);
  else if (result == HA_ERR_KEY_NOT_FOUND)
    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
    Load the prefix of the next group into group_prefix and 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.

  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.
    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
  {
    /* Apply the constant equality conditions to the non-group select fields. */
    if (key_infix_len > 0)
    {
      if ((result= file->index_read(record, group_prefix, real_prefix_len,
                                    HA_READ_KEY_EXACT)))
        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);
      result= file->index_read(record, tmp_record,
                               real_prefix_len + min_max_arg_len,
                               HA_READ_AFTER_KEY);
      /*
        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)
      {
        if(key_cmp(index_info->key_part, group_prefix, real_prefix_len))
          key_restore(record, tmp_record, index_info, 0);
      } else if (result == HA_ERR_KEY_NOT_FOUND) 
        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
    If there was no previous next_min call to determine the next group prefix,
    then load the next prefix into group_prefix, then lookup the maximal key of
    the group, and store it into this->record.

  RETURN
    0                    on success
    HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
    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
    result= file->index_read(record, group_prefix, real_prefix_len,
                             HA_READ_PREFIX_LAST);
  DBUG_RETURN(result);
}


/*
  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)
  {
    byte *cur_prefix= seen_first_key ? group_prefix : NULL;
    if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
                                                      cur_prefix)))
      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. */
      result= file->index_read(record, group_prefix, group_prefix_len,
                               HA_READ_AFTER_KEY);
      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
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
{
  ha_rkey_function find_flag;
  uint search_prefix_len;
  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. */
    get_dynamic(&min_max_ranges, (gptr)&cur_range, range_idx);

    /*
      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) &&
8069
        (key_cmp(min_max_arg_part, (const byte*) cur_range->max_key,
8070
                 min_max_arg_len) == 1))
8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195
      continue;

    if (cur_range->flag & NO_MIN_RANGE)
    {
      find_flag= HA_READ_KEY_EXACT;
      search_prefix_len= real_prefix_len;
    }
    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);
      search_prefix_len= real_prefix_len + min_max_arg_len;
      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;
    }

    result= file->index_read(record, group_prefix, search_prefix_len,
                             find_flag);
    if ((result == HA_ERR_KEY_NOT_FOUND) &&
        (cur_range->flag & (EQ_RANGE | NULL_RANGE)))
        continue; /* Check the next range. */
    else if (result)
        /*
          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.
        */
      break;

    /* 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)
    { /* Remember this key, and continue looking for a non-NULL key that */
      /* satisfies some other condition. */
      memcpy(tmp_record, record, head->rec_buff_length);
      found_null= TRUE;
      continue;
    }

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
    {
      result = HA_ERR_KEY_NOT_FOUND;
      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. */
      byte *max_key= (byte*) my_alloca(real_prefix_len + min_max_arg_len);
      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);
      if (!((cur_range->flag & NEAR_MAX) && (cmp_res == -1) ||
            (cmp_res <= 0)))
      {
        result = HA_ERR_KEY_NOT_FOUND;
        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)
  {
    memcpy(record, tmp_record, head->rec_buff_length);
    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
    other                if some error
*/

int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
{
  ha_rkey_function find_flag;
  uint search_prefix_len;
  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. */
    get_dynamic(&min_max_ranges, (gptr)&cur_range, range_idx - 1);

    /*
      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) &&
8196
        (key_cmp(min_max_arg_part, (const byte*) cur_range->min_key,
8197
                 min_max_arg_len) == -1))
8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325
      continue;

    if (cur_range->flag & NO_MAX_RANGE)
    {
      find_flag= HA_READ_PREFIX_LAST;
      search_prefix_len= real_prefix_len;
    }
    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);
      search_prefix_len= real_prefix_len + min_max_arg_len;
      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;
    }

    result= file->index_read(record, group_prefix, search_prefix_len,
                             find_flag);

    if ((result == HA_ERR_KEY_NOT_FOUND) && (cur_range->flag & EQ_RANGE))
      continue; /* Check the next range. */
    else if (result)
      /*
        In no key was found with this upper bound, there certainly are no keys
        in the ranges to the left.
      */
      return result;

    /* A key was found. */
    if (cur_range->flag & EQ_RANGE)
      return result; /* No need to perform the checks below for equal keys. */

    /* Check if record belongs to the current group. */
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
    {
      result = HA_ERR_KEY_NOT_FOUND;
      continue;
    }

    /* 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. */
      byte *min_key= (byte*) my_alloca(real_prefix_len + min_max_arg_len);
      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);
      if (!((cur_range->flag & NEAR_MIN) && (cmp_res == 1) ||
            (cmp_res >= 0)))
        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();
}


8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340
/*
  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.

*/

8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351
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);
}


8352
#ifndef DBUG_OFF
8353

8354 8355 8356 8357 8358 8359 8360 8361 8362
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");
  if (! _db_on_)
    DBUG_VOID_RETURN;
8363

8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379
  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())
    tmp.append("(empty)");
8380

8381
  DBUG_PRINT("info", ("SEL_TREE %p (%s) scans:%s", tree, msg, tmp.ptr()));
8382

8383 8384
  DBUG_VOID_RETURN;
}
8385

8386 8387 8388 8389

static void print_ror_scans_arr(TABLE *table, const char *msg,
                                struct st_ror_scan_info **start,
                                struct st_ror_scan_info **end)
8390
{
8391 8392 8393 8394 8395 8396 8397 8398
  DBUG_ENTER("print_ror_scans");
  if (! _db_on_)
    DBUG_VOID_RETURN;

  char buff[1024];
  String tmp(buff,sizeof(buff),&my_charset_bin);
  tmp.length(0);
  for(;start != end; start++)
8399
  {
8400 8401 8402
    if (tmp.length())
      tmp.append(',');
    tmp.append(table->key_info[(*start)->keynr].name);
8403
  }
8404 8405 8406 8407
  if (!tmp.length())
    tmp.append("(empty)");
  DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.ptr()));
  DBUG_VOID_RETURN;
8408 8409 8410
}


unknown's avatar
unknown committed
8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421
/*****************************************************************************
** 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
print_key(KEY_PART *key_part,const char *key,uint used_length)
{
  char buff[1024];
unknown's avatar
unknown committed
8422
  const char *key_end= key+used_length;
unknown's avatar
unknown committed
8423
  String tmp(buff,sizeof(buff),&my_charset_bin);
unknown's avatar
unknown committed
8424
  uint store_length;
unknown's avatar
unknown committed
8425

unknown's avatar
unknown committed
8426
  for (; key < key_end; key+=store_length, key_part++)
unknown's avatar
unknown committed
8427
  {
unknown's avatar
unknown committed
8428 8429 8430
    Field *field=      key_part->field;
    store_length= key_part->store_length;

unknown's avatar
unknown committed
8431 8432
    if (field->real_maybe_null())
    {
unknown's avatar
unknown committed
8433
      if (*key)
unknown's avatar
unknown committed
8434 8435 8436 8437
      {
	fwrite("NULL",sizeof(char),4,DBUG_FILE);
	continue;
      }
unknown's avatar
unknown committed
8438 8439
      key++;					// Skip null byte
      store_length--;
unknown's avatar
unknown committed
8440
    }
unknown's avatar
unknown committed
8441 8442
    field->set_key_image((char*) key, key_part->length, field->charset());
    field->val_str(&tmp);
unknown's avatar
unknown committed
8443
    fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
unknown's avatar
unknown committed
8444 8445
    if (key+store_length < key_end)
      fputc('/',DBUG_FILE);
unknown's avatar
unknown committed
8446 8447 8448
  }
}

unknown's avatar
unknown committed
8449

8450
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
unknown's avatar
unknown committed
8451
{
8452
  char buf[MAX_KEY/8+1];
8453
  DBUG_ENTER("print_quick");
unknown's avatar
unknown committed
8454 8455
  if (! _db_on_ || !quick)
    DBUG_VOID_RETURN;
8456
  DBUG_LOCK_FILE;
unknown's avatar
unknown committed
8457

unknown's avatar
unknown committed
8458
  quick->dbug_dump(0, TRUE);
8459
  fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));
unknown's avatar
unknown committed
8460

8461
  DBUG_UNLOCK_FILE;
unknown's avatar
unknown committed
8462 8463 8464
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
8465

8466
static void print_rowid(byte* val, int len)
unknown's avatar
unknown committed
8467
{
8468
  byte *pb;
unknown's avatar
unknown committed
8469
  DBUG_LOCK_FILE;
8470 8471 8472 8473 8474 8475 8476 8477 8478 8479
  fputc('\"', DBUG_FILE);
  for (pb= val; pb!= val + len; ++pb)
    fprintf(DBUG_FILE, "%c", *pb);
  fprintf(DBUG_FILE, "\", hex: ");

  for (pb= val; pb!= val + len; ++pb)
    fprintf(DBUG_FILE, "%x ", *pb);
  fputc('\n', DBUG_FILE);
  DBUG_UNLOCK_FILE;
}
8480

8481 8482 8483 8484
void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
{
  fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
	  indent, "", head->key_info[index].name, max_used_key_length);
unknown's avatar
unknown committed
8485

8486
  if (verbose)
unknown's avatar
unknown committed
8487
  {
8488 8489
    QUICK_RANGE *range;
    QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
unknown's avatar
unknown committed
8490
    QUICK_RANGE **last_range= pr + ranges.elements;
8491
    for (; pr!=last_range; ++pr)
unknown's avatar
unknown committed
8492
    {
8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503
      fprintf(DBUG_FILE, "%*s", indent + 2, "");
      range= *pr;
      if (!(range->flag & NO_MIN_RANGE))
      {
        print_key(key_parts,range->min_key,range->min_length);
        if (range->flag & NEAR_MIN)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
      }
      fputs("X",DBUG_FILE);
unknown's avatar
unknown committed
8504

8505 8506 8507 8508 8509 8510 8511 8512 8513
      if (!(range->flag & NO_MAX_RANGE))
      {
        if (range->flag & NEAR_MAX)
	  fputs(" < ",DBUG_FILE);
        else
	  fputs(" <= ",DBUG_FILE);
        print_key(key_parts,range->max_key,range->max_length);
      }
      fputs("\n",DBUG_FILE);
unknown's avatar
unknown committed
8514 8515
    }
  }
8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527
}

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)
  {
unknown's avatar
unknown committed
8528
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
8529 8530 8531 8532 8533 8534 8535 8536 8537
    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;
unknown's avatar
unknown committed
8538
  fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
8539 8540 8541
          indent, "", need_to_fetch_row? "":"non-");
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
  while ((quick= it++))
unknown's avatar
unknown committed
8542
    quick->dbug_dump(indent+2, verbose);
8543 8544
  if (cpk_quick)
  {
unknown's avatar
unknown committed
8545
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559
    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, "");
unknown's avatar
unknown committed
8560 8561
}

8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605

/*
  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);
  }
}


unknown's avatar
unknown committed
8606
#endif /* NOT_USED */
unknown's avatar
unknown committed
8607 8608

/*****************************************************************************
8609
** Instantiate templates
unknown's avatar
unknown committed
8610 8611 8612 8613 8614 8615
*****************************************************************************/

#ifdef __GNUC__
template class List<QUICK_RANGE>;
template class List_iterator<QUICK_RANGE>;
#endif