00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00022 #include <drizzled/cached_item.h>
00023 #include <drizzled/field/varstring.h>
00024 #include <drizzled/item/null.h>
00025 #include <drizzled/enum_nested_loop_state.h>
00026 #include <drizzled/optimizer/position.h>
00027 #include <drizzled/optimizer/sargable_param.h>
00028 #include <drizzled/optimizer/key_use.h>
00029 #include <drizzled/join_cache.h>
00030 #include <drizzled/join_table.h>
00031 #include <drizzled/records.h>
00032 #include <drizzled/stored_key.h>
00033
00034 #include <vector>
00035
00036 namespace drizzled
00037 {
00038
00039 class Item_func;
00040 class Select_Lex_Unit;
00041 class select_result;
00042 class st_dynamic_array;
00043
00048
00049 #define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1))
00050
00051
00052 #define KEY_OPTIMIZE_EXISTS 1
00053 #define KEY_OPTIMIZE_REF_OR_NULL 2
00054
00055 class Join;
00056
00057 enum_nested_loop_state sub_select_cache(Join *join, JoinTable *join_tab, bool end_of_records);
00058 enum_nested_loop_state sub_select(Join *join,JoinTable *join_tab, bool end_of_records);
00059 enum_nested_loop_state end_send_group(Join *join, JoinTable *join_tab, bool end_of_records);
00060 enum_nested_loop_state end_write_group(Join *join, JoinTable *join_tab, bool end_of_records);
00061
00062 class Rollup
00063 {
00064 public:
00065 enum State { STATE_NONE, STATE_INITED, STATE_READY };
00066
00067 Rollup()
00068 :
00069 state(),
00070 null_items(NULL),
00071 ref_pointer_arrays(NULL),
00072 fields()
00073 {}
00074
00075 Rollup(State in_state,
00076 Item_null_result **in_null_items,
00077 Item ***in_ref_pointer_arrays,
00078 List<Item> *in_fields)
00079 :
00080 state(in_state),
00081 null_items(in_null_items),
00082 ref_pointer_arrays(in_ref_pointer_arrays),
00083 fields(in_fields)
00084 {}
00085
00086 State getState() const
00087 {
00088 return state;
00089 }
00090
00091 void setState(State in_state)
00092 {
00093 state= in_state;
00094 }
00095
00096 Item_null_result **getNullItems() const
00097 {
00098 return null_items;
00099 }
00100
00101 void setNullItems(Item_null_result **in_null_items)
00102 {
00103 null_items= in_null_items;
00104 }
00105
00106 Item ***getRefPointerArrays() const
00107 {
00108 return ref_pointer_arrays;
00109 }
00110
00111 void setRefPointerArrays(Item ***in_ref_pointer_arrays)
00112 {
00113 ref_pointer_arrays= in_ref_pointer_arrays;
00114 }
00115
00116 List<Item> *getFields() const
00117 {
00118 return fields;
00119 }
00120
00121 void setFields(List<Item> *in_fields)
00122 {
00123 fields= in_fields;
00124 }
00125
00126 private:
00127 State state;
00128 Item_null_result **null_items;
00129 Item ***ref_pointer_arrays;
00130 List<Item> *fields;
00131 };
00132
00133 }
00134
00137 #include <drizzled/join.h>
00138
00139 namespace drizzled
00140 {
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 typedef std::pair<Item*, Item_func*> COND_CMP;
00151
00152 void TEST_join(Join *join);
00153
00154
00155 bool store_val_in_field(Field *field, Item *val, enum_check_fields check_flag);
00156 Table *create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
00157 Order *group, bool distinct, bool save_sum_fields,
00158 uint64_t select_options, ha_rows rows_limit,
00159 const char* alias);
00160 void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
00161 List<Item> &fields, bool reset_with_sum_func);
00162 bool setup_copy_fields(Session *session, Tmp_Table_Param *param,
00163 Item **ref_pointer_array,
00164 List<Item> &new_list1, List<Item> &new_list2,
00165 uint32_t elements, List<Item> &fields);
00166 void copy_fields(Tmp_Table_Param *param);
00167 bool copy_funcs(Item **func_ptr, const Session *session);
00168 Field* create_tmp_field_from_field(Session *session, Field* org_field,
00169 const char *name, Table *table,
00170 Item_field *item, uint32_t convert_blob_length);
00171 bool test_if_ref(Item_field *left_item,Item *right_item);
00172 COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value);
00173 COND *make_cond_for_table(COND *cond,table_map table, table_map used_table, bool exclude_expensive_cond);
00174 COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx);
00175 bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data);
00176 bool find_field_in_order_list (Field *field, void *data);
00177 bool find_field_in_item_list (Field *field, void *data);
00178 bool test_if_skip_sort_order(JoinTable *tab,Order *order,ha_rows select_limit, bool no_changes, const key_map *map);
00179 Order *create_distinct_group(Session *session,
00180 Item **ref_pointer_array,
00181 Order *order_list,
00182 List<Item> &fields,
00183 List<Item> &,
00184 bool *all_order_by_fields_used);
00185
00186 bool change_to_use_tmp_fields(Session *session,
00187 Item **ref_pointer_array,
00188 List<Item> &res_selected_fields,
00189 List<Item> &res_all_fields,
00190 uint32_t elements,
00191 List<Item> &all_fields);
00192 int do_select(Join *join, List<Item> *fields, Table *tmp_table);
00193 bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
00194 int create_sort_index(Session *session, Join *join, Order *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by);
00195 void save_index_subquery_explain_info(JoinTable *join_tab, Item* where);
00196 Item *remove_additional_cond(Item* conds);
00197 bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
00198 bool init_sum_functions(Item_sum **func, Item_sum **end);
00199 bool update_sum_func(Item_sum **func);
00200 void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
00201 bool change_refs_to_tmp_fields(Session *session,
00202 Item **ref_pointer_array,
00203 List<Item> &res_selected_fields,
00204 List<Item> &res_all_fields,
00205 uint32_t elements,
00206 List<Item> &all_fields);
00207 bool change_group_ref(Session *session, Item_func *expr, Order *group_list, bool *changed);
00208 bool check_interleaving_with_nj(JoinTable *next);
00209 void update_const_equal_items(COND *cond, JoinTable *tab);
00210 int join_read_const(JoinTable *tab);
00211 int join_read_key(JoinTable *tab);
00212 int join_read_always_key(JoinTable *tab);
00213 int join_read_last_key(JoinTable *tab);
00214 int join_no_more_records(ReadRecord *info);
00215 int join_read_next(ReadRecord *info);
00216 int join_read_next_different(ReadRecord *info);
00217 int join_init_quick_read_record(JoinTable *tab);
00218 int init_read_record_seq(JoinTable *tab);
00219 int test_if_quick_select(JoinTable *tab);
00220 int join_init_read_record(JoinTable *tab);
00221 int join_read_first(JoinTable *tab);
00222 int join_read_next_same(ReadRecord *info);
00223 int join_read_next_same_diff(ReadRecord *info);
00224 int join_read_last(JoinTable *tab);
00225 int join_read_prev_same(ReadRecord *info);
00226 int join_read_prev(ReadRecord *info);
00227 int join_read_always_key_or_null(JoinTable *tab);
00228 int join_read_next_same_or_null(ReadRecord *info);
00229
00230 void calc_used_field_length(Session *, JoinTable *join_tab);
00231 StoredKey *get_store_key(Session *session,
00232 optimizer::KeyUse *keyuse,
00233 table_map used_tables,
00234 KeyPartInfo *key_part,
00235 unsigned char *key_buff,
00236 uint32_t maybe_null);
00237 int join_tab_cmp(const void* ptr1, const void* ptr2);
00238 int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
00239 void push_index_cond(JoinTable *tab, uint32_t keyno, bool other_tbls_ok);
00240 void add_not_null_conds(Join *join);
00241 uint32_t max_part_bit(key_part_map bits);
00242 COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab);
00243 bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab);
00244 int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having);
00245 int remove_dup_with_hash_index(Session *session,
00246 Table *table,
00247 uint32_t field_count,
00248 Field **first_field,
00249 uint32_t key_length,
00250 Item *having);
00251 bool update_ref_and_keys(Session *session,
00252 DYNAMIC_ARRAY *keyuse,
00253 JoinTable *join_tab,
00254 uint32_t tables,
00255 COND *cond,
00256 COND_EQUAL *,
00257 table_map normal_tables,
00258 Select_Lex *select_lex,
00259 std::vector<optimizer::SargableParam> &sargables);
00260 ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit);
00261 void optimize_keyuse(Join *join, DYNAMIC_ARRAY *keyuse_array);
00262 void add_group_and_distinct_keys(Join *join, JoinTable *join_tab);
00263 void read_cached_record(JoinTable *tab);
00264 bool select_query(Session *session, Item ***rref_pointer_array,
00265 TableList *tables, uint32_t wild_num, List<Item> &list,
00266 COND *conds, uint32_t og_num, Order *order, Order *group,
00267 Item *having, uint64_t select_type,
00268 select_result *result, Select_Lex_Unit *unit,
00269 Select_Lex *select_lex);
00270
00271 void init_tmptable_sum_functions(Item_sum **func);
00272 void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
00273 bool only_eq_ref_tables(Join *join, Order *order, table_map tables);
00274 bool create_ref_for_key(Join *join, JoinTable *j,
00275 optimizer::KeyUse *org_keyuse,
00276 table_map used_tables);
00277
00278 bool cp_buffer_from_ref(Session *session, table_reference_st *ref);
00279 int safe_index_read(JoinTable *tab);
00280 COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value);
00281 int test_if_item_cache_changed(List<Cached_item> &list);
00282
00283 void print_join(Session *session, String *str, List<TableList> *tables);
00284
00285 }
00286