~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.h

  • Committer: Brian Aker
  • Date: 2009-04-27 14:36:40 UTC
  • Revision ID: brian@gaz-20090427143640-f6zjmtt9vm55qgm2
Patch on show processlist from  davi@apache.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#ifndef DRIZZLED_OPT_RANGE_H
24
24
#define DRIZZLED_OPT_RANGE_H
25
25
 
26
 
#ifdef USE_PRAGMA_INTERFACE
27
 
#pragma interface                       /* gcc class implementation */
28
 
#endif
 
26
#include <drizzled/field.h>
 
27
#include <drizzled/item/sum.h>
 
28
#include <queue>
 
29
#include <bitset>
 
30
 
 
31
class JOIN;
 
32
typedef class Item COND;
 
33
 
 
34
typedef struct st_handler_buffer HANDLER_BUFFER;
29
35
 
30
36
typedef struct st_key_part {
31
37
  uint16_t           key,part;
173
179
    reset() should be called when it is certain that row retrieval will be
174
180
    necessary. This call may do heavyweight initialization like buffering first
175
181
    N records etc. If reset() call fails get_next() must not be called.
176
 
    Note that reset() may be called several times if 
 
182
    Note that reset() may be called several times if
177
183
     * the quick select is executed in a subselect
178
184
     * a JOIN buffer is used
179
 
    
 
185
 
180
186
    RETURN
181
187
      0      OK
182
188
      other  Error code
216
222
      0     Ok
217
223
      other Error
218
224
  */
219
 
  virtual int init_ror_merged_scan(bool reuse_handler __attribute__((unused)))
 
225
  virtual int init_ror_merged_scan(bool)
220
226
  { assert(0); return 1; }
221
227
 
222
228
  /*
238
244
    This function is implemented only by quick selects that merge other quick
239
245
    selects output and/or can produce output suitable for merging.
240
246
  */
241
 
  virtual void add_info_string(String *str __attribute__((unused))) {};
 
247
  virtual void add_info_string(String *) {};
242
248
  /*
243
249
    Return 1 if any index used by this quick select
244
250
    uses field which is marked in passed bitmap.
245
251
  */
246
 
  virtual bool is_keys_used(const MY_BITMAP *fields);
 
252
  virtual bool is_keys_used(const std::bitset<MAX_FIELDS> *fields);
247
253
 
248
254
  /*
249
255
    rowid of last row retrieved by this quick select. This is used only when
290
296
 
291
297
  /* Members to deal with case when this quick select is a ROR-merged scan */
292
298
  bool in_ror_merged_scan;
293
 
  MY_BITMAP column_bitmap, *save_read_set, *save_write_set;
 
299
  std::bitset<MAX_FIELDS> column_bitmap, *save_read_set, *save_write_set;
294
300
  bool free_file;   /* TRUE <=> this->file is "owned" by this quick select */
295
301
 
296
302
  /* Range pointers to be used when not using MRR interface */
297
303
  QUICK_RANGE **cur_range;  /* current element in ranges  */
298
304
  QUICK_RANGE *last_range;
299
 
  
 
305
 
300
306
  /* Members needed to use the MRR interface */
301
307
  QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
302
308
public:
303
309
  uint32_t mrr_flags; /* Flags to be used with MRR interface */
304
310
protected:
305
 
  uint32_t mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */  
 
311
  uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
306
312
  HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
307
313
 
308
314
  /* Info about index we're scanning */
309
315
  KEY_PART *key_parts;
310
316
  KEY_PART_INFO *key_part_info;
311
 
  
 
317
 
312
318
  bool dont_free; /* Used by QUICK_SELECT_DESC */
313
319
 
314
320
  int cmp_next(QUICK_RANGE *range);
317
323
public:
318
324
  MEM_ROOT alloc;
319
325
 
320
 
  QUICK_RANGE_SELECT(THD *thd, Table *table,uint32_t index_arg,bool no_alloc,
321
 
                     MEM_ROOT *parent_alloc, bool *create_err);
 
326
  QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
 
327
                     MEM_ROOT *parent_alloc);
322
328
  ~QUICK_RANGE_SELECT();
323
329
 
324
330
  int init();
330
336
  bool reverse_sorted() { return 0; }
331
337
  bool unique_key_range();
332
338
  int init_ror_merged_scan(bool reuse_handler);
333
 
  void save_last_pos()
334
 
  { file->position(record); }
 
339
  void save_last_pos();
335
340
  int get_type() { return QS_TYPE_RANGE; }
336
341
  void add_keys_and_lengths(String *key_names, String *used_lengths);
337
342
  void add_info_string(String *str);
340
345
  QUICK_RANGE_SELECT(const QUICK_RANGE_SELECT& org) : QUICK_SELECT_I()
341
346
  {
342
347
    memmove(this, &org, sizeof(*this));
343
 
    /* 
 
348
    /*
344
349
      Use default MRR implementation for reverse scans. No table engine
345
350
      currently can do an MRR scan with output in reverse index order.
346
351
    */
350
355
  }
351
356
  friend class TRP_ROR_INTERSECT;
352
357
  friend
353
 
  QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
 
358
  QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
354
359
                                               struct st_table_ref *ref,
355
360
                                               ha_rows records);
356
 
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick, 
357
 
                             KEY_PART *key, SEL_ARG *key_tree, 
 
361
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
 
362
                             KEY_PART *key, SEL_ARG *key_tree,
358
363
                             unsigned char *min_key, uint32_t min_key_flag,
359
364
                             unsigned char *max_key, uint32_t max_key_flag);
360
365
  friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
436
441
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
437
442
{
438
443
public:
439
 
  QUICK_INDEX_MERGE_SELECT(THD *thd, Table *table);
 
444
  QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
440
445
  ~QUICK_INDEX_MERGE_SELECT();
441
446
 
442
447
  int  init();
447
452
  int get_type() { return QS_TYPE_INDEX_MERGE; }
448
453
  void add_keys_and_lengths(String *key_names, String *used_lengths);
449
454
  void add_info_string(String *str);
450
 
  bool is_keys_used(const MY_BITMAP *fields);
 
455
  bool is_keys_used(const std::bitset<MAX_FIELDS> *fields);
451
456
 
452
457
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
453
458
 
461
466
  bool  doing_pk_scan;
462
467
 
463
468
  MEM_ROOT alloc;
464
 
  THD *thd;
 
469
  Session *session;
465
470
  int read_keys_and_merge();
466
471
 
467
472
  /* used to get rows collected in Unique */
490
495
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
491
496
{
492
497
public:
493
 
  QUICK_ROR_INTERSECT_SELECT(THD *thd, Table *table,
 
498
  QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
494
499
                             bool retrieve_full_rows,
495
500
                             MEM_ROOT *parent_alloc);
496
501
  ~QUICK_ROR_INTERSECT_SELECT();
503
508
  int get_type() { return QS_TYPE_ROR_INTERSECT; }
504
509
  void add_keys_and_lengths(String *key_names, String *used_lengths);
505
510
  void add_info_string(String *str);
506
 
  bool is_keys_used(const MY_BITMAP *fields);
 
511
  bool is_keys_used(const std::bitset<MAX_FIELDS> *fields);
507
512
  int init_ror_merged_scan(bool reuse_handler);
508
513
  bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
509
514
 
520
525
  QUICK_RANGE_SELECT *cpk_quick;
521
526
 
522
527
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
523
 
  THD *thd;       /* current thread */
 
528
  Session *session;       /* current thread */
524
529
  bool need_to_fetch_row; /* if true, do retrieve full table records. */
525
530
  /* in top-level quick select, true if merged scans where initialized */
526
 
  bool scans_inited; 
 
531
  bool scans_inited;
527
532
};
528
533
 
529
534
 
530
535
/*
 
536
 * This function object is defined in drizzled/opt_range.cc
 
537
 * We need this here for the priority_queue definition in the
 
538
 * QUICK_ROR_UNION_SELECT class.
 
539
 */
 
540
class compare_functor;
 
541
 
 
542
 
 
543
/*
531
544
  Rowid-Ordered Retrieval index union select.
532
545
  This quick select produces union of row sequences returned by several
533
546
  quick select it "merges".
543
556
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
544
557
{
545
558
public:
546
 
  QUICK_ROR_UNION_SELECT(THD *thd, Table *table);
 
559
  QUICK_ROR_UNION_SELECT(Session *session, Table *table);
547
560
  ~QUICK_ROR_UNION_SELECT();
548
561
 
549
562
  int  init();
554
567
  int get_type() { return QS_TYPE_ROR_UNION; }
555
568
  void add_keys_and_lengths(String *key_names, String *used_lengths);
556
569
  void add_info_string(String *str);
557
 
  bool is_keys_used(const MY_BITMAP *fields);
 
570
  bool is_keys_used(const std::bitset<MAX_FIELDS> *fields);
558
571
 
559
572
  bool push_quick_back(QUICK_SELECT_I *quick_sel_range);
560
573
 
561
574
  List<QUICK_SELECT_I> quick_selects; /* Merged quick selects */
562
575
 
563
 
  QUEUE queue;    /* Priority queue for merge operation */
 
576
  /* Priority queue for merge operation */
 
577
  std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor >
 
578
    *queue;
564
579
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
565
580
 
566
 
  THD *thd;             /* current thread */
 
581
  Session *session;             /* current thread */
567
582
  unsigned char *cur_rowid;      /* buffer used in get_next() */
568
583
  unsigned char *prev_rowid;     /* rowid of last row returned by get_next() */
569
584
  bool have_prev_rowid; /* true if prev_rowid has valid data */
570
585
  uint32_t rowid_length;    /* table rowid length */
571
586
private:
572
 
  static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
573
 
  bool scans_inited; 
 
587
  bool scans_inited;
574
588
};
575
589
 
576
 
 
577
590
/*
578
591
  Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
579
592
 
675
688
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
676
689
{
677
690
public:
678
 
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts, 
 
691
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
679
692
                    bool *create_err);
680
693
  int get_next();
681
694
  bool reverse_sorted() { return 1; }
682
695
  int get_type() { return QS_TYPE_RANGE_DESC; }
683
696
private:
684
697
  bool range_reads_after_key(QUICK_RANGE *range);
685
 
#ifdef NOT_USED
686
 
  bool test_if_null_range(QUICK_RANGE *range, uint32_t used_key_parts);
687
 
#endif
688
698
  int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
689
699
  List<QUICK_RANGE> rev_ranges;
690
700
  List_iterator<QUICK_RANGE> rev_it;
707
717
  SQL_SELECT();
708
718
  ~SQL_SELECT();
709
719
  void cleanup();
710
 
  bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
711
 
  {
712
 
    key_map tmp;
713
 
    tmp.set_all();
714
 
    return test_quick_select(thd, tmp, 0, limit, force_quick_range, false) < 0;
715
 
  }
716
 
  inline bool skip_record() { return cond ? cond->val_int() == 0 : 0; }
717
 
  int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
718
 
                        ha_rows limit, bool force_quick_range, 
 
720
  bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
 
721
  bool skip_record();
 
722
  int test_quick_select(Session *session, key_map keys, table_map prev_tables,
 
723
                        ha_rows limit, bool force_quick_range,
719
724
                        bool ordered_output);
720
725
};
721
726
 
722
 
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
 
727
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
723
728
                                             struct st_table_ref *ref,
724
729
                                             ha_rows records);
725
730
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);