~drizzle-trunk/drizzle/development

1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
5
 *
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.
9
 *
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.
14
 *
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
18
 */
19
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
20
#pragma once
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
21
2221.7.4 by Olaf van der Spek
Refactor
22
#include <drizzled/dynamic_array.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/optimizer/range.h>
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
24
1802.16.5 by Padraig O'Sullivan
Adding help method for dynamic bitset functions. Temporary until MyBitmap is removed. Got rid of another MyBitmap usage in the range optimizer.
25
#include <boost/dynamic_bitset.hpp>
1237.13.25 by Padraig O'Sullivan
Replaced an instance of List with std::vector in the range optimizer.
26
#include <vector>
27
2252.1.21 by Olaf van der Spek
Common fwd
28
namespace drizzled {
29
namespace optimizer {
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
30
31
/**
32
 * Quick select that does a range scan on a single key. 
33
 *
34
 * The records are returned in key order.
35
 * 
36
 */
37
class QuickRangeSelect : public QuickSelectInterface
38
{
1513 by Brian Aker
This patch reverts the ROR change which created a memory leak (bzr diff -r 1294..1293)
39
protected:
40
  Cursor *cursor;
41
  DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
42
43
  /** Members to deal with case when this quick select is a ROR-merged scan */
44
  bool in_ror_merged_scan;
1802.16.5 by Padraig O'Sullivan
Adding help method for dynamic bitset functions. Temporary until MyBitmap is removed. Got rid of another MyBitmap usage in the range optimizer.
45
  boost::dynamic_bitset<> *column_bitmap;
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
46
  boost::dynamic_bitset<> *save_read_set;
47
  boost::dynamic_bitset<> *save_write_set;
1513 by Brian Aker
This patch reverts the ROR change which created a memory leak (bzr diff -r 1294..1293)
48
  bool free_file; /**< True when this->file is "owned" by this quick select */
49
50
  /* Range pointers to be used when not using MRR interface */
51
  QuickRange **cur_range; /**< current element in ranges  */
52
  QuickRange *last_range;
53
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 */
57
58
  /** Info about index we're scanning */
59
  KEY_PART *key_parts;
1534 by Brian Aker
Remove of KeyPartInfo
60
  KeyPartInfo *key_part_info;
1513 by Brian Aker
This patch reverts the ROR change which created a memory leak (bzr diff -r 1294..1293)
61
62
  bool dont_free; /**< Used by QuickSelectDescending */
63
64
  /**
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().
68
   */
69
  int cmp_next(QuickRange *range);
70
71
  /**
72
   * @return 0 if found key is inside range (found key >= range->min_key).
73
   */
74
  int cmp_prev(QuickRange *range);
75
76
  /**
77
   * Check if current row will be retrieved by this QuickRangeSelect
78
   *
79
   * NOTES
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
82
   * quick select.
83
   * The implementation does a binary search on sorted array of disjoint
84
   * ranges, without taking size of range into account.
85
   *
86
   * This function is used to filter out clustered PK scan rows in
87
   * index_merge quick select.
88
   *
89
   * RETURN
90
   * @retval true  if current row will be retrieved by this quick select
91
   * false if not
92
   */
93
  bool row_in_ranges();
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
94
95
public:
96
97
  uint32_t mrr_flags; /**< Flags to be used with MRR interface */
98
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
99
  memory::Root alloc;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
100
101
  QuickRangeSelect(Session *session,
102
                     Table *table,
103
                     uint32_t index_arg,
104
                     bool no_alloc,
1802.16.5 by Padraig O'Sullivan
Adding help method for dynamic bitset functions. Temporary until MyBitmap is removed. Got rid of another MyBitmap usage in the range optimizer.
105
                     memory::Root *parent_alloc);
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
106
107
  ~QuickRangeSelect();
108
109
  int init();
110
111
  int reset(void);
112
113
  /**
114
   * Get next possible record using quick-struct.
115
   *
116
   * SYNOPSIS
117
   * QuickRangeSelect::get_next()
118
   *
119
   * NOTES
1672.3.6 by Brian Aker
First pass in encapsulating row
120
   * Record is read into table->getInsertRecord()
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
121
   *
122
   * RETURN
123
   * @retval 0			Found row
124
   * @retval HA_ERR_END_OF_FILE	No (more) rows in range
125
   * @retaval #	Error code
126
   */
127
  int get_next();
128
129
  void range_end();
130
131
  /**
132
   * Get the next record with a different prefix.
133
   *
134
   * SYNOPSIS
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
138
   *
139
   * DESCRIPTION
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
144
   * this->record.
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.
147
   *
148
   * @todo
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
151
   * duplication.
152
   *
153
   * RETURN
154
   * @retval 0                  on success
155
   * @retval HA_ERR_END_OF_FILE if returned all keys
156
   * @retval other              if some error occurred
157
   */
158
  int get_next_prefix(uint32_t prefix_length,
159
                      key_part_map keypart_map,
160
                      unsigned char *cur_prefix);
161
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
162
  bool reverse_sorted() const
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
163
  {
164
    return false;
165
  }
166
167
  /**
168
   * @return true if there is only one range and this uses the whole primary key
169
   */
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
170
  bool unique_key_range() const;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
171
172
  /**
173
   * Initialize this quick select to be a ROR-merged scan.
174
   *
175
   * SYNOPSIS
176
   * QuickRangeSelect::init_ror_merged_scan()
177
   * @param[in] reuse_handler If true, use head->cursor, otherwise create a separate Cursor object
178
   *
179
   * NOTES
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.
184
   *
185
   * In ROR-merge the quick select doing merge does full records retrieval,
186
   * merged quick selects read only keys.
187
   *
188
   * RETURN
189
   * @reval 0  ROR child scan initialized, ok to use.
190
   * @retval 1  error
191
   */
192
  int init_ror_merged_scan(bool reuse_handler);
193
194
  void save_last_pos();
195
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
196
  int get_type() const
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
197
  {
198
    return QS_TYPE_RANGE;
199
  }
200
2170.4.4 by Stewart Smith
tmp3 String to std::string in explain_plan (and used_lengths parameter to add_keys_and_lengths
201
  void add_keys_and_lengths(std::string *key_names, std::string *used_lengths);
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
202
2170.4.5 by Stewart Smith
convert explain_plan extra String to std::string. This means that add_info_string() also gets converted to std::string
203
  void add_info_string(std::string *str);
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
204
205
  void resetCursor()
206
  {
207
    cursor= NULL;
208
  }
209
210
private:
211
212
  /* Used only by QuickSelectDescending */
213
  QuickRangeSelect(const QuickRangeSelect& org) : QuickSelectInterface()
214
  {
215
    memmove(this, &org, sizeof(*this));
216
    /*
217
      Use default MRR implementation for reverse scans. No table engine
218
      currently can do an MRR scan with output in reverse index order.
219
    */
220
    mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
221
    mrr_buf_size= 0;
222
  }
223
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
224
  friend class ::drizzled::RorIntersectReadPlan; 
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
225
226
  friend
227
  QuickRangeSelect *get_quick_select_for_ref(Session *session, Table *table,
228
                                             struct table_reference_st *ref,
229
                                             ha_rows records);
230
1237.13.7 by Padraig O'Sullivan
Renamed PARAM to Parameter and RANGE_OPT_PARAM to RangeParameter.
231
  friend bool get_quick_keys(Parameter *param, 
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
232
                             QuickRangeSelect *quick,
233
                             KEY_PART *key, 
234
                             SEL_ARG *key_tree,
235
                             unsigned char *min_key, 
236
                             uint32_t min_key_flag,
237
                             unsigned char *max_key, 
238
                             uint32_t max_key_flag);
239
1237.13.7 by Padraig O'Sullivan
Renamed PARAM to Parameter and RANGE_OPT_PARAM to RangeParameter.
240
  friend QuickRangeSelect *get_quick_select(Parameter *,
241
                                            uint32_t idx,
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
242
                                            SEL_ARG *key_tree,
243
                                            uint32_t mrr_flags,
244
                                            uint32_t mrr_buf_size,
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
245
                                            memory::Root *alloc);
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
246
  friend class QuickSelectDescending;
247
248
  friend class QuickIndexMergeSelect;
249
1237.13.12 by Padraig O'Sullivan
Corrected the name of the QUICK_ROR_INTERSECT_CLASS class to adhere to the drizzle coding standards.
250
  friend class QuickRorIntersectSelect;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
251
1237.13.21 by Padraig O'Sullivan
Corrected some style issues in the QuickGroupMinMaxSelect class.
252
  friend class QuickGroupMinMaxSelect;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
253
254
  friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
255
256
  friend range_seq_t quick_range_seq_init(void *init_param,
257
                                          uint32_t n_ranges, 
258
                                          uint32_t flags);
259
1541.1.1 by Brian Aker
JOIN -> Join rename
260
  friend void select_describe(Join *join, 
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
261
                              bool need_tmp_table, 
262
                              bool need_order,
263
                              bool distinct,
264
                              const char *message);
265
};
266
267
class QuickSelectDescending : public QuickRangeSelect
268
{
269
public:
270
271
  QuickSelectDescending(QuickRangeSelect *q, 
272
                        uint32_t used_key_parts,
273
                        bool *create_err);
274
275
  int get_next();
276
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
277
  bool reverse_sorted() const
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
278
  { 
279
    return true; 
280
  }
281
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
282
  int get_type() const
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
283
  { 
284
    return QS_TYPE_RANGE_DESC;
285
  }
286
287
private:
288
289
  bool range_reads_after_key(QuickRange *range);
290
291
  int reset(void) 
292
  { 
1237.13.25 by Padraig O'Sullivan
Replaced an instance of List with std::vector in the range optimizer.
293
    rev_it= rev_ranges.begin();
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
294
    return QuickRangeSelect::reset();
295
  }
296
1237.13.25 by Padraig O'Sullivan
Replaced an instance of List with std::vector in the range optimizer.
297
  std::vector<QuickRange *> rev_ranges;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
298
1237.13.25 by Padraig O'Sullivan
Replaced an instance of List with std::vector in the range optimizer.
299
  std::vector<QuickRange *>::iterator rev_it;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
300
301
};
302
303
} /* namespace optimizer */
304
305
} /* namespace drizzled */
306