Commit 3dd08a11 authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru

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.
parent b532be9f
...@@ -40,11 +40,11 @@ pk a b rank dense_rank ...@@ -40,11 +40,11 @@ pk a b rank dense_rank
3 1 10 3 2 3 1 10 3 2
4 1 10 3 2 4 1 10 3 2
8 2 10 5 3 8 2 10 5 3
5 2 20 1 0 5 2 20 1 1
6 2 20 1 0 6 2 20 1 1
7 2 20 1 0 7 2 20 1 1
9 4 20 4 1 9 4 20 4 2
10 4 20 4 1 10 4 20 4 2
drop table t1; drop table t1;
# #
# Test with null values in the table. # Test with null values in the table.
......
...@@ -185,8 +185,11 @@ void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec) ...@@ -185,8 +185,11 @@ void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec)
bool Item_sum_dense_rank::add() 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++; dense_rank++;
}
return false; return false;
} }
......
...@@ -213,7 +213,9 @@ class Item_sum_rank: public Item_sum_int ...@@ -213,7 +213,9 @@ class Item_sum_rank: public Item_sum_int
class Item_sum_dense_rank: public Item_sum_int class Item_sum_dense_rank: public Item_sum_int
{ {
longlong dense_rank; longlong dense_rank;
bool first_add;
Group_bound_tracker peer_tracker; Group_bound_tracker peer_tracker;
public:
/* /*
XXX(cvicentiu) This class could potentially be implemented in the rank XXX(cvicentiu) This class could potentially be implemented in the rank
class, with a switch for the DENSE case. class, with a switch for the DENSE case.
...@@ -221,6 +223,7 @@ class Item_sum_dense_rank: public Item_sum_int ...@@ -221,6 +223,7 @@ class Item_sum_dense_rank: public Item_sum_int
void clear() void clear()
{ {
dense_rank= 0; dense_rank= 0;
first_add= true;
} }
bool add(); bool add();
void update_field() {} void update_field() {}
...@@ -229,9 +232,8 @@ class Item_sum_dense_rank: public Item_sum_int ...@@ -229,9 +232,8 @@ class Item_sum_dense_rank: public Item_sum_int
return dense_rank; return dense_rank;
} }
public:
Item_sum_dense_rank(THD *thd) 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 enum Sumfunctype sum_func () const
{ {
return DENSE_RANK_FUNC; return DENSE_RANK_FUNC;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment