~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 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
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
20
#ifndef DRIZZLED_ITEM_SUBSELECT_H
21
#define DRIZZLED_ITEM_SUBSELECT_H
22
1 by brian
clean slate
23
/* subselect Item */
24
25
1237.9.3 by Padraig O'Sullivan
Removed one the includes I put in server_includes.h for the last commit to get rid of the inclusion
26
#include "drizzled/comp_creator.h"
27
#include "drizzled/item/ref.h"
28
#include "drizzled/item/field.h"
29
#include "drizzled/item/bin_string.h"
1802.16.11 by Padraig O'Sullivan
Remove cpp and header file with MyBitmap class and associated functions. No longer needed.
30
#include "drizzled/util/test.h"
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
31
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
32
namespace drizzled
33
{
34
846 by Brian Aker
Removing on typedeffed class.
35
class Select_Lex;
848 by Brian Aker
typdef class removal (just... use the name of the class).
36
class Select_Lex_Unit;
1541.1.1 by Brian Aker
JOIN -> Join rename
37
class Join;
1 by brian
clean slate
38
class select_result_interceptor;
39
class subselect_engine;
40
class subselect_hash_sj_engine;
41
class Item_bool_func2;
42
class Cached_item;
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
43
class Item_in_optimizer;
44
class Item_func_not_all;
851 by Brian Aker
Class rewrite of Session (aka get all of the junk out)
45
class Tmp_Table_Param;
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
46
1 by brian
clean slate
47
48
/* base class for subselects */
49
50
class Item_subselect :public Item_result_field
51
{
275 by Brian Aker
Full removal of my_bool from central server.
52
  bool value_assigned; /* value already assigned to subselect */
1 by brian
clean slate
53
public:
54
  /* thread handler, will be assigned in fix_fields only */
520.1.22 by Brian Aker
Second pass of thd cleanup
55
  Session *session;
1 by brian
clean slate
56
  /* substitution instead of subselect in case of optimization */
57
  Item *substitution;
58
  /* unit of subquery */
848 by Brian Aker
typdef class removal (just... use the name of the class).
59
  Select_Lex_Unit *unit;
1 by brian
clean slate
60
protected:
61
  /* engine that perform execution of subselect (single select or union) */
62
  subselect_engine *engine;
63
  /* old engine if engine was changed */
64
  subselect_engine *old_engine;
65
  /* cache of used external tables */
66
  table_map used_tables_cache;
67
  /* allowed number of columns (1 for single value subqueries) */
482 by Brian Aker
Remove uint.
68
  uint32_t max_columns;
1 by brian
clean slate
69
  /* where subquery is placed */
70
  enum_parsing_place parsing_place;
71
  /* work with 'substitution' */
72
  bool have_to_be_excluded;
73
  /* cache of constant state */
74
  bool const_item_cache;
75
76
public:
77
  /* changed engine indicator */
78
  bool engine_changed;
79
  /* subquery is transformed */
80
  bool changed;
81
82
  /* TRUE <=> The underlying SELECT is correlated w.r.t some ancestor select */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
83
  bool is_correlated;
1 by brian
clean slate
84
85
  enum trans_res {RES_OK, RES_REDUCE, RES_ERROR};
86
  enum subs_type {UNKNOWN_SUBS, SINGLEROW_SUBS,
87
		  EXISTS_SUBS, IN_SUBS, ALL_SUBS, ANY_SUBS};
88
89
  Item_subselect();
90
91
  virtual subs_type substype() { return UNKNOWN_SUBS; }
92
93
  /*
94
    We need this method, because some compilers do not allow 'this'
95
    pointer in constructor initialization list, but we need to pass a pointer
96
    to subselect Item class to select_result_interceptor's constructor.
97
  */
846 by Brian Aker
Removing on typedeffed class.
98
  virtual void init (Select_Lex *select_lex,
1 by brian
clean slate
99
		     select_result_interceptor *result);
100
101
  ~Item_subselect();
102
  void cleanup();
103
  virtual void reset()
104
  {
105
    null_value= 1;
106
  }
1541.1.1 by Brian Aker
JOIN -> Join rename
107
  virtual trans_res select_transformer(Join *join);
1 by brian
clean slate
108
  bool assigned() { return value_assigned; }
109
  void assigned(bool a) { value_assigned= a; }
110
  enum Type type() const;
111
  bool is_null()
112
  {
113
    update_null_value();
114
    return null_value;
115
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
116
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
117
  virtual bool exec();
118
  virtual void fix_length_and_dec();
119
  table_map used_tables() const;
120
  table_map not_null_tables() const { return 0; }
121
  bool const_item() const;
122
  inline table_map get_used_tables_cache() { return used_tables_cache; }
123
  inline bool get_const_item_cache() { return const_item_cache; }
520.1.22 by Brian Aker
Second pass of thd cleanup
124
  Item *get_tmp_table_item(Session *session);
1 by brian
clean slate
125
  void update_used_tables();
126
  virtual void print(String *str, enum_query_type query_type);
51.1.24 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
127
  virtual bool have_guarded_conds() { return false; }
1 by brian
clean slate
128
  bool change_engine(subselect_engine *eng)
129
  {
130
    old_engine= engine;
131
    engine= eng;
132
    engine_changed= 1;
133
    return eng == 0;
134
  }
135
  /*
136
    True if this subquery has been already evaluated. Implemented only for
137
    single select and union subqueries only.
138
  */
139
  bool is_evaluated() const;
140
  bool is_uncacheable() const;
141
142
  /*
143
    Used by max/min subquery to initialize value presence registration
144
    mechanism. Engine call this method before rexecution query.
145
  */
146
  virtual void reset_value_registration() {}
147
  enum_parsing_place place() { return parsing_place; }
481 by Brian Aker
Remove all of uchar.
148
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1 by brian
clean slate
149
150
  /**
846 by Brian Aker
Removing on typedeffed class.
151
    Get the Select_Lex structure associated with this Item.
152
    @return the Select_Lex structure associated with this Item
1 by brian
clean slate
153
  */
846 by Brian Aker
Removing on typedeffed class.
154
  Select_Lex* get_select_lex();
1 by brian
clean slate
155
156
  friend class select_result_interceptor;
157
  friend class Item_in_optimizer;
520.1.21 by Brian Aker
THD -> Session rename
158
  friend bool Item_field::fix_fields(Session *, Item **);
159
  friend int  Item_field::fix_outer_field(Session *, Field **, Item **);
160
  friend bool Item_ref::fix_fields(Session *, Item **);
161
  friend void mark_select_range_as_dependent(Session*,
846 by Brian Aker
Removing on typedeffed class.
162
                                             Select_Lex*, Select_Lex*,
1 by brian
clean slate
163
                                             Field*, Item*, Item_ident*);
164
};
165
166
/* single value subselect */
167
168
class Item_cache;
169
class Item_singlerow_subselect :public Item_subselect
170
{
171
protected:
172
  Item_cache *value, **row;
173
public:
846 by Brian Aker
Removing on typedeffed class.
174
  Item_singlerow_subselect(Select_Lex *select_lex);
1 by brian
clean slate
175
  Item_singlerow_subselect() :Item_subselect(), value(0), row (0) {}
176
177
  void cleanup();
178
  subs_type substype() { return SINGLEROW_SUBS; }
179
180
  void reset();
1541.1.1 by Brian Aker
JOIN -> Join rename
181
  trans_res select_transformer(Join *join);
482 by Brian Aker
Remove uint.
182
  void store(uint32_t i, Item* item);
1 by brian
clean slate
183
  double val_real();
152 by Brian Aker
longlong replacement
184
  int64_t val_int ();
1 by brian
clean slate
185
  String *val_str (String *);
2030.1.4 by Brian Aker
Change my_decimal to Decimal
186
  type::Decimal *val_decimal(type::Decimal *);
1 by brian
clean slate
187
  bool val_bool();
188
  enum Item_result result_type() const;
189
  enum_field_types field_type() const;
190
  void fix_length_and_dec();
191
482 by Brian Aker
Remove uint.
192
  uint32_t cols();
193
  Item* element_index(uint32_t i) { return reinterpret_cast<Item*>(row[i]); }
194
  Item** addr(uint32_t i) { return (Item**)row + i; }
195
  bool check_cols(uint32_t c);
1 by brian
clean slate
196
  bool null_inside();
197
  void bring_value();
198
199
  /**
200
    This method is used to implement a special case of semantic tree
201
    rewriting, mandated by a SQL:2003 exception in the specification.
202
    The only caller of this method is handle_sql2003_note184_exception(),
203
    see the code there for more details.
204
    Note that this method breaks the object internal integrity, by
846 by Brian Aker
Removing on typedeffed class.
205
    removing it's association with the corresponding Select_Lex,
1 by brian
clean slate
206
    making this object orphan from the parse tree.
207
    No other method, beside the destructor, should be called on this
208
    object, as it is now invalid.
846 by Brian Aker
Removing on typedeffed class.
209
    @return the Select_Lex structure that was given in the constructor.
1 by brian
clean slate
210
  */
846 by Brian Aker
Removing on typedeffed class.
211
  Select_Lex* invalidate_and_restore_select_lex();
1 by brian
clean slate
212
213
  friend class select_singlerow_subselect;
214
};
215
216
/* used in static ALL/ANY optimization */
217
class select_max_min_finder_subselect;
218
class Item_maxmin_subselect :public Item_singlerow_subselect
219
{
220
protected:
221
  bool max;
222
  bool was_values;  // Set if we have found at least one row
223
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
224
  Item_maxmin_subselect(Session *session, Item_subselect *parent,
846 by Brian Aker
Removing on typedeffed class.
225
			Select_Lex *select_lex, bool max);
1 by brian
clean slate
226
  virtual void print(String *str, enum_query_type query_type);
227
  void cleanup();
228
  bool any_value() { return was_values; }
51.1.24 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
229
  void register_value() { was_values= true; }
230
  void reset_value_registration() { was_values= false; }
1 by brian
clean slate
231
};
232
233
/* exists subselect */
234
235
class Item_exists_subselect :public Item_subselect
236
{
237
protected:
238
  bool value; /* value of this item (boolean: exists/not-exists) */
239
240
public:
846 by Brian Aker
Removing on typedeffed class.
241
  Item_exists_subselect(Select_Lex *select_lex);
1 by brian
clean slate
242
  Item_exists_subselect(): Item_subselect() {}
243
244
  subs_type substype() { return EXISTS_SUBS; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
245
  void reset()
1 by brian
clean slate
246
  {
247
    value= 0;
248
  }
249
250
  enum Item_result result_type() const { return INT_RESULT;}
152 by Brian Aker
longlong replacement
251
  int64_t val_int();
1 by brian
clean slate
252
  double val_real();
253
  String *val_str(String*);
2030.1.4 by Brian Aker
Change my_decimal to Decimal
254
  type::Decimal *val_decimal(type::Decimal *);
1 by brian
clean slate
255
  bool val_bool();
256
  void fix_length_and_dec();
257
  virtual void print(String *str, enum_query_type query_type);
258
259
  friend class select_exists_subselect;
260
  friend class subselect_uniquesubquery_engine;
261
  friend class subselect_indexsubquery_engine;
262
};
263
264
265
/**
266
  Representation of IN subquery predicates of the form
267
  "left_expr IN (SELECT ...)".
268
269
  @detail
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
270
  This class has:
1 by brian
clean slate
271
   - A "subquery execution engine" (as a subclass of Item_subselect) that allows
272
     it to evaluate subqueries. (and this class participates in execution by
273
     having was_null variable where part of execution result is stored.
274
   - Transformation methods (todo: more on this).
275
276
  This class is not used directly, it is "wrapped" into Item_in_optimizer
277
  which provides some small bits of subquery evaluation.
278
*/
279
280
class Item_in_subselect :public Item_exists_subselect
281
{
282
public:
283
  Item *left_expr;
284
protected:
285
  /*
286
    Cache of the left operand of the subquery predicate. Allocated in the
287
    runtime memory root, for each execution, thus need not be freed.
288
  */
1101.1.16 by Monty Taylor
Reverted 1103
289
  List<Cached_item> *left_expr_cache;
1 by brian
clean slate
290
  bool first_execution;
291
292
  /*
293
    expr & optimizer used in subselect rewriting to store Item for
294
    all JOIN in UNION
295
  */
296
  Item *expr;
297
  Item_in_optimizer *optimizer;
298
  bool was_null;
299
  bool abort_on_null;
300
301
public:
302
  /* Used to trigger on/off conditions that were pushed down to subselect */
303
  bool *pushed_cond_guards;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
304
1 by brian
clean slate
305
  /* Priority of this predicate in the convert-to-semi-join-nest process. */
306
  int sj_convert_priority;
307
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
308
  /*
1 by brian
clean slate
309
    Location of the subquery predicate. It is either
310
     - pointer to join nest if the subquery predicate is in the ON expression
327.2.4 by Brian Aker
Refactoring table.h
311
     - (TableList*)1 if the predicate is in the WHERE.
1 by brian
clean slate
312
  */
327.2.4 by Brian Aker
Refactoring table.h
313
  TableList *expr_join_nest;
1 by brian
clean slate
314
315
  /* The method chosen to execute the IN predicate.  */
316
  enum enum_exec_method {
317
    NOT_TRANSFORMED, /* No execution method was chosen for this IN. */
318
    SEMI_JOIN,   /* IN was converted to semi-join nest and should be removed. */
319
    IN_TO_EXISTS, /* IN was converted to correlated EXISTS. */
320
    MATERIALIZATION /* IN will be executed via subquery materialization. */
321
  };
322
  enum_exec_method exec_method;
323
324
  bool *get_cond_guard(int i)
325
  {
326
    return pushed_cond_guards ? pushed_cond_guards + i : NULL;
327
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
328
  void set_cond_guard_var(int i, bool v)
329
  {
1 by brian
clean slate
330
    if ( pushed_cond_guards)
331
      pushed_cond_guards[i]= v;
332
  }
1637.5.4 by Prafulla Tekawade
Reverting some un-necessary changes.
333
  bool have_guarded_conds() { return test(pushed_cond_guards); }
1 by brian
clean slate
334
335
  Item_func_not_all *upper_item; // point on NOT/NOP before ALL/SOME subquery
336
846 by Brian Aker
Removing on typedeffed class.
337
  Item_in_subselect(Item * left_expr, Select_Lex *select_lex);
1 by brian
clean slate
338
  Item_in_subselect()
1221.1.1 by Jay Pipes
Fixes some valgrind warnings regarding conditionals depending on unintialized variables. Use initializer lists properly, dang it. :) Also, removed the new_Cached_item() function's use_result_field, as this was only used for views and was producing a valgrind warning unnecessarily.
339
    :
340
      Item_exists_subselect(),
341
      left_expr(NULL),
342
      left_expr_cache(NULL),
343
      first_execution(true),
344
      optimizer(NULL),
345
      abort_on_null(false),
346
      pushed_cond_guards(NULL),
347
      sj_convert_priority(0),
348
      expr_join_nest(NULL),
349
      exec_method(NOT_TRANSFORMED),
350
      upper_item(NULL)
1 by brian
clean slate
351
  {}
352
  void cleanup();
353
  subs_type substype() { return IN_SUBS; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
354
  void reset()
1 by brian
clean slate
355
  {
356
    value= 0;
357
    null_value= 0;
358
    was_null= 0;
359
  }
1541.1.1 by Brian Aker
JOIN -> Join rename
360
  trans_res select_transformer(Join *join);
361
  trans_res select_in_like_transformer(Join *join, const Comp_creator *func);
362
  trans_res single_value_transformer(Join *join, const Comp_creator *func);
363
  trans_res row_value_transformer(Join * join);
364
  trans_res single_value_in_to_exists_transformer(Join * join,
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
365
                                                  const Comp_creator *func);
1541.1.1 by Brian Aker
JOIN -> Join rename
366
  trans_res row_value_in_to_exists_transformer(Join * join);
1 by brian
clean slate
367
  virtual bool exec();
152 by Brian Aker
longlong replacement
368
  int64_t val_int();
1 by brian
clean slate
369
  double val_real();
370
  String *val_str(String*);
2030.1.4 by Brian Aker
Change my_decimal to Decimal
371
  type::Decimal *val_decimal(type::Decimal *);
1 by brian
clean slate
372
  void update_null_value () { (void) val_bool(); }
373
  bool val_bool();
374
  void top_level_item() { abort_on_null=1; }
375
  inline bool is_top_level_item() { return abort_on_null; }
848 by Brian Aker
typdef class removal (just... use the name of the class).
376
  bool test_limit(Select_Lex_Unit *unit);
1 by brian
clean slate
377
  virtual void print(String *str, enum_query_type query_type);
520.1.22 by Brian Aker
Second pass of thd cleanup
378
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
379
  bool setup_engine();
380
  bool init_left_expr_cache();
481 by Brian Aker
Remove all of uchar.
381
  bool is_expensive_processor(unsigned char *arg);
1 by brian
clean slate
382
383
  friend class Item_ref_null_helper;
384
  friend class Item_is_not_null_test;
385
  friend class Item_in_optimizer;
386
  friend class subselect_indexsubquery_engine;
387
  friend class subselect_hash_sj_engine;
388
};
389
390
391
/* ALL/ANY/SOME subselect */
392
class Item_allany_subselect :public Item_in_subselect
393
{
394
public:
395
  chooser_compare_func_creator func_creator;
396
  Comp_creator *func;
397
  bool all;
398
399
  Item_allany_subselect(Item * left_expr, chooser_compare_func_creator fc,
846 by Brian Aker
Removing on typedeffed class.
400
                        Select_Lex *select_lex, bool all);
1 by brian
clean slate
401
402
  // only ALL subquery has upper not
403
  subs_type substype() { return all?ALL_SUBS:ANY_SUBS; }
1541.1.1 by Brian Aker
JOIN -> Join rename
404
  trans_res select_transformer(Join *join);
1 by brian
clean slate
405
  virtual void print(String *str, enum_query_type query_type);
406
};
407
408
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
409
class subselect_engine: public memory::SqlAlloc
1 by brian
clean slate
410
{
411
protected:
412
  select_result_interceptor *result; /* results storage class */
520.1.22 by Brian Aker
Second pass of thd cleanup
413
  Session *session; /* pointer to current Session */
1 by brian
clean slate
414
  Item_subselect *item; /* item, that use this engine */
415
  enum Item_result res_type; /* type of results */
416
  enum_field_types res_field_type; /* column type of the results */
417
  bool maybe_null; /* may be null (first item in select) */
418
public:
419
420
  enum enum_engine_type {ABSTRACT_ENGINE, SINGLE_SELECT_ENGINE,
421
                         UNION_ENGINE, UNIQUESUBQUERY_ENGINE,
422
                         INDEXSUBQUERY_ENGINE, HASH_SJ_ENGINE};
423
424
  subselect_engine(Item_subselect *si, select_result_interceptor *res)
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
425
    :session(NULL)
1 by brian
clean slate
426
  {
427
    result= res;
428
    item= si;
429
    res_type= STRING_RESULT;
241 by Brian Aker
First pass of CHAR removal.
430
    res_field_type= DRIZZLE_TYPE_VARCHAR;
1 by brian
clean slate
431
    maybe_null= 0;
432
  }
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
433
  virtual ~subselect_engine() {} // to satisfy compiler
1 by brian
clean slate
434
  virtual void cleanup()= 0;
435
436
  /*
520.1.22 by Brian Aker
Second pass of thd cleanup
437
    Also sets "session" for subselect_engine::result.
1 by brian
clean slate
438
    Should be called before prepare().
439
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
440
  void set_session(Session *session_arg);
441
  Session * get_session() { return session; }
1 by brian
clean slate
442
  virtual int prepare()= 0;
443
  virtual void fix_length_and_dec(Item_cache** row)= 0;
444
  /*
445
    Execute the engine
446
447
    SYNOPSIS
448
      exec()
449
450
    DESCRIPTION
451
      Execute the engine. The result of execution is subquery value that is
452
      either captured by previously set up select_result-based 'sink' or
453
      stored somewhere by the exec() method itself.
454
455
      A required side effect: If at least one pushed-down predicate is
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
456
      disabled, subselect_engine->no_rows() must return correct result after
1 by brian
clean slate
457
      the exec() call.
458
459
    RETURN
460
      0 - OK
461
      1 - Either an execution error, or the engine was "changed", and the
462
          caller should call exec() again for the new engine.
463
  */
464
  virtual int exec()= 0;
482 by Brian Aker
Remove uint.
465
  virtual uint32_t cols()= 0; /* return number of columns in select */
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
466
  virtual bool uncacheable()= 0; /* query is uncacheable */
467
  virtual bool uncacheable(uint32_t bit_pos)= 0; /* query is uncacheable */
1 by brian
clean slate
468
  enum Item_result type() { return res_type; }
469
  enum_field_types field_type() { return res_field_type; }
470
  virtual void exclude()= 0;
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
471
  virtual bool may_be_null() { return maybe_null; }
1 by brian
clean slate
472
  virtual table_map upper_select_const_tables()= 0;
327.2.4 by Brian Aker
Refactoring table.h
473
  static table_map calc_const_tables(TableList *);
1 by brian
clean slate
474
  virtual void print(String *str, enum_query_type query_type)= 0;
475
  virtual bool change_result(Item_subselect *si,
476
                             select_result_interceptor *result)= 0;
477
  virtual bool no_tables()= 0;
51.1.24 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
478
  virtual bool is_executed() const { return false; }
1 by brian
clean slate
479
  /* Check if subquery produced any rows during last query execution */
480
  virtual bool no_rows() = 0;
481
  virtual enum_engine_type engine_type() { return ABSTRACT_ENGINE; }
482
483
protected:
484
  void set_row(List<Item> &item_list, Item_cache **row);
485
};
486
487
488
class subselect_single_select_engine: public subselect_engine
489
{
275 by Brian Aker
Full removal of my_bool from central server.
490
  bool prepared; /* simple subselect is prepared */
491
  bool optimized; /* simple subselect is optimized */
492
  bool executed; /* simple subselect is executed */
846 by Brian Aker
Removing on typedeffed class.
493
  Select_Lex *select_lex; /* corresponding select_lex */
1541.1.1 by Brian Aker
JOIN -> Join rename
494
  Join * join; /* corresponding JOIN structure */
1 by brian
clean slate
495
public:
846 by Brian Aker
Removing on typedeffed class.
496
  subselect_single_select_engine(Select_Lex *select,
1 by brian
clean slate
497
				 select_result_interceptor *result,
498
				 Item_subselect *item);
499
  void cleanup();
500
  int prepare();
501
  void fix_length_and_dec(Item_cache** row);
502
  int exec();
482 by Brian Aker
Remove uint.
503
  uint32_t cols();
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
504
  bool uncacheable();
505
  bool uncacheable(uint32_t bit_pos);
1 by brian
clean slate
506
  void exclude();
507
  table_map upper_select_const_tables();
508
  virtual void print (String *str, enum_query_type query_type);
509
  bool change_result(Item_subselect *si, select_result_interceptor *result);
510
  bool no_tables();
511
  bool may_be_null();
512
  bool is_executed() const { return executed; }
513
  bool no_rows();
514
  virtual enum_engine_type engine_type() { return SINGLE_SELECT_ENGINE; }
515
516
  friend class subselect_hash_sj_engine;
517
  friend class Item_in_subselect;
518
};
519
520
521
class subselect_union_engine: public subselect_engine
522
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
523
  Select_Lex_Unit *unit;  /* corresponding unit structure */
1 by brian
clean slate
524
public:
848 by Brian Aker
typdef class removal (just... use the name of the class).
525
  subselect_union_engine(Select_Lex_Unit *u,
1 by brian
clean slate
526
			 select_result_interceptor *result,
527
			 Item_subselect *item);
528
  void cleanup();
529
  int prepare();
530
  void fix_length_and_dec(Item_cache** row);
531
  int exec();
482 by Brian Aker
Remove uint.
532
  uint32_t cols();
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
533
  bool uncacheable();
534
  bool uncacheable(uint32_t bit_pos);
1 by brian
clean slate
535
  void exclude();
536
  table_map upper_select_const_tables();
537
  virtual void print (String *str, enum_query_type query_type);
538
  bool change_result(Item_subselect *si, select_result_interceptor *result);
539
  bool no_tables();
540
  bool is_executed() const;
541
  bool no_rows();
542
  virtual enum_engine_type engine_type() { return UNION_ENGINE; }
543
};
544
545
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
546
class JoinTable;
1 by brian
clean slate
547
548
549
/*
550
  A subquery execution engine that evaluates the subquery by doing one index
551
  lookup in a unique index.
552
553
  This engine is used to resolve subqueries in forms
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
554
555
    outer_expr IN (SELECT tbl.unique_key FROM tbl WHERE subq_where)
556
1 by brian
clean slate
557
  or, tuple-based:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
558
1 by brian
clean slate
559
    (oe1, .. oeN) IN (SELECT uniq_key_part1, ... uniq_key_partK
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
560
                      FROM tbl WHERE subqwhere)
561
1 by brian
clean slate
562
  i.e. the subquery is a single table SELECT without GROUP BY, aggregate
563
  functions, etc.
564
*/
565
566
class subselect_uniquesubquery_engine: public subselect_engine
567
{
568
protected:
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
569
  JoinTable *tab;
1 by brian
clean slate
570
  Item *cond; /* The WHERE condition of subselect */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
571
  /*
1 by brian
clean slate
572
    TRUE<=> last execution produced empty set. Valid only when left
573
    expression is NULL.
574
  */
575
  bool empty_result_set;
576
  bool null_keypart; /* TRUE <=> constructed search tuple has a NULL */
577
public:
578
1541.1.1 by Brian Aker
JOIN -> Join rename
579
  // constructor can assign Session because it will be called after Join::prepare
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
580
  subselect_uniquesubquery_engine(Session *session_arg, JoinTable *tab_arg,
1 by brian
clean slate
581
				  Item_subselect *subs, Item *where)
582
    :subselect_engine(subs, 0), tab(tab_arg), cond(where)
583
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
584
    set_session(session_arg);
1 by brian
clean slate
585
  }
586
  void cleanup();
587
  int prepare();
588
  void fix_length_and_dec(Item_cache** row);
589
  int exec();
482 by Brian Aker
Remove uint.
590
  uint32_t cols() { return 1; }
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
591
  bool uncacheable() { return true; }
592
  bool uncacheable(uint32_t) { return true; }
1 by brian
clean slate
593
  void exclude();
594
  table_map upper_select_const_tables() { return 0; }
595
  virtual void print (String *str, enum_query_type query_type);
596
  bool change_result(Item_subselect *si, select_result_interceptor *result);
597
  bool no_tables();
598
  int scan_table();
599
  bool copy_ref_key();
600
  bool no_rows() { return empty_result_set; }
601
  virtual enum_engine_type engine_type() { return UNIQUESUBQUERY_ENGINE; }
602
};
603
604
605
class subselect_indexsubquery_engine: public subselect_uniquesubquery_engine
606
{
607
  /* FALSE for 'ref', TRUE for 'ref-or-null'. */
608
  bool check_null;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
609
  /*
1 by brian
clean slate
610
    The "having" clause. This clause (further reffered to as "artificial
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
611
    having") was inserted by subquery transformation code. It contains
612
    Item(s) that have a side-effect: they record whether the subquery has
1 by brian
clean slate
613
    produced a row with NULL certain components. We need to use it for cases
614
    like
615
      (oe1, oe2) IN (SELECT t.key, t.no_key FROM t1)
616
    where we do index lookup on t.key=oe1 but need also to check if there
617
    was a row such that t.no_key IS NULL.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
618
1 by brian
clean slate
619
    NOTE: This is currently here and not in the uniquesubquery_engine. Ideally
620
    it should have been in uniquesubquery_engine in order to allow execution of
621
    subqueries like
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
622
1 by brian
clean slate
623
      (oe1, oe2) IN (SELECT primary_key, non_key_maybe_null_field FROM tbl)
624
625
    We could use uniquesubquery_engine for the first component and let
626
    Item_is_not_null_test( non_key_maybe_null_field) to handle the second.
627
628
    However, subqueries like the above are currently not handled by index
629
    lookup-based subquery engines, the engine applicability check misses
630
    them: it doesn't switch the engine for case of artificial having and
631
    [eq_]ref access (only for artifical having + ref_or_null or no having).
632
    The above example subquery is handled as a full-blown SELECT with eq_ref
633
    access to one table.
634
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
635
    Due to this limitation, the "artificial having" currently needs to be
1 by brian
clean slate
636
    checked by only in indexsubquery_engine.
637
  */
638
  Item *having;
639
public:
640
1541.1.1 by Brian Aker
JOIN -> Join rename
641
  // constructor can assign Session because it will be called after Join::prepare
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
642
  subselect_indexsubquery_engine(Session *session_arg, JoinTable *tab_arg,
1 by brian
clean slate
643
				 Item_subselect *subs, Item *where,
644
                                 Item *having_arg, bool chk_null)
520.1.22 by Brian Aker
Second pass of thd cleanup
645
    :subselect_uniquesubquery_engine(session_arg, tab_arg, subs, where),
1 by brian
clean slate
646
     check_null(chk_null),
647
     having(having_arg)
648
  {}
649
  int exec();
650
  virtual void print (String *str, enum_query_type query_type);
651
  virtual enum_engine_type engine_type() { return INDEXSUBQUERY_ENGINE; }
652
};
653
654
655
inline bool Item_subselect::is_evaluated() const
656
{
657
  return engine->is_executed();
658
}
659
660
661
inline bool Item_subselect::is_uncacheable() const
662
{
663
  return engine->uncacheable();
664
}
665
666
667
/**
668
  Compute an IN predicate via a hash semi-join. The subquery is materialized
669
  during the first evaluation of the IN predicate. The IN predicate is executed
670
  via the functionality inherited from subselect_uniquesubquery_engine.
671
*/
672
673
class subselect_hash_sj_engine: public subselect_uniquesubquery_engine
674
{
675
protected:
676
  /* TRUE if the subquery was materialized into a temp table. */
677
  bool is_materialized;
678
  /*
679
    The old engine already chosen at parse time and stored in permanent memory.
680
    Through this member we can re-create and re-prepare materialize_join for
681
    each execution of a prepared statement. We akso resuse the functionality
682
    of subselect_single_select_engine::[prepare | cols].
683
  */
684
  subselect_single_select_engine *materialize_engine;
685
  /*
686
    QEP to execute the subquery and materialize its result into a
687
    temporary table. Created during the first call to exec().
688
  */
1541.1.1 by Brian Aker
JOIN -> Join rename
689
  Join *materialize_join;
1 by brian
clean slate
690
  /* Temp table context of the outer select's JOIN. */
851 by Brian Aker
Class rewrite of Session (aka get all of the junk out)
691
  Tmp_Table_Param *tmp_param;
1 by brian
clean slate
692
693
public:
779.3.10 by Monty Taylor
Turned on -Wshadow.
694
  subselect_hash_sj_engine(Session *session_in, Item_subselect *in_predicate,
1 by brian
clean slate
695
                               subselect_single_select_engine *old_engine)
779.3.10 by Monty Taylor
Turned on -Wshadow.
696
    :subselect_uniquesubquery_engine(session_in, NULL, in_predicate, NULL),
51.1.24 by Jay Pipes
Removed DBUG symbols and fixed TRUE/FALSE
697
    is_materialized(false), materialize_engine(old_engine),
1 by brian
clean slate
698
    materialize_join(NULL), tmp_param(NULL)
699
  {}
700
  ~subselect_hash_sj_engine();
701
702
  bool init_permanent(List<Item> *tmp_columns);
703
  bool init_runtime();
704
  void cleanup();
705
  int prepare() { return 0; }
706
  int exec();
707
  virtual void print (String *str, enum_query_type query_type);
482 by Brian Aker
Remove uint.
708
  uint32_t cols()
1 by brian
clean slate
709
  {
710
    return materialize_engine->cols();
711
  }
712
  virtual enum_engine_type engine_type() { return HASH_SJ_ENGINE; }
713
};
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
714
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
715
} /* namespace drizzled */
716
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
717
#endif /* DRIZZLED_ITEM_SUBSELECT_H */