Drizzled Public API Documentation

join.h
Go to the documentation of this file.
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00027 #pragma once
00028 
00029 #include <drizzled/dynamic_array.h>
00030 #include <drizzled/optimizer/position.h>
00031 #include <drizzled/sql_select.h>
00032 #include <drizzled/tmp_table_param.h>
00033 #include <bitset>
00034 
00035 namespace drizzled {
00036 
00037 class Join : public memory::SqlAlloc, boost::noncopyable
00038 {
00043   optimizer::Position positions[MAX_TABLES+1];
00044 
00049   optimizer::Position best_positions[MAX_TABLES+1];
00050 
00051 public:
00052   JoinTable *join_tab;
00053   JoinTable **best_ref;
00054   JoinTable **map2table;    
00055   JoinTable *join_tab_save; 
00057   Table **table;
00058   Table **all_tables;
00064   Table *sort_by_table;
00065 
00066   uint32_t tables;        
00067   uint32_t outer_tables;  
00068   uint32_t const_tables;
00069   uint32_t send_group_parts;
00070 
00071   bool sort_and_group;
00072   bool first_record;
00073   bool full_join;
00074   bool group;
00075   bool no_field_update;
00076   bool do_send_rows;
00081   bool resume_nested_loop;
00089   bool no_const_tables;
00090   bool select_distinct;       
00098   bool group_optimized_away;
00099 
00100   /*
00101     simple_xxxxx is set if order_st/GROUP BY doesn't include any references
00102     to other tables than the first non-constant table in the Join.
00103     It's also set if order_st/GROUP BY is empty.
00104   */
00105   bool simple_order;
00106   bool simple_group;
00111   bool no_order;
00113   bool skip_sort_order;
00114   bool union_part; 
00115   bool optimized; 
00116   bool need_tmp;
00117   bool hidden_group_fields;
00118 
00119   table_map const_table_map;
00120   table_map found_const_table_map;
00121   table_map outer_join;
00122 
00123   ha_rows send_records;
00124   ha_rows found_records;
00125   ha_rows examined_rows;
00126   ha_rows row_limit;
00127   ha_rows select_limit;
00137   ha_rows fetch_limit;
00138 
00139   Session *session;
00140   List<Item> *fields;
00141   List<Item> &fields_list; 
00142   List<TableList> *join_list; 
00144   Select_Lex_Unit *unit;
00146   Select_Lex *select_lex;
00147   optimizer::SqlSelect *select; 
00153   std::bitset<64> cur_embedding_map;
00154 
00159   double best_read;
00160   List<Cached_item> group_fields;
00161   List<Cached_item> group_fields_cache;
00162   Table *tmp_table;
00164   Table *exec_tmp_table1;
00165   Table *exec_tmp_table2;
00166   Item_sum **sum_funcs;
00167   Item_sum ***sum_funcs_end;
00169   Item_sum **sum_funcs2;
00170   Item_sum ***sum_funcs_end2;
00171   Item *having;
00172   Item *tmp_having; 
00173   Item *having_history; 
00174   uint64_t select_options;
00175   select_result *result;
00176   Tmp_Table_Param tmp_table_param;
00177   DrizzleLock *lock;
00178 
00179   Join *tmp_join; 
00180   Rollup rollup;        
00181   DYNAMIC_ARRAY keyuse;
00182   Item::cond_result cond_value;
00183   Item::cond_result having_value;
00184   List<Item> all_fields; 
00186   List<Item> tmp_all_fields1;
00187   List<Item> tmp_all_fields2;
00188   List<Item> tmp_all_fields3;
00190   List<Item> tmp_fields_list1;
00191   List<Item> tmp_fields_list2;
00192   List<Item> tmp_fields_list3;
00193   int error;
00194 
00195   Order *order;
00196   Order *group_list; 
00197   COND *conds;                            // ---"---
00198   Item *conds_history; 
00199   TableList *tables_list; 
00200   COND_EQUAL *cond_equal;
00201   JoinTable *return_tab; 
00202   Item **ref_pointer_array; 
00204   Item **items0;
00205   Item **items1;
00206   Item **items2;
00207   Item **items3;
00208   Item **current_ref_pointer_array;
00209   uint32_t ref_pointer_array_size; 
00210   const char *zero_result_cause; 
00211 
00212   /*
00213     storage for caching buffers allocated during query execution.
00214     These buffers allocations need to be cached as the thread memory pool is
00215     cleared only at the end of the execution of the whole query and not caching
00216     allocations that occur in repetition at execution time will result in
00217     excessive memory usage.
00218   */
00219   SortField *sortorder;                        // make_unireg_sortorder()
00220   Table **table_reexec;                         // make_simple_join()
00221   JoinTable *join_tab_reexec;                    // make_simple_join()
00222   /* end of allocation caching storage */
00223 
00225   Join(Session *session_arg, 
00226        List<Item> &fields_arg, 
00227        uint64_t select_options_arg,
00228        select_result *result_arg);
00229 
00236   void reset(Session *session_arg, 
00237              List<Item> &fields_arg, 
00238              uint64_t select_options_arg,
00239              select_result *result_arg);
00240 
00241   int prepare(Item ***rref_pointer_array, 
00242               TableList *tables,
00243               uint32_t wind_num,
00244               COND *conds,
00245               uint32_t og_num,
00246               Order *order,
00247               Order *group,
00248               Item *having,
00249               Select_Lex *select,
00250               Select_Lex_Unit *unit);
00251 
00252   int optimize();
00253   int reinit();
00254   void exec();
00255   int destroy();
00256   void restore_tmp();
00257   bool alloc_func_list();
00258   bool setup_subquery_materialization();
00259   bool make_sum_func_list(List<Item> &all_fields, 
00260                           List<Item> &send_fields,
00261                           bool before_group_by,
00262                           bool recompute= false);
00263 
00264   inline void set_items_ref_array(Item **ptr)
00265   {
00266     memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
00267     current_ref_pointer_array= ptr;
00268   }
00269   inline void init_items_ref_array()
00270   {
00271     items0= ref_pointer_array + all_fields.size();
00272     memcpy(items0, ref_pointer_array, ref_pointer_array_size);
00273     current_ref_pointer_array= items0;
00274   }
00275 
00276   bool rollup_init();
00277   bool rollup_make_fields(List<Item> &all_fields, 
00278                           List<Item> &fields,
00279                           Item_sum ***func);
00280   int rollup_send_data(uint32_t idx);
00281   int rollup_write_data(uint32_t idx, Table *table);
00282   void remove_subq_pushed_predicates(Item **where);
00289   void join_free();
00291   void cleanup(bool full);
00292   void clear();
00293   void save_join_tab();
00294   void init_save_join_tab();
00295   bool send_row_on_empty_set()
00296   {
00297     return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
00298       !group_list);
00299   }
00300   bool change_result(select_result *result);
00301   bool is_top_level_join() const;
00302 
00308   void copyPartialPlanIntoOptimalPlan(uint32_t size)
00309   {
00310     memcpy(best_positions, positions, 
00311            sizeof(optimizer::Position) * size);
00312   }
00313 
00314   void cache_const_exprs();
00315 
00321   optimizer::Position &getPosFromOptimalPlan(uint32_t index)
00322   {
00323     return best_positions[index];
00324   }
00325 
00331   optimizer::Position &getPosFromPartialPlan(uint32_t index)
00332   {
00333     return positions[index];
00334   }
00335 
00340   void setPosInPartialPlan(uint32_t index, optimizer::Position &in_pos)
00341   {
00342     positions[index]= in_pos;
00343   }
00344 
00348   optimizer::Position *getFirstPosInPartialPlan()
00349   {
00350     return positions;
00351   }
00352 
00358   optimizer::Position *getSpecificPosInPartialPlan(int32_t index)
00359   {
00360     return positions + index;
00361   }
00362 
00363 };
00364 
00365 enum_nested_loop_state evaluate_join_record(Join *join, JoinTable *join_tab, int error);
00366 enum_nested_loop_state evaluate_null_complemented_join_record(Join *join, JoinTable *join_tab);
00367 enum_nested_loop_state flush_cached_records(Join *join, JoinTable *join_tab, bool skip_last);
00368 enum_nested_loop_state end_send(Join *join, JoinTable *join_tab, bool end_of_records);
00369 enum_nested_loop_state end_write(Join *join, JoinTable *join_tab, bool end_of_records);
00370 enum_nested_loop_state end_update(Join *join, JoinTable *join_tab, bool end_of_records);
00371 enum_nested_loop_state end_unique_update(Join *join, JoinTable *join_tab, bool end_of_records);
00372 
00373 } /* namespace drizzled */
00374