~drizzle-pbxt/drizzle/drizzle-pbxt-2

« back to all changes in this revision

Viewing changes to drizzled/opt_range.h

  • Committer: Paul McCullagh
  • Date: 2009-11-10 14:18:39 UTC
  • mfrom: (1038.1.7 drizzle-pbxt-pre-merge)
  • Revision ID: paul.mccullagh@primebase.org-20091110141839-2j3k43b17ag6f605
Merged Drizzle trunk and PBXT 1.0.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
 
21
20
/* classes to use when handling where clause */
22
21
 
23
22
#ifndef DRIZZLED_OPT_RANGE_H
32
31
 
33
32
typedef struct st_handler_buffer HANDLER_BUFFER;
34
33
 
35
 
typedef struct st_key_part {
36
 
  uint16_t           key,part;
 
34
typedef struct st_key_part
 
35
{
 
36
  uint16_t key;
 
37
  uint16_t part;
37
38
  /* See KEY_PART_INFO for meaning of the next two: */
38
 
  uint16_t           store_length, length;
39
 
  uint8_t            null_bit;
40
 
  /*
 
39
  uint16_t store_length;
 
40
  uint16_t length;
 
41
  uint8_t null_bit;
 
42
  /**
41
43
    Keypart flags (0 when this structure is used by partition pruning code
42
44
    for fake partitioning index description)
43
45
  */
44
46
  uint8_t flag;
45
 
  Field            *field;
46
 
  Field::imagetype image_type;
 
47
  Field *field;
47
48
} KEY_PART;
48
49
 
49
 
 
50
 
class QUICK_RANGE :public Sql_alloc {
51
 
 public:
52
 
  unsigned char *min_key,*max_key;
53
 
  uint16_t min_length,max_length,flag;
54
 
  key_part_map min_keypart_map, // bitmap of used keyparts in min_key
55
 
               max_keypart_map; // bitmap of used keyparts in max_key
 
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 */
56
60
#ifdef HAVE_purify
57
61
  uint16_t dummy;                                       /* Avoid warnings on 'flag' */
58
62
#endif
59
 
  QUICK_RANGE();                                /* Full range */
60
 
  QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
 
63
  QUICK_RANGE(); /**< Constructor for a "full range" */
 
64
  QUICK_RANGE(const unsigned char *min_key_arg,
 
65
              uint32_t min_length_arg,
61
66
              key_part_map min_keypart_map_arg,
62
 
              const unsigned char *max_key_arg, uint32_t max_length_arg,
 
67
                    const unsigned char *max_key_arg, 
 
68
              uint32_t max_length_arg,
63
69
              key_part_map max_keypart_map_arg,
64
 
              uint32_t flag_arg)
65
 
    : min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
 
70
                    uint32_t flag_arg)
 
71
    : 
 
72
      min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
66
73
      max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
67
74
      min_length((uint16_t) min_length_arg),
68
75
      max_length((uint16_t) max_length_arg),
76
83
    }
77
84
};
78
85
 
79
 
 
80
 
/*
 
86
/**
81
87
  Quick select interface.
82
88
  This class is a parent for all QUICK_*_SELECT classes.
83
89
 
119
125
    delete quick;
120
126
 
121
127
*/
122
 
 
123
128
class QUICK_SELECT_I
124
129
{
125
130
public:
126
131
  bool sorted;
127
 
  ha_rows records;  /* estimate of # of records to be retrieved */
128
 
  double  read_time; /* time to perform this retrieval          */
129
 
  Table   *head;
130
 
  /*
 
132
  ha_rows records; /**< estimate of # of records to be retrieved */
 
133
  double read_time; /**< time to perform this retrieval */
 
134
  Table *head;
 
135
  /**
131
136
    Index this quick select uses, or MAX_KEY for quick selects
132
137
    that use several indexes
133
138
  */
134
139
  uint32_t index;
135
 
 
136
 
  /*
 
140
  /**
137
141
    Total length of first used_key_parts parts of the key.
138
142
    Applicable if index!= MAX_KEY.
139
143
  */
140
144
  uint32_t max_used_key_length;
141
 
 
142
 
  /*
143
 
    Max. number of (first) key parts this quick select uses for retrieval.
 
145
  /**
 
146
    Maximum number of (first) key parts this quick select uses for retrieval.
144
147
    eg. for "(key1p1=c1 AND key1p2=c2) OR key1p1=c2" used_key_parts == 2.
145
148
    Applicable if index!= MAX_KEY.
146
149
 
147
150
    For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
148
151
  */
149
152
  uint32_t used_key_parts;
 
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;
150
163
 
151
164
  QUICK_SELECT_I();
152
165
  virtual ~QUICK_SELECT_I(){};
153
166
 
154
 
  /*
155
 
    Do post-constructor initialization.
156
 
    SYNOPSIS
157
 
      init()
158
 
 
159
 
    init() performs initializations that should have been in constructor if
160
 
    it was possible to return errors from constructors. The join optimizer may
161
 
    create and then delete quick selects without retrieving any rows so init()
162
 
    must not contain any IO or CPU intensive code.
163
 
 
164
 
    If init() call fails the only valid action is to delete this quick select,
165
 
    reset() and get_next() must not be called.
166
 
 
167
 
    RETURN
168
 
      0      OK
169
 
      other  Error code
170
 
  */
171
 
  virtual int  init() = 0;
172
 
 
173
 
  /*
174
 
    Initialize quick select for row retrieval.
175
 
    SYNOPSIS
176
 
      reset()
177
 
 
178
 
    reset() should be called when it is certain that row retrieval will be
179
 
    necessary. This call may do heavyweight initialization like buffering first
180
 
    N records etc. If reset() call fails get_next() must not be called.
181
 
    Note that reset() may be called several times if
182
 
     * the quick select is executed in a subselect
183
 
     * a JOIN buffer is used
184
 
 
185
 
    RETURN
186
 
      0      OK
187
 
      other  Error code
188
 
  */
189
 
  virtual int  reset(void) = 0;
190
 
 
191
 
  virtual int  get_next() = 0;   /* get next record to retrieve */
192
 
 
193
 
  /* Range end should be called when we have looped over the whole index */
 
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 */
194
209
  virtual void range_end() {}
195
210
 
196
211
  virtual bool reverse_sorted() = 0;
197
 
  virtual bool unique_key_range() { return false; }
 
212
  virtual bool unique_key_range()
 
213
  {
 
214
    return false;
 
215
  }
198
216
 
199
 
  enum {
200
 
    QS_TYPE_RANGE = 0,
201
 
    QS_TYPE_INDEX_MERGE = 1,
202
 
    QS_TYPE_RANGE_DESC = 2,
203
 
    QS_TYPE_ROR_INTERSECT = 4,
204
 
    QS_TYPE_ROR_UNION = 5,
205
 
    QS_TYPE_GROUP_MIN_MAX = 6
 
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
206
225
  };
207
226
 
208
 
  /* Get type of this quick select - one of the QS_TYPE_* values */
 
227
  /** Returns the type of this quick select - one of the QS_TYPE_* values */
209
228
  virtual int get_type() = 0;
210
229
 
211
 
  /*
212
 
    Initialize this quick select as a merged scan inside a ROR-union or a ROR-
213
 
    intersection scan. The caller must not additionally call init() if this
214
 
    function is called.
215
 
    SYNOPSIS
216
 
      init_ror_merged_scan()
217
 
        reuse_handler  If true, the quick select may use table->handler,
218
 
                       otherwise it must create and use a separate handler
219
 
                       object.
220
 
    RETURN
221
 
      0     Ok
222
 
      other Error
223
 
  */
 
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->Cursor,
 
236
   *        otherwise it must create and use a separate Cursor
 
237
   *        object.
 
238
   *
 
239
   * @retval
 
240
   *  0     Ok
 
241
   * @retval
 
242
   *  other Error
 
243
   */
224
244
  virtual int init_ror_merged_scan(bool)
225
 
  { assert(0); return 1; }
 
245
  {
 
246
    assert(0);
 
247
    return 1;
 
248
  }
226
249
 
227
 
  /*
228
 
    Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
229
 
  */
 
250
  /**
 
251
   * Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
 
252
   */
230
253
  virtual void save_last_pos(){};
231
254
 
232
 
  /*
233
 
    Append comma-separated list of keys this quick select uses to key_names;
234
 
    append comma-separated list of corresponding used lengths to used_lengths.
235
 
    This is used by select_describe.
236
 
  */
237
 
  virtual void add_keys_and_lengths(String *key_names,
238
 
                                    String *used_lengths)=0;
239
 
 
240
 
  /*
241
 
    Append text representation of quick select structure (what and how is
242
 
    merged) to str. The result is added to "Extra" field in EXPLAIN output.
243
 
    This function is implemented only by quick selects that merge other quick
244
 
    selects output and/or can produce output suitable for merging.
245
 
  */
246
 
  virtual void add_info_string(String *) {};
247
 
  /*
248
 
    Return 1 if any index used by this quick select
249
 
    uses field which is marked in passed bitmap.
250
 
  */
251
 
  virtual bool is_keys_used(const MY_BITMAP *fields);
252
 
 
253
 
  /*
254
 
    rowid of last row retrieved by this quick select. This is used only when
255
 
    doing ROR-index_merge selects
256
 
  */
257
 
  unsigned char    *last_rowid;
258
 
 
259
 
  /*
260
 
    Table record buffer used by this quick select.
261
 
  */
262
 
  unsigned char    *record;
 
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;
 
262
 
 
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
   */
 
279
  virtual bool is_keys_used(const MyBitmap *fields);
263
280
};
264
281
 
265
 
 
266
282
struct st_qsel_param;
267
283
class PARAM;
268
284
class SEL_ARG;
269
285
 
270
 
 
271
 
/*
272
 
  MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
273
 
  context.
274
 
*/
 
286
/**
 
287
 * MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
 
288
 * context.
 
289
 */
275
290
typedef struct st_quick_range_seq_ctx
276
291
{
277
292
  QUICK_RANGE **first;
282
297
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
283
298
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
284
299
 
285
 
 
286
 
/*
287
 
  Quick select that does a range scan on a single key. The records are
288
 
  returned in key order.
289
 
*/
 
300
/**
 
301
 * Quick select that does a range scan on a single key. 
 
302
 *
 
303
 * The records are returned in key order.
 
304
 * 
 
305
 */
290
306
class QUICK_RANGE_SELECT : public QUICK_SELECT_I
291
307
{
292
308
protected:
293
 
  handler *file;
294
 
  DYNAMIC_ARRAY ranges;     /* ordered array of range ptrs */
 
309
  Cursor *file;
 
310
  DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
295
311
 
296
 
  /* Members to deal with case when this quick select is a ROR-merged scan */
 
312
  /** Members to deal with case when this quick select is a ROR-merged scan */
297
313
  bool in_ror_merged_scan;
298
 
  MY_BITMAP column_bitmap, *save_read_set, *save_write_set;
299
 
  bool free_file;   /* TRUE <=> this->file is "owned" by this quick select */
 
314
  MyBitmap column_bitmap;
 
315
  MyBitmap *save_read_set;
 
316
  MyBitmap *save_write_set;
 
317
  bool free_file; /**< True when this->file is "owned" by this quick select */
300
318
 
301
319
  /* Range pointers to be used when not using MRR interface */
302
 
  QUICK_RANGE **cur_range;  /* current element in ranges  */
 
320
  QUICK_RANGE **cur_range; /**< current element in ranges  */
303
321
  QUICK_RANGE *last_range;
304
322
 
305
 
  /* Members needed to use the MRR interface */
 
323
  /** Members needed to use the MRR interface */
306
324
  QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
307
 
public:
308
 
  uint32_t mrr_flags; /* Flags to be used with MRR interface */
309
 
protected:
310
 
  uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
311
 
  HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
 
325
  uint32_t mrr_buf_size; /**< copy from session->variables.read_rnd_buff_size */
 
326
  HANDLER_BUFFER *mrr_buf_desc; /**< the Cursor buffer */
312
327
 
313
 
  /* Info about index we're scanning */
 
328
  /** Info about index we're scanning */
314
329
  KEY_PART *key_parts;
315
330
  KEY_PART_INFO *key_part_info;
316
331
 
317
 
  bool dont_free; /* Used by QUICK_SELECT_DESC */
 
332
  bool dont_free; /**< Used by QUICK_SELECT_DESC */
318
333
 
319
334
  int cmp_next(QUICK_RANGE *range);
320
335
  int cmp_prev(QUICK_RANGE *range);
321
336
  bool row_in_ranges();
322
337
public:
 
338
  uint32_t mrr_flags; /**< Flags to be used with MRR interface */
323
339
  MEM_ROOT alloc;
324
340
 
325
 
  QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
326
 
                     MEM_ROOT *parent_alloc, bool *create_err);
 
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);
327
347
  ~QUICK_RANGE_SELECT();
328
348
 
329
349
  int init();
330
350
  int reset(void);
331
351
  int get_next();
332
352
  void range_end();
333
 
  int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
 
353
  int get_next_prefix(uint32_t prefix_length,
 
354
                      key_part_map keypart_map,
334
355
                      unsigned char *cur_prefix);
335
 
  bool reverse_sorted() { return 0; }
 
356
  bool reverse_sorted()
 
357
  {
 
358
    return false;
 
359
  }
336
360
  bool unique_key_range();
337
361
  int init_ror_merged_scan(bool reuse_handler);
338
362
  void save_last_pos();
339
 
  int get_type() { return QS_TYPE_RANGE; }
 
363
  int get_type()
 
364
  {
 
365
    return QS_TYPE_RANGE;
 
366
  }
340
367
  void add_keys_and_lengths(String *key_names, String *used_lengths);
341
368
  void add_info_string(String *str);
342
369
private:
349
376
      currently can do an MRR scan with output in reverse index order.
350
377
    */
351
378
    mrr_buf_desc= NULL;
352
 
    mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
 
379
    mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
353
380
    mrr_buf_size= 0;
354
381
  }
355
382
  friend class TRP_ROR_INTERSECT;
356
383
  friend
357
384
  QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
358
 
                                               struct st_table_ref *ref,
 
385
                                               struct table_reference_st *ref,
359
386
                                               ha_rows records);
360
387
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
361
388
                             KEY_PART *key, SEL_ARG *key_tree,
377
404
                              bool distinct,const char *message);
378
405
};
379
406
 
380
 
 
381
 
/*
 
407
/**
382
408
  QUICK_INDEX_MERGE_SELECT - index_merge access method quick select.
383
409
 
384
410
    QUICK_INDEX_MERGE_SELECT uses
436
462
      retrieve all rows for CPK scan;
437
463
    }
438
464
*/
439
 
 
440
465
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
441
466
{
442
467
public:
443
468
  QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
444
469
  ~QUICK_INDEX_MERGE_SELECT();
445
470
 
446
 
  int  init();
447
 
  int  reset(void);
448
 
  int  get_next();
449
 
  bool reverse_sorted() { return false; }
450
 
  bool unique_key_range() { return false; }
451
 
  int get_type() { return QS_TYPE_INDEX_MERGE; }
 
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
  }
452
486
  void add_keys_and_lengths(String *key_names, String *used_lengths);
453
487
  void add_info_string(String *str);
454
 
  bool is_keys_used(const MY_BITMAP *fields);
 
488
  bool is_keys_used(const MyBitmap *fields);
455
489
 
456
490
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
457
491
 
473
507
};
474
508
 
475
509
 
476
 
/*
 
510
/**
477
511
  Rowid-Ordered Retrieval (ROR) index intersection quick select.
478
512
  This quick select produces intersection of row sequences returned
479
513
  by several QUICK_RANGE_SELECTs it "merges".
490
524
  If one of the merged quick selects is a Clustered PK range scan, it is
491
525
  used only to filter rowid sequence produced by other merged quick selects.
492
526
*/
493
 
 
494
527
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
495
528
{
496
529
public:
499
532
                             MEM_ROOT *parent_alloc);
500
533
  ~QUICK_ROR_INTERSECT_SELECT();
501
534
 
502
 
  int  init();
503
 
  int  reset(void);
504
 
  int  get_next();
505
 
  bool reverse_sorted() { return false; }
506
 
  bool unique_key_range() { return false; }
507
 
  int get_type() { return QS_TYPE_ROR_INTERSECT; }
 
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
  }
508
550
  void add_keys_and_lengths(String *key_names, String *used_lengths);
509
551
  void add_info_string(String *str);
510
 
  bool is_keys_used(const MY_BITMAP *fields);
 
552
  bool is_keys_used(const MyBitmap *fields);
511
553
  int init_ror_merged_scan(bool reuse_handler);
512
554
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
513
555
 
514
 
  /*
515
 
    Range quick selects this intersection consists of, not including
516
 
    cpk_quick.
517
 
  */
 
556
  /**
 
557
   * Range quick selects this intersection consists of, not including
 
558
   * cpk_quick.
 
559
   */
518
560
  List<QUICK_RANGE_SELECT> quick_selects;
519
561
 
520
 
  /*
521
 
    Merged quick select that uses Clustered PK, if there is one. This quick
522
 
    select is not used for row retrieval, it is used for row retrieval.
523
 
  */
 
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
   */
524
566
  QUICK_RANGE_SELECT *cpk_quick;
525
567
 
526
 
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
527
 
  Session *session;       /* current thread */
528
 
  bool need_to_fetch_row; /* if true, do retrieve full table records. */
529
 
  /* in top-level quick select, true if merged scans where initialized */
 
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 */
530
572
  bool scans_inited;
531
573
};
532
574
 
538
580
 */
539
581
class compare_functor;
540
582
 
541
 
 
542
 
/*
 
583
/**
543
584
  Rowid-Ordered Retrieval index union select.
544
585
  This quick select produces union of row sequences returned by several
545
586
  quick select it "merges".
551
592
  ROR-union quick select always retrieves full records.
552
593
 
553
594
*/
554
 
 
555
595
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
556
596
{
557
597
public:
561
601
  int  init();
562
602
  int  reset(void);
563
603
  int  get_next();
564
 
  bool reverse_sorted() { return false; }
565
 
  bool unique_key_range() { return false; }
566
 
  int get_type() { return QS_TYPE_ROR_UNION; }
 
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
  }
567
616
  void add_keys_and_lengths(String *key_names, String *used_lengths);
568
617
  void add_info_string(String *str);
569
 
  bool is_keys_used(const MY_BITMAP *fields);
 
618
  bool is_keys_used(const MyBitmap *fields);
570
619
 
571
620
  bool push_quick_back(QUICK_SELECT_I *quick_sel_range);
572
621
 
573
 
  List<QUICK_SELECT_I> quick_selects; /* Merged quick selects */
574
 
 
575
 
  /* Priority queue for merge operation */
576
 
  std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor >
577
 
    *queue;
578
 
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
579
 
 
580
 
  Session *session;             /* current thread */
581
 
  unsigned char *cur_rowid;      /* buffer used in get_next() */
582
 
  unsigned char *prev_rowid;     /* rowid of last row returned by get_next() */
583
 
  bool have_prev_rowid; /* true if prev_rowid has valid data */
584
 
  uint32_t rowid_length;    /* table rowid length */
 
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 */
585
633
private:
586
634
  bool scans_inited;
587
635
};
588
636
 
589
 
/*
 
637
/**
590
638
  Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
591
639
 
592
640
  This class provides a specialized index access method for GROUP-BY queries
618
666
  Since one of the requirements is that all select fields are part of the same
619
667
  index, this class produces only index keys, and not complete records.
620
668
*/
621
 
 
622
669
class QUICK_GROUP_MIN_MAX_SELECT : public QUICK_SELECT_I
623
670
{
624
671
private:
625
 
  handler *file;         /* The handler used to get data. */
626
 
  JOIN *join;            /* Descriptor of the current query */
627
 
  KEY  *index_info;      /* The index chosen for data access */
628
 
  unsigned char *record;          /* Buffer where the next record is returned. */
629
 
  unsigned char *tmp_record;      /* Temporary storage for next_min(), next_max(). */
630
 
  unsigned char *group_prefix;    /* Key prefix consisting of the GROUP fields. */
631
 
  uint32_t group_prefix_len; /* Length of the group prefix. */
632
 
  uint32_t group_key_parts;  /* A number of keyparts in the group prefix */
633
 
  unsigned char *last_prefix;     /* Prefix of the last group for detecting EOF. */
634
 
  bool have_min;         /* Specify whether we are computing */
635
 
  bool have_max;         /*   a MIN, a MAX, or both.         */
636
 
  bool seen_first_key;   /* Denotes whether the first key was retrieved.*/
637
 
  KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
638
 
                                   /* of all MIN/MAX functions.              */
639
 
  uint32_t min_max_arg_len;  /* The length of the MIN/MAX argument field */
640
 
  unsigned char *key_infix;       /* Infix of constants from equality predicates. */
 
672
  Cursor *file; /**< The Cursor 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. */
641
687
  uint32_t key_infix_len;
642
 
  DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
643
 
  uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
644
 
  uint32_t real_key_parts;  /* A number of keyparts in the above value.      */
 
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.      */
645
691
  List<Item_sum> *min_functions;
646
692
  List<Item_sum> *max_functions;
647
693
  List_iterator<Item_sum> *min_functions_it;
651
697
    The following two members are public to allow easy access from
652
698
    TRP_GROUP_MIN_MAX::make_quick()
653
699
  */
654
 
  MEM_ROOT alloc; /* Memory pool for this and quick_prefix_select data. */
655
 
  QUICK_RANGE_SELECT *quick_prefix_select;/* For retrieval of group prefixes. */
 
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. */
656
702
private:
657
 
  int  next_prefix();
658
 
  int  next_min_in_range();
659
 
  int  next_max_in_range();
660
 
  int  next_min();
661
 
  int  next_max();
 
703
  int next_prefix();
 
704
  int next_min_in_range();
 
705
  int next_max_in_range();
 
706
  int next_min();
 
707
  int next_max();
662
708
  void update_min_result();
663
709
  void update_max_result();
664
710
public:
677
723
  int init();
678
724
  int reset();
679
725
  int get_next();
680
 
  bool reverse_sorted() { return false; }
681
 
  bool unique_key_range() { return false; }
682
 
  int get_type() { return QS_TYPE_GROUP_MIN_MAX; }
 
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
  }
683
738
  void add_keys_and_lengths(String *key_names, String *used_lengths);
684
739
};
685
740
 
686
 
 
687
741
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
688
742
{
689
743
public:
699
753
  List_iterator<QUICK_RANGE> rev_it;
700
754
};
701
755
 
702
 
 
 
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
 */
703
764
class SQL_SELECT :public Sql_alloc {
704
765
 public:
705
 
  QUICK_SELECT_I *quick;        // If quick-select used
706
 
  COND          *cond;          // where condition
 
766
  QUICK_SELECT_I *quick; /**< If quick-select used */
 
767
  COND *cond; /**< where condition */
707
768
  Table *head;
708
 
  IO_CACHE file;                // Positions to used records
709
 
  ha_rows records;              // Records in use if read from file
710
 
  double read_time;             // Time to read rows
711
 
  key_map quick_keys;           // Possible quick keys
712
 
  key_map needed_reg;           // Possible quick keys after prev tables.
713
 
  table_map const_tables,read_tables;
714
 
  bool  free_cond;
 
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;
715
777
 
716
778
  SQL_SELECT();
717
779
  ~SQL_SELECT();
724
786
};
725
787
 
726
788
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
727
 
                                             struct st_table_ref *ref,
 
789
                                             struct table_reference_st *ref,
728
790
                                             ha_rows records);
729
791
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);
730
792
 
731
 
#endif
 
793
#endif /* DRIZZLED_OPT_RANGE_H */