Skip to content

Commit a74e8d3

Browse files
committed
For some window functions an order list must be present.
1 parent 13f9535 commit a74e8d3

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

mysql-test/r/win.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,17 @@ dense_rank() over (partition by c order by pk
470470
rows between 1 preceding and 1 following) as r
471471
from t1;
472472
ERROR HY000: Window frame is not allowed with 'dense_rank'
473+
select
474+
pk, c,
475+
rank() over w1 as r
476+
from t1
477+
window w1 as (partition by c);
478+
ERROR HY000: No order list in window specification for 'rank'
479+
select
480+
pk, c,
481+
dense_rank() over (partition by c) as r
482+
from t1;
483+
ERROR HY000: No order list in window specification for 'dense_rank'
473484
drop table t0,t1;
474485
#
475486
# MDEV-9634: Window function produces incorrect value

mysql-test/t/win.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,19 @@ select
311311
rows between 1 preceding and 1 following) as r
312312
from t1;
313313

314+
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
315+
select
316+
pk, c,
317+
rank() over w1 as r
318+
from t1
319+
window w1 as (partition by c);
320+
321+
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
322+
select
323+
pk, c,
324+
dense_rank() over (partition by c) as r
325+
from t1;
326+
314327
drop table t0,t1;
315328

316329
--echo #

sql/item_windowfunc.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ Item_window_func::fix_fields(THD *thd, Item **ref)
5959
return true;
6060
}
6161

62+
if (window_spec->order_list->elements == 0 && is_order_list_mandatory())
63+
{
64+
my_error(ER_NO_ORDER_LIST_IN_WINDOW_SPEC, MYF(0), window_func->func_name());
65+
return true;
66+
}
6267
/*
6368
TODO: why the last parameter is 'ref' in this call? What if window_func
6469
decides to substitute itself for something else and does *ref=.... ?

sql/item_windowfunc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,19 @@ class Item_window_func : public Item_result_field
448448
default:
449449
return false;
450450
}
451+
}
452+
453+
bool is_order_list_mandatory()
454+
{
455+
switch (window_func->sum_func()) {
456+
case Item_sum::RANK_FUNC:
457+
case Item_sum::DENSE_RANK_FUNC:
458+
case Item_sum::PERCENT_RANK_FUNC:
459+
case Item_sum::CUME_DIST_FUNC:
460+
return true;
461+
default:
462+
return false;
463+
}
451464
}
452465

453466
/*

sql/share/errmsg-utf8.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7150,6 +7150,8 @@ ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
71507150
eng "Unacceptable combination of window frame bound specifications"
71517151
ER_NOT_ALLOWED_WINDOW_FRAME
71527152
eng "Window frame is not allowed with '%s'"
7153+
ER_NO_ORDER_LIST_IN_WINDOW_SPEC
7154+
eng "No order list in window specification for '%s'"
71537155
ER_RANGE_FRAME_NEEDS_SIMPLE_ORDERBY
71547156
eng "RANGE-type frame requires ORDER BY clause with single sort key"
71557157
ER_WRONG_TYPE_FOR_ROWS_FRAME

0 commit comments

Comments
 (0)