Skip to content

Commit ce8a0d8

Browse files
committed
MDEV-9676: RANGE-type frames for window functions
- Handle ORDER BY DESC in window definitions. - Fix an issue in Frame_range_current_row_top
1 parent b8d8d9b commit ce8a0d8

File tree

3 files changed

+208
-20
lines changed

3 files changed

+208
-20
lines changed

mysql-test/r/win.result

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ pk a rank() over (order by a)
9191
7 2 5
9292
9 4 9
9393
10 4 9
94+
select pk, a, rank() over (order by a desc) from t2;
95+
pk a rank() over (order by a desc)
96+
1 0 9
97+
2 0 9
98+
3 1 7
99+
4 1 7
100+
8 2 3
101+
5 2 3
102+
6 2 3
103+
7 2 3
104+
9 4 1
105+
10 4 1
94106
drop table t2;
95107
#
96108
# Try DENSE_RANK() function
@@ -252,6 +264,23 @@ pk c CNT
252264
8 2 2
253265
9 2 2
254266
10 2 1
267+
# Check ORDER BY DESC
268+
select
269+
pk, c,
270+
count(*) over (partition by c order by pk desc
271+
rows between 2 preceding and 2 following) as CNT
272+
from t1;
273+
pk c CNT
274+
1 1 3
275+
2 1 4
276+
3 1 4
277+
4 1 3
278+
5 2 3
279+
6 2 4
280+
7 2 5
281+
8 2 5
282+
9 2 4
283+
10 2 3
255284
drop table t0,t1;
256285
#
257286
# Resolution of window names
@@ -817,6 +846,22 @@ pk a cnt
817846
9 72 9
818847
select
819848
pk, a,
849+
count(a) over (ORDER BY a DESC
850+
RANGE BETWEEN UNBOUNDED PRECEDING
851+
AND 10 FOLLOWING) as cnt
852+
from t1;
853+
pk a cnt
854+
1 1 9
855+
2 2 9
856+
3 4 9
857+
4 8 9
858+
5 26 5
859+
6 27 5
860+
7 40 3
861+
8 71 2
862+
9 72 2
863+
select
864+
pk, a,
820865
count(a) over (ORDER BY a
821866
RANGE BETWEEN UNBOUNDED PRECEDING
822867
AND 1 FOLLOWING) as cnt
@@ -849,6 +894,22 @@ pk a cnt
849894
9 72 7
850895
select
851896
pk, a,
897+
count(a) over (ORDER BY a DESC
898+
RANGE BETWEEN UNBOUNDED PRECEDING
899+
AND 10 PRECEDING) as cnt
900+
from t1;
901+
pk a cnt
902+
1 1 5
903+
2 2 5
904+
3 4 5
905+
4 8 5
906+
5 26 3
907+
6 27 3
908+
7 40 2
909+
8 71 0
910+
9 72 0
911+
select
912+
pk, a,
852913
count(a) over (ORDER BY a
853914
RANGE BETWEEN UNBOUNDED PRECEDING
854915
AND 1 PRECEDING) as cnt
@@ -881,6 +942,22 @@ pk a cnt
881942
9 72 2
882943
select
883944
pk, a,
945+
count(a) over (ORDER BY a DESC
946+
RANGE BETWEEN 1 PRECEDING
947+
AND CURRENT ROW) as cnt
948+
from t1;
949+
pk a cnt
950+
1 1 2
951+
2 2 1
952+
3 4 1
953+
4 8 1
954+
5 26 2
955+
6 27 1
956+
7 40 1
957+
8 71 2
958+
9 72 1
959+
select
960+
pk, a,
884961
count(a) over (ORDER BY a
885962
RANGE BETWEEN 1 FOLLOWING
886963
AND 3 FOLLOWING) as cnt
@@ -895,6 +972,39 @@ pk a cnt
895972
7 40 0
896973
8 71 1
897974
9 72 0
975+
# Try CURRENT ROW with[out] DESC
976+
select
977+
pk, a,
978+
count(a) over (ORDER BY a
979+
RANGE BETWEEN CURRENT ROW
980+
AND 1 FOLLOWING) as cnt
981+
from t1;
982+
pk a cnt
983+
1 1 2
984+
2 2 1
985+
3 4 1
986+
4 8 1
987+
5 26 2
988+
6 27 1
989+
7 40 1
990+
8 71 2
991+
9 72 1
992+
select
993+
pk, a,
994+
count(a) over (order by a desc
995+
range between current row
996+
and 1 following) as cnt
997+
from t1;
998+
pk a cnt
999+
1 1 1
1000+
2 2 2
1001+
3 4 1
1002+
4 8 1
1003+
5 26 1
1004+
6 27 2
1005+
7 40 1
1006+
8 71 1
1007+
9 72 2
8981008
insert into t1 select 22, pk, a from t1;
8991009
select
9001010
part_id, pk, a,

mysql-test/t/win.test

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ insert into t2 values
8282
(10 , 4);
8383

8484
select pk, a, rank() over (order by a) from t2;
85+
select pk, a, rank() over (order by a desc) from t2;
8586

8687
drop table t2;
8788

@@ -158,6 +159,13 @@ select
158159
rows between current row and 1 following) as CNT
159160
from t1;
160161

162+
--echo # Check ORDER BY DESC
163+
select
164+
pk, c,
165+
count(*) over (partition by c order by pk desc
166+
rows between 2 preceding and 2 following) as CNT
167+
from t1;
168+
161169
drop table t0,t1;
162170

163171
--echo #
@@ -514,6 +522,13 @@ select
514522
AND 10 FOLLOWING) as cnt
515523
from t1;
516524

525+
select
526+
pk, a,
527+
count(a) over (ORDER BY a DESC
528+
RANGE BETWEEN UNBOUNDED PRECEDING
529+
AND 10 FOLLOWING) as cnt
530+
from t1;
531+
517532
select
518533
pk, a,
519534
count(a) over (ORDER BY a
@@ -528,6 +543,13 @@ select
528543
AND 10 PRECEDING) as cnt
529544
from t1;
530545

546+
select
547+
pk, a,
548+
count(a) over (ORDER BY a DESC
549+
RANGE BETWEEN UNBOUNDED PRECEDING
550+
AND 10 PRECEDING) as cnt
551+
from t1;
552+
531553
select
532554
pk, a,
533555
count(a) over (ORDER BY a
@@ -543,13 +565,36 @@ select
543565
AND CURRENT ROW) as cnt
544566
from t1;
545567

568+
select
569+
pk, a,
570+
count(a) over (ORDER BY a DESC
571+
RANGE BETWEEN 1 PRECEDING
572+
AND CURRENT ROW) as cnt
573+
from t1;
574+
546575
select
547576
pk, a,
548577
count(a) over (ORDER BY a
549578
RANGE BETWEEN 1 FOLLOWING
550579
AND 3 FOLLOWING) as cnt
551580
from t1;
552581

582+
--echo # Try CURRENT ROW with[out] DESC
583+
select
584+
pk, a,
585+
count(a) over (ORDER BY a
586+
RANGE BETWEEN CURRENT ROW
587+
AND 1 FOLLOWING) as cnt
588+
from t1;
589+
590+
select
591+
pk, a,
592+
count(a) over (order by a desc
593+
range between current row
594+
and 1 following) as cnt
595+
from t1;
596+
597+
553598
# Try with partitions
554599
insert into t1 select 22, pk, a from t1;
555600
select

0 commit comments

Comments
 (0)