Commit 9a673e0a authored by Vicențiu Ciorbaru's avatar Vicențiu Ciorbaru

Implement DENSE_RANK function.

parent c30119ad
...@@ -92,3 +92,46 @@ pk a rank() over (order by a) ...@@ -92,3 +92,46 @@ pk a rank() over (order by a)
9 4 9 9 4 9
10 4 9 10 4 9
drop table t2; drop table t2;
#
# Try DENSE_RANK() function
#
create table t3 (
pk int primary key,
a int,
b int
);
insert into t3 values
( 1 , 0, 10),
( 2 , 0, 10),
( 3 , 1, 10),
( 4 , 1, 10),
( 8 , 2, 10),
( 5 , 2, 20),
( 6 , 2, 20),
( 7 , 2, 20),
( 9 , 4, 20),
(10 , 4, 20);
select pk, a, b, rank() over (order by a), dense_rank() over (order by a) from t3;
pk a b rank() over (order by a) dense_rank() over (order by a)
1 0 10 1 1
2 0 10 1 1
3 1 10 3 2
4 1 10 3 2
8 2 10 5 3
5 2 20 5 3
6 2 20 5 3
7 2 20 5 3
9 4 20 9 4
10 4 20 9 4
select pk, a, b, rank() over (partition by b order by a), dense_rank() over (partition by b order by a) from t3;
pk a b rank() over (partition by b order by a) dense_rank() over (partition by b order by a)
1 0 10 1 1
2 0 10 1 1
3 1 10 3 2
4 1 10 3 2
8 2 10 5 3
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
...@@ -85,3 +85,29 @@ select pk, a, rank() over (order by a) from t2; ...@@ -85,3 +85,29 @@ select pk, a, rank() over (order by a) from t2;
drop table t2; drop table t2;
--echo #
--echo # Try DENSE_RANK() function
--echo #
create table t3 (
pk int primary key,
a int,
b int
);
insert into t3 values
( 1 , 0, 10),
( 2 , 0, 10),
( 3 , 1, 10),
( 4 , 1, 10),
( 8 , 2, 10),
( 5 , 2, 20),
( 6 , 2, 20),
( 7 , 2, 20),
( 9 , 4, 20),
(10 , 4, 20);
select pk, a, b, rank() over (order by a), dense_rank() over (order by a) from t3;
select pk, a, b, rank() over (partition by b order by a), dense_rank() over (partition by b order by a) from t3;
drop table tr;
...@@ -55,6 +55,24 @@ void Item_sum_rank::setup_window_func(THD *thd, Window_spec *window_spec) ...@@ -55,6 +55,24 @@ void Item_sum_rank::setup_window_func(THD *thd, Window_spec *window_spec)
clear(); clear();
} }
void Item_sum_dense_rank::setup_window_func(THD *thd, Window_spec *window_spec)
{
/* TODO: consider moving this && Item_sum_rank's implementation */
for (ORDER *curr = window_spec->order_list.first; curr; curr=curr->next) {
Cached_item *tmp= new_Cached_item(thd, curr->item[0], TRUE);
orderby_fields.push_back(tmp);
}
clear();
}
bool Item_sum_dense_rank::add()
{
if (test_if_group_changed(orderby_fields) > -1)
dense_rank++;
return false;
}
bool Item_sum_rank::add() bool Item_sum_rank::add()
{ {
......
...@@ -135,7 +135,7 @@ class Item_sum_rank: public Item_sum_int ...@@ -135,7 +135,7 @@ class Item_sum_rank: public Item_sum_int
/* /*
RANK() OVER (...) Windowing function DENSE_RANK() OVER (...) Windowing function
@detail @detail
- This is a Window function (not just an aggregate) - This is a Window function (not just an aggregate)
...@@ -156,11 +156,16 @@ class Item_sum_rank: public Item_sum_int ...@@ -156,11 +156,16 @@ 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;
List<Cached_item> orderby_fields;
/* TODO: implementation is missing */ /* TODO: implementation is missing */
void clear() {} void clear() {
bool add() { return false; } dense_rank= 1;
}
bool add();
void update_field() {} void update_field() {}
longlong val_int() {
return dense_rank;
}
public: public:
Item_sum_dense_rank(THD *thd) Item_sum_dense_rank(THD *thd)
...@@ -175,6 +180,8 @@ class Item_sum_dense_rank: public Item_sum_int ...@@ -175,6 +180,8 @@ class Item_sum_dense_rank: public Item_sum_int
return "dense_rank"; return "dense_rank";
} }
void setup_window_func(THD *thd, Window_spec *window_spec);
}; };
......
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