~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008-2009 Sun Microsystems
 *
 *  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; version 2 of the License.
 *
 *  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.
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DRIZZLED_OPTIMIZER_TABLE_READ_PLAN_H
#define DRIZZLED_OPTIMIZER_TABLE_READ_PLAN_H

#include "drizzled/util/functors.h"
#include <algorithm>

namespace drizzled
{

struct st_ror_scan_info;

namespace optimizer
{

class Parameter;
class SEL_ARG;
class SEL_TREE;

/*
  Table rows retrieval plan. Range optimizer creates QuickSelectInterface-derived
  objects from table read plans.
*/
class TableReadPlan
{
public:
  /*
    Plan read cost, with or without cost of full row retrieval, depending
    on plan creation parameters.
  */
  double read_cost;
  ha_rows records; /* estimate of #rows to be examined */

  /*
    If true, the scan returns rows in rowid order. This is used only for
    scans that can be both ROR and non-ROR.
  */
  bool is_ror;

  /*
    Create quick select for this plan.
    SYNOPSIS
     make_quick()
       param               Parameter from test_quick_select
       retrieve_full_rows  If true, created quick select will do full record
                           retrieval.
       parent_alloc        Memory pool to use, if any.

    NOTES
      retrieve_full_rows is ignored by some implementations.

    RETURN
      created quick select
      NULL on any error.
  */
  virtual QuickSelectInterface *make_quick(Parameter *param,
                                           bool retrieve_full_rows,
                                           memory::Root *parent_alloc= NULL) = 0;

  /* Table read plans are allocated on memory::Root and are never deleted */
  static void *operator new(size_t size, memory::Root *mem_root)
  { 
    return (void*) alloc_root(mem_root, (uint32_t) size); 
  }

  static void operator delete(void *, size_t)
  { }

  static void operator delete(void *, memory::Root *)
    { /* Never called */ }

  virtual ~TableReadPlan() {} /* Remove gcc warning */

};


/*
  Plan for a QuickRangeSelect scan.
  RangeReadPlan::make_quick ignores retrieve_full_rows parameter because
  QuickRangeSelect doesn't distinguish between 'index only' scans and full
  record retrieval scans.
*/
class RangeReadPlan : public TableReadPlan
{

public:

  RangeReadPlan(SEL_ARG *key_arg, uint32_t idx_arg, uint32_t mrr_flags_arg)
    :
      key(key_arg),
      key_idx(idx_arg),
      mrr_flags(mrr_flags_arg),
      mrr_buf_size(0)
  {}

  virtual ~RangeReadPlan() {}                     /* Remove gcc warning */

  QuickSelectInterface *make_quick(Parameter *param, bool, memory::Root *parent_alloc);

  void setMRRBufferSize(uint32_t in_mrr_buf_size)
  {
    mrr_buf_size= in_mrr_buf_size;
  }

  uint32_t getKeyIndex() const
  {
    return key_idx;
  }

  uint32_t getMRRBufferSize() const
  {
    return mrr_buf_size;
  }

private:

  /** set of intervals to be used in "range" method retrieval */
  SEL_ARG *key;
  /** key number in Parameter::key */
  uint32_t     key_idx;
  uint32_t     mrr_flags;
  uint32_t     mrr_buf_size;

};


/* Plan for QuickRorIntersectSelect scan. */
class RorIntersectReadPlan : public TableReadPlan
{
public:

  RorIntersectReadPlan() 
    :
      ror_range_scans(),
      cpk_scan(NULL),
      is_covering(false),
      index_scan_costs(0.0)
  {}

  virtual ~RorIntersectReadPlan() 
  {
    std::for_each(ror_range_scans.begin(),
                  ror_range_scans.end(),
                  DeletePtr());
    ror_range_scans.clear();
  }

  QuickSelectInterface *make_quick(Parameter *param,
                                   bool retrieve_full_rows,
                                   memory::Root *parent_alloc);

  void setRowRetrievalNecessary(bool in_is_covering)
  {
    is_covering= in_is_covering;
  }

  void setCostOfIndexScans(double in_index_scan_costs)
  {
    index_scan_costs= in_index_scan_costs;
  }

  /**
   * @return true if row retrival phase is necessary.
   */
  bool isRowRetrievalNecessary() const
  {
    return ! is_covering;
  }

  /**
   * @return the sum of the cost of each index scan
   */
  double getCostOfIndexScans() const
  {
    return index_scan_costs;
  }

  /** Vector of pointers to ROR range scans used in this intersection */
  std::vector<struct st_ror_scan_info *> ror_range_scans;
  struct st_ror_scan_info *cpk_scan;  /* Clustered PK scan, if there is one */

private:

  /** true if no row retrieval phase is necessary */
  bool is_covering; 
  /* SUM(cost(index_scan)) */
  double index_scan_costs; 

};


/*
  Plan for QuickRorUnionSelect scan.
  QuickRorUnionSelect always retrieves full rows, so retrieve_full_rows
  is ignored by make_quick.
*/

class RorUnionReadPlan : public TableReadPlan
{
public:
  RorUnionReadPlan() {}                          /* Remove gcc warning */
  virtual ~RorUnionReadPlan() {}                 /* Remove gcc warning */
  QuickSelectInterface *make_quick(Parameter *param,
                                   bool retrieve_full_rows,
                                   memory::Root *parent_alloc);
  /** vector of plans for merged scans */
  std::vector<TableReadPlan *> merged_scans;
};


/*
  Plan for QuickIndexMergeSelect scan.
  QuickRorIntersectSelect always retrieves full rows, so retrieve_full_rows
  is ignored by make_quick.
*/

class IndexMergeReadPlan : public TableReadPlan
{
public:
  IndexMergeReadPlan() {}                        /* Remove gcc warning */
  virtual ~IndexMergeReadPlan() {}               /* Remove gcc warning */
  QuickSelectInterface *make_quick(Parameter *param,
                                   bool retrieve_full_rows,
                                   memory::Root *parent_alloc);
  RangeReadPlan **range_scans; /* array of ptrs to plans of merged scans */
  RangeReadPlan **range_scans_end; /* end of the array */
};


/*
  Plan for a QuickGroupMinMaxSelect scan.
*/

class GroupMinMaxReadPlan : public TableReadPlan
{

public:

  GroupMinMaxReadPlan(bool have_min_arg, 
                      bool have_max_arg,
                      KEY_PART_INFO *min_max_arg_part_arg,
                      uint32_t group_prefix_len_arg, 
                      uint32_t used_key_parts_arg,
                      uint32_t group_key_parts_arg, 
                      KEY *index_info_arg,
                      uint32_t index_arg, 
                      uint32_t key_infix_len_arg,
                      unsigned char *key_infix_arg,
                      SEL_TREE *tree_arg, 
                      SEL_ARG *index_tree_arg,
                      uint32_t param_idx_arg, 
                      ha_rows quick_prefix_records_arg)
    :
      quick_prefix_records(quick_prefix_records_arg),
      have_min(have_min_arg),
      have_max(have_max_arg),
      min_max_arg_part(min_max_arg_part_arg),
      group_prefix_len(group_prefix_len_arg),
      used_key_parts(used_key_parts_arg),
      group_key_parts(group_key_parts_arg),
      index_info(index_info_arg),
      index(index_arg),
      key_infix_len(key_infix_len_arg),
      range_tree(tree_arg),
      index_tree(index_tree_arg),
      param_idx(param_idx_arg)
    {
      if (key_infix_len)
        memcpy(this->key_infix, key_infix_arg, key_infix_len);
    }
  virtual ~GroupMinMaxReadPlan() {}             /* Remove gcc warning */

  QuickSelectInterface *make_quick(Parameter *param,
                                   bool retrieve_full_rows,
                                   memory::Root *parent_alloc);

  /* Number of records selected by the ranges in index_tree. */
  ha_rows quick_prefix_records;

private:

  bool have_min;
  bool have_max;
  KEY_PART_INFO *min_max_arg_part;
  uint32_t group_prefix_len;
  uint32_t used_key_parts;
  uint32_t group_key_parts;
  KEY *index_info;
  uint32_t index;
  uint32_t key_infix_len;
  unsigned char key_infix[MAX_KEY_LENGTH];
  SEL_TREE *range_tree; /* Represents all range predicates in the query. */
  SEL_ARG *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
  uint32_t param_idx; /* Index of used key in param->key. */
};


} /* namespace optimizer */

} /* namespace drizzled */

#endif /* DRIZZLED_OPTIMIZER_TABLE_READ_PLAN_H */