56#include <drizzled/sql_select.h>
57#include <drizzled/item/sum.h>
58#include <drizzled/item/cmpfunc.h>
59#include <drizzled/optimizer/sum.h>
60#include <drizzled/plugin/storage_engine.h>
61#include <drizzled/table_list.h>
62#include <drizzled/key.h>
63#include <drizzled/error.h>
64#include <drizzled/key_part_info.h>
74 uint32_t *key_prefix_length);
101static uint64_t get_exact_record_count(
TableList *tables)
106 ha_rows tmp= tl->table->cursor->records();
107 if ((tmp == HA_POS_ERROR))
119 List<Item>::iterator it(all_fields.begin());
121 bool recalc_const_item=
false;
123 bool is_exact_count=
true;
124 bool maybe_exact_count=
true;
125 table_map removed_tables= 0;
126 table_map outer_tables= 0;
127 table_map used_tables= 0;
128 table_map where_tables= 0;
134 where_tables= conds->used_tables();
144 for (embedded= tl; embedded; embedded= embedded->getEmbedding())
146 if (embedded->on_expr)
152 outer_tables|= tl->table->map;
160 if (tl->table->map & where_tables)
165 used_tables|= tl->table->map;
175 if (! (tl->table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)))
177 maybe_exact_count&= test(tl->table->cursor->getEngine()->check_flag(HTON_BIT_HAS_RECORDS));
178 is_exact_count=
false;
183 error= tl->table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
186 tl->table->print_error(error, MYF(ME_FATALERROR));
189 count*= tl->table->cursor->stats.records;
200 if (item->type() == Item::SUM_FUNC_ITEM)
203 switch (item_sum->sum_func())
205 case Item_sum::COUNT_FUNC:
212 ! outer_tables && maybe_exact_count)
214 if (! is_exact_count)
216 if ((count= get_exact_record_count(tables)) == UINT64_MAX)
225 recalc_const_item= 1;
232 case Item_sum::MIN_FUNC:
239 Item *expr=item_sum->args[0];
240 if (expr->real_item()->type() == Item::FIELD_ITEM)
242 unsigned char key_buff[MAX_KEY_LENGTH];
244 uint32_t range_fl, prefix_len;
258 if (table->cursor->inited ||
259 (outer_tables & table->map) ||
270 error= table->
cursor->startIndexScan(
static_cast<uint32_t
>(ref.key), 1);
276 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
278 table->print_error(error, MYF(0));
282 if (! ref.key_length)
284 error= table->cursor->index_first(table->record[0]);
302 if (! (range_fl & NEAR_MIN))
308 error= table->cursor->index_read_map(table->record[0],
310 make_prev_keypart_map(ref.key_parts),
311 HA_READ_KEY_OR_NEXT);
321 error= table->cursor->index_read_map(table->record[0],
323 make_prev_keypart_map(ref.key_parts),
334 if (item_field->field->real_maybe_null() &&
335 ref.key_buff[prefix_len] == 1 &&
343 (error == HA_ERR_KEY_NOT_FOUND ||
346 assert(item_field->field->real_maybe_null());
347 error= table->cursor->index_read_map(table->record[0],
349 make_prev_keypart_map(ref.key_parts),
363 error= HA_ERR_KEY_NOT_FOUND;
368 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
370 table->cursor->endIndexScan();
373 if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
375 return HA_ERR_KEY_NOT_FOUND;
378 table->print_error(error, MYF(0));
381 removed_tables|= table->map;
383 else if (! expr->const_item() || ! is_exact_count)
407 recalc_const_item= 1;
410 case Item_sum::MAX_FUNC:
417 Item *expr= item_sum->args[0];
418 if (expr->real_item()->type() == Item::FIELD_ITEM)
420 unsigned char key_buff[MAX_KEY_LENGTH];
422 uint32_t range_fl, prefix_len;
436 if (table->cursor->inited ||
437 (outer_tables & table->map) ||
448 error= table->
cursor->startIndexScan(
static_cast<uint32_t
>(ref.key), 1);
450 if (! ref.key_length)
452 error= table->cursor->index_last(table->record[0]);
456 error= table->cursor->index_read_map(table->record[0],
458 make_prev_keypart_map(ref.key_parts),
459 range_fl & NEAR_MAX ?
461 HA_READ_PREFIX_LAST_OR_PREV);
471 error= HA_ERR_KEY_NOT_FOUND;
476 table->cursor->extra(HA_EXTRA_NO_KEYREAD);
478 table->cursor->endIndexScan();
481 if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
483 return HA_ERR_KEY_NOT_FOUND;
486 table->print_error(error, MYF(ME_FATALERROR));
489 removed_tables|= table->map;
491 else if (! expr->const_item() || ! is_exact_count)
515 recalc_const_item= 1;
523 else if (const_result)
525 if (recalc_const_item)
527 item->update_used_tables();
529 if (! item->const_item())
542 if (removed_tables && used_tables != removed_tables)
550bool optimizer::simple_pred(
Item_func *func_item,
Item **args,
bool &inv_order)
554 switch (func_item->argument_count())
560 Item_equal_iterator it(item_equal->begin());
566 if (! (args[1]= item_equal->get_const()))
574 item= func_item->arguments()[0];
575 if (item->type() != Item::FIELD_ITEM)
583 item= func_item->arguments()[0];
584 if (item->type() == Item::FIELD_ITEM)
587 item= func_item->arguments()[1];
588 if (! item->const_item())
594 else if (item->const_item())
597 item= func_item->arguments()[1];
598 if (item->type() != Item::FIELD_ITEM)
612 item= func_item->arguments()[0];
613 if (item->type() == Item::FIELD_ITEM)
616 for (
int i= 1 ; i <= 2; i++)
618 item= func_item->arguments()[i];
619 if (! item->const_item())
669 key_part_map *key_part_used,
671 uint32_t *prefix_len)
677 Field *field= field_part->field;
679 field->setWriteSet();
681 if (! (cond->
used_tables() & field->getTable()->map))
686 if (cond->type() == Item::COND_ITEM)
688 if (((
Item_cond*) cond)->functype() == Item_func::COND_OR_FUNC)
694 List<Item>::iterator li(((
Item_cond*) cond)->argument_list()->begin());
713 if (cond->type() != Item::FUNC_ITEM)
719 bool noeq_type=
false;
726 case Item_func::ISNULL_FUNC:
728 case Item_func::EQ_FUNC:
729 case Item_func::EQUAL_FUNC:
732 case Item_func::LT_FUNC:
734 case Item_func::LE_FUNC:
737 case Item_func::GT_FUNC:
739 case Item_func::GE_FUNC:
741 case Item_func::BETWEEN:
744 case Item_func::MULT_EQUAL_FUNC:
755 if (! optimizer::simple_pred((
Item_func*) cond, args, inv))
760 if (inv && ! eq_type)
762 less_fl= 1 - less_fl;
766 unsigned char *key_ptr= ref->
key_buff;
768 for (part= keyinfo->key_part; ; key_ptr+= part++->store_length)
771 if (part > field_part)
775 if (part->field->eq(((
Item_field*) args[0])->field))
781 bool is_field_part= part == field_part;
782 if (! (is_field_part || eq_type))
787 key_part_map org_key_part_used= *key_part_used;
788 if (eq_type || between || max_fl == less_fl)
790 uint32_t length= (key_ptr-ref->
key_buff)+part->store_length;
795 ref->
key_parts= (part - keyinfo->key_part) + 1;
797 if (! *prefix_len && part + 1 == field_part)
801 if (is_field_part && eq_type)
806 *key_part_used|= (key_part_map) 1 << (part - keyinfo->key_part);
809 if (org_key_part_used != *key_part_used ||
811 (between || eq_type || max_fl == less_fl) && ! cond->
val_int()))
824 part->field->set_null();
825 *key_ptr= (
unsigned char) 1;
833 *key_ptr++= (
unsigned char) test(part->field->is_null());
839 if (between || eq_type)
841 *range_fl&= ~(NO_MAX_RANGE | NO_MIN_RANGE);
845 *range_fl&= ~(max_fl ? NO_MAX_RANGE : NO_MIN_RANGE);
848 *range_fl|= (max_fl ? NEAR_MAX : NEAR_MIN);
852 *range_fl&= ~(max_fl ? NEAR_MAX : NEAR_MIN);
859 if ((! is_null && !cond->
val_int()) ||
860 (is_null && !test(part->field->is_null())))
865 else if (is_field_part)
867 *range_fl&= ~(max_fl ? NO_MIN_RANGE : NO_MAX_RANGE);
919 uint32_t *prefix_len)
921 if (! (field->flags & PART_KEY_FLAG))
926 Table *table= field->getTable();
929 KeyInfo *keyinfo,*keyinfo_end= NULL;
930 for (keyinfo= table->key_info, keyinfo_end= keyinfo+table->getShare()->sizeKeys();
931 keyinfo != keyinfo_end;
936 key_part_map key_part_to_use= 0;
941 if (! table->keys_in_use_for_query.test(idx))
947 for (part= keyinfo->key_part, part_end= part+keyinfo->key_parts;
949 part++, jdx++, key_part_to_use= (key_part_to_use << 1) | 1)
951 if (! (table->index_flags(idx) & HA_READ_ORDER))
957 Field *part_field= table->getField(part->fieldnr-1);
958 part_field->setWriteSet();
960 if ((part_field->flags & BLOB_FLAG) ||
961 part->length < part_field->key_length())
966 if (field->eq(part->field))
971 key_part_map key_part_used= 0;
972 *range_fl= NO_MIN_RANGE | NO_MAX_RANGE;
981 ! (key_part_to_use & ~key_part_used))
983 if (! max_fl && key_part_used == key_part_to_use && part->null_bit)
1004 *range_fl&= ~NO_MIN_RANGE;
1005 *range_fl|= NEAR_MIN;
1011 if (field->part_of_key.test(idx))
1014 table->cursor->extra(HA_EXTRA_KEYREAD);
1045 uint32_t prefix_len)
1051 if (! cond || (range_fl & (max_fl ? NO_MIN_RANGE : NO_MAX_RANGE)))
1074 if (cond->type() == Item::COND_ITEM)
1076 List<Item>::iterator li(((
Item_cond*) cond)->argument_list()->begin());
1077 while (
Item* item= li++)
1085 if (cond->
used_tables() != field->getTable()->map)
1089 bool less_fl=
false;
1090 switch (((
Item_func*) cond)->functype())
1092 case Item_func::BETWEEN:
1094 case Item_func::LT_FUNC:
1095 case Item_func::LE_FUNC:
1098 case Item_func::GT_FUNC:
1099 case Item_func::GE_FUNC:
1103 if (! item->const_item())
1113 if (max_fl != less_fl)
1119 case Item_func::EQ_FUNC:
1120 case Item_func::EQUAL_FUNC:
virtual uint32_t get_key_image(unsigned char *buff, uint32_t length)
virtual int64_t val_int()=0
virtual table_map used_tables() const
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
TODO: Rename this file - func.h is stupid.
static bool find_key_for_maxmin(bool max_fl, table_reference_st *ref, Field *field, COND *cond, uint32_t *range_fl, uint32_t *key_prefix_length)
static int reckey_in_range(bool max_fl, table_reference_st *ref, Field *field, COND *cond, uint32_t range_fl, uint32_t prefix_len)
static bool matching_cond(bool max_fl, table_reference_st *ref, KeyInfo *keyinfo, KeyPartInfo *field_part, COND *cond, key_part_map *key_part_used, uint32_t *range_fl, uint32_t *prefix_len)
static int maxmin_in_range(bool max_fl, Field *field, COND *cond)
bool key_cmp_if_same(Table *table, const unsigned char *key, uint32_t idx, uint32_t key_length)