From 9d72244f218e7455eabb2e884ba0b6ac148b53ad Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Fri, 31 Jul 2026 14:18:08 -0400 Subject: [PATCH 1/2] Create failing test case --- mysql-test/main/mdev_19039.result | 14 ++++++++++++++ mysql-test/main/mdev_19039.test | 7 +++++++ 2 files changed, 21 insertions(+) create mode 100644 mysql-test/main/mdev_19039.result create mode 100644 mysql-test/main/mdev_19039.test diff --git a/mysql-test/main/mdev_19039.result b/mysql-test/main/mdev_19039.result new file mode 100644 index 0000000000000..7e8c24e8fb357 --- /dev/null +++ b/mysql-test/main/mdev_19039.result @@ -0,0 +1,14 @@ +CREATE TABLE t1 (i int); +INSERT INTO t1 VALUES (1), (1), (2), (2), (3), (3); +SELECT i, SUM(i) OVER () FROM t1 GROUP BY i ; +i SUM(i) OVER () +1 6 +2 6 +3 6 +SELECT i, SUM(i) OVER () FROM t1 GROUP BY i WITH ROLLUP; +i SUM(i) OVER () +NULL 6 +1 6 +2 6 +3 6 +DROP TABLE t1; diff --git a/mysql-test/main/mdev_19039.test b/mysql-test/main/mdev_19039.test new file mode 100644 index 0000000000000..837ce88bcc63e --- /dev/null +++ b/mysql-test/main/mdev_19039.test @@ -0,0 +1,7 @@ +CREATE TABLE t1 (i int); +INSERT INTO t1 VALUES (1), (1), (2), (2), (3), (3); + +SELECT i, SUM(i) OVER () FROM t1 GROUP BY i ; +SELECT i, SUM(i) OVER () FROM t1 GROUP BY i WITH ROLLUP; + +DROP TABLE t1; \ No newline at end of file From 84138096fb467f3acf5362ce97be578d70dadfbb Mon Sep 17 00:00:00 2001 From: Jaeheon Shim Date: Mon, 3 Aug 2026 00:15:31 -0400 Subject: [PATCH 2/2] In the rollup struct, store both all_fields and fields_list so that hidden fields can be replaced with Item_null_result so that they are correctly recorded in the temp table path (rollup_write_data) --- sql/sql_select.cc | 42 ++++++++++++++++++++++++------------------ sql/sql_select.h | 3 ++- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 8502fd93b40b7..759ff8d328135 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -30577,9 +30577,10 @@ bool JOIN::rollup_init() reinterpret_cast (thd->alloc((sizeof(Ref_ptr_array) + all_fields.elements * sizeof(Item*)) * send_group_parts)); - rollup.fields= thd->alloc >(send_group_parts); - - if (!null_items || !rollup.ref_pointer_arrays || !rollup.fields) + rollup.all_fields= thd->alloc>(send_group_parts); + rollup.fields_list= thd->alloc>(send_group_parts); + if (!null_items || !rollup.ref_pointer_arrays || !rollup.all_fields || + !rollup.fields_list) return true; ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts); @@ -30593,15 +30594,22 @@ bool JOIN::rollup_init() if (!(rollup.null_items[i]= new (thd->mem_root) Item_null_result(thd))) return true; - List *rollup_fields= &rollup.fields[i]; + List *rollup_fields= &rollup.all_fields[i]; rollup_fields->empty(); rollup.ref_pointer_arrays[i]= Ref_ptr_array(ref_array, all_fields.elements); ref_array+= all_fields.elements; } + uint hidden_fields= all_fields.elements - fields_list.elements; for (i= 0 ; i < send_group_parts; i++) { - for (j=0 ; j < fields_list.elements ; j++) - rollup.fields[i].push_back(rollup.null_items[i], thd->mem_root); + List_iterator_fast it(rollup.all_fields[i]); + for (j= 0; j < all_fields.elements; j++) + { + rollup.all_fields[i].push_back(rollup.null_items[i], thd->mem_root); + if (j < hidden_fields) + it++; + } + it.sublist(rollup.fields_list[i], fields_list.elements); } List_iterator it(all_fields); Item *item; @@ -30740,7 +30748,7 @@ bool JOIN::rollup_make_fields(List &fields_arg, List &sel_fields, uint pos= send_group_parts - level -1; bool real_fields= 0; Item *item; - List_iterator new_it(rollup.fields[pos]); + List_iterator new_it(rollup.all_fields[pos]); Ref_ptr_array ref_array_start= rollup.ref_pointer_arrays[pos]; ORDER *start_group; @@ -30790,8 +30798,8 @@ bool JOIN::rollup_make_fields(List &fields_arg, List &sel_fields, for (group_tmp= start_group, i= pos ; group_tmp ; group_tmp= group_tmp->next, i++) { - if (*group_tmp->item == item) - { + if (item->eq(*group_tmp->item, 0)) + { /* This is an element that is used by the GROUP BY and should be set to NULL in this level @@ -30808,12 +30816,10 @@ bool JOIN::rollup_make_fields(List &fields_arg, List &sel_fields, } } ref_array_start[ref_array_ix]= item; + (void) new_it++; // Point to next item + new_it.replace(item); // Replace previous if (real_fields) - { - (void) new_it++; // Point to next item - new_it.replace(item); // Replace previous - ref_array_ix++; - } + ref_array_ix++; else ref_array_ix--; } @@ -30852,9 +30858,9 @@ int JOIN::rollup_send_data(uint idx) if ((!having || having->val_bool())) { if (send_records < unit->lim.get_select_limit() && do_send_rows && - (res= result->send_data_with_check(rollup.fields[i], - unit, send_records)) > 0) - return 1; + (res= result->send_data_with_check(rollup.fields_list[i], unit, + send_records)) > 0) + return 1; if (!res) send_records++; } @@ -30896,7 +30902,7 @@ int JOIN::rollup_write_data(uint idx, TMP_TABLE_PARAM *tmp_table_param_arg, { int write_error; Item *item; - List_iterator_fast it(rollup.fields[i]); + List_iterator_fast it(rollup.all_fields[i]); while ((item= it++)) { if (item->type() == Item::NULL_ITEM && item->is_result_field()) diff --git a/sql/sql_select.h b/sql/sql_select.h index 23a927bfcb14e..74d646c705a46 100644 --- a/sql/sql_select.h +++ b/sql/sql_select.h @@ -1390,7 +1390,8 @@ typedef struct st_rollup State state; Item_null_array null_items; Ref_ptr_array *ref_pointer_arrays; - List *fields; + List *all_fields; + List *fields_list; } ROLLUP;