diff --git a/sql/filesort.cc b/sql/filesort.cc index 6f2a6096aa2..e682f3389da 100644 --- a/sql/filesort.cc +++ b/sql/filesort.cc @@ -60,9 +60,7 @@ static bool save_index(Sort_param *param, uint count, static uint suffix_length(ulong string_length); static uint sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length, bool *multi_byte_charset); -static SORT_ADDON_FIELD *get_addon_fields(ulong max_length_for_sort_data, - Field **ptabfield, - uint sortlength, +static SORT_ADDON_FIELD *get_addon_fields(TABLE *table, uint sortlength, LEX_STRING *addon_buf); static void unpack_addon_fields(struct st_sort_addon_field *addon_field, uchar *buff, uchar *buff_end); @@ -71,7 +69,6 @@ static bool check_if_pq_applicable(Sort_param *param, SORT_INFO *info, ha_rows records, size_t memory_available); void Sort_param::init_for_filesort(uint sortlen, TABLE *table, - ulong max_length_for_sort_data, ha_rows maxrows, bool sort_positions) { DBUG_ASSERT(addon_field == 0 && addon_buf.length == 0); @@ -85,8 +82,7 @@ void Sort_param::init_for_filesort(uint sortlen, TABLE *table, Get the descriptors of all fields whose values are appended to sorted fields and get its total length in addon_buf.length */ - addon_field= get_addon_fields(max_length_for_sort_data, - table->field, sort_length, &addon_buf); + addon_field= get_addon_fields(table, sort_length, &addon_buf); } if (addon_field) { @@ -190,9 +186,7 @@ SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort, param.init_for_filesort(sortlength(thd, filesort->sortorder, s_length, &multi_byte_charset), - table, - thd->variables.max_length_for_sort_data, - max_rows, filesort->sort_positions); + table, max_rows, filesort->sort_positions); sort->addon_buf= param.addon_buf; sort->addon_field= param.addon_field; @@ -1970,7 +1964,7 @@ sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length, The function first finds out what fields are used in the result set. Then it calculates the length of the buffer to store the values of - these fields together with the value of sort values. + these fields together with the value of sort values. If the calculated length is not greater than max_length_for_sort_data the function allocates memory for an array of descriptors containing layouts for the values of the non-sorted fields in the buffer and @@ -1992,8 +1986,7 @@ sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length, */ static SORT_ADDON_FIELD * -get_addon_fields(ulong max_length_for_sort_data, - Field **ptabfield, uint sortlength, LEX_STRING *addon_buf) +get_addon_fields(TABLE *table, uint sortlength, LEX_STRING *addon_buf) { Field **pfield; Field *field; @@ -2001,7 +1994,8 @@ get_addon_fields(ulong max_length_for_sort_data, uint length= 0; uint fields= 0; uint null_fields= 0; - MY_BITMAP *read_set= (*ptabfield)->table->read_set; + MY_BITMAP *read_set= table->read_set; + ulong max_sort_len= table->in_use->variables.max_length_for_sort_data; DBUG_ENTER("get_addon_fields"); /* @@ -2010,14 +2004,14 @@ get_addon_fields(ulong max_length_for_sort_data, Note for future refinement: This this a too strong condition. Actually we need only the fields referred in the - result set. And for some of them it makes sense to use + result set. And for some of them it makes sense to use the values directly from sorted fields. But beware the case when item->cmp_type() != item->result_type() */ addon_buf->str= 0; addon_buf->length= 0; - for (pfield= ptabfield; (field= *pfield) ; pfield++) + for (pfield= table->field; (field= *pfield) ; pfield++) { if (!bitmap_is_set(read_set, field->field_index)) continue; @@ -2027,12 +2021,12 @@ get_addon_fields(ulong max_length_for_sort_data, if (field->maybe_null()) null_fields++; fields++; - } + } if (!fields) DBUG_RETURN(0); length+= (null_fields+7)/8; - if (length+sortlength > max_length_for_sort_data || + if (length+sortlength > max_sort_len || !my_multi_malloc(MYF(MY_WME | MY_THREAD_SPECIFIC), &addonf, sizeof(SORT_ADDON_FIELD) * (fields+1), &addon_buf->str, length, @@ -2043,7 +2037,7 @@ get_addon_fields(ulong max_length_for_sort_data, addon_buf->length= length; length= (null_fields+7)/8; null_fields= 0; - for (pfield= ptabfield; (field= *pfield) ; pfield++) + for (pfield= table->field; (field= *pfield) ; pfield++) { if (!bitmap_is_set(read_set, field->field_index)) continue; @@ -2065,7 +2059,7 @@ get_addon_fields(ulong max_length_for_sort_data, addonf++; } addonf->field= 0; // Put end marker - + DBUG_PRINT("info",("addon_length: %d",length)); DBUG_RETURN(addonf-fields); } diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 7408dc378ef..4a1ed59d7df 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -5795,7 +5795,7 @@ int Ordered_key::cmp_keys_by_row_data(ha_rows a, ha_rows b) { uchar *rowid_a, *rowid_b; - int __attribute__((unused)) error; + int error; int cmp_res; /* The length in bytes of the rowids (positions) of tmp_table. */ uint rowid_length= tbl->file->ref_length; @@ -5892,7 +5892,7 @@ int Ordered_key::cmp_key_with_search_key(rownum_t row_num) /* The length in bytes of the rowids (positions) of tmp_table. */ uint rowid_length= tbl->file->ref_length; uchar *cur_rowid= row_num_to_rowid + row_num * rowid_length; - int __attribute__((unused)) error; + int error; int cmp_res; if (unlikely((error= tbl->file->ha_rnd_pos(tbl->record[0], cur_rowid)))) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 1c7010ebb81..dc0aaad7868 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -2501,7 +2501,7 @@ int JOIN::optimize_stage2() { JOIN_TAB *tab= &join_tab[const_tables]; - if (order) + if (order && !need_tmp) { /* Force using of tmp table if sorting by a SP or UDF function due to @@ -3206,7 +3206,7 @@ bool JOIN::make_aggr_tables_info() or end_write_group()) if JOIN::group is set to false. */ // the temporary table was explicitly requested - DBUG_ASSERT(MY_TEST(select_options & OPTION_BUFFER_RESULT)); + DBUG_ASSERT(select_options & OPTION_BUFFER_RESULT); // the temporary table does not have a grouping expression DBUG_ASSERT(!curr_tab->table->group); } @@ -13191,7 +13191,8 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond, tab++) tab->cached_eq_ref_table= FALSE; - *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1; + JOIN_TAB *head= join->join_tab + join->const_tables; + *simple_order= head->on_expr_ref[0] == NULL; } else { diff --git a/sql/sql_sort.h b/sql/sql_sort.h index c29bf1440c9..231bc93ce75 100644 --- a/sql/sql_sort.h +++ b/sql/sql_sort.h @@ -92,7 +92,6 @@ public: memset(this, 0, sizeof(*this)); } void init_for_filesort(uint sortlen, TABLE *table, - ulong max_length_for_sort_data, ha_rows maxrows, bool sort_positions); };