23
23
#ifndef DRIZZLED_OPT_RANGE_H
24
24
#define DRIZZLED_OPT_RANGE_H
26
#include <mysys/queues.h>
26
#ifdef USE_PRAGMA_INTERFACE
27
#pragma interface /* gcc class implementation */
28
30
typedef struct st_key_part {
43
45
class QUICK_RANGE :public Sql_alloc {
45
unsigned char *min_key,*max_key;
47
uchar *min_key,*max_key;
46
48
uint16_t min_length,max_length,flag;
47
49
key_part_map min_keypart_map, // bitmap of used keyparts in min_key
48
50
max_keypart_map; // bitmap of used keyparts in max_key
50
52
uint16_t dummy; /* Avoid warnings on 'flag' */
52
54
QUICK_RANGE(); /* Full range */
53
QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
55
QUICK_RANGE(const uchar *min_key_arg, uint min_length_arg,
54
56
key_part_map min_keypart_map_arg,
55
const unsigned char *max_key_arg, uint32_t max_length_arg,
57
const uchar *max_key_arg, uint max_length_arg,
56
58
key_part_map max_keypart_map_arg,
58
: min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
59
max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
60
: min_key((uchar*) sql_memdup(min_key_arg,min_length_arg+1)),
61
max_key((uchar*) sql_memdup(max_key_arg,max_length_arg+1)),
60
62
min_length((uint16_t) min_length_arg),
61
63
max_length((uint16_t) max_length_arg),
62
64
flag((uint16_t) flag_arg),
124
126
Index this quick select uses, or MAX_KEY for quick selects
125
127
that use several indexes
130
132
Total length of first used_key_parts parts of the key.
131
133
Applicable if index!= MAX_KEY.
133
uint32_t max_used_key_length;
135
uint max_used_key_length;
136
138
Max. number of (first) key parts this quick select uses for retrieval.
236
238
This function is implemented only by quick selects that merge other quick
237
239
selects output and/or can produce output suitable for merging.
239
virtual void add_info_string(String *) {};
241
virtual void add_info_string(String *str __attribute__((unused))) {};
241
243
Return 1 if any index used by this quick select
242
244
uses field which is marked in passed bitmap.
272
274
QUICK_RANGE **last;
273
275
} QUICK_RANGE_SEQ_CTX;
275
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
276
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
277
range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags);
278
uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
298
300
/* Members needed to use the MRR interface */
299
301
QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
301
uint32_t mrr_flags; /* Flags to be used with MRR interface */
303
uint mrr_flags; /* Flags to be used with MRR interface */
303
uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
305
uint mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */
304
306
HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
306
308
/* Info about index we're scanning */
318
QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
320
QUICK_RANGE_SELECT(THD *thd, Table *table,uint index_arg,bool no_alloc,
319
321
MEM_ROOT *parent_alloc, bool *create_err);
320
322
~QUICK_RANGE_SELECT();
325
327
void range_end();
326
int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
327
unsigned char *cur_prefix);
328
int get_next_prefix(uint prefix_length, key_part_map keypart_map,
328
330
bool reverse_sorted() { return 0; }
329
331
bool unique_key_range();
330
332
int init_ror_merged_scan(bool reuse_handler);
349
351
friend class TRP_ROR_INTERSECT;
351
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
353
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
352
354
struct st_table_ref *ref,
353
355
ha_rows records);
354
356
friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
355
357
KEY_PART *key, SEL_ARG *key_tree,
356
unsigned char *min_key, uint32_t min_key_flag,
357
unsigned char *max_key, uint32_t max_key_flag);
358
friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
358
uchar *min_key, uint min_key_flag,
359
uchar *max_key, uint max_key_flag);
360
friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint idx,
359
361
SEL_ARG *key_tree,
361
uint32_t mrr_buf_size,
362
364
MEM_ROOT *alloc);
363
365
friend class QUICK_SELECT_DESC;
364
366
friend class QUICK_INDEX_MERGE_SELECT;
365
367
friend class QUICK_ROR_INTERSECT_SELECT;
366
368
friend class QUICK_GROUP_MIN_MAX_SELECT;
367
friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
369
friend uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
368
370
friend range_seq_t quick_range_seq_init(void *init_param,
369
uint32_t n_ranges, uint32_t flags);
371
uint n_ranges, uint flags);
370
372
friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
371
373
bool distinct,const char *message);
434
436
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
437
QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
439
QUICK_INDEX_MERGE_SELECT(THD *thd, Table *table);
438
440
~QUICK_INDEX_MERGE_SELECT();
488
490
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
491
QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
493
QUICK_ROR_INTERSECT_SELECT(THD *thd, Table *table,
492
494
bool retrieve_full_rows,
493
495
MEM_ROOT *parent_alloc);
494
496
~QUICK_ROR_INTERSECT_SELECT();
518
520
QUICK_RANGE_SELECT *cpk_quick;
520
522
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
521
Session *session; /* current thread */
523
THD *thd; /* current thread */
522
524
bool need_to_fetch_row; /* if true, do retrieve full table records. */
523
525
/* in top-level quick select, true if merged scans where initialized */
524
526
bool scans_inited;
541
543
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
544
QUICK_ROR_UNION_SELECT(Session *session, Table *table);
546
QUICK_ROR_UNION_SELECT(THD *thd, Table *table);
545
547
~QUICK_ROR_UNION_SELECT();
561
563
QUEUE queue; /* Priority queue for merge operation */
562
564
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
564
Session *session; /* current thread */
565
unsigned char *cur_rowid; /* buffer used in get_next() */
566
unsigned char *prev_rowid; /* rowid of last row returned by get_next() */
566
THD *thd; /* current thread */
567
uchar *cur_rowid; /* buffer used in get_next() */
568
uchar *prev_rowid; /* rowid of last row returned by get_next() */
567
569
bool have_prev_rowid; /* true if prev_rowid has valid data */
568
uint32_t rowid_length; /* table rowid length */
570
uint rowid_length; /* table rowid length */
570
static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
572
static int queue_cmp(void *arg, uchar *val1, uchar *val2);
571
573
bool scans_inited;
611
613
handler *file; /* The handler used to get data. */
612
614
JOIN *join; /* Descriptor of the current query */
613
615
KEY *index_info; /* The index chosen for data access */
614
unsigned char *record; /* Buffer where the next record is returned. */
615
unsigned char *tmp_record; /* Temporary storage for next_min(), next_max(). */
616
unsigned char *group_prefix; /* Key prefix consisting of the GROUP fields. */
617
uint32_t group_prefix_len; /* Length of the group prefix. */
618
uint32_t group_key_parts; /* A number of keyparts in the group prefix */
619
unsigned char *last_prefix; /* Prefix of the last group for detecting EOF. */
616
uchar *record; /* Buffer where the next record is returned. */
617
uchar *tmp_record; /* Temporary storage for next_min(), next_max(). */
618
uchar *group_prefix; /* Key prefix consisting of the GROUP fields. */
619
uint group_prefix_len; /* Length of the group prefix. */
620
uint group_key_parts; /* A number of keyparts in the group prefix */
621
uchar *last_prefix; /* Prefix of the last group for detecting EOF. */
620
622
bool have_min; /* Specify whether we are computing */
621
623
bool have_max; /* a MIN, a MAX, or both. */
622
624
bool seen_first_key; /* Denotes whether the first key was retrieved.*/
623
625
KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
624
626
/* of all MIN/MAX functions. */
625
uint32_t min_max_arg_len; /* The length of the MIN/MAX argument field */
626
unsigned char *key_infix; /* Infix of constants from equality predicates. */
627
uint32_t key_infix_len;
627
uint min_max_arg_len; /* The length of the MIN/MAX argument field */
628
uchar *key_infix; /* Infix of constants from equality predicates. */
628
630
DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
629
uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
630
uint32_t real_key_parts; /* A number of keyparts in the above value. */
631
uint real_prefix_len; /* Length of key prefix extended with key_infix. */
632
uint real_key_parts; /* A number of keyparts in the above value. */
631
633
List<Item_sum> *min_functions;
632
634
List<Item_sum> *max_functions;
633
635
List_iterator<Item_sum> *min_functions_it;
651
653
QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
652
654
bool have_max, KEY_PART_INFO *min_max_arg_part,
653
uint32_t group_prefix_len, uint32_t group_key_parts,
654
uint32_t used_key_parts, KEY *index_info, uint
655
uint group_prefix_len, uint group_key_parts,
656
uint used_key_parts, KEY *index_info, uint
655
657
use_index, double read_cost, ha_rows records, uint
656
key_infix_len, unsigned char *key_infix, MEM_ROOT
658
key_infix_len, uchar *key_infix, MEM_ROOT
658
660
~QUICK_GROUP_MIN_MAX_SELECT();
659
661
bool add_range(SEL_ARG *sel_range);
673
675
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
676
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
678
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts,
677
679
bool *create_err);
679
681
bool reverse_sorted() { return 1; }
682
684
bool range_reads_after_key(QUICK_RANGE *range);
684
bool test_if_null_range(QUICK_RANGE *range, uint32_t used_key_parts);
686
bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
686
688
int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
687
689
List<QUICK_RANGE> rev_ranges;
708
bool check_quick(Session *session, bool force_quick_range, ha_rows limit)
710
bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
712
return test_quick_select(session, tmp, 0, limit, force_quick_range, false) < 0;
714
return test_quick_select(thd, tmp, 0, limit, force_quick_range, false) < 0;
714
716
inline bool skip_record() { return cond ? cond->val_int() == 0 : 0; }
715
int test_quick_select(Session *session, key_map keys, table_map prev_tables,
717
int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
716
718
ha_rows limit, bool force_quick_range,
717
719
bool ordered_output);
720
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
722
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
721
723
struct st_table_ref *ref,
722
724
ha_rows records);
723
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);
725
uint get_index_for_order(Table *table, order_st *order, ha_rows limit);