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