diff options
Diffstat (limited to 'sql/sql_select.cc')
-rw-r--r-- | sql/sql_select.cc | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 85ebb9f207f..4456c631032 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -466,8 +466,17 @@ JOIN::optimize() if (tables_list && tmp_table_param.sum_func_count && ! group_list) { int res; + /* + opt_sum_query() returns -1 if no rows match to the WHERE conditions, + or 1 if all items were resolved, or 0, or an error number HA_ERR_... + */ if ((res=opt_sum_query(tables_list, all_fields, conds))) { + if (res > 1) + { + delete procedure; + DBUG_RETURN(-1); + } if (res < 0) { zero_result_cause= "No matching min/max row"; @@ -610,7 +619,17 @@ JOIN::optimize() select_distinct= 0; no_order= !order; if (all_order_fields_used) + { + if (order && skip_sort_order) + { + /* + Force MySQL to read the table in sorted order to get result in + ORDER BY order. + */ + join.tmp_table_param.quick_group=0; + } order=0; + } group=1; // For end_write_group } else @@ -2379,7 +2398,10 @@ find_best(JOIN *join,table_map rest_tables,uint idx,double record_count, if (table->quick_keys & ((key_map) 1 << key)) records= (double) table->quick_rows[key]; else - records= (double) s->records/rec; // quick_range couldn't use key! + { + /* quick_range couldn't use key! */ + records= (double) s->records/rec; + } } else { @@ -3037,6 +3059,9 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) join->unit->select_limit_cnt)) < 0) DBUG_RETURN(1); // Impossible range sel->cond=orig_cond; + /* Fix for EXPLAIN */ + if (sel->quick) + join->best_positions[i].records_read= sel->quick->records; } else { @@ -7249,7 +7274,8 @@ setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, while ((item=li++)) { - if (item->type() != Item::SUM_FUNC_ITEM && !item->marker) + if (item->type() != Item::SUM_FUNC_ITEM && !item->marker && + !item->const_item()) { my_printf_error(ER_WRONG_FIELD_WITH_GROUP, ER(ER_WRONG_FIELD_WITH_GROUP), |