mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
Cleanup win testcase to always be deterministic
Also remove some whitespace
This commit is contained in:
@ -4,7 +4,7 @@ drop view if exists v1;
|
|||||||
# # Parser tests
|
# # Parser tests
|
||||||
# ########################################################################
|
# ########################################################################
|
||||||
#
|
#
|
||||||
# Check what happens when one attempts to use window function without OVER clause
|
# Check what happens when one attempts to use window function without OVER clause
|
||||||
create table t1 (a int, b int);
|
create table t1 (a int, b int);
|
||||||
insert into t1 values (1,1),(2,2);
|
insert into t1 values (1,1),(2,2);
|
||||||
select row_number() from t1;
|
select row_number() from t1;
|
||||||
@ -44,15 +44,15 @@ a b x row_number() over (partition by a order by x)
|
|||||||
3 20 vvv 1
|
3 20 vvv 1
|
||||||
drop table t1;
|
drop table t1;
|
||||||
create table t1 (pk int primary key, a int, b int);
|
create table t1 (pk int primary key, a int, b int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1, 10, 22),
|
(1, 10, 22),
|
||||||
(2, 11, 21),
|
(2, 11, 21),
|
||||||
(3, 12, 20),
|
(3, 12, 20),
|
||||||
(4, 13, 19),
|
(4, 13, 19),
|
||||||
(5, 14, 18);
|
(5, 14, 18);
|
||||||
select
|
select
|
||||||
pk, a, b,
|
pk, a, b,
|
||||||
row_number() over (order by a),
|
row_number() over (order by a),
|
||||||
row_number() over (order by b)
|
row_number() over (order by b)
|
||||||
from t1;
|
from t1;
|
||||||
pk a b row_number() over (order by a) row_number() over (order by b)
|
pk a b row_number() over (order by a) row_number() over (order by b)
|
||||||
@ -66,7 +66,7 @@ drop table t1;
|
|||||||
# Try RANK() function
|
# Try RANK() function
|
||||||
#
|
#
|
||||||
create table t2 (
|
create table t2 (
|
||||||
pk int primary key,
|
pk int primary key,
|
||||||
a int
|
a int
|
||||||
);
|
);
|
||||||
insert into t2 values
|
insert into t2 values
|
||||||
@ -83,27 +83,27 @@ insert into t2 values
|
|||||||
select pk, a, rank() over (order by a) from t2;
|
select pk, a, rank() over (order by a) from t2;
|
||||||
pk a rank() over (order by a)
|
pk a rank() over (order by a)
|
||||||
1 0 1
|
1 0 1
|
||||||
|
10 4 9
|
||||||
2 0 1
|
2 0 1
|
||||||
3 1 3
|
3 1 3
|
||||||
4 1 3
|
4 1 3
|
||||||
8 2 5
|
|
||||||
5 2 5
|
5 2 5
|
||||||
6 2 5
|
6 2 5
|
||||||
7 2 5
|
7 2 5
|
||||||
|
8 2 5
|
||||||
9 4 9
|
9 4 9
|
||||||
10 4 9
|
|
||||||
select pk, a, rank() over (order by a desc) from t2;
|
select pk, a, rank() over (order by a desc) from t2;
|
||||||
pk a rank() over (order by a desc)
|
pk a rank() over (order by a desc)
|
||||||
1 0 9
|
1 0 9
|
||||||
|
10 4 1
|
||||||
2 0 9
|
2 0 9
|
||||||
3 1 7
|
3 1 7
|
||||||
4 1 7
|
4 1 7
|
||||||
8 2 3
|
|
||||||
5 2 3
|
5 2 3
|
||||||
6 2 3
|
6 2 3
|
||||||
7 2 3
|
7 2 3
|
||||||
|
8 2 3
|
||||||
9 4 1
|
9 4 1
|
||||||
10 4 1
|
|
||||||
drop table t2;
|
drop table t2;
|
||||||
#
|
#
|
||||||
# Try Aggregates as window functions. With frames.
|
# Try Aggregates as window functions. With frames.
|
||||||
@ -125,9 +125,9 @@ pk c
|
|||||||
8 2
|
8 2
|
||||||
9 2
|
9 2
|
||||||
10 2
|
10 2
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following) as CNT
|
rows between 2 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
pk c CNT
|
pk c CNT
|
||||||
@ -141,9 +141,9 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 1 preceding and 2 following) as CNT
|
rows between 1 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
pk c CNT
|
pk c CNT
|
||||||
@ -158,9 +158,9 @@ pk c CNT
|
|||||||
9 2 3
|
9 2 3
|
||||||
10 2 2
|
10 2 2
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 2 preceding and current row) as CNT
|
rows between 2 preceding and current row) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
pk c CNT
|
pk c CNT
|
||||||
1 1 1
|
1 1 1
|
||||||
@ -173,8 +173,8 @@ pk c CNT
|
|||||||
8 2 3
|
8 2 3
|
||||||
9 2 3
|
9 2 3
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk,c,
|
pk,c,
|
||||||
count(*) over (partition by c order by pk rows
|
count(*) over (partition by c order by pk rows
|
||||||
between 1 following and 2 following) as CNT
|
between 1 following and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
@ -189,8 +189,8 @@ pk c CNT
|
|||||||
8 2 2
|
8 2 2
|
||||||
9 2 1
|
9 2 1
|
||||||
10 2 0
|
10 2 0
|
||||||
select
|
select
|
||||||
pk,c,
|
pk,c,
|
||||||
count(*) over (partition by c order by pk rows
|
count(*) over (partition by c order by pk rows
|
||||||
between 2 preceding and 1 preceding) as CNT
|
between 2 preceding and 1 preceding) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
@ -206,9 +206,9 @@ pk c CNT
|
|||||||
9 2 2
|
9 2 2
|
||||||
10 2 2
|
10 2 2
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between current row and 1 following) as CNT
|
rows between current row and 1 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
pk c CNT
|
pk c CNT
|
||||||
1 1 2
|
1 1 2
|
||||||
@ -222,8 +222,8 @@ pk c CNT
|
|||||||
9 2 2
|
9 2 2
|
||||||
10 2 1
|
10 2 1
|
||||||
# Check ORDER BY DESC
|
# Check ORDER BY DESC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk desc
|
count(*) over (partition by c order by pk desc
|
||||||
rows between 2 preceding and 2 following) as CNT
|
rows between 2 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
@ -259,11 +259,11 @@ pk c
|
|||||||
8 2
|
8 2
|
||||||
9 2
|
9 2
|
||||||
10 2
|
10 2
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
pk c CNT
|
pk c CNT
|
||||||
1 1 3
|
1 1 3
|
||||||
@ -276,8 +276,8 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w1 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
@ -292,8 +292,8 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 order by pk rows between 2 preceding and 2 following) as CNT
|
count(*) over (w1 order by pk rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
@ -308,8 +308,8 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 order by pk);
|
window w1 as (partition by c), w2 as (w1 order by pk);
|
||||||
@ -324,12 +324,12 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w3 as CNT
|
count(*) over w3 as CNT
|
||||||
from t1
|
from t1
|
||||||
window
|
window
|
||||||
w1 as (partition by c),
|
w1 as (partition by c),
|
||||||
w2 as (w1 order by pk),
|
w2 as (w1 order by pk),
|
||||||
w3 as (w2 rows between 2 preceding and 2 following);
|
w3 as (w2 rows between 2 preceding and 2 following);
|
||||||
pk c CNT
|
pk c CNT
|
||||||
@ -343,141 +343,141 @@ pk c CNT
|
|||||||
8 2 5
|
8 2 5
|
||||||
9 2 4
|
9 2 4
|
||||||
10 2 3
|
10 2 3
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w as CNT
|
count(*) over w as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
ERROR HY000: Window specification with name 'w' is not defined
|
ERROR HY000: Window specification with name 'w' is not defined
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w1 as (order by pk);
|
window w1 as (partition by c), w1 as (order by pk);
|
||||||
ERROR HY000: Multiple window specifications with the same name 'w1'
|
ERROR HY000: Multiple window specifications with the same name 'w1'
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w partition by c order by pk);
|
window w1 as (partition by c), w2 as (w partition by c order by pk);
|
||||||
ERROR HY000: Window specification with name 'w' is not defined
|
ERROR HY000: Window specification with name 'w' is not defined
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 partition by c order by pk);
|
window w1 as (partition by c), w2 as (w1 partition by c order by pk);
|
||||||
ERROR HY000: Window specification referencing another one 'w1' cannot contain partition list
|
ERROR HY000: Window specification referencing another one 'w1' cannot contain partition list
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk), w2 as (w1 order by pk);
|
window w1 as (partition by c order by pk), w2 as (w1 order by pk);
|
||||||
ERROR HY000: Referenced window specification 'w1' already contains order list
|
ERROR HY000: Referenced window specification 'w1' already contains order list
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w3 as CNT
|
count(*) over w3 as CNT
|
||||||
from t1
|
from t1
|
||||||
window
|
window
|
||||||
w1 as (partition by c),
|
w1 as (partition by c),
|
||||||
w2 as (w1 order by pk rows between 3 preceding and 2 following),
|
w2 as (w1 order by pk rows between 3 preceding and 2 following),
|
||||||
w3 as (w2 rows between 2 preceding and 2 following);
|
w3 as (w2 rows between 2 preceding and 2 following);
|
||||||
ERROR HY000: Referenced window specification 'w2' cannot contain window frame
|
ERROR HY000: Referenced window specification 'w2' cannot contain window frame
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between unbounded following and 2 following);
|
rows between unbounded following and 2 following);
|
||||||
ERROR HY000: Unacceptable combination of window frame bound specifications
|
ERROR HY000: Unacceptable combination of window frame bound specifications
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 rows between 2 preceding and unbounded preceding) as CNT
|
count(*) over (w1 rows between 2 preceding and unbounded preceding) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
ERROR HY000: Unacceptable combination of window frame bound specifications
|
ERROR HY000: Unacceptable combination of window frame bound specifications
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 order by pk rows between current row and 2 preceding) as CNT
|
count(*) over (w1 order by pk rows between current row and 2 preceding) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
ERROR HY000: Unacceptable combination of window frame bound specifications
|
ERROR HY000: Unacceptable combination of window frame bound specifications
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 following and current row) as CNT
|
count(*) over (w2 rows between 2 following and current row) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 order by pk);
|
window w1 as (partition by c), w2 as (w1 order by pk);
|
||||||
ERROR HY000: Unacceptable combination of window frame bound specifications
|
ERROR HY000: Unacceptable combination of window frame bound specifications
|
||||||
select
|
select
|
||||||
pk, c
|
pk, c
|
||||||
from t1 where rank() over w1 > 2
|
from t1 where rank() over w1 > 2
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
||||||
select
|
select
|
||||||
c, max(pk) as m
|
c, max(pk) as m
|
||||||
from t1
|
from t1
|
||||||
group by c + rank() over w1
|
group by c + rank() over w1
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r
|
c, max(pk) as m, rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
group by c+r
|
group by c+r
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r
|
c, max(pk) as m, rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
group by c having c+r > 3
|
group by c having c+r > 3
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
ERROR HY000: Window function is allowed only in SELECT list and ORDER BY clause
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r,
|
c, max(pk) as m, rank() over w1 as r,
|
||||||
rank() over (partition by r+1 order by m)
|
rank() over (partition by r+1 order by m)
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
ERROR HY000: Window function is not allowed in window specification
|
ERROR HY000: Window function is not allowed in window specification
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r,
|
c, max(pk) as m, rank() over w1 as r,
|
||||||
rank() over (partition by m order by r)
|
rank() over (partition by m order by r)
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
ERROR HY000: Window function is not allowed in window specification
|
ERROR HY000: Window function is not allowed in window specification
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr
|
c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m), w2 as (partition by r order by m);
|
window w1 as (order by m), w2 as (partition by r order by m);
|
||||||
ERROR HY000: Window function is not allowed in window specification
|
ERROR HY000: Window function is not allowed in window specification
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
row_number() over (partition by c order by pk
|
row_number() over (partition by c order by pk
|
||||||
range between unbounded preceding and current row) as r
|
range between unbounded preceding and current row) as r
|
||||||
from t1;
|
from t1;
|
||||||
ERROR HY000: Window frame is not allowed with 'row_number('
|
ERROR HY000: Window frame is not allowed with 'row_number('
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
rank() over w1 as r
|
rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
ERROR HY000: Window frame is not allowed with 'rank'
|
ERROR HY000: Window frame is not allowed with 'rank'
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
dense_rank() over (partition by c order by pk
|
dense_rank() over (partition by c order by pk
|
||||||
rows between 1 preceding and 1 following) as r
|
rows between 1 preceding and 1 following) as r
|
||||||
from t1;
|
from t1;
|
||||||
ERROR HY000: Window frame is not allowed with 'dense_rank'
|
ERROR HY000: Window frame is not allowed with 'dense_rank'
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
rank() over w1 as r
|
rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
ERROR HY000: No order list in window specification for 'rank'
|
ERROR HY000: No order list in window specification for 'rank'
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
dense_rank() over (partition by c) as r
|
dense_rank() over (partition by c) as r
|
||||||
from t1;
|
from t1;
|
||||||
ERROR HY000: No order list in window specification for 'dense_rank'
|
ERROR HY000: No order list in window specification for 'dense_rank'
|
||||||
@ -488,7 +488,7 @@ drop table t0,t1;
|
|||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
create table t2 (part_id int, pk int, a int);
|
create table t2 (part_id int, pk int, a int);
|
||||||
insert into t2 select
|
insert into t2 select
|
||||||
if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
|
if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
|
||||||
select * from t2;
|
select * from t2;
|
||||||
part_id pk a
|
part_id pk a
|
||||||
@ -502,7 +502,7 @@ part_id pk a
|
|||||||
1 7 1
|
1 7 1
|
||||||
1 8 1
|
1 8 1
|
||||||
1 9 1
|
1 9 1
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
count(a) over (partition by part_id order by pk
|
count(a) over (partition by part_id order by pk
|
||||||
rows between 1 preceding and 1 following) as CNT
|
rows between 1 preceding and 1 following) as CNT
|
||||||
@ -520,10 +520,10 @@ part_id pk a CNT
|
|||||||
1 9 1 2
|
1 9 1 2
|
||||||
drop table t0, t2;
|
drop table t0, t2;
|
||||||
#
|
#
|
||||||
# RANGE-type bounds
|
# RANGE-type bounds
|
||||||
#
|
#
|
||||||
create table t3 (
|
create table t3 (
|
||||||
pk int,
|
pk int,
|
||||||
val int
|
val int
|
||||||
);
|
);
|
||||||
insert into t3 values
|
insert into t3 values
|
||||||
@ -534,12 +534,12 @@ insert into t3 values
|
|||||||
(4, 2),
|
(4, 2),
|
||||||
(5, 2),
|
(5, 2),
|
||||||
(6, 2);
|
(6, 2);
|
||||||
select
|
select
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (order by val
|
count(val) over (order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t3;
|
from t3;
|
||||||
pk val CNT
|
pk val CNT
|
||||||
@ -550,15 +550,15 @@ pk val CNT
|
|||||||
4 2 4
|
4 2 4
|
||||||
5 2 4
|
5 2 4
|
||||||
6 2 4
|
6 2 4
|
||||||
insert into t3 values
|
insert into t3 values
|
||||||
(7, 3),
|
(7, 3),
|
||||||
(8, 3);
|
(8, 3);
|
||||||
select
|
select
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (order by val
|
count(val) over (order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t3;
|
from t3;
|
||||||
pk val CNT
|
pk val CNT
|
||||||
@ -597,14 +597,14 @@ insert into t4 values
|
|||||||
(5678, 206, 2),
|
(5678, 206, 2),
|
||||||
(5678, 207, 3),
|
(5678, 207, 3),
|
||||||
(5678, 208, 3);
|
(5678, 208, 3);
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
part_id pk val CNT
|
part_id pk val CNT
|
||||||
@ -629,14 +629,14 @@ part_id pk val CNT
|
|||||||
#
|
#
|
||||||
# Try RANGE UNBOUNDED PRECEDING | FOLLOWING
|
# Try RANGE UNBOUNDED PRECEDING | FOLLOWING
|
||||||
#
|
#
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between unbounded preceding and
|
range between unbounded preceding and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
part_id pk val CNT
|
part_id pk val CNT
|
||||||
@ -658,14 +658,14 @@ part_id pk val CNT
|
|||||||
5678 206 2 7
|
5678 206 2 7
|
||||||
5678 207 3 9
|
5678 207 3 9
|
||||||
5678 208 3 9
|
5678 208 3 9
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
unbounded following)
|
unbounded following)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
part_id pk val CNT
|
part_id pk val CNT
|
||||||
@ -687,14 +687,14 @@ part_id pk val CNT
|
|||||||
5678 206 2 6
|
5678 206 2 6
|
||||||
5678 207 3 2
|
5678 207 3 2
|
||||||
5678 208 3 2
|
5678 208 3 2
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between unbounded preceding and
|
range between unbounded preceding and
|
||||||
unbounded following)
|
unbounded following)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
part_id pk val CNT
|
part_id pk val CNT
|
||||||
@ -742,7 +742,7 @@ pk a b bit_or
|
|||||||
6 2 64 224
|
6 2 64 224
|
||||||
7 2 128 208
|
7 2 128 208
|
||||||
8 2 16 144
|
8 2 16 144
|
||||||
# Extra ROWS n PRECEDING tests
|
# Extra ROWS n PRECEDING tests
|
||||||
select pk, a, b,
|
select pk, a, b,
|
||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
|
||||||
from t1;
|
from t1;
|
||||||
@ -757,8 +757,8 @@ pk a b bit_or
|
|||||||
8 2 16 128
|
8 2 16 128
|
||||||
drop table t1;
|
drop table t1;
|
||||||
create table t2 (
|
create table t2 (
|
||||||
pk int,
|
pk int,
|
||||||
a int,
|
a int,
|
||||||
b int
|
b int
|
||||||
);
|
);
|
||||||
insert into t2 values
|
insert into t2 values
|
||||||
@ -822,7 +822,7 @@ pk a b bit_or
|
|||||||
10 2 512 256
|
10 2 512 256
|
||||||
11 2 1024 768
|
11 2 1024 768
|
||||||
12 2 2048 1536
|
12 2 2048 1536
|
||||||
# Check CURRENT ROW
|
# Check CURRENT ROW
|
||||||
select pk, a, b,
|
select pk, a, b,
|
||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN CURRENT ROW AND CURRENT ROW) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN CURRENT ROW AND CURRENT ROW) as bit_or
|
||||||
from t2;
|
from t2;
|
||||||
@ -848,12 +848,12 @@ part_id int,
|
|||||||
pk int,
|
pk int,
|
||||||
a int
|
a int
|
||||||
);
|
);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(10, 1, 1),
|
(10, 1, 1),
|
||||||
(10, 2, 2),
|
(10, 2, 2),
|
||||||
(10, 3, 4),
|
(10, 3, 4),
|
||||||
(10, 4, 8),
|
(10, 4, 8),
|
||||||
(10, 5,26),
|
(10, 5,26),
|
||||||
(10, 6,27),
|
(10, 6,27),
|
||||||
(10, 7,40),
|
(10, 7,40),
|
||||||
(10, 8,71),
|
(10, 8,71),
|
||||||
@ -861,7 +861,7 @@ insert into t1 values
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -877,7 +877,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -893,7 +893,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 FOLLOWING) as cnt
|
AND 1 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -909,7 +909,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 PRECEDING) as cnt
|
AND 10 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -925,7 +925,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 PRECEDING) as cnt
|
AND 10 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -941,7 +941,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 PRECEDING) as cnt
|
AND 1 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -957,7 +957,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN 1 PRECEDING
|
RANGE BETWEEN 1 PRECEDING
|
||||||
AND CURRENT ROW) as cnt
|
AND CURRENT ROW) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -973,7 +973,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN 1 PRECEDING
|
RANGE BETWEEN 1 PRECEDING
|
||||||
AND CURRENT ROW) as cnt
|
AND CURRENT ROW) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -989,7 +989,7 @@ pk a cnt
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN 1 FOLLOWING
|
RANGE BETWEEN 1 FOLLOWING
|
||||||
AND 3 FOLLOWING) as cnt
|
AND 3 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -1040,7 +1040,7 @@ select
|
|||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
count(a) over (PARTITION BY part_id
|
count(a) over (PARTITION BY part_id
|
||||||
ORDER BY a
|
ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
part_id pk a cnt
|
part_id pk a cnt
|
||||||
@ -1066,7 +1066,7 @@ select
|
|||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (PARTITION BY part_id
|
count(a) over (PARTITION BY part_id
|
||||||
ORDER BY a
|
ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 PRECEDING) as cnt
|
AND 1 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk a cnt
|
pk a cnt
|
||||||
@ -1101,13 +1101,13 @@ insert into t1 values (1, 1.21);
|
|||||||
insert into t1 values (1, 1.22);
|
insert into t1 values (1, 1.22);
|
||||||
insert into t1 values (1, 3.33);
|
insert into t1 values (1, 3.33);
|
||||||
select
|
select
|
||||||
a,
|
a,
|
||||||
count(col1) over (order by a
|
count(col1) over (order by a
|
||||||
range between 0.1 preceding
|
range between 0.1 preceding
|
||||||
and 0.1 following)
|
and 0.1 following)
|
||||||
from t1;
|
from t1;
|
||||||
a count(col1) over (order by a
|
a count(col1) over (order by a
|
||||||
range between 0.1 preceding
|
range between 0.1 preceding
|
||||||
and 0.1 following)
|
and 0.1 following)
|
||||||
0.450 3
|
0.450 3
|
||||||
0.500 3
|
0.500 3
|
||||||
@ -1116,9 +1116,9 @@ and 0.1 following)
|
|||||||
1.220 2
|
1.220 2
|
||||||
3.330 1
|
3.330 1
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# RANGE-type frames and NULL values
|
# RANGE-type frames and NULL values
|
||||||
#
|
#
|
||||||
create table t1 (
|
create table t1 (
|
||||||
pk int,
|
pk int,
|
||||||
a int,
|
a int,
|
||||||
@ -1132,10 +1132,10 @@ insert into t1 values (5, 11 ,1);
|
|||||||
insert into t1 values (6, 12 ,1);
|
insert into t1 values (6, 12 ,1);
|
||||||
insert into t1 values (7, 13 ,1);
|
insert into t1 values (7, 13 ,1);
|
||||||
insert into t1 values (8, 14 ,1);
|
insert into t1 values (8, 14 ,1);
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(b) over (order by a
|
count(b) over (order by a
|
||||||
range between 2 preceding
|
range between 2 preceding
|
||||||
and 2 following) as CNT
|
and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
pk a CNT
|
pk a CNT
|
||||||
@ -1148,9 +1148,9 @@ pk a CNT
|
|||||||
7 13 4
|
7 13 4
|
||||||
8 14 3
|
8 14 3
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# Try ranges that have bound1 > bound2. The standard actually allows them
|
# Try ranges that have bound1 > bound2. The standard actually allows them
|
||||||
#
|
#
|
||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
create table t1 (pk int, c int);
|
create table t1 (pk int, c int);
|
||||||
@ -1173,7 +1173,7 @@ pk, c,
|
|||||||
count(*) over (partition by c
|
count(*) over (partition by c
|
||||||
order by pk
|
order by pk
|
||||||
rows between 1 preceding
|
rows between 1 preceding
|
||||||
and 2 preceding)
|
and 2 preceding)
|
||||||
as cnt
|
as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk c cnt
|
pk c cnt
|
||||||
@ -1230,7 +1230,7 @@ pk, c,
|
|||||||
count(*) over (partition by c
|
count(*) over (partition by c
|
||||||
order by pk
|
order by pk
|
||||||
range between 1 preceding
|
range between 1 preceding
|
||||||
and 2 preceding)
|
and 2 preceding)
|
||||||
as cnt
|
as cnt
|
||||||
from t1;
|
from t1;
|
||||||
pk c cnt
|
pk c cnt
|
||||||
@ -1251,7 +1251,7 @@ drop table t0, t1;
|
|||||||
create table t1 (a int, b int, c varchar(32));
|
create table t1 (a int, b int, c varchar(32));
|
||||||
insert into t1 values (1,1,'foo');
|
insert into t1 values (1,1,'foo');
|
||||||
insert into t1 values (2,2,'bar');
|
insert into t1 values (2,2,'bar');
|
||||||
select
|
select
|
||||||
count(*) over (order by a,b
|
count(*) over (order by a,b
|
||||||
range between unbounded preceding and current row)
|
range between unbounded preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1283,7 +1283,7 @@ rows between current row and 3.14 following)
|
|||||||
from t1;
|
from t1;
|
||||||
ERROR HY000: Integer is required for ROWS-type frame
|
ERROR HY000: Integer is required for ROWS-type frame
|
||||||
#
|
#
|
||||||
# EXCLUDE clause is parsed but not supported
|
# EXCLUDE clause is parsed but not supported
|
||||||
#
|
#
|
||||||
select
|
select
|
||||||
count(*) over (order by a
|
count(*) over (order by a
|
||||||
@ -1314,9 +1314,9 @@ exclude no others)
|
|||||||
2
|
2
|
||||||
2
|
2
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# Window function in grouping query
|
# Window function in grouping query
|
||||||
#
|
#
|
||||||
create table t1 (
|
create table t1 (
|
||||||
username varchar(32),
|
username varchar(32),
|
||||||
amount int
|
amount int
|
||||||
@ -1328,9 +1328,9 @@ insert into t1 values
|
|||||||
('user2',10),
|
('user2',10),
|
||||||
('user2',20),
|
('user2',20),
|
||||||
('user2',30);
|
('user2',30);
|
||||||
select
|
select
|
||||||
username,
|
username,
|
||||||
sum(amount) as s,
|
sum(amount) as s,
|
||||||
rank() over (order by s desc)
|
rank() over (order by s desc)
|
||||||
from t1
|
from t1
|
||||||
group by username;
|
group by username;
|
||||||
@ -1338,9 +1338,9 @@ username s rank() over (order by s desc)
|
|||||||
user1 9 2
|
user1 9 2
|
||||||
user2 60 1
|
user2 60 1
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# mdev-9719: Window function in prepared statement
|
# mdev-9719: Window function in prepared statement
|
||||||
#
|
#
|
||||||
create table t1(a int, b int, x char(32));
|
create table t1(a int, b int, x char(32));
|
||||||
insert into t1 values (2, 10, 'xx');
|
insert into t1 values (2, 10, 'xx');
|
||||||
insert into t1 values (2, 10, 'zz');
|
insert into t1 values (2, 10, 'zz');
|
||||||
@ -1356,9 +1356,9 @@ a row_number() over (partition by a order by b)
|
|||||||
3 1
|
3 1
|
||||||
3 2
|
3 2
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# mdev-9754: Window name resolution in prepared statement
|
# mdev-9754: Window name resolution in prepared statement
|
||||||
#
|
#
|
||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
create table t1 (pk int, c int);
|
create table t1 (pk int, c int);
|
||||||
@ -1376,12 +1376,12 @@ pk c
|
|||||||
8 2
|
8 2
|
||||||
9 2
|
9 2
|
||||||
10 2
|
10 2
|
||||||
prepare stmt from
|
prepare stmt from
|
||||||
'select
|
'select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following)';
|
rows between 2 preceding and 2 following)';
|
||||||
execute stmt;
|
execute stmt;
|
||||||
pk c CNT
|
pk c CNT
|
||||||
@ -1560,10 +1560,10 @@ insert into t1 values
|
|||||||
select sum(b) over (partition by a order by b,pk
|
select sum(b) over (partition by a order by b,pk
|
||||||
rows between unbounded preceding and current row) as c1,
|
rows between unbounded preceding and current row) as c1,
|
||||||
avg(b) over (w1 rows between 1 preceding and 1 following) as c2,
|
avg(b) over (w1 rows between 1 preceding and 1 following) as c2,
|
||||||
sum(c) over (w2 rows between 1 preceding and 1 following) as c5,
|
sum(c) over (w2 rows between 1 preceding and 1 following) as c5,
|
||||||
avg(b) over (w1 rows between 5 preceding and 5 following) as c3,
|
avg(b) over (w1 rows between 5 preceding and 5 following) as c3,
|
||||||
sum(b) over (w1 rows between 1 preceding and 1 following) as c4
|
sum(b) over (w1 rows between 1 preceding and 1 following) as c4
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by a order by b,pk),
|
window w1 as (partition by a order by b,pk),
|
||||||
w2 as (partition by b order by c,pk);
|
w2 as (partition by b order by c,pk);
|
||||||
c1 c2 c5 c3 c4
|
c1 c2 c5 c3 c4
|
||||||
@ -1599,13 +1599,13 @@ drop table t1;
|
|||||||
# MDEV-9848: Window functions: reuse sorting and/or scanning
|
# MDEV-9848: Window functions: reuse sorting and/or scanning
|
||||||
#
|
#
|
||||||
create table t1 (a int, b int, c int);
|
create table t1 (a int, b int, c int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1,3,1),
|
(1,3,1),
|
||||||
(2,2,1),
|
(2,2,1),
|
||||||
(3,1,1);
|
(3,1,1);
|
||||||
# Check using counters
|
# Check using counters
|
||||||
flush status;
|
flush status;
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by b)
|
rank() over (partition by c order by b)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1621,7 +1621,7 @@ Sort_range 0
|
|||||||
Sort_rows 6
|
Sort_rows 6
|
||||||
Sort_scan 2
|
Sort_scan 2
|
||||||
flush status;
|
flush status;
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1637,7 +1637,7 @@ Sort_range 0
|
|||||||
Sort_rows 3
|
Sort_rows 3
|
||||||
Sort_scan 1
|
Sort_scan 1
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1663,7 +1663,7 @@ EXPLAIN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (order by a),
|
rank() over (order by a),
|
||||||
row_number() over (order by a)
|
row_number() over (order by a)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1689,7 +1689,7 @@ EXPLAIN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
count(*) over (partition by c)
|
count(*) over (partition by c)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1715,7 +1715,7 @@ EXPLAIN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
count(*) over (partition by c),
|
count(*) over (partition by c),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1811,7 +1811,7 @@ drop table t1;
|
|||||||
# Try window functions that are not directly present in the select list
|
# Try window functions that are not directly present in the select list
|
||||||
#
|
#
|
||||||
create table t1 (a int, b int);
|
create table t1 (a int, b int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1,3),
|
(1,3),
|
||||||
(2,2),
|
(2,2),
|
||||||
(3,1);
|
(3,1);
|
||||||
@ -1845,7 +1845,7 @@ insert into t1 values (1, 1, 1);
|
|||||||
insert into t1 values (1, 2, 2);
|
insert into t1 values (1, 2, 2);
|
||||||
insert into t1 values (1, 3, 4);
|
insert into t1 values (1, 3, 4);
|
||||||
insert into t1 values (1, 4, 8);
|
insert into t1 values (1, 4, 8);
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
sum(a) over (order by pk rows between 0 preceding and current row)
|
sum(a) over (order by pk rows between 0 preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1854,8 +1854,8 @@ pk a sum(a) over (order by pk rows between 0 preceding and current row)
|
|||||||
2 2 2
|
2 2 2
|
||||||
3 4 4
|
3 4 4
|
||||||
4 8 8
|
4 8 8
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
||||||
from t1;
|
from t1;
|
||||||
pk a sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
pk a sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
||||||
@ -1867,7 +1867,7 @@ insert into t1 values (200, 1, 1);
|
|||||||
insert into t1 values (200, 2, 2);
|
insert into t1 values (200, 2, 2);
|
||||||
insert into t1 values (200, 3, 4);
|
insert into t1 values (200, 3, 4);
|
||||||
insert into t1 values (200, 4, 8);
|
insert into t1 values (200, 4, 8);
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
|
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1880,8 +1880,8 @@ part_id pk a sum(a) over (partition by part_id order by pk rows between 0 preced
|
|||||||
200 2 2 2
|
200 2 2 2
|
||||||
200 3 4 4
|
200 3 4 4
|
||||||
200 4 8 8
|
200 4 8 8
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
||||||
from t1;
|
from t1;
|
||||||
part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
||||||
@ -1896,7 +1896,7 @@ part_id pk a sum(a) over (partition by part_id order by pk rows between 1 preced
|
|||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
|
# MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
|
||||||
# window functions are present" part
|
# window functions are present" part
|
||||||
#
|
#
|
||||||
create table t1 (part_id int, a int);
|
create table t1 (part_id int, a int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
@ -1953,8 +1953,8 @@ EXPLAIN
|
|||||||
}
|
}
|
||||||
drop table t1;
|
drop table t1;
|
||||||
#
|
#
|
||||||
# MDEV-9893: Window functions with different ORDER BY lists,
|
# MDEV-9893: Window functions with different ORDER BY lists,
|
||||||
# one of these lists containing an expression
|
# one of these lists containing an expression
|
||||||
#
|
#
|
||||||
create table t1 (s1 int, s2 char(5));
|
create table t1 (s1 int, s2 char(5));
|
||||||
insert into t1 values (1,'a');
|
insert into t1 values (1,'a');
|
||||||
@ -1963,10 +1963,10 @@ insert into t1 values (3,null);
|
|||||||
insert into t1 values (4,'a');
|
insert into t1 values (4,'a');
|
||||||
insert into t1 values (2,'b');
|
insert into t1 values (2,'b');
|
||||||
insert into t1 values (-1,'');
|
insert into t1 values (-1,'');
|
||||||
select
|
select
|
||||||
*,
|
*,
|
||||||
ROW_NUMBER() OVER (order by s1),
|
ROW_NUMBER() OVER (order by s1),
|
||||||
CUME_DIST() OVER (order by -s1)
|
CUME_DIST() OVER (order by -s1)
|
||||||
from t1;
|
from t1;
|
||||||
s1 s2 ROW_NUMBER() OVER (order by s1) CUME_DIST() OVER (order by -s1)
|
s1 s2 ROW_NUMBER() OVER (order by s1) CUME_DIST() OVER (order by -s1)
|
||||||
1 a 3 0.8333333333
|
1 a 3 0.8333333333
|
||||||
@ -2016,7 +2016,7 @@ AND 15 FOLLOWING)
|
|||||||
238 NULL
|
238 NULL
|
||||||
DROP table orders;
|
DROP table orders;
|
||||||
#
|
#
|
||||||
# MDEV-10842: window functions with the same order column
|
# MDEV-10842: window functions with the same order column
|
||||||
# but different directions
|
# but different directions
|
||||||
#
|
#
|
||||||
create table t1 (
|
create table t1 (
|
||||||
|
@ -11,7 +11,7 @@ drop view if exists v1;
|
|||||||
--echo # # Parser tests
|
--echo # # Parser tests
|
||||||
--echo # ########################################################################
|
--echo # ########################################################################
|
||||||
--echo #
|
--echo #
|
||||||
--echo # Check what happens when one attempts to use window function without OVER clause
|
--echo # Check what happens when one attempts to use window function without OVER clause
|
||||||
create table t1 (a int, b int);
|
create table t1 (a int, b int);
|
||||||
insert into t1 values (1,1),(2,2);
|
insert into t1 values (1,1),(2,2);
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ select row_number() from t1;
|
|||||||
select rank() from t1;
|
select rank() from t1;
|
||||||
|
|
||||||
--echo # Attempt to use window function in the WHERE clause
|
--echo # Attempt to use window function in the WHERE clause
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select * from t1 where 1=rank() over (order by a);
|
select * from t1 where 1=rank() over (order by a);
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select * from t1 where 1>row_number() over (partition by b order by a);
|
select * from t1 where 1>row_number() over (partition by b order by a);
|
||||||
@ -47,16 +47,16 @@ select a, b, x, row_number() over (partition by a order by x) from t1;
|
|||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
create table t1 (pk int primary key, a int, b int);
|
create table t1 (pk int primary key, a int, b int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1, 10, 22),
|
(1, 10, 22),
|
||||||
(2, 11, 21),
|
(2, 11, 21),
|
||||||
(3, 12, 20),
|
(3, 12, 20),
|
||||||
(4, 13, 19),
|
(4, 13, 19),
|
||||||
(5, 14, 18);
|
(5, 14, 18);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a, b,
|
pk, a, b,
|
||||||
row_number() over (order by a),
|
row_number() over (order by a),
|
||||||
row_number() over (order by b)
|
row_number() over (order by b)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ drop table t1;
|
|||||||
--echo # Try RANK() function
|
--echo # Try RANK() function
|
||||||
--echo #
|
--echo #
|
||||||
create table t2 (
|
create table t2 (
|
||||||
pk int primary key,
|
pk int primary key,
|
||||||
a int
|
a int
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -82,7 +82,9 @@ insert into t2 values
|
|||||||
( 9 , 4),
|
( 9 , 4),
|
||||||
(10 , 4);
|
(10 , 4);
|
||||||
|
|
||||||
|
--sorted_result
|
||||||
select pk, a, rank() over (order by a) from t2;
|
select pk, a, rank() over (order by a) from t2;
|
||||||
|
--sorted_result
|
||||||
select pk, a, rank() over (order by a desc) from t2;
|
select pk, a, rank() over (order by a desc) from t2;
|
||||||
|
|
||||||
drop table t2;
|
drop table t2;
|
||||||
@ -98,45 +100,45 @@ insert into t1 select a+1,1 from t0;
|
|||||||
update t1 set c=2 where pk not in (1,2,3,4);
|
update t1 set c=2 where pk not in (1,2,3,4);
|
||||||
select * from t1;
|
select * from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following) as CNT
|
rows between 2 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 1 preceding and 2 following) as CNT
|
rows between 1 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between 2 preceding and current row) as CNT
|
rows between 2 preceding and current row) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk,c,
|
pk,c,
|
||||||
count(*) over (partition by c order by pk rows
|
count(*) over (partition by c order by pk rows
|
||||||
between 1 following and 2 following) as CNT
|
between 1 following and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk,c,
|
pk,c,
|
||||||
count(*) over (partition by c order by pk rows
|
count(*) over (partition by c order by pk rows
|
||||||
between 2 preceding and 1 preceding) as CNT
|
between 2 preceding and 1 preceding) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk
|
count(*) over (partition by c order by pk
|
||||||
rows between current row and 1 following) as CNT
|
rows between current row and 1 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
--echo # Check ORDER BY DESC
|
--echo # Check ORDER BY DESC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (partition by c order by pk desc
|
count(*) over (partition by c order by pk desc
|
||||||
rows between 2 preceding and 2 following) as CNT
|
rows between 2 preceding and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
@ -155,197 +157,197 @@ insert into t1 select a+1,1 from t0;
|
|||||||
update t1 set c=2 where pk not in (1,2,3,4);
|
update t1 set c=2 where pk not in (1,2,3,4);
|
||||||
select * from t1;
|
select * from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w1 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 order by pk rows between 2 preceding and 2 following) as CNT
|
count(*) over (w1 order by pk rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 order by pk);
|
window w1 as (partition by c), w2 as (w1 order by pk);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w3 as CNT
|
count(*) over w3 as CNT
|
||||||
from t1
|
from t1
|
||||||
window
|
window
|
||||||
w1 as (partition by c),
|
w1 as (partition by c),
|
||||||
w2 as (w1 order by pk),
|
w2 as (w1 order by pk),
|
||||||
w3 as (w2 rows between 2 preceding and 2 following);
|
w3 as (w2 rows between 2 preceding and 2 following);
|
||||||
|
|
||||||
--error ER_WRONG_WINDOW_SPEC_NAME
|
--error ER_WRONG_WINDOW_SPEC_NAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w as CNT
|
count(*) over w as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
|
|
||||||
--error ER_DUP_WINDOW_NAME
|
--error ER_DUP_WINDOW_NAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w1 as (order by pk);
|
window w1 as (partition by c), w1 as (order by pk);
|
||||||
|
|
||||||
--error ER_WRONG_WINDOW_SPEC_NAME
|
--error ER_WRONG_WINDOW_SPEC_NAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w partition by c order by pk);
|
window w1 as (partition by c), w2 as (w partition by c order by pk);
|
||||||
|
|
||||||
--error ER_PARTITION_LIST_IN_REFERENCING_WINDOW_SPEC
|
--error ER_PARTITION_LIST_IN_REFERENCING_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 partition by c order by pk);
|
window w1 as (partition by c), w2 as (w1 partition by c order by pk);
|
||||||
|
|
||||||
--error ER_ORDER_LIST_IN_REFERENCING_WINDOW_SPEC
|
--error ER_ORDER_LIST_IN_REFERENCING_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
count(*) over (w2 rows between 2 preceding and 2 following) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk), w2 as (w1 order by pk);
|
window w1 as (partition by c order by pk), w2 as (w1 order by pk);
|
||||||
|
|
||||||
--error ER_WINDOW_FRAME_IN_REFERENCED_WINDOW_SPEC
|
--error ER_WINDOW_FRAME_IN_REFERENCED_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w3 as CNT
|
count(*) over w3 as CNT
|
||||||
from t1
|
from t1
|
||||||
window
|
window
|
||||||
w1 as (partition by c),
|
w1 as (partition by c),
|
||||||
w2 as (w1 order by pk rows between 3 preceding and 2 following),
|
w2 as (w1 order by pk rows between 3 preceding and 2 following),
|
||||||
w3 as (w2 rows between 2 preceding and 2 following);
|
w3 as (w2 rows between 2 preceding and 2 following);
|
||||||
|
|
||||||
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between unbounded following and 2 following);
|
rows between unbounded following and 2 following);
|
||||||
|
|
||||||
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 rows between 2 preceding and unbounded preceding) as CNT
|
count(*) over (w1 rows between 2 preceding and unbounded preceding) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
|
|
||||||
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w1 order by pk rows between current row and 2 preceding) as CNT
|
count(*) over (w1 order by pk rows between current row and 2 preceding) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
|
|
||||||
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
--error ER_BAD_COMBINATION_OF_WINDOW_FRAME_BOUND_SPECS
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over (w2 rows between 2 following and current row) as CNT
|
count(*) over (w2 rows between 2 following and current row) as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c), w2 as (w1 order by pk);
|
window w1 as (partition by c), w2 as (w1 order by pk);
|
||||||
|
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select
|
select
|
||||||
pk, c
|
pk, c
|
||||||
from t1 where rank() over w1 > 2
|
from t1 where rank() over w1 > 2
|
||||||
window w1 as (partition by c order by pk);
|
window w1 as (partition by c order by pk);
|
||||||
|
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select
|
select
|
||||||
c, max(pk) as m
|
c, max(pk) as m
|
||||||
from t1
|
from t1
|
||||||
group by c + rank() over w1
|
group by c + rank() over w1
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
|
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r
|
c, max(pk) as m, rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
group by c+r
|
group by c+r
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
|
|
||||||
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
--error ER_WRONG_PLACEMENT_OF_WINDOW_FUNCTION
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r
|
c, max(pk) as m, rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
group by c having c+r > 3
|
group by c having c+r > 3
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
|
|
||||||
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r,
|
c, max(pk) as m, rank() over w1 as r,
|
||||||
rank() over (partition by r+1 order by m)
|
rank() over (partition by r+1 order by m)
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
|
|
||||||
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r,
|
c, max(pk) as m, rank() over w1 as r,
|
||||||
rank() over (partition by m order by r)
|
rank() over (partition by m order by r)
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m);
|
window w1 as (order by m);
|
||||||
|
|
||||||
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
--error ER_WINDOW_FUNCTION_IN_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr
|
c, max(pk) as m, rank() over w1 as r, dense_rank() over w2 as dr
|
||||||
from t1
|
from t1
|
||||||
group by c
|
group by c
|
||||||
window w1 as (order by m), w2 as (partition by r order by m);
|
window w1 as (order by m), w2 as (partition by r order by m);
|
||||||
|
|
||||||
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
row_number() over (partition by c order by pk
|
row_number() over (partition by c order by pk
|
||||||
range between unbounded preceding and current row) as r
|
range between unbounded preceding and current row) as r
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
rank() over w1 as r
|
rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following);
|
rows between 2 preceding and 2 following);
|
||||||
|
|
||||||
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
--error ER_NOT_ALLOWED_WINDOW_FRAME
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
dense_rank() over (partition by c order by pk
|
dense_rank() over (partition by c order by pk
|
||||||
rows between 1 preceding and 1 following) as r
|
rows between 1 preceding and 1 following) as r
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
|
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
rank() over w1 as r
|
rank() over w1 as r
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c);
|
window w1 as (partition by c);
|
||||||
|
|
||||||
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
|
--error ER_NO_ORDER_LIST_IN_WINDOW_SPEC
|
||||||
select
|
select
|
||||||
pk, c,
|
pk, c,
|
||||||
dense_rank() over (partition by c) as r
|
dense_rank() over (partition by c) as r
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -358,11 +360,11 @@ drop table t0,t1;
|
|||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
create table t2 (part_id int, pk int, a int);
|
create table t2 (part_id int, pk int, a int);
|
||||||
insert into t2 select
|
insert into t2 select
|
||||||
if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
|
if(a<5, 0, 1), a, if(a<5, NULL, 1) from t0;
|
||||||
select * from t2;
|
select * from t2;
|
||||||
|
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
count(a) over (partition by part_id order by pk
|
count(a) over (partition by part_id order by pk
|
||||||
rows between 1 preceding and 1 following) as CNT
|
rows between 1 preceding and 1 following) as CNT
|
||||||
@ -371,11 +373,11 @@ from t2;
|
|||||||
drop table t0, t2;
|
drop table t0, t2;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # RANGE-type bounds
|
--echo # RANGE-type bounds
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t3 (
|
create table t3 (
|
||||||
pk int,
|
pk int,
|
||||||
val int
|
val int
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -388,25 +390,25 @@ insert into t3 values
|
|||||||
(5, 2),
|
(5, 2),
|
||||||
(6, 2);
|
(6, 2);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (order by val
|
count(val) over (order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t3;
|
from t3;
|
||||||
|
|
||||||
insert into t3 values
|
insert into t3 values
|
||||||
(7, 3),
|
(7, 3),
|
||||||
(8, 3);
|
(8, 3);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (order by val
|
count(val) over (order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t3;
|
from t3;
|
||||||
|
|
||||||
@ -440,50 +442,50 @@ insert into t4 values
|
|||||||
(5678, 207, 3),
|
(5678, 207, 3),
|
||||||
(5678, 208, 3);
|
(5678, 208, 3);
|
||||||
|
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # Try RANGE UNBOUNDED PRECEDING | FOLLOWING
|
--echo # Try RANGE UNBOUNDED PRECEDING | FOLLOWING
|
||||||
--echo #
|
--echo #
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between unbounded preceding and
|
range between unbounded preceding and
|
||||||
current row)
|
current row)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
|
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between current row and
|
range between current row and
|
||||||
unbounded following)
|
unbounded following)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
|
|
||||||
select
|
select
|
||||||
part_id,
|
part_id,
|
||||||
pk,
|
pk,
|
||||||
val,
|
val,
|
||||||
count(val) over (partition by part_id
|
count(val) over (partition by part_id
|
||||||
order by val
|
order by val
|
||||||
range between unbounded preceding and
|
range between unbounded preceding and
|
||||||
unbounded following)
|
unbounded following)
|
||||||
as CNT
|
as CNT
|
||||||
from t4;
|
from t4;
|
||||||
|
|
||||||
@ -502,12 +504,12 @@ insert into t1 values
|
|||||||
( 6 , 2, 64),
|
( 6 , 2, 64),
|
||||||
( 7 , 2, 128),
|
( 7 , 2, 128),
|
||||||
( 8 , 2, 16);
|
( 8 , 2, 16);
|
||||||
|
|
||||||
select pk, a, b,
|
select pk, a, b,
|
||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
--echo # Extra ROWS n PRECEDING tests
|
--echo # Extra ROWS n PRECEDING tests
|
||||||
select pk, a, b,
|
select pk, a, b,
|
||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) as bit_or
|
||||||
from t1;
|
from t1;
|
||||||
@ -515,8 +517,8 @@ drop table t1;
|
|||||||
|
|
||||||
|
|
||||||
create table t2 (
|
create table t2 (
|
||||||
pk int,
|
pk int,
|
||||||
a int,
|
a int,
|
||||||
b int
|
b int
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -546,7 +548,7 @@ select pk, a, b,
|
|||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) as bit_or
|
||||||
from t2;
|
from t2;
|
||||||
|
|
||||||
--echo # Check CURRENT ROW
|
--echo # Check CURRENT ROW
|
||||||
|
|
||||||
select pk, a, b,
|
select pk, a, b,
|
||||||
bit_or(b) over (partition by a order by pk ROWS BETWEEN CURRENT ROW AND CURRENT ROW) as bit_or
|
bit_or(b) over (partition by a order by pk ROWS BETWEEN CURRENT ROW AND CURRENT ROW) as bit_or
|
||||||
@ -563,12 +565,12 @@ create table t1 (
|
|||||||
a int
|
a int
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(10, 1, 1),
|
(10, 1, 1),
|
||||||
(10, 2, 2),
|
(10, 2, 2),
|
||||||
(10, 3, 4),
|
(10, 3, 4),
|
||||||
(10, 4, 8),
|
(10, 4, 8),
|
||||||
(10, 5,26),
|
(10, 5,26),
|
||||||
(10, 6,27),
|
(10, 6,27),
|
||||||
(10, 7,40),
|
(10, 7,40),
|
||||||
(10, 8,71),
|
(10, 8,71),
|
||||||
@ -577,64 +579,64 @@ insert into t1 values
|
|||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 FOLLOWING) as cnt
|
AND 1 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 PRECEDING) as cnt
|
AND 10 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 PRECEDING) as cnt
|
AND 10 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 PRECEDING) as cnt
|
AND 1 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
# Try bottom bound
|
# Try bottom bound
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN 1 PRECEDING
|
RANGE BETWEEN 1 PRECEDING
|
||||||
AND CURRENT ROW) as cnt
|
AND CURRENT ROW) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a DESC
|
count(a) over (ORDER BY a DESC
|
||||||
RANGE BETWEEN 1 PRECEDING
|
RANGE BETWEEN 1 PRECEDING
|
||||||
AND CURRENT ROW) as cnt
|
AND CURRENT ROW) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (ORDER BY a
|
count(a) over (ORDER BY a
|
||||||
RANGE BETWEEN 1 FOLLOWING
|
RANGE BETWEEN 1 FOLLOWING
|
||||||
AND 3 FOLLOWING) as cnt
|
AND 3 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -660,7 +662,7 @@ select
|
|||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
count(a) over (PARTITION BY part_id
|
count(a) over (PARTITION BY part_id
|
||||||
ORDER BY a
|
ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 10 FOLLOWING) as cnt
|
AND 10 FOLLOWING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -668,7 +670,7 @@ select
|
|||||||
pk, a,
|
pk, a,
|
||||||
count(a) over (PARTITION BY part_id
|
count(a) over (PARTITION BY part_id
|
||||||
ORDER BY a
|
ORDER BY a
|
||||||
RANGE BETWEEN UNBOUNDED PRECEDING
|
RANGE BETWEEN UNBOUNDED PRECEDING
|
||||||
AND 1 PRECEDING) as cnt
|
AND 1 PRECEDING) as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -689,17 +691,17 @@ insert into t1 values (1, 1.22);
|
|||||||
insert into t1 values (1, 3.33);
|
insert into t1 values (1, 3.33);
|
||||||
|
|
||||||
select
|
select
|
||||||
a,
|
a,
|
||||||
count(col1) over (order by a
|
count(col1) over (order by a
|
||||||
range between 0.1 preceding
|
range between 0.1 preceding
|
||||||
and 0.1 following)
|
and 0.1 following)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # RANGE-type frames and NULL values
|
--echo # RANGE-type frames and NULL values
|
||||||
--echo #
|
--echo #
|
||||||
create table t1 (
|
create table t1 (
|
||||||
pk int,
|
pk int,
|
||||||
a int,
|
a int,
|
||||||
@ -716,17 +718,17 @@ insert into t1 values (7, 13 ,1);
|
|||||||
insert into t1 values (8, 14 ,1);
|
insert into t1 values (8, 14 ,1);
|
||||||
|
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
count(b) over (order by a
|
count(b) over (order by a
|
||||||
range between 2 preceding
|
range between 2 preceding
|
||||||
and 2 following) as CNT
|
and 2 following) as CNT
|
||||||
from t1;
|
from t1;
|
||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # Try ranges that have bound1 > bound2. The standard actually allows them
|
--echo # Try ranges that have bound1 > bound2. The standard actually allows them
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
@ -741,7 +743,7 @@ select
|
|||||||
count(*) over (partition by c
|
count(*) over (partition by c
|
||||||
order by pk
|
order by pk
|
||||||
rows between 1 preceding
|
rows between 1 preceding
|
||||||
and 2 preceding)
|
and 2 preceding)
|
||||||
as cnt
|
as cnt
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -769,7 +771,7 @@ select
|
|||||||
count(*) over (partition by c
|
count(*) over (partition by c
|
||||||
order by pk
|
order by pk
|
||||||
range between 1 preceding
|
range between 1 preceding
|
||||||
and 2 preceding)
|
and 2 preceding)
|
||||||
as cnt
|
as cnt
|
||||||
from t1;
|
from t1;
|
||||||
drop table t0, t1;
|
drop table t0, t1;
|
||||||
@ -782,7 +784,7 @@ create table t1 (a int, b int, c varchar(32));
|
|||||||
insert into t1 values (1,1,'foo');
|
insert into t1 values (1,1,'foo');
|
||||||
insert into t1 values (2,2,'bar');
|
insert into t1 values (2,2,'bar');
|
||||||
--error ER_RANGE_FRAME_NEEDS_SIMPLE_ORDERBY
|
--error ER_RANGE_FRAME_NEEDS_SIMPLE_ORDERBY
|
||||||
select
|
select
|
||||||
count(*) over (order by a,b
|
count(*) over (order by a,b
|
||||||
range between unbounded preceding and current row)
|
range between unbounded preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
@ -819,7 +821,7 @@ select
|
|||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # EXCLUDE clause is parsed but not supported
|
--echo # EXCLUDE clause is parsed but not supported
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
--error ER_FRAME_EXCLUSION_NOT_SUPPORTED
|
--error ER_FRAME_EXCLUSION_NOT_SUPPORTED
|
||||||
@ -852,15 +854,15 @@ from t1;
|
|||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # Window function in grouping query
|
--echo # Window function in grouping query
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t1 (
|
create table t1 (
|
||||||
username varchar(32),
|
username varchar(32),
|
||||||
amount int
|
amount int
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
('user1',1),
|
('user1',1),
|
||||||
('user1',5),
|
('user1',5),
|
||||||
@ -868,19 +870,19 @@ insert into t1 values
|
|||||||
('user2',10),
|
('user2',10),
|
||||||
('user2',20),
|
('user2',20),
|
||||||
('user2',30);
|
('user2',30);
|
||||||
|
|
||||||
select
|
select
|
||||||
username,
|
username,
|
||||||
sum(amount) as s,
|
sum(amount) as s,
|
||||||
rank() over (order by s desc)
|
rank() over (order by s desc)
|
||||||
from t1
|
from t1
|
||||||
group by username;
|
group by username;
|
||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # mdev-9719: Window function in prepared statement
|
--echo # mdev-9719: Window function in prepared statement
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t1(a int, b int, x char(32));
|
create table t1(a int, b int, x char(32));
|
||||||
insert into t1 values (2, 10, 'xx');
|
insert into t1 values (2, 10, 'xx');
|
||||||
@ -888,31 +890,31 @@ insert into t1 values (2, 10, 'zz');
|
|||||||
insert into t1 values (2, 20, 'yy');
|
insert into t1 values (2, 20, 'yy');
|
||||||
insert into t1 values (3, 10, 'xxx');
|
insert into t1 values (3, 10, 'xxx');
|
||||||
insert into t1 values (3, 20, 'vvv');
|
insert into t1 values (3, 20, 'vvv');
|
||||||
|
|
||||||
prepare stmt from 'select a, row_number() over (partition by a order by b) from t1';
|
prepare stmt from 'select a, row_number() over (partition by a order by b) from t1';
|
||||||
--sorted_result
|
--sorted_result
|
||||||
execute stmt;
|
execute stmt;
|
||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # mdev-9754: Window name resolution in prepared statement
|
--echo # mdev-9754: Window name resolution in prepared statement
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t0 (a int);
|
create table t0 (a int);
|
||||||
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
|
|
||||||
create table t1 (pk int, c int);
|
create table t1 (pk int, c int);
|
||||||
insert into t1 select a+1,1 from t0;
|
insert into t1 select a+1,1 from t0;
|
||||||
update t1 set c=2 where pk not in (1,2,3,4);
|
update t1 set c=2 where pk not in (1,2,3,4);
|
||||||
select * from t1;
|
select * from t1;
|
||||||
|
|
||||||
prepare stmt from
|
prepare stmt from
|
||||||
'select
|
'select
|
||||||
pk, c,
|
pk, c,
|
||||||
count(*) over w1 as CNT
|
count(*) over w1 as CNT
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by c order by pk
|
window w1 as (partition by c order by pk
|
||||||
rows between 2 preceding and 2 following)';
|
rows between 2 preceding and 2 following)';
|
||||||
execute stmt;
|
execute stmt;
|
||||||
|
|
||||||
@ -999,10 +1001,10 @@ insert into t1 values
|
|||||||
select sum(b) over (partition by a order by b,pk
|
select sum(b) over (partition by a order by b,pk
|
||||||
rows between unbounded preceding and current row) as c1,
|
rows between unbounded preceding and current row) as c1,
|
||||||
avg(b) over (w1 rows between 1 preceding and 1 following) as c2,
|
avg(b) over (w1 rows between 1 preceding and 1 following) as c2,
|
||||||
sum(c) over (w2 rows between 1 preceding and 1 following) as c5,
|
sum(c) over (w2 rows between 1 preceding and 1 following) as c5,
|
||||||
avg(b) over (w1 rows between 5 preceding and 5 following) as c3,
|
avg(b) over (w1 rows between 5 preceding and 5 following) as c3,
|
||||||
sum(b) over (w1 rows between 1 preceding and 1 following) as c4
|
sum(b) over (w1 rows between 1 preceding and 1 following) as c4
|
||||||
from t1
|
from t1
|
||||||
window w1 as (partition by a order by b,pk),
|
window w1 as (partition by a order by b,pk),
|
||||||
w2 as (partition by b order by c,pk);
|
w2 as (partition by b order by c,pk);
|
||||||
|
|
||||||
@ -1014,47 +1016,47 @@ drop table t1;
|
|||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t1 (a int, b int, c int);
|
create table t1 (a int, b int, c int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1,3,1),
|
(1,3,1),
|
||||||
(2,2,1),
|
(2,2,1),
|
||||||
(3,1,1);
|
(3,1,1);
|
||||||
|
|
||||||
--echo # Check using counters
|
--echo # Check using counters
|
||||||
flush status;
|
flush status;
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by b)
|
rank() over (partition by c order by b)
|
||||||
from t1;
|
from t1;
|
||||||
show status like '%sort%';
|
show status like '%sort%';
|
||||||
|
|
||||||
flush status;
|
flush status;
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
show status like '%sort%';
|
show status like '%sort%';
|
||||||
|
|
||||||
# Check using EXPLAIN FORMAT=JSON
|
# Check using EXPLAIN FORMAT=JSON
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (order by a),
|
rank() over (order by a),
|
||||||
row_number() over (order by a)
|
row_number() over (order by a)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
rank() over (partition by c order by a),
|
rank() over (partition by c order by a),
|
||||||
count(*) over (partition by c)
|
count(*) over (partition by c)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
explain format=json
|
explain format=json
|
||||||
select
|
select
|
||||||
count(*) over (partition by c),
|
count(*) over (partition by c),
|
||||||
rank() over (partition by c order by a)
|
rank() over (partition by c order by a)
|
||||||
from t1;
|
from t1;
|
||||||
@ -1094,7 +1096,7 @@ drop table t1;
|
|||||||
--echo # Try window functions that are not directly present in the select list
|
--echo # Try window functions that are not directly present in the select list
|
||||||
--echo #
|
--echo #
|
||||||
create table t1 (a int, b int);
|
create table t1 (a int, b int);
|
||||||
insert into t1 values
|
insert into t1 values
|
||||||
(1,3),
|
(1,3),
|
||||||
(2,2),
|
(2,2),
|
||||||
(3,1);
|
(3,1);
|
||||||
@ -1128,13 +1130,13 @@ insert into t1 values (1, 2, 2);
|
|||||||
insert into t1 values (1, 3, 4);
|
insert into t1 values (1, 3, 4);
|
||||||
insert into t1 values (1, 4, 8);
|
insert into t1 values (1, 4, 8);
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
sum(a) over (order by pk rows between 0 preceding and current row)
|
sum(a) over (order by pk rows between 0 preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
pk, a,
|
pk, a,
|
||||||
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
sum(a) over (order by pk rows between 1 preceding and 0 preceding)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
@ -1142,20 +1144,20 @@ insert into t1 values (200, 1, 1);
|
|||||||
insert into t1 values (200, 2, 2);
|
insert into t1 values (200, 2, 2);
|
||||||
insert into t1 values (200, 3, 4);
|
insert into t1 values (200, 3, 4);
|
||||||
insert into t1 values (200, 4, 8);
|
insert into t1 values (200, 4, 8);
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
|
sum(a) over (partition by part_id order by pk rows between 0 preceding and current row)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
select
|
select
|
||||||
part_id, pk, a,
|
part_id, pk, a,
|
||||||
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
sum(a) over (partition by part_id order by pk rows between 1 preceding and 0 preceding)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
--echo #
|
--echo #
|
||||||
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
|
--echo # MDEV-9780, The "DISTINCT must not bet converted into GROUP BY when
|
||||||
--echo # window functions are present" part
|
--echo # window functions are present" part
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t1 (part_id int, a int);
|
create table t1 (part_id int, a int);
|
||||||
@ -1178,8 +1180,8 @@ select distinct rank() over (partition by part_id order by a) from t1;
|
|||||||
drop table t1;
|
drop table t1;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # MDEV-9893: Window functions with different ORDER BY lists,
|
--echo # MDEV-9893: Window functions with different ORDER BY lists,
|
||||||
--echo # one of these lists containing an expression
|
--echo # one of these lists containing an expression
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
create table t1 (s1 int, s2 char(5));
|
create table t1 (s1 int, s2 char(5));
|
||||||
@ -1189,11 +1191,11 @@ insert into t1 values (3,null);
|
|||||||
insert into t1 values (4,'a');
|
insert into t1 values (4,'a');
|
||||||
insert into t1 values (2,'b');
|
insert into t1 values (2,'b');
|
||||||
insert into t1 values (-1,'');
|
insert into t1 values (-1,'');
|
||||||
|
|
||||||
select
|
select
|
||||||
*,
|
*,
|
||||||
ROW_NUMBER() OVER (order by s1),
|
ROW_NUMBER() OVER (order by s1),
|
||||||
CUME_DIST() OVER (order by -s1)
|
CUME_DIST() OVER (order by -s1)
|
||||||
from t1;
|
from t1;
|
||||||
|
|
||||||
drop table t1;
|
drop table t1;
|
||||||
@ -1221,7 +1223,7 @@ CREATE TABLE `orders` (
|
|||||||
`o_orderkey` int(11) NOT NULL,
|
`o_orderkey` int(11) NOT NULL,
|
||||||
`o_custkey` int(11) DEFAULT NULL,
|
`o_custkey` int(11) DEFAULT NULL,
|
||||||
PRIMARY KEY (`o_orderkey`)
|
PRIMARY KEY (`o_orderkey`)
|
||||||
) DEFAULT CHARSET=latin1;
|
) DEFAULT CHARSET=latin1;
|
||||||
|
|
||||||
INSERT INTO `orders` VALUES (59908,242);
|
INSERT INTO `orders` VALUES (59908,242);
|
||||||
INSERT INTO `orders` VALUES (59940,238);
|
INSERT INTO `orders` VALUES (59940,238);
|
||||||
@ -1233,7 +1235,7 @@ SELECT o_custkey, avg(o_custkey) OVER (PARTITION BY abs(o_custkey)
|
|||||||
DROP table orders;
|
DROP table orders;
|
||||||
|
|
||||||
--echo #
|
--echo #
|
||||||
--echo # MDEV-10842: window functions with the same order column
|
--echo # MDEV-10842: window functions with the same order column
|
||||||
--echo # but different directions
|
--echo # but different directions
|
||||||
--echo #
|
--echo #
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user