From 1d0b44394aff4f474570e38f69efd5204fac2e13 Mon Sep 17 00:00:00 2001 From: "hope.lb" Date: Fri, 18 Aug 2023 19:42:07 +0800 Subject: [PATCH] Temptable field generated from Item_rollup_sum_switcher has the wrong name Description ----------- When field name is indicated to derived table, the item_name has been modified to Item_rollup_sum_switcher, but not the hidden aggregation Item. But it calls the hidden item's create_tmp_field function which causes a wrong item_name is given to field. Fix --- Before calling Item_rollup_sum_switcher::create_tmp_field, set the hidden Item's item_name to the switcher's name and restore it after creation. --- sql/item_sum.cc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sql/item_sum.cc b/sql/item_sum.cc index eb119a940e8..286e8debe07 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -6351,7 +6351,25 @@ void Item_rollup_sum_switcher::print(const THD *thd, String *str, } Field *Item_rollup_sum_switcher::create_tmp_field(bool group, TABLE *table) { - return master()->create_tmp_field(group, table); + bool item_name_diff; + const char *saved_str; + size_t saved_length; + + /* + The wrapper's item name could be modified as required. When generating temp + table field, the field name should keep the same for locating field. + */ + if ((item_name_diff = !item_name.eq_safe(master()->item_name))) { + saved_str = master()->item_name.ptr(); + saved_length = master()->item_name.length(); + master()->item_name.set(item_name.ptr(), item_name.length()); + } + + Field *tmp_field = master()->create_tmp_field(group, table); + + if (item_name_diff) master()->item_name.set(saved_str, saved_length); + + return tmp_field; } void Item_rollup_sum_switcher::clear() { -- 2.19.1.6.gb485710b