opt_range.h 4.55 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

bk@work.mysql.com's avatar
bk@work.mysql.com committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

bk@work.mysql.com's avatar
bk@work.mysql.com committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

bk@work.mysql.com's avatar
bk@work.mysql.com committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* classes to use when handling where clause */

#ifndef _opt_range_h
#define _opt_range_h

#ifdef __GNUC__
#pragma interface			/* gcc class implementation */
#endif

#define NO_MIN_RANGE	1
#define NO_MAX_RANGE	2
#define NEAR_MIN	4
#define NEAR_MAX	8
#define UNIQUE_RANGE	16
#define EQ_RANGE	32
33
#define NULL_RANGE	64
34 35
#define GEOM_FLAG      128

bk@work.mysql.com's avatar
bk@work.mysql.com committed
36 37

typedef struct st_key_part {
38
  uint16           key,part, store_length, length;
39 40 41
  uint8            null_bit;
  Field            *field;
  Field::imagetype image_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
42 43
} KEY_PART;

44

bk@work.mysql.com's avatar
bk@work.mysql.com committed
45 46 47 48
class QUICK_RANGE :public Sql_alloc {
 public:
  char *min_key,*max_key;
  uint16 min_length,max_length,flag;
49 50 51
#ifdef HAVE_purify
  uint16 dummy;					/* Avoid warnings on 'flag' */
#endif
bk@work.mysql.com's avatar
bk@work.mysql.com committed
52 53 54 55 56 57
  QUICK_RANGE();				/* Full range */
  QUICK_RANGE(const char *min_key_arg,uint min_length_arg,
	      const char *max_key_arg,uint max_length_arg,
	      uint flag_arg)
    : min_key((char*) sql_memdup(min_key_arg,min_length_arg+1)),
      max_key((char*) sql_memdup(max_key_arg,max_length_arg+1)),
58 59 60
      min_length((uint16) min_length_arg),
      max_length((uint16) max_length_arg),
      flag((uint16) flag_arg)
61 62 63 64 65
    {
#ifdef HAVE_purify
      dummy=0;
#endif
    }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
66 67
};

68

bk@work.mysql.com's avatar
bk@work.mysql.com committed
69 70
class QUICK_SELECT {
public:
71
  bool next,dont_free,sorted;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
72
  int error;
73
  uint index, max_used_key_length, used_key_parts;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
74 75 76 77 78 79 80 81 82
  TABLE *head;
  handler *file;
  byte    *record;
  List<QUICK_RANGE> ranges;
  List_iterator<QUICK_RANGE> it;
  QUICK_RANGE *range;
  MEM_ROOT alloc;

  KEY_PART *key_parts;
83
  KEY_PART_INFO *key_part_info;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
84 85 86
  ha_rows records;
  double read_time;

87
  QUICK_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc=0);
88
  virtual ~QUICK_SELECT();
bk@work.mysql.com's avatar
bk@work.mysql.com committed
89
  void reset(void) { next=0; it.rewind(); }
90 91 92
  int init()
  {
    key_part_info= head->key_info[index].key_part;
93
    return error=file->ha_index_init(index);
94
  }
95
  virtual int get_next();
96
  virtual bool reverse_sorted() { return 0; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
97 98 99
  bool unique_key_range();
};

100

101 102 103 104 105 106 107 108 109 110
class QUICK_SELECT_GEOM: public QUICK_SELECT
{
public:
  QUICK_SELECT_GEOM(THD *thd, TABLE *table, uint index_arg, bool no_alloc)
    :QUICK_SELECT(thd, table, index_arg, no_alloc)
    {};
  virtual int get_next();
};


111 112 113
class QUICK_SELECT_DESC: public QUICK_SELECT
{
public:
114
  QUICK_SELECT_DESC(QUICK_SELECT *q, uint used_key_parts);
115
  int get_next();
116
  bool reverse_sorted() { return 1; }
117 118 119
private:
  int cmp_prev(QUICK_RANGE *range);
  bool range_reads_after_key(QUICK_RANGE *range);
120
#ifdef NOT_USED
121
  bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
122
#endif
123
  void reset(void) { next=0; rev_it.rewind(); }
124 125 126 127
  List<QUICK_RANGE> rev_ranges;
  List_iterator<QUICK_RANGE> rev_it;
};

128

bk@work.mysql.com's avatar
bk@work.mysql.com committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
class SQL_SELECT :public Sql_alloc {
 public:
  QUICK_SELECT *quick;		// If quick-select used
  COND		*cond;		// where condition
  TABLE	*head;
  IO_CACHE file;		// Positions to used records
  ha_rows records;		// Records in use if read from file
  double read_time;		// Time to read rows
  key_map quick_keys;		// Possible quick keys
  key_map needed_reg;		// Possible quick keys after prev tables.
  table_map const_tables,read_tables;
  bool	free_cond;

  SQL_SELECT();
  ~SQL_SELECT();
144
  void cleanup();
145
  bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
serg@serg.mylan's avatar
serg@serg.mylan committed
146
  { return test_quick_select(thd, key_map(~0), 0, limit, force_quick_range) < 0; }
147
  inline bool skip_record() { return cond ? cond->val_int() == 0 : 0; }
148 149
  int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
			ha_rows limit, bool force_quick_range=0);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
150 151
};

ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
152 153 154 155 156

class FT_SELECT: public QUICK_SELECT {
public:
  FT_SELECT(THD *thd, TABLE *table, uint key):
    QUICK_SELECT (thd, table, key, 1) { init(); }
157
  ~FT_SELECT() { file->ft_end(); }
ram@gw.mysql.r18.ru's avatar
ram@gw.mysql.r18.ru committed
158 159 160 161 162
  int init() { return error= file->ft_init(); }
  int get_next() { return error= file->ft_read(record); }
};


163 164
QUICK_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
				       struct st_table_ref *ref);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
165 166

#endif