From a74e8d36dde934b72b230b6d50b89f8e1ec034ae Mon Sep 17 00:00:00 2001
From: Igor Babaev <igor@askmonty.org>
Date: Fri, 18 Mar 2016 10:52:02 -0700
Subject: [PATCH] For some window functions an order list must be present.

---
 mysql-test/r/win.result   | 11 +++++++++++
 mysql-test/t/win.test     | 13 +++++++++++++
 sql/item_windowfunc.cc    |  5 +++++
 sql/item_windowfunc.h     | 13 +++++++++++++
 sql/share/errmsg-utf8.txt |  2 ++
 5 files changed, 44 insertions(+)

diff --git a/mysql-test/r/win.result b/mysql-test/r/win.result
index cb94c84b8a5..2bfd3d469d4 100644
--- a/mysql-test/r/win.result
+++ b/mysql-test/r/win.result
@@ -470,6 +470,17 @@ dense_rank() over (partition by c order by pk
 rows between 1 preceding and 1 following) as r
 from t1;
 ERROR HY000: Window frame is not allowed with 'dense_rank'
+select 
+pk, c, 
+rank() over w1 as r
+from t1
+window w1 as (partition by c);
+ERROR HY000: No order list in window specification for 'rank'
+select 
+pk, c, 
+dense_rank() over (partition by c) as r
+from t1;
+ERROR HY000: No order list in window specification for 'dense_rank'
 drop table t0,t1;
 #
 # MDEV-9634: Window function produces incorrect value
diff --git a/mysql-test/t/win.test b/mysql-test/t/win.test
index d2dcce57a87..1b527a9c22e 100644
--- a/mysql-test/t/win.test
+++ b/mysql-test/t/win.test
@@ -311,6 +311,19 @@ select
                     rows between 1 preceding and 1 following) as r
 from t1;
 
+--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
+select 
+  pk, c, 
+  rank() over w1 as r
+from t1
+window w1 as (partition by c);
+
+--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
+select 
+  pk, c, 
+  dense_rank() over (partition by c) as r
+from t1;
+
 drop table t0,t1;
 
 --echo #
diff --git a/sql/item_windowfunc.cc b/sql/item_windowfunc.cc
index 7f668ab8fab..251b98d3817 100644
--- a/sql/item_windowfunc.cc
+++ b/sql/item_windowfunc.cc
@@ -59,6 +59,11 @@ Item_window_func::fix_fields(THD *thd, Item **ref)
     return true;
   }
 
+  if (window_spec->order_list->elements == 0 && is_order_list_mandatory())
+  {
+    my_error(ER_NO_ORDER_LIST_IN_WINDOW_SPEC, MYF(0), window_func->func_name());
+    return true;
+  }
   /*
     TODO: why the last parameter is 'ref' in this call? What if window_func
     decides to substitute itself for something else and does *ref=.... ? 
diff --git a/sql/item_windowfunc.h b/sql/item_windowfunc.h
index 4e0ba7fc43b..983f196a86c 100644
--- a/sql/item_windowfunc.h
+++ b/sql/item_windowfunc.h
@@ -448,6 +448,19 @@ class Item_window_func : public Item_result_field
     default: 
       return false;
     }
+  }
+
+  bool is_order_list_mandatory()
+  {
+    switch (window_func->sum_func()) {
+    case Item_sum::RANK_FUNC:
+    case Item_sum::DENSE_RANK_FUNC:
+    case Item_sum::PERCENT_RANK_FUNC:
+    case Item_sum::CUME_DIST_FUNC:
+      return true;
+    default: 
+      return false;
+    }
   }  
 
   /*
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index ec11075a498..9e868fcafdf 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -7150,6 +7150,8 @@ ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
         eng "Unacceptable combination of window frame bound specifications"
 ER_NOT_ALLOWED_WINDOW_FRAME
         eng "Window frame is not allowed with '%s'"
+ER_NO_ORDER_LIST_IN_WINDOW_SPEC
+        eng "No order list in window specification for '%s'"
 ER_RANGE_FRAME_NEEDS_SIMPLE_ORDERBY
         eng "RANGE-type frame requires ORDER BY clause with single sort key"
 ER_WRONG_TYPE_FOR_ROWS_FRAME
-- 
2.30.9