mirror of
https://github.com/MariaDB/server.git
synced 2025-12-24 11:21:21 +03:00
113 lines
2.5 KiB
Plaintext
113 lines
2.5 KiB
Plaintext
#
|
|
# Window Functions Tests
|
|
#
|
|
|
|
--disable_warnings
|
|
drop table if exists t1,t2;
|
|
--enable_warnings
|
|
|
|
--echo # ########################################################################
|
|
--echo # # Parser tests
|
|
--echo # ########################################################################
|
|
--echo #
|
|
--echo # Check what happens when one attempts to use window function without OVER clause
|
|
create table t1 (a int, b int);
|
|
insert into t1 values (1,1),(2,2);
|
|
|
|
--error ER_PARSE_ERROR
|
|
select row_number() from t1;
|
|
--error ER_PARSE_ERROR
|
|
select rank() from t1;
|
|
|
|
--echo # Attempt to use window function in the WHERE clause
|
|
--error ER_INVALID_GROUP_FUNC_USE
|
|
select * from t1 where 1=rank() over (order by a);
|
|
--error ER_INVALID_GROUP_FUNC_USE
|
|
select * from t1 where 1>row_number() over (partition by b order by a);
|
|
drop table t1;
|
|
|
|
--echo # ########################################################################
|
|
--echo # # Functionality tests
|
|
--echo # ########################################################################
|
|
--echo #
|
|
--echo # Check if ROW_NUMBER() works in basic cases
|
|
create table t1(a int, b int, x char(32));
|
|
insert into t1 values (2, 10, 'xx');
|
|
insert into t1 values (2, 10, 'zz');
|
|
insert into t1 values (2, 20, 'yy');
|
|
insert into t1 values (3, 10, 'xxx');
|
|
insert into t1 values (3, 20, 'vvv');
|
|
|
|
--sorted_result
|
|
select a, row_number() over (partition by a order by b) from t1;
|
|
|
|
select a, b, x, row_number() over (partition by a order by x) from t1;
|
|
|
|
drop table t1;
|
|
|
|
create table t1 (pk int primary key, a int, b int);
|
|
insert into t1 values
|
|
(1, 10, 22),
|
|
(2, 11, 21),
|
|
(3, 12, 20),
|
|
(4, 13, 19),
|
|
(5, 14, 18);
|
|
|
|
select
|
|
pk, a, b,
|
|
row_number() over (order by a),
|
|
row_number() over (order by b)
|
|
from t1;
|
|
|
|
drop table t1;
|
|
|
|
--echo #
|
|
--echo # Try RANK() function
|
|
--echo #
|
|
create table t2 (
|
|
pk int primary key,
|
|
a int
|
|
);
|
|
|
|
insert into t2 values
|
|
( 1 , 0),
|
|
( 2 , 0),
|
|
( 3 , 1),
|
|
( 4 , 1),
|
|
( 8 , 2),
|
|
( 5 , 2),
|
|
( 6 , 2),
|
|
( 7 , 2),
|
|
( 9 , 4),
|
|
(10 , 4);
|
|
|
|
select pk, a, rank() over (order by a) from 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 t3;
|