diff options
Diffstat (limited to 'sql/opt_sum.cc')
-rw-r--r-- | sql/opt_sum.cc | 74 |
1 files changed, 50 insertions, 24 deletions
diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index f912d67fe06..9222e15ff91 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -2,8 +2,7 @@ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -59,9 +58,9 @@ static int maxmin_in_range(bool max_fl, Field* field, COND *cond); SYNOPSIS opt_sum_query() - tables Tables in query - all_fields All fields to be returned - conds WHERE clause + tables list of leaves of join table tree + all_fields All fields to be returned + conds WHERE clause NOTE: This function is only called for queries with sum functions and no @@ -94,10 +93,16 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) Analyze outer join dependencies, and, if possible, compute the number of returned rows. */ - for (TABLE_LIST *tl=tables; tl ; tl= tl->next) + for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf) { + TABLE_LIST *embedded; + for (embedded= tl ; embedded; embedded= embedded->embedding) + { + if (embedded->on_expr) + break; + } + if (embedded) /* Don't replace expression on a table that is part of an outer join */ - if (tl->on_expr) { outer_tables|= tl->table->map; @@ -117,8 +122,11 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) If the storage manager of 'tl' gives exact row count, compute the total number of rows. If there are no outer table dependencies, this count may be used as the real count. + Schema tables are filled after this function is invoked, so we can't + get row count */ - if (tl->table->file->table_flags() & HA_NOT_EXACT_COUNT) + if ((tl->table->file->table_flags() & HA_NOT_EXACT_COUNT) || + tl->schema_table) { is_exact_count= FALSE; count= 1; // ensure count != 0 @@ -148,7 +156,7 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) switch (item_sum->sum_func()) { case Item_sum::COUNT_FUNC: /* - If the expr in count(expr) can never be null we can change this + If the expr in COUNT(expr) can never be null we can change this to the number of rows in the tables if this number is exact and there are no outer joins. */ @@ -169,14 +177,14 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) indexes to find the key. */ Item *expr=item_sum->args[0]; - if (expr->type() == Item::FIELD_ITEM) + if (expr->real_item()->type() == Item::FIELD_ITEM) { byte key_buff[MAX_KEY_LENGTH]; TABLE_REF ref; uint range_fl, prefix_len; ref.key_buff= key_buff; - Item_field *item_field= ((Item_field*) expr); + Item_field *item_field= (Item_field*) (expr->real_item()); TABLE *table= item_field->field->table; /* @@ -256,14 +264,14 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) indexes to find the key. */ Item *expr=item_sum->args[0]; - if (expr->type() == Item::FIELD_ITEM) + if (expr->real_item()->type() == Item::FIELD_ITEM) { byte key_buff[MAX_KEY_LENGTH]; TABLE_REF ref; - uint range_fl, prefix_len; + uint range_fl, prefix_len; ref.key_buff= key_buff; - Item_field *item_field= ((Item_field*) expr); + Item_field *item_field= (Item_field*) (expr->real_item()); TABLE *table= item_field->field->table; /* @@ -356,7 +364,7 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) removed_tables is != 0 if we have used MIN() or MAX(). */ if (removed_tables && used_tables != removed_tables) - const_result= 0; // We didn't remove all tables + const_result= 0; // We didn't remove all tables return const_result; } @@ -366,20 +374,34 @@ int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds) SYNOPSIS simple_pred() - func_item in: Predicate item + func_item Predicate item args out: Here we store the field followed by constants - inv_order out: Is set to 1 if the predicate is of the form 'const op field' + inv_order out: Is set to 1 if the predicate is of the form + 'const op field' RETURN - 0 func_item is a simple predicate: a field is compared with constants + 0 func_item is a simple predicate: a field is compared with + constants 1 Otherwise */ -static bool simple_pred(Item_func *func_item, Item **args, bool *inv_order) +bool simple_pred(Item_func *func_item, Item **args, bool *inv_order) { Item *item; *inv_order= 0; switch (func_item->argument_count()) { + case 0: + /* MULT_EQUAL_FUNC */ + { + Item_equal *item_equal= (Item_equal *) func_item; + Item_equal_iterator it(*item_equal); + args[0]= it++; + if (it++) + return 0; + if (!(args[1]= item_equal->get_const())) + return 0; + } + break; case 1: /* field IS NULL */ item= func_item->arguments()[0]; @@ -520,6 +542,9 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, case Item_func::BETWEEN: between= 1; break; + case Item_func::MULT_EQUAL_FUNC: + eq_type= 1; + break; default: return 0; // Can't optimize function } @@ -591,8 +616,7 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, CHECK_FIELD_IGNORE); if (part->null_bit) *key_ptr++= (byte) test(part->field->is_null()); - part->field->get_key_image((char*) key_ptr, part->length, - part->field->charset(), Field::itRAW); + part->field->get_key_image((char*) key_ptr, part->length, Field::itRAW); } if (is_field_part) { @@ -675,7 +699,7 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, uint idx= 0; KEY *keyinfo,*keyinfo_end; - for (keyinfo= table->key_info, keyinfo_end= keyinfo+table->keys ; + for (keyinfo= table->key_info, keyinfo_end= keyinfo+table->s->keys ; keyinfo != keyinfo_end; keyinfo++,idx++) { @@ -696,8 +720,10 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, if (!(table->file->index_flags(idx, jdx, 0) & HA_READ_ORDER)) return 0; - /* Check whether the index component is partial */ - if (part->length < table->field[part->fieldnr-1]->pack_length()) + /* Check whether the index component is partial */ + Field *part_field= table->field[part->fieldnr-1]; + if ((part_field->flags & BLOB_FLAG) || + part->length < part_field->key_length()) break; if (field->eq(part->field)) |