summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicențiu Ciorbaru <vicentiu@mariadb.org>2016-04-13 10:39:06 +0200
committerVicențiu Ciorbaru <vicentiu@mariadb.org>2016-04-13 10:39:06 +0200
commit3dd08a11de23455182f88c524c2e0b0eb862cc60 (patch)
tree19fea22ff565fd7f1c874ce8ec2aa0383e4946e2
parentb532be9f8ce4872be4a3f1ef2fa296a1de347859 (diff)
downloadmariadb-git-3dd08a11de23455182f88c524c2e0b0eb862cc60.tar.gz
Fix another bug in dense_rank.
When ordering by a column and partitioning by another, we must reset the peer_tracker for dense_rank, regardless if the value for the order column changes or not. Example: select a, b, dense_rank() over (partition by b order by a) a | b | dense_rank ---------------------- 1 | p1 | 1 2 | p1 | 2 2 | p2 | 1 // Here, without this fix we returned 0. 2 | p2 | 2 // And 1 here.
-rw-r--r--mysql-test/r/win_rank.result10
-rw-r--r--sql/item_windowfunc.cc5
-rw-r--r--sql/item_windowfunc.h6
3 files changed, 13 insertions, 8 deletions
diff --git a/mysql-test/r/win_rank.result b/mysql-test/r/win_rank.result
index 54f70147b1a..725683d3869 100644
--- a/mysql-test/r/win_rank.result
+++ b/mysql-test/r/win_rank.result
@@ -40,11 +40,11 @@ pk a b rank dense_rank
3 1 10 3 2
4 1 10 3 2
8 2 10 5 3
-5 2 20 1 0
-6 2 20 1 0
-7 2 20 1 0
-9 4 20 4 1
-10 4 20 4 1
+5 2 20 1 1
+6 2 20 1 1
+7 2 20 1 1
+9 4 20 4 2
+10 4 20 4 2
drop table t1;
#
# Test with null values in the table.
diff --git a/sql/item_windowfunc.cc b/sql/item_windowfunc.cc
index c8f398577b5..d157d545dad 100644
--- a/sql/item_windowfunc.cc
+++ b/sql/item_windowfunc.cc
@@ -185,8 +185,11 @@ void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec)
bool Item_sum_dense_rank::add()
{
- if (peer_tracker.check_if_next_group())
+ if (peer_tracker.check_if_next_group() || first_add)
+ {
+ first_add= false;
dense_rank++;
+ }
return false;
}
diff --git a/sql/item_windowfunc.h b/sql/item_windowfunc.h
index 35fff511e5a..8da98b93c72 100644
--- a/sql/item_windowfunc.h
+++ b/sql/item_windowfunc.h
@@ -213,7 +213,9 @@ public:
class Item_sum_dense_rank: public Item_sum_int
{
longlong dense_rank;
+ bool first_add;
Group_bound_tracker peer_tracker;
+ public:
/*
XXX(cvicentiu) This class could potentially be implemented in the rank
class, with a switch for the DENSE case.
@@ -221,6 +223,7 @@ class Item_sum_dense_rank: public Item_sum_int
void clear()
{
dense_rank= 0;
+ first_add= true;
}
bool add();
void update_field() {}
@@ -229,9 +232,8 @@ class Item_sum_dense_rank: public Item_sum_int
return dense_rank;
}
- public:
Item_sum_dense_rank(THD *thd)
- : Item_sum_int(thd), dense_rank(0) {}
+ : Item_sum_int(thd), dense_rank(0), first_add(true) {}
enum Sumfunctype sum_func () const
{
return DENSE_RANK_FUNC;