diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index dfb9580721a..c0f639b55ec 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -2441,6 +2441,7 @@ void st_select_lex::init_query() m_custom_agg_func_used= false; window_specs.empty(); window_funcs.empty(); + is_win_spec_list_built= false; tvc= 0; in_tvc= false; versioned_tables= 0; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index d47f196d679..2e0b3604244 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1530,6 +1530,7 @@ public: bool no_to_clones); List window_specs; + bool is_win_spec_list_built; void prepare_add_window_spec(THD *thd); bool add_window_def(THD *thd, LEX_CSTRING *win_name, LEX_CSTRING *win_ref, SQL_I_List win_partition_list, diff --git a/sql/sql_window.cc b/sql/sql_window.cc index df43efe18a4..7677c3e9113 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -207,27 +207,33 @@ setup_windows(THD *thd, Ref_ptr_array ref_pointer_array, TABLE_LIST *tables, DBUG_ENTER("setup_windows"); List_iterator it(win_specs); - /* - Move all unnamed specifications after the named ones. - We could have avoided it if we had built two separate lists for - named and unnamed specifications. - */ - Query_arena *arena, backup; - arena= thd->activate_stmt_arena_if_needed(&backup); - uint i = 0; - uint elems= win_specs.elements; - while ((win_spec= it++) && i++ < elems) + if (!thd->lex->current_select->is_win_spec_list_built) { - if (win_spec->name() == NULL) - { - it.remove(); - win_specs.push_back(win_spec); - } - } - if (arena) - thd->restore_active_arena(arena, &backup); - it.rewind(); + /* + Move all unnamed specifications after the named ones. + We could have avoided it if we had built two separate lists for + named and unnamed specifications. + */ + Query_arena *arena, backup; + arena= thd->activate_stmt_arena_if_needed(&backup); + uint i = 0; + uint elems= win_specs.elements; + while ((win_spec= it++) && i++ < elems) + { + if (win_spec->name() == NULL) + { + it.remove(); + win_specs.push_back(win_spec); + } + } + if (arena) + thd->restore_active_arena(arena, &backup); + + it.rewind(); + + thd->lex->current_select->is_win_spec_list_built= true; + } List_iterator_fast itp(win_specs);