diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result index 2a4f6408377..c84c98c6032 100644 --- a/mysql-test/r/win.result +++ b/mysql-test/r/win.result @@ -2521,3 +2521,27 @@ id rnk 2 2 drop view v1; drop table t1; +# +# MDEV-11138: window function in the query without tables +# +select row_number() over (); +row_number() over () +1 +select count(*) over (); +count(*) over () +1 +select sum(5) over (); +sum(5) over () +5 +select row_number() over (), sum(5) over (); +row_number() over () sum(5) over () +1 5 +select row_number() over (order by 2); +row_number() over (order by 2) +1 +select row_number() over (partition by 2); +row_number() over (partition by 2) +1 +select row_number() over (partition by 4 order by 1+2); +row_number() over (partition by 4 order by 1+2) +1 diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test index 15163f3a1d7..430304e80e6 100644 --- a/mysql-test/t/win.test +++ b/mysql-test/t/win.test @@ -1535,3 +1535,15 @@ select * from v1; drop view v1; drop table t1; + +--echo # +--echo # MDEV-11138: window function in the query without tables +--echo # + +select row_number() over (); +select count(*) over (); +select sum(5) over (); +select row_number() over (), sum(5) over (); +select row_number() over (order by 2); +select row_number() over (partition by 2); +select row_number() over (partition by 4 order by 1+2); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 4b652ff74ee..10e03413eca 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1439,6 +1439,13 @@ JOIN::optimize_inner() { DBUG_PRINT("info",("No tables")); error= 0; + if (select_lex->have_window_funcs()) + { + if (!join_tab && + !(join_tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)))) + DBUG_RETURN(1); + need_tmp= 1; + } if (make_aggr_tables_info()) DBUG_RETURN(1); goto setup_subq_exit; @@ -2186,7 +2193,7 @@ bool JOIN::make_aggr_tables_info() Setup last table to provide fields and all_fields lists to the next node in the plan. */ - if (join_tab) + if (join_tab && top_join_tab_count) { join_tab[top_join_tab_count - 1].fields= &fields_list; join_tab[top_join_tab_count - 1].all_fields= &all_fields; @@ -2310,7 +2317,8 @@ bool JOIN::make_aggr_tables_info() single table queries, thus it is sufficient to test only the first join_tab element of the plan for its access method. */ - if (join_tab && join_tab->is_using_loose_index_scan()) + if (join_tab && top_join_tab_count && + join_tab->is_using_loose_index_scan()) tmp_table_param.precomputed_group_by= !join_tab->is_using_agg_loose_index_scan(); @@ -2770,7 +2778,7 @@ JOIN::create_postjoin_aggr_table(JOIN_TAB *tab, List *table_fields, tmp_table_param.using_outer_summary_function= tab->tmp_table_param->using_outer_summary_function; tab->join= this; - DBUG_ASSERT(tab > tab->join->join_tab); + DBUG_ASSERT(tab > tab->join->join_tab || !top_join_tab_count); (tab - 1)->next_select= sub_select_postjoin_aggr; tab->aggr= new (thd->mem_root) AGGR_OP(tab); if (!tab->aggr) @@ -3424,7 +3432,6 @@ JOIN::destroy() if (join_tab) { - DBUG_ASSERT(table_count+aggr_tables > 0); for (JOIN_TAB *tab= first_linear_tab(this, WITH_BUSH_ROOTS, WITH_CONST_TABLES); tab; tab= next_linear_tab(this, tab, WITH_BUSH_ROOTS)) diff --git a/sql/sql_window.cc b/sql/sql_window.cc index f49cc945504..e9e531714f6 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -2801,6 +2801,11 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel, sort_order= order; } filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL); + if (!join_tab->join->top_join_tab_count) + { + filesort->tracker= + new (thd->mem_root) Filesort_tracker(thd->lex->analyze_stmt); + } /* Apply the same condition that the subsequent sort has. */ filesort->select= sel;