~drizzle-trunk/drizzle/development

1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
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.
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
/**
22
 * @file
23
 *
1541.1.1 by Brian Aker
JOIN -> Join rename
24
 * Defines the Join class
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
25
 */
26
2234 by Brian Aker
Mass removal of ifdef/endif in favor of pragma once.
27
#pragma once
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
28
2221.7.4 by Olaf van der Spek
Refactor
29
#include <drizzled/dynamic_array.h>
1108.6.29 by Padraig O'Sullivan
Added an optimizer namespace and sub-directory within drizzled. Moved the
30
#include <drizzled/optimizer/position.h>
2154.2.7 by Brian Aker
class Join constructor moved out.
31
#include <drizzled/sql_select.h>
2154.2.17 by Brian Aker
Additional removal of session
32
#include <drizzled/tmp_table_param.h>
1100.4.2 by Padraig O'Sullivan
Removed the typedef for nested_join_map and instead just declare these
33
#include <bitset>
34
2252.1.14 by Olaf van der Spek
Common fwd
35
namespace drizzled {
2154.2.7 by Brian Aker
class Join constructor moved out.
36
1541.1.1 by Brian Aker
JOIN -> Join rename
37
class Join :public memory::SqlAlloc
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
38
{
1541.1.1 by Brian Aker
JOIN -> Join rename
39
  Join(const Join &rhs);                        /**< not implemented */
40
  Join& operator=(const Join &rhs);             /**< not implemented */
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
41
42
  /**
1108.6.8 by Padraig O'Sullivan
Made the positions member of the JOIN class private (finally got that
43
   * Contains a partial query execution plan which is extended during
44
   * cost-based optimization.
45
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
46
  optimizer::Position positions[MAX_TABLES+1];
1108.6.8 by Padraig O'Sullivan
Made the positions member of the JOIN class private (finally got that
47
48
  /**
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
49
   * Contains the optimal query execution plan after cost-based optimization
50
   * has taken place. 
51
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
52
  optimizer::Position best_positions[MAX_TABLES+1];
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
53
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
54
public:
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
55
  JoinTable *join_tab;
56
  JoinTable **best_ref;
57
  JoinTable **map2table;    /**< mapping between table indexes and JoinTables */
58
  JoinTable *join_tab_save; /**< saved join_tab for subquery reexecution */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
59
60
  Table **table;
61
  Table **all_tables;
62
  /**
63
    The table which has an index that allows to produce the requried ordering.
64
    A special value of 0x1 means that the ordering will be produced by
65
    passing 1st non-const table to filesort(). NULL means no such table exists.
66
  */
67
  Table *sort_by_table;
68
69
  uint32_t tables;        /**< Number of tables in the join */
70
  uint32_t outer_tables;  /**< Number of tables that are not inside semijoin */
71
  uint32_t const_tables;
72
  uint32_t send_group_parts;
73
74
  bool sort_and_group;
75
  bool first_record;
76
  bool full_join;
77
  bool group;
78
  bool no_field_update;
79
  bool do_send_rows;
80
  /**
81
    true when we want to resume nested loop iterations when
82
    fetching data from a cursor
83
  */
84
  bool resume_nested_loop;
85
  /**
86
    true <=> optimizer must not mark any table as a constant table.
87
    This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
88
    when we optimize the select that reads the results of the union from a
89
    temporary table, we must not mark the temp. table as constant because
90
    the number of rows in it may vary from one subquery execution to another.
91
  */
92
  bool no_const_tables;
93
  bool select_distinct;				/**< Set if SELECT DISTINCT */
94
  /**
95
    If we have the GROUP BY statement in the query,
96
    but the group_list was emptied by optimizer, this
97
    flag is true.
98
    It happens when fields in the GROUP BY are from
99
    constant table
100
  */
101
  bool group_optimized_away;
102
103
  /*
104
    simple_xxxxx is set if order_st/GROUP BY doesn't include any references
1541.1.1 by Brian Aker
JOIN -> Join rename
105
    to other tables than the first non-constant table in the Join.
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
106
    It's also set if order_st/GROUP BY is empty.
107
  */
108
  bool simple_order;
109
  bool simple_group;
110
  /**
111
    Is set only in case if we have a GROUP BY clause
1273.2.15 by Stewart Smith
fix accidental mangling of comment: s/order_st BY/ORDER BY/. in drizzled/join.h
112
    and no ORDER BY after constant elimination of 'order'.
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
113
  */
114
  bool no_order;
1273.2.15 by Stewart Smith
fix accidental mangling of comment: s/order_st BY/ORDER BY/. in drizzled/join.h
115
  /** Is set if we have a GROUP BY and we have ORDER BY on a constant. */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
116
  bool skip_sort_order;
117
  bool union_part; /**< this subselect is part of union */
118
  bool optimized; /**< flag to avoid double optimization in EXPLAIN */
119
  bool need_tmp;
120
  bool hidden_group_fields;
121
122
  table_map const_table_map;
123
  table_map found_const_table_map;
124
  table_map outer_join;
125
126
  ha_rows send_records;
127
  ha_rows found_records;
128
  ha_rows examined_rows;
129
  ha_rows row_limit;
130
  ha_rows select_limit;
131
  /**
132
    Used to fetch no more than given amount of rows per one
133
    fetch operation of server side cursor.
134
    The value is checked in end_send and end_send_group in fashion, similar
135
    to offset_limit_cnt:
136
      - fetch_limit= HA_POS_ERROR if there is no cursor.
137
      - when we open a cursor, we set fetch_limit to 0,
138
      - on each fetch iteration we add num_rows to fetch to fetch_limit
139
  */
140
  ha_rows fetch_limit;
141
142
  Session	*session;
143
  List<Item> *fields;
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
144
  List<Item> &fields_list; /**< hold field list passed to select_query */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
145
  List<TableList> *join_list; /**< list of joined tables in reverse order */
146
  /** unit structure (with global parameters) for this select */
147
  Select_Lex_Unit *unit;
148
  /** select that processed */
149
  Select_Lex *select_lex;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
150
  optimizer::SqlSelect *select; /**< created in optimization phase */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
151
152
  /**
153
    Bitmap of nested joins embedding the position at the end of the current
154
    partial join (valid only during join optimizer run).
155
  */
1100.4.2 by Padraig O'Sullivan
Removed the typedef for nested_join_map and instead just declare these
156
  std::bitset<64> cur_embedding_map;
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
157
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
158
  /**
159
   * The cost for the final query execution plan chosen after optimization
160
   * has completed. The QEP is stored in the best_positions variable.
161
   */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
162
  double best_read;
1101.1.16 by Monty Taylor
Reverted 1103
163
  List<Cached_item> group_fields;
164
  List<Cached_item> group_fields_cache;
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
165
  Table *tmp_table;
166
  /** used to store 2 possible tmp table of SELECT */
167
  Table *exec_tmp_table1;
168
  Table *exec_tmp_table2;
169
  Item_sum **sum_funcs;
170
  Item_sum ***sum_funcs_end;
171
  /** second copy of sumfuncs (for queries with 2 temporary tables */
172
  Item_sum **sum_funcs2;
173
  Item_sum ***sum_funcs_end2;
174
  Item *having;
175
  Item *tmp_having; /**< To store having when processed temporary table */
176
  Item *having_history; /**< Store having for explain */
177
  uint64_t select_options;
178
  select_result *result;
179
  Tmp_Table_Param tmp_table_param;
1711.6.1 by Brian Aker
Style on structure cleanup
180
  DrizzleLock *lock;
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
181
1541.1.1 by Brian Aker
JOIN -> Join rename
182
  Join *tmp_join; /**< copy of this Join to be used with temporary tables */
2082.3.1 by vjsamuel1990 at gmail
Merge conversion of ROLLUP from struct to class.
183
  Rollup rollup;				/**< Used with rollup */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
184
  DYNAMIC_ARRAY keyuse;
185
  Item::cond_result cond_value;
186
  Item::cond_result having_value;
187
  List<Item> all_fields; /**< to store all fields that used in query */
188
  /** Above list changed to use temporary table */
189
  List<Item> tmp_all_fields1;
190
  List<Item> tmp_all_fields2;
191
  List<Item> tmp_all_fields3;
192
  /** Part, shared with list above, emulate following list */
193
  List<Item> tmp_fields_list1;
194
  List<Item> tmp_fields_list2;
195
  List<Item> tmp_fields_list3;
196
  int error;
197
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
198
  Order *order;
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
199
  Order *group_list; /**< hold parameters of select_query */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
200
  COND *conds;                            // ---"---
201
  Item *conds_history; /**< store WHERE for explain */
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
202
  TableList *tables_list; /**< hold 'tables' parameter of select_query */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
203
  COND_EQUAL *cond_equal;
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
204
  JoinTable *return_tab; /**< used only for outer joins */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
205
  Item **ref_pointer_array; /**< used pointer reference for this select */
206
  /** Copy of above to be used with different lists */
207
  Item **items0;
208
  Item **items1;
209
  Item **items2;
210
  Item **items3;
211
  Item **current_ref_pointer_array;
212
  uint32_t ref_pointer_array_size; ///< size of above in bytes
213
  const char *zero_result_cause; ///< not 0 if exec must return zero result
214
215
  /*
216
    storage for caching buffers allocated during query execution.
217
    These buffers allocations need to be cached as the thread memory pool is
218
    cleared only at the end of the execution of the whole query and not caching
219
    allocations that occur in repetition at execution time will result in
220
    excessive memory usage.
221
  */
1711.6.1 by Brian Aker
Style on structure cleanup
222
  SortField *sortorder;                        // make_unireg_sortorder()
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
223
  Table **table_reexec;                         // make_simple_join()
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
224
  JoinTable *join_tab_reexec;                    // make_simple_join()
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
225
  /* end of allocation caching storage */
226
227
  /** Constructors */
1541.1.1 by Brian Aker
JOIN -> Join rename
228
  Join(Session *session_arg, 
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
229
       List<Item> &fields_arg, 
230
       uint64_t select_options_arg,
2154.2.7 by Brian Aker
class Join constructor moved out.
231
       select_result *result_arg);
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
232
233
  /** 
234
   * This method is currently only used when a subselect EXPLAIN is performed.
235
   * I pulled out the init() method and have simply reset the values to what
236
   * was previously in the init() method.  See the note about the hack in 
237
   * sql_union.cc...
238
   */
2154.2.7 by Brian Aker
class Join constructor moved out.
239
  void reset(Session *session_arg, 
240
             List<Item> &fields_arg, 
241
             uint64_t select_options_arg,
242
             select_result *result_arg);
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
243
244
  int prepare(Item ***rref_pointer_array, 
245
              TableList *tables,
246
              uint32_t wind_num,
247
              COND *conds,
248
              uint32_t og_num,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
249
              Order *order,
250
              Order *group,
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
251
              Item *having,
252
              Select_Lex *select,
253
              Select_Lex_Unit *unit);
2154.2.7 by Brian Aker
class Join constructor moved out.
254
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
255
  int optimize();
256
  int reinit();
257
  void exec();
258
  int destroy();
259
  void restore_tmp();
260
  bool alloc_func_list();
261
  bool setup_subquery_materialization();
262
  bool make_sum_func_list(List<Item> &all_fields, 
263
                          List<Item> &send_fields,
264
                  			  bool before_group_by,
265
                          bool recompute= false);
266
267
  inline void set_items_ref_array(Item **ptr)
268
  {
269
    memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
270
    current_ref_pointer_array= ptr;
271
  }
272
  inline void init_items_ref_array()
273
  {
2183.2.17 by Olaf van der Spek
Use List::size()
274
    items0= ref_pointer_array + all_fields.size();
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
275
    memcpy(items0, ref_pointer_array, ref_pointer_array_size);
276
    current_ref_pointer_array= items0;
277
  }
278
279
  bool rollup_init();
280
  bool rollup_make_fields(List<Item> &all_fields, 
281
                          List<Item> &fields,
282
                  			  Item_sum ***func);
283
  int rollup_send_data(uint32_t idx);
284
  int rollup_write_data(uint32_t idx, Table *table);
285
  void remove_subq_pushed_predicates(Item **where);
286
  /**
287
    Release memory and, if possible, the open tables held by this execution
288
    plan (and nested plans). It's used to release some tables before
289
    the end of execution in order to increase concurrency and reduce
290
    memory consumption.
291
  */
292
  void join_free();
1541.1.1 by Brian Aker
JOIN -> Join rename
293
  /** Cleanup this Join, possibly for reuse */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
294
  void cleanup(bool full);
295
  void clear();
296
  bool save_join_tab();
297
  bool init_save_join_tab();
298
  bool send_row_on_empty_set()
299
  {
300
    return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
301
	    !group_list);
302
  }
303
  bool change_result(select_result *result);
2154.2.7 by Brian Aker
class Join constructor moved out.
304
  bool is_top_level_join() const;
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
305
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
306
  /**
307
   * Copy the partial query plan into the optimal query plan.
308
   *
309
   * @param[in] size the size of the plan which is to be copied
310
   */
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
311
  void copyPartialPlanIntoOptimalPlan(uint32_t size)
312
  {
313
    memcpy(best_positions, positions, 
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
314
           sizeof(optimizer::Position) * size);
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
315
  }
316
1240.8.2 by Dennis Schoen
add cache_const_exprs() to JOIN
317
  void cache_const_exprs();
318
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
319
  /**
320
   * @param[in] index the index of the position to retrieve
321
   * @return a reference to the specified position in the optimal
322
   *         query plan
323
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
324
  optimizer::Position &getPosFromOptimalPlan(uint32_t index)
1108.6.4 by Padraig O'Sullivan
Made the best_positions member of the JOIN class private and added any
325
  {
326
    return best_positions[index];
327
  }
328
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
329
  /**
330
   * @param[in] index the index of the position to retrieve
331
   * @return a reference to the specified position in the partial
332
   *         query plan
333
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
334
  optimizer::Position &getPosFromPartialPlan(uint32_t index)
1108.6.6 by Padraig O'Sullivan
Adding accessors to the JOIN class for the positions member. The end goal is
335
  {
336
    return positions[index];
337
  }
338
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
339
  /**
340
   * @param[in] index the index of the position to set
341
   * @param[in] in_pos the value to set the position to
342
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
343
  void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
1108.6.6 by Padraig O'Sullivan
Adding accessors to the JOIN class for the positions member. The end goal is
344
  {
1108.6.9 by Padraig O'Sullivan
Cleaned up the method for setting an element in the positions array.
345
    positions[index]= in_pos;
1108.6.6 by Padraig O'Sullivan
Adding accessors to the JOIN class for the positions member. The end goal is
346
  }
347
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
348
  /**
349
   * @return a pointer to the first position in the partial query plan
350
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
351
  optimizer::Position *getFirstPosInPartialPlan()
1108.6.8 by Padraig O'Sullivan
Made the positions member of the JOIN class private (finally got that
352
  {
353
    return positions;
354
  }
355
1108.6.11 by Padraig O'Sullivan
Added some doxygen comments to the new methods added to the JOIN class.
356
  /**
357
   * @param[in] index the index of the operator to retrieve from the partial
358
   *                  query plan
359
   * @return a pointer to the position in the partial query plan
360
   */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
361
  optimizer::Position *getSpecificPosInPartialPlan(int32_t index)
1108.6.8 by Padraig O'Sullivan
Made the positions member of the JOIN class private (finally got that
362
  {
363
    return positions + index;
364
  }
365
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
366
};
367
1541.1.1 by Brian Aker
JOIN -> Join rename
368
enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error);
369
enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab);
370
enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last);
371
enum_nested_loop_state end_send(Join *join, JoinTable *join_tab, bool end_of_records);
372
enum_nested_loop_state end_write(Join *join, JoinTable *join_tab, bool end_of_records);
373
enum_nested_loop_state end_update(Join *join, JoinTable *join_tab, bool end_of_records);
374
enum_nested_loop_state end_unique_update(Join *join, JoinTable *join_tab, bool end_of_records);
1039.2.2 by Jay Pipes
Phase 2 of JOIN refactoring.
375
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
376
} /* namespace drizzled */
377