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 */
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
17
21
/* classes to use when handling where clause */
22
#ifdef USE_PRAGMA_INTERFACE
23
#pragma interface /* gcc class implementation */
23
#ifndef DRIZZLED_OPT_RANGE_H
24
#define DRIZZLED_OPT_RANGE_H
26
#include <drizzled/field.h>
27
#include <mysys/queues.h>
28
#include <drizzled/item/sum.h>
31
typedef class Item COND;
33
typedef struct st_handler_buffer HANDLER_BUFFER;
26
35
typedef struct st_key_part {
41
50
class QUICK_RANGE :public Sql_alloc {
43
uchar *min_key,*max_key;
52
unsigned char *min_key,*max_key;
44
53
uint16_t min_length,max_length,flag;
45
54
key_part_map min_keypart_map, // bitmap of used keyparts in min_key
46
55
max_keypart_map; // bitmap of used keyparts in max_key
48
57
uint16_t dummy; /* Avoid warnings on 'flag' */
50
59
QUICK_RANGE(); /* Full range */
51
QUICK_RANGE(const uchar *min_key_arg, uint min_length_arg,
60
QUICK_RANGE(const unsigned char *min_key_arg, uint32_t min_length_arg,
52
61
key_part_map min_keypart_map_arg,
53
const uchar *max_key_arg, uint max_length_arg,
62
const unsigned char *max_key_arg, uint32_t max_length_arg,
54
63
key_part_map max_keypart_map_arg,
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)),
65
: min_key((unsigned char*) sql_memdup(min_key_arg,min_length_arg+1)),
66
max_key((unsigned char*) sql_memdup(max_key_arg,max_length_arg+1)),
58
67
min_length((uint16_t) min_length_arg),
59
68
max_length((uint16_t) max_length_arg),
60
69
flag((uint16_t) flag_arg),
118
127
ha_rows records; /* estimate of # of records to be retrieved */
119
128
double read_time; /* time to perform this retrieval */
122
131
Index this quick select uses, or MAX_KEY for quick selects
123
132
that use several indexes
128
137
Total length of first used_key_parts parts of the key.
129
138
Applicable if index!= MAX_KEY.
131
uint max_used_key_length;
140
uint32_t max_used_key_length;
134
143
Max. number of (first) key parts this quick select uses for retrieval.
169
178
reset() should be called when it is certain that row retrieval will be
170
179
necessary. This call may do heavyweight initialization like buffering first
171
180
N records etc. If reset() call fails get_next() must not be called.
172
Note that reset() may be called several times if
181
Note that reset() may be called several times if
173
182
* the quick select is executed in a subselect
174
183
* a JOIN buffer is used
234
243
This function is implemented only by quick selects that merge other quick
235
244
selects output and/or can produce output suitable for merging.
237
virtual void add_info_string(String *str __attribute__((unused))) {};
246
virtual void add_info_string(String *) {};
239
248
Return 1 if any index used by this quick select
240
249
uses field which is marked in passed bitmap.
270
279
QUICK_RANGE **last;
271
280
} QUICK_RANGE_SEQ_CTX;
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);
282
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
283
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
292
301
/* Range pointers to be used when not using MRR interface */
293
302
QUICK_RANGE **cur_range; /* current element in ranges */
294
303
QUICK_RANGE *last_range;
296
305
/* Members needed to use the MRR interface */
297
306
QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
299
uint mrr_flags; /* Flags to be used with MRR interface */
308
uint32_t mrr_flags; /* Flags to be used with MRR interface */
301
uint mrr_buf_size; /* copy from thd->variables.read_rnd_buff_size */
310
uint32_t mrr_buf_size; /* copy from session->variables.read_rnd_buff_size */
302
311
HANDLER_BUFFER *mrr_buf_desc; /* the handler buffer */
304
313
/* Info about index we're scanning */
305
314
KEY_PART *key_parts;
306
315
KEY_PART_INFO *key_part_info;
308
317
bool dont_free; /* Used by QUICK_SELECT_DESC */
310
319
int cmp_next(QUICK_RANGE *range);
316
QUICK_RANGE_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc,
325
QUICK_RANGE_SELECT(Session *session, Table *table,uint32_t index_arg,bool no_alloc,
317
326
MEM_ROOT *parent_alloc, bool *create_err);
318
327
~QUICK_RANGE_SELECT();
323
332
void range_end();
324
int get_next_prefix(uint prefix_length, key_part_map keypart_map,
333
int get_next_prefix(uint32_t prefix_length, key_part_map keypart_map,
334
unsigned char *cur_prefix);
326
335
bool reverse_sorted() { return 0; }
327
336
bool unique_key_range();
328
337
int init_ror_merged_scan(bool reuse_handler);
330
{ file->position(record); }
338
void save_last_pos();
331
339
int get_type() { return QS_TYPE_RANGE; }
332
340
void add_keys_and_lengths(String *key_names, String *used_lengths);
333
341
void add_info_string(String *str);
336
344
QUICK_RANGE_SELECT(const QUICK_RANGE_SELECT& org) : QUICK_SELECT_I()
338
346
memmove(this, &org, sizeof(*this));
340
348
Use default MRR implementation for reverse scans. No table engine
341
349
currently can do an MRR scan with output in reverse index order.
347
355
friend class TRP_ROR_INTERSECT;
349
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
357
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
350
358
struct st_table_ref *ref,
351
359
ha_rows records);
352
friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
353
KEY_PART *key, SEL_ARG *key_tree,
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,
360
friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
361
KEY_PART *key, SEL_ARG *key_tree,
362
unsigned char *min_key, uint32_t min_key_flag,
363
unsigned char *max_key, uint32_t max_key_flag);
364
friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
357
365
SEL_ARG *key_tree,
367
uint32_t mrr_buf_size,
360
368
MEM_ROOT *alloc);
361
369
friend class QUICK_SELECT_DESC;
362
370
friend class QUICK_INDEX_MERGE_SELECT;
363
371
friend class QUICK_ROR_INTERSECT_SELECT;
364
372
friend class QUICK_GROUP_MIN_MAX_SELECT;
365
friend uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
373
friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
366
374
friend range_seq_t quick_range_seq_init(void *init_param,
367
uint n_ranges, uint flags);
375
uint32_t n_ranges, uint32_t flags);
368
376
friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
369
377
bool distinct,const char *message);
486
494
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
489
QUICK_ROR_INTERSECT_SELECT(THD *thd, TABLE *table,
497
QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
490
498
bool retrieve_full_rows,
491
499
MEM_ROOT *parent_alloc);
492
500
~QUICK_ROR_INTERSECT_SELECT();
516
524
QUICK_RANGE_SELECT *cpk_quick;
518
526
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
519
THD *thd; /* current thread */
527
Session *session; /* current thread */
520
528
bool need_to_fetch_row; /* if true, do retrieve full table records. */
521
529
/* in top-level quick select, true if merged scans where initialized */
559
567
QUEUE queue; /* Priority queue for merge operation */
560
568
MEM_ROOT alloc; /* Memory pool for this and merged quick selects data. */
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() */
570
Session *session; /* current thread */
571
unsigned char *cur_rowid; /* buffer used in get_next() */
572
unsigned char *prev_rowid; /* rowid of last row returned by get_next() */
565
573
bool have_prev_rowid; /* true if prev_rowid has valid data */
566
uint rowid_length; /* table rowid length */
574
uint32_t rowid_length; /* table rowid length */
568
static int queue_cmp(void *arg, uchar *val1, uchar *val2);
576
static int queue_cmp(void *arg, unsigned char *val1, unsigned char *val2);
609
617
handler *file; /* The handler used to get data. */
610
618
JOIN *join; /* Descriptor of the current query */
611
619
KEY *index_info; /* The index chosen for data access */
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
unsigned char *record; /* Buffer where the next record is returned. */
621
unsigned char *tmp_record; /* Temporary storage for next_min(), next_max(). */
622
unsigned char *group_prefix; /* Key prefix consisting of the GROUP fields. */
623
uint32_t group_prefix_len; /* Length of the group prefix. */
624
uint32_t group_key_parts; /* A number of keyparts in the group prefix */
625
unsigned char *last_prefix; /* Prefix of the last group for detecting EOF. */
618
626
bool have_min; /* Specify whether we are computing */
619
627
bool have_max; /* a MIN, a MAX, or both. */
620
628
bool seen_first_key; /* Denotes whether the first key was retrieved.*/
621
629
KEY_PART_INFO *min_max_arg_part; /* The keypart of the only argument field */
622
630
/* of all MIN/MAX functions. */
623
uint min_max_arg_len; /* The length of the MIN/MAX argument field */
624
uchar *key_infix; /* Infix of constants from equality predicates. */
631
uint32_t min_max_arg_len; /* The length of the MIN/MAX argument field */
632
unsigned char *key_infix; /* Infix of constants from equality predicates. */
633
uint32_t key_infix_len;
626
634
DYNAMIC_ARRAY min_max_ranges; /* Array of range ptrs for the MIN/MAX field. */
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. */
635
uint32_t real_prefix_len; /* Length of key prefix extended with key_infix. */
636
uint32_t real_key_parts; /* A number of keyparts in the above value. */
629
637
List<Item_sum> *min_functions;
630
638
List<Item_sum> *max_functions;
631
639
List_iterator<Item_sum> *min_functions_it;
646
654
void update_min_result();
647
655
void update_max_result();
649
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join, bool have_min,
657
QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
650
658
bool have_max, KEY_PART_INFO *min_max_arg_part,
651
uint group_prefix_len, uint group_key_parts,
652
uint used_key_parts, KEY *index_info, uint
659
uint32_t group_prefix_len, uint32_t group_key_parts,
660
uint32_t used_key_parts, KEY *index_info, uint
653
661
use_index, double read_cost, ha_rows records, uint
654
key_infix_len, uchar *key_infix, MEM_ROOT
662
key_infix_len, unsigned char *key_infix, MEM_ROOT
656
664
~QUICK_GROUP_MIN_MAX_SELECT();
657
665
bool add_range(SEL_ARG *sel_range);
671
679
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
674
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint used_key_parts,
682
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
675
683
bool *create_err);
677
685
bool reverse_sorted() { return 1; }
680
688
bool range_reads_after_key(QUICK_RANGE *range);
682
bool test_if_null_range(QUICK_RANGE *range, uint used_key_parts);
690
bool test_if_null_range(QUICK_RANGE *range, uint32_t used_key_parts);
684
692
int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
685
693
List<QUICK_RANGE> rev_ranges;
692
700
QUICK_SELECT_I *quick; // If quick-select used
693
701
COND *cond; // where condition
695
703
IO_CACHE file; // Positions to used records
696
704
ha_rows records; // Records in use if read from file
697
705
double read_time; // Time to read rows
706
bool check_quick(THD *thd, bool force_quick_range, ha_rows limit)
710
return test_quick_select(thd, tmp, 0, limit, force_quick_range, false) < 0;
712
inline bool skip_record() { return cond ? cond->val_int() == 0 : 0; }
713
int test_quick_select(THD *thd, key_map keys, table_map prev_tables,
714
ha_rows limit, bool force_quick_range,
714
bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
716
int test_quick_select(Session *session, key_map keys, table_map prev_tables,
717
ha_rows limit, bool force_quick_range,
715
718
bool ordered_output);
718
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
721
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
719
722
struct st_table_ref *ref,
720
723
ha_rows records);
721
uint get_index_for_order(TABLE *table, ORDER *order, ha_rows limit);
724
uint32_t get_index_for_order(Table *table, order_st *order, ha_rows limit);