Skip to content

Commit 3dd08a1

Browse files
committed
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.
1 parent b532be9 commit 3dd08a1

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

mysql-test/r/win_rank.result

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ pk a b rank dense_rank
4040
3 1 10 3 2
4141
4 1 10 3 2
4242
8 2 10 5 3
43-
5 2 20 1 0
44-
6 2 20 1 0
45-
7 2 20 1 0
46-
9 4 20 4 1
47-
10 4 20 4 1
43+
5 2 20 1 1
44+
6 2 20 1 1
45+
7 2 20 1 1
46+
9 4 20 4 2
47+
10 4 20 4 2
4848
drop table t1;
4949
#
5050
# Test with null values in the table.

sql/item_windowfunc.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,11 @@ void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec)
185185

186186
bool Item_sum_dense_rank::add()
187187
{
188-
if (peer_tracker.check_if_next_group())
188+
if (peer_tracker.check_if_next_group() || first_add)
189+
{
190+
first_add= false;
189191
dense_rank++;
192+
}
190193

191194
return false;
192195
}

sql/item_windowfunc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,17 @@ class Item_sum_rank: public Item_sum_int
213213
class Item_sum_dense_rank: public Item_sum_int
214214
{
215215
longlong dense_rank;
216+
bool first_add;
216217
Group_bound_tracker peer_tracker;
218+
public:
217219
/*
218220
XXX(cvicentiu) This class could potentially be implemented in the rank
219221
class, with a switch for the DENSE case.
220222
*/
221223
void clear()
222224
{
223225
dense_rank= 0;
226+
first_add= true;
224227
}
225228
bool add();
226229
void update_field() {}
@@ -229,9 +232,8 @@ class Item_sum_dense_rank: public Item_sum_int
229232
return dense_rank;
230233
}
231234

232-
public:
233235
Item_sum_dense_rank(THD *thd)
234-
: Item_sum_int(thd), dense_rank(0) {}
236+
: Item_sum_int(thd), dense_rank(0), first_add(true) {}
235237
enum Sumfunctype sum_func () const
236238
{
237239
return DENSE_RANK_FUNC;

0 commit comments

Comments
 (0)