1 #ifndef SQL_SELECT_INCLUDED
2 #define SQL_SELECT_INCLUDED
27 #include "procedure.h"
29 #include "sql_array.h"
31 #include "opt_range.h"
34 #include "mem_root_array.h"
35 #include "sql_executor.h"
36 #include "opt_explain_format.h"
44 #define LOWER_BITS(type,A) ((type) (((type) 1 << (A)) -1))
47 #define KEY_OPTIMIZE_EXISTS 1
48 #define KEY_OPTIMIZE_REF_OR_NULL 2
49 #define FT_KEYPART (MAX_REF_PARTS+10)
72 uint key_arg, uint keypart_arg, uint optimize_arg,
73 key_part_map keypart_map_arg, ha_rows ref_table_rows_arg,
74 bool null_rejecting_arg,
bool *cond_guard_arg,
75 uint sj_pred_no_arg) :
228 DBUG_ASSERT(
key_parts == 0 || cond_guards != NULL);
309 #define TAB_INFO_HAVE_VALUE 1
310 #define TAB_INFO_USING_INDEX 2
311 #define TAB_INFO_USING_WHERE 4
312 #define TAB_INFO_FULL_SCAN_ON_NULL 8
317 #define SJ_OPT_NONE 0
318 #define SJ_OPT_DUPS_WEEDOUT 1
319 #define SJ_OPT_LOOSE_SCAN 2
320 #define SJ_OPT_FIRST_MATCH 3
321 #define SJ_OPT_MATERIALIZE_LOOKUP 4
322 #define SJ_OPT_MATERIALIZE_SCAN 5
324 inline bool sj_is_materialize_strategy(uint strategy)
326 return strategy >= SJ_OPT_MATERIALIZE_LOOKUP;
386 table_map ref_depend_map;
387 bool use_join_buffer;
392 double prefix_record_count;
424 uint first_loosescan_table;
429 table_map loosescan_need_tables;
438 uint loosescan_parts;
445 uint first_firstmatch_table;
450 table_map first_firstmatch_rtbl;
455 table_map firstmatch_need_tables;
459 uint first_dupsweedout_table;
464 table_map dupsweedout_tables;
468 uint sjm_scan_last_inner;
474 table_map sjm_scan_need_tables;
482 sj_strategy= SJ_OPT_NONE;
485 void set_prefix_costs(
double read_time_arg,
double row_count_arg)
488 prefix_cost.
add_io(read_time_arg);
489 prefix_record_count= row_count_arg;
502 table_map prefix_tables()
const {
return prefix_tables_map; }
504 table_map added_tables()
const {
return added_tables_map; }
515 prefix_tables_map= prefix_tables;
516 added_tables_map= prefix_tables & ~prev_tables;
525 { prefix_tables_map|= tables; added_tables_map|= tables; }
564 Item *pre_idx_push_cond;
574 READ_RECORD::Setup_func materialize_table;
581 Next_select_func next_select;
588 READ_RECORD::Setup_func save_read_first_record;
589 READ_RECORD::Read_func save_read_record;
618 ha_rows found_records;
639 table_map prefix_tables_map;
644 table_map added_tables_map;
648 uint used_fields,used_fieldlength,used_blobs;
649 uint used_null_fields;
650 uint used_rowid_fields;
651 uint used_uneven_bit_fields;
654 bool not_used_in_distinct;
671 Item *cache_idx_cond;
689 struct st_join_table *last_sj_inner_tab;
701 struct st_join_table *firstmatch_return;
707 uint loosescan_key_len;
710 uchar *loosescan_buf;
721 struct st_join_table *match_tab;
733 int keep_current_rowid;
737 nested_join_map embedding_map;
740 TMP_TABLE_PARAM *tmp_table_param;
774 inline bool is_using_loose_index_scan()
781 DBUG_ASSERT(!(select && select->quick && filesort));
784 return (sel && sel->quick &&
785 (sel->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX));
787 bool is_using_agg_loose_index_scan ()
794 DBUG_ASSERT(!(select && select->quick && filesort));
797 return (sel && sel->quick &&
798 (sel->quick->get_type() ==
799 QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX) &&
800 static_cast<QUICK_GROUP_MIN_MAX_SELECT*>(sel->quick)->
804 bool check_rowid_field()
806 if (keep_current_rowid && !used_rowid_fields)
808 used_rowid_fields= 1;
811 return test(used_rowid_fields);
813 bool is_inner_table_of_outer_join()
817 bool is_single_inner_of_semi_join()
821 bool is_single_inner_of_outer_join()
825 bool is_first_inner_for_outer_join()
829 Item *condition()
const
833 void set_condition(
Item *
to, uint line)
836 (
"JOIN_TAB::m_condition changes %p -> %p at line %u tab %p",
837 m_condition, to, line,
this));
842 Item *set_jt_and_sel_condition(
Item *new_cond, uint line)
844 Item *tmp_cond= m_condition;
845 set_condition(new_cond, line);
847 select->cond= new_cond;
884 bool remove_duplicates();
904 pre_idx_push_cond(NULL),
907 materialize_table(NULL),
911 save_read_first_record(NULL),
912 save_read_record(NULL),
927 prefix_tables_map(0),
934 used_rowid_fields(0),
935 used_uneven_bit_fields(0),
938 not_used_in_distinct(false),
945 cache_idx_cond(NULL),
951 last_sj_inner_tab(NULL),
953 flush_weedout_table(NULL),
954 check_weed_out_table(NULL),
955 firstmatch_return(NULL),
956 loosescan_key_len(0),
961 keep_current_rowid(0),
962 copy_current_rowid(NULL),
964 tmp_table_param(NULL),
978 memset(&read_record, 0,
sizeof(read_record));
1018 public std::binary_function<const JOIN_TAB*, const JOIN_TAB*, bool>
1024 DBUG_ASSERT(jt1 != jt2);
1031 const bool jt1_keydep_jt2= jt1->
key_dependent & jt2->table->map;
1032 const bool jt2_keydep_jt1= jt2->
key_dependent & jt1->table->map;
1034 if (jt1_keydep_jt2 && !jt2_keydep_jt1)
1036 if (jt2_keydep_jt1 && !jt1_keydep_jt2)
1039 if (jt1->found_records > jt2->found_records)
1041 if (jt1->found_records < jt2->found_records)
1056 public std::binary_function<const JOIN_TAB*, const JOIN_TAB*, bool>
1062 DBUG_ASSERT(jt1 != jt2);
1069 DBUG_ASSERT(!jt1->emb_sj_nest);
1070 DBUG_ASSERT(!jt2->emb_sj_nest);
1087 public std::binary_function<const JOIN_TAB*, const JOIN_TAB*, bool>
1098 DBUG_ASSERT(jt1 != jt2);
1100 if (jt1->emb_sj_nest == emb_nest && jt2->emb_sj_nest != emb_nest)
1102 if (jt1->emb_sj_nest != emb_nest && jt2->emb_sj_nest == emb_nest)
1106 return cmp(jt1,jt2);
1114 uint const_ref,reg_ref;
1119 List<Item> &fields,
bool reset_with_sum_func);
1128 extern "C" int refpos_order_cmp(
const void* arg,
const void *a,
const void *b);
1136 enum store_key_result { STORE_KEY_OK, STORE_KEY_FATAL, STORE_KEY_CONV };
1137 store_key(THD *thd,
Field *field_arg, uchar *ptr, uchar *null, uint length)
1138 :null_key(0), null_ptr(null), err(0)
1140 if (field_arg->type() == MYSQL_TYPE_BLOB
1141 || field_arg->type() == MYSQL_TYPE_GEOMETRY)
1148 Field::NONE, field_arg->field_name,
1149 field_arg->table->s, field_arg->charset());
1150 to_field->init(field_arg->table);
1153 to_field=field_arg->new_key_field(thd->mem_root, field_arg->table,
1157 virtual const char *
name()
const=0;
1167 enum store_key_result result;
1168 THD *thd= to_field->table->in_use;
1169 enum_check_fields saved_count_cuted_fields= thd->count_cuted_fields;
1170 sql_mode_t sql_mode= thd->variables.sql_mode;
1171 thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
1173 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1175 result= copy_inner();
1177 thd->count_cuted_fields= saved_count_cuted_fields;
1178 thd->variables.sql_mode= sql_mode;
1188 virtual enum store_key_result copy_inner()=0;
1192 static store_key::store_key_result
1193 type_conversion_status_to_store_key (type_conversion_status ts)
1198 return store_key::STORE_KEY_OK;
1199 case TYPE_NOTE_TIME_TRUNCATED:
1200 return store_key::STORE_KEY_CONV;
1201 case TYPE_WARN_OUT_OF_RANGE:
1202 case TYPE_NOTE_TRUNCATED:
1203 case TYPE_WARN_TRUNCATED:
1204 case TYPE_ERR_NULL_CONSTRAINT_VIOLATION:
1205 case TYPE_ERR_BAD_VALUE:
1207 return store_key::STORE_KEY_FATAL;
1212 return store_key::STORE_KEY_FATAL;
1218 const char *field_name;
1221 uchar *null_ptr_arg,
1222 uint length,
Field *from_field,
const char *name_arg)
1224 null_ptr_arg ? null_ptr_arg : from_field->maybe_null() ? &err
1225 : (uchar*) 0, length), field_name(name_arg)
1229 copy_field.set(to_field,from_field,0);
1232 const char *
name()
const {
return field_name; }
1235 enum store_key_result copy_inner()
1238 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
1240 copy_field.do_copy(©_field);
1241 dbug_tmp_restore_column_map(table->write_set, old_map);
1242 null_key= to_field->is_null();
1243 return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
1254 uchar *null_ptr_arg, uint length,
Item *item_arg)
1256 null_ptr_arg ? null_ptr_arg : item_arg->maybe_null ?
1257 &err : (uchar*) 0, length), item(item_arg)
1259 const char *
name()
const {
return "func"; }
1262 enum store_key_result copy_inner()
1264 TABLE *table= to_field->table;
1265 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
1267 type_conversion_status save_res= item->save_in_field(to_field,
true);
1268 store_key_result res;
1273 if (save_res != TYPE_OK && table->in_use->is_error())
1274 res= STORE_KEY_FATAL;
1276 res= type_conversion_status_to_store_key(save_res);
1277 dbug_tmp_restore_column_map(table->write_set, old_map);
1278 null_key= to_field->is_null() || item->null_value;
1279 return (err != 0) ? STORE_KEY_FATAL : res;
1289 uchar *null_ptr_arg, uint length,
1292 null_ptr_arg, length, item_arg), inited(0)
1295 const char *
name()
const {
return "const"; }
1298 enum store_key_result copy_inner()
1303 store_key_result res= store_key_item::copy_inner();
1307 return (err > 2 ? STORE_KEY_FATAL : (store_key_result) err);
1313 ulong setup_tables_done_option);
1318 Item *having, ulonglong select_type,
1319 select_result *result, SELECT_LEX_UNIT *unit,
1320 SELECT_LEX *select_lex);
1326 inline bool optimizer_flag(THD *thd, uint flag)
1328 return (thd->variables.optimizer_switch & flag);
1332 ha_rows
limit,
bool *need_sort,
bool *reverse);
1334 bool const_expression_in_where(
Item *cond,
Item *comp_item,
1335 Field *comp_field= NULL,
1336 Item **const_item= NULL);
1341 const bool no_changes,
const key_map *map,
1342 const char *clause_type);
1345 table_map used_tables);
1358 uint *used_key_parts= NULL);