Skip to content

Commit e51b015

Browse files
committed
Fixed bug mdev-11138.
Supported usage of expressions with window functions in SELECTs without tables.
1 parent 20aae56 commit e51b015

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

mysql-test/r/win.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,3 +2521,27 @@ id rnk
25212521
2 2
25222522
drop view v1;
25232523
drop table t1;
2524+
#
2525+
# MDEV-11138: window function in the query without tables
2526+
#
2527+
select row_number() over ();
2528+
row_number() over ()
2529+
1
2530+
select count(*) over ();
2531+
count(*) over ()
2532+
1
2533+
select sum(5) over ();
2534+
sum(5) over ()
2535+
5
2536+
select row_number() over (), sum(5) over ();
2537+
row_number() over () sum(5) over ()
2538+
1 5
2539+
select row_number() over (order by 2);
2540+
row_number() over (order by 2)
2541+
1
2542+
select row_number() over (partition by 2);
2543+
row_number() over (partition by 2)
2544+
1
2545+
select row_number() over (partition by 4 order by 1+2);
2546+
row_number() over (partition by 4 order by 1+2)
2547+
1

mysql-test/t/win.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,3 +1535,15 @@ select * from v1;
15351535

15361536
drop view v1;
15371537
drop table t1;
1538+
1539+
--echo #
1540+
--echo # MDEV-11138: window function in the query without tables
1541+
--echo #
1542+
1543+
select row_number() over ();
1544+
select count(*) over ();
1545+
select sum(5) over ();
1546+
select row_number() over (), sum(5) over ();
1547+
select row_number() over (order by 2);
1548+
select row_number() over (partition by 2);
1549+
select row_number() over (partition by 4 order by 1+2);

sql/sql_select.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,13 @@ JOIN::optimize_inner()
14391439
{
14401440
DBUG_PRINT("info",("No tables"));
14411441
error= 0;
1442+
if (select_lex->have_window_funcs())
1443+
{
1444+
if (!join_tab &&
1445+
!(join_tab= (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB))))
1446+
DBUG_RETURN(1);
1447+
need_tmp= 1;
1448+
}
14421449
if (make_aggr_tables_info())
14431450
DBUG_RETURN(1);
14441451
goto setup_subq_exit;
@@ -2186,7 +2193,7 @@ bool JOIN::make_aggr_tables_info()
21862193
Setup last table to provide fields and all_fields lists to the next
21872194
node in the plan.
21882195
*/
2189-
if (join_tab)
2196+
if (join_tab && top_join_tab_count)
21902197
{
21912198
join_tab[top_join_tab_count - 1].fields= &fields_list;
21922199
join_tab[top_join_tab_count - 1].all_fields= &all_fields;
@@ -2310,7 +2317,8 @@ bool JOIN::make_aggr_tables_info()
23102317
single table queries, thus it is sufficient to test only the first
23112318
join_tab element of the plan for its access method.
23122319
*/
2313-
if (join_tab && join_tab->is_using_loose_index_scan())
2320+
if (join_tab && top_join_tab_count &&
2321+
join_tab->is_using_loose_index_scan())
23142322
tmp_table_param.precomputed_group_by=
23152323
!join_tab->is_using_agg_loose_index_scan();
23162324

@@ -2770,7 +2778,7 @@ JOIN::create_postjoin_aggr_table(JOIN_TAB *tab, List<Item> *table_fields,
27702778
tmp_table_param.using_outer_summary_function=
27712779
tab->tmp_table_param->using_outer_summary_function;
27722780
tab->join= this;
2773-
DBUG_ASSERT(tab > tab->join->join_tab);
2781+
DBUG_ASSERT(tab > tab->join->join_tab || !top_join_tab_count);
27742782
(tab - 1)->next_select= sub_select_postjoin_aggr;
27752783
tab->aggr= new (thd->mem_root) AGGR_OP(tab);
27762784
if (!tab->aggr)
@@ -3424,7 +3432,6 @@ JOIN::destroy()
34243432

34253433
if (join_tab)
34263434
{
3427-
DBUG_ASSERT(table_count+aggr_tables > 0);
34283435
for (JOIN_TAB *tab= first_linear_tab(this, WITH_BUSH_ROOTS,
34293436
WITH_CONST_TABLES);
34303437
tab; tab= next_linear_tab(this, tab, WITH_BUSH_ROOTS))

sql/sql_window.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,11 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
28012801
sort_order= order;
28022802
}
28032803
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL);
2804+
if (!join_tab->join->top_join_tab_count)
2805+
{
2806+
filesort->tracker=
2807+
new (thd->mem_root) Filesort_tracker(thd->lex->analyze_stmt);
2808+
}
28042809

28052810
/* Apply the same condition that the subsequent sort has. */
28062811
filesort->select= sel;

0 commit comments

Comments
 (0)