~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 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 by brian
clean slate
19
20
/* classes to use when handling where clause */
21
327.2.3 by Brian Aker
Refactoring of class Table
22
#ifndef DRIZZLED_OPT_RANGE_H
23
#define DRIZZLED_OPT_RANGE_H
1 by brian
clean slate
24
584.1.14 by Monty Taylor
Removed field.h from common_includes.
25
#include <drizzled/field.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
26
#include <drizzled/item/sum.h>
957.1.2 by Padraig O'Sullivan
Whoops, for to have the proper include directive for std::priority_queue
27
#include <queue>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
28
29
class JOIN;
30
typedef class Item COND;
31
32
typedef struct st_handler_buffer HANDLER_BUFFER;
1 by brian
clean slate
33
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
34
typedef struct st_key_part
35
{
36
  uint16_t key;
37
  uint16_t part;
1 by brian
clean slate
38
  /* See KEY_PART_INFO for meaning of the next two: */
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
39
  uint16_t store_length;
40
  uint16_t length;
41
  uint8_t null_bit;
42
  /**
1 by brian
clean slate
43
    Keypart flags (0 when this structure is used by partition pruning code
44
    for fake partitioning index description)
45
  */
206 by Brian Aker
Removed final uint dead types.
46
  uint8_t flag;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
47
  Field *field;
1 by brian
clean slate
48
} KEY_PART;
49
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
50
class QUICK_RANGE :public Sql_alloc 
51
{
52
public:
53
  unsigned char *min_key;
54
  unsigned char *max_key;
55
  uint16_t min_length;
56
  uint16_t max_length;
57
  uint16_t flag;
58
  key_part_map min_keypart_map; /**< bitmap of used keyparts in min_key */
59
  key_part_map max_keypart_map; /**< bitmap of used keyparts in max_key */
1 by brian
clean slate
60
#ifdef HAVE_purify
206 by Brian Aker
Removed final uint dead types.
61
  uint16_t dummy;					/* Avoid warnings on 'flag' */
1 by brian
clean slate
62
#endif
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
63
  QUICK_RANGE(); /**< Constructor for a "full range" */
64
  QUICK_RANGE(const unsigned char *min_key_arg,
65
              uint32_t min_length_arg,
1 by brian
clean slate
66
              key_part_map min_keypart_map_arg,
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
67
	            const unsigned char *max_key_arg, 
68
              uint32_t max_length_arg,
1 by brian
clean slate
69
              key_part_map max_keypart_map_arg,
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
70
	            uint32_t flag_arg)
71
    : 
72
      min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
481 by Brian Aker
Remove all of uchar.
73
      max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
206 by Brian Aker
Removed final uint dead types.
74
      min_length((uint16_t) min_length_arg),
75
      max_length((uint16_t) max_length_arg),
76
      flag((uint16_t) flag_arg),
1 by brian
clean slate
77
      min_keypart_map(min_keypart_map_arg),
78
      max_keypart_map(max_keypart_map_arg)
79
    {
80
#ifdef HAVE_purify
81
      dummy=0;
82
#endif
83
    }
84
};
85
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
86
/**
1 by brian
clean slate
87
  Quick select interface.
160.1.2 by mark
remove FTPARSER and last remains of full text search
88
  This class is a parent for all QUICK_*_SELECT classes.
1 by brian
clean slate
89
90
  The usage scenario is as follows:
91
  1. Create quick select
92
    quick= new QUICK_XXX_SELECT(...);
93
94
  2. Perform lightweight initialization. This can be done in 2 ways:
95
  2.a: Regular initialization
96
    if (quick->init())
97
    {
98
      //the only valid action after failed init() call is delete
99
      delete quick;
100
    }
101
  2.b: Special initialization for quick selects merged by QUICK_ROR_*_SELECT
102
    if (quick->init_ror_merged_scan())
103
      delete quick;
104
105
  3. Perform zero, one, or more scans.
106
    while (...)
107
    {
108
      // initialize quick select for scan. This may allocate
109
      // buffers and/or prefetch rows.
110
      if (quick->reset())
111
      {
112
        //the only valid action after failed reset() call is delete
113
        delete quick;
114
        //abort query
115
      }
116
117
      // perform the scan
118
      do
119
      {
120
        res= quick->get_next();
121
      } while (res && ...)
122
    }
123
124
  4. Delete the select:
125
    delete quick;
126
127
*/
128
class QUICK_SELECT_I
129
{
130
public:
131
  bool sorted;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
132
  ha_rows records; /**< estimate of # of records to be retrieved */
133
  double read_time; /**< time to perform this retrieval */
134
  Table *head;
135
  /**
1 by brian
clean slate
136
    Index this quick select uses, or MAX_KEY for quick selects
137
    that use several indexes
138
  */
482 by Brian Aker
Remove uint.
139
  uint32_t index;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
140
  /**
1 by brian
clean slate
141
    Total length of first used_key_parts parts of the key.
142
    Applicable if index!= MAX_KEY.
143
  */
482 by Brian Aker
Remove uint.
144
  uint32_t max_used_key_length;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
145
  /**
146
    Maximum number of (first) key parts this quick select uses for retrieval.
1 by brian
clean slate
147
    eg. for "(key1p1=c1 AND key1p2=c2) OR key1p1=c2" used_key_parts == 2.
148
    Applicable if index!= MAX_KEY.
149
150
    For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
151
  */
482 by Brian Aker
Remove uint.
152
  uint32_t used_key_parts;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
153
  /**
154
   * The rowid of last row retrieved by this quick select. This is used only when
155
   * doing ROR-index_merge selects
156
   */
157
  unsigned char *last_rowid;
158
159
  /**
160
   * Table record buffer used by this quick select.
161
   */
162
  unsigned char *record;
1 by brian
clean slate
163
164
  QUICK_SELECT_I();
165
  virtual ~QUICK_SELECT_I(){};
166
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
167
  /**
168
   * Do post-constructor initialization.
169
   *
170
   * @details
171
   *
172
   * Performs initializations that should have been in constructor if
173
   * it was possible to return errors from constructors. The join optimizer may
174
   * create and then delete quick selects without retrieving any rows so init()
175
   * must not contain any IO or CPU intensive code.
176
   *
177
   * If init() call fails the only valid action is to delete this quick select,
178
   * reset() and get_next() must not be called.
179
   *
180
   * @retval
181
   *  0      OK
182
   * @retval
183
   *  other  Error code
184
  */
185
  virtual int init() = 0;
186
187
  /**
188
   * Initializes quick select for row retrieval.
189
   *
190
   * @details
191
   *
192
   * Should be called when it is certain that row retrieval will be
193
   * necessary. This call may do heavyweight initialization like buffering first
194
   * N records etc. If reset() call fails get_next() must not be called.
195
   * Note that reset() may be called several times if
196
   * - the quick select is executed in a subselect
197
   * - a JOIN buffer is used
198
   *
199
   * @retval 
200
   *  0      OK
201
   * @retval
202
   *  other  Error code
203
   */
204
  virtual int reset(void) = 0;
205
  /** Gets next record to retrieve */
206
  virtual int get_next() = 0;
207
208
  /** Range end should be called when we have looped over the whole index */
1 by brian
clean slate
209
  virtual void range_end() {}
210
211
  virtual bool reverse_sorted() = 0;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
212
  virtual bool unique_key_range()
213
  {
214
    return false;
215
  }
1 by brian
clean slate
216
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
217
  enum 
218
  {
219
    QS_TYPE_RANGE= 0,
220
    QS_TYPE_INDEX_MERGE= 1,
221
    QS_TYPE_RANGE_DESC= 2,
222
    QS_TYPE_ROR_INTERSECT= 4,
223
    QS_TYPE_ROR_UNION= 5,
224
    QS_TYPE_GROUP_MIN_MAX= 6
1 by brian
clean slate
225
  };
226
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
227
  /** Returns the type of this quick select - one of the QS_TYPE_* values */
1 by brian
clean slate
228
  virtual int get_type() = 0;
229
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
230
  /**
231
   * Initialize this quick select as a merged scan inside a ROR-union or a ROR-
232
   * intersection scan. The caller must not additionally call init() if this
233
   * function is called.
234
   *
235
   * @param If true, the quick select may use table->handler,
236
   *        otherwise it must create and use a separate handler
237
   *        object.
238
   *
239
   * @retval
240
   *  0     Ok
241
   * @retval
242
   *  other Error
243
   */
520.9.3 by mordred
zomg. Solaris actually builds all the way!!!
244
  virtual int init_ror_merged_scan(bool)
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
245
  {
246
    assert(0);
247
    return 1;
248
  }
1 by brian
clean slate
249
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
250
  /**
251
   * Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
252
   */
1 by brian
clean slate
253
  virtual void save_last_pos(){};
254
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
255
  /**
256
   * Append comma-separated list of keys this quick select uses to key_names;
257
   * append comma-separated list of corresponding used lengths to used_lengths.
258
   * 
259
   * @note This is used by select_describe.
260
   */
261
  virtual void add_keys_and_lengths(String *key_names, String *used_lengths)=0;
1 by brian
clean slate
262
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
263
  /**
264
   * Append text representation of quick select structure (what and how is
265
   * merged) to str. The result is added to "Extra" field in EXPLAIN output.
266
   *
267
   * @note
268
   *
269
   * This function is implemented only by quick selects that merge other quick
270
   * selects output and/or can produce output suitable for merging.
271
   */
272
  virtual void add_info_string(String *) 
273
  {}
274
  
275
  /**
276
   * Returns true if any index used by this quick select
277
   * uses field which is marked in passed bitmap.
278
   */
1005.2.3 by Monty Taylor
Further reversion of P.
279
  virtual bool is_keys_used(const MY_BITMAP *fields);
1 by brian
clean slate
280
};
281
282
struct st_qsel_param;
283
class PARAM;
284
class SEL_ARG;
285
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
286
/**
287
 * MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
288
 * context.
289
 */
1 by brian
clean slate
290
typedef struct st_quick_range_seq_ctx
291
{
292
  QUICK_RANGE **first;
293
  QUICK_RANGE **cur;
294
  QUICK_RANGE **last;
295
} QUICK_RANGE_SEQ_CTX;
296
482 by Brian Aker
Remove uint.
297
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
298
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
1 by brian
clean slate
299
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
300
/**
301
 * Quick select that does a range scan on a single key. 
302
 *
303
 * The records are returned in key order.
304
 * 
305
 */
1 by brian
clean slate
306
class QUICK_RANGE_SELECT : public QUICK_SELECT_I
307
{
308
protected:
309
  handler *file;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
310
  DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
1 by brian
clean slate
311
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
312
  /** Members to deal with case when this quick select is a ROR-merged scan */
1 by brian
clean slate
313
  bool in_ror_merged_scan;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
314
  MY_BITMAP column_bitmap;
315
  MY_BITMAP *save_read_set;
316
  MY_BITMAP *save_write_set;
317
  bool free_file; /**< True when this->file is "owned" by this quick select */
1 by brian
clean slate
318
319
  /* Range pointers to be used when not using MRR interface */
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
320
  QUICK_RANGE **cur_range; /**< current element in ranges  */
1 by brian
clean slate
321
  QUICK_RANGE *last_range;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
322
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
323
  /** Members needed to use the MRR interface */
1 by brian
clean slate
324
  QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
325
  uint32_t mrr_buf_size; /**< copy from session->variables.read_rnd_buff_size */
326
  HANDLER_BUFFER *mrr_buf_desc; /**< the handler buffer */
1 by brian
clean slate
327
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
328
  /** Info about index we're scanning */
1 by brian
clean slate
329
  KEY_PART *key_parts;
330
  KEY_PART_INFO *key_part_info;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
331
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
332
  bool dont_free; /**< Used by QUICK_SELECT_DESC */
1 by brian
clean slate
333
334
  int cmp_next(QUICK_RANGE *range);
335
  int cmp_prev(QUICK_RANGE *range);
336
  bool row_in_ranges();
337
public:
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
338
  uint32_t mrr_flags; /**< Flags to be used with MRR interface */
1 by brian
clean slate
339
  MEM_ROOT alloc;
340
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
341
  QUICK_RANGE_SELECT(Session *session,
342
                     Table *table,
343
                     uint32_t index_arg,
344
                     bool no_alloc,
345
                     MEM_ROOT *parent_alloc,
346
                     bool *create_err);
1 by brian
clean slate
347
  ~QUICK_RANGE_SELECT();
348
349
  int init();
350
  int reset(void);
351
  int get_next();
352
  void range_end();
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
353
  int get_next_prefix(uint32_t prefix_length,
354
                      key_part_map keypart_map,
481 by Brian Aker
Remove all of uchar.
355
                      unsigned char *cur_prefix);
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
356
  bool reverse_sorted()
357
  {
358
    return false;
359
  }
1 by brian
clean slate
360
  bool unique_key_range();
361
  int init_ror_merged_scan(bool reuse_handler);
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
362
  void save_last_pos();
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
363
  int get_type()
364
  {
365
    return QS_TYPE_RANGE;
366
  }
1 by brian
clean slate
367
  void add_keys_and_lengths(String *key_names, String *used_lengths);
368
  void add_info_string(String *str);
369
private:
370
  /* Used only by QUICK_SELECT_DESC */
371
  QUICK_RANGE_SELECT(const QUICK_RANGE_SELECT& org) : QUICK_SELECT_I()
372
  {
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
373
    memmove(this, &org, sizeof(*this));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
374
    /*
1 by brian
clean slate
375
      Use default MRR implementation for reverse scans. No table engine
376
      currently can do an MRR scan with output in reverse index order.
377
    */
378
    mrr_buf_desc= NULL;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
379
    mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
1 by brian
clean slate
380
    mrr_buf_size= 0;
381
  }
382
  friend class TRP_ROR_INTERSECT;
383
  friend
520.1.22 by Brian Aker
Second pass of thd cleanup
384
  QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
1089.1.14 by Brian Aker
Fix TABLE_REF structure
385
                                               struct table_reference_st *ref,
1 by brian
clean slate
386
                                               ha_rows records);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
387
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
388
                             KEY_PART *key, SEL_ARG *key_tree,
482 by Brian Aker
Remove uint.
389
                             unsigned char *min_key, uint32_t min_key_flag,
390
                             unsigned char *max_key, uint32_t max_key_flag);
391
  friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
1 by brian
clean slate
392
                                              SEL_ARG *key_tree,
482 by Brian Aker
Remove uint.
393
                                              uint32_t mrr_flags,
394
                                              uint32_t mrr_buf_size,
1 by brian
clean slate
395
                                              MEM_ROOT *alloc);
396
  friend class QUICK_SELECT_DESC;
397
  friend class QUICK_INDEX_MERGE_SELECT;
398
  friend class QUICK_ROR_INTERSECT_SELECT;
399
  friend class QUICK_GROUP_MIN_MAX_SELECT;
482 by Brian Aker
Remove uint.
400
  friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
1 by brian
clean slate
401
  friend range_seq_t quick_range_seq_init(void *init_param,
482 by Brian Aker
Remove uint.
402
                                          uint32_t n_ranges, uint32_t flags);
1 by brian
clean slate
403
  friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
404
                              bool distinct,const char *message);
405
};
406
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
407
/**
1 by brian
clean slate
408
  QUICK_INDEX_MERGE_SELECT - index_merge access method quick select.
409
410
    QUICK_INDEX_MERGE_SELECT uses
411
     * QUICK_RANGE_SELECTs to get rows
412
     * Unique class to remove duplicate rows
413
414
  INDEX MERGE OPTIMIZER
415
    Current implementation doesn't detect all cases where index_merge could
416
    be used, in particular:
417
     * index_merge will never be used if range scan is possible (even if
418
       range scan is more expensive)
419
420
     * index_merge+'using index' is not supported (this the consequence of
421
       the above restriction)
422
423
     * If WHERE part contains complex nested AND and OR conditions, some ways
424
       to retrieve rows using index_merge will not be considered. The choice
425
       of read plan may depend on the order of conjuncts/disjuncts in WHERE
426
       part of the query, see comments near imerge_list_or_list and
427
       SEL_IMERGE::or_sel_tree_with_checks functions for details.
428
429
     * There is no "index_merge_ref" method (but index_merge on non-first
430
       table in join is possible with 'range checked for each record').
431
432
    See comments around SEL_IMERGE class and test_quick_select for more
433
    details.
434
435
  ROW RETRIEVAL ALGORITHM
436
437
    index_merge uses Unique class for duplicates removal.  index_merge takes
438
    advantage of Clustered Primary Key (CPK) if the table has one.
439
    The index_merge algorithm consists of two phases:
440
441
    Phase 1 (implemented in QUICK_INDEX_MERGE_SELECT::prepare_unique):
442
    prepare()
443
    {
444
      activate 'index only';
445
      while(retrieve next row for non-CPK scan)
446
      {
447
        if (there is a CPK scan and row will be retrieved by it)
448
          skip this row;
449
        else
450
          put its rowid into Unique;
451
      }
452
      deactivate 'index only';
453
    }
454
455
    Phase 2 (implemented as sequence of QUICK_INDEX_MERGE_SELECT::get_next
456
    calls):
457
458
    fetch()
459
    {
460
      retrieve all rows from row pointers stored in Unique;
461
      free Unique;
462
      retrieve all rows for CPK scan;
463
    }
464
*/
465
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
466
{
467
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
468
  QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
1 by brian
clean slate
469
  ~QUICK_INDEX_MERGE_SELECT();
470
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
471
  int init();
472
  int reset(void);
473
  int get_next();
474
  bool reverse_sorted()
475
  {
476
    return false;
477
  }
478
  bool unique_key_range()
479
  {
480
    return false;
481
  }
482
  int get_type()
483
  {
484
    return QS_TYPE_INDEX_MERGE;
485
  }
1 by brian
clean slate
486
  void add_keys_and_lengths(String *key_names, String *used_lengths);
487
  void add_info_string(String *str);
1005.2.3 by Monty Taylor
Further reversion of P.
488
  bool is_keys_used(const MY_BITMAP *fields);
1 by brian
clean slate
489
490
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
491
492
  /* range quick selects this index_merge read consists of */
493
  List<QUICK_RANGE_SELECT> quick_selects;
494
495
  /* quick select that uses clustered primary key (NULL if none) */
496
  QUICK_RANGE_SELECT* pk_quick_select;
497
498
  /* true if this select is currently doing a clustered PK scan */
499
  bool  doing_pk_scan;
500
501
  MEM_ROOT alloc;
520.1.22 by Brian Aker
Second pass of thd cleanup
502
  Session *session;
1 by brian
clean slate
503
  int read_keys_and_merge();
504
505
  /* used to get rows collected in Unique */
506
  READ_RECORD read_record;
507
};
508
509
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
510
/**
1 by brian
clean slate
511
  Rowid-Ordered Retrieval (ROR) index intersection quick select.
512
  This quick select produces intersection of row sequences returned
513
  by several QUICK_RANGE_SELECTs it "merges".
514
515
  All merged QUICK_RANGE_SELECTs must return rowids in rowid order.
516
  QUICK_ROR_INTERSECT_SELECT will return rows in rowid order, too.
517
518
  All merged quick selects retrieve {rowid, covered_fields} tuples (not full
519
  table records).
520
  QUICK_ROR_INTERSECT_SELECT retrieves full records if it is not being used
521
  by QUICK_ROR_INTERSECT_SELECT and all merged quick selects together don't
522
  cover needed all fields.
523
524
  If one of the merged quick selects is a Clustered PK range scan, it is
525
  used only to filter rowid sequence produced by other merged quick selects.
526
*/
527
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
528
{
529
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
530
  QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
1 by brian
clean slate
531
                             bool retrieve_full_rows,
532
                             MEM_ROOT *parent_alloc);
533
  ~QUICK_ROR_INTERSECT_SELECT();
534
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
535
  int init();
536
  int reset(void);
537
  int get_next();
538
  bool reverse_sorted()
539
  {
540
    return false;
541
  }
542
  bool unique_key_range()
543
  {
544
    return false;
545
  }
546
  int get_type()
547
  {
548
    return QS_TYPE_ROR_INTERSECT;
549
  }
1 by brian
clean slate
550
  void add_keys_and_lengths(String *key_names, String *used_lengths);
551
  void add_info_string(String *str);
1005.2.3 by Monty Taylor
Further reversion of P.
552
  bool is_keys_used(const MY_BITMAP *fields);
1 by brian
clean slate
553
  int init_ror_merged_scan(bool reuse_handler);
554
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
555
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
556
  /**
557
   * Range quick selects this intersection consists of, not including
558
   * cpk_quick.
559
   */
1 by brian
clean slate
560
  List<QUICK_RANGE_SELECT> quick_selects;
561
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
562
  /**
563
   * Merged quick select that uses Clustered PK, if there is one. This quick
564
   * select is not used for row retrieval, it is used for row retrieval.
565
   */
1 by brian
clean slate
566
  QUICK_RANGE_SELECT *cpk_quick;
567
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
568
  MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
569
  Session *session; /**< Pointer to the current session */
570
  bool need_to_fetch_row; /**< if true, do retrieve full table records. */
571
  /** in top-level quick select, true if merged scans where initialized */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
572
  bool scans_inited;
1 by brian
clean slate
573
};
574
957.1.4 by Padraig O'Sullivan
Moving my function object to the header file.
575
957.1.6 by Padraig O'Sullivan
Fixed function object to ensure it correctly returns a boolean type since
576
/*
577
 * This function object is defined in drizzled/opt_range.cc
578
 * We need this here for the priority_queue definition in the
579
 * QUICK_ROR_UNION_SELECT class.
580
 */
581
class compare_functor;
582
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
583
/**
1 by brian
clean slate
584
  Rowid-Ordered Retrieval index union select.
585
  This quick select produces union of row sequences returned by several
586
  quick select it "merges".
587
588
  All merged quick selects must return rowids in rowid order.
589
  QUICK_ROR_UNION_SELECT will return rows in rowid order, too.
590
591
  All merged quick selects are set not to retrieve full table records.
592
  ROR-union quick select always retrieves full records.
593
594
*/
595
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
596
{
597
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
598
  QUICK_ROR_UNION_SELECT(Session *session, Table *table);
1 by brian
clean slate
599
  ~QUICK_ROR_UNION_SELECT();
600
601
  int  init();
602
  int  reset(void);
603
  int  get_next();
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
604
  bool reverse_sorted()
605
  {
606
    return false;
607
  }
608
  bool unique_key_range()
609
  {
610
    return false;
611
  }
612
  int get_type()
613
  {
614
    return QS_TYPE_ROR_UNION;
615
  }
1 by brian
clean slate
616
  void add_keys_and_lengths(String *key_names, String *used_lengths);
617
  void add_info_string(String *str);
1005.2.3 by Monty Taylor
Further reversion of P.
618
  bool is_keys_used(const MY_BITMAP *fields);
1 by brian
clean slate
619
620
  bool push_quick_back(QUICK_SELECT_I *quick_sel_range);
621
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
622
  List<QUICK_SELECT_I> quick_selects; /**< Merged quick selects */
623
624
  /** Priority queue for merge operation */
625
  std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor > *queue;
626
  MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
627
628
  Session *session; /**< current thread */
629
  unsigned char *cur_rowid; /**< buffer used in get_next() */
630
  unsigned char *prev_rowid; /**< rowid of last row returned by get_next() */
631
  bool have_prev_rowid; /**< true if prev_rowid has valid data */
632
  uint32_t rowid_length; /**< table rowid length */
1 by brian
clean slate
633
private:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
634
  bool scans_inited;
1 by brian
clean slate
635
};
636
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
637
/**
1 by brian
clean slate
638
  Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
639
640
  This class provides a specialized index access method for GROUP-BY queries
641
  of the forms:
642
643
       SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)]
644
         FROM T
645
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
646
         [AND EQ(B_1,...,B_m)]
647
         [AND PC(C)]
648
         [AND PA(A_i1,...,A_iq)]
649
       GROUP BY A_1,...,A_k;
650
651
    or
652
653
       SELECT DISTINCT A_i1,...,A_ik
654
         FROM T
655
        WHERE [RNG(A_1,...,A_p ; where p <= k)]
656
         [AND PA(A_i1,...,A_iq)];
657
658
  where all selected fields are parts of the same index.
659
  The class of queries that can be processed by this quick select is fully
660
  specified in the description of get_best_trp_group_min_max() in opt_range.cc.
661
662
  The get_next() method directly produces result tuples, thus obviating the
663
  need to call end_send_group() because all grouping is already done inside
664
  get_next().
665
666
  Since one of the requirements is that all select fields are part of the same
667
  index, this class produces only index keys, and not complete records.
668
*/
669
class QUICK_GROUP_MIN_MAX_SELECT : public QUICK_SELECT_I
670
{
671
private:
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
672
  handler *file; /**< The handler used to get data. */
673
  JOIN *join; /**< Descriptor of the current query */
674
  KEY *index_info; /**< The index chosen for data access */
675
  unsigned char *record; /**< Buffer where the next record is returned. */
676
  unsigned char *tmp_record; /**< Temporary storage for next_min(), next_max(). */
677
  unsigned char *group_prefix; /**< Key prefix consisting of the GROUP fields. */
678
  uint32_t group_prefix_len; /**< Length of the group prefix. */
679
  uint32_t group_key_parts; /**< A number of keyparts in the group prefix */
680
  unsigned char *last_prefix; /**< Prefix of the last group for detecting EOF. */
681
  bool have_min; /**< Specify whether we are computing */
682
  bool have_max; /**< a MIN, a MAX, or both. */
683
  bool seen_first_key; /**< Denotes whether the first key was retrieved.*/
684
  KEY_PART_INFO *min_max_arg_part; /** The keypart of the only argument field of all MIN/MAX functions. */
685
  uint32_t min_max_arg_len; /**< The length of the MIN/MAX argument field */
686
  unsigned char *key_infix; /**< Infix of constants from equality predicates. */
482 by Brian Aker
Remove uint.
687
  uint32_t key_infix_len;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
688
  DYNAMIC_ARRAY min_max_ranges; /**< Array of range ptrs for the MIN/MAX field. */
689
  uint32_t real_prefix_len; /**< Length of key prefix extended with key_infix. */
690
  uint32_t real_key_parts;  /**< A number of keyparts in the above value.      */
1 by brian
clean slate
691
  List<Item_sum> *min_functions;
692
  List<Item_sum> *max_functions;
693
  List_iterator<Item_sum> *min_functions_it;
694
  List_iterator<Item_sum> *max_functions_it;
695
public:
696
  /*
697
    The following two members are public to allow easy access from
698
    TRP_GROUP_MIN_MAX::make_quick()
699
  */
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
700
  MEM_ROOT alloc; /**< Memory pool for this and quick_prefix_select data. */
701
  QUICK_RANGE_SELECT *quick_prefix_select; /**< For retrieval of group prefixes. */
1 by brian
clean slate
702
private:
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
703
  int next_prefix();
704
  int next_min_in_range();
705
  int next_max_in_range();
706
  int next_min();
707
  int next_max();
1 by brian
clean slate
708
  void update_min_result();
709
  void update_max_result();
710
public:
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
711
  QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
1 by brian
clean slate
712
                             bool have_max, KEY_PART_INFO *min_max_arg_part,
482 by Brian Aker
Remove uint.
713
                             uint32_t group_prefix_len, uint32_t group_key_parts,
714
                             uint32_t used_key_parts, KEY *index_info, uint
1 by brian
clean slate
715
                             use_index, double read_cost, ha_rows records, uint
481 by Brian Aker
Remove all of uchar.
716
                             key_infix_len, unsigned char *key_infix, MEM_ROOT
1 by brian
clean slate
717
                             *parent_alloc);
718
  ~QUICK_GROUP_MIN_MAX_SELECT();
719
  bool add_range(SEL_ARG *sel_range);
720
  void update_key_stat();
721
  void adjust_prefix_ranges();
722
  bool alloc_buffers();
723
  int init();
724
  int reset();
725
  int get_next();
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
726
  bool reverse_sorted()
727
  {
728
    return false;
729
  }
730
  bool unique_key_range()
731
  {
732
    return false;
733
  }
734
  int get_type()
735
  {
736
    return QS_TYPE_GROUP_MIN_MAX;
737
  }
1 by brian
clean slate
738
  void add_keys_and_lengths(String *key_names, String *used_lengths);
739
};
740
741
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
742
{
743
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
744
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
1 by brian
clean slate
745
                    bool *create_err);
746
  int get_next();
747
  bool reverse_sorted() { return 1; }
748
  int get_type() { return QS_TYPE_RANGE_DESC; }
749
private:
750
  bool range_reads_after_key(QUICK_RANGE *range);
751
  int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
752
  List<QUICK_RANGE> rev_ranges;
753
  List_iterator<QUICK_RANGE> rev_it;
754
};
755
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
756
/**
757
 * Executor class for SELECT statements.
758
 *
759
 * @details
760
 *
761
 * The QUICK_SELECT_I member variable is the implementor
762
 * of the SELECT execution.
763
 */
1 by brian
clean slate
764
class SQL_SELECT :public Sql_alloc {
765
 public:
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
766
  QUICK_SELECT_I *quick; /**< If quick-select used */
767
  COND *cond; /**< where condition */
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
768
  Table	*head;
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
769
  IO_CACHE file; /**< Positions to used records */
770
  ha_rows records; /**< Records in use if read from file */
771
  double read_time; /**< Time to read rows */
772
  key_map quick_keys; /**< Possible quick keys */
773
  key_map needed_reg; /**< Possible quick keys after prev tables. */
774
  table_map const_tables;
775
  table_map read_tables;
776
  bool free_cond;
1 by brian
clean slate
777
778
  SQL_SELECT();
779
  ~SQL_SELECT();
780
  void cleanup();
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
781
  bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
782
  bool skip_record();
520.1.22 by Brian Aker
Second pass of thd cleanup
783
  int test_quick_select(Session *session, key_map keys, table_map prev_tables,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
784
                        ha_rows limit, bool force_quick_range,
1 by brian
clean slate
785
                        bool ordered_output);
786
};
787
520.1.22 by Brian Aker
Second pass of thd cleanup
788
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
1089.1.14 by Brian Aker
Fix TABLE_REF structure
789
                                             struct table_reference_st *ref,
1 by brian
clean slate
790
                                             ha_rows records);
482 by Brian Aker
Remove uint.
791
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);
1 by brian
clean slate
792
793
#endif