1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
17
/* classes to use when handling where clause */
23
19
#ifndef DRIZZLED_OPT_RANGE_H
24
20
#define DRIZZLED_OPT_RANGE_H
26
#include <mysys/queues.h>
22
#ifdef USE_PRAGMA_INTERFACE
23
#pragma interface /* gcc class implementation */
28
26
typedef struct st_key_part {
43
41
class QUICK_RANGE :public Sql_alloc {
45
unsigned char *min_key,*max_key;
43
uchar *min_key,*max_key;
46
44
uint16_t min_length,max_length,flag;
47
45
key_part_map min_keypart_map, // bitmap of used keyparts in min_key
48
46
max_keypart_map; // bitmap of used keyparts in max_key
50
48
uint16_t dummy; /* Avoid warnings on 'flag' */
52
50
QUICK_RANGE(); /* Full range */
53
QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
51
QUICK_RANGE(const uchar *min_key_arg, uint min_length_arg,
54
52
key_part_map min_keypart_map_arg,
55
const unsigned char *max_key_arg, uint32_t max_length_arg,
53
const uchar *max_key_arg, uint max_length_arg,
56
54
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)),
56
: min_key((uchar*) sql_memdup(min_key_arg,min_length_arg+1)),
57
max_key((uchar*) sql_memdup(max_key_arg,max_length_arg+1)),
60
58
min_length((uint16_t) min_length_arg),
61
59
max_length((uint16_t) max_length_arg),
62
60
flag((uint16_t) flag_arg),
124
122
Index this quick select uses, or MAX_KEY for quick selects
125
123
that use several indexes
130
128
Total length of first used_key_parts parts of the key.
131
129
Applicable if index!= MAX_KEY.
133
uint32_t max_used_key_length;
131
uint max_used_key_length;
136
134
Max. number of (first) key parts this quick select uses for retrieval.
236
234
This function is implemented only by quick selects that merge other quick
237
235
selects output and/or can produce output suitable for merging.
239
virtual void add_info_string(String *) {};
237
virtual void add_info_string(String *str __attribute__((unused))) {};
241
239
Return 1 if any index used by this quick select
242
240
uses field which is marked in passed bitmap.
272
270
QUICK_RANGE **last;
273
271
} 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);
273
range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags);
274
uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
298
296
/* Members needed to use the MRR interface */
299
297
QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
301
uint32_t mrr_flags; /* Flags to be used with MRR interface */
299
uint mrr_flags; /* Flags to be used with MRR interface */
303
uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
301
uint mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */
304
302
HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
306
304
/* Info about index we're scanning */
318
QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
316
QUICK_RANGE_SELECT(THD *thd, Table *table,uint index_arg,bool no_alloc,
319
317
MEM_ROOT *parent_alloc, bool *create_err);
320
318
~QUICK_RANGE_SELECT();
325
323
void range_end();
326
int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
327
unsigned char *cur_prefix);
324
int get_next_prefix(uint prefix_length, key_part_map keypart_map,
328
326
bool reverse_sorted() { return 0; }
329
327
bool unique_key_range();
330
328
int init_ror_merged_scan(bool reuse_handler);
349
347
friend class TRP_ROR_INTERSECT;
351
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
349
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
352
350
struct st_table_ref *ref,
353
351
ha_rows records);
354
352
friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
355
353
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,
354
uchar *min_key, uint min_key_flag,
355
uchar *max_key, uint max_key_flag);
356
friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint idx,
359
357
SEL_ARG *key_tree,
361
uint32_t mrr_buf_size,
362
360
MEM_ROOT *alloc);
363
361
friend class QUICK_SELECT_DESC;
364
362
friend class QUICK_INDEX_MERGE_SELECT;
365
363
friend class QUICK_ROR_INTERSECT_SELECT;
366
364
friend class QUICK_GROUP_MIN_MAX_SELECT;
367
friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
365
friend uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
368
366
friend range_seq_t quick_range_seq_init(void *init_param,
369
uint32_t n_ranges, uint32_t flags);
367
uint n_ranges, uint flags);
370
368
friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
371
369
bool distinct,const char *message);
434
432
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
437
QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
435
QUICK_INDEX_MERGE_SELECT(THD *thd, Table *table);
438
436
~QUICK_INDEX_MERGE_SELECT();
488
486
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
491
QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
489
QUICK_ROR_INTERSECT_SELECT(THD *thd, Table *table,
492
490
bool retrieve_full_rows,
493
491
MEM_ROOT *parent_alloc);
494
492
~QUICK_ROR_INTERSECT_SELECT();
518
516
QUICK_RANGE_SELECT *cpk_quick;
520
518
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
521
Session *session; /* current thread */
519
THD *thd; /* current thread */
522
520
bool need_to_fetch_row; /* if true, do retrieve full table records. */
523
521
/* in top-level quick select, true if merged scans where initialized */
524
522
bool scans_inited;
541
539
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
544
QUICK_ROR_UNION_SELECT(Session *session, Table *table);
542
QUICK_ROR_UNION_SELECT(THD *thd, Table *table);
545
543
~QUICK_ROR_UNION_SELECT();
561
559
QUEUE queue; /* Priority queue for merge operation */
562
560
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() */
562
THD *thd; /* current thread */
563
uchar *cur_rowid; /* buffer used in get_next() */
564
uchar *prev_rowid; /* rowid of last row returned by get_next() */
567
565
bool have_prev_rowid; /* true if prev_rowid has valid data */
568
uint32_t rowid_length; /* table rowid length */
566
uint rowid_length; /* table rowid length */
570
static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
568
static int queue_cmp(void *arg, uchar *val1, uchar *val2);
571
569
bool scans_inited;
611
609
handler *file; /* The handler used to get data. */
612
610
JOIN *join; /* Descriptor of the current query */
613
611
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. */
612
uchar *record; /* Buffer where the next record is returned. */
613
uchar *tmp_record; /* Temporary storage for next_min(), next_max(). */
614
uchar *group_prefix; /* Key prefix consisting of the GROUP fields. */
615
uint group_prefix_len; /* Length of the group prefix. */
616
uint group_key_parts; /* A number of keyparts in the group prefix */
617
uchar *last_prefix; /* Prefix of the last group for detecting EOF. */
620
618
bool have_min; /* Specify whether we are computing */
621
619
bool have_max; /* a MIN, a MAX, or both. */
622
620
bool seen_first_key; /* Denotes whether the first key was retrieved.*/
623
621
KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
624
622
/* 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;
623
uint min_max_arg_len; /* The length of the MIN/MAX argument field */
624
uchar *key_infix; /* Infix of constants from equality predicates. */
628
626
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. */
627
uint real_prefix_len; /* Length of key prefix extended with key_infix. */
628
uint real_key_parts; /* A number of keyparts in the above value. */
631
629
List<Item_sum> *min_functions;
632
630
List<Item_sum> *max_functions;
633
631
List_iterator<Item_sum> *min_functions_it;
651
649
QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
652
650
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
651
uint group_prefix_len, uint group_key_parts,
652
uint used_key_parts, KEY *index_info, uint
655
653
use_index, double read_cost, ha_rows records, uint
656
key_infix_len, unsigned char *key_infix, MEM_ROOT
654
key_infix_len, uchar *key_infix, MEM_ROOT
658
656
~QUICK_GROUP_MIN_MAX_SELECT();
659
657
bool add_range(SEL_ARG *sel_range);
673
671
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
676
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
674
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts,
677
675
bool *create_err);
679
677
bool reverse_sorted() { return 1; }
682
680
bool range_reads_after_key(QUICK_RANGE *range);
684
bool test_if_null_range(QUICK_RANGE *range, uint32_t used_key_parts);
682
bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
686
684
int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
687
685
List<QUICK_RANGE> rev_ranges;
708
bool check_quick(Session *session, bool force_quick_range, ha_rows limit)
706
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;
710
return test_quick_select(thd, tmp, 0, limit, force_quick_range, false) < 0;
714
712
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,
713
int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
716
714
ha_rows limit, bool force_quick_range,
717
715
bool ordered_output);
720
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
718
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, Table *table,
721
719
struct st_table_ref *ref,
722
720
ha_rows records);
723
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);
721
uint get_index_for_order(Table *table, order_st *order, ha_rows limit);