~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.h

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

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
25
 
 
26
 
#include <drizzled/field.h>
27
 
#include <mysys/queues.h>
28
 
#include <drizzled/item/sum.h>
29
 
 
30
 
class JOIN;
31
 
typedef class Item COND;
32
 
 
33
 
typedef struct st_handler_buffer HANDLER_BUFFER;
 
19
#ifndef _opt_range_h
 
20
#define _opt_range_h
 
21
 
 
22
#ifdef USE_PRAGMA_INTERFACE
 
23
#pragma interface                       /* gcc class implementation */
 
24
#endif
34
25
 
35
26
typedef struct st_key_part {
36
27
  uint16_t           key,part;
49
40
 
50
41
class QUICK_RANGE :public Sql_alloc {
51
42
 public:
52
 
  unsigned char *min_key,*max_key;
 
43
  uchar *min_key,*max_key;
53
44
  uint16_t min_length,max_length,flag;
54
45
  key_part_map min_keypart_map, // bitmap of used keyparts in min_key
55
46
               max_keypart_map; // bitmap of used keyparts in max_key
57
48
  uint16_t dummy;                                       /* Avoid warnings on 'flag' */
58
49
#endif
59
50
  QUICK_RANGE();                                /* Full range */
60
 
  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,
61
52
              key_part_map min_keypart_map_arg,
62
 
              const unsigned char *max_key_arg, uint32_t max_length_arg,
 
53
              const uchar *max_key_arg, uint max_length_arg,
63
54
              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)),
66
 
      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)),
67
58
      min_length((uint16_t) min_length_arg),
68
59
      max_length((uint16_t) max_length_arg),
69
60
      flag((uint16_t) flag_arg),
126
117
  bool sorted;
127
118
  ha_rows records;  /* estimate of # of records to be retrieved */
128
119
  double  read_time; /* time to perform this retrieval          */
129
 
  Table   *head;
 
120
  TABLE   *head;
130
121
  /*
131
122
    Index this quick select uses, or MAX_KEY for quick selects
132
123
    that use several indexes
133
124
  */
134
 
  uint32_t index;
 
125
  uint index;
135
126
 
136
127
  /*
137
128
    Total length of first used_key_parts parts of the key.
138
129
    Applicable if index!= MAX_KEY.
139
130
  */
140
 
  uint32_t max_used_key_length;
 
131
  uint max_used_key_length;
141
132
 
142
133
  /*
143
134
    Max. number of (first) key parts this quick select uses for retrieval.
146
137
 
147
138
    For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
148
139
  */
149
 
  uint32_t used_key_parts;
 
140
  uint used_key_parts;
150
141
 
151
142
  QUICK_SELECT_I();
152
143
  virtual ~QUICK_SELECT_I(){};
221
212
      0     Ok
222
213
      other Error
223
214
  */
224
 
  virtual int init_ror_merged_scan(bool)
 
215
  virtual int init_ror_merged_scan(bool reuse_handler __attribute__((unused)))
225
216
  { assert(0); return 1; }
226
217
 
227
218
  /*
243
234
    This function is implemented only by quick selects that merge other quick
244
235
    selects output and/or can produce output suitable for merging.
245
236
  */
246
 
  virtual void add_info_string(String *) {};
 
237
  virtual void add_info_string(String *str __attribute__((unused))) {};
247
238
  /*
248
239
    Return 1 if any index used by this quick select
249
240
    uses field which is marked in passed bitmap.
254
245
    rowid of last row retrieved by this quick select. This is used only when
255
246
    doing ROR-index_merge selects
256
247
  */
257
 
  unsigned char    *last_rowid;
 
248
  uchar    *last_rowid;
258
249
 
259
250
  /*
260
251
    Table record buffer used by this quick select.
261
252
  */
262
 
  unsigned char    *record;
 
253
  uchar    *record;
263
254
};
264
255
 
265
256
 
279
270
  QUICK_RANGE **last;
280
271
} QUICK_RANGE_SEQ_CTX;
281
272
 
282
 
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
283
 
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);
284
275
 
285
276
 
286
277
/*
305
296
  /* Members needed to use the MRR interface */
306
297
  QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
307
298
public:
308
 
  uint32_t mrr_flags; /* Flags to be used with MRR interface */
 
299
  uint mrr_flags; /* Flags to be used with MRR interface */
309
300
protected:
310
 
  uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */  
 
301
  uint mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */  
311
302
  HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
312
303
 
313
304
  /* Info about index we're scanning */
322
313
public:
323
314
  MEM_ROOT alloc;
324
315
 
325
 
  QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
 
316
  QUICK_RANGE_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc,
326
317
                     MEM_ROOT *parent_alloc, bool *create_err);
327
318
  ~QUICK_RANGE_SELECT();
328
319
 
330
321
  int reset(void);
331
322
  int get_next();
332
323
  void range_end();
333
 
  int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
334
 
                      unsigned char *cur_prefix);
 
324
  int get_next_prefix(uint prefix_length, key_part_map keypart_map,
 
325
                      uchar *cur_prefix);
335
326
  bool reverse_sorted() { return 0; }
336
327
  bool unique_key_range();
337
328
  int init_ror_merged_scan(bool reuse_handler);
338
 
  void save_last_pos();
 
329
  void save_last_pos()
 
330
  { file->position(record); }
339
331
  int get_type() { return QS_TYPE_RANGE; }
340
332
  void add_keys_and_lengths(String *key_names, String *used_lengths);
341
333
  void add_info_string(String *str);
354
346
  }
355
347
  friend class TRP_ROR_INTERSECT;
356
348
  friend
357
 
  QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
 
349
  QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
358
350
                                               struct st_table_ref *ref,
359
351
                                               ha_rows records);
360
352
  friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick, 
361
353
                             KEY_PART *key, SEL_ARG *key_tree, 
362
 
                             unsigned char *min_key, uint32_t min_key_flag,
363
 
                             unsigned char *max_key, uint32_t max_key_flag);
364
 
  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,
365
357
                                              SEL_ARG *key_tree,
366
 
                                              uint32_t mrr_flags,
367
 
                                              uint32_t mrr_buf_size,
 
358
                                              uint mrr_flags,
 
359
                                              uint mrr_buf_size,
368
360
                                              MEM_ROOT *alloc);
369
361
  friend class QUICK_SELECT_DESC;
370
362
  friend class QUICK_INDEX_MERGE_SELECT;
371
363
  friend class QUICK_ROR_INTERSECT_SELECT;
372
364
  friend class QUICK_GROUP_MIN_MAX_SELECT;
373
 
  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);
374
366
  friend range_seq_t quick_range_seq_init(void *init_param,
375
 
                                          uint32_t n_ranges, uint32_t flags);
 
367
                                          uint n_ranges, uint flags);
376
368
  friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
377
369
                              bool distinct,const char *message);
378
370
};
440
432
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
441
433
{
442
434
public:
443
 
  QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
 
435
  QUICK_INDEX_MERGE_SELECT(THD *thd, TABLE *table);
444
436
  ~QUICK_INDEX_MERGE_SELECT();
445
437
 
446
438
  int  init();
465
457
  bool  doing_pk_scan;
466
458
 
467
459
  MEM_ROOT alloc;
468
 
  Session *session;
 
460
  THD *thd;
469
461
  int read_keys_and_merge();
470
462
 
471
463
  /* used to get rows collected in Unique */
494
486
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
495
487
{
496
488
public:
497
 
  QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
 
489
  QUICK_ROR_INTERSECT_SELECT(THD *thd, TABLE *table,
498
490
                             bool retrieve_full_rows,
499
491
                             MEM_ROOT *parent_alloc);
500
492
  ~QUICK_ROR_INTERSECT_SELECT();
524
516
  QUICK_RANGE_SELECT *cpk_quick;
525
517
 
526
518
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
527
 
  Session *session;       /* current thread */
 
519
  THD *thd;       /* current thread */
528
520
  bool need_to_fetch_row; /* if true, do retrieve full table records. */
529
521
  /* in top-level quick select, true if merged scans where initialized */
530
522
  bool scans_inited; 
547
539
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
548
540
{
549
541
public:
550
 
  QUICK_ROR_UNION_SELECT(Session *session, Table *table);
 
542
  QUICK_ROR_UNION_SELECT(THD *thd, TABLE *table);
551
543
  ~QUICK_ROR_UNION_SELECT();
552
544
 
553
545
  int  init();
567
559
  QUEUE queue;    /* Priority queue for merge operation */
568
560
  MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
569
561
 
570
 
  Session *session;             /* current thread */
571
 
  unsigned char *cur_rowid;      /* buffer used in get_next() */
572
 
  unsigned char *prev_rowid;     /* rowid of last row returned by get_next() */
 
562
  THD *thd;             /* current thread */
 
563
  uchar *cur_rowid;      /* buffer used in get_next() */
 
564
  uchar *prev_rowid;     /* rowid of last row returned by get_next() */
573
565
  bool have_prev_rowid; /* true if prev_rowid has valid data */
574
 
  uint32_t rowid_length;    /* table rowid length */
 
566
  uint rowid_length;    /* table rowid length */
575
567
private:
576
 
  static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
 
568
  static int queue_cmp(void *arg, uchar *val1, uchar *val2);
577
569
  bool scans_inited; 
578
570
};
579
571
 
617
609
  handler *file;         /* The handler used to get data. */
618
610
  JOIN *join;            /* Descriptor of the current query */
619
611
  KEY  *index_info;      /* The index chosen for data access */
620
 
  unsigned char *record;          /* Buffer where the next record is returned. */
621
 
  unsigned char *tmp_record;      /* Temporary storage for next_min(), next_max(). */
622
 
  unsigned char *group_prefix;    /* Key prefix consisting of the GROUP fields. */
623
 
  uint32_t group_prefix_len; /* Length of the group prefix. */
624
 
  uint32_t group_key_parts;  /* A number of keyparts in the group prefix */
625
 
  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. */
626
618
  bool have_min;         /* Specify whether we are computing */
627
619
  bool have_max;         /*   a MIN, a MAX, or both.         */
628
620
  bool seen_first_key;   /* Denotes whether the first key was retrieved.*/
629
621
  KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
630
622
                                   /* of all MIN/MAX functions.              */
631
 
  uint32_t min_max_arg_len;  /* The length of the MIN/MAX argument field */
632
 
  unsigned char *key_infix;       /* Infix of constants from equality predicates. */
633
 
  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;
634
626
  DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
635
 
  uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
636
 
  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.      */
637
629
  List<Item_sum> *min_functions;
638
630
  List<Item_sum> *max_functions;
639
631
  List_iterator<Item_sum> *min_functions_it;
654
646
  void update_min_result();
655
647
  void update_max_result();
656
648
public:
657
 
  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,
658
650
                             bool have_max, KEY_PART_INFO *min_max_arg_part,
659
 
                             uint32_t group_prefix_len, uint32_t group_key_parts,
660
 
                             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
661
653
                             use_index, double read_cost, ha_rows records, uint
662
 
                             key_infix_len, unsigned char *key_infix, MEM_ROOT
 
654
                             key_infix_len, uchar *key_infix, MEM_ROOT
663
655
                             *parent_alloc);
664
656
  ~QUICK_GROUP_MIN_MAX_SELECT();
665
657
  bool add_range(SEL_ARG *sel_range);
679
671
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
680
672
{
681
673
public:
682
 
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts, 
 
674
  QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts, 
683
675
                    bool *create_err);
684
676
  int get_next();
685
677
  bool reverse_sorted() { return 1; }
687
679
private:
688
680
  bool range_reads_after_key(QUICK_RANGE *range);
689
681
#ifdef NOT_USED
690
 
  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);
691
683
#endif
692
684
  int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
693
685
  List<QUICK_RANGE> rev_ranges;
699
691
 public:
700
692
  QUICK_SELECT_I *quick;        // If quick-select used
701
693
  COND          *cond;          // where condition
702
 
  Table *head;
 
694
  TABLE *head;
703
695
  IO_CACHE file;                // Positions to used records
704
696
  ha_rows records;              // Records in use if read from file
705
697
  double read_time;             // Time to read rows
711
703
  SQL_SELECT();
712
704
  ~SQL_SELECT();
713
705
  void cleanup();
714
 
  bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
715
 
  bool skip_record();
716
 
  int test_quick_select(Session *session, key_map keys, table_map prev_tables,
717
 
                        ha_rows limit, bool force_quick_range,
 
706
  bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
 
707
  {
 
708
    key_map tmp;
 
709
    tmp.set_all();
 
710
    return test_quick_select(thd, tmp, 0, limit, force_quick_range, false) < 0;
 
711
  }
 
712
  inline bool skip_record() { return cond ? cond->val_int() == 0 : 0; }
 
713
  int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
 
714
                        ha_rows limit, bool force_quick_range, 
718
715
                        bool ordered_output);
719
716
};
720
717
 
721
 
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
 
718
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
722
719
                                             struct st_table_ref *ref,
723
720
                                             ha_rows records);
724
 
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);
725
722
 
726
723
#endif