33
32
typedef struct st_handler_buffer HANDLER_BUFFER;
35
typedef struct st_key_part {
34
typedef struct st_key_part
37
38
/* See KEY_PART_INFO for meaning of the next two: */
38
uint16_t store_length, length;
39
uint16_t store_length;
41
43
Keypart flags (0 when this structure is used by partition pruning code
42
44
for fake partitioning index description)
46
Field::imagetype image_type;
50
class QUICK_RANGE :public Sql_alloc {
52
unsigned char *min_key,*max_key;
53
uint16_t min_length,max_length,flag;
54
key_part_map min_keypart_map, // bitmap of used keyparts in min_key
55
max_keypart_map; // bitmap of used keyparts in max_key
50
class QUICK_RANGE :public Sql_alloc
53
unsigned char *min_key;
54
unsigned char *max_key;
58
key_part_map min_keypart_map; /**< bitmap of used keyparts in min_key */
59
key_part_map max_keypart_map; /**< bitmap of used keyparts in max_key */
57
61
uint16_t dummy; /* Avoid warnings on 'flag' */
59
QUICK_RANGE(); /* Full range */
60
QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
63
QUICK_RANGE(); /**< Constructor for a "full range" */
64
QUICK_RANGE(const unsigned char *min_key_arg,
65
uint32_t min_length_arg,
61
66
key_part_map min_keypart_map_arg,
62
const unsigned char *max_key_arg, uint32_t max_length_arg,
67
const unsigned char *max_key_arg,
68
uint32_t max_length_arg,
63
69
key_part_map max_keypart_map_arg,
65
: min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
72
min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
66
73
max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
67
74
min_length((uint16_t) min_length_arg),
68
75
max_length((uint16_t) max_length_arg),
123
128
class QUICK_SELECT_I
127
ha_rows records; /* estimate of # of records to be retrieved */
128
double read_time; /* time to perform this retrieval */
132
ha_rows records; /**< estimate of # of records to be retrieved */
133
double read_time; /**< time to perform this retrieval */
131
136
Index this quick select uses, or MAX_KEY for quick selects
132
137
that use several indexes
137
141
Total length of first used_key_parts parts of the key.
138
142
Applicable if index!= MAX_KEY.
140
144
uint32_t max_used_key_length;
143
Max. number of (first) key parts this quick select uses for retrieval.
146
Maximum number of (first) key parts this quick select uses for retrieval.
144
147
eg. for "(key1p1=c1 AND key1p2=c2) OR key1p1=c2" used_key_parts == 2.
145
148
Applicable if index!= MAX_KEY.
147
150
For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
149
152
uint32_t used_key_parts;
154
* The rowid of last row retrieved by this quick select. This is used only when
155
* doing ROR-index_merge selects
157
unsigned char *last_rowid;
160
* Table record buffer used by this quick select.
162
unsigned char *record;
151
164
QUICK_SELECT_I();
152
165
virtual ~QUICK_SELECT_I(){};
155
Do post-constructor initialization.
159
init() performs initializations that should have been in constructor if
160
it was possible to return errors from constructors. The join optimizer may
161
create and then delete quick selects without retrieving any rows so init()
162
must not contain any IO or CPU intensive code.
164
If init() call fails the only valid action is to delete this quick select,
165
reset() and get_next() must not be called.
171
virtual int init() = 0;
174
Initialize quick select for row retrieval.
178
reset() should be called when it is certain that row retrieval will be
179
necessary. This call may do heavyweight initialization like buffering first
180
N records etc. If reset() call fails get_next() must not be called.
181
Note that reset() may be called several times if
182
* the quick select is executed in a subselect
183
* a JOIN buffer is used
189
virtual int reset(void) = 0;
191
virtual int get_next() = 0; /* get next record to retrieve */
193
/* Range end should be called when we have looped over the whole index */
168
* Do post-constructor initialization.
172
* Performs initializations that should have been in constructor if
173
* it was possible to return errors from constructors. The join optimizer may
174
* create and then delete quick selects without retrieving any rows so init()
175
* must not contain any IO or CPU intensive code.
177
* If init() call fails the only valid action is to delete this quick select,
178
* reset() and get_next() must not be called.
185
virtual int init() = 0;
188
* Initializes quick select for row retrieval.
192
* Should be called when it is certain that row retrieval will be
193
* necessary. This call may do heavyweight initialization like buffering first
194
* N records etc. If reset() call fails get_next() must not be called.
195
* Note that reset() may be called several times if
196
* - the quick select is executed in a subselect
197
* - a JOIN buffer is used
204
virtual int reset(void) = 0;
205
/** Gets next record to retrieve */
206
virtual int get_next() = 0;
208
/** Range end should be called when we have looped over the whole index */
194
209
virtual void range_end() {}
196
211
virtual bool reverse_sorted() = 0;
197
virtual bool unique_key_range() { return false; }
212
virtual bool unique_key_range()
201
QS_TYPE_INDEX_MERGE = 1,
202
QS_TYPE_RANGE_DESC = 2,
203
QS_TYPE_ROR_INTERSECT = 4,
204
QS_TYPE_ROR_UNION = 5,
205
QS_TYPE_GROUP_MIN_MAX = 6
220
QS_TYPE_INDEX_MERGE= 1,
221
QS_TYPE_RANGE_DESC= 2,
222
QS_TYPE_ROR_INTERSECT= 4,
223
QS_TYPE_ROR_UNION= 5,
224
QS_TYPE_GROUP_MIN_MAX= 6
208
/* Get type of this quick select - one of the QS_TYPE_* values */
227
/** Returns the type of this quick select - one of the QS_TYPE_* values */
209
228
virtual int get_type() = 0;
212
Initialize this quick select as a merged scan inside a ROR-union or a ROR-
213
intersection scan. The caller must not additionally call init() if this
216
init_ror_merged_scan()
217
reuse_handler If true, the quick select may use table->handler,
218
otherwise it must create and use a separate handler
231
* Initialize this quick select as a merged scan inside a ROR-union or a ROR-
232
* intersection scan. The caller must not additionally call init() if this
233
* function is called.
235
* @param If true, the quick select may use table->Cursor,
236
* otherwise it must create and use a separate Cursor
224
244
virtual int init_ror_merged_scan(bool)
225
{ assert(0); return 1; }
228
Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
251
* Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
230
253
virtual void save_last_pos(){};
233
Append comma-separated list of keys this quick select uses to key_names;
234
append comma-separated list of corresponding used lengths to used_lengths.
235
This is used by select_describe.
237
virtual void add_keys_and_lengths(String *key_names,
238
String *used_lengths)=0;
241
Append text representation of quick select structure (what and how is
242
merged) to str. The result is added to "Extra" field in EXPLAIN output.
243
This function is implemented only by quick selects that merge other quick
244
selects output and/or can produce output suitable for merging.
246
virtual void add_info_string(String *) {};
248
Return 1 if any index used by this quick select
249
uses field which is marked in passed bitmap.
251
virtual bool is_keys_used(const MY_BITMAP *fields);
254
rowid of last row retrieved by this quick select. This is used only when
255
doing ROR-index_merge selects
257
unsigned char *last_rowid;
260
Table record buffer used by this quick select.
262
unsigned char *record;
256
* Append comma-separated list of keys this quick select uses to key_names;
257
* append comma-separated list of corresponding used lengths to used_lengths.
259
* @note This is used by select_describe.
261
virtual void add_keys_and_lengths(String *key_names, String *used_lengths)=0;
264
* Append text representation of quick select structure (what and how is
265
* merged) to str. The result is added to "Extra" field in EXPLAIN output.
269
* This function is implemented only by quick selects that merge other quick
270
* selects output and/or can produce output suitable for merging.
272
virtual void add_info_string(String *)
276
* Returns true if any index used by this quick select
277
* uses field which is marked in passed bitmap.
279
virtual bool is_keys_used(const MyBitmap *fields);
266
282
struct st_qsel_param;
272
MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
287
* MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
275
290
typedef struct st_quick_range_seq_ctx
277
292
QUICK_RANGE **first;
282
297
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
283
298
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
287
Quick select that does a range scan on a single key. The records are
288
returned in key order.
301
* Quick select that does a range scan on a single key.
303
* The records are returned in key order.
290
306
class QUICK_RANGE_SELECT : public QUICK_SELECT_I
294
DYNAMIC_ARRAY ranges; /* ordered array of range ptrs */
310
DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
296
/* Members to deal with case when this quick select is a ROR-merged scan */
312
/** Members to deal with case when this quick select is a ROR-merged scan */
297
313
bool in_ror_merged_scan;
298
MY_BITMAP column_bitmap, *save_read_set, *save_write_set;
299
bool free_file; /* TRUE <=> this->file is "owned" by this quick select */
314
MyBitmap column_bitmap;
315
MyBitmap *save_read_set;
316
MyBitmap *save_write_set;
317
bool free_file; /**< True when this->file is "owned" by this quick select */
301
319
/* Range pointers to be used when not using MRR interface */
302
QUICK_RANGE **cur_range; /* current element in ranges */
320
QUICK_RANGE **cur_range; /**< current element in ranges */
303
321
QUICK_RANGE *last_range;
305
/* Members needed to use the MRR interface */
323
/** Members needed to use the MRR interface */
306
324
QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
308
uint32_t mrr_flags; /* Flags to be used with MRR interface */
310
uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
311
HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
325
uint32_t mrr_buf_size; /**< copy from session->variables.read_rnd_buff_size */
326
HANDLER_BUFFER *mrr_buf_desc; /**< the Cursor buffer */
313
/* Info about index we're scanning */
328
/** Info about index we're scanning */
314
329
KEY_PART *key_parts;
315
330
KEY_PART_INFO *key_part_info;
317
bool dont_free; /* Used by QUICK_SELECT_DESC */
332
bool dont_free; /**< Used by QUICK_SELECT_DESC */
319
334
int cmp_next(QUICK_RANGE *range);
320
335
int cmp_prev(QUICK_RANGE *range);
321
336
bool row_in_ranges();
338
uint32_t mrr_flags; /**< Flags to be used with MRR interface */
325
QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
326
MEM_ROOT *parent_alloc, bool *create_err);
341
QUICK_RANGE_SELECT(Session *session,
345
MEM_ROOT *parent_alloc,
327
347
~QUICK_RANGE_SELECT();
332
352
void range_end();
333
int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
353
int get_next_prefix(uint32_t prefix_length,
354
key_part_map keypart_map,
334
355
unsigned char *cur_prefix);
335
bool reverse_sorted() { return 0; }
356
bool reverse_sorted()
336
360
bool unique_key_range();
337
361
int init_ror_merged_scan(bool reuse_handler);
338
362
void save_last_pos();
339
int get_type() { return QS_TYPE_RANGE; }
365
return QS_TYPE_RANGE;
340
367
void add_keys_and_lengths(String *key_names, String *used_lengths);
341
368
void add_info_string(String *str);
499
532
MEM_ROOT *parent_alloc);
500
533
~QUICK_ROR_INTERSECT_SELECT();
505
bool reverse_sorted() { return false; }
506
bool unique_key_range() { return false; }
507
int get_type() { return QS_TYPE_ROR_INTERSECT; }
538
bool reverse_sorted()
542
bool unique_key_range()
548
return QS_TYPE_ROR_INTERSECT;
508
550
void add_keys_and_lengths(String *key_names, String *used_lengths);
509
551
void add_info_string(String *str);
510
bool is_keys_used(const MY_BITMAP *fields);
552
bool is_keys_used(const MyBitmap *fields);
511
553
int init_ror_merged_scan(bool reuse_handler);
512
554
bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
515
Range quick selects this intersection consists of, not including
557
* Range quick selects this intersection consists of, not including
518
560
List<QUICK_RANGE_SELECT> quick_selects;
521
Merged quick select that uses Clustered PK, if there is one. This quick
522
select is not used for row retrieval, it is used for row retrieval.
563
* Merged quick select that uses Clustered PK, if there is one. This quick
564
* select is not used for row retrieval, it is used for row retrieval.
524
566
QUICK_RANGE_SELECT *cpk_quick;
526
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
527
Session *session; /* current thread */
528
bool need_to_fetch_row; /* if true, do retrieve full table records. */
529
/* in top-level quick select, true if merged scans where initialized */
568
MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
569
Session *session; /**< Pointer to the current session */
570
bool need_to_fetch_row; /**< if true, do retrieve full table records. */
571
/** in top-level quick select, true if merged scans where initialized */
530
572
bool scans_inited;
564
bool reverse_sorted() { return false; }
565
bool unique_key_range() { return false; }
566
int get_type() { return QS_TYPE_ROR_UNION; }
604
bool reverse_sorted()
608
bool unique_key_range()
614
return QS_TYPE_ROR_UNION;
567
616
void add_keys_and_lengths(String *key_names, String *used_lengths);
568
617
void add_info_string(String *str);
569
bool is_keys_used(const MY_BITMAP *fields);
618
bool is_keys_used(const MyBitmap *fields);
571
620
bool push_quick_back(QUICK_SELECT_I *quick_sel_range);
573
List<QUICK_SELECT_I> quick_selects; /* Merged quick selects */
575
/* Priority queue for merge operation */
576
std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor >
578
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
580
Session *session; /* current thread */
581
unsigned char *cur_rowid; /* buffer used in get_next() */
582
unsigned char *prev_rowid; /* rowid of last row returned by get_next() */
583
bool have_prev_rowid; /* true if prev_rowid has valid data */
584
uint32_t rowid_length; /* table rowid length */
622
List<QUICK_SELECT_I> quick_selects; /**< Merged quick selects */
624
/** Priority queue for merge operation */
625
std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor > *queue;
626
MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
628
Session *session; /**< current thread */
629
unsigned char *cur_rowid; /**< buffer used in get_next() */
630
unsigned char *prev_rowid; /**< rowid of last row returned by get_next() */
631
bool have_prev_rowid; /**< true if prev_rowid has valid data */
632
uint32_t rowid_length; /**< table rowid length */
586
634
bool scans_inited;
590
638
Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
592
640
This class provides a specialized index access method for GROUP-BY queries
618
666
Since one of the requirements is that all select fields are part of the same
619
667
index, this class produces only index keys, and not complete records.
622
669
class QUICK_GROUP_MIN_MAX_SELECT : public QUICK_SELECT_I
625
handler *file; /* The handler used to get data. */
626
JOIN *join; /* Descriptor of the current query */
627
KEY *index_info; /* The index chosen for data access */
628
unsigned char *record; /* Buffer where the next record is returned. */
629
unsigned char *tmp_record; /* Temporary storage for next_min(), next_max(). */
630
unsigned char *group_prefix; /* Key prefix consisting of the GROUP fields. */
631
uint32_t group_prefix_len; /* Length of the group prefix. */
632
uint32_t group_key_parts; /* A number of keyparts in the group prefix */
633
unsigned char *last_prefix; /* Prefix of the last group for detecting EOF. */
634
bool have_min; /* Specify whether we are computing */
635
bool have_max; /* a MIN, a MAX, or both. */
636
bool seen_first_key; /* Denotes whether the first key was retrieved.*/
637
KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
638
/* of all MIN/MAX functions. */
639
uint32_t min_max_arg_len; /* The length of the MIN/MAX argument field */
640
unsigned char *key_infix; /* Infix of constants from equality predicates. */
672
Cursor *file; /**< The Cursor used to get data. */
673
JOIN *join; /**< Descriptor of the current query */
674
KEY *index_info; /**< The index chosen for data access */
675
unsigned char *record; /**< Buffer where the next record is returned. */
676
unsigned char *tmp_record; /**< Temporary storage for next_min(), next_max(). */
677
unsigned char *group_prefix; /**< Key prefix consisting of the GROUP fields. */
678
uint32_t group_prefix_len; /**< Length of the group prefix. */
679
uint32_t group_key_parts; /**< A number of keyparts in the group prefix */
680
unsigned char *last_prefix; /**< Prefix of the last group for detecting EOF. */
681
bool have_min; /**< Specify whether we are computing */
682
bool have_max; /**< a MIN, a MAX, or both. */
683
bool seen_first_key; /**< Denotes whether the first key was retrieved.*/
684
KEY_PART_INFO *min_max_arg_part; /** The keypart of the only argument field of all MIN/MAX functions. */
685
uint32_t min_max_arg_len; /**< The length of the MIN/MAX argument field */
686
unsigned char *key_infix; /**< Infix of constants from equality predicates. */
641
687
uint32_t key_infix_len;
642
DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
643
uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
644
uint32_t real_key_parts; /* A number of keyparts in the above value. */
688
DYNAMIC_ARRAY min_max_ranges; /**< Array of range ptrs for the MIN/MAX field. */
689
uint32_t real_prefix_len; /**< Length of key prefix extended with key_infix. */
690
uint32_t real_key_parts; /**< A number of keyparts in the above value. */
645
691
List<Item_sum> *min_functions;
646
692
List<Item_sum> *max_functions;
647
693
List_iterator<Item_sum> *min_functions_it;
699
753
List_iterator<QUICK_RANGE> rev_it;
757
* Executor class for SELECT statements.
761
* The QUICK_SELECT_I member variable is the implementor
762
* of the SELECT execution.
703
764
class SQL_SELECT :public Sql_alloc {
705
QUICK_SELECT_I *quick; // If quick-select used
706
COND *cond; // where condition
766
QUICK_SELECT_I *quick; /**< If quick-select used */
767
COND *cond; /**< where condition */
708
IO_CACHE file; // Positions to used records
709
ha_rows records; // Records in use if read from file
710
double read_time; // Time to read rows
711
key_map quick_keys; // Possible quick keys
712
key_map needed_reg; // Possible quick keys after prev tables.
713
table_map const_tables,read_tables;
769
IO_CACHE file; /**< Positions to used records */
770
ha_rows records; /**< Records in use if read from file */
771
double read_time; /**< Time to read rows */
772
key_map quick_keys; /**< Possible quick keys */
773
key_map needed_reg; /**< Possible quick keys after prev tables. */
774
table_map const_tables;
775
table_map read_tables;