~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.h

  • Committer: Brian Aker
  • Date: 2008-08-12 03:12:57 UTC
  • Revision ID: brian@tangent.org-20080812031257-ln3uk87y1r22byeg
First pass of new sql_db.cc work

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 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
/* classes to use when handling where clause */
22
18
 
23
 
#ifndef DRIZZLED_OPT_RANGE_H
24
 
#define DRIZZLED_OPT_RANGE_H
 
19
#ifndef _opt_range_h
 
20
#define _opt_range_h
25
21
 
26
22
#ifdef USE_PRAGMA_INTERFACE
27
23
#pragma interface                       /* gcc class implementation */
44
40
 
45
41
class QUICK_RANGE :public Sql_alloc {
46
42
 public:
47
 
  unsigned char *min_key,*max_key;
 
43
  uchar *min_key,*max_key;
48
44
  uint16_t min_length,max_length,flag;
49
45
  key_part_map min_keypart_map, // bitmap of used keyparts in min_key
50
46
               max_keypart_map; // bitmap of used keyparts in max_key
52
48
  uint16_t dummy;                                       /* Avoid warnings on 'flag' */
53
49
#endif
54
50
  QUICK_RANGE();                                /* Full range */
55
 
  QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
 
51
  QUICK_RANGE(const uchar *min_key_arg, uint min_length_arg,
56
52
              key_part_map min_keypart_map_arg,
57
 
              const unsigned char *max_key_arg, uint32_t max_length_arg,
 
53
              const uchar *max_key_arg, uint max_length_arg,
58
54
              key_part_map max_keypart_map_arg,
59
 
              uint32_t flag_arg)
60
 
    : min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
61
 
      max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
 
55
              uint flag_arg)
 
56
    : min_key((uchar*) sql_memdup(min_key_arg,min_length_arg+1)),
 
57
      max_key((uchar*) sql_memdup(max_key_arg,max_length_arg+1)),
62
58
      min_length((uint16_t) min_length_arg),
63
59
      max_length((uint16_t) max_length_arg),
64
60
      flag((uint16_t) flag_arg),
121
117
  bool sorted;
122
118
  ha_rows records;  /* estimate of # of records to be retrieved */
123
119
  double  read_time; /* time to perform this retrieval          */
124
 
  Table   *head;
 
120
  TABLE   *head;
125
121
  /*
126
122
    Index this quick select uses, or MAX_KEY for quick selects
127
123
    that use several indexes
128
124
  */
129
 
  uint32_t index;
 
125
  uint index;
130
126
 
131
127
  /*
132
128
    Total length of first used_key_parts parts of the key.
133
129
    Applicable if index!= MAX_KEY.
134
130
  */
135
 
  uint32_t max_used_key_length;
 
131
  uint max_used_key_length;
136
132
 
137
133
  /*
138
134
    Max. number of (first) key parts this quick select uses for retrieval.
141
137
 
142
138
    For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
143
139
  */
144
 
  uint32_t used_key_parts;
 
140
  uint used_key_parts;
145
141
 
146
142
  QUICK_SELECT_I();
147
143
  virtual ~QUICK_SELECT_I(){};
249
245
    rowid of last row retrieved by this quick select. This is used only when
250
246
    doing ROR-index_merge selects
251
247
  */
252
 
  unsigned char    *last_rowid;
 
248
  uchar    *last_rowid;
253
249
 
254
250
  /*
255
251
    Table record buffer used by this quick select.
256
252
  */
257
 
  unsigned char    *record;
 
253
  uchar    *record;
258
254
};
259
255
 
260
256
 
274
270
  QUICK_RANGE **last;
275
271
} QUICK_RANGE_SEQ_CTX;
276
272
 
277
 
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
278
 
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
 
273
range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags);
 
274
uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
279
275
 
280
276
 
281
277
/*
300
296
  /* Members needed to use the MRR interface */
301
297
  QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
302
298
public:
303
 
  uint32_t mrr_flags; /* Flags to be used with MRR interface */
 
299
  uint mrr_flags; /* Flags to be used with MRR interface */
304
300
protected:
305
 
  uint32_t mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */  
 
301
  uint mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */  
306
302
  HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
307
303
 
308
304
  /* Info about index we're scanning */
317
313
public:
318
314
  MEM_ROOT alloc;
319
315
 
320
 
  QUICK_RANGE_SELECT(THD *thd, Table *table,uint32_t index_arg,bool no_alloc,
 
316
  QUICK_RANGE_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc,
321
317
                     MEM_ROOT *parent_alloc, bool *create_err);
322
318
  ~QUICK_RANGE_SELECT();
323
319
 
325
321
  int reset(void);
326
322
  int get_next();
327
323
  void range_end();
328
 
  int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
329
 
                      unsigned char *cur_prefix);
 
324
  int get_next_prefix(uint prefix_length, key_part_map keypart_map,
 
325
                      uchar *cur_prefix);
330
326
  bool reverse_sorted() { return 0; }
331
327
  bool unique_key_range();
332
328
  int init_ror_merged_scan(bool reuse_handler);
350
346
  }
351
347
  friend class TRP_ROR_INTERSECT;
352
348
  friend
353
 
  QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
 
349
  QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
354
350
                                               struct st_table_ref *ref,
355
351
                                               ha_rows records);
356
352
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick, 
357
353
                             KEY_PART *key, SEL_ARG *key_tree, 
358
 
                             unsigned char *min_key, uint32_t min_key_flag,
359
 
                             unsigned char *max_key, uint32_t max_key_flag);
360
 
  friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
 
354
                             uchar *min_key, uint min_key_flag,
 
355
                             uchar *max_key, uint max_key_flag);
 
356
  friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint idx,
361
357
                                              SEL_ARG *key_tree,
362
 
                                              uint32_t mrr_flags,
363
 
                                              uint32_t mrr_buf_size,
 
358
                                              uint mrr_flags,
 
359
                                              uint mrr_buf_size,
364
360
                                              MEM_ROOT *alloc);
365
361
  friend class QUICK_SELECT_DESC;
366
362
  friend class QUICK_INDEX_MERGE_SELECT;
367
363
  friend class QUICK_ROR_INTERSECT_SELECT;
368
364
  friend class QUICK_GROUP_MIN_MAX_SELECT;
369
 
  friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
 
365
  friend uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
370
366
  friend range_seq_t quick_range_seq_init(void *init_param,
371
 
                                          uint32_t n_ranges, uint32_t flags);
 
367
                                          uint n_ranges, uint flags);
372
368
  friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
373
369
                              bool distinct,const char *message);
374
370
};
436
432
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
437
433
{
438
434
public:
439
 
  QUICK_INDEX_MERGE_SELECT(THD *thd, Table *table);
 
435
  QUICK_INDEX_MERGE_SELECT(THD *thd, TABLE *table);
440
436
  ~QUICK_INDEX_MERGE_SELECT();
441
437
 
442
438
  int  init();
490
486
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
491
487
{
492
488
public:
493
 
  QUICK_ROR_INTERSECT_SELECT(THD *thd, Table *table,
 
489
  QUICK_ROR_INTERSECT_SELECT(THD *thd, TABLE *table,
494
490
                             bool retrieve_full_rows,
495
491
                             MEM_ROOT *parent_alloc);
496
492
  ~QUICK_ROR_INTERSECT_SELECT();
543
539
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
544
540
{
545
541
public:
546
 
  QUICK_ROR_UNION_SELECT(THD *thd, Table *table);
 
542
  QUICK_ROR_UNION_SELECT(THD *thd, TABLE *table);
547
543
  ~QUICK_ROR_UNION_SELECT();
548
544
 
549
545
  int  init();
564
560
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
565
561
 
566
562
  THD *thd;             /* current thread */
567
 
  unsigned char *cur_rowid;      /* buffer used in get_next() */
568
 
  unsigned char *prev_rowid;     /* rowid of last row returned by get_next() */
 
563
  uchar *cur_rowid;      /* buffer used in get_next() */
 
564
  uchar *prev_rowid;     /* rowid of last row returned by get_next() */
569
565
  bool have_prev_rowid; /* true if prev_rowid has valid data */
570
 
  uint32_t rowid_length;    /* table rowid length */
 
566
  uint rowid_length;    /* table rowid length */
571
567
private:
572
 
  static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
 
568
  static int queue_cmp(void *arg, uchar *val1, uchar *val2);
573
569
  bool scans_inited; 
574
570
};
575
571
 
613
609
  handler *file;         /* The handler used to get data. */
614
610
  JOIN *join;            /* Descriptor of the current query */
615
611
  KEY  *index_info;      /* The index chosen for data access */
616
 
  unsigned char *record;          /* Buffer where the next record is returned. */
617
 
  unsigned char *tmp_record;      /* Temporary storage for next_min(), next_max(). */
618
 
  unsigned char *group_prefix;    /* Key prefix consisting of the GROUP fields. */
619
 
  uint32_t group_prefix_len; /* Length of the group prefix. */
620
 
  uint32_t group_key_parts;  /* A number of keyparts in the group prefix */
621
 
  unsigned char *last_prefix;     /* Prefix of the last group for detecting EOF. */
 
612
  uchar *record;          /* Buffer where the next record is returned. */
 
613
  uchar *tmp_record;      /* Temporary storage for next_min(), next_max(). */
 
614
  uchar *group_prefix;    /* Key prefix consisting of the GROUP fields. */
 
615
  uint group_prefix_len; /* Length of the group prefix. */
 
616
  uint group_key_parts;  /* A number of keyparts in the group prefix */
 
617
  uchar *last_prefix;     /* Prefix of the last group for detecting EOF. */
622
618
  bool have_min;         /* Specify whether we are computing */
623
619
  bool have_max;         /*   a MIN, a MAX, or both.         */
624
620
  bool seen_first_key;   /* Denotes whether the first key was retrieved.*/
625
621
  KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
626
622
                                   /* of all MIN/MAX functions.              */
627
 
  uint32_t min_max_arg_len;  /* The length of the MIN/MAX argument field */
628
 
  unsigned char *key_infix;       /* Infix of constants from equality predicates. */
629
 
  uint32_t key_infix_len;
 
623
  uint min_max_arg_len;  /* The length of the MIN/MAX argument field */
 
624
  uchar *key_infix;       /* Infix of constants from equality predicates. */
 
625
  uint key_infix_len;
630
626
  DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
631
 
  uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
632
 
  uint32_t real_key_parts;  /* A number of keyparts in the above value.      */
 
627
  uint real_prefix_len; /* Length of key prefix extended with key_infix. */
 
628
  uint real_key_parts;  /* A number of keyparts in the above value.      */
633
629
  List<Item_sum> *min_functions;
634
630
  List<Item_sum> *max_functions;
635
631
  List_iterator<Item_sum> *min_functions_it;
650
646
  void update_min_result();
651
647
  void update_max_result();
652
648
public:
653
 
  QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
 
649
  QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join, bool have_min,
654
650
                             bool have_max, KEY_PART_INFO *min_max_arg_part,
655
 
                             uint32_t group_prefix_len, uint32_t group_key_parts,
656
 
                             uint32_t used_key_parts, KEY *index_info, uint
 
651
                             uint group_prefix_len, uint group_key_parts,
 
652
                             uint used_key_parts, KEY *index_info, uint
657
653
                             use_index, double read_cost, ha_rows records, uint
658
 
                             key_infix_len, unsigned char *key_infix, MEM_ROOT
 
654
                             key_infix_len, uchar *key_infix, MEM_ROOT
659
655
                             *parent_alloc);
660
656
  ~QUICK_GROUP_MIN_MAX_SELECT();
661
657
  bool add_range(SEL_ARG *sel_range);
675
671
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
676
672
{
677
673
public:
678
 
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts, 
 
674
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts, 
679
675
                    bool *create_err);
680
676
  int get_next();
681
677
  bool reverse_sorted() { return 1; }
683
679
private:
684
680
  bool range_reads_after_key(QUICK_RANGE *range);
685
681
#ifdef NOT_USED
686
 
  bool test_if_null_range(QUICK_RANGE *range, uint32_t used_key_parts);
 
682
  bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
687
683
#endif
688
684
  int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
689
685
  List<QUICK_RANGE> rev_ranges;
695
691
 public:
696
692
  QUICK_SELECT_I *quick;        // If quick-select used
697
693
  COND          *cond;          // where condition
698
 
  Table *head;
 
694
  TABLE *head;
699
695
  IO_CACHE file;                // Positions to used records
700
696
  ha_rows records;              // Records in use if read from file
701
697
  double read_time;             // Time to read rows
719
715
                        bool ordered_output);
720
716
};
721
717
 
722
 
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
 
718
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
723
719
                                             struct st_table_ref *ref,
724
720
                                             ha_rows records);
725
 
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);
 
721
uint get_index_for_order(TABLE *table, ORDER *order, ha_rows limit);
726
722
 
727
723
#endif