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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
*
* 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
*/
/* classes to use when handling where clause */
#ifndef DRIZZLED_OPTIMIZER_RANGE_H
#define DRIZZLED_OPTIMIZER_RANGE_H
#include "drizzled/field.h"
#include "drizzled/item/sum.h"
#include <queue>
namespace drizzled
{
class Join;
class RorIntersectReadPlan;
typedef class Item COND;
namespace internal
{
typedef struct st_io_cache IO_CACHE;
}
typedef struct st_key_part
{
uint16_t key;
uint16_t part;
/* See KeyPartInfo for meaning of the next two: */
uint16_t store_length;
uint16_t length;
uint8_t null_bit;
/**
Keypart flags (0 when this structure is used by partition pruning code
for fake partitioning index description)
*/
uint8_t flag;
Field *field;
} KEY_PART;
namespace optimizer
{
class Parameter;
class SEL_ARG;
/**
Quick select interface.
This class is a parent for all QUICK_*_SELECT classes.
The usage scenario is as follows:
1. Create quick select
quick= new QUICK_XXX_SELECT(...);
2. Perform lightweight initialization. This can be done in 2 ways:
2.a: Regular initialization
if (quick->init())
{
//the only valid action after failed init() call is delete
delete quick;
}
2.b: Special initialization for quick selects merged by QUICK_ROR_*_SELECT
if (quick->init_ror_merged_scan())
delete quick;
3. Perform zero, one, or more scans.
while (...)
{
// initialize quick select for scan. This may allocate
// buffers and/or prefetch rows.
if (quick->reset())
{
//the only valid action after failed reset() call is delete
delete quick;
//abort query
}
// perform the scan
do
{
res= quick->get_next();
} while (res && ...)
}
4. Delete the select:
delete quick;
*/
class QuickSelectInterface
{
public:
bool sorted;
ha_rows records; /**< estimate of # of records to be retrieved */
double read_time; /**< time to perform this retrieval */
Table *head;
/**
Index this quick select uses, or MAX_KEY for quick selects
that use several indexes
*/
uint32_t index;
/**
Total length of first used_key_parts parts of the key.
Applicable if index!= MAX_KEY.
*/
uint32_t max_used_key_length;
/**
Maximum number of (first) key parts this quick select uses for retrieval.
eg. for "(key1p1=c1 AND key1p2=c2) OR key1p1=c2" used_key_parts == 2.
Applicable if index!= MAX_KEY.
For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
*/
uint32_t used_key_parts;
/**
* The rowid of last row retrieved by this quick select. This is used only when
* doing ROR-index_merge selects
*/
unsigned char *last_rowid;
/**
* Table record buffer used by this quick select.
*/
unsigned char *record;
QuickSelectInterface();
virtual ~QuickSelectInterface(){};
/**
* Do post-constructor initialization.
*
* @details
*
* Performs initializations that should have been in constructor if
* it was possible to return errors from constructors. The join optimizer may
* create and then delete quick selects without retrieving any rows so init()
* must not contain any IO or CPU intensive code.
*
* If init() call fails the only valid action is to delete this quick select,
* reset() and get_next() must not be called.
*
* @retval
* 0 OK
* @retval
* other Error code
*/
virtual int init() = 0;
/**
* Initializes quick select for row retrieval.
*
* @details
*
* Should be called when it is certain that row retrieval will be
* necessary. This call may do heavyweight initialization like buffering first
* N records etc. If reset() call fails get_next() must not be called.
* Note that reset() may be called several times if
* - the quick select is executed in a subselect
* - a JOIN buffer is used
*
* @retval
* 0 OK
* @retval
* other Error code
*/
virtual int reset(void) = 0;
/** Gets next record to retrieve */
virtual int get_next() = 0;
/** Range end should be called when we have looped over the whole index */
virtual void range_end() {}
virtual bool reverse_sorted() const = 0;
virtual bool unique_key_range() const
{
return false;
}
enum
{
QS_TYPE_RANGE= 0,
QS_TYPE_INDEX_MERGE= 1,
QS_TYPE_RANGE_DESC= 2,
QS_TYPE_ROR_INTERSECT= 4,
QS_TYPE_ROR_UNION= 5,
QS_TYPE_GROUP_MIN_MAX= 6
};
/** Returns the type of this quick select - one of the QS_TYPE_* values */
virtual int get_type() const = 0;
/**
* Initialize this quick select as a merged scan inside a ROR-union or a ROR-
* intersection scan. The caller must not additionally call init() if this
* function is called.
*
* @param If true, the quick select may use table->Cursor,
* otherwise it must create and use a separate Cursor
* object.
*
* @retval
* 0 Ok
* @retval
* other Error
*/
virtual int init_ror_merged_scan(bool)
{
assert(0);
return 1;
}
/**
* Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
*/
virtual void save_last_pos(){};
/**
* Append comma-separated list of keys this quick select uses to key_names;
* append comma-separated list of corresponding used lengths to used_lengths.
*
* @note This is used by during explain plan.
*/
virtual void add_keys_and_lengths(String *key_names, String *used_lengths)=0;
/**
* Append text representation of quick select structure (what and how is
* merged) to str. The result is added to "Extra" field in EXPLAIN output.
*
* @note
*
* This function is implemented only by quick selects that merge other quick
* selects output and/or can produce output suitable for merging.
*/
virtual void add_info_string(String *)
{}
/**
* Returns true if any index used by this quick select
* uses field which is marked in passed bitmap.
*/
virtual bool is_keys_used(const boost::dynamic_bitset<>& fields);
};
struct st_qsel_param;
class QuickRange;
class QuickRangeSelect;
/**
* MRR range sequence, array<QuickRange> implementation: sequence traversal
* context.
*/
typedef struct st_quick_range_seq_ctx
{
QuickRange **first;
QuickRange **cur;
QuickRange **last;
} QuickRangeSequenceContext;
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
/**
* Executor class for SELECT statements.
*
* @details
*
* The QuickSelectInterface member variable is the implementor
* of the SELECT execution.
*/
class SqlSelect : public memory::SqlAlloc
{
public:
QuickSelectInterface *quick; /**< If quick-select used */
COND *cond; /**< where condition */
Table *head;
internal::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;
table_map read_tables;
bool free_cond;
SqlSelect();
~SqlSelect();
void cleanup();
bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
bool skip_record();
int test_quick_select(Session *session, key_map keys, table_map prev_tables,
ha_rows limit, bool force_quick_range,
bool ordered_output);
};
QuickRangeSelect *get_quick_select_for_ref(Session *session,
Table *table,
struct table_reference_st *ref,
ha_rows records);
/*
Create a QuickRangeSelect from given key and SEL_ARG tree for that key.
SYNOPSIS
get_quick_select()
param
idx Index of used key in param->key.
key_tree SEL_ARG tree for the used key
mrr_flags MRR parameter for quick select
mrr_buf_size MRR parameter for quick select
parent_alloc If not NULL, use it to allocate memory for
quick select data. Otherwise use quick->alloc.
NOTES
The caller must call QUICK_SELECT::init for returned quick select.
CAUTION! This function may change session->mem_root to a memory::Root which will be
deallocated when the returned quick select is deleted.
RETURN
NULL on error
otherwise created quick select
*/
QuickRangeSelect *get_quick_select(Parameter *param,
uint32_t index,
SEL_ARG *key_tree,
uint32_t mrr_flags,
uint32_t mrr_buf_size,
memory::Root *alloc);
uint32_t get_index_for_order(Table *table, Order *order, ha_rows limit);
SqlSelect *make_select(Table *head,
table_map const_tables,
table_map read_tables,
COND *conds,
bool allow_null_cond,
int *error);
bool get_quick_keys(Parameter *param,
QuickRangeSelect *quick,
KEY_PART *key,
SEL_ARG *key_tree,
unsigned char *min_key,
uint32_t min_key_flag,
unsigned char *max_key,
uint32_t max_key_flag);
} /* namespace optimizer */
} /* namespace drizzled */
#endif /* DRIZZLED_OPTIMIZER_RANGE_H */
|