~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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.
390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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
 */
1 by brian
clean slate
19
20
/* classes to use when handling where clause */
21
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
22
#ifndef DRIZZLED_OPTIMIZER_RANGE_H
23
#define DRIZZLED_OPTIMIZER_RANGE_H
24
25
#include "drizzled/field.h"
26
#include "drizzled/item/sum.h"
27
957.1.2 by Padraig O'Sullivan
Whoops, for to have the proper include directive for std::priority_queue
28
#include <queue>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
29
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
namespace drizzled
31
{
32
1541.1.1 by Brian Aker
JOIN -> Join rename
33
class Join;
1237.13.27 by Padraig O'Sullivan
Correcting the case of a number of classes in the optimizer to adhere to the coding standards. Also
34
class RorIntersectReadPlan; 
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
35
typedef class Item COND;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
36
37
namespace internal
38
{
1241.9.48 by Monty Taylor
Made one of the drizzled instances of IO_CACHE a pointer.
39
typedef struct st_io_cache IO_CACHE;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
40
}
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
41
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
42
typedef struct st_key_part
43
{
44
  uint16_t key;
45
  uint16_t part;
1534 by Brian Aker
Remove of KeyPartInfo
46
  /* See KeyPartInfo for meaning of the next two: */
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
47
  uint16_t store_length;
48
  uint16_t length;
49
  uint8_t null_bit;
50
  /**
1 by brian
clean slate
51
    Keypart flags (0 when this structure is used by partition pruning code
52
    for fake partitioning index description)
53
  */
206 by Brian Aker
Removed final uint dead types.
54
  uint8_t flag;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
55
  Field *field;
1 by brian
clean slate
56
} KEY_PART;
57
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
58
59
namespace optimizer
60
{
61
1237.13.7 by Padraig O'Sullivan
Renamed PARAM to Parameter and RANGE_OPT_PARAM to RangeParameter.
62
class Parameter;
1237.13.6 by Padraig O'Sullivan
Moved the SEL_ARG class into its own header and implementation files. Cleaned up a bunch of style
63
class SEL_ARG;
64
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
65
/**
1 by brian
clean slate
66
  Quick select interface.
160.1.2 by mark
remove FTPARSER and last remains of full text search
67
  This class is a parent for all QUICK_*_SELECT classes.
1 by brian
clean slate
68
69
  The usage scenario is as follows:
70
  1. Create quick select
71
    quick= new QUICK_XXX_SELECT(...);
72
73
  2. Perform lightweight initialization. This can be done in 2 ways:
74
  2.a: Regular initialization
75
    if (quick->init())
76
    {
77
      //the only valid action after failed init() call is delete
78
      delete quick;
79
    }
80
  2.b: Special initialization for quick selects merged by QUICK_ROR_*_SELECT
81
    if (quick->init_ror_merged_scan())
82
      delete quick;
83
84
  3. Perform zero, one, or more scans.
85
    while (...)
86
    {
87
      // initialize quick select for scan. This may allocate
88
      // buffers and/or prefetch rows.
89
      if (quick->reset())
90
      {
91
        //the only valid action after failed reset() call is delete
92
        delete quick;
93
        //abort query
94
      }
95
96
      // perform the scan
97
      do
98
      {
99
        res= quick->get_next();
100
      } while (res && ...)
101
    }
102
103
  4. Delete the select:
104
    delete quick;
105
106
*/
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
107
class QuickSelectInterface
1 by brian
clean slate
108
{
109
public:
110
  bool sorted;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
111
  ha_rows records; /**< estimate of # of records to be retrieved */
112
  double read_time; /**< time to perform this retrieval */
113
  Table *head;
114
  /**
1 by brian
clean slate
115
    Index this quick select uses, or MAX_KEY for quick selects
116
    that use several indexes
117
  */
482 by Brian Aker
Remove uint.
118
  uint32_t index;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
119
  /**
1 by brian
clean slate
120
    Total length of first used_key_parts parts of the key.
121
    Applicable if index!= MAX_KEY.
122
  */
482 by Brian Aker
Remove uint.
123
  uint32_t max_used_key_length;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
124
  /**
125
    Maximum number of (first) key parts this quick select uses for retrieval.
1 by brian
clean slate
126
    eg. for "(key1p1=c1 AND key1p2=c2) OR key1p1=c2" used_key_parts == 2.
127
    Applicable if index!= MAX_KEY.
128
129
    For QUICK_GROUP_MIN_MAX_SELECT it includes MIN/MAX argument keyparts.
130
  */
482 by Brian Aker
Remove uint.
131
  uint32_t used_key_parts;
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
132
  /**
133
   * The rowid of last row retrieved by this quick select. This is used only when
134
   * doing ROR-index_merge selects
135
   */
136
  unsigned char *last_rowid;
137
138
  /**
139
   * Table record buffer used by this quick select.
140
   */
141
  unsigned char *record;
1 by brian
clean slate
142
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
143
  QuickSelectInterface();
144
  virtual ~QuickSelectInterface(){};
1 by brian
clean slate
145
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
146
  /**
147
   * Do post-constructor initialization.
148
   *
149
   * @details
150
   *
151
   * Performs initializations that should have been in constructor if
152
   * it was possible to return errors from constructors. The join optimizer may
153
   * create and then delete quick selects without retrieving any rows so init()
154
   * must not contain any IO or CPU intensive code.
155
   *
156
   * If init() call fails the only valid action is to delete this quick select,
157
   * reset() and get_next() must not be called.
158
   *
159
   * @retval
160
   *  0      OK
161
   * @retval
162
   *  other  Error code
163
  */
164
  virtual int init() = 0;
165
166
  /**
167
   * Initializes quick select for row retrieval.
168
   *
169
   * @details
170
   *
171
   * Should be called when it is certain that row retrieval will be
172
   * necessary. This call may do heavyweight initialization like buffering first
173
   * N records etc. If reset() call fails get_next() must not be called.
174
   * Note that reset() may be called several times if
175
   * - the quick select is executed in a subselect
176
   * - a JOIN buffer is used
177
   *
178
   * @retval 
179
   *  0      OK
180
   * @retval
181
   *  other  Error code
182
   */
183
  virtual int reset(void) = 0;
184
  /** Gets next record to retrieve */
185
  virtual int get_next() = 0;
186
187
  /** Range end should be called when we have looped over the whole index */
1 by brian
clean slate
188
  virtual void range_end() {}
189
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
190
  virtual bool reverse_sorted() const = 0;
191
192
  virtual bool unique_key_range() const
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
193
  {
194
    return false;
195
  }
1 by brian
clean slate
196
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
197
  enum 
198
  {
199
    QS_TYPE_RANGE= 0,
200
    QS_TYPE_INDEX_MERGE= 1,
201
    QS_TYPE_RANGE_DESC= 2,
202
    QS_TYPE_ROR_INTERSECT= 4,
203
    QS_TYPE_ROR_UNION= 5,
204
    QS_TYPE_GROUP_MIN_MAX= 6
1 by brian
clean slate
205
  };
206
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
207
  /** Returns the type of this quick select - one of the QS_TYPE_* values */
1237.13.11 by Padraig O'Sullivan
Split the QUICK_ROR_INTERSECT_SELECT class out into its own header and implementation files.
208
  virtual int get_type() const = 0;
1 by brian
clean slate
209
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
210
  /**
211
   * Initialize this quick select as a merged scan inside a ROR-union or a ROR-
212
   * intersection scan. The caller must not additionally call init() if this
213
   * function is called.
214
   *
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
215
   * @param If true, the quick select may use table->Cursor,
216
   *        otherwise it must create and use a separate Cursor
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
217
   *        object.
218
   *
219
   * @retval
220
   *  0     Ok
221
   * @retval
222
   *  other Error
223
   */
520.9.3 by mordred
zomg. Solaris actually builds all the way!!!
224
  virtual int init_ror_merged_scan(bool)
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
225
  {
226
    assert(0);
227
    return 1;
228
  }
1 by brian
clean slate
229
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
230
  /**
231
   * Save ROWID of last retrieved row in file->ref. This used in ROR-merging.
232
   */
1 by brian
clean slate
233
  virtual void save_last_pos(){};
234
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
235
  /**
236
   * Append comma-separated list of keys this quick select uses to key_names;
237
   * append comma-separated list of corresponding used lengths to used_lengths.
238
   * 
1240.7.1 by Padraig O'Sullivan
Created an ExplainPlan class in the optimizer namespace. All printing of an explain in drizzle goes
239
   * @note This is used by during explain plan.
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
240
   */
241
  virtual void add_keys_and_lengths(String *key_names, String *used_lengths)=0;
1 by brian
clean slate
242
1055.2.21 by Jay Pipes
Documentation and indentation/style cleanups in sql_select, opt_range.h and join_tab.h. This patch doxygenates classes/structs and their member variables.
243
  /**
244
   * Append text representation of quick select structure (what and how is
245
   * merged) to str. The result is added to "Extra" field in EXPLAIN output.
246
   *
247
   * @note
248
   *
249
   * This function is implemented only by quick selects that merge other quick
250
   * selects output and/or can produce output suitable for merging.
251
   */
252
  virtual void add_info_string(String *) 
253
  {}
254
  
255
  /**
256
   * Returns true if any index used by this quick select
257
   * uses field which is marked in passed bitmap.
258
   */
1802.16.6 by Padraig O'Sullivan
Added temporary conversion of a bitmap to dynamic_bitset in order to remove references to MyBitmap within optimizer code.
259
  virtual bool is_keys_used(const boost::dynamic_bitset<>& fields);
1 by brian
clean slate
260
};
261
262
struct st_qsel_param;
1237.13.5 by Padraig O'Sullivan
Split some classes from the range optimizer out in to their own header and implementation files.
263
class QuickRange;
264
class QuickRangeSelect;
1 by brian
clean slate
265
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
266
/**
1237.13.2 by Padraig O'Sullivan
Modified the names of 2 classes in the optimizer to adhere to the coding standards.
267
 * MRR range sequence, array<QuickRange> implementation: sequence traversal
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
268
 * context.
269
 */
1 by brian
clean slate
270
typedef struct st_quick_range_seq_ctx
271
{
1237.13.2 by Padraig O'Sullivan
Modified the names of 2 classes in the optimizer to adhere to the coding standards.
272
  QuickRange **first;
273
  QuickRange **cur;
274
  QuickRange **last;
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
275
} QuickRangeSequenceContext;
1 by brian
clean slate
276
482 by Brian Aker
Remove uint.
277
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
278
482 by Brian Aker
Remove uint.
279
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
1 by brian
clean slate
280
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
281
/**
282
 * Executor class for SELECT statements.
283
 *
284
 * @details
285
 *
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
286
 * The QuickSelectInterface member variable is the implementor
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
287
 * of the SELECT execution.
288
 */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
289
class SqlSelect : public memory::SqlAlloc 
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
290
{
1 by brian
clean slate
291
 public:
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
292
  QuickSelectInterface *quick; /**< If quick-select used */
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
293
  COND *cond; /**< where condition */
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
294
  Table	*head;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
295
  internal::IO_CACHE *file; /**< Positions to used records */
1055.2.22 by Jay Pipes
Documents and doxygenates all the myriad classes in opt_range.h (all the QUIK_SELECT class interfaces
296
  ha_rows records; /**< Records in use if read from file */
297
  double read_time; /**< Time to read rows */
298
  key_map quick_keys; /**< Possible quick keys */
299
  key_map needed_reg; /**< Possible quick keys after prev tables. */
300
  table_map const_tables;
301
  table_map read_tables;
302
  bool free_cond;
1 by brian
clean slate
303
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
304
  SqlSelect();
305
  ~SqlSelect();
1 by brian
clean slate
306
  void cleanup();
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
307
  bool check_quick(Session *session, bool force_quick_range, ha_rows limit);
308
  bool skip_record();
520.1.22 by Brian Aker
Second pass of thd cleanup
309
  int test_quick_select(Session *session, key_map keys, table_map prev_tables,
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
310
                        ha_rows limit, bool force_quick_range,
1 by brian
clean slate
311
                        bool ordered_output);
312
};
313
1237.13.2 by Padraig O'Sullivan
Modified the names of 2 classes in the optimizer to adhere to the coding standards.
314
QuickRangeSelect *get_quick_select_for_ref(Session *session, 
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
315
                                             Table *table,
1089.1.14 by Brian Aker
Fix TABLE_REF structure
316
                                             struct table_reference_st *ref,
1 by brian
clean slate
317
                                             ha_rows records);
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
318
319
/*
1237.13.2 by Padraig O'Sullivan
Modified the names of 2 classes in the optimizer to adhere to the coding standards.
320
  Create a QuickRangeSelect from given key and SEL_ARG tree for that key.
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
321
322
  SYNOPSIS
323
    get_quick_select()
324
      param
325
      idx            Index of used key in param->key.
326
      key_tree       SEL_ARG tree for the used key
327
      mrr_flags      MRR parameter for quick select
328
      mrr_buf_size   MRR parameter for quick select
329
      parent_alloc   If not NULL, use it to allocate memory for
330
                     quick select data. Otherwise use quick->alloc.
331
  NOTES
332
    The caller must call QUICK_SELECT::init for returned quick select.
333
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
334
    CAUTION! This function may change session->mem_root to a memory::Root which will be
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
335
    deallocated when the returned quick select is deleted.
336
337
  RETURN
338
    NULL on error
339
    otherwise created quick select
340
*/
1237.13.7 by Padraig O'Sullivan
Renamed PARAM to Parameter and RANGE_OPT_PARAM to RangeParameter.
341
QuickRangeSelect *get_quick_select(Parameter *param,
1237.13.6 by Padraig O'Sullivan
Moved the SEL_ARG class into its own header and implementation files. Cleaned up a bunch of style
342
                                   uint32_t index,
343
                                   SEL_ARG *key_tree, 
344
                                   uint32_t mrr_flags,
345
                                   uint32_t mrr_buf_size, 
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
346
                                   memory::Root *alloc);
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
347
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
348
uint32_t get_index_for_order(Table *table, Order *order, ha_rows limit);
1 by brian
clean slate
349
1237.13.3 by Padraig O'Sullivan
Performed numerous style cleanups in range.[cc,h].
350
SqlSelect *make_select(Table *head, 
1237.13.12 by Padraig O'Sullivan
Corrected the name of the QUICK_ROR_INTERSECT_CLASS class to adhere to the drizzle coding standards.
351
                       table_map const_tables,
352
                       table_map read_tables, 
353
                       COND *conds,
354
                       bool allow_null_cond,
355
                       int *error);
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
356
1237.13.7 by Padraig O'Sullivan
Renamed PARAM to Parameter and RANGE_OPT_PARAM to RangeParameter.
357
bool get_quick_keys(Parameter *param, 
1237.13.6 by Padraig O'Sullivan
Moved the SEL_ARG class into its own header and implementation files. Cleaned up a bunch of style
358
                    QuickRangeSelect *quick,
359
                    KEY_PART *key,
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
360
                    SEL_ARG *key_tree, 
361
                    unsigned char *min_key,
362
                    uint32_t min_key_flag,
363
                    unsigned char *max_key,
364
                    uint32_t max_key_flag);
365
366
} /* namespace optimizer */
367
368
} /* namespace drizzled */
369
370
#endif /* DRIZZLED_OPTIMIZER_RANGE_H */