~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/opt_range.cc

Renamed more stuff to drizzle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
15
 
20
16
/*
21
17
  TODO:
27
23
*/
28
24
 
29
25
/*
30
 
  This cursor contains:
 
26
  This file contains:
31
27
 
32
 
  RangeAnalysisModule
33
 
    A module that accepts a condition, index (or partitioning) description,
34
 
    and builds lists of intervals (in index/partitioning space), such that
35
 
    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 
36
32
    intervals.
37
33
    The entry point for the range analysis module is get_mm_tree() function.
38
 
 
 
34
    
39
35
    The lists are returned in form of complicated structure of interlinked
40
 
    optimizer::SEL_TREE/optimizer::SEL_IMERGE/SEL_ARG objects.
41
 
    See quick_range_seq_next, find_used_partitions for examples of how to walk
 
36
    SEL_TREE/SEL_IMERGE/SEL_ARG objects.
 
37
    See quick_range_seq_next, find_used_partitions for examples of how to walk 
42
38
    this structure.
43
 
    All direct "users" of this module are located within this cursor, too.
44
 
 
45
 
 
46
 
  Range/index_merge/groupby-minmax optimizer module
47
 
    A module that accepts a table, condition, and returns
 
39
    All direct "users" of this module are located within this file, too.
 
40
 
 
41
 
 
42
  PartitionPruningModule
 
43
    A module that accepts a partitioned table, condition, and finds which
 
44
    partitions we will need to use in query execution. Search down for
 
45
    "PartitionPruningModule" for description.
 
46
    The module has single entry point - prune_partitions() function.
 
47
 
 
48
 
 
49
  Range/index_merge/groupby-minmax optimizer module  
 
50
    A module that accepts a table, condition, and returns 
48
51
     - a QUICK_*_SELECT object that can be used to retrieve rows that match
49
 
       the specified condition, or a "no records will match the condition"
 
52
       the specified condition, or a "no records will match the condition" 
50
53
       statement.
51
54
 
52
55
    The module entry points are
59
62
 
60
63
  KeyTupleFormat
61
64
  ~~~~~~~~~~~~~~
62
 
  The code in this cursor (and elsewhere) makes operations on key value tuples.
 
65
  The code in this file (and elsewhere) makes operations on key value tuples.
63
66
  Those tuples are stored in the following format:
64
 
 
 
67
  
65
68
  The tuple is a sequence of key part values. The length of key part value
66
69
  depends only on its type (and not depends on the what value is stored)
67
 
 
 
70
  
68
71
    KeyTuple: keypart1-data, keypart2-data, ...
69
 
 
 
72
  
70
73
  The value of each keypart is stored in the following format:
71
 
 
 
74
  
72
75
    keypart_data: [isnull_byte] keypart-value-bytes
73
76
 
74
77
  If a keypart may have a NULL value (key_part->field->real_maybe_null() can
75
 
  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 
76
79
  following valid values:
77
80
    1  - keypart has NULL value.
78
81
    0  - keypart has non-NULL value.
83
86
 
84
87
  keypart-value-bytes holds the value. Its format depends on the field type.
85
88
  The length of keypart-value-bytes may or may not depend on the value being
86
 
  stored. The default is that length is static and equal to
87
 
  KeyPartInfo::length.
88
 
 
89
 
  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the
 
89
  stored. The default is that length is static and equal to 
 
90
  KEY_PART_INFO::length.
 
91
  
 
92
  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the 
90
93
  value:
91
 
 
 
94
  
92
95
     keypart-value-bytes: value_length value_bytes
93
96
 
94
97
  The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.
96
99
  See key_copy() and key_restore() for code to move data between index tuple
97
100
  and table record
98
101
 
99
 
  CAUTION: the above description is only sergefp's understanding of the
 
102
  CAUTION: the above description is only sergefp's understanding of the 
100
103
           subject and may omit some details.
101
104
*/
102
105
 
103
 
#include "config.h"
104
 
 
105
 
#include <math.h>
106
 
#include <float.h>
107
 
 
108
 
#include <string>
109
 
#include <vector>
110
 
#include <algorithm>
111
 
 
112
 
#include "drizzled/sql_base.h"
113
 
#include "drizzled/sql_select.h"
114
 
#include "drizzled/error.h"
115
 
#include "drizzled/optimizer/cost_vector.h"
116
 
#include "drizzled/item/cmpfunc.h"
117
 
#include "drizzled/field/num.h"
118
 
#include "drizzled/check_stack_overrun.h"
119
 
#include "drizzled/optimizer/sum.h"
120
 
#include "drizzled/optimizer/range.h"
121
 
#include "drizzled/optimizer/quick_range.h"
122
 
#include "drizzled/optimizer/quick_range_select.h"
123
 
#include "drizzled/optimizer/quick_group_min_max_select.h"
124
 
#include "drizzled/optimizer/quick_index_merge_select.h"
125
 
#include "drizzled/optimizer/quick_ror_intersect_select.h"
126
 
#include "drizzled/optimizer/quick_ror_union_select.h"
127
 
#include "drizzled/optimizer/table_read_plan.h"
128
 
#include "drizzled/optimizer/sel_arg.h"
129
 
#include "drizzled/optimizer/sel_imerge.h"
130
 
#include "drizzled/optimizer/sel_tree.h"
131
 
#include "drizzled/optimizer/range_param.h"
132
 
#include "drizzled/records.h"
133
 
#include "drizzled/internal/my_sys.h"
134
 
#include "drizzled/internal/iocache.h"
135
 
 
136
 
#include "drizzled/temporal.h" /* Needed in get_mm_leaf() for timestamp -> datetime comparisons */
137
 
 
138
 
using namespace std;
139
 
namespace drizzled
140
 
{
141
 
 
142
 
#define HA_END_SPACE_KEY 0
 
106
#ifdef USE_PRAGMA_IMPLEMENTATION
 
107
#pragma implementation                          // gcc: Class implementation
 
108
#endif
 
109
 
 
110
#include "mysql_priv.h"
 
111
#include <m_ctype.h>
 
112
#include "sql_select.h"
 
113
 
 
114
#ifndef EXTRA_DEBUG
 
115
#define test_rb_tree(A,B) {}
 
116
#define test_use_count(A) {}
 
117
#endif
143
118
 
144
119
/*
145
120
  Convert double value to #rows. Currently this does floor(), and we
146
121
  might consider using round() instead.
147
122
*/
148
 
static inline ha_rows double2rows(double x)
149
 
{
150
 
    return static_cast<ha_rows>(x);
151
 
}
152
 
 
153
 
static unsigned char is_null_string[2]= {1,0};
154
 
 
155
 
 
156
 
/**
157
 
  Get cost of reading nrows table records in a "disk sweep"
158
 
 
159
 
  A disk sweep read is a sequence of Cursor->rnd_pos(rowid) calls that made
160
 
  for an ordered sequence of rowids.
161
 
 
162
 
  We assume hard disk IO. The read is performed as follows:
163
 
 
164
 
   1. The disk head is moved to the needed cylinder
165
 
   2. The controller waits for the plate to rotate
166
 
   3. The data is transferred
167
 
 
168
 
  Time to do #3 is insignificant compared to #2+#1.
169
 
 
170
 
  Time to move the disk head is proportional to head travel distance.
171
 
 
172
 
  Time to wait for the plate to rotate depends on whether the disk head
173
 
  was moved or not.
174
 
 
175
 
  If disk head wasn't moved, the wait time is proportional to distance
176
 
  between the previous block and the block we're reading.
177
 
 
178
 
  If the head was moved, we don't know how much we'll need to wait for the
179
 
  plate to rotate. We assume the wait time to be a variate with a mean of
180
 
  0.5 of full rotation time.
181
 
 
182
 
  Our cost units are "random disk seeks". The cost of random disk seek is
183
 
  actually not a constant, it depends one range of cylinders we're going
184
 
  to access. We make it constant by introducing a fuzzy concept of "typical
185
 
  datafile length" (it's fuzzy as it's hard to tell whether it should
186
 
  include index cursor, temp.tables etc). Then random seek cost is:
187
 
 
188
 
    1 = half_rotation_cost + move_cost * 1/3 * typical_data_file_length
189
 
 
190
 
  We define half_rotation_cost as DISK_SEEK_BASE_COST=0.9.
191
 
 
192
 
  @param table             Table to be accessed
193
 
  @param nrows             Number of rows to retrieve
194
 
  @param interrupted       true <=> Assume that the disk sweep will be
195
 
                           interrupted by other disk IO. false - otherwise.
196
 
  @param cost         OUT  The cost.
 
123
#define double2rows(x) ((ha_rows)(x))
 
124
 
 
125
static int sel_cmp(Field *f,uchar *a,uchar *b,uint8 a_flag,uint8 b_flag);
 
126
 
 
127
static uchar is_null_string[2]= {1,0};
 
128
 
 
129
class RANGE_OPT_PARAM;
 
130
/*
 
131
  A construction block of the SEL_ARG-graph.
 
132
  
 
133
  The following description only covers graphs of SEL_ARG objects with 
 
134
  sel_arg->type==KEY_RANGE:
 
135
 
 
136
  One SEL_ARG object represents an "elementary interval" in form
 
137
  
 
138
      min_value <=?  table.keypartX  <=? max_value
 
139
  
 
140
  The interval is a non-empty interval of any kind: with[out] minimum/maximum
 
141
  bound, [half]open/closed, single-point interval, etc.
 
142
 
 
143
  1. SEL_ARG GRAPH STRUCTURE
 
144
  
 
145
  SEL_ARG objects are linked together in a graph. The meaning of the graph
 
146
  is better demostrated by an example:
 
147
  
 
148
     tree->keys[i]
 
149
      | 
 
150
      |             $              $
 
151
      |    part=1   $     part=2   $    part=3
 
152
      |             $              $
 
153
      |  +-------+  $   +-------+  $   +--------+
 
154
      |  | kp1<1 |--$-->| kp2=5 |--$-->| kp3=10 |
 
155
      |  +-------+  $   +-------+  $   +--------+
 
156
      |      |      $              $       |
 
157
      |      |      $              $   +--------+
 
158
      |      |      $              $   | kp3=12 | 
 
159
      |      |      $              $   +--------+ 
 
160
      |  +-------+  $              $   
 
161
      \->| kp1=2 |--$--------------$-+ 
 
162
         +-------+  $              $ |   +--------+
 
163
             |      $              $  ==>| kp3=11 |
 
164
         +-------+  $              $ |   +--------+
 
165
         | kp1=3 |--$--------------$-+       |
 
166
         +-------+  $              $     +--------+
 
167
             |      $              $     | kp3=14 |
 
168
            ...     $              $     +--------+
 
169
 
 
170
  The entire graph is partitioned into "interval lists".
 
171
 
 
172
  An interval list is a sequence of ordered disjoint intervals over the same
 
173
  key part. SEL_ARG are linked via "next" and "prev" pointers. Additionally,
 
174
  all intervals in the list form an RB-tree, linked via left/right/parent 
 
175
  pointers. The RB-tree root SEL_ARG object will be further called "root of the
 
176
  interval list".
 
177
  
 
178
    In the example pic, there are 4 interval lists: 
 
179
    "kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
 
180
    The vertical lines represent SEL_ARG::next/prev pointers.
 
181
    
 
182
  In an interval list, each member X may have SEL_ARG::next_key_part pointer
 
183
  pointing to the root of another interval list Y. The pointed interval list
 
184
  must cover a key part with greater number (i.e. Y->part > X->part).
 
185
    
 
186
    In the example pic, the next_key_part pointers are represented by
 
187
    horisontal lines.
 
188
 
 
189
  2. SEL_ARG GRAPH SEMANTICS
 
190
 
 
191
  It represents a condition in a special form (we don't have a name for it ATM)
 
192
  The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
 
193
  
 
194
  For example, the picture represents the condition in form:
 
195
   (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR 
 
196
   (kp1=2 AND (kp3=11 OR kp3=14)) OR 
 
197
   (kp1=3 AND (kp3=11 OR kp3=14))
 
198
 
 
199
 
 
200
  3. SEL_ARG GRAPH USE
 
201
 
 
202
  Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
 
203
  Then walk the SEL_ARG graph and get a list of dijsoint ordered key
 
204
  intervals (i.e. intervals in form
 
205
  
 
206
   (constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)
 
207
 
 
208
  Those intervals can be used to access the index. The uses are in:
 
209
   - check_quick_select() - Walk the SEL_ARG graph and find an estimate of
 
210
                            how many table records are contained within all
 
211
                            intervals.
 
212
   - get_quick_select()   - Walk the SEL_ARG, materialize the key intervals,
 
213
                            and create QUICK_RANGE_SELECT object that will
 
214
                            read records within these intervals.
 
215
 
 
216
  4. SPACE COMPLEXITY NOTES 
 
217
 
 
218
    SEL_ARG graph is a representation of an ordered disjoint sequence of
 
219
    intervals over the ordered set of index tuple values.
 
220
 
 
221
    For multi-part keys, one can construct a WHERE expression such that its
 
222
    list of intervals will be of combinatorial size. Here is an example:
 
223
     
 
224
      (keypart1 IN (1,2, ..., n1)) AND 
 
225
      (keypart2 IN (1,2, ..., n2)) AND 
 
226
      (keypart3 IN (1,2, ..., n3))
 
227
    
 
228
    For this WHERE clause the list of intervals will have n1*n2*n3 intervals
 
229
    of form
 
230
     
 
231
      (keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
 
232
    
 
233
    SEL_ARG graph structure aims to reduce the amount of required space by
 
234
    "sharing" the elementary intervals when possible (the pic at the
 
235
    beginning of this comment has examples of such sharing). The sharing may 
 
236
    prevent combinatorial blowup:
 
237
 
 
238
      There are WHERE clauses that have combinatorial-size interval lists but
 
239
      will be represented by a compact SEL_ARG graph.
 
240
      Example:
 
241
        (keypartN IN (1,2, ..., n1)) AND 
 
242
        ...
 
243
        (keypart2 IN (1,2, ..., n2)) AND 
 
244
        (keypart1 IN (1,2, ..., n3))
 
245
 
 
246
    but not in all cases:
 
247
 
 
248
    - There are WHERE clauses that do have a compact SEL_ARG-graph
 
249
      representation but get_mm_tree() and its callees will construct a
 
250
      graph of combinatorial size.
 
251
      Example:
 
252
        (keypart1 IN (1,2, ..., n1)) AND 
 
253
        (keypart2 IN (1,2, ..., n2)) AND 
 
254
        ...
 
255
        (keypartN IN (1,2, ..., n3))
 
256
 
 
257
    - There are WHERE clauses for which the minimal possible SEL_ARG graph
 
258
      representation will have combinatorial size.
 
259
      Example:
 
260
        By induction: Let's take any interval on some keypart in the middle:
 
261
 
 
262
           kp15=c0
 
263
        
 
264
        Then let's AND it with this interval 'structure' from preceding and
 
265
        following keyparts:
 
266
 
 
267
          (kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
 
268
        
 
269
        We will obtain this SEL_ARG graph:
 
270
 
 
271
             kp14     $      kp15      $      kp16
 
272
                      $                $
 
273
         +---------+  $   +---------+  $   +---------+
 
274
         | kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
 
275
         +---------+  $   +---------+  $   +---------+
 
276
              |       $                $              
 
277
         +---------+  $   +---------+  $             
 
278
         | kp14=c2 |--$-->| kp15=c0 |  $             
 
279
         +---------+  $   +---------+  $             
 
280
                      $                $
 
281
                      
 
282
       Note that we had to duplicate "kp15=c0" and there was no way to avoid
 
283
       that. 
 
284
       The induction step: AND the obtained expression with another "wrapping"
 
285
       expression like (*).
 
286
       When the process ends because of the limit on max. number of keyparts 
 
287
       we'll have:
 
288
 
 
289
         WHERE clause length  is O(3*#max_keyparts)
 
290
         SEL_ARG graph size   is O(2^(#max_keyparts/2))
 
291
 
 
292
       (it is also possible to construct a case where instead of 2 in 2^n we
 
293
        have a bigger constant, e.g. 4, and get a graph with 4^(31/2)= 2^31
 
294
        nodes)
 
295
 
 
296
    We avoid consuming too much memory by setting a limit on the number of
 
297
    SEL_ARG object we can construct during one range analysis invocation.
197
298
*/
198
299
 
199
 
static void get_sweep_read_cost(Table *table,
200
 
                                ha_rows nrows,
201
 
                                bool interrupted,
202
 
                                optimizer::CostVector *cost)
203
 
{
204
 
  cost->zero();
205
 
  if (table->cursor->primary_key_is_clustered())
206
 
  {
207
 
    cost->setIOCount(table->cursor->read_time(table->getShare()->getPrimaryKey(),
208
 
                                             static_cast<uint32_t>(nrows),
209
 
                                             nrows));
210
 
  }
211
 
  else
212
 
  {
213
 
    double n_blocks=
214
 
      ceil(uint64_t2double(table->cursor->stats.data_file_length) / IO_SIZE);
215
 
    double busy_blocks=
216
 
      n_blocks * (1.0 - pow(1.0 - 1.0/n_blocks, rows2double(nrows)));
217
 
    if (busy_blocks < 1.0)
218
 
      busy_blocks= 1.0;
219
 
 
220
 
    cost->setIOCount(busy_blocks);
221
 
 
222
 
    if (! interrupted)
223
 
    {
224
 
      /* Assume reading is done in one 'sweep' */
225
 
      cost->setAvgIOCost((DISK_SEEK_BASE_COST +
226
 
                          DISK_SEEK_PROP_COST*n_blocks/busy_blocks));
227
 
    }
228
 
  }
229
 
}
 
300
class SEL_ARG :public Sql_alloc
 
301
{
 
302
public:
 
303
  uint8 min_flag,max_flag,maybe_flag;
 
304
  uint8 part;                                   // Which key part
 
305
  uint8 maybe_null;
 
306
  /* 
 
307
    Number of children of this element in the RB-tree, plus 1 for this
 
308
    element itself.
 
309
  */
 
310
  uint16 elements;
 
311
  /*
 
312
    Valid only for elements which are RB-tree roots: Number of times this
 
313
    RB-tree is referred to (it is referred by SEL_ARG::next_key_part or by
 
314
    SEL_TREE::keys[i] or by a temporary SEL_ARG* variable)
 
315
  */
 
316
  ulong use_count;
 
317
 
 
318
  Field *field;
 
319
  uchar *min_value,*max_value;                  // Pointer to range
 
320
 
 
321
  /*
 
322
    eq_tree() requires that left == right == 0 if the type is MAYBE_KEY.
 
323
   */
 
324
  SEL_ARG *left,*right;   /* R-B tree children */
 
325
  SEL_ARG *next,*prev;    /* Links for bi-directional interval list */
 
326
  SEL_ARG *parent;        /* R-B tree parent */
 
327
  SEL_ARG *next_key_part; 
 
328
  enum leaf_color { BLACK,RED } color;
 
329
  enum Type { IMPOSSIBLE, MAYBE, MAYBE_KEY, KEY_RANGE } type;
 
330
 
 
331
  enum { MAX_SEL_ARGS = 16000 };
 
332
 
 
333
  SEL_ARG() {}
 
334
  SEL_ARG(SEL_ARG &);
 
335
  SEL_ARG(Field *,const uchar *, const uchar *);
 
336
  SEL_ARG(Field *field, uint8 part, uchar *min_value, uchar *max_value,
 
337
          uint8 min_flag, uint8 max_flag, uint8 maybe_flag);
 
338
  SEL_ARG(enum Type type_arg)
 
339
    :min_flag(0),elements(1),use_count(1),left(0),right(0),next_key_part(0),
 
340
    color(BLACK), type(type_arg)
 
341
  {}
 
342
  inline bool is_same(SEL_ARG *arg)
 
343
  {
 
344
    if (type != arg->type || part != arg->part)
 
345
      return 0;
 
346
    if (type != KEY_RANGE)
 
347
      return 1;
 
348
    return cmp_min_to_min(arg) == 0 && cmp_max_to_max(arg) == 0;
 
349
  }
 
350
  inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; }
 
351
  inline void maybe_smaller() { maybe_flag=1; }
 
352
  /* Return true iff it's a single-point null interval */
 
353
  inline bool is_null_interval() { return maybe_null && max_value[0] == 1; } 
 
354
  inline int cmp_min_to_min(SEL_ARG* arg)
 
355
  {
 
356
    return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag);
 
357
  }
 
358
  inline int cmp_min_to_max(SEL_ARG* arg)
 
359
  {
 
360
    return sel_cmp(field,min_value, arg->max_value, min_flag, arg->max_flag);
 
361
  }
 
362
  inline int cmp_max_to_max(SEL_ARG* arg)
 
363
  {
 
364
    return sel_cmp(field,max_value, arg->max_value, max_flag, arg->max_flag);
 
365
  }
 
366
  inline int cmp_max_to_min(SEL_ARG* arg)
 
367
  {
 
368
    return sel_cmp(field,max_value, arg->min_value, max_flag, arg->min_flag);
 
369
  }
 
370
  SEL_ARG *clone_and(SEL_ARG* arg)
 
371
  {                                             // Get overlapping range
 
372
    uchar *new_min,*new_max;
 
373
    uint8 flag_min,flag_max;
 
374
    if (cmp_min_to_min(arg) >= 0)
 
375
    {
 
376
      new_min=min_value; flag_min=min_flag;
 
377
    }
 
378
    else
 
379
    {
 
380
      new_min=arg->min_value; flag_min=arg->min_flag; /* purecov: deadcode */
 
381
    }
 
382
    if (cmp_max_to_max(arg) <= 0)
 
383
    {
 
384
      new_max=max_value; flag_max=max_flag;
 
385
    }
 
386
    else
 
387
    {
 
388
      new_max=arg->max_value; flag_max=arg->max_flag;
 
389
    }
 
390
    return new SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
 
391
                       test(maybe_flag && arg->maybe_flag));
 
392
  }
 
393
  SEL_ARG *clone_first(SEL_ARG *arg)
 
394
  {                                             // min <= X < arg->min
 
395
    return new SEL_ARG(field,part, min_value, arg->min_value,
 
396
                       min_flag, arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX,
 
397
                       maybe_flag | arg->maybe_flag);
 
398
  }
 
399
  SEL_ARG *clone_last(SEL_ARG *arg)
 
400
  {                                             // min <= X <= key_max
 
401
    return new SEL_ARG(field, part, min_value, arg->max_value,
 
402
                       min_flag, arg->max_flag, maybe_flag | arg->maybe_flag);
 
403
  }
 
404
  SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
 
405
 
 
406
  bool copy_min(SEL_ARG* arg)
 
407
  {                                             // Get overlapping range
 
408
    if (cmp_min_to_min(arg) > 0)
 
409
    {
 
410
      min_value=arg->min_value; min_flag=arg->min_flag;
 
411
      if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
 
412
          (NO_MAX_RANGE | NO_MIN_RANGE))
 
413
        return 1;                               // Full range
 
414
    }
 
415
    maybe_flag|=arg->maybe_flag;
 
416
    return 0;
 
417
  }
 
418
  bool copy_max(SEL_ARG* arg)
 
419
  {                                             // Get overlapping range
 
420
    if (cmp_max_to_max(arg) <= 0)
 
421
    {
 
422
      max_value=arg->max_value; max_flag=arg->max_flag;
 
423
      if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
 
424
          (NO_MAX_RANGE | NO_MIN_RANGE))
 
425
        return 1;                               // Full range
 
426
    }
 
427
    maybe_flag|=arg->maybe_flag;
 
428
    return 0;
 
429
  }
 
430
 
 
431
  void copy_min_to_min(SEL_ARG *arg)
 
432
  {
 
433
    min_value=arg->min_value; min_flag=arg->min_flag;
 
434
  }
 
435
  void copy_min_to_max(SEL_ARG *arg)
 
436
  {
 
437
    max_value=arg->min_value;
 
438
    max_flag=arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
 
439
  }
 
440
  void copy_max_to_min(SEL_ARG *arg)
 
441
  {
 
442
    min_value=arg->max_value;
 
443
    min_flag=arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
 
444
  }
 
445
  /* returns a number of keypart values (0 or 1) appended to the key buffer */
 
446
  int store_min(uint length, uchar **min_key,uint min_key_flag)
 
447
  {
 
448
    /* "(kp1 > c1) AND (kp2 OP c2) AND ..." -> (kp1 > c1) */
 
449
    if ((!(min_flag & NO_MIN_RANGE) &&
 
450
        !(min_key_flag & (NO_MIN_RANGE | NEAR_MIN))))
 
451
    {
 
452
      if (maybe_null && *min_value)
 
453
      {
 
454
        **min_key=1;
 
455
        bzero(*min_key+1,length-1);
 
456
      }
 
457
      else
 
458
        memcpy(*min_key,min_value,length);
 
459
      (*min_key)+= length;
 
460
      return 1;
 
461
    }
 
462
    return 0;
 
463
  }
 
464
  /* returns a number of keypart values (0 or 1) appended to the key buffer */
 
465
  int store_max(uint length, uchar **max_key, uint max_key_flag)
 
466
  {
 
467
    if (!(max_flag & NO_MAX_RANGE) &&
 
468
        !(max_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
 
469
    {
 
470
      if (maybe_null && *max_value)
 
471
      {
 
472
        **max_key=1;
 
473
        bzero(*max_key+1,length-1);
 
474
      }
 
475
      else
 
476
        memcpy(*max_key,max_value,length);
 
477
      (*max_key)+= length;
 
478
      return 1;
 
479
    }
 
480
    return 0;
 
481
  }
 
482
 
 
483
  /* returns a number of keypart values appended to the key buffer */
 
484
  int store_min_key(KEY_PART *key, uchar **range_key, uint *range_key_flag)
 
485
  {
 
486
    SEL_ARG *key_tree= first();
 
487
    uint res= key_tree->store_min(key[key_tree->part].store_length,
 
488
                                  range_key, *range_key_flag);
 
489
    *range_key_flag|= key_tree->min_flag;
 
490
    
 
491
    if (key_tree->next_key_part &&
 
492
        key_tree->next_key_part->part == key_tree->part+1 &&
 
493
        !(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)) &&
 
494
        key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
 
495
      res+= key_tree->next_key_part->store_min_key(key, range_key,
 
496
                                                   range_key_flag);
 
497
    return res;
 
498
  }
 
499
 
 
500
  /* returns a number of keypart values appended to the key buffer */
 
501
  int store_max_key(KEY_PART *key, uchar **range_key, uint *range_key_flag)
 
502
  {
 
503
    SEL_ARG *key_tree= last();
 
504
    uint res=key_tree->store_max(key[key_tree->part].store_length,
 
505
                                 range_key, *range_key_flag);
 
506
    (*range_key_flag)|= key_tree->max_flag;
 
507
    if (key_tree->next_key_part &&
 
508
        key_tree->next_key_part->part == key_tree->part+1 &&
 
509
        !(*range_key_flag & (NO_MAX_RANGE | NEAR_MAX)) &&
 
510
        key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
 
511
      res+= key_tree->next_key_part->store_max_key(key, range_key,
 
512
                                                   range_key_flag);
 
513
    return res;
 
514
  }
 
515
 
 
516
  SEL_ARG *insert(SEL_ARG *key);
 
517
  SEL_ARG *tree_delete(SEL_ARG *key);
 
518
  SEL_ARG *find_range(SEL_ARG *key);
 
519
  SEL_ARG *rb_insert(SEL_ARG *leaf);
 
520
  friend SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key, SEL_ARG *par);
 
521
#ifdef EXTRA_DEBUG
 
522
  friend int test_rb_tree(SEL_ARG *element,SEL_ARG *parent);
 
523
  void test_use_count(SEL_ARG *root);
 
524
#endif
 
525
  SEL_ARG *first();
 
526
  SEL_ARG *last();
 
527
  void make_root();
 
528
  inline bool simple_key()
 
529
  {
 
530
    return !next_key_part && elements == 1;
 
531
  }
 
532
  void increment_use_count(long count)
 
533
  {
 
534
    if (next_key_part)
 
535
    {
 
536
      next_key_part->use_count+=count;
 
537
      count*= (next_key_part->use_count-count);
 
538
      for (SEL_ARG *pos=next_key_part->first(); pos ; pos=pos->next)
 
539
        if (pos->next_key_part)
 
540
          pos->increment_use_count(count);
 
541
    }
 
542
  }
 
543
  void free_tree()
 
544
  {
 
545
    for (SEL_ARG *pos=first(); pos ; pos=pos->next)
 
546
      if (pos->next_key_part)
 
547
      {
 
548
        pos->next_key_part->use_count--;
 
549
        pos->next_key_part->free_tree();
 
550
      }
 
551
  }
 
552
 
 
553
  inline SEL_ARG **parent_ptr()
 
554
  {
 
555
    return parent->left == this ? &parent->left : &parent->right;
 
556
  }
 
557
 
 
558
 
 
559
  /*
 
560
    Check if this SEL_ARG object represents a single-point interval
 
561
 
 
562
    SYNOPSIS
 
563
      is_singlepoint()
 
564
    
 
565
    DESCRIPTION
 
566
      Check if this SEL_ARG object (not tree) represents a single-point
 
567
      interval, i.e. if it represents a "keypart = const" or 
 
568
      "keypart IS NULL".
 
569
 
 
570
    RETURN
 
571
      true   This SEL_ARG object represents a singlepoint interval
 
572
      false  Otherwise
 
573
  */
 
574
 
 
575
  bool is_singlepoint()
 
576
  {
 
577
    /* 
 
578
      Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field) 
 
579
      flags, and the same for right edge.
 
580
    */
 
581
    if (min_flag || max_flag)
 
582
      return false;
 
583
    uchar *min_val= min_value;
 
584
    uchar *max_val= max_value;
 
585
 
 
586
    if (maybe_null)
 
587
    {
 
588
      /* First byte is a NULL value indicator */
 
589
      if (*min_val != *max_val)
 
590
        return false;
 
591
 
 
592
      if (*min_val)
 
593
        return true; /* This "x IS NULL" */
 
594
      min_val++;
 
595
      max_val++;
 
596
    }
 
597
    return !field->key_cmp(min_val, max_val);
 
598
  }
 
599
  SEL_ARG *clone_tree(RANGE_OPT_PARAM *param);
 
600
};
 
601
 
 
602
class SEL_IMERGE;
 
603
 
 
604
 
 
605
class SEL_TREE :public Sql_alloc
 
606
{
 
607
public:
 
608
  /*
 
609
    Starting an effort to document this field:
 
610
    (for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) => 
 
611
       (type == SEL_TREE::IMPOSSIBLE)
 
612
  */
 
613
  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
 
614
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
 
615
  SEL_TREE() :type(KEY)
 
616
  {
 
617
    keys_map.clear_all();
 
618
    bzero((char*) keys,sizeof(keys));
 
619
  }
 
620
  /*
 
621
    Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
 
622
    keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
 
623
    merit in range analyzer functions (e.g. get_mm_parts) returning a
 
624
    pointer to such SEL_TREE instead of NULL)
 
625
  */
 
626
  SEL_ARG *keys[MAX_KEY];
 
627
  key_map keys_map;        /* bitmask of non-NULL elements in keys */
 
628
 
 
629
  /*
 
630
    Possible ways to read rows using index_merge. The list is non-empty only
 
631
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
 
632
  */
 
633
  List<SEL_IMERGE> merges;
 
634
 
 
635
  /* The members below are filled/used only after get_mm_tree is done */
 
636
  key_map ror_scans_map;   /* bitmask of ROR scan-able elements in keys */
 
637
  uint    n_ror_scans;     /* number of set bits in ror_scans_map */
 
638
 
 
639
  struct st_ror_scan_info **ror_scans;     /* list of ROR key scans */
 
640
  struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
 
641
  /* Note that #records for each key scan is stored in table->quick_rows */
 
642
};
 
643
 
 
644
class RANGE_OPT_PARAM
 
645
{
 
646
public:
 
647
  THD   *thd;   /* Current thread handle */
 
648
  TABLE *table; /* Table being analyzed */
 
649
  COND *cond;   /* Used inside get_mm_tree(). */
 
650
  table_map prev_tables;
 
651
  table_map read_tables;
 
652
  table_map current_table; /* Bit of the table being analyzed */
 
653
 
 
654
  /* Array of parts of all keys for which range analysis is performed */
 
655
  KEY_PART *key_parts;
 
656
  KEY_PART *key_parts_end;
 
657
  MEM_ROOT *mem_root; /* Memory that will be freed when range analysis completes */
 
658
  MEM_ROOT *old_root; /* Memory that will last until the query end */
 
659
  /*
 
660
    Number of indexes used in range analysis (In SEL_TREE::keys only first
 
661
    #keys elements are not empty)
 
662
  */
 
663
  uint keys;
 
664
  
 
665
  /* 
 
666
    If true, the index descriptions describe real indexes (and it is ok to
 
667
    call field->optimize_range(real_keynr[...], ...).
 
668
    Otherwise index description describes fake indexes.
 
669
  */
 
670
  bool using_real_indexes;
 
671
  
 
672
  bool remove_jump_scans;
 
673
  
 
674
  /*
 
675
    used_key_no -> table_key_no translation table. Only makes sense if
 
676
    using_real_indexes==true
 
677
  */
 
678
  uint real_keynr[MAX_KEY];
 
679
  /* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
 
680
  uint alloced_sel_args; 
 
681
  bool force_default_mrr;
 
682
};
 
683
 
 
684
class PARAM : public RANGE_OPT_PARAM
 
685
{
 
686
public:
 
687
  KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
 
688
  longlong baseflag;
 
689
  uint max_key_part;
 
690
  /* Number of ranges in the last checked tree->key */
 
691
  uint range_count;
 
692
  uchar min_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH],
 
693
    max_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];
 
694
  bool quick;                           // Don't calulate possible keys
 
695
 
 
696
  uint fields_bitmap_size;
 
697
  MY_BITMAP needed_fields;    /* bitmask of fields needed by the query */
 
698
  MY_BITMAP tmp_covered_fields;
 
699
 
 
700
  key_map *needed_reg;        /* ptr to SQL_SELECT::needed_reg */
 
701
 
 
702
  uint *imerge_cost_buff;     /* buffer for index_merge cost estimates */
 
703
  uint imerge_cost_buff_size; /* size of the buffer */
 
704
 
 
705
  /* true if last checked tree->key can be used for ROR-scan */
 
706
  bool is_ror_scan;
 
707
  /* Number of ranges in the last checked tree->key */
 
708
  uint n_ranges;
 
709
};
 
710
 
 
711
class TABLE_READ_PLAN;
 
712
  class TRP_RANGE;
 
713
  class TRP_ROR_INTERSECT;
 
714
  class TRP_ROR_UNION;
 
715
  class TRP_ROR_INDEX_MERGE;
 
716
  class TRP_GROUP_MIN_MAX;
230
717
 
231
718
struct st_ror_scan_info;
232
719
 
233
 
static optimizer::SEL_TREE * get_mm_parts(optimizer::RangeParameter *param,
234
 
                               COND *cond_func,
235
 
                               Field *field,
236
 
                                                 Item_func::Functype type,
237
 
                               Item *value,
238
 
                                                 Item_result cmp_type);
239
 
 
240
 
static optimizer::SEL_ARG *get_mm_leaf(optimizer::RangeParameter *param,
241
 
                                       COND *cond_func,
242
 
                                       Field *field,
243
 
                                       KEY_PART *key_part,
244
 
                                                         Item_func::Functype type,
245
 
                                       Item *value);
246
 
 
247
 
static optimizer::SEL_TREE *get_mm_tree(optimizer::RangeParameter *param, COND *cond);
248
 
 
249
 
static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts);
250
 
 
251
 
static ha_rows check_quick_select(Session *session,
252
 
                                  optimizer::Parameter *param,
253
 
                                  uint32_t idx,
254
 
                                  bool index_only,
255
 
                                  optimizer::SEL_ARG *tree,
256
 
                                  bool update_tbl_stats,
257
 
                                  uint32_t *mrr_flags,
258
 
                                  uint32_t *bufsize,
259
 
                                  optimizer::CostVector *cost);
260
 
 
261
 
static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
262
 
                                                      optimizer::Parameter *param,
263
 
                                                      optimizer::SEL_TREE *tree,
264
 
                                                      bool index_read_must_be_used,
265
 
                                                      bool update_tbl_stats,
266
 
                                                      double read_time);
267
 
 
268
 
static
269
 
optimizer::RorIntersectReadPlan *get_best_ror_intersect(const optimizer::Parameter *param,
270
 
                                                        optimizer::SEL_TREE *tree,
271
 
                                                        double read_time,
272
 
                                                        bool *are_all_covering);
273
 
 
274
 
static
275
 
optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
276
 
                                                                 optimizer::SEL_TREE *tree,
277
 
                                                                 double read_time);
278
 
 
279
 
static
280
 
optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
281
 
                                                  optimizer::Parameter *param,
282
 
                                                  optimizer::SEL_IMERGE *imerge,
283
 
                                                  double read_time);
284
 
 
285
 
static
286
 
optimizer::GroupMinMaxReadPlan *get_best_group_min_max(optimizer::Parameter *param, optimizer::SEL_TREE *tree);
287
 
 
288
 
static optimizer::SEL_TREE *tree_and(optimizer::RangeParameter *param, 
289
 
                                     optimizer::SEL_TREE *tree1, 
290
 
                                     optimizer::SEL_TREE *tree2);
291
 
 
292
 
static optimizer::SEL_ARG *sel_add(optimizer::SEL_ARG *key1, optimizer::SEL_ARG *key2);
293
 
 
294
 
static optimizer::SEL_ARG *key_and(optimizer::RangeParameter *param,
295
 
                                   optimizer::SEL_ARG *key1,
296
 
                                   optimizer::SEL_ARG *key2,
297
 
                                   uint32_t clone_flag);
298
 
 
299
 
static bool get_range(optimizer::SEL_ARG **e1, optimizer::SEL_ARG **e2, optimizer::SEL_ARG *root1);
300
 
 
301
 
optimizer::SEL_ARG optimizer::null_element(optimizer::SEL_ARG::IMPOSSIBLE);
302
 
 
303
 
static bool null_part_in_key(KEY_PART *key_part,
304
 
                             const unsigned char *key,
305
 
                             uint32_t length);
306
 
 
307
 
bool sel_trees_can_be_ored(optimizer::SEL_TREE *tree1, 
308
 
                           optimizer::SEL_TREE *tree2, 
309
 
                           optimizer::RangeParameter *param);
310
 
 
311
 
 
312
 
 
313
 
 
 
720
static SEL_TREE * get_mm_parts(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
 
721
                               Item_func::Functype type,Item *value,
 
722
                               Item_result cmp_type);
 
723
static SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
 
724
                            KEY_PART *key_part,
 
725
                            Item_func::Functype type,Item *value);
 
726
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond);
 
727
 
 
728
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);
 
729
static ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
 
730
                                  SEL_ARG *tree, bool update_tbl_stats, 
 
731
                                  uint *mrr_flags, uint *bufsize,
 
732
                                  COST_VECT *cost);
 
733
                                  //bool update_tbl_stats);
 
734
/*static ha_rows check_quick_keys(PARAM *param,uint index,SEL_ARG *key_tree,
 
735
                                uchar *min_key, uint min_key_flag, int,
 
736
                                uchar *max_key, uint max_key_flag, int);
 
737
*/
 
738
 
 
739
QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
 
740
                                     SEL_ARG *key_tree, uint mrr_flags, 
 
741
                                     uint mrr_buf_size, MEM_ROOT *alloc);
 
742
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
 
743
                                       bool index_read_must_be_used,
 
744
                                       bool update_tbl_stats,
 
745
                                       double read_time);
 
746
static
 
747
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
 
748
                                          double read_time,
 
749
                                          bool *are_all_covering);
 
750
static
 
751
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
 
752
                                                   SEL_TREE *tree,
 
753
                                                   double read_time);
 
754
static
 
755
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
 
756
                                         double read_time);
 
757
static
 
758
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree);
 
759
 
 
760
#ifndef DBUG_OFF
 
761
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
 
762
                           const char *msg);
 
763
static void print_ror_scans_arr(TABLE *table, const char *msg,
 
764
                                struct st_ror_scan_info **start,
 
765
                                struct st_ror_scan_info **end);
 
766
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
 
767
#endif
 
768
 
 
769
static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
 
770
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
 
771
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
 
772
static SEL_ARG *key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2);
 
773
static SEL_ARG *key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
 
774
                        uint clone_flag);
 
775
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
 
776
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
 
777
                    SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
 
778
                    uchar *max_key,uint max_key_flag);
 
779
static bool eq_tree(SEL_ARG* a,SEL_ARG *b);
 
780
 
 
781
static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
 
782
static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
 
783
                             uint length);
 
784
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM* param);
 
785
 
 
786
 
 
787
/*
 
788
  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
 
789
  a condition in the following form:
 
790
   (t_1||t_2||...||t_N) && (next)
 
791
 
 
792
  where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
 
793
  (t_i,t_j) contains SEL_ARGS for the same index.
 
794
 
 
795
  SEL_TREE contained in SEL_IMERGE always has merges=NULL.
 
796
 
 
797
  This class relies on memory manager to do the cleanup.
 
798
*/
 
799
 
 
800
class SEL_IMERGE : public Sql_alloc
 
801
{
 
802
  enum { PREALLOCED_TREES= 10};
 
803
public:
 
804
  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
 
805
  SEL_TREE **trees;             /* trees used to do index_merge   */
 
806
  SEL_TREE **trees_next;        /* last of these trees            */
 
807
  SEL_TREE **trees_end;         /* end of allocated space         */
 
808
 
 
809
  SEL_ARG  ***best_keys;        /* best keys to read in SEL_TREEs */
 
810
 
 
811
  SEL_IMERGE() :
 
812
    trees(&trees_prealloced[0]),
 
813
    trees_next(trees),
 
814
    trees_end(trees + PREALLOCED_TREES)
 
815
  {}
 
816
  int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
 
817
  int or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree);
 
818
  int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge);
 
819
};
 
820
 
 
821
 
 
822
/*
 
823
  Add SEL_TREE to this index_merge without any checks,
 
824
 
 
825
  NOTES
 
826
    This function implements the following:
 
827
      (x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs
 
828
 
 
829
  RETURN
 
830
     0 - OK
 
831
    -1 - Out of memory.
 
832
*/
 
833
 
 
834
int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
 
835
{
 
836
  if (trees_next == trees_end)
 
837
  {
 
838
    const int realloc_ratio= 2;         /* Double size for next round */
 
839
    uint old_elements= (trees_end - trees);
 
840
    uint old_size= sizeof(SEL_TREE**) * old_elements;
 
841
    uint new_size= old_size * realloc_ratio;
 
842
    SEL_TREE **new_trees;
 
843
    if (!(new_trees= (SEL_TREE**)alloc_root(param->mem_root, new_size)))
 
844
      return -1;
 
845
    memcpy(new_trees, trees, old_size);
 
846
    trees=      new_trees;
 
847
    trees_next= trees + old_elements;
 
848
    trees_end=  trees + old_elements * realloc_ratio;
 
849
  }
 
850
  *(trees_next++)= tree;
 
851
  return 0;
 
852
}
 
853
 
 
854
 
 
855
/*
 
856
  Perform OR operation on this SEL_IMERGE and supplied SEL_TREE new_tree,
 
857
  combining new_tree with one of the trees in this SEL_IMERGE if they both
 
858
  have SEL_ARGs for the same key.
 
859
 
 
860
  SYNOPSIS
 
861
    or_sel_tree_with_checks()
 
862
      param    PARAM from SQL_SELECT::test_quick_select
 
863
      new_tree SEL_TREE with type KEY or KEY_SMALLER.
 
864
 
 
865
  NOTES
 
866
    This does the following:
 
867
    (t_1||...||t_k)||new_tree =
 
868
     either
 
869
       = (t_1||...||t_k||new_tree)
 
870
     or
 
871
       = (t_1||....||(t_j|| new_tree)||...||t_k),
 
872
 
 
873
     where t_i, y are SEL_TREEs.
 
874
    new_tree is combined with the first t_j it has a SEL_ARG on common
 
875
    key with. As a consequence of this, choice of keys to do index_merge
 
876
    read may depend on the order of conditions in WHERE part of the query.
 
877
 
 
878
  RETURN
 
879
    0  OK
 
880
    1  One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
 
881
       and (*this) should be discarded.
 
882
   -1  An error occurred.
 
883
*/
 
884
 
 
885
int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree)
 
886
{
 
887
  for (SEL_TREE** tree = trees;
 
888
       tree != trees_next;
 
889
       tree++)
 
890
  {
 
891
    if (sel_trees_can_be_ored(*tree, new_tree, param))
 
892
    {
 
893
      *tree = tree_or(param, *tree, new_tree);
 
894
      if (!*tree)
 
895
        return 1;
 
896
      if (((*tree)->type == SEL_TREE::MAYBE) ||
 
897
          ((*tree)->type == SEL_TREE::ALWAYS))
 
898
        return 1;
 
899
      /* SEL_TREE::IMPOSSIBLE is impossible here */
 
900
      return 0;
 
901
    }
 
902
  }
 
903
 
 
904
  /* New tree cannot be combined with any of existing trees. */
 
905
  return or_sel_tree(param, new_tree);
 
906
}
 
907
 
 
908
 
 
909
/*
 
910
  Perform OR operation on this index_merge and supplied index_merge list.
 
911
 
 
912
  RETURN
 
913
    0 - OK
 
914
    1 - One of conditions in result is always true and this SEL_IMERGE
 
915
        should be discarded.
 
916
   -1 - An error occurred
 
917
*/
 
918
 
 
919
int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge)
 
920
{
 
921
  for (SEL_TREE** tree= imerge->trees;
 
922
       tree != imerge->trees_next;
 
923
       tree++)
 
924
  {
 
925
    if (or_sel_tree_with_checks(param, *tree))
 
926
      return 1;
 
927
  }
 
928
  return 0;
 
929
}
314
930
 
315
931
 
316
932
/*
317
933
  Perform AND operation on two index_merge lists and store result in *im1.
318
934
*/
319
935
 
320
 
inline void imerge_list_and_list(List<optimizer::SEL_IMERGE> *im1, List<optimizer::SEL_IMERGE> *im2)
 
936
inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
321
937
{
322
938
  im1->concat(im2);
323
939
}
324
940
 
325
941
 
 
942
/*
 
943
  Perform OR operation on 2 index_merge lists, storing result in first list.
 
944
 
 
945
  NOTES
 
946
    The following conversion is implemented:
 
947
     (a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
 
948
      => (a_1||b_1).
 
949
 
 
950
    i.e. all conjuncts except the first one are currently dropped.
 
951
    This is done to avoid producing N*K ways to do index_merge.
 
952
 
 
953
    If (a_1||b_1) produce a condition that is always true, NULL is returned
 
954
    and index_merge is discarded (while it is actually possible to try
 
955
    harder).
 
956
 
 
957
    As a consequence of this, choice of keys to do index_merge read may depend
 
958
    on the order of conditions in WHERE part of the query.
 
959
 
 
960
  RETURN
 
961
    0     OK, result is stored in *im1
 
962
    other Error, both passed lists are unusable
 
963
*/
 
964
 
 
965
int imerge_list_or_list(RANGE_OPT_PARAM *param,
 
966
                        List<SEL_IMERGE> *im1,
 
967
                        List<SEL_IMERGE> *im2)
 
968
{
 
969
  SEL_IMERGE *imerge= im1->head();
 
970
  im1->empty();
 
971
  im1->push_back(imerge);
 
972
 
 
973
  return imerge->or_sel_imerge_with_checks(param, im2->head());
 
974
}
 
975
 
 
976
 
 
977
/*
 
978
  Perform OR operation on index_merge list and key tree.
 
979
 
 
980
  RETURN
 
981
    0     OK, result is stored in *im1.
 
982
    other Error
 
983
*/
 
984
 
 
985
int imerge_list_or_tree(RANGE_OPT_PARAM *param,
 
986
                        List<SEL_IMERGE> *im1,
 
987
                        SEL_TREE *tree)
 
988
{
 
989
  SEL_IMERGE *imerge;
 
990
  List_iterator<SEL_IMERGE> it(*im1);
 
991
  while ((imerge= it++))
 
992
  {
 
993
    if (imerge->or_sel_tree_with_checks(param, tree))
 
994
      it.remove();
 
995
  }
 
996
  return im1->is_empty();
 
997
}
 
998
 
 
999
 
326
1000
/***************************************************************************
327
 
** Basic functions for SqlSelect and QuickRangeSelect
 
1001
** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
328
1002
***************************************************************************/
329
1003
 
330
1004
        /* make a select from mysql info
333
1007
           1 = Got some error (out of memory?)
334
1008
           */
335
1009
 
336
 
optimizer::SqlSelect *optimizer::make_select(Table *head,
337
 
                                             table_map const_tables,
338
 
                                             table_map read_tables,
339
 
                                             COND *conds,
340
 
                                             bool allow_null_cond,
341
 
                                             int *error)
 
1010
SQL_SELECT *make_select(TABLE *head, table_map const_tables,
 
1011
                        table_map read_tables, COND *conds,
 
1012
                        bool allow_null_cond,
 
1013
                        int *error)
342
1014
{
343
 
  optimizer::SqlSelect *select= NULL;
344
 
 
345
 
  *error= 0;
346
 
 
347
 
  if (! conds && ! allow_null_cond)
348
 
  {
349
 
    return 0;
350
 
  }
351
 
  if (! (select= new optimizer::SqlSelect))
 
1015
  SQL_SELECT *select;
 
1016
  DBUG_ENTER("make_select");
 
1017
 
 
1018
  *error=0;
 
1019
 
 
1020
  if (!conds && !allow_null_cond)
 
1021
    DBUG_RETURN(0);
 
1022
  if (!(select= new SQL_SELECT))
352
1023
  {
353
1024
    *error= 1;                  // out of memory
354
 
    return 0;
 
1025
    DBUG_RETURN(0);             /* purecov: inspected */
355
1026
  }
356
1027
  select->read_tables=read_tables;
357
1028
  select->const_tables=const_tables;
360
1031
 
361
1032
  if (head->sort.io_cache)
362
1033
  {
363
 
    memcpy(select->file, head->sort.io_cache, sizeof(internal::IO_CACHE));
364
 
    select->records=(ha_rows) (select->file->end_of_file/
365
 
                               head->cursor->ref_length);
366
 
    delete head->sort.io_cache;
 
1034
    select->file= *head->sort.io_cache;
 
1035
    select->records=(ha_rows) (select->file.end_of_file/
 
1036
                               head->file->ref_length);
 
1037
    my_free(head->sort.io_cache, MYF(0));
367
1038
    head->sort.io_cache=0;
368
1039
  }
369
 
  return(select);
370
 
}
371
 
 
372
 
 
373
 
optimizer::SqlSelect::SqlSelect() 
374
 
  :
375
 
    quick(NULL),
376
 
    cond(NULL),
377
 
    file(static_cast<internal::IO_CACHE *>(memory::sql_calloc(sizeof(internal::IO_CACHE)))),
378
 
    free_cond(false)
379
 
{
380
 
  quick_keys.reset();
381
 
  needed_reg.reset();
382
 
  my_b_clear(file);
383
 
}
384
 
 
385
 
 
386
 
void optimizer::SqlSelect::cleanup()
387
 
{
388
 
  if (quick)
389
 
  {
390
 
    delete quick;
391
 
    quick= NULL;
392
 
  }
393
 
 
 
1040
  DBUG_RETURN(select);
 
1041
}
 
1042
 
 
1043
 
 
1044
SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
 
1045
{
 
1046
  quick_keys.clear_all(); needed_reg.clear_all();
 
1047
  my_b_clear(&file);
 
1048
}
 
1049
 
 
1050
 
 
1051
void SQL_SELECT::cleanup()
 
1052
{
 
1053
  delete quick;
 
1054
  quick= 0;
394
1055
  if (free_cond)
395
1056
  {
396
 
    free_cond= 0;
 
1057
    free_cond=0;
397
1058
    delete cond;
398
1059
    cond= 0;
399
1060
  }
400
 
  close_cached_file(file);
 
1061
  close_cached_file(&file);
401
1062
}
402
1063
 
403
1064
 
404
 
optimizer::SqlSelect::~SqlSelect()
 
1065
SQL_SELECT::~SQL_SELECT()
405
1066
{
406
1067
  cleanup();
407
1068
}
408
1069
 
409
 
 
410
 
bool optimizer::SqlSelect::check_quick(Session *session, 
411
 
                                       bool force_quick_range,
412
 
                                       ha_rows limit)
413
 
{
414
 
  key_map tmp;
415
 
  tmp.set();
416
 
  return (test_quick_select(session, 
417
 
                           tmp, 
418
 
                           0, 
419
 
                           limit,
420
 
                           force_quick_range, 
421
 
                           false) < 0);
422
 
}
423
 
 
424
 
 
425
 
bool optimizer::SqlSelect::skip_record()
426
 
{
427
 
  return (cond ? cond->val_int() == 0 : 0);
428
 
}
429
 
 
430
 
 
431
 
optimizer::QuickSelectInterface::QuickSelectInterface()
432
 
  :
433
 
    max_used_key_length(0),
434
 
    used_key_parts(0)
435
 
{}
 
1070
#undef index                                    // Fix for Unixware 7
 
1071
 
 
1072
QUICK_SELECT_I::QUICK_SELECT_I()
 
1073
  :max_used_key_length(0),
 
1074
   used_key_parts(0)
 
1075
{}
 
1076
 
 
1077
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
 
1078
                                       bool no_alloc, MEM_ROOT *parent_alloc,
 
1079
                                       bool *create_error)
 
1080
  :free_file(0),cur_range(NULL),last_range(0),dont_free(0)
 
1081
{
 
1082
  my_bitmap_map *bitmap;
 
1083
  DBUG_ENTER("QUICK_RANGE_SELECT::QUICK_RANGE_SELECT");
 
1084
 
 
1085
  in_ror_merged_scan= 0;
 
1086
  sorted= 0;
 
1087
  index= key_nr;
 
1088
  head=  table;
 
1089
  key_part_info= head->key_info[index].key_part;
 
1090
  my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
 
1091
 
 
1092
  /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
 
1093
  mrr_buf_size= thd->variables.read_rnd_buff_size;
 
1094
  mrr_buf_desc= NULL;
 
1095
 
 
1096
  if (!no_alloc && !parent_alloc)
 
1097
  {
 
1098
    // Allocates everything through the internal memroot
 
1099
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1100
    thd->mem_root= &alloc;
 
1101
  }
 
1102
  else
 
1103
    bzero((char*) &alloc,sizeof(alloc));
 
1104
  file= head->file;
 
1105
  record= head->record[0];
 
1106
  save_read_set= head->read_set;
 
1107
  save_write_set= head->write_set;
 
1108
 
 
1109
  /* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
 
1110
  if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size,
 
1111
                                           MYF(MY_WME))))
 
1112
  {
 
1113
    column_bitmap.bitmap= 0;
 
1114
    *create_error= 1;
 
1115
  }
 
1116
  else
 
1117
    bitmap_init(&column_bitmap, bitmap, head->s->fields, false);
 
1118
  DBUG_VOID_RETURN;
 
1119
}
 
1120
 
 
1121
 
 
1122
int QUICK_RANGE_SELECT::init()
 
1123
{
 
1124
  DBUG_ENTER("QUICK_RANGE_SELECT::init");
 
1125
 
 
1126
  if (file->inited != handler::NONE)
 
1127
    file->ha_index_or_rnd_end();
 
1128
  DBUG_RETURN(file->ha_index_init(index, 1));
 
1129
}
 
1130
 
 
1131
 
 
1132
void QUICK_RANGE_SELECT::range_end()
 
1133
{
 
1134
  if (file->inited != handler::NONE)
 
1135
    file->ha_index_or_rnd_end();
 
1136
}
 
1137
 
 
1138
 
 
1139
QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
 
1140
{
 
1141
  DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
 
1142
  if (!dont_free)
 
1143
  {
 
1144
    /* file is NULL for CPK scan on covering ROR-intersection */
 
1145
    if (file) 
 
1146
    {
 
1147
      range_end();
 
1148
      if (head->key_read)
 
1149
      {
 
1150
        head->key_read= 0;
 
1151
        file->extra(HA_EXTRA_NO_KEYREAD);
 
1152
      }
 
1153
      if (free_file)
 
1154
      {
 
1155
        DBUG_PRINT("info", ("Freeing separate handler 0x%lx (free: %d)", (long) file,
 
1156
                            free_file));
 
1157
        file->ha_external_lock(current_thd, F_UNLCK);
 
1158
        file->close();
 
1159
        delete file;
 
1160
      }
 
1161
    }
 
1162
    delete_dynamic(&ranges); /* ranges are allocated in alloc */
 
1163
    free_root(&alloc,MYF(0));
 
1164
    my_free((char*) column_bitmap.bitmap, MYF(MY_ALLOW_ZERO_PTR));
 
1165
  }
 
1166
  head->column_bitmaps_set(save_read_set, save_write_set);
 
1167
  x_free(mrr_buf_desc);
 
1168
  DBUG_VOID_RETURN;
 
1169
}
 
1170
 
 
1171
 
 
1172
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
 
1173
                                                   TABLE *table)
 
1174
  :pk_quick_select(NULL), thd(thd_param)
 
1175
{
 
1176
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT");
 
1177
  index= MAX_KEY;
 
1178
  head= table;
 
1179
  bzero(&read_record, sizeof(read_record));
 
1180
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1181
  DBUG_VOID_RETURN;
 
1182
}
 
1183
 
 
1184
int QUICK_INDEX_MERGE_SELECT::init()
 
1185
{
 
1186
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::init");
 
1187
  DBUG_RETURN(0);
 
1188
}
 
1189
 
 
1190
int QUICK_INDEX_MERGE_SELECT::reset()
 
1191
{
 
1192
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::reset");
 
1193
  DBUG_RETURN(read_keys_and_merge());
 
1194
}
 
1195
 
 
1196
bool
 
1197
QUICK_INDEX_MERGE_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
 
1198
{
 
1199
  /*
 
1200
    Save quick_select that does scan on clustered primary key as it will be
 
1201
    processed separately.
 
1202
  */
 
1203
  if (head->file->primary_key_is_clustered() &&
 
1204
      quick_sel_range->index == head->s->primary_key)
 
1205
    pk_quick_select= quick_sel_range;
 
1206
  else
 
1207
    return quick_selects.push_back(quick_sel_range);
 
1208
  return 0;
 
1209
}
 
1210
 
 
1211
QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT()
 
1212
{
 
1213
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
 
1214
  QUICK_RANGE_SELECT* quick;
 
1215
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT");
 
1216
  quick_it.rewind();
 
1217
  while ((quick= quick_it++))
 
1218
    quick->file= NULL;
 
1219
  quick_selects.delete_elements();
 
1220
  delete pk_quick_select;
 
1221
  free_root(&alloc,MYF(0));
 
1222
  DBUG_VOID_RETURN;
 
1223
}
 
1224
 
 
1225
 
 
1226
QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
 
1227
                                                       TABLE *table,
 
1228
                                                       bool retrieve_full_rows,
 
1229
                                                       MEM_ROOT *parent_alloc)
 
1230
  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
 
1231
    scans_inited(false)
 
1232
{
 
1233
  index= MAX_KEY;
 
1234
  head= table;
 
1235
  record= head->record[0];
 
1236
  if (!parent_alloc)
 
1237
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1238
  else
 
1239
    bzero(&alloc, sizeof(MEM_ROOT));
 
1240
  last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc,
 
1241
                                  head->file->ref_length);
 
1242
}
 
1243
 
 
1244
 
 
1245
/*
 
1246
  Do post-constructor initialization.
 
1247
  SYNOPSIS
 
1248
    QUICK_ROR_INTERSECT_SELECT::init()
 
1249
 
 
1250
  RETURN
 
1251
    0      OK
 
1252
    other  Error code
 
1253
*/
 
1254
 
 
1255
int QUICK_ROR_INTERSECT_SELECT::init()
 
1256
{
 
1257
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init");
 
1258
 /* Check if last_rowid was successfully allocated in ctor */
 
1259
  DBUG_RETURN(!last_rowid);
 
1260
}
 
1261
 
 
1262
 
 
1263
/*
 
1264
  Initialize this quick select to be a ROR-merged scan.
 
1265
 
 
1266
  SYNOPSIS
 
1267
    QUICK_RANGE_SELECT::init_ror_merged_scan()
 
1268
      reuse_handler If true, use head->file, otherwise create a separate
 
1269
                    handler object
 
1270
 
 
1271
  NOTES
 
1272
    This function creates and prepares for subsequent use a separate handler
 
1273
    object if it can't reuse head->file. The reason for this is that during
 
1274
    ROR-merge several key scans are performed simultaneously, and a single
 
1275
    handler is only capable of preserving context of a single key scan.
 
1276
 
 
1277
    In ROR-merge the quick select doing merge does full records retrieval,
 
1278
    merged quick selects read only keys.
 
1279
 
 
1280
  RETURN
 
1281
    0  ROR child scan initialized, ok to use.
 
1282
    1  error
 
1283
*/
 
1284
 
 
1285
int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
 
1286
{
 
1287
  handler *save_file= file, *org_file;
 
1288
  THD *thd;
 
1289
  DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");
 
1290
 
 
1291
  in_ror_merged_scan= 1;
 
1292
  if (reuse_handler)
 
1293
  {
 
1294
    DBUG_PRINT("info", ("Reusing handler 0x%lx", (long) file));
 
1295
    if (init() || reset())
 
1296
    {
 
1297
      DBUG_RETURN(1);
 
1298
    }
 
1299
    head->column_bitmaps_set(&column_bitmap, &column_bitmap);
 
1300
    goto end;
 
1301
  }
 
1302
 
 
1303
  /* Create a separate handler object for this quick select */
 
1304
  if (free_file)
 
1305
  {
 
1306
    /* already have own 'handler' object. */
 
1307
    DBUG_RETURN(0);
 
1308
  }
 
1309
 
 
1310
  thd= head->in_use;
 
1311
  if (!(file= head->file->clone(thd->mem_root)))
 
1312
  {
 
1313
    /* 
 
1314
      Manually set the error flag. Note: there seems to be quite a few
 
1315
      places where a failure could cause the server to "hang" the client by
 
1316
      sending no response to a query. ATM those are not real errors because 
 
1317
      the storage engine calls in question happen to never fail with the 
 
1318
      existing storage engines. 
 
1319
    */
 
1320
    my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
 
1321
    /* Caller will free the memory */
 
1322
    goto failure;  /* purecov: inspected */
 
1323
  }
 
1324
 
 
1325
  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
 
1326
 
 
1327
  if (file->ha_external_lock(thd, F_RDLCK))
 
1328
    goto failure;
 
1329
 
 
1330
  if (init() || reset())
 
1331
  {
 
1332
    file->ha_external_lock(thd, F_UNLCK);
 
1333
    file->close();
 
1334
    goto failure;
 
1335
  }
 
1336
  free_file= true;
 
1337
  last_rowid= file->ref;
 
1338
 
 
1339
end:
 
1340
  /*
 
1341
    We are only going to read key fields and call position() on 'file'
 
1342
    The following sets head->tmp_set to only use this key and then updates
 
1343
    head->read_set and head->write_set to use this bitmap.
 
1344
    The now bitmap is stored in 'column_bitmap' which is used in ::get_next()
 
1345
  */
 
1346
  org_file= head->file;
 
1347
  head->file= file;
 
1348
  /* We don't have to set 'head->keyread' here as the 'file' is unique */
 
1349
  if (!head->no_keyread)
 
1350
  {
 
1351
    head->key_read= 1;
 
1352
    head->mark_columns_used_by_index(index);
 
1353
  }
 
1354
  head->prepare_for_position();
 
1355
  head->file= org_file;
 
1356
  bitmap_copy(&column_bitmap, head->read_set);
 
1357
  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
 
1358
 
 
1359
  DBUG_RETURN(0);
 
1360
 
 
1361
failure:
 
1362
  head->column_bitmaps_set(save_read_set, save_write_set);
 
1363
  delete file;
 
1364
  file= save_file;
 
1365
  DBUG_RETURN(1);
 
1366
}
 
1367
 
 
1368
 
 
1369
/*
 
1370
  Initialize this quick select to be a part of a ROR-merged scan.
 
1371
  SYNOPSIS
 
1372
    QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
 
1373
      reuse_handler If true, use head->file, otherwise create separate
 
1374
                    handler object.
 
1375
  RETURN
 
1376
    0     OK
 
1377
    other error code
 
1378
*/
 
1379
int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler)
 
1380
{
 
1381
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
 
1382
  QUICK_RANGE_SELECT* quick;
 
1383
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");
 
1384
 
 
1385
  /* Initialize all merged "children" quick selects */
 
1386
  DBUG_ASSERT(!need_to_fetch_row || reuse_handler);
 
1387
  if (!need_to_fetch_row && reuse_handler)
 
1388
  {
 
1389
    quick= quick_it++;
 
1390
    /*
 
1391
      There is no use of this->file. Use it for the first of merged range
 
1392
      selects.
 
1393
    */
 
1394
    if (quick->init_ror_merged_scan(true))
 
1395
      DBUG_RETURN(1);
 
1396
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
 
1397
  }
 
1398
  while ((quick= quick_it++))
 
1399
  {
 
1400
    if (quick->init_ror_merged_scan(false))
 
1401
      DBUG_RETURN(1);
 
1402
    quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
 
1403
    /* All merged scans share the same record buffer in intersection. */
 
1404
    quick->record= head->record[0];
 
1405
  }
 
1406
 
 
1407
  if (need_to_fetch_row && head->file->ha_rnd_init(1))
 
1408
  {
 
1409
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
 
1410
    DBUG_RETURN(1);
 
1411
  }
 
1412
  DBUG_RETURN(0);
 
1413
}
 
1414
 
 
1415
 
 
1416
/*
 
1417
  Initialize quick select for row retrieval.
 
1418
  SYNOPSIS
 
1419
    reset()
 
1420
  RETURN
 
1421
    0      OK
 
1422
    other  Error code
 
1423
*/
 
1424
 
 
1425
int QUICK_ROR_INTERSECT_SELECT::reset()
 
1426
{
 
1427
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
 
1428
  if (!scans_inited && init_ror_merged_scan(true))
 
1429
    DBUG_RETURN(1);
 
1430
  scans_inited= true;
 
1431
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
1432
  QUICK_RANGE_SELECT *quick;
 
1433
  while ((quick= it++))
 
1434
    quick->reset();
 
1435
  DBUG_RETURN(0);
 
1436
}
 
1437
 
 
1438
 
 
1439
/*
 
1440
  Add a merged quick select to this ROR-intersection quick select.
 
1441
 
 
1442
  SYNOPSIS
 
1443
    QUICK_ROR_INTERSECT_SELECT::push_quick_back()
 
1444
      quick Quick select to be added. The quick select must return
 
1445
            rows in rowid order.
 
1446
  NOTES
 
1447
    This call can only be made before init() is called.
 
1448
 
 
1449
  RETURN
 
1450
    false OK
 
1451
    true  Out of memory.
 
1452
*/
 
1453
 
 
1454
bool
 
1455
QUICK_ROR_INTERSECT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick)
 
1456
{
 
1457
  return quick_selects.push_back(quick);
 
1458
}
 
1459
 
 
1460
QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
 
1461
{
 
1462
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
 
1463
  quick_selects.delete_elements();
 
1464
  delete cpk_quick;
 
1465
  free_root(&alloc,MYF(0));
 
1466
  if (need_to_fetch_row && head->file->inited != handler::NONE)
 
1467
    head->file->ha_rnd_end();
 
1468
  DBUG_VOID_RETURN;
 
1469
}
 
1470
 
 
1471
 
 
1472
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
 
1473
                                               TABLE *table)
 
1474
  : thd(thd_param), scans_inited(false)
 
1475
{
 
1476
  index= MAX_KEY;
 
1477
  head= table;
 
1478
  rowid_length= table->file->ref_length;
 
1479
  record= head->record[0];
 
1480
  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
1481
  thd_param->mem_root= &alloc;
 
1482
}
 
1483
 
 
1484
 
 
1485
/*
 
1486
  Do post-constructor initialization.
 
1487
  SYNOPSIS
 
1488
    QUICK_ROR_UNION_SELECT::init()
 
1489
 
 
1490
  RETURN
 
1491
    0      OK
 
1492
    other  Error code
 
1493
*/
 
1494
 
 
1495
int QUICK_ROR_UNION_SELECT::init()
 
1496
{
 
1497
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::init");
 
1498
  if (init_queue(&queue, quick_selects.elements, 0,
 
1499
                 false , QUICK_ROR_UNION_SELECT::queue_cmp,
 
1500
                 (void*) this))
 
1501
  {
 
1502
    bzero(&queue, sizeof(QUEUE));
 
1503
    DBUG_RETURN(1);
 
1504
  }
 
1505
 
 
1506
  if (!(cur_rowid= (uchar*) alloc_root(&alloc, 2*head->file->ref_length)))
 
1507
    DBUG_RETURN(1);
 
1508
  prev_rowid= cur_rowid + head->file->ref_length;
 
1509
  DBUG_RETURN(0);
 
1510
}
 
1511
 
 
1512
 
 
1513
/*
 
1514
  Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
 
1515
  queue.
 
1516
 
 
1517
  SYNPOSIS
 
1518
    QUICK_ROR_UNION_SELECT::queue_cmp()
 
1519
      arg   Pointer to QUICK_ROR_UNION_SELECT
 
1520
      val1  First merged select
 
1521
      val2  Second merged select
 
1522
*/
 
1523
 
 
1524
int QUICK_ROR_UNION_SELECT::queue_cmp(void *arg, uchar *val1, uchar *val2)
 
1525
{
 
1526
  QUICK_ROR_UNION_SELECT *self= (QUICK_ROR_UNION_SELECT*)arg;
 
1527
  return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
 
1528
                                   ((QUICK_SELECT_I*)val2)->last_rowid);
 
1529
}
 
1530
 
 
1531
 
 
1532
/*
 
1533
  Initialize quick select for row retrieval.
 
1534
  SYNOPSIS
 
1535
    reset()
 
1536
 
 
1537
  RETURN
 
1538
    0      OK
 
1539
    other  Error code
 
1540
*/
 
1541
 
 
1542
int QUICK_ROR_UNION_SELECT::reset()
 
1543
{
 
1544
  QUICK_SELECT_I *quick;
 
1545
  int error;
 
1546
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
 
1547
  have_prev_rowid= false;
 
1548
  if (!scans_inited)
 
1549
  {
 
1550
    List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
1551
    while ((quick= it++))
 
1552
    {
 
1553
      if (quick->init_ror_merged_scan(false))
 
1554
        DBUG_RETURN(1);
 
1555
    }
 
1556
    scans_inited= true;
 
1557
  }
 
1558
  queue_remove_all(&queue);
 
1559
  /*
 
1560
    Initialize scans for merged quick selects and put all merged quick
 
1561
    selects into the queue.
 
1562
  */
 
1563
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
1564
  while ((quick= it++))
 
1565
  {
 
1566
    if (quick->reset())
 
1567
      DBUG_RETURN(1);
 
1568
    if ((error= quick->get_next()))
 
1569
    {
 
1570
      if (error == HA_ERR_END_OF_FILE)
 
1571
        continue;
 
1572
      DBUG_RETURN(error);
 
1573
    }
 
1574
    quick->save_last_pos();
 
1575
    queue_insert(&queue, (uchar*)quick);
 
1576
  }
 
1577
 
 
1578
  if (head->file->ha_rnd_init(1))
 
1579
  {
 
1580
    DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
 
1581
    DBUG_RETURN(1);
 
1582
  }
 
1583
 
 
1584
  DBUG_RETURN(0);
 
1585
}
 
1586
 
 
1587
 
 
1588
bool
 
1589
QUICK_ROR_UNION_SELECT::push_quick_back(QUICK_SELECT_I *quick_sel_range)
 
1590
{
 
1591
  return quick_selects.push_back(quick_sel_range);
 
1592
}
 
1593
 
 
1594
QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT()
 
1595
{
 
1596
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT");
 
1597
  delete_queue(&queue);
 
1598
  quick_selects.delete_elements();
 
1599
  if (head->file->inited != handler::NONE)
 
1600
    head->file->ha_rnd_end();
 
1601
  free_root(&alloc,MYF(0));
 
1602
  DBUG_VOID_RETURN;
 
1603
}
 
1604
 
 
1605
 
 
1606
QUICK_RANGE::QUICK_RANGE()
 
1607
  :min_key(0),max_key(0),min_length(0),max_length(0),
 
1608
   flag(NO_MIN_RANGE | NO_MAX_RANGE),
 
1609
  min_keypart_map(0), max_keypart_map(0)
 
1610
{}
 
1611
 
 
1612
SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
 
1613
{
 
1614
  type=arg.type;
 
1615
  min_flag=arg.min_flag;
 
1616
  max_flag=arg.max_flag;
 
1617
  maybe_flag=arg.maybe_flag;
 
1618
  maybe_null=arg.maybe_null;
 
1619
  part=arg.part;
 
1620
  field=arg.field;
 
1621
  min_value=arg.min_value;
 
1622
  max_value=arg.max_value;
 
1623
  next_key_part=arg.next_key_part;
 
1624
  use_count=1; elements=1;
 
1625
}
 
1626
 
 
1627
 
 
1628
inline void SEL_ARG::make_root()
 
1629
{
 
1630
  left=right= &null_element;
 
1631
  color=BLACK;
 
1632
  next=prev=0;
 
1633
  use_count=0; elements=1;
 
1634
}
 
1635
 
 
1636
SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
 
1637
                 const uchar *max_value_arg)
 
1638
  :min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
 
1639
   elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
 
1640
   max_value((uchar*) max_value_arg), next(0),prev(0),
 
1641
   next_key_part(0),color(BLACK),type(KEY_RANGE)
 
1642
{
 
1643
  left=right= &null_element;
 
1644
}
 
1645
 
 
1646
SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
 
1647
                 uchar *min_value_, uchar *max_value_,
 
1648
                 uint8 min_flag_,uint8 max_flag_,uint8 maybe_flag_)
 
1649
  :min_flag(min_flag_),max_flag(max_flag_),maybe_flag(maybe_flag_),
 
1650
   part(part_),maybe_null(field_->real_maybe_null()), elements(1),use_count(1),
 
1651
   field(field_), min_value(min_value_), max_value(max_value_),
 
1652
   next(0),prev(0),next_key_part(0),color(BLACK),type(KEY_RANGE)
 
1653
{
 
1654
  left=right= &null_element;
 
1655
}
 
1656
 
 
1657
SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, 
 
1658
                        SEL_ARG **next_arg)
 
1659
{
 
1660
  SEL_ARG *tmp;
 
1661
 
 
1662
  /* Bail out if we have already generated too many SEL_ARGs */
 
1663
  if (++param->alloced_sel_args > MAX_SEL_ARGS)
 
1664
    return 0;
 
1665
 
 
1666
  if (type != KEY_RANGE)
 
1667
  {
 
1668
    if (!(tmp= new (param->mem_root) SEL_ARG(type)))
 
1669
      return 0;                                 // out of memory
 
1670
    tmp->prev= *next_arg;                       // Link into next/prev chain
 
1671
    (*next_arg)->next=tmp;
 
1672
    (*next_arg)= tmp;
 
1673
  }
 
1674
  else
 
1675
  {
 
1676
    if (!(tmp= new (param->mem_root) SEL_ARG(field,part, min_value,max_value,
 
1677
                                             min_flag, max_flag, maybe_flag)))
 
1678
      return 0;                                 // OOM
 
1679
    tmp->parent=new_parent;
 
1680
    tmp->next_key_part=next_key_part;
 
1681
    if (left != &null_element)
 
1682
      if (!(tmp->left=left->clone(param, tmp, next_arg)))
 
1683
        return 0;                               // OOM
 
1684
 
 
1685
    tmp->prev= *next_arg;                       // Link into next/prev chain
 
1686
    (*next_arg)->next=tmp;
 
1687
    (*next_arg)= tmp;
 
1688
 
 
1689
    if (right != &null_element)
 
1690
      if (!(tmp->right= right->clone(param, tmp, next_arg)))
 
1691
        return 0;                               // OOM
 
1692
  }
 
1693
  increment_use_count(1);
 
1694
  tmp->color= color;
 
1695
  tmp->elements= this->elements;
 
1696
  return tmp;
 
1697
}
 
1698
 
 
1699
SEL_ARG *SEL_ARG::first()
 
1700
{
 
1701
  SEL_ARG *next_arg=this;
 
1702
  if (!next_arg->left)
 
1703
    return 0;                                   // MAYBE_KEY
 
1704
  while (next_arg->left != &null_element)
 
1705
    next_arg=next_arg->left;
 
1706
  return next_arg;
 
1707
}
 
1708
 
 
1709
SEL_ARG *SEL_ARG::last()
 
1710
{
 
1711
  SEL_ARG *next_arg=this;
 
1712
  if (!next_arg->right)
 
1713
    return 0;                                   // MAYBE_KEY
 
1714
  while (next_arg->right != &null_element)
 
1715
    next_arg=next_arg->right;
 
1716
  return next_arg;
 
1717
}
 
1718
 
 
1719
 
 
1720
/*
 
1721
  Check if a compare is ok, when one takes ranges in account
 
1722
  Returns -2 or 2 if the ranges where 'joined' like  < 2 and >= 2
 
1723
*/
 
1724
 
 
1725
static int sel_cmp(Field *field, uchar *a, uchar *b, uint8 a_flag,
 
1726
                   uint8 b_flag)
 
1727
{
 
1728
  int cmp;
 
1729
  /* First check if there was a compare to a min or max element */
 
1730
  if (a_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
 
1731
  {
 
1732
    if ((a_flag & (NO_MIN_RANGE | NO_MAX_RANGE)) ==
 
1733
        (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE)))
 
1734
      return 0;
 
1735
    return (a_flag & NO_MIN_RANGE) ? -1 : 1;
 
1736
  }
 
1737
  if (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
 
1738
    return (b_flag & NO_MIN_RANGE) ? 1 : -1;
 
1739
 
 
1740
  if (field->real_maybe_null())                 // If null is part of key
 
1741
  {
 
1742
    if (*a != *b)
 
1743
    {
 
1744
      return *a ? -1 : 1;
 
1745
    }
 
1746
    if (*a)
 
1747
      goto end;                                 // NULL where equal
 
1748
    a++; b++;                                   // Skip NULL marker
 
1749
  }
 
1750
  cmp=field->key_cmp(a , b);
 
1751
  if (cmp) return cmp < 0 ? -1 : 1;             // The values differed
 
1752
 
 
1753
  // Check if the compared equal arguments was defined with open/closed range
 
1754
 end:
 
1755
  if (a_flag & (NEAR_MIN | NEAR_MAX))
 
1756
  {
 
1757
    if ((a_flag & (NEAR_MIN | NEAR_MAX)) == (b_flag & (NEAR_MIN | NEAR_MAX)))
 
1758
      return 0;
 
1759
    if (!(b_flag & (NEAR_MIN | NEAR_MAX)))
 
1760
      return (a_flag & NEAR_MIN) ? 2 : -2;
 
1761
    return (a_flag & NEAR_MIN) ? 1 : -1;
 
1762
  }
 
1763
  if (b_flag & (NEAR_MIN | NEAR_MAX))
 
1764
    return (b_flag & NEAR_MIN) ? -2 : 2;
 
1765
  return 0;                                     // The elements where equal
 
1766
}
 
1767
 
 
1768
 
 
1769
SEL_ARG *SEL_ARG::clone_tree(RANGE_OPT_PARAM *param)
 
1770
{
 
1771
  SEL_ARG tmp_link,*next_arg,*root;
 
1772
  next_arg= &tmp_link;
 
1773
  if (!(root= clone(param, (SEL_ARG *) 0, &next_arg)))
 
1774
    return 0;
 
1775
  next_arg->next=0;                             // Fix last link
 
1776
  tmp_link.next->prev=0;                        // Fix first link
 
1777
  if (root)                                     // If not OOM
 
1778
    root->use_count= 0;
 
1779
  return root;
 
1780
}
436
1781
 
437
1782
 
438
1783
/*
445
1790
      limit  Number of records that will be retrieved
446
1791
 
447
1792
  DESCRIPTION
448
 
    Find the best index that allows to retrieve first #limit records in the
 
1793
    Find the best index that allows to retrieve first #limit records in the 
449
1794
    given order cheaper then one would retrieve them using full table scan.
450
1795
 
451
1796
  IMPLEMENTATION
464
1809
    MAX_KEY if no such index was found.
465
1810
*/
466
1811
 
467
 
uint32_t optimizer::get_index_for_order(Table *table, order_st *order, ha_rows limit)
 
1812
uint get_index_for_order(TABLE *table, ORDER *order, ha_rows limit)
468
1813
{
469
 
  uint32_t idx;
470
 
  uint32_t match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
471
 
  order_st *ord;
472
 
 
 
1814
  uint idx;
 
1815
  uint match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
 
1816
  ORDER *ord;
 
1817
  
473
1818
  for (ord= order; ord; ord= ord->next)
474
1819
    if (!ord->asc)
475
1820
      return MAX_KEY;
476
1821
 
477
 
  for (idx= 0; idx < table->getShare()->sizeKeys(); idx++)
 
1822
  for (idx= 0; idx < table->s->keys; idx++)
478
1823
  {
479
 
    if (!(table->keys_in_use_for_query.test(idx)))
 
1824
    if (!(table->keys_in_use_for_query.is_set(idx)))
480
1825
      continue;
481
 
    KeyPartInfo *keyinfo= table->key_info[idx].key_part;
482
 
    uint32_t n_parts=  table->key_info[idx].key_parts;
483
 
    uint32_t partno= 0;
484
 
 
485
 
    /*
486
 
      The below check is sufficient considering we now have either BTREE
487
 
      indexes (records are returned in order for any index prefix) or HASH
 
1826
    KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
 
1827
    uint n_parts=  table->key_info[idx].key_parts;
 
1828
    uint partno= 0;
 
1829
    
 
1830
    /* 
 
1831
      The below check is sufficient considering we now have either BTREE 
 
1832
      indexes (records are returned in order for any index prefix) or HASH 
488
1833
      indexes (records are not returned in order for any index prefix).
489
1834
    */
490
 
    if (! (table->index_flags(idx) & HA_READ_ORDER))
 
1835
    if (!(table->file->index_flags(idx, 0, 1) & HA_READ_ORDER))
491
1836
      continue;
492
1837
    for (ord= order; ord && partno < n_parts; ord= ord->next, partno++)
493
1838
    {
494
1839
      Item *item= order->item[0];
495
 
      if (! (item->type() == Item::FIELD_ITEM &&
 
1840
      if (!(item->type() == Item::FIELD_ITEM &&
496
1841
           ((Item_field*)item)->field->eq(keyinfo[partno].field)))
497
1842
        break;
498
1843
    }
499
 
 
500
 
    if (! ord && table->key_info[idx].key_length < match_key_len)
 
1844
    
 
1845
    if (!ord && table->key_info[idx].key_length < match_key_len)
501
1846
    {
502
 
      /*
 
1847
      /* 
503
1848
        Ok, the ordering is compatible and this key is shorter then
504
1849
        previous match (we want shorter keys as we'll have to read fewer
505
1850
        index pages for the same number of records)
511
1856
 
512
1857
  if (match_key != MAX_KEY)
513
1858
  {
514
 
    /*
515
 
      Found an index that allows records to be retrieved in the requested
 
1859
    /* 
 
1860
      Found an index that allows records to be retrieved in the requested 
516
1861
      order. Now we'll check if using the index is cheaper then doing a table
517
1862
      scan.
518
1863
    */
519
 
    double full_scan_time= table->cursor->scan_time();
520
 
    double index_scan_time= table->cursor->read_time(match_key, 1, limit);
 
1864
    double full_scan_time= table->file->scan_time();
 
1865
    double index_scan_time= table->file->read_time(match_key, 1, limit);
521
1866
    if (index_scan_time > full_scan_time)
522
1867
      match_key= MAX_KEY;
523
1868
  }
525
1870
}
526
1871
 
527
1872
 
 
1873
/*
 
1874
  Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
 
1875
  objects from table read plans.
 
1876
*/
 
1877
class TABLE_READ_PLAN
 
1878
{
 
1879
public:
 
1880
  /*
 
1881
    Plan read cost, with or without cost of full row retrieval, depending
 
1882
    on plan creation parameters.
 
1883
  */
 
1884
  double read_cost;
 
1885
  ha_rows records; /* estimate of #rows to be examined */
 
1886
 
 
1887
  /*
 
1888
    If true, the scan returns rows in rowid order. This is used only for
 
1889
    scans that can be both ROR and non-ROR.
 
1890
  */
 
1891
  bool is_ror;
 
1892
 
 
1893
  /*
 
1894
    Create quick select for this plan.
 
1895
    SYNOPSIS
 
1896
     make_quick()
 
1897
       param               Parameter from test_quick_select
 
1898
       retrieve_full_rows  If true, created quick select will do full record
 
1899
                           retrieval.
 
1900
       parent_alloc        Memory pool to use, if any.
 
1901
 
 
1902
    NOTES
 
1903
      retrieve_full_rows is ignored by some implementations.
 
1904
 
 
1905
    RETURN
 
1906
      created quick select
 
1907
      NULL on any error.
 
1908
  */
 
1909
  virtual QUICK_SELECT_I *make_quick(PARAM *param,
 
1910
                                     bool retrieve_full_rows,
 
1911
                                     MEM_ROOT *parent_alloc=NULL) = 0;
 
1912
 
 
1913
  /* Table read plans are allocated on MEM_ROOT and are never deleted */
 
1914
  static void *operator new(size_t size, MEM_ROOT *mem_root)
 
1915
  { return (void*) alloc_root(mem_root, (uint) size); }
 
1916
  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
 
1917
  static void operator delete(void *ptr, MEM_ROOT *mem_root) { /* Never called */ }
 
1918
  virtual ~TABLE_READ_PLAN() {}               /* Remove gcc warning */
 
1919
 
 
1920
};
 
1921
 
 
1922
class TRP_ROR_INTERSECT;
 
1923
class TRP_ROR_UNION;
 
1924
class TRP_INDEX_MERGE;
 
1925
 
 
1926
 
 
1927
/*
 
1928
  Plan for a QUICK_RANGE_SELECT scan.
 
1929
  TRP_RANGE::make_quick ignores retrieve_full_rows parameter because
 
1930
  QUICK_RANGE_SELECT doesn't distinguish between 'index only' scans and full
 
1931
  record retrieval scans.
 
1932
*/
 
1933
 
 
1934
class TRP_RANGE : public TABLE_READ_PLAN
 
1935
{
 
1936
public:
 
1937
  SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
 
1938
  uint     key_idx; /* key number in PARAM::key */
 
1939
  uint     mrr_flags; 
 
1940
  uint     mrr_buf_size;
 
1941
 
 
1942
  TRP_RANGE(SEL_ARG *key_arg, uint idx_arg, uint mrr_flags_arg)
 
1943
   : key(key_arg), key_idx(idx_arg), mrr_flags(mrr_flags_arg)
 
1944
  {}
 
1945
  virtual ~TRP_RANGE() {}                     /* Remove gcc warning */
 
1946
 
 
1947
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
 
1948
                             MEM_ROOT *parent_alloc)
 
1949
  {
 
1950
    DBUG_ENTER("TRP_RANGE::make_quick");
 
1951
    QUICK_RANGE_SELECT *quick;
 
1952
    if ((quick= get_quick_select(param, key_idx, key, mrr_flags, mrr_buf_size,
 
1953
                                 parent_alloc)))
 
1954
    {
 
1955
      quick->records= records;
 
1956
      quick->read_time= read_cost;
 
1957
    }
 
1958
    DBUG_RETURN(quick);
 
1959
  }
 
1960
};
 
1961
 
 
1962
 
 
1963
/* Plan for QUICK_ROR_INTERSECT_SELECT scan. */
 
1964
 
 
1965
class TRP_ROR_INTERSECT : public TABLE_READ_PLAN
 
1966
{
 
1967
public:
 
1968
  TRP_ROR_INTERSECT() {}                      /* Remove gcc warning */
 
1969
  virtual ~TRP_ROR_INTERSECT() {}             /* Remove gcc warning */
 
1970
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
 
1971
                             MEM_ROOT *parent_alloc);
 
1972
 
 
1973
  /* Array of pointers to ROR range scans used in this intersection */
 
1974
  struct st_ror_scan_info **first_scan;
 
1975
  struct st_ror_scan_info **last_scan; /* End of the above array */
 
1976
  struct st_ror_scan_info *cpk_scan;  /* Clustered PK scan, if there is one */
 
1977
  bool is_covering; /* true if no row retrieval phase is necessary */
 
1978
  double index_scan_costs; /* SUM(cost(index_scan)) */
 
1979
};
 
1980
 
 
1981
 
 
1982
/*
 
1983
  Plan for QUICK_ROR_UNION_SELECT scan.
 
1984
  QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
 
1985
  is ignored by make_quick.
 
1986
*/
 
1987
 
 
1988
class TRP_ROR_UNION : public TABLE_READ_PLAN
 
1989
{
 
1990
public:
 
1991
  TRP_ROR_UNION() {}                          /* Remove gcc warning */
 
1992
  virtual ~TRP_ROR_UNION() {}                 /* Remove gcc warning */
 
1993
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
 
1994
                             MEM_ROOT *parent_alloc);
 
1995
  TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
 
1996
  TABLE_READ_PLAN **last_ror;  /* end of the above array */
 
1997
};
 
1998
 
 
1999
 
 
2000
/*
 
2001
  Plan for QUICK_INDEX_MERGE_SELECT scan.
 
2002
  QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
 
2003
  is ignored by make_quick.
 
2004
*/
 
2005
 
 
2006
class TRP_INDEX_MERGE : public TABLE_READ_PLAN
 
2007
{
 
2008
public:
 
2009
  TRP_INDEX_MERGE() {}                        /* Remove gcc warning */
 
2010
  virtual ~TRP_INDEX_MERGE() {}               /* Remove gcc warning */
 
2011
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
 
2012
                             MEM_ROOT *parent_alloc);
 
2013
  TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
 
2014
  TRP_RANGE **range_scans_end; /* end of the array */
 
2015
};
 
2016
 
 
2017
 
 
2018
/*
 
2019
  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan. 
 
2020
*/
 
2021
 
 
2022
class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
 
2023
{
 
2024
private:
 
2025
  bool have_min, have_max;
 
2026
  KEY_PART_INFO *min_max_arg_part;
 
2027
  uint group_prefix_len;
 
2028
  uint used_key_parts;
 
2029
  uint group_key_parts;
 
2030
  KEY *index_info;
 
2031
  uint index;
 
2032
  uint key_infix_len;
 
2033
  uchar key_infix[MAX_KEY_LENGTH];
 
2034
  SEL_TREE *range_tree; /* Represents all range predicates in the query. */
 
2035
  SEL_ARG  *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
 
2036
  uint param_idx; /* Index of used key in param->key. */
 
2037
  /* Number of records selected by the ranges in index_tree. */
 
2038
public:
 
2039
  ha_rows quick_prefix_records;
 
2040
public:
 
2041
  TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg,
 
2042
                    KEY_PART_INFO *min_max_arg_part_arg,
 
2043
                    uint group_prefix_len_arg, uint used_key_parts_arg,
 
2044
                    uint group_key_parts_arg, KEY *index_info_arg,
 
2045
                    uint index_arg, uint key_infix_len_arg,
 
2046
                    uchar *key_infix_arg,
 
2047
                    SEL_TREE *tree_arg, SEL_ARG *index_tree_arg,
 
2048
                    uint param_idx_arg, ha_rows quick_prefix_records_arg)
 
2049
  : have_min(have_min_arg), have_max(have_max_arg),
 
2050
    min_max_arg_part(min_max_arg_part_arg),
 
2051
    group_prefix_len(group_prefix_len_arg), used_key_parts(used_key_parts_arg),
 
2052
    group_key_parts(group_key_parts_arg), index_info(index_info_arg),
 
2053
    index(index_arg), key_infix_len(key_infix_len_arg), range_tree(tree_arg),
 
2054
    index_tree(index_tree_arg), param_idx(param_idx_arg),
 
2055
    quick_prefix_records(quick_prefix_records_arg)
 
2056
    {
 
2057
      if (key_infix_len)
 
2058
        memcpy(this->key_infix, key_infix_arg, key_infix_len);
 
2059
    }
 
2060
  virtual ~TRP_GROUP_MIN_MAX() {}             /* Remove gcc warning */
 
2061
 
 
2062
  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
 
2063
                             MEM_ROOT *parent_alloc);
 
2064
};
 
2065
 
528
2066
 
529
2067
/*
530
2068
  Fill param->needed_fields with bitmap of fields used in the query.
540
2078
    1  Out of memory.
541
2079
*/
542
2080
 
543
 
static int fill_used_fields_bitmap(optimizer::Parameter *param)
 
2081
static int fill_used_fields_bitmap(PARAM *param)
544
2082
{
545
 
  Table *table= param->table;
 
2083
  TABLE *table= param->table;
546
2084
  my_bitmap_map *tmp;
547
 
  uint32_t pk;
548
 
  param->tmp_covered_fields.setBitmap(0);
549
 
  param->fields_bitmap_size= table->getShare()->column_bitmap_size;
550
 
  if (!(tmp= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)) ||
551
 
      param->needed_fields.init(tmp, table->getShare()->sizeFields()))
552
 
  {
 
2085
  uint pk;
 
2086
  param->tmp_covered_fields.bitmap= 0;
 
2087
  param->fields_bitmap_size= table->s->column_bitmap_size;
 
2088
  if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
 
2089
                                  param->fields_bitmap_size)) ||
 
2090
      bitmap_init(&param->needed_fields, tmp, table->s->fields, false))
553
2091
    return 1;
554
 
  }
555
2092
 
556
 
  param->needed_fields= *table->read_set;
 
2093
  bitmap_copy(&param->needed_fields, table->read_set);
557
2094
  bitmap_union(&param->needed_fields, table->write_set);
558
2095
 
559
 
  pk= param->table->getShare()->getPrimaryKey();
560
 
  if (pk != MAX_KEY && param->table->cursor->primary_key_is_clustered())
 
2096
  pk= param->table->s->primary_key;
 
2097
  if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
561
2098
  {
562
2099
    /* The table uses clustered PK and it is not internally generated */
563
 
    KeyPartInfo *key_part= param->table->key_info[pk].key_part;
564
 
    KeyPartInfo *key_part_end= key_part +
 
2100
    KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
 
2101
    KEY_PART_INFO *key_part_end= key_part +
565
2102
                                 param->table->key_info[pk].key_parts;
566
2103
    for (;key_part != key_part_end; ++key_part)
567
 
      param->needed_fields.clearBit(key_part->fieldnr-1);
 
2104
      bitmap_clear_bit(&param->needed_fields, key_part->fieldnr-1);
568
2105
  }
569
2106
  return 0;
570
2107
}
574
2111
  Test if a key can be used in different ranges
575
2112
 
576
2113
  SYNOPSIS
577
 
    SqlSelect::test_quick_select()
578
 
      session               Current thread
 
2114
    SQL_SELECT::test_quick_select()
 
2115
      thd               Current thread
579
2116
      keys_to_use       Keys to use for range retrieval
580
2117
      prev_tables       Tables assumed to be already read when the scan is
581
2118
                        performed (but not read at the moment of this call)
595
2132
 
596
2133
  IMPLEMENTATION
597
2134
    quick_condition_rows value is obtained as follows:
598
 
 
 
2135
      
599
2136
      It is a minimum of E(#output rows) for all considered table access
600
2137
      methods (range and index_merge accesses over various indexes).
601
 
 
 
2138
    
602
2139
    The obtained value is not a true E(#rows that satisfy table condition)
603
2140
    but rather a pessimistic estimate. To obtain a true E(#...) one would
604
2141
    need to combine estimates of various access methods, taking into account
605
2142
    correlations between sets of rows they will return.
606
 
 
 
2143
    
607
2144
    For example, if values of tbl.key1 and tbl.key2 are independent (a right
608
2145
    assumption if we have no information about their correlation) then the
609
2146
    correct estimate will be:
610
 
 
611
 
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) =
 
2147
    
 
2148
      E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) = 
612
2149
      = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
613
2150
 
614
 
    which is smaller than
615
 
 
 
2151
    which is smaller than 
 
2152
      
616
2153
       MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))
617
2154
 
618
2155
    which is currently produced.
619
2156
 
620
2157
  TODO
621
2158
   * Change the value returned in quick_condition_rows from a pessimistic
622
 
     estimate to true E(#rows that satisfy table condition).
623
 
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection
 
2159
     estimate to true E(#rows that satisfy table condition). 
 
2160
     (we can re-use some of E(#rows) calcuation code from index_merge/intersection 
624
2161
      for this)
625
 
 
 
2162
   
626
2163
   * Check if this function really needs to modify keys_to_use, and change the
627
2164
     code to pass it by reference if it doesn't.
628
2165
 
636
2173
    1 if found usable ranges and quick select has been successfully created.
637
2174
*/
638
2175
 
639
 
int optimizer::SqlSelect::test_quick_select(Session *session,
640
 
                                            key_map keys_to_use,
641
 
                                                                    table_map prev_tables,
642
 
                                                                    ha_rows limit,
643
 
                                            bool force_quick_range,
644
 
                                            bool ordered_output)
 
2176
int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
 
2177
                                  table_map prev_tables,
 
2178
                                  ha_rows limit, bool force_quick_range, 
 
2179
                                  bool ordered_output)
645
2180
{
646
 
  uint32_t idx;
 
2181
  uint idx;
647
2182
  double scan_time;
648
 
  if (quick)
649
 
  {
650
 
    delete quick;
651
 
    quick= NULL;
652
 
  }
653
 
  needed_reg.reset();
654
 
  quick_keys.reset();
655
 
  if (keys_to_use.none())
656
 
    return 0;
657
 
  records= head->cursor->stats.records;
 
2183
  DBUG_ENTER("SQL_SELECT::test_quick_select");
 
2184
  DBUG_PRINT("enter",("keys_to_use: %lu  prev_tables: %lu  const_tables: %lu",
 
2185
                      (ulong) keys_to_use.to_ulonglong(), (ulong) prev_tables,
 
2186
                      (ulong) const_tables));
 
2187
  DBUG_PRINT("info", ("records: %lu", (ulong) head->file->stats.records));
 
2188
  delete quick;
 
2189
  quick=0;
 
2190
  needed_reg.clear_all();
 
2191
  quick_keys.clear_all();
 
2192
  if (keys_to_use.is_clear_all())
 
2193
    DBUG_RETURN(0);
 
2194
  records= head->file->stats.records;
658
2195
  if (!records)
659
 
    records++;
 
2196
    records++;                                  /* purecov: inspected */
660
2197
  scan_time= (double) records / TIME_FOR_COMPARE + 1;
661
 
  read_time= (double) head->cursor->scan_time() + scan_time + 1.1;
 
2198
  read_time= (double) head->file->scan_time() + scan_time + 1.1;
662
2199
  if (head->force_index)
663
2200
    scan_time= read_time= DBL_MAX;
664
2201
  if (limit < records)
665
2202
    read_time= (double) records + scan_time + 1; // Force to use index
666
2203
  else if (read_time <= 2.0 && !force_quick_range)
667
 
    return 0;                           /* No need for quick select */
668
 
 
669
 
  keys_to_use&= head->keys_in_use_for_query;
670
 
  if (keys_to_use.any())
 
2204
    DBUG_RETURN(0);                             /* No need for quick select */
 
2205
 
 
2206
  DBUG_PRINT("info",("Time to scan table: %g", read_time));
 
2207
 
 
2208
  keys_to_use.intersect(head->keys_in_use_for_query);
 
2209
  if (!keys_to_use.is_clear_all())
671
2210
  {
672
 
    memory::Root alloc;
673
 
    optimizer::SEL_TREE *tree= NULL;
 
2211
    MEM_ROOT alloc;
 
2212
    SEL_TREE *tree= NULL;
674
2213
    KEY_PART *key_parts;
675
 
    KeyInfo *key_info;
676
 
    optimizer::Parameter param;
 
2214
    KEY *key_info;
 
2215
    PARAM param;
677
2216
 
678
 
    if (check_stack_overrun(session, 2*STACK_MIN_SIZE, NULL))
679
 
      return 0;                           // Fatal error flag is set
 
2217
    if (check_stack_overrun(thd, 2*STACK_MIN_SIZE, NULL))
 
2218
      DBUG_RETURN(0);                           // Fatal error flag is set
680
2219
 
681
2220
    /* set up parameter that is passed to all functions */
682
 
    param.session= session;
683
 
    param.prev_tables= prev_tables | const_tables;
684
 
    param.read_tables= read_tables;
 
2221
    param.thd= thd;
 
2222
    param.baseflag= head->file->ha_table_flags();
 
2223
    param.prev_tables=prev_tables | const_tables;
 
2224
    param.read_tables=read_tables;
685
2225
    param.current_table= head->map;
686
2226
    param.table=head;
687
2227
    param.keys=0;
688
2228
    param.mem_root= &alloc;
689
 
    param.old_root= session->mem_root;
 
2229
    param.old_root= thd->mem_root;
690
2230
    param.needed_reg= &needed_reg;
691
2231
    param.imerge_cost_buff_size= 0;
692
2232
    param.using_real_indexes= true;
693
2233
    param.remove_jump_scans= true;
694
2234
    param.force_default_mrr= ordered_output;
695
2235
 
696
 
    session->no_errors=1;                               // Don't warn about NULL
697
 
    memory::init_sql_alloc(&alloc, session->variables.range_alloc_block_size, 0);
698
 
    if (!(param.key_parts= (KEY_PART*) alloc.alloc_root( sizeof(KEY_PART) * head->getShare()->key_parts)) ||
 
2236
    thd->no_errors=1;                           // Don't warn about NULL
 
2237
    init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
 
2238
    if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc,
 
2239
                                                  sizeof(KEY_PART)*
 
2240
                                                  head->s->key_parts)) ||
699
2241
        fill_used_fields_bitmap(&param))
700
2242
    {
701
 
      session->no_errors=0;
702
 
      alloc.free_root(MYF(0));                  // Return memory & allocator
703
 
 
704
 
      return 0;                         // Can't use range
 
2243
      thd->no_errors=0;
 
2244
      free_root(&alloc,MYF(0));                 // Return memory & allocator
 
2245
      DBUG_RETURN(0);                           // Can't use range
705
2246
    }
706
2247
    key_parts= param.key_parts;
707
 
    session->mem_root= &alloc;
 
2248
    thd->mem_root= &alloc;
708
2249
 
709
2250
    /*
710
2251
      Make an array with description of all key parts of all table keys.
711
2252
      This is used in get_mm_parts function.
712
2253
    */
713
2254
    key_info= head->key_info;
714
 
    for (idx=0 ; idx < head->getShare()->sizeKeys() ; idx++, key_info++)
 
2255
    for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
715
2256
    {
716
 
      KeyPartInfo *key_part_info;
717
 
      if (! keys_to_use.test(idx))
 
2257
      KEY_PART_INFO *key_part_info;
 
2258
      if (!keys_to_use.is_set(idx))
718
2259
        continue;
719
2260
 
720
2261
      param.key[param.keys]=key_parts;
721
2262
      key_part_info= key_info->key_part;
722
 
      for (uint32_t part=0;
723
 
           part < key_info->key_parts;
724
 
           part++, key_parts++, key_part_info++)
 
2263
      for (uint part=0 ; part < key_info->key_parts ;
 
2264
           part++, key_parts++, key_part_info++)
725
2265
      {
726
 
        key_parts->key= param.keys;
727
 
        key_parts->part= part;
728
 
        key_parts->length= key_part_info->length;
729
 
        key_parts->store_length= key_part_info->store_length;
730
 
        key_parts->field= key_part_info->field;
731
 
        key_parts->null_bit= key_part_info->null_bit;
 
2266
        key_parts->key=          param.keys;
 
2267
        key_parts->part=         part;
 
2268
        key_parts->length=       key_part_info->length;
 
2269
        key_parts->store_length= key_part_info->store_length;
 
2270
        key_parts->field=        key_part_info->field;
 
2271
        key_parts->null_bit=     key_part_info->null_bit;
 
2272
        key_parts->image_type =  Field::itRAW;
732
2273
        /* Only HA_PART_KEY_SEG is used */
733
 
        key_parts->flag= (uint8_t) key_part_info->key_part_flag;
 
2274
        key_parts->flag=         (uint8) key_part_info->key_part_flag;
734
2275
      }
735
2276
      param.real_keynr[param.keys++]=idx;
736
2277
    }
738
2279
    param.alloced_sel_args= 0;
739
2280
 
740
2281
    /* Calculate cost of full index read for the shortest covering index */
741
 
    if (!head->covering_keys.none())
 
2282
    if (!head->covering_keys.is_clear_all())
742
2283
    {
743
 
      int key_for_use= head->find_shortest_key(&head->covering_keys);
744
 
      double key_read_time=
745
 
        param.table->cursor->index_only_read_time(key_for_use,
 
2284
      int key_for_use= find_shortest_key(head, &head->covering_keys);
 
2285
      double key_read_time= 
 
2286
        param.table->file->index_only_read_time(key_for_use, 
746
2287
                                                rows2double(records)) +
747
2288
        (double) records / TIME_FOR_COMPARE;
 
2289
      DBUG_PRINT("info",  ("'all'+'using index' scan will be using key %d, "
 
2290
                           "read time %g", key_for_use, key_read_time));
748
2291
      if (key_read_time < read_time)
749
2292
        read_time= key_read_time;
750
2293
    }
751
2294
 
752
 
    optimizer::TableReadPlan *best_trp= NULL;
753
 
    optimizer::GroupMinMaxReadPlan *group_trp= NULL;
 
2295
    TABLE_READ_PLAN *best_trp= NULL;
 
2296
    TRP_GROUP_MIN_MAX *group_trp;
754
2297
    double best_read_time= read_time;
755
2298
 
756
2299
    if (cond)
757
2300
    {
758
2301
      if ((tree= get_mm_tree(&param,cond)))
759
2302
      {
760
 
        if (tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
 
2303
        if (tree->type == SEL_TREE::IMPOSSIBLE)
761
2304
        {
762
2305
          records=0L;                      /* Return -1 from this function. */
763
2306
          read_time= (double) HA_POS_ERROR;
767
2310
          If the tree can't be used for range scans, proceed anyway, as we
768
2311
          can construct a group-min-max quick select
769
2312
        */
770
 
        if (tree->type != optimizer::SEL_TREE::KEY && tree->type != optimizer::SEL_TREE::KEY_SMALLER)
 
2313
        if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
771
2314
          tree= NULL;
772
2315
      }
773
2316
    }
774
2317
 
775
2318
    /*
776
 
      Try to construct a QuickGroupMinMaxSelect.
 
2319
      Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
777
2320
      Notice that it can be constructed no matter if there is a range tree.
778
2321
    */
779
2322
    group_trp= get_best_group_min_max(&param, tree);
780
2323
    if (group_trp)
781
2324
    {
782
2325
      param.table->quick_condition_rows= min(group_trp->records,
783
 
                                             head->cursor->stats.records);
 
2326
                                             head->file->stats.records);
784
2327
      if (group_trp->read_cost < best_read_time)
785
2328
      {
786
2329
        best_trp= group_trp;
796
2339
      */
797
2340
      if (tree->merges.is_empty())
798
2341
      {
799
 
        optimizer::RangeReadPlan *range_trp= NULL;
800
 
        optimizer::RorIntersectReadPlan *rori_trp= NULL;
 
2342
        TRP_RANGE         *range_trp;
 
2343
        TRP_ROR_INTERSECT *rori_trp;
801
2344
        bool can_build_covering= false;
802
2345
 
803
2346
        /* Get best 'range' plan and prepare data for making other plans */
804
 
        if ((range_trp= get_key_scans_params(session, &param, tree, false, true,
 
2347
        if ((range_trp= get_key_scans_params(&param, tree, false, true,
805
2348
                                             best_read_time)))
806
2349
        {
807
2350
          best_trp= range_trp;
809
2352
        }
810
2353
 
811
2354
        /*
812
 
          Simultaneous key scans and row deletes on several Cursor
 
2355
          Simultaneous key scans and row deletes on several handler
813
2356
          objects are not allowed so don't use ROR-intersection for
814
2357
          table deletes.
815
2358
        */
816
 
        if ((session->lex->sql_command != SQLCOM_DELETE))
 
2359
        if ((thd->lex->sql_command != SQLCOM_DELETE))
817
2360
        {
818
2361
          /*
819
2362
            Get best non-covering ROR-intersection plan and prepare data for
838
2381
      else
839
2382
      {
840
2383
        /* Try creating index_merge/ROR-union scan. */
841
 
        optimizer::SEL_IMERGE *imerge= NULL;
842
 
        optimizer::TableReadPlan *best_conj_trp= NULL;
843
 
        optimizer::TableReadPlan *new_conj_trp= NULL;
844
 
        List_iterator_fast<optimizer::SEL_IMERGE> it(tree->merges);
 
2384
        SEL_IMERGE *imerge;
 
2385
        TABLE_READ_PLAN *best_conj_trp= NULL, *new_conj_trp;
 
2386
        DBUG_PRINT("info",("No range reads possible,"
 
2387
                           " trying to construct index_merge"));
 
2388
        List_iterator_fast<SEL_IMERGE> it(tree->merges);
845
2389
        while ((imerge= it++))
846
2390
        {
847
 
          new_conj_trp= get_best_disjunct_quick(session, &param, imerge, best_read_time);
 
2391
          new_conj_trp= get_best_disjunct_quick(&param, imerge, best_read_time);
848
2392
          if (new_conj_trp)
849
 
            set_if_smaller(param.table->quick_condition_rows,
 
2393
            set_if_smaller(param.table->quick_condition_rows, 
850
2394
                           new_conj_trp->records);
851
2395
          if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
852
2396
                                 best_conj_trp->read_cost))
857
2401
      }
858
2402
    }
859
2403
 
860
 
    session->mem_root= param.old_root;
 
2404
    thd->mem_root= param.old_root;
861
2405
 
862
2406
    /* If we got a read plan, create a quick select from it. */
863
2407
    if (best_trp)
864
2408
    {
865
2409
      records= best_trp->records;
866
 
      if (! (quick= best_trp->make_quick(&param, true)) || quick->init())
 
2410
      if (!(quick= best_trp->make_quick(&param, true)) || quick->init())
867
2411
      {
868
 
        /* quick can already be free here */
869
 
        if (quick)
870
 
        {
871
 
          delete quick;
872
 
          quick= NULL;
873
 
        }
 
2412
        delete quick;
 
2413
        quick= NULL;
874
2414
      }
875
2415
    }
876
2416
 
877
2417
  free_mem:
878
 
    alloc.free_root(MYF(0));                    // Return memory & allocator
879
 
    session->mem_root= param.old_root;
880
 
    session->no_errors=0;
 
2418
    free_root(&alloc,MYF(0));                   // Return memory & allocator
 
2419
    thd->mem_root= param.old_root;
 
2420
    thd->no_errors=0;
881
2421
  }
882
2422
 
 
2423
  DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
 
2424
 
883
2425
  /*
884
2426
    Assume that if the user is using 'limit' we will only need to scan
885
2427
    limit rows if we are using a key
886
2428
  */
887
 
  return(records ? test(quick) : -1);
 
2429
  DBUG_RETURN(records ? test(quick) : -1);
888
2430
}
889
2431
 
890
2432
/*
891
 
  Get best plan for a optimizer::SEL_IMERGE disjunctive expression.
 
2433
  Get best plan for a SEL_IMERGE disjunctive expression.
892
2434
  SYNOPSIS
893
2435
    get_best_disjunct_quick()
894
 
      session
895
2436
      param     Parameter from check_quick_select function
896
2437
      imerge    Expression to use
897
2438
      read_time Don't create scans with cost > read_time
915
2456
          {cost of ordinary clustered PK scan with n_ranges=n_rows}
916
2457
 
917
2458
      Otherwise, we use the following model to calculate costs:
918
 
      We need to retrieve n_rows rows from cursor that occupies n_blocks blocks.
 
2459
      We need to retrieve n_rows rows from file that occupies n_blocks blocks.
919
2460
      We assume that offsets of rows we need are independent variates with
920
2461
      uniform distribution in [0..max_file_offset] range.
921
2462
 
923
2464
      and "empty" if doesn't contain rows we need.
924
2465
 
925
2466
      Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
926
 
      applies to any block in cursor). Let x_i be a variate taking value 1 if
 
2467
      applies to any block in file). Let x_i be a variate taking value 1 if
927
2468
      block #i is empty and 0 otherwise.
928
2469
 
929
2470
      Then E(x_i) = (1 - 1/n_blocks)^n_rows;
954
2495
*/
955
2496
 
956
2497
static
957
 
optimizer::TableReadPlan *get_best_disjunct_quick(Session *session,
958
 
                                                  optimizer::Parameter *param,
959
 
                                                  optimizer::SEL_IMERGE *imerge,
960
 
                                                  double read_time)
 
2498
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
 
2499
                                         double read_time)
961
2500
{
962
 
  optimizer::SEL_TREE **ptree= NULL;
963
 
  optimizer::IndexMergeReadPlan *imerge_trp= NULL;
964
 
  uint32_t n_child_scans= imerge->trees_next - imerge->trees;
965
 
  optimizer::RangeReadPlan **range_scans= NULL;
966
 
  optimizer::RangeReadPlan **cur_child= NULL;
967
 
  optimizer::RangeReadPlan **cpk_scan= NULL;
 
2501
  SEL_TREE **ptree;
 
2502
  TRP_INDEX_MERGE *imerge_trp= NULL;
 
2503
  uint n_child_scans= imerge->trees_next - imerge->trees;
 
2504
  TRP_RANGE **range_scans;
 
2505
  TRP_RANGE **cur_child;
 
2506
  TRP_RANGE **cpk_scan= NULL;
968
2507
  bool imerge_too_expensive= false;
969
2508
  double imerge_cost= 0.0;
970
2509
  ha_rows cpk_scan_records= 0;
971
2510
  ha_rows non_cpk_scan_records= 0;
972
 
  bool pk_is_clustered= param->table->cursor->primary_key_is_clustered();
 
2511
  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
973
2512
  bool all_scans_ror_able= true;
974
2513
  bool all_scans_rors= true;
975
 
  uint32_t unique_calc_buff_size;
976
 
  optimizer::TableReadPlan **roru_read_plans= NULL;
977
 
  optimizer::TableReadPlan **cur_roru_plan= NULL;
 
2514
  uint unique_calc_buff_size;
 
2515
  TABLE_READ_PLAN **roru_read_plans;
 
2516
  TABLE_READ_PLAN **cur_roru_plan;
978
2517
  double roru_index_costs;
979
2518
  ha_rows roru_total_records;
980
2519
  double roru_intersect_part= 1.0;
981
 
 
982
 
  if (! (range_scans= (optimizer::RangeReadPlan**)param->mem_root->alloc_root(sizeof(optimizer::RangeReadPlan*)* n_child_scans)))
983
 
  {
984
 
    return NULL;
985
 
  }
986
 
 
 
2520
  DBUG_ENTER("get_best_disjunct_quick");
 
2521
  DBUG_PRINT("info", ("Full table scan cost: %g", read_time));
 
2522
 
 
2523
  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
 
2524
                                             sizeof(TRP_RANGE*)*
 
2525
                                             n_child_scans)))
 
2526
    DBUG_RETURN(NULL);
987
2527
  /*
988
2528
    Collect best 'range' scan for each of disjuncts, and, while doing so,
989
2529
    analyze possibility of ROR scans. Also calculate some values needed by
993
2533
       ptree != imerge->trees_next;
994
2534
       ptree++, cur_child++)
995
2535
  {
996
 
    if (!(*cur_child= get_key_scans_params(session, param, *ptree, true, false, read_time)))
 
2536
    DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
 
2537
                                        "tree in SEL_IMERGE"););
 
2538
    if (!(*cur_child= get_key_scans_params(param, *ptree, true, false, read_time)))
997
2539
    {
998
2540
      /*
999
2541
        One of index scans in this index_merge is more expensive than entire
1000
2542
        table read for another available option. The entire index_merge (and
1001
2543
        any possible ROR-union) will be more expensive then, too. We continue
1002
 
        here only to update SqlSelect members.
 
2544
        here only to update SQL_SELECT members.
1003
2545
      */
1004
2546
      imerge_too_expensive= true;
1005
2547
    }
1011
2553
    all_scans_rors &= (*cur_child)->is_ror;
1012
2554
    if (pk_is_clustered &&
1013
2555
        param->real_keynr[(*cur_child)->key_idx] ==
1014
 
        param->table->getShare()->getPrimaryKey())
 
2556
        param->table->s->primary_key)
1015
2557
    {
1016
2558
      cpk_scan= cur_child;
1017
2559
      cpk_scan_records= (*cur_child)->records;
1020
2562
      non_cpk_scan_records += (*cur_child)->records;
1021
2563
  }
1022
2564
 
 
2565
  DBUG_PRINT("info", ("index_merge scans cost %g", imerge_cost));
1023
2566
  if (imerge_too_expensive || (imerge_cost > read_time) ||
1024
 
      ((non_cpk_scan_records+cpk_scan_records >= param->table->cursor->stats.records) && read_time != DBL_MAX))
 
2567
      ((non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) && read_time != DBL_MAX))
1025
2568
  {
1026
2569
    /*
1027
2570
      Bail out if it is obvious that both index_merge and ROR-union will be
1028
2571
      more expensive
1029
2572
    */
1030
 
    return NULL;
 
2573
    DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
 
2574
                        "full table scan, bailing out"));
 
2575
    DBUG_RETURN(NULL);
1031
2576
  }
1032
2577
  if (all_scans_rors)
1033
2578
  {
1034
 
    roru_read_plans= (optimizer::TableReadPlan **) range_scans;
 
2579
    roru_read_plans= (TABLE_READ_PLAN**)range_scans;
1035
2580
    goto skip_to_ror_scan;
1036
2581
  }
1037
2582
  if (cpk_scan)
1038
2583
  {
1039
2584
    /*
1040
2585
      Add one ROWID comparison for each row retrieved on non-CPK scan.  (it
1041
 
      is done in QuickRangeSelect::row_in_ranges)
 
2586
      is done in QUICK_RANGE_SELECT::row_in_ranges)
1042
2587
     */
1043
2588
    imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
1044
2589
  }
1045
2590
 
1046
2591
  /* Calculate cost(rowid_to_row_scan) */
1047
2592
  {
1048
 
    optimizer::CostVector sweep_cost;
1049
 
    Join *join= param->session->lex->select_lex.join;
 
2593
    COST_VECT sweep_cost;
 
2594
    JOIN *join= param->thd->lex->select_lex.join;
1050
2595
    bool is_interrupted= test(join && join->tables == 1);
1051
2596
    get_sweep_read_cost(param->table, non_cpk_scan_records, is_interrupted,
1052
2597
                        &sweep_cost);
1053
2598
    imerge_cost += sweep_cost.total_cost();
1054
2599
  }
 
2600
  DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
 
2601
                     imerge_cost));
1055
2602
  if (imerge_cost > read_time)
1056
2603
    goto build_ror_index_merge;
1057
2604
 
1058
2605
  /* Add Unique operations cost */
1059
2606
  unique_calc_buff_size=
1060
2607
    Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
1061
 
                                    param->table->cursor->ref_length,
1062
 
                                    param->session->variables.sortbuff_size);
 
2608
                                    param->table->file->ref_length,
 
2609
                                    param->thd->variables.sortbuff_size);
1063
2610
  if (param->imerge_cost_buff_size < unique_calc_buff_size)
1064
2611
  {
1065
 
    if (!(param->imerge_cost_buff= (uint*)param->mem_root->alloc_root(unique_calc_buff_size)))
1066
 
    {
1067
 
      return NULL;
1068
 
    }
1069
 
 
 
2612
    if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
 
2613
                                                     unique_calc_buff_size)))
 
2614
      DBUG_RETURN(NULL);
1070
2615
    param->imerge_cost_buff_size= unique_calc_buff_size;
1071
2616
  }
1072
2617
 
1073
2618
  imerge_cost +=
1074
 
    Unique::get_use_cost(param->imerge_cost_buff, (uint32_t)non_cpk_scan_records,
1075
 
                         param->table->cursor->ref_length,
1076
 
                         param->session->variables.sortbuff_size);
 
2619
    Unique::get_use_cost(param->imerge_cost_buff, (uint)non_cpk_scan_records,
 
2620
                         param->table->file->ref_length,
 
2621
                         param->thd->variables.sortbuff_size);
 
2622
  DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
 
2623
                     imerge_cost, read_time));
1077
2624
  if (imerge_cost < read_time)
1078
2625
  {
1079
 
    if ((imerge_trp= new (param->mem_root) optimizer::IndexMergeReadPlan))
 
2626
    if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
1080
2627
    {
1081
2628
      imerge_trp->read_cost= imerge_cost;
1082
2629
      imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
1083
2630
      imerge_trp->records= min(imerge_trp->records,
1084
 
                               param->table->cursor->stats.records);
 
2631
                               param->table->file->stats.records);
1085
2632
      imerge_trp->range_scans= range_scans;
1086
2633
      imerge_trp->range_scans_end= range_scans + n_child_scans;
1087
2634
      read_time= imerge_cost;
1089
2636
  }
1090
2637
 
1091
2638
build_ror_index_merge:
1092
 
  if (!all_scans_ror_able || param->session->lex->sql_command == SQLCOM_DELETE)
1093
 
    return(imerge_trp);
 
2639
  if (!all_scans_ror_able || param->thd->lex->sql_command == SQLCOM_DELETE)
 
2640
    DBUG_RETURN(imerge_trp);
1094
2641
 
1095
2642
  /* Ok, it is possible to build a ROR-union, try it. */
1096
2643
  bool dummy;
1097
 
  if (! (roru_read_plans=
1098
 
          (optimizer::TableReadPlan **) param->mem_root->alloc_root(sizeof(optimizer::TableReadPlan*) * n_child_scans)))
1099
 
  {
1100
 
    return imerge_trp;
1101
 
  }
 
2644
  if (!(roru_read_plans=
 
2645
          (TABLE_READ_PLAN**)alloc_root(param->mem_root,
 
2646
                                        sizeof(TABLE_READ_PLAN*)*
 
2647
                                        n_child_scans)))
 
2648
    DBUG_RETURN(imerge_trp);
1102
2649
skip_to_ror_scan:
1103
2650
  roru_index_costs= 0.0;
1104
2651
  roru_total_records= 0;
1119
2666
    if ((*cur_child)->is_ror)
1120
2667
    {
1121
2668
      /* Ok, we have index_only cost, now get full rows scan cost */
1122
 
      cost= param->table->cursor->
 
2669
      cost= param->table->file->
1123
2670
              read_time(param->real_keynr[(*cur_child)->key_idx], 1,
1124
2671
                        (*cur_child)->records) +
1125
2672
              rows2double((*cur_child)->records) / TIME_FOR_COMPARE;
1127
2674
    else
1128
2675
      cost= read_time;
1129
2676
 
1130
 
    optimizer::TableReadPlan *prev_plan= *cur_child;
 
2677
    TABLE_READ_PLAN *prev_plan= *cur_child;
1131
2678
    if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
1132
2679
                                                 &dummy)))
1133
2680
    {
1134
2681
      if (prev_plan->is_ror)
1135
2682
        *cur_roru_plan= prev_plan;
1136
2683
      else
1137
 
        return(imerge_trp);
 
2684
        DBUG_RETURN(imerge_trp);
1138
2685
      roru_index_costs += (*cur_roru_plan)->read_cost;
1139
2686
    }
1140
2687
    else
1141
2688
      roru_index_costs +=
1142
 
        ((optimizer::RorIntersectReadPlan*)(*cur_roru_plan))->index_scan_costs;
 
2689
        ((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
1143
2690
    roru_total_records += (*cur_roru_plan)->records;
1144
2691
    roru_intersect_part *= (*cur_roru_plan)->records /
1145
 
                           param->table->cursor->stats.records;
 
2692
                           param->table->file->stats.records;
1146
2693
  }
1147
2694
 
1148
2695
  /*
1152
2699
    in disjunction do not share key parts.
1153
2700
  */
1154
2701
  roru_total_records -= (ha_rows)(roru_intersect_part*
1155
 
                                  param->table->cursor->stats.records);
 
2702
                                  param->table->file->stats.records);
1156
2703
  /* ok, got a ROR read plan for each of the disjuncts
1157
2704
    Calculate cost:
1158
2705
    cost(index_union_scan(scan_1, ... scan_n)) =
1163
2710
  */
1164
2711
  double roru_total_cost;
1165
2712
  {
1166
 
    optimizer::CostVector sweep_cost;
1167
 
    Join *join= param->session->lex->select_lex.join;
 
2713
    COST_VECT sweep_cost;
 
2714
    JOIN *join= param->thd->lex->select_lex.join;
1168
2715
    bool is_interrupted= test(join && join->tables == 1);
1169
2716
    get_sweep_read_cost(param->table, roru_total_records, is_interrupted,
1170
2717
                        &sweep_cost);
1174
2721
                     sweep_cost.total_cost();
1175
2722
  }
1176
2723
 
1177
 
  optimizer::RorUnionReadPlan *roru= NULL;
 
2724
  DBUG_PRINT("info", ("ROR-union: cost %g, %d members", roru_total_cost,
 
2725
                      n_child_scans));
 
2726
  TRP_ROR_UNION* roru;
1178
2727
  if (roru_total_cost < read_time)
1179
2728
  {
1180
 
    if ((roru= new (param->mem_root) optimizer::RorUnionReadPlan))
 
2729
    if ((roru= new (param->mem_root) TRP_ROR_UNION))
1181
2730
    {
1182
2731
      roru->first_ror= roru_read_plans;
1183
2732
      roru->last_ror= roru_read_plans + n_child_scans;
1184
2733
      roru->read_cost= roru_total_cost;
1185
2734
      roru->records= roru_total_records;
1186
 
      return roru;
 
2735
      DBUG_RETURN(roru);
1187
2736
    }
1188
2737
  }
1189
 
  return(imerge_trp);
 
2738
  DBUG_RETURN(imerge_trp);
1190
2739
}
1191
2740
 
1192
2741
 
1193
2742
typedef struct st_ror_scan_info
1194
2743
{
1195
 
  uint32_t      idx;      /* # of used key in param->keys */
1196
 
  uint32_t      keynr;    /* # of used key in table */
 
2744
  uint      idx;      /* # of used key in param->keys */
 
2745
  uint      keynr;    /* # of used key in table */
1197
2746
  ha_rows   records;  /* estimate of # records this scan will return */
1198
2747
 
1199
2748
  /* Set of intervals over key fields that will be used for row retrieval. */
1200
 
  optimizer::SEL_ARG   *sel_arg;
 
2749
  SEL_ARG   *sel_arg;
1201
2750
 
1202
2751
  /* Fields used in the query and covered by this ROR scan. */
1203
 
  MyBitmap covered_fields;
1204
 
  uint32_t      used_fields_covered; /* # of set bits in covered_fields */
 
2752
  MY_BITMAP covered_fields;
 
2753
  uint      used_fields_covered; /* # of set bits in covered_fields */
1205
2754
  int       key_rec_length; /* length of key record (including rowid) */
1206
2755
 
1207
2756
  /*
1209
2758
    (assuming there is no need to access full table records)
1210
2759
  */
1211
2760
  double    index_read_cost;
1212
 
  uint32_t      first_uncovered_field; /* first unused bit in covered_fields */
1213
 
  uint32_t      key_components; /* # of parts in the key */
 
2761
  uint      first_uncovered_field; /* first unused bit in covered_fields */
 
2762
  uint      key_components; /* # of parts in the key */
1214
2763
} ROR_SCAN_INFO;
1215
2764
 
1216
2765
 
1230
2779
*/
1231
2780
 
1232
2781
static
1233
 
ROR_SCAN_INFO *make_ror_scan(const optimizer::Parameter *param, int idx, optimizer::SEL_ARG *sel_arg)
 
2782
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
1234
2783
{
1235
2784
  ROR_SCAN_INFO *ror_scan;
1236
2785
  my_bitmap_map *bitmap_buf;
1237
 
 
1238
 
  uint32_t keynr;
1239
 
 
1240
 
  if (!(ror_scan= (ROR_SCAN_INFO*)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO))))
1241
 
    return NULL;
 
2786
  uint keynr;
 
2787
  DBUG_ENTER("make_ror_scan");
 
2788
 
 
2789
  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
 
2790
                                             sizeof(ROR_SCAN_INFO))))
 
2791
    DBUG_RETURN(NULL);
1242
2792
 
1243
2793
  ror_scan->idx= idx;
1244
2794
  ror_scan->keynr= keynr= param->real_keynr[idx];
1245
2795
  ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
1246
 
                             param->table->cursor->ref_length);
 
2796
                             param->table->file->ref_length);
1247
2797
  ror_scan->sel_arg= sel_arg;
1248
2798
  ror_scan->records= param->table->quick_rows[keynr];
1249
2799
 
1250
 
  if (!(bitmap_buf= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)))
1251
 
  {
1252
 
    return NULL;
1253
 
  }
1254
 
 
1255
 
  if (ror_scan->covered_fields.init(bitmap_buf, param->table->getShare()->sizeFields()))
1256
 
  {
1257
 
    return NULL;
1258
 
  }
1259
 
  ror_scan->covered_fields.clearAll();
1260
 
 
1261
 
  KeyPartInfo *key_part= param->table->key_info[keynr].key_part;
1262
 
  KeyPartInfo *key_part_end= key_part +
 
2800
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
 
2801
                                                param->fields_bitmap_size)))
 
2802
    DBUG_RETURN(NULL);
 
2803
 
 
2804
  if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
 
2805
                  param->table->s->fields, false))
 
2806
    DBUG_RETURN(NULL);
 
2807
  bitmap_clear_all(&ror_scan->covered_fields);
 
2808
 
 
2809
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
 
2810
  KEY_PART_INFO *key_part_end= key_part +
1263
2811
                               param->table->key_info[keynr].key_parts;
1264
2812
  for (;key_part != key_part_end; ++key_part)
1265
2813
  {
1266
 
    if (param->needed_fields.isBitSet(key_part->fieldnr-1))
1267
 
      ror_scan->covered_fields.setBit(key_part->fieldnr-1);
 
2814
    if (bitmap_is_set(&param->needed_fields, key_part->fieldnr-1))
 
2815
      bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
1268
2816
  }
1269
2817
  double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
1270
2818
  ror_scan->index_read_cost=
1271
 
    param->table->cursor->index_only_read_time(ror_scan->keynr, rows);
1272
 
  return(ror_scan);
 
2819
    param->table->file->index_only_read_time(ror_scan->keynr, rows);
 
2820
  DBUG_RETURN(ror_scan);
1273
2821
}
1274
2822
 
1275
2823
 
1293
2841
  return (val1 < val2)? -1: (val1 == val2)? 0 : 1;
1294
2842
}
1295
2843
 
1296
 
 
1297
2844
/*
1298
2845
  Compare two ROR_SCAN_INFO** by
1299
2846
   (#covered fields in F desc,
1328
2875
  return 0;
1329
2876
}
1330
2877
 
 
2878
 
1331
2879
/* Auxiliary structure for incremental ROR-intersection creation */
1332
2880
typedef struct
1333
2881
{
1334
 
  const optimizer::Parameter *param;
1335
 
  MyBitmap covered_fields; /* union of fields covered by all scans */
 
2882
  const PARAM *param;
 
2883
  MY_BITMAP covered_fields; /* union of fields covered by all scans */
1336
2884
  /*
1337
2885
    Fraction of table records that satisfies conditions of all scans.
1338
2886
    This is the number of full records that will be retrieved if a
1361
2909
*/
1362
2910
 
1363
2911
static
1364
 
ROR_INTERSECT_INFO* ror_intersect_init(const optimizer::Parameter *param)
 
2912
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
1365
2913
{
1366
2914
  ROR_INTERSECT_INFO *info;
1367
2915
  my_bitmap_map* buf;
1368
 
  if (!(info= (ROR_INTERSECT_INFO*)param->mem_root->alloc_root(sizeof(ROR_INTERSECT_INFO))))
 
2916
  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
 
2917
                                              sizeof(ROR_INTERSECT_INFO))))
1369
2918
    return NULL;
1370
2919
  info->param= param;
1371
 
  if (!(buf= (my_bitmap_map*) param->mem_root->alloc_root(param->fields_bitmap_size)))
 
2920
  if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
 
2921
                                         param->fields_bitmap_size)))
1372
2922
    return NULL;
1373
 
  if (info->covered_fields.init(buf, param->table->getShare()->sizeFields()))
 
2923
  if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
 
2924
                  false))
1374
2925
    return NULL;
1375
2926
  info->is_covering= false;
1376
2927
  info->index_scan_costs= 0.0;
1377
2928
  info->index_records= 0;
1378
 
  info->out_rows= (double) param->table->cursor->stats.records;
1379
 
  info->covered_fields.clearAll();
 
2929
  info->out_rows= (double) param->table->file->stats.records;
 
2930
  bitmap_clear_all(&info->covered_fields);
1380
2931
  return info;
1381
2932
}
1382
2933
 
1383
 
static void ror_intersect_cpy(ROR_INTERSECT_INFO *dst,
1384
 
                              const ROR_INTERSECT_INFO *src)
 
2934
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
1385
2935
{
1386
2936
  dst->param= src->param;
1387
 
  dst->covered_fields= src->covered_fields;
 
2937
  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap, 
 
2938
         no_bytes_in_map(&src->covered_fields));
1388
2939
  dst->out_rows= src->out_rows;
1389
2940
  dst->is_covering= src->is_covering;
1390
2941
  dst->index_records= src->index_records;
1398
2949
 
1399
2950
  SYNOPSIS
1400
2951
    ror_scan_selectivity()
1401
 
      info  ROR-interection
 
2952
      info  ROR-interection 
1402
2953
      scan  ROR scan
1403
 
 
 
2954
      
1404
2955
  NOTES
1405
2956
    Suppose we have a condition on several keys
1406
2957
    cond=k_11=c_11 AND k_12=c_12 AND ...  // parts of first key
1483
3034
    Selectivity of given ROR scan.
1484
3035
*/
1485
3036
 
1486
 
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
 
3037
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info, 
1487
3038
                                   const ROR_SCAN_INFO *scan)
1488
3039
{
1489
3040
  double selectivity_mult= 1.0;
1490
 
  KeyPartInfo *key_part= info->param->table->key_info[scan->keynr].key_part;
1491
 
  unsigned char key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
1492
 
  unsigned char *key_ptr= key_val;
1493
 
  optimizer::SEL_ARG *sel_arg= NULL;
1494
 
  optimizer::SEL_ARG *tuple_arg= NULL;
 
3041
  KEY_PART_INFO *key_part= info->param->table->key_info[scan->keynr].key_part;
 
3042
  uchar key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
 
3043
  uchar *key_ptr= key_val;
 
3044
  SEL_ARG *sel_arg, *tuple_arg= NULL;
1495
3045
  key_part_map keypart_map= 0;
1496
3046
  bool cur_covered;
1497
 
  bool prev_covered= test(info->covered_fields.isBitSet(key_part->fieldnr-1));
 
3047
  bool prev_covered= test(bitmap_is_set(&info->covered_fields,
 
3048
                                        key_part->fieldnr-1));
1498
3049
  key_range min_range;
1499
3050
  key_range max_range;
1500
3051
  min_range.key= key_val;
1501
3052
  min_range.flag= HA_READ_KEY_EXACT;
1502
3053
  max_range.key= key_val;
1503
3054
  max_range.flag= HA_READ_AFTER_KEY;
1504
 
  ha_rows prev_records= info->param->table->cursor->stats.records;
 
3055
  ha_rows prev_records= info->param->table->file->stats.records;
 
3056
  DBUG_ENTER("ror_scan_selectivity");
1505
3057
 
1506
3058
  for (sel_arg= scan->sel_arg; sel_arg;
1507
3059
       sel_arg= sel_arg->next_key_part)
1508
3060
  {
1509
 
    cur_covered=
1510
 
      test(info->covered_fields.isBitSet(key_part[sel_arg->part].fieldnr-1));
 
3061
    DBUG_PRINT("info",("sel_arg step"));
 
3062
    cur_covered= test(bitmap_is_set(&info->covered_fields,
 
3063
                                    key_part[sel_arg->part].fieldnr-1));
1511
3064
    if (cur_covered != prev_covered)
1512
3065
    {
1513
3066
      /* create (part1val, ..., part{n-1}val) tuple. */
1528
3081
      }
1529
3082
      min_range.length= max_range.length= (size_t) (key_ptr - key_val);
1530
3083
      min_range.keypart_map= max_range.keypart_map= keypart_map;
1531
 
      records= (info->param->table->cursor->
 
3084
      records= (info->param->table->file->
1532
3085
                records_in_range(scan->keynr, &min_range, &max_range));
1533
3086
      if (cur_covered)
1534
3087
      {
1535
3088
        /* uncovered -> covered */
1536
3089
        double tmp= rows2double(records)/rows2double(prev_records);
 
3090
        DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
1537
3091
        selectivity_mult *= tmp;
1538
3092
        prev_records= HA_POS_ERROR;
1539
3093
      }
1549
3103
  {
1550
3104
    double tmp= rows2double(info->param->table->quick_rows[scan->keynr]) /
1551
3105
                rows2double(prev_records);
 
3106
    DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
1552
3107
    selectivity_mult *= tmp;
1553
3108
  }
1554
 
  return(selectivity_mult);
 
3109
  DBUG_PRINT("info", ("Returning multiplier: %g", selectivity_mult));
 
3110
  DBUG_RETURN(selectivity_mult);
1555
3111
}
1556
3112
 
1557
3113
 
1585
3141
 
1586
3142
    E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
1587
3143
                           ror_scan_selectivity({scan1}, scan2) * ... *
1588
 
                           ror_scan_selectivity({scan1,...}, scanN).
 
3144
                           ror_scan_selectivity({scan1,...}, scanN). 
1589
3145
  RETURN
1590
3146
    true   ROR scan added to ROR-intersection, cost updated.
1591
3147
    false  It doesn't make sense to add this ROR scan to this ROR-intersection.
1596
3152
{
1597
3153
  double selectivity_mult= 1.0;
1598
3154
 
 
3155
  DBUG_ENTER("ror_intersect_add");
 
3156
  DBUG_PRINT("info", ("Current out_rows= %g", info->out_rows));
 
3157
  DBUG_PRINT("info", ("Adding scan on %s",
 
3158
                      info->param->table->key_info[ror_scan->keynr].name));
 
3159
  DBUG_PRINT("info", ("is_cpk_scan: %d",is_cpk_scan));
 
3160
 
1599
3161
  selectivity_mult = ror_scan_selectivity(info, ror_scan);
1600
3162
  if (selectivity_mult == 1.0)
1601
3163
  {
1602
3164
    /* Don't add this scan if it doesn't improve selectivity. */
1603
 
    return false;
 
3165
    DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
 
3166
    DBUG_RETURN(false);
1604
3167
  }
1605
 
 
 
3168
  
1606
3169
  info->out_rows *= selectivity_mult;
1607
 
 
 
3170
  
1608
3171
  if (is_cpk_scan)
1609
3172
  {
1610
3173
    /*
1611
 
      CPK scan is used to filter out rows. We apply filtering for
 
3174
      CPK scan is used to filter out rows. We apply filtering for 
1612
3175
      each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
1613
3176
      per check this gives us:
1614
3177
    */
1615
 
    info->index_scan_costs += rows2double(info->index_records) /
 
3178
    info->index_scan_costs += rows2double(info->index_records) / 
1616
3179
                              TIME_FOR_COMPARE_ROWID;
1617
3180
  }
1618
3181
  else
1623
3186
    if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
1624
3187
                                               &info->covered_fields))
1625
3188
    {
 
3189
      DBUG_PRINT("info", ("ROR-intersect is covering now"));
1626
3190
      info->is_covering= true;
1627
3191
    }
1628
3192
  }
1629
3193
 
1630
3194
  info->total_cost= info->index_scan_costs;
 
3195
  DBUG_PRINT("info", ("info->total_cost: %g", info->total_cost));
1631
3196
  if (!info->is_covering)
1632
3197
  {
1633
 
    optimizer::CostVector sweep_cost;
1634
 
    Join *join= info->param->session->lex->select_lex.join;
 
3198
    COST_VECT sweep_cost;
 
3199
    JOIN *join= info->param->thd->lex->select_lex.join;
1635
3200
    bool is_interrupted= test(join && join->tables == 1);
1636
3201
    get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
1637
3202
                        is_interrupted, &sweep_cost);
1638
3203
    info->total_cost += sweep_cost.total_cost();
1639
 
  }
1640
 
  return true;
1641
 
}
1642
 
 
1643
 
 
1644
 
/*
1645
 
  Get best covering ROR-intersection.
1646
 
  SYNOPSIS
1647
 
    get_best_covering_ror_intersect()
1648
 
      param     Parameter from test_quick_select function.
1649
 
      tree      optimizer::SEL_TREE with sets of intervals for different keys.
1650
 
      read_time Don't return table read plans with cost > read_time.
1651
 
 
1652
 
  RETURN
1653
 
    Best covering ROR-intersection plan
1654
 
    NULL if no plan found.
1655
 
 
1656
 
  NOTES
1657
 
    get_best_ror_intersect must be called for a tree before calling this
1658
 
    function for it.
1659
 
    This function invalidates tree->ror_scans member values.
1660
 
 
1661
 
  The following approximate algorithm is used:
1662
 
    I=set of all covering indexes
1663
 
    F=set of all fields to cover
1664
 
    S={}
1665
 
 
1666
 
    do
1667
 
    {
1668
 
      Order I by (#covered fields in F desc,
1669
 
                  #components asc,
1670
 
                  number of first not covered component asc);
1671
 
      F=F-covered by first(I);
1672
 
      S=S+first(I);
1673
 
      I=I-first(I);
1674
 
    } while F is not empty.
1675
 
*/
1676
 
 
1677
 
static
1678
 
optimizer::RorIntersectReadPlan *get_best_covering_ror_intersect(optimizer::Parameter *param,
1679
 
                                                            optimizer::SEL_TREE *tree,
1680
 
                                                            double read_time)
1681
 
{
1682
 
  ROR_SCAN_INFO **ror_scan_mark;
1683
 
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
1684
 
 
1685
 
  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
1686
 
    (*scan)->key_components=
1687
 
      param->table->key_info[(*scan)->keynr].key_parts;
1688
 
 
1689
 
  /*
1690
 
    Run covering-ROR-search algorithm.
1691
 
    Assume set I is [ror_scan .. ror_scans_end)
1692
 
  */
1693
 
 
1694
 
  /*I=set of all covering indexes */
1695
 
  ror_scan_mark= tree->ror_scans;
1696
 
 
1697
 
  MyBitmap *covered_fields= &param->tmp_covered_fields;
1698
 
  if (! covered_fields->getBitmap())
1699
 
  {
1700
 
    my_bitmap_map *tmp_bitmap= (my_bitmap_map*)param->mem_root->alloc_root(param->fields_bitmap_size);
1701
 
    covered_fields->setBitmap(tmp_bitmap);
1702
 
  }
1703
 
  if (! covered_fields->getBitmap() ||
1704
 
      covered_fields->init(covered_fields->getBitmap(),
1705
 
                           param->table->getShare()->sizeFields()))
1706
 
    return 0;
1707
 
  covered_fields->clearAll();
1708
 
 
1709
 
  double total_cost= 0.0f;
1710
 
  ha_rows records=0;
1711
 
  bool all_covered;
1712
 
 
1713
 
  do
1714
 
  {
1715
 
    /*
1716
 
      Update changed sorting info:
1717
 
        #covered fields,
1718
 
        number of first not covered component
1719
 
      Calculate and save these values for each of remaining scans.
1720
 
    */
1721
 
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
1722
 
    {
1723
 
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
1724
 
      (*scan)->used_fields_covered=
1725
 
        (*scan)->covered_fields.getBitsSet();
1726
 
      (*scan)->first_uncovered_field=
1727
 
        (*scan)->covered_fields.getFirst();
1728
 
    }
1729
 
 
1730
 
    internal::my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark,
1731
 
                       sizeof(ROR_SCAN_INFO*),
1732
 
                       (qsort_cmp)cmp_ror_scan_info_covering);
1733
 
 
1734
 
    /* I=I-first(I) */
1735
 
    total_cost += (*ror_scan_mark)->index_read_cost;
1736
 
    records += (*ror_scan_mark)->records;
1737
 
    if (total_cost > read_time)
1738
 
      return NULL;
1739
 
    /* F=F-covered by first(I) */
1740
 
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
1741
 
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
1742
 
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
1743
 
 
1744
 
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
1745
 
    return NULL;
1746
 
 
1747
 
  /*
1748
 
    Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
1749
 
    cost total_cost.
1750
 
  */
1751
 
  /* Add priority queue use cost. */
1752
 
  total_cost += rows2double(records)*
1753
 
                log((double)(ror_scan_mark - tree->ror_scans)) /
1754
 
                (TIME_FOR_COMPARE_ROWID * M_LN2);
1755
 
 
1756
 
  if (total_cost > read_time)
1757
 
    return NULL;
1758
 
 
1759
 
  optimizer::RorIntersectReadPlan *trp= NULL;
1760
 
  if (! (trp= new (param->mem_root) optimizer::RorIntersectReadPlan))
1761
 
  {
1762
 
    return trp;
1763
 
  }
1764
 
 
1765
 
  uint32_t best_num= (ror_scan_mark - tree->ror_scans);
1766
 
  if (!(trp->first_scan= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)* best_num)))
1767
 
    return NULL;
1768
 
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
1769
 
  trp->last_scan=  trp->first_scan + best_num;
1770
 
  trp->is_covering= true;
1771
 
  trp->read_cost= total_cost;
1772
 
  trp->records= records;
1773
 
  trp->cpk_scan= NULL;
1774
 
  set_if_smaller(param->table->quick_condition_rows, records);
1775
 
 
1776
 
  return(trp);
 
3204
    DBUG_PRINT("info", ("info->total_cost= %g", info->total_cost));
 
3205
  }
 
3206
  DBUG_PRINT("info", ("New out_rows: %g", info->out_rows));
 
3207
  DBUG_PRINT("info", ("New cost: %g, %scovering", info->total_cost,
 
3208
                      info->is_covering?"" : "non-"));
 
3209
  DBUG_RETURN(true);
1777
3210
}
1778
3211
 
1779
3212
 
1793
3226
 
1794
3227
  NOTES
1795
3228
    get_key_scans_params must be called before this function can be called.
1796
 
 
 
3229
    
1797
3230
    When this function is called by ROR-union construction algorithm it
1798
3231
    assumes it is building an uncovered ROR-intersection (and thus # of full
1799
3232
    records to be retrieved is wrong here). This is a hack.
1815
3248
        firstR= R - first(R);
1816
3249
        if (!selectivity(S + firstR < selectivity(S)))
1817
3250
          continue;
1818
 
 
 
3251
          
1819
3252
        S= S + first(R);
1820
3253
        if (cost(S) < min_cost)
1821
3254
        {
1842
3275
*/
1843
3276
 
1844
3277
static
1845
 
optimizer::RorIntersectReadPlan *get_best_ror_intersect(const optimizer::Parameter *param,
1846
 
                                                   optimizer::SEL_TREE *tree,
1847
 
                                                   double read_time,
1848
 
                                                   bool *are_all_covering)
 
3278
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
 
3279
                                          double read_time,
 
3280
                                          bool *are_all_covering)
1849
3281
{
1850
 
  uint32_t idx= 0;
 
3282
  uint idx;
1851
3283
  double min_cost= DBL_MAX;
 
3284
  DBUG_ENTER("get_best_ror_intersect");
1852
3285
 
1853
 
  if ((tree->n_ror_scans < 2) || ! param->table->cursor->stats.records)
1854
 
    return NULL;
 
3286
  if ((tree->n_ror_scans < 2) || !param->table->file->stats.records)
 
3287
    DBUG_RETURN(NULL);
1855
3288
 
1856
3289
  /*
1857
 
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
 
3290
    Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of 
1858
3291
    them. Also find and save clustered PK scan if there is one.
1859
3292
  */
1860
 
  ROR_SCAN_INFO **cur_ror_scan= NULL;
 
3293
  ROR_SCAN_INFO **cur_ror_scan;
1861
3294
  ROR_SCAN_INFO *cpk_scan= NULL;
1862
 
  uint32_t cpk_no= 0;
 
3295
  uint cpk_no;
1863
3296
  bool cpk_scan_used= false;
1864
3297
 
1865
 
  if (! (tree->ror_scans= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)* param->keys)))
1866
 
  {
 
3298
  if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
 
3299
                                                     sizeof(ROR_SCAN_INFO*)*
 
3300
                                                     param->keys)))
1867
3301
    return NULL;
1868
 
  }
1869
 
  cpk_no= ((param->table->cursor->primary_key_is_clustered()) ?
1870
 
           param->table->getShare()->getPrimaryKey() : MAX_KEY);
 
3302
  cpk_no= ((param->table->file->primary_key_is_clustered()) ?
 
3303
           param->table->s->primary_key : MAX_KEY);
1871
3304
 
1872
3305
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
1873
3306
  {
1874
3307
    ROR_SCAN_INFO *scan;
1875
 
    if (! tree->ror_scans_map.test(idx))
 
3308
    if (!tree->ror_scans_map.is_set(idx))
1876
3309
      continue;
1877
3310
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
1878
3311
      return NULL;
1886
3319
  }
1887
3320
 
1888
3321
  tree->ror_scans_end= cur_ror_scan;
 
3322
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
 
3323
                                          tree->ror_scans,
 
3324
                                          tree->ror_scans_end););
1889
3325
  /*
1890
3326
    Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
1891
3327
    ROR_SCAN_INFO's.
1892
3328
    Step 2: Get best ROR-intersection using an approximate algorithm.
1893
3329
  */
1894
 
  internal::my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
1895
 
                     (qsort_cmp)cmp_ror_scan_info);
 
3330
  my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
 
3331
           (qsort_cmp)cmp_ror_scan_info);
 
3332
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
 
3333
                                          tree->ror_scans,
 
3334
                                          tree->ror_scans_end););
1896
3335
 
1897
 
  ROR_SCAN_INFO **intersect_scans= NULL; /* ROR scans used in index intersection */
1898
 
  ROR_SCAN_INFO **intersect_scans_end= NULL;
1899
 
  if (! (intersect_scans= (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*) * tree->n_ror_scans)))
 
3336
  ROR_SCAN_INFO **intersect_scans; /* ROR scans used in index intersection */
 
3337
  ROR_SCAN_INFO **intersect_scans_end;
 
3338
  if (!(intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
 
3339
                                                     sizeof(ROR_SCAN_INFO*)*
 
3340
                                                     tree->n_ror_scans)))
1900
3341
    return NULL;
1901
3342
  intersect_scans_end= intersect_scans;
1902
3343
 
1903
3344
  /* Create and incrementally update ROR intersection. */
1904
 
  ROR_INTERSECT_INFO *intersect= NULL;
1905
 
  ROR_INTERSECT_INFO *intersect_best= NULL;
1906
 
  if (! (intersect= ror_intersect_init(param)) ||
1907
 
      ! (intersect_best= ror_intersect_init(param)))
 
3345
  ROR_INTERSECT_INFO *intersect, *intersect_best;
 
3346
  if (!(intersect= ror_intersect_init(param)) || 
 
3347
      !(intersect_best= ror_intersect_init(param)))
1908
3348
    return NULL;
1909
3349
 
1910
3350
  /* [intersect_scans,intersect_scans_best) will hold the best intersection */
1911
 
  ROR_SCAN_INFO **intersect_scans_best= NULL;
 
3351
  ROR_SCAN_INFO **intersect_scans_best;
1912
3352
  cur_ror_scan= tree->ror_scans;
1913
3353
  intersect_scans_best= intersect_scans;
1914
3354
  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
1919
3359
      cur_ror_scan++;
1920
3360
      continue;
1921
3361
    }
1922
 
 
 
3362
    
1923
3363
    *(intersect_scans_end++)= *(cur_ror_scan++);
1924
3364
 
1925
3365
    if (intersect->total_cost < min_cost)
1933
3373
 
1934
3374
  if (intersect_scans_best == intersect_scans)
1935
3375
  {
1936
 
    return NULL;
 
3376
    DBUG_PRINT("info", ("None of scans increase selectivity"));
 
3377
    DBUG_RETURN(NULL);
1937
3378
  }
 
3379
    
 
3380
  DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
 
3381
                                          "best ROR-intersection",
 
3382
                                          intersect_scans,
 
3383
                                          intersect_scans_best););
1938
3384
 
1939
3385
  *are_all_covering= intersect->is_covering;
1940
 
  uint32_t best_num= intersect_scans_best - intersect_scans;
 
3386
  uint best_num= intersect_scans_best - intersect_scans;
1941
3387
  ror_intersect_cpy(intersect, intersect_best);
1942
3388
 
1943
3389
  /*
1944
3390
    Ok, found the best ROR-intersection of non-CPK key scans.
1945
 
    Check if we should add a CPK scan. If the obtained ROR-intersection is
 
3391
    Check if we should add a CPK scan. If the obtained ROR-intersection is 
1946
3392
    covering, it doesn't make sense to add CPK scan.
1947
3393
  */
1948
3394
  if (cpk_scan && !intersect->is_covering)
1949
3395
  {
1950
 
    if (ror_intersect_add(intersect, cpk_scan, true) &&
 
3396
    if (ror_intersect_add(intersect, cpk_scan, true) && 
1951
3397
        (intersect->total_cost < min_cost))
1952
3398
    {
1953
3399
      cpk_scan_used= true;
1956
3402
  }
1957
3403
 
1958
3404
  /* Ok, return ROR-intersect plan if we have found one */
1959
 
  optimizer::RorIntersectReadPlan *trp= NULL;
 
3405
  TRP_ROR_INTERSECT *trp= NULL;
1960
3406
  if (min_cost < read_time && (cpk_scan_used || best_num > 1))
1961
3407
  {
1962
 
    if (! (trp= new (param->mem_root) optimizer::RorIntersectReadPlan))
1963
 
      return trp;
1964
 
 
1965
 
    if (! (trp->first_scan=
1966
 
           (ROR_SCAN_INFO**)param->mem_root->alloc_root(sizeof(ROR_SCAN_INFO*)*best_num)))
1967
 
      return NULL;
 
3408
    if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
 
3409
      DBUG_RETURN(trp);
 
3410
    if (!(trp->first_scan=
 
3411
           (ROR_SCAN_INFO**)alloc_root(param->mem_root,
 
3412
                                       sizeof(ROR_SCAN_INFO*)*best_num)))
 
3413
      DBUG_RETURN(NULL);
1968
3414
    memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
1969
3415
    trp->last_scan=  trp->first_scan + best_num;
1970
3416
    trp->is_covering= intersect_best->is_covering;
1971
3417
    trp->read_cost= intersect_best->total_cost;
1972
3418
    /* Prevent divisons by zero */
1973
3419
    ha_rows best_rows = double2rows(intersect_best->out_rows);
1974
 
    if (! best_rows)
 
3420
    if (!best_rows)
1975
3421
      best_rows= 1;
1976
3422
    set_if_smaller(param->table->quick_condition_rows, best_rows);
1977
3423
    trp->records= best_rows;
1978
3424
    trp->index_scan_costs= intersect_best->index_scan_costs;
1979
3425
    trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
 
3426
    DBUG_PRINT("info", ("Returning non-covering ROR-intersect plan:"
 
3427
                        "cost %g, records %lu",
 
3428
                        trp->read_cost, (ulong) trp->records));
1980
3429
  }
1981
 
  return trp;
1982
 
}
1983
 
 
1984
 
 
1985
 
/*
1986
 
  Get best "range" table read plan for given optimizer::SEL_TREE, also update some info
 
3430
  DBUG_RETURN(trp);
 
3431
}
 
3432
 
 
3433
 
 
3434
/*
 
3435
  Get best covering ROR-intersection.
 
3436
  SYNOPSIS
 
3437
    get_best_covering_ror_intersect()
 
3438
      param     Parameter from test_quick_select function.
 
3439
      tree      SEL_TREE with sets of intervals for different keys.
 
3440
      read_time Don't return table read plans with cost > read_time.
 
3441
 
 
3442
  RETURN
 
3443
    Best covering ROR-intersection plan
 
3444
    NULL if no plan found.
 
3445
 
 
3446
  NOTES
 
3447
    get_best_ror_intersect must be called for a tree before calling this
 
3448
    function for it.
 
3449
    This function invalidates tree->ror_scans member values.
 
3450
 
 
3451
  The following approximate algorithm is used:
 
3452
    I=set of all covering indexes
 
3453
    F=set of all fields to cover
 
3454
    S={}
 
3455
 
 
3456
    do
 
3457
    {
 
3458
      Order I by (#covered fields in F desc,
 
3459
                  #components asc,
 
3460
                  number of first not covered component asc);
 
3461
      F=F-covered by first(I);
 
3462
      S=S+first(I);
 
3463
      I=I-first(I);
 
3464
    } while F is not empty.
 
3465
*/
 
3466
 
 
3467
static
 
3468
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
 
3469
                                                   SEL_TREE *tree,
 
3470
                                                   double read_time)
 
3471
{
 
3472
  ROR_SCAN_INFO **ror_scan_mark;
 
3473
  ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
 
3474
  DBUG_ENTER("get_best_covering_ror_intersect");
 
3475
 
 
3476
  for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
 
3477
    (*scan)->key_components=
 
3478
      param->table->key_info[(*scan)->keynr].key_parts;
 
3479
 
 
3480
  /*
 
3481
    Run covering-ROR-search algorithm.
 
3482
    Assume set I is [ror_scan .. ror_scans_end)
 
3483
  */
 
3484
 
 
3485
  /*I=set of all covering indexes */
 
3486
  ror_scan_mark= tree->ror_scans;
 
3487
 
 
3488
  MY_BITMAP *covered_fields= &param->tmp_covered_fields;
 
3489
  if (!covered_fields->bitmap) 
 
3490
    covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
 
3491
                                               param->fields_bitmap_size);
 
3492
  if (!covered_fields->bitmap ||
 
3493
      bitmap_init(covered_fields, covered_fields->bitmap,
 
3494
                  param->table->s->fields, false))
 
3495
    DBUG_RETURN(0);
 
3496
  bitmap_clear_all(covered_fields);
 
3497
 
 
3498
  double total_cost= 0.0f;
 
3499
  ha_rows records=0;
 
3500
  bool all_covered;
 
3501
 
 
3502
  DBUG_PRINT("info", ("Building covering ROR-intersection"));
 
3503
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
 
3504
                                           "building covering ROR-I",
 
3505
                                           ror_scan_mark, ror_scans_end););
 
3506
  do
 
3507
  {
 
3508
    /*
 
3509
      Update changed sorting info:
 
3510
        #covered fields,
 
3511
        number of first not covered component
 
3512
      Calculate and save these values for each of remaining scans.
 
3513
    */
 
3514
    for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
 
3515
    {
 
3516
      bitmap_subtract(&(*scan)->covered_fields, covered_fields);
 
3517
      (*scan)->used_fields_covered=
 
3518
        bitmap_bits_set(&(*scan)->covered_fields);
 
3519
      (*scan)->first_uncovered_field=
 
3520
        bitmap_get_first(&(*scan)->covered_fields);
 
3521
    }
 
3522
 
 
3523
    my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
 
3524
             (qsort_cmp)cmp_ror_scan_info_covering);
 
3525
 
 
3526
    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
 
3527
                                             "remaining scans",
 
3528
                                             ror_scan_mark, ror_scans_end););
 
3529
 
 
3530
    /* I=I-first(I) */
 
3531
    total_cost += (*ror_scan_mark)->index_read_cost;
 
3532
    records += (*ror_scan_mark)->records;
 
3533
    DBUG_PRINT("info", ("Adding scan on %s",
 
3534
                        param->table->key_info[(*ror_scan_mark)->keynr].name));
 
3535
    if (total_cost > read_time)
 
3536
      DBUG_RETURN(NULL);
 
3537
    /* F=F-covered by first(I) */
 
3538
    bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
 
3539
    all_covered= bitmap_is_subset(&param->needed_fields, covered_fields);
 
3540
  } while ((++ror_scan_mark < ror_scans_end) && !all_covered);
 
3541
  
 
3542
  if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
 
3543
    DBUG_RETURN(NULL);
 
3544
 
 
3545
  /*
 
3546
    Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
 
3547
    cost total_cost.
 
3548
  */
 
3549
  DBUG_PRINT("info", ("Covering ROR-intersect scans cost: %g", total_cost));
 
3550
  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
 
3551
                                           "creating covering ROR-intersect",
 
3552
                                           tree->ror_scans, ror_scan_mark););
 
3553
 
 
3554
  /* Add priority queue use cost. */
 
3555
  total_cost += rows2double(records)*
 
3556
                log((double)(ror_scan_mark - tree->ror_scans)) /
 
3557
                (TIME_FOR_COMPARE_ROWID * M_LN2);
 
3558
  DBUG_PRINT("info", ("Covering ROR-intersect full cost: %g", total_cost));
 
3559
 
 
3560
  if (total_cost > read_time)
 
3561
    DBUG_RETURN(NULL);
 
3562
 
 
3563
  TRP_ROR_INTERSECT *trp;
 
3564
  if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
 
3565
    DBUG_RETURN(trp);
 
3566
  uint best_num= (ror_scan_mark - tree->ror_scans);
 
3567
  if (!(trp->first_scan= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
 
3568
                                                     sizeof(ROR_SCAN_INFO*)*
 
3569
                                                     best_num)))
 
3570
    DBUG_RETURN(NULL);
 
3571
  memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
 
3572
  trp->last_scan=  trp->first_scan + best_num;
 
3573
  trp->is_covering= true;
 
3574
  trp->read_cost= total_cost;
 
3575
  trp->records= records;
 
3576
  trp->cpk_scan= NULL;
 
3577
  set_if_smaller(param->table->quick_condition_rows, records); 
 
3578
 
 
3579
  DBUG_PRINT("info",
 
3580
             ("Returning covering ROR-intersect plan: cost %g, records %lu",
 
3581
              trp->read_cost, (ulong) trp->records));
 
3582
  DBUG_RETURN(trp);
 
3583
}
 
3584
 
 
3585
 
 
3586
/*
 
3587
  Get best "range" table read plan for given SEL_TREE, also update some info
1987
3588
 
1988
3589
  SYNOPSIS
1989
3590
    get_key_scans_params()
1990
 
      session
1991
3591
      param                    Parameters from test_quick_select
1992
 
      tree                     Make range select for this optimizer::SEL_TREE
 
3592
      tree                     Make range select for this SEL_TREE
1993
3593
      index_read_must_be_used  true <=> assume 'index only' option will be set
1994
3594
                               (except for clustered PK indexes)
1995
3595
      update_tbl_stats         true <=> update table->quick_* with information
1996
3596
                               about range scans we've evaluated.
1997
 
      read_time                Maximum cost. i.e. don't create read plans with
 
3597
      read_time                Maximum cost. i.e. don't create read plans with 
1998
3598
                               cost > read_time.
1999
3599
 
2000
3600
  DESCRIPTION
2001
 
    Find the best "range" table read plan for given optimizer::SEL_TREE.
2002
 
    The side effects are
 
3601
    Find the best "range" table read plan for given SEL_TREE. 
 
3602
    The side effects are 
2003
3603
     - tree->ror_scans is updated to indicate which scans are ROR scans.
2004
3604
     - if update_tbl_stats=true then table->quick_* is updated with info
2005
3605
       about every possible range scan.
2009
3609
    NULL if no plan found or error occurred
2010
3610
*/
2011
3611
 
2012
 
static optimizer::RangeReadPlan *get_key_scans_params(Session *session,
2013
 
                                                      optimizer::Parameter *param,
2014
 
                                                      optimizer::SEL_TREE *tree,
2015
 
                                                      bool index_read_must_be_used,
2016
 
                                                      bool update_tbl_stats,
2017
 
                                                      double read_time)
 
3612
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
 
3613
                                       bool index_read_must_be_used, 
 
3614
                                       bool update_tbl_stats,
 
3615
                                       double read_time)
2018
3616
{
2019
 
  uint32_t idx;
2020
 
  optimizer::SEL_ARG **key= NULL;
2021
 
  optimizer::SEL_ARG **end= NULL;
2022
 
  optimizer::SEL_ARG **key_to_read= NULL;
 
3617
  uint idx;
 
3618
  SEL_ARG **key,**end, **key_to_read= NULL;
2023
3619
  ha_rows best_records= 0;
2024
 
  uint32_t best_mrr_flags= 0;
2025
 
  uint32_t best_buf_size= 0;
2026
 
  optimizer::RangeReadPlan *read_plan= NULL;
 
3620
  uint    best_mrr_flags= 0, best_buf_size= 0;
 
3621
  TRP_RANGE* read_plan= NULL;
 
3622
  DBUG_ENTER("get_key_scans_params");
2027
3623
  /*
2028
 
    Note that there may be trees that have type optimizer::SEL_TREE::KEY but contain no
 
3624
    Note that there may be trees that have type SEL_TREE::KEY but contain no
2029
3625
    key reads at all, e.g. tree for expression "key1 is not null" where key1
2030
3626
    is defined as "not null".
2031
3627
  */
2032
 
  tree->ror_scans_map.reset();
 
3628
  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
 
3629
                                      "tree scans"););
 
3630
  tree->ror_scans_map.clear_all();
2033
3631
  tree->n_ror_scans= 0;
2034
3632
  for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
2035
3633
  {
2036
3634
    if (*key)
2037
3635
    {
2038
3636
      ha_rows found_records;
2039
 
      optimizer::CostVector cost;
2040
 
      double found_read_time= 0.0;
2041
 
      uint32_t mrr_flags, buf_size;
2042
 
      uint32_t keynr= param->real_keynr[idx];
2043
 
      if ((*key)->type == optimizer::SEL_ARG::MAYBE_KEY ||
 
3637
      COST_VECT cost;
 
3638
      double found_read_time;
 
3639
      uint mrr_flags, buf_size;
 
3640
      uint keynr= param->real_keynr[idx];
 
3641
      if ((*key)->type == SEL_ARG::MAYBE_KEY ||
2044
3642
          (*key)->maybe_flag)
2045
 
        param->needed_reg->set(keynr);
2046
 
 
2047
 
      bool read_index_only= index_read_must_be_used ||
2048
 
                            param->table->covering_keys.test(keynr);
2049
 
 
2050
 
      found_records= check_quick_select(session, param, idx, read_index_only, *key,
 
3643
        param->needed_reg->set_bit(keynr);
 
3644
 
 
3645
      bool read_index_only= index_read_must_be_used || 
 
3646
                            param->table->covering_keys.is_set(keynr);
 
3647
 
 
3648
      found_records= check_quick_select(param, idx, read_index_only, *key,
2051
3649
                                        update_tbl_stats, &mrr_flags,
2052
3650
                                        &buf_size, &cost);
2053
3651
      found_read_time= cost.total_cost();
2054
3652
      if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
2055
3653
      {
2056
3654
        tree->n_ror_scans++;
2057
 
        tree->ror_scans_map.set(idx);
 
3655
        tree->ror_scans_map.set_bit(idx);
2058
3656
      }
2059
3657
      if (read_time > found_read_time && found_records != HA_POS_ERROR)
2060
3658
      {
2067
3665
    }
2068
3666
  }
2069
3667
 
 
3668
  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->ror_scans_map,
 
3669
                                      "ROR scans"););
2070
3670
  if (key_to_read)
2071
3671
  {
2072
3672
    idx= key_to_read - tree->keys;
2073
 
    if ((read_plan= new (param->mem_root) optimizer::RangeReadPlan(*key_to_read, idx,
2074
 
                                                                   best_mrr_flags)))
 
3673
    if ((read_plan= new (param->mem_root) TRP_RANGE(*key_to_read, idx,
 
3674
                                                    best_mrr_flags)))
2075
3675
    {
2076
3676
      read_plan->records= best_records;
2077
 
      read_plan->is_ror= tree->ror_scans_map.test(idx);
 
3677
      read_plan->is_ror= tree->ror_scans_map.is_set(idx);
2078
3678
      read_plan->read_cost= read_time;
2079
3679
      read_plan->mrr_buf_size= best_buf_size;
 
3680
      DBUG_PRINT("info",
 
3681
                 ("Returning range plan for key %s, cost %g, records %lu",
 
3682
                  param->table->key_info[param->real_keynr[idx]].name,
 
3683
                  read_plan->read_cost, (ulong) read_plan->records));
2080
3684
    }
2081
3685
  }
 
3686
  else
 
3687
    DBUG_PRINT("info", ("No 'range' table read plan found"));
2082
3688
 
2083
 
  return(read_plan);
 
3689
  DBUG_RETURN(read_plan);
2084
3690
}
2085
3691
 
2086
3692
 
2087
 
optimizer::QuickSelectInterface *optimizer::IndexMergeReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *)
 
3693
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
 
3694
                                            bool retrieve_full_rows,
 
3695
                                            MEM_ROOT *parent_alloc)
2088
3696
{
2089
 
  optimizer::QuickIndexMergeSelect *quick_imerge;
2090
 
  optimizer::QuickRangeSelect *quick= NULL;
 
3697
  QUICK_INDEX_MERGE_SELECT *quick_imerge;
 
3698
  QUICK_RANGE_SELECT *quick;
2091
3699
  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
2092
 
  if (! (quick_imerge= new optimizer::QuickIndexMergeSelect(param->session, param->table)))
2093
 
  {
 
3700
  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
2094
3701
    return NULL;
2095
 
  }
2096
3702
 
2097
3703
  quick_imerge->records= records;
2098
3704
  quick_imerge->read_time= read_cost;
2099
 
  for (optimizer::RangeReadPlan **range_scan= range_scans; 
2100
 
       range_scan != range_scans_end;
 
3705
  for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
2101
3706
       range_scan++)
2102
3707
  {
2103
 
    if (! (quick= (optimizer::QuickRangeSelect*)
2104
 
          ((*range_scan)->make_quick(param, false, &quick_imerge->alloc))) ||
 
3708
    if (!(quick= (QUICK_RANGE_SELECT*)
 
3709
          ((*range_scan)->make_quick(param, false, &quick_imerge->alloc)))||
2105
3710
        quick_imerge->push_quick_back(quick))
2106
3711
    {
2107
3712
      delete quick;
2112
3717
  return quick_imerge;
2113
3718
}
2114
3719
 
2115
 
optimizer::QuickSelectInterface *optimizer::RorIntersectReadPlan::make_quick(optimizer::Parameter *param,
2116
 
                                                                             bool retrieve_full_rows,
2117
 
                                                                             memory::Root *parent_alloc)
 
3720
QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
 
3721
                                              bool retrieve_full_rows,
 
3722
                                              MEM_ROOT *parent_alloc)
2118
3723
{
2119
 
  optimizer::QuickRorIntersectSelect *quick_intersect= NULL;
2120
 
  optimizer::QuickRangeSelect *quick= NULL;
2121
 
  memory::Root *alloc= NULL;
 
3724
  QUICK_ROR_INTERSECT_SELECT *quick_intrsect;
 
3725
  QUICK_RANGE_SELECT *quick;
 
3726
  DBUG_ENTER("TRP_ROR_INTERSECT::make_quick");
 
3727
  MEM_ROOT *alloc;
2122
3728
 
2123
 
  if ((quick_intersect=
2124
 
         new optimizer::QuickRorIntersectSelect(param->session,
2125
 
                                                param->table,
2126
 
                                                (retrieve_full_rows? (! is_covering) : false),
2127
 
                                                parent_alloc)))
 
3729
  if ((quick_intrsect=
 
3730
         new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
 
3731
                                        (retrieve_full_rows? (!is_covering) :
 
3732
                                         false),
 
3733
                                        parent_alloc)))
2128
3734
  {
2129
 
    alloc= parent_alloc ? parent_alloc : &quick_intersect->alloc;
2130
 
    for (; first_scan != last_scan; ++first_scan)
 
3735
    DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
 
3736
                                             "creating ROR-intersect",
 
3737
                                             first_scan, last_scan););
 
3738
    alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
 
3739
    for (; first_scan != last_scan;++first_scan)
2131
3740
    {
2132
 
      if (! (quick= optimizer::get_quick_select(param,
2133
 
                                                (*first_scan)->idx,
2134
 
                                                (*first_scan)->sel_arg,
2135
 
                                                HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
2136
 
                                                0,
2137
 
                                                alloc)) ||
2138
 
          quick_intersect->push_quick_back(quick))
 
3741
      if (!(quick= get_quick_select(param, (*first_scan)->idx,
 
3742
                                    (*first_scan)->sel_arg,
 
3743
                                    HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
 
3744
                                    0, alloc)) ||
 
3745
          quick_intrsect->push_quick_back(quick))
2139
3746
      {
2140
 
        delete quick_intersect;
2141
 
        return NULL;
 
3747
        delete quick_intrsect;
 
3748
        DBUG_RETURN(NULL);
2142
3749
      }
2143
3750
    }
2144
3751
    if (cpk_scan)
2145
3752
    {
2146
 
      if (! (quick= optimizer::get_quick_select(param,
2147
 
                                                cpk_scan->idx,
2148
 
                                                cpk_scan->sel_arg,
2149
 
                                                HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
2150
 
                                                0,
2151
 
                                                alloc)))
 
3753
      if (!(quick= get_quick_select(param, cpk_scan->idx,
 
3754
                                    cpk_scan->sel_arg,
 
3755
                                    HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
 
3756
                                    0, alloc)))
2152
3757
      {
2153
 
        delete quick_intersect;
2154
 
        return NULL;
 
3758
        delete quick_intrsect;
 
3759
        DBUG_RETURN(NULL);
2155
3760
      }
2156
 
      quick->resetCursor();
2157
 
      quick_intersect->cpk_quick= quick;
 
3761
      quick->file= NULL; 
 
3762
      quick_intrsect->cpk_quick= quick;
2158
3763
    }
2159
 
    quick_intersect->records= records;
2160
 
    quick_intersect->read_time= read_cost;
 
3764
    quick_intrsect->records= records;
 
3765
    quick_intrsect->read_time= read_cost;
2161
3766
  }
2162
 
  return quick_intersect;
 
3767
  DBUG_RETURN(quick_intrsect);
2163
3768
}
2164
3769
 
2165
3770
 
2166
 
optimizer::QuickSelectInterface *optimizer::RorUnionReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *)
 
3771
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
 
3772
                                          bool retrieve_full_rows,
 
3773
                                          MEM_ROOT *parent_alloc)
2167
3774
{
2168
 
  optimizer::QuickRorUnionSelect *quick_roru= NULL;
2169
 
  optimizer::TableReadPlan **scan= NULL;
2170
 
  optimizer::QuickSelectInterface *quick= NULL;
 
3775
  QUICK_ROR_UNION_SELECT *quick_roru;
 
3776
  TABLE_READ_PLAN **scan;
 
3777
  QUICK_SELECT_I *quick;
 
3778
  DBUG_ENTER("TRP_ROR_UNION::make_quick");
2171
3779
  /*
2172
3780
    It is impossible to construct a ROR-union that will not retrieve full
2173
3781
    rows, ignore retrieve_full_rows parameter.
2174
3782
  */
2175
 
  if ((quick_roru= new optimizer::QuickRorUnionSelect(param->session, param->table)))
 
3783
  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
2176
3784
  {
2177
3785
    for (scan= first_ror; scan != last_ror; scan++)
2178
3786
    {
2179
 
      if (! (quick= (*scan)->make_quick(param, false, &quick_roru->alloc)) ||
 
3787
      if (!(quick= (*scan)->make_quick(param, false, &quick_roru->alloc)) ||
2180
3788
          quick_roru->push_quick_back(quick))
2181
 
      {
2182
 
        return NULL;
2183
 
      }
 
3789
        DBUG_RETURN(NULL);
2184
3790
    }
2185
3791
    quick_roru->records= records;
2186
3792
    quick_roru->read_time= read_cost;
2187
3793
  }
2188
 
  return quick_roru;
 
3794
  DBUG_RETURN(quick_roru);
2189
3795
}
2190
3796
 
2191
3797
 
2192
3798
/*
2193
 
  Build a optimizer::SEL_TREE for <> or NOT BETWEEN predicate
2194
 
 
 
3799
  Build a SEL_TREE for <> or NOT BETWEEN predicate
 
3800
 
2195
3801
  SYNOPSIS
2196
3802
    get_ne_mm_tree()
2197
 
      param       Parameter from SqlSelect::test_quick_select
 
3803
      param       PARAM from SQL_SELECT::test_quick_select
2198
3804
      cond_func   item for the predicate
2199
3805
      field       field in the predicate
2200
3806
      lt_value    constant that field should be smaller
2201
3807
      gt_value    constant that field should be greaterr
2202
3808
      cmp_type    compare type for the field
2203
3809
 
2204
 
  RETURN
 
3810
  RETURN 
2205
3811
    #  Pointer to tree built tree
2206
3812
    0  on error
2207
3813
*/
2208
 
static optimizer::SEL_TREE *get_ne_mm_tree(optimizer::RangeParameter *param,
2209
 
                                Item_func *cond_func,
 
3814
 
 
3815
static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
2210
3816
                                Field *field,
2211
3817
                                Item *lt_value, Item *gt_value,
2212
3818
                                Item_result cmp_type)
2213
3819
{
2214
 
  optimizer::SEL_TREE *tree= NULL;
 
3820
  SEL_TREE *tree;
2215
3821
  tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
2216
3822
                     lt_value, cmp_type);
2217
3823
  if (tree)
2218
3824
  {
2219
 
    tree= tree_or(param,
2220
 
                  tree,
2221
 
                  get_mm_parts(param, cond_func, field,
2222
 
                                                Item_func::GT_FUNC,
2223
 
                                                gt_value,
2224
 
                  cmp_type));
 
3825
    tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
 
3826
                                            Item_func::GT_FUNC,
 
3827
                                            gt_value, cmp_type));
2225
3828
  }
2226
3829
  return tree;
2227
3830
}
2228
 
 
 
3831
   
2229
3832
 
2230
3833
/*
2231
 
  Build a optimizer::SEL_TREE for a simple predicate
2232
 
 
 
3834
  Build a SEL_TREE for a simple predicate
 
3835
 
2233
3836
  SYNOPSIS
2234
3837
    get_func_mm_tree()
2235
 
      param       Parameter from SqlSelect::test_quick_select
 
3838
      param       PARAM from SQL_SELECT::test_quick_select
2236
3839
      cond_func   item for the predicate
2237
3840
      field       field in the predicate
2238
3841
      value       constant in the predicate
2239
3842
      cmp_type    compare type for the field
2240
3843
      inv         true <> NOT cond_func is considered
2241
 
                  (makes sense only when cond_func is BETWEEN or IN)
 
3844
                  (makes sense only when cond_func is BETWEEN or IN) 
2242
3845
 
2243
 
  RETURN
 
3846
  RETURN 
2244
3847
    Pointer to the tree built tree
2245
3848
*/
2246
 
static optimizer::SEL_TREE *get_func_mm_tree(optimizer::RangeParameter *param,
2247
 
                                  Item_func *cond_func,
2248
 
                                  Field *field, 
2249
 
                                  Item *value,
2250
 
                                  Item_result cmp_type, 
2251
 
                                  bool inv)
 
3849
 
 
3850
static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func, 
 
3851
                                  Field *field, Item *value,
 
3852
                                  Item_result cmp_type, bool inv)
2252
3853
{
2253
 
  optimizer::SEL_TREE *tree= NULL;
 
3854
  SEL_TREE *tree= 0;
 
3855
  DBUG_ENTER("get_func_mm_tree");
2254
3856
 
2255
 
  switch (cond_func->functype()) 
2256
 
  {
 
3857
  switch (cond_func->functype()) {
2257
3858
 
2258
3859
  case Item_func::NE_FUNC:
2259
3860
    tree= get_ne_mm_tree(param, cond_func, field, value, value, cmp_type);
2261
3862
 
2262
3863
  case Item_func::BETWEEN:
2263
3864
  {
2264
 
    if (! value)
 
3865
    if (!value)
2265
3866
    {
2266
3867
      if (inv)
2267
3868
      {
2268
 
        tree= get_ne_mm_tree(param, 
2269
 
                             cond_func, 
2270
 
                             field, 
2271
 
                             cond_func->arguments()[1],
2272
 
                             cond_func->arguments()[2], 
2273
 
                             cmp_type);
 
3869
        tree= get_ne_mm_tree(param, cond_func, field, cond_func->arguments()[1],
 
3870
                             cond_func->arguments()[2], cmp_type);
2274
3871
      }
2275
3872
      else
2276
3873
      {
2277
 
        tree= get_mm_parts(param, 
2278
 
                           cond_func, 
2279
 
                           field, 
2280
 
                           Item_func::GE_FUNC,
2281
 
                                       cond_func->arguments()[1],
2282
 
                           cmp_type);
 
3874
        tree= get_mm_parts(param, cond_func, field, Item_func::GE_FUNC,
 
3875
                           cond_func->arguments()[1],cmp_type);
2283
3876
        if (tree)
2284
3877
        {
2285
 
          tree= tree_and(param, 
2286
 
                         tree, 
2287
 
                         get_mm_parts(param, cond_func, field,
2288
 
                                                       Item_func::LE_FUNC,
2289
 
                                                       cond_func->arguments()[2],
2290
 
                         cmp_type));
 
3878
          tree= tree_and(param, tree, get_mm_parts(param, cond_func, field,
 
3879
                                                   Item_func::LE_FUNC,
 
3880
                                                   cond_func->arguments()[2],
 
3881
                                                   cmp_type));
2291
3882
        }
2292
3883
      }
2293
3884
    }
2294
3885
    else
2295
 
      tree= get_mm_parts(param, 
2296
 
                         cond_func, 
2297
 
                         field,
 
3886
      tree= get_mm_parts(param, cond_func, field,
2298
3887
                         (inv ?
2299
3888
                          (value == (Item*)1 ? Item_func::GT_FUNC :
2300
3889
                                               Item_func::LT_FUNC):
2301
3890
                          (value == (Item*)1 ? Item_func::LE_FUNC :
2302
3891
                                               Item_func::GE_FUNC)),
2303
 
                         cond_func->arguments()[0], 
2304
 
                         cmp_type);
 
3892
                         cond_func->arguments()[0], cmp_type);
2305
3893
    break;
2306
3894
  }
2307
3895
  case Item_func::IN_FUNC:
2308
3896
  {
2309
 
    Item_func_in *func= (Item_func_in*) cond_func;
 
3897
    Item_func_in *func=(Item_func_in*) cond_func;
2310
3898
 
2311
3899
    /*
2312
3900
      Array for IN() is constructed when all values have the same result
2313
3901
      type. Tree won't be built for values with different result types,
2314
3902
      so we check it here to avoid unnecessary work.
2315
3903
    */
2316
 
    if (! func->arg_types_compatible)
2317
 
      break;
 
3904
    if (!func->arg_types_compatible)
 
3905
      break;     
2318
3906
 
2319
3907
    if (inv)
2320
3908
    {
2322
3910
      {
2323
3911
        /*
2324
3912
          We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
2325
 
          where c{i} are constants. Our goal is to produce a optimizer::SEL_TREE that
 
3913
          where c{i} are constants. Our goal is to produce a SEL_TREE that 
2326
3914
          represents intervals:
2327
 
 
 
3915
          
2328
3916
          ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ...    (*)
2329
 
 
 
3917
          
2330
3918
          where $MIN is either "-inf" or NULL.
2331
 
 
 
3919
          
2332
3920
          The most straightforward way to produce it is to convert NOT IN
2333
3921
          into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
2334
 
          analyzer to build optimizer::SEL_TREE from that. The problem is that the
 
3922
          analyzer to build SEL_TREE from that. The problem is that the
2335
3923
          range analyzer will use O(N^2) memory (which is probably a bug),
2336
3924
          and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
2337
3925
          will run out of memory.
2338
3926
 
2339
3927
          Another problem with big lists like (*) is that a big list is
2340
3928
          unlikely to produce a good "range" access, while considering that
2341
 
          range access will require expensive CPU calculations (and for
 
3929
          range access will require expensive CPU calculations (and for 
2342
3930
          MyISAM even index accesses). In short, big NOT IN lists are rarely
2343
3931
          worth analyzing.
2344
3932
 
2345
3933
          Considering the above, we'll handle NOT IN as follows:
2346
3934
          * if the number of entries in the NOT IN list is less than
2347
 
            NOT_IN_IGNORE_THRESHOLD, construct the optimizer::SEL_TREE (*) manually.
2348
 
          * Otherwise, don't produce a optimizer::SEL_TREE.
 
3935
            NOT_IN_IGNORE_THRESHOLD, construct the SEL_TREE (*) manually.
 
3936
          * Otherwise, don't produce a SEL_TREE.
2349
3937
        */
2350
3938
#define NOT_IN_IGNORE_THRESHOLD 1000
2351
 
        memory::Root *tmp_root= param->mem_root;
2352
 
        param->session->mem_root= param->old_root;
2353
 
        /*
 
3939
        MEM_ROOT *tmp_root= param->mem_root;
 
3940
        param->thd->mem_root= param->old_root;
 
3941
        /* 
2354
3942
          Create one Item_type constant object. We'll need it as
2355
3943
          get_mm_parts only accepts constant values wrapped in Item_Type
2356
3944
          objects.
2357
3945
          We create the Item on param->mem_root which points to
2358
 
          per-statement mem_root (while session->mem_root is currently pointing
 
3946
          per-statement mem_root (while thd->mem_root is currently pointing
2359
3947
          to mem_root local to range optimizer).
2360
3948
        */
2361
3949
        Item *value_item= func->array->create_item();
2362
 
        param->session->mem_root= tmp_root;
 
3950
        param->thd->mem_root= tmp_root;
2363
3951
 
2364
 
        if (func->array->count > NOT_IN_IGNORE_THRESHOLD || ! value_item)
 
3952
        if (func->array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
2365
3953
          break;
2366
3954
 
2367
 
        /* Get a optimizer::SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
2368
 
        uint32_t i=0;
2369
 
        do
 
3955
        /* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval.  */
 
3956
        uint i=0;
 
3957
        do 
2370
3958
        {
2371
3959
          func->array->value_to_item(i, value_item);
2372
 
          tree= get_mm_parts(param, 
2373
 
                             cond_func, 
2374
 
                             field, Item_func::LT_FUNC,
2375
 
                             value_item, 
2376
 
                             cmp_type);
2377
 
          if (! tree)
 
3960
          tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
 
3961
                             value_item, cmp_type);
 
3962
          if (!tree)
2378
3963
            break;
2379
3964
          i++;
2380
 
        } while (i < func->array->count && tree->type == optimizer::SEL_TREE::IMPOSSIBLE);
 
3965
        } while (i < func->array->count && tree->type == SEL_TREE::IMPOSSIBLE);
2381
3966
 
2382
 
        if (!tree || tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
 
3967
        if (!tree || tree->type == SEL_TREE::IMPOSSIBLE)
2383
3968
        {
2384
3969
          /* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
2385
3970
          tree= NULL;
2386
3971
          break;
2387
3972
        }
2388
 
        optimizer::SEL_TREE *tree2= NULL;
 
3973
        SEL_TREE *tree2;
2389
3974
        for (; i < func->array->count; i++)
2390
3975
        {
2391
3976
          if (func->array->compare_elems(i, i-1))
2392
3977
          {
2393
 
            /* Get a optimizer::SEL_TREE for "-inf < X < c_i" interval */
 
3978
            /* Get a SEL_TREE for "-inf < X < c_i" interval */
2394
3979
            func->array->value_to_item(i, value_item);
2395
3980
            tree2= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
2396
3981
                                value_item, cmp_type);
2401
3986
            }
2402
3987
 
2403
3988
            /* Change all intervals to be "c_{i-1} < X < c_i" */
2404
 
            for (uint32_t idx= 0; idx < param->keys; idx++)
 
3989
            for (uint idx= 0; idx < param->keys; idx++)
2405
3990
            {
2406
 
              optimizer::SEL_ARG *new_interval, *last_val;
 
3991
              SEL_ARG *new_interval, *last_val;
2407
3992
              if (((new_interval= tree2->keys[idx])) &&
2408
3993
                  (tree->keys[idx]) &&
2409
3994
                  ((last_val= tree->keys[idx]->last())))
2412
3997
                new_interval->min_flag= NEAR_MIN;
2413
3998
              }
2414
3999
            }
2415
 
            /*
 
4000
            /* 
2416
4001
              The following doesn't try to allocate memory so no need to
2417
4002
              check for NULL.
2418
4003
            */
2419
4004
            tree= tree_or(param, tree, tree2);
2420
4005
          }
2421
4006
        }
2422
 
 
2423
 
        if (tree && tree->type != optimizer::SEL_TREE::IMPOSSIBLE)
 
4007
        
 
4008
        if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
2424
4009
        {
2425
 
          /*
2426
 
            Get the optimizer::SEL_TREE for the last "c_last < X < +inf" interval
 
4010
          /* 
 
4011
            Get the SEL_TREE for the last "c_last < X < +inf" interval 
2427
4012
            (value_item cotains c_last already)
2428
4013
          */
2429
4014
          tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
2442
4027
          for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
2443
4028
               arg < end ; arg++)
2444
4029
          {
2445
 
            tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field,
 
4030
            tree=  tree_and(param, tree, get_ne_mm_tree(param, cond_func, field, 
2446
4031
                                                        *arg, *arg, cmp_type));
2447
4032
          }
2448
4033
        }
2449
4034
      }
2450
4035
    }
2451
4036
    else
2452
 
    {
 
4037
    {    
2453
4038
      tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
2454
4039
                         func->arguments()[1], cmp_type);
2455
4040
      if (tree)
2458
4043
        for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
2459
4044
             arg < end ; arg++)
2460
4045
        {
2461
 
          tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
 
4046
          tree= tree_or(param, tree, get_mm_parts(param, cond_func, field, 
2462
4047
                                                  Item_func::EQ_FUNC,
2463
4048
                                                  *arg, cmp_type));
2464
4049
        }
2466
4051
    }
2467
4052
    break;
2468
4053
  }
2469
 
  default:
 
4054
  default: 
2470
4055
  {
2471
 
    /*
 
4056
    /* 
2472
4057
       Here the function for the following predicates are processed:
2473
4058
       <, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL.
2474
4059
       If the predicate is of the form (value op field) it is handled
2482
4067
  }
2483
4068
  }
2484
4069
 
2485
 
  return(tree);
 
4070
  DBUG_RETURN(tree);
2486
4071
}
2487
4072
 
2488
4073
 
2489
4074
/*
2490
 
  Build conjunction of all optimizer::SEL_TREEs for a simple predicate applying equalities
2491
 
 
 
4075
  Build conjunction of all SEL_TREEs for a simple predicate applying equalities
 
4076
 
2492
4077
  SYNOPSIS
2493
4078
    get_full_func_mm_tree()
2494
 
      param       Parameter from SqlSelect::test_quick_select
 
4079
      param       PARAM from SQL_SELECT::test_quick_select
2495
4080
      cond_func   item for the predicate
2496
4081
      field_item  field in the predicate
2497
4082
      value       constant in the predicate
2498
4083
                  (for BETWEEN it contains the number of the field argument,
2499
 
                   for IN it's always 0)
 
4084
                   for IN it's always 0) 
2500
4085
      inv         true <> NOT cond_func is considered
2501
4086
                  (makes sense only when cond_func is BETWEEN or IN)
2502
4087
 
2503
4088
  DESCRIPTION
2504
4089
    For a simple SARGable predicate of the form (f op c), where f is a field and
2505
 
    c is a constant, the function builds a conjunction of all optimizer::SEL_TREES that can
 
4090
    c is a constant, the function builds a conjunction of all SEL_TREES that can
2506
4091
    be obtained by the substitution of f for all different fields equal to f.
2507
4092
 
2508
 
  NOTES
 
4093
  NOTES  
2509
4094
    If the WHERE condition contains a predicate (fi op c),
2510
4095
    then not only SELL_TREE for this predicate is built, but
2511
4096
    the trees for the results of substitution of fi for
2512
4097
    each fj belonging to the same multiple equality as fi
2513
4098
    are built as well.
2514
 
    E.g. for WHERE t1.a=t2.a AND t2.a > 10
2515
 
    a optimizer::SEL_TREE for t2.a > 10 will be built for quick select from t2
2516
 
    and
2517
 
    a optimizer::SEL_TREE for t1.a > 10 will be built for quick select from t1.
 
4099
    E.g. for WHERE t1.a=t2.a AND t2.a > 10 
 
4100
    a SEL_TREE for t2.a > 10 will be built for quick select from t2
 
4101
    and   
 
4102
    a SEL_TREE for t1.a > 10 will be built for quick select from t1.
2518
4103
 
2519
4104
    A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
2520
4105
    in a similar way: we build a conjuction of trees for the results
2522
4107
    Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
2523
4108
    differently. It is considered as a conjuction of two SARGable
2524
4109
    predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
2525
 
    is called for each of them separately producing trees for
2526
 
       AND j (f1j <=c ) and AND j (f2j <= c)
 
4110
    is called for each of them separately producing trees for 
 
4111
       AND j (f1j <=c ) and AND j (f2j <= c) 
2527
4112
    After this these two trees are united in one conjunctive tree.
2528
4113
    It's easy to see that the same tree is obtained for
2529
4114
       AND j,k (f1j <=c AND f2k<=c)
2530
 
    which is equivalent to
 
4115
    which is equivalent to 
2531
4116
       AND j,k (c BETWEEN f1j AND f2k).
2532
4117
    The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
2533
4118
    which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
2534
4119
    function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
2535
4120
    producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
2536
 
    trees are united in one OR-tree. The expression
 
4121
    trees are united in one OR-tree. The expression 
2537
4122
      (AND j (f1j > c) OR AND j (f2j < c)
2538
4123
    is equivalent to the expression
2539
 
      AND j,k (f1j > c OR f2k < c)
2540
 
    which is just a translation of
 
4124
      AND j,k (f1j > c OR f2k < c) 
 
4125
    which is just a translation of 
2541
4126
      AND j,k (c NOT BETWEEN f1j AND f2k)
2542
4127
 
2543
4128
    In the cases when one of the items f1, f2 is a constant c1 we do not create
2550
4135
    As to IN predicates only ones of the form (f IN (c1,...,cn)),
2551
4136
    where f1 is a field and c1,...,cn are constant, are considered as
2552
4137
    SARGable. We never try to narrow the index scan using predicates of
2553
 
    the form (c IN (c1,...,f,...,cn)).
2554
 
 
2555
 
  RETURN
2556
 
    Pointer to the tree representing the built conjunction of optimizer::SEL_TREEs
 
4138
    the form (c IN (c1,...,f,...,cn)). 
 
4139
      
 
4140
  RETURN 
 
4141
    Pointer to the tree representing the built conjunction of SEL_TREEs
2557
4142
*/
2558
4143
 
2559
 
static optimizer::SEL_TREE *get_full_func_mm_tree(optimizer::RangeParameter *param,
 
4144
static SEL_TREE *get_full_func_mm_tree(RANGE_OPT_PARAM *param,
2560
4145
                                       Item_func *cond_func,
2561
 
                                       Item_field *field_item, Item *value,
 
4146
                                       Item_field *field_item, Item *value, 
2562
4147
                                       bool inv)
2563
4148
{
2564
 
  optimizer::SEL_TREE *tree= 0;
2565
 
  optimizer::SEL_TREE *ftree= 0;
 
4149
  SEL_TREE *tree= 0;
 
4150
  SEL_TREE *ftree= 0;
2566
4151
  table_map ref_tables= 0;
2567
4152
  table_map param_comp= ~(param->prev_tables | param->read_tables |
2568
4153
                          param->current_table);
 
4154
  DBUG_ENTER("get_full_func_mm_tree");
2569
4155
 
2570
 
  for (uint32_t i= 0; i < cond_func->arg_count; i++)
 
4156
  for (uint i= 0; i < cond_func->arg_count; i++)
2571
4157
  {
2572
4158
    Item *arg= cond_func->arguments()[i]->real_item();
2573
4159
    if (arg != field_item)
2574
4160
      ref_tables|= arg->used_tables();
2575
4161
  }
2576
 
 
2577
4162
  Field *field= field_item->field;
2578
 
  field->setWriteSet();
2579
 
 
2580
4163
  Item_result cmp_type= field->cmp_type();
2581
 
  if (!((ref_tables | field->getTable()->map) & param_comp))
 
4164
  if (!((ref_tables | field->table->map) & param_comp))
2582
4165
    ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
2583
4166
  Item_equal *item_equal= field_item->item_equal;
2584
4167
  if (item_equal)
2588
4171
    while ((item= it++))
2589
4172
    {
2590
4173
      Field *f= item->field;
2591
 
      f->setWriteSet();
2592
 
 
2593
4174
      if (field->eq(f))
2594
4175
        continue;
2595
 
      if (!((ref_tables | f->getTable()->map) & param_comp))
 
4176
      if (!((ref_tables | f->table->map) & param_comp))
2596
4177
      {
2597
4178
        tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
2598
4179
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
2599
4180
      }
2600
4181
    }
2601
4182
  }
2602
 
  return(ftree);
 
4183
  DBUG_RETURN(ftree);
2603
4184
}
2604
4185
 
2605
4186
        /* make a select tree of all keys in condition */
2606
4187
 
2607
 
static optimizer::SEL_TREE *get_mm_tree(optimizer::RangeParameter *param, COND *cond)
 
4188
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond)
2608
4189
{
2609
 
  optimizer::SEL_TREE *tree=0;
2610
 
  optimizer::SEL_TREE *ftree= 0;
 
4190
  SEL_TREE *tree=0;
 
4191
  SEL_TREE *ftree= 0;
2611
4192
  Item_field *field_item= 0;
2612
4193
  bool inv= false;
2613
4194
  Item *value= 0;
 
4195
  DBUG_ENTER("get_mm_tree");
2614
4196
 
2615
4197
  if (cond->type() == Item::COND_ITEM)
2616
4198
  {
2622
4204
      Item *item;
2623
4205
      while ((item=li++))
2624
4206
      {
2625
 
        optimizer::SEL_TREE *new_tree= get_mm_tree(param,item);
2626
 
        if (param->session->is_fatal_error ||
2627
 
            param->alloced_sel_args > optimizer::SEL_ARG::MAX_SEL_ARGS)
2628
 
          return 0;     // out of memory
 
4207
        SEL_TREE *new_tree=get_mm_tree(param,item);
 
4208
        if (param->thd->is_fatal_error || 
 
4209
            param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
 
4210
          DBUG_RETURN(0);       // out of memory
2629
4211
        tree=tree_and(param,tree,new_tree);
2630
 
        if (tree && tree->type == optimizer::SEL_TREE::IMPOSSIBLE)
 
4212
        if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
2631
4213
          break;
2632
4214
      }
2633
4215
    }
2634
4216
    else
2635
4217
    {                                           // COND OR
2636
 
      tree= get_mm_tree(param,li++);
 
4218
      tree=get_mm_tree(param,li++);
2637
4219
      if (tree)
2638
4220
      {
2639
4221
        Item *item;
2640
4222
        while ((item=li++))
2641
4223
        {
2642
 
          optimizer::SEL_TREE *new_tree= get_mm_tree(param,item);
 
4224
          SEL_TREE *new_tree=get_mm_tree(param,item);
2643
4225
          if (!new_tree)
2644
 
            return 0;   // out of memory
 
4226
            DBUG_RETURN(0);     // out of memory
2645
4227
          tree=tree_or(param,tree,new_tree);
2646
 
          if (!tree || tree->type == optimizer::SEL_TREE::ALWAYS)
 
4228
          if (!tree || tree->type == SEL_TREE::ALWAYS)
2647
4229
            break;
2648
4230
        }
2649
4231
      }
2650
4232
    }
2651
 
    return(tree);
 
4233
    DBUG_RETURN(tree);
2652
4234
  }
2653
 
  /* Here when simple cond
2654
 
     There are limits on what kinds of const items we can evaluate, grep for
2655
 
     DontEvaluateMaterializedSubqueryTooEarly.
2656
 
  */
2657
 
  if (cond->const_item()  && !cond->is_expensive())
 
4235
  /* Here when simple cond */
 
4236
  if (cond->const_item())
2658
4237
  {
2659
4238
    /*
2660
 
      During the cond->val_int() evaluation we can come across a subselect
2661
 
      item which may allocate memory on the session->mem_root and assumes
2662
 
      all the memory allocated has the same life span as the subselect
 
4239
      During the cond->val_int() evaluation we can come across a subselect 
 
4240
      item which may allocate memory on the thd->mem_root and assumes 
 
4241
      all the memory allocated has the same life span as the subselect 
2663
4242
      item itself. So we have to restore the thread's mem_root here.
2664
4243
    */
2665
 
    memory::Root *tmp_root= param->mem_root;
2666
 
    param->session->mem_root= param->old_root;
2667
 
    tree= cond->val_int() ? new(tmp_root) optimizer::SEL_TREE(optimizer::SEL_TREE::ALWAYS) :
2668
 
                            new(tmp_root) optimizer::SEL_TREE(optimizer::SEL_TREE::IMPOSSIBLE);
2669
 
    param->session->mem_root= tmp_root;
2670
 
    return(tree);
 
4244
    MEM_ROOT *tmp_root= param->mem_root;
 
4245
    param->thd->mem_root= param->old_root;
 
4246
    tree= cond->val_int() ? new(tmp_root) SEL_TREE(SEL_TREE::ALWAYS) :
 
4247
                            new(tmp_root) SEL_TREE(SEL_TREE::IMPOSSIBLE);
 
4248
    param->thd->mem_root= tmp_root;
 
4249
    DBUG_RETURN(tree);
2671
4250
  }
2672
4251
 
2673
4252
  table_map ref_tables= 0;
2678
4257
    ref_tables= cond->used_tables();
2679
4258
    if ((ref_tables & param->current_table) ||
2680
4259
        (ref_tables & ~(param->prev_tables | param->read_tables)))
2681
 
      return 0;
2682
 
    return(new optimizer::SEL_TREE(optimizer::SEL_TREE::MAYBE));
 
4260
      DBUG_RETURN(0);
 
4261
    DBUG_RETURN(new SEL_TREE(SEL_TREE::MAYBE));
2683
4262
  }
2684
4263
 
2685
4264
  Item_func *cond_func= (Item_func*) cond;
2687
4266
      cond_func->functype() == Item_func::IN_FUNC)
2688
4267
    inv= ((Item_func_opt_neg *) cond_func)->negated;
2689
4268
  else if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
2690
 
    return 0;
 
4269
    DBUG_RETURN(0);                            
2691
4270
 
2692
4271
  param->cond= cond;
2693
4272
 
2703
4282
      Concerning the code below see the NOTES section in
2704
4283
      the comments for the function get_full_func_mm_tree()
2705
4284
    */
2706
 
    for (uint32_t i= 1 ; i < cond_func->arg_count ; i++)
 
4285
    for (uint i= 1 ; i < cond_func->arg_count ; i++)
2707
4286
    {
2708
4287
      if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
2709
4288
      {
2710
4289
        field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
2711
 
        optimizer::SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func,
2712
 
                                    field_item, (Item*)(intptr_t)i, inv);
 
4290
        SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func, 
 
4291
                                    field_item, (Item*)(intptr)i, inv);
2713
4292
        if (inv)
2714
4293
          tree= !tree ? tmp : tree_or(param, tree, tmp);
2715
 
        else
 
4294
        else 
2716
4295
          tree= tree_and(param, tree, tmp);
2717
4296
      }
2718
4297
      else if (inv)
2719
 
      {
 
4298
      { 
2720
4299
        tree= 0;
2721
4300
        break;
2722
4301
      }
2728
4307
  {
2729
4308
    Item_func_in *func=(Item_func_in*) cond_func;
2730
4309
    if (func->key_item()->real_item()->type() != Item::FIELD_ITEM)
2731
 
      return 0;
 
4310
      DBUG_RETURN(0);
2732
4311
    field_item= (Item_field*) (func->key_item()->real_item());
2733
4312
    ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
2734
4313
    break;
2735
4314
  }
2736
4315
  case Item_func::MULT_EQUAL_FUNC:
2737
4316
  {
2738
 
    Item_equal *item_equal= (Item_equal *) cond;
 
4317
    Item_equal *item_equal= (Item_equal *) cond;    
2739
4318
    if (!(value= item_equal->get_const()))
2740
 
      return 0;
 
4319
      DBUG_RETURN(0);
2741
4320
    Item_equal_iterator it(*item_equal);
2742
4321
    ref_tables= value->used_tables();
2743
4322
    while ((field_item= it++))
2744
4323
    {
2745
4324
      Field *field= field_item->field;
2746
 
      field->setWriteSet();
2747
 
 
2748
4325
      Item_result cmp_type= field->cmp_type();
2749
 
      if (!((ref_tables | field->getTable()->map) & param_comp))
 
4326
      if (!((ref_tables | field->table->map) & param_comp))
2750
4327
      {
2751
4328
        tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
2752
4329
                           value,cmp_type);
2753
4330
        ftree= !ftree ? tree : tree_and(param, ftree, tree);
2754
4331
      }
2755
4332
    }
2756
 
 
2757
 
    return(ftree);
 
4333
    
 
4334
    DBUG_RETURN(ftree);
2758
4335
  }
2759
4336
  default:
2760
4337
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
2770
4347
      value= cond_func->arguments()[0];
2771
4348
    }
2772
4349
    else
2773
 
      return 0;
 
4350
      DBUG_RETURN(0);
2774
4351
    ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
2775
4352
  }
2776
4353
 
2777
 
  return(ftree);
 
4354
  DBUG_RETURN(ftree);
2778
4355
}
2779
4356
 
2780
4357
 
2781
 
static optimizer::SEL_TREE *
2782
 
get_mm_parts(optimizer::RangeParameter *param,
2783
 
             COND *cond_func,
2784
 
             Field *field,
2785
 
                   Item_func::Functype type,
2786
 
                   Item *value, Item_result)
 
4358
static SEL_TREE *
 
4359
get_mm_parts(RANGE_OPT_PARAM *param, COND *cond_func, Field *field,
 
4360
             Item_func::Functype type,
 
4361
             Item *value, Item_result cmp_type)
2787
4362
{
2788
 
  if (field->getTable() != param->table)
2789
 
    return 0;
 
4363
  DBUG_ENTER("get_mm_parts");
 
4364
  if (field->table != param->table)
 
4365
    DBUG_RETURN(0);
2790
4366
 
2791
4367
  KEY_PART *key_part = param->key_parts;
2792
4368
  KEY_PART *end = param->key_parts_end;
2793
 
  optimizer::SEL_TREE *tree=0;
 
4369
  SEL_TREE *tree=0;
2794
4370
  if (value &&
2795
4371
      value->used_tables() & ~(param->prev_tables | param->read_tables))
2796
 
    return 0;
2797
 
  for (; key_part != end; key_part++)
 
4372
    DBUG_RETURN(0);
 
4373
  for (; key_part != end ; key_part++)
2798
4374
  {
2799
4375
    if (field->eq(key_part->field))
2800
4376
    {
2801
 
      optimizer::SEL_ARG *sel_arg=0;
2802
 
      if (!tree && !(tree=new optimizer::SEL_TREE()))
2803
 
        return 0;                               // OOM
 
4377
      SEL_ARG *sel_arg=0;
 
4378
      if (!tree && !(tree=new SEL_TREE()))
 
4379
        DBUG_RETURN(0);                         // OOM
2804
4380
      if (!value || !(value->used_tables() & ~param->read_tables))
2805
4381
      {
2806
 
        sel_arg= get_mm_leaf(param,cond_func,
2807
 
            key_part->field,key_part,type,value);
2808
 
        if (! sel_arg)
2809
 
          continue;
2810
 
        if (sel_arg->type == optimizer::SEL_ARG::IMPOSSIBLE)
2811
 
        {
2812
 
          tree->type=optimizer::SEL_TREE::IMPOSSIBLE;
2813
 
          return(tree);
2814
 
        }
 
4382
        sel_arg=get_mm_leaf(param,cond_func,
 
4383
                            key_part->field,key_part,type,value);
 
4384
        if (!sel_arg)
 
4385
          continue;
 
4386
        if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
 
4387
        {
 
4388
          tree->type=SEL_TREE::IMPOSSIBLE;
 
4389
          DBUG_RETURN(tree);
 
4390
        }
2815
4391
      }
2816
4392
      else
2817
4393
      {
2818
 
        // This key may be used later
2819
 
        if (! (sel_arg= new optimizer::SEL_ARG(optimizer::SEL_ARG::MAYBE_KEY)))
2820
 
          return 0;                     // OOM
 
4394
        // This key may be used later
 
4395
        if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
 
4396
          DBUG_RETURN(0);                       // OOM
2821
4397
      }
2822
 
      sel_arg->part=(unsigned char) key_part->part;
 
4398
      sel_arg->part=(uchar) key_part->part;
2823
4399
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
2824
 
      tree->keys_map.set(key_part->key);
 
4400
      tree->keys_map.set_bit(key_part->key);
2825
4401
    }
2826
4402
  }
2827
 
 
2828
 
  return tree;
 
4403
  
 
4404
  DBUG_RETURN(tree);
2829
4405
}
2830
4406
 
2831
4407
 
2832
 
static optimizer::SEL_ARG *
2833
 
get_mm_leaf(optimizer::RangeParameter *param,
2834
 
            COND *conf_func,
2835
 
            Field *field,
2836
 
            KEY_PART *key_part,
2837
 
            Item_func::Functype type,
2838
 
            Item *value)
 
4408
static SEL_ARG *
 
4409
get_mm_leaf(RANGE_OPT_PARAM *param, COND *conf_func, Field *field,
 
4410
            KEY_PART *key_part, Item_func::Functype type,Item *value)
2839
4411
{
2840
 
  uint32_t maybe_null=(uint32_t) field->real_maybe_null();
 
4412
  uint maybe_null=(uint) field->real_maybe_null();
2841
4413
  bool optimize_range;
2842
 
  optimizer::SEL_ARG *tree= NULL;
2843
 
  memory::Root *alloc= param->mem_root;
2844
 
  unsigned char *str;
2845
 
  int err= 0;
 
4414
  SEL_ARG *tree= 0;
 
4415
  MEM_ROOT *alloc= param->mem_root;
 
4416
  uchar *str;
 
4417
  ulong orig_sql_mode;
 
4418
  int err;
 
4419
  DBUG_ENTER("get_mm_leaf");
2846
4420
 
2847
4421
  /*
2848
4422
    We need to restore the runtime mem_root of the thread in this
2850
4424
    the argument can be any, e.g. a subselect. The subselect
2851
4425
    items, in turn, assume that all the memory allocated during
2852
4426
    the evaluation has the same life span as the item itself.
2853
 
    TODO: opitimizer/range.cc should not reset session->mem_root at all.
 
4427
    TODO: opt_range.cc should not reset thd->mem_root at all.
2854
4428
  */
2855
 
  param->session->mem_root= param->old_root;
 
4429
  param->thd->mem_root= param->old_root;
2856
4430
  if (!value)                                   // IS NULL or IS NOT NULL
2857
4431
  {
2858
 
    if (field->getTable()->maybe_null)          // Can't use a key on this
 
4432
    if (field->table->maybe_null)               // Can't use a key on this
2859
4433
      goto end;
2860
4434
    if (!maybe_null)                            // Not null field
2861
4435
    {
2862
4436
      if (type == Item_func::ISNULL_FUNC)
2863
 
        tree= &optimizer::null_element;
 
4437
        tree= &null_element;
2864
4438
      goto end;
2865
4439
    }
2866
 
    if (!(tree= new (alloc) optimizer::SEL_ARG(field,is_null_string,is_null_string)))
 
4440
    if (!(tree= new (alloc) SEL_ARG(field,is_null_string,is_null_string)))
2867
4441
      goto end;                                 // out of memory
2868
4442
    if (type == Item_func::ISNOTNULL_FUNC)
2869
4443
    {
2887
4461
  */
2888
4462
  if (field->result_type() == STRING_RESULT &&
2889
4463
      value->result_type() == STRING_RESULT &&
 
4464
      key_part->image_type == Field::itRAW &&
2890
4465
      ((Field_str*)field)->charset() != conf_func->compare_collation() &&
2891
4466
      !(conf_func->compare_collation()->state & MY_CS_BINSORT))
2892
4467
    goto end;
2901
4476
  {
2902
4477
    bool like_error;
2903
4478
    char buff1[MAX_FIELD_WIDTH];
2904
 
    unsigned char *min_str,*max_str;
 
4479
    uchar *min_str,*max_str;
2905
4480
    String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
2906
4481
    size_t length, offset, min_length, max_length;
2907
 
    uint32_t field_length= field->pack_length()+maybe_null;
 
4482
    uint field_length= field->pack_length()+maybe_null;
2908
4483
 
2909
4484
    if (!optimize_range)
2910
4485
      goto end;
2911
4486
    if (!(res= value->val_str(&tmp)))
2912
4487
    {
2913
 
      tree= &optimizer::null_element;
 
4488
      tree= &null_element;
2914
4489
      goto end;
2915
4490
    }
2916
4491
 
2940
4515
    {
2941
4516
      if (unlikely(length < field_length))
2942
4517
      {
2943
 
        /*
2944
 
          This can only happen in a table created with UNIREG where one key
2945
 
          overlaps many fields
2946
 
        */
2947
 
        length= field_length;
 
4518
        /*
 
4519
          This can only happen in a table created with UNIREG where one key
 
4520
          overlaps many fields
 
4521
        */
 
4522
        length= field_length;
2948
4523
      }
2949
4524
      else
2950
 
        field_length= length;
 
4525
        field_length= length;
2951
4526
    }
2952
4527
    length+=offset;
2953
 
    if (!(min_str= (unsigned char*) alloc->alloc_root(length*2)))
2954
 
    {
 
4528
    if (!(min_str= (uchar*) alloc_root(alloc, length*2)))
2955
4529
      goto end;
2956
 
    }
2957
4530
 
2958
4531
    max_str=min_str+length;
2959
4532
    if (maybe_null)
2960
4533
      max_str[0]= min_str[0]=0;
2961
4534
 
2962
4535
    field_length-= maybe_null;
2963
 
    int escape_code= make_escape_code(field->charset(),
2964
 
                                      ((Item_func_like*)(param->cond))->escape);
2965
4536
    like_error= my_like_range(field->charset(),
2966
 
                              res->ptr(), res->length(),
2967
 
                              escape_code,
2968
 
                              internal::wild_one, internal::wild_many,
2969
 
                              field_length,
2970
 
                              (char*) min_str+offset, (char*) max_str+offset,
2971
 
                              &min_length, &max_length);
 
4537
                              res->ptr(), res->length(),
 
4538
                              ((Item_func_like*)(param->cond))->escape,
 
4539
                              wild_one, wild_many,
 
4540
                              field_length,
 
4541
                              (char*) min_str+offset, (char*) max_str+offset,
 
4542
                              &min_length, &max_length);
2972
4543
    if (like_error)                             // Can't optimize with LIKE
2973
4544
      goto end;
2974
4545
 
2977
4548
      int2store(min_str+maybe_null,min_length);
2978
4549
      int2store(max_str+maybe_null,max_length);
2979
4550
    }
2980
 
    tree= new (alloc) optimizer::SEL_ARG(field, min_str, max_str);
 
4551
    tree= new (alloc) SEL_ARG(field, min_str, max_str);
2981
4552
    goto end;
2982
4553
  }
2983
4554
 
2984
 
  if (! optimize_range &&
 
4555
  if (!optimize_range &&
2985
4556
      type != Item_func::EQ_FUNC &&
2986
4557
      type != Item_func::EQUAL_FUNC)
2987
4558
    goto end;                                   // Can't optimize this
2994
4565
      value->result_type() != STRING_RESULT &&
2995
4566
      field->cmp_type() != value->result_type())
2996
4567
    goto end;
2997
 
 
2998
 
  /*
2999
 
   * Some notes from Jay...
3000
 
   *
3001
 
   * OK, so previously, and in MySQL, what the optimizer does here is
3002
 
   * override the sql_mode variable to ignore out-of-range or bad date-
3003
 
   * time values.  It does this because the optimizer is populating the
3004
 
   * field variable with the incoming value from the comparison field,
3005
 
   * and the value may exceed the bounds of a proper column type.
3006
 
   *
3007
 
   * For instance, assume the following:
3008
 
   *
3009
 
   * CREATE TABLE t1 (ts TIMESTAMP);
3010
 
   * INSERT INTO t1 ('2009-03-04 00:00:00');
3011
 
   * CREATE TABLE t2 (dt1 DATETIME, dt2 DATETIME);
3012
 
   * INSERT INT t2 ('2003-12-31 00:00:00','2999-12-31 00:00:00');
3013
 
   *
3014
 
   * If we issue this query:
3015
 
   *
3016
 
   * SELECT * FROM t1, t2 WHERE t1.ts BETWEEN t2.dt1 AND t2.dt2;
3017
 
   *
3018
 
   * We will come into bounds issues.  Field_timestamp::store() will be
3019
 
   * called with a datetime value of "2999-12-31 00:00:00" and will throw
3020
 
   * an error for out-of-bounds.  MySQL solves this via a hack with sql_mode
3021
 
   * but Drizzle always throws errors on bad data storage in a Field class.
3022
 
   *
3023
 
   * Therefore, to get around the problem of the Field class being used for
3024
 
   * "storage" here without actually storing anything...we must check to see
3025
 
   * if the value being stored in a Field_timestamp here is out of range.  If
3026
 
   * it is, then we must convert to the highest Timestamp value (or lowest,
3027
 
   * depending on whether the datetime is before or after the epoch.
3028
 
   */
3029
 
  if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
3030
 
  {
3031
 
    /*
3032
 
     * The left-side of the range comparison is a timestamp field.  Therefore,
3033
 
     * we must check to see if the value in the right-hand side is outside the
3034
 
     * range of the UNIX epoch, and cut to the epoch bounds if it is.
3035
 
     */
3036
 
    /* Datetime and date columns are Item::FIELD_ITEM ... and have a result type of STRING_RESULT */
3037
 
    if (value->real_item()->type() == Item::FIELD_ITEM
3038
 
        && value->result_type() == STRING_RESULT)
3039
 
    {
3040
 
      char buff[DateTime::MAX_STRING_LENGTH];
3041
 
      String tmp(buff, sizeof(buff), &my_charset_bin);
3042
 
      String *res= value->val_str(&tmp);
3043
 
 
3044
 
      if (!res)
3045
 
        goto end;
3046
 
      else
3047
 
      {
3048
 
        /*
3049
 
         * Create a datetime from the string and compare to fixed timestamp
3050
 
         * instances representing the epoch boundaries.
3051
 
         */
3052
 
        DateTime value_datetime;
3053
 
 
3054
 
        if (! value_datetime.from_string(res->c_ptr(), (size_t) res->length()))
3055
 
          goto end;
3056
 
 
3057
 
        Timestamp max_timestamp;
3058
 
        Timestamp min_timestamp;
3059
 
 
3060
 
        (void) max_timestamp.from_time_t((time_t) INT32_MAX);
3061
 
        (void) min_timestamp.from_time_t((time_t) 0);
3062
 
 
3063
 
        /* We rely on Temporal class operator overloads to do our comparisons. */
3064
 
        if (value_datetime < min_timestamp)
3065
 
        {
3066
 
          /*
3067
 
           * Datetime in right-hand side column is before UNIX epoch, so adjust to
3068
 
           * lower bound.
3069
 
           */
3070
 
          char new_value_buff[DateTime::MAX_STRING_LENGTH];
3071
 
          int new_value_length;
3072
 
          String new_value_string(new_value_buff, sizeof(new_value_buff), &my_charset_bin);
3073
 
 
3074
 
          new_value_length= min_timestamp.to_string(new_value_string.c_ptr(),
3075
 
                                    DateTime::MAX_STRING_LENGTH);
3076
 
          assert((new_value_length+1) < DateTime::MAX_STRING_LENGTH);
3077
 
          new_value_string.length(new_value_length);
3078
 
          err= value->save_str_value_in_field(field, &new_value_string);
3079
 
        }
3080
 
        else if (value_datetime > max_timestamp)
3081
 
        {
3082
 
          /*
3083
 
           * Datetime in right hand side column is after UNIX epoch, so adjust
3084
 
           * to the higher bound of the epoch.
3085
 
           */
3086
 
          char new_value_buff[DateTime::MAX_STRING_LENGTH];
3087
 
          int new_value_length;
3088
 
          String new_value_string(new_value_buff, sizeof(new_value_buff), &my_charset_bin);
3089
 
 
3090
 
          new_value_length= max_timestamp.to_string(new_value_string.c_ptr(),
3091
 
                                        DateTime::MAX_STRING_LENGTH);
3092
 
          assert((new_value_length+1) < DateTime::MAX_STRING_LENGTH);
3093
 
          new_value_string.length(new_value_length);
3094
 
          err= value->save_str_value_in_field(field, &new_value_string);
3095
 
        }
3096
 
        else
3097
 
          err= value->save_in_field(field, 1);
3098
 
      }
3099
 
    }
3100
 
    else /* Not a datetime -> timestamp comparison */
3101
 
      err= value->save_in_field(field, 1);
3102
 
  }
3103
 
  else /* Not a timestamp comparison */
3104
 
    err= value->save_in_field(field, 1);
3105
 
 
 
4568
  /* For comparison purposes allow invalid dates like 2000-01-32 */
 
4569
  orig_sql_mode= field->table->in_use->variables.sql_mode;
 
4570
  if (value->real_item()->type() == Item::STRING_ITEM &&
 
4571
      (field->type() == MYSQL_TYPE_NEWDATE ||
 
4572
       field->type() == MYSQL_TYPE_DATETIME))
 
4573
    field->table->in_use->variables.sql_mode|= MODE_INVALID_DATES;
 
4574
  err= value->save_in_field_no_warnings(field, 1);
3106
4575
  if (err > 0)
3107
4576
  {
3108
4577
    if (field->cmp_type() != value->result_type())
3111
4580
          value->result_type() == item_cmp_type(field->result_type(),
3112
4581
                                                value->result_type()))
3113
4582
      {
3114
 
        tree= new (alloc) optimizer::SEL_ARG(field, 0, 0);
3115
 
        tree->type= optimizer::SEL_ARG::IMPOSSIBLE;
 
4583
        tree= new (alloc) SEL_ARG(field, 0, 0);
 
4584
        tree->type= SEL_ARG::IMPOSSIBLE;
3116
4585
        goto end;
3117
4586
      }
3118
4587
      else
3122
4591
          for the cases like int_field > 999999999999999999999999 as well.
3123
4592
        */
3124
4593
        tree= 0;
3125
 
        if (err == 3 && field->type() == DRIZZLE_TYPE_DATE &&
 
4594
        if (err == 3 && field->type() == FIELD_TYPE_NEWDATE &&
3126
4595
            (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC ||
3127
4596
             type == Item_func::LT_FUNC || type == Item_func::LE_FUNC) )
3128
4597
        {
3131
4600
            but a non-zero time part was cut off.
3132
4601
 
3133
4602
            In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
3134
 
            values. Index over a DATE column uses DATE comparison. Changing
 
4603
            values. Index over a DATE column uses DATE comparison. Changing 
3135
4604
            from one comparison to the other is possible:
3136
4605
 
3137
4606
            datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
3168
4637
  }
3169
4638
  else if (err < 0)
3170
4639
  {
 
4640
    field->table->in_use->variables.sql_mode= orig_sql_mode;
3171
4641
    /* This happens when we try to insert a NULL field in a not null column */
3172
 
    tree= &optimizer::null_element;                        // cmp with NULL is never true
3173
 
    goto end;
3174
 
  }
3175
 
 
3176
 
  /*
3177
 
    Any predicate except "<=>"(null-safe equality operator) involving NULL as a
3178
 
    constant is always FALSE
3179
 
    Put IMPOSSIBLE Tree(null_element) here.
3180
 
  */  
3181
 
  if (type != Item_func::EQUAL_FUNC && field->is_real_null())
3182
 
  {
3183
 
    tree= &optimizer::null_element;
3184
 
    goto end;
3185
 
  }
3186
 
 
3187
 
  str= (unsigned char*) alloc->alloc_root(key_part->store_length+1);
 
4642
    tree= &null_element;                        // cmp with NULL is never true
 
4643
    goto end;
 
4644
  }
 
4645
  field->table->in_use->variables.sql_mode= orig_sql_mode;
 
4646
  str= (uchar*) alloc_root(alloc, key_part->store_length+1);
3188
4647
  if (!str)
3189
4648
    goto end;
3190
4649
  if (maybe_null)
3191
 
    *str= (unsigned char) field->is_real_null();        // Set to 1 if null
3192
 
  field->get_key_image(str+maybe_null, key_part->length);
3193
 
  if (! (tree= new (alloc) optimizer::SEL_ARG(field, str, str)))
3194
 
    goto end; // out of memory
 
4650
    *str= (uchar) field->is_real_null();        // Set to 1 if null
 
4651
  field->get_key_image(str+maybe_null, key_part->length,
 
4652
                       key_part->image_type);
 
4653
  if (!(tree= new (alloc) SEL_ARG(field, str, str)))
 
4654
    goto end;                                   // out of memory
3195
4655
 
3196
4656
  /*
3197
4657
    Check if we are comparing an UNSIGNED integer with a negative constant.
3208
4668
      value->result_type() == INT_RESULT &&
3209
4669
      ((Field_num*)field)->unsigned_flag && !((Item_int*)value)->unsigned_flag)
3210
4670
  {
3211
 
    int64_t item_val= value->val_int();
 
4671
    longlong item_val= value->val_int();
3212
4672
    if (item_val < 0)
3213
4673
    {
3214
4674
      if (type == Item_func::LT_FUNC || type == Item_func::LE_FUNC)
3215
4675
      {
3216
 
        tree->type= optimizer::SEL_ARG::IMPOSSIBLE;
 
4676
        tree->type= SEL_ARG::IMPOSSIBLE;
3217
4677
        goto end;
3218
4678
      }
3219
4679
      if (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC)
3252
4712
  }
3253
4713
 
3254
4714
end:
3255
 
  param->session->mem_root= alloc;
3256
 
  return(tree);
 
4715
  param->thd->mem_root= alloc;
 
4716
  DBUG_RETURN(tree);
3257
4717
}
3258
4718
 
3259
4719
 
3274
4734
  This will never be called for same key parts.
3275
4735
*/
3276
4736
 
3277
 
static optimizer::SEL_ARG *
3278
 
sel_add(optimizer::SEL_ARG *key1, optimizer::SEL_ARG *key2)
 
4737
static SEL_ARG *
 
4738
sel_add(SEL_ARG *key1,SEL_ARG *key2)
3279
4739
{
3280
 
  optimizer::SEL_ARG *root= NULL;
3281
 
  optimizer::SEL_ARG **key_link= NULL;
 
4740
  SEL_ARG *root,**key_link;
3282
4741
 
3283
4742
  if (!key1)
3284
4743
    return key2;
3310
4769
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)
3311
4770
 
3312
4771
 
3313
 
static optimizer::SEL_TREE *
3314
 
tree_and(optimizer::RangeParameter *param, optimizer::SEL_TREE *tree1, optimizer::SEL_TREE *tree2)
 
4772
static SEL_TREE *
 
4773
tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
3315
4774
{
 
4775
  DBUG_ENTER("tree_and");
3316
4776
  if (!tree1)
3317
 
    return(tree2);
 
4777
    DBUG_RETURN(tree2);
3318
4778
  if (!tree2)
3319
 
    return(tree1);
3320
 
  if (tree1->type == optimizer::SEL_TREE::IMPOSSIBLE || tree2->type == optimizer::SEL_TREE::ALWAYS)
3321
 
    return(tree1);
3322
 
  if (tree2->type == optimizer::SEL_TREE::IMPOSSIBLE || tree1->type == optimizer::SEL_TREE::ALWAYS)
3323
 
    return(tree2);
3324
 
  if (tree1->type == optimizer::SEL_TREE::MAYBE)
 
4779
    DBUG_RETURN(tree1);
 
4780
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
 
4781
    DBUG_RETURN(tree1);
 
4782
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
 
4783
    DBUG_RETURN(tree2);
 
4784
  if (tree1->type == SEL_TREE::MAYBE)
3325
4785
  {
3326
 
    if (tree2->type == optimizer::SEL_TREE::KEY)
3327
 
      tree2->type=optimizer::SEL_TREE::KEY_SMALLER;
3328
 
    return(tree2);
 
4786
    if (tree2->type == SEL_TREE::KEY)
 
4787
      tree2->type=SEL_TREE::KEY_SMALLER;
 
4788
    DBUG_RETURN(tree2);
3329
4789
  }
3330
 
  if (tree2->type == optimizer::SEL_TREE::MAYBE)
 
4790
  if (tree2->type == SEL_TREE::MAYBE)
3331
4791
  {
3332
 
    tree1->type=optimizer::SEL_TREE::KEY_SMALLER;
3333
 
    return(tree1);
 
4792
    tree1->type=SEL_TREE::KEY_SMALLER;
 
4793
    DBUG_RETURN(tree1);
3334
4794
  }
3335
4795
  key_map  result_keys;
3336
 
  result_keys.reset();
3337
 
 
 
4796
  result_keys.clear_all();
 
4797
  
3338
4798
  /* Join the trees key per key */
3339
 
  optimizer::SEL_ARG **key1,**key2,**end;
 
4799
  SEL_ARG **key1,**key2,**end;
3340
4800
  for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
3341
4801
       key1 != end ; key1++,key2++)
3342
4802
  {
3343
 
    uint32_t flag=0;
 
4803
    uint flag=0;
3344
4804
    if (*key1 || *key2)
3345
4805
    {
3346
4806
      if (*key1 && !(*key1)->simple_key())
3347
 
        flag|=CLONE_KEY1_MAYBE;
 
4807
        flag|=CLONE_KEY1_MAYBE;
3348
4808
      if (*key2 && !(*key2)->simple_key())
3349
 
        flag|=CLONE_KEY2_MAYBE;
 
4809
        flag|=CLONE_KEY2_MAYBE;
3350
4810
      *key1=key_and(param, *key1, *key2, flag);
3351
 
      if (*key1 && (*key1)->type == optimizer::SEL_ARG::IMPOSSIBLE)
 
4811
      if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
3352
4812
      {
3353
 
        tree1->type= optimizer::SEL_TREE::IMPOSSIBLE;
3354
 
        return(tree1);
 
4813
        tree1->type= SEL_TREE::IMPOSSIBLE;
 
4814
        DBUG_RETURN(tree1);
3355
4815
      }
3356
 
      result_keys.set(key1 - tree1->keys);
 
4816
      result_keys.set_bit(key1 - tree1->keys);
 
4817
#ifdef EXTRA_DEBUG
 
4818
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
 
4819
          (*key1)->test_use_count(*key1);
 
4820
#endif
3357
4821
    }
3358
4822
  }
3359
4823
  tree1->keys_map= result_keys;
3360
4824
  /* dispose index_merge if there is a "range" option */
3361
 
  if (result_keys.any())
 
4825
  if (!result_keys.is_clear_all())
3362
4826
  {
3363
4827
    tree1->merges.empty();
3364
 
    return(tree1);
 
4828
    DBUG_RETURN(tree1);
3365
4829
  }
3366
4830
 
3367
4831
  /* ok, both trees are index_merge trees */
3368
4832
  imerge_list_and_list(&tree1->merges, &tree2->merges);
3369
 
  return(tree1);
3370
 
}
3371
 
 
 
4833
  DBUG_RETURN(tree1);
 
4834
}
 
4835
 
 
4836
 
 
4837
/*
 
4838
  Check if two SEL_TREES can be combined into one (i.e. a single key range
 
4839
  read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
 
4840
  using index_merge.
 
4841
*/
 
4842
 
 
4843
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, 
 
4844
                           RANGE_OPT_PARAM* param)
 
4845
{
 
4846
  key_map common_keys= tree1->keys_map;
 
4847
  DBUG_ENTER("sel_trees_can_be_ored");
 
4848
  common_keys.intersect(tree2->keys_map);
 
4849
 
 
4850
  if (common_keys.is_clear_all())
 
4851
    DBUG_RETURN(false);
 
4852
 
 
4853
  /* trees have a common key, check if they refer to same key part */
 
4854
  SEL_ARG **key1,**key2;
 
4855
  for (uint key_no=0; key_no < param->keys; key_no++)
 
4856
  {
 
4857
    if (common_keys.is_set(key_no))
 
4858
    {
 
4859
      key1= tree1->keys + key_no;
 
4860
      key2= tree2->keys + key_no;
 
4861
      if ((*key1)->part == (*key2)->part)
 
4862
      {
 
4863
        DBUG_RETURN(true);
 
4864
      }
 
4865
    }
 
4866
  }
 
4867
  DBUG_RETURN(false);
 
4868
}
 
4869
 
 
4870
 
 
4871
/*
 
4872
  Remove the trees that are not suitable for record retrieval.
 
4873
  SYNOPSIS
 
4874
    param  Range analysis parameter
 
4875
    tree   Tree to be processed, tree->type is KEY or KEY_SMALLER
 
4876
 
 
4877
  DESCRIPTION
 
4878
    This function walks through tree->keys[] and removes the SEL_ARG* trees
 
4879
    that are not "maybe" trees (*) and cannot be used to construct quick range
 
4880
    selects.
 
4881
    (*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
 
4882
          these types here as well.
 
4883
 
 
4884
    A SEL_ARG* tree cannot be used to construct quick select if it has
 
4885
    tree->part != 0. (e.g. it could represent "keypart2 < const").
 
4886
 
 
4887
    WHY THIS FUNCTION IS NEEDED
 
4888
    
 
4889
    Normally we allow construction of SEL_TREE objects that have SEL_ARG
 
4890
    trees that do not allow quick range select construction. For example for
 
4891
    " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
 
4892
    tree1= SEL_TREE { SEL_ARG{keypart1=1} }
 
4893
    tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
 
4894
                                               from this
 
4895
    call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
 
4896
                                   tree.
 
4897
    
 
4898
    There is an exception though: when we construct index_merge SEL_TREE,
 
4899
    any SEL_ARG* tree that cannot be used to construct quick range select can
 
4900
    be removed, because current range analysis code doesn't provide any way
 
4901
    that tree could be later combined with another tree.
 
4902
    Consider an example: we should not construct
 
4903
    st1 = SEL_TREE { 
 
4904
      merges = SEL_IMERGE { 
 
4905
                            SEL_TREE(t.key1part1 = 1), 
 
4906
                            SEL_TREE(t.key2part2 = 2)   -- (*)
 
4907
                          } 
 
4908
                   };
 
4909
    because 
 
4910
     - (*) cannot be used to construct quick range select, 
 
4911
     - There is no execution path that would cause (*) to be converted to 
 
4912
       a tree that could be used.
 
4913
 
 
4914
    The latter is easy to verify: first, notice that the only way to convert
 
4915
    (*) into a usable tree is to call tree_and(something, (*)).
 
4916
 
 
4917
    Second look at what tree_and/tree_or function would do when passed a
 
4918
    SEL_TREE that has the structure like st1 tree has, and conlcude that 
 
4919
    tree_and(something, (*)) will not be called.
 
4920
 
 
4921
  RETURN
 
4922
    0  Ok, some suitable trees left
 
4923
    1  No tree->keys[] left.
 
4924
*/
 
4925
 
 
4926
static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
 
4927
{
 
4928
  bool res= false;
 
4929
  for (uint i=0; i < param->keys; i++)
 
4930
  {
 
4931
    if (tree->keys[i])
 
4932
    {
 
4933
      if (tree->keys[i]->part)
 
4934
      {
 
4935
        tree->keys[i]= NULL;
 
4936
        tree->keys_map.clear_bit(i);
 
4937
      }
 
4938
      else
 
4939
        res= true;
 
4940
    }
 
4941
  }
 
4942
  return !res;
 
4943
}
 
4944
 
 
4945
 
 
4946
static SEL_TREE *
 
4947
tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
 
4948
{
 
4949
  DBUG_ENTER("tree_or");
 
4950
  if (!tree1 || !tree2)
 
4951
    DBUG_RETURN(0);
 
4952
  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
 
4953
    DBUG_RETURN(tree2);
 
4954
  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
 
4955
    DBUG_RETURN(tree1);
 
4956
  if (tree1->type == SEL_TREE::MAYBE)
 
4957
    DBUG_RETURN(tree1);                         // Can't use this
 
4958
  if (tree2->type == SEL_TREE::MAYBE)
 
4959
    DBUG_RETURN(tree2);
 
4960
 
 
4961
  SEL_TREE *result= 0;
 
4962
  key_map  result_keys;
 
4963
  result_keys.clear_all();
 
4964
  if (sel_trees_can_be_ored(tree1, tree2, param))
 
4965
  {
 
4966
    /* Join the trees key per key */
 
4967
    SEL_ARG **key1,**key2,**end;
 
4968
    for (key1= tree1->keys,key2= tree2->keys,end= key1+param->keys ;
 
4969
         key1 != end ; key1++,key2++)
 
4970
    {
 
4971
      *key1=key_or(param, *key1, *key2);
 
4972
      if (*key1)
 
4973
      {
 
4974
        result=tree1;                           // Added to tree1
 
4975
        result_keys.set_bit(key1 - tree1->keys);
 
4976
#ifdef EXTRA_DEBUG
 
4977
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS) 
 
4978
          (*key1)->test_use_count(*key1);
 
4979
#endif
 
4980
      }
 
4981
    }
 
4982
    if (result)
 
4983
      result->keys_map= result_keys;
 
4984
  }
 
4985
  else
 
4986
  {
 
4987
    /* ok, two trees have KEY type but cannot be used without index merge */
 
4988
    if (tree1->merges.is_empty() && tree2->merges.is_empty())
 
4989
    {
 
4990
      if (param->remove_jump_scans)
 
4991
      {
 
4992
        bool no_trees= remove_nonrange_trees(param, tree1);
 
4993
        no_trees= no_trees || remove_nonrange_trees(param, tree2);
 
4994
        if (no_trees)
 
4995
          DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
 
4996
      }
 
4997
      SEL_IMERGE *merge;
 
4998
      /* both trees are "range" trees, produce new index merge structure */
 
4999
      if (!(result= new SEL_TREE()) || !(merge= new SEL_IMERGE()) ||
 
5000
          (result->merges.push_back(merge)) ||
 
5001
          (merge->or_sel_tree(param, tree1)) ||
 
5002
          (merge->or_sel_tree(param, tree2)))
 
5003
        result= NULL;
 
5004
      else
 
5005
        result->type= tree1->type;
 
5006
    }
 
5007
    else if (!tree1->merges.is_empty() && !tree2->merges.is_empty())
 
5008
    {
 
5009
      if (imerge_list_or_list(param, &tree1->merges, &tree2->merges))
 
5010
        result= new SEL_TREE(SEL_TREE::ALWAYS);
 
5011
      else
 
5012
        result= tree1;
 
5013
    }
 
5014
    else
 
5015
    {
 
5016
      /* one tree is index merge tree and another is range tree */
 
5017
      if (tree1->merges.is_empty())
 
5018
        swap_variables(SEL_TREE*, tree1, tree2);
 
5019
      
 
5020
      if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
 
5021
         DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
 
5022
      /* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
 
5023
      if (imerge_list_or_tree(param, &tree1->merges, tree2))
 
5024
        result= new SEL_TREE(SEL_TREE::ALWAYS);
 
5025
      else
 
5026
        result= tree1;
 
5027
    }
 
5028
  }
 
5029
  DBUG_RETURN(result);
 
5030
}
3372
5031
 
3373
5032
 
3374
5033
/* And key trees where key1->part < key2 -> part */
3375
5034
 
3376
 
static optimizer::SEL_ARG *
3377
 
and_all_keys(optimizer::RangeParameter *param,
3378
 
             optimizer::SEL_ARG *key1,
3379
 
             optimizer::SEL_ARG *key2,
3380
 
             uint32_t clone_flag)
 
5035
static SEL_ARG *
 
5036
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, 
 
5037
             uint clone_flag)
3381
5038
{
3382
 
  optimizer::SEL_ARG *next= NULL;
 
5039
  SEL_ARG *next;
3383
5040
  ulong use_count=key1->use_count;
3384
5041
 
3385
5042
  if (key1->elements != 1)
3387
5044
    key2->use_count+=key1->elements-1; //psergey: why we don't count that key1 has n-k-p?
3388
5045
    key2->increment_use_count((int) key1->elements-1);
3389
5046
  }
3390
 
  if (key1->type == optimizer::SEL_ARG::MAYBE_KEY)
 
5047
  if (key1->type == SEL_ARG::MAYBE_KEY)
3391
5048
  {
3392
 
    key1->right= key1->left= &optimizer::null_element;
 
5049
    key1->right= key1->left= &null_element;
3393
5050
    key1->next= key1->prev= 0;
3394
5051
  }
3395
 
  for (next= key1->first(); next ; next=next->next)
 
5052
  for (next=key1->first(); next ; next=next->next)
3396
5053
  {
3397
5054
    if (next->next_key_part)
3398
5055
    {
3399
 
      optimizer::SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
3400
 
      if (tmp && tmp->type == optimizer::SEL_ARG::IMPOSSIBLE)
 
5056
      SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
 
5057
      if (tmp && tmp->type == SEL_ARG::IMPOSSIBLE)
3401
5058
      {
3402
 
        key1=key1->tree_delete(next);
3403
 
        continue;
 
5059
        key1=key1->tree_delete(next);
 
5060
        continue;
3404
5061
      }
3405
5062
      next->next_key_part=tmp;
3406
5063
      if (use_count)
3407
 
        next->increment_use_count(use_count);
3408
 
      if (param->alloced_sel_args > optimizer::SEL_ARG::MAX_SEL_ARGS)
 
5064
        next->increment_use_count(use_count);
 
5065
      if (param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
3409
5066
        break;
3410
5067
    }
3411
5068
    else
3412
5069
      next->next_key_part=key2;
3413
5070
  }
3414
 
  if (! key1)
3415
 
    return &optimizer::null_element;                    // Impossible ranges
 
5071
  if (!key1)
 
5072
    return &null_element;                       // Impossible ranges
3416
5073
  key1->use_count++;
3417
5074
  return key1;
3418
5075
}
3433
5090
    NULL if the result of AND operation is an empty interval {0}.
3434
5091
*/
3435
5092
 
3436
 
static optimizer::SEL_ARG *
3437
 
key_and(optimizer::RangeParameter *param,
3438
 
        optimizer::SEL_ARG *key1,
3439
 
        optimizer::SEL_ARG *key2,
3440
 
        uint32_t clone_flag)
 
5093
static SEL_ARG *
 
5094
key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
3441
5095
{
3442
 
  if (! key1)
 
5096
  if (!key1)
3443
5097
    return key2;
3444
 
  if (! key2)
 
5098
  if (!key2)
3445
5099
    return key1;
3446
5100
  if (key1->part != key2->part)
3447
5101
  {
3448
5102
    if (key1->part > key2->part)
3449
5103
    {
3450
 
      std::swap(key1, key2);
 
5104
      swap_variables(SEL_ARG *, key1, key2);
3451
5105
      clone_flag=swap_clone_flag(clone_flag);
3452
5106
    }
3453
5107
    // key1->part < key2->part
3454
5108
    key1->use_count--;
3455
5109
    if (key1->use_count > 0)
3456
 
      if (! (key1= key1->clone_tree(param)))
3457
 
        return 0;                               // OOM
 
5110
      if (!(key1= key1->clone_tree(param)))
 
5111
        return 0;                               // OOM
3458
5112
    return and_all_keys(param, key1, key2, clone_flag);
3459
5113
  }
3460
5114
 
3461
5115
  if (((clone_flag & CLONE_KEY2_MAYBE) &&
3462
 
       ! (clone_flag & CLONE_KEY1_MAYBE) &&
3463
 
       key2->type != optimizer::SEL_ARG::MAYBE_KEY) ||
3464
 
      key1->type == optimizer::SEL_ARG::MAYBE_KEY)
 
5116
       !(clone_flag & CLONE_KEY1_MAYBE) &&
 
5117
       key2->type != SEL_ARG::MAYBE_KEY) ||
 
5118
      key1->type == SEL_ARG::MAYBE_KEY)
3465
5119
  {                                             // Put simple key in key2
3466
 
    std::swap(key1, key2);
3467
 
    clone_flag= swap_clone_flag(clone_flag);
 
5120
    swap_variables(SEL_ARG *, key1, key2);
 
5121
    clone_flag=swap_clone_flag(clone_flag);
3468
5122
  }
3469
5123
 
3470
5124
  /* If one of the key is MAYBE_KEY then the found region may be smaller */
3471
 
  if (key2->type == optimizer::SEL_ARG::MAYBE_KEY)
 
5125
  if (key2->type == SEL_ARG::MAYBE_KEY)
3472
5126
  {
3473
5127
    if (key1->use_count > 1)
3474
5128
    {
3475
5129
      key1->use_count--;
3476
 
      if (! (key1=key1->clone_tree(param)))
3477
 
        return 0;                               // OOM
 
5130
      if (!(key1=key1->clone_tree(param)))
 
5131
        return 0;                               // OOM
3478
5132
      key1->use_count++;
3479
5133
    }
3480
 
    if (key1->type == optimizer::SEL_ARG::MAYBE_KEY)
 
5134
    if (key1->type == SEL_ARG::MAYBE_KEY)
3481
5135
    {                                           // Both are maybe key
3482
 
      key1->next_key_part= key_and(param,
3483
 
                                   key1->next_key_part,
3484
 
                                   key2->next_key_part,
3485
 
                                   clone_flag);
 
5136
      key1->next_key_part=key_and(param, key1->next_key_part, 
 
5137
                                  key2->next_key_part, clone_flag);
3486
5138
      if (key1->next_key_part &&
3487
 
          key1->next_key_part->type == optimizer::SEL_ARG::IMPOSSIBLE)
3488
 
        return key1;
 
5139
          key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
 
5140
        return key1;
3489
5141
    }
3490
5142
    else
3491
5143
    {
3492
5144
      key1->maybe_smaller();
3493
5145
      if (key2->next_key_part)
3494
5146
      {
3495
 
        key1->use_count--;                      // Incremented in and_all_keys
3496
 
        return and_all_keys(param, key1, key2, clone_flag);
 
5147
        key1->use_count--;                      // Incremented in and_all_keys
 
5148
        return and_all_keys(param, key1, key2, clone_flag);
3497
5149
      }
3498
5150
      key2->use_count--;                        // Key2 doesn't have a tree
3499
5151
    }
3502
5154
 
3503
5155
  key1->use_count--;
3504
5156
  key2->use_count--;
3505
 
  optimizer::SEL_ARG *e1= key1->first();
3506
 
  optimizer::SEL_ARG *e2= key2->first();
3507
 
  optimizer::SEL_ARG *new_tree= NULL;
 
5157
  SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;
3508
5158
 
3509
5159
  while (e1 && e2)
3510
5160
  {
3511
 
    int cmp= e1->cmp_min_to_min(e2);
 
5161
    int cmp=e1->cmp_min_to_min(e2);
3512
5162
    if (cmp < 0)
3513
5163
    {
3514
 
      if (get_range(&e1, &e2, key1))
3515
 
        continue;
 
5164
      if (get_range(&e1,&e2,key1))
 
5165
        continue;
3516
5166
    }
3517
 
    else if (get_range(&e2, &e1, key2))
 
5167
    else if (get_range(&e2,&e1,key2))
3518
5168
      continue;
3519
 
    optimizer::SEL_ARG *next= key_and(param,
3520
 
                                      e1->next_key_part,
3521
 
                                      e2->next_key_part,
3522
 
                                      clone_flag);
 
5169
    SEL_ARG *next=key_and(param, e1->next_key_part, e2->next_key_part,
 
5170
                          clone_flag);
3523
5171
    e1->increment_use_count(1);
3524
5172
    e2->increment_use_count(1);
3525
 
    if (! next || next->type != optimizer::SEL_ARG::IMPOSSIBLE)
 
5173
    if (!next || next->type != SEL_ARG::IMPOSSIBLE)
3526
5174
    {
3527
 
      optimizer::SEL_ARG *new_arg= e1->clone_and(e2);
3528
 
      if (! new_arg)
3529
 
        return &optimizer::null_element;                        // End of memory
 
5175
      SEL_ARG *new_arg= e1->clone_and(e2);
 
5176
      if (!new_arg)
 
5177
        return &null_element;                   // End of memory
3530
5178
      new_arg->next_key_part=next;
3531
 
      if (! new_tree)
 
5179
      if (!new_tree)
3532
5180
      {
3533
 
        new_tree=new_arg;
 
5181
        new_tree=new_arg;
3534
5182
      }
3535
5183
      else
3536
 
        new_tree=new_tree->insert(new_arg);
 
5184
        new_tree=new_tree->insert(new_arg);
3537
5185
    }
3538
5186
    if (e1->cmp_max_to_max(e2) < 0)
3539
5187
      e1=e1->next;                              // e1 can't overlapp next e2
3542
5190
  }
3543
5191
  key1->free_tree();
3544
5192
  key2->free_tree();
3545
 
  if (! new_tree)
3546
 
    return &optimizer::null_element;                    // Impossible range
 
5193
  if (!new_tree)
 
5194
    return &null_element;                       // Impossible range
3547
5195
  return new_tree;
3548
5196
}
3549
5197
 
3550
5198
 
3551
5199
static bool
3552
 
get_range(optimizer::SEL_ARG **e1, optimizer::SEL_ARG **e2, optimizer::SEL_ARG *root1)
 
5200
get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1)
3553
5201
{
3554
 
  (*e1)= root1->find_range(*e2);                        // first e1->min < e2->min
 
5202
  (*e1)=root1->find_range(*e2);                 // first e1->min < e2->min
3555
5203
  if ((*e1)->cmp_max_to_min(*e2) < 0)
3556
5204
  {
3557
 
    if (! ((*e1)=(*e1)->next))
 
5205
    if (!((*e1)=(*e1)->next))
3558
5206
      return 1;
3559
5207
    if ((*e1)->cmp_min_to_max(*e2) > 0)
3560
5208
    {
3566
5214
}
3567
5215
 
3568
5216
 
 
5217
static SEL_ARG *
 
5218
key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
 
5219
{
 
5220
  if (!key1)
 
5221
  {
 
5222
    if (key2)
 
5223
    {
 
5224
      key2->use_count--;
 
5225
      key2->free_tree();
 
5226
    }
 
5227
    return 0;
 
5228
  }
 
5229
  if (!key2)
 
5230
  {
 
5231
    key1->use_count--;
 
5232
    key1->free_tree();
 
5233
    return 0;
 
5234
  }
 
5235
  key1->use_count--;
 
5236
  key2->use_count--;
 
5237
 
 
5238
  if (key1->part != key2->part)
 
5239
  {
 
5240
    key1->free_tree();
 
5241
    key2->free_tree();
 
5242
    return 0;                                   // Can't optimize this
 
5243
  }
 
5244
 
 
5245
  // If one of the key is MAYBE_KEY then the found region may be bigger
 
5246
  if (key1->type == SEL_ARG::MAYBE_KEY)
 
5247
  {
 
5248
    key2->free_tree();
 
5249
    key1->use_count++;
 
5250
    return key1;
 
5251
  }
 
5252
  if (key2->type == SEL_ARG::MAYBE_KEY)
 
5253
  {
 
5254
    key1->free_tree();
 
5255
    key2->use_count++;
 
5256
    return key2;
 
5257
  }
 
5258
 
 
5259
  if (key1->use_count > 0)
 
5260
  {
 
5261
    if (key2->use_count == 0 || key1->elements > key2->elements)
 
5262
    {
 
5263
      swap_variables(SEL_ARG *,key1,key2);
 
5264
    }
 
5265
    if (key1->use_count > 0 || !(key1=key1->clone_tree(param)))
 
5266
      return 0;                                 // OOM
 
5267
  }
 
5268
 
 
5269
  // Add tree at key2 to tree at key1
 
5270
  bool key2_shared=key2->use_count != 0;
 
5271
  key1->maybe_flag|=key2->maybe_flag;
 
5272
 
 
5273
  for (key2=key2->first(); key2; )
 
5274
  {
 
5275
    SEL_ARG *tmp=key1->find_range(key2);        // Find key1.min <= key2.min
 
5276
    int cmp;
 
5277
 
 
5278
    if (!tmp)
 
5279
    {
 
5280
      tmp=key1->first();                        // tmp.min > key2.min
 
5281
      cmp= -1;
 
5282
    }
 
5283
    else if ((cmp=tmp->cmp_max_to_min(key2)) < 0)
 
5284
    {                                           // Found tmp.max < key2.min
 
5285
      SEL_ARG *next=tmp->next;
 
5286
      if (cmp == -2 && eq_tree(tmp->next_key_part,key2->next_key_part))
 
5287
      {
 
5288
        // Join near ranges like tmp.max < 0 and key2.min >= 0
 
5289
        SEL_ARG *key2_next=key2->next;
 
5290
        if (key2_shared)
 
5291
        {
 
5292
          if (!(key2=new SEL_ARG(*key2)))
 
5293
            return 0;           // out of memory
 
5294
          key2->increment_use_count(key1->use_count+1);
 
5295
          key2->next=key2_next;                 // New copy of key2
 
5296
        }
 
5297
        key2->copy_min(tmp);
 
5298
        if (!(key1=key1->tree_delete(tmp)))
 
5299
        {                                       // Only one key in tree
 
5300
          key1=key2;
 
5301
          key1->make_root();
 
5302
          key2=key2_next;
 
5303
          break;
 
5304
        }
 
5305
      }
 
5306
      if (!(tmp=next))                          // tmp.min > key2.min
 
5307
        break;                                  // Copy rest of key2
 
5308
    }
 
5309
    if (cmp < 0)
 
5310
    {                                           // tmp.min > key2.min
 
5311
      int tmp_cmp;
 
5312
      if ((tmp_cmp=tmp->cmp_min_to_max(key2)) > 0) // if tmp.min > key2.max
 
5313
      {
 
5314
        if (tmp_cmp == 2 && eq_tree(tmp->next_key_part,key2->next_key_part))
 
5315
        {                                       // ranges are connected
 
5316
          tmp->copy_min_to_min(key2);
 
5317
          key1->merge_flags(key2);
 
5318
          if (tmp->min_flag & NO_MIN_RANGE &&
 
5319
              tmp->max_flag & NO_MAX_RANGE)
 
5320
          {
 
5321
            if (key1->maybe_flag)
 
5322
              return new SEL_ARG(SEL_ARG::MAYBE_KEY);
 
5323
            return 0;
 
5324
          }
 
5325
          key2->increment_use_count(-1);        // Free not used tree
 
5326
          key2=key2->next;
 
5327
          continue;
 
5328
        }
 
5329
        else
 
5330
        {
 
5331
          SEL_ARG *next=key2->next;             // Keys are not overlapping
 
5332
          if (key2_shared)
 
5333
          {
 
5334
            SEL_ARG *cpy= new SEL_ARG(*key2);   // Must make copy
 
5335
            if (!cpy)
 
5336
              return 0;                         // OOM
 
5337
            key1=key1->insert(cpy);
 
5338
            key2->increment_use_count(key1->use_count+1);
 
5339
          }
 
5340
          else
 
5341
            key1=key1->insert(key2);            // Will destroy key2_root
 
5342
          key2=next;
 
5343
          continue;
 
5344
        }
 
5345
      }
 
5346
    }
 
5347
 
 
5348
    // tmp.max >= key2.min && tmp.min <= key.max  (overlapping ranges)
 
5349
    if (eq_tree(tmp->next_key_part,key2->next_key_part))
 
5350
    {
 
5351
      if (tmp->is_same(key2))
 
5352
      {
 
5353
        tmp->merge_flags(key2);                 // Copy maybe flags
 
5354
        key2->increment_use_count(-1);          // Free not used tree
 
5355
      }
 
5356
      else
 
5357
      {
 
5358
        SEL_ARG *last=tmp;
 
5359
        while (last->next && last->next->cmp_min_to_max(key2) <= 0 &&
 
5360
               eq_tree(last->next->next_key_part,key2->next_key_part))
 
5361
        {
 
5362
          SEL_ARG *save=last;
 
5363
          last=last->next;
 
5364
          key1=key1->tree_delete(save);
 
5365
        }
 
5366
        last->copy_min(tmp);
 
5367
        if (last->copy_min(key2) || last->copy_max(key2))
 
5368
        {                                       // Full range
 
5369
          key1->free_tree();
 
5370
          for (; key2 ; key2=key2->next)
 
5371
            key2->increment_use_count(-1);      // Free not used tree
 
5372
          if (key1->maybe_flag)
 
5373
            return new SEL_ARG(SEL_ARG::MAYBE_KEY);
 
5374
          return 0;
 
5375
        }
 
5376
      }
 
5377
      key2=key2->next;
 
5378
      continue;
 
5379
    }
 
5380
 
 
5381
    if (cmp >= 0 && tmp->cmp_min_to_min(key2) < 0)
 
5382
    {                                           // tmp.min <= x < key2.min
 
5383
      SEL_ARG *new_arg=tmp->clone_first(key2);
 
5384
      if (!new_arg)
 
5385
        return 0;                               // OOM
 
5386
      if ((new_arg->next_key_part= key1->next_key_part))
 
5387
        new_arg->increment_use_count(key1->use_count+1);
 
5388
      tmp->copy_min_to_min(key2);
 
5389
      key1=key1->insert(new_arg);
 
5390
    }
 
5391
 
 
5392
    // tmp.min >= key2.min && tmp.min <= key2.max
 
5393
    SEL_ARG key(*key2);                         // Get copy we can modify
 
5394
    for (;;)
 
5395
    {
 
5396
      if (tmp->cmp_min_to_min(&key) > 0)
 
5397
      {                                         // key.min <= x < tmp.min
 
5398
        SEL_ARG *new_arg=key.clone_first(tmp);
 
5399
        if (!new_arg)
 
5400
          return 0;                             // OOM
 
5401
        if ((new_arg->next_key_part=key.next_key_part))
 
5402
          new_arg->increment_use_count(key1->use_count+1);
 
5403
        key1=key1->insert(new_arg);
 
5404
      }
 
5405
      if ((cmp=tmp->cmp_max_to_max(&key)) <= 0)
 
5406
      {                                         // tmp.min. <= x <= tmp.max
 
5407
        tmp->maybe_flag|= key.maybe_flag;
 
5408
        key.increment_use_count(key1->use_count+1);
 
5409
        tmp->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
 
5410
        if (!cmp)                               // Key2 is ready
 
5411
          break;
 
5412
        key.copy_max_to_min(tmp);
 
5413
        if (!(tmp=tmp->next))
 
5414
        {
 
5415
          SEL_ARG *tmp2= new SEL_ARG(key);
 
5416
          if (!tmp2)
 
5417
            return 0;                           // OOM
 
5418
          key1=key1->insert(tmp2);
 
5419
          key2=key2->next;
 
5420
          goto end;
 
5421
        }
 
5422
        if (tmp->cmp_min_to_max(&key) > 0)
 
5423
        {
 
5424
          SEL_ARG *tmp2= new SEL_ARG(key);
 
5425
          if (!tmp2)
 
5426
            return 0;                           // OOM
 
5427
          key1=key1->insert(tmp2);
 
5428
          break;
 
5429
        }
 
5430
      }
 
5431
      else
 
5432
      {
 
5433
        SEL_ARG *new_arg=tmp->clone_last(&key); // tmp.min <= x <= key.max
 
5434
        if (!new_arg)
 
5435
          return 0;                             // OOM
 
5436
        tmp->copy_max_to_min(&key);
 
5437
        tmp->increment_use_count(key1->use_count+1);
 
5438
        /* Increment key count as it may be used for next loop */
 
5439
        key.increment_use_count(1);
 
5440
        new_arg->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
 
5441
        key1=key1->insert(new_arg);
 
5442
        break;
 
5443
      }
 
5444
    }
 
5445
    key2=key2->next;
 
5446
  }
 
5447
 
 
5448
end:
 
5449
  while (key2)
 
5450
  {
 
5451
    SEL_ARG *next=key2->next;
 
5452
    if (key2_shared)
 
5453
    {
 
5454
      SEL_ARG *tmp=new SEL_ARG(*key2);          // Must make copy
 
5455
      if (!tmp)
 
5456
        return 0;
 
5457
      key2->increment_use_count(key1->use_count+1);
 
5458
      key1=key1->insert(tmp);
 
5459
    }
 
5460
    else
 
5461
      key1=key1->insert(key2);                  // Will destroy key2_root
 
5462
    key2=next;
 
5463
  }
 
5464
  key1->use_count++;
 
5465
  return key1;
 
5466
}
 
5467
 
 
5468
 
 
5469
/* Compare if two trees are equal */
 
5470
 
 
5471
static bool eq_tree(SEL_ARG* a,SEL_ARG *b)
 
5472
{
 
5473
  if (a == b)
 
5474
    return 1;
 
5475
  if (!a || !b || !a->is_same(b))
 
5476
    return 0;
 
5477
  if (a->left != &null_element && b->left != &null_element)
 
5478
  {
 
5479
    if (!eq_tree(a->left,b->left))
 
5480
      return 0;
 
5481
  }
 
5482
  else if (a->left != &null_element || b->left != &null_element)
 
5483
    return 0;
 
5484
  if (a->right != &null_element && b->right != &null_element)
 
5485
  {
 
5486
    if (!eq_tree(a->right,b->right))
 
5487
      return 0;
 
5488
  }
 
5489
  else if (a->right != &null_element || b->right != &null_element)
 
5490
    return 0;
 
5491
  if (a->next_key_part != b->next_key_part)
 
5492
  {                                             // Sub range
 
5493
    if (!a->next_key_part != !b->next_key_part ||
 
5494
        !eq_tree(a->next_key_part, b->next_key_part))
 
5495
      return 0;
 
5496
  }
 
5497
  return 1;
 
5498
}
 
5499
 
 
5500
 
 
5501
SEL_ARG *
 
5502
SEL_ARG::insert(SEL_ARG *key)
 
5503
{
 
5504
  SEL_ARG *element, **par= NULL, *last_element= NULL;
 
5505
 
 
5506
  for (element= this; element != &null_element ; )
 
5507
  {
 
5508
    last_element=element;
 
5509
    if (key->cmp_min_to_min(element) > 0)
 
5510
    {
 
5511
      par= &element->right; element= element->right;
 
5512
    }
 
5513
    else
 
5514
    {
 
5515
      par = &element->left; element= element->left;
 
5516
    }
 
5517
  }
 
5518
  *par=key;
 
5519
  key->parent=last_element;
 
5520
        /* Link in list */
 
5521
  if (par == &last_element->left)
 
5522
  {
 
5523
    key->next=last_element;
 
5524
    if ((key->prev=last_element->prev))
 
5525
      key->prev->next=key;
 
5526
    last_element->prev=key;
 
5527
  }
 
5528
  else
 
5529
  {
 
5530
    if ((key->next=last_element->next))
 
5531
      key->next->prev=key;
 
5532
    key->prev=last_element;
 
5533
    last_element->next=key;
 
5534
  }
 
5535
  key->left=key->right= &null_element;
 
5536
  SEL_ARG *root=rb_insert(key);                 // rebalance tree
 
5537
  root->use_count=this->use_count;              // copy root info
 
5538
  root->elements= this->elements+1;
 
5539
  root->maybe_flag=this->maybe_flag;
 
5540
  return root;
 
5541
}
 
5542
 
 
5543
 
 
5544
/*
 
5545
** Find best key with min <= given key
 
5546
** Because the call context this should never return 0 to get_range
 
5547
*/
 
5548
 
 
5549
SEL_ARG *
 
5550
SEL_ARG::find_range(SEL_ARG *key)
 
5551
{
 
5552
  SEL_ARG *element=this,*found=0;
 
5553
 
 
5554
  for (;;)
 
5555
  {
 
5556
    if (element == &null_element)
 
5557
      return found;
 
5558
    int cmp=element->cmp_min_to_min(key);
 
5559
    if (cmp == 0)
 
5560
      return element;
 
5561
    if (cmp < 0)
 
5562
    {
 
5563
      found=element;
 
5564
      element=element->right;
 
5565
    }
 
5566
    else
 
5567
      element=element->left;
 
5568
  }
 
5569
}
 
5570
 
 
5571
 
 
5572
/*
 
5573
  Remove a element from the tree
 
5574
 
 
5575
  SYNOPSIS
 
5576
    tree_delete()
 
5577
    key         Key that is to be deleted from tree (this)
 
5578
 
 
5579
  NOTE
 
5580
    This also frees all sub trees that is used by the element
 
5581
 
 
5582
  RETURN
 
5583
    root of new tree (with key deleted)
 
5584
*/
 
5585
 
 
5586
SEL_ARG *
 
5587
SEL_ARG::tree_delete(SEL_ARG *key)
 
5588
{
 
5589
  enum leaf_color remove_color;
 
5590
  SEL_ARG *root,*nod,**par,*fix_par;
 
5591
  DBUG_ENTER("tree_delete");
 
5592
 
 
5593
  root=this;
 
5594
  this->parent= 0;
 
5595
 
 
5596
  /* Unlink from list */
 
5597
  if (key->prev)
 
5598
    key->prev->next=key->next;
 
5599
  if (key->next)
 
5600
    key->next->prev=key->prev;
 
5601
  key->increment_use_count(-1);
 
5602
  if (!key->parent)
 
5603
    par= &root;
 
5604
  else
 
5605
    par=key->parent_ptr();
 
5606
 
 
5607
  if (key->left == &null_element)
 
5608
  {
 
5609
    *par=nod=key->right;
 
5610
    fix_par=key->parent;
 
5611
    if (nod != &null_element)
 
5612
      nod->parent=fix_par;
 
5613
    remove_color= key->color;
 
5614
  }
 
5615
  else if (key->right == &null_element)
 
5616
  {
 
5617
    *par= nod=key->left;
 
5618
    nod->parent=fix_par=key->parent;
 
5619
    remove_color= key->color;
 
5620
  }
 
5621
  else
 
5622
  {
 
5623
    SEL_ARG *tmp=key->next;                     // next bigger key (exist!)
 
5624
    nod= *tmp->parent_ptr()= tmp->right;        // unlink tmp from tree
 
5625
    fix_par=tmp->parent;
 
5626
    if (nod != &null_element)
 
5627
      nod->parent=fix_par;
 
5628
    remove_color= tmp->color;
 
5629
 
 
5630
    tmp->parent=key->parent;                    // Move node in place of key
 
5631
    (tmp->left=key->left)->parent=tmp;
 
5632
    if ((tmp->right=key->right) != &null_element)
 
5633
      tmp->right->parent=tmp;
 
5634
    tmp->color=key->color;
 
5635
    *par=tmp;
 
5636
    if (fix_par == key)                         // key->right == key->next
 
5637
      fix_par=tmp;                              // new parent of nod
 
5638
  }
 
5639
 
 
5640
  if (root == &null_element)
 
5641
    DBUG_RETURN(0);                             // Maybe root later
 
5642
  if (remove_color == BLACK)
 
5643
    root=rb_delete_fixup(root,nod,fix_par);
 
5644
  test_rb_tree(root,root->parent);
 
5645
 
 
5646
  root->use_count=this->use_count;              // Fix root counters
 
5647
  root->elements=this->elements-1;
 
5648
  root->maybe_flag=this->maybe_flag;
 
5649
  DBUG_RETURN(root);
 
5650
}
 
5651
 
 
5652
 
 
5653
        /* Functions to fix up the tree after insert and delete */
 
5654
 
 
5655
static void left_rotate(SEL_ARG **root,SEL_ARG *leaf)
 
5656
{
 
5657
  SEL_ARG *y=leaf->right;
 
5658
  leaf->right=y->left;
 
5659
  if (y->left != &null_element)
 
5660
    y->left->parent=leaf;
 
5661
  if (!(y->parent=leaf->parent))
 
5662
    *root=y;
 
5663
  else
 
5664
    *leaf->parent_ptr()=y;
 
5665
  y->left=leaf;
 
5666
  leaf->parent=y;
 
5667
}
 
5668
 
 
5669
static void right_rotate(SEL_ARG **root,SEL_ARG *leaf)
 
5670
{
 
5671
  SEL_ARG *y=leaf->left;
 
5672
  leaf->left=y->right;
 
5673
  if (y->right != &null_element)
 
5674
    y->right->parent=leaf;
 
5675
  if (!(y->parent=leaf->parent))
 
5676
    *root=y;
 
5677
  else
 
5678
    *leaf->parent_ptr()=y;
 
5679
  y->right=leaf;
 
5680
  leaf->parent=y;
 
5681
}
 
5682
 
 
5683
 
 
5684
SEL_ARG *
 
5685
SEL_ARG::rb_insert(SEL_ARG *leaf)
 
5686
{
 
5687
  SEL_ARG *y,*par,*par2,*root;
 
5688
  root= this; root->parent= 0;
 
5689
 
 
5690
  leaf->color=RED;
 
5691
  while (leaf != root && (par= leaf->parent)->color == RED)
 
5692
  {                                     // This can't be root or 1 level under
 
5693
    if (par == (par2= leaf->parent->parent)->left)
 
5694
    {
 
5695
      y= par2->right;
 
5696
      if (y->color == RED)
 
5697
      {
 
5698
        par->color=BLACK;
 
5699
        y->color=BLACK;
 
5700
        leaf=par2;
 
5701
        leaf->color=RED;                /* And the loop continues */
 
5702
      }
 
5703
      else
 
5704
      {
 
5705
        if (leaf == par->right)
 
5706
        {
 
5707
          left_rotate(&root,leaf->parent);
 
5708
          par=leaf;                     /* leaf is now parent to old leaf */
 
5709
        }
 
5710
        par->color=BLACK;
 
5711
        par2->color=RED;
 
5712
        right_rotate(&root,par2);
 
5713
        break;
 
5714
      }
 
5715
    }
 
5716
    else
 
5717
    {
 
5718
      y= par2->left;
 
5719
      if (y->color == RED)
 
5720
      {
 
5721
        par->color=BLACK;
 
5722
        y->color=BLACK;
 
5723
        leaf=par2;
 
5724
        leaf->color=RED;                /* And the loop continues */
 
5725
      }
 
5726
      else
 
5727
      {
 
5728
        if (leaf == par->left)
 
5729
        {
 
5730
          right_rotate(&root,par);
 
5731
          par=leaf;
 
5732
        }
 
5733
        par->color=BLACK;
 
5734
        par2->color=RED;
 
5735
        left_rotate(&root,par2);
 
5736
        break;
 
5737
      }
 
5738
    }
 
5739
  }
 
5740
  root->color=BLACK;
 
5741
  test_rb_tree(root,root->parent);
 
5742
  return root;
 
5743
}
 
5744
 
 
5745
 
 
5746
SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key,SEL_ARG *par)
 
5747
{
 
5748
  SEL_ARG *x,*w;
 
5749
  root->parent=0;
 
5750
 
 
5751
  x= key;
 
5752
  while (x != root && x->color == SEL_ARG::BLACK)
 
5753
  {
 
5754
    if (x == par->left)
 
5755
    {
 
5756
      w=par->right;
 
5757
      if (w->color == SEL_ARG::RED)
 
5758
      {
 
5759
        w->color=SEL_ARG::BLACK;
 
5760
        par->color=SEL_ARG::RED;
 
5761
        left_rotate(&root,par);
 
5762
        w=par->right;
 
5763
      }
 
5764
      if (w->left->color == SEL_ARG::BLACK && w->right->color == SEL_ARG::BLACK)
 
5765
      {
 
5766
        w->color=SEL_ARG::RED;
 
5767
        x=par;
 
5768
      }
 
5769
      else
 
5770
      {
 
5771
        if (w->right->color == SEL_ARG::BLACK)
 
5772
        {
 
5773
          w->left->color=SEL_ARG::BLACK;
 
5774
          w->color=SEL_ARG::RED;
 
5775
          right_rotate(&root,w);
 
5776
          w=par->right;
 
5777
        }
 
5778
        w->color=par->color;
 
5779
        par->color=SEL_ARG::BLACK;
 
5780
        w->right->color=SEL_ARG::BLACK;
 
5781
        left_rotate(&root,par);
 
5782
        x=root;
 
5783
        break;
 
5784
      }
 
5785
    }
 
5786
    else
 
5787
    {
 
5788
      w=par->left;
 
5789
      if (w->color == SEL_ARG::RED)
 
5790
      {
 
5791
        w->color=SEL_ARG::BLACK;
 
5792
        par->color=SEL_ARG::RED;
 
5793
        right_rotate(&root,par);
 
5794
        w=par->left;
 
5795
      }
 
5796
      if (w->right->color == SEL_ARG::BLACK && w->left->color == SEL_ARG::BLACK)
 
5797
      {
 
5798
        w->color=SEL_ARG::RED;
 
5799
        x=par;
 
5800
      }
 
5801
      else
 
5802
      {
 
5803
        if (w->left->color == SEL_ARG::BLACK)
 
5804
        {
 
5805
          w->right->color=SEL_ARG::BLACK;
 
5806
          w->color=SEL_ARG::RED;
 
5807
          left_rotate(&root,w);
 
5808
          w=par->left;
 
5809
        }
 
5810
        w->color=par->color;
 
5811
        par->color=SEL_ARG::BLACK;
 
5812
        w->left->color=SEL_ARG::BLACK;
 
5813
        right_rotate(&root,par);
 
5814
        x=root;
 
5815
        break;
 
5816
      }
 
5817
    }
 
5818
    par=x->parent;
 
5819
  }
 
5820
  x->color=SEL_ARG::BLACK;
 
5821
  return root;
 
5822
}
 
5823
 
 
5824
 
 
5825
        /* Test that the properties for a red-black tree hold */
 
5826
 
 
5827
#ifdef EXTRA_DEBUG
 
5828
int test_rb_tree(SEL_ARG *element,SEL_ARG *parent)
 
5829
{
 
5830
  int count_l,count_r;
 
5831
 
 
5832
  if (element == &null_element)
 
5833
    return 0;                                   // Found end of tree
 
5834
  if (element->parent != parent)
 
5835
  {
 
5836
    sql_print_error("Wrong tree: Parent doesn't point at parent");
 
5837
    return -1;
 
5838
  }
 
5839
  if (element->color == SEL_ARG::RED &&
 
5840
      (element->left->color == SEL_ARG::RED ||
 
5841
       element->right->color == SEL_ARG::RED))
 
5842
  {
 
5843
    sql_print_error("Wrong tree: Found two red in a row");
 
5844
    return -1;
 
5845
  }
 
5846
  if (element->left == element->right && element->left != &null_element)
 
5847
  {                                             // Dummy test
 
5848
    sql_print_error("Wrong tree: Found right == left");
 
5849
    return -1;
 
5850
  }
 
5851
  count_l=test_rb_tree(element->left,element);
 
5852
  count_r=test_rb_tree(element->right,element);
 
5853
  if (count_l >= 0 && count_r >= 0)
 
5854
  {
 
5855
    if (count_l == count_r)
 
5856
      return count_l+(element->color == SEL_ARG::BLACK);
 
5857
    sql_print_error("Wrong tree: Incorrect black-count: %d - %d",
 
5858
            count_l,count_r);
 
5859
  }
 
5860
  return -1;                                    // Error, no more warnings
 
5861
}
 
5862
 
 
5863
 
 
5864
/*
 
5865
  Count how many times SEL_ARG graph "root" refers to its part "key"
 
5866
  
 
5867
  SYNOPSIS
 
5868
    count_key_part_usage()
 
5869
      root  An RB-Root node in a SEL_ARG graph.
 
5870
      key   Another RB-Root node in that SEL_ARG graph.
 
5871
 
 
5872
  DESCRIPTION
 
5873
    The passed "root" node may refer to "key" node via root->next_key_part,
 
5874
    root->next->n
 
5875
 
 
5876
    This function counts how many times the node "key" is referred (via
 
5877
    SEL_ARG::next_key_part) by 
 
5878
     - intervals of RB-tree pointed by "root", 
 
5879
     - intervals of RB-trees that are pointed by SEL_ARG::next_key_part from 
 
5880
       intervals of RB-tree pointed by "root",
 
5881
     - and so on.
 
5882
    
 
5883
    Here is an example (horizontal links represent next_key_part pointers, 
 
5884
    vertical links - next/prev prev pointers):  
 
5885
    
 
5886
         +----+               $
 
5887
         |root|-----------------+
 
5888
         +----+               $ |
 
5889
           |                  $ |
 
5890
           |                  $ |
 
5891
         +----+       +---+   $ |     +---+    Here the return value
 
5892
         |    |- ... -|   |---$-+--+->|key|    will be 4.
 
5893
         +----+       +---+   $ |  |  +---+
 
5894
           |                  $ |  |
 
5895
          ...                 $ |  |
 
5896
           |                  $ |  |
 
5897
         +----+   +---+       $ |  |
 
5898
         |    |---|   |---------+  |
 
5899
         +----+   +---+       $    |
 
5900
           |        |         $    |
 
5901
          ...     +---+       $    |
 
5902
                  |   |------------+
 
5903
                  +---+       $
 
5904
  RETURN 
 
5905
    Number of links to "key" from nodes reachable from "root".
 
5906
*/
 
5907
 
 
5908
static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
 
5909
{
 
5910
  ulong count= 0;
 
5911
  for (root=root->first(); root ; root=root->next)
 
5912
  {
 
5913
    if (root->next_key_part)
 
5914
    {
 
5915
      if (root->next_key_part == key)
 
5916
        count++;
 
5917
      if (root->next_key_part->part < key->part)
 
5918
        count+=count_key_part_usage(root->next_key_part,key);
 
5919
    }
 
5920
  }
 
5921
  return count;
 
5922
}
 
5923
 
 
5924
 
 
5925
/*
 
5926
  Check if SEL_ARG::use_count value is correct
 
5927
 
 
5928
  SYNOPSIS
 
5929
    SEL_ARG::test_use_count()
 
5930
      root  The root node of the SEL_ARG graph (an RB-tree root node that
 
5931
            has the least value of sel_arg->part in the entire graph, and
 
5932
            thus is the "origin" of the graph)
 
5933
 
 
5934
  DESCRIPTION
 
5935
    Check if SEL_ARG::use_count value is correct. See the definition of
 
5936
    use_count for what is "correct".
 
5937
*/
 
5938
 
 
5939
void SEL_ARG::test_use_count(SEL_ARG *root)
 
5940
{
 
5941
  uint e_count=0;
 
5942
  if (this == root && use_count != 1)
 
5943
  {
 
5944
    sql_print_information("Use_count: Wrong count %lu for root",use_count);
 
5945
    return;
 
5946
  }
 
5947
  if (this->type != SEL_ARG::KEY_RANGE)
 
5948
    return;
 
5949
  for (SEL_ARG *pos=first(); pos ; pos=pos->next)
 
5950
  {
 
5951
    e_count++;
 
5952
    if (pos->next_key_part)
 
5953
    {
 
5954
      ulong count=count_key_part_usage(root,pos->next_key_part);
 
5955
      if (count > pos->next_key_part->use_count)
 
5956
      {
 
5957
        sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu "
 
5958
                              "should be %lu", (long unsigned int)pos,
 
5959
                              pos->next_key_part->use_count, count);
 
5960
        return;
 
5961
      }
 
5962
      pos->next_key_part->test_use_count(root);
 
5963
    }
 
5964
  }
 
5965
  if (e_count != elements)
 
5966
    sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
 
5967
                      e_count, elements, (long unsigned int) this);
 
5968
}
 
5969
 
 
5970
#endif
 
5971
 
3569
5972
/****************************************************************************
3570
5973
  MRR Range Sequence Interface implementation that walks a SEL_ARG* tree.
3571
5974
 ****************************************************************************/
3572
5975
 
3573
5976
/* MRR range sequence, SEL_ARG* implementation: stack entry */
3574
 
typedef struct st_range_seq_entry
 
5977
typedef struct st_range_seq_entry 
3575
5978
{
3576
 
  /*
 
5979
  /* 
3577
5980
    Pointers in min and max keys. They point to right-after-end of key
3578
5981
    images. The 0-th entry has these pointing to key tuple start.
3579
5982
  */
3580
 
  unsigned char *min_key, *max_key;
3581
 
 
3582
 
  /*
 
5983
  uchar *min_key, *max_key;
 
5984
  
 
5985
  /* 
3583
5986
    Flags, for {keypart0, keypart1, ... this_keypart} subtuple.
3584
5987
    min_key_flag may have NULL_RANGE set.
3585
5988
  */
3586
 
  uint32_t min_key_flag, max_key_flag;
3587
 
 
 
5989
  uint min_key_flag, max_key_flag;
 
5990
  
3588
5991
  /* Number of key parts */
3589
 
  uint32_t min_key_parts, max_key_parts;
3590
 
  optimizer::SEL_ARG *key_tree;
 
5992
  uint min_key_parts, max_key_parts;
 
5993
  SEL_ARG *key_tree;
3591
5994
} RANGE_SEQ_ENTRY;
3592
5995
 
3593
5996
 
3596
5999
*/
3597
6000
typedef struct st_sel_arg_range_seq
3598
6001
{
3599
 
  uint32_t keyno;      /* index of used tree in optimizer::SEL_TREE structure */
3600
 
  uint32_t real_keyno; /* Number of the index in tables */
3601
 
  optimizer::Parameter *param;
3602
 
  optimizer::SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
3603
 
 
 
6002
  uint keyno;      /* index of used tree in SEL_TREE structure */
 
6003
  uint real_keyno; /* Number of the index in tables */
 
6004
  PARAM *param;
 
6005
  SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
 
6006
  
3604
6007
  RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];
3605
6008
  int i; /* Index of last used element in the above array */
3606
 
 
 
6009
  
3607
6010
  bool at_start; /* true <=> The traversal has just started */
3608
6011
} SEL_ARG_RANGE_SEQ;
3609
6012
 
3614
6017
  SYNOPSIS
3615
6018
    init()
3616
6019
      init_params  SEL_ARG tree traversal context
3617
 
      n_ranges     [ignored] The number of ranges obtained
 
6020
      n_ranges     [ignored] The number of ranges obtained 
3618
6021
      flags        [ignored] HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
3619
6022
 
3620
6023
  RETURN
3621
6024
    Value of init_param
3622
6025
*/
3623
6026
 
3624
 
static range_seq_t sel_arg_range_seq_init(void *init_param, uint32_t, uint32_t)
 
6027
range_seq_t sel_arg_range_seq_init(void *init_param, uint n_ranges, uint flags)
3625
6028
{
3626
6029
  SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)init_param;
3627
6030
  seq->at_start= true;
3638
6041
}
3639
6042
 
3640
6043
 
3641
 
static void step_down_to(SEL_ARG_RANGE_SEQ *arg, optimizer::SEL_ARG *key_tree)
 
6044
static void step_down_to(SEL_ARG_RANGE_SEQ *arg, SEL_ARG *key_tree)
3642
6045
{
3643
6046
  RANGE_SEQ_ENTRY *cur= &arg->stack[arg->i+1];
3644
6047
  RANGE_SEQ_ENTRY *prev= &arg->stack[arg->i];
3645
 
 
 
6048
  
3646
6049
  cur->key_tree= key_tree;
3647
6050
  cur->min_key= prev->min_key;
3648
6051
  cur->max_key= prev->max_key;
3649
6052
  cur->min_key_parts= prev->min_key_parts;
3650
6053
  cur->max_key_parts= prev->max_key_parts;
3651
6054
 
3652
 
  uint16_t stor_length= arg->param->key[arg->keyno][key_tree->part].store_length;
 
6055
  uint16 stor_length= arg->param->key[arg->keyno][key_tree->part].store_length;
3653
6056
  cur->min_key_parts += key_tree->store_min(stor_length, &cur->min_key,
3654
6057
                                            prev->min_key_flag);
3655
6058
  cur->max_key_parts += key_tree->store_max(stor_length, &cur->max_key,
3666
6069
 
3667
6070
/*
3668
6071
  Range sequence interface, SEL_ARG* implementation: get the next interval
3669
 
 
 
6072
  
3670
6073
  SYNOPSIS
3671
6074
    sel_arg_range_seq_next()
3672
6075
      rseq        Value returned from sel_arg_range_seq_init
3688
6091
*/
3689
6092
 
3690
6093
//psergey-merge-todo: support check_quick_keys:max_keypart
3691
 
static uint32_t sel_arg_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
 
6094
uint sel_arg_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
3692
6095
{
3693
 
  optimizer::SEL_ARG *key_tree;
 
6096
  SEL_ARG *key_tree;
3694
6097
  SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)rseq;
3695
6098
  if (seq->at_start)
3696
6099
  {
3701
6104
 
3702
6105
  key_tree= seq->stack[seq->i].key_tree;
3703
6106
  /* Ok, we're at some "full tuple" position in the tree */
3704
 
 
 
6107
 
3705
6108
  /* Step down if we can */
3706
 
  if (key_tree->next && key_tree->next != &optimizer::null_element)
 
6109
  if (key_tree->next && key_tree->next != &null_element)
3707
6110
  {
3708
6111
    //step down; (update the tuple, we'll step right and stay there)
3709
6112
    seq->i--;
3723
6126
    key_tree= seq->stack[seq->i].key_tree;
3724
6127
 
3725
6128
    /* Step down if we can */
3726
 
    if (key_tree->next && key_tree->next != &optimizer::null_element)
 
6129
    if (key_tree->next && key_tree->next != &null_element)
3727
6130
    {
3728
6131
      // Step down; update the tuple
3729
6132
      seq->i--;
3738
6141
    Walk right-up while we can
3739
6142
  */
3740
6143
walk_right_n_up:
3741
 
  while (key_tree->next_key_part && key_tree->next_key_part != &optimizer::null_element &&
 
6144
  while (key_tree->next_key_part && key_tree->next_key_part != &null_element && 
3742
6145
         key_tree->next_key_part->part == key_tree->part + 1 &&
3743
 
         key_tree->next_key_part->type == optimizer::SEL_ARG::KEY_RANGE)
 
6146
         key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
3744
6147
  {
3745
6148
    {
3746
6149
      RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
3747
 
      uint32_t min_key_length= cur->min_key - seq->param->min_key;
3748
 
      uint32_t max_key_length= cur->max_key - seq->param->max_key;
3749
 
      uint32_t len= cur->min_key - cur[-1].min_key;
3750
 
      if (! (min_key_length == max_key_length &&
3751
 
          ! memcmp(cur[-1].min_key, cur[-1].max_key, len) &&
3752
 
          ! key_tree->min_flag && !key_tree->max_flag))
 
6150
      uint min_key_length= cur->min_key - seq->param->min_key;
 
6151
      uint max_key_length= cur->max_key - seq->param->max_key;
 
6152
      uint len= cur->min_key - cur[-1].min_key;
 
6153
      if (!(min_key_length == max_key_length &&
 
6154
            !memcmp(cur[-1].min_key, cur[-1].max_key, len) &&
 
6155
            !key_tree->min_flag && !key_tree->max_flag))
3753
6156
      {
3754
6157
        seq->param->is_ror_scan= false;
3755
 
        if (! key_tree->min_flag)
3756
 
          cur->min_key_parts +=
 
6158
        if (!key_tree->min_flag)
 
6159
          cur->min_key_parts += 
3757
6160
            key_tree->next_key_part->store_min_key(seq->param->key[seq->keyno],
3758
6161
                                                   &cur->min_key,
3759
6162
                                                   &cur->min_key_flag);
3760
 
        if (! key_tree->max_flag)
3761
 
          cur->max_key_parts +=
 
6163
        if (!key_tree->max_flag)
 
6164
          cur->max_key_parts += 
3762
6165
            key_tree->next_key_part->store_max_key(seq->param->key[seq->keyno],
3763
6166
                                                   &cur->max_key,
3764
6167
                                                   &cur->max_key_flag);
3765
6168
        break;
3766
6169
      }
3767
6170
    }
3768
 
 
 
6171
  
3769
6172
    /*
3770
6173
      Ok, current atomic interval is in form "t.field=const" and there is
3771
6174
      next_key_part interval. Step right, and walk up from there.
3773
6176
    key_tree= key_tree->next_key_part;
3774
6177
 
3775
6178
walk_up_n_right:
3776
 
    while (key_tree->prev && key_tree->prev != &optimizer::null_element)
 
6179
    while (key_tree->prev && key_tree->prev != &null_element)
3777
6180
    {
3778
6181
      /* Step up */
3779
6182
      key_tree= key_tree->prev;
3783
6186
 
3784
6187
  /* Ok got a tuple */
3785
6188
  RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
3786
 
 
 
6189
  
3787
6190
  range->ptr= (char*)(int)(key_tree->part);
3788
6191
  {
3789
6192
    range->range_flag= cur->min_key_flag | cur->max_key_flag;
3790
 
 
 
6193
    
3791
6194
    range->start_key.key=    seq->param->min_key;
3792
6195
    range->start_key.length= cur->min_key - seq->param->min_key;
3793
6196
    range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
3794
 
    range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
 
6197
    range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY : 
3795
6198
                                                           HA_READ_KEY_EXACT);
3796
6199
 
3797
6200
    range->end_key.key=    seq->param->max_key;
3798
6201
    range->end_key.length= cur->max_key - seq->param->max_key;
3799
 
    range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
 
6202
    range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY : 
3800
6203
                                                         HA_READ_AFTER_KEY);
3801
6204
    range->end_key.keypart_map= make_prev_keypart_map(cur->max_key_parts);
3802
6205
 
3803
6206
    if (!(cur->min_key_flag & ~NULL_RANGE) && !cur->max_key_flag &&
3804
 
        (uint32_t)key_tree->part+1 == seq->param->table->key_info[seq->real_keyno].key_parts &&
 
6207
        (uint)key_tree->part+1 == seq->param->table->key_info[seq->real_keyno].key_parts &&
3805
6208
        (seq->param->table->key_info[seq->real_keyno].flags & (HA_NOSAME)) ==
3806
6209
        HA_NOSAME &&
3807
6210
        range->start_key.length == range->end_key.length &&
3808
6211
        !memcmp(seq->param->min_key,seq->param->max_key,range->start_key.length))
3809
6212
      range->range_flag= UNIQUE_RANGE | (cur->min_key_flag & NULL_RANGE);
3810
 
 
 
6213
      
3811
6214
    if (seq->param->is_ror_scan)
3812
6215
    {
3813
6216
      /*
3827
6230
    }
3828
6231
  }
3829
6232
  seq->param->range_count++;
3830
 
  seq->param->max_key_part= max(seq->param->max_key_part,(uint32_t)key_tree->part);
 
6233
  seq->param->max_key_part=max(seq->param->max_key_part,key_tree->part);
3831
6234
  return 0;
3832
6235
}
3833
6236
 
3834
6237
 
3835
6238
/*
3836
 
  Calculate cost and E(#rows) for a given index and intervals tree
 
6239
  Calculate cost and E(#rows) for a given index and intervals tree 
3837
6240
 
3838
6241
  SYNOPSIS
3839
6242
    check_quick_select()
3840
6243
      param             Parameter from test_quick_select
3841
 
      idx               Number of index to use in Parameter::key optimizer::SEL_TREE::key
 
6244
      idx               Number of index to use in PARAM::key SEL_TREE::key
3842
6245
      index_only        true  - assume only index tuples will be accessed
3843
6246
                        false - assume full table rows will be read
3844
6247
      tree              Transformed selection condition, tree->key[idx] holds
3856
6259
 
3857
6260
  RETURN
3858
6261
    Estimate # of records to be retrieved.
3859
 
    HA_POS_ERROR if estimate calculation failed due to table Cursor problems.
 
6262
    HA_POS_ERROR if estimate calculation failed due to table handler problems.
3860
6263
*/
3861
6264
 
3862
6265
static
3863
 
ha_rows check_quick_select(Session *session,
3864
 
                           optimizer::Parameter *param,
3865
 
                           uint32_t idx,
3866
 
                           bool index_only,
3867
 
                           optimizer::SEL_ARG *tree,
3868
 
                           bool update_tbl_stats,
3869
 
                           uint32_t *mrr_flags,
3870
 
                           uint32_t *bufsize,
3871
 
                           optimizer::CostVector *cost)
 
6266
ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
 
6267
                           SEL_ARG *tree, bool update_tbl_stats, 
 
6268
                           uint *mrr_flags, uint *bufsize, COST_VECT *cost)
3872
6269
{
3873
6270
  SEL_ARG_RANGE_SEQ seq;
3874
6271
  RANGE_SEQ_IF seq_if = {sel_arg_range_seq_init, sel_arg_range_seq_next};
3875
 
  Cursor *cursor= param->table->cursor;
 
6272
  handler *file= param->table->file;
3876
6273
  ha_rows rows;
3877
 
  uint32_t keynr= param->real_keynr[idx];
3878
 
 
 
6274
  uint keynr= param->real_keynr[idx];
 
6275
  DBUG_ENTER("check_quick_select");
 
6276
  
3879
6277
  /* Handle cases when we don't have a valid non-empty list of range */
3880
 
  if (! tree)
3881
 
    return(HA_POS_ERROR);
3882
 
  if (tree->type == optimizer::SEL_ARG::IMPOSSIBLE)
3883
 
    return(0L);
3884
 
  if (tree->type != optimizer::SEL_ARG::KEY_RANGE || tree->part != 0)
3885
 
    return(HA_POS_ERROR);
 
6278
  if (!tree)
 
6279
    DBUG_RETURN(HA_POS_ERROR);
 
6280
  if (tree->type == SEL_ARG::IMPOSSIBLE)
 
6281
    DBUG_RETURN(0L);
 
6282
  if (tree->type != SEL_ARG::KEY_RANGE || tree->part != 0)
 
6283
    DBUG_RETURN(HA_POS_ERROR);
3886
6284
 
3887
6285
  seq.keyno= idx;
3888
6286
  seq.real_keyno= keynr;
3893
6291
  param->max_key_part=0;
3894
6292
 
3895
6293
  param->is_ror_scan= true;
3896
 
  if (param->table->index_flags(keynr) & HA_KEY_SCAN_NOT_ROR)
 
6294
  if (file->index_flags(keynr, 0, true) & HA_KEY_SCAN_NOT_ROR)
3897
6295
    param->is_ror_scan= false;
3898
 
 
 
6296
  
3899
6297
  *mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
3900
6298
  *mrr_flags|= HA_MRR_NO_ASSOCIATION;
3901
6299
 
3902
 
  bool pk_is_clustered= cursor->primary_key_is_clustered();
3903
 
  if (index_only &&
3904
 
      (param->table->index_flags(keynr) & HA_KEYREAD_ONLY) &&
3905
 
      !(pk_is_clustered && keynr == param->table->getShare()->getPrimaryKey()))
 
6300
  bool pk_is_clustered= file->primary_key_is_clustered();
 
6301
  if (index_only && 
 
6302
      (file->index_flags(keynr, param->max_key_part, 1) & HA_KEYREAD_ONLY) &&
 
6303
      !(pk_is_clustered && keynr == param->table->s->primary_key))
3906
6304
     *mrr_flags |= HA_MRR_INDEX_ONLY;
3907
 
 
3908
 
  if (session->lex->sql_command != SQLCOM_SELECT)
 
6305
  
 
6306
  if (current_thd->lex->sql_command != SQLCOM_SELECT)
3909
6307
    *mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
3910
6308
 
3911
 
  *bufsize= param->session->variables.read_rnd_buff_size;
3912
 
  rows= cursor->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
 
6309
  *bufsize= param->thd->variables.read_rnd_buff_size;
 
6310
  rows= file->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
3913
6311
                                          bufsize, mrr_flags, cost);
3914
6312
  if (rows != HA_POS_ERROR)
3915
6313
  {
3916
6314
    param->table->quick_rows[keynr]=rows;
3917
6315
    if (update_tbl_stats)
3918
6316
    {
3919
 
      param->table->quick_keys.set(keynr);
 
6317
      param->table->quick_keys.set_bit(keynr);
3920
6318
      param->table->quick_key_parts[keynr]=param->max_key_part+1;
3921
6319
      param->table->quick_n_ranges[keynr]= param->range_count;
3922
6320
      param->table->quick_condition_rows=
3927
6325
  enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
3928
6326
  if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
3929
6327
  {
3930
 
    /*
 
6328
    /* 
3931
6329
      All scans are non-ROR scans for those index types.
3932
 
      TODO: Don't have this logic here, make table engines return
 
6330
      TODO: Don't have this logic here, make table engines return 
3933
6331
      appropriate flags instead.
3934
6332
    */
3935
6333
    param->is_ror_scan= false;
3937
6335
  else
3938
6336
  {
3939
6337
    /* Clustered PK scan is always a ROR scan (TODO: same as above) */
3940
 
    if (param->table->getShare()->getPrimaryKey() == keynr && pk_is_clustered)
 
6338
    if (param->table->s->primary_key == keynr && pk_is_clustered)
3941
6339
      param->is_ror_scan= true;
3942
6340
  }
3943
6341
 
3944
 
  return(rows); //psergey-merge:todo: maintain first_null_comp.
 
6342
  DBUG_PRINT("exit", ("Records: %lu", (ulong) rows));
 
6343
  DBUG_RETURN(rows); //psergey-merge:todo: maintain first_null_comp.
3945
6344
}
3946
6345
 
3947
6346
 
3968
6367
 
3969
6368
    where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])
3970
6369
 
3971
 
    and the table has a clustered Primary Key defined as
3972
 
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k)
3973
 
 
3974
 
    i.e. the first key parts of it are identical to uncovered parts ot the
 
6370
    and the table has a clustered Primary Key defined as 
 
6371
      PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k) 
 
6372
    
 
6373
    i.e. the first key parts of it are identical to uncovered parts ot the 
3975
6374
    key being scanned. This function assumes that the index flags do not
3976
6375
    include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
3977
6376
 
3982
6381
    false  Otherwise
3983
6382
*/
3984
6383
 
3985
 
static bool is_key_scan_ror(optimizer::Parameter *param, uint32_t keynr, uint8_t nparts)
 
6384
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts)
3986
6385
{
3987
 
  KeyInfo *table_key= param->table->key_info + keynr;
3988
 
  KeyPartInfo *key_part= table_key->key_part + nparts;
3989
 
  KeyPartInfo *key_part_end= (table_key->key_part +
 
6386
  KEY *table_key= param->table->key_info + keynr;
 
6387
  KEY_PART_INFO *key_part= table_key->key_part + nparts;
 
6388
  KEY_PART_INFO *key_part_end= (table_key->key_part +
3990
6389
                                table_key->key_parts);
3991
 
  uint32_t pk_number;
3992
 
 
3993
 
  for (KeyPartInfo *kp= table_key->key_part; kp < key_part; kp++)
 
6390
  uint pk_number;
 
6391
  
 
6392
  for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
3994
6393
  {
3995
 
    uint16_t fieldnr= param->table->key_info[keynr].
 
6394
    uint16 fieldnr= param->table->key_info[keynr].
3996
6395
                    key_part[kp - table_key->key_part].fieldnr - 1;
3997
 
    if (param->table->getField(fieldnr)->key_length() != kp->length)
 
6396
    if (param->table->field[fieldnr]->key_length() != kp->length)
3998
6397
      return false;
3999
6398
  }
4000
6399
 
4002
6401
    return true;
4003
6402
 
4004
6403
  key_part= table_key->key_part + nparts;
4005
 
  pk_number= param->table->getShare()->getPrimaryKey();
4006
 
  if (!param->table->cursor->primary_key_is_clustered() || pk_number == MAX_KEY)
 
6404
  pk_number= param->table->s->primary_key;
 
6405
  if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
4007
6406
    return false;
4008
6407
 
4009
 
  KeyPartInfo *pk_part= param->table->key_info[pk_number].key_part;
4010
 
  KeyPartInfo *pk_part_end= pk_part +
 
6408
  KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
 
6409
  KEY_PART_INFO *pk_part_end= pk_part +
4011
6410
                              param->table->key_info[pk_number].key_parts;
4012
6411
  for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
4013
6412
       ++key_part, ++pk_part)
4020
6419
}
4021
6420
 
4022
6421
 
4023
 
optimizer::QuickRangeSelect *
4024
 
optimizer::get_quick_select(Parameter *param,
4025
 
                            uint32_t idx,
4026
 
                            optimizer::SEL_ARG *key_tree,
4027
 
                            uint32_t mrr_flags,
4028
 
                            uint32_t mrr_buf_size,
4029
 
                            memory::Root *parent_alloc)
 
6422
/*
 
6423
  Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.
 
6424
 
 
6425
  SYNOPSIS
 
6426
    get_quick_select()
 
6427
      param
 
6428
      idx            Index of used key in param->key.
 
6429
      key_tree       SEL_ARG tree for the used key
 
6430
      mrr_flags      MRR parameter for quick select
 
6431
      mrr_buf_size   MRR parameter for quick select
 
6432
      parent_alloc   If not NULL, use it to allocate memory for
 
6433
                     quick select data. Otherwise use quick->alloc.
 
6434
  NOTES
 
6435
    The caller must call QUICK_SELECT::init for returned quick select.
 
6436
 
 
6437
    CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
 
6438
    deallocated when the returned quick select is deleted.
 
6439
 
 
6440
  RETURN
 
6441
    NULL on error
 
6442
    otherwise created quick select
 
6443
*/
 
6444
 
 
6445
QUICK_RANGE_SELECT *
 
6446
get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree, uint mrr_flags,
 
6447
                 uint mrr_buf_size, MEM_ROOT *parent_alloc)
4030
6448
{
4031
 
  optimizer::QuickRangeSelect *quick= NULL;
 
6449
  QUICK_RANGE_SELECT *quick;
4032
6450
  bool create_err= false;
 
6451
  DBUG_ENTER("get_quick_select");
4033
6452
 
4034
 
  quick= new optimizer::QuickRangeSelect(param->session,
4035
 
                                         param->table,
4036
 
                                         param->real_keynr[idx],
4037
 
                                         test(parent_alloc),
4038
 
                                         NULL,
4039
 
                                         &create_err);
 
6453
  quick=new QUICK_RANGE_SELECT(param->thd, param->table,
 
6454
                               param->real_keynr[idx],
 
6455
                               test(parent_alloc), NULL, &create_err);
4040
6456
 
4041
6457
  if (quick)
4042
6458
  {
4043
6459
    if (create_err ||
4044
 
              get_quick_keys(param,
4045
 
                       quick,
4046
 
                       param->key[idx],
4047
 
                       key_tree,
4048
 
                       param->min_key,
4049
 
                       0,
4050
 
                                   param->max_key,
4051
 
                       0))
 
6460
        get_quick_keys(param,quick,param->key[idx],key_tree,param->min_key,0,
 
6461
                       param->max_key,0))
4052
6462
    {
4053
6463
      delete quick;
4054
 
      quick= NULL;
 
6464
      quick=0;
4055
6465
    }
4056
6466
    else
4057
6467
    {
4058
6468
      quick->mrr_flags= mrr_flags;
4059
6469
      quick->mrr_buf_size= mrr_buf_size;
4060
 
      if (parent_alloc)
4061
 
      {
4062
 
        quick->key_parts=(KEY_PART*)
4063
 
          parent_alloc->memdup_root( (char*) param->key[idx], sizeof(KEY_PART)* param->table->key_info[param->real_keynr[idx]].key_parts);
4064
 
      }
4065
 
      else
4066
 
      {
4067
 
        quick->key_parts=(KEY_PART*)
4068
 
          quick->alloc.memdup_root((char*) param->key[idx], sizeof(KEY_PART)* param->table->key_info[param->real_keynr[idx]].key_parts);
4069
 
      }
 
6470
      quick->key_parts=(KEY_PART*)
 
6471
        memdup_root(parent_alloc? parent_alloc : &quick->alloc,
 
6472
                    (char*) param->key[idx],
 
6473
                    sizeof(KEY_PART)*
 
6474
                    param->table->key_info[param->real_keynr[idx]].key_parts);
4070
6475
    }
4071
6476
  }
4072
 
  return quick;
 
6477
  DBUG_RETURN(quick);
4073
6478
}
4074
6479
 
4075
6480
 
4077
6482
** Fix this to get all possible sub_ranges
4078
6483
*/
4079
6484
bool
4080
 
optimizer::get_quick_keys(optimizer::Parameter *param,
4081
 
                          optimizer::QuickRangeSelect *quick,
4082
 
                          KEY_PART *key,
4083
 
                                optimizer::SEL_ARG *key_tree,
4084
 
                          unsigned char *min_key,
4085
 
                          uint32_t min_key_flag,
4086
 
                                unsigned char *max_key,
4087
 
                          uint32_t max_key_flag)
 
6485
get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
 
6486
               SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
 
6487
               uchar *max_key, uint max_key_flag)
4088
6488
{
4089
 
  optimizer::QuickRange *range= NULL;
4090
 
  uint32_t flag;
4091
 
  int min_part= key_tree->part - 1; // # of keypart values in min_key buffer
4092
 
  int max_part= key_tree->part - 1; // # of keypart values in max_key buffer
 
6489
  QUICK_RANGE *range;
 
6490
  uint flag;
 
6491
  int min_part= key_tree->part-1, // # of keypart values in min_key buffer
 
6492
      max_part= key_tree->part-1; // # of keypart values in max_key buffer
4093
6493
 
4094
 
  if (key_tree->left != &optimizer::null_element)
 
6494
  if (key_tree->left != &null_element)
4095
6495
  {
4096
 
    if (get_quick_keys(param,
4097
 
                       quick,
4098
 
                       key,
4099
 
                       key_tree->left,
4100
 
                                   min_key,
4101
 
                       min_key_flag,
4102
 
                       max_key,
4103
 
                       max_key_flag))
4104
 
    {
 
6496
    if (get_quick_keys(param,quick,key,key_tree->left,
 
6497
                       min_key,min_key_flag, max_key, max_key_flag))
4105
6498
      return 1;
4106
 
    }
4107
6499
  }
4108
 
  unsigned char *tmp_min_key= min_key,*tmp_max_key= max_key;
 
6500
  uchar *tmp_min_key=min_key,*tmp_max_key=max_key;
4109
6501
  min_part+= key_tree->store_min(key[key_tree->part].store_length,
4110
6502
                                 &tmp_min_key,min_key_flag);
4111
6503
  max_part+= key_tree->store_max(key[key_tree->part].store_length,
4113
6505
 
4114
6506
  if (key_tree->next_key_part &&
4115
6507
      key_tree->next_key_part->part == key_tree->part+1 &&
4116
 
      key_tree->next_key_part->type == optimizer::SEL_ARG::KEY_RANGE)
 
6508
      key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
4117
6509
  {                                               // const key as prefix
4118
6510
    if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
4119
 
        memcmp(min_key, max_key, (uint32_t)(tmp_max_key - max_key))==0 &&
4120
 
        key_tree->min_flag==0 && key_tree->max_flag==0)
 
6511
         memcmp(min_key, max_key, (uint)(tmp_max_key - max_key))==0 &&
 
6512
         key_tree->min_flag==0 && key_tree->max_flag==0)
4121
6513
    {
4122
 
      if (get_quick_keys(param,
4123
 
                         quick,
4124
 
                         key,
4125
 
                         key_tree->next_key_part,
4126
 
                         tmp_min_key,
4127
 
                         min_key_flag | key_tree->min_flag,
4128
 
                         tmp_max_key,
4129
 
                         max_key_flag | key_tree->max_flag))
4130
 
      {
4131
 
        return 1;
4132
 
      }
 
6514
      if (get_quick_keys(param,quick,key,key_tree->next_key_part,
 
6515
                         tmp_min_key, min_key_flag | key_tree->min_flag,
 
6516
                         tmp_max_key, max_key_flag | key_tree->max_flag))
 
6517
        return 1;
4133
6518
      goto end;                                 // Ugly, but efficient
4134
6519
    }
4135
6520
    {
4136
 
      uint32_t tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
4137
 
      if (! tmp_min_flag)
4138
 
      {
4139
 
        min_part+= key_tree->next_key_part->store_min_key(key,
4140
 
                                                          &tmp_min_key,
 
6521
      uint tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
 
6522
      if (!tmp_min_flag)
 
6523
        min_part+= key_tree->next_key_part->store_min_key(key, &tmp_min_key,
4141
6524
                                                          &tmp_min_flag);
4142
 
      }
4143
 
      if (! tmp_max_flag)
4144
 
      {
4145
 
        max_part+= key_tree->next_key_part->store_max_key(key,
4146
 
                                                          &tmp_max_key,
 
6525
      if (!tmp_max_flag)
 
6526
        max_part+= key_tree->next_key_part->store_max_key(key, &tmp_max_key,
4147
6527
                                                          &tmp_max_flag);
4148
 
      }
4149
6528
      flag=tmp_min_flag | tmp_max_flag;
4150
6529
    }
4151
6530
  }
4158
6537
    Ensure that some part of min_key and max_key are used.  If not,
4159
6538
    regard this as no lower/upper range
4160
6539
  */
4161
 
  if (tmp_min_key != param->min_key)
4162
 
  {
4163
 
    flag&= ~NO_MIN_RANGE;
4164
 
  }
4165
 
  else
4166
 
  {
4167
 
    flag|= NO_MIN_RANGE;
4168
 
  }
4169
 
  if (tmp_max_key != param->max_key)
4170
 
  {
4171
 
    flag&= ~NO_MAX_RANGE;
4172
 
  }
4173
 
  else
4174
 
  {
4175
 
    flag|= NO_MAX_RANGE;
 
6540
  {
 
6541
    if (tmp_min_key != param->min_key)
 
6542
      flag&= ~NO_MIN_RANGE;
 
6543
    else
 
6544
      flag|= NO_MIN_RANGE;
 
6545
    if (tmp_max_key != param->max_key)
 
6546
      flag&= ~NO_MAX_RANGE;
 
6547
    else
 
6548
      flag|= NO_MAX_RANGE;
4176
6549
  }
4177
6550
  if (flag == 0)
4178
6551
  {
4179
 
    uint32_t length= (uint32_t) (tmp_min_key - param->min_key);
4180
 
    if (length == (uint32_t) (tmp_max_key - param->max_key) &&
4181
 
              ! memcmp(param->min_key,param->max_key,length))
 
6552
    uint length= (uint) (tmp_min_key - param->min_key);
 
6553
    if (length == (uint) (tmp_max_key - param->max_key) &&
 
6554
        !memcmp(param->min_key,param->max_key,length))
4182
6555
    {
4183
 
      KeyInfo *table_key= quick->head->key_info+quick->index;
4184
 
      flag= EQ_RANGE;
 
6556
      KEY *table_key=quick->head->key_info+quick->index;
 
6557
      flag=EQ_RANGE;
4185
6558
      if ((table_key->flags & (HA_NOSAME)) == HA_NOSAME &&
4186
 
                key->part == table_key->key_parts-1)
 
6559
          key->part == table_key->key_parts-1)
4187
6560
      {
4188
 
        if (! (table_key->flags & HA_NULL_PART_KEY) ||
4189
 
            ! null_part_in_key(key,
4190
 
                               param->min_key,
4191
 
                               (uint32_t) (tmp_min_key - param->min_key)))
4192
 
        {
4193
 
          flag|= UNIQUE_RANGE;
4194
 
        }
4195
 
        else
4196
 
        {
4197
 
          flag|= NULL_RANGE;
4198
 
        }
 
6561
        if (!(table_key->flags & HA_NULL_PART_KEY) ||
 
6562
            !null_part_in_key(key,
 
6563
                              param->min_key,
 
6564
                              (uint) (tmp_min_key - param->min_key)))
 
6565
          flag|= UNIQUE_RANGE;
 
6566
        else
 
6567
          flag|= NULL_RANGE;
4199
6568
      }
4200
6569
    }
4201
6570
  }
4202
6571
 
4203
6572
  /* Get range for retrieving rows in QUICK_SELECT::get_next */
4204
 
  if (! (range= new optimizer::QuickRange(param->min_key,
4205
 
                                                             (uint32_t) (tmp_min_key - param->min_key),
4206
 
                                           min_part >=0 ? make_keypart_map(min_part) : 0,
4207
 
                                                             param->max_key,
4208
 
                                                             (uint32_t) (tmp_max_key - param->max_key),
4209
 
                                           max_part >=0 ? make_keypart_map(max_part) : 0,
4210
 
                                                             flag)))
4211
 
  {
 
6573
  if (!(range= new QUICK_RANGE(param->min_key,
 
6574
                               (uint) (tmp_min_key - param->min_key),
 
6575
                               min_part >=0 ? make_keypart_map(min_part) : 0,
 
6576
                               param->max_key,
 
6577
                               (uint) (tmp_max_key - param->max_key),
 
6578
                               max_part >=0 ? make_keypart_map(max_part) : 0,
 
6579
                               flag)))
4212
6580
    return 1;                   // out of memory
4213
 
  }
4214
6581
 
4215
 
  set_if_bigger(quick->max_used_key_length, (uint32_t)range->min_length);
4216
 
  set_if_bigger(quick->max_used_key_length, (uint32_t)range->max_length);
4217
 
  set_if_bigger(quick->used_key_parts, (uint32_t) key_tree->part+1);
4218
 
  if (insert_dynamic(&quick->ranges, (unsigned char*) &range))
4219
 
  {
 
6582
  set_if_bigger(quick->max_used_key_length, range->min_length);
 
6583
  set_if_bigger(quick->max_used_key_length, range->max_length);
 
6584
  set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
 
6585
  if (insert_dynamic(&quick->ranges, (uchar*) &range))
4220
6586
    return 1;
4221
 
  }
4222
6587
 
4223
6588
 end:
4224
 
  if (key_tree->right != &optimizer::null_element)
 
6589
  if (key_tree->right != &null_element)
 
6590
    return get_quick_keys(param,quick,key,key_tree->right,
 
6591
                          min_key,min_key_flag,
 
6592
                          max_key,max_key_flag);
 
6593
  return 0;
 
6594
}
 
6595
 
 
6596
/*
 
6597
  Return 1 if there is only one range and this uses the whole primary key
 
6598
*/
 
6599
 
 
6600
bool QUICK_RANGE_SELECT::unique_key_range()
 
6601
{
 
6602
  if (ranges.elements == 1)
4225
6603
  {
4226
 
    return get_quick_keys(param,
4227
 
                          quick,
4228
 
                          key,
4229
 
                          key_tree->right,
4230
 
                                            min_key,
4231
 
                          min_key_flag,
4232
 
                                            max_key,
4233
 
                          max_key_flag);
 
6604
    QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
 
6605
    if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
 
6606
    {
 
6607
      KEY *key=head->key_info+index;
 
6608
      return ((key->flags & (HA_NOSAME)) == HA_NOSAME &&
 
6609
              key->key_length == tmp->min_length);
 
6610
    }
4234
6611
  }
4235
6612
  return 0;
4236
6613
}
4237
6614
 
 
6615
 
 
6616
 
4238
6617
/*
4239
6618
  Return true if any part of the key is NULL
4240
6619
 
4241
6620
  SYNOPSIS
4242
 
    null_part_in_key()
 
6621
    null_part_in_key()    
4243
6622
      key_part  Array of key parts (index description)
4244
6623
      key       Key values tuple
4245
6624
      length    Length of key values tuple in bytes.
4249
6628
    false  Otherwise
4250
6629
*/
4251
6630
 
4252
 
static bool null_part_in_key(KEY_PART *key_part, const unsigned char *key, uint32_t length)
 
6631
static bool null_part_in_key(KEY_PART *key_part, const uchar *key, uint length)
4253
6632
{
4254
 
  for (const unsigned char *end=key+length ;
 
6633
  for (const uchar *end=key+length ;
4255
6634
       key < end;
4256
6635
       key+= key_part++->store_length)
4257
6636
  {
4262
6641
}
4263
6642
 
4264
6643
 
4265
 
bool optimizer::QuickSelectInterface::is_keys_used(const MyBitmap *fields)
 
6644
bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
4266
6645
{
4267
6646
  return is_key_used(head, index, fields);
4268
6647
}
4269
6648
 
 
6649
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
 
6650
{
 
6651
  QUICK_RANGE_SELECT *quick;
 
6652
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
6653
  while ((quick= it++))
 
6654
  {
 
6655
    if (is_key_used(head, quick->index, fields))
 
6656
      return 1;
 
6657
  }
 
6658
  return 0;
 
6659
}
 
6660
 
 
6661
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
 
6662
{
 
6663
  QUICK_RANGE_SELECT *quick;
 
6664
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
6665
  while ((quick= it++))
 
6666
  {
 
6667
    if (is_key_used(head, quick->index, fields))
 
6668
      return 1;
 
6669
  }
 
6670
  return 0;
 
6671
}
 
6672
 
 
6673
bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
 
6674
{
 
6675
  QUICK_SELECT_I *quick;
 
6676
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
6677
  while ((quick= it++))
 
6678
  {
 
6679
    if (quick->is_keys_used(fields))
 
6680
      return 1;
 
6681
  }
 
6682
  return 0;
 
6683
}
 
6684
 
4270
6685
 
4271
6686
/*
4272
6687
  Create quick select from ref/ref_or_null scan.
4273
6688
 
4274
6689
  SYNOPSIS
4275
6690
    get_quick_select_for_ref()
4276
 
      session      Thread handle
 
6691
      thd      Thread handle
4277
6692
      table    Table to access
4278
6693
      ref      ref[_or_null] scan parameters
4279
6694
      records  Estimate of number of records (needed only to construct
4287
6702
    NULL on error.
4288
6703
*/
4289
6704
 
4290
 
optimizer::QuickRangeSelect *optimizer::get_quick_select_for_ref(Session *session,
4291
 
                                                                 Table *table,
4292
 
                                                                 table_reference_st *ref,
4293
 
                                                                 ha_rows records)
 
6705
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
 
6706
                                             TABLE_REF *ref, ha_rows records)
4294
6707
{
4295
 
  memory::Root *old_root, *alloc;
4296
 
  optimizer::QuickRangeSelect *quick= NULL;
4297
 
  KeyInfo *key_info = &table->key_info[ref->key];
 
6708
  MEM_ROOT *old_root, *alloc;
 
6709
  QUICK_RANGE_SELECT *quick;
 
6710
  KEY *key_info = &table->key_info[ref->key];
4298
6711
  KEY_PART *key_part;
4299
 
  optimizer::QuickRange *range= NULL;
4300
 
  uint32_t part;
 
6712
  QUICK_RANGE *range;
 
6713
  uint part;
4301
6714
  bool create_err= false;
4302
 
  optimizer::CostVector cost;
 
6715
  COST_VECT cost;
4303
6716
 
4304
 
  old_root= session->mem_root;
4305
 
  /* The following call may change session->mem_root */
4306
 
  quick= new optimizer::QuickRangeSelect(session, table, ref->key, 0, 0, &create_err);
4307
 
  /* save mem_root set by QuickRangeSelect constructor */
4308
 
  alloc= session->mem_root;
 
6717
  old_root= thd->mem_root;
 
6718
  /* The following call may change thd->mem_root */
 
6719
  quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0, 0, &create_err);
 
6720
  /* save mem_root set by QUICK_RANGE_SELECT constructor */
 
6721
  alloc= thd->mem_root;
4309
6722
  /*
4310
 
    return back default mem_root (session->mem_root) changed by
4311
 
    QuickRangeSelect constructor
 
6723
    return back default mem_root (thd->mem_root) changed by
 
6724
    QUICK_RANGE_SELECT constructor
4312
6725
  */
4313
 
  session->mem_root= old_root;
 
6726
  thd->mem_root= old_root;
4314
6727
 
4315
6728
  if (!quick || create_err)
4316
6729
    return 0;                   /* no ranges found */
4318
6731
    goto err;
4319
6732
  quick->records= records;
4320
6733
 
4321
 
  if ((cp_buffer_from_ref(session, ref) && session->is_fatal_error) ||
4322
 
      !(range= new(alloc) optimizer::QuickRange()))
 
6734
  if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) ||
 
6735
      !(range= new(alloc) QUICK_RANGE()))
4323
6736
    goto err;                                   // out of memory
4324
6737
 
4325
6738
  range->min_key= range->max_key= ref->key_buff;
4327
6740
  range->min_keypart_map= range->max_keypart_map=
4328
6741
    make_prev_keypart_map(ref->key_parts);
4329
6742
  range->flag= ((ref->key_length == key_info->key_length &&
4330
 
                 (key_info->flags & HA_END_SPACE_KEY) == 0) ? EQ_RANGE : 0);
4331
 
 
 
6743
                 key_info->flags == 0) ? EQ_RANGE : 0);
4332
6744
 
4333
6745
  if (!(quick->key_parts=key_part=(KEY_PART *)
4334
 
        quick->alloc.alloc_root(sizeof(KEY_PART)*ref->key_parts)))
 
6746
        alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
4335
6747
    goto err;
4336
6748
 
4337
6749
  for (part=0 ; part < ref->key_parts ;part++,key_part++)
4341
6753
    key_part->length=       key_info->key_part[part].length;
4342
6754
    key_part->store_length= key_info->key_part[part].store_length;
4343
6755
    key_part->null_bit=     key_info->key_part[part].null_bit;
4344
 
    key_part->flag=         (uint8_t) key_info->key_part[part].key_part_flag;
 
6756
    key_part->flag=         (uint8) key_info->key_part[part].key_part_flag;
4345
6757
  }
4346
 
  if (insert_dynamic(&quick->ranges,(unsigned char*)&range))
 
6758
  if (insert_dynamic(&quick->ranges,(uchar*)&range))
4347
6759
    goto err;
4348
6760
 
4349
6761
  /*
4354
6766
  */
4355
6767
  if (ref->null_ref_key)
4356
6768
  {
4357
 
    optimizer::QuickRange *null_range= NULL;
 
6769
    QUICK_RANGE *null_range;
4358
6770
 
4359
6771
    *ref->null_ref_key= 1;              // Set null byte then create a range
4360
6772
    if (!(null_range= new (alloc)
4361
 
          optimizer::QuickRange(ref->key_buff, ref->key_length,
4362
 
                                 make_prev_keypart_map(ref->key_parts),
4363
 
                                 ref->key_buff, ref->key_length,
4364
 
                                 make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
 
6773
          QUICK_RANGE(ref->key_buff, ref->key_length,
 
6774
                      make_prev_keypart_map(ref->key_parts),
 
6775
                      ref->key_buff, ref->key_length,
 
6776
                      make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
4365
6777
      goto err;
4366
6778
    *ref->null_ref_key= 0;              // Clear null byte
4367
 
    if (insert_dynamic(&quick->ranges,(unsigned char*)&null_range))
 
6779
    if (insert_dynamic(&quick->ranges,(uchar*)&null_range))
4368
6780
      goto err;
4369
6781
  }
4370
6782
 
4371
6783
  /* Call multi_range_read_info() to get the MRR flags and buffer size */
4372
 
  quick->mrr_flags= HA_MRR_NO_ASSOCIATION |
 
6784
  quick->mrr_flags= HA_MRR_NO_ASSOCIATION | 
4373
6785
                    (table->key_read ? HA_MRR_INDEX_ONLY : 0);
4374
 
  if (session->lex->sql_command != SQLCOM_SELECT)
 
6786
  if (thd->lex->sql_command != SQLCOM_SELECT)
4375
6787
    quick->mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
4376
6788
 
4377
 
  quick->mrr_buf_size= session->variables.read_rnd_buff_size;
4378
 
  if (table->cursor->multi_range_read_info(quick->index, 1, (uint32_t)records,
4379
 
                                           &quick->mrr_buf_size,
4380
 
                                           &quick->mrr_flags, &cost))
 
6789
  quick->mrr_buf_size= thd->variables.read_rnd_buff_size;
 
6790
  if (table->file->multi_range_read_info(quick->index, 1, (uint)records,
 
6791
                                         &quick->mrr_buf_size,
 
6792
                                         &quick->mrr_flags, &cost))
4381
6793
    goto err;
4382
6794
 
4383
6795
  return quick;
4388
6800
 
4389
6801
 
4390
6802
/*
4391
 
  Range sequence interface implementation for array<QuickRange>: initialize
4392
 
 
 
6803
  Perform key scans for all used indexes (except CPK), get rowids and merge 
 
6804
  them into an ordered non-recurrent sequence of rowids.
 
6805
  
 
6806
  The merge/duplicate removal is performed using Unique class. We put all
 
6807
  rowids into Unique, get the sorted sequence and destroy the Unique.
 
6808
  
 
6809
  If table has a clustered primary key that covers all rows (true for bdb
 
6810
  and innodb currently) and one of the index_merge scans is a scan on PK,
 
6811
  then rows that will be retrieved by PK scan are not put into Unique and 
 
6812
  primary key scan is not performed here, it is performed later separately.
 
6813
 
 
6814
  RETURN
 
6815
    0     OK
 
6816
    other error
 
6817
*/
 
6818
 
 
6819
int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
 
6820
{
 
6821
  List_iterator_fast<QUICK_RANGE_SELECT> cur_quick_it(quick_selects);
 
6822
  QUICK_RANGE_SELECT* cur_quick;
 
6823
  int result;
 
6824
  Unique *unique;
 
6825
  handler *file= head->file;
 
6826
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
 
6827
 
 
6828
  file->extra(HA_EXTRA_KEYREAD);
 
6829
  head->prepare_for_position();
 
6830
 
 
6831
  cur_quick_it.rewind();
 
6832
  cur_quick= cur_quick_it++;
 
6833
  DBUG_ASSERT(cur_quick != 0);
 
6834
  
 
6835
  /*
 
6836
    We reuse the same instance of handler so we need to call both init and 
 
6837
    reset here.
 
6838
  */
 
6839
  if (cur_quick->init() || cur_quick->reset())
 
6840
    DBUG_RETURN(1);
 
6841
 
 
6842
  unique= new Unique(refpos_order_cmp, (void *)file,
 
6843
                     file->ref_length,
 
6844
                     thd->variables.sortbuff_size);
 
6845
  if (!unique)
 
6846
    DBUG_RETURN(1);
 
6847
  for (;;)
 
6848
  {
 
6849
    while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
 
6850
    {
 
6851
      cur_quick->range_end();
 
6852
      cur_quick= cur_quick_it++;
 
6853
      if (!cur_quick)
 
6854
        break;
 
6855
 
 
6856
      if (cur_quick->file->inited != handler::NONE) 
 
6857
        cur_quick->file->ha_index_end();
 
6858
      if (cur_quick->init() || cur_quick->reset())
 
6859
        DBUG_RETURN(1);
 
6860
    }
 
6861
 
 
6862
    if (result)
 
6863
    {
 
6864
      if (result != HA_ERR_END_OF_FILE)
 
6865
      {
 
6866
        cur_quick->range_end();
 
6867
        DBUG_RETURN(result);
 
6868
      }
 
6869
      break;
 
6870
    }
 
6871
 
 
6872
    if (thd->killed)
 
6873
      DBUG_RETURN(1);
 
6874
 
 
6875
    /* skip row if it will be retrieved by clustered PK scan */
 
6876
    if (pk_quick_select && pk_quick_select->row_in_ranges())
 
6877
      continue;
 
6878
 
 
6879
    cur_quick->file->position(cur_quick->record);
 
6880
    result= unique->unique_add((char*)cur_quick->file->ref);
 
6881
    if (result)
 
6882
      DBUG_RETURN(1);
 
6883
 
 
6884
  }
 
6885
 
 
6886
  DBUG_PRINT("info", ("ok"));
 
6887
  /* ok, all row ids are in Unique */
 
6888
  result= unique->get(head);
 
6889
  delete unique;
 
6890
  doing_pk_scan= false;
 
6891
  /* index_merge currently doesn't support "using index" at all */
 
6892
  file->extra(HA_EXTRA_NO_KEYREAD);
 
6893
  /* start table scan */
 
6894
  init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1);
 
6895
  DBUG_RETURN(result);
 
6896
}
 
6897
 
 
6898
 
 
6899
/*
 
6900
  Get next row for index_merge.
 
6901
  NOTES
 
6902
    The rows are read from
 
6903
      1. rowids stored in Unique.
 
6904
      2. QUICK_RANGE_SELECT with clustered primary key (if any).
 
6905
    The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
 
6906
*/
 
6907
 
 
6908
int QUICK_INDEX_MERGE_SELECT::get_next()
 
6909
{
 
6910
  int result;
 
6911
  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");
 
6912
 
 
6913
  if (doing_pk_scan)
 
6914
    DBUG_RETURN(pk_quick_select->get_next());
 
6915
 
 
6916
  if ((result= read_record.read_record(&read_record)) == -1)
 
6917
  {
 
6918
    result= HA_ERR_END_OF_FILE;
 
6919
    end_read_record(&read_record);
 
6920
    /* All rows from Unique have been retrieved, do a clustered PK scan */
 
6921
    if (pk_quick_select)
 
6922
    {
 
6923
      doing_pk_scan= true;
 
6924
      if ((result= pk_quick_select->init()) ||
 
6925
          (result= pk_quick_select->reset()))
 
6926
        DBUG_RETURN(result);
 
6927
      DBUG_RETURN(pk_quick_select->get_next());
 
6928
    }
 
6929
  }
 
6930
 
 
6931
  DBUG_RETURN(result);
 
6932
}
 
6933
 
 
6934
 
 
6935
/*
 
6936
  Retrieve next record.
 
6937
  SYNOPSIS
 
6938
     QUICK_ROR_INTERSECT_SELECT::get_next()
 
6939
 
 
6940
  NOTES
 
6941
    Invariant on enter/exit: all intersected selects have retrieved all index
 
6942
    records with rowid <= some_rowid_val and no intersected select has
 
6943
    retrieved any index records with rowid > some_rowid_val.
 
6944
    We start fresh and loop until we have retrieved the same rowid in each of
 
6945
    the key scans or we got an error.
 
6946
 
 
6947
    If a Clustered PK scan is present, it is used only to check if row
 
6948
    satisfies its condition (and never used for row retrieval).
 
6949
 
 
6950
  RETURN
 
6951
   0     - Ok
 
6952
   other - Error code if any error occurred.
 
6953
*/
 
6954
 
 
6955
int QUICK_ROR_INTERSECT_SELECT::get_next()
 
6956
{
 
6957
  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
 
6958
  QUICK_RANGE_SELECT* quick;
 
6959
  int error, cmp;
 
6960
  uint last_rowid_count=0;
 
6961
  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next");
 
6962
 
 
6963
  do
 
6964
  {
 
6965
    /* Get a rowid for first quick and save it as a 'candidate' */
 
6966
    quick= quick_it++;
 
6967
    error= quick->get_next();
 
6968
    if (cpk_quick)
 
6969
    {
 
6970
      while (!error && !cpk_quick->row_in_ranges())
 
6971
        error= quick->get_next();
 
6972
    }
 
6973
    if (error)
 
6974
      DBUG_RETURN(error);
 
6975
 
 
6976
    quick->file->position(quick->record);
 
6977
    memcpy(last_rowid, quick->file->ref, head->file->ref_length);
 
6978
    last_rowid_count= 1;
 
6979
 
 
6980
    while (last_rowid_count < quick_selects.elements)
 
6981
    {
 
6982
      if (!(quick= quick_it++))
 
6983
      {
 
6984
        quick_it.rewind();
 
6985
        quick= quick_it++;
 
6986
      }
 
6987
 
 
6988
      do
 
6989
      {
 
6990
        if ((error= quick->get_next()))
 
6991
          DBUG_RETURN(error);
 
6992
        quick->file->position(quick->record);
 
6993
        cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
 
6994
      } while (cmp < 0);
 
6995
 
 
6996
      /* Ok, current select 'caught up' and returned ref >= cur_ref */
 
6997
      if (cmp > 0)
 
6998
      {
 
6999
        /* Found a row with ref > cur_ref. Make it a new 'candidate' */
 
7000
        if (cpk_quick)
 
7001
        {
 
7002
          while (!cpk_quick->row_in_ranges())
 
7003
          {
 
7004
            if ((error= quick->get_next()))
 
7005
              DBUG_RETURN(error);
 
7006
          }
 
7007
        }
 
7008
        memcpy(last_rowid, quick->file->ref, head->file->ref_length);
 
7009
        last_rowid_count= 1;
 
7010
      }
 
7011
      else
 
7012
      {
 
7013
        /* current 'candidate' row confirmed by this select */
 
7014
        last_rowid_count++;
 
7015
      }
 
7016
    }
 
7017
 
 
7018
    /* We get here if we got the same row ref in all scans. */
 
7019
    if (need_to_fetch_row)
 
7020
      error= head->file->rnd_pos(head->record[0], last_rowid);
 
7021
  } while (error == HA_ERR_RECORD_DELETED);
 
7022
  DBUG_RETURN(error);
 
7023
}
 
7024
 
 
7025
 
 
7026
/*
 
7027
  Retrieve next record.
 
7028
  SYNOPSIS
 
7029
    QUICK_ROR_UNION_SELECT::get_next()
 
7030
 
 
7031
  NOTES
 
7032
    Enter/exit invariant:
 
7033
    For each quick select in the queue a {key,rowid} tuple has been
 
7034
    retrieved but the corresponding row hasn't been passed to output.
 
7035
 
 
7036
  RETURN
 
7037
   0     - Ok
 
7038
   other - Error code if any error occurred.
 
7039
*/
 
7040
 
 
7041
int QUICK_ROR_UNION_SELECT::get_next()
 
7042
{
 
7043
  int error, dup_row;
 
7044
  QUICK_SELECT_I *quick;
 
7045
  uchar *tmp;
 
7046
  DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");
 
7047
 
 
7048
  do
 
7049
  {
 
7050
    do
 
7051
    {
 
7052
      if (!queue.elements)
 
7053
        DBUG_RETURN(HA_ERR_END_OF_FILE);
 
7054
      /* Ok, we have a queue with >= 1 scans */
 
7055
 
 
7056
      quick= (QUICK_SELECT_I*)queue_top(&queue);
 
7057
      memcpy(cur_rowid, quick->last_rowid, rowid_length);
 
7058
 
 
7059
      /* put into queue rowid from the same stream as top element */
 
7060
      if ((error= quick->get_next()))
 
7061
      {
 
7062
        if (error != HA_ERR_END_OF_FILE)
 
7063
          DBUG_RETURN(error);
 
7064
        queue_remove(&queue, 0);
 
7065
      }
 
7066
      else
 
7067
      {
 
7068
        quick->save_last_pos();
 
7069
        queue_replaced(&queue);
 
7070
      }
 
7071
 
 
7072
      if (!have_prev_rowid)
 
7073
      {
 
7074
        /* No rows have been returned yet */
 
7075
        dup_row= false;
 
7076
        have_prev_rowid= true;
 
7077
      }
 
7078
      else
 
7079
        dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
 
7080
    } while (dup_row);
 
7081
 
 
7082
    tmp= cur_rowid;
 
7083
    cur_rowid= prev_rowid;
 
7084
    prev_rowid= tmp;
 
7085
 
 
7086
    error= head->file->rnd_pos(quick->record, prev_rowid);
 
7087
  } while (error == HA_ERR_RECORD_DELETED);
 
7088
  DBUG_RETURN(error);
 
7089
}
 
7090
 
 
7091
 
 
7092
int QUICK_RANGE_SELECT::reset()
 
7093
{
 
7094
  uint  buf_size;
 
7095
  uchar *mrange_buff;
 
7096
  int   error;
 
7097
  HANDLER_BUFFER empty_buf;
 
7098
  DBUG_ENTER("QUICK_RANGE_SELECT::reset");
 
7099
  last_range= NULL;
 
7100
  cur_range= (QUICK_RANGE**) ranges.buffer;
 
7101
 
 
7102
  if (file->inited == handler::NONE && (error= file->ha_index_init(index,1)))
 
7103
    DBUG_RETURN(error);
 
7104
 
 
7105
  /* Allocate buffer if we need one but haven't allocated it yet */
 
7106
  if (mrr_buf_size && !mrr_buf_desc)
 
7107
  {
 
7108
    buf_size= mrr_buf_size;
 
7109
    while (buf_size && !my_multi_malloc(MYF(MY_WME),
 
7110
                                        &mrr_buf_desc, sizeof(*mrr_buf_desc),
 
7111
                                        &mrange_buff, buf_size,
 
7112
                                        NullS))
 
7113
    {
 
7114
      /* Try to shrink the buffers until both are 0. */
 
7115
      buf_size/= 2;
 
7116
    }
 
7117
    if (!mrr_buf_desc)
 
7118
      DBUG_RETURN(HA_ERR_OUT_OF_MEM);
 
7119
 
 
7120
    /* Initialize the handler buffer. */
 
7121
    mrr_buf_desc->buffer= mrange_buff;
 
7122
    mrr_buf_desc->buffer_end= mrange_buff + buf_size;
 
7123
    mrr_buf_desc->end_of_used_area= mrange_buff;
 
7124
  }
 
7125
 
 
7126
  if (!mrr_buf_desc)
 
7127
    empty_buf.buffer= empty_buf.buffer_end= empty_buf.end_of_used_area= NULL;
 
7128
 
 
7129
  if (sorted)
 
7130
     mrr_flags |= HA_MRR_SORTED;
 
7131
  RANGE_SEQ_IF seq_funcs= {quick_range_seq_init, quick_range_seq_next};
 
7132
  error= file->multi_range_read_init(&seq_funcs, (void*)this, ranges.elements,
 
7133
                                     mrr_flags, mrr_buf_desc? mrr_buf_desc: 
 
7134
                                                              &empty_buf);
 
7135
  DBUG_RETURN(error);
 
7136
}
 
7137
 
 
7138
 
 
7139
/*
 
7140
  Range sequence interface implementation for array<QUICK_RANGE>: initialize
 
7141
  
4393
7142
  SYNOPSIS
4394
7143
    quick_range_seq_init()
4395
 
      init_param  Caller-opaque paramenter: QuickRangeSelect* pointer
 
7144
      init_param  Caller-opaque paramenter: QUICK_RANGE_SELECT* pointer
4396
7145
      n_ranges    Number of ranges in the sequence (ignored)
4397
 
      flags       MRR flags (currently not used)
 
7146
      flags       MRR flags (currently not used) 
4398
7147
 
4399
7148
  RETURN
4400
7149
    Opaque value to be passed to quick_range_seq_next
4401
7150
*/
4402
7151
 
4403
 
range_seq_t optimizer::quick_range_seq_init(void *init_param, uint32_t, uint32_t)
 
7152
range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags)
4404
7153
{
4405
 
  optimizer::QuickRangeSelect *quick= (optimizer::QuickRangeSelect*)init_param;
4406
 
  quick->qr_traversal_ctx.first=  (optimizer::QuickRange**)quick->ranges.buffer;
4407
 
  quick->qr_traversal_ctx.cur=    (optimizer::QuickRange**)quick->ranges.buffer;
4408
 
  quick->qr_traversal_ctx.last=   quick->qr_traversal_ctx.cur +
 
7154
  QUICK_RANGE_SELECT *quick= (QUICK_RANGE_SELECT*)init_param;
 
7155
  quick->qr_traversal_ctx.first=  (QUICK_RANGE**)quick->ranges.buffer;
 
7156
  quick->qr_traversal_ctx.cur=    (QUICK_RANGE**)quick->ranges.buffer;
 
7157
  quick->qr_traversal_ctx.last=   quick->qr_traversal_ctx.cur + 
4409
7158
                                  quick->ranges.elements;
4410
7159
  return &quick->qr_traversal_ctx;
4411
7160
}
4412
7161
 
4413
7162
 
4414
7163
/*
4415
 
  Range sequence interface implementation for array<QuickRange>: get next
4416
 
 
 
7164
  Range sequence interface implementation for array<QUICK_RANGE>: get next
 
7165
  
4417
7166
  SYNOPSIS
4418
7167
    quick_range_seq_next()
4419
7168
      rseq        Value returned from quick_range_seq_init
4423
7172
    0  Ok
4424
7173
    1  No more ranges in the sequence
4425
7174
*/
4426
 
uint32_t optimizer::quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
 
7175
 
 
7176
uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
4427
7177
{
4428
 
  QuickRangeSequenceContext *ctx= (QuickRangeSequenceContext*) rseq;
 
7178
  QUICK_RANGE_SEQ_CTX *ctx= (QUICK_RANGE_SEQ_CTX*)rseq;
4429
7179
 
4430
7180
  if (ctx->cur == ctx->last)
4431
7181
    return 1; /* no more ranges */
4432
7182
 
4433
 
  optimizer::QuickRange *cur= *(ctx->cur);
 
7183
  QUICK_RANGE *cur= *(ctx->cur);
4434
7184
  key_range *start_key= &range->start_key;
4435
 
  key_range *end_key= &range->end_key;
 
7185
  key_range *end_key=   &range->end_key;
4436
7186
 
4437
 
  start_key->key= cur->min_key;
 
7187
  start_key->key=    cur->min_key;
4438
7188
  start_key->length= cur->min_length;
4439
7189
  start_key->keypart_map= cur->min_keypart_map;
4440
 
  start_key->flag= ((cur->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
4441
 
                                             (cur->flag & EQ_RANGE) ?
4442
 
                                             HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
4443
 
  end_key->key= cur->max_key;
4444
 
  end_key->length= cur->max_length;
 
7190
  start_key->flag=   ((cur->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
 
7191
                      (cur->flag & EQ_RANGE) ?
 
7192
                      HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
 
7193
  end_key->key=      cur->max_key;
 
7194
  end_key->length=   cur->max_length;
4445
7195
  end_key->keypart_map= cur->max_keypart_map;
4446
7196
  /*
4447
7197
    We use HA_READ_AFTER_KEY here because if we are reading on a key
4448
7198
    prefix. We want to find all keys with this prefix.
4449
7199
  */
4450
 
  end_key->flag= (cur->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
4451
 
                                         HA_READ_AFTER_KEY);
 
7200
  end_key->flag=     (cur->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
 
7201
                      HA_READ_AFTER_KEY);
4452
7202
  range->range_flag= cur->flag;
4453
7203
  ctx->cur++;
4454
7204
  return 0;
4455
7205
}
4456
7206
 
4457
7207
 
4458
 
static inline uint32_t get_field_keypart(KeyInfo *index, Field *field);
4459
 
 
4460
 
static inline optimizer::SEL_ARG * get_index_range_tree(uint32_t index,
4461
 
                                                        optimizer::SEL_TREE *range_tree,
4462
 
                                                        optimizer::Parameter *param,
4463
 
                                                        uint32_t *param_idx);
4464
 
 
4465
 
static bool get_constant_key_infix(KeyInfo *index_info,
4466
 
                                   optimizer::SEL_ARG *index_range_tree,
4467
 
                                   KeyPartInfo *first_non_group_part,
4468
 
                                   KeyPartInfo *min_max_arg_part,
4469
 
                                   KeyPartInfo *last_part,
4470
 
                                   Session *session,
4471
 
                                   unsigned char *key_infix,
4472
 
                                   uint32_t *key_infix_len,
4473
 
                                   KeyPartInfo **first_non_infix_part);
4474
 
 
4475
 
static bool check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item);
 
7208
/*
 
7209
  MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
 
7210
 
 
7211
  SYNOPSIS
 
7212
    mrr_persistent_flag_storage()
 
7213
      seq  Range sequence being traversed
 
7214
      idx  Number of range
 
7215
 
 
7216
  DESCRIPTION
 
7217
    MRR/NDB implementation needs to store some bits for each range. This
 
7218
    function returns a reference to the "range_flag" associated with the
 
7219
    range number idx.
 
7220
 
 
7221
    This function should be removed when we get a proper MRR/NDB 
 
7222
    implementation.
 
7223
 
 
7224
  RETURN
 
7225
    Reference to range_flag associated with range number #idx
 
7226
*/
 
7227
 
 
7228
uint16 &mrr_persistent_flag_storage(range_seq_t seq, uint idx)
 
7229
{
 
7230
  QUICK_RANGE_SEQ_CTX *ctx= (QUICK_RANGE_SEQ_CTX*)seq;
 
7231
  return ctx->first[idx]->flag;
 
7232
}
 
7233
 
 
7234
 
 
7235
/*
 
7236
  MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
 
7237
 
 
7238
  SYNOPSIS
 
7239
    mrr_get_ptr_by_idx()
 
7240
      seq  Range sequence bening traversed
 
7241
      idx  Number of the range
 
7242
 
 
7243
  DESCRIPTION
 
7244
    An extension of MRR range sequence interface needed by NDB: return the
 
7245
    data associated with the given range.
 
7246
 
 
7247
    A proper MRR interface implementer is supposed to store and return
 
7248
    range-associated data. NDB stores number of the range instead. So this
 
7249
    is a helper function that translates range number to range associated
 
7250
    data.
 
7251
 
 
7252
    This function does nothing, as currrently there is only one user of the
 
7253
    MRR interface - the quick range select code, and this user doesn't need
 
7254
    to use range-associated data.
 
7255
 
 
7256
  RETURN
 
7257
    Reference to range-associated data
 
7258
*/
 
7259
 
 
7260
char* &mrr_get_ptr_by_idx(range_seq_t seq, uint idx)
 
7261
{
 
7262
  static char *dummy;
 
7263
  return dummy;
 
7264
}
 
7265
 
 
7266
 
 
7267
/*
 
7268
  Get next possible record using quick-struct.
 
7269
 
 
7270
  SYNOPSIS
 
7271
    QUICK_RANGE_SELECT::get_next()
 
7272
 
 
7273
  NOTES
 
7274
    Record is read into table->record[0]
 
7275
 
 
7276
  RETURN
 
7277
    0                   Found row
 
7278
    HA_ERR_END_OF_FILE  No (more) rows in range
 
7279
    #                   Error code
 
7280
*/
 
7281
 
 
7282
int QUICK_RANGE_SELECT::get_next()
 
7283
{
 
7284
  char *dummy;
 
7285
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next");
 
7286
  if (in_ror_merged_scan)
 
7287
  {
 
7288
    /*
 
7289
      We don't need to signal the bitmap change as the bitmap is always the
 
7290
      same for this head->file
 
7291
    */
 
7292
    head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap);
 
7293
  }
 
7294
 
 
7295
  int result= file->multi_range_read_next(&dummy);
 
7296
 
 
7297
  if (in_ror_merged_scan)
 
7298
  {
 
7299
    /* Restore bitmaps set on entry */
 
7300
    head->column_bitmaps_set_no_signal(save_read_set, save_write_set);
 
7301
  }
 
7302
  DBUG_RETURN(result);
 
7303
}
 
7304
 
 
7305
 
 
7306
/*
 
7307
  Get the next record with a different prefix.
 
7308
 
 
7309
  SYNOPSIS
 
7310
    QUICK_RANGE_SELECT::get_next_prefix()
 
7311
    prefix_length  length of cur_prefix
 
7312
    cur_prefix     prefix of a key to be searched for
 
7313
 
 
7314
  DESCRIPTION
 
7315
    Each subsequent call to the method retrieves the first record that has a
 
7316
    prefix with length prefix_length different from cur_prefix, such that the
 
7317
    record with the new prefix is within the ranges described by
 
7318
    this->ranges. The record found is stored into the buffer pointed by
 
7319
    this->record.
 
7320
    The method is useful for GROUP-BY queries with range conditions to
 
7321
    discover the prefix of the next group that satisfies the range conditions.
 
7322
 
 
7323
  TODO
 
7324
    This method is a modified copy of QUICK_RANGE_SELECT::get_next(), so both
 
7325
    methods should be unified into a more general one to reduce code
 
7326
    duplication.
 
7327
 
 
7328
  RETURN
 
7329
    0                  on success
 
7330
    HA_ERR_END_OF_FILE if returned all keys
 
7331
    other              if some error occurred
 
7332
*/
 
7333
 
 
7334
int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
 
7335
                                        key_part_map keypart_map,
 
7336
                                        uchar *cur_prefix)
 
7337
{
 
7338
  DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");
 
7339
 
 
7340
  for (;;)
 
7341
  {
 
7342
    int result;
 
7343
    key_range start_key, end_key;
 
7344
    if (last_range)
 
7345
    {
 
7346
      /* Read the next record in the same range with prefix after cur_prefix. */
 
7347
      DBUG_ASSERT(cur_prefix != 0);
 
7348
      result= file->index_read_map(record, cur_prefix, keypart_map,
 
7349
                                   HA_READ_AFTER_KEY);
 
7350
      if (result || (file->compare_key(file->end_range) <= 0))
 
7351
        DBUG_RETURN(result);
 
7352
    }
 
7353
 
 
7354
    uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
 
7355
    if (count == 0)
 
7356
    {
 
7357
      /* Ranges have already been used up before. None is left for read. */
 
7358
      last_range= 0;
 
7359
      DBUG_RETURN(HA_ERR_END_OF_FILE);
 
7360
    }
 
7361
    last_range= *(cur_range++);
 
7362
 
 
7363
    start_key.key=    (const uchar*) last_range->min_key;
 
7364
    start_key.length= min(last_range->min_length, prefix_length);
 
7365
    start_key.keypart_map= last_range->min_keypart_map & keypart_map;
 
7366
    start_key.flag=   ((last_range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
 
7367
                       (last_range->flag & EQ_RANGE) ?
 
7368
                       HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
 
7369
    end_key.key=      (const uchar*) last_range->max_key;
 
7370
    end_key.length=   min(last_range->max_length, prefix_length);
 
7371
    end_key.keypart_map= last_range->max_keypart_map & keypart_map;
 
7372
    /*
 
7373
      We use READ_AFTER_KEY here because if we are reading on a key
 
7374
      prefix we want to find all keys with this prefix
 
7375
    */
 
7376
    end_key.flag=     (last_range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
 
7377
                       HA_READ_AFTER_KEY);
 
7378
 
 
7379
    result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0,
 
7380
                                   last_range->max_keypart_map ? &end_key : 0,
 
7381
                                   test(last_range->flag & EQ_RANGE),
 
7382
                                   sorted);
 
7383
    if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
 
7384
      last_range= 0;                    // Stop searching
 
7385
 
 
7386
    if (result != HA_ERR_END_OF_FILE)
 
7387
      DBUG_RETURN(result);
 
7388
    last_range= 0;                      // No matching rows; go to next range
 
7389
  }
 
7390
}
 
7391
 
 
7392
 
 
7393
/*
 
7394
  Check if current row will be retrieved by this QUICK_RANGE_SELECT
 
7395
 
 
7396
  NOTES
 
7397
    It is assumed that currently a scan is being done on another index
 
7398
    which reads all necessary parts of the index that is scanned by this
 
7399
    quick select.
 
7400
    The implementation does a binary search on sorted array of disjoint
 
7401
    ranges, without taking size of range into account.
 
7402
 
 
7403
    This function is used to filter out clustered PK scan rows in
 
7404
    index_merge quick select.
 
7405
 
 
7406
  RETURN
 
7407
    true  if current row will be retrieved by this quick select
 
7408
    false if not
 
7409
*/
 
7410
 
 
7411
bool QUICK_RANGE_SELECT::row_in_ranges()
 
7412
{
 
7413
  QUICK_RANGE *res;
 
7414
  uint min= 0;
 
7415
  uint max= ranges.elements - 1;
 
7416
  uint mid= (max + min)/2;
 
7417
 
 
7418
  while (min != max)
 
7419
  {
 
7420
    if (cmp_next(*(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid)))
 
7421
    {
 
7422
      /* current row value > mid->max */
 
7423
      min= mid + 1;
 
7424
    }
 
7425
    else
 
7426
      max= mid;
 
7427
    mid= (min + max) / 2;
 
7428
  }
 
7429
  res= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
 
7430
  return (!cmp_next(res) && !cmp_prev(res));
 
7431
}
 
7432
 
 
7433
/*
 
7434
  This is a hack: we inherit from QUICK_SELECT so that we can use the
 
7435
  get_next() interface, but we have to hold a pointer to the original
 
7436
  QUICK_SELECT because its data are used all over the place.  What
 
7437
  should be done is to factor out the data that is needed into a base
 
7438
  class (QUICK_SELECT), and then have two subclasses (_ASC and _DESC)
 
7439
  which handle the ranges and implement the get_next() function.  But
 
7440
  for now, this seems to work right at least.
 
7441
 */
 
7442
 
 
7443
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
 
7444
                                     uint used_key_parts_arg,
 
7445
                                     bool *create_err)
 
7446
 :QUICK_RANGE_SELECT(*q), rev_it(rev_ranges)
 
7447
{
 
7448
  QUICK_RANGE *r;
 
7449
 
 
7450
  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
 
7451
  QUICK_RANGE **end_range= pr + ranges.elements;
 
7452
  for (; pr!=end_range; pr++)
 
7453
    rev_ranges.push_front(*pr);
 
7454
 
 
7455
  /* Remove EQ_RANGE flag for keys that are not using the full key */
 
7456
  for (r = rev_it++; r; r = rev_it++)
 
7457
  {
 
7458
    if ((r->flag & EQ_RANGE) &&
 
7459
        head->key_info[index].key_length != r->max_length)
 
7460
      r->flag&= ~EQ_RANGE;
 
7461
  }
 
7462
  rev_it.rewind();
 
7463
  q->dont_free=1;                               // Don't free shared mem
 
7464
  delete q;
 
7465
}
 
7466
 
 
7467
 
 
7468
int QUICK_SELECT_DESC::get_next()
 
7469
{
 
7470
  DBUG_ENTER("QUICK_SELECT_DESC::get_next");
 
7471
 
 
7472
  /* The max key is handled as follows:
 
7473
   *   - if there is NO_MAX_RANGE, start at the end and move backwards
 
7474
   *   - if it is an EQ_RANGE, which means that max key covers the entire
 
7475
   *     key, go directly to the key and read through it (sorting backwards is
 
7476
   *     same as sorting forwards)
 
7477
   *   - if it is NEAR_MAX, go to the key or next, step back once, and
 
7478
   *     move backwards
 
7479
   *   - otherwise (not NEAR_MAX == include the key), go after the key,
 
7480
   *     step back once, and move backwards
 
7481
   */
 
7482
 
 
7483
  for (;;)
 
7484
  {
 
7485
    int result;
 
7486
    if (last_range)
 
7487
    {                                           // Already read through key
 
7488
      result = ((last_range->flag & EQ_RANGE)
 
7489
                ? file->index_next_same(record, last_range->min_key,
 
7490
                                        last_range->min_length) :
 
7491
                file->index_prev(record));
 
7492
      if (!result)
 
7493
      {
 
7494
        if (cmp_prev(*rev_it.ref()) == 0)
 
7495
          DBUG_RETURN(0);
 
7496
      }
 
7497
      else if (result != HA_ERR_END_OF_FILE)
 
7498
        DBUG_RETURN(result);
 
7499
    }
 
7500
 
 
7501
    if (!(last_range= rev_it++))
 
7502
      DBUG_RETURN(HA_ERR_END_OF_FILE);          // All ranges used
 
7503
 
 
7504
    if (last_range->flag & NO_MAX_RANGE)        // Read last record
 
7505
    {
 
7506
      int local_error;
 
7507
      if ((local_error=file->index_last(record)))
 
7508
        DBUG_RETURN(local_error);               // Empty table
 
7509
      if (cmp_prev(last_range) == 0)
 
7510
        DBUG_RETURN(0);
 
7511
      last_range= 0;                            // No match; go to next range
 
7512
      continue;
 
7513
    }
 
7514
 
 
7515
    if (last_range->flag & EQ_RANGE)
 
7516
    {
 
7517
      result = file->index_read_map(record, last_range->max_key,
 
7518
                                    last_range->max_keypart_map,
 
7519
                                    HA_READ_KEY_EXACT);
 
7520
    }
 
7521
    else
 
7522
    {
 
7523
      DBUG_ASSERT(last_range->flag & NEAR_MAX ||
 
7524
                  range_reads_after_key(last_range));
 
7525
      result=file->index_read_map(record, last_range->max_key,
 
7526
                                  last_range->max_keypart_map,
 
7527
                                  ((last_range->flag & NEAR_MAX) ?
 
7528
                                   HA_READ_BEFORE_KEY :
 
7529
                                   HA_READ_PREFIX_LAST_OR_PREV));
 
7530
    }
 
7531
    if (result)
 
7532
    {
 
7533
      if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
 
7534
        DBUG_RETURN(result);
 
7535
      last_range= 0;                            // Not found, to next range
 
7536
      continue;
 
7537
    }
 
7538
    if (cmp_prev(last_range) == 0)
 
7539
    {
 
7540
      if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
 
7541
        last_range= 0;                          // Stop searching
 
7542
      DBUG_RETURN(0);                           // Found key is in range
 
7543
    }
 
7544
    last_range= 0;                              // To next range
 
7545
  }
 
7546
}
 
7547
 
 
7548
 
 
7549
/*
 
7550
  Compare if found key is over max-value
 
7551
  Returns 0 if key <= range->max_key
 
7552
  TODO: Figure out why can't this function be as simple as cmp_prev(). 
 
7553
*/
 
7554
 
 
7555
int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
 
7556
{
 
7557
  if (range_arg->flag & NO_MAX_RANGE)
 
7558
    return 0;                                   /* key can't be to large */
 
7559
 
 
7560
  KEY_PART *key_part=key_parts;
 
7561
  uint store_length;
 
7562
 
 
7563
  for (uchar *key=range_arg->max_key, *end=key+range_arg->max_length;
 
7564
       key < end;
 
7565
       key+= store_length, key_part++)
 
7566
  {
 
7567
    int cmp;
 
7568
    store_length= key_part->store_length;
 
7569
    if (key_part->null_bit)
 
7570
    {
 
7571
      if (*key)
 
7572
      {
 
7573
        if (!key_part->field->is_null())
 
7574
          return 1;
 
7575
        continue;
 
7576
      }
 
7577
      else if (key_part->field->is_null())
 
7578
        return 0;
 
7579
      key++;                                    // Skip null byte
 
7580
      store_length--;
 
7581
    }
 
7582
    if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
 
7583
      return 0;
 
7584
    if (cmp > 0)
 
7585
      return 1;
 
7586
  }
 
7587
  return (range_arg->flag & NEAR_MAX) ? 1 : 0;          // Exact match
 
7588
}
 
7589
 
 
7590
 
 
7591
/*
 
7592
  Returns 0 if found key is inside range (found key >= range->min_key).
 
7593
*/
 
7594
 
 
7595
int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
 
7596
{
 
7597
  int cmp;
 
7598
  if (range_arg->flag & NO_MIN_RANGE)
 
7599
    return 0;                                   /* key can't be to small */
 
7600
 
 
7601
  cmp= key_cmp(key_part_info, range_arg->min_key,
 
7602
               range_arg->min_length);
 
7603
  if (cmp > 0 || (cmp == 0 && (range_arg->flag & NEAR_MIN) == false))
 
7604
    return 0;
 
7605
  return 1;                                     // outside of range
 
7606
}
 
7607
 
 
7608
 
 
7609
/*
 
7610
 * true if this range will require using HA_READ_AFTER_KEY
 
7611
   See comment in get_next() about this
 
7612
 */
 
7613
 
 
7614
bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
 
7615
{
 
7616
  return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
 
7617
          !(range_arg->flag & EQ_RANGE) ||
 
7618
          head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
 
7619
}
 
7620
 
 
7621
 
 
7622
void QUICK_RANGE_SELECT::add_info_string(String *str)
 
7623
{
 
7624
  KEY *key_info= head->key_info + index;
 
7625
  str->append(key_info->name);
 
7626
}
 
7627
 
 
7628
void QUICK_INDEX_MERGE_SELECT::add_info_string(String *str)
 
7629
{
 
7630
  QUICK_RANGE_SELECT *quick;
 
7631
  bool first= true;
 
7632
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
7633
  str->append(STRING_WITH_LEN("sort_union("));
 
7634
  while ((quick= it++))
 
7635
  {
 
7636
    if (!first)
 
7637
      str->append(',');
 
7638
    else
 
7639
      first= false;
 
7640
    quick->add_info_string(str);
 
7641
  }
 
7642
  if (pk_quick_select)
 
7643
  {
 
7644
    str->append(',');
 
7645
    pk_quick_select->add_info_string(str);
 
7646
  }
 
7647
  str->append(')');
 
7648
}
 
7649
 
 
7650
void QUICK_ROR_INTERSECT_SELECT::add_info_string(String *str)
 
7651
{
 
7652
  bool first= true;
 
7653
  QUICK_RANGE_SELECT *quick;
 
7654
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
7655
  str->append(STRING_WITH_LEN("intersect("));
 
7656
  while ((quick= it++))
 
7657
  {
 
7658
    KEY *key_info= head->key_info + quick->index;
 
7659
    if (!first)
 
7660
      str->append(',');
 
7661
    else
 
7662
      first= false;
 
7663
    str->append(key_info->name);
 
7664
  }
 
7665
  if (cpk_quick)
 
7666
  {
 
7667
    KEY *key_info= head->key_info + cpk_quick->index;
 
7668
    str->append(',');
 
7669
    str->append(key_info->name);
 
7670
  }
 
7671
  str->append(')');
 
7672
}
 
7673
 
 
7674
void QUICK_ROR_UNION_SELECT::add_info_string(String *str)
 
7675
{
 
7676
  bool first= true;
 
7677
  QUICK_SELECT_I *quick;
 
7678
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
7679
  str->append(STRING_WITH_LEN("union("));
 
7680
  while ((quick= it++))
 
7681
  {
 
7682
    if (!first)
 
7683
      str->append(',');
 
7684
    else
 
7685
      first= false;
 
7686
    quick->add_info_string(str);
 
7687
  }
 
7688
  str->append(')');
 
7689
}
 
7690
 
 
7691
 
 
7692
void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
 
7693
                                              String *used_lengths)
 
7694
{
 
7695
  char buf[64];
 
7696
  uint length;
 
7697
  KEY *key_info= head->key_info + index;
 
7698
  key_names->append(key_info->name);
 
7699
  length= longlong2str(max_used_key_length, buf, 10) - buf;
 
7700
  used_lengths->append(buf, length);
 
7701
}
 
7702
 
 
7703
void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
 
7704
                                                    String *used_lengths)
 
7705
{
 
7706
  char buf[64];
 
7707
  uint length;
 
7708
  bool first= true;
 
7709
  QUICK_RANGE_SELECT *quick;
 
7710
 
 
7711
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
7712
  while ((quick= it++))
 
7713
  {
 
7714
    if (first)
 
7715
      first= false;
 
7716
    else
 
7717
    {
 
7718
      key_names->append(',');
 
7719
      used_lengths->append(',');
 
7720
    }
 
7721
 
 
7722
    KEY *key_info= head->key_info + quick->index;
 
7723
    key_names->append(key_info->name);
 
7724
    length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
 
7725
    used_lengths->append(buf, length);
 
7726
  }
 
7727
  if (pk_quick_select)
 
7728
  {
 
7729
    KEY *key_info= head->key_info + pk_quick_select->index;
 
7730
    key_names->append(',');
 
7731
    key_names->append(key_info->name);
 
7732
    length= longlong2str(pk_quick_select->max_used_key_length, buf, 10) - buf;
 
7733
    used_lengths->append(',');
 
7734
    used_lengths->append(buf, length);
 
7735
  }
 
7736
}
 
7737
 
 
7738
void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
 
7739
                                                      String *used_lengths)
 
7740
{
 
7741
  char buf[64];
 
7742
  uint length;
 
7743
  bool first= true;
 
7744
  QUICK_RANGE_SELECT *quick;
 
7745
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
7746
  while ((quick= it++))
 
7747
  {
 
7748
    KEY *key_info= head->key_info + quick->index;
 
7749
    if (first)
 
7750
      first= false;
 
7751
    else
 
7752
    {
 
7753
      key_names->append(',');
 
7754
      used_lengths->append(',');
 
7755
    }
 
7756
    key_names->append(key_info->name);
 
7757
    length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
 
7758
    used_lengths->append(buf, length);
 
7759
  }
 
7760
 
 
7761
  if (cpk_quick)
 
7762
  {
 
7763
    KEY *key_info= head->key_info + cpk_quick->index;
 
7764
    key_names->append(',');
 
7765
    key_names->append(key_info->name);
 
7766
    length= longlong2str(cpk_quick->max_used_key_length, buf, 10) - buf;
 
7767
    used_lengths->append(',');
 
7768
    used_lengths->append(buf, length);
 
7769
  }
 
7770
}
 
7771
 
 
7772
void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
 
7773
                                                  String *used_lengths)
 
7774
{
 
7775
  bool first= true;
 
7776
  QUICK_SELECT_I *quick;
 
7777
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
7778
  while ((quick= it++))
 
7779
  {
 
7780
    if (first)
 
7781
      first= false;
 
7782
    else
 
7783
    {
 
7784
      used_lengths->append(',');
 
7785
      key_names->append(',');
 
7786
    }
 
7787
    quick->add_keys_and_lengths(key_names, used_lengths);
 
7788
  }
 
7789
}
 
7790
 
 
7791
 
 
7792
/*******************************************************************************
 
7793
* Implementation of QUICK_GROUP_MIN_MAX_SELECT
 
7794
*******************************************************************************/
 
7795
 
 
7796
static inline uint get_field_keypart(KEY *index, Field *field);
 
7797
static inline SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree,
 
7798
                                             PARAM *param, uint *param_idx);
 
7799
static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
 
7800
                       KEY_PART_INFO *first_non_group_part,
 
7801
                       KEY_PART_INFO *min_max_arg_part,
 
7802
                       KEY_PART_INFO *last_part, THD *thd,
 
7803
                       uchar *key_infix, uint *key_infix_len,
 
7804
                       KEY_PART_INFO **first_non_infix_part);
 
7805
static bool
 
7806
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
 
7807
                               Field::imagetype image_type);
4476
7808
 
4477
7809
static void
4478
 
cost_group_min_max(Table* table,
4479
 
                   KeyInfo *index_info,
4480
 
                   uint32_t used_key_parts,
4481
 
                   uint32_t group_key_parts,
4482
 
                   optimizer::SEL_TREE *range_tree,
4483
 
                   optimizer::SEL_ARG *index_tree,
4484
 
                   ha_rows quick_prefix_records,
4485
 
                   bool have_min,
4486
 
                   bool have_max,
4487
 
                   double *read_cost,
4488
 
                   ha_rows *records);
 
7810
cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
 
7811
                   uint group_key_parts, SEL_TREE *range_tree,
 
7812
                   SEL_ARG *index_tree, ha_rows quick_prefix_records,
 
7813
                   bool have_min, bool have_max,
 
7814
                   double *read_cost, ha_rows *records);
4489
7815
 
4490
7816
 
4491
7817
/*
4498
7824
    sel_tree Range tree generated by get_mm_tree
4499
7825
 
4500
7826
  DESCRIPTION
4501
 
    Test whether a query can be computed via a QuickGroupMinMaxSelect.
4502
 
    Queries computable via a QuickGroupMinMaxSelect must satisfy the
 
7827
    Test whether a query can be computed via a QUICK_GROUP_MIN_MAX_SELECT.
 
7828
    Queries computable via a QUICK_GROUP_MIN_MAX_SELECT must satisfy the
4503
7829
    following conditions:
4504
7830
    A) Table T has at least one compound index I of the form:
4505
7831
       I = <A_1, ...,A_k, [B_1,..., B_m], C, [D_1,...,D_n]>
4516
7842
        - NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
4517
7843
          GROUP BY and not referenced by MIN/MAX functions.
4518
7844
        with the following properties specified below.
4519
 
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not
 
7845
    B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not 
4520
7846
        applicable.
4521
7847
 
4522
7848
    SA1. There is at most one attribute in SA referenced by any number of
4583
7909
  NOTES
4584
7910
    If the current query satisfies the conditions above, and if
4585
7911
    (mem_root! = NULL), then the function constructs and returns a new TRP
4586
 
    object, that is later used to construct a new QuickGroupMinMaxSelect.
 
7912
    object, that is later used to construct a new QUICK_GROUP_MIN_MAX_SELECT.
4587
7913
    If (mem_root == NULL), then the function only tests whether the current
4588
7914
    query satisfies the conditions above, and, if so, sets
4589
7915
    is_applicable = true.
4604
7930
    other field as in: "select min(a) from t1 group by a" ?
4605
7931
  - We assume that the general correctness of the GROUP-BY query was checked
4606
7932
    before this point. Is this correct, or do we have to check it completely?
4607
 
  - Lift the limitation in condition (B3), that is, make this access method
 
7933
  - Lift the limitation in condition (B3), that is, make this access method 
4608
7934
    applicable to ROLLUP queries.
4609
7935
 
4610
7936
  RETURN
4611
7937
    If mem_root != NULL
4612
 
    - valid GroupMinMaxReadPlan object if this QUICK class can be used for
 
7938
    - valid TRP_GROUP_MIN_MAX object if this QUICK class can be used for
4613
7939
      the query
4614
7940
    -  NULL o/w.
4615
7941
    If mem_root == NULL
4616
7942
    - NULL
4617
7943
*/
4618
 
static optimizer::GroupMinMaxReadPlan *
4619
 
get_best_group_min_max(optimizer::Parameter *param, optimizer::SEL_TREE *tree)
 
7944
 
 
7945
static TRP_GROUP_MIN_MAX *
 
7946
get_best_group_min_max(PARAM *param, SEL_TREE *tree)
4620
7947
{
4621
 
  Session *session= param->session;
4622
 
  Join *join= session->lex->current_select->join;
4623
 
  Table *table= param->table;
 
7948
  THD *thd= param->thd;
 
7949
  JOIN *join= thd->lex->current_select->join;
 
7950
  TABLE *table= param->table;
4624
7951
  bool have_min= false;              /* true if there is a MIN function. */
4625
7952
  bool have_max= false;              /* true if there is a MAX function. */
4626
7953
  Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
4627
 
  KeyPartInfo *min_max_arg_part= NULL; /* The corresponding keypart. */
4628
 
  uint32_t group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
4629
 
  KeyInfo *index_info= NULL;    /* The index chosen for data access. */
4630
 
  uint32_t index= 0;            /* The id of the chosen index. */
4631
 
  uint32_t group_key_parts= 0;  // Number of index key parts in the group prefix.
4632
 
  uint32_t used_key_parts= 0;   /* Number of index key parts used for access. */
4633
 
  unsigned char key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
4634
 
  uint32_t key_infix_len= 0;          /* Length of key_infix. */
4635
 
  optimizer::GroupMinMaxReadPlan *read_plan= NULL; /* The eventually constructed TRP. */
4636
 
  uint32_t key_part_nr;
4637
 
  order_st *tmp_group= NULL;
4638
 
  Item *item= NULL;
4639
 
  Item_field *item_field= NULL;
 
7954
  KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
 
7955
  uint group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
 
7956
  KEY *index_info= NULL;    /* The index chosen for data access. */
 
7957
  uint index= 0;            /* The id of the chosen index. */
 
7958
  uint group_key_parts= 0;  // Number of index key parts in the group prefix.
 
7959
  uint used_key_parts= 0;   /* Number of index key parts used for access. */
 
7960
  uchar key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
 
7961
  uint key_infix_len= 0;          /* Length of key_infix. */
 
7962
  TRP_GROUP_MIN_MAX *read_plan= NULL; /* The eventually constructed TRP. */
 
7963
  uint key_part_nr;
 
7964
  ORDER *tmp_group;
 
7965
  Item *item;
 
7966
  Item_field *item_field;
 
7967
  DBUG_ENTER("get_best_group_min_max");
4640
7968
 
4641
7969
  /* Perform few 'cheap' tests whether this access method is applicable. */
4642
 
  if (! join)
4643
 
    return NULL;        /* This is not a select statement. */
4644
 
 
 
7970
  if (!join)
 
7971
    DBUG_RETURN(NULL);        /* This is not a select statement. */
4645
7972
  if ((join->tables != 1) ||  /* The query must reference one table. */
4646
 
      ((! join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
4647
 
       (! join->select_distinct)) ||
 
7973
      ((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
 
7974
       (!join->select_distinct)) ||
4648
7975
      (join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
4649
 
    return NULL;
4650
 
  if (table->getShare()->sizeKeys() == 0)        /* There are no indexes to use. */
4651
 
    return NULL;
 
7976
    DBUG_RETURN(NULL);
 
7977
  if (table->s->keys == 0)        /* There are no indexes to use. */
 
7978
    DBUG_RETURN(NULL);
4652
7979
 
4653
7980
  /* Analyze the query in more detail. */
4654
7981
  List_iterator<Item> select_items_it(join->fields_list);
4655
7982
 
4656
7983
  /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
4657
7984
  if (join->make_sum_func_list(join->all_fields, join->fields_list, 1))
4658
 
    return NULL;
4659
 
 
 
7985
    DBUG_RETURN(NULL);
4660
7986
  if (join->sum_funcs[0])
4661
7987
  {
4662
 
    Item_sum *min_max_item= NULL;
 
7988
    Item_sum *min_max_item;
4663
7989
    Item_sum **func_ptr= join->sum_funcs;
4664
7990
    while ((min_max_item= *(func_ptr++)))
4665
7991
    {
4668
7994
      else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
4669
7995
        have_max= true;
4670
7996
      else
4671
 
        return NULL;
 
7997
        DBUG_RETURN(NULL);
4672
7998
 
4673
7999
      /* The argument of MIN/MAX. */
4674
 
      Item *expr= min_max_item->args[0]->real_item();
 
8000
      Item *expr= min_max_item->args[0]->real_item();    
4675
8001
      if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
4676
8002
      {
4677
8003
        if (! min_max_arg_item)
4678
8004
          min_max_arg_item= (Item_field*) expr;
4679
8005
        else if (! min_max_arg_item->eq(expr, 1))
4680
 
          return NULL;
 
8006
          DBUG_RETURN(NULL);
4681
8007
      }
4682
8008
      else
4683
 
        return NULL;
 
8009
        DBUG_RETURN(NULL);
4684
8010
    }
4685
8011
  }
4686
8012
 
4690
8016
    while ((item= select_items_it++))
4691
8017
    {
4692
8018
      if (item->type() != Item::FIELD_ITEM)
4693
 
        return NULL;
 
8019
        DBUG_RETURN(NULL);
4694
8020
    }
4695
8021
  }
4696
8022
 
4698
8024
  for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
4699
8025
  {
4700
8026
    if ((*tmp_group->item)->type() != Item::FIELD_ITEM)
4701
 
      return NULL;
 
8027
      DBUG_RETURN(NULL);
4702
8028
  }
4703
8029
 
4704
8030
  /*
4706
8032
    (GA1,GA2) are all true. If there is more than one such index, select the
4707
8033
    first one. Here we set the variables: group_prefix_len and index_info.
4708
8034
  */
4709
 
  KeyInfo *cur_index_info= table->key_info;
4710
 
  KeyInfo *cur_index_info_end= cur_index_info + table->getShare()->sizeKeys();
4711
 
  KeyPartInfo *cur_part= NULL;
4712
 
  KeyPartInfo *end_part= NULL; /* Last part for loops. */
 
8035
  KEY *cur_index_info= table->key_info;
 
8036
  KEY *cur_index_info_end= cur_index_info + table->s->keys;
 
8037
  KEY_PART_INFO *cur_part= NULL;
 
8038
  KEY_PART_INFO *end_part; /* Last part for loops. */
4713
8039
  /* Last index part. */
4714
 
  KeyPartInfo *last_part= NULL;
4715
 
  KeyPartInfo *first_non_group_part= NULL;
4716
 
  KeyPartInfo *first_non_infix_part= NULL;
4717
 
  uint32_t key_infix_parts= 0;
4718
 
  uint32_t cur_group_key_parts= 0;
4719
 
  uint32_t cur_group_prefix_len= 0;
 
8040
  KEY_PART_INFO *last_part= NULL;
 
8041
  KEY_PART_INFO *first_non_group_part= NULL;
 
8042
  KEY_PART_INFO *first_non_infix_part= NULL;
 
8043
  uint key_infix_parts= 0;
 
8044
  uint cur_group_key_parts= 0;
 
8045
  uint cur_group_prefix_len= 0;
4720
8046
  /* Cost-related variables for the best index so far. */
4721
8047
  double best_read_cost= DBL_MAX;
4722
8048
  ha_rows best_records= 0;
4723
 
  optimizer::SEL_ARG *best_index_tree= NULL;
 
8049
  SEL_ARG *best_index_tree= NULL;
4724
8050
  ha_rows best_quick_prefix_records= 0;
4725
 
  uint32_t best_param_idx= 0;
 
8051
  uint best_param_idx= 0;
4726
8052
  double cur_read_cost= DBL_MAX;
4727
8053
  ha_rows cur_records;
4728
 
  optimizer::SEL_ARG *cur_index_tree= NULL;
 
8054
  SEL_ARG *cur_index_tree= NULL;
4729
8055
  ha_rows cur_quick_prefix_records= 0;
4730
 
  uint32_t cur_param_idx= MAX_KEY;
4731
 
  key_map used_key_parts_map;
4732
 
  uint32_t cur_key_infix_len= 0;
4733
 
  unsigned char cur_key_infix[MAX_KEY_LENGTH];
4734
 
  uint32_t cur_used_key_parts= 0;
4735
 
  uint32_t pk= param->table->getShare()->getPrimaryKey();
 
8056
  uint cur_param_idx=MAX_KEY;
 
8057
  key_map cur_used_key_parts;
 
8058
  uint pk= param->table->s->primary_key;
4736
8059
 
4737
 
  for (uint32_t cur_index= 0;
4738
 
       cur_index_info != cur_index_info_end;
 
8060
  for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
4739
8061
       cur_index_info++, cur_index++)
4740
8062
  {
4741
8063
    /* Check (B1) - if current index is covering. */
4742
 
    if (! table->covering_keys.test(cur_index))
 
8064
    if (!table->covering_keys.is_set(cur_index))
4743
8065
      goto next_index;
4744
8066
 
4745
8067
    /*
4752
8074
      we check that all query fields are indeed covered by 'cur_index'.
4753
8075
    */
4754
8076
    if (pk < MAX_KEY && cur_index != pk &&
4755
 
        (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)))
 
8077
        (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
4756
8078
    {
4757
8079
      /* For each table field */
4758
 
      for (uint32_t i= 0; i < table->getShare()->sizeFields(); i++)
 
8080
      for (uint i= 0; i < table->s->fields; i++)
4759
8081
      {
4760
 
        Field *cur_field= table->getField(i);
 
8082
        Field *cur_field= table->field[i];
4761
8083
        /*
4762
8084
          If the field is used in the current query ensure that it's
4763
8085
          part of 'cur_index'
4764
8086
        */
4765
 
        if ((cur_field->isReadSet()) &&
4766
 
            ! cur_field->part_of_key_not_clustered.test(cur_index))
 
8087
        if (bitmap_is_set(table->read_set, cur_field->field_index) &&
 
8088
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
4767
8089
          goto next_index;                  // Field was not part of key
4768
8090
      }
4769
8091
    }
4776
8098
      cur_part= cur_index_info->key_part;
4777
8099
      end_part= cur_part + cur_index_info->key_parts;
4778
8100
      /* Iterate in parallel over the GROUP list and the index parts. */
4779
 
      for (tmp_group= join->group_list;
4780
 
           tmp_group && (cur_part != end_part);
 
8101
      for (tmp_group= join->group_list; tmp_group && (cur_part != end_part);
4781
8102
           tmp_group= tmp_group->next, cur_part++)
4782
8103
      {
4783
8104
        /*
4786
8107
          first Item? If so, then why? What is the array for?
4787
8108
        */
4788
8109
        /* Above we already checked that all group items are fields. */
4789
 
        assert((*tmp_group->item)->type() == Item::FIELD_ITEM);
 
8110
        DBUG_ASSERT((*tmp_group->item)->type() == Item::FIELD_ITEM);
4790
8111
        Item_field *group_field= (Item_field *) (*tmp_group->item);
4791
8112
        if (group_field->field->eq(cur_part->field))
4792
8113
        {
4799
8120
    }
4800
8121
    /*
4801
8122
      Check (GA2) if this is a DISTINCT query.
4802
 
      If GA2, then Store a new order_st object in group_fields_array at the
4803
 
      position of the key part of item_field->field. Thus we get the order_st
 
8123
      If GA2, then Store a new ORDER object in group_fields_array at the
 
8124
      position of the key part of item_field->field. Thus we get the ORDER
4804
8125
      objects for each field ordered as the corresponding key parts.
4805
 
      Later group_fields_array of order_st objects is used to convert the query
 
8126
      Later group_fields_array of ORDER objects is used to convert the query
4806
8127
      to a GROUP query.
4807
8128
    */
4808
8129
    else if (join->select_distinct)
4809
8130
    {
4810
8131
      select_items_it.rewind();
4811
 
      used_key_parts_map.reset();
4812
 
      uint32_t max_key_part= 0;
 
8132
      cur_used_key_parts.clear_all();
 
8133
      uint max_key_part= 0;
4813
8134
      while ((item= select_items_it++))
4814
8135
      {
4815
8136
        item_field= (Item_field*) item; /* (SA5) already checked above. */
4819
8140
          Check if this attribute was already present in the select list.
4820
8141
          If it was present, then its corresponding key part was alredy used.
4821
8142
        */
4822
 
        if (used_key_parts_map.test(key_part_nr))
 
8143
        if (cur_used_key_parts.is_set(key_part_nr))
4823
8144
          continue;
4824
8145
        if (key_part_nr < 1 || key_part_nr > join->fields_list.elements)
4825
8146
          goto next_index;
4826
8147
        cur_part= cur_index_info->key_part + key_part_nr - 1;
4827
8148
        cur_group_prefix_len+= cur_part->store_length;
4828
 
        used_key_parts_map.set(key_part_nr);
 
8149
        cur_used_key_parts.set_bit(key_part_nr);
4829
8150
        ++cur_group_key_parts;
4830
8151
        max_key_part= max(max_key_part,key_part_nr);
4831
8152
      }
4835
8156
        all_parts have all bits set from 0 to (max_key_part-1).
4836
8157
        cur_parts have bits set for only used keyparts.
4837
8158
      */
4838
 
      key_map all_parts;
4839
 
      key_map cur_parts;
4840
 
      for (uint32_t pos= 0; pos < max_key_part; pos++)
4841
 
        all_parts.set(pos);
4842
 
      cur_parts= used_key_parts_map >> 1;
 
8159
      ulonglong all_parts, cur_parts;
 
8160
      all_parts= (1<<max_key_part) - 1;
 
8161
      cur_parts= cur_used_key_parts.to_ulonglong() >> 1;
4843
8162
      if (all_parts != cur_parts)
4844
8163
        goto next_index;
4845
8164
    }
4846
8165
    else
4847
 
      assert(false);
 
8166
      DBUG_ASSERT(false);
4848
8167
 
4849
8168
    /* Check (SA2). */
4850
8169
    if (min_max_arg_item)
4880
8199
                             NULL :
4881
8200
                           NULL;
4882
8201
    if (first_non_group_part &&
4883
 
        (! min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
 
8202
        (!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
4884
8203
    {
4885
8204
      if (tree)
4886
8205
      {
4887
 
        uint32_t dummy;
4888
 
        optimizer::SEL_ARG *index_range_tree= get_index_range_tree(cur_index,
4889
 
                                                                   tree,
4890
 
                                                                   param,
4891
 
                                                                   &dummy);
4892
 
        if (! get_constant_key_infix(cur_index_info,
4893
 
                                     index_range_tree,
4894
 
                                     first_non_group_part,
4895
 
                                     min_max_arg_part,
4896
 
                                     last_part,
4897
 
                                     session,
4898
 
                                     cur_key_infix,
4899
 
                                     &cur_key_infix_len,
4900
 
                                     &first_non_infix_part))
4901
 
        {
 
8206
        uint dummy;
 
8207
        SEL_ARG *index_range_tree= get_index_range_tree(cur_index, tree, param,
 
8208
                                                        &dummy);
 
8209
        if (!get_constant_key_infix(cur_index_info, index_range_tree,
 
8210
                                    first_non_group_part, min_max_arg_part,
 
8211
                                    last_part, thd, key_infix, &key_infix_len,
 
8212
                                    &first_non_infix_part))
4902
8213
          goto next_index;
4903
 
        }
4904
8214
      }
4905
8215
      else if (min_max_arg_part &&
4906
8216
               (min_max_arg_part - first_non_group_part > 0))
4925
8235
          Store the first and last keyparts that need to be analyzed
4926
8236
          into one array that can be passed as parameter.
4927
8237
        */
4928
 
        KeyPartInfo *key_part_range[2];
 
8238
        KEY_PART_INFO *key_part_range[2];
4929
8239
        key_part_range[0]= first_non_group_part;
4930
8240
        key_part_range[1]= last_part;
4931
8241
 
4932
8242
        /* Check if cur_part is referenced in the WHERE clause. */
4933
 
        if (join->conds->walk(&Item::find_item_in_field_list_processor,
4934
 
                              0,
4935
 
                              (unsigned char*) key_part_range))
 
8243
        if (join->conds->walk(&Item::find_item_in_field_list_processor, 0,
 
8244
                              (uchar*) key_part_range))
4936
8245
          goto next_index;
4937
8246
      }
4938
8247
    }
4947
8256
                (min_max_arg_part && (min_max_arg_part < last_part));
4948
8257
      for (; cur_part != last_part; cur_part++)
4949
8258
      {
4950
 
        if (cur_part->field->isReadSet())
 
8259
        if (bitmap_is_set(table->read_set, cur_part->field->field_index))
4951
8260
          goto next_index;
4952
8261
      }
4953
8262
    }
4954
8263
 
4955
8264
    /* If we got to this point, cur_index_info passes the test. */
4956
 
    key_infix_parts= cur_key_infix_len ?
 
8265
    key_infix_parts= key_infix_len ?
4957
8266
                     (first_non_infix_part - first_non_group_part) : 0;
4958
 
    cur_used_key_parts= cur_group_key_parts + key_infix_parts;
 
8267
    used_key_parts= cur_group_key_parts + key_infix_parts;
4959
8268
 
4960
8269
    /* Compute the cost of using this index. */
4961
8270
    if (tree)
4962
8271
    {
4963
8272
      /* Find the SEL_ARG sub-tree that corresponds to the chosen index. */
4964
 
      cur_index_tree= get_index_range_tree(cur_index,
4965
 
                                           tree,
4966
 
                                           param,
 
8273
      cur_index_tree= get_index_range_tree(cur_index, tree, param,
4967
8274
                                           &cur_param_idx);
4968
8275
      /* Check if this range tree can be used for prefix retrieval. */
4969
 
      optimizer::CostVector dummy_cost;
4970
 
      uint32_t mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
4971
 
      uint32_t mrr_bufsize= 0;
4972
 
      cur_quick_prefix_records= check_quick_select(session,
4973
 
                                                   param,
4974
 
                                                   cur_param_idx,
4975
 
                                                   false /*don't care*/,
4976
 
                                                   cur_index_tree,
4977
 
                                                   true,
4978
 
                                                   &mrr_flags,
4979
 
                                                   &mrr_bufsize,
 
8276
      COST_VECT dummy_cost;
 
8277
      uint mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
 
8278
      uint mrr_bufsize=0;
 
8279
      cur_quick_prefix_records= check_quick_select(param, cur_param_idx, 
 
8280
                                                   false /*don't care*/, 
 
8281
                                                   cur_index_tree, true,
 
8282
                                                   &mrr_flags, &mrr_bufsize,
4980
8283
                                                   &dummy_cost);
4981
8284
    }
4982
 
    cost_group_min_max(table,
4983
 
                       cur_index_info,
4984
 
                       cur_used_key_parts,
4985
 
                       cur_group_key_parts,
4986
 
                       tree,
4987
 
                       cur_index_tree,
4988
 
                       cur_quick_prefix_records,
4989
 
                       have_min,
4990
 
                       have_max,
4991
 
                       &cur_read_cost,
4992
 
                       &cur_records);
 
8285
    cost_group_min_max(table, cur_index_info, used_key_parts,
 
8286
                       cur_group_key_parts, tree, cur_index_tree,
 
8287
                       cur_quick_prefix_records, have_min, have_max,
 
8288
                       &cur_read_cost, &cur_records);
4993
8289
    /*
4994
8290
      If cur_read_cost is lower than best_read_cost use cur_index.
4995
8291
      Do not compare doubles directly because they may have different
4997
8293
    */
4998
8294
    if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
4999
8295
    {
5000
 
      assert(tree != 0 || cur_param_idx == MAX_KEY);
 
8296
      DBUG_ASSERT(tree != 0 || cur_param_idx == MAX_KEY);
5001
8297
      index_info= cur_index_info;
5002
8298
      index= cur_index;
5003
8299
      best_read_cost= cur_read_cost;
5007
8303
      best_param_idx= cur_param_idx;
5008
8304
      group_key_parts= cur_group_key_parts;
5009
8305
      group_prefix_len= cur_group_prefix_len;
5010
 
      key_infix_len= cur_key_infix_len;
5011
 
 
5012
 
      if (key_infix_len)
5013
 
      {
5014
 
        memcpy(key_infix, cur_key_infix, sizeof (key_infix));
5015
 
      }
5016
 
 
5017
 
      used_key_parts= cur_used_key_parts;
5018
8306
    }
5019
8307
 
5020
8308
  next_index:
5021
8309
    cur_group_key_parts= 0;
5022
8310
    cur_group_prefix_len= 0;
5023
 
    cur_key_infix_len= 0;
5024
8311
  }
5025
 
  if (! index_info) /* No usable index found. */
5026
 
    return NULL;
 
8312
  if (!index_info) /* No usable index found. */
 
8313
    DBUG_RETURN(NULL);
5027
8314
 
5028
8315
  /* Check (SA3) for the where clause. */
5029
8316
  if (join->conds && min_max_arg_item &&
5030
 
      ! check_group_min_max_predicates(join->conds, min_max_arg_item))
5031
 
    return NULL;
 
8317
      !check_group_min_max_predicates(join->conds, min_max_arg_item,
 
8318
                                      (index_info->flags & HA_SPATIAL) ?
 
8319
                                      Field::itMBR : Field::itRAW))
 
8320
    DBUG_RETURN(NULL);
5032
8321
 
5033
8322
  /* The query passes all tests, so construct a new TRP object. */
5034
 
  read_plan=
5035
 
    new(param->mem_root) optimizer::GroupMinMaxReadPlan(have_min,
5036
 
                                                        have_max,
5037
 
                                                        min_max_arg_part,
5038
 
                                                        group_prefix_len,
5039
 
                                                        used_key_parts,
5040
 
                                                        group_key_parts,
5041
 
                                                        index_info,
5042
 
                                                        index,
5043
 
                                                        key_infix_len,
5044
 
                                                        (key_infix_len > 0) ? key_infix : NULL,
5045
 
                                                        tree,
5046
 
                                                        best_index_tree,
5047
 
                                                        best_param_idx,
5048
 
                                                        best_quick_prefix_records);
 
8323
  read_plan= new (param->mem_root)
 
8324
                 TRP_GROUP_MIN_MAX(have_min, have_max, min_max_arg_part,
 
8325
                                   group_prefix_len, used_key_parts,
 
8326
                                   group_key_parts, index_info, index,
 
8327
                                   key_infix_len,
 
8328
                                   (key_infix_len > 0) ? key_infix : NULL,
 
8329
                                   tree, best_index_tree, best_param_idx,
 
8330
                                   best_quick_prefix_records);
5049
8331
  if (read_plan)
5050
8332
  {
5051
8333
    if (tree && read_plan->quick_prefix_records == 0)
5052
 
      return NULL;
 
8334
      DBUG_RETURN(NULL);
5053
8335
 
5054
8336
    read_plan->read_cost= best_read_cost;
5055
 
    read_plan->records= best_records;
 
8337
    read_plan->records=   best_records;
 
8338
 
 
8339
    DBUG_PRINT("info",
 
8340
               ("Returning group min/max plan: cost: %g, records: %lu",
 
8341
                read_plan->read_cost, (ulong) read_plan->records));
5056
8342
  }
5057
8343
 
5058
 
  return read_plan;
 
8344
  DBUG_RETURN(read_plan);
5059
8345
}
5060
8346
 
5061
8347
 
5080
8366
    true  if cond passes the test
5081
8367
    false o/w
5082
8368
*/
5083
 
static bool check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item)
 
8369
 
 
8370
static bool
 
8371
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
 
8372
                               Field::imagetype image_type)
5084
8373
{
5085
 
  assert(cond && min_max_arg_item);
 
8374
  DBUG_ENTER("check_group_min_max_predicates");
 
8375
  DBUG_ASSERT(cond && min_max_arg_item);
5086
8376
 
5087
8377
  cond= cond->real_item();
5088
8378
  Item::Type cond_type= cond->type();
5089
8379
  if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
5090
8380
  {
 
8381
    DBUG_PRINT("info", ("Analyzing: %s", ((Item_func*) cond)->func_name()));
5091
8382
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
5092
 
    Item *and_or_arg= NULL;
 
8383
    Item *and_or_arg;
5093
8384
    while ((and_or_arg= li++))
5094
8385
    {
5095
 
      if (! check_group_min_max_predicates(and_or_arg, min_max_arg_item))
5096
 
        return false;
 
8386
      if (!check_group_min_max_predicates(and_or_arg, min_max_arg_item,
 
8387
                                         image_type))
 
8388
        DBUG_RETURN(false);
5097
8389
    }
5098
 
    return true;
 
8390
    DBUG_RETURN(true);
5099
8391
  }
5100
8392
 
5101
8393
  /*
5108
8400
    so.
5109
8401
  */
5110
8402
  if (cond_type == Item::SUBSELECT_ITEM)
5111
 
    return false;
5112
 
 
 
8403
    DBUG_RETURN(false);
 
8404
  
5113
8405
  /* We presume that at this point there are no other Items than functions. */
5114
 
  assert(cond_type == Item::FUNC_ITEM);
 
8406
  DBUG_ASSERT(cond_type == Item::FUNC_ITEM);
5115
8407
 
5116
8408
  /* Test if cond references only group-by or non-group fields. */
5117
8409
  Item_func *pred= (Item_func*) cond;
5118
8410
  Item **arguments= pred->arguments();
5119
 
  Item *cur_arg= NULL;
5120
 
  for (uint32_t arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
 
8411
  Item *cur_arg;
 
8412
  DBUG_PRINT("info", ("Analyzing: %s", pred->func_name()));
 
8413
  for (uint arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
5121
8414
  {
5122
8415
    cur_arg= arguments[arg_idx]->real_item();
 
8416
    DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
5123
8417
    if (cur_arg->type() == Item::FIELD_ITEM)
5124
8418
    {
5125
 
      if (min_max_arg_item->eq(cur_arg, 1))
 
8419
      if (min_max_arg_item->eq(cur_arg, 1)) 
5126
8420
      {
5127
8421
       /*
5128
8422
         If pred references the MIN/MAX argument, check whether pred is a range
5139
8433
            pred_type != Item_func::ISNOTNULL_FUNC &&
5140
8434
            pred_type != Item_func::EQ_FUNC        &&
5141
8435
            pred_type != Item_func::NE_FUNC)
5142
 
          return false;
 
8436
          DBUG_RETURN(false);
5143
8437
 
5144
8438
        /* Check that pred compares min_max_arg_item with a constant. */
5145
8439
        Item *args[3];
5146
 
        memset(args, 0, 3 * sizeof(Item*));
5147
 
        bool inv= false;
 
8440
        bzero(args, 3 * sizeof(Item*));
 
8441
        bool inv;
5148
8442
        /* Test if this is a comparison of a field and a constant. */
5149
 
        if (! optimizer::simple_pred(pred, args, inv))
5150
 
          return false;
 
8443
        if (!simple_pred(pred, args, &inv))
 
8444
          DBUG_RETURN(false);
5151
8445
 
5152
8446
        /* Check for compatible string comparisons - similar to get_mm_leaf. */
5153
8447
        if (args[0] && args[1] && !args[2] && // this is a binary function
5156
8450
              Don't use an index when comparing strings of different collations.
5157
8451
            */
5158
8452
            ((args[1]->result_type() == STRING_RESULT &&
 
8453
              image_type == Field::itRAW &&
5159
8454
              ((Field_str*) min_max_arg_item->field)->charset() !=
5160
8455
              pred->compare_collation())
5161
8456
             ||
5165
8460
             */
5166
8461
             (args[1]->result_type() != STRING_RESULT &&
5167
8462
              min_max_arg_item->field->cmp_type() != args[1]->result_type())))
5168
 
        {
5169
 
          return false;
5170
 
        }
 
8463
          DBUG_RETURN(false);
5171
8464
      }
5172
8465
    }
5173
8466
    else if (cur_arg->type() == Item::FUNC_ITEM)
5174
8467
    {
5175
 
      if (! check_group_min_max_predicates(cur_arg, min_max_arg_item))
5176
 
        return false;
 
8468
      if (!check_group_min_max_predicates(cur_arg, min_max_arg_item,
 
8469
                                         image_type))
 
8470
        DBUG_RETURN(false);
5177
8471
    }
5178
8472
    else if (cur_arg->const_item())
5179
8473
    {
5180
 
      return true;
 
8474
      DBUG_RETURN(true);
5181
8475
    }
5182
8476
    else
5183
 
      return false;
 
8477
      DBUG_RETURN(false);
5184
8478
  }
5185
8479
 
5186
 
  return true;
 
8480
  DBUG_RETURN(true);
5187
8481
}
5188
8482
 
5189
8483
 
5197
8491
    first_non_group_part   [in]  First index part after group attribute parts
5198
8492
    min_max_arg_part       [in]  The keypart of the MIN/MAX argument if any
5199
8493
    last_part              [in]  Last keypart of the index
5200
 
    session                    [in]  Current thread
 
8494
    thd                    [in]  Current thread
5201
8495
    key_infix              [out] Infix of constants to be used for index lookup
5202
8496
    key_infix_len          [out] Lenghth of the infix
5203
8497
    first_non_infix_part   [out] The first keypart after the infix (if any)
5204
 
 
 
8498
    
5205
8499
  DESCRIPTION
5206
8500
    Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
5207
8501
    for each keypart field NGF_i not in GROUP-BY, check that there is a
5217
8511
    true  if the index passes the test
5218
8512
    false o/w
5219
8513
*/
 
8514
 
5220
8515
static bool
5221
 
get_constant_key_infix(KeyInfo *,
5222
 
                       optimizer::SEL_ARG *index_range_tree,
5223
 
                       KeyPartInfo *first_non_group_part,
5224
 
                       KeyPartInfo *min_max_arg_part,
5225
 
                       KeyPartInfo *last_part,
5226
 
                       Session *,
5227
 
                       unsigned char *key_infix,
5228
 
                       uint32_t *key_infix_len,
5229
 
                       KeyPartInfo **first_non_infix_part)
 
8516
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
 
8517
                       KEY_PART_INFO *first_non_group_part,
 
8518
                       KEY_PART_INFO *min_max_arg_part,
 
8519
                       KEY_PART_INFO *last_part, THD *thd,
 
8520
                       uchar *key_infix, uint *key_infix_len,
 
8521
                       KEY_PART_INFO **first_non_infix_part)
5230
8522
{
5231
 
  optimizer::SEL_ARG *cur_range= NULL;
5232
 
  KeyPartInfo *cur_part= NULL;
 
8523
  SEL_ARG       *cur_range;
 
8524
  KEY_PART_INFO *cur_part;
5233
8525
  /* End part for the first loop below. */
5234
 
  KeyPartInfo *end_part= min_max_arg_part ? min_max_arg_part : last_part;
 
8526
  KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
5235
8527
 
5236
8528
  *key_infix_len= 0;
5237
 
  unsigned char *key_ptr= key_infix;
 
8529
  uchar *key_ptr= key_infix;
5238
8530
  for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
5239
8531
  {
5240
8532
    /*
5247
8539
      if (cur_range->field->eq(cur_part->field))
5248
8540
        break;
5249
8541
    }
5250
 
    if (! cur_range)
 
8542
    if (!cur_range)
5251
8543
    {
5252
8544
      if (min_max_arg_part)
5253
8545
        return false; /* The current keypart has no range predicates at all. */
5263
8555
      return false; /* This is not the only range predicate for the field. */
5264
8556
    if ((cur_range->min_flag & NO_MIN_RANGE) ||
5265
8557
        (cur_range->max_flag & NO_MAX_RANGE) ||
5266
 
        (cur_range->min_flag & NEAR_MIN) ||
5267
 
        (cur_range->max_flag & NEAR_MAX))
 
8558
        (cur_range->min_flag & NEAR_MIN) || (cur_range->max_flag & NEAR_MAX))
5268
8559
      return false;
5269
8560
 
5270
 
    uint32_t field_length= cur_part->store_length;
 
8561
    uint field_length= cur_part->store_length;
5271
8562
    if ((cur_range->maybe_null &&
5272
8563
         cur_range->min_value[0] && cur_range->max_value[0]) ||
5273
8564
        !memcmp(cur_range->min_value, cur_range->max_value, field_length))
5297
8588
    field  field that possibly references some key part in index
5298
8589
 
5299
8590
  NOTES
5300
 
    The return value can be used to get a KeyPartInfo pointer by
 
8591
    The return value can be used to get a KEY_PART_INFO pointer by
5301
8592
    part= index->key_part + get_field_keypart(...) - 1;
5302
8593
 
5303
8594
  RETURN
5304
8595
    Positive number which is the consecutive number of the key part, or
5305
8596
    0 if field does not reference any index field.
5306
8597
*/
 
8598
 
5307
8599
static inline uint
5308
 
get_field_keypart(KeyInfo *index, Field *field)
 
8600
get_field_keypart(KEY *index, Field *field)
5309
8601
{
5310
 
  KeyPartInfo *part= NULL;
5311
 
  KeyPartInfo *end= NULL;
 
8602
  KEY_PART_INFO *part, *end;
5312
8603
 
5313
8604
  for (part= index->key_part, end= part + index->key_parts; part < end; part++)
5314
8605
  {
5326
8617
    get_index_range_tree()
5327
8618
    index     [in]  The ID of the index being looked for
5328
8619
    range_tree[in]  Tree of ranges being searched
5329
 
    param     [in]  Parameter from SqlSelect::test_quick_select
5330
 
    param_idx [out] Index in the array Parameter::key that corresponds to 'index'
 
8620
    param     [in]  PARAM from SQL_SELECT::test_quick_select
 
8621
    param_idx [out] Index in the array PARAM::key that corresponds to 'index'
5331
8622
 
5332
8623
  DESCRIPTION
5333
8624
 
5334
 
    A optimizer::SEL_TREE contains range trees for all usable indexes. This procedure
5335
 
    finds the SEL_ARG sub-tree for 'index'. The members of a optimizer::SEL_TREE are
5336
 
    ordered in the same way as the members of Parameter::key, thus we first find
5337
 
    the corresponding index in the array Parameter::key. This index is returned
 
8625
    A SEL_TREE contains range trees for all usable indexes. This procedure
 
8626
    finds the SEL_ARG sub-tree for 'index'. The members of a SEL_TREE are
 
8627
    ordered in the same way as the members of PARAM::key, thus we first find
 
8628
    the corresponding index in the array PARAM::key. This index is returned
5338
8629
    through the variable param_idx, to be used later as argument of
5339
8630
    check_quick_select().
5340
8631
 
5341
8632
  RETURN
5342
8633
    Pointer to the SEL_ARG subtree that corresponds to index.
5343
8634
*/
5344
 
optimizer::SEL_ARG *get_index_range_tree(uint32_t index,
5345
 
                                         optimizer::SEL_TREE* range_tree,
5346
 
                                         optimizer::Parameter *param,
5347
 
                                         uint32_t *param_idx)
 
8635
 
 
8636
SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree, PARAM *param,
 
8637
                               uint *param_idx)
5348
8638
{
5349
 
  uint32_t idx= 0; /* Index nr in param->key_parts */
 
8639
  uint idx= 0; /* Index nr in param->key_parts */
5350
8640
  while (idx < param->keys)
5351
8641
  {
5352
8642
    if (index == param->real_keynr[idx])
5354
8644
    idx++;
5355
8645
  }
5356
8646
  *param_idx= idx;
5357
 
  return range_tree->keys[idx];
 
8647
  return(range_tree->keys[idx]);
5358
8648
}
5359
8649
 
5360
8650
 
5377
8667
    records             [out] The number of rows retrieved
5378
8668
 
5379
8669
  DESCRIPTION
5380
 
    This method computes the access cost of a GroupMinMaxReadPlan instance and
 
8670
    This method computes the access cost of a TRP_GROUP_MIN_MAX instance and
5381
8671
    the number of rows returned. It updates this->read_cost and this->records.
5382
8672
 
5383
8673
  NOTES
5417
8707
  RETURN
5418
8708
    None
5419
8709
*/
5420
 
void cost_group_min_max(Table* table,
5421
 
                        KeyInfo *index_info,
5422
 
                        uint32_t used_key_parts,
5423
 
                        uint32_t group_key_parts,
5424
 
                        optimizer::SEL_TREE *range_tree,
5425
 
                        optimizer::SEL_ARG *,
5426
 
                        ha_rows quick_prefix_records,
5427
 
                        bool have_min,
5428
 
                        bool have_max,
5429
 
                        double *read_cost,
5430
 
                        ha_rows *records)
 
8710
 
 
8711
void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
 
8712
                        uint group_key_parts, SEL_TREE *range_tree,
 
8713
                        SEL_ARG *index_tree, ha_rows quick_prefix_records,
 
8714
                        bool have_min, bool have_max,
 
8715
                        double *read_cost, ha_rows *records)
5431
8716
{
5432
8717
  ha_rows table_records;
5433
 
  uint32_t num_groups;
5434
 
  uint32_t num_blocks;
5435
 
  uint32_t keys_per_block;
5436
 
  uint32_t keys_per_group;
5437
 
  uint32_t keys_per_subgroup; /* Average number of keys in sub-groups */
 
8718
  uint num_groups;
 
8719
  uint num_blocks;
 
8720
  uint keys_per_block;
 
8721
  uint keys_per_group;
 
8722
  uint keys_per_subgroup; /* Average number of keys in sub-groups */
5438
8723
                          /* formed by a key infix. */
5439
8724
  double p_overlap; /* Probability that a sub-group overlaps two blocks. */
5440
8725
  double quick_prefix_selectivity;
5441
8726
  double io_cost;
5442
8727
  double cpu_cost= 0; /* TODO: CPU cost of index_read calls? */
 
8728
  DBUG_ENTER("cost_group_min_max");
5443
8729
 
5444
 
  table_records= table->cursor->stats.records;
5445
 
  keys_per_block= (table->cursor->stats.block_size / 2 /
5446
 
                   (index_info->key_length + table->cursor->ref_length)
 
8730
  table_records= table->file->stats.records;
 
8731
  keys_per_block= (table->file->stats.block_size / 2 /
 
8732
                   (index_info->key_length + table->file->ref_length)
5447
8733
                        + 1);
5448
 
  num_blocks= (uint32_t) (table_records / keys_per_block) + 1;
 
8734
  num_blocks= (uint)(table_records / keys_per_block) + 1;
5449
8735
 
5450
8736
  /* Compute the number of keys in a group. */
5451
8737
  keys_per_group= index_info->rec_per_key[group_key_parts - 1];
5452
8738
  if (keys_per_group == 0) /* If there is no statistics try to guess */
5453
8739
    /* each group contains 10% of all records */
5454
 
    keys_per_group= (uint32_t)(table_records / 10) + 1;
5455
 
  num_groups= (uint32_t)(table_records / keys_per_group) + 1;
 
8740
    keys_per_group= (uint)(table_records / 10) + 1;
 
8741
  num_groups= (uint)(table_records / keys_per_group) + 1;
5456
8742
 
5457
8743
  /* Apply the selectivity of the quick select for group prefixes. */
5458
8744
  if (range_tree && (quick_prefix_records != HA_POS_ERROR))
5459
8745
  {
5460
8746
    quick_prefix_selectivity= (double) quick_prefix_records /
5461
8747
                              (double) table_records;
5462
 
    num_groups= (uint32_t) rint(num_groups * quick_prefix_selectivity);
5463
 
    set_if_bigger(num_groups, 1U);
 
8748
    num_groups= (uint) rint(num_groups * quick_prefix_selectivity);
 
8749
    set_if_bigger(num_groups, 1);
5464
8750
  }
5465
8751
 
5466
8752
  if (used_key_parts > group_key_parts)
5477
8763
      p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
5478
8764
      p_overlap= min(p_overlap, 1.0);
5479
8765
    }
5480
 
    io_cost= (double) min(num_groups * (1 + p_overlap), (double)num_blocks);
 
8766
    io_cost= (double) min(num_groups * (1 + p_overlap), num_blocks);
5481
8767
  }
5482
8768
  else
5483
8769
    io_cost= (keys_per_group > keys_per_block) ?
5488
8774
  /*
5489
8775
    TODO: If there is no WHERE clause and no other expressions, there should be
5490
8776
    no CPU cost. We leave it here to make this cost comparable to that of index
5491
 
    scan as computed in SqlSelect::test_quick_select().
 
8777
    scan as computed in SQL_SELECT::test_quick_select().
5492
8778
  */
5493
8779
  cpu_cost= (double) num_groups / TIME_FOR_COMPARE;
5494
8780
 
5495
8781
  *read_cost= io_cost + cpu_cost;
5496
8782
  *records= num_groups;
 
8783
 
 
8784
  DBUG_PRINT("info",
 
8785
             ("table rows: %lu  keys/block: %u  keys/group: %u  result rows: %lu  blocks: %u",
 
8786
              (ulong)table_records, keys_per_block, keys_per_group, 
 
8787
              (ulong) *records, num_blocks));
 
8788
  DBUG_VOID_RETURN;
5497
8789
}
5498
8790
 
5499
8791
 
5501
8793
  Construct a new quick select object for queries with group by with min/max.
5502
8794
 
5503
8795
  SYNOPSIS
5504
 
    GroupMinMaxReadPlan::make_quick()
 
8796
    TRP_GROUP_MIN_MAX::make_quick()
5505
8797
    param              Parameter from test_quick_select
5506
8798
    retrieve_full_rows ignored
5507
8799
    parent_alloc       Memory pool to use, if any.
5508
8800
 
5509
8801
  NOTES
5510
8802
    Make_quick ignores the retrieve_full_rows parameter because
5511
 
    QuickGroupMinMaxSelect always performs 'index only' scans.
 
8803
    QUICK_GROUP_MIN_MAX_SELECT always performs 'index only' scans.
5512
8804
    The other parameter are ignored as well because all necessary
5513
8805
    data to create the QUICK object is computed at this TRP creation
5514
8806
    time.
5515
8807
 
5516
8808
  RETURN
5517
 
    New QuickGroupMinMaxSelect object if successfully created,
 
8809
    New QUICK_GROUP_MIN_MAX_SELECT object if successfully created,
5518
8810
    NULL otherwise.
5519
8811
*/
5520
 
optimizer::QuickSelectInterface *
5521
 
optimizer::GroupMinMaxReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *parent_alloc)
 
8812
 
 
8813
QUICK_SELECT_I *
 
8814
TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool retrieve_full_rows,
 
8815
                              MEM_ROOT *parent_alloc)
5522
8816
{
5523
 
  optimizer::QuickGroupMinMaxSelect *quick= NULL;
 
8817
  QUICK_GROUP_MIN_MAX_SELECT *quick;
 
8818
  DBUG_ENTER("TRP_GROUP_MIN_MAX::make_quick");
5524
8819
 
5525
 
  quick= new optimizer::QuickGroupMinMaxSelect(param->table,
5526
 
                                               param->session->lex->current_select->join,
5527
 
                                               have_min,
5528
 
                                               have_max,
5529
 
                                               min_max_arg_part,
5530
 
                                               group_prefix_len,
5531
 
                                               group_key_parts,
5532
 
                                               used_key_parts,
5533
 
                                               index_info,
5534
 
                                               index,
5535
 
                                               read_cost,
5536
 
                                               records,
5537
 
                                               key_infix_len,
5538
 
                                               key_infix,
5539
 
                                               parent_alloc);
5540
 
  if (! quick)
5541
 
  {
5542
 
    return NULL;
5543
 
  }
 
8820
  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
 
8821
                                        param->thd->lex->current_select->join,
 
8822
                                        have_min, have_max, min_max_arg_part,
 
8823
                                        group_prefix_len, group_key_parts,
 
8824
                                        used_key_parts, index_info, index,
 
8825
                                        read_cost, records, key_infix_len,
 
8826
                                        key_infix, parent_alloc);
 
8827
  if (!quick)
 
8828
    DBUG_RETURN(NULL);
5544
8829
 
5545
8830
  if (quick->init())
5546
8831
  {
5547
8832
    delete quick;
5548
 
    return NULL;
 
8833
    DBUG_RETURN(NULL);
5549
8834
  }
5550
8835
 
5551
8836
  if (range_tree)
5552
8837
  {
5553
 
    assert(quick_prefix_records > 0);
 
8838
    DBUG_ASSERT(quick_prefix_records > 0);
5554
8839
    if (quick_prefix_records == HA_POS_ERROR)
5555
 
    {
5556
8840
      quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
5557
 
    }
5558
8841
    else
5559
 
    {
5560
 
      /* Make a QuickRangeSelect to be used for group prefix retrieval. */
5561
 
      quick->quick_prefix_select= optimizer::get_quick_select(param,
5562
 
                                                              param_idx,
5563
 
                                                              index_tree,
5564
 
                                                              HA_MRR_USE_DEFAULT_IMPL,
5565
 
                                                              0,
5566
 
                                                              &quick->alloc);
5567
 
    }
 
8842
      /* Make a QUICK_RANGE_SELECT to be used for group prefix retrieval. */
 
8843
      quick->quick_prefix_select= get_quick_select(param, param_idx,
 
8844
                                                   index_tree,
 
8845
                                                   HA_MRR_USE_DEFAULT_IMPL, 0,
 
8846
                                                   &quick->alloc);
5568
8847
 
5569
8848
    /*
5570
8849
      Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
5571
 
      attribute, and create an array of QuickRanges to be used by the
 
8850
      attribute, and create an array of QUICK_RANGES to be used by the
5572
8851
      new quick select.
5573
8852
    */
5574
8853
    if (min_max_arg_part)
5575
8854
    {
5576
 
      optimizer::SEL_ARG *min_max_range= index_tree;
 
8855
      SEL_ARG *min_max_range= index_tree;
5577
8856
      while (min_max_range) /* Find the tree for the MIN/MAX key part. */
5578
8857
      {
5579
8858
        if (min_max_range->field->eq(min_max_arg_part->field))
5583
8862
      /* Scroll to the leftmost interval for the MIN/MAX argument. */
5584
8863
      while (min_max_range && min_max_range->prev)
5585
8864
        min_max_range= min_max_range->prev;
5586
 
      /* Create an array of QuickRanges for the MIN/MAX argument. */
 
8865
      /* Create an array of QUICK_RANGEs for the MIN/MAX argument. */
5587
8866
      while (min_max_range)
5588
8867
      {
5589
8868
        if (quick->add_range(min_max_range))
5590
8869
        {
5591
8870
          delete quick;
5592
8871
          quick= NULL;
5593
 
          return NULL;
 
8872
          DBUG_RETURN(NULL);
5594
8873
        }
5595
8874
        min_max_range= min_max_range->next;
5596
8875
      }
5602
8881
  quick->update_key_stat();
5603
8882
  quick->adjust_prefix_ranges();
5604
8883
 
5605
 
  return quick;
5606
 
}
5607
 
 
5608
 
 
5609
 
optimizer::QuickSelectInterface *optimizer::RangeReadPlan::make_quick(optimizer::Parameter *param, bool, memory::Root *parent_alloc)
5610
 
{
5611
 
  optimizer::QuickRangeSelect *quick= NULL;
5612
 
  if ((quick= optimizer::get_quick_select(param,
5613
 
                                          key_idx,
5614
 
                                          key,
5615
 
                                          mrr_flags,
5616
 
                                          mrr_buf_size,
5617
 
                                          parent_alloc)))
5618
 
  {
5619
 
    quick->records= records;
5620
 
    quick->read_time= read_cost;
5621
 
  }
5622
 
  return quick;
5623
 
}
5624
 
 
5625
 
 
5626
 
} /* namespace drizzled */
 
8884
  DBUG_RETURN(quick);
 
8885
}
 
8886
 
 
8887
 
 
8888
/*
 
8889
  Construct new quick select for group queries with min/max.
 
8890
 
 
8891
  SYNOPSIS
 
8892
    QUICK_GROUP_MIN_MAX_SELECT::QUICK_GROUP_MIN_MAX_SELECT()
 
8893
    table             The table being accessed
 
8894
    join              Descriptor of the current query
 
8895
    have_min          true if the query selects a MIN function
 
8896
    have_max          true if the query selects a MAX function
 
8897
    min_max_arg_part  The only argument field of all MIN/MAX functions
 
8898
    group_prefix_len  Length of all key parts in the group prefix
 
8899
    prefix_key_parts  All key parts in the group prefix
 
8900
    index_info        The index chosen for data access
 
8901
    use_index         The id of index_info
 
8902
    read_cost         Cost of this access method
 
8903
    records           Number of records returned
 
8904
    key_infix_len     Length of the key infix appended to the group prefix
 
8905
    key_infix         Infix of constants from equality predicates
 
8906
    parent_alloc      Memory pool for this and quick_prefix_select data
 
8907
 
 
8908
  RETURN
 
8909
    None
 
8910
*/
 
8911
 
 
8912
QUICK_GROUP_MIN_MAX_SELECT::
 
8913
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
 
8914
                           bool have_max_arg,
 
8915
                           KEY_PART_INFO *min_max_arg_part_arg,
 
8916
                           uint group_prefix_len_arg, uint group_key_parts_arg,
 
8917
                           uint used_key_parts_arg, KEY *index_info_arg,
 
8918
                           uint use_index, double read_cost_arg,
 
8919
                           ha_rows records_arg, uint key_infix_len_arg,
 
8920
                           uchar *key_infix_arg, MEM_ROOT *parent_alloc)
 
8921
  :join(join_arg), index_info(index_info_arg),
 
8922
   group_prefix_len(group_prefix_len_arg),
 
8923
   group_key_parts(group_key_parts_arg), have_min(have_min_arg),
 
8924
   have_max(have_max_arg), seen_first_key(false),
 
8925
   min_max_arg_part(min_max_arg_part_arg), key_infix(key_infix_arg),
 
8926
   key_infix_len(key_infix_len_arg), min_functions_it(NULL),
 
8927
   max_functions_it(NULL)
 
8928
{
 
8929
  head=       table;
 
8930
  file=       head->file;
 
8931
  index=      use_index;
 
8932
  record=     head->record[0];
 
8933
  tmp_record= head->record[1];
 
8934
  read_time= read_cost_arg;
 
8935
  records= records_arg;
 
8936
  used_key_parts= used_key_parts_arg;
 
8937
  real_key_parts= used_key_parts_arg;
 
8938
  real_prefix_len= group_prefix_len + key_infix_len;
 
8939
  group_prefix= NULL;
 
8940
  min_max_arg_len= min_max_arg_part ? min_max_arg_part->store_length : 0;
 
8941
 
 
8942
  /*
 
8943
    We can't have parent_alloc set as the init function can't handle this case
 
8944
    yet.
 
8945
  */
 
8946
  DBUG_ASSERT(!parent_alloc);
 
8947
  if (!parent_alloc)
 
8948
  {
 
8949
    init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
 
8950
    join->thd->mem_root= &alloc;
 
8951
  }
 
8952
  else
 
8953
    bzero(&alloc, sizeof(MEM_ROOT));            // ensure that it's not used
 
8954
}
 
8955
 
 
8956
 
 
8957
/*
 
8958
  Do post-constructor initialization.
 
8959
 
 
8960
  SYNOPSIS
 
8961
    QUICK_GROUP_MIN_MAX_SELECT::init()
 
8962
  
 
8963
  DESCRIPTION
 
8964
    The method performs initialization that cannot be done in the constructor
 
8965
    such as memory allocations that may fail. It allocates memory for the
 
8966
    group prefix and inifix buffers, and for the lists of MIN/MAX item to be
 
8967
    updated during execution.
 
8968
 
 
8969
  RETURN
 
8970
    0      OK
 
8971
    other  Error code
 
8972
*/
 
8973
 
 
8974
int QUICK_GROUP_MIN_MAX_SELECT::init()
 
8975
{
 
8976
  if (group_prefix) /* Already initialized. */
 
8977
    return 0;
 
8978
 
 
8979
  if (!(last_prefix= (uchar*) alloc_root(&alloc, group_prefix_len)))
 
8980
      return 1;
 
8981
  /*
 
8982
    We may use group_prefix to store keys with all select fields, so allocate
 
8983
    enough space for it.
 
8984
  */
 
8985
  if (!(group_prefix= (uchar*) alloc_root(&alloc,
 
8986
                                         real_prefix_len + min_max_arg_len)))
 
8987
    return 1;
 
8988
 
 
8989
  if (key_infix_len > 0)
 
8990
  {
 
8991
    /*
 
8992
      The memory location pointed to by key_infix will be deleted soon, so
 
8993
      allocate a new buffer and copy the key_infix into it.
 
8994
    */
 
8995
    uchar *tmp_key_infix= (uchar*) alloc_root(&alloc, key_infix_len);
 
8996
    if (!tmp_key_infix)
 
8997
      return 1;
 
8998
    memcpy(tmp_key_infix, this->key_infix, key_infix_len);
 
8999
    this->key_infix= tmp_key_infix;
 
9000
  }
 
9001
 
 
9002
  if (min_max_arg_part)
 
9003
  {
 
9004
    if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16))
 
9005
      return 1;
 
9006
 
 
9007
    if (have_min)
 
9008
    {
 
9009
      if (!(min_functions= new List<Item_sum>))
 
9010
        return 1;
 
9011
    }
 
9012
    else
 
9013
      min_functions= NULL;
 
9014
    if (have_max)
 
9015
    {
 
9016
      if (!(max_functions= new List<Item_sum>))
 
9017
        return 1;
 
9018
    }
 
9019
    else
 
9020
      max_functions= NULL;
 
9021
 
 
9022
    Item_sum *min_max_item;
 
9023
    Item_sum **func_ptr= join->sum_funcs;
 
9024
    while ((min_max_item= *(func_ptr++)))
 
9025
    {
 
9026
      if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
 
9027
        min_functions->push_back(min_max_item);
 
9028
      else if (have_max && (min_max_item->sum_func() == Item_sum::MAX_FUNC))
 
9029
        max_functions->push_back(min_max_item);
 
9030
    }
 
9031
 
 
9032
    if (have_min)
 
9033
    {
 
9034
      if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
 
9035
        return 1;
 
9036
    }
 
9037
 
 
9038
    if (have_max)
 
9039
    {
 
9040
      if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
 
9041
        return 1;
 
9042
    }
 
9043
  }
 
9044
  else
 
9045
    min_max_ranges.elements= 0;
 
9046
 
 
9047
  return 0;
 
9048
}
 
9049
 
 
9050
 
 
9051
QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
 
9052
{
 
9053
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT");
 
9054
  if (file->inited != handler::NONE) 
 
9055
    file->ha_index_end();
 
9056
  if (min_max_arg_part)
 
9057
    delete_dynamic(&min_max_ranges);
 
9058
  free_root(&alloc,MYF(0));
 
9059
  delete min_functions_it;
 
9060
  delete max_functions_it;
 
9061
  delete quick_prefix_select;
 
9062
  DBUG_VOID_RETURN; 
 
9063
}
 
9064
 
 
9065
 
 
9066
/*
 
9067
  Eventually create and add a new quick range object.
 
9068
 
 
9069
  SYNOPSIS
 
9070
    QUICK_GROUP_MIN_MAX_SELECT::add_range()
 
9071
    sel_range  Range object from which a 
 
9072
 
 
9073
  NOTES
 
9074
    Construct a new QUICK_RANGE object from a SEL_ARG object, and
 
9075
    add it to the array min_max_ranges. If sel_arg is an infinite
 
9076
    range, e.g. (x < 5 or x > 4), then skip it and do not construct
 
9077
    a quick range.
 
9078
 
 
9079
  RETURN
 
9080
    false on success
 
9081
    true  otherwise
 
9082
*/
 
9083
 
 
9084
bool QUICK_GROUP_MIN_MAX_SELECT::add_range(SEL_ARG *sel_range)
 
9085
{
 
9086
  QUICK_RANGE *range;
 
9087
  uint range_flag= sel_range->min_flag | sel_range->max_flag;
 
9088
 
 
9089
  /* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
 
9090
  if ((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
 
9091
    return false;
 
9092
 
 
9093
  if (!(sel_range->min_flag & NO_MIN_RANGE) &&
 
9094
      !(sel_range->max_flag & NO_MAX_RANGE))
 
9095
  {
 
9096
    if (sel_range->maybe_null &&
 
9097
        sel_range->min_value[0] && sel_range->max_value[0])
 
9098
      range_flag|= NULL_RANGE; /* IS NULL condition */
 
9099
    else if (memcmp(sel_range->min_value, sel_range->max_value,
 
9100
                    min_max_arg_len) == 0)
 
9101
      range_flag|= EQ_RANGE;  /* equality condition */
 
9102
  }
 
9103
  range= new QUICK_RANGE(sel_range->min_value, min_max_arg_len,
 
9104
                         make_keypart_map(sel_range->part),
 
9105
                         sel_range->max_value, min_max_arg_len,
 
9106
                         make_keypart_map(sel_range->part),
 
9107
                         range_flag);
 
9108
  if (!range)
 
9109
    return true;
 
9110
  if (insert_dynamic(&min_max_ranges, (uchar*)&range))
 
9111
    return true;
 
9112
  return false;
 
9113
}
 
9114
 
 
9115
 
 
9116
/*
 
9117
  Opens the ranges if there are more conditions in quick_prefix_select than
 
9118
  the ones used for jumping through the prefixes.
 
9119
 
 
9120
  SYNOPSIS
 
9121
    QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges()
 
9122
 
 
9123
  NOTES
 
9124
    quick_prefix_select is made over the conditions on the whole key.
 
9125
    It defines a number of ranges of length x. 
 
9126
    However when jumping through the prefixes we use only the the first 
 
9127
    few most significant keyparts in the range key. However if there
 
9128
    are more keyparts to follow the ones we are using we must make the 
 
9129
    condition on the key inclusive (because x < "ab" means 
 
9130
    x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
 
9131
    To achive the above we must turn off the NEAR_MIN/NEAR_MAX
 
9132
*/
 
9133
void QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges ()
 
9134
{
 
9135
  if (quick_prefix_select &&
 
9136
      group_prefix_len < quick_prefix_select->max_used_key_length)
 
9137
  {
 
9138
    DYNAMIC_ARRAY *arr;
 
9139
    uint inx;
 
9140
 
 
9141
    for (inx= 0, arr= &quick_prefix_select->ranges; inx < arr->elements; inx++)
 
9142
    {
 
9143
      QUICK_RANGE *range;
 
9144
 
 
9145
      get_dynamic(arr, (uchar*)&range, inx);
 
9146
      range->flag &= ~(NEAR_MIN | NEAR_MAX);
 
9147
    }
 
9148
  }
 
9149
}
 
9150
 
 
9151
 
 
9152
/*
 
9153
  Determine the total number and length of the keys that will be used for
 
9154
  index lookup.
 
9155
 
 
9156
  SYNOPSIS
 
9157
    QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
 
9158
 
 
9159
  DESCRIPTION
 
9160
    The total length of the keys used for index lookup depends on whether
 
9161
    there are any predicates referencing the min/max argument, and/or if
 
9162
    the min/max argument field can be NULL.
 
9163
    This function does an optimistic analysis whether the search key might
 
9164
    be extended by a constant for the min/max keypart. It is 'optimistic'
 
9165
    because during actual execution it may happen that a particular range
 
9166
    is skipped, and then a shorter key will be used. However this is data
 
9167
    dependent and can't be easily estimated here.
 
9168
 
 
9169
  RETURN
 
9170
    None
 
9171
*/
 
9172
 
 
9173
void QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
 
9174
{
 
9175
  max_used_key_length= real_prefix_len;
 
9176
  if (min_max_ranges.elements > 0)
 
9177
  {
 
9178
    QUICK_RANGE *cur_range;
 
9179
    if (have_min)
 
9180
    { /* Check if the right-most range has a lower boundary. */
 
9181
      get_dynamic(&min_max_ranges, (uchar*)&cur_range,
 
9182
                  min_max_ranges.elements - 1);
 
9183
      if (!(cur_range->flag & NO_MIN_RANGE))
 
9184
      {
 
9185
        max_used_key_length+= min_max_arg_len;
 
9186
        used_key_parts++;
 
9187
        return;
 
9188
      }
 
9189
    }
 
9190
    if (have_max)
 
9191
    { /* Check if the left-most range has an upper boundary. */
 
9192
      get_dynamic(&min_max_ranges, (uchar*)&cur_range, 0);
 
9193
      if (!(cur_range->flag & NO_MAX_RANGE))
 
9194
      {
 
9195
        max_used_key_length+= min_max_arg_len;
 
9196
        used_key_parts++;
 
9197
        return;
 
9198
      }
 
9199
    }
 
9200
  }
 
9201
  else if (have_min && min_max_arg_part &&
 
9202
           min_max_arg_part->field->real_maybe_null())
 
9203
  {
 
9204
    /*
 
9205
      If a MIN/MAX argument value is NULL, we can quickly determine
 
9206
      that we're in the beginning of the next group, because NULLs
 
9207
      are always < any other value. This allows us to quickly
 
9208
      determine the end of the current group and jump to the next
 
9209
      group (see next_min()) and thus effectively increases the
 
9210
      usable key length.
 
9211
    */
 
9212
    max_used_key_length+= min_max_arg_len;
 
9213
    used_key_parts++;
 
9214
  }
 
9215
}
 
9216
 
 
9217
 
 
9218
/*
 
9219
  Initialize a quick group min/max select for key retrieval.
 
9220
 
 
9221
  SYNOPSIS
 
9222
    QUICK_GROUP_MIN_MAX_SELECT::reset()
 
9223
 
 
9224
  DESCRIPTION
 
9225
    Initialize the index chosen for access and find and store the prefix
 
9226
    of the last group. The method is expensive since it performs disk access.
 
9227
 
 
9228
  RETURN
 
9229
    0      OK
 
9230
    other  Error code
 
9231
*/
 
9232
 
 
9233
int QUICK_GROUP_MIN_MAX_SELECT::reset(void)
 
9234
{
 
9235
  int result;
 
9236
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::reset");
 
9237
 
 
9238
  file->extra(HA_EXTRA_KEYREAD); /* We need only the key attributes */
 
9239
  if ((result= file->ha_index_init(index,1)))
 
9240
    DBUG_RETURN(result);
 
9241
  if (quick_prefix_select && quick_prefix_select->reset())
 
9242
    DBUG_RETURN(1);
 
9243
  result= file->index_last(record);
 
9244
  if (result == HA_ERR_END_OF_FILE)
 
9245
    DBUG_RETURN(0);
 
9246
  /* Save the prefix of the last group. */
 
9247
  key_copy(last_prefix, record, index_info, group_prefix_len);
 
9248
 
 
9249
  DBUG_RETURN(0);
 
9250
}
 
9251
 
 
9252
 
 
9253
 
 
9254
/* 
 
9255
  Get the next key containing the MIN and/or MAX key for the next group.
 
9256
 
 
9257
  SYNOPSIS
 
9258
    QUICK_GROUP_MIN_MAX_SELECT::get_next()
 
9259
 
 
9260
  DESCRIPTION
 
9261
    The method finds the next subsequent group of records that satisfies the
 
9262
    query conditions and finds the keys that contain the MIN/MAX values for
 
9263
    the key part referenced by the MIN/MAX function(s). Once a group and its
 
9264
    MIN/MAX values are found, store these values in the Item_sum objects for
 
9265
    the MIN/MAX functions. The rest of the values in the result row are stored
 
9266
    in the Item_field::result_field of each select field. If the query does
 
9267
    not contain MIN and/or MAX functions, then the function only finds the
 
9268
    group prefix, which is a query answer itself.
 
9269
 
 
9270
  NOTES
 
9271
    If both MIN and MAX are computed, then we use the fact that if there is
 
9272
    no MIN key, there can't be a MAX key as well, so we can skip looking
 
9273
    for a MAX key in this case.
 
9274
 
 
9275
  RETURN
 
9276
    0                  on success
 
9277
    HA_ERR_END_OF_FILE if returned all keys
 
9278
    other              if some error occurred
 
9279
*/
 
9280
 
 
9281
int QUICK_GROUP_MIN_MAX_SELECT::get_next()
 
9282
{
 
9283
  int min_res= 0;
 
9284
  int max_res= 0;
 
9285
  int result;
 
9286
  int is_last_prefix= 0;
 
9287
 
 
9288
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::get_next");
 
9289
 
 
9290
  /*
 
9291
    Loop until a group is found that satisfies all query conditions or the last
 
9292
    group is reached.
 
9293
  */
 
9294
  do
 
9295
  {
 
9296
    result= next_prefix();
 
9297
    /*
 
9298
      Check if this is the last group prefix. Notice that at this point
 
9299
      this->record contains the current prefix in record format.
 
9300
    */
 
9301
    if (!result)
 
9302
    {
 
9303
      is_last_prefix= key_cmp(index_info->key_part, last_prefix,
 
9304
                              group_prefix_len);
 
9305
      DBUG_ASSERT(is_last_prefix <= 0);
 
9306
    }
 
9307
    else 
 
9308
    {
 
9309
      if (result == HA_ERR_KEY_NOT_FOUND)
 
9310
        continue;
 
9311
      break;
 
9312
    }
 
9313
 
 
9314
    if (have_min)
 
9315
    {
 
9316
      min_res= next_min();
 
9317
      if (min_res == 0)
 
9318
        update_min_result();
 
9319
    }
 
9320
    /* If there is no MIN in the group, there is no MAX either. */
 
9321
    if ((have_max && !have_min) ||
 
9322
        (have_max && have_min && (min_res == 0)))
 
9323
    {
 
9324
      max_res= next_max();
 
9325
      if (max_res == 0)
 
9326
        update_max_result();
 
9327
      /* If a MIN was found, a MAX must have been found as well. */
 
9328
      DBUG_ASSERT((have_max && !have_min) ||
 
9329
                  (have_max && have_min && (max_res == 0)));
 
9330
    }
 
9331
    /*
 
9332
      If this is just a GROUP BY or DISTINCT without MIN or MAX and there
 
9333
      are equality predicates for the key parts after the group, find the
 
9334
      first sub-group with the extended prefix.
 
9335
    */
 
9336
    if (!have_min && !have_max && key_infix_len > 0)
 
9337
      result= file->index_read_map(record, group_prefix,
 
9338
                                   make_prev_keypart_map(real_key_parts),
 
9339
                                   HA_READ_KEY_EXACT);
 
9340
 
 
9341
    result= have_min ? min_res : have_max ? max_res : result;
 
9342
  } while ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
 
9343
           is_last_prefix != 0);
 
9344
 
 
9345
  if (result == 0)
 
9346
  {
 
9347
    /*
 
9348
      Partially mimic the behavior of end_select_send. Copy the
 
9349
      field data from Item_field::field into Item_field::result_field
 
9350
      of each non-aggregated field (the group fields, and optionally
 
9351
      other fields in non-ANSI SQL mode).
 
9352
    */
 
9353
    copy_fields(&join->tmp_table_param);
 
9354
  }
 
9355
  else if (result == HA_ERR_KEY_NOT_FOUND)
 
9356
    result= HA_ERR_END_OF_FILE;
 
9357
 
 
9358
  DBUG_RETURN(result);
 
9359
}
 
9360
 
 
9361
 
 
9362
/*
 
9363
  Retrieve the minimal key in the next group.
 
9364
 
 
9365
  SYNOPSIS
 
9366
    QUICK_GROUP_MIN_MAX_SELECT::next_min()
 
9367
 
 
9368
  DESCRIPTION
 
9369
    Find the minimal key within this group such that the key satisfies the query
 
9370
    conditions and NULL semantics. The found key is loaded into this->record.
 
9371
 
 
9372
  IMPLEMENTATION
 
9373
    Depending on the values of min_max_ranges.elements, key_infix_len, and
 
9374
    whether there is a  NULL in the MIN field, this function may directly
 
9375
    return without any data access. In this case we use the key loaded into
 
9376
    this->record by the call to this->next_prefix() just before this call.
 
9377
 
 
9378
  RETURN
 
9379
    0                    on success
 
9380
    HA_ERR_KEY_NOT_FOUND if no MIN key was found that fulfills all conditions.
 
9381
    HA_ERR_END_OF_FILE   - "" -
 
9382
    other                if some error occurred
 
9383
*/
 
9384
 
 
9385
int QUICK_GROUP_MIN_MAX_SELECT::next_min()
 
9386
{
 
9387
  int result= 0;
 
9388
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_min");
 
9389
 
 
9390
  /* Find the MIN key using the eventually extended group prefix. */
 
9391
  if (min_max_ranges.elements > 0)
 
9392
  {
 
9393
    if ((result= next_min_in_range()))
 
9394
      DBUG_RETURN(result);
 
9395
  }
 
9396
  else
 
9397
  {
 
9398
    /* Apply the constant equality conditions to the non-group select fields */
 
9399
    if (key_infix_len > 0)
 
9400
    {
 
9401
      if ((result= file->index_read_map(record, group_prefix,
 
9402
                                        make_prev_keypart_map(real_key_parts),
 
9403
                                        HA_READ_KEY_EXACT)))
 
9404
        DBUG_RETURN(result);
 
9405
    }
 
9406
 
 
9407
    /*
 
9408
      If the min/max argument field is NULL, skip subsequent rows in the same
 
9409
      group with NULL in it. Notice that:
 
9410
      - if the first row in a group doesn't have a NULL in the field, no row
 
9411
      in the same group has (because NULL < any other value),
 
9412
      - min_max_arg_part->field->ptr points to some place in 'record'.
 
9413
    */
 
9414
    if (min_max_arg_part && min_max_arg_part->field->is_null())
 
9415
    {
 
9416
      /* Find the first subsequent record without NULL in the MIN/MAX field. */
 
9417
      key_copy(tmp_record, record, index_info, 0);
 
9418
      result= file->index_read_map(record, tmp_record,
 
9419
                                   make_keypart_map(real_key_parts),
 
9420
                                   HA_READ_AFTER_KEY);
 
9421
      /*
 
9422
        Check if the new record belongs to the current group by comparing its
 
9423
        prefix with the group's prefix. If it is from the next group, then the
 
9424
        whole group has NULLs in the MIN/MAX field, so use the first record in
 
9425
        the group as a result.
 
9426
        TODO:
 
9427
        It is possible to reuse this new record as the result candidate for the
 
9428
        next call to next_min(), and to save one lookup in the next call. For
 
9429
        this add a new member 'this->next_group_prefix'.
 
9430
      */
 
9431
      if (!result)
 
9432
      {
 
9433
        if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
 
9434
          key_restore(record, tmp_record, index_info, 0);
 
9435
      }
 
9436
      else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE)
 
9437
        result= 0; /* There is a result in any case. */
 
9438
    }
 
9439
  }
 
9440
 
 
9441
  /*
 
9442
    If the MIN attribute is non-nullable, this->record already contains the
 
9443
    MIN key in the group, so just return.
 
9444
  */
 
9445
  DBUG_RETURN(result);
 
9446
}
 
9447
 
 
9448
 
 
9449
/* 
 
9450
  Retrieve the maximal key in the next group.
 
9451
 
 
9452
  SYNOPSIS
 
9453
    QUICK_GROUP_MIN_MAX_SELECT::next_max()
 
9454
 
 
9455
  DESCRIPTION
 
9456
    Lookup the maximal key of the group, and store it into this->record.
 
9457
 
 
9458
  RETURN
 
9459
    0                    on success
 
9460
    HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
 
9461
    HA_ERR_END_OF_FILE   - "" -
 
9462
    other                if some error occurred
 
9463
*/
 
9464
 
 
9465
int QUICK_GROUP_MIN_MAX_SELECT::next_max()
 
9466
{
 
9467
  int result;
 
9468
 
 
9469
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_max");
 
9470
 
 
9471
  /* Get the last key in the (possibly extended) group. */
 
9472
  if (min_max_ranges.elements > 0)
 
9473
    result= next_max_in_range();
 
9474
  else
 
9475
    result= file->index_read_map(record, group_prefix,
 
9476
                                 make_prev_keypart_map(real_key_parts),
 
9477
                                 HA_READ_PREFIX_LAST);
 
9478
  DBUG_RETURN(result);
 
9479
}
 
9480
 
 
9481
 
 
9482
/*
 
9483
  Determine the prefix of the next group.
 
9484
 
 
9485
  SYNOPSIS
 
9486
    QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
 
9487
 
 
9488
  DESCRIPTION
 
9489
    Determine the prefix of the next group that satisfies the query conditions.
 
9490
    If there is a range condition referencing the group attributes, use a
 
9491
    QUICK_RANGE_SELECT object to retrieve the *first* key that satisfies the
 
9492
    condition. If there is a key infix of constants, append this infix
 
9493
    immediately after the group attributes. The possibly extended prefix is
 
9494
    stored in this->group_prefix. The first key of the found group is stored in
 
9495
    this->record, on which relies this->next_min().
 
9496
 
 
9497
  RETURN
 
9498
    0                    on success
 
9499
    HA_ERR_KEY_NOT_FOUND if there is no key with the formed prefix
 
9500
    HA_ERR_END_OF_FILE   if there are no more keys
 
9501
    other                if some error occurred
 
9502
*/
 
9503
int QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
 
9504
{
 
9505
  int result;
 
9506
  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_prefix");
 
9507
 
 
9508
  if (quick_prefix_select)
 
9509
  {
 
9510
    uchar *cur_prefix= seen_first_key ? group_prefix : NULL;
 
9511
    if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
 
9512
                         make_prev_keypart_map(group_key_parts), cur_prefix)))
 
9513
      DBUG_RETURN(result);
 
9514
    seen_first_key= true;
 
9515
  }
 
9516
  else
 
9517
  {
 
9518
    if (!seen_first_key)
 
9519
    {
 
9520
      result= file->index_first(record);
 
9521
      if (result)
 
9522
        DBUG_RETURN(result);
 
9523
      seen_first_key= true;
 
9524
    }
 
9525
    else
 
9526
    {
 
9527
      /* Load the first key in this group into record. */
 
9528
      result= file->index_read_map(record, group_prefix,
 
9529
                                   make_prev_keypart_map(group_key_parts),
 
9530
                                   HA_READ_AFTER_KEY);
 
9531
      if (result)
 
9532
        DBUG_RETURN(result);
 
9533
    }
 
9534
  }
 
9535
 
 
9536
  /* Save the prefix of this group for subsequent calls. */
 
9537
  key_copy(group_prefix, record, index_info, group_prefix_len);
 
9538
  /* Append key_infix to group_prefix. */
 
9539
  if (key_infix_len > 0)
 
9540
    memcpy(group_prefix + group_prefix_len,
 
9541
           key_infix, key_infix_len);
 
9542
 
 
9543
  DBUG_RETURN(0);
 
9544
}
 
9545
 
 
9546
 
 
9547
/*
 
9548
  Find the minimal key in a group that satisfies some range conditions for the
 
9549
  min/max argument field.
 
9550
 
 
9551
  SYNOPSIS
 
9552
    QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
 
9553
 
 
9554
  DESCRIPTION
 
9555
    Given the sequence of ranges min_max_ranges, find the minimal key that is
 
9556
    in the left-most possible range. If there is no such key, then the current
 
9557
    group does not have a MIN key that satisfies the WHERE clause. If a key is
 
9558
    found, its value is stored in this->record.
 
9559
 
 
9560
  RETURN
 
9561
    0                    on success
 
9562
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
 
9563
                         the ranges
 
9564
    HA_ERR_END_OF_FILE   - "" -
 
9565
    other                if some error
 
9566
*/
 
9567
 
 
9568
int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
 
9569
{
 
9570
  ha_rkey_function find_flag;
 
9571
  key_part_map keypart_map;
 
9572
  QUICK_RANGE *cur_range;
 
9573
  bool found_null= false;
 
9574
  int result= HA_ERR_KEY_NOT_FOUND;
 
9575
 
 
9576
  DBUG_ASSERT(min_max_ranges.elements > 0);
 
9577
 
 
9578
  for (uint range_idx= 0; range_idx < min_max_ranges.elements; range_idx++)
 
9579
  { /* Search from the left-most range to the right. */
 
9580
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx);
 
9581
 
 
9582
    /*
 
9583
      If the current value for the min/max argument is bigger than the right
 
9584
      boundary of cur_range, there is no need to check this range.
 
9585
    */
 
9586
    if (range_idx != 0 && !(cur_range->flag & NO_MAX_RANGE) &&
 
9587
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->max_key,
 
9588
                 min_max_arg_len) == 1))
 
9589
      continue;
 
9590
 
 
9591
    if (cur_range->flag & NO_MIN_RANGE)
 
9592
    {
 
9593
      keypart_map= make_prev_keypart_map(real_key_parts);
 
9594
      find_flag= HA_READ_KEY_EXACT;
 
9595
    }
 
9596
    else
 
9597
    {
 
9598
      /* Extend the search key with the lower boundary for this range. */
 
9599
      memcpy(group_prefix + real_prefix_len, cur_range->min_key,
 
9600
             cur_range->min_length);
 
9601
      keypart_map= make_keypart_map(real_key_parts);
 
9602
      find_flag= (cur_range->flag & (EQ_RANGE | NULL_RANGE)) ?
 
9603
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MIN) ?
 
9604
                 HA_READ_AFTER_KEY : HA_READ_KEY_OR_NEXT;
 
9605
    }
 
9606
 
 
9607
    result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
 
9608
    if (result)
 
9609
    {
 
9610
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
 
9611
          (cur_range->flag & (EQ_RANGE | NULL_RANGE)))
 
9612
        continue; /* Check the next range. */
 
9613
 
 
9614
      /*
 
9615
        In all other cases (HA_ERR_*, HA_READ_KEY_EXACT with NO_MIN_RANGE,
 
9616
        HA_READ_AFTER_KEY, HA_READ_KEY_OR_NEXT) if the lookup failed for this
 
9617
        range, it can't succeed for any other subsequent range.
 
9618
      */
 
9619
      break;
 
9620
    }
 
9621
 
 
9622
    /* A key was found. */
 
9623
    if (cur_range->flag & EQ_RANGE)
 
9624
      break; /* No need to perform the checks below for equal keys. */
 
9625
 
 
9626
    if (cur_range->flag & NULL_RANGE)
 
9627
    {
 
9628
      /*
 
9629
        Remember this key, and continue looking for a non-NULL key that
 
9630
        satisfies some other condition.
 
9631
      */
 
9632
      memcpy(tmp_record, record, head->s->rec_buff_length);
 
9633
      found_null= true;
 
9634
      continue;
 
9635
    }
 
9636
 
 
9637
    /* Check if record belongs to the current group. */
 
9638
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
 
9639
    {
 
9640
      result= HA_ERR_KEY_NOT_FOUND;
 
9641
      continue;
 
9642
    }
 
9643
 
 
9644
    /* If there is an upper limit, check if the found key is in the range. */
 
9645
    if ( !(cur_range->flag & NO_MAX_RANGE) )
 
9646
    {
 
9647
      /* Compose the MAX key for the range. */
 
9648
      uchar *max_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
 
9649
      memcpy(max_key, group_prefix, real_prefix_len);
 
9650
      memcpy(max_key + real_prefix_len, cur_range->max_key,
 
9651
             cur_range->max_length);
 
9652
      /* Compare the found key with max_key. */
 
9653
      int cmp_res= key_cmp(index_info->key_part, max_key,
 
9654
                           real_prefix_len + min_max_arg_len);
 
9655
      if ((!((cur_range->flag & NEAR_MAX) && (cmp_res == -1)) || (cmp_res <= 0)))
 
9656
      {
 
9657
        result= HA_ERR_KEY_NOT_FOUND;
 
9658
        continue;
 
9659
      }
 
9660
    }
 
9661
    /* If we got to this point, the current key qualifies as MIN. */
 
9662
    DBUG_ASSERT(result == 0);
 
9663
    break;
 
9664
  }
 
9665
  /*
 
9666
    If there was a key with NULL in the MIN/MAX field, and there was no other
 
9667
    key without NULL from the same group that satisfies some other condition,
 
9668
    then use the key with the NULL.
 
9669
  */
 
9670
  if (found_null && result)
 
9671
  {
 
9672
    memcpy(record, tmp_record, head->s->rec_buff_length);
 
9673
    result= 0;
 
9674
  }
 
9675
  return result;
 
9676
}
 
9677
 
 
9678
 
 
9679
/*
 
9680
  Find the maximal key in a group that satisfies some range conditions for the
 
9681
  min/max argument field.
 
9682
 
 
9683
  SYNOPSIS
 
9684
    QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
 
9685
 
 
9686
  DESCRIPTION
 
9687
    Given the sequence of ranges min_max_ranges, find the maximal key that is
 
9688
    in the right-most possible range. If there is no such key, then the current
 
9689
    group does not have a MAX key that satisfies the WHERE clause. If a key is
 
9690
    found, its value is stored in this->record.
 
9691
 
 
9692
  RETURN
 
9693
    0                    on success
 
9694
    HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
 
9695
                         the ranges
 
9696
    HA_ERR_END_OF_FILE   - "" -
 
9697
    other                if some error
 
9698
*/
 
9699
 
 
9700
int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
 
9701
{
 
9702
  ha_rkey_function find_flag;
 
9703
  key_part_map keypart_map;
 
9704
  QUICK_RANGE *cur_range;
 
9705
  int result;
 
9706
 
 
9707
  DBUG_ASSERT(min_max_ranges.elements > 0);
 
9708
 
 
9709
  for (uint range_idx= min_max_ranges.elements; range_idx > 0; range_idx--)
 
9710
  { /* Search from the right-most range to the left. */
 
9711
    get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx - 1);
 
9712
 
 
9713
    /*
 
9714
      If the current value for the min/max argument is smaller than the left
 
9715
      boundary of cur_range, there is no need to check this range.
 
9716
    */
 
9717
    if (range_idx != min_max_ranges.elements &&
 
9718
        !(cur_range->flag & NO_MIN_RANGE) &&
 
9719
        (key_cmp(min_max_arg_part, (const uchar*) cur_range->min_key,
 
9720
                 min_max_arg_len) == -1))
 
9721
      continue;
 
9722
 
 
9723
    if (cur_range->flag & NO_MAX_RANGE)
 
9724
    {
 
9725
      keypart_map= make_prev_keypart_map(real_key_parts);
 
9726
      find_flag= HA_READ_PREFIX_LAST;
 
9727
    }
 
9728
    else
 
9729
    {
 
9730
      /* Extend the search key with the upper boundary for this range. */
 
9731
      memcpy(group_prefix + real_prefix_len, cur_range->max_key,
 
9732
             cur_range->max_length);
 
9733
      keypart_map= make_keypart_map(real_key_parts);
 
9734
      find_flag= (cur_range->flag & EQ_RANGE) ?
 
9735
                 HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MAX) ?
 
9736
                 HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV;
 
9737
    }
 
9738
 
 
9739
    result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
 
9740
 
 
9741
    if (result)
 
9742
    {
 
9743
      if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
 
9744
          (cur_range->flag & EQ_RANGE))
 
9745
        continue; /* Check the next range. */
 
9746
 
 
9747
      /*
 
9748
        In no key was found with this upper bound, there certainly are no keys
 
9749
        in the ranges to the left.
 
9750
      */
 
9751
      return result;
 
9752
    }
 
9753
    /* A key was found. */
 
9754
    if (cur_range->flag & EQ_RANGE)
 
9755
      return 0; /* No need to perform the checks below for equal keys. */
 
9756
 
 
9757
    /* Check if record belongs to the current group. */
 
9758
    if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
 
9759
      continue;                                 // Row not found
 
9760
 
 
9761
    /* If there is a lower limit, check if the found key is in the range. */
 
9762
    if ( !(cur_range->flag & NO_MIN_RANGE) )
 
9763
    {
 
9764
      /* Compose the MIN key for the range. */
 
9765
      uchar *min_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
 
9766
      memcpy(min_key, group_prefix, real_prefix_len);
 
9767
      memcpy(min_key + real_prefix_len, cur_range->min_key,
 
9768
             cur_range->min_length);
 
9769
      /* Compare the found key with min_key. */
 
9770
      int cmp_res= key_cmp(index_info->key_part, min_key,
 
9771
                           real_prefix_len + min_max_arg_len);
 
9772
      if ((!((cur_range->flag & NEAR_MIN) && (cmp_res == 1)) ||
 
9773
            (cmp_res >= 0)))
 
9774
        continue;
 
9775
    }
 
9776
    /* If we got to this point, the current key qualifies as MAX. */
 
9777
    return result;
 
9778
  }
 
9779
  return HA_ERR_KEY_NOT_FOUND;
 
9780
}
 
9781
 
 
9782
 
 
9783
/*
 
9784
  Update all MIN function results with the newly found value.
 
9785
 
 
9786
  SYNOPSIS
 
9787
    QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
 
9788
 
 
9789
  DESCRIPTION
 
9790
    The method iterates through all MIN functions and updates the result value
 
9791
    of each function by calling Item_sum::reset(), which in turn picks the new
 
9792
    result value from this->head->record[0], previously updated by
 
9793
    next_min(). The updated value is stored in a member variable of each of the
 
9794
    Item_sum objects, depending on the value type.
 
9795
 
 
9796
  IMPLEMENTATION
 
9797
    The update must be done separately for MIN and MAX, immediately after
 
9798
    next_min() was called and before next_max() is called, because both MIN and
 
9799
    MAX take their result value from the same buffer this->head->record[0]
 
9800
    (i.e.  this->record).
 
9801
 
 
9802
  RETURN
 
9803
    None
 
9804
*/
 
9805
 
 
9806
void QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
 
9807
{
 
9808
  Item_sum *min_func;
 
9809
 
 
9810
  min_functions_it->rewind();
 
9811
  while ((min_func= (*min_functions_it)++))
 
9812
    min_func->reset();
 
9813
}
 
9814
 
 
9815
 
 
9816
/*
 
9817
  Update all MAX function results with the newly found value.
 
9818
 
 
9819
  SYNOPSIS
 
9820
    QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
 
9821
 
 
9822
  DESCRIPTION
 
9823
    The method iterates through all MAX functions and updates the result value
 
9824
    of each function by calling Item_sum::reset(), which in turn picks the new
 
9825
    result value from this->head->record[0], previously updated by
 
9826
    next_max(). The updated value is stored in a member variable of each of the
 
9827
    Item_sum objects, depending on the value type.
 
9828
 
 
9829
  IMPLEMENTATION
 
9830
    The update must be done separately for MIN and MAX, immediately after
 
9831
    next_max() was called, because both MIN and MAX take their result value
 
9832
    from the same buffer this->head->record[0] (i.e.  this->record).
 
9833
 
 
9834
  RETURN
 
9835
    None
 
9836
*/
 
9837
 
 
9838
void QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
 
9839
{
 
9840
  Item_sum *max_func;
 
9841
 
 
9842
  max_functions_it->rewind();
 
9843
  while ((max_func= (*max_functions_it)++))
 
9844
    max_func->reset();
 
9845
}
 
9846
 
 
9847
 
 
9848
/*
 
9849
  Append comma-separated list of keys this quick select uses to key_names;
 
9850
  append comma-separated list of corresponding used lengths to used_lengths.
 
9851
 
 
9852
  SYNOPSIS
 
9853
    QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths()
 
9854
    key_names    [out] Names of used indexes
 
9855
    used_lengths [out] Corresponding lengths of the index names
 
9856
 
 
9857
  DESCRIPTION
 
9858
    This method is used by select_describe to extract the names of the
 
9859
    indexes used by a quick select.
 
9860
 
 
9861
*/
 
9862
 
 
9863
void QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths(String *key_names,
 
9864
                                                      String *used_lengths)
 
9865
{
 
9866
  char buf[64];
 
9867
  uint length;
 
9868
  key_names->append(index_info->name);
 
9869
  length= longlong2str(max_used_key_length, buf, 10) - buf;
 
9870
  used_lengths->append(buf, length);
 
9871
}
 
9872
 
 
9873
 
 
9874
#ifndef DBUG_OFF
 
9875
 
 
9876
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
 
9877
                           const char *msg)
 
9878
{
 
9879
  SEL_ARG **key,**end;
 
9880
  int idx;
 
9881
  char buff[1024];
 
9882
  DBUG_ENTER("print_sel_tree");
 
9883
 
 
9884
  String tmp(buff,sizeof(buff),&my_charset_bin);
 
9885
  tmp.length(0);
 
9886
  for (idx= 0,key=tree->keys, end=key+param->keys ;
 
9887
       key != end ;
 
9888
       key++,idx++)
 
9889
  {
 
9890
    if (tree_map->is_set(idx))
 
9891
    {
 
9892
      uint keynr= param->real_keynr[idx];
 
9893
      if (tmp.length())
 
9894
        tmp.append(',');
 
9895
      tmp.append(param->table->key_info[keynr].name);
 
9896
    }
 
9897
  }
 
9898
  if (!tmp.length())
 
9899
    tmp.append(STRING_WITH_LEN("(empty)"));
 
9900
 
 
9901
  DBUG_PRINT("info", ("SEL_TREE: 0x%lx (%s)  scans: %s", (long) tree, msg, tmp.ptr()));
 
9902
 
 
9903
  DBUG_VOID_RETURN;
 
9904
}
 
9905
 
 
9906
 
 
9907
static void print_ror_scans_arr(TABLE *table, const char *msg,
 
9908
                                struct st_ror_scan_info **start,
 
9909
                                struct st_ror_scan_info **end)
 
9910
{
 
9911
  DBUG_ENTER("print_ror_scans_arr");
 
9912
 
 
9913
  char buff[1024];
 
9914
  String tmp(buff,sizeof(buff),&my_charset_bin);
 
9915
  tmp.length(0);
 
9916
  for (;start != end; start++)
 
9917
  {
 
9918
    if (tmp.length())
 
9919
      tmp.append(',');
 
9920
    tmp.append(table->key_info[(*start)->keynr].name);
 
9921
  }
 
9922
  if (!tmp.length())
 
9923
    tmp.append(STRING_WITH_LEN("(empty)"));
 
9924
  DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.ptr()));
 
9925
  DBUG_VOID_RETURN;
 
9926
}
 
9927
 
 
9928
 
 
9929
/*****************************************************************************
 
9930
** Print a quick range for debugging
 
9931
** TODO:
 
9932
** This should be changed to use a String to store each row instead
 
9933
** of locking the DEBUG stream !
 
9934
*****************************************************************************/
 
9935
 
 
9936
static void
 
9937
print_key(KEY_PART *key_part, const uchar *key, uint used_length)
 
9938
{
 
9939
  char buff[1024];
 
9940
  const uchar *key_end= key+used_length;
 
9941
  String tmp(buff,sizeof(buff),&my_charset_bin);
 
9942
  uint store_length;
 
9943
  TABLE *table= key_part->field->table;
 
9944
  my_bitmap_map *old_write_set, *old_read_set;
 
9945
  old_write_set= dbug_tmp_use_all_columns(table, table->write_set);
 
9946
  old_read_set=  dbug_tmp_use_all_columns(table, table->read_set);
 
9947
 
 
9948
  for (; key < key_end; key+=store_length, key_part++)
 
9949
  {
 
9950
    Field *field=      key_part->field;
 
9951
    store_length= key_part->store_length;
 
9952
 
 
9953
    if (field->real_maybe_null())
 
9954
    {
 
9955
      if (*key)
 
9956
      {
 
9957
        fwrite("NULL",sizeof(char),4,DBUG_FILE);
 
9958
        continue;
 
9959
      }
 
9960
      key++;                                    // Skip null byte
 
9961
      store_length--;
 
9962
    }
 
9963
    field->set_key_image(key, key_part->length);
 
9964
    field->val_str(&tmp);
 
9965
    fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
 
9966
    if (key+store_length < key_end)
 
9967
      fputc('/',DBUG_FILE);
 
9968
  }
 
9969
  dbug_tmp_restore_column_map(table->write_set, old_write_set);
 
9970
  dbug_tmp_restore_column_map(table->read_set, old_read_set);
 
9971
}
 
9972
 
 
9973
 
 
9974
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
 
9975
{
 
9976
  char buf[MAX_KEY/8+1];
 
9977
  TABLE *table;
 
9978
  my_bitmap_map *old_read_map, *old_write_map;
 
9979
  DBUG_ENTER("print_quick");
 
9980
  if (!quick)
 
9981
    DBUG_VOID_RETURN;
 
9982
  DBUG_LOCK_FILE;
 
9983
 
 
9984
  table= quick->head;
 
9985
  old_read_map=  dbug_tmp_use_all_columns(table, table->read_set);
 
9986
  old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
 
9987
  quick->dbug_dump(0, true);
 
9988
  dbug_tmp_restore_column_map(table->read_set, old_read_map);
 
9989
  dbug_tmp_restore_column_map(table->write_set, old_write_map);
 
9990
 
 
9991
  fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));
 
9992
 
 
9993
  DBUG_UNLOCK_FILE;
 
9994
  DBUG_VOID_RETURN;
 
9995
}
 
9996
 
 
9997
 
 
9998
void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
 
9999
{
 
10000
  /* purecov: begin inspected */
 
10001
  fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
 
10002
          indent, "", head->key_info[index].name, max_used_key_length);
 
10003
 
 
10004
  if (verbose)
 
10005
  {
 
10006
    QUICK_RANGE *range;
 
10007
    QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
 
10008
    QUICK_RANGE **end_range= pr + ranges.elements;
 
10009
    for (; pr != end_range; ++pr)
 
10010
    {
 
10011
      fprintf(DBUG_FILE, "%*s", indent + 2, "");
 
10012
      range= *pr;
 
10013
      if (!(range->flag & NO_MIN_RANGE))
 
10014
      {
 
10015
        print_key(key_parts, range->min_key, range->min_length);
 
10016
        if (range->flag & NEAR_MIN)
 
10017
          fputs(" < ",DBUG_FILE);
 
10018
        else
 
10019
          fputs(" <= ",DBUG_FILE);
 
10020
      }
 
10021
      fputs("X",DBUG_FILE);
 
10022
 
 
10023
      if (!(range->flag & NO_MAX_RANGE))
 
10024
      {
 
10025
        if (range->flag & NEAR_MAX)
 
10026
          fputs(" < ",DBUG_FILE);
 
10027
        else
 
10028
          fputs(" <= ",DBUG_FILE);
 
10029
        print_key(key_parts, range->max_key, range->max_length);
 
10030
      }
 
10031
      fputs("\n",DBUG_FILE);
 
10032
    }
 
10033
  }
 
10034
  /* purecov: end */    
 
10035
}
 
10036
 
 
10037
void QUICK_INDEX_MERGE_SELECT::dbug_dump(int indent, bool verbose)
 
10038
{
 
10039
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
10040
  QUICK_RANGE_SELECT *quick;
 
10041
  fprintf(DBUG_FILE, "%*squick index_merge select\n", indent, "");
 
10042
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
 
10043
  while ((quick= it++))
 
10044
    quick->dbug_dump(indent+2, verbose);
 
10045
  if (pk_quick_select)
 
10046
  {
 
10047
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
 
10048
    pk_quick_select->dbug_dump(indent+2, verbose);
 
10049
  }
 
10050
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
 
10051
}
 
10052
 
 
10053
void QUICK_ROR_INTERSECT_SELECT::dbug_dump(int indent, bool verbose)
 
10054
{
 
10055
  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
 
10056
  QUICK_RANGE_SELECT *quick;
 
10057
  fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
 
10058
          indent, "", need_to_fetch_row? "":"non-");
 
10059
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
 
10060
  while ((quick= it++))
 
10061
    quick->dbug_dump(indent+2, verbose);
 
10062
  if (cpk_quick)
 
10063
  {
 
10064
    fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
 
10065
    cpk_quick->dbug_dump(indent+2, verbose);
 
10066
  }
 
10067
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
 
10068
}
 
10069
 
 
10070
void QUICK_ROR_UNION_SELECT::dbug_dump(int indent, bool verbose)
 
10071
{
 
10072
  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
 
10073
  QUICK_SELECT_I *quick;
 
10074
  fprintf(DBUG_FILE, "%*squick ROR-union select\n", indent, "");
 
10075
  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
 
10076
  while ((quick= it++))
 
10077
    quick->dbug_dump(indent+2, verbose);
 
10078
  fprintf(DBUG_FILE, "%*s}\n", indent, "");
 
10079
}
 
10080
 
 
10081
 
 
10082
/*
 
10083
  Print quick select information to DBUG_FILE.
 
10084
 
 
10085
  SYNOPSIS
 
10086
    QUICK_GROUP_MIN_MAX_SELECT::dbug_dump()
 
10087
    indent  Indentation offset
 
10088
    verbose If true show more detailed output.
 
10089
 
 
10090
  DESCRIPTION
 
10091
    Print the contents of this quick select to DBUG_FILE. The method also
 
10092
    calls dbug_dump() for the used quick select if any.
 
10093
 
 
10094
  IMPLEMENTATION
 
10095
    Caller is responsible for locking DBUG_FILE before this call and unlocking
 
10096
    it afterwards.
 
10097
 
 
10098
  RETURN
 
10099
    None
 
10100
*/
 
10101
 
 
10102
void QUICK_GROUP_MIN_MAX_SELECT::dbug_dump(int indent, bool verbose)
 
10103
{
 
10104
  fprintf(DBUG_FILE,
 
10105
          "%*squick_group_min_max_select: index %s (%d), length: %d\n",
 
10106
          indent, "", index_info->name, index, max_used_key_length);
 
10107
  if (key_infix_len > 0)
 
10108
  {
 
10109
    fprintf(DBUG_FILE, "%*susing key_infix with length %d:\n",
 
10110
            indent, "", key_infix_len);
 
10111
  }
 
10112
  if (quick_prefix_select)
 
10113
  {
 
10114
    fprintf(DBUG_FILE, "%*susing quick_range_select:\n", indent, "");
 
10115
    quick_prefix_select->dbug_dump(indent + 2, verbose);
 
10116
  }
 
10117
  if (min_max_ranges.elements > 0)
 
10118
  {
 
10119
    fprintf(DBUG_FILE, "%*susing %d quick_ranges for MIN/MAX:\n",
 
10120
            indent, "", min_max_ranges.elements);
 
10121
  }
 
10122
}
 
10123
 
 
10124
 
 
10125
#endif /* NOT_USED */
 
10126
 
 
10127
/*****************************************************************************
 
10128
** Instantiate templates 
 
10129
*****************************************************************************/
 
10130
 
 
10131
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
10132
template class List<QUICK_RANGE>;
 
10133
template class List_iterator<QUICK_RANGE>;
 
10134
#endif