~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.cc

  • Committer: Toru Maesaka
  • Date: 2008-12-17 07:16:37 UTC
  • mto: (685.1.40 devel) (713.1.5 devel)
  • mto: This revision was merged to the branch mainline in revision 713.
  • Revision ID: dev@torum.net-20081217071637-7j9040w7lpms77r2
Removed my_time() and added error checking

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
/*
26
26
  This file contains:
27
27
 
28
 
  RangeAnalysisModule  
29
 
    A module that accepts a condition, index (or partitioning) description, 
30
 
    and builds lists of intervals (in index/partitioning space), such that 
31
 
    all possible records that match the condition are contained within the 
 
28
  RangeAnalysisModule
 
29
    A module that accepts a condition, index (or partitioning) description,
 
30
    and builds lists of intervals (in index/partitioning space), such that
 
31
    all possible records that match the condition are contained within the
32
32
    intervals.
33
33
    The entry point for the range analysis module is get_mm_tree() function.
34
 
    
 
34
 
35
35
    The lists are returned in form of complicated structure of interlinked
36
36
    SEL_TREE/SEL_IMERGE/SEL_ARG objects.
37
 
    See quick_range_seq_next, find_used_partitions for examples of how to walk 
 
37
    See quick_range_seq_next, find_used_partitions for examples of how to walk
38
38
    this structure.
39
39
    All direct "users" of this module are located within this file, too.
40
40
 
46
46
    The module has single entry point - prune_partitions() function.
47
47
 
48
48
 
49
 
  Range/index_merge/groupby-minmax optimizer module  
50
 
    A module that accepts a table, condition, and returns 
 
49
  Range/index_merge/groupby-minmax optimizer module
 
50
    A module that accepts a table, condition, and returns
51
51
     - a QUICK_*_SELECT object that can be used to retrieve rows that match
52
 
       the specified condition, or a "no records will match the condition" 
 
52
       the specified condition, or a "no records will match the condition"
53
53
       statement.
54
54
 
55
55
    The module entry points are
64
64
  ~~~~~~~~~~~~~~
65
65
  The code in this file (and elsewhere) makes operations on key value tuples.
66
66
  Those tuples are stored in the following format:
67
 
  
 
67
 
68
68
  The tuple is a sequence of key part values. The length of key part value
69
69
  depends only on its type (and not depends on the what value is stored)
70
 
  
 
70
 
71
71
    KeyTuple: keypart1-data, keypart2-data, ...
72
 
  
 
72
 
73
73
  The value of each keypart is stored in the following format:
74
 
  
 
74
 
75
75
    keypart_data: [isnull_byte] keypart-value-bytes
76
76
 
77
77
  If a keypart may have a NULL value (key_part->field->real_maybe_null() can
78
 
  be used to check this), then the first byte is a NULL indicator with the 
 
78
  be used to check this), then the first byte is a NULL indicator with the
79
79
  following valid values:
80
80
    1  - keypart has NULL value.
81
81
    0  - keypart has non-NULL value.
86
86
 
87
87
  keypart-value-bytes holds the value. Its format depends on the field type.
88
88
  The length of keypart-value-bytes may or may not depend on the value being
89
 
  stored. The default is that length is static and equal to 
 
89
  stored. The default is that length is static and equal to
90
90
  KEY_PART_INFO::length.
91
 
  
92
 
  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the 
 
91
 
 
92
  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the
93
93
  value:
94
 
  
 
94
 
95
95
     keypart-value-bytes: value_length value_bytes
96
96
 
97
97
  The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.
99
99
  See key_copy() and key_restore() for code to move data between index tuple
100
100
  and table record
101
101
 
102
 
  CAUTION: the above description is only sergefp's understanding of the 
 
102
  CAUTION: the above description is only sergefp's understanding of the
103
103
           subject and may omit some details.
104
104
*/
105
105
 
106
106
#include <drizzled/server_includes.h>
107
107
#include <drizzled/sql_select.h>
108
 
 
109
 
#ifndef EXTRA_DEBUG
110
 
#define test_rb_tree(A,B) {}
111
 
#define test_use_count(A) {}
 
108
#include <drizzled/error.h>
 
109
#include <drizzled/cost_vect.h>
 
110
#include <drizzled/item/cmpfunc.h>
 
111
#include <drizzled/field/num.h>
 
112
#include <drizzled/check_stack_overrun.h>
 
113
 
 
114
#include <string>
 
115
#include CMATH_H
 
116
 
 
117
using namespace std;
 
118
#if defined(CMATH_NAMESPACE)
 
119
using namespace CMATH_NAMESPACE;
112
120
#endif
113
121
 
114
122
/*
124
132
class RANGE_OPT_PARAM;
125
133
/*
126
134
  A construction block of the SEL_ARG-graph.
127
 
  
128
 
  The following description only covers graphs of SEL_ARG objects with 
 
135
 
 
136
  The following description only covers graphs of SEL_ARG objects with
129
137
  sel_arg->type==KEY_RANGE:
130
138
 
131
139
  One SEL_ARG object represents an "elementary interval" in form
132
 
  
 
140
 
133
141
      min_value <=?  table.keypartX  <=? max_value
134
 
  
 
142
 
135
143
  The interval is a non-empty interval of any kind: with[out] minimum/maximum
136
144
  bound, [half]open/closed, single-point interval, etc.
137
145
 
138
146
  1. SEL_ARG GRAPH STRUCTURE
139
 
  
 
147
 
140
148
  SEL_ARG objects are linked together in a graph. The meaning of the graph
141
149
  is better demostrated by an example:
142
 
  
 
150
 
143
151
     tree->keys[i]
144
 
      | 
 
152
      |
145
153
      |             $              $
146
154
      |    part=1   $     part=2   $    part=3
147
155
      |             $              $
150
158
      |  +-------+  $   +-------+  $   +--------+
151
159
      |      |      $              $       |
152
160
      |      |      $              $   +--------+
153
 
      |      |      $              $   | kp3=12 | 
154
 
      |      |      $              $   +--------+ 
155
 
      |  +-------+  $              $   
156
 
      \->| kp1=2 |--$--------------$-+ 
 
161
      |      |      $              $   | kp3=12 |
 
162
      |      |      $              $   +--------+
 
163
      |  +-------+  $              $
 
164
      \->| kp1=2 |--$--------------$-+
157
165
         +-------+  $              $ |   +--------+
158
166
             |      $              $  ==>| kp3=11 |
159
167
         +-------+  $              $ |   +--------+
161
169
         +-------+  $              $     +--------+
162
170
             |      $              $     | kp3=14 |
163
171
            ...     $              $     +--------+
164
 
 
 
172
 
165
173
  The entire graph is partitioned into "interval lists".
166
174
 
167
175
  An interval list is a sequence of ordered disjoint intervals over the same
168
176
  key part. SEL_ARG are linked via "next" and "prev" pointers. Additionally,
169
 
  all intervals in the list form an RB-tree, linked via left/right/parent 
 
177
  all intervals in the list form an RB-tree, linked via left/right/parent
170
178
  pointers. The RB-tree root SEL_ARG object will be further called "root of the
171
179
  interval list".
172
 
  
173
 
    In the example pic, there are 4 interval lists: 
 
180
 
 
181
    In the example pic, there are 4 interval lists:
174
182
    "kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
175
183
    The vertical lines represent SEL_ARG::next/prev pointers.
176
 
    
 
184
 
177
185
  In an interval list, each member X may have SEL_ARG::next_key_part pointer
178
186
  pointing to the root of another interval list Y. The pointed interval list
179
187
  must cover a key part with greater number (i.e. Y->part > X->part).
180
 
    
 
188
 
181
189
    In the example pic, the next_key_part pointers are represented by
182
190
    horisontal lines.
183
191
 
185
193
 
186
194
  It represents a condition in a special form (we don't have a name for it ATM)
187
195
  The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
188
 
  
 
196
 
189
197
  For example, the picture represents the condition in form:
190
 
   (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR 
191
 
   (kp1=2 AND (kp3=11 OR kp3=14)) OR 
 
198
   (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR
 
199
   (kp1=2 AND (kp3=11 OR kp3=14)) OR
192
200
   (kp1=3 AND (kp3=11 OR kp3=14))
193
201
 
194
202
 
197
205
  Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
198
206
  Then walk the SEL_ARG graph and get a list of dijsoint ordered key
199
207
  intervals (i.e. intervals in form
200
 
  
 
208
 
201
209
   (constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)
202
210
 
203
211
  Those intervals can be used to access the index. The uses are in:
208
216
                            and create QUICK_RANGE_SELECT object that will
209
217
                            read records within these intervals.
210
218
 
211
 
  4. SPACE COMPLEXITY NOTES 
 
219
  4. SPACE COMPLEXITY NOTES
212
220
 
213
221
    SEL_ARG graph is a representation of an ordered disjoint sequence of
214
222
    intervals over the ordered set of index tuple values.
215
223
 
216
224
    For multi-part keys, one can construct a WHERE expression such that its
217
225
    list of intervals will be of combinatorial size. Here is an example:
218
 
     
219
 
      (keypart1 IN (1,2, ..., n1)) AND 
220
 
      (keypart2 IN (1,2, ..., n2)) AND 
 
226
 
 
227
      (keypart1 IN (1,2, ..., n1)) AND
 
228
      (keypart2 IN (1,2, ..., n2)) AND
221
229
      (keypart3 IN (1,2, ..., n3))
222
 
    
 
230
 
223
231
    For this WHERE clause the list of intervals will have n1*n2*n3 intervals
224
232
    of form
225
 
     
 
233
 
226
234
      (keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
227
 
    
 
235
 
228
236
    SEL_ARG graph structure aims to reduce the amount of required space by
229
237
    "sharing" the elementary intervals when possible (the pic at the
230
 
    beginning of this comment has examples of such sharing). The sharing may 
 
238
    beginning of this comment has examples of such sharing). The sharing may
231
239
    prevent combinatorial blowup:
232
240
 
233
241
      There are WHERE clauses that have combinatorial-size interval lists but
234
242
      will be represented by a compact SEL_ARG graph.
235
243
      Example:
236
 
        (keypartN IN (1,2, ..., n1)) AND 
 
244
        (keypartN IN (1,2, ..., n1)) AND
237
245
        ...
238
 
        (keypart2 IN (1,2, ..., n2)) AND 
 
246
        (keypart2 IN (1,2, ..., n2)) AND
239
247
        (keypart1 IN (1,2, ..., n3))
240
248
 
241
249
    but not in all cases:
244
252
      representation but get_mm_tree() and its callees will construct a
245
253
      graph of combinatorial size.
246
254
      Example:
247
 
        (keypart1 IN (1,2, ..., n1)) AND 
248
 
        (keypart2 IN (1,2, ..., n2)) AND 
 
255
        (keypart1 IN (1,2, ..., n1)) AND
 
256
        (keypart2 IN (1,2, ..., n2)) AND
249
257
        ...
250
258
        (keypartN IN (1,2, ..., n3))
251
259
 
255
263
        By induction: Let's take any interval on some keypart in the middle:
256
264
 
257
265
           kp15=c0
258
 
        
 
266
 
259
267
        Then let's AND it with this interval 'structure' from preceding and
260
268
        following keyparts:
261
269
 
262
270
          (kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
263
 
        
 
271
 
264
272
        We will obtain this SEL_ARG graph:
265
 
 
 
273
 
266
274
             kp14     $      kp15      $      kp16
267
275
                      $                $
268
276
         +---------+  $   +---------+  $   +---------+
269
277
         | kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
270
278
         +---------+  $   +---------+  $   +---------+
271
 
              |       $                $              
272
 
         +---------+  $   +---------+  $             
273
 
         | kp14=c2 |--$-->| kp15=c0 |  $             
274
 
         +---------+  $   +---------+  $             
 
279
              |       $                $
 
280
         +---------+  $   +---------+  $
 
281
         | kp14=c2 |--$-->| kp15=c0 |  $
 
282
         +---------+  $   +---------+  $
275
283
                      $                $
276
 
                      
 
284
 
277
285
       Note that we had to duplicate "kp15=c0" and there was no way to avoid
278
 
       that. 
 
286
       that.
279
287
       The induction step: AND the obtained expression with another "wrapping"
280
288
       expression like (*).
281
 
       When the process ends because of the limit on max. number of keyparts 
 
289
       When the process ends because of the limit on max. number of keyparts
282
290
       we'll have:
283
291
 
284
292
         WHERE clause length  is O(3*#max_keyparts)
298
306
  uint8_t min_flag,max_flag,maybe_flag;
299
307
  uint8_t part;                                 // Which key part
300
308
  uint8_t maybe_null;
301
 
  /* 
 
309
  /*
302
310
    Number of children of this element in the RB-tree, plus 1 for this
303
311
    element itself.
304
312
  */
319
327
  SEL_ARG *left,*right;   /* R-B tree children */
320
328
  SEL_ARG *next,*prev;    /* Links for bi-directional interval list */
321
329
  SEL_ARG *parent;        /* R-B tree parent */
322
 
  SEL_ARG *next_key_part; 
 
330
  SEL_ARG *next_key_part;
323
331
  enum leaf_color { BLACK,RED } color;
324
332
  enum Type { IMPOSSIBLE, MAYBE, MAYBE_KEY, KEY_RANGE } type;
325
333
 
345
353
  inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; }
346
354
  inline void maybe_smaller() { maybe_flag=1; }
347
355
  /* Return true iff it's a single-point null interval */
348
 
  inline bool is_null_interval() { return maybe_null && max_value[0] == 1; } 
 
356
  inline bool is_null_interval() { return maybe_null && max_value[0] == 1; }
349
357
  inline int cmp_min_to_min(SEL_ARG* arg)
350
358
  {
351
359
    return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag);
482
490
    uint32_t res= key_tree->store_min(key[key_tree->part].store_length,
483
491
                                  range_key, *range_key_flag);
484
492
    *range_key_flag|= key_tree->min_flag;
485
 
    
 
493
 
486
494
    if (key_tree->next_key_part &&
487
495
        key_tree->next_key_part->part == key_tree->part+1 &&
488
496
        !(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)) &&
556
564
 
557
565
    SYNOPSIS
558
566
      is_singlepoint()
559
 
    
 
567
 
560
568
    DESCRIPTION
561
569
      Check if this SEL_ARG object (not tree) represents a single-point
562
 
      interval, i.e. if it represents a "keypart = const" or 
 
570
      interval, i.e. if it represents a "keypart = const" or
563
571
      "keypart IS NULL".
564
572
 
565
573
    RETURN
569
577
 
570
578
  bool is_singlepoint()
571
579
  {
572
 
    /* 
573
 
      Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field) 
 
580
    /*
 
581
      Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field)
574
582
      flags, and the same for right edge.
575
583
    */
576
584
    if (min_flag || max_flag)
602
610
public:
603
611
  /*
604
612
    Starting an effort to document this field:
605
 
    (for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) => 
 
613
    (for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) =>
606
614
       (type == SEL_TREE::IMPOSSIBLE)
607
615
  */
608
616
  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
639
647
class RANGE_OPT_PARAM
640
648
{
641
649
public:
642
 
  THD   *thd;   /* Current thread handle */
 
650
  Session       *session;   /* Current thread handle */
643
651
  Table *table; /* Table being analyzed */
644
652
  COND *cond;   /* Used inside get_mm_tree(). */
645
653
  table_map prev_tables;
656
664
    #keys elements are not empty)
657
665
  */
658
666
  uint32_t keys;
659
 
  
660
 
  /* 
 
667
 
 
668
  /*
661
669
    If true, the index descriptions describe real indexes (and it is ok to
662
670
    call field->optimize_range(real_keynr[...], ...).
663
671
    Otherwise index description describes fake indexes.
664
672
  */
665
673
  bool using_real_indexes;
666
 
  
 
674
 
667
675
  bool remove_jump_scans;
668
 
  
 
676
 
669
677
  /*
670
678
    used_key_no -> table_key_no translation table. Only makes sense if
671
679
    using_real_indexes==true
672
680
  */
673
681
  uint32_t real_keynr[MAX_KEY];
674
682
  /* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
675
 
  uint32_t alloced_sel_args; 
 
683
  uint32_t alloced_sel_args;
676
684
  bool force_default_mrr;
677
685
};
678
686
 
722
730
 
723
731
static bool is_key_scan_ror(PARAM *param, uint32_t keynr, uint8_t nparts);
724
732
static ha_rows check_quick_select(PARAM *param, uint32_t idx, bool index_only,
725
 
                                  SEL_ARG *tree, bool update_tbl_stats, 
 
733
                                  SEL_ARG *tree, bool update_tbl_stats,
726
734
                                  uint32_t *mrr_flags, uint32_t *bufsize,
727
735
                                  COST_VECT *cost);
728
736
                                  //bool update_tbl_stats);
732
740
*/
733
741
 
734
742
QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint32_t index,
735
 
                                     SEL_ARG *key_tree, uint32_t mrr_flags, 
 
743
                                     SEL_ARG *key_tree, uint32_t mrr_flags,
736
744
                                     uint32_t mrr_buf_size, MEM_ROOT *alloc);
737
745
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
738
746
                                       bool index_read_must_be_used,
1058
1066
  cleanup();
1059
1067
}
1060
1068
 
 
1069
 
 
1070
bool SQL_SELECT::check_quick(Session *session, bool force_quick_range,
 
1071
                             ha_rows limit)
 
1072
{
 
1073
  key_map tmp;
 
1074
  tmp.set_all();
 
1075
  return test_quick_select(session, tmp, 0, limit,
 
1076
                           force_quick_range, false) < 0;
 
1077
}
 
1078
 
 
1079
 
 
1080
bool SQL_SELECT::skip_record()
 
1081
{
 
1082
  return cond ? cond->val_int() == 0 : 0;
 
1083
}
 
1084
 
 
1085
 
1061
1086
QUICK_SELECT_I::QUICK_SELECT_I()
1062
1087
  :max_used_key_length(0),
1063
1088
   used_key_parts(0)
1064
1089
{}
1065
1090
 
1066
 
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, Table *table, uint32_t key_nr,
 
1091
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(Session *session, Table *table, uint32_t key_nr,
1067
1092
                                       bool no_alloc, MEM_ROOT *parent_alloc,
1068
1093
                                       bool *create_error)
1069
1094
  :free_file(0),cur_range(NULL),last_range(0),dont_free(0)
1077
1102
  key_part_info= head->key_info[index].key_part;
1078
1103
  my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
1079
1104
 
1080
 
  /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
1081
 
  mrr_buf_size= thd->variables.read_rnd_buff_size;
 
1105
  /* 'session' is not accessible in QUICK_RANGE_SELECT::reset(). */
 
1106
  mrr_buf_size= session->variables.read_rnd_buff_size;
1082
1107
  mrr_buf_desc= NULL;
1083
1108
 
1084
1109
  if (!no_alloc && !parent_alloc)
1085
1110
  {
1086
1111
    // Allocates everything through the internal memroot
1087
 
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1088
 
    thd->mem_root= &alloc;
 
1112
    init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
 
1113
    session->mem_root= &alloc;
1089
1114
  }
1090
1115
  else
1091
1116
    memset(&alloc, 0, sizeof(alloc));
1095
1120
  save_write_set= head->write_set;
1096
1121
 
1097
1122
  /* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
1098
 
  if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size,
1099
 
                                           MYF(MY_WME))))
 
1123
  if (!(bitmap= (my_bitmap_map*) malloc(head->s->column_bitmap_size)))
1100
1124
  {
1101
1125
    column_bitmap.bitmap= 0;
1102
1126
    *create_error= 1;
1127
1151
  if (!dont_free)
1128
1152
  {
1129
1153
    /* file is NULL for CPK scan on covering ROR-intersection */
1130
 
    if (file) 
 
1154
    if (file)
1131
1155
    {
1132
1156
      range_end();
1133
1157
      if (head->key_read)
1137
1161
      }
1138
1162
      if (free_file)
1139
1163
      {
1140
 
        file->ha_external_lock(current_thd, F_UNLCK);
 
1164
        file->ha_external_lock(current_session, F_UNLCK);
1141
1165
        file->close();
1142
1166
        delete file;
1143
1167
      }
1153
1177
}
1154
1178
 
1155
1179
 
1156
 
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
 
1180
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(Session *session_param,
1157
1181
                                                   Table *table)
1158
 
  :pk_quick_select(NULL), thd(thd_param)
 
1182
  :pk_quick_select(NULL), session(session_param)
1159
1183
{
1160
1184
  index= MAX_KEY;
1161
1185
  head= table;
1162
1186
  memset(&read_record, 0, sizeof(read_record));
1163
 
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1187
  init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
1164
1188
  return;
1165
1189
}
1166
1190
 
1203
1227
}
1204
1228
 
1205
1229
 
1206
 
QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
 
1230
QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(Session *session_param,
1207
1231
                                                       Table *table,
1208
1232
                                                       bool retrieve_full_rows,
1209
1233
                                                       MEM_ROOT *parent_alloc)
1210
 
  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
 
1234
  : cpk_quick(NULL), session(session_param), need_to_fetch_row(retrieve_full_rows),
1211
1235
    scans_inited(false)
1212
1236
{
1213
1237
  index= MAX_KEY;
1214
1238
  head= table;
1215
1239
  record= head->record[0];
1216
1240
  if (!parent_alloc)
1217
 
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1241
    init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
1218
1242
  else
1219
1243
    memset(&alloc, 0, sizeof(MEM_ROOT));
1220
1244
  last_rowid= (unsigned char*) alloc_root(parent_alloc? parent_alloc : &alloc,
1264
1288
int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
1265
1289
{
1266
1290
  handler *save_file= file, *org_file;
1267
 
  THD *thd;
 
1291
  Session *session;
1268
1292
 
1269
1293
  in_ror_merged_scan= 1;
1270
1294
  if (reuse_handler)
1284
1308
    return(0);
1285
1309
  }
1286
1310
 
1287
 
  thd= head->in_use;
1288
 
  if (!(file= head->file->clone(thd->mem_root)))
 
1311
  session= head->in_use;
 
1312
  if (!(file= head->file->clone(session->mem_root)))
1289
1313
  {
1290
 
    /* 
 
1314
    /*
1291
1315
      Manually set the error flag. Note: there seems to be quite a few
1292
1316
      places where a failure could cause the server to "hang" the client by
1293
 
      sending no response to a query. ATM those are not real errors because 
1294
 
      the storage engine calls in question happen to never fail with the 
1295
 
      existing storage engines. 
 
1317
      sending no response to a query. ATM those are not real errors because
 
1318
      the storage engine calls in question happen to never fail with the
 
1319
      existing storage engines.
1296
1320
    */
1297
1321
    my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
1298
1322
    /* Caller will free the memory */
1301
1325
 
1302
1326
  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1303
1327
 
1304
 
  if (file->ha_external_lock(thd, F_RDLCK))
 
1328
  if (file->ha_external_lock(session, F_RDLCK))
1305
1329
    goto failure;
1306
1330
 
1307
1331
  if (init() || reset())
1308
1332
  {
1309
 
    file->ha_external_lock(thd, F_UNLCK);
 
1333
    file->ha_external_lock(session, F_UNLCK);
1310
1334
    file->close();
1311
1335
    goto failure;
1312
1336
  }
1343
1367
}
1344
1368
 
1345
1369
 
 
1370
void QUICK_RANGE_SELECT::save_last_pos()
 
1371
{
 
1372
  file->position(record);
 
1373
}
 
1374
 
 
1375
 
1346
1376
/*
1347
1377
  Initialize this quick select to be a part of a ROR-merged scan.
1348
1378
  SYNOPSIS
1442
1472
}
1443
1473
 
1444
1474
 
1445
 
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
 
1475
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(Session *session_param,
1446
1476
                                               Table *table)
1447
 
  : thd(thd_param), scans_inited(false)
 
1477
  : session(session_param), scans_inited(false)
1448
1478
{
1449
1479
  index= MAX_KEY;
1450
1480
  head= table;
1451
1481
  rowid_length= table->file->ref_length;
1452
1482
  record= head->record[0];
1453
 
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1454
 
  thd_param->mem_root= &alloc;
 
1483
  init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
 
1484
  session_param->mem_root= &alloc;
1455
1485
}
1456
1486
 
1457
1487
 
1623
1653
  left=right= &null_element;
1624
1654
}
1625
1655
 
1626
 
SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, 
 
1656
SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent,
1627
1657
                        SEL_ARG **next_arg)
1628
1658
{
1629
1659
  SEL_ARG *tmp;
1759
1789
      limit  Number of records that will be retrieved
1760
1790
 
1761
1791
  DESCRIPTION
1762
 
    Find the best index that allows to retrieve first #limit records in the 
 
1792
    Find the best index that allows to retrieve first #limit records in the
1763
1793
    given order cheaper then one would retrieve them using full table scan.
1764
1794
 
1765
1795
  IMPLEMENTATION
1783
1813
  uint32_t idx;
1784
1814
  uint32_t match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
1785
1815
  order_st *ord;
1786
 
  
 
1816
 
1787
1817
  for (ord= order; ord; ord= ord->next)
1788
1818
    if (!ord->asc)
1789
1819
      return MAX_KEY;
1795
1825
    KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
1796
1826
    uint32_t n_parts=  table->key_info[idx].key_parts;
1797
1827
    uint32_t partno= 0;
1798
 
    
1799
 
    /* 
1800
 
      The below check is sufficient considering we now have either BTREE 
1801
 
      indexes (records are returned in order for any index prefix) or HASH 
 
1828
 
 
1829
    /*
 
1830
      The below check is sufficient considering we now have either BTREE
 
1831
      indexes (records are returned in order for any index prefix) or HASH
1802
1832
      indexes (records are not returned in order for any index prefix).
1803
1833
    */
1804
1834
    if (!(table->file->index_flags(idx, 0, 1) & HA_READ_ORDER))
1810
1840
           ((Item_field*)item)->field->eq(keyinfo[partno].field)))
1811
1841
        break;
1812
1842
    }
1813
 
    
 
1843
 
1814
1844
    if (!ord && table->key_info[idx].key_length < match_key_len)
1815
1845
    {
1816
 
      /* 
 
1846
      /*
1817
1847
        Ok, the ordering is compatible and this key is shorter then
1818
1848
        previous match (we want shorter keys as we'll have to read fewer
1819
1849
        index pages for the same number of records)
1825
1855
 
1826
1856
  if (match_key != MAX_KEY)
1827
1857
  {
1828
 
    /* 
1829
 
      Found an index that allows records to be retrieved in the requested 
 
1858
    /*
 
1859
      Found an index that allows records to be retrieved in the requested
1830
1860
      order. Now we'll check if using the index is cheaper then doing a table
1831
1861
      scan.
1832
1862
    */
1882
1912
  /* Table read plans are allocated on MEM_ROOT and are never deleted */
1883
1913
  static void *operator new(size_t size, MEM_ROOT *mem_root)
1884
1914
  { return (void*) alloc_root(mem_root, (uint) size); }
1885
 
  static void operator delete(void *ptr __attribute__((unused)),
1886
 
                              size_t size __attribute__((unused)))
 
1915
  static void operator delete(void *, size_t)
1887
1916
    { TRASH(ptr, size); }
1888
 
  static void operator delete(void *ptr __attribute__((unused)),
1889
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
1917
  static void operator delete(void *, MEM_ROOT *)
1890
1918
    { /* Never called */ }
1891
1919
  virtual ~TABLE_READ_PLAN() {}               /* Remove gcc warning */
1892
1920
 
1909
1937
public:
1910
1938
  SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
1911
1939
  uint32_t     key_idx; /* key number in PARAM::key */
1912
 
  uint32_t     mrr_flags; 
 
1940
  uint32_t     mrr_flags;
1913
1941
  uint32_t     mrr_buf_size;
1914
1942
 
1915
1943
  TRP_RANGE(SEL_ARG *key_arg, uint32_t idx_arg, uint32_t mrr_flags_arg)
1917
1945
  {}
1918
1946
  virtual ~TRP_RANGE() {}                     /* Remove gcc warning */
1919
1947
 
1920
 
  QUICK_SELECT_I *make_quick(PARAM *param,
1921
 
                             bool retrieve_full_rows __attribute__((unused)),
1922
 
                             MEM_ROOT *parent_alloc)
 
1948
  QUICK_SELECT_I *make_quick(PARAM *param, bool, MEM_ROOT *parent_alloc)
1923
1949
  {
1924
1950
    QUICK_RANGE_SELECT *quick;
1925
1951
    if ((quick= get_quick_select(param, key_idx, key, mrr_flags, mrr_buf_size,
1989
2015
 
1990
2016
 
1991
2017
/*
1992
 
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan. 
 
2018
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan.
1993
2019
*/
1994
2020
 
1995
2021
class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
2085
2111
 
2086
2112
  SYNOPSIS
2087
2113
    SQL_SELECT::test_quick_select()
2088
 
      thd               Current thread
 
2114
      session               Current thread
2089
2115
      keys_to_use       Keys to use for range retrieval
2090
2116
      prev_tables       Tables assumed to be already read when the scan is
2091
2117
                        performed (but not read at the moment of this call)
2105
2131
 
2106
2132
  IMPLEMENTATION
2107
2133
    quick_condition_rows value is obtained as follows:
2108
 
      
 
2134
 
2109
2135
      It is a minimum of E(#output rows) for all considered table access
2110
2136
      methods (range and index_merge accesses over various indexes).
2111
 
    
 
2137
 
2112
2138
    The obtained value is not a true E(#rows that satisfy table condition)
2113
2139
    but rather a pessimistic estimate. To obtain a true E(#...) one would
2114
2140
    need to combine estimates of various access methods, taking into account
2115
2141
    correlations between sets of rows they will return.
2116
 
    
 
2142
 
2117
2143
    For example, if values of tbl.key1 and tbl.key2 are independent (a right
2118
2144
    assumption if we have no information about their correlation) then the
2119
2145
    correct estimate will be:
2120
 
    
2121
 
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) = 
 
2146
 
 
2147
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) =
2122
2148
      = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
2123
2149
 
2124
 
    which is smaller than 
2125
 
      
 
2150
    which is smaller than
 
2151
 
2126
2152
       MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))
2127
2153
 
2128
2154
    which is currently produced.
2129
2155
 
2130
2156
  TODO
2131
2157
   * Change the value returned in quick_condition_rows from a pessimistic
2132
 
     estimate to true E(#rows that satisfy table condition). 
2133
 
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection 
 
2158
     estimate to true E(#rows that satisfy table condition).
 
2159
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection
2134
2160
      for this)
2135
 
   
 
2161
 
2136
2162
   * Check if this function really needs to modify keys_to_use, and change the
2137
2163
     code to pass it by reference if it doesn't.
2138
2164
 
2146
2172
    1 if found usable ranges and quick select has been successfully created.
2147
2173
*/
2148
2174
 
2149
 
int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
 
2175
int SQL_SELECT::test_quick_select(Session *session, key_map keys_to_use,
2150
2176
                                  table_map prev_tables,
2151
 
                                  ha_rows limit, bool force_quick_range, 
 
2177
                                  ha_rows limit, bool force_quick_range,
2152
2178
                                  bool ordered_output)
2153
2179
{
2154
2180
  uint32_t idx;
2180
2206
    KEY *key_info;
2181
2207
    PARAM param;
2182
2208
 
2183
 
    if (check_stack_overrun(thd, 2*STACK_MIN_SIZE, NULL))
 
2209
    if (check_stack_overrun(session, 2*STACK_MIN_SIZE, NULL))
2184
2210
      return(0);                           // Fatal error flag is set
2185
2211
 
2186
2212
    /* set up parameter that is passed to all functions */
2187
 
    param.thd= thd;
 
2213
    param.session= session;
2188
2214
    param.baseflag= head->file->ha_table_flags();
2189
2215
    param.prev_tables=prev_tables | const_tables;
2190
2216
    param.read_tables=read_tables;
2192
2218
    param.table=head;
2193
2219
    param.keys=0;
2194
2220
    param.mem_root= &alloc;
2195
 
    param.old_root= thd->mem_root;
 
2221
    param.old_root= session->mem_root;
2196
2222
    param.needed_reg= &needed_reg;
2197
2223
    param.imerge_cost_buff_size= 0;
2198
2224
    param.using_real_indexes= true;
2199
2225
    param.remove_jump_scans= true;
2200
2226
    param.force_default_mrr= ordered_output;
2201
2227
 
2202
 
    thd->no_errors=1;                           // Don't warn about NULL
2203
 
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
2228
    session->no_errors=1;                               // Don't warn about NULL
 
2229
    init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
2204
2230
    if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc,
2205
2231
                                                  sizeof(KEY_PART)*
2206
2232
                                                  head->s->key_parts)) ||
2207
2233
        fill_used_fields_bitmap(&param))
2208
2234
    {
2209
 
      thd->no_errors=0;
 
2235
      session->no_errors=0;
2210
2236
      free_root(&alloc,MYF(0));                 // Return memory & allocator
2211
2237
      return(0);                                // Can't use range
2212
2238
    }
2213
2239
    key_parts= param.key_parts;
2214
 
    thd->mem_root= &alloc;
 
2240
    session->mem_root= &alloc;
2215
2241
 
2216
2242
    /*
2217
2243
      Make an array with description of all key parts of all table keys.
2248
2274
    if (!head->covering_keys.is_clear_all())
2249
2275
    {
2250
2276
      int key_for_use= head->find_shortest_key(&head->covering_keys);
2251
 
      double key_read_time= 
2252
 
        param.table->file->index_only_read_time(key_for_use, 
 
2277
      double key_read_time=
 
2278
        param.table->file->index_only_read_time(key_for_use,
2253
2279
                                                rows2double(records)) +
2254
2280
        (double) records / TIME_FOR_COMPARE;
2255
2281
      if (key_read_time < read_time)
2320
2346
          objects are not allowed so don't use ROR-intersection for
2321
2347
          table deletes.
2322
2348
        */
2323
 
        if ((thd->lex->sql_command != SQLCOM_DELETE))
 
2349
        if ((session->lex->sql_command != SQLCOM_DELETE))
2324
2350
        {
2325
2351
          /*
2326
2352
            Get best non-covering ROR-intersection plan and prepare data for
2352
2378
        {
2353
2379
          new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
2354
2380
          if (new_conj_trp)
2355
 
            set_if_smaller(param.table->quick_condition_rows, 
 
2381
            set_if_smaller(param.table->quick_condition_rows,
2356
2382
                           new_conj_trp->records);
2357
2383
          if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
2358
2384
                                 best_conj_trp->read_cost))
2363
2389
      }
2364
2390
    }
2365
2391
 
2366
 
    thd->mem_root= param.old_root;
 
2392
    session->mem_root= param.old_root;
2367
2393
 
2368
2394
    /* If we got a read plan, create a quick select from it. */
2369
2395
    if (best_trp)
2378
2404
 
2379
2405
  free_mem:
2380
2406
    free_root(&alloc,MYF(0));                   // Return memory & allocator
2381
 
    thd->mem_root= param.old_root;
2382
 
    thd->no_errors=0;
 
2407
    session->mem_root= param.old_root;
 
2408
    session->no_errors=0;
2383
2409
  }
2384
2410
 
2385
2411
  /*
2545
2571
  /* Calculate cost(rowid_to_row_scan) */
2546
2572
  {
2547
2573
    COST_VECT sweep_cost;
2548
 
    JOIN *join= param->thd->lex->select_lex.join;
 
2574
    JOIN *join= param->session->lex->select_lex.join;
2549
2575
    bool is_interrupted= test(join && join->tables == 1);
2550
2576
    get_sweep_read_cost(param->table, non_cpk_scan_records, is_interrupted,
2551
2577
                        &sweep_cost);
2558
2584
  unique_calc_buff_size=
2559
2585
    Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
2560
2586
                                    param->table->file->ref_length,
2561
 
                                    param->thd->variables.sortbuff_size);
 
2587
                                    param->session->variables.sortbuff_size);
2562
2588
  if (param->imerge_cost_buff_size < unique_calc_buff_size)
2563
2589
  {
2564
2590
    if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
2570
2596
  imerge_cost +=
2571
2597
    Unique::get_use_cost(param->imerge_cost_buff, (uint)non_cpk_scan_records,
2572
2598
                         param->table->file->ref_length,
2573
 
                         param->thd->variables.sortbuff_size);
 
2599
                         param->session->variables.sortbuff_size);
2574
2600
  if (imerge_cost < read_time)
2575
2601
  {
2576
2602
    if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
2586
2612
  }
2587
2613
 
2588
2614
build_ror_index_merge:
2589
 
  if (!all_scans_ror_able || param->thd->lex->sql_command == SQLCOM_DELETE)
 
2615
  if (!all_scans_ror_able || param->session->lex->sql_command == SQLCOM_DELETE)
2590
2616
    return(imerge_trp);
2591
2617
 
2592
2618
  /* Ok, it is possible to build a ROR-union, try it. */
2661
2687
  double roru_total_cost;
2662
2688
  {
2663
2689
    COST_VECT sweep_cost;
2664
 
    JOIN *join= param->thd->lex->select_lex.join;
 
2690
    JOIN *join= param->session->lex->select_lex.join;
2665
2691
    bool is_interrupted= test(join && join->tables == 1);
2666
2692
    get_sweep_read_cost(param->table, roru_total_records, is_interrupted,
2667
2693
                        &sweep_cost);
2881
2907
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
2882
2908
{
2883
2909
  dst->param= src->param;
2884
 
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap, 
 
2910
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap,
2885
2911
         no_bytes_in_map(&src->covered_fields));
2886
2912
  dst->out_rows= src->out_rows;
2887
2913
  dst->is_covering= src->is_covering;
2896
2922
 
2897
2923
  SYNOPSIS
2898
2924
    ror_scan_selectivity()
2899
 
      info  ROR-interection 
 
2925
      info  ROR-interection
2900
2926
      scan  ROR scan
2901
 
      
 
2927
 
2902
2928
  NOTES
2903
2929
    Suppose we have a condition on several keys
2904
2930
    cond=k_11=c_11 AND k_12=c_12 AND ...  // parts of first key
2981
3007
    Selectivity of given ROR scan.
2982
3008
*/
2983
3009
 
2984
 
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info, 
 
3010
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
2985
3011
                                   const ROR_SCAN_INFO *scan)
2986
3012
{
2987
3013
  double selectivity_mult= 1.0;
3083
3109
 
3084
3110
    E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
3085
3111
                           ror_scan_selectivity({scan1}, scan2) * ... *
3086
 
                           ror_scan_selectivity({scan1,...}, scanN). 
 
3112
                           ror_scan_selectivity({scan1,...}, scanN).
3087
3113
  RETURN
3088
3114
    true   ROR scan added to ROR-intersection, cost updated.
3089
3115
    false  It doesn't make sense to add this ROR scan to this ROR-intersection.
3100
3126
    /* Don't add this scan if it doesn't improve selectivity. */
3101
3127
    return(false);
3102
3128
  }
3103
 
  
 
3129
 
3104
3130
  info->out_rows *= selectivity_mult;
3105
 
  
 
3131
 
3106
3132
  if (is_cpk_scan)
3107
3133
  {
3108
3134
    /*
3109
 
      CPK scan is used to filter out rows. We apply filtering for 
 
3135
      CPK scan is used to filter out rows. We apply filtering for
3110
3136
      each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
3111
3137
      per check this gives us:
3112
3138
    */
3113
 
    info->index_scan_costs += rows2double(info->index_records) / 
 
3139
    info->index_scan_costs += rows2double(info->index_records) /
3114
3140
                              TIME_FOR_COMPARE_ROWID;
3115
3141
  }
3116
3142
  else
3129
3155
  if (!info->is_covering)
3130
3156
  {
3131
3157
    COST_VECT sweep_cost;
3132
 
    JOIN *join= info->param->thd->lex->select_lex.join;
 
3158
    JOIN *join= info->param->session->lex->select_lex.join;
3133
3159
    bool is_interrupted= test(join && join->tables == 1);
3134
3160
    get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
3135
3161
                        is_interrupted, &sweep_cost);
3155
3181
 
3156
3182
  NOTES
3157
3183
    get_key_scans_params must be called before this function can be called.
3158
 
    
 
3184
 
3159
3185
    When this function is called by ROR-union construction algorithm it
3160
3186
    assumes it is building an uncovered ROR-intersection (and thus # of full
3161
3187
    records to be retrieved is wrong here). This is a hack.
3177
3203
        firstR= R - first(R);
3178
3204
        if (!selectivity(S + firstR < selectivity(S)))
3179
3205
          continue;
3180
 
          
 
3206
 
3181
3207
        S= S + first(R);
3182
3208
        if (cost(S) < min_cost)
3183
3209
        {
3215
3241
    return(NULL);
3216
3242
 
3217
3243
  /*
3218
 
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of 
 
3244
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
3219
3245
    them. Also find and save clustered PK scan if there is one.
3220
3246
  */
3221
3247
  ROR_SCAN_INFO **cur_ror_scan;
3271
3297
 
3272
3298
  /* Create and incrementally update ROR intersection. */
3273
3299
  ROR_INTERSECT_INFO *intersect, *intersect_best;
3274
 
  if (!(intersect= ror_intersect_init(param)) || 
 
3300
  if (!(intersect= ror_intersect_init(param)) ||
3275
3301
      !(intersect_best= ror_intersect_init(param)))
3276
3302
    return NULL;
3277
3303
 
3287
3313
      cur_ror_scan++;
3288
3314
      continue;
3289
3315
    }
3290
 
    
 
3316
 
3291
3317
    *(intersect_scans_end++)= *(cur_ror_scan++);
3292
3318
 
3293
3319
    if (intersect->total_cost < min_cost)
3303
3329
  {
3304
3330
    return(NULL);
3305
3331
  }
3306
 
    
 
3332
 
3307
3333
  print_ror_scans_arr(param->table,
3308
3334
                                          "best ROR-intersection",
3309
3335
                                          intersect_scans,
3315
3341
 
3316
3342
  /*
3317
3343
    Ok, found the best ROR-intersection of non-CPK key scans.
3318
 
    Check if we should add a CPK scan. If the obtained ROR-intersection is 
 
3344
    Check if we should add a CPK scan. If the obtained ROR-intersection is
3319
3345
    covering, it doesn't make sense to add CPK scan.
3320
3346
  */
3321
3347
  if (cpk_scan && !intersect->is_covering)
3322
3348
  {
3323
 
    if (ror_intersect_add(intersect, cpk_scan, true) && 
 
3349
    if (ror_intersect_add(intersect, cpk_scan, true) &&
3324
3350
        (intersect->total_cost < min_cost))
3325
3351
    {
3326
3352
      cpk_scan_used= true;
3409
3435
  ror_scan_mark= tree->ror_scans;
3410
3436
 
3411
3437
  MY_BITMAP *covered_fields= &param->tmp_covered_fields;
3412
 
  if (!covered_fields->bitmap) 
 
3438
  if (!covered_fields->bitmap)
3413
3439
    covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
3414
3440
                                               param->fields_bitmap_size);
3415
3441
  if (!covered_fields->bitmap ||
3458
3484
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
3459
3485
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
3460
3486
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
3461
 
  
 
3487
 
3462
3488
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
3463
3489
    return(NULL);
3464
3490
 
3492
3518
  trp->read_cost= total_cost;
3493
3519
  trp->records= records;
3494
3520
  trp->cpk_scan= NULL;
3495
 
  set_if_smaller(param->table->quick_condition_rows, records); 
 
3521
  set_if_smaller(param->table->quick_condition_rows, records);
3496
3522
 
3497
3523
  return(trp);
3498
3524
}
3509
3535
                               (except for clustered PK indexes)
3510
3536
      update_tbl_stats         true <=> update table->quick_* with information
3511
3537
                               about range scans we've evaluated.
3512
 
      read_time                Maximum cost. i.e. don't create read plans with 
 
3538
      read_time                Maximum cost. i.e. don't create read plans with
3513
3539
                               cost > read_time.
3514
3540
 
3515
3541
  DESCRIPTION
3516
 
    Find the best "range" table read plan for given SEL_TREE. 
3517
 
    The side effects are 
 
3542
    Find the best "range" table read plan for given SEL_TREE.
 
3543
    The side effects are
3518
3544
     - tree->ror_scans is updated to indicate which scans are ROR scans.
3519
3545
     - if update_tbl_stats=true then table->quick_* is updated with info
3520
3546
       about every possible range scan.
3525
3551
*/
3526
3552
 
3527
3553
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
3528
 
                                       bool index_read_must_be_used, 
 
3554
                                       bool index_read_must_be_used,
3529
3555
                                       bool update_tbl_stats,
3530
3556
                                       double read_time)
3531
3557
{
3555
3581
          (*key)->maybe_flag)
3556
3582
        param->needed_reg->set_bit(keynr);
3557
3583
 
3558
 
      bool read_index_only= index_read_must_be_used || 
 
3584
      bool read_index_only= index_read_must_be_used ||
3559
3585
                            param->table->covering_keys.is_set(keynr);
3560
3586
 
3561
3587
      found_records= check_quick_select(param, idx, read_index_only, *key,
3596
3622
}
3597
3623
 
3598
3624
 
3599
 
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
3600
 
                                            bool retrieve_full_rows __attribute__((unused)),
3601
 
                                            MEM_ROOT *parent_alloc __attribute__((unused)))
 
3625
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param, bool, MEM_ROOT *)
3602
3626
{
3603
3627
  QUICK_INDEX_MERGE_SELECT *quick_imerge;
3604
3628
  QUICK_RANGE_SELECT *quick;
3605
3629
  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
3606
 
  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
 
3630
  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->session, param->table)))
3607
3631
    return NULL;
3608
3632
 
3609
3633
  quick_imerge->records= records;
3632
3656
  MEM_ROOT *alloc;
3633
3657
 
3634
3658
  if ((quick_intrsect=
3635
 
         new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
 
3659
         new QUICK_ROR_INTERSECT_SELECT(param->session, param->table,
3636
3660
                                        (retrieve_full_rows? (!is_covering) :
3637
3661
                                         false),
3638
3662
                                        parent_alloc)))
3663
3687
        delete quick_intrsect;
3664
3688
        return(NULL);
3665
3689
      }
3666
 
      quick->file= NULL; 
 
3690
      quick->file= NULL;
3667
3691
      quick_intrsect->cpk_quick= quick;
3668
3692
    }
3669
3693
    quick_intrsect->records= records;
3673
3697
}
3674
3698
 
3675
3699
 
3676
 
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
3677
 
                                          bool retrieve_full_rows __attribute__((unused)),
3678
 
                                          MEM_ROOT *parent_alloc __attribute__((unused)))
 
3700
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param, bool, MEM_ROOT *)
3679
3701
{
3680
3702
  QUICK_ROR_UNION_SELECT *quick_roru;
3681
3703
  TABLE_READ_PLAN **scan;
3684
3706
    It is impossible to construct a ROR-union that will not retrieve full
3685
3707
    rows, ignore retrieve_full_rows parameter.
3686
3708
  */
3687
 
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
 
3709
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->session, param->table)))
3688
3710
  {
3689
3711
    for (scan= first_ror; scan != last_ror; scan++)
3690
3712
    {
3701
3723
 
3702
3724
/*
3703
3725
  Build a SEL_TREE for <> or NOT BETWEEN predicate
3704
 
 
 
3726
 
3705
3727
  SYNOPSIS
3706
3728
    get_ne_mm_tree()
3707
3729
      param       PARAM from SQL_SELECT::test_quick_select
3711
3733
      gt_value    constant that field should be greaterr
3712
3734
      cmp_type    compare type for the field
3713
3735
 
3714
 
  RETURN 
 
3736
  RETURN
3715
3737
    #  Pointer to tree built tree
3716
3738
    0  on error
3717
3739
*/
3718
3740
 
3719
 
static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
 
3741
static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
3720
3742
                                Field *field,
3721
3743
                                Item *lt_value, Item *gt_value,
3722
3744
                                Item_result cmp_type)
3732
3754
  }
3733
3755
  return tree;
3734
3756
}
3735
 
   
 
3757
 
3736
3758
 
3737
3759
/*
3738
3760
  Build a SEL_TREE for a simple predicate
3739
 
 
 
3761
 
3740
3762
  SYNOPSIS
3741
3763
    get_func_mm_tree()
3742
3764
      param       PARAM from SQL_SELECT::test_quick_select
3745
3767
      value       constant in the predicate
3746
3768
      cmp_type    compare type for the field
3747
3769
      inv         true <> NOT cond_func is considered
3748
 
                  (makes sense only when cond_func is BETWEEN or IN) 
 
3770
                  (makes sense only when cond_func is BETWEEN or IN)
3749
3771
 
3750
 
  RETURN 
 
3772
  RETURN
3751
3773
    Pointer to the tree built tree
3752
3774
*/
3753
3775
 
3754
 
static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
 
3776
static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
3755
3777
                                  Field *field, Item *value,
3756
3778
                                  Item_result cmp_type, bool inv)
3757
3779
{
3805
3827
      so we check it here to avoid unnecessary work.
3806
3828
    */
3807
3829
    if (!func->arg_types_compatible)
3808
 
      break;     
 
3830
      break;
3809
3831
 
3810
3832
    if (inv)
3811
3833
    {
3813
3835
      {
3814
3836
        /*
3815
3837
          We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
3816
 
          where c{i} are constants. Our goal is to produce a SEL_TREE that 
 
3838
          where c{i} are constants. Our goal is to produce a SEL_TREE that
3817
3839
          represents intervals:
3818
 
          
 
3840
 
3819
3841
          ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ...    (*)
3820
 
          
 
3842
 
3821
3843
          where $MIN is either "-inf" or NULL.
3822
 
          
 
3844
 
3823
3845
          The most straightforward way to produce it is to convert NOT IN
3824
3846
          into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
3825
3847
          analyzer to build SEL_TREE from that. The problem is that the
3829
3851
 
3830
3852
          Another problem with big lists like (*) is that a big list is
3831
3853
          unlikely to produce a good "range" access, while considering that
3832
 
          range access will require expensive CPU calculations (and for 
 
3854
          range access will require expensive CPU calculations (and for
3833
3855
          MyISAM even index accesses). In short, big NOT IN lists are rarely
3834
3856
          worth analyzing.
3835
3857
 
3840
3862
        */
3841
3863
#define NOT_IN_IGNORE_THRESHOLD 1000
3842
3864
        MEM_ROOT *tmp_root= param->mem_root;
3843
 
        param->thd->mem_root= param->old_root;
3844
 
        /* 
 
3865
        param->session->mem_root= param->old_root;
 
3866
        /*
3845
3867
          Create one Item_type constant object. We'll need it as
3846
3868
          get_mm_parts only accepts constant values wrapped in Item_Type
3847
3869
          objects.
3848
3870
          We create the Item on param->mem_root which points to
3849
 
          per-statement mem_root (while thd->mem_root is currently pointing
 
3871
          per-statement mem_root (while session->mem_root is currently pointing
3850
3872
          to mem_root local to range optimizer).
3851
3873
        */
3852
3874
        Item *value_item= func->array->create_item();
3853
 
        param->thd->mem_root= tmp_root;
 
3875
        param->session->mem_root= tmp_root;
3854
3876
 
3855
3877
        if (func->array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
3856
3878
          break;
3857
3879
 
3858
3880
        /* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
3859
3881
        uint32_t i=0;
3860
 
        do 
 
3882
        do
3861
3883
        {
3862
3884
          func->array->value_to_item(i, value_item);
3863
3885
          tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
3900
3922
                new_interval->min_flag= NEAR_MIN;
3901
3923
              }
3902
3924
            }
3903
 
            /* 
 
3925
            /*
3904
3926
              The following doesn't try to allocate memory so no need to
3905
3927
              check for NULL.
3906
3928
            */
3907
3929
            tree= tree_or(param, tree, tree2);
3908
3930
          }
3909
3931
        }
3910
 
        
 
3932
 
3911
3933
        if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
3912
3934
        {
3913
 
          /* 
3914
 
            Get the SEL_TREE for the last "c_last < X < +inf" interval 
 
3935
          /*
 
3936
            Get the SEL_TREE for the last "c_last < X < +inf" interval
3915
3937
            (value_item cotains c_last already)
3916
3938
          */
3917
3939
          tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
3930
3952
          for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
3931
3953
               arg < end ; arg++)
3932
3954
          {
3933
 
            tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field, 
 
3955
            tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field,
3934
3956
                                                        *arg, *arg, cmp_type));
3935
3957
          }
3936
3958
        }
3937
3959
      }
3938
3960
    }
3939
3961
    else
3940
 
    {    
 
3962
    {
3941
3963
      tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
3942
3964
                         func->arguments()[1], cmp_type);
3943
3965
      if (tree)
3946
3968
        for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
3947
3969
             arg < end ; arg++)
3948
3970
        {
3949
 
          tree= tree_or(param, tree, get_mm_parts(param, cond_func, field, 
 
3971
          tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
3950
3972
                                                  Item_func::EQ_FUNC,
3951
3973
                                                  *arg, cmp_type));
3952
3974
        }
3954
3976
    }
3955
3977
    break;
3956
3978
  }
3957
 
  default: 
 
3979
  default:
3958
3980
  {
3959
 
    /* 
 
3981
    /*
3960
3982
       Here the function for the following predicates are processed:
3961
3983
       <, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL.
3962
3984
       If the predicate is of the form (value op field) it is handled
3976
3998
 
3977
3999
/*
3978
4000
  Build conjunction of all SEL_TREEs for a simple predicate applying equalities
3979
 
 
 
4001
 
3980
4002
  SYNOPSIS
3981
4003
    get_full_func_mm_tree()
3982
4004
      param       PARAM from SQL_SELECT::test_quick_select
3984
4006
      field_item  field in the predicate
3985
4007
      value       constant in the predicate
3986
4008
                  (for BETWEEN it contains the number of the field argument,
3987
 
                   for IN it's always 0) 
 
4009
                   for IN it's always 0)
3988
4010
      inv         true <> NOT cond_func is considered
3989
4011
                  (makes sense only when cond_func is BETWEEN or IN)
3990
4012
 
3993
4015
    c is a constant, the function builds a conjunction of all SEL_TREES that can
3994
4016
    be obtained by the substitution of f for all different fields equal to f.
3995
4017
 
3996
 
  NOTES  
 
4018
  NOTES
3997
4019
    If the WHERE condition contains a predicate (fi op c),
3998
4020
    then not only SELL_TREE for this predicate is built, but
3999
4021
    the trees for the results of substitution of fi for
4000
4022
    each fj belonging to the same multiple equality as fi
4001
4023
    are built as well.
4002
 
    E.g. for WHERE t1.a=t2.a AND t2.a > 10 
 
4024
    E.g. for WHERE t1.a=t2.a AND t2.a > 10
4003
4025
    a SEL_TREE for t2.a > 10 will be built for quick select from t2
4004
 
    and   
 
4026
    and
4005
4027
    a SEL_TREE for t1.a > 10 will be built for quick select from t1.
4006
4028
 
4007
4029
    A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
4010
4032
    Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
4011
4033
    differently. It is considered as a conjuction of two SARGable
4012
4034
    predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
4013
 
    is called for each of them separately producing trees for 
4014
 
       AND j (f1j <=c ) and AND j (f2j <= c) 
 
4035
    is called for each of them separately producing trees for
 
4036
       AND j (f1j <=c ) and AND j (f2j <= c)
4015
4037
    After this these two trees are united in one conjunctive tree.
4016
4038
    It's easy to see that the same tree is obtained for
4017
4039
       AND j,k (f1j <=c AND f2k<=c)
4018
 
    which is equivalent to 
 
4040
    which is equivalent to
4019
4041
       AND j,k (c BETWEEN f1j AND f2k).
4020
4042
    The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
4021
4043
    which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
4022
4044
    function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
4023
4045
    producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
4024
 
    trees are united in one OR-tree. The expression 
 
4046
    trees are united in one OR-tree. The expression
4025
4047
      (AND j (f1j > c) OR AND j (f2j < c)
4026
4048
    is equivalent to the expression
4027
 
      AND j,k (f1j > c OR f2k < c) 
4028
 
    which is just a translation of 
 
4049
      AND j,k (f1j > c OR f2k < c)
 
4050
    which is just a translation of
4029
4051
      AND j,k (c NOT BETWEEN f1j AND f2k)
4030
4052
 
4031
4053
    In the cases when one of the items f1, f2 is a constant c1 we do not create
4038
4060
    As to IN predicates only ones of the form (f IN (c1,...,cn)),
4039
4061
    where f1 is a field and c1,...,cn are constant, are considered as
4040
4062
    SARGable. We never try to narrow the index scan using predicates of
4041
 
    the form (c IN (c1,...,f,...,cn)). 
4042
 
      
4043
 
  RETURN 
 
4063
    the form (c IN (c1,...,f,...,cn)).
 
4064
 
 
4065
  RETURN
4044
4066
    Pointer to the tree representing the built conjunction of SEL_TREEs
4045
4067
*/
4046
4068
 
4047
4069
static SEL_TREE *get_full_func_mm_tree(RANGE_OPT_PARAM *param,
4048
4070
                                       Item_func *cond_func,
4049
 
                                       Item_field *field_item, Item *value, 
 
4071
                                       Item_field *field_item, Item *value,
4050
4072
                                       bool inv)
4051
4073
{
4052
4074
  SEL_TREE *tree= 0;
4106
4128
      while ((item=li++))
4107
4129
      {
4108
4130
        SEL_TREE *new_tree=get_mm_tree(param,item);
4109
 
        if (param->thd->is_fatal_error || 
 
4131
        if (param->session->is_fatal_error ||
4110
4132
            param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
4111
4133
          return(0);    // out of memory
4112
4134
        tree=tree_and(param,tree,new_tree);
4137
4159
  if (cond->const_item())
4138
4160
  {
4139
4161
    /*
4140
 
      During the cond->val_int() evaluation we can come across a subselect 
4141
 
      item which may allocate memory on the thd->mem_root and assumes 
4142
 
      all the memory allocated has the same life span as the subselect 
 
4162
      During the cond->val_int() evaluation we can come across a subselect
 
4163
      item which may allocate memory on the session->mem_root and assumes
 
4164
      all the memory allocated has the same life span as the subselect
4143
4165
      item itself. So we have to restore the thread's mem_root here.
4144
4166
    */
4145
4167
    MEM_ROOT *tmp_root= param->mem_root;
4146
 
    param->thd->mem_root= param->old_root;
 
4168
    param->session->mem_root= param->old_root;
4147
4169
    tree= cond->val_int() ? new(tmp_root) SEL_TREE(SEL_TREE::ALWAYS) :
4148
4170
                            new(tmp_root) SEL_TREE(SEL_TREE::IMPOSSIBLE);
4149
 
    param->thd->mem_root= tmp_root;
 
4171
    param->session->mem_root= tmp_root;
4150
4172
    return(tree);
4151
4173
  }
4152
4174
 
4167
4189
      cond_func->functype() == Item_func::IN_FUNC)
4168
4190
    inv= ((Item_func_opt_neg *) cond_func)->negated;
4169
4191
  else if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
4170
 
    return(0);                         
 
4192
    return(0);
4171
4193
 
4172
4194
  param->cond= cond;
4173
4195
 
4188
4210
      if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
4189
4211
      {
4190
4212
        field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
4191
 
        SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func, 
 
4213
        SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func,
4192
4214
                                    field_item, (Item*)(intptr_t)i, inv);
4193
4215
        if (inv)
4194
4216
          tree= !tree ? tmp : tree_or(param, tree, tmp);
4195
 
        else 
 
4217
        else
4196
4218
          tree= tree_and(param, tree, tmp);
4197
4219
      }
4198
4220
      else if (inv)
4199
 
      { 
 
4221
      {
4200
4222
        tree= 0;
4201
4223
        break;
4202
4224
      }
4215
4237
  }
4216
4238
  case Item_func::MULT_EQUAL_FUNC:
4217
4239
  {
4218
 
    Item_equal *item_equal= (Item_equal *) cond;    
 
4240
    Item_equal *item_equal= (Item_equal *) cond;
4219
4241
    if (!(value= item_equal->get_const()))
4220
4242
      return(0);
4221
4243
    Item_equal_iterator it(*item_equal);
4231
4253
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
4232
4254
      }
4233
4255
    }
4234
 
    
 
4256
 
4235
4257
    return(ftree);
4236
4258
  }
4237
4259
  default:
4259
4281
static SEL_TREE *
4260
4282
get_mm_parts(RANGE_OPT_PARAM *param, COND *cond_func, Field *field,
4261
4283
             Item_func::Functype type,
4262
 
             Item *value,
4263
 
             Item_result cmp_type __attribute__((unused)))
 
4284
             Item *value, Item_result)
4264
4285
{
4265
4286
  if (field->table != param->table)
4266
4287
    return(0);
4301
4322
      tree->keys_map.set_bit(key_part->key);
4302
4323
    }
4303
4324
  }
4304
 
  
 
4325
 
4305
4326
  return(tree);
4306
4327
}
4307
4328
 
4324
4345
    the argument can be any, e.g. a subselect. The subselect
4325
4346
    items, in turn, assume that all the memory allocated during
4326
4347
    the evaluation has the same life span as the item itself.
4327
 
    TODO: opt_range.cc should not reset thd->mem_root at all.
 
4348
    TODO: opt_range.cc should not reset session->mem_root at all.
4328
4349
  */
4329
 
  param->thd->mem_root= param->old_root;
 
4350
  param->session->mem_root= param->old_root;
4330
4351
  if (!value)                                   // IS NULL or IS NOT NULL
4331
4352
  {
4332
4353
    if (field->table->maybe_null)               // Can't use a key on this
4468
4489
  /* For comparison purposes allow invalid dates like 2000-01-32 */
4469
4490
  orig_sql_mode= field->table->in_use->variables.sql_mode;
4470
4491
  if (value->real_item()->type() == Item::STRING_ITEM &&
4471
 
      (field->type() == DRIZZLE_TYPE_NEWDATE ||
 
4492
      (field->type() == DRIZZLE_TYPE_DATE ||
4472
4493
       field->type() == DRIZZLE_TYPE_DATETIME))
4473
4494
    field->table->in_use->variables.sql_mode|= MODE_INVALID_DATES;
4474
4495
  err= value->save_in_field_no_warnings(field, 1);
4491
4512
          for the cases like int_field > 999999999999999999999999 as well.
4492
4513
        */
4493
4514
        tree= 0;
4494
 
        if (err == 3 && field->type() == DRIZZLE_TYPE_NEWDATE &&
 
4515
        if (err == 3 && field->type() == DRIZZLE_TYPE_DATE &&
4495
4516
            (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC ||
4496
4517
             type == Item_func::LT_FUNC || type == Item_func::LE_FUNC) )
4497
4518
        {
4500
4521
            but a non-zero time part was cut off.
4501
4522
 
4502
4523
            In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
4503
 
            values. Index over a DATE column uses DATE comparison. Changing 
 
4524
            values. Index over a DATE column uses DATE comparison. Changing
4504
4525
            from one comparison to the other is possible:
4505
4526
 
4506
4527
            datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
4612
4633
  }
4613
4634
 
4614
4635
end:
4615
 
  param->thd->mem_root= alloc;
 
4636
  param->session->mem_root= alloc;
4616
4637
  return(tree);
4617
4638
}
4618
4639
 
4693
4714
  }
4694
4715
  key_map  result_keys;
4695
4716
  result_keys.clear_all();
4696
 
  
 
4717
 
4697
4718
  /* Join the trees key per key */
4698
4719
  SEL_ARG **key1,**key2,**end;
4699
4720
  for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
4714
4735
      }
4715
4736
      result_keys.set_bit(key1 - tree1->keys);
4716
4737
#ifdef EXTRA_DEBUG
4717
 
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
 
4738
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4718
4739
          (*key1)->test_use_count(*key1);
4719
4740
#endif
4720
4741
    }
4739
4760
  using index_merge.
4740
4761
*/
4741
4762
 
4742
 
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, 
 
4763
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
4743
4764
                           RANGE_OPT_PARAM* param)
4744
4765
{
4745
4766
  key_map common_keys= tree1->keys_map;
4771
4792
  SYNOPSIS
4772
4793
    param  Range analysis parameter
4773
4794
    tree   Tree to be processed, tree->type is KEY or KEY_SMALLER
4774
 
 
 
4795
 
4775
4796
  DESCRIPTION
4776
4797
    This function walks through tree->keys[] and removes the SEL_ARG* trees
4777
4798
    that are not "maybe" trees (*) and cannot be used to construct quick range
4783
4804
    tree->part != 0. (e.g. it could represent "keypart2 < const").
4784
4805
 
4785
4806
    WHY THIS FUNCTION IS NEEDED
4786
 
    
 
4807
 
4787
4808
    Normally we allow construction of SEL_TREE objects that have SEL_ARG
4788
4809
    trees that do not allow quick range select construction. For example for
4789
4810
    " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
4792
4813
                                               from this
4793
4814
    call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
4794
4815
                                   tree.
4795
 
    
 
4816
 
4796
4817
    There is an exception though: when we construct index_merge SEL_TREE,
4797
4818
    any SEL_ARG* tree that cannot be used to construct quick range select can
4798
4819
    be removed, because current range analysis code doesn't provide any way
4799
4820
    that tree could be later combined with another tree.
4800
4821
    Consider an example: we should not construct
4801
 
    st1 = SEL_TREE { 
4802
 
      merges = SEL_IMERGE { 
4803
 
                            SEL_TREE(t.key1part1 = 1), 
 
4822
    st1 = SEL_TREE {
 
4823
      merges = SEL_IMERGE {
 
4824
                            SEL_TREE(t.key1part1 = 1),
4804
4825
                            SEL_TREE(t.key2part2 = 2)   -- (*)
4805
 
                          } 
 
4826
                          }
4806
4827
                   };
4807
 
    because 
4808
 
     - (*) cannot be used to construct quick range select, 
4809
 
     - There is no execution path that would cause (*) to be converted to 
 
4828
    because
 
4829
     - (*) cannot be used to construct quick range select,
 
4830
     - There is no execution path that would cause (*) to be converted to
4810
4831
       a tree that could be used.
4811
4832
 
4812
4833
    The latter is easy to verify: first, notice that the only way to convert
4813
4834
    (*) into a usable tree is to call tree_and(something, (*)).
4814
4835
 
4815
4836
    Second look at what tree_and/tree_or function would do when passed a
4816
 
    SEL_TREE that has the structure like st1 tree has, and conlcude that 
 
4837
    SEL_TREE that has the structure like st1 tree has, and conlcude that
4817
4838
    tree_and(something, (*)) will not be called.
4818
4839
 
4819
4840
  RETURN
4871
4892
        result=tree1;                           // Added to tree1
4872
4893
        result_keys.set_bit(key1 - tree1->keys);
4873
4894
#ifdef EXTRA_DEBUG
4874
 
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
 
4895
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4875
4896
          (*key1)->test_use_count(*key1);
4876
4897
#endif
4877
4898
      }
4913
4934
      /* one tree is index merge tree and another is range tree */
4914
4935
      if (tree1->merges.is_empty())
4915
4936
        std::swap(tree1, tree2);
4916
 
      
 
4937
 
4917
4938
      if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
4918
4939
         return(new SEL_TREE(SEL_TREE::ALWAYS));
4919
4940
      /* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
4930
4951
/* And key trees where key1->part < key2 -> part */
4931
4952
 
4932
4953
static SEL_ARG *
4933
 
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, 
 
4954
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
4934
4955
             uint32_t clone_flag)
4935
4956
{
4936
4957
  SEL_ARG *next;
5030
5051
    }
5031
5052
    if (key1->type == SEL_ARG::MAYBE_KEY)
5032
5053
    {                                           // Both are maybe key
5033
 
      key1->next_key_part=key_and(param, key1->next_key_part, 
 
5054
      key1->next_key_part=key_and(param, key1->next_key_part,
5034
5055
                                  key2->next_key_part, clone_flag);
5035
5056
      if (key1->next_key_part &&
5036
5057
          key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
5537
5558
    return(0);                          // Maybe root later
5538
5559
  if (remove_color == BLACK)
5539
5560
    root=rb_delete_fixup(root,nod,fix_par);
 
5561
#ifdef EXTRA_DEBUG
5540
5562
  test_rb_tree(root,root->parent);
 
5563
#endif /* EXTRA_DEBUG */
5541
5564
 
5542
5565
  root->use_count=this->use_count;              // Fix root counters
5543
5566
  root->elements=this->elements-1;
5634
5657
    }
5635
5658
  }
5636
5659
  root->color=BLACK;
 
5660
#ifdef EXTRA_DEBUG
5637
5661
  test_rb_tree(root,root->parent);
 
5662
#endif /* EXTRA_DEBUG */
 
5663
 
5638
5664
  return root;
5639
5665
}
5640
5666
 
5759
5785
 
5760
5786
/*
5761
5787
  Count how many times SEL_ARG graph "root" refers to its part "key"
5762
 
  
 
5788
 
5763
5789
  SYNOPSIS
5764
5790
    count_key_part_usage()
5765
5791
      root  An RB-Root node in a SEL_ARG graph.
5770
5796
    root->next->n
5771
5797
 
5772
5798
    This function counts how many times the node "key" is referred (via
5773
 
    SEL_ARG::next_key_part) by 
5774
 
     - intervals of RB-tree pointed by "root", 
5775
 
     - intervals of RB-trees that are pointed by SEL_ARG::next_key_part from 
 
5799
    SEL_ARG::next_key_part) by
 
5800
     - intervals of RB-tree pointed by "root",
 
5801
     - intervals of RB-trees that are pointed by SEL_ARG::next_key_part from
5776
5802
       intervals of RB-tree pointed by "root",
5777
5803
     - and so on.
5778
 
    
5779
 
    Here is an example (horizontal links represent next_key_part pointers, 
5780
 
    vertical links - next/prev prev pointers):  
5781
 
    
 
5804
 
 
5805
    Here is an example (horizontal links represent next_key_part pointers,
 
5806
    vertical links - next/prev prev pointers):
 
5807
 
5782
5808
         +----+               $
5783
5809
         |root|-----------------+
5784
5810
         +----+               $ |
5797
5823
          ...     +---+       $    |
5798
5824
                  |   |------------+
5799
5825
                  +---+       $
5800
 
  RETURN 
 
5826
  RETURN
5801
5827
    Number of links to "key" from nodes reachable from "root".
5802
5828
*/
5803
5829
 
5870
5896
 ****************************************************************************/
5871
5897
 
5872
5898
/* MRR range sequence, SEL_ARG* implementation: stack entry */
5873
 
typedef struct st_range_seq_entry 
 
5899
typedef struct st_range_seq_entry
5874
5900
{
5875
 
  /* 
 
5901
  /*
5876
5902
    Pointers in min and max keys. They point to right-after-end of key
5877
5903
    images. The 0-th entry has these pointing to key tuple start.
5878
5904
  */
5879
5905
  unsigned char *min_key, *max_key;
5880
 
  
5881
 
  /* 
 
5906
 
 
5907
  /*
5882
5908
    Flags, for {keypart0, keypart1, ... this_keypart} subtuple.
5883
5909
    min_key_flag may have NULL_RANGE set.
5884
5910
  */
5885
5911
  uint32_t min_key_flag, max_key_flag;
5886
 
  
 
5912
 
5887
5913
  /* Number of key parts */
5888
5914
  uint32_t min_key_parts, max_key_parts;
5889
5915
  SEL_ARG *key_tree;
5899
5925
  uint32_t real_keyno; /* Number of the index in tables */
5900
5926
  PARAM *param;
5901
5927
  SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
5902
 
  
 
5928
 
5903
5929
  RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];
5904
5930
  int i; /* Index of last used element in the above array */
5905
 
  
 
5931
 
5906
5932
  bool at_start; /* true <=> The traversal has just started */
5907
5933
} SEL_ARG_RANGE_SEQ;
5908
5934
 
5913
5939
  SYNOPSIS
5914
5940
    init()
5915
5941
      init_params  SEL_ARG tree traversal context
5916
 
      n_ranges     [ignored] The number of ranges obtained 
 
5942
      n_ranges     [ignored] The number of ranges obtained
5917
5943
      flags        [ignored] HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
5918
5944
 
5919
5945
  RETURN
5920
5946
    Value of init_param
5921
5947
*/
5922
5948
 
5923
 
range_seq_t sel_arg_range_seq_init(void *init_param,
5924
 
                                   uint32_t n_ranges __attribute__((unused)),
5925
 
                                   uint32_t flags __attribute__((unused)))
 
5949
range_seq_t sel_arg_range_seq_init(void *init_param, uint32_t, uint32_t)
5926
5950
{
5927
5951
  SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)init_param;
5928
5952
  seq->at_start= true;
5943
5967
{
5944
5968
  RANGE_SEQ_ENTRY *cur= &arg->stack[arg->i+1];
5945
5969
  RANGE_SEQ_ENTRY *prev= &arg->stack[arg->i];
5946
 
  
 
5970
 
5947
5971
  cur->key_tree= key_tree;
5948
5972
  cur->min_key= prev->min_key;
5949
5973
  cur->max_key= prev->max_key;
5967
5991
 
5968
5992
/*
5969
5993
  Range sequence interface, SEL_ARG* implementation: get the next interval
5970
 
  
 
5994
 
5971
5995
  SYNOPSIS
5972
5996
    sel_arg_range_seq_next()
5973
5997
      rseq        Value returned from sel_arg_range_seq_init
6002
6026
 
6003
6027
  key_tree= seq->stack[seq->i].key_tree;
6004
6028
  /* Ok, we're at some "full tuple" position in the tree */
6005
 
 
 
6029
 
6006
6030
  /* Step down if we can */
6007
6031
  if (key_tree->next && key_tree->next != &null_element)
6008
6032
  {
6039
6063
    Walk right-up while we can
6040
6064
  */
6041
6065
walk_right_n_up:
6042
 
  while (key_tree->next_key_part && key_tree->next_key_part != &null_element && 
 
6066
  while (key_tree->next_key_part && key_tree->next_key_part != &null_element &&
6043
6067
         key_tree->next_key_part->part == key_tree->part + 1 &&
6044
6068
         key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
6045
6069
  {
6054
6078
      {
6055
6079
        seq->param->is_ror_scan= false;
6056
6080
        if (!key_tree->min_flag)
6057
 
          cur->min_key_parts += 
 
6081
          cur->min_key_parts +=
6058
6082
            key_tree->next_key_part->store_min_key(seq->param->key[seq->keyno],
6059
6083
                                                   &cur->min_key,
6060
6084
                                                   &cur->min_key_flag);
6061
6085
        if (!key_tree->max_flag)
6062
 
          cur->max_key_parts += 
 
6086
          cur->max_key_parts +=
6063
6087
            key_tree->next_key_part->store_max_key(seq->param->key[seq->keyno],
6064
6088
                                                   &cur->max_key,
6065
6089
                                                   &cur->max_key_flag);
6066
6090
        break;
6067
6091
      }
6068
6092
    }
6069
 
  
 
6093
 
6070
6094
    /*
6071
6095
      Ok, current atomic interval is in form "t.field=const" and there is
6072
6096
      next_key_part interval. Step right, and walk up from there.
6084
6108
 
6085
6109
  /* Ok got a tuple */
6086
6110
  RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
6087
 
  
 
6111
 
6088
6112
  range->ptr= (char*)(int)(key_tree->part);
6089
6113
  {
6090
6114
    range->range_flag= cur->min_key_flag | cur->max_key_flag;
6091
 
    
 
6115
 
6092
6116
    range->start_key.key=    seq->param->min_key;
6093
6117
    range->start_key.length= cur->min_key - seq->param->min_key;
6094
6118
    range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
6095
 
    range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY : 
 
6119
    range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
6096
6120
                                                           HA_READ_KEY_EXACT);
6097
6121
 
6098
6122
    range->end_key.key=    seq->param->max_key;
6099
6123
    range->end_key.length= cur->max_key - seq->param->max_key;
6100
 
    range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY : 
 
6124
    range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
6101
6125
                                                         HA_READ_AFTER_KEY);
6102
6126
    range->end_key.keypart_map= make_prev_keypart_map(cur->max_key_parts);
6103
6127
 
6108
6132
        range->start_key.length == range->end_key.length &&
6109
6133
        !memcmp(seq->param->min_key,seq->param->max_key,range->start_key.length))
6110
6134
      range->range_flag= UNIQUE_RANGE | (cur->min_key_flag & NULL_RANGE);
6111
 
      
 
6135
 
6112
6136
    if (seq->param->is_ror_scan)
6113
6137
    {
6114
6138
      /*
6134
6158
 
6135
6159
 
6136
6160
/*
6137
 
  Calculate cost and E(#rows) for a given index and intervals tree 
 
6161
  Calculate cost and E(#rows) for a given index and intervals tree
6138
6162
 
6139
6163
  SYNOPSIS
6140
6164
    check_quick_select()
6162
6186
 
6163
6187
static
6164
6188
ha_rows check_quick_select(PARAM *param, uint32_t idx, bool index_only,
6165
 
                           SEL_ARG *tree, bool update_tbl_stats, 
 
6189
                           SEL_ARG *tree, bool update_tbl_stats,
6166
6190
                           uint32_t *mrr_flags, uint32_t *bufsize, COST_VECT *cost)
6167
6191
{
6168
6192
  SEL_ARG_RANGE_SEQ seq;
6170
6194
  handler *file= param->table->file;
6171
6195
  ha_rows rows;
6172
6196
  uint32_t keynr= param->real_keynr[idx];
6173
 
  
 
6197
 
6174
6198
  /* Handle cases when we don't have a valid non-empty list of range */
6175
6199
  if (!tree)
6176
6200
    return(HA_POS_ERROR);
6190
6214
  param->is_ror_scan= true;
6191
6215
  if (file->index_flags(keynr, 0, true) & HA_KEY_SCAN_NOT_ROR)
6192
6216
    param->is_ror_scan= false;
6193
 
  
 
6217
 
6194
6218
  *mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
6195
6219
  *mrr_flags|= HA_MRR_NO_ASSOCIATION;
6196
6220
 
6197
6221
  bool pk_is_clustered= file->primary_key_is_clustered();
6198
 
  if (index_only && 
 
6222
  if (index_only &&
6199
6223
      (file->index_flags(keynr, param->max_key_part, 1) & HA_KEYREAD_ONLY) &&
6200
6224
      !(pk_is_clustered && keynr == param->table->s->primary_key))
6201
6225
     *mrr_flags |= HA_MRR_INDEX_ONLY;
6202
 
  
6203
 
  if (current_thd->lex->sql_command != SQLCOM_SELECT)
 
6226
 
 
6227
  if (current_session->lex->sql_command != SQLCOM_SELECT)
6204
6228
    *mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
6205
6229
 
6206
 
  *bufsize= param->thd->variables.read_rnd_buff_size;
 
6230
  *bufsize= param->session->variables.read_rnd_buff_size;
6207
6231
  rows= file->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
6208
6232
                                          bufsize, mrr_flags, cost);
6209
6233
  if (rows != HA_POS_ERROR)
6222
6246
  enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
6223
6247
  if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
6224
6248
  {
6225
 
    /* 
 
6249
    /*
6226
6250
      All scans are non-ROR scans for those index types.
6227
 
      TODO: Don't have this logic here, make table engines return 
 
6251
      TODO: Don't have this logic here, make table engines return
6228
6252
      appropriate flags instead.
6229
6253
    */
6230
6254
    param->is_ror_scan= false;
6263
6287
 
6264
6288
    where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])
6265
6289
 
6266
 
    and the table has a clustered Primary Key defined as 
6267
 
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k) 
6268
 
    
6269
 
    i.e. the first key parts of it are identical to uncovered parts ot the 
 
6290
    and the table has a clustered Primary Key defined as
 
6291
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k)
 
6292
 
 
6293
    i.e. the first key parts of it are identical to uncovered parts ot the
6270
6294
    key being scanned. This function assumes that the index flags do not
6271
6295
    include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
6272
6296
 
6284
6308
  KEY_PART_INFO *key_part_end= (table_key->key_part +
6285
6309
                                table_key->key_parts);
6286
6310
  uint32_t pk_number;
6287
 
  
 
6311
 
6288
6312
  for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
6289
6313
  {
6290
6314
    uint16_t fieldnr= param->table->key_info[keynr].
6330
6354
  NOTES
6331
6355
    The caller must call QUICK_SELECT::init for returned quick select.
6332
6356
 
6333
 
    CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
 
6357
    CAUTION! This function may change session->mem_root to a MEM_ROOT which will be
6334
6358
    deallocated when the returned quick select is deleted.
6335
6359
 
6336
6360
  RETURN
6345
6369
  QUICK_RANGE_SELECT *quick;
6346
6370
  bool create_err= false;
6347
6371
 
6348
 
  quick=new QUICK_RANGE_SELECT(param->thd, param->table,
 
6372
  quick=new QUICK_RANGE_SELECT(param->session, param->table,
6349
6373
                               param->real_keynr[idx],
6350
6374
                               test(parent_alloc), NULL, &create_err);
6351
6375
 
6513
6537
  Return true if any part of the key is NULL
6514
6538
 
6515
6539
  SYNOPSIS
6516
 
    null_part_in_key()    
 
6540
    null_part_in_key()
6517
6541
      key_part  Array of key parts (index description)
6518
6542
      key       Key values tuple
6519
6543
      length    Length of key values tuple in bytes.
6583
6607
 
6584
6608
  SYNOPSIS
6585
6609
    get_quick_select_for_ref()
6586
 
      thd      Thread handle
 
6610
      session      Thread handle
6587
6611
      table    Table to access
6588
6612
      ref      ref[_or_null] scan parameters
6589
6613
      records  Estimate of number of records (needed only to construct
6597
6621
    NULL on error.
6598
6622
*/
6599
6623
 
6600
 
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
 
6624
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
6601
6625
                                             TABLE_REF *ref, ha_rows records)
6602
6626
{
6603
6627
  MEM_ROOT *old_root, *alloc;
6609
6633
  bool create_err= false;
6610
6634
  COST_VECT cost;
6611
6635
 
6612
 
  old_root= thd->mem_root;
6613
 
  /* The following call may change thd->mem_root */
6614
 
  quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0, 0, &create_err);
 
6636
  old_root= session->mem_root;
 
6637
  /* The following call may change session->mem_root */
 
6638
  quick= new QUICK_RANGE_SELECT(session, table, ref->key, 0, 0, &create_err);
6615
6639
  /* save mem_root set by QUICK_RANGE_SELECT constructor */
6616
 
  alloc= thd->mem_root;
 
6640
  alloc= session->mem_root;
6617
6641
  /*
6618
 
    return back default mem_root (thd->mem_root) changed by
 
6642
    return back default mem_root (session->mem_root) changed by
6619
6643
    QUICK_RANGE_SELECT constructor
6620
6644
  */
6621
 
  thd->mem_root= old_root;
 
6645
  session->mem_root= old_root;
6622
6646
 
6623
6647
  if (!quick || create_err)
6624
6648
    return 0;                   /* no ranges found */
6626
6650
    goto err;
6627
6651
  quick->records= records;
6628
6652
 
6629
 
  if ((cp_buffer_from_ref(thd, ref) && thd->is_fatal_error) ||
 
6653
  if ((cp_buffer_from_ref(session, ref) && session->is_fatal_error) ||
6630
6654
      !(range= new(alloc) QUICK_RANGE()))
6631
6655
    goto err;                                   // out of memory
6632
6656
 
6676
6700
  }
6677
6701
 
6678
6702
  /* Call multi_range_read_info() to get the MRR flags and buffer size */
6679
 
  quick->mrr_flags= HA_MRR_NO_ASSOCIATION | 
 
6703
  quick->mrr_flags= HA_MRR_NO_ASSOCIATION |
6680
6704
                    (table->key_read ? HA_MRR_INDEX_ONLY : 0);
6681
 
  if (thd->lex->sql_command != SQLCOM_SELECT)
 
6705
  if (session->lex->sql_command != SQLCOM_SELECT)
6682
6706
    quick->mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
6683
6707
 
6684
 
  quick->mrr_buf_size= thd->variables.read_rnd_buff_size;
 
6708
  quick->mrr_buf_size= session->variables.read_rnd_buff_size;
6685
6709
  if (table->file->multi_range_read_info(quick->index, 1, (uint)records,
6686
6710
                                         &quick->mrr_buf_size,
6687
6711
                                         &quick->mrr_flags, &cost))
6695
6719
 
6696
6720
 
6697
6721
/*
6698
 
  Perform key scans for all used indexes (except CPK), get rowids and merge 
 
6722
  Perform key scans for all used indexes (except CPK), get rowids and merge
6699
6723
  them into an ordered non-recurrent sequence of rowids.
6700
 
  
 
6724
 
6701
6725
  The merge/duplicate removal is performed using Unique class. We put all
6702
6726
  rowids into Unique, get the sorted sequence and destroy the Unique.
6703
 
  
 
6727
 
6704
6728
  If table has a clustered primary key that covers all rows (true for bdb
6705
6729
  and innodb currently) and one of the index_merge scans is a scan on PK,
6706
 
  then rows that will be retrieved by PK scan are not put into Unique and 
 
6730
  then rows that will be retrieved by PK scan are not put into Unique and
6707
6731
  primary key scan is not performed here, it is performed later separately.
6708
6732
 
6709
6733
  RETURN
6725
6749
  cur_quick_it.rewind();
6726
6750
  cur_quick= cur_quick_it++;
6727
6751
  assert(cur_quick != 0);
6728
 
  
 
6752
 
6729
6753
  /*
6730
 
    We reuse the same instance of handler so we need to call both init and 
 
6754
    We reuse the same instance of handler so we need to call both init and
6731
6755
    reset here.
6732
6756
  */
6733
6757
  if (cur_quick->init() || cur_quick->reset())
6735
6759
 
6736
6760
  unique= new Unique(refpos_order_cmp, (void *)file,
6737
6761
                     file->ref_length,
6738
 
                     thd->variables.sortbuff_size);
 
6762
                     session->variables.sortbuff_size);
6739
6763
  if (!unique)
6740
6764
    return(1);
6741
6765
  for (;;)
6747
6771
      if (!cur_quick)
6748
6772
        break;
6749
6773
 
6750
 
      if (cur_quick->file->inited != handler::NONE) 
 
6774
      if (cur_quick->file->inited != handler::NONE)
6751
6775
        cur_quick->file->ha_index_end();
6752
6776
      if (cur_quick->init() || cur_quick->reset())
6753
6777
        return(1);
6763
6787
      break;
6764
6788
    }
6765
6789
 
6766
 
    if (thd->killed)
 
6790
    if (session->killed)
6767
6791
      return(1);
6768
6792
 
6769
6793
    /* skip row if it will be retrieved by clustered PK scan */
6784
6808
  /* index_merge currently doesn't support "using index" at all */
6785
6809
  file->extra(HA_EXTRA_NO_KEYREAD);
6786
6810
  /* start table scan */
6787
 
  init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1);
 
6811
  init_read_record(&read_record, session, head, (SQL_SELECT*) 0, 1, 1);
6788
6812
  return(result);
6789
6813
}
6790
6814
 
6990
7014
 
6991
7015
  if (file->inited == handler::NONE && (error= file->ha_index_init(index,1)))
6992
7016
    return(error);
6993
 
 
 
7017
 
6994
7018
  /* Allocate buffer if we need one but haven't allocated it yet */
6995
7019
  if (mrr_buf_size && !mrr_buf_desc)
6996
7020
  {
7014
7038
 
7015
7039
  if (!mrr_buf_desc)
7016
7040
    empty_buf.buffer= empty_buf.buffer_end= empty_buf.end_of_used_area= NULL;
7017
 
 
 
7041
 
7018
7042
  if (sorted)
7019
7043
     mrr_flags |= HA_MRR_SORTED;
7020
7044
  RANGE_SEQ_IF seq_funcs= {quick_range_seq_init, quick_range_seq_next};
7021
7045
  error= file->multi_range_read_init(&seq_funcs, (void*)this, ranges.elements,
7022
 
                                     mrr_flags, mrr_buf_desc? mrr_buf_desc: 
 
7046
                                     mrr_flags, mrr_buf_desc? mrr_buf_desc:
7023
7047
                                                              &empty_buf);
7024
7048
  return(error);
7025
7049
}
7027
7051
 
7028
7052
/*
7029
7053
  Range sequence interface implementation for array<QUICK_RANGE>: initialize
7030
 
  
 
7054
 
7031
7055
  SYNOPSIS
7032
7056
    quick_range_seq_init()
7033
7057
      init_param  Caller-opaque paramenter: QUICK_RANGE_SELECT* pointer
7034
7058
      n_ranges    Number of ranges in the sequence (ignored)
7035
 
      flags       MRR flags (currently not used) 
 
7059
      flags       MRR flags (currently not used)
7036
7060
 
7037
7061
  RETURN
7038
7062
    Opaque value to be passed to quick_range_seq_next
7039
7063
*/
7040
7064
 
7041
 
range_seq_t quick_range_seq_init(void *init_param,
7042
 
                                 uint32_t n_ranges __attribute__((unused)),
7043
 
                                 uint32_t flags __attribute__((unused)))
 
7065
range_seq_t quick_range_seq_init(void *init_param, uint32_t, uint32_t)
7044
7066
{
7045
7067
  QUICK_RANGE_SELECT *quick= (QUICK_RANGE_SELECT*)init_param;
7046
7068
  quick->qr_traversal_ctx.first=  (QUICK_RANGE**)quick->ranges.buffer;
7053
7075
 
7054
7076
/*
7055
7077
  Range sequence interface implementation for array<QUICK_RANGE>: get next
7056
 
  
 
7078
 
7057
7079
  SYNOPSIS
7058
7080
    quick_range_seq_next()
7059
7081
      rseq        Value returned from quick_range_seq_init
7109
7131
    function returns a reference to the "range_flag" associated with the
7110
7132
    range number idx.
7111
7133
 
7112
 
    This function should be removed when we get a proper MRR/NDB 
 
7134
    This function should be removed when we get a proper MRR/NDB
7113
7135
    implementation.
7114
7136
 
7115
7137
  RETURN
7148
7170
    Reference to range-associated data
7149
7171
*/
7150
7172
 
7151
 
char* &mrr_get_ptr_by_idx(range_seq_t seq __attribute__((unused)),
7152
 
                          uint32_t idx __attribute__((unused)))
 
7173
char* &mrr_get_ptr_by_idx(range_seq_t, uint32_t)
7153
7174
{
7154
7175
  static char *dummy;
7155
7176
  return dummy;
7329
7350
  for now, this seems to work right at least.
7330
7351
 */
7331
7352
 
7332
 
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
7333
 
                                     uint32_t used_key_parts_arg __attribute__((unused)),
7334
 
                                     bool *create_err __attribute__((unused)))
 
7353
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t, bool *)
7335
7354
 :QUICK_RANGE_SELECT(*q), rev_it(rev_ranges)
7336
7355
{
7337
7356
  QUICK_RANGE *r;
7436
7455
/*
7437
7456
  Compare if found key is over max-value
7438
7457
  Returns 0 if key <= range->max_key
7439
 
  TODO: Figure out why can't this function be as simple as cmp_prev(). 
 
7458
  TODO: Figure out why can't this function be as simple as cmp_prev().
7440
7459
*/
7441
7460
 
7442
7461
int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
7686
7705
static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
7687
7706
                       KEY_PART_INFO *first_non_group_part,
7688
7707
                       KEY_PART_INFO *min_max_arg_part,
7689
 
                       KEY_PART_INFO *last_part, THD *thd,
 
7708
                       KEY_PART_INFO *last_part, Session *session,
7690
7709
                       unsigned char *key_infix, uint32_t *key_infix_len,
7691
7710
                       KEY_PART_INFO **first_non_infix_part);
7692
7711
static bool
7729
7748
        - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
7730
7749
          GROUP BY and not referenced by MIN/MAX functions.
7731
7750
        with the following properties specified below.
7732
 
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not 
 
7751
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not
7733
7752
        applicable.
7734
7753
 
7735
7754
    SA1. There is at most one attribute in SA referenced by any number of
7817
7836
    other field as in: "select min(a) from t1 group by a" ?
7818
7837
  - We assume that the general correctness of the GROUP-BY query was checked
7819
7838
    before this point. Is this correct, or do we have to check it completely?
7820
 
  - Lift the limitation in condition (B3), that is, make this access method 
 
7839
  - Lift the limitation in condition (B3), that is, make this access method
7821
7840
    applicable to ROLLUP queries.
7822
7841
 
7823
7842
  RETURN
7832
7851
static TRP_GROUP_MIN_MAX *
7833
7852
get_best_group_min_max(PARAM *param, SEL_TREE *tree)
7834
7853
{
7835
 
  THD *thd= param->thd;
7836
 
  JOIN *join= thd->lex->current_select->join;
 
7854
  Session *session= param->session;
 
7855
  JOIN *join= session->lex->current_select->join;
7837
7856
  Table *table= param->table;
7838
7857
  bool have_min= false;              /* true if there is a MIN function. */
7839
7858
  bool have_max= false;              /* true if there is a MAX function. */
7883
7902
        return(NULL);
7884
7903
 
7885
7904
      /* The argument of MIN/MAX. */
7886
 
      Item *expr= min_max_item->args[0]->real_item();    
 
7905
      Item *expr= min_max_item->args[0]->real_item();
7887
7906
      if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
7888
7907
      {
7889
7908
        if (! min_max_arg_item)
8094
8113
                                                        &dummy);
8095
8114
        if (!get_constant_key_infix(cur_index_info, index_range_tree,
8096
8115
                                    first_non_group_part, min_max_arg_part,
8097
 
                                    last_part, thd, key_infix, &key_infix_len,
 
8116
                                    last_part, session, key_infix, &key_infix_len,
8098
8117
                                    &first_non_infix_part))
8099
8118
          goto next_index;
8100
8119
      }
8162
8181
      COST_VECT dummy_cost;
8163
8182
      uint32_t mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
8164
8183
      uint32_t mrr_bufsize=0;
8165
 
      cur_quick_prefix_records= check_quick_select(param, cur_param_idx, 
8166
 
                                                   false /*don't care*/, 
 
8184
      cur_quick_prefix_records= check_quick_select(param, cur_param_idx,
 
8185
                                                   false /*don't care*/,
8167
8186
                                                   cur_index_tree, true,
8168
8187
                                                   &mrr_flags, &mrr_bufsize,
8169
8188
                                                   &dummy_cost);
8279
8298
  */
8280
8299
  if (cond_type == Item::SUBSELECT_ITEM)
8281
8300
    return(false);
8282
 
  
 
8301
 
8283
8302
  /* We presume that at this point there are no other Items than functions. */
8284
8303
  assert(cond_type == Item::FUNC_ITEM);
8285
8304
 
8292
8311
    cur_arg= arguments[arg_idx]->real_item();
8293
8312
    if (cur_arg->type() == Item::FIELD_ITEM)
8294
8313
    {
8295
 
      if (min_max_arg_item->eq(cur_arg, 1)) 
 
8314
      if (min_max_arg_item->eq(cur_arg, 1))
8296
8315
      {
8297
8316
       /*
8298
8317
         If pred references the MIN/MAX argument, check whether pred is a range
8367
8386
    first_non_group_part   [in]  First index part after group attribute parts
8368
8387
    min_max_arg_part       [in]  The keypart of the MIN/MAX argument if any
8369
8388
    last_part              [in]  Last keypart of the index
8370
 
    thd                    [in]  Current thread
 
8389
    session                    [in]  Current thread
8371
8390
    key_infix              [out] Infix of constants to be used for index lookup
8372
8391
    key_infix_len          [out] Lenghth of the infix
8373
8392
    first_non_infix_part   [out] The first keypart after the infix (if any)
8374
 
    
 
8393
 
8375
8394
  DESCRIPTION
8376
8395
    Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
8377
8396
    for each keypart field NGF_i not in GROUP-BY, check that there is a
8389
8408
*/
8390
8409
 
8391
8410
static bool
8392
 
get_constant_key_infix(KEY *index_info __attribute__((unused)),
8393
 
                       SEL_ARG *index_range_tree,
 
8411
get_constant_key_infix(KEY *, SEL_ARG *index_range_tree,
8394
8412
                       KEY_PART_INFO *first_non_group_part,
8395
8413
                       KEY_PART_INFO *min_max_arg_part,
8396
8414
                       KEY_PART_INFO *last_part,
8397
 
                       THD *thd __attribute__((unused)),
8398
 
                       unsigned char *key_infix, uint32_t *key_infix_len,
 
8415
                       Session *, unsigned char *key_infix, uint32_t *key_infix_len,
8399
8416
                       KEY_PART_INFO **first_non_infix_part)
8400
8417
{
8401
8418
  SEL_ARG       *cur_range;
8588
8605
 
8589
8606
void cost_group_min_max(Table* table, KEY *index_info, uint32_t used_key_parts,
8590
8607
                        uint32_t group_key_parts, SEL_TREE *range_tree,
8591
 
                        SEL_ARG *index_tree __attribute__((unused)),
8592
 
                        ha_rows quick_prefix_records,
 
8608
                        SEL_ARG *, ha_rows quick_prefix_records,
8593
8609
                        bool have_min, bool have_max,
8594
8610
                        double *read_cost, ha_rows *records)
8595
8611
{
8684
8700
*/
8685
8701
 
8686
8702
QUICK_SELECT_I *
8687
 
TRP_GROUP_MIN_MAX::make_quick(PARAM *param,
8688
 
                              bool retrieve_full_rows __attribute__((unused)),
8689
 
                              MEM_ROOT *parent_alloc)
 
8703
TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool, MEM_ROOT *parent_alloc)
8690
8704
{
8691
8705
  QUICK_GROUP_MIN_MAX_SELECT *quick;
8692
8706
 
8693
8707
  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
8694
 
                                        param->thd->lex->current_select->join,
 
8708
                                        param->session->lex->current_select->join,
8695
8709
                                        have_min, have_max, min_max_arg_part,
8696
8710
                                        group_prefix_len, group_key_parts,
8697
8711
                                        used_key_parts, index_info, index,
8819
8833
  assert(!parent_alloc);
8820
8834
  if (!parent_alloc)
8821
8835
  {
8822
 
    init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
8823
 
    join->thd->mem_root= &alloc;
 
8836
    init_sql_alloc(&alloc, join->session->variables.range_alloc_block_size, 0);
 
8837
    join->session->mem_root= &alloc;
8824
8838
  }
8825
8839
  else
8826
8840
    memset(&alloc, 0, sizeof(MEM_ROOT));  // ensure that it's not used
8832
8846
 
8833
8847
  SYNOPSIS
8834
8848
    QUICK_GROUP_MIN_MAX_SELECT::init()
8835
 
  
 
8849
 
8836
8850
  DESCRIPTION
8837
8851
    The method performs initialization that cannot be done in the constructor
8838
8852
    such as memory allocations that may fail. It allocates memory for the
8923
8937
 
8924
8938
QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
8925
8939
{
8926
 
  if (file->inited != handler::NONE) 
 
8940
  if (file->inited != handler::NONE)
8927
8941
    file->ha_index_end();
8928
8942
  if (min_max_arg_part)
8929
8943
    delete_dynamic(&min_max_ranges);
8931
8945
  delete min_functions_it;
8932
8946
  delete max_functions_it;
8933
8947
  delete quick_prefix_select;
8934
 
  return; 
 
8948
  return;
8935
8949
}
8936
8950
 
8937
8951
 
8940
8954
 
8941
8955
  SYNOPSIS
8942
8956
    QUICK_GROUP_MIN_MAX_SELECT::add_range()
8943
 
    sel_range  Range object from which a 
 
8957
    sel_range  Range object from which a
8944
8958
 
8945
8959
  NOTES
8946
8960
    Construct a new QUICK_RANGE object from a SEL_ARG object, and
8994
9008
 
8995
9009
  NOTES
8996
9010
    quick_prefix_select is made over the conditions on the whole key.
8997
 
    It defines a number of ranges of length x. 
8998
 
    However when jumping through the prefixes we use only the the first 
 
9011
    It defines a number of ranges of length x.
 
9012
    However when jumping through the prefixes we use only the the first
8999
9013
    few most significant keyparts in the range key. However if there
9000
 
    are more keyparts to follow the ones we are using we must make the 
9001
 
    condition on the key inclusive (because x < "ab" means 
 
9014
    are more keyparts to follow the ones we are using we must make the
 
9015
    condition on the key inclusive (because x < "ab" means
9002
9016
    x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
9003
9017
    To achive the above we must turn off the NEAR_MIN/NEAR_MAX
9004
9018
*/
9122
9136
 
9123
9137
 
9124
9138
 
9125
 
/* 
 
9139
/*
9126
9140
  Get the next key containing the MIN and/or MAX key for the next group.
9127
9141
 
9128
9142
  SYNOPSIS
9173
9187
                              group_prefix_len);
9174
9188
      assert(is_last_prefix <= 0);
9175
9189
    }
9176
 
    else 
 
9190
    else
9177
9191
    {
9178
9192
      if (result == HA_ERR_KEY_NOT_FOUND)
9179
9193
        continue;
9314
9328
}
9315
9329
 
9316
9330
 
9317
 
/* 
 
9331
/*
9318
9332
  Retrieve the maximal key in the next group.
9319
9333
 
9320
9334
  SYNOPSIS
9437
9451
  QUICK_RANGE *cur_range;
9438
9452
  bool found_null= false;
9439
9453
  int result= HA_ERR_KEY_NOT_FOUND;
 
9454
  basic_string<unsigned char> max_key;
 
9455
 
 
9456
  max_key.reserve(real_prefix_len + min_max_arg_len);
9440
9457
 
9441
9458
  assert(min_max_ranges.elements > 0);
9442
9459
 
9510
9527
    if ( !(cur_range->flag & NO_MAX_RANGE) )
9511
9528
    {
9512
9529
      /* Compose the MAX key for the range. */
9513
 
      unsigned char *max_key= (unsigned char*) my_alloca(real_prefix_len + min_max_arg_len);
9514
 
      memcpy(max_key, group_prefix, real_prefix_len);
9515
 
      memcpy(max_key + real_prefix_len, cur_range->max_key,
9516
 
             cur_range->max_length);
 
9530
      max_key.clear();
 
9531
      max_key.append(group_prefix, real_prefix_len);
 
9532
      max_key.append(cur_range->max_key, cur_range->max_length);
9517
9533
      /* Compare the found key with max_key. */
9518
 
      int cmp_res= key_cmp(index_info->key_part, max_key,
 
9534
      int cmp_res= key_cmp(index_info->key_part,
 
9535
                           max_key.data(),
9519
9536
                           real_prefix_len + min_max_arg_len);
9520
9537
      if ((!((cur_range->flag & NEAR_MAX) && (cmp_res == -1)) || (cmp_res <= 0)))
9521
9538
      {
9568
9585
  key_part_map keypart_map;
9569
9586
  QUICK_RANGE *cur_range;
9570
9587
  int result;
 
9588
  basic_string<unsigned char> min_key;
 
9589
  min_key.reserve(real_prefix_len + min_max_arg_len);
9571
9590
 
9572
9591
  assert(min_max_ranges.elements > 0);
9573
9592
 
9627
9646
    if ( !(cur_range->flag & NO_MIN_RANGE) )
9628
9647
    {
9629
9648
      /* Compose the MIN key for the range. */
9630
 
      unsigned char *min_key= (unsigned char*) my_alloca(real_prefix_len + min_max_arg_len);
9631
 
      memcpy(min_key, group_prefix, real_prefix_len);
9632
 
      memcpy(min_key + real_prefix_len, cur_range->min_key,
9633
 
             cur_range->min_length);
 
9649
      min_key.clear();
 
9650
      min_key.append(group_prefix, real_prefix_len);
 
9651
      min_key.append(cur_range->min_key, cur_range->min_length);
 
9652
 
9634
9653
      /* Compare the found key with min_key. */
9635
 
      int cmp_res= key_cmp(index_info->key_part, min_key,
 
9654
      int cmp_res= key_cmp(index_info->key_part,
 
9655
                           min_key.data(),
9636
9656
                           real_prefix_len + min_max_arg_len);
9637
9657
      if ((!((cur_range->flag & NEAR_MIN) && (cmp_res == 1)) ||
9638
9658
            (cmp_res >= 0)))
9735
9755
  used_lengths->append(buf, length);
9736
9756
}
9737
9757
 
9738
 
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
9739
 
                           const char *msg __attribute__((unused)))
 
9758
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map, const char *)
9740
9759
{
9741
9760
  SEL_ARG **key,**end;
9742
9761
  int idx;
9764
9783
 
9765
9784
 
9766
9785
static void print_ror_scans_arr(Table *table,
9767
 
                                const char *msg __attribute__((unused)),
9768
 
                                struct st_ror_scan_info **start,
 
9786
                                const char *, struct st_ror_scan_info **start,
9769
9787
                                struct st_ror_scan_info **end)
9770
9788
{
9771
9789
  char buff[1024];
9783
9801
}
9784
9802
 
9785
9803
/*****************************************************************************
9786
 
** Instantiate templates 
 
9804
** Instantiate templates
9787
9805
*****************************************************************************/
9788
9806
 
9789
9807
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION