summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/ha_heap.cc13
-rw-r--r--sql/item.cc52
-rw-r--r--sql/item.h3
-rw-r--r--sql/item_cmpfunc.cc56
-rw-r--r--sql/item_func.cc19
-rw-r--r--sql/item_row.cc18
-rw-r--r--sql/item_strfunc.cc14
-rw-r--r--sql/item_subselect.cc6
-rw-r--r--sql/sql_list.h9
-rw-r--r--sql/sql_select.cc15
10 files changed, 105 insertions, 100 deletions
diff --git a/sql/ha_heap.cc b/sql/ha_heap.cc
index 02d81882e7a..d30435310dc 100644
--- a/sql/ha_heap.cc
+++ b/sql/ha_heap.cc
@@ -60,8 +60,7 @@ int ha_heap::open(const char *name, int mode, uint test_if_locked)
{
/* Initialize variables for the opened table */
set_keys_for_scanning();
- if (table->s->tmp_table == NO_TMP_TABLE)
- update_key_stats();
+ update_key_stats();
}
return (file ? 0 : 1);
}
@@ -104,6 +103,8 @@ void ha_heap::update_key_stats()
for (uint i= 0; i < table->s->keys; i++)
{
KEY *key=table->key_info+i;
+ if (!key->rec_per_key)
+ continue;
if (key->algorithm != HA_KEY_ALG_BTREE)
{
ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
@@ -124,8 +125,8 @@ int ha_heap::write_row(byte * buf)
if (table->next_number_field && buf == table->record[0])
update_auto_increment();
res= heap_write(file,buf);
- if (!res && table->s->tmp_table == NO_TMP_TABLE &&
- ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > file->s->records)
+ if (!res && (++records_changed*HEAP_STATS_UPDATE_THRESHOLD >
+ file->s->records))
update_key_stats();
return res;
}
@@ -137,8 +138,8 @@ int ha_heap::update_row(const byte * old_data, byte * new_data)
if (table->timestamp_field_type & TIMESTAMP_AUTO_SET_ON_UPDATE)
table->timestamp_field->set_time();
res= heap_update(file,old_data,new_data);
- if (!res && table->s->tmp_table == NO_TMP_TABLE &&
- ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > file->s->records)
+ if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD >
+ file->s->records)
update_key_stats();
return res;
}
diff --git a/sql/item.cc b/sql/item.cc
index 17ded05766f..96806ea2ed1 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -543,6 +543,58 @@ void Item_splocal::print(String *str)
/*
+ Move SUM items out from item tree and replace with reference
+
+ SYNOPSIS
+ split_sum_func2()
+ thd Thread handler
+ ref_pointer_array Pointer to array of reference fields
+ fields All fields in select
+ ref Pointer to item
+
+ NOTES
+ This is from split_sum_func2() for items that should be split
+
+ All found SUM items are added FIRST in the fields list and
+ we replace the item with a reference.
+
+ thd->fatal_error() may be called if we are out of memory
+*/
+
+
+void Item::split_sum_func2(THD *thd, Item **ref_pointer_array,
+ List<Item> &fields, Item **ref)
+{
+ if (type() != SUM_FUNC_ITEM && with_sum_func)
+ {
+ /* Will split complicated items and ignore simple ones */
+ split_sum_func(thd, ref_pointer_array, fields);
+ }
+ else if ((type() == SUM_FUNC_ITEM ||
+ (used_tables() & ~PARAM_TABLE_BIT)) &&
+ type() != REF_ITEM)
+ {
+ /*
+ Replace item with a reference so that we can easily calculate
+ it (in case of sum functions) or copy it (in case of fields)
+
+ The test above is to ensure we don't do a reference for things
+ that are constants (PARAM_TABLE_BIT is in effect a constant)
+ or already referenced (for example an item in HAVING)
+ */
+ uint el= fields.elements;
+ Item *new_item;
+ ref_pointer_array[el]= this;
+ if (!(new_item= new Item_ref(ref_pointer_array + el, 0, name)))
+ return; // fatal_error is set
+ fields.push_front(this);
+ ref_pointer_array[el]= this;
+ thd->change_item_tree(ref, new_item);
+ }
+}
+
+
+/*
Aggregate two collations together taking
into account their coercibility (aka derivation):
diff --git a/sql/item.h b/sql/item.h
index ccbc00470e5..e2e72dbee40 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -334,6 +334,9 @@ public:
virtual void update_used_tables() {}
virtual void split_sum_func(THD *thd, Item **ref_pointer_array,
List<Item> &fields) {}
+ /* Called for items that really have to be split */
+ void split_sum_func2(THD *thd, Item **ref_pointer_array, List<Item> &fields,
+ Item **ref);
virtual bool get_date(TIME *ltime,uint fuzzydate);
virtual bool get_time(TIME *ltime);
virtual bool get_date_result(TIME *ltime,uint fuzzydate)
diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc
index 71a4e4dda53..c26365e3f0e 100644
--- a/sql/item_cmpfunc.cc
+++ b/sql/item_cmpfunc.cc
@@ -704,13 +704,12 @@ longlong Item_in_optimizer::val_int()
{
DBUG_ASSERT(fixed == 1);
cache->store(args[0]);
- longlong tmp= args[1]->val_int_result();
if (cache->null_value)
{
- if (tmp)
- null_value= 1;
+ null_value= 1;
return 0;
}
+ longlong tmp= args[1]->val_int_result();
null_value= args[1]->null_value;
return tmp;
}
@@ -2349,10 +2348,10 @@ Item *Item_cond::transform(Item_transformer transformer, byte *arg)
Move SUM items out from item tree and replace with reference
SYNOPSIS
- split_sum_func()
- thd Thread handler
- ref_pointer_array Pointer to array of reference fields
- fields All fields in select
+ split_sum_func()
+ thd Thread handler
+ ref_pointer_array Pointer to array of reference fields
+ fields All fields in select
NOTES
This function is run on all expression (SELECT list, WHERE, HAVING etc)
@@ -2362,16 +2361,6 @@ Item *Item_cond::transform(Item_transformer transformer, byte *arg)
so that we can easily find and calculate them.
(Calculation done by update_sum_func() and copy_sum_funcs() in
sql_select.cc)
-
- All found SUM items are added FIRST in the fields list and
- we replace the item with a reference.
-
- We also replace all functions without side effects (like RAND() or UDF's)
- that uses columns as arguments.
- For functions with side effects, we just remember any fields referred
- by the function to ensure that we get a copy of the field value for the
- first accepted row. This ensures that we can do things like
- SELECT a*SUM(b) FROM t1 WHERE a=1
*/
void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
@@ -2379,37 +2368,8 @@ void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
{
List_iterator<Item> li(list);
Item *item;
- used_tables_cache=0;
- const_item_cache=1;
- while ((item=li++))
- {
- /* with_sum_func is set for items that contains a SUM expression */
- if (item->type() != SUM_FUNC_ITEM &&
- (item->with_sum_func ||
- (item->used_tables() & PSEUDO_TABLE_BITS)))
- item->split_sum_func(thd, ref_pointer_array, fields);
- else if (item->type() == SUM_FUNC_ITEM ||
- (item->used_tables() && item->type() != REF_ITEM))
- {
- /*
- Replace item with a reference so that we can easily calculate
- it (in case of sum functions) or copy it (in case of fields)
-
- The test above is to ensure we don't do a reference for things
- that are constants or are not yet calculated as in:
- SELECT RAND() as r1, SUM(a) as r2 FROM t1 HAVING r1 > 1 AND r2 > 0
- */
- Item **ref= li.ref();
- uint el= fields.elements;
- ref_pointer_array[el]= item;
- Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name);
- fields.push_front(item);
- thd->change_item_tree(ref, new_item);
- }
- item->update_used_tables();
- used_tables_cache|=item->used_tables();
- const_item_cache&=item->const_item();
- }
+ while ((item= li++))
+ item->split_sum_func2(thd, ref_pointer_array, fields, li.ref());
}
diff --git a/sql/item_func.cc b/sql/item_func.cc
index a4c1110da32..1e61474f412 100644
--- a/sql/item_func.cc
+++ b/sql/item_func.cc
@@ -401,27 +401,14 @@ Item *Item_func::transform(Item_transformer transformer, byte *argument)
}
+/* See comments in Item_cmp_func::split_sum_func() */
+
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
List<Item> &fields)
{
Item **arg, **arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
- {
- Item *item=* arg;
- if (item->type() != SUM_FUNC_ITEM &&
- (item->with_sum_func ||
- (item->used_tables() & PSEUDO_TABLE_BITS)))
- item->split_sum_func(thd, ref_pointer_array, fields);
- else if (item->type() == SUM_FUNC_ITEM ||
- (item->used_tables() && item->type() != REF_ITEM))
- {
- uint el= fields.elements;
- ref_pointer_array[el]= item;
- Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name);
- fields.push_front(item);
- thd->change_item_tree(arg, new_item);
- }
- }
+ (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg);
}
diff --git a/sql/item_row.cc b/sql/item_row.cc
index 08c682afa85..00d849e55de 100644
--- a/sql/item_row.cc
+++ b/sql/item_row.cc
@@ -90,24 +90,10 @@ void Item_row::split_sum_func(THD *thd, Item **ref_pointer_array,
{
Item **arg, **arg_end;
for (arg= items, arg_end= items+arg_count; arg != arg_end ; arg++)
- {
- Item *item= *arg;
- if (item->type() != SUM_FUNC_ITEM &&
- (item->with_sum_func ||
- (item->used_tables() & PSEUDO_TABLE_BITS)))
- item->split_sum_func(thd, ref_pointer_array, fields);
- else if (item->type() == SUM_FUNC_ITEM ||
- (item->used_tables() && item->type() != REF_ITEM))
- {
- uint el= fields.elements;
- ref_pointer_array[el]= *arg;
- Item *new_item= new Item_ref(ref_pointer_array + el, 0, (*arg)->name);
- fields.push_front(*arg);
- thd->change_item_tree(arg, new_item);
- }
- }
+ (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg);
}
+
void Item_row::update_used_tables()
{
used_tables_cache= 0;
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 55185a5f75b..02f33d39465 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -1778,19 +1778,7 @@ String *Item_func_elt::val_str(String *str)
void Item_func_make_set::split_sum_func(THD *thd, Item **ref_pointer_array,
List<Item> &fields)
{
- if (item->type() != SUM_FUNC_ITEM &&
- (item->with_sum_func ||
- (item->used_tables() & PSEUDO_TABLE_BITS)))
- item->split_sum_func(thd, ref_pointer_array, fields);
- else if (item->type() == SUM_FUNC_ITEM ||
- (item->used_tables() && item->type() != REF_ITEM))
- {
- uint el= fields.elements;
- ref_pointer_array[el]= item;
- Item *new_item= new Item_ref(ref_pointer_array + el, 0, item->name);
- fields.push_front(item);
- thd->change_item_tree(&item, new_item);
- }
+ item->split_sum_func2(thd, ref_pointer_array, fields, &item);
Item_str_func::split_sum_func(thd, ref_pointer_array, fields);
}
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 8d392232f02..fe849bd213a 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -889,8 +889,10 @@ Item_in_subselect::single_value_transformer(JOIN *join,
ref_pointer_array,
(char *)"<ref>",
this->full_name()));
+#ifdef CORRECT_BUT_TOO_SLOW_TO_BE_USABLE
if (!abort_on_null && left_expr->maybe_null)
item= new Item_cond_or(new Item_func_isnull(left_expr), item);
+#endif
/*
AND and comparison functions can't be changed during fix_fields()
we can assign select_lex->having here, and pass 0 as last
@@ -944,8 +946,10 @@ Item_in_subselect::single_value_transformer(JOIN *join,
goto err;
item= new Item_cond_or(item,
new Item_func_isnull(orig_item));
+#ifdef CORRECT_BUT_TOO_SLOW_TO_BE_USABLE
if (left_expr->maybe_null)
item= new Item_cond_or(new Item_func_isnull(left_expr), item);
+#endif
}
item->name= (char *)in_additional_cond;
/*
@@ -975,8 +979,10 @@ Item_in_subselect::single_value_transformer(JOIN *join,
new Item_null_helper(this, item,
(char *)"<no matter>",
(char *)"<result>"));
+#ifdef CORRECT_BUT_TOO_SLOW_TO_BE_USABLE
if (!abort_on_null && left_expr->maybe_null)
item= new Item_cond_or(new Item_func_isnull(left_expr), item);
+#endif
select_lex->having= join->having= item;
select_lex->having_fix_field= 1;
/*
diff --git a/sql/sql_list.h b/sql/sql_list.h
index 657943f1e69..6317d215399 100644
--- a/sql/sql_list.h
+++ b/sql/sql_list.h
@@ -140,6 +140,15 @@ public:
delete *prev;
*prev=node;
}
+ inline void concat(base_list *list)
+ {
+ if (!list->is_empty())
+ {
+ *last= list->first;
+ last= list->last;
+ elements+= list->elements;
+ }
+ }
inline void *pop(void)
{
if (first == &end_of_list) return 0;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 6af257893d4..f49f980f364 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -8247,6 +8247,7 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
keyinfo->key_length=(uint16) reclength;
keyinfo->name= (char*) "distinct_key";
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
+ keyinfo->rec_per_key=0;
if (null_pack_length)
{
key_part_info->null_bit=0;
@@ -11831,6 +11832,7 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
res_selected_fields.empty();
res_all_fields.empty();
List_iterator_fast<Item> itr(res_all_fields);
+ List<Item> extra_funcs;
uint i, border= all_fields.elements - elements;
DBUG_ENTER("setup_copy_fields");
@@ -11892,7 +11894,12 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
*/
if (!(pos=new Item_copy_string(pos)))
goto err;
- if (param->copy_funcs.push_back(pos))
+ if (i < border) // HAVING, ORDER and GROUP BY
+ {
+ if (extra_funcs.push_back(pos))
+ goto err;
+ }
+ else if (param->copy_funcs.push_back(pos))
goto err;
}
res_all_fields.push_back(pos);
@@ -11904,6 +11911,12 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
for (i= 0; i < border; i++)
itr++;
itr.sublist(res_selected_fields, elements);
+ /*
+ Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
+ reference used in these will resolve to a item that is already calculated
+ */
+ param->copy_funcs.concat(&extra_funcs);
+
DBUG_RETURN(0);
err: