1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
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
22
#include <drizzled/dynamic_array.h>
23
#include <drizzled/optimizer/range.h>
25
#include <boost/dynamic_bitset.hpp>
32
* Quick select that does a range scan on a single key.
34
* The records are returned in key order.
37
class QuickRangeSelect : public QuickSelectInterface
41
DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
43
/** Members to deal with case when this quick select is a ROR-merged scan */
44
bool in_ror_merged_scan;
45
boost::dynamic_bitset<> *column_bitmap;
46
boost::dynamic_bitset<> *save_read_set;
47
boost::dynamic_bitset<> *save_write_set;
48
bool free_file; /**< True when this->file is "owned" by this quick select */
50
/* Range pointers to be used when not using MRR interface */
51
QuickRange **cur_range; /**< current element in ranges */
52
QuickRange *last_range;
54
/** Members needed to use the MRR interface */
55
QuickRangeSequenceContext qr_traversal_ctx;
56
uint32_t mrr_buf_size; /**< copy from session->variables.read_rnd_buff_size */
58
/** Info about index we're scanning */
60
KeyPartInfo *key_part_info;
62
bool dont_free; /**< Used by QuickSelectDescending */
65
* Compare if found key is over max-value
66
* @return 0 if key <= range->max_key
67
* @todo: Figure out why can't this function be as simple as cmp_prev().
69
int cmp_next(QuickRange *range);
72
* @return 0 if found key is inside range (found key >= range->min_key).
74
int cmp_prev(QuickRange *range);
77
* Check if current row will be retrieved by this QuickRangeSelect
80
* It is assumed that currently a scan is being done on another index
81
* which reads all necessary parts of the index that is scanned by this
83
* The implementation does a binary search on sorted array of disjoint
84
* ranges, without taking size of range into account.
86
* This function is used to filter out clustered PK scan rows in
87
* index_merge quick select.
90
* @retval true if current row will be retrieved by this quick select
97
uint32_t mrr_flags; /**< Flags to be used with MRR interface */
101
QuickRangeSelect(Session *session,
105
memory::Root *parent_alloc);
114
* Get next possible record using quick-struct.
117
* QuickRangeSelect::get_next()
120
* Record is read into table->getInsertRecord()
123
* @retval 0 Found row
124
* @retval HA_ERR_END_OF_FILE No (more) rows in range
125
* @retaval # Error code
132
* Get the next record with a different prefix.
135
* QuickRangeSelect::get_next_prefix()
136
* @param[in] prefix_length length of cur_prefix
137
* @param[in] cur_prefix prefix of a key to be searched for
140
* Each subsequent call to the method retrieves the first record that has a
141
* prefix with length prefix_length different from cur_prefix, such that the
142
* record with the new prefix is within the ranges described by
143
* this->ranges. The record found is stored into the buffer pointed by
145
* The method is useful for GROUP-BY queries with range conditions to
146
* discover the prefix of the next group that satisfies the range conditions.
149
* This method is a modified copy of QuickRangeSelect::get_next(), so both
150
* methods should be unified into a more general one to reduce code
154
* @retval 0 on success
155
* @retval HA_ERR_END_OF_FILE if returned all keys
156
* @retval other if some error occurred
158
int get_next_prefix(uint32_t prefix_length,
159
key_part_map keypart_map,
160
unsigned char *cur_prefix);
162
bool reverse_sorted() const
168
* @return true if there is only one range and this uses the whole primary key
170
bool unique_key_range() const;
173
* Initialize this quick select to be a ROR-merged scan.
176
* QuickRangeSelect::init_ror_merged_scan()
177
* @param[in] reuse_handler If true, use head->cursor, otherwise create a separate Cursor object
180
* This function creates and prepares for subsequent use a separate Cursor
181
* object if it can't reuse head->cursor. The reason for this is that during
182
* ROR-merge several key scans are performed simultaneously, and a single
183
* Cursor is only capable of preserving context of a single key scan.
185
* In ROR-merge the quick select doing merge does full records retrieval,
186
* merged quick selects read only keys.
189
* @reval 0 ROR child scan initialized, ok to use.
192
int init_ror_merged_scan(bool reuse_handler);
194
void save_last_pos();
198
return QS_TYPE_RANGE;
201
void add_keys_and_lengths(std::string *key_names, std::string *used_lengths);
203
void add_info_string(std::string *str);
212
/* Used only by QuickSelectDescending */
213
QuickRangeSelect(const QuickRangeSelect& org) : QuickSelectInterface()
215
memmove(this, &org, sizeof(*this));
217
Use default MRR implementation for reverse scans. No table engine
218
currently can do an MRR scan with output in reverse index order.
220
mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
224
friend class ::drizzled::RorIntersectReadPlan;
227
QuickRangeSelect *get_quick_select_for_ref(Session *session, Table *table,
228
struct table_reference_st *ref,
231
friend bool get_quick_keys(Parameter *param,
232
QuickRangeSelect *quick,
235
unsigned char *min_key,
236
uint32_t min_key_flag,
237
unsigned char *max_key,
238
uint32_t max_key_flag);
240
friend QuickRangeSelect *get_quick_select(Parameter *,
244
uint32_t mrr_buf_size,
245
memory::Root *alloc);
246
friend class QuickSelectDescending;
248
friend class QuickIndexMergeSelect;
250
friend class QuickRorIntersectSelect;
252
friend class QuickGroupMinMaxSelect;
254
friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
256
friend range_seq_t quick_range_seq_init(void *init_param,
260
friend void select_describe(Join *join,
264
const char *message);
267
class QuickSelectDescending : public QuickRangeSelect
271
QuickSelectDescending(QuickRangeSelect *q,
272
uint32_t used_key_parts,
277
bool reverse_sorted() const
284
return QS_TYPE_RANGE_DESC;
289
bool range_reads_after_key(QuickRange *range);
293
rev_it= rev_ranges.begin();
294
return QuickRangeSelect::reset();
297
std::vector<QuickRange *> rev_ranges;
299
std::vector<QuickRange *>::iterator rev_it;
303
} /* namespace optimizer */
305
} /* namespace drizzled */